blob: f9350673f3e1c7874bb98a7c79b44a809709f06d [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**
drh2fa18682008-03-19 14:15:34 +000021** @(#) $Id: pager.c,v 1.418 2008/03/19 14:15:35 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 */
353 u8 changeCountDone; /* Set after incrementing the change-counter */
drh33f4e022007-09-03 15:19:34 +0000354 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
drhe49f9822006-09-15 12:29:16 +0000355 int errCode; /* One of several kinds of errors */
drhfcd35c72005-05-21 02:48:08 +0000356 int dbSize; /* Number of pages in the file */
357 int origDbSize; /* dbSize before the current change */
358 int stmtSize; /* Size of database (in pages) at stmt_begin() */
359 int nRec; /* Number of pages written to the journal */
360 u32 cksumInit; /* Quasi-random value added to every checksum */
361 int stmtNRec; /* Number of records in stmt subjournal */
362 int nExtra; /* Add this many bytes to each in-memory page */
363 int pageSize; /* Number of bytes in a page */
364 int nPage; /* Total number of in-memory pages */
drhfcd35c72005-05-21 02:48:08 +0000365 int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */
366 int mxPage; /* Maximum number of pages to hold in cache */
drhf8e632b2007-05-08 14:51:36 +0000367 Pgno mxPgno; /* Maximum allowed size of the database */
drhf5e7bb52008-02-18 14:47:33 +0000368 Bitvec *pInJournal; /* One bit for each page in the database file */
369 Bitvec *pInStmt; /* One bit for each page in the database */
drhfcd35c72005-05-21 02:48:08 +0000370 char *zFilename; /* Name of the database file */
371 char *zJournal; /* Name of the journal file */
372 char *zDirectory; /* Directory hold database and journal files */
drh334b2992007-09-06 23:28:23 +0000373 char *zStmtJrnl; /* Name of the statement journal file */
danielk197762079062007-08-15 17:08:46 +0000374 sqlite3_file *fd, *jfd; /* File descriptors for database and journal */
375 sqlite3_file *stfd; /* File descriptor for the statement subjournal*/
drh726de592004-06-10 23:35:50 +0000376 BusyHandler *pBusyHandler; /* Pointer to sqlite.busyHandler */
danielk19779f61c2f2007-08-27 17:27:49 +0000377 PagerLruList lru; /* LRU list of free pages */
drhd9b02572001-04-15 00:37:09 +0000378 PgHdr *pAll; /* List of all pages */
drhac69b052004-05-12 13:30:07 +0000379 PgHdr *pStmt; /* List of pages in the statement subjournal */
drh605ad8f2006-05-03 23:34:05 +0000380 PgHdr *pDirty; /* List of all dirty pages */
drheb206252004-10-01 02:00:31 +0000381 i64 journalOff; /* Current byte offset in the journal file */
382 i64 journalHdr; /* Byte offset to previous journal header */
383 i64 stmtHdrOff; /* First journal header written this statement */
384 i64 stmtCksum; /* cksumInit when statement was started */
drh6d156e42005-05-20 20:11:20 +0000385 i64 stmtJSize; /* Size of journal at stmt_begin() */
danielk197776572402004-06-25 02:38:54 +0000386 int sectorSize; /* Assumed sector size during rollback */
drhfcd35c72005-05-21 02:48:08 +0000387#ifdef SQLITE_TEST
drh7c4ac0c2007-04-05 11:25:58 +0000388 int nHit, nMiss; /* Cache hits and missing */
389 int nRead, nWrite; /* Database pages read/written */
drhfcd35c72005-05-21 02:48:08 +0000390#endif
danielk19773b8a05f2007-03-19 17:44:26 +0000391 void (*xDestructor)(DbPage*,int); /* Call this routine when freeing pages */
392 void (*xReiniter)(DbPage*,int); /* Call this routine when reloading pages */
drh7c4ac0c2007-04-05 11:25:58 +0000393#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000394 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
drh6d156e42005-05-20 20:11:20 +0000395 void *pCodecArg; /* First argument to xCodec() */
drh7c4ac0c2007-04-05 11:25:58 +0000396#endif
drh8ca0c722006-05-07 17:49:38 +0000397 int nHash; /* Size of the pager hash table */
398 PgHdr **aHash; /* Hash table to map page number to PgHdr */
drh6f7adc82006-01-11 21:41:20 +0000399#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +0000400 Pager *pNext; /* Doubly linked list of pagers on which */
401 Pager *pPrev; /* sqlite3_release_memory() will work */
402 int iInUseMM; /* Non-zero if unavailable to MM */
403 int iInUseDB; /* Non-zero if in sqlite3_release_memory() */
danielk197713f72992005-12-18 08:51:22 +0000404#endif
danielk19778186df82007-03-06 13:45:59 +0000405 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
drh86a88112007-04-16 15:02:19 +0000406 char dbFileVers[16]; /* Changes whenever database file changes */
drhd9b02572001-04-15 00:37:09 +0000407};
408
409/*
drh538f5702007-04-13 02:14:30 +0000410** The following global variables hold counters used for
411** testing purposes only. These variables do not exist in
412** a non-testing build. These variables are not thread-safe.
drhfcd35c72005-05-21 02:48:08 +0000413*/
414#ifdef SQLITE_TEST
drh538f5702007-04-13 02:14:30 +0000415int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
416int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
417int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
418int sqlite3_pager_pgfree_count = 0; /* Number of cache pages freed */
419# define PAGER_INCR(v) v++
drhfcd35c72005-05-21 02:48:08 +0000420#else
drh538f5702007-04-13 02:14:30 +0000421# define PAGER_INCR(v)
drhfcd35c72005-05-21 02:48:08 +0000422#endif
423
drh86f8c192007-08-22 00:39:19 +0000424/*
425** The following variable points to the head of a double-linked list
426** of all pagers that are eligible for page stealing by the
427** sqlite3_release_memory() interface. Access to this list is
428** protected by the SQLITE_MUTEX_STATIC_MEM2 mutex.
429*/
430#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
431static Pager *sqlite3PagerList = 0;
danielk19779f61c2f2007-08-27 17:27:49 +0000432static PagerLruList sqlite3LruPageList = {0, 0, 0};
drh86f8c192007-08-22 00:39:19 +0000433#endif
drh538f5702007-04-13 02:14:30 +0000434
435
drhfcd35c72005-05-21 02:48:08 +0000436/*
drh5e00f6c2001-09-13 13:46:56 +0000437** Journal files begin with the following magic string. The data
438** was obtained from /dev/random. It is used only as a sanity check.
drh94f33312002-08-12 12:29:56 +0000439**
drhae2b40c2004-06-09 19:03:54 +0000440** Since version 2.8.0, the journal format contains additional sanity
441** checking information. If the power fails while the journal is begin
442** written, semi-random garbage data might appear in the journal
443** file after power is restored. If an attempt is then made
drh968af522003-02-11 14:55:40 +0000444** to roll the journal back, the database could be corrupted. The additional
445** sanity checking data is an attempt to discover the garbage in the
446** journal and ignore it.
447**
drhae2b40c2004-06-09 19:03:54 +0000448** The sanity checking information for the new journal format consists
drh968af522003-02-11 14:55:40 +0000449** of a 32-bit checksum on each page of data. The checksum covers both
drh90f5ecb2004-07-22 01:19:35 +0000450** the page number and the pPager->pageSize bytes of data for the page.
drh968af522003-02-11 14:55:40 +0000451** This cksum is initialized to a 32-bit random value that appears in the
452** journal file right after the header. The random initializer is important,
453** because garbage data that appears at the end of a journal is likely
454** data that was once in other files that have now been deleted. If the
455** garbage data came from an obsolete journal file, the checksums might
456** be correct. But by initializing the checksum to random value which
457** is different for every journal, we minimize that risk.
drhd9b02572001-04-15 00:37:09 +0000458*/
drhae2b40c2004-06-09 19:03:54 +0000459static const unsigned char aJournalMagic[] = {
460 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
drhed7c8552001-04-11 14:29:21 +0000461};
462
463/*
drh726de592004-06-10 23:35:50 +0000464** The size of the header and of each page in the journal is determined
465** by the following macros.
drh968af522003-02-11 14:55:40 +0000466*/
drhae2b40c2004-06-09 19:03:54 +0000467#define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
drh968af522003-02-11 14:55:40 +0000468
danielk197776572402004-06-25 02:38:54 +0000469/*
470** The journal header size for this pager. In the future, this could be
471** set to some value read from the disk controller. The important
472** characteristic is that it is the same size as a disk sector.
473*/
474#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
475
drhb7f91642004-10-31 02:22:47 +0000476/*
477** The macro MEMDB is true if we are dealing with an in-memory database.
478** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
479** the value of MEMDB will be a constant and the compiler will optimize
480** out code that would never execute.
481*/
482#ifdef SQLITE_OMIT_MEMORYDB
483# define MEMDB 0
484#else
485# define MEMDB pPager->memDb
486#endif
487
488/*
danielk197776572402004-06-25 02:38:54 +0000489** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
490** reserved for working around a windows/posix incompatibility). It is
491** used in the journal to signify that the remainder of the journal file
492** is devoted to storing a master journal name - there are no more pages to
493** roll back. See comments for function writeMasterJournal() for details.
494*/
danielk1977599fcba2004-11-08 07:13:13 +0000495/* #define PAGER_MJ_PGNO(x) (PENDING_BYTE/((x)->pageSize)) */
496#define PAGER_MJ_PGNO(x) ((PENDING_BYTE/((x)->pageSize))+1)
danielk197713adf8a2004-06-03 16:08:41 +0000497
drh968af522003-02-11 14:55:40 +0000498/*
danielk197726836652005-01-17 01:33:13 +0000499** The maximum legal page number is (2^31 - 1).
500*/
501#define PAGER_MAX_PGNO 2147483647
502
503/*
drh86f8c192007-08-22 00:39:19 +0000504** The pagerEnter() and pagerLeave() routines acquire and release
505** a mutex on each pager. The mutex is recursive.
506**
507** This is a special-purpose mutex. It only provides mutual exclusion
508** between the Btree and the Memory Management sqlite3_release_memory()
509** function. It does not prevent, for example, two Btrees from accessing
510** the same pager at the same time. Other general-purpose mutexes in
511** the btree layer handle that chore.
512*/
513#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
514 static void pagerEnter(Pager *p){
515 p->iInUseDB++;
516 if( p->iInUseMM && p->iInUseDB==1 ){
517 sqlite3_mutex *mutex;
518 mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
519 p->iInUseDB = 0;
520 sqlite3_mutex_enter(mutex);
521 p->iInUseDB = 1;
522 sqlite3_mutex_leave(mutex);
523 }
524 assert( p->iInUseMM==0 );
525 }
526 static void pagerLeave(Pager *p){
527 p->iInUseDB--;
528 assert( p->iInUseDB>=0 );
529 }
530#else
531# define pagerEnter(X)
532# define pagerLeave(X)
533#endif
534
535/*
danielk197784f786f2007-08-28 08:00:17 +0000536** Add page pPg to the end of the linked list managed by structure
537** pList (pPg becomes the last entry in the list - the most recently
538** used). Argument pLink should point to either pPg->free or pPg->gfree,
539** depending on whether pPg is being added to the pager-specific or
540** global LRU list.
541*/
danielk19779f61c2f2007-08-27 17:27:49 +0000542static void listAdd(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){
543 pLink->pNext = 0;
544 pLink->pPrev = pList->pLast;
545
danielk197784f786f2007-08-28 08:00:17 +0000546#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
547 assert(pLink==&pPg->free || pLink==&pPg->gfree);
548 assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList);
549#endif
550
danielk19779f61c2f2007-08-27 17:27:49 +0000551 if( pList->pLast ){
552 int iOff = (char *)pLink - (char *)pPg;
553 PagerLruLink *pLastLink = (PagerLruLink *)(&((u8 *)pList->pLast)[iOff]);
554 pLastLink->pNext = pPg;
555 }else{
556 assert(!pList->pFirst);
557 pList->pFirst = pPg;
558 }
559
560 pList->pLast = pPg;
561 if( !pList->pFirstSynced && pPg->needSync==0 ){
562 pList->pFirstSynced = pPg;
563 }
564}
565
danielk197784f786f2007-08-28 08:00:17 +0000566/*
567** Remove pPg from the list managed by the structure pointed to by pList.
568**
569** Argument pLink should point to either pPg->free or pPg->gfree, depending
570** on whether pPg is being added to the pager-specific or global LRU list.
571*/
danielk19779f61c2f2007-08-27 17:27:49 +0000572static void listRemove(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){
573 int iOff = (char *)pLink - (char *)pPg;
574
danielk197784f786f2007-08-28 08:00:17 +0000575#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
576 assert(pLink==&pPg->free || pLink==&pPg->gfree);
577 assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList);
578#endif
579
danielk19779f61c2f2007-08-27 17:27:49 +0000580 if( pPg==pList->pFirst ){
581 pList->pFirst = pLink->pNext;
582 }
583 if( pPg==pList->pLast ){
584 pList->pLast = pLink->pPrev;
585 }
586 if( pLink->pPrev ){
587 PagerLruLink *pPrevLink = (PagerLruLink *)(&((u8 *)pLink->pPrev)[iOff]);
588 pPrevLink->pNext = pLink->pNext;
589 }
590 if( pLink->pNext ){
591 PagerLruLink *pNextLink = (PagerLruLink *)(&((u8 *)pLink->pNext)[iOff]);
592 pNextLink->pPrev = pLink->pPrev;
593 }
594 if( pPg==pList->pFirstSynced ){
595 PgHdr *p = pLink->pNext;
596 while( p && p->needSync ){
597 PagerLruLink *pL = (PagerLruLink *)(&((u8 *)p)[iOff]);
598 p = pL->pNext;
599 }
600 pList->pFirstSynced = p;
601 }
602
603 pLink->pNext = pLink->pPrev = 0;
604}
605
606/*
danielk197784f786f2007-08-28 08:00:17 +0000607** Add page pPg to the list of free pages for the pager. If
608** memory-management is enabled, also add the page to the global
609** list of free pages.
danielk19779f61c2f2007-08-27 17:27:49 +0000610*/
611static void lruListAdd(PgHdr *pPg){
612 listAdd(&pPg->pPager->lru, &pPg->free, pPg);
613#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
614 if( !pPg->pPager->memDb ){
615 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
616 listAdd(&sqlite3LruPageList, &pPg->gfree, pPg);
617 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
618 }
619#endif
620}
621
622/*
danielk197784f786f2007-08-28 08:00:17 +0000623** Remove page pPg from the list of free pages for the associated pager.
624** If memory-management is enabled, also remove pPg from the global list
625** of free pages.
danielk19779f61c2f2007-08-27 17:27:49 +0000626*/
627static void lruListRemove(PgHdr *pPg){
628 listRemove(&pPg->pPager->lru, &pPg->free, pPg);
629#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
630 if( !pPg->pPager->memDb ){
631 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
632 listRemove(&sqlite3LruPageList, &pPg->gfree, pPg);
633 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
634 }
635#endif
636}
637
638/*
danielk197784f786f2007-08-28 08:00:17 +0000639** This function is called just after the needSync flag has been cleared
640** from all pages managed by pPager (usually because the journal file
641** has just been synced). It updates the pPager->lru.pFirstSynced variable
642** and, if memory-management is enabled, the sqlite3LruPageList.pFirstSynced
643** variable also.
danielk19779f61c2f2007-08-27 17:27:49 +0000644*/
645static void lruListSetFirstSynced(Pager *pPager){
646 pPager->lru.pFirstSynced = pPager->lru.pFirst;
647#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
648 if( !pPager->memDb ){
649 PgHdr *p;
650 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
651 for(p=sqlite3LruPageList.pFirst; p && p->needSync; p=p->gfree.pNext);
652 assert(p==pPager->lru.pFirstSynced || p==sqlite3LruPageList.pFirstSynced);
653 sqlite3LruPageList.pFirstSynced = p;
654 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
655 }
656#endif
657}
658
danielk1977f35843b2007-04-07 15:03:17 +0000659/*
660** Return true if page *pPg has already been written to the statement
661** journal (or statement snapshot has been created, if *pPg is part
662** of an in-memory database).
663*/
664static int pageInStatement(PgHdr *pPg){
665 Pager *pPager = pPg->pPager;
666 if( MEMDB ){
667 return PGHDR_TO_HIST(pPg, pPager)->inStmt;
668 }else{
drhf5e7bb52008-02-18 14:47:33 +0000669 return sqlite3BitvecTest(pPager->pInStmt, pPg->pgno);
danielk1977f35843b2007-04-07 15:03:17 +0000670 }
671}
drh8ca0c722006-05-07 17:49:38 +0000672
673/*
674** Change the size of the pager hash table to N. N must be a power
675** of two.
676*/
677static void pager_resize_hash_table(Pager *pPager, int N){
678 PgHdr **aHash, *pPg;
679 assert( N>0 && (N&(N-1))==0 );
drheee4c8c2008-02-18 22:24:57 +0000680#ifdef SQLITE_MALLOC_SOFT_LIMIT
681 if( N*sizeof(aHash[0])>SQLITE_MALLOC_SOFT_LIMIT ){
682 N = SQLITE_MALLOC_SOFT_LIMIT/sizeof(aHash[0]);
683 }
684 if( N==pPager->nHash ) return;
685#endif
drhed138fb2007-08-22 22:04:37 +0000686 pagerLeave(pPager);
drh643167f2008-01-22 21:30:53 +0000687 sqlite3FaultBenign(SQLITE_FAULTINJECTOR_MALLOC, pPager->aHash!=0);
drh17435752007-08-16 04:30:38 +0000688 aHash = sqlite3MallocZero( sizeof(aHash[0])*N );
drh643167f2008-01-22 21:30:53 +0000689 sqlite3FaultBenign(SQLITE_FAULTINJECTOR_MALLOC, 0);
drhed138fb2007-08-22 22:04:37 +0000690 pagerEnter(pPager);
drh8ca0c722006-05-07 17:49:38 +0000691 if( aHash==0 ){
692 /* Failure to rehash is not an error. It is only a performance hit. */
693 return;
694 }
drh17435752007-08-16 04:30:38 +0000695 sqlite3_free(pPager->aHash);
drh8ca0c722006-05-07 17:49:38 +0000696 pPager->nHash = N;
697 pPager->aHash = aHash;
698 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
drh3765df42006-06-28 18:18:09 +0000699 int h;
700 if( pPg->pgno==0 ){
701 assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
702 continue;
703 }
704 h = pPg->pgno & (N-1);
drh8ca0c722006-05-07 17:49:38 +0000705 pPg->pNextHash = aHash[h];
706 if( aHash[h] ){
707 aHash[h]->pPrevHash = pPg;
708 }
709 aHash[h] = pPg;
710 pPg->pPrevHash = 0;
711 }
712}
713
drhdd793422001-06-28 01:54:48 +0000714/*
drh34e79ce2004-02-08 06:05:46 +0000715** Read a 32-bit integer from the given file descriptor. Store the integer
716** that is read in *pRes. Return SQLITE_OK if everything worked, or an
717** error code is something goes wrong.
drh726de592004-06-10 23:35:50 +0000718**
719** All values are stored on disk as big-endian.
drh94f33312002-08-12 12:29:56 +0000720*/
danielk197762079062007-08-15 17:08:46 +0000721static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
drh3b59a5c2006-01-15 20:28:28 +0000722 unsigned char ac[4];
danielk197762079062007-08-15 17:08:46 +0000723 int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
drhae2b40c2004-06-09 19:03:54 +0000724 if( rc==SQLITE_OK ){
drha3152892007-05-05 11:48:52 +0000725 *pRes = sqlite3Get4byte(ac);
drh94f33312002-08-12 12:29:56 +0000726 }
drh94f33312002-08-12 12:29:56 +0000727 return rc;
728}
729
730/*
drh97b57482006-01-10 20:32:32 +0000731** Write a 32-bit integer into a string buffer in big-endian byte order.
732*/
drha3152892007-05-05 11:48:52 +0000733#define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
drh97b57482006-01-10 20:32:32 +0000734
735/*
drh34e79ce2004-02-08 06:05:46 +0000736** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
737** on success or an error code is something goes wrong.
drh94f33312002-08-12 12:29:56 +0000738*/
danielk197762079062007-08-15 17:08:46 +0000739static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
danielk1977bab45c62006-01-16 15:14:27 +0000740 char ac[4];
drh97b57482006-01-10 20:32:32 +0000741 put32bits(ac, val);
danielk197762079062007-08-15 17:08:46 +0000742 return sqlite3OsWrite(fd, ac, 4, offset);
drh94f33312002-08-12 12:29:56 +0000743}
744
drh2554f8b2003-01-22 01:26:44 +0000745/*
danielk19777a2b1ee2007-08-21 14:27:01 +0000746** If file pFd is open, call sqlite3OsUnlock() on it.
747*/
748static int osUnlock(sqlite3_file *pFd, int eLock){
749 if( !pFd->pMethods ){
750 return SQLITE_OK;
751 }
752 return sqlite3OsUnlock(pFd, eLock);
753}
754
755/*
danielk1977c7b60172007-08-22 11:22:03 +0000756** This function determines whether or not the atomic-write optimization
757** can be used with this pager. The optimization can be used if:
758**
759** (a) the value returned by OsDeviceCharacteristics() indicates that
760** a database page may be written atomically, and
761** (b) the value returned by OsSectorSize() is less than or equal
762** to the page size.
763**
764** If the optimization cannot be used, 0 is returned. If it can be used,
765** then the value returned is the size of the journal file when it
766** contains rollback data for exactly one page.
767*/
768#ifdef SQLITE_ENABLE_ATOMIC_WRITE
769static int jrnlBufferSize(Pager *pPager){
770 int dc; /* Device characteristics */
771 int nSector; /* Sector size */
772 int nPage; /* Page size */
773 sqlite3_file *fd = pPager->fd;
774
775 if( fd->pMethods ){
776 dc = sqlite3OsDeviceCharacteristics(fd);
777 nSector = sqlite3OsSectorSize(fd);
778 nPage = pPager->pageSize;
779 }
780
781 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
782 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
783
danielk1977f8940ae2007-08-23 11:07:10 +0000784 if( !fd->pMethods || (dc&(SQLITE_IOCAP_ATOMIC|(nPage>>8))&&nSector<=nPage) ){
danielk1977c7b60172007-08-22 11:22:03 +0000785 return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
786 }
787 return 0;
788}
789#endif
790
791/*
danielk1977aef0bf62005-12-30 16:28:01 +0000792** This function should be called when an error occurs within the pager
danielk1977a96a7102006-01-16 12:46:41 +0000793** code. The first argument is a pointer to the pager structure, the
794** second the error-code about to be returned by a pager API function.
795** The value returned is a copy of the second argument to this function.
796**
drh4f0ee682007-03-30 20:43:40 +0000797** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
danielk1977ae72d982007-10-03 08:46:44 +0000798** the error becomes persistent. Until the persisten error is cleared,
799** subsequent API calls on this Pager will immediately return the same
800** error code.
801**
802** A persistent error indicates that the contents of the pager-cache
803** cannot be trusted. This state can be cleared by completely discarding
804** the contents of the pager-cache. If a transaction was active when
805** the persistent error occured, then the rollback journal may need
806** to be replayed.
danielk1977aef0bf62005-12-30 16:28:01 +0000807*/
danielk1977ae72d982007-10-03 08:46:44 +0000808static void pager_unlock(Pager *pPager);
danielk1977aef0bf62005-12-30 16:28:01 +0000809static int pager_error(Pager *pPager, int rc){
drh4ac285a2006-09-15 07:28:50 +0000810 int rc2 = rc & 0xff;
drh34f56212007-08-10 23:56:35 +0000811 assert(
812 pPager->errCode==SQLITE_FULL ||
813 pPager->errCode==SQLITE_OK ||
814 (pPager->errCode & 0xff)==SQLITE_IOERR
815 );
danielk1977979f38e2007-03-27 16:19:51 +0000816 if(
drh4ac285a2006-09-15 07:28:50 +0000817 rc2==SQLITE_FULL ||
818 rc2==SQLITE_IOERR ||
drh4f0ee682007-03-30 20:43:40 +0000819 rc2==SQLITE_CORRUPT
danielk1977efaaf572006-01-16 11:29:19 +0000820 ){
821 pPager->errCode = rc;
danielk1977ae72d982007-10-03 08:46:44 +0000822 if( pPager->state==PAGER_UNLOCK && pPager->nRef==0 ){
823 /* If the pager is already unlocked, call pager_unlock() now to
824 ** clear the error state and ensure that the pager-cache is
825 ** completely empty.
826 */
827 pager_unlock(pPager);
828 }
danielk1977aef0bf62005-12-30 16:28:01 +0000829 }
830 return rc;
831}
832
drh477731b2007-06-16 03:06:27 +0000833/*
834** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
835** on the cache using a hash function. This is used for testing
836** and debugging only.
837*/
danielk19773c407372005-02-15 02:54:14 +0000838#ifdef SQLITE_CHECK_PAGES
839/*
840** Return a 32-bit hash of the page data for pPage.
841*/
drh477731b2007-06-16 03:06:27 +0000842static u32 pager_datahash(int nByte, unsigned char *pData){
danielk19773c407372005-02-15 02:54:14 +0000843 u32 hash = 0;
844 int i;
drh477731b2007-06-16 03:06:27 +0000845 for(i=0; i<nByte; i++){
846 hash = (hash*1039) + pData[i];
danielk19773c407372005-02-15 02:54:14 +0000847 }
848 return hash;
849}
drh477731b2007-06-16 03:06:27 +0000850static u32 pager_pagehash(PgHdr *pPage){
851 return pager_datahash(pPage->pPager->pageSize,
852 (unsigned char *)PGHDR_TO_DATA(pPage));
853}
danielk19773c407372005-02-15 02:54:14 +0000854
855/*
856** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
857** is defined, and NDEBUG is not defined, an assert() statement checks
858** that the page is either dirty or still matches the calculated page-hash.
859*/
860#define CHECK_PAGE(x) checkPage(x)
861static void checkPage(PgHdr *pPg){
862 Pager *pPager = pPg->pPager;
danielk1977efaaf572006-01-16 11:29:19 +0000863 assert( !pPg->pageHash || pPager->errCode || MEMDB || pPg->dirty ||
danielk19773c407372005-02-15 02:54:14 +0000864 pPg->pageHash==pager_pagehash(pPg) );
865}
866
867#else
drh8ffa8172007-06-18 17:25:17 +0000868#define pager_datahash(X,Y) 0
drh477731b2007-06-16 03:06:27 +0000869#define pager_pagehash(X) 0
danielk19773c407372005-02-15 02:54:14 +0000870#define CHECK_PAGE(x)
871#endif
872
drhed7c8552001-04-11 14:29:21 +0000873/*
danielk197776572402004-06-25 02:38:54 +0000874** When this is called the journal file for pager pPager must be open.
875** The master journal file name is read from the end of the file and
danielk197765839c62007-08-30 08:08:17 +0000876** written into memory supplied by the caller.
danielk197776572402004-06-25 02:38:54 +0000877**
danielk197765839c62007-08-30 08:08:17 +0000878** zMaster must point to a buffer of at least nMaster bytes allocated by
879** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
880** enough space to write the master journal name). If the master journal
881** name in the journal is longer than nMaster bytes (including a
882** nul-terminator), then this is handled as if no master journal name
883** were present in the journal.
884**
885** If no master journal file name is present zMaster[0] is set to 0 and
danielk197776572402004-06-25 02:38:54 +0000886** SQLITE_OK returned.
887*/
danielk197765839c62007-08-30 08:08:17 +0000888static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, int nMaster){
danielk197776572402004-06-25 02:38:54 +0000889 int rc;
890 u32 len;
drheb206252004-10-01 02:00:31 +0000891 i64 szJ;
danielk1977c3e8f5e2004-06-28 01:16:46 +0000892 u32 cksum;
danielk1977cafadba2004-06-25 11:11:54 +0000893 int i;
danielk197776572402004-06-25 02:38:54 +0000894 unsigned char aMagic[8]; /* A buffer to hold the magic header */
895
danielk197765839c62007-08-30 08:08:17 +0000896 zMaster[0] = '\0';
danielk197776572402004-06-25 02:38:54 +0000897
drh054889e2005-11-30 03:20:31 +0000898 rc = sqlite3OsFileSize(pJrnl, &szJ);
danielk1977cafadba2004-06-25 11:11:54 +0000899 if( rc!=SQLITE_OK || szJ<16 ) return rc;
danielk197776572402004-06-25 02:38:54 +0000900
danielk197762079062007-08-15 17:08:46 +0000901 rc = read32bits(pJrnl, szJ-16, &len);
danielk197776572402004-06-25 02:38:54 +0000902 if( rc!=SQLITE_OK ) return rc;
903
danielk197765839c62007-08-30 08:08:17 +0000904 if( len>=nMaster ){
905 return SQLITE_OK;
906 }
907
danielk197762079062007-08-15 17:08:46 +0000908 rc = read32bits(pJrnl, szJ-12, &cksum);
danielk1977cafadba2004-06-25 11:11:54 +0000909 if( rc!=SQLITE_OK ) return rc;
910
danielk197762079062007-08-15 17:08:46 +0000911 rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8);
danielk197776572402004-06-25 02:38:54 +0000912 if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc;
913
danielk197765839c62007-08-30 08:08:17 +0000914 rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len);
danielk197776572402004-06-25 02:38:54 +0000915 if( rc!=SQLITE_OK ){
danielk197776572402004-06-25 02:38:54 +0000916 return rc;
917 }
danielk197765839c62007-08-30 08:08:17 +0000918 zMaster[len] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000919
920 /* See if the checksum matches the master journal name */
921 for(i=0; i<len; i++){
danielk197765839c62007-08-30 08:08:17 +0000922 cksum -= zMaster[i];
923 }
danielk19778191bff2004-06-28 04:52:30 +0000924 if( cksum ){
925 /* If the checksum doesn't add up, then one or more of the disk sectors
926 ** containing the master journal filename is corrupted. This means
927 ** definitely roll back, so just return SQLITE_OK and report a (nul)
928 ** master-journal filename.
929 */
danielk197765839c62007-08-30 08:08:17 +0000930 zMaster[0] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000931 }
danielk197776572402004-06-25 02:38:54 +0000932
933 return SQLITE_OK;
934}
935
936/*
937** Seek the journal file descriptor to the next sector boundary where a
938** journal header may be read or written. Pager.journalOff is updated with
939** the new seek offset.
940**
941** i.e for a sector size of 512:
942**
943** Input Offset Output Offset
944** ---------------------------------------
945** 0 0
946** 512 512
947** 100 512
948** 2000 2048
949**
950*/
danielk197762079062007-08-15 17:08:46 +0000951static void seekJournalHdr(Pager *pPager){
drheb206252004-10-01 02:00:31 +0000952 i64 offset = 0;
953 i64 c = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000954 if( c ){
955 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
956 }
957 assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
958 assert( offset>=c );
959 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
960 pPager->journalOff = offset;
danielk197776572402004-06-25 02:38:54 +0000961}
962
963/*
964** The journal file must be open when this routine is called. A journal
965** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
966** current location.
967**
968** The format for the journal header is as follows:
969** - 8 bytes: Magic identifying journal format.
970** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
971** - 4 bytes: Random number used for page hash.
972** - 4 bytes: Initial database page count.
973** - 4 bytes: Sector size used by the process that wrote this journal.
974**
danielk1977f42f25c2004-06-25 07:21:28 +0000975** Followed by (JOURNAL_HDR_SZ - 24) bytes of unused space.
danielk197776572402004-06-25 02:38:54 +0000976*/
977static int writeJournalHdr(Pager *pPager){
drh97b57482006-01-10 20:32:32 +0000978 char zHeader[sizeof(aJournalMagic)+16];
danielk19774099f6e2007-03-19 11:25:20 +0000979 int rc;
danielk197776572402004-06-25 02:38:54 +0000980
danielk19774099f6e2007-03-19 11:25:20 +0000981 if( pPager->stmtHdrOff==0 ){
982 pPager->stmtHdrOff = pPager->journalOff;
983 }
984
danielk197762079062007-08-15 17:08:46 +0000985 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000986 pPager->journalHdr = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000987
drh97b57482006-01-10 20:32:32 +0000988 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
danielk19774cd2cd52007-08-23 14:48:23 +0000989
990 /*
991 ** Write the nRec Field - the number of page records that follow this
992 ** journal header. Normally, zero is written to this value at this time.
993 ** After the records are added to the journal (and the journal synced,
994 ** if in full-sync mode), the zero is overwritten with the true number
995 ** of records (see syncJournal()).
996 **
997 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
998 ** reading the journal this value tells SQLite to assume that the
999 ** rest of the journal file contains valid page records. This assumption
1000 ** is dangerous, as if a failure occured whilst writing to the journal
1001 ** file it may contain some garbage data. There are two scenarios
1002 ** where this risk can be ignored:
1003 **
1004 ** * When the pager is in no-sync mode. Corruption can follow a
1005 ** power failure in this case anyway.
1006 **
1007 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
1008 ** that garbage data is never appended to the journal file.
1009 */
1010 assert(pPager->fd->pMethods||pPager->noSync);
1011 if( (pPager->noSync)
1012 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
1013 ){
1014 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
1015 }else{
1016 put32bits(&zHeader[sizeof(aJournalMagic)], 0);
1017 }
1018
drh97b57482006-01-10 20:32:32 +00001019 /* The random check-hash initialiser */
drh2fa18682008-03-19 14:15:34 +00001020 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
drh97b57482006-01-10 20:32:32 +00001021 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
1022 /* The initial database size */
1023 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbSize);
1024 /* The assumed sector size for this process */
1025 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
drhb0603412007-02-28 04:47:26 +00001026 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, sizeof(zHeader)))
danielk197762079062007-08-15 17:08:46 +00001027 rc = sqlite3OsWrite(pPager->jfd, zHeader, sizeof(zHeader),pPager->journalOff);
1028 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk197776572402004-06-25 02:38:54 +00001029
1030 /* The journal header has been written successfully. Seek the journal
1031 ** file descriptor to the end of the journal header sector.
1032 */
1033 if( rc==SQLITE_OK ){
drhb0603412007-02-28 04:47:26 +00001034 IOTRACE(("JTAIL %p %lld\n", pPager, pPager->journalOff-1))
danielk197762079062007-08-15 17:08:46 +00001035 rc = sqlite3OsWrite(pPager->jfd, "\000", 1, pPager->journalOff-1);
danielk197776572402004-06-25 02:38:54 +00001036 }
1037 return rc;
1038}
1039
1040/*
1041** The journal file must be open when this is called. A journal header file
1042** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
1043** file. See comments above function writeJournalHdr() for a description of
1044** the journal header format.
1045**
1046** If the header is read successfully, *nRec is set to the number of
1047** page records following this header and *dbSize is set to the size of the
1048** database before the transaction began, in pages. Also, pPager->cksumInit
1049** is set to the value read from the journal header. SQLITE_OK is returned
1050** in this case.
1051**
1052** If the journal header file appears to be corrupted, SQLITE_DONE is
1053** returned and *nRec and *dbSize are not set. If JOURNAL_HDR_SZ bytes
1054** cannot be read from the journal file an error code is returned.
1055*/
1056static int readJournalHdr(
1057 Pager *pPager,
drheb206252004-10-01 02:00:31 +00001058 i64 journalSize,
danielk197776572402004-06-25 02:38:54 +00001059 u32 *pNRec,
1060 u32 *pDbSize
1061){
1062 int rc;
1063 unsigned char aMagic[8]; /* A buffer to hold the magic header */
danielk197762079062007-08-15 17:08:46 +00001064 i64 jrnlOff;
danielk197776572402004-06-25 02:38:54 +00001065
danielk197762079062007-08-15 17:08:46 +00001066 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +00001067 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
1068 return SQLITE_DONE;
1069 }
danielk197762079062007-08-15 17:08:46 +00001070 jrnlOff = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +00001071
danielk197762079062007-08-15 17:08:46 +00001072 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff);
danielk197776572402004-06-25 02:38:54 +00001073 if( rc ) return rc;
danielk197762079062007-08-15 17:08:46 +00001074 jrnlOff += sizeof(aMagic);
danielk197776572402004-06-25 02:38:54 +00001075
1076 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
1077 return SQLITE_DONE;
1078 }
1079
danielk197762079062007-08-15 17:08:46 +00001080 rc = read32bits(pPager->jfd, jrnlOff, pNRec);
danielk197776572402004-06-25 02:38:54 +00001081 if( rc ) return rc;
1082
danielk197762079062007-08-15 17:08:46 +00001083 rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit);
danielk197776572402004-06-25 02:38:54 +00001084 if( rc ) return rc;
1085
danielk197762079062007-08-15 17:08:46 +00001086 rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize);
danielk197776572402004-06-25 02:38:54 +00001087 if( rc ) return rc;
1088
1089 /* Update the assumed sector-size to match the value used by
1090 ** the process that created this journal. If this journal was
1091 ** created by a process other than this one, then this routine
1092 ** is being called from within pager_playback(). The local value
1093 ** of Pager.sectorSize is restored at the end of that routine.
1094 */
danielk197762079062007-08-15 17:08:46 +00001095 rc = read32bits(pPager->jfd, jrnlOff+12, (u32 *)&pPager->sectorSize);
danielk197776572402004-06-25 02:38:54 +00001096 if( rc ) return rc;
1097
1098 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk197762079062007-08-15 17:08:46 +00001099 return SQLITE_OK;
danielk197776572402004-06-25 02:38:54 +00001100}
1101
1102
1103/*
1104** Write the supplied master journal name into the journal file for pager
danielk1977cafadba2004-06-25 11:11:54 +00001105** pPager at the current location. The master journal name must be the last
1106** thing written to a journal file. If the pager is in full-sync mode, the
1107** journal file descriptor is advanced to the next sector boundary before
1108** anything is written. The format is:
1109**
1110** + 4 bytes: PAGER_MJ_PGNO.
1111** + N bytes: length of master journal name.
1112** + 4 bytes: N
1113** + 4 bytes: Master journal name checksum.
1114** + 8 bytes: aJournalMagic[].
1115**
1116** The master journal page checksum is the sum of the bytes in the master
1117** journal name.
danielk1977aef0bf62005-12-30 16:28:01 +00001118**
1119** If zMaster is a NULL pointer (occurs for a single database transaction),
1120** this call is a no-op.
danielk197776572402004-06-25 02:38:54 +00001121*/
1122static int writeMasterJournal(Pager *pPager, const char *zMaster){
1123 int rc;
1124 int len;
danielk1977cafadba2004-06-25 11:11:54 +00001125 int i;
danielk197762079062007-08-15 17:08:46 +00001126 i64 jrnlOff;
drh97b57482006-01-10 20:32:32 +00001127 u32 cksum = 0;
1128 char zBuf[sizeof(aJournalMagic)+2*4];
danielk197776572402004-06-25 02:38:54 +00001129
1130 if( !zMaster || pPager->setMaster) return SQLITE_OK;
1131 pPager->setMaster = 1;
1132
1133 len = strlen(zMaster);
danielk1977cafadba2004-06-25 11:11:54 +00001134 for(i=0; i<len; i++){
1135 cksum += zMaster[i];
1136 }
danielk197776572402004-06-25 02:38:54 +00001137
1138 /* If in full-sync mode, advance to the next disk sector before writing
1139 ** the master journal name. This is in case the previous page written to
1140 ** the journal has already been synced.
1141 */
1142 if( pPager->fullSync ){
danielk197762079062007-08-15 17:08:46 +00001143 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +00001144 }
danielk197762079062007-08-15 17:08:46 +00001145 jrnlOff = pPager->journalOff;
danielk1977cafadba2004-06-25 11:11:54 +00001146 pPager->journalOff += (len+20);
danielk197776572402004-06-25 02:38:54 +00001147
danielk197762079062007-08-15 17:08:46 +00001148 rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager));
danielk197776572402004-06-25 02:38:54 +00001149 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001150 jrnlOff += 4;
danielk197776572402004-06-25 02:38:54 +00001151
danielk197762079062007-08-15 17:08:46 +00001152 rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff);
danielk197776572402004-06-25 02:38:54 +00001153 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001154 jrnlOff += len;
danielk197776572402004-06-25 02:38:54 +00001155
drh97b57482006-01-10 20:32:32 +00001156 put32bits(zBuf, len);
1157 put32bits(&zBuf[4], cksum);
1158 memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic));
danielk197762079062007-08-15 17:08:46 +00001159 rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff);
drh2c8997b2005-08-27 16:36:48 +00001160 pPager->needSync = !pPager->noSync;
danielk197776572402004-06-25 02:38:54 +00001161 return rc;
1162}
1163
1164/*
drh03eb96a2002-11-10 23:32:56 +00001165** Add or remove a page from the list of all pages that are in the
drhac69b052004-05-12 13:30:07 +00001166** statement journal.
drh03eb96a2002-11-10 23:32:56 +00001167**
1168** The Pager keeps a separate list of pages that are currently in
danielk19773b8a05f2007-03-19 17:44:26 +00001169** the statement journal. This helps the sqlite3PagerStmtCommit()
drh03eb96a2002-11-10 23:32:56 +00001170** routine run MUCH faster for the common case where there are many
drhac69b052004-05-12 13:30:07 +00001171** pages in memory but only a few are in the statement journal.
drh03eb96a2002-11-10 23:32:56 +00001172*/
drh3aac2dd2004-04-26 14:10:20 +00001173static void page_add_to_stmt_list(PgHdr *pPg){
drh03eb96a2002-11-10 23:32:56 +00001174 Pager *pPager = pPg->pPager;
danielk1977f35843b2007-04-07 15:03:17 +00001175 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
1176 assert( MEMDB );
1177 if( !pHist->inStmt ){
1178 assert( pHist->pPrevStmt==0 && pHist->pNextStmt==0 );
1179 if( pPager->pStmt ){
1180 PGHDR_TO_HIST(pPager->pStmt, pPager)->pPrevStmt = pPg;
1181 }
1182 pHist->pNextStmt = pPager->pStmt;
1183 pPager->pStmt = pPg;
1184 pHist->inStmt = 1;
drh03eb96a2002-11-10 23:32:56 +00001185 }
drh03eb96a2002-11-10 23:32:56 +00001186}
1187
1188/*
drhed7c8552001-04-11 14:29:21 +00001189** Find a page in the hash table given its page number. Return
1190** a pointer to the page or NULL if not found.
1191*/
drhd9b02572001-04-15 00:37:09 +00001192static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drh8ca0c722006-05-07 17:49:38 +00001193 PgHdr *p;
1194 if( pPager->aHash==0 ) return 0;
1195 p = pPager->aHash[pgno & (pPager->nHash-1)];
drhed7c8552001-04-11 14:29:21 +00001196 while( p && p->pgno!=pgno ){
1197 p = p->pNextHash;
1198 }
1199 return p;
1200}
1201
1202/*
danielk1977e180dd92007-04-05 17:15:52 +00001203** Clear the in-memory cache. This routine
drhed7c8552001-04-11 14:29:21 +00001204** sets the state of the pager back to what it was when it was first
1205** opened. Any outstanding pages are invalidated and subsequent attempts
1206** to access those pages will likely result in a coredump.
1207*/
drhd9b02572001-04-15 00:37:09 +00001208static void pager_reset(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00001209 PgHdr *pPg, *pNext;
danielk1977efaaf572006-01-16 11:29:19 +00001210 if( pPager->errCode ) return;
drhd9b02572001-04-15 00:37:09 +00001211 for(pPg=pPager->pAll; pPg; pPg=pNext){
drh538f5702007-04-13 02:14:30 +00001212 IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
1213 PAGER_INCR(sqlite3_pager_pgfree_count);
drhd9b02572001-04-15 00:37:09 +00001214 pNext = pPg->pNextAll;
danielk19779f61c2f2007-08-27 17:27:49 +00001215 lruListRemove(pPg);
drh0d180202008-02-14 23:26:56 +00001216 sqlite3_free(pPg->pData);
drh17435752007-08-16 04:30:38 +00001217 sqlite3_free(pPg);
drhed7c8552001-04-11 14:29:21 +00001218 }
danielk19779f61c2f2007-08-27 17:27:49 +00001219 assert(pPager->lru.pFirst==0);
1220 assert(pPager->lru.pFirstSynced==0);
1221 assert(pPager->lru.pLast==0);
danielk1977b94bf852007-03-19 13:53:37 +00001222 pPager->pStmt = 0;
drhd9b02572001-04-15 00:37:09 +00001223 pPager->pAll = 0;
danielk1977ae72d982007-10-03 08:46:44 +00001224 pPager->pDirty = 0;
drh8ca0c722006-05-07 17:49:38 +00001225 pPager->nHash = 0;
drh17435752007-08-16 04:30:38 +00001226 sqlite3_free(pPager->aHash);
drhed7c8552001-04-11 14:29:21 +00001227 pPager->nPage = 0;
drh8ca0c722006-05-07 17:49:38 +00001228 pPager->aHash = 0;
drhed7c8552001-04-11 14:29:21 +00001229 pPager->nRef = 0;
danielk1977e277be02007-03-23 18:12:06 +00001230}
1231
1232/*
danielk1977ae72d982007-10-03 08:46:44 +00001233** Unlock the database file.
1234**
1235** If the pager is currently in error state, discard the contents of
1236** the cache and reset the Pager structure internal state. If there is
1237** an open journal-file, then the next time a shared-lock is obtained
1238** on the pager file (by this or any other process), it will be
1239** treated as a hot-journal and rolled back.
1240*/
1241static void pager_unlock(Pager *pPager){
1242 if( !pPager->exclusiveMode ){
1243 if( !MEMDB ){
drh1aa5af12008-03-07 19:51:14 +00001244 int rc = osUnlock(pPager->fd, NO_LOCK);
drh308aa322008-03-07 20:14:38 +00001245 if( rc ) pPager->errCode = rc;
danielk1977ae72d982007-10-03 08:46:44 +00001246 pPager->dbSize = -1;
1247 IOTRACE(("UNLOCK %p\n", pPager))
1248
1249 /* If Pager.errCode is set, the contents of the pager cache cannot be
1250 ** trusted. Now that the pager file is unlocked, the contents of the
1251 ** cache can be discarded and the error code safely cleared.
1252 */
1253 if( pPager->errCode ){
drh1aa5af12008-03-07 19:51:14 +00001254 if( rc==SQLITE_OK ) pPager->errCode = SQLITE_OK;
danielk1977ae72d982007-10-03 08:46:44 +00001255 pager_reset(pPager);
1256 if( pPager->stmtOpen ){
1257 sqlite3OsClose(pPager->stfd);
drhf5e7bb52008-02-18 14:47:33 +00001258 sqlite3BitvecDestroy(pPager->pInStmt);
1259 pPager->pInStmt = 0;
danielk1977ae72d982007-10-03 08:46:44 +00001260 }
1261 if( pPager->journalOpen ){
1262 sqlite3OsClose(pPager->jfd);
1263 pPager->journalOpen = 0;
drhf5e7bb52008-02-18 14:47:33 +00001264 sqlite3BitvecDestroy(pPager->pInJournal);
1265 pPager->pInJournal = 0;
danielk1977ae72d982007-10-03 08:46:44 +00001266 }
1267 pPager->stmtOpen = 0;
1268 pPager->stmtInUse = 0;
1269 pPager->journalOff = 0;
1270 pPager->journalStarted = 0;
1271 pPager->stmtAutoopen = 0;
1272 pPager->origDbSize = 0;
1273 }
1274 }
1275
1276 if( !MEMDB || pPager->errCode==SQLITE_OK ){
1277 pPager->state = PAGER_UNLOCK;
1278 pPager->changeCountDone = 0;
1279 }
1280 }
1281}
1282
1283/*
1284** Execute a rollback if a transaction is active and unlock the
1285** database file. If the pager has already entered the error state,
1286** do not attempt the rollback.
1287*/
1288static void pagerUnlockAndRollback(Pager *p){
1289 assert( p->state>=PAGER_RESERVED || p->journalOpen==0 );
1290 if( p->errCode==SQLITE_OK && p->state>=PAGER_RESERVED ){
1291 sqlite3PagerRollback(p);
1292 }
1293 pager_unlock(p);
1294 assert( p->errCode || !p->journalOpen || (p->exclusiveMode&&!p->journalOff) );
1295 assert( p->errCode || !p->stmtOpen || p->exclusiveMode );
1296}
1297
1298/*
drh80e35f42007-03-30 14:06:34 +00001299** This routine ends a transaction. A transaction is ended by either
1300** a COMMIT or a ROLLBACK.
1301**
drhed7c8552001-04-11 14:29:21 +00001302** When this routine is called, the pager has the journal file open and
drh80e35f42007-03-30 14:06:34 +00001303** a RESERVED or EXCLUSIVE lock on the database. This routine will release
1304** the database lock and acquires a SHARED lock in its place if that is
1305** the appropriate thing to do. Release locks usually is appropriate,
1306** unless we are in exclusive access mode or unless this is a
1307** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation.
1308**
1309** The journal file is either deleted or truncated.
drh50457892003-09-06 01:10:47 +00001310**
1311** TODO: Consider keeping the journal file open for temporary databases.
1312** This might give a performance improvement on windows where opening
1313** a file is an expensive operation.
drhed7c8552001-04-11 14:29:21 +00001314*/
drh80e35f42007-03-30 14:06:34 +00001315static int pager_end_transaction(Pager *pPager){
drhd9b02572001-04-15 00:37:09 +00001316 PgHdr *pPg;
danielk197741483462007-03-24 16:45:04 +00001317 int rc = SQLITE_OK;
danielk1977979f38e2007-03-27 16:19:51 +00001318 int rc2 = SQLITE_OK;
drhb7f91642004-10-31 02:22:47 +00001319 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +00001320 if( pPager->state<PAGER_RESERVED ){
1321 return SQLITE_OK;
1322 }
danielk19773b8a05f2007-03-19 17:44:26 +00001323 sqlite3PagerStmtCommit(pPager);
danielk1977334cdb62007-03-26 08:05:12 +00001324 if( pPager->stmtOpen && !pPager->exclusiveMode ){
danielk1977b4b47412007-08-17 15:53:36 +00001325 sqlite3OsClose(pPager->stfd);
danielk1977979f38e2007-03-27 16:19:51 +00001326 pPager->stmtOpen = 0;
drh0f892532002-05-30 12:27:03 +00001327 }
drhda47d772002-12-02 04:25:19 +00001328 if( pPager->journalOpen ){
drh01fa4c32007-04-02 11:22:22 +00001329 if( pPager->exclusiveMode
1330 && (rc = sqlite3OsTruncate(pPager->jfd, 0))==SQLITE_OK ){;
danielk197741483462007-03-24 16:45:04 +00001331 pPager->journalOff = 0;
danielk1977334cdb62007-03-26 08:05:12 +00001332 pPager->journalStarted = 0;
danielk197741483462007-03-24 16:45:04 +00001333 }else{
danielk1977b4b47412007-08-17 15:53:36 +00001334 sqlite3OsClose(pPager->jfd);
danielk197741483462007-03-24 16:45:04 +00001335 pPager->journalOpen = 0;
drh01fa4c32007-04-02 11:22:22 +00001336 if( rc==SQLITE_OK ){
danielk1977fee2d252007-08-18 10:59:19 +00001337 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
danielk19777152de82007-03-29 17:28:14 +00001338 }
danielk197741483462007-03-24 16:45:04 +00001339 }
drhf5e7bb52008-02-18 14:47:33 +00001340 sqlite3BitvecDestroy(pPager->pInJournal);
1341 pPager->pInJournal = 0;
drhda47d772002-12-02 04:25:19 +00001342 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1343 pPg->inJournal = 0;
1344 pPg->dirty = 0;
drhdb48ee02003-01-16 13:42:43 +00001345 pPg->needSync = 0;
danielk197741483462007-03-24 16:45:04 +00001346 pPg->alwaysRollback = 0;
danielk19773c407372005-02-15 02:54:14 +00001347#ifdef SQLITE_CHECK_PAGES
1348 pPg->pageHash = pager_pagehash(pPg);
1349#endif
drhda47d772002-12-02 04:25:19 +00001350 }
drh605ad8f2006-05-03 23:34:05 +00001351 pPager->pDirty = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001352 pPager->dirtyCache = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001353 pPager->nRec = 0;
drhda47d772002-12-02 04:25:19 +00001354 }else{
drhf5e7bb52008-02-18 14:47:33 +00001355 assert( pPager->pInJournal==0 );
drha6abd042004-06-09 17:37:22 +00001356 assert( pPager->dirtyCache==0 || pPager->useJournal==0 );
drhd9b02572001-04-15 00:37:09 +00001357 }
danielk1977979f38e2007-03-27 16:19:51 +00001358
danielk197741483462007-03-24 16:45:04 +00001359 if( !pPager->exclusiveMode ){
danielk19777a2b1ee2007-08-21 14:27:01 +00001360 rc2 = osUnlock(pPager->fd, SHARED_LOCK);
danielk197741483462007-03-24 16:45:04 +00001361 pPager->state = PAGER_SHARED;
danielk1977334cdb62007-03-26 08:05:12 +00001362 }else if( pPager->state==PAGER_SYNCED ){
1363 pPager->state = PAGER_EXCLUSIVE;
danielk197741483462007-03-24 16:45:04 +00001364 }
danielk1977334cdb62007-03-26 08:05:12 +00001365 pPager->origDbSize = 0;
danielk197776572402004-06-25 02:38:54 +00001366 pPager->setMaster = 0;
danielk1977c4da5b92006-01-21 12:08:54 +00001367 pPager->needSync = 0;
danielk19779f61c2f2007-08-27 17:27:49 +00001368 lruListSetFirstSynced(pPager);
drh588f5bc2007-01-02 18:41:54 +00001369 pPager->dbSize = -1;
danielk1977979f38e2007-03-27 16:19:51 +00001370
1371 return (rc==SQLITE_OK?rc2:rc);
drhed7c8552001-04-11 14:29:21 +00001372}
1373
drhed7c8552001-04-11 14:29:21 +00001374/*
drh968af522003-02-11 14:55:40 +00001375** Compute and return a checksum for the page of data.
drh34e79ce2004-02-08 06:05:46 +00001376**
1377** This is not a real checksum. It is really just the sum of the
drh726de592004-06-10 23:35:50 +00001378** random initial value and the page number. We experimented with
1379** a checksum of the entire data, but that was found to be too slow.
1380**
1381** Note that the page number is stored at the beginning of data and
1382** the checksum is stored at the end. This is important. If journal
1383** corruption occurs due to a power failure, the most likely scenario
1384** is that one end or the other of the record will be changed. It is
1385** much less likely that the two ends of the journal record will be
1386** correct and the middle be corrupt. Thus, this "checksum" scheme,
1387** though fast and simple, catches the mostly likely kind of corruption.
1388**
1389** FIX ME: Consider adding every 200th (or so) byte of the data to the
1390** checksum. That way if a single page spans 3 or more disk sectors and
1391** only the middle sector is corrupt, we will still have a reasonable
1392** chance of failing the checksum and thus detecting the problem.
drh968af522003-02-11 14:55:40 +00001393*/
drh74161702006-02-24 02:53:49 +00001394static u32 pager_cksum(Pager *pPager, const u8 *aData){
danielk1977ef317ab2004-06-23 10:43:10 +00001395 u32 cksum = pPager->cksumInit;
1396 int i = pPager->pageSize-200;
1397 while( i>0 ){
1398 cksum += aData[i];
1399 i -= 200;
1400 }
drh968af522003-02-11 14:55:40 +00001401 return cksum;
1402}
1403
drh605ad8f2006-05-03 23:34:05 +00001404/* Forward declaration */
1405static void makeClean(PgHdr*);
1406
drh968af522003-02-11 14:55:40 +00001407/*
drhfa86c412002-02-02 15:01:15 +00001408** Read a single page from the journal file opened on file descriptor
1409** jfd. Playback this one page.
drh968af522003-02-11 14:55:40 +00001410**
drh726de592004-06-10 23:35:50 +00001411** If useCksum==0 it means this journal does not use checksums. Checksums
1412** are not used in statement journals because statement journals do not
1413** need to survive power failures.
drhfa86c412002-02-02 15:01:15 +00001414*/
danielk197762079062007-08-15 17:08:46 +00001415static int pager_playback_one_page(
1416 Pager *pPager,
1417 sqlite3_file *jfd,
1418 i64 offset,
1419 int useCksum
1420){
drhfa86c412002-02-02 15:01:15 +00001421 int rc;
drhae2b40c2004-06-09 19:03:54 +00001422 PgHdr *pPg; /* An existing page in the cache */
1423 Pgno pgno; /* The page number of a page in journal */
1424 u32 cksum; /* Checksum used for sanity checking */
danielk19778186df82007-03-06 13:45:59 +00001425 u8 *aData = (u8 *)pPager->pTmpSpace; /* Temp storage for a page */
drhfa86c412002-02-02 15:01:15 +00001426
drh96362842005-03-20 22:47:56 +00001427 /* useCksum should be true for the main journal and false for
1428 ** statement journals. Verify that this is always the case
1429 */
drh9cbe6352005-11-29 03:13:21 +00001430 assert( jfd == (useCksum ? pPager->jfd : pPager->stfd) );
danielk19778186df82007-03-06 13:45:59 +00001431 assert( aData );
drh96362842005-03-20 22:47:56 +00001432
danielk197762079062007-08-15 17:08:46 +00001433 rc = read32bits(jfd, offset, &pgno);
drh99ee3602003-02-16 19:13:36 +00001434 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001435 rc = sqlite3OsRead(jfd, aData, pPager->pageSize, offset+4);
drh99ee3602003-02-16 19:13:36 +00001436 if( rc!=SQLITE_OK ) return rc;
danielk197776572402004-06-25 02:38:54 +00001437 pPager->journalOff += pPager->pageSize + 4;
drhfa86c412002-02-02 15:01:15 +00001438
drh968af522003-02-11 14:55:40 +00001439 /* Sanity checking on the page. This is more important that I originally
1440 ** thought. If a power failure occurs while the journal is being written,
1441 ** it could cause invalid data to be written into the journal. We need to
1442 ** detect this invalid data (with high probability) and ignore it.
1443 */
danielk197775edc162004-06-26 01:48:18 +00001444 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh968af522003-02-11 14:55:40 +00001445 return SQLITE_DONE;
1446 }
drhae2b40c2004-06-09 19:03:54 +00001447 if( pgno>(unsigned)pPager->dbSize ){
drh968af522003-02-11 14:55:40 +00001448 return SQLITE_OK;
1449 }
drhae2b40c2004-06-09 19:03:54 +00001450 if( useCksum ){
danielk197762079062007-08-15 17:08:46 +00001451 rc = read32bits(jfd, offset+pPager->pageSize+4, &cksum);
drh99ee3602003-02-16 19:13:36 +00001452 if( rc ) return rc;
danielk197776572402004-06-25 02:38:54 +00001453 pPager->journalOff += 4;
drh74161702006-02-24 02:53:49 +00001454 if( pager_cksum(pPager, aData)!=cksum ){
drh968af522003-02-11 14:55:40 +00001455 return SQLITE_DONE;
1456 }
1457 }
drhfa86c412002-02-02 15:01:15 +00001458
danielk1977aa5ccdf2004-06-14 05:10:42 +00001459 assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE );
danielk1977a3f3a5f2004-06-10 04:32:16 +00001460
1461 /* If the pager is in RESERVED state, then there must be a copy of this
1462 ** page in the pager cache. In this case just update the pager cache,
danielk19770de0bb32004-06-10 05:59:24 +00001463 ** not the database file. The page is left marked dirty in this case.
1464 **
danielk19772df71c72007-05-24 07:22:42 +00001465 ** An exception to the above rule: If the database is in no-sync mode
1466 ** and a page is moved during an incremental vacuum then the page may
danielk1977369f3a02007-05-24 09:41:20 +00001467 ** not be in the pager cache. Later: if a malloc() or IO error occurs
1468 ** during a Movepage() call, then the page may not be in the cache
1469 ** either. So the condition described in the above paragraph is not
1470 ** assert()able.
danielk19772df71c72007-05-24 07:22:42 +00001471 **
danielk1977a3f3a5f2004-06-10 04:32:16 +00001472 ** If in EXCLUSIVE state, then we update the pager cache if it exists
1473 ** and the main file. The page is then marked not dirty.
drh96362842005-03-20 22:47:56 +00001474 **
1475 ** Ticket #1171: The statement journal might contain page content that is
1476 ** different from the page content at the start of the transaction.
1477 ** This occurs when a page is changed prior to the start of a statement
1478 ** then changed again within the statement. When rolling back such a
1479 ** statement we must not write to the original database unless we know
drh5e385312007-06-16 04:42:12 +00001480 ** for certain that original page contents are synced into the main rollback
1481 ** journal. Otherwise, a power loss might leave modified data in the
1482 ** database file without an entry in the rollback journal that can
1483 ** restore the database to its original form. Two conditions must be
1484 ** met before writing to the database files. (1) the database must be
1485 ** locked. (2) we know that the original page content is fully synced
1486 ** in the main journal either because the page is not in cache or else
1487 ** the page is marked as needSync==0.
drhfa86c412002-02-02 15:01:15 +00001488 */
drhae2b40c2004-06-09 19:03:54 +00001489 pPg = pager_lookup(pPager, pgno);
drh477731b2007-06-16 03:06:27 +00001490 PAGERTRACE4("PLAYBACK %d page %d hash(%08x)\n",
1491 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData));
drh96362842005-03-20 22:47:56 +00001492 if( pPager->state>=PAGER_EXCLUSIVE && (pPg==0 || pPg->needSync==0) ){
danielk197762079062007-08-15 17:08:46 +00001493 i64 offset = (pgno-1)*(i64)pPager->pageSize;
1494 rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, offset);
drh605ad8f2006-05-03 23:34:05 +00001495 if( pPg ){
1496 makeClean(pPg);
1497 }
danielk1977a3f3a5f2004-06-10 04:32:16 +00001498 }
drhfa86c412002-02-02 15:01:15 +00001499 if( pPg ){
danielk197728129562005-01-11 10:25:06 +00001500 /* No page should ever be explicitly rolled back that is in use, except
1501 ** for page 1 which is held in use in order to keep the lock on the
1502 ** database active. However such a page may be rolled back as a result
1503 ** of an internal error resulting in an automatic call to
danielk19773b8a05f2007-03-19 17:44:26 +00001504 ** sqlite3PagerRollback().
drhacf4ac92003-12-17 23:57:34 +00001505 */
drhb6f41482004-05-14 01:58:11 +00001506 void *pData;
danielk197728129562005-01-11 10:25:06 +00001507 /* assert( pPg->nRef==0 || pPg->pgno==1 ); */
drhb6f41482004-05-14 01:58:11 +00001508 pData = PGHDR_TO_DATA(pPg);
drhae2b40c2004-06-09 19:03:54 +00001509 memcpy(pData, aData, pPager->pageSize);
danielk19779038bb62007-04-09 11:20:54 +00001510 if( pPager->xReiniter ){
1511 pPager->xReiniter(pPg, pPager->pageSize);
drhde647132004-05-07 17:57:49 +00001512 }
danielk19773c407372005-02-15 02:54:14 +00001513#ifdef SQLITE_CHECK_PAGES
drh96362842005-03-20 22:47:56 +00001514 pPg->pageHash = pager_pagehash(pPg);
danielk19773c407372005-02-15 02:54:14 +00001515#endif
drh86a88112007-04-16 15:02:19 +00001516 /* If this was page 1, then restore the value of Pager.dbFileVers.
1517 ** Do this before any decoding. */
danielk197741483462007-03-24 16:45:04 +00001518 if( pgno==1 ){
drh86a88112007-04-16 15:02:19 +00001519 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
danielk197741483462007-03-24 16:45:04 +00001520 }
drh86a88112007-04-16 15:02:19 +00001521
1522 /* Decode the page just read from disk */
1523 CODEC1(pPager, pData, pPg->pgno, 3);
drhfa86c412002-02-02 15:01:15 +00001524 }
1525 return rc;
1526}
1527
1528/*
danielk197713adf8a2004-06-03 16:08:41 +00001529** Parameter zMaster is the name of a master journal file. A single journal
1530** file that referred to the master journal file has just been rolled back.
1531** This routine checks if it is possible to delete the master journal file,
1532** and does so if it is.
drh726de592004-06-10 23:35:50 +00001533**
danielk197765839c62007-08-30 08:08:17 +00001534** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not
1535** available for use within this function.
1536**
1537**
drh726de592004-06-10 23:35:50 +00001538** The master journal file contains the names of all child journals.
1539** To tell if a master journal can be deleted, check to each of the
1540** children. If all children are either missing or do not refer to
1541** a different master journal, then this master journal can be deleted.
danielk197713adf8a2004-06-03 16:08:41 +00001542*/
danielk1977b4b47412007-08-17 15:53:36 +00001543static int pager_delmaster(Pager *pPager, const char *zMaster){
1544 sqlite3_vfs *pVfs = pPager->pVfs;
danielk197713adf8a2004-06-03 16:08:41 +00001545 int rc;
1546 int master_open = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001547 sqlite3_file *pMaster;
1548 sqlite3_file *pJournal;
danielk197713adf8a2004-06-03 16:08:41 +00001549 char *zMasterJournal = 0; /* Contents of master journal file */
drheb206252004-10-01 02:00:31 +00001550 i64 nMasterJournal; /* Size of master journal file */
danielk197713adf8a2004-06-03 16:08:41 +00001551
1552 /* Open the master journal file exclusively in case some other process
1553 ** is running this routine also. Not that it makes too much difference.
1554 */
danielk1977b4b47412007-08-17 15:53:36 +00001555 pMaster = (sqlite3_file *)sqlite3_malloc(pVfs->szOsFile * 2);
danielk1977fee2d252007-08-18 10:59:19 +00001556 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
danielk1977b4b47412007-08-17 15:53:36 +00001557 if( !pMaster ){
1558 rc = SQLITE_NOMEM;
1559 }else{
danielk1977fee2d252007-08-18 10:59:19 +00001560 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
1561 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
danielk1977b4b47412007-08-17 15:53:36 +00001562 }
danielk197713adf8a2004-06-03 16:08:41 +00001563 if( rc!=SQLITE_OK ) goto delmaster_out;
1564 master_open = 1;
danielk1977b4b47412007-08-17 15:53:36 +00001565
1566 rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001567 if( rc!=SQLITE_OK ) goto delmaster_out;
1568
1569 if( nMasterJournal>0 ){
danielk19775865e3d2004-06-14 06:03:57 +00001570 char *zJournal;
danielk197776572402004-06-25 02:38:54 +00001571 char *zMasterPtr = 0;
danielk197765839c62007-08-30 08:08:17 +00001572 int nMasterPtr = pPager->pVfs->mxPathname+1;
danielk19775865e3d2004-06-14 06:03:57 +00001573
1574 /* Load the entire master journal file into space obtained from
drh17435752007-08-16 04:30:38 +00001575 ** sqlite3_malloc() and pointed to by zMasterJournal.
danielk19775865e3d2004-06-14 06:03:57 +00001576 */
danielk197765839c62007-08-30 08:08:17 +00001577 zMasterJournal = (char *)sqlite3_malloc(nMasterJournal + nMasterPtr);
danielk197713adf8a2004-06-03 16:08:41 +00001578 if( !zMasterJournal ){
1579 rc = SQLITE_NOMEM;
1580 goto delmaster_out;
1581 }
danielk197765839c62007-08-30 08:08:17 +00001582 zMasterPtr = &zMasterJournal[nMasterJournal];
danielk1977b4b47412007-08-17 15:53:36 +00001583 rc = sqlite3OsRead(pMaster, zMasterJournal, nMasterJournal, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001584 if( rc!=SQLITE_OK ) goto delmaster_out;
1585
danielk19775865e3d2004-06-14 06:03:57 +00001586 zJournal = zMasterJournal;
1587 while( (zJournal-zMasterJournal)<nMasterJournal ){
danielk1977b4b47412007-08-17 15:53:36 +00001588 if( sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS) ){
danielk197713adf8a2004-06-03 16:08:41 +00001589 /* One of the journals pointed to by the master journal exists.
1590 ** Open it and check if it points at the master journal. If
1591 ** so, return without deleting the master journal file.
1592 */
drh3b7b78b2004-11-24 01:16:43 +00001593 int c;
danielk1977fee2d252007-08-18 10:59:19 +00001594 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
1595 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001596 if( rc!=SQLITE_OK ){
danielk197713adf8a2004-06-03 16:08:41 +00001597 goto delmaster_out;
1598 }
danielk197713adf8a2004-06-03 16:08:41 +00001599
danielk197765839c62007-08-30 08:08:17 +00001600 rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
danielk1977b4b47412007-08-17 15:53:36 +00001601 sqlite3OsClose(pJournal);
danielk19779eed5052004-06-04 10:38:30 +00001602 if( rc!=SQLITE_OK ){
danielk19779eed5052004-06-04 10:38:30 +00001603 goto delmaster_out;
1604 }
danielk197776572402004-06-25 02:38:54 +00001605
danielk197765839c62007-08-30 08:08:17 +00001606 c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
drh3b7b78b2004-11-24 01:16:43 +00001607 if( c ){
danielk197776572402004-06-25 02:38:54 +00001608 /* We have a match. Do not delete the master journal file. */
1609 goto delmaster_out;
danielk197713adf8a2004-06-03 16:08:41 +00001610 }
1611 }
danielk19775865e3d2004-06-14 06:03:57 +00001612 zJournal += (strlen(zJournal)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001613 }
1614 }
1615
danielk1977fee2d252007-08-18 10:59:19 +00001616 rc = sqlite3OsDelete(pVfs, zMaster, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001617
1618delmaster_out:
1619 if( zMasterJournal ){
drh17435752007-08-16 04:30:38 +00001620 sqlite3_free(zMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001621 }
1622 if( master_open ){
danielk1977b4b47412007-08-17 15:53:36 +00001623 sqlite3OsClose(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001624 }
danielk1977b4b47412007-08-17 15:53:36 +00001625 sqlite3_free(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001626 return rc;
1627}
1628
drha6abd042004-06-09 17:37:22 +00001629
danielk1977e180dd92007-04-05 17:15:52 +00001630static void pager_truncate_cache(Pager *pPager);
1631
drha6abd042004-06-09 17:37:22 +00001632/*
drhcb4c40b2004-08-18 19:09:43 +00001633** Truncate the main file of the given pager to the number of pages
danielk1977e180dd92007-04-05 17:15:52 +00001634** indicated. Also truncate the cached representation of the file.
drh7fe3f7e2007-11-29 18:44:27 +00001635**
1636** Might might be the case that the file on disk is smaller than nPage.
1637** This can happen, for example, if we are in the middle of a transaction
1638** which has extended the file size and the new pages are still all held
1639** in cache, then an INSERT or UPDATE does a statement rollback. Some
1640** operating system implementations can get confused if you try to
1641** truncate a file to some size that is larger than it currently is,
1642** so detect this case and do not do the truncation.
drhcb4c40b2004-08-18 19:09:43 +00001643*/
1644static int pager_truncate(Pager *pPager, int nPage){
danielk1977e180dd92007-04-05 17:15:52 +00001645 int rc = SQLITE_OK;
danielk19777a2b1ee2007-08-21 14:27:01 +00001646 if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){
drh7fe3f7e2007-11-29 18:44:27 +00001647 i64 currentSize, newSize;
1648 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
1649 newSize = pPager->pageSize*(i64)nPage;
1650 if( rc==SQLITE_OK && currentSize>newSize ){
1651 rc = sqlite3OsTruncate(pPager->fd, newSize);
1652 }
danielk1977e180dd92007-04-05 17:15:52 +00001653 }
1654 if( rc==SQLITE_OK ){
1655 pPager->dbSize = nPage;
1656 pager_truncate_cache(pPager);
1657 }
1658 return rc;
drhcb4c40b2004-08-18 19:09:43 +00001659}
1660
1661/*
drhc80f0582007-05-01 16:59:48 +00001662** Set the sectorSize for the given pager.
1663**
1664** The sector size is the larger of the sector size reported
1665** by sqlite3OsSectorSize() and the pageSize.
1666*/
1667static void setSectorSize(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00001668 assert(pPager->fd->pMethods||pPager->tempFile);
1669 if( !pPager->tempFile ){
1670 /* Sector size doesn't matter for temporary files. Also, the file
1671 ** may not have been opened yet, in whcih case the OsSectorSize()
1672 ** call will segfault.
1673 */
1674 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
1675 }
drhc80f0582007-05-01 16:59:48 +00001676 if( pPager->sectorSize<pPager->pageSize ){
1677 pPager->sectorSize = pPager->pageSize;
1678 }
1679}
1680
1681/*
drhed7c8552001-04-11 14:29:21 +00001682** Playback the journal and thus restore the database file to
1683** the state it was in before we started making changes.
1684**
drh34e79ce2004-02-08 06:05:46 +00001685** The journal file format is as follows:
1686**
drhae2b40c2004-06-09 19:03:54 +00001687** (1) 8 byte prefix. A copy of aJournalMagic[].
1688** (2) 4 byte big-endian integer which is the number of valid page records
drh34e79ce2004-02-08 06:05:46 +00001689** in the journal. If this value is 0xffffffff, then compute the
drhae2b40c2004-06-09 19:03:54 +00001690** number of page records from the journal size.
1691** (3) 4 byte big-endian integer which is the initial value for the
1692** sanity checksum.
1693** (4) 4 byte integer which is the number of pages to truncate the
drh34e79ce2004-02-08 06:05:46 +00001694** database to during a rollback.
drhae2b40c2004-06-09 19:03:54 +00001695** (5) 4 byte integer which is the number of bytes in the master journal
1696** name. The value may be zero (indicate that there is no master
1697** journal.)
1698** (6) N bytes of the master journal name. The name will be nul-terminated
1699** and might be shorter than the value read from (5). If the first byte
1700** of the name is \000 then there is no master journal. The master
1701** journal name is stored in UTF-8.
1702** (7) Zero or more pages instances, each as follows:
drh34e79ce2004-02-08 06:05:46 +00001703** + 4 byte page number.
drhae2b40c2004-06-09 19:03:54 +00001704** + pPager->pageSize bytes of data.
1705** + 4 byte checksum
drh34e79ce2004-02-08 06:05:46 +00001706**
drhae2b40c2004-06-09 19:03:54 +00001707** When we speak of the journal header, we mean the first 6 items above.
1708** Each entry in the journal is an instance of the 7th item.
drh34e79ce2004-02-08 06:05:46 +00001709**
1710** Call the value from the second bullet "nRec". nRec is the number of
1711** valid page entries in the journal. In most cases, you can compute the
1712** value of nRec from the size of the journal file. But if a power
1713** failure occurred while the journal was being written, it could be the
1714** case that the size of the journal file had already been increased but
1715** the extra entries had not yet made it safely to disk. In such a case,
1716** the value of nRec computed from the file size would be too large. For
1717** that reason, we always use the nRec value in the header.
1718**
1719** If the nRec value is 0xffffffff it means that nRec should be computed
1720** from the file size. This value is used when the user selects the
1721** no-sync option for the journal. A power failure could lead to corruption
1722** in this case. But for things like temporary table (which will be
1723** deleted when the power is restored) we don't care.
1724**
drhd9b02572001-04-15 00:37:09 +00001725** If the file opened as the journal file is not a well-formed
danielk1977ece80f12004-06-23 01:05:26 +00001726** journal file then all pages up to the first corrupted page are rolled
1727** back (or no pages if the journal header is corrupted). The journal file
1728** is then deleted and SQLITE_OK returned, just as if no corruption had
1729** been encountered.
1730**
1731** If an I/O or malloc() error occurs, the journal-file is not deleted
1732** and an error code is returned.
drhed7c8552001-04-11 14:29:21 +00001733*/
danielk1977e277be02007-03-23 18:12:06 +00001734static int pager_playback(Pager *pPager, int isHot){
danielk1977b4b47412007-08-17 15:53:36 +00001735 sqlite3_vfs *pVfs = pPager->pVfs;
drheb206252004-10-01 02:00:31 +00001736 i64 szJ; /* Size of the journal file in bytes */
danielk1977c3e8f5e2004-06-28 01:16:46 +00001737 u32 nRec; /* Number of Records in the journal */
drhd9b02572001-04-15 00:37:09 +00001738 int i; /* Loop counter */
1739 Pgno mxPg = 0; /* Size of the original file in pages */
drhae2b40c2004-06-09 19:03:54 +00001740 int rc; /* Result code of a subroutine */
danielk197713adf8a2004-06-03 16:08:41 +00001741 char *zMaster = 0; /* Name of master journal file if any */
drhed7c8552001-04-11 14:29:21 +00001742
drhc3a64ba2001-11-22 00:01:27 +00001743 /* Figure out how many records are in the journal. Abort early if
1744 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +00001745 */
drh8cfbf082001-09-19 13:22:39 +00001746 assert( pPager->journalOpen );
drh054889e2005-11-30 03:20:31 +00001747 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
danielk1977334cdb62007-03-26 08:05:12 +00001748 if( rc!=SQLITE_OK || szJ==0 ){
drhc3a64ba2001-11-22 00:01:27 +00001749 goto end_playback;
1750 }
drh240c5792004-02-08 00:40:52 +00001751
danielk197776572402004-06-25 02:38:54 +00001752 /* Read the master journal name from the journal, if it is present.
1753 ** If a master journal file name is specified, but the file is not
1754 ** present on disk, then the journal is not hot and does not need to be
1755 ** played back.
drh240c5792004-02-08 00:40:52 +00001756 */
danielk197765839c62007-08-30 08:08:17 +00001757 zMaster = pPager->pTmpSpace;
1758 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
danielk197776572402004-06-25 02:38:54 +00001759 assert( rc!=SQLITE_DONE );
danielk1977b4b47412007-08-17 15:53:36 +00001760 if( rc!=SQLITE_OK
danielk197765839c62007-08-30 08:08:17 +00001761 || (zMaster[0] && !sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS))
danielk1977b4b47412007-08-17 15:53:36 +00001762 ){
danielk197776572402004-06-25 02:38:54 +00001763 zMaster = 0;
1764 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
drhc3a64ba2001-11-22 00:01:27 +00001765 goto end_playback;
1766 }
danielk197776572402004-06-25 02:38:54 +00001767 pPager->journalOff = 0;
danielk197765839c62007-08-30 08:08:17 +00001768 zMaster = 0;
drhc3a64ba2001-11-22 00:01:27 +00001769
danielk197776572402004-06-25 02:38:54 +00001770 /* This loop terminates either when the readJournalHdr() call returns
1771 ** SQLITE_DONE or an IO error occurs. */
1772 while( 1 ){
drhae2b40c2004-06-09 19:03:54 +00001773
danielk197776572402004-06-25 02:38:54 +00001774 /* Read the next journal header from the journal file. If there are
1775 ** not enough bytes left in the journal file for a complete header, or
1776 ** it is corrupted, then a process must of failed while writing it.
1777 ** This indicates nothing more needs to be rolled back.
1778 */
1779 rc = readJournalHdr(pPager, szJ, &nRec, &mxPg);
1780 if( rc!=SQLITE_OK ){
drh968af522003-02-11 14:55:40 +00001781 if( rc==SQLITE_DONE ){
drh968af522003-02-11 14:55:40 +00001782 rc = SQLITE_OK;
1783 }
danielk197776572402004-06-25 02:38:54 +00001784 goto end_playback;
1785 }
1786
1787 /* If nRec is 0xffffffff, then this journal was created by a process
1788 ** working in no-sync mode. This means that the rest of the journal
1789 ** file consists of pages, there are no more journal headers. Compute
1790 ** the value of nRec based on this assumption.
1791 */
1792 if( nRec==0xffffffff ){
1793 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
1794 nRec = (szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager);
1795 }
1796
danielk1977e277be02007-03-23 18:12:06 +00001797 /* If nRec is 0 and this rollback is of a transaction created by this
drh8940f4e2007-08-11 00:26:20 +00001798 ** process and if this is the final header in the journal, then it means
1799 ** that this part of the journal was being filled but has not yet been
1800 ** synced to disk. Compute the number of pages based on the remaining
1801 ** size of the file.
1802 **
1803 ** The third term of the test was added to fix ticket #2565.
danielk1977e277be02007-03-23 18:12:06 +00001804 */
drh8940f4e2007-08-11 00:26:20 +00001805 if( nRec==0 && !isHot &&
1806 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
danielk1977e277be02007-03-23 18:12:06 +00001807 nRec = (szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager);
1808 }
1809
danielk197776572402004-06-25 02:38:54 +00001810 /* If this is the first header read from the journal, truncate the
drh85b623f2007-12-13 21:54:09 +00001811 ** database file back to its original size.
danielk197776572402004-06-25 02:38:54 +00001812 */
danielk1977e180dd92007-04-05 17:15:52 +00001813 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
drhcb4c40b2004-08-18 19:09:43 +00001814 rc = pager_truncate(pPager, mxPg);
danielk197776572402004-06-25 02:38:54 +00001815 if( rc!=SQLITE_OK ){
1816 goto end_playback;
1817 }
danielk197776572402004-06-25 02:38:54 +00001818 }
1819
danielk197776572402004-06-25 02:38:54 +00001820 /* Copy original pages out of the journal and back into the database file.
1821 */
1822 for(i=0; i<nRec; i++){
danielk197762079062007-08-15 17:08:46 +00001823 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001824 if( rc!=SQLITE_OK ){
1825 if( rc==SQLITE_DONE ){
1826 rc = SQLITE_OK;
1827 pPager->journalOff = szJ;
1828 break;
1829 }else{
1830 goto end_playback;
1831 }
1832 }
drh968af522003-02-11 14:55:40 +00001833 }
drhed7c8552001-04-11 14:29:21 +00001834 }
drh580eeaf2006-02-24 03:09:37 +00001835 /*NOTREACHED*/
1836 assert( 0 );
drh4a0681e2003-02-13 01:58:20 +00001837
1838end_playback:
danielk19778191bff2004-06-28 04:52:30 +00001839 if( rc==SQLITE_OK ){
danielk197765839c62007-08-30 08:08:17 +00001840 zMaster = pPager->pTmpSpace;
1841 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
1842 }
1843 if( rc==SQLITE_OK ){
drh80e35f42007-03-30 14:06:34 +00001844 rc = pager_end_transaction(pPager);
danielk19778191bff2004-06-28 04:52:30 +00001845 }
danielk197765839c62007-08-30 08:08:17 +00001846 if( rc==SQLITE_OK && zMaster[0] ){
danielk1977979f38e2007-03-27 16:19:51 +00001847 /* If there was a master journal and this routine will return success,
danielk197732554c12005-01-22 03:39:39 +00001848 ** see if it is possible to delete the master journal.
danielk197713adf8a2004-06-03 16:08:41 +00001849 */
danielk197765839c62007-08-30 08:08:17 +00001850 rc = pager_delmaster(pPager, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001851 }
danielk197776572402004-06-25 02:38:54 +00001852
1853 /* The Pager.sectorSize variable may have been updated while rolling
drh3ceeb752007-03-29 18:19:52 +00001854 ** back a journal created by a process with a different sector size
danielk197776572402004-06-25 02:38:54 +00001855 ** value. Reset it to the correct value for this process.
1856 */
drhc80f0582007-05-01 16:59:48 +00001857 setSectorSize(pPager);
drhd9b02572001-04-15 00:37:09 +00001858 return rc;
drhed7c8552001-04-11 14:29:21 +00001859}
1860
1861/*
drhac69b052004-05-12 13:30:07 +00001862** Playback the statement journal.
drhfa86c412002-02-02 15:01:15 +00001863**
1864** This is similar to playing back the transaction journal but with
1865** a few extra twists.
1866**
drh663fc632002-02-02 18:49:19 +00001867** (1) The number of pages in the database file at the start of
drhac69b052004-05-12 13:30:07 +00001868** the statement is stored in pPager->stmtSize, not in the
drh663fc632002-02-02 18:49:19 +00001869** journal file itself.
drhfa86c412002-02-02 15:01:15 +00001870**
drhac69b052004-05-12 13:30:07 +00001871** (2) In addition to playing back the statement journal, also
drhfa86c412002-02-02 15:01:15 +00001872** playback all pages of the transaction journal beginning
drhac69b052004-05-12 13:30:07 +00001873** at offset pPager->stmtJSize.
drhfa86c412002-02-02 15:01:15 +00001874*/
drh3aac2dd2004-04-26 14:10:20 +00001875static int pager_stmt_playback(Pager *pPager){
drheb206252004-10-01 02:00:31 +00001876 i64 szJ; /* Size of the full journal */
1877 i64 hdrOff;
drh968af522003-02-11 14:55:40 +00001878 int nRec; /* Number of Records */
drhfa86c412002-02-02 15:01:15 +00001879 int i; /* Loop counter */
1880 int rc;
1881
danielk197776572402004-06-25 02:38:54 +00001882 szJ = pPager->journalOff;
1883#ifndef NDEBUG
1884 {
drheb206252004-10-01 02:00:31 +00001885 i64 os_szJ;
drh054889e2005-11-30 03:20:31 +00001886 rc = sqlite3OsFileSize(pPager->jfd, &os_szJ);
danielk197776572402004-06-25 02:38:54 +00001887 if( rc!=SQLITE_OK ) return rc;
1888 assert( szJ==os_szJ );
1889 }
1890#endif
1891
danielk19774099f6e2007-03-19 11:25:20 +00001892 /* Set hdrOff to be the offset just after the end of the last journal
1893 ** page written before the first journal-header for this statement
1894 ** transaction was written, or the end of the file if no journal
danielk197776572402004-06-25 02:38:54 +00001895 ** header was written.
1896 */
1897 hdrOff = pPager->stmtHdrOff;
1898 assert( pPager->fullSync || !hdrOff );
1899 if( !hdrOff ){
1900 hdrOff = szJ;
1901 }
1902
drhfa86c412002-02-02 15:01:15 +00001903 /* Truncate the database back to its original size.
1904 */
danielk1977e180dd92007-04-05 17:15:52 +00001905 rc = pager_truncate(pPager, pPager->stmtSize);
drh1aa2d8b2007-01-03 15:34:29 +00001906 assert( pPager->state>=PAGER_SHARED );
drhfa86c412002-02-02 15:01:15 +00001907
drhac69b052004-05-12 13:30:07 +00001908 /* Figure out how many records are in the statement journal.
drhfa86c412002-02-02 15:01:15 +00001909 */
drhac69b052004-05-12 13:30:07 +00001910 assert( pPager->stmtInUse && pPager->journalOpen );
drhac69b052004-05-12 13:30:07 +00001911 nRec = pPager->stmtNRec;
drhfa86c412002-02-02 15:01:15 +00001912
drhac69b052004-05-12 13:30:07 +00001913 /* Copy original pages out of the statement journal and back into the
drhae2b40c2004-06-09 19:03:54 +00001914 ** database file. Note that the statement journal omits checksums from
1915 ** each record since power-failure recovery is not important to statement
1916 ** journals.
drhfa86c412002-02-02 15:01:15 +00001917 */
danielk197762079062007-08-15 17:08:46 +00001918 for(i=0; i<nRec; i++){
1919 i64 offset = i*(4+pPager->pageSize);
1920 rc = pager_playback_one_page(pPager, pPager->stfd, offset, 0);
drh968af522003-02-11 14:55:40 +00001921 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001922 if( rc!=SQLITE_OK ) goto end_stmt_playback;
drhfa86c412002-02-02 15:01:15 +00001923 }
1924
danielk197776572402004-06-25 02:38:54 +00001925 /* Now roll some pages back from the transaction journal. Pager.stmtJSize
1926 ** was the size of the journal file when this statement was started, so
1927 ** everything after that needs to be rolled back, either into the
1928 ** database, the memory cache, or both.
1929 **
1930 ** If it is not zero, then Pager.stmtHdrOff is the offset to the start
1931 ** of the first journal header written during this statement transaction.
drhfa86c412002-02-02 15:01:15 +00001932 */
danielk197776572402004-06-25 02:38:54 +00001933 pPager->journalOff = pPager->stmtJSize;
danielk197775edc162004-06-26 01:48:18 +00001934 pPager->cksumInit = pPager->stmtCksum;
danielk19774099f6e2007-03-19 11:25:20 +00001935 while( pPager->journalOff < hdrOff ){
danielk197762079062007-08-15 17:08:46 +00001936 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001937 assert( rc!=SQLITE_DONE );
1938 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1939 }
1940
1941 while( pPager->journalOff < szJ ){
danielk1977f0113002006-01-24 12:09:17 +00001942 u32 nJRec; /* Number of Journal Records */
danielk197776572402004-06-25 02:38:54 +00001943 u32 dummy;
danielk1977f0113002006-01-24 12:09:17 +00001944 rc = readJournalHdr(pPager, szJ, &nJRec, &dummy);
drh968af522003-02-11 14:55:40 +00001945 if( rc!=SQLITE_OK ){
1946 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001947 goto end_stmt_playback;
drh968af522003-02-11 14:55:40 +00001948 }
danielk1977f0113002006-01-24 12:09:17 +00001949 if( nJRec==0 ){
1950 nJRec = (szJ - pPager->journalOff) / (pPager->pageSize+8);
danielk197775edc162004-06-26 01:48:18 +00001951 }
danielk1977f0113002006-01-24 12:09:17 +00001952 for(i=nJRec-1; i>=0 && pPager->journalOff < szJ; i--){
danielk197762079062007-08-15 17:08:46 +00001953 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001954 assert( rc!=SQLITE_DONE );
1955 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1956 }
drhfa86c412002-02-02 15:01:15 +00001957 }
danielk197776572402004-06-25 02:38:54 +00001958
1959 pPager->journalOff = szJ;
drhfa86c412002-02-02 15:01:15 +00001960
drh3aac2dd2004-04-26 14:10:20 +00001961end_stmt_playback:
danielk19778a7aea32006-01-23 15:25:48 +00001962 if( rc==SQLITE_OK) {
danielk197775edc162004-06-26 01:48:18 +00001963 pPager->journalOff = szJ;
1964 /* pager_reload_cache(pPager); */
drhfa86c412002-02-02 15:01:15 +00001965 }
1966 return rc;
1967}
1968
1969/*
drhf57b14a2001-09-14 18:54:08 +00001970** Change the maximum number of in-memory pages that are allowed.
1971*/
danielk19773b8a05f2007-03-19 17:44:26 +00001972void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
drhf57b14a2001-09-14 18:54:08 +00001973 if( mxPage>10 ){
1974 pPager->mxPage = mxPage;
danielk1977ef317ab2004-06-23 10:43:10 +00001975 }else{
1976 pPager->mxPage = 10;
drhf57b14a2001-09-14 18:54:08 +00001977 }
1978}
1979
1980/*
drh973b6e32003-02-12 14:09:42 +00001981** Adjust the robustness of the database to damage due to OS crashes
1982** or power failures by changing the number of syncs()s when writing
1983** the rollback journal. There are three levels:
1984**
drh054889e2005-11-30 03:20:31 +00001985** OFF sqlite3OsSync() is never called. This is the default
drh973b6e32003-02-12 14:09:42 +00001986** for temporary and transient files.
1987**
1988** NORMAL The journal is synced once before writes begin on the
1989** database. This is normally adequate protection, but
1990** it is theoretically possible, though very unlikely,
1991** that an inopertune power failure could leave the journal
1992** in a state which would cause damage to the database
1993** when it is rolled back.
1994**
1995** FULL The journal is synced twice before writes begin on the
drh34e79ce2004-02-08 06:05:46 +00001996** database (with some additional information - the nRec field
1997** of the journal header - being written in between the two
1998** syncs). If we assume that writing a
drh973b6e32003-02-12 14:09:42 +00001999** single disk sector is atomic, then this mode provides
2000** assurance that the journal will not be corrupted to the
2001** point of causing damage to the database during rollback.
2002**
2003** Numeric values associated with these states are OFF==1, NORMAL=2,
2004** and FULL=3.
2005*/
danielk197793758c82005-01-21 08:13:14 +00002006#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk19773b8a05f2007-03-19 17:44:26 +00002007void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int full_fsync){
drh973b6e32003-02-12 14:09:42 +00002008 pPager->noSync = level==1 || pPager->tempFile;
2009 pPager->fullSync = level==3 && !pPager->tempFile;
danielk1977f036aef2007-08-20 05:36:51 +00002010 pPager->sync_flags = (full_fsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL);
danielk19771d850a72004-05-31 08:26:49 +00002011 if( pPager->noSync ) pPager->needSync = 0;
drh973b6e32003-02-12 14:09:42 +00002012}
danielk197793758c82005-01-21 08:13:14 +00002013#endif
drh973b6e32003-02-12 14:09:42 +00002014
2015/*
drhaf6df112005-06-07 02:12:30 +00002016** The following global variable is incremented whenever the library
2017** attempts to open a temporary file. This information is used for
2018** testing and analysis only.
2019*/
drh0f7eb612006-08-08 13:51:43 +00002020#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00002021int sqlite3_opentemp_count = 0;
drh0f7eb612006-08-08 13:51:43 +00002022#endif
drhaf6df112005-06-07 02:12:30 +00002023
2024/*
drh3f56e6e2007-03-15 01:16:47 +00002025** Open a temporary file.
2026**
2027** Write the file descriptor into *fd. Return SQLITE_OK on success or some
danielk1977fee2d252007-08-18 10:59:19 +00002028** other error code if we fail. The OS will automatically delete the temporary
2029** file when it is closed.
drhfa86c412002-02-02 15:01:15 +00002030*/
danielk1977b4b47412007-08-17 15:53:36 +00002031static int sqlite3PagerOpentemp(
drh33f4e022007-09-03 15:19:34 +00002032 sqlite3_vfs *pVfs, /* The virtual file system layer */
2033 sqlite3_file *pFile, /* Write the file descriptor here */
2034 char *zFilename, /* Name of the file. Might be NULL */
2035 int vfsFlags /* Flags passed through to the VFS */
danielk1977b4b47412007-08-17 15:53:36 +00002036){
drhfa86c412002-02-02 15:01:15 +00002037 int rc;
drh334b2992007-09-06 23:28:23 +00002038 assert( zFilename!=0 );
drh3f56e6e2007-03-15 01:16:47 +00002039
drh0f7eb612006-08-08 13:51:43 +00002040#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00002041 sqlite3_opentemp_count++; /* Used for testing and analysis only */
drh0f7eb612006-08-08 13:51:43 +00002042#endif
danielk1977b4b47412007-08-17 15:53:36 +00002043
drh33f4e022007-09-03 15:19:34 +00002044 vfsFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
2045 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
2046 rc = sqlite3OsOpen(pVfs, zFilename, pFile, vfsFlags, 0);
drhae28c012007-08-24 16:29:23 +00002047 assert( rc!=SQLITE_OK || pFile->pMethods );
drhfa86c412002-02-02 15:01:15 +00002048 return rc;
2049}
2050
2051/*
drhed7c8552001-04-11 14:29:21 +00002052** Create a new page cache and put a pointer to the page cache in *ppPager.
drh5e00f6c2001-09-13 13:46:56 +00002053** The file to be cached need not exist. The file is not locked until
danielk19773b8a05f2007-03-19 17:44:26 +00002054** the first call to sqlite3PagerGet() and is only held open until the
2055** last page is released using sqlite3PagerUnref().
drh382c0242001-10-06 16:33:02 +00002056**
drh6446c4d2001-12-15 14:22:18 +00002057** If zFilename is NULL then a randomly-named temporary file is created
drh1cc8c442007-08-24 16:08:29 +00002058** and used as the file to be cached. The file will be deleted
drh6446c4d2001-12-15 14:22:18 +00002059** automatically when it is closed.
drh90f5ecb2004-07-22 01:19:35 +00002060**
2061** If zFilename is ":memory:" then all information is held in cache.
2062** It is never written to disk. This can be used to implement an
2063** in-memory database.
drhed7c8552001-04-11 14:29:21 +00002064*/
danielk19773b8a05f2007-03-19 17:44:26 +00002065int sqlite3PagerOpen(
drh86f8c192007-08-22 00:39:19 +00002066 sqlite3_vfs *pVfs, /* The virtual file system to use */
drh7e3b0a02001-04-28 16:52:40 +00002067 Pager **ppPager, /* Return the Pager structure here */
2068 const char *zFilename, /* Name of the database file to open */
drhda47d772002-12-02 04:25:19 +00002069 int nExtra, /* Extra bytes append to each in-memory page */
drh33f4e022007-09-03 15:19:34 +00002070 int flags, /* flags controlling this file */
2071 int vfsFlags /* flags passed through to sqlite3_vfs.xOpen() */
drh7e3b0a02001-04-28 16:52:40 +00002072){
danielk1977b4b47412007-08-17 15:53:36 +00002073 u8 *pPtr;
danielk1977aef0bf62005-12-30 16:28:01 +00002074 Pager *pPager = 0;
danielk1977cfe9a692004-06-16 12:00:29 +00002075 int rc = SQLITE_OK;
2076 int i;
danielk19778def5ea2004-06-16 10:39:52 +00002077 int tempFile = 0;
drhac69b052004-05-12 13:30:07 +00002078 int memDb = 0;
drh5e00f6c2001-09-13 13:46:56 +00002079 int readOnly = 0;
drh7bec5052005-02-06 02:45:41 +00002080 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0;
2081 int noReadlock = (flags & PAGER_NO_READLOCK)!=0;
danielk1977c7b60172007-08-22 11:22:03 +00002082 int journalFileSize = sqlite3JournalSize(pVfs);
drh1cc8c442007-08-24 16:08:29 +00002083 int nDefaultPage = SQLITE_DEFAULT_PAGE_SIZE;
2084 char *zPathname;
2085 int nPathname;
drh99b90c32008-03-17 13:50:58 +00002086 char *zStmtJrnl;
2087 int nStmtJrnl;
danielk1977b4b47412007-08-17 15:53:36 +00002088
drh86f8c192007-08-22 00:39:19 +00002089 /* The default return is a NULL pointer */
drhd9b02572001-04-15 00:37:09 +00002090 *ppPager = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00002091
drh1cc8c442007-08-24 16:08:29 +00002092 /* Compute the full pathname */
danielk1977adfb9b02007-09-17 07:02:56 +00002093 nPathname = pVfs->mxPathname+1;
drh99b90c32008-03-17 13:50:58 +00002094 zPathname = sqlite3_malloc(nPathname*2);
drh1cc8c442007-08-24 16:08:29 +00002095 if( zPathname==0 ){
2096 return SQLITE_NOMEM;
2097 }
2098 if( zFilename && zFilename[0] ){
2099#ifndef SQLITE_OMIT_MEMORYDB
2100 if( strcmp(zFilename,":memory:")==0 ){
2101 memDb = 1;
2102 zPathname[0] = 0;
2103 }else
2104#endif
2105 {
danielk1977adfb9b02007-09-17 07:02:56 +00002106 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
drh1cc8c442007-08-24 16:08:29 +00002107 }
2108 }else{
danielk1977adfb9b02007-09-17 07:02:56 +00002109 rc = sqlite3OsGetTempname(pVfs, nPathname, zPathname);
drhae28c012007-08-24 16:29:23 +00002110 }
2111 if( rc!=SQLITE_OK ){
2112 sqlite3_free(zPathname);
2113 return rc;
drh1cc8c442007-08-24 16:08:29 +00002114 }
2115 nPathname = strlen(zPathname);
2116
drh99b90c32008-03-17 13:50:58 +00002117 /* Put the statement journal in temporary disk space since this is
2118 ** sometimes RAM disk or other optimized storage. Unlikely the main
2119 ** main journal file, the statement journal does not need to be
2120 ** colocated with the database nor does it need to be persistent.
2121 */
2122 zStmtJrnl = &zPathname[nPathname+1];
2123 rc = sqlite3OsGetTempname(pVfs, pVfs->mxPathname+1, zStmtJrnl);
2124 if( rc!=SQLITE_OK ){
2125 sqlite3_free(zPathname);
2126 return rc;
2127 }
2128 nStmtJrnl = strlen(zStmtJrnl);
2129
danielk1977b4b47412007-08-17 15:53:36 +00002130 /* Allocate memory for the pager structure */
2131 pPager = sqlite3MallocZero(
2132 sizeof(*pPager) + /* Pager structure */
danielk1977c7b60172007-08-22 11:22:03 +00002133 journalFileSize + /* The journal file structure */
drh17c0ba22008-02-26 18:40:11 +00002134 pVfs->szOsFile * 3 + /* The main db and two journal files */
drh99b90c32008-03-17 13:50:58 +00002135 3*nPathname + 40 + /* zFilename, zDirectory, zJournal */
2136 nStmtJrnl /* zStmtJrnl */
danielk1977b4b47412007-08-17 15:53:36 +00002137 );
2138 if( !pPager ){
drh1cc8c442007-08-24 16:08:29 +00002139 sqlite3_free(zPathname);
danielk1977b4b47412007-08-17 15:53:36 +00002140 return SQLITE_NOMEM;
2141 }
2142 pPtr = (u8 *)&pPager[1];
drh33f4e022007-09-03 15:19:34 +00002143 pPager->vfsFlags = vfsFlags;
danielk1977b4b47412007-08-17 15:53:36 +00002144 pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0];
danielk1977c7b60172007-08-22 11:22:03 +00002145 pPager->stfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*1];
2146 pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*2];
2147 pPager->zFilename = (char*)&pPtr[pVfs->szOsFile*2+journalFileSize];
drh1cc8c442007-08-24 16:08:29 +00002148 pPager->zDirectory = &pPager->zFilename[nPathname+1];
2149 pPager->zJournal = &pPager->zDirectory[nPathname+1];
drh334b2992007-09-06 23:28:23 +00002150 pPager->zStmtJrnl = &pPager->zJournal[nPathname+10];
danielk1977b4b47412007-08-17 15:53:36 +00002151 pPager->pVfs = pVfs;
drh1cc8c442007-08-24 16:08:29 +00002152 memcpy(pPager->zFilename, zPathname, nPathname+1);
drh99b90c32008-03-17 13:50:58 +00002153 memcpy(pPager->zStmtJrnl, zStmtJrnl, nStmtJrnl+1);
drh1cc8c442007-08-24 16:08:29 +00002154 sqlite3_free(zPathname);
danielk1977b4b47412007-08-17 15:53:36 +00002155
drh153c62c2007-08-24 03:51:33 +00002156 /* Open the pager file.
danielk1977aef0bf62005-12-30 16:28:01 +00002157 */
drhae28c012007-08-24 16:29:23 +00002158 if( zFilename && zFilename[0] && !memDb ){
drh1cc8c442007-08-24 16:08:29 +00002159 if( nPathname>(pVfs->mxPathname - sizeof("-journal")) ){
2160 rc = SQLITE_CANTOPEN;
2161 }else{
drh1cc8c442007-08-24 16:08:29 +00002162 int fout = 0;
drh33f4e022007-09-03 15:19:34 +00002163 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd,
2164 pPager->vfsFlags, &fout);
drh1cc8c442007-08-24 16:08:29 +00002165 readOnly = (fout&SQLITE_OPEN_READONLY);
2166
2167 /* If the file was successfully opened for read/write access,
2168 ** choose a default page size in case we have to create the
2169 ** database file. The default page size is the maximum of:
2170 **
2171 ** + SQLITE_DEFAULT_PAGE_SIZE,
2172 ** + The value returned by sqlite3OsSectorSize()
2173 ** + The largest page size that can be written atomically.
2174 */
2175 if( rc==SQLITE_OK && !readOnly ){
2176 int iSectorSize = sqlite3OsSectorSize(pPager->fd);
2177 if( nDefaultPage<iSectorSize ){
2178 nDefaultPage = iSectorSize;
2179 }
danielk19779663b8f2007-08-24 11:52:28 +00002180#ifdef SQLITE_ENABLE_ATOMIC_WRITE
drh1cc8c442007-08-24 16:08:29 +00002181 {
2182 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
2183 int ii;
2184 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
2185 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
2186 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
2187 for(ii=nDefaultPage; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
2188 if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ) nDefaultPage = ii;
danielk19779663b8f2007-08-24 11:52:28 +00002189 }
danielk1977b4b47412007-08-17 15:53:36 +00002190 }
drh1cc8c442007-08-24 16:08:29 +00002191#endif
2192 if( nDefaultPage>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
2193 nDefaultPage = SQLITE_MAX_DEFAULT_PAGE_SIZE;
2194 }
danielk19778def5ea2004-06-16 10:39:52 +00002195 }
drhac69b052004-05-12 13:30:07 +00002196 }
drh1cc8c442007-08-24 16:08:29 +00002197 }else if( !memDb ){
danielk19777a2b1ee2007-08-21 14:27:01 +00002198 /* If a temporary file is requested, it is not opened immediately.
2199 ** In this case we accept the default page size and delay actually
2200 ** opening the file until the first call to OsWrite().
2201 */
danielk19777a2b1ee2007-08-21 14:27:01 +00002202 tempFile = 1;
2203 pPager->state = PAGER_EXCLUSIVE;
drh5e00f6c2001-09-13 13:46:56 +00002204 }
danielk1977aef0bf62005-12-30 16:28:01 +00002205
danielk1977b4b47412007-08-17 15:53:36 +00002206 if( pPager && rc==SQLITE_OK ){
danielk19779663b8f2007-08-24 11:52:28 +00002207 pPager->pTmpSpace = (char *)sqlite3_malloc(nDefaultPage);
drh3e7a6092002-12-07 21:45:14 +00002208 }
danielk1977aef0bf62005-12-30 16:28:01 +00002209
drh153c62c2007-08-24 03:51:33 +00002210 /* If an error occured in either of the blocks above.
2211 ** Free the Pager structure and close the file.
2212 ** Since the pager is not allocated there is no need to set
danielk1977aef0bf62005-12-30 16:28:01 +00002213 ** any Pager.errMask variables.
2214 */
danielk1977b4b47412007-08-17 15:53:36 +00002215 if( !pPager || !pPager->pTmpSpace ){
2216 sqlite3OsClose(pPager->fd);
drh17435752007-08-16 04:30:38 +00002217 sqlite3_free(pPager);
danielk1977aef0bf62005-12-30 16:28:01 +00002218 return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc);
drhed7c8552001-04-11 14:29:21 +00002219 }
danielk1977aef0bf62005-12-30 16:28:01 +00002220
drh153c62c2007-08-24 03:51:33 +00002221 PAGERTRACE3("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename);
2222 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
danielk1977aef0bf62005-12-30 16:28:01 +00002223
danielk1977b4b47412007-08-17 15:53:36 +00002224 /* Fill in Pager.zDirectory[] */
drh1cc8c442007-08-24 16:08:29 +00002225 memcpy(pPager->zDirectory, pPager->zFilename, nPathname+1);
danielk1977b4b47412007-08-17 15:53:36 +00002226 for(i=strlen(pPager->zDirectory); i>0 && pPager->zDirectory[i-1]!='/'; i--){}
drha76c82e2003-07-27 18:59:42 +00002227 if( i>0 ) pPager->zDirectory[i-1] = 0;
danielk1977b4b47412007-08-17 15:53:36 +00002228
drh99b90c32008-03-17 13:50:58 +00002229 /* Fill in Pager.zJournal[] */
drh1cc8c442007-08-24 16:08:29 +00002230 memcpy(pPager->zJournal, pPager->zFilename, nPathname);
2231 memcpy(&pPager->zJournal[nPathname], "-journal", 9);
danielk1977b4b47412007-08-17 15:53:36 +00002232
drh3b59a5c2006-01-15 20:28:28 +00002233 /* pPager->journalOpen = 0; */
drhac69b052004-05-12 13:30:07 +00002234 pPager->useJournal = useJournal && !memDb;
drh7bec5052005-02-06 02:45:41 +00002235 pPager->noReadlock = noReadlock && readOnly;
drh3b59a5c2006-01-15 20:28:28 +00002236 /* pPager->stmtOpen = 0; */
2237 /* pPager->stmtInUse = 0; */
2238 /* pPager->nRef = 0; */
drhac69b052004-05-12 13:30:07 +00002239 pPager->dbSize = memDb-1;
danielk19779663b8f2007-08-24 11:52:28 +00002240 pPager->pageSize = nDefaultPage;
drh3b59a5c2006-01-15 20:28:28 +00002241 /* pPager->stmtSize = 0; */
2242 /* pPager->stmtJSize = 0; */
2243 /* pPager->nPage = 0; */
drh90f5ecb2004-07-22 01:19:35 +00002244 pPager->mxPage = 100;
drhf8e632b2007-05-08 14:51:36 +00002245 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
drh3b59a5c2006-01-15 20:28:28 +00002246 /* pPager->state = PAGER_UNLOCK; */
drh1cc8c442007-08-24 16:08:29 +00002247 assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
drh3b59a5c2006-01-15 20:28:28 +00002248 /* pPager->errMask = 0; */
drh5e00f6c2001-09-13 13:46:56 +00002249 pPager->tempFile = tempFile;
drh369339d2007-03-30 16:01:55 +00002250 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
2251 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
2252 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
2253 pPager->exclusiveMode = tempFile;
drhac69b052004-05-12 13:30:07 +00002254 pPager->memDb = memDb;
drh5e00f6c2001-09-13 13:46:56 +00002255 pPager->readOnly = readOnly;
drh3b59a5c2006-01-15 20:28:28 +00002256 /* pPager->needSync = 0; */
drhda47d772002-12-02 04:25:19 +00002257 pPager->noSync = pPager->tempFile || !useJournal;
danielk197775edc162004-06-26 01:48:18 +00002258 pPager->fullSync = (pPager->noSync?0:1);
danielk1977f036aef2007-08-20 05:36:51 +00002259 pPager->sync_flags = SQLITE_SYNC_NORMAL;
drh3b59a5c2006-01-15 20:28:28 +00002260 /* pPager->pFirst = 0; */
2261 /* pPager->pFirstSynced = 0; */
2262 /* pPager->pLast = 0; */
drh887dc4c2004-10-22 16:22:57 +00002263 pPager->nExtra = FORCE_ALIGNMENT(nExtra);
danielk19777a2b1ee2007-08-21 14:27:01 +00002264 assert(pPager->fd->pMethods||memDb||tempFile);
danielk1977b4721172007-03-19 05:54:48 +00002265 if( !memDb ){
drhc80f0582007-05-01 16:59:48 +00002266 setSectorSize(pPager);
danielk1977b4721172007-03-19 05:54:48 +00002267 }
drh3b59a5c2006-01-15 20:28:28 +00002268 /* pPager->pBusyHandler = 0; */
2269 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
drhed7c8552001-04-11 14:29:21 +00002270 *ppPager = pPager;
drh6f7adc82006-01-11 21:41:20 +00002271#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +00002272 pPager->iInUseMM = 0;
2273 pPager->iInUseDB = 0;
2274 if( !memDb ){
2275 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
2276 sqlite3_mutex_enter(mutex);
2277 pPager->pNext = sqlite3PagerList;
2278 if( sqlite3PagerList ){
2279 assert( sqlite3PagerList->pPrev==0 );
2280 sqlite3PagerList->pPrev = pPager;
2281 }
2282 pPager->pPrev = 0;
2283 sqlite3PagerList = pPager;
2284 sqlite3_mutex_leave(mutex);
2285 }
danielk197752622822006-01-09 09:59:49 +00002286#endif
drhed7c8552001-04-11 14:29:21 +00002287 return SQLITE_OK;
2288}
2289
2290/*
drh90f5ecb2004-07-22 01:19:35 +00002291** Set the busy handler function.
2292*/
danielk19773b8a05f2007-03-19 17:44:26 +00002293void sqlite3PagerSetBusyhandler(Pager *pPager, BusyHandler *pBusyHandler){
drh90f5ecb2004-07-22 01:19:35 +00002294 pPager->pBusyHandler = pBusyHandler;
2295}
2296
2297/*
drh72f82862001-05-24 21:06:34 +00002298** Set the destructor for this pager. If not NULL, the destructor is called
drh5e00f6c2001-09-13 13:46:56 +00002299** when the reference count on each page reaches zero. The destructor can
2300** be used to clean up information in the extra segment appended to each page.
drh72f82862001-05-24 21:06:34 +00002301**
danielk19773b8a05f2007-03-19 17:44:26 +00002302** The destructor is not called as a result sqlite3PagerClose().
2303** Destructors are only called by sqlite3PagerUnref().
drh72f82862001-05-24 21:06:34 +00002304*/
danielk19773b8a05f2007-03-19 17:44:26 +00002305void sqlite3PagerSetDestructor(Pager *pPager, void (*xDesc)(DbPage*,int)){
drh72f82862001-05-24 21:06:34 +00002306 pPager->xDestructor = xDesc;
2307}
2308
2309/*
drha6abd042004-06-09 17:37:22 +00002310** Set the reinitializer for this pager. If not NULL, the reinitializer
2311** is called when the content of a page in cache is restored to its original
2312** value as a result of a rollback. The callback gives higher-level code
2313** an opportunity to restore the EXTRA section to agree with the restored
2314** page data.
2315*/
danielk19773b8a05f2007-03-19 17:44:26 +00002316void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*,int)){
drha6abd042004-06-09 17:37:22 +00002317 pPager->xReiniter = xReinit;
2318}
2319
2320/*
danielk1977a1644fd2007-08-29 12:31:25 +00002321** Set the page size to *pPageSize. If the suggest new page size is
2322** inappropriate, then an alternative page size is set to that
2323** value before returning.
drh90f5ecb2004-07-22 01:19:35 +00002324*/
danielk1977a1644fd2007-08-29 12:31:25 +00002325int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){
2326 int rc = SQLITE_OK;
2327 u16 pageSize = *pPageSize;
danielk19779663b8f2007-08-24 11:52:28 +00002328 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
danielk1977a1644fd2007-08-29 12:31:25 +00002329 if( pageSize && pageSize!=pPager->pageSize
2330 && !pPager->memDb && pPager->nRef==0
2331 ){
2332 char *pNew = (char *)sqlite3_malloc(pageSize);
2333 if( !pNew ){
2334 rc = SQLITE_NOMEM;
2335 }else{
2336 pagerEnter(pPager);
2337 pager_reset(pPager);
2338 pPager->pageSize = pageSize;
2339 setSectorSize(pPager);
2340 sqlite3_free(pPager->pTmpSpace);
2341 pPager->pTmpSpace = pNew;
2342 pagerLeave(pPager);
2343 }
drh1c7880e2005-05-20 20:01:55 +00002344 }
danielk1977a1644fd2007-08-29 12:31:25 +00002345 *pPageSize = pPager->pageSize;
2346 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002347}
2348
2349/*
drh26b79942007-11-28 16:19:56 +00002350** Return a pointer to the "temporary page" buffer held internally
2351** by the pager. This is a buffer that is big enough to hold the
2352** entire content of a database page. This buffer is used internally
2353** during rollback and will be overwritten whenever a rollback
2354** occurs. But other modules are free to use it too, as long as
2355** no rollbacks are happening.
2356*/
2357void *sqlite3PagerTempSpace(Pager *pPager){
2358 return pPager->pTmpSpace;
2359}
2360
2361/*
drhf8e632b2007-05-08 14:51:36 +00002362** Attempt to set the maximum database page count if mxPage is positive.
2363** Make no changes if mxPage is zero or negative. And never reduce the
2364** maximum page count below the current size of the database.
2365**
2366** Regardless of mxPage, return the current maximum page count.
2367*/
2368int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
2369 if( mxPage>0 ){
2370 pPager->mxPgno = mxPage;
2371 }
2372 sqlite3PagerPagecount(pPager);
2373 return pPager->mxPgno;
2374}
2375
2376/*
drhc9ac5ca2005-11-04 22:03:30 +00002377** The following set of routines are used to disable the simulated
2378** I/O error mechanism. These routines are used to avoid simulated
2379** errors in places where we do not care about errors.
2380**
2381** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
2382** and generate no code.
2383*/
2384#ifdef SQLITE_TEST
2385extern int sqlite3_io_error_pending;
2386extern int sqlite3_io_error_hit;
2387static int saved_cnt;
drhc9ac5ca2005-11-04 22:03:30 +00002388void disable_simulated_io_errors(void){
2389 saved_cnt = sqlite3_io_error_pending;
2390 sqlite3_io_error_pending = -1;
2391}
2392void enable_simulated_io_errors(void){
2393 sqlite3_io_error_pending = saved_cnt;
2394}
2395#else
drh152410f2005-11-05 15:11:22 +00002396# define disable_simulated_io_errors()
2397# define enable_simulated_io_errors()
drhc9ac5ca2005-11-04 22:03:30 +00002398#endif
2399
2400/*
drh90f5ecb2004-07-22 01:19:35 +00002401** Read the first N bytes from the beginning of the file into memory
danielk1977aef0bf62005-12-30 16:28:01 +00002402** that pDest points to.
2403**
2404** No error checking is done. The rational for this is that this function
2405** may be called even if the file does not exist or contain a header. In
2406** these cases sqlite3OsRead() will return an error, to which the correct
2407** response is to zero the memory at pDest and continue. A real IO error
2408** will presumably recur and be picked up later (Todo: Think about this).
drh90f5ecb2004-07-22 01:19:35 +00002409*/
danielk19773b8a05f2007-03-19 17:44:26 +00002410int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
drh551b7732006-11-06 21:20:25 +00002411 int rc = SQLITE_OK;
drh90f5ecb2004-07-22 01:19:35 +00002412 memset(pDest, 0, N);
danielk19777a2b1ee2007-08-21 14:27:01 +00002413 assert(MEMDB||pPager->fd->pMethods||pPager->tempFile);
2414 if( pPager->fd->pMethods ){
drhb0603412007-02-28 04:47:26 +00002415 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
danielk197762079062007-08-15 17:08:46 +00002416 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
drh551b7732006-11-06 21:20:25 +00002417 if( rc==SQLITE_IOERR_SHORT_READ ){
2418 rc = SQLITE_OK;
2419 }
drh90f5ecb2004-07-22 01:19:35 +00002420 }
drh551b7732006-11-06 21:20:25 +00002421 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002422}
2423
2424/*
drh5e00f6c2001-09-13 13:46:56 +00002425** Return the total number of pages in the disk file associated with
danielk197715f411d2005-09-16 10:18:45 +00002426** pPager.
2427**
2428** If the PENDING_BYTE lies on the page directly after the end of the
2429** file, then consider this page part of the file too. For example, if
2430** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the
2431** file is 4096 bytes, 5 is returned instead of 4.
drhed7c8552001-04-11 14:29:21 +00002432*/
danielk19773b8a05f2007-03-19 17:44:26 +00002433int sqlite3PagerPagecount(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00002434 i64 n = 0;
drhe49f9822006-09-15 12:29:16 +00002435 int rc;
drhd9b02572001-04-15 00:37:09 +00002436 assert( pPager!=0 );
drha7aea3d2007-03-15 12:51:16 +00002437 if( pPager->errCode ){
danielk197743468d92008-02-26 06:05:31 +00002438 return -1;
drha7aea3d2007-03-15 12:51:16 +00002439 }
drhed7c8552001-04-11 14:29:21 +00002440 if( pPager->dbSize>=0 ){
danielk197715f411d2005-09-16 10:18:45 +00002441 n = pPager->dbSize;
2442 } else {
danielk19777a2b1ee2007-08-21 14:27:01 +00002443 assert(pPager->fd->pMethods||pPager->tempFile);
2444 if( (pPager->fd->pMethods)
2445 && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){
danielk1977ae72d982007-10-03 08:46:44 +00002446 pPager->nRef++;
drhe49f9822006-09-15 12:29:16 +00002447 pager_error(pPager, rc);
danielk1977ae72d982007-10-03 08:46:44 +00002448 pPager->nRef--;
danielk197743468d92008-02-26 06:05:31 +00002449 return -1;
danielk197715f411d2005-09-16 10:18:45 +00002450 }
2451 if( n>0 && n<pPager->pageSize ){
2452 n = 1;
2453 }else{
2454 n /= pPager->pageSize;
2455 }
2456 if( pPager->state!=PAGER_UNLOCK ){
2457 pPager->dbSize = n;
2458 }
drhed7c8552001-04-11 14:29:21 +00002459 }
danielk197715f411d2005-09-16 10:18:45 +00002460 if( n==(PENDING_BYTE/pPager->pageSize) ){
drh1f595712004-06-15 01:40:29 +00002461 n++;
2462 }
drhf8e632b2007-05-08 14:51:36 +00002463 if( n>pPager->mxPgno ){
2464 pPager->mxPgno = n;
2465 }
drhed7c8552001-04-11 14:29:21 +00002466 return n;
2467}
2468
drhf07e7d52006-04-07 13:54:46 +00002469
2470#ifndef SQLITE_OMIT_MEMORYDB
2471/*
2472** Clear a PgHistory block
2473*/
2474static void clearHistory(PgHistory *pHist){
drh17435752007-08-16 04:30:38 +00002475 sqlite3_free(pHist->pOrig);
2476 sqlite3_free(pHist->pStmt);
drhf07e7d52006-04-07 13:54:46 +00002477 pHist->pOrig = 0;
2478 pHist->pStmt = 0;
2479}
2480#else
2481#define clearHistory(x)
2482#endif
2483
drhed7c8552001-04-11 14:29:21 +00002484/*
drhf7c57532003-04-25 13:22:51 +00002485** Forward declaration
2486*/
danielk197776572402004-06-25 02:38:54 +00002487static int syncJournal(Pager*);
drhac69b052004-05-12 13:30:07 +00002488
2489/*
drh85b623f2007-12-13 21:54:09 +00002490** Unlink pPg from its hash chain. Also set the page number to 0 to indicate
danielk1977f5fdda82004-11-03 08:44:05 +00002491** that the page is not part of any hash chain. This is required because the
danielk19773b8a05f2007-03-19 17:44:26 +00002492** sqlite3PagerMovepage() routine can leave a page in the
danielk1977f5fdda82004-11-03 08:44:05 +00002493** pNextFree/pPrevFree list that is not a part of any hash-chain.
2494*/
2495static void unlinkHashChain(Pager *pPager, PgHdr *pPg){
2496 if( pPg->pgno==0 ){
drh3765df42006-06-28 18:18:09 +00002497 assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
danielk1977f5fdda82004-11-03 08:44:05 +00002498 return;
2499 }
2500 if( pPg->pNextHash ){
2501 pPg->pNextHash->pPrevHash = pPg->pPrevHash;
2502 }
2503 if( pPg->pPrevHash ){
drh8ca0c722006-05-07 17:49:38 +00002504 assert( pPager->aHash[pPg->pgno & (pPager->nHash-1)]!=pPg );
danielk1977f5fdda82004-11-03 08:44:05 +00002505 pPg->pPrevHash->pNextHash = pPg->pNextHash;
2506 }else{
drh8ca0c722006-05-07 17:49:38 +00002507 int h = pPg->pgno & (pPager->nHash-1);
danielk1977f5fdda82004-11-03 08:44:05 +00002508 pPager->aHash[h] = pPg->pNextHash;
2509 }
drh5229ae42006-03-23 23:29:04 +00002510 if( MEMDB ){
drh5229ae42006-03-23 23:29:04 +00002511 clearHistory(PGHDR_TO_HIST(pPg, pPager));
2512 }
danielk1977f5fdda82004-11-03 08:44:05 +00002513 pPg->pgno = 0;
2514 pPg->pNextHash = pPg->pPrevHash = 0;
2515}
2516
2517/*
drhac69b052004-05-12 13:30:07 +00002518** Unlink a page from the free list (the list of all pages where nRef==0)
2519** and from its hash collision chain.
2520*/
2521static void unlinkPage(PgHdr *pPg){
2522 Pager *pPager = pPg->pPager;
2523
danielk19779f61c2f2007-08-27 17:27:49 +00002524 /* Unlink from free page list */
2525 lruListRemove(pPg);
drhac69b052004-05-12 13:30:07 +00002526
2527 /* Unlink from the pgno hash table */
danielk1977f5fdda82004-11-03 08:44:05 +00002528 unlinkHashChain(pPager, pPg);
drhac69b052004-05-12 13:30:07 +00002529}
2530
2531/*
danielk1977e180dd92007-04-05 17:15:52 +00002532** This routine is used to truncate the cache when a database
2533** is truncated. Drop from the cache all pages whose pgno is
2534** larger than pPager->dbSize and is unreferenced.
2535**
drhac69b052004-05-12 13:30:07 +00002536** Referenced pages larger than pPager->dbSize are zeroed.
danielk1977e180dd92007-04-05 17:15:52 +00002537**
2538** Actually, at the point this routine is called, it would be
2539** an error to have a referenced page. But rather than delete
2540** that page and guarantee a subsequent segfault, it seems better
2541** to zero it and hope that we error out sanely.
drhac69b052004-05-12 13:30:07 +00002542*/
danielk1977e180dd92007-04-05 17:15:52 +00002543static void pager_truncate_cache(Pager *pPager){
drhac69b052004-05-12 13:30:07 +00002544 PgHdr *pPg;
2545 PgHdr **ppPg;
2546 int dbSize = pPager->dbSize;
2547
2548 ppPg = &pPager->pAll;
2549 while( (pPg = *ppPg)!=0 ){
2550 if( pPg->pgno<=dbSize ){
2551 ppPg = &pPg->pNextAll;
2552 }else if( pPg->nRef>0 ){
2553 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
2554 ppPg = &pPg->pNextAll;
2555 }else{
2556 *ppPg = pPg->pNextAll;
drh538f5702007-04-13 02:14:30 +00002557 IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
2558 PAGER_INCR(sqlite3_pager_pgfree_count);
drhac69b052004-05-12 13:30:07 +00002559 unlinkPage(pPg);
drh605ad8f2006-05-03 23:34:05 +00002560 makeClean(pPg);
drh0d180202008-02-14 23:26:56 +00002561 sqlite3_free(pPg->pData);
drh17435752007-08-16 04:30:38 +00002562 sqlite3_free(pPg);
drhac69b052004-05-12 13:30:07 +00002563 pPager->nPage--;
2564 }
2565 }
2566}
2567
drhf7c57532003-04-25 13:22:51 +00002568/*
danielk197717221812005-02-15 03:38:05 +00002569** Try to obtain a lock on a file. Invoke the busy callback if the lock
drha4afb652005-07-09 02:16:02 +00002570** is currently not available. Repeat until the busy callback returns
danielk197717221812005-02-15 03:38:05 +00002571** false or until the lock succeeds.
2572**
2573** Return SQLITE_OK on success and an error code if we cannot obtain
2574** the lock.
2575*/
2576static int pager_wait_on_lock(Pager *pPager, int locktype){
2577 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002578
2579 /* The OS lock values must be the same as the Pager lock values */
danielk197717221812005-02-15 03:38:05 +00002580 assert( PAGER_SHARED==SHARED_LOCK );
2581 assert( PAGER_RESERVED==RESERVED_LOCK );
2582 assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
drh1aa2d8b2007-01-03 15:34:29 +00002583
2584 /* If the file is currently unlocked then the size must be unknown */
2585 assert( pPager->state>=PAGER_SHARED || pPager->dbSize<0 || MEMDB );
2586
danielk197717221812005-02-15 03:38:05 +00002587 if( pPager->state>=locktype ){
2588 rc = SQLITE_OK;
2589 }else{
drhbefdf832008-03-14 19:33:05 +00002590 if( pPager->pBusyHandler ) pPager->pBusyHandler->nBusy = 0;
danielk197717221812005-02-15 03:38:05 +00002591 do {
drh054889e2005-11-30 03:20:31 +00002592 rc = sqlite3OsLock(pPager->fd, locktype);
drha4afb652005-07-09 02:16:02 +00002593 }while( rc==SQLITE_BUSY && sqlite3InvokeBusyHandler(pPager->pBusyHandler) );
danielk197717221812005-02-15 03:38:05 +00002594 if( rc==SQLITE_OK ){
2595 pPager->state = locktype;
drhb0603412007-02-28 04:47:26 +00002596 IOTRACE(("LOCK %p %d\n", pPager, locktype))
danielk197717221812005-02-15 03:38:05 +00002597 }
2598 }
2599 return rc;
2600}
2601
2602/*
drhf7c57532003-04-25 13:22:51 +00002603** Truncate the file to the number of pages specified.
2604*/
danielk19773b8a05f2007-03-19 17:44:26 +00002605int sqlite3PagerTruncate(Pager *pPager, Pgno nPage){
drhf7c57532003-04-25 13:22:51 +00002606 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002607 assert( pPager->state>=PAGER_SHARED || MEMDB );
danielk19773b8a05f2007-03-19 17:44:26 +00002608 sqlite3PagerPagecount(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00002609 if( pPager->errCode ){
2610 rc = pPager->errCode;
drh2e6d11b2003-04-25 15:37:57 +00002611 return rc;
2612 }
drh7d02cb72003-06-04 16:24:39 +00002613 if( nPage>=(unsigned)pPager->dbSize ){
drhf7c57532003-04-25 13:22:51 +00002614 return SQLITE_OK;
2615 }
drhb7f91642004-10-31 02:22:47 +00002616 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00002617 pPager->dbSize = nPage;
danielk1977e180dd92007-04-05 17:15:52 +00002618 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00002619 return SQLITE_OK;
2620 }
drh86f8c192007-08-22 00:39:19 +00002621 pagerEnter(pPager);
danielk197776572402004-06-25 02:38:54 +00002622 rc = syncJournal(pPager);
drh86f8c192007-08-22 00:39:19 +00002623 pagerLeave(pPager);
danielk1977369f27e2004-06-15 11:40:04 +00002624 if( rc!=SQLITE_OK ){
2625 return rc;
2626 }
danielk197717221812005-02-15 03:38:05 +00002627
2628 /* Get an exclusive lock on the database before truncating. */
drh86f8c192007-08-22 00:39:19 +00002629 pagerEnter(pPager);
danielk197717221812005-02-15 03:38:05 +00002630 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
drh86f8c192007-08-22 00:39:19 +00002631 pagerLeave(pPager);
danielk197717221812005-02-15 03:38:05 +00002632 if( rc!=SQLITE_OK ){
2633 return rc;
2634 }
2635
drhcb4c40b2004-08-18 19:09:43 +00002636 rc = pager_truncate(pPager, nPage);
drhf7c57532003-04-25 13:22:51 +00002637 return rc;
2638}
2639
2640/*
drhed7c8552001-04-11 14:29:21 +00002641** Shutdown the page cache. Free all memory and close all files.
2642**
2643** If a transaction was in progress when this routine is called, that
2644** transaction is rolled back. All outstanding pages are invalidated
2645** and their memory is freed. Any attempt to use a page associated
2646** with this page cache after this function returns will likely
2647** result in a coredump.
danielk1977aef0bf62005-12-30 16:28:01 +00002648**
2649** This function always succeeds. If a transaction is active an attempt
2650** is made to roll it back. If an error occurs during the rollback
2651** a hot journal may be left in the filesystem but no error is returned
2652** to the caller.
drhed7c8552001-04-11 14:29:21 +00002653*/
danielk19773b8a05f2007-03-19 17:44:26 +00002654int sqlite3PagerClose(Pager *pPager){
drh6f7adc82006-01-11 21:41:20 +00002655#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +00002656 if( !MEMDB ){
2657 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
2658 sqlite3_mutex_enter(mutex);
2659 if( pPager->pPrev ){
2660 pPager->pPrev->pNext = pPager->pNext;
2661 }else{
2662 sqlite3PagerList = pPager->pNext;
2663 }
2664 if( pPager->pNext ){
2665 pPager->pNext->pPrev = pPager->pPrev;
2666 }
2667 sqlite3_mutex_leave(mutex);
2668 }
danielk197713f72992005-12-18 08:51:22 +00002669#endif
2670
drhbafda092007-01-03 23:36:22 +00002671 disable_simulated_io_errors();
drhc2ee76c2007-01-04 14:58:14 +00002672 pPager->errCode = 0;
danielk197741483462007-03-24 16:45:04 +00002673 pPager->exclusiveMode = 0;
drhbafda092007-01-03 23:36:22 +00002674 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00002675 pagerUnlockAndRollback(pPager);
drhbafda092007-01-03 23:36:22 +00002676 enable_simulated_io_errors();
drh4f0c5872007-03-26 22:05:01 +00002677 PAGERTRACE2("CLOSE %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002678 IOTRACE(("CLOSE %p\n", pPager))
danielk1977efaaf572006-01-16 11:29:19 +00002679 assert( pPager->errCode || (pPager->journalOpen==0 && pPager->stmtOpen==0) );
danielk1977e94ddc92005-03-21 03:53:38 +00002680 if( pPager->journalOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002681 sqlite3OsClose(pPager->jfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002682 }
drhf5e7bb52008-02-18 14:47:33 +00002683 sqlite3BitvecDestroy(pPager->pInJournal);
danielk1977e94ddc92005-03-21 03:53:38 +00002684 if( pPager->stmtOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002685 sqlite3OsClose(pPager->stfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002686 }
danielk1977b4b47412007-08-17 15:53:36 +00002687 sqlite3OsClose(pPager->fd);
drh0f892532002-05-30 12:27:03 +00002688 /* Temp files are automatically deleted by the OS
2689 ** if( pPager->tempFile ){
drh66560ad2006-01-06 14:32:19 +00002690 ** sqlite3OsDelete(pPager->zFilename);
drh0f892532002-05-30 12:27:03 +00002691 ** }
2692 */
danielk1977aca790a2005-01-13 11:07:52 +00002693
drh17435752007-08-16 04:30:38 +00002694 sqlite3_free(pPager->aHash);
2695 sqlite3_free(pPager->pTmpSpace);
2696 sqlite3_free(pPager);
drhed7c8552001-04-11 14:29:21 +00002697 return SQLITE_OK;
2698}
2699
drh87cc3b32007-05-08 21:45:27 +00002700#if !defined(NDEBUG) || defined(SQLITE_TEST)
drhed7c8552001-04-11 14:29:21 +00002701/*
drh5e00f6c2001-09-13 13:46:56 +00002702** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +00002703*/
danielk19773b8a05f2007-03-19 17:44:26 +00002704Pgno sqlite3PagerPagenumber(DbPage *p){
drhed7c8552001-04-11 14:29:21 +00002705 return p->pgno;
2706}
drh87cc3b32007-05-08 21:45:27 +00002707#endif
drhed7c8552001-04-11 14:29:21 +00002708
2709/*
drhc8629a12004-05-08 20:07:40 +00002710** The page_ref() function increments the reference count for a page.
2711** If the page is currently on the freelist (the reference count is zero) then
drh7e3b0a02001-04-28 16:52:40 +00002712** remove it from the freelist.
drhc8629a12004-05-08 20:07:40 +00002713**
2714** For non-test systems, page_ref() is a macro that calls _page_ref()
2715** online of the reference count is zero. For test systems, page_ref()
2716** is a real function so that we can set breakpoints and trace it.
drh7e3b0a02001-04-28 16:52:40 +00002717*/
drh836faa42003-01-11 13:30:57 +00002718static void _page_ref(PgHdr *pPg){
drh7e3b0a02001-04-28 16:52:40 +00002719 if( pPg->nRef==0 ){
2720 /* The page is currently on the freelist. Remove it. */
danielk19779f61c2f2007-08-27 17:27:49 +00002721 lruListRemove(pPg);
drh7e3b0a02001-04-28 16:52:40 +00002722 pPg->pPager->nRef++;
2723 }
2724 pPg->nRef++;
drhdf0b3b02001-06-23 11:36:20 +00002725}
danielk1977aca790a2005-01-13 11:07:52 +00002726#ifdef SQLITE_DEBUG
drhc8629a12004-05-08 20:07:40 +00002727 static void page_ref(PgHdr *pPg){
2728 if( pPg->nRef==0 ){
2729 _page_ref(pPg);
2730 }else{
2731 pPg->nRef++;
drhc8629a12004-05-08 20:07:40 +00002732 }
2733 }
2734#else
2735# define page_ref(P) ((P)->nRef==0?_page_ref(P):(void)(P)->nRef++)
2736#endif
drhdf0b3b02001-06-23 11:36:20 +00002737
2738/*
2739** Increment the reference count for a page. The input pointer is
2740** a reference to the page data.
2741*/
danielk19773b8a05f2007-03-19 17:44:26 +00002742int sqlite3PagerRef(DbPage *pPg){
drh86f8c192007-08-22 00:39:19 +00002743 pagerEnter(pPg->pPager);
drhdf0b3b02001-06-23 11:36:20 +00002744 page_ref(pPg);
drh86f8c192007-08-22 00:39:19 +00002745 pagerLeave(pPg->pPager);
drh8c42ca92001-06-22 19:15:00 +00002746 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00002747}
2748
2749/*
drh34e79ce2004-02-08 06:05:46 +00002750** Sync the journal. In other words, make sure all the pages that have
2751** been written to the journal have actually reached the surface of the
2752** disk. It is not safe to modify the original database file until after
2753** the journal has been synced. If the original database is modified before
2754** the journal is synced and a power failure occurs, the unsynced journal
2755** data would be lost and we would be unable to completely rollback the
2756** database changes. Database corruption would occur.
2757**
2758** This routine also updates the nRec field in the header of the journal.
2759** (See comments on the pager_playback() routine for additional information.)
2760** If the sync mode is FULL, two syncs will occur. First the whole journal
2761** is synced, then the nRec field is updated, then a second sync occurs.
drhb19a2bc2001-09-16 00:13:26 +00002762**
drh34e79ce2004-02-08 06:05:46 +00002763** For temporary databases, we do not care if we are able to rollback
danielk19774cd2cd52007-08-23 14:48:23 +00002764** after a power failure, so no sync occurs.
2765**
2766** If the IOCAP_SEQUENTIAL flag is set for the persistent media on which
2767** the database is stored, then OsSync() is never called on the journal
2768** file. In this case all that is required is to update the nRec field in
2769** the journal header.
drhfa86c412002-02-02 15:01:15 +00002770**
drh34e79ce2004-02-08 06:05:46 +00002771** This routine clears the needSync field of every page current held in
2772** memory.
drh50e5dad2001-09-15 00:57:28 +00002773*/
danielk197776572402004-06-25 02:38:54 +00002774static int syncJournal(Pager *pPager){
drh50e5dad2001-09-15 00:57:28 +00002775 PgHdr *pPg;
2776 int rc = SQLITE_OK;
drh03eb96a2002-11-10 23:32:56 +00002777
danielk19774cd2cd52007-08-23 14:48:23 +00002778
drh03eb96a2002-11-10 23:32:56 +00002779 /* Sync the journal before modifying the main database
2780 ** (assuming there is a journal and it needs to be synced.)
2781 */
danielk197776572402004-06-25 02:38:54 +00002782 if( pPager->needSync ){
drhfa86c412002-02-02 15:01:15 +00002783 if( !pPager->tempFile ){
danielk19774cd2cd52007-08-23 14:48:23 +00002784 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
drhdb48ee02003-01-16 13:42:43 +00002785 assert( pPager->journalOpen );
danielk19774cd2cd52007-08-23 14:48:23 +00002786
drh946966f2004-02-25 02:20:41 +00002787 /* assert( !pPager->noSync ); // noSync might be set if synchronous
2788 ** was turned off after the transaction was started. Ticket #615 */
drh968af522003-02-11 14:55:40 +00002789#ifndef NDEBUG
2790 {
drh34e79ce2004-02-08 06:05:46 +00002791 /* Make sure the pPager->nRec counter we are keeping agrees
2792 ** with the nRec computed from the size of the journal file.
2793 */
drheb206252004-10-01 02:00:31 +00002794 i64 jSz;
drh054889e2005-11-30 03:20:31 +00002795 rc = sqlite3OsFileSize(pPager->jfd, &jSz);
drh968af522003-02-11 14:55:40 +00002796 if( rc!=0 ) return rc;
danielk197776572402004-06-25 02:38:54 +00002797 assert( pPager->journalOff==jSz );
drh968af522003-02-11 14:55:40 +00002798 }
2799#endif
danielk19774cd2cd52007-08-23 14:48:23 +00002800 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
danielk197776572402004-06-25 02:38:54 +00002801 /* Write the nRec value into the journal file header. If in
2802 ** full-synchronous mode, sync the journal first. This ensures that
2803 ** all data has really hit the disk before nRec is updated to mark
danielk19774cd2cd52007-08-23 14:48:23 +00002804 ** it as a candidate for rollback.
2805 **
2806 ** This is not required if the persistent media supports the
2807 ** SAFE_APPEND property. Because in this case it is not possible
2808 ** for garbage data to be appended to the file, the nRec field
2809 ** is populated with 0xFFFFFFFF when the journal header is written
2810 ** and never needs to be updated.
danielk197776572402004-06-25 02:38:54 +00002811 */
danielk19774cd2cd52007-08-23 14:48:23 +00002812 i64 jrnlOff;
2813 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
drh4f0c5872007-03-26 22:05:01 +00002814 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002815 IOTRACE(("JSYNC %p\n", pPager))
danielk1977f036aef2007-08-20 05:36:51 +00002816 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags);
drhd8d66e82003-02-12 02:10:15 +00002817 if( rc!=0 ) return rc;
2818 }
danielk197713adf8a2004-06-03 16:08:41 +00002819
danielk197762079062007-08-15 17:08:46 +00002820 jrnlOff = pPager->journalHdr + sizeof(aJournalMagic);
2821 IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4));
2822 rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec);
drhb4746b92005-09-09 01:32:06 +00002823 if( rc ) return rc;
drh968af522003-02-11 14:55:40 +00002824 }
danielk19774cd2cd52007-08-23 14:48:23 +00002825 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
2826 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
2827 IOTRACE(("JSYNC %p\n", pPager))
2828 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags|
2829 (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
2830 );
2831 if( rc!=0 ) return rc;
2832 }
drhdb48ee02003-01-16 13:42:43 +00002833 pPager->journalStarted = 1;
drhfa86c412002-02-02 15:01:15 +00002834 }
drh50e5dad2001-09-15 00:57:28 +00002835 pPager->needSync = 0;
drh341eae82003-01-21 02:39:36 +00002836
2837 /* Erase the needSync flag from every page.
2838 */
2839 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2840 pPg->needSync = 0;
2841 }
danielk19779f61c2f2007-08-27 17:27:49 +00002842 lruListSetFirstSynced(pPager);
drh50e5dad2001-09-15 00:57:28 +00002843 }
drh03eb96a2002-11-10 23:32:56 +00002844
drh341eae82003-01-21 02:39:36 +00002845#ifndef NDEBUG
2846 /* If the Pager.needSync flag is clear then the PgHdr.needSync
2847 ** flag must also be clear for all pages. Verify that this
2848 ** invariant is true.
drh03eb96a2002-11-10 23:32:56 +00002849 */
drh341eae82003-01-21 02:39:36 +00002850 else{
2851 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2852 assert( pPg->needSync==0 );
2853 }
danielk19779f61c2f2007-08-27 17:27:49 +00002854 assert( pPager->lru.pFirstSynced==pPager->lru.pFirst );
drh03eb96a2002-11-10 23:32:56 +00002855 }
drh341eae82003-01-21 02:39:36 +00002856#endif
drhdb48ee02003-01-16 13:42:43 +00002857
drh81a20f22001-10-12 17:30:04 +00002858 return rc;
drh50e5dad2001-09-15 00:57:28 +00002859}
2860
2861/*
drhdc3e10b2006-06-15 14:31:06 +00002862** Merge two lists of pages connected by pDirty and in pgno order.
2863** Do not both fixing the pPrevDirty pointers.
2864*/
2865static PgHdr *merge_pagelist(PgHdr *pA, PgHdr *pB){
2866 PgHdr result, *pTail;
2867 pTail = &result;
2868 while( pA && pB ){
2869 if( pA->pgno<pB->pgno ){
2870 pTail->pDirty = pA;
2871 pTail = pA;
2872 pA = pA->pDirty;
2873 }else{
2874 pTail->pDirty = pB;
2875 pTail = pB;
2876 pB = pB->pDirty;
2877 }
2878 }
2879 if( pA ){
2880 pTail->pDirty = pA;
2881 }else if( pB ){
2882 pTail->pDirty = pB;
2883 }else{
2884 pTail->pDirty = 0;
2885 }
2886 return result.pDirty;
2887}
2888
2889/*
2890** Sort the list of pages in accending order by pgno. Pages are
2891** connected by pDirty pointers. The pPrevDirty pointers are
2892** corrupted by this sort.
2893*/
danielk197724168722007-04-02 05:07:47 +00002894#define N_SORT_BUCKET_ALLOC 25
2895#define N_SORT_BUCKET 25
2896#ifdef SQLITE_TEST
2897 int sqlite3_pager_n_sort_bucket = 0;
2898 #undef N_SORT_BUCKET
2899 #define N_SORT_BUCKET \
2900 (sqlite3_pager_n_sort_bucket?sqlite3_pager_n_sort_bucket:N_SORT_BUCKET_ALLOC)
2901#endif
drhdc3e10b2006-06-15 14:31:06 +00002902static PgHdr *sort_pagelist(PgHdr *pIn){
danielk197724168722007-04-02 05:07:47 +00002903 PgHdr *a[N_SORT_BUCKET_ALLOC], *p;
drhdc3e10b2006-06-15 14:31:06 +00002904 int i;
2905 memset(a, 0, sizeof(a));
2906 while( pIn ){
2907 p = pIn;
2908 pIn = p->pDirty;
2909 p->pDirty = 0;
2910 for(i=0; i<N_SORT_BUCKET-1; i++){
2911 if( a[i]==0 ){
2912 a[i] = p;
2913 break;
2914 }else{
2915 p = merge_pagelist(a[i], p);
2916 a[i] = 0;
2917 }
2918 }
2919 if( i==N_SORT_BUCKET-1 ){
danielk197724168722007-04-02 05:07:47 +00002920 /* Coverage: To get here, there need to be 2^(N_SORT_BUCKET)
2921 ** elements in the input list. This is possible, but impractical.
2922 ** Testing this line is the point of global variable
2923 ** sqlite3_pager_n_sort_bucket.
2924 */
drhdc3e10b2006-06-15 14:31:06 +00002925 a[i] = merge_pagelist(a[i], p);
2926 }
2927 }
2928 p = a[0];
2929 for(i=1; i<N_SORT_BUCKET; i++){
2930 p = merge_pagelist(p, a[i]);
2931 }
2932 return p;
2933}
2934
2935/*
drh2554f8b2003-01-22 01:26:44 +00002936** Given a list of pages (connected by the PgHdr.pDirty pointer) write
2937** every one of those pages out to the database file and mark them all
2938** as clean.
2939*/
2940static int pager_write_pagelist(PgHdr *pList){
2941 Pager *pPager;
drh153c62c2007-08-24 03:51:33 +00002942 PgHdr *p;
drh2554f8b2003-01-22 01:26:44 +00002943 int rc;
2944
2945 if( pList==0 ) return SQLITE_OK;
2946 pPager = pList->pPager;
danielk19779eed5052004-06-04 10:38:30 +00002947
2948 /* At this point there may be either a RESERVED or EXCLUSIVE lock on the
2949 ** database file. If there is already an EXCLUSIVE lock, the following
drh054889e2005-11-30 03:20:31 +00002950 ** calls to sqlite3OsLock() are no-ops.
danielk19779eed5052004-06-04 10:38:30 +00002951 **
drha6abd042004-06-09 17:37:22 +00002952 ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
2953 ** through an intermediate state PENDING. A PENDING lock prevents new
2954 ** readers from attaching to the database but is unsufficient for us to
2955 ** write. The idea of a PENDING lock is to prevent new readers from
2956 ** coming in while we wait for existing readers to clear.
danielk19779eed5052004-06-04 10:38:30 +00002957 **
drha6abd042004-06-09 17:37:22 +00002958 ** While the pager is in the RESERVED state, the original database file
2959 ** is unchanged and we can rollback without having to playback the
2960 ** journal into the original database file. Once we transition to
2961 ** EXCLUSIVE, it means the database file has been changed and any rollback
2962 ** will require a journal playback.
danielk19779eed5052004-06-04 10:38:30 +00002963 */
drh684917c2004-10-05 02:41:42 +00002964 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
danielk19779eed5052004-06-04 10:38:30 +00002965 if( rc!=SQLITE_OK ){
2966 return rc;
2967 }
2968
drhdc3e10b2006-06-15 14:31:06 +00002969 pList = sort_pagelist(pList);
drh153c62c2007-08-24 03:51:33 +00002970 for(p=pList; p; p=p->pDirty){
2971 assert( p->dirty );
2972 p->dirty = 0;
2973 }
drh2554f8b2003-01-22 01:26:44 +00002974 while( pList ){
danielk19777a2b1ee2007-08-21 14:27:01 +00002975
2976 /* If the file has not yet been opened, open it now. */
2977 if( !pPager->fd->pMethods ){
2978 assert(pPager->tempFile);
drh33f4e022007-09-03 15:19:34 +00002979 rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->fd, pPager->zFilename,
2980 pPager->vfsFlags);
danielk19777a2b1ee2007-08-21 14:27:01 +00002981 if( rc ) return rc;
2982 }
2983
danielk1977687566d2004-11-02 12:56:41 +00002984 /* If there are dirty pages in the page cache with page numbers greater
danielk19773b8a05f2007-03-19 17:44:26 +00002985 ** than Pager.dbSize, this means sqlite3PagerTruncate() was called to
danielk1977687566d2004-11-02 12:56:41 +00002986 ** make the file smaller (presumably by auto-vacuum code). Do not write
2987 ** any such pages to the file.
2988 */
2989 if( pList->pgno<=pPager->dbSize ){
danielk197762079062007-08-15 17:08:46 +00002990 i64 offset = (pList->pgno-1)*(i64)pPager->pageSize;
drhc001c582006-03-06 18:23:16 +00002991 char *pData = CODEC2(pPager, PGHDR_TO_DATA(pList), pList->pgno, 6);
drh477731b2007-06-16 03:06:27 +00002992 PAGERTRACE4("STORE %d page %d hash(%08x)\n",
2993 PAGERID(pPager), pList->pgno, pager_pagehash(pList));
drh538f5702007-04-13 02:14:30 +00002994 IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno));
danielk197762079062007-08-15 17:08:46 +00002995 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00002996 PAGER_INCR(sqlite3_pager_writedb_count);
2997 PAGER_INCR(pPager->nWrite);
drh86a88112007-04-16 15:02:19 +00002998 if( pList->pgno==1 ){
2999 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
3000 }
danielk1977687566d2004-11-02 12:56:41 +00003001 }
3002#ifndef NDEBUG
3003 else{
drh4f0c5872007-03-26 22:05:01 +00003004 PAGERTRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno);
danielk1977687566d2004-11-02 12:56:41 +00003005 }
3006#endif
drh2554f8b2003-01-22 01:26:44 +00003007 if( rc ) return rc;
danielk19773c407372005-02-15 02:54:14 +00003008#ifdef SQLITE_CHECK_PAGES
3009 pList->pageHash = pager_pagehash(pList);
3010#endif
drh2554f8b2003-01-22 01:26:44 +00003011 pList = pList->pDirty;
3012 }
3013 return SQLITE_OK;
3014}
3015
3016/*
3017** Collect every dirty page into a dirty list and
3018** return a pointer to the head of that list. All pages are
3019** collected even if they are still in use.
3020*/
3021static PgHdr *pager_get_all_dirty_pages(Pager *pPager){
drh8f2e9a12008-02-26 14:46:04 +00003022
3023#ifndef NDEBUG
3024 /* Verify the sanity of the dirty list when we are running
3025 ** in debugging mode. This is expensive, so do not
3026 ** do this on a normal build. */
3027 int n1 = 0;
3028 int n2 = 0;
3029 PgHdr *p;
3030 for(p=pPager->pAll; p; p=p->pNextAll){ if( p->dirty ) n1++; }
3031 for(p=pPager->pDirty; p; p=p->pDirty){ n2++; }
3032 assert( n1==n2 );
3033#endif
3034
drh605ad8f2006-05-03 23:34:05 +00003035 return pPager->pDirty;
drh2554f8b2003-01-22 01:26:44 +00003036}
3037
3038/*
drh165ffe92005-03-15 17:09:30 +00003039** Return TRUE if there is a hot journal on the given pager.
3040** A hot journal is one that needs to be played back.
3041**
3042** If the current size of the database file is 0 but a journal file
3043** exists, that is probably an old journal left over from a prior
3044** database with the same name. Just delete the journal.
3045*/
3046static int hasHotJournal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00003047 sqlite3_vfs *pVfs = pPager->pVfs;
drh165ffe92005-03-15 17:09:30 +00003048 if( !pPager->useJournal ) return 0;
drhd3cf2472007-11-27 16:55:07 +00003049 if( !pPager->fd->pMethods ) return 0;
danielk1977b4b47412007-08-17 15:53:36 +00003050 if( !sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS) ){
drhbb5f18d2007-04-06 18:23:17 +00003051 return 0;
3052 }
3053 if( sqlite3OsCheckReservedLock(pPager->fd) ){
3054 return 0;
3055 }
danielk19773b8a05f2007-03-19 17:44:26 +00003056 if( sqlite3PagerPagecount(pPager)==0 ){
danielk1977fee2d252007-08-18 10:59:19 +00003057 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
drh165ffe92005-03-15 17:09:30 +00003058 return 0;
3059 }else{
3060 return 1;
3061 }
3062}
3063
danielk19775591df52005-12-20 09:19:37 +00003064/*
danielk1977aef0bf62005-12-30 16:28:01 +00003065** Try to find a page in the cache that can be recycled.
3066**
3067** This routine may return SQLITE_IOERR, SQLITE_FULL or SQLITE_OK. It
danielk1977efaaf572006-01-16 11:29:19 +00003068** does not set the pPager->errCode variable.
danielk19775591df52005-12-20 09:19:37 +00003069*/
danielk1977843e65f2007-09-01 16:16:15 +00003070static int pager_recycle(Pager *pPager, PgHdr **ppPg){
danielk197713f72992005-12-18 08:51:22 +00003071 PgHdr *pPg;
3072 *ppPg = 0;
3073
danielk1977843e65f2007-09-01 16:16:15 +00003074 /* It is illegal to call this function unless the pager object
3075 ** pointed to by pPager has at least one free page (page with nRef==0).
3076 */
danielk1977c551edc2007-04-05 13:12:13 +00003077 assert(!MEMDB);
danielk1977843e65f2007-09-01 16:16:15 +00003078 assert(pPager->lru.pFirst);
danielk1977c551edc2007-04-05 13:12:13 +00003079
danielk197713f72992005-12-18 08:51:22 +00003080 /* Find a page to recycle. Try to locate a page that does not
3081 ** require us to do an fsync() on the journal.
3082 */
danielk19779f61c2f2007-08-27 17:27:49 +00003083 pPg = pPager->lru.pFirstSynced;
danielk197713f72992005-12-18 08:51:22 +00003084
3085 /* If we could not find a page that does not require an fsync()
3086 ** on the journal file then fsync the journal file. This is a
3087 ** very slow operation, so we work hard to avoid it. But sometimes
3088 ** it can't be helped.
3089 */
danielk1977843e65f2007-09-01 16:16:15 +00003090 if( pPg==0 && pPager->lru.pFirst){
danielk19774cd2cd52007-08-23 14:48:23 +00003091 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
danielk197713f72992005-12-18 08:51:22 +00003092 int rc = syncJournal(pPager);
3093 if( rc!=0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00003094 return rc;
danielk197713f72992005-12-18 08:51:22 +00003095 }
danielk19774cd2cd52007-08-23 14:48:23 +00003096 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
danielk197713f72992005-12-18 08:51:22 +00003097 /* If in full-sync mode, write a new journal header into the
3098 ** journal file. This is done to avoid ever modifying a journal
3099 ** header that is involved in the rollback of pages that have
3100 ** already been written to the database (in case the header is
3101 ** trashed when the nRec field is updated).
3102 */
3103 pPager->nRec = 0;
3104 assert( pPager->journalOff > 0 );
danielk19774099f6e2007-03-19 11:25:20 +00003105 assert( pPager->doNotSync==0 );
danielk197713f72992005-12-18 08:51:22 +00003106 rc = writeJournalHdr(pPager);
3107 if( rc!=0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00003108 return rc;
danielk197713f72992005-12-18 08:51:22 +00003109 }
3110 }
danielk19779f61c2f2007-08-27 17:27:49 +00003111 pPg = pPager->lru.pFirst;
danielk197713f72992005-12-18 08:51:22 +00003112 }
danielk197713f72992005-12-18 08:51:22 +00003113
3114 assert( pPg->nRef==0 );
3115
3116 /* Write the page to the database file if it is dirty.
3117 */
3118 if( pPg->dirty ){
3119 int rc;
3120 assert( pPg->needSync==0 );
drh605ad8f2006-05-03 23:34:05 +00003121 makeClean(pPg);
3122 pPg->dirty = 1;
danielk197713f72992005-12-18 08:51:22 +00003123 pPg->pDirty = 0;
3124 rc = pager_write_pagelist( pPg );
drh153c62c2007-08-24 03:51:33 +00003125 pPg->dirty = 0;
danielk197713f72992005-12-18 08:51:22 +00003126 if( rc!=SQLITE_OK ){
danielk1977aef0bf62005-12-30 16:28:01 +00003127 return rc;
danielk197713f72992005-12-18 08:51:22 +00003128 }
3129 }
3130 assert( pPg->dirty==0 );
3131
3132 /* If the page we are recycling is marked as alwaysRollback, then
3133 ** set the global alwaysRollback flag, thus disabling the
drh80e35f42007-03-30 14:06:34 +00003134 ** sqlite3PagerDontRollback() optimization for the rest of this transaction.
danielk197713f72992005-12-18 08:51:22 +00003135 ** It is necessary to do this because the page marked alwaysRollback
3136 ** might be reloaded at a later time but at that point we won't remember
3137 ** that is was marked alwaysRollback. This means that all pages must
3138 ** be marked as alwaysRollback from here on out.
3139 */
3140 if( pPg->alwaysRollback ){
drh602c2372007-03-01 00:29:13 +00003141 IOTRACE(("ALWAYS_ROLLBACK %p\n", pPager))
danielk197713f72992005-12-18 08:51:22 +00003142 pPager->alwaysRollback = 1;
3143 }
3144
3145 /* Unlink the old page from the free list and the hash table
3146 */
3147 unlinkPage(pPg);
drh7c4ac0c2007-04-05 11:25:58 +00003148 assert( pPg->pgno==0 );
danielk197713f72992005-12-18 08:51:22 +00003149
3150 *ppPg = pPg;
3151 return SQLITE_OK;
3152}
3153
drh86f8c192007-08-22 00:39:19 +00003154#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk197713f72992005-12-18 08:51:22 +00003155/*
3156** This function is called to free superfluous dynamically allocated memory
3157** held by the pager system. Memory in use by any SQLite pager allocated
drh17435752007-08-16 04:30:38 +00003158** by the current thread may be sqlite3_free()ed.
danielk197713f72992005-12-18 08:51:22 +00003159**
3160** nReq is the number of bytes of memory required. Once this much has
drh86f8c192007-08-22 00:39:19 +00003161** been released, the function returns. The return value is the total number
danielk197713f72992005-12-18 08:51:22 +00003162** of bytes of memory released.
3163*/
danielk19773b8a05f2007-03-19 17:44:26 +00003164int sqlite3PagerReleaseMemory(int nReq){
drh86f8c192007-08-22 00:39:19 +00003165 int nReleased = 0; /* Bytes of memory released so far */
3166 sqlite3_mutex *mutex; /* The MEM2 mutex */
3167 Pager *pPager; /* For looping over pagers */
drhe5fe6902007-12-07 18:55:28 +00003168 BusyHandler *savedBusy; /* Saved copy of the busy handler */
danielk19779f61c2f2007-08-27 17:27:49 +00003169 int rc = SQLITE_OK;
danielk197713f72992005-12-18 08:51:22 +00003170
drh86f8c192007-08-22 00:39:19 +00003171 /* Acquire the memory-management mutex
danielk1977441b09a2006-01-05 13:48:29 +00003172 */
drh86f8c192007-08-22 00:39:19 +00003173 mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
3174 sqlite3_mutex_enter(mutex);
3175
3176 /* Signal all database connections that memory management wants
3177 ** to have access to the pagers.
3178 */
3179 for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
3180 pPager->iInUseMM = 1;
danielk1977441b09a2006-01-05 13:48:29 +00003181 }
3182
danielk19779f61c2f2007-08-27 17:27:49 +00003183 while( rc==SQLITE_OK && (nReq<0 || nReleased<nReq) ){
3184 PgHdr *pPg;
3185 PgHdr *pRecycled;
3186
3187 /* Try to find a page to recycle that does not require a sync(). If
3188 ** this is not possible, find one that does require a sync().
3189 */
3190 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
3191 pPg = sqlite3LruPageList.pFirstSynced;
3192 while( pPg && (pPg->needSync || pPg->pPager->iInUseDB) ){
3193 pPg = pPg->gfree.pNext;
3194 }
3195 if( !pPg ){
3196 pPg = sqlite3LruPageList.pFirst;
3197 while( pPg && pPg->pPager->iInUseDB ){
3198 pPg = pPg->gfree.pNext;
3199 }
3200 }
3201 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
danielk197713f72992005-12-18 08:51:22 +00003202
danielk197784f786f2007-08-28 08:00:17 +00003203 /* If pPg==0, then the block above has failed to find a page to
3204 ** recycle. In this case return early - no further memory will
3205 ** be released.
3206 */
danielk19779f61c2f2007-08-27 17:27:49 +00003207 if( !pPg ) break;
danielk197713f72992005-12-18 08:51:22 +00003208
danielk19779f61c2f2007-08-27 17:27:49 +00003209 pPager = pPg->pPager;
3210 assert(!pPg->needSync || pPg==pPager->lru.pFirst);
3211 assert(pPg->needSync || pPg==pPager->lru.pFirstSynced);
3212
drhe5fe6902007-12-07 18:55:28 +00003213 savedBusy = pPager->pBusyHandler;
3214 pPager->pBusyHandler = 0;
danielk1977843e65f2007-09-01 16:16:15 +00003215 rc = pager_recycle(pPager, &pRecycled);
drhe5fe6902007-12-07 18:55:28 +00003216 pPager->pBusyHandler = savedBusy;
danielk19779f61c2f2007-08-27 17:27:49 +00003217 assert(pRecycled==pPg || rc!=SQLITE_OK);
3218 if( rc==SQLITE_OK ){
3219 /* We've found a page to free. At this point the page has been
3220 ** removed from the page hash-table, free-list and synced-list
3221 ** (pFirstSynced). It is still in the all pages (pAll) list.
3222 ** Remove it from this list before freeing.
3223 **
3224 ** Todo: Check the Pager.pStmt list to make sure this is Ok. It
3225 ** probably is though.
danielk197713f72992005-12-18 08:51:22 +00003226 */
danielk19779f61c2f2007-08-27 17:27:49 +00003227 PgHdr *pTmp;
3228 assert( pPg );
3229 if( pPg==pPager->pAll ){
3230 pPager->pAll = pPg->pNextAll;
3231 }else{
3232 for( pTmp=pPager->pAll; pTmp->pNextAll!=pPg; pTmp=pTmp->pNextAll ){}
3233 pTmp->pNextAll = pPg->pNextAll;
danielk197713f72992005-12-18 08:51:22 +00003234 }
danielk19779f61c2f2007-08-27 17:27:49 +00003235 nReleased += (
3236 sizeof(*pPg) + pPager->pageSize
3237 + sizeof(u32) + pPager->nExtra
3238 + MEMDB*sizeof(PgHistory)
3239 );
3240 IOTRACE(("PGFREE %p %d *\n", pPager, pPg->pgno));
3241 PAGER_INCR(sqlite3_pager_pgfree_count);
drh0d180202008-02-14 23:26:56 +00003242 sqlite3_free(pPg->pData);
danielk19779f61c2f2007-08-27 17:27:49 +00003243 sqlite3_free(pPg);
danielk197784f786f2007-08-28 08:00:17 +00003244 pPager->nPage--;
danielk19779f61c2f2007-08-27 17:27:49 +00003245 }else{
3246 /* An error occured whilst writing to the database file or
3247 ** journal in pager_recycle(). The error is not returned to the
3248 ** caller of this function. Instead, set the Pager.errCode variable.
3249 ** The error will be returned to the user (or users, in the case
3250 ** of a shared pager cache) of the pager for which the error occured.
3251 */
3252 assert(
3253 (rc&0xff)==SQLITE_IOERR ||
3254 rc==SQLITE_FULL ||
3255 rc==SQLITE_BUSY
3256 );
3257 assert( pPager->state>=PAGER_RESERVED );
3258 pager_error(pPager, rc);
danielk197713f72992005-12-18 08:51:22 +00003259 }
3260 }
danielk1977aef0bf62005-12-30 16:28:01 +00003261
drh86f8c192007-08-22 00:39:19 +00003262 /* Clear the memory management flags and release the mutex
3263 */
3264 for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
3265 pPager->iInUseMM = 0;
3266 }
3267 sqlite3_mutex_leave(mutex);
3268
3269 /* Return the number of bytes released
3270 */
danielk197713f72992005-12-18 08:51:22 +00003271 return nReleased;
3272}
drh86f8c192007-08-22 00:39:19 +00003273#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
danielk197713f72992005-12-18 08:51:22 +00003274
drh165ffe92005-03-15 17:09:30 +00003275/*
danielk1977e180dd92007-04-05 17:15:52 +00003276** Read the content of page pPg out of the database file.
3277*/
3278static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){
3279 int rc;
danielk197762079062007-08-15 17:08:46 +00003280 i64 offset;
danielk1977e180dd92007-04-05 17:15:52 +00003281 assert( MEMDB==0 );
danielk19777a2b1ee2007-08-21 14:27:01 +00003282 assert(pPager->fd->pMethods||pPager->tempFile);
3283 if( !pPager->fd->pMethods ){
3284 return SQLITE_IOERR_SHORT_READ;
3285 }
danielk197762079062007-08-15 17:08:46 +00003286 offset = (pgno-1)*(i64)pPager->pageSize;
3287 rc = sqlite3OsRead(pPager->fd, PGHDR_TO_DATA(pPg), pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00003288 PAGER_INCR(sqlite3_pager_readdb_count);
3289 PAGER_INCR(pPager->nRead);
3290 IOTRACE(("PGIN %p %d\n", pPager, pgno));
drh86a88112007-04-16 15:02:19 +00003291 if( pgno==1 ){
3292 memcpy(&pPager->dbFileVers, &((u8*)PGHDR_TO_DATA(pPg))[24],
3293 sizeof(pPager->dbFileVers));
3294 }
danielk1977e180dd92007-04-05 17:15:52 +00003295 CODEC1(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3);
drh477731b2007-06-16 03:06:27 +00003296 PAGERTRACE4("FETCH %d page %d hash(%08x)\n",
3297 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg));
danielk1977e180dd92007-04-05 17:15:52 +00003298 return rc;
3299}
3300
3301
3302/*
danielk1977e277be02007-03-23 18:12:06 +00003303** This function is called to obtain the shared lock required before
3304** data may be read from the pager cache. If the shared lock has already
3305** been obtained, this function is a no-op.
danielk1977393f0682007-03-31 10:00:48 +00003306**
3307** Immediately after obtaining the shared lock (if required), this function
3308** checks for a hot-journal file. If one is found, an emergency rollback
3309** is performed immediately.
danielk1977e277be02007-03-23 18:12:06 +00003310*/
3311static int pagerSharedLock(Pager *pPager){
3312 int rc = SQLITE_OK;
danielk1977ae72d982007-10-03 08:46:44 +00003313 int isHot = 0;
danielk1977e277be02007-03-23 18:12:06 +00003314
danielk1977ae72d982007-10-03 08:46:44 +00003315 /* If this database is opened for exclusive access, has no outstanding
3316 ** page references and is in an error-state, now is the chance to clear
3317 ** the error. Discard the contents of the pager-cache and treat any
3318 ** open journal file as a hot-journal.
3319 */
3320 if( !MEMDB && pPager->exclusiveMode && pPager->nRef==0 && pPager->errCode ){
3321 if( pPager->journalOpen ){
3322 isHot = 1;
3323 }
3324 pager_reset(pPager);
3325 pPager->errCode = SQLITE_OK;
3326 }
3327
3328 /* If the pager is still in an error state, do not proceed. The error
3329 ** state will be cleared at some point in the future when all page
3330 ** references are dropped and the cache can be discarded.
3331 */
3332 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
3333 return pPager->errCode;
3334 }
3335
3336 if( pPager->state==PAGER_UNLOCK || isHot ){
danielk1977b4b47412007-08-17 15:53:36 +00003337 sqlite3_vfs *pVfs = pPager->pVfs;
danielk1977e277be02007-03-23 18:12:06 +00003338 if( !MEMDB ){
3339 assert( pPager->nRef==0 );
3340 if( !pPager->noReadlock ){
3341 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
3342 if( rc!=SQLITE_OK ){
3343 return pager_error(pPager, rc);
3344 }
3345 assert( pPager->state>=SHARED_LOCK );
3346 }
3347
3348 /* If a journal file exists, and there is no RESERVED lock on the
3349 ** database file, then it either needs to be played back or deleted.
3350 */
danielk1977ae72d982007-10-03 08:46:44 +00003351 if( hasHotJournal(pPager) || isHot ){
danielk1977e277be02007-03-23 18:12:06 +00003352 /* Get an EXCLUSIVE lock on the database file. At this point it is
3353 ** important that a RESERVED lock is not obtained on the way to the
3354 ** EXCLUSIVE lock. If it were, another process might open the
3355 ** database file, detect the RESERVED lock, and conclude that the
3356 ** database is safe to read while this process is still rolling it
3357 ** back.
3358 **
3359 ** Because the intermediate RESERVED lock is not requested, the
3360 ** second process will get to this point in the code and fail to
drh85b623f2007-12-13 21:54:09 +00003361 ** obtain its own EXCLUSIVE lock on the database file.
danielk1977e277be02007-03-23 18:12:06 +00003362 */
danielk1977ae72d982007-10-03 08:46:44 +00003363 if( pPager->state<EXCLUSIVE_LOCK ){
3364 rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
3365 if( rc!=SQLITE_OK ){
3366 pager_unlock(pPager);
3367 return pager_error(pPager, rc);
3368 }
3369 pPager->state = PAGER_EXCLUSIVE;
danielk1977e277be02007-03-23 18:12:06 +00003370 }
danielk1977e277be02007-03-23 18:12:06 +00003371
3372 /* Open the journal for reading only. Return SQLITE_BUSY if
3373 ** we are unable to open the journal file.
3374 **
3375 ** The journal file does not need to be locked itself. The
3376 ** journal file is never open unless the main database file holds
3377 ** a write lock, so there is never any chance of two or more
3378 ** processes opening the journal at the same time.
danielk1977979f38e2007-03-27 16:19:51 +00003379 **
drhfd131da2007-08-07 17:13:03 +00003380 ** Open the journal for read/write access. This is because in
3381 ** exclusive-access mode the file descriptor will be kept open and
danielk1977979f38e2007-03-27 16:19:51 +00003382 ** possibly used for a transaction later on. On some systems, the
3383 ** OsTruncate() call used in exclusive-access mode also requires
3384 ** a read/write file handle.
danielk1977e277be02007-03-23 18:12:06 +00003385 */
danielk1977ae72d982007-10-03 08:46:44 +00003386 if( !isHot ){
3387 rc = SQLITE_BUSY;
3388 if( sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS) ){
3389 int fout = 0;
3390 int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
3391 assert( !pPager->tempFile );
3392 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
3393 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
3394 if( fout&SQLITE_OPEN_READONLY ){
3395 rc = SQLITE_BUSY;
3396 sqlite3OsClose(pPager->jfd);
3397 }
danielk1977979f38e2007-03-27 16:19:51 +00003398 }
3399 }
danielk1977e277be02007-03-23 18:12:06 +00003400 if( rc!=SQLITE_OK ){
3401 pager_unlock(pPager);
drh1aa5af12008-03-07 19:51:14 +00003402 switch( rc ){
3403 case SQLITE_NOMEM:
3404 case SQLITE_IOERR_UNLOCK:
3405 case SQLITE_IOERR_NOMEM:
3406 return rc;
3407 default:
3408 return SQLITE_BUSY;
3409 }
danielk1977e277be02007-03-23 18:12:06 +00003410 }
3411 pPager->journalOpen = 1;
3412 pPager->journalStarted = 0;
3413 pPager->journalOff = 0;
3414 pPager->setMaster = 0;
3415 pPager->journalHdr = 0;
3416
3417 /* Playback and delete the journal. Drop the database write
3418 ** lock and reacquire the read lock.
3419 */
3420 rc = pager_playback(pPager, 1);
3421 if( rc!=SQLITE_OK ){
3422 return pager_error(pPager, rc);
3423 }
danielk1977c5859712007-03-26 12:26:27 +00003424 assert(pPager->state==PAGER_SHARED ||
3425 (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
3426 );
danielk1977e277be02007-03-23 18:12:06 +00003427 }
3428
3429 if( pPager->pAll ){
danielk197724168722007-04-02 05:07:47 +00003430 /* The shared-lock has just been acquired on the database file
3431 ** and there are already pages in the cache (from a previous
drh86a88112007-04-16 15:02:19 +00003432 ** read or write transaction). Check to see if the database
3433 ** has been modified. If the database has changed, flush the
3434 ** cache.
3435 **
3436 ** Database changes is detected by looking at 15 bytes beginning
3437 ** at offset 24 into the file. The first 4 of these 16 bytes are
3438 ** a 32-bit counter that is incremented with each change. The
3439 ** other bytes change randomly with each file change when
3440 ** a codec is in use.
3441 **
3442 ** There is a vanishingly small chance that a change will not be
drh6fa51032007-05-09 20:35:31 +00003443 ** detected. The chance of an undetected change is so small that
drh86a88112007-04-16 15:02:19 +00003444 ** it can be neglected.
danielk197724168722007-04-02 05:07:47 +00003445 */
drh86a88112007-04-16 15:02:19 +00003446 char dbFileVers[sizeof(pPager->dbFileVers)];
danielk1977e180dd92007-04-05 17:15:52 +00003447 sqlite3PagerPagecount(pPager);
danielk197724168722007-04-02 05:07:47 +00003448
danielk1977e180dd92007-04-05 17:15:52 +00003449 if( pPager->errCode ){
3450 return pPager->errCode;
danielk1977e277be02007-03-23 18:12:06 +00003451 }
3452
danielk1977e180dd92007-04-05 17:15:52 +00003453 if( pPager->dbSize>0 ){
drhae5e4452007-05-03 17:18:36 +00003454 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
danielk197762079062007-08-15 17:08:46 +00003455 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
danielk1977e180dd92007-04-05 17:15:52 +00003456 if( rc!=SQLITE_OK ){
3457 return rc;
3458 }
drh86a88112007-04-16 15:02:19 +00003459 }else{
3460 memset(dbFileVers, 0, sizeof(dbFileVers));
danielk1977e180dd92007-04-05 17:15:52 +00003461 }
3462
drh86a88112007-04-16 15:02:19 +00003463 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
danielk1977e180dd92007-04-05 17:15:52 +00003464 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00003465 }
3466 }
3467 }
danielk1977c5859712007-03-26 12:26:27 +00003468 assert( pPager->exclusiveMode || pPager->state<=PAGER_SHARED );
3469 if( pPager->state==PAGER_UNLOCK ){
3470 pPager->state = PAGER_SHARED;
3471 }
danielk1977e277be02007-03-23 18:12:06 +00003472 }
3473
3474 return rc;
3475}
3476
3477/*
danielk1977e180dd92007-04-05 17:15:52 +00003478** Allocate a PgHdr object. Either create a new one or reuse
3479** an existing one that is not otherwise in use.
3480**
3481** A new PgHdr structure is created if any of the following are
3482** true:
3483**
3484** (1) We have not exceeded our maximum allocated cache size
3485** as set by the "PRAGMA cache_size" command.
3486**
3487** (2) There are no unused PgHdr objects available at this time.
3488**
3489** (3) This is an in-memory database.
3490**
3491** (4) There are no PgHdr objects that do not require a journal
3492** file sync and a sync of the journal file is currently
3493** prohibited.
3494**
3495** Otherwise, reuse an existing PgHdr. In other words, reuse an
3496** existing PgHdr if all of the following are true:
3497**
3498** (1) We have reached or exceeded the maximum cache size
3499** allowed by "PRAGMA cache_size".
3500**
3501** (2) There is a PgHdr available with PgHdr->nRef==0
3502**
3503** (3) We are not in an in-memory database
3504**
3505** (4) Either there is an available PgHdr that does not need
3506** to be synced to disk or else disk syncing is currently
3507** allowed.
danielk197724168722007-04-02 05:07:47 +00003508*/
3509static int pagerAllocatePage(Pager *pPager, PgHdr **ppPg){
3510 int rc = SQLITE_OK;
3511 PgHdr *pPg;
drh1e3af332007-10-20 13:17:54 +00003512 int nByteHdr;
danielk197724168722007-04-02 05:07:47 +00003513
danielk1977e180dd92007-04-05 17:15:52 +00003514 /* Create a new PgHdr if any of the four conditions defined
danielk19772a6bdf62007-08-20 16:07:00 +00003515 ** above are met: */
danielk1977e180dd92007-04-05 17:15:52 +00003516 if( pPager->nPage<pPager->mxPage
danielk19779f61c2f2007-08-27 17:27:49 +00003517 || pPager->lru.pFirst==0
danielk1977e180dd92007-04-05 17:15:52 +00003518 || MEMDB
danielk19779f61c2f2007-08-27 17:27:49 +00003519 || (pPager->lru.pFirstSynced==0 && pPager->doNotSync)
danielk1977e180dd92007-04-05 17:15:52 +00003520 ){
drh0d180202008-02-14 23:26:56 +00003521 void *pData;
danielk197724168722007-04-02 05:07:47 +00003522 if( pPager->nPage>=pPager->nHash ){
3523 pager_resize_hash_table(pPager,
3524 pPager->nHash<256 ? 256 : pPager->nHash*2);
3525 if( pPager->nHash==0 ){
3526 rc = SQLITE_NOMEM;
3527 goto pager_allocate_out;
3528 }
3529 }
drhed138fb2007-08-22 22:04:37 +00003530 pagerLeave(pPager);
drh1e3af332007-10-20 13:17:54 +00003531 nByteHdr = sizeof(*pPg) + sizeof(u32) + pPager->nExtra
3532 + MEMDB*sizeof(PgHistory);
drh0d180202008-02-14 23:26:56 +00003533 pPg = sqlite3_malloc( nByteHdr );
3534 if( pPg ){
3535 pData = sqlite3_malloc( pPager->pageSize );
3536 if( pData==0 ){
3537 sqlite3_free(pPg);
3538 pPg = 0;
3539 }
3540 }
drhed138fb2007-08-22 22:04:37 +00003541 pagerEnter(pPager);
danielk197724168722007-04-02 05:07:47 +00003542 if( pPg==0 ){
3543 rc = SQLITE_NOMEM;
3544 goto pager_allocate_out;
3545 }
drh1e3af332007-10-20 13:17:54 +00003546 memset(pPg, 0, nByteHdr);
drh0d180202008-02-14 23:26:56 +00003547 pPg->pData = pData;
danielk197724168722007-04-02 05:07:47 +00003548 pPg->pPager = pPager;
3549 pPg->pNextAll = pPager->pAll;
3550 pPager->pAll = pPg;
3551 pPager->nPage++;
danielk197724168722007-04-02 05:07:47 +00003552 }else{
3553 /* Recycle an existing page with a zero ref-count. */
danielk1977843e65f2007-09-01 16:16:15 +00003554 rc = pager_recycle(pPager, &pPg);
danielk1977e965ac72007-06-13 15:22:28 +00003555 if( rc==SQLITE_BUSY ){
3556 rc = SQLITE_IOERR_BLOCKED;
3557 }
danielk197724168722007-04-02 05:07:47 +00003558 if( rc!=SQLITE_OK ){
3559 goto pager_allocate_out;
3560 }
3561 assert( pPager->state>=SHARED_LOCK );
3562 assert(pPg);
3563 }
3564 *ppPg = pPg;
3565
3566pager_allocate_out:
3567 return rc;
3568}
3569
3570/*
drhd33d5a82007-04-26 12:11:28 +00003571** Make sure we have the content for a page. If the page was
3572** previously acquired with noContent==1, then the content was
3573** just initialized to zeros instead of being read from disk.
3574** But now we need the real data off of disk. So make sure we
3575** have it. Read it in if we do not have it already.
3576*/
3577static int pager_get_content(PgHdr *pPg){
3578 if( pPg->needRead ){
3579 int rc = readDbPage(pPg->pPager, pPg, pPg->pgno);
3580 if( rc==SQLITE_OK ){
3581 pPg->needRead = 0;
3582 }else{
3583 return rc;
3584 }
3585 }
3586 return SQLITE_OK;
3587}
3588
3589/*
drhd9b02572001-04-15 00:37:09 +00003590** Acquire a page.
3591**
drh58a11682001-11-10 13:51:08 +00003592** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00003593** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00003594**
drhd33d5a82007-04-26 12:11:28 +00003595** This routine works for any page number greater than 0. If the database
drh306dc212001-05-21 13:45:10 +00003596** file is smaller than the requested page, then no actual disk
3597** read occurs and the memory image of the page is initialized to
3598** all zeros. The extra data appended to a page is always initialized
3599** to zeros the first time a page is loaded into memory.
3600**
drhd9b02572001-04-15 00:37:09 +00003601** The acquisition might fail for several reasons. In all cases,
3602** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00003603**
drhd33d5a82007-04-26 12:11:28 +00003604** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
drh7e3b0a02001-04-28 16:52:40 +00003605** to find a page in the in-memory cache first. If the page is not already
drhd33d5a82007-04-26 12:11:28 +00003606** in memory, this routine goes to disk to read it in whereas Lookup()
drh7e3b0a02001-04-28 16:52:40 +00003607** just returns 0. This routine acquires a read-lock the first time it
3608** has to go to disk, and could also playback an old journal if necessary.
drhd33d5a82007-04-26 12:11:28 +00003609** Since Lookup() never goes to disk, it never has to deal with locks
drh7e3b0a02001-04-28 16:52:40 +00003610** or journal files.
drh0787db62007-03-04 13:15:27 +00003611**
drh538f5702007-04-13 02:14:30 +00003612** If noContent is false, the page contents are actually read from disk.
3613** If noContent is true, it means that we do not care about the contents
3614** of the page at this time, so do not do a disk read. Just fill in the
3615** page content with zeros. But mark the fact that we have not read the
3616** content by setting the PgHdr.needRead flag. Later on, if
drhd33d5a82007-04-26 12:11:28 +00003617** sqlite3PagerWrite() is called on this page or if this routine is
3618** called again with noContent==0, that means that the content is needed
3619** and the disk read should occur at that point.
drhed7c8552001-04-11 14:29:21 +00003620*/
drh86f8c192007-08-22 00:39:19 +00003621static int pagerAcquire(
drh538f5702007-04-13 02:14:30 +00003622 Pager *pPager, /* The pager open on the database file */
3623 Pgno pgno, /* Page number to fetch */
3624 DbPage **ppPage, /* Write a pointer to the page here */
3625 int noContent /* Do not bother reading content from disk if true */
3626){
drhed7c8552001-04-11 14:29:21 +00003627 PgHdr *pPg;
drh165ffe92005-03-15 17:09:30 +00003628 int rc;
drhed7c8552001-04-11 14:29:21 +00003629
danielk1977e277be02007-03-23 18:12:06 +00003630 assert( pPager->state==PAGER_UNLOCK || pPager->nRef>0 || pgno==1 );
3631
danielk197726836652005-01-17 01:33:13 +00003632 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
3633 ** number greater than this, or zero, is requested.
3634 */
drh267cb322005-09-16 17:16:52 +00003635 if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh49285702005-09-17 15:20:26 +00003636 return SQLITE_CORRUPT_BKPT;
danielk197726836652005-01-17 01:33:13 +00003637 }
3638
drhd9b02572001-04-15 00:37:09 +00003639 /* Make sure we have not hit any critical errors.
3640 */
drh836faa42003-01-11 13:30:57 +00003641 assert( pPager!=0 );
drh2e6d11b2003-04-25 15:37:57 +00003642 *ppPage = 0;
drhd9b02572001-04-15 00:37:09 +00003643
danielk197713adf8a2004-06-03 16:08:41 +00003644 /* If this is the first page accessed, then get a SHARED lock
danielk1977334cdb62007-03-26 08:05:12 +00003645 ** on the database file. pagerSharedLock() is a no-op if
3646 ** a database lock is already held.
drhed7c8552001-04-11 14:29:21 +00003647 */
danielk1977e277be02007-03-23 18:12:06 +00003648 rc = pagerSharedLock(pPager);
3649 if( rc!=SQLITE_OK ){
3650 return rc;
drhed7c8552001-04-11 14:29:21 +00003651 }
danielk1977e277be02007-03-23 18:12:06 +00003652 assert( pPager->state!=PAGER_UNLOCK );
3653
3654 pPg = pager_lookup(pPager, pgno);
drhed7c8552001-04-11 14:29:21 +00003655 if( pPg==0 ){
drhd9b02572001-04-15 00:37:09 +00003656 /* The requested page is not in the page cache. */
danielk1977979f38e2007-03-27 16:19:51 +00003657 int nMax;
drhed7c8552001-04-11 14:29:21 +00003658 int h;
drh538f5702007-04-13 02:14:30 +00003659 PAGER_INCR(pPager->nMiss);
danielk197724168722007-04-02 05:07:47 +00003660 rc = pagerAllocatePage(pPager, &pPg);
3661 if( rc!=SQLITE_OK ){
3662 return rc;
drhed7c8552001-04-11 14:29:21 +00003663 }
danielk197724168722007-04-02 05:07:47 +00003664
drhed7c8552001-04-11 14:29:21 +00003665 pPg->pgno = pgno;
danielk1977f35843b2007-04-07 15:03:17 +00003666 assert( !MEMDB || pgno>pPager->stmtSize );
drhf5e7bb52008-02-18 14:47:33 +00003667 pPg->inJournal = sqlite3BitvecTest(pPager->pInJournal, pgno);
3668 pPg->needSync = 0;
danielk1977f35843b2007-04-07 15:03:17 +00003669
drh605ad8f2006-05-03 23:34:05 +00003670 makeClean(pPg);
drhed7c8552001-04-11 14:29:21 +00003671 pPg->nRef = 1;
danielk197775bab7d2006-01-23 13:09:45 +00003672
drhd9b02572001-04-15 00:37:09 +00003673 pPager->nRef++;
drh2e6d11b2003-04-25 15:37:57 +00003674 if( pPager->nExtra>0 ){
drh90f5ecb2004-07-22 01:19:35 +00003675 memset(PGHDR_TO_EXTRA(pPg, pPager), 0, pPager->nExtra);
drh2e6d11b2003-04-25 15:37:57 +00003676 }
danielk1977979f38e2007-03-27 16:19:51 +00003677 nMax = sqlite3PagerPagecount(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00003678 if( pPager->errCode ){
danielk1977efaaf572006-01-16 11:29:19 +00003679 rc = pPager->errCode;
danielk1977ae72d982007-10-03 08:46:44 +00003680 sqlite3PagerUnref(pPg);
drh2e6d11b2003-04-25 15:37:57 +00003681 return rc;
3682 }
danielk197775bab7d2006-01-23 13:09:45 +00003683
3684 /* Populate the page with data, either by reading from the database
3685 ** file, or by setting the entire page to zero.
3686 */
drhd215acf2007-04-13 04:01:58 +00003687 if( nMax<(int)pgno || MEMDB || (noContent && !pPager->alwaysRollback) ){
drhf8e632b2007-05-08 14:51:36 +00003688 if( pgno>pPager->mxPgno ){
danielk1977de3bea72007-05-09 15:56:39 +00003689 sqlite3PagerUnref(pPg);
drhf8e632b2007-05-08 14:51:36 +00003690 return SQLITE_FULL;
3691 }
drh90f5ecb2004-07-22 01:19:35 +00003692 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
drhd215acf2007-04-13 04:01:58 +00003693 pPg->needRead = noContent && !pPager->alwaysRollback;
drh538f5702007-04-13 02:14:30 +00003694 IOTRACE(("ZERO %p %d\n", pPager, pgno));
drh306dc212001-05-21 13:45:10 +00003695 }else{
danielk1977e180dd92007-04-05 17:15:52 +00003696 rc = readDbPage(pPager, pPg, pgno);
drh551b7732006-11-06 21:20:25 +00003697 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
3698 pPg->pgno = 0;
danielk19773b8a05f2007-03-19 17:44:26 +00003699 sqlite3PagerUnref(pPg);
drh551b7732006-11-06 21:20:25 +00003700 return rc;
drh81a20f22001-10-12 17:30:04 +00003701 }
danielk1977b4626a32007-04-28 15:47:43 +00003702 pPg->needRead = 0;
drh306dc212001-05-21 13:45:10 +00003703 }
danielk197775bab7d2006-01-23 13:09:45 +00003704
3705 /* Link the page into the page hash table */
drh8ca0c722006-05-07 17:49:38 +00003706 h = pgno & (pPager->nHash-1);
drh3765df42006-06-28 18:18:09 +00003707 assert( pgno!=0 );
danielk197775bab7d2006-01-23 13:09:45 +00003708 pPg->pNextHash = pPager->aHash[h];
3709 pPager->aHash[h] = pPg;
3710 if( pPg->pNextHash ){
3711 assert( pPg->pNextHash->pPrevHash==0 );
3712 pPg->pNextHash->pPrevHash = pPg;
3713 }
3714
danielk19773c407372005-02-15 02:54:14 +00003715#ifdef SQLITE_CHECK_PAGES
3716 pPg->pageHash = pager_pagehash(pPg);
3717#endif
drhed7c8552001-04-11 14:29:21 +00003718 }else{
drhd9b02572001-04-15 00:37:09 +00003719 /* The requested page is in the page cache. */
danielk1977e277be02007-03-23 18:12:06 +00003720 assert(pPager->nRef>0 || pgno==1);
drh538f5702007-04-13 02:14:30 +00003721 PAGER_INCR(pPager->nHit);
drhd33d5a82007-04-26 12:11:28 +00003722 if( !noContent ){
3723 rc = pager_get_content(pPg);
3724 if( rc ){
3725 return rc;
3726 }
3727 }
drhdf0b3b02001-06-23 11:36:20 +00003728 page_ref(pPg);
drhed7c8552001-04-11 14:29:21 +00003729 }
danielk19773b8a05f2007-03-19 17:44:26 +00003730 *ppPage = pPg;
drhed7c8552001-04-11 14:29:21 +00003731 return SQLITE_OK;
3732}
drh86f8c192007-08-22 00:39:19 +00003733int sqlite3PagerAcquire(
3734 Pager *pPager, /* The pager open on the database file */
3735 Pgno pgno, /* Page number to fetch */
3736 DbPage **ppPage, /* Write a pointer to the page here */
3737 int noContent /* Do not bother reading content from disk if true */
3738){
3739 int rc;
3740 pagerEnter(pPager);
3741 rc = pagerAcquire(pPager, pgno, ppPage, noContent);
3742 pagerLeave(pPager);
3743 return rc;
3744}
3745
drhed7c8552001-04-11 14:29:21 +00003746
3747/*
drh7e3b0a02001-04-28 16:52:40 +00003748** Acquire a page if it is already in the in-memory cache. Do
3749** not read the page from disk. Return a pointer to the page,
3750** or 0 if the page is not in cache.
3751**
danielk19773b8a05f2007-03-19 17:44:26 +00003752** See also sqlite3PagerGet(). The difference between this routine
3753** and sqlite3PagerGet() is that _get() will go to the disk and read
drh7e3b0a02001-04-28 16:52:40 +00003754** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00003755** returns NULL if the page is not in cache or if a disk I/O error
3756** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00003757*/
danielk19773b8a05f2007-03-19 17:44:26 +00003758DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
drh86f8c192007-08-22 00:39:19 +00003759 PgHdr *pPg = 0;
drh7e3b0a02001-04-28 16:52:40 +00003760
drh836faa42003-01-11 13:30:57 +00003761 assert( pPager!=0 );
3762 assert( pgno!=0 );
danielk1977e277be02007-03-23 18:12:06 +00003763
drh86f8c192007-08-22 00:39:19 +00003764 pagerEnter(pPager);
danielk1977e277be02007-03-23 18:12:06 +00003765 if( pPager->state==PAGER_UNLOCK ){
danielk1977334cdb62007-03-26 08:05:12 +00003766 assert( !pPager->pAll || pPager->exclusiveMode );
drh86f8c192007-08-22 00:39:19 +00003767 }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
3768 /* Do nothing */
3769 }else if( (pPg = pager_lookup(pPager, pgno))!=0 ){
3770 page_ref(pPg);
danielk1977e277be02007-03-23 18:12:06 +00003771 }
drh86f8c192007-08-22 00:39:19 +00003772 pagerLeave(pPager);
danielk19773b8a05f2007-03-19 17:44:26 +00003773 return pPg;
drh7e3b0a02001-04-28 16:52:40 +00003774}
3775
3776/*
drhed7c8552001-04-11 14:29:21 +00003777** Release a page.
3778**
3779** If the number of references to the page drop to zero, then the
3780** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00003781** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00003782** removed.
3783*/
danielk19773b8a05f2007-03-19 17:44:26 +00003784int sqlite3PagerUnref(DbPage *pPg){
danielk1977ae72d982007-10-03 08:46:44 +00003785 Pager *pPager = pPg->pPager;
drhd9b02572001-04-15 00:37:09 +00003786
3787 /* Decrement the reference count for this page
3788 */
drhed7c8552001-04-11 14:29:21 +00003789 assert( pPg->nRef>0 );
drh86f8c192007-08-22 00:39:19 +00003790 pagerEnter(pPg->pPager);
drhed7c8552001-04-11 14:29:21 +00003791 pPg->nRef--;
drhd9b02572001-04-15 00:37:09 +00003792
danielk19773c407372005-02-15 02:54:14 +00003793 CHECK_PAGE(pPg);
3794
drh72f82862001-05-24 21:06:34 +00003795 /* When the number of references to a page reach 0, call the
3796 ** destructor and add the page to the freelist.
drhd9b02572001-04-15 00:37:09 +00003797 */
drhed7c8552001-04-11 14:29:21 +00003798 if( pPg->nRef==0 ){
danielk19779f61c2f2007-08-27 17:27:49 +00003799
3800 lruListAdd(pPg);
drh72f82862001-05-24 21:06:34 +00003801 if( pPager->xDestructor ){
danielk19773b8a05f2007-03-19 17:44:26 +00003802 pPager->xDestructor(pPg, pPager->pageSize);
drh72f82862001-05-24 21:06:34 +00003803 }
drhd9b02572001-04-15 00:37:09 +00003804
3805 /* When all pages reach the freelist, drop the read lock from
3806 ** the database file.
3807 */
3808 pPager->nRef--;
3809 assert( pPager->nRef>=0 );
danielk19773dedc192007-03-27 17:37:31 +00003810 if( pPager->nRef==0 && (!pPager->exclusiveMode || pPager->journalOff>0) ){
danielk1977e277be02007-03-23 18:12:06 +00003811 pagerUnlockAndRollback(pPager);
drhd9b02572001-04-15 00:37:09 +00003812 }
drhed7c8552001-04-11 14:29:21 +00003813 }
danielk1977ae72d982007-10-03 08:46:44 +00003814 pagerLeave(pPager);
drhd9b02572001-04-15 00:37:09 +00003815 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00003816}
3817
3818/*
drha6abd042004-06-09 17:37:22 +00003819** Create a journal file for pPager. There should already be a RESERVED
3820** or EXCLUSIVE lock on the database file when this routine is called.
drhda47d772002-12-02 04:25:19 +00003821**
3822** Return SQLITE_OK if everything. Return an error code and release the
3823** write lock if anything goes wrong.
3824*/
3825static int pager_open_journal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00003826 sqlite3_vfs *pVfs = pPager->pVfs;
3827 int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE);
3828
drhda47d772002-12-02 04:25:19 +00003829 int rc;
drhb7f91642004-10-31 02:22:47 +00003830 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +00003831 assert( pPager->state>=PAGER_RESERVED );
drhda47d772002-12-02 04:25:19 +00003832 assert( pPager->journalOpen==0 );
3833 assert( pPager->useJournal );
drhf5e7bb52008-02-18 14:47:33 +00003834 assert( pPager->pInJournal==0 );
danielk19773b8a05f2007-03-19 17:44:26 +00003835 sqlite3PagerPagecount(pPager);
drhed138fb2007-08-22 22:04:37 +00003836 pagerLeave(pPager);
drhf5e7bb52008-02-18 14:47:33 +00003837 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
drhed138fb2007-08-22 22:04:37 +00003838 pagerEnter(pPager);
drhf5e7bb52008-02-18 14:47:33 +00003839 if( pPager->pInJournal==0 ){
drh9c105bb2004-10-02 20:38:28 +00003840 rc = SQLITE_NOMEM;
3841 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003842 }
danielk1977b4b47412007-08-17 15:53:36 +00003843
3844 if( pPager->tempFile ){
danielk1977fee2d252007-08-18 10:59:19 +00003845 flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
3846 }else{
3847 flags |= (SQLITE_OPEN_MAIN_JOURNAL);
danielk1977b4b47412007-08-17 15:53:36 +00003848 }
danielk1977c7b60172007-08-22 11:22:03 +00003849#ifdef SQLITE_ENABLE_ATOMIC_WRITE
3850 rc = sqlite3JournalOpen(
3851 pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
3852 );
3853#else
danielk1977b4b47412007-08-17 15:53:36 +00003854 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
danielk1977c7b60172007-08-22 11:22:03 +00003855#endif
danielk1977b4b47412007-08-17 15:53:36 +00003856 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
danielk197776572402004-06-25 02:38:54 +00003857 pPager->journalOff = 0;
3858 pPager->setMaster = 0;
3859 pPager->journalHdr = 0;
drhda47d772002-12-02 04:25:19 +00003860 if( rc!=SQLITE_OK ){
drh600e46a2007-03-28 01:59:33 +00003861 if( rc==SQLITE_NOMEM ){
danielk1977fee2d252007-08-18 10:59:19 +00003862 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
drh600e46a2007-03-28 01:59:33 +00003863 }
drh9c105bb2004-10-02 20:38:28 +00003864 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003865 }
3866 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00003867 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00003868 pPager->needSync = 0;
3869 pPager->alwaysRollback = 0;
drh968af522003-02-11 14:55:40 +00003870 pPager->nRec = 0;
danielk1977efaaf572006-01-16 11:29:19 +00003871 if( pPager->errCode ){
3872 rc = pPager->errCode;
drhdd5b2fa2005-03-28 03:39:55 +00003873 goto failed_to_open_journal;
drh2e6d11b2003-04-25 15:37:57 +00003874 }
drhda47d772002-12-02 04:25:19 +00003875 pPager->origDbSize = pPager->dbSize;
drhae2b40c2004-06-09 19:03:54 +00003876
danielk197776572402004-06-25 02:38:54 +00003877 rc = writeJournalHdr(pPager);
3878
drhac69b052004-05-12 13:30:07 +00003879 if( pPager->stmtAutoopen && rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00003880 rc = sqlite3PagerStmtBegin(pPager);
drhda47d772002-12-02 04:25:19 +00003881 }
danielk1977ae72d982007-10-03 08:46:44 +00003882 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_NOMEM ){
drh80e35f42007-03-30 14:06:34 +00003883 rc = pager_end_transaction(pPager);
drhda47d772002-12-02 04:25:19 +00003884 if( rc==SQLITE_OK ){
3885 rc = SQLITE_FULL;
3886 }
3887 }
drh9c105bb2004-10-02 20:38:28 +00003888 return rc;
3889
3890failed_to_open_journal:
drhf5e7bb52008-02-18 14:47:33 +00003891 sqlite3BitvecDestroy(pPager->pInJournal);
3892 pPager->pInJournal = 0;
drh9c105bb2004-10-02 20:38:28 +00003893 return rc;
drhda47d772002-12-02 04:25:19 +00003894}
3895
3896/*
drh4b845d72002-03-05 12:41:19 +00003897** Acquire a write-lock on the database. The lock is removed when
3898** the any of the following happen:
3899**
drh80e35f42007-03-30 14:06:34 +00003900** * sqlite3PagerCommitPhaseTwo() is called.
danielk19773b8a05f2007-03-19 17:44:26 +00003901** * sqlite3PagerRollback() is called.
3902** * sqlite3PagerClose() is called.
3903** * sqlite3PagerUnref() is called to on every outstanding page.
drh4b845d72002-03-05 12:41:19 +00003904**
danielk197713adf8a2004-06-03 16:08:41 +00003905** The first parameter to this routine is a pointer to any open page of the
3906** database file. Nothing changes about the page - it is used merely to
3907** acquire a pointer to the Pager structure and as proof that there is
3908** already a read-lock on the database.
drh4b845d72002-03-05 12:41:19 +00003909**
danielk197713adf8a2004-06-03 16:08:41 +00003910** The second parameter indicates how much space in bytes to reserve for a
3911** master journal file-name at the start of the journal when it is created.
3912**
3913** A journal file is opened if this is not a temporary file. For temporary
3914** files, the opening of the journal file is deferred until there is an
3915** actual need to write to the journal.
drhda47d772002-12-02 04:25:19 +00003916**
drha6abd042004-06-09 17:37:22 +00003917** If the database is already reserved for writing, this routine is a no-op.
drh684917c2004-10-05 02:41:42 +00003918**
3919** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file
3920** immediately instead of waiting until we try to flush the cache. The
3921** exFlag is ignored if a transaction is already active.
drh4b845d72002-03-05 12:41:19 +00003922*/
danielk19773b8a05f2007-03-19 17:44:26 +00003923int sqlite3PagerBegin(DbPage *pPg, int exFlag){
drh4b845d72002-03-05 12:41:19 +00003924 Pager *pPager = pPg->pPager;
3925 int rc = SQLITE_OK;
drh86f8c192007-08-22 00:39:19 +00003926 pagerEnter(pPager);
drh4b845d72002-03-05 12:41:19 +00003927 assert( pPg->nRef>0 );
drha6abd042004-06-09 17:37:22 +00003928 assert( pPager->state!=PAGER_UNLOCK );
3929 if( pPager->state==PAGER_SHARED ){
drhf5e7bb52008-02-18 14:47:33 +00003930 assert( pPager->pInJournal==0 );
drhb7f91642004-10-31 02:22:47 +00003931 if( MEMDB ){
drha6abd042004-06-09 17:37:22 +00003932 pPager->state = PAGER_EXCLUSIVE;
drhac69b052004-05-12 13:30:07 +00003933 pPager->origDbSize = pPager->dbSize;
3934 }else{
drh054889e2005-11-30 03:20:31 +00003935 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
drh684917c2004-10-05 02:41:42 +00003936 if( rc==SQLITE_OK ){
3937 pPager->state = PAGER_RESERVED;
3938 if( exFlag ){
3939 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
3940 }
3941 }
danielk19779eed5052004-06-04 10:38:30 +00003942 if( rc!=SQLITE_OK ){
drh86f8c192007-08-22 00:39:19 +00003943 pagerLeave(pPager);
danielk19779eed5052004-06-04 10:38:30 +00003944 return rc;
drhac69b052004-05-12 13:30:07 +00003945 }
drha6abd042004-06-09 17:37:22 +00003946 pPager->dirtyCache = 0;
drh4f0c5872007-03-26 22:05:01 +00003947 PAGERTRACE2("TRANSACTION %d\n", PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00003948 if( pPager->useJournal && !pPager->tempFile ){
3949 rc = pager_open_journal(pPager);
3950 }
drh4b845d72002-03-05 12:41:19 +00003951 }
danielk1977334cdb62007-03-26 08:05:12 +00003952 }else if( pPager->journalOpen && pPager->journalOff==0 ){
3953 /* This happens when the pager was in exclusive-access mode last
3954 ** time a (read or write) transaction was successfully concluded
3955 ** by this connection. Instead of deleting the journal file it was
3956 ** kept open and truncated to 0 bytes.
3957 */
3958 assert( pPager->nRec==0 );
3959 assert( pPager->origDbSize==0 );
drhf5e7bb52008-02-18 14:47:33 +00003960 assert( pPager->pInJournal==0 );
danielk1977334cdb62007-03-26 08:05:12 +00003961 sqlite3PagerPagecount(pPager);
drhed138fb2007-08-22 22:04:37 +00003962 pagerLeave(pPager);
drhf5e7bb52008-02-18 14:47:33 +00003963 pPager->pInJournal = sqlite3BitvecCreate( pPager->dbSize );
drhed138fb2007-08-22 22:04:37 +00003964 pagerEnter(pPager);
drhf5e7bb52008-02-18 14:47:33 +00003965 if( !pPager->pInJournal ){
danielk1977334cdb62007-03-26 08:05:12 +00003966 rc = SQLITE_NOMEM;
3967 }else{
drh369339d2007-03-30 16:01:55 +00003968 pPager->origDbSize = pPager->dbSize;
danielk1977334cdb62007-03-26 08:05:12 +00003969 rc = writeJournalHdr(pPager);
3970 }
drh4b845d72002-03-05 12:41:19 +00003971 }
danielk1977334cdb62007-03-26 08:05:12 +00003972 assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK );
drh86f8c192007-08-22 00:39:19 +00003973 pagerLeave(pPager);
drh4b845d72002-03-05 12:41:19 +00003974 return rc;
3975}
3976
3977/*
drh605ad8f2006-05-03 23:34:05 +00003978** Make a page dirty. Set its dirty flag and add it to the dirty
3979** page list.
3980*/
3981static void makeDirty(PgHdr *pPg){
3982 if( pPg->dirty==0 ){
3983 Pager *pPager = pPg->pPager;
3984 pPg->dirty = 1;
3985 pPg->pDirty = pPager->pDirty;
3986 if( pPager->pDirty ){
3987 pPager->pDirty->pPrevDirty = pPg;
3988 }
3989 pPg->pPrevDirty = 0;
3990 pPager->pDirty = pPg;
3991 }
3992}
3993
3994/*
3995** Make a page clean. Clear its dirty bit and remove it from the
3996** dirty page list.
3997*/
3998static void makeClean(PgHdr *pPg){
3999 if( pPg->dirty ){
4000 pPg->dirty = 0;
4001 if( pPg->pDirty ){
drh153c62c2007-08-24 03:51:33 +00004002 assert( pPg->pDirty->pPrevDirty==pPg );
drh605ad8f2006-05-03 23:34:05 +00004003 pPg->pDirty->pPrevDirty = pPg->pPrevDirty;
4004 }
4005 if( pPg->pPrevDirty ){
drh153c62c2007-08-24 03:51:33 +00004006 assert( pPg->pPrevDirty->pDirty==pPg );
drh605ad8f2006-05-03 23:34:05 +00004007 pPg->pPrevDirty->pDirty = pPg->pDirty;
4008 }else{
drh153c62c2007-08-24 03:51:33 +00004009 assert( pPg->pPager->pDirty==pPg );
drh605ad8f2006-05-03 23:34:05 +00004010 pPg->pPager->pDirty = pPg->pDirty;
4011 }
4012 }
4013}
4014
4015
4016/*
drhed7c8552001-04-11 14:29:21 +00004017** Mark a data page as writeable. The page is written into the journal
4018** if it is not there already. This routine must be called before making
4019** changes to a page.
4020**
4021** The first time this routine is called, the pager creates a new
drha6abd042004-06-09 17:37:22 +00004022** journal and acquires a RESERVED lock on the database. If the RESERVED
drhed7c8552001-04-11 14:29:21 +00004023** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00004024** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00004025** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00004026**
4027** If the journal file could not be written because the disk is full,
4028** then this routine returns SQLITE_FULL and does an immediate rollback.
4029** All subsequent write attempts also return SQLITE_FULL until there
danielk19773b8a05f2007-03-19 17:44:26 +00004030** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to
drhd9b02572001-04-15 00:37:09 +00004031** reset.
drhed7c8552001-04-11 14:29:21 +00004032*/
danielk19773b8a05f2007-03-19 17:44:26 +00004033static int pager_write(PgHdr *pPg){
4034 void *pData = PGHDR_TO_DATA(pPg);
drh69688d52001-04-14 16:38:23 +00004035 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00004036 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00004037
drh6446c4d2001-12-15 14:22:18 +00004038 /* Check for errors
4039 */
danielk1977efaaf572006-01-16 11:29:19 +00004040 if( pPager->errCode ){
4041 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004042 }
drh5e00f6c2001-09-13 13:46:56 +00004043 if( pPager->readOnly ){
4044 return SQLITE_PERM;
4045 }
drh6446c4d2001-12-15 14:22:18 +00004046
danielk197776572402004-06-25 02:38:54 +00004047 assert( !pPager->setMaster );
4048
danielk19773c407372005-02-15 02:54:14 +00004049 CHECK_PAGE(pPg);
4050
drh538f5702007-04-13 02:14:30 +00004051 /* If this page was previously acquired with noContent==1, that means
4052 ** we didn't really read in the content of the page. This can happen
4053 ** (for example) when the page is being moved to the freelist. But
4054 ** now we are (perhaps) moving the page off of the freelist for
4055 ** reuse and we need to know its original content so that content
4056 ** can be stored in the rollback journal. So do the read at this
4057 ** time.
4058 */
drhd33d5a82007-04-26 12:11:28 +00004059 rc = pager_get_content(pPg);
4060 if( rc ){
4061 return rc;
drh538f5702007-04-13 02:14:30 +00004062 }
4063
drh6446c4d2001-12-15 14:22:18 +00004064 /* Mark the page as dirty. If the page has already been written
4065 ** to the journal then we can return right away.
4066 */
drh605ad8f2006-05-03 23:34:05 +00004067 makeDirty(pPg);
danielk1977f35843b2007-04-07 15:03:17 +00004068 if( pPg->inJournal && (pageInStatement(pPg) || pPager->stmtInUse==0) ){
drha6abd042004-06-09 17:37:22 +00004069 pPager->dirtyCache = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00004070 }else{
drh6446c4d2001-12-15 14:22:18 +00004071
danielk1977a0bf2652004-11-04 14:30:04 +00004072 /* If we get this far, it means that the page needs to be
4073 ** written to the transaction journal or the ckeckpoint journal
4074 ** or both.
4075 **
4076 ** First check to see that the transaction journal exists and
4077 ** create it if it does not.
4078 */
4079 assert( pPager->state!=PAGER_UNLOCK );
danielk19773b8a05f2007-03-19 17:44:26 +00004080 rc = sqlite3PagerBegin(pPg, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00004081 if( rc!=SQLITE_OK ){
4082 return rc;
4083 }
4084 assert( pPager->state>=PAGER_RESERVED );
4085 if( !pPager->journalOpen && pPager->useJournal ){
4086 rc = pager_open_journal(pPager);
4087 if( rc!=SQLITE_OK ) return rc;
4088 }
4089 assert( pPager->journalOpen || !pPager->useJournal );
4090 pPager->dirtyCache = 1;
4091
4092 /* The transaction journal now exists and we have a RESERVED or an
4093 ** EXCLUSIVE lock on the main database file. Write the current page to
4094 ** the transaction journal if it is not there already.
4095 */
4096 if( !pPg->inJournal && (pPager->useJournal || MEMDB) ){
4097 if( (int)pPg->pgno <= pPager->origDbSize ){
danielk1977a0bf2652004-11-04 14:30:04 +00004098 if( MEMDB ){
4099 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
drh4f0c5872007-03-26 22:05:01 +00004100 PAGERTRACE3("JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00004101 assert( pHist->pOrig==0 );
drh17435752007-08-16 04:30:38 +00004102 pHist->pOrig = sqlite3_malloc( pPager->pageSize );
danielk1977662278e2007-11-05 15:30:12 +00004103 if( !pHist->pOrig ){
4104 return SQLITE_NOMEM;
danielk1977a0bf2652004-11-04 14:30:04 +00004105 }
danielk1977662278e2007-11-05 15:30:12 +00004106 memcpy(pHist->pOrig, PGHDR_TO_DATA(pPg), pPager->pageSize);
danielk1977a0bf2652004-11-04 14:30:04 +00004107 }else{
drhbf4bca52007-09-06 22:19:14 +00004108 u32 cksum;
4109 char *pData2;
danielk1977dd97a492007-08-22 18:54:32 +00004110
drh267cb322005-09-16 17:16:52 +00004111 /* We should never write to the journal file the page that
4112 ** contains the database locks. The following assert verifies
4113 ** that we do not. */
4114 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
drhc001c582006-03-06 18:23:16 +00004115 pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
drh37527852006-03-16 16:19:56 +00004116 cksum = pager_cksum(pPager, (u8*)pData2);
drhbf4bca52007-09-06 22:19:14 +00004117 rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno);
4118 if( rc==SQLITE_OK ){
4119 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize,
4120 pPager->journalOff + 4);
4121 pPager->journalOff += pPager->pageSize+4;
4122 }
4123 if( rc==SQLITE_OK ){
4124 rc = write32bits(pPager->jfd, pPager->journalOff, cksum);
4125 pPager->journalOff += 4;
4126 }
4127 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
danielk1977667a6c92007-09-10 06:12:04 +00004128 pPager->journalOff, pPager->pageSize));
drh538f5702007-04-13 02:14:30 +00004129 PAGER_INCR(sqlite3_pager_writej_count);
drh477731b2007-06-16 03:06:27 +00004130 PAGERTRACE5("JOURNAL %d page %d needSync=%d hash(%08x)\n",
4131 PAGERID(pPager), pPg->pgno, pPg->needSync, pager_pagehash(pPg));
danielk197707cb5602006-01-20 10:55:05 +00004132
drhfd131da2007-08-07 17:13:03 +00004133 /* An error has occured writing to the journal file. The
danielk197707cb5602006-01-20 10:55:05 +00004134 ** transaction will be rolled back by the layer above.
4135 */
danielk1977a0bf2652004-11-04 14:30:04 +00004136 if( rc!=SQLITE_OK ){
danielk1977a0bf2652004-11-04 14:30:04 +00004137 return rc;
4138 }
danielk197707cb5602006-01-20 10:55:05 +00004139
danielk1977a0bf2652004-11-04 14:30:04 +00004140 pPager->nRec++;
drhf5e7bb52008-02-18 14:47:33 +00004141 assert( pPager->pInJournal!=0 );
4142 sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00004143 pPg->needSync = !pPager->noSync;
4144 if( pPager->stmtInUse ){
drhf5e7bb52008-02-18 14:47:33 +00004145 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00004146 }
drhac69b052004-05-12 13:30:07 +00004147 }
danielk197713adf8a2004-06-03 16:08:41 +00004148 }else{
danielk1977a0bf2652004-11-04 14:30:04 +00004149 pPg->needSync = !pPager->journalStarted && !pPager->noSync;
drh4f0c5872007-03-26 22:05:01 +00004150 PAGERTRACE4("APPEND %d page %d needSync=%d\n",
danielk1977ef73ee92004-11-06 12:26:07 +00004151 PAGERID(pPager), pPg->pgno, pPg->needSync);
danielk1977a0bf2652004-11-04 14:30:04 +00004152 }
4153 if( pPg->needSync ){
4154 pPager->needSync = 1;
4155 }
4156 pPg->inJournal = 1;
4157 }
4158
4159 /* If the statement journal is open and the page is not in it,
4160 ** then write the current page to the statement journal. Note that
4161 ** the statement journal format differs from the standard journal format
4162 ** in that it omits the checksums and the header.
4163 */
danielk1977f35843b2007-04-07 15:03:17 +00004164 if( pPager->stmtInUse
4165 && !pageInStatement(pPg)
4166 && (int)pPg->pgno<=pPager->stmtSize
4167 ){
danielk1977a0bf2652004-11-04 14:30:04 +00004168 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
4169 if( MEMDB ){
4170 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4171 assert( pHist->pStmt==0 );
drh17435752007-08-16 04:30:38 +00004172 pHist->pStmt = sqlite3_malloc( pPager->pageSize );
danielk1977a0bf2652004-11-04 14:30:04 +00004173 if( pHist->pStmt ){
4174 memcpy(pHist->pStmt, PGHDR_TO_DATA(pPg), pPager->pageSize);
4175 }
drh4f0c5872007-03-26 22:05:01 +00004176 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk1977f35843b2007-04-07 15:03:17 +00004177 page_add_to_stmt_list(pPg);
danielk1977a0bf2652004-11-04 14:30:04 +00004178 }else{
danielk197762079062007-08-15 17:08:46 +00004179 i64 offset = pPager->stmtNRec*(4+pPager->pageSize);
drhbf4bca52007-09-06 22:19:14 +00004180 char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
4181 rc = write32bits(pPager->stfd, offset, pPg->pgno);
4182 if( rc==SQLITE_OK ){
4183 rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize, offset+4);
4184 }
drh4f0c5872007-03-26 22:05:01 +00004185 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
drhac69b052004-05-12 13:30:07 +00004186 if( rc!=SQLITE_OK ){
drhac69b052004-05-12 13:30:07 +00004187 return rc;
4188 }
danielk1977a0bf2652004-11-04 14:30:04 +00004189 pPager->stmtNRec++;
drhf5e7bb52008-02-18 14:47:33 +00004190 assert( pPager->pInStmt!=0 );
4191 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
drhdb48ee02003-01-16 13:42:43 +00004192 }
drhd9b02572001-04-15 00:37:09 +00004193 }
drhfa86c412002-02-02 15:01:15 +00004194 }
4195
4196 /* Update the database size and return.
4197 */
drh1aa2d8b2007-01-03 15:34:29 +00004198 assert( pPager->state>=PAGER_SHARED );
drh1ab43002002-01-14 09:28:19 +00004199 if( pPager->dbSize<(int)pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00004200 pPager->dbSize = pPg->pgno;
drhb7f91642004-10-31 02:22:47 +00004201 if( !MEMDB && pPager->dbSize==PENDING_BYTE/pPager->pageSize ){
drh1f595712004-06-15 01:40:29 +00004202 pPager->dbSize++;
4203 }
drh306dc212001-05-21 13:45:10 +00004204 }
drh69688d52001-04-14 16:38:23 +00004205 return rc;
drhed7c8552001-04-11 14:29:21 +00004206}
4207
4208/*
danielk19774099f6e2007-03-19 11:25:20 +00004209** This function is used to mark a data-page as writable. It uses
4210** pager_write() to open a journal file (if it is not already open)
4211** and write the page *pData to the journal.
4212**
4213** The difference between this function and pager_write() is that this
4214** function also deals with the special case where 2 or more pages
4215** fit on a single disk sector. In this case all co-resident pages
4216** must have been written to the journal file before returning.
4217*/
danielk19773b8a05f2007-03-19 17:44:26 +00004218int sqlite3PagerWrite(DbPage *pDbPage){
danielk19774099f6e2007-03-19 11:25:20 +00004219 int rc = SQLITE_OK;
4220
danielk19773b8a05f2007-03-19 17:44:26 +00004221 PgHdr *pPg = pDbPage;
danielk19774099f6e2007-03-19 11:25:20 +00004222 Pager *pPager = pPg->pPager;
4223 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
4224
drh86f8c192007-08-22 00:39:19 +00004225 pagerEnter(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00004226 if( !MEMDB && nPagePerSector>1 ){
4227 Pgno nPageCount; /* Total number of pages in database file */
4228 Pgno pg1; /* First page of the sector pPg is located on. */
4229 int nPage; /* Number of pages starting at pg1 to journal */
4230 int ii;
danielk1977dd97a492007-08-22 18:54:32 +00004231 int needSync = 0;
danielk19774099f6e2007-03-19 11:25:20 +00004232
4233 /* Set the doNotSync flag to 1. This is because we cannot allow a journal
4234 ** header to be written between the pages journaled by this function.
4235 */
4236 assert( pPager->doNotSync==0 );
4237 pPager->doNotSync = 1;
4238
4239 /* This trick assumes that both the page-size and sector-size are
4240 ** an integer power of 2. It sets variable pg1 to the identifier
4241 ** of the first page of the sector pPg is located on.
4242 */
4243 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
4244
danielk19773b8a05f2007-03-19 17:44:26 +00004245 nPageCount = sqlite3PagerPagecount(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00004246 if( pPg->pgno>nPageCount ){
4247 nPage = (pPg->pgno - pg1)+1;
4248 }else if( (pg1+nPagePerSector-1)>nPageCount ){
4249 nPage = nPageCount+1-pg1;
4250 }else{
4251 nPage = nPagePerSector;
4252 }
4253 assert(nPage>0);
4254 assert(pg1<=pPg->pgno);
4255 assert((pg1+nPage)>pPg->pgno);
4256
4257 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
4258 Pgno pg = pg1+ii;
danielk1977dd97a492007-08-22 18:54:32 +00004259 PgHdr *pPage;
drhf5e7bb52008-02-18 14:47:33 +00004260 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
danielk19774099f6e2007-03-19 11:25:20 +00004261 if( pg!=PAGER_MJ_PGNO(pPager) ){
danielk19773b8a05f2007-03-19 17:44:26 +00004262 rc = sqlite3PagerGet(pPager, pg, &pPage);
danielk19774099f6e2007-03-19 11:25:20 +00004263 if( rc==SQLITE_OK ){
4264 rc = pager_write(pPage);
danielk1977dd97a492007-08-22 18:54:32 +00004265 if( pPage->needSync ){
4266 needSync = 1;
4267 }
danielk19773b8a05f2007-03-19 17:44:26 +00004268 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00004269 }
4270 }
drhc81945e2008-03-10 14:12:53 +00004271 }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
danielk1977dd97a492007-08-22 18:54:32 +00004272 if( pPage->needSync ){
4273 needSync = 1;
4274 }
danielk19774099f6e2007-03-19 11:25:20 +00004275 }
4276 }
4277
danielk1977dd97a492007-08-22 18:54:32 +00004278 /* If the PgHdr.needSync flag is set for any of the nPage pages
4279 ** starting at pg1, then it needs to be set for all of them. Because
4280 ** writing to any of these nPage pages may damage the others, the
4281 ** journal file must contain sync()ed copies of all of them
4282 ** before any of them can be written out to the database file.
4283 */
4284 if( needSync ){
4285 for(ii=0; ii<nPage && needSync; ii++){
4286 PgHdr *pPage = pager_lookup(pPager, pg1+ii);
4287 if( pPage ) pPage->needSync = 1;
4288 }
4289 assert(pPager->needSync);
4290 }
4291
danielk19774099f6e2007-03-19 11:25:20 +00004292 assert( pPager->doNotSync==1 );
4293 pPager->doNotSync = 0;
4294 }else{
danielk19773b8a05f2007-03-19 17:44:26 +00004295 rc = pager_write(pDbPage);
danielk19774099f6e2007-03-19 11:25:20 +00004296 }
drh86f8c192007-08-22 00:39:19 +00004297 pagerLeave(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00004298 return rc;
4299}
4300
4301/*
drhaacc5432002-01-06 17:07:40 +00004302** Return TRUE if the page given in the argument was previously passed
danielk19773b8a05f2007-03-19 17:44:26 +00004303** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
drh6019e162001-07-02 17:51:45 +00004304** to change the content of the page.
4305*/
danielk19777d3a6662006-01-23 16:21:05 +00004306#ifndef NDEBUG
danielk19773b8a05f2007-03-19 17:44:26 +00004307int sqlite3PagerIswriteable(DbPage *pPg){
drh6019e162001-07-02 17:51:45 +00004308 return pPg->dirty;
4309}
danielk19777d3a6662006-01-23 16:21:05 +00004310#endif
drh6019e162001-07-02 17:51:45 +00004311
danielk1977b84f96f2005-01-20 11:32:23 +00004312#ifndef SQLITE_OMIT_VACUUM
drh6019e162001-07-02 17:51:45 +00004313/*
drh001bbcb2003-03-19 03:14:00 +00004314** Replace the content of a single page with the information in the third
4315** argument.
4316*/
danielk19773b8a05f2007-03-19 17:44:26 +00004317int sqlite3PagerOverwrite(Pager *pPager, Pgno pgno, void *pData){
4318 PgHdr *pPg;
drh001bbcb2003-03-19 03:14:00 +00004319 int rc;
4320
drh86f8c192007-08-22 00:39:19 +00004321 pagerEnter(pPager);
danielk19773b8a05f2007-03-19 17:44:26 +00004322 rc = sqlite3PagerGet(pPager, pgno, &pPg);
drh001bbcb2003-03-19 03:14:00 +00004323 if( rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00004324 rc = sqlite3PagerWrite(pPg);
drh001bbcb2003-03-19 03:14:00 +00004325 if( rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00004326 memcpy(sqlite3PagerGetData(pPg), pData, pPager->pageSize);
drh001bbcb2003-03-19 03:14:00 +00004327 }
danielk19773b8a05f2007-03-19 17:44:26 +00004328 sqlite3PagerUnref(pPg);
drh001bbcb2003-03-19 03:14:00 +00004329 }
drh86f8c192007-08-22 00:39:19 +00004330 pagerLeave(pPager);
drh001bbcb2003-03-19 03:14:00 +00004331 return rc;
4332}
danielk1977b84f96f2005-01-20 11:32:23 +00004333#endif
drh001bbcb2003-03-19 03:14:00 +00004334
4335/*
drh30e58752002-03-02 20:41:57 +00004336** A call to this routine tells the pager that it is not necessary to
drh538f5702007-04-13 02:14:30 +00004337** write the information on page pPg back to the disk, even though
drh30e58752002-03-02 20:41:57 +00004338** that page might be marked as dirty.
4339**
4340** The overlying software layer calls this routine when all of the data
4341** on the given page is unused. The pager marks the page as clean so
4342** that it does not get written to disk.
4343**
4344** Tests show that this optimization, together with the
danielk19773b8a05f2007-03-19 17:44:26 +00004345** sqlite3PagerDontRollback() below, more than double the speed
drh30e58752002-03-02 20:41:57 +00004346** of large INSERT operations and quadruple the speed of large DELETEs.
drh8e298f92002-07-06 16:28:47 +00004347**
4348** When this routine is called, set the alwaysRollback flag to true.
danielk19773b8a05f2007-03-19 17:44:26 +00004349** Subsequent calls to sqlite3PagerDontRollback() for the same page
drh8e298f92002-07-06 16:28:47 +00004350** will thereafter be ignored. This is necessary to avoid a problem
4351** where a page with data is added to the freelist during one part of
4352** a transaction then removed from the freelist during a later part
4353** of the same transaction and reused for some other purpose. When it
4354** is first added to the freelist, this routine is called. When reused,
drh80e35f42007-03-30 14:06:34 +00004355** the sqlite3PagerDontRollback() routine is called. But because the
4356** page contains critical data, we still need to be sure it gets
4357** rolled back in spite of the sqlite3PagerDontRollback() call.
drh30e58752002-03-02 20:41:57 +00004358*/
drh538f5702007-04-13 02:14:30 +00004359void sqlite3PagerDontWrite(DbPage *pDbPage){
4360 PgHdr *pPg = pDbPage;
4361 Pager *pPager = pPg->pPager;
drh8e298f92002-07-06 16:28:47 +00004362
drhb7f91642004-10-31 02:22:47 +00004363 if( MEMDB ) return;
drh86f8c192007-08-22 00:39:19 +00004364 pagerEnter(pPager);
drh8e298f92002-07-06 16:28:47 +00004365 pPg->alwaysRollback = 1;
drh43617e92006-03-06 20:55:46 +00004366 if( pPg->dirty && !pPager->stmtInUse ){
drh1aa2d8b2007-01-03 15:34:29 +00004367 assert( pPager->state>=PAGER_SHARED );
drh8124a302002-06-25 14:43:57 +00004368 if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
4369 /* If this pages is the last page in the file and the file has grown
4370 ** during the current transaction, then do NOT mark the page as clean.
4371 ** When the database file grows, we must make sure that the last page
4372 ** gets written at least once so that the disk file will be the correct
4373 ** size. If you do not write this page and the size of the file
4374 ** on the disk ends up being too small, that can lead to database
4375 ** corruption during the next transaction.
4376 */
4377 }else{
drh538f5702007-04-13 02:14:30 +00004378 PAGERTRACE3("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager));
4379 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
drh605ad8f2006-05-03 23:34:05 +00004380 makeClean(pPg);
danielk19773c407372005-02-15 02:54:14 +00004381#ifdef SQLITE_CHECK_PAGES
4382 pPg->pageHash = pager_pagehash(pPg);
4383#endif
drh8124a302002-06-25 14:43:57 +00004384 }
drh30e58752002-03-02 20:41:57 +00004385 }
drh86f8c192007-08-22 00:39:19 +00004386 pagerLeave(pPager);
drh30e58752002-03-02 20:41:57 +00004387}
4388
4389/*
4390** A call to this routine tells the pager that if a rollback occurs,
4391** it is not necessary to restore the data on the given page. This
4392** means that the pager does not have to record the given page in the
4393** rollback journal.
drh538f5702007-04-13 02:14:30 +00004394**
4395** If we have not yet actually read the content of this page (if
4396** the PgHdr.needRead flag is set) then this routine acts as a promise
4397** that we will never need to read the page content in the future.
4398** so the needRead flag can be cleared at this point.
danielk1977a55e9352008-01-21 13:04:34 +00004399**
4400** This routine is only called from a single place in the sqlite btree
4401** code (when a leaf is removed from the free-list). This allows the
4402** following assumptions to be made about pPg:
4403**
4404** 1. PagerDontWrite() has been called on the page, OR
4405** PagerWrite() has not yet been called on the page.
4406**
4407** 2. The page existed when the transaction was started.
4408**
4409** Details: DontRollback() (this routine) is only called when a leaf is
4410** removed from the free list. DontWrite() is called whenever a page
4411** becomes a free-list leaf.
drh30e58752002-03-02 20:41:57 +00004412*/
danielk19773b8a05f2007-03-19 17:44:26 +00004413void sqlite3PagerDontRollback(DbPage *pPg){
drh30e58752002-03-02 20:41:57 +00004414 Pager *pPager = pPg->pPager;
4415
drh86f8c192007-08-22 00:39:19 +00004416 pagerEnter(pPager);
drhd3627af2006-12-18 18:34:51 +00004417 assert( pPager->state>=PAGER_RESERVED );
danielk1977a55e9352008-01-21 13:04:34 +00004418
4419 /* If the journal file is not open, or DontWrite() has been called on
4420 ** this page (DontWrite() sets the alwaysRollback flag), then this
4421 ** function is a no-op.
4422 */
4423 if( pPager->journalOpen==0 || pPg->alwaysRollback || pPager->alwaysRollback ){
danielk197787c29a92008-01-18 11:33:16 +00004424 pagerLeave(pPager);
4425 return;
4426 }
danielk1977a55e9352008-01-21 13:04:34 +00004427 assert( !MEMDB ); /* For a memdb, pPager->journalOpen is always 0 */
4428
4429 /* Check that PagerWrite() has not yet been called on this page, and
4430 ** that the page existed when the transaction started.
4431 */
4432 assert( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize );
4433
drhf5e7bb52008-02-18 14:47:33 +00004434 assert( pPager->pInJournal!=0 );
4435 sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
danielk1977a55e9352008-01-21 13:04:34 +00004436 pPg->inJournal = 1;
4437 pPg->needRead = 0;
4438 if( pPager->stmtInUse ){
4439 assert( pPager->stmtSize <= pPager->origDbSize );
drhf5e7bb52008-02-18 14:47:33 +00004440 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
drh30e58752002-03-02 20:41:57 +00004441 }
danielk1977a55e9352008-01-21 13:04:34 +00004442 PAGERTRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager));
4443 IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno))
drh86f8c192007-08-22 00:39:19 +00004444 pagerLeave(pPager);
drh30e58752002-03-02 20:41:57 +00004445}
4446
drhac69b052004-05-12 13:30:07 +00004447
drh30e58752002-03-02 20:41:57 +00004448/*
drh80e35f42007-03-30 14:06:34 +00004449** This routine is called to increment the database file change-counter,
4450** stored at byte 24 of the pager file.
4451*/
danielk1977c7b60172007-08-22 11:22:03 +00004452static int pager_incr_changecounter(Pager *pPager, int isDirect){
drh80e35f42007-03-30 14:06:34 +00004453 PgHdr *pPgHdr;
4454 u32 change_counter;
danielk1977c7b60172007-08-22 11:22:03 +00004455 int rc = SQLITE_OK;
drh80e35f42007-03-30 14:06:34 +00004456
4457 if( !pPager->changeCountDone ){
4458 /* Open page 1 of the file for writing. */
4459 rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
4460 if( rc!=SQLITE_OK ) return rc;
danielk1977c7b60172007-08-22 11:22:03 +00004461
4462 if( !isDirect ){
4463 rc = sqlite3PagerWrite(pPgHdr);
danielk1977ae72d982007-10-03 08:46:44 +00004464 if( rc!=SQLITE_OK ){
4465 sqlite3PagerUnref(pPgHdr);
4466 return rc;
4467 }
danielk1977c7b60172007-08-22 11:22:03 +00004468 }
4469
drh80e35f42007-03-30 14:06:34 +00004470 /* Increment the value just read and write it back to byte 24. */
drhb1003912007-07-20 00:33:36 +00004471 change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
drh80e35f42007-03-30 14:06:34 +00004472 change_counter++;
4473 put32bits(((char*)PGHDR_TO_DATA(pPgHdr))+24, change_counter);
danielk1977c7b60172007-08-22 11:22:03 +00004474
4475 if( isDirect && pPager->fd->pMethods ){
4476 const void *zBuf = PGHDR_TO_DATA(pPgHdr);
4477 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
4478 }
4479
drh80e35f42007-03-30 14:06:34 +00004480 /* Release the page reference. */
4481 sqlite3PagerUnref(pPgHdr);
4482 pPager->changeCountDone = 1;
4483 }
danielk1977c7b60172007-08-22 11:22:03 +00004484 return rc;
drh80e35f42007-03-30 14:06:34 +00004485}
4486
4487/*
4488** Sync the database file for the pager pPager. zMaster points to the name
4489** of a master journal file that should be written into the individual
4490** journal file. zMaster may be NULL, which is interpreted as no master
4491** journal (a single database transaction).
4492**
4493** This routine ensures that the journal is synced, all dirty pages written
4494** to the database file and the database file synced. The only thing that
4495** remains to commit the transaction is to delete the journal file (or
4496** master journal file if specified).
4497**
4498** Note that if zMaster==NULL, this does not overwrite a previous value
4499** passed to an sqlite3PagerCommitPhaseOne() call.
4500**
4501** If parameter nTrunc is non-zero, then the pager file is truncated to
4502** nTrunc pages (this is used by auto-vacuum databases).
4503*/
4504int sqlite3PagerCommitPhaseOne(Pager *pPager, const char *zMaster, Pgno nTrunc){
4505 int rc = SQLITE_OK;
4506
4507 PAGERTRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n",
4508 pPager->zFilename, zMaster, nTrunc);
drh86f8c192007-08-22 00:39:19 +00004509 pagerEnter(pPager);
drh80e35f42007-03-30 14:06:34 +00004510
4511 /* If this is an in-memory db, or no pages have been written to, or this
4512 ** function has already been called, it is a no-op.
4513 */
4514 if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){
4515 PgHdr *pPg;
drh80e35f42007-03-30 14:06:34 +00004516
danielk1977c7b60172007-08-22 11:22:03 +00004517#ifdef SQLITE_ENABLE_ATOMIC_WRITE
4518 /* The atomic-write optimization can be used if all of the
4519 ** following are true:
4520 **
4521 ** + The file-system supports the atomic-write property for
4522 ** blocks of size page-size, and
4523 ** + This commit is not part of a multi-file transaction, and
4524 ** + Exactly one page has been modified and store in the journal file.
4525 **
4526 ** If the optimization can be used, then the journal file will never
4527 ** be created for this transaction.
4528 */
danielk1977f55b8992007-08-24 08:15:53 +00004529 int useAtomicWrite = (
4530 !zMaster &&
4531 pPager->journalOff==jrnlBufferSize(pPager) &&
4532 nTrunc==0 &&
4533 (0==pPager->pDirty || 0==pPager->pDirty->pDirty)
4534 );
4535 if( useAtomicWrite ){
danielk1977c7b60172007-08-22 11:22:03 +00004536 /* Update the nRec field in the journal file. */
4537 int offset = pPager->journalHdr + sizeof(aJournalMagic);
4538 assert(pPager->nRec==1);
4539 rc = write32bits(pPager->jfd, offset, pPager->nRec);
4540
4541 /* Update the db file change counter. The following call will modify
4542 ** the in-memory representation of page 1 to include the updated
4543 ** change counter and then write page 1 directly to the database
4544 ** file. Because of the atomic-write property of the host file-system,
4545 ** this is safe.
4546 */
danielk1977ae72d982007-10-03 08:46:44 +00004547 if( rc==SQLITE_OK ){
4548 rc = pager_incr_changecounter(pPager, 1);
4549 }
danielk1977f55b8992007-08-24 08:15:53 +00004550 }else{
4551 rc = sqlite3JournalCreate(pPager->jfd);
danielk1977f55b8992007-08-24 08:15:53 +00004552 }
4553
danielk1977ae72d982007-10-03 08:46:44 +00004554 if( !useAtomicWrite && rc==SQLITE_OK )
danielk1977c7b60172007-08-22 11:22:03 +00004555#endif
4556
drh80e35f42007-03-30 14:06:34 +00004557 /* If a master journal file name has already been written to the
4558 ** journal file, then no sync is required. This happens when it is
4559 ** written, then the process fails to upgrade from a RESERVED to an
4560 ** EXCLUSIVE lock. The next time the process tries to commit the
4561 ** transaction the m-j name will have already been written.
4562 */
4563 if( !pPager->setMaster ){
danielk1977f55b8992007-08-24 08:15:53 +00004564 assert( pPager->journalOpen );
danielk1977c7b60172007-08-22 11:22:03 +00004565 rc = pager_incr_changecounter(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00004566 if( rc!=SQLITE_OK ) goto sync_exit;
4567#ifndef SQLITE_OMIT_AUTOVACUUM
4568 if( nTrunc!=0 ){
4569 /* If this transaction has made the database smaller, then all pages
4570 ** being discarded by the truncation must be written to the journal
4571 ** file.
4572 */
4573 Pgno i;
4574 int iSkip = PAGER_MJ_PGNO(pPager);
4575 for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){
drhf5e7bb52008-02-18 14:47:33 +00004576 if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
drh80e35f42007-03-30 14:06:34 +00004577 rc = sqlite3PagerGet(pPager, i, &pPg);
4578 if( rc!=SQLITE_OK ) goto sync_exit;
4579 rc = sqlite3PagerWrite(pPg);
4580 sqlite3PagerUnref(pPg);
4581 if( rc!=SQLITE_OK ) goto sync_exit;
4582 }
4583 }
4584 }
4585#endif
4586 rc = writeMasterJournal(pPager, zMaster);
4587 if( rc!=SQLITE_OK ) goto sync_exit;
4588 rc = syncJournal(pPager);
drh80e35f42007-03-30 14:06:34 +00004589 }
danielk1977c7b60172007-08-22 11:22:03 +00004590 if( rc!=SQLITE_OK ) goto sync_exit;
drh80e35f42007-03-30 14:06:34 +00004591
4592#ifndef SQLITE_OMIT_AUTOVACUUM
4593 if( nTrunc!=0 ){
4594 rc = sqlite3PagerTruncate(pPager, nTrunc);
4595 if( rc!=SQLITE_OK ) goto sync_exit;
4596 }
4597#endif
4598
4599 /* Write all dirty pages to the database file */
4600 pPg = pager_get_all_dirty_pages(pPager);
4601 rc = pager_write_pagelist(pPg);
drh153c62c2007-08-24 03:51:33 +00004602 if( rc!=SQLITE_OK ){
drh04c3a462008-02-26 16:16:45 +00004603 assert( rc!=SQLITE_IOERR_BLOCKED );
4604 /* The error might have left the dirty list all fouled up here,
4605 ** but that does not matter because if the if the dirty list did
4606 ** get corrupted, then the transaction will roll back and
4607 ** discard the dirty list. There is an assert in
4608 ** pager_get_all_dirty_pages() that verifies that no attempt
4609 ** is made to use an invalid dirty list.
4610 */
drh153c62c2007-08-24 03:51:33 +00004611 goto sync_exit;
4612 }
drh3cdd7d32007-03-30 14:46:01 +00004613 pPager->pDirty = 0;
drh80e35f42007-03-30 14:06:34 +00004614
4615 /* Sync the database file. */
drh1cc8c442007-08-24 16:08:29 +00004616 if( !pPager->noSync ){
danielk1977f036aef2007-08-20 05:36:51 +00004617 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
drh80e35f42007-03-30 14:06:34 +00004618 }
4619 IOTRACE(("DBSYNC %p\n", pPager))
4620
4621 pPager->state = PAGER_SYNCED;
4622 }else if( MEMDB && nTrunc!=0 ){
4623 rc = sqlite3PagerTruncate(pPager, nTrunc);
4624 }
4625
4626sync_exit:
danielk1977e965ac72007-06-13 15:22:28 +00004627 if( rc==SQLITE_IOERR_BLOCKED ){
4628 /* pager_incr_changecounter() may attempt to obtain an exclusive
4629 * lock to spill the cache and return IOERR_BLOCKED. But since
drh85b623f2007-12-13 21:54:09 +00004630 * there is no chance the cache is inconsistent, it is
danielk1977e965ac72007-06-13 15:22:28 +00004631 * better to return SQLITE_BUSY.
4632 */
4633 rc = SQLITE_BUSY;
4634 }
drh86f8c192007-08-22 00:39:19 +00004635 pagerLeave(pPager);
drh80e35f42007-03-30 14:06:34 +00004636 return rc;
4637}
4638
4639
4640/*
drhed7c8552001-04-11 14:29:21 +00004641** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00004642**
4643** If the commit fails for any reason, a rollback attempt is made
4644** and an error code is returned. If the commit worked, SQLITE_OK
4645** is returned.
drhed7c8552001-04-11 14:29:21 +00004646*/
drh80e35f42007-03-30 14:06:34 +00004647int sqlite3PagerCommitPhaseTwo(Pager *pPager){
drha1b351a2001-09-14 16:42:12 +00004648 int rc;
drhed7c8552001-04-11 14:29:21 +00004649 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +00004650
danielk1977efaaf572006-01-16 11:29:19 +00004651 if( pPager->errCode ){
danielk19777f7bc662006-01-23 13:47:47 +00004652 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004653 }
drha6abd042004-06-09 17:37:22 +00004654 if( pPager->state<PAGER_RESERVED ){
drhd9b02572001-04-15 00:37:09 +00004655 return SQLITE_ERROR;
4656 }
drh86f8c192007-08-22 00:39:19 +00004657 pagerEnter(pPager);
drh4f0c5872007-03-26 22:05:01 +00004658 PAGERTRACE2("COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004659 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004660 pPg = pager_get_all_dirty_pages(pPager);
4661 while( pPg ){
danielk1977f35843b2007-04-07 15:03:17 +00004662 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4663 clearHistory(pHist);
drhac69b052004-05-12 13:30:07 +00004664 pPg->dirty = 0;
4665 pPg->inJournal = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004666 pHist->inStmt = 0;
drh5229ae42006-03-23 23:29:04 +00004667 pPg->needSync = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004668 pHist->pPrevStmt = pHist->pNextStmt = 0;
drhac69b052004-05-12 13:30:07 +00004669 pPg = pPg->pDirty;
4670 }
drh605ad8f2006-05-03 23:34:05 +00004671 pPager->pDirty = 0;
danielk1977eac7a362004-06-16 07:45:24 +00004672#ifndef NDEBUG
4673 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
4674 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4675 assert( !pPg->alwaysRollback );
4676 assert( !pHist->pOrig );
4677 assert( !pHist->pStmt );
4678 }
4679#endif
drhac69b052004-05-12 13:30:07 +00004680 pPager->pStmt = 0;
drha6abd042004-06-09 17:37:22 +00004681 pPager->state = PAGER_SHARED;
danielk197787c29a92008-01-18 11:33:16 +00004682 pagerLeave(pPager);
drhac69b052004-05-12 13:30:07 +00004683 return SQLITE_OK;
4684 }
drh3cdd7d32007-03-30 14:46:01 +00004685 assert( pPager->journalOpen || !pPager->dirtyCache );
4686 assert( pPager->state==PAGER_SYNCED || !pPager->dirtyCache );
4687 rc = pager_end_transaction(pPager);
drh86f8c192007-08-22 00:39:19 +00004688 rc = pager_error(pPager, rc);
4689 pagerLeave(pPager);
4690 return rc;
drhed7c8552001-04-11 14:29:21 +00004691}
4692
4693/*
drha6abd042004-06-09 17:37:22 +00004694** Rollback all changes. The database falls back to PAGER_SHARED mode.
drhed7c8552001-04-11 14:29:21 +00004695** All in-memory cache pages revert to their original data contents.
4696** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00004697**
4698** This routine cannot fail unless some other process is not following
drh4f0ee682007-03-30 20:43:40 +00004699** the correct locking protocol or unless some other
drhd9b02572001-04-15 00:37:09 +00004700** process is writing trash into the journal file (SQLITE_CORRUPT) or
4701** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
4702** codes are returned for all these occasions. Otherwise,
4703** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00004704*/
danielk19773b8a05f2007-03-19 17:44:26 +00004705int sqlite3PagerRollback(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00004706 int rc;
drh4f0c5872007-03-26 22:05:01 +00004707 PAGERTRACE2("ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004708 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004709 PgHdr *p;
4710 for(p=pPager->pAll; p; p=p->pNextAll){
4711 PgHistory *pHist;
danielk1977eac7a362004-06-16 07:45:24 +00004712 assert( !p->alwaysRollback );
4713 if( !p->dirty ){
4714 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pOrig );
4715 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pStmt );
4716 continue;
4717 }
4718
drhac69b052004-05-12 13:30:07 +00004719 pHist = PGHDR_TO_HIST(p, pPager);
4720 if( pHist->pOrig ){
4721 memcpy(PGHDR_TO_DATA(p), pHist->pOrig, pPager->pageSize);
drh4f0c5872007-03-26 22:05:01 +00004722 PAGERTRACE3("ROLLBACK-PAGE %d of %d\n", p->pgno, PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00004723 }else{
drh4f0c5872007-03-26 22:05:01 +00004724 PAGERTRACE3("PAGE %d is clean on %d\n", p->pgno, PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00004725 }
4726 clearHistory(pHist);
4727 p->dirty = 0;
4728 p->inJournal = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004729 pHist->inStmt = 0;
4730 pHist->pPrevStmt = pHist->pNextStmt = 0;
danielk1977369f27e2004-06-15 11:40:04 +00004731 if( pPager->xReiniter ){
danielk19773b8a05f2007-03-19 17:44:26 +00004732 pPager->xReiniter(p, pPager->pageSize);
danielk1977369f27e2004-06-15 11:40:04 +00004733 }
drhac69b052004-05-12 13:30:07 +00004734 }
drh605ad8f2006-05-03 23:34:05 +00004735 pPager->pDirty = 0;
drhac69b052004-05-12 13:30:07 +00004736 pPager->pStmt = 0;
4737 pPager->dbSize = pPager->origDbSize;
danielk1977e180dd92007-04-05 17:15:52 +00004738 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00004739 pPager->stmtInUse = 0;
drha6abd042004-06-09 17:37:22 +00004740 pPager->state = PAGER_SHARED;
drhac69b052004-05-12 13:30:07 +00004741 return SQLITE_OK;
4742 }
4743
drh86f8c192007-08-22 00:39:19 +00004744 pagerEnter(pPager);
drha6abd042004-06-09 17:37:22 +00004745 if( !pPager->dirtyCache || !pPager->journalOpen ){
drh80e35f42007-03-30 14:06:34 +00004746 rc = pager_end_transaction(pPager);
drh86f8c192007-08-22 00:39:19 +00004747 pagerLeave(pPager);
drhda47d772002-12-02 04:25:19 +00004748 return rc;
4749 }
drhdb48ee02003-01-16 13:42:43 +00004750
danielk1977efaaf572006-01-16 11:29:19 +00004751 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
drha6abd042004-06-09 17:37:22 +00004752 if( pPager->state>=PAGER_EXCLUSIVE ){
danielk1977e277be02007-03-23 18:12:06 +00004753 pager_playback(pPager, 0);
drh4b845d72002-03-05 12:41:19 +00004754 }
drh86f8c192007-08-22 00:39:19 +00004755 pagerLeave(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00004756 return pPager->errCode;
drhed7c8552001-04-11 14:29:21 +00004757 }
drha6abd042004-06-09 17:37:22 +00004758 if( pPager->state==PAGER_RESERVED ){
danielk197717221812005-02-15 03:38:05 +00004759 int rc2;
danielk1977e277be02007-03-23 18:12:06 +00004760 rc = pager_playback(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00004761 rc2 = pager_end_transaction(pPager);
drha6abd042004-06-09 17:37:22 +00004762 if( rc==SQLITE_OK ){
4763 rc = rc2;
4764 }
4765 }else{
danielk1977e277be02007-03-23 18:12:06 +00004766 rc = pager_playback(pPager, 0);
drhd9b02572001-04-15 00:37:09 +00004767 }
danielk1977e180dd92007-04-05 17:15:52 +00004768 /* pager_reset(pPager); */
drhd9b02572001-04-15 00:37:09 +00004769 pPager->dbSize = -1;
danielk197707cb5602006-01-20 10:55:05 +00004770
4771 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
4772 ** cache. So call pager_error() on the way out to make any error
4773 ** persistent.
4774 */
drh86f8c192007-08-22 00:39:19 +00004775 rc = pager_error(pPager, rc);
4776 pagerLeave(pPager);
4777 return rc;
drh98808ba2001-10-18 12:34:46 +00004778}
drhd9b02572001-04-15 00:37:09 +00004779
4780/*
drh5e00f6c2001-09-13 13:46:56 +00004781** Return TRUE if the database file is opened read-only. Return FALSE
4782** if the database is (in theory) writable.
4783*/
danielk19773b8a05f2007-03-19 17:44:26 +00004784int sqlite3PagerIsreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00004785 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00004786}
4787
4788/*
drh0f7eb612006-08-08 13:51:43 +00004789** Return the number of references to the pager.
4790*/
danielk19773b8a05f2007-03-19 17:44:26 +00004791int sqlite3PagerRefcount(Pager *pPager){
drh0f7eb612006-08-08 13:51:43 +00004792 return pPager->nRef;
4793}
4794
4795#ifdef SQLITE_TEST
4796/*
drhd9b02572001-04-15 00:37:09 +00004797** This routine is used for testing and analysis only.
4798*/
danielk19773b8a05f2007-03-19 17:44:26 +00004799int *sqlite3PagerStats(Pager *pPager){
danielk197742741be2005-01-08 12:42:39 +00004800 static int a[11];
drhd9b02572001-04-15 00:37:09 +00004801 a[0] = pPager->nRef;
4802 a[1] = pPager->nPage;
4803 a[2] = pPager->mxPage;
4804 a[3] = pPager->dbSize;
4805 a[4] = pPager->state;
danielk1977efaaf572006-01-16 11:29:19 +00004806 a[5] = pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004807 a[6] = pPager->nHit;
4808 a[7] = pPager->nMiss;
drh7c4ac0c2007-04-05 11:25:58 +00004809 a[8] = 0; /* Used to be pPager->nOvfl */
danielk197742741be2005-01-08 12:42:39 +00004810 a[9] = pPager->nRead;
4811 a[10] = pPager->nWrite;
drhd9b02572001-04-15 00:37:09 +00004812 return a;
4813}
drh0f7eb612006-08-08 13:51:43 +00004814#endif
drhdd793422001-06-28 01:54:48 +00004815
drhfa86c412002-02-02 15:01:15 +00004816/*
drhac69b052004-05-12 13:30:07 +00004817** Set the statement rollback point.
drhfa86c412002-02-02 15:01:15 +00004818**
4819** This routine should be called with the transaction journal already
drhac69b052004-05-12 13:30:07 +00004820** open. A new statement journal is created that can be used to rollback
drhaaab5722002-02-19 13:39:21 +00004821** changes of a single SQL command within a larger transaction.
drhfa86c412002-02-02 15:01:15 +00004822*/
drh86f8c192007-08-22 00:39:19 +00004823static int pagerStmtBegin(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00004824 int rc;
drhac69b052004-05-12 13:30:07 +00004825 assert( !pPager->stmtInUse );
drh1aa2d8b2007-01-03 15:34:29 +00004826 assert( pPager->state>=PAGER_SHARED );
drhdc3ff9c2004-08-18 02:10:15 +00004827 assert( pPager->dbSize>=0 );
drh4f0c5872007-03-26 22:05:01 +00004828 PAGERTRACE2("STMT-BEGIN %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004829 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004830 pPager->stmtInUse = 1;
4831 pPager->stmtSize = pPager->dbSize;
4832 return SQLITE_OK;
4833 }
drhda47d772002-12-02 04:25:19 +00004834 if( !pPager->journalOpen ){
drhac69b052004-05-12 13:30:07 +00004835 pPager->stmtAutoopen = 1;
drhda47d772002-12-02 04:25:19 +00004836 return SQLITE_OK;
4837 }
drhfa86c412002-02-02 15:01:15 +00004838 assert( pPager->journalOpen );
drhed138fb2007-08-22 22:04:37 +00004839 pagerLeave(pPager);
drhf5e7bb52008-02-18 14:47:33 +00004840 assert( pPager->pInStmt==0 );
4841 pPager->pInStmt = sqlite3BitvecCreate(pPager->dbSize);
drhed138fb2007-08-22 22:04:37 +00004842 pagerEnter(pPager);
drhf5e7bb52008-02-18 14:47:33 +00004843 if( pPager->pInStmt==0 ){
danielk1977261919c2005-12-06 12:52:59 +00004844 /* sqlite3OsLock(pPager->fd, SHARED_LOCK); */
drhfa86c412002-02-02 15:01:15 +00004845 return SQLITE_NOMEM;
4846 }
drh968af522003-02-11 14:55:40 +00004847#ifndef NDEBUG
drh054889e2005-11-30 03:20:31 +00004848 rc = sqlite3OsFileSize(pPager->jfd, &pPager->stmtJSize);
drhac69b052004-05-12 13:30:07 +00004849 if( rc ) goto stmt_begin_failed;
danielk197776572402004-06-25 02:38:54 +00004850 assert( pPager->stmtJSize == pPager->journalOff );
drh968af522003-02-11 14:55:40 +00004851#endif
danielk197776572402004-06-25 02:38:54 +00004852 pPager->stmtJSize = pPager->journalOff;
drhac69b052004-05-12 13:30:07 +00004853 pPager->stmtSize = pPager->dbSize;
danielk197776572402004-06-25 02:38:54 +00004854 pPager->stmtHdrOff = 0;
danielk197775edc162004-06-26 01:48:18 +00004855 pPager->stmtCksum = pPager->cksumInit;
drhac69b052004-05-12 13:30:07 +00004856 if( !pPager->stmtOpen ){
drh334b2992007-09-06 23:28:23 +00004857 rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->stfd, pPager->zStmtJrnl,
drh33f4e022007-09-03 15:19:34 +00004858 SQLITE_OPEN_SUBJOURNAL);
drh334b2992007-09-06 23:28:23 +00004859 if( rc ){
4860 goto stmt_begin_failed;
4861 }
drhac69b052004-05-12 13:30:07 +00004862 pPager->stmtOpen = 1;
4863 pPager->stmtNRec = 0;
drh0f892532002-05-30 12:27:03 +00004864 }
drhac69b052004-05-12 13:30:07 +00004865 pPager->stmtInUse = 1;
drhfa86c412002-02-02 15:01:15 +00004866 return SQLITE_OK;
4867
drhac69b052004-05-12 13:30:07 +00004868stmt_begin_failed:
drhf5e7bb52008-02-18 14:47:33 +00004869 if( pPager->pInStmt ){
4870 sqlite3BitvecDestroy(pPager->pInStmt);
4871 pPager->pInStmt = 0;
drhfa86c412002-02-02 15:01:15 +00004872 }
4873 return rc;
4874}
drh86f8c192007-08-22 00:39:19 +00004875int sqlite3PagerStmtBegin(Pager *pPager){
4876 int rc;
4877 pagerEnter(pPager);
4878 rc = pagerStmtBegin(pPager);
4879 pagerLeave(pPager);
4880 return rc;
4881}
drhfa86c412002-02-02 15:01:15 +00004882
4883/*
drhac69b052004-05-12 13:30:07 +00004884** Commit a statement.
drhfa86c412002-02-02 15:01:15 +00004885*/
danielk19773b8a05f2007-03-19 17:44:26 +00004886int sqlite3PagerStmtCommit(Pager *pPager){
drh86f8c192007-08-22 00:39:19 +00004887 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00004888 if( pPager->stmtInUse ){
drh03eb96a2002-11-10 23:32:56 +00004889 PgHdr *pPg, *pNext;
drh4f0c5872007-03-26 22:05:01 +00004890 PAGERTRACE2("STMT-COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004891 if( !MEMDB ){
drh054889e2005-11-30 03:20:31 +00004892 /* sqlite3OsTruncate(pPager->stfd, 0); */
drhf5e7bb52008-02-18 14:47:33 +00004893 sqlite3BitvecDestroy(pPager->pInStmt);
4894 pPager->pInStmt = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004895 }else{
4896 for(pPg=pPager->pStmt; pPg; pPg=pNext){
drhac69b052004-05-12 13:30:07 +00004897 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
danielk1977f35843b2007-04-07 15:03:17 +00004898 pNext = pHist->pNextStmt;
4899 assert( pHist->inStmt );
4900 pHist->inStmt = 0;
4901 pHist->pPrevStmt = pHist->pNextStmt = 0;
drh17435752007-08-16 04:30:38 +00004902 sqlite3_free(pHist->pStmt);
drhac69b052004-05-12 13:30:07 +00004903 pHist->pStmt = 0;
4904 }
4905 }
4906 pPager->stmtNRec = 0;
4907 pPager->stmtInUse = 0;
4908 pPager->pStmt = 0;
drh663fc632002-02-02 18:49:19 +00004909 }
drhac69b052004-05-12 13:30:07 +00004910 pPager->stmtAutoopen = 0;
drh86f8c192007-08-22 00:39:19 +00004911 pagerLeave(pPager);
drhfa86c412002-02-02 15:01:15 +00004912 return SQLITE_OK;
4913}
4914
4915/*
drhac69b052004-05-12 13:30:07 +00004916** Rollback a statement.
drhfa86c412002-02-02 15:01:15 +00004917*/
danielk19773b8a05f2007-03-19 17:44:26 +00004918int sqlite3PagerStmtRollback(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00004919 int rc;
drh86f8c192007-08-22 00:39:19 +00004920 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00004921 if( pPager->stmtInUse ){
drh4f0c5872007-03-26 22:05:01 +00004922 PAGERTRACE2("STMT-ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004923 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004924 PgHdr *pPg;
danielk1977f35843b2007-04-07 15:03:17 +00004925 PgHistory *pHist;
4926 for(pPg=pPager->pStmt; pPg; pPg=pHist->pNextStmt){
4927 pHist = PGHDR_TO_HIST(pPg, pPager);
drhac69b052004-05-12 13:30:07 +00004928 if( pHist->pStmt ){
4929 memcpy(PGHDR_TO_DATA(pPg), pHist->pStmt, pPager->pageSize);
drh17435752007-08-16 04:30:38 +00004930 sqlite3_free(pHist->pStmt);
drhac69b052004-05-12 13:30:07 +00004931 pHist->pStmt = 0;
4932 }
4933 }
4934 pPager->dbSize = pPager->stmtSize;
danielk1977e180dd92007-04-05 17:15:52 +00004935 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00004936 rc = SQLITE_OK;
4937 }else{
4938 rc = pager_stmt_playback(pPager);
4939 }
danielk19773b8a05f2007-03-19 17:44:26 +00004940 sqlite3PagerStmtCommit(pPager);
drh663fc632002-02-02 18:49:19 +00004941 }else{
4942 rc = SQLITE_OK;
4943 }
drhac69b052004-05-12 13:30:07 +00004944 pPager->stmtAutoopen = 0;
drh86f8c192007-08-22 00:39:19 +00004945 pagerLeave(pPager);
drhfa86c412002-02-02 15:01:15 +00004946 return rc;
4947}
4948
drh73509ee2003-04-06 20:44:45 +00004949/*
4950** Return the full pathname of the database file.
4951*/
danielk19773b8a05f2007-03-19 17:44:26 +00004952const char *sqlite3PagerFilename(Pager *pPager){
drh73509ee2003-04-06 20:44:45 +00004953 return pPager->zFilename;
4954}
4955
drhb20ea9d2004-02-09 01:20:36 +00004956/*
drhd0679ed2007-08-28 22:24:34 +00004957** Return the VFS structure for the pager.
4958*/
4959const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
4960 return pPager->pVfs;
4961}
4962
4963/*
drhcc6bb3e2007-08-31 16:11:35 +00004964** Return the file handle for the database file associated
4965** with the pager. This might return NULL if the file has
4966** not yet been opened.
4967*/
4968sqlite3_file *sqlite3PagerFile(Pager *pPager){
4969 return pPager->fd;
4970}
4971
4972/*
danielk19775865e3d2004-06-14 06:03:57 +00004973** Return the directory of the database file.
4974*/
danielk19773b8a05f2007-03-19 17:44:26 +00004975const char *sqlite3PagerDirname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004976 return pPager->zDirectory;
4977}
4978
4979/*
4980** Return the full pathname of the journal file.
4981*/
danielk19773b8a05f2007-03-19 17:44:26 +00004982const char *sqlite3PagerJournalname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004983 return pPager->zJournal;
4984}
4985
4986/*
drh2c8997b2005-08-27 16:36:48 +00004987** Return true if fsync() calls are disabled for this pager. Return FALSE
4988** if fsync()s are executed normally.
4989*/
danielk19773b8a05f2007-03-19 17:44:26 +00004990int sqlite3PagerNosync(Pager *pPager){
drh2c8997b2005-08-27 16:36:48 +00004991 return pPager->noSync;
4992}
4993
drh7c4ac0c2007-04-05 11:25:58 +00004994#ifdef SQLITE_HAS_CODEC
drh2c8997b2005-08-27 16:36:48 +00004995/*
drhb20ea9d2004-02-09 01:20:36 +00004996** Set the codec for this pager
4997*/
danielk19773b8a05f2007-03-19 17:44:26 +00004998void sqlite3PagerSetCodec(
drhb20ea9d2004-02-09 01:20:36 +00004999 Pager *pPager,
drhc001c582006-03-06 18:23:16 +00005000 void *(*xCodec)(void*,void*,Pgno,int),
drhb20ea9d2004-02-09 01:20:36 +00005001 void *pCodecArg
5002){
5003 pPager->xCodec = xCodec;
5004 pPager->pCodecArg = pCodecArg;
5005}
drh7c4ac0c2007-04-05 11:25:58 +00005006#endif
drhb20ea9d2004-02-09 01:20:36 +00005007
danielk1977687566d2004-11-02 12:56:41 +00005008#ifndef SQLITE_OMIT_AUTOVACUUM
5009/*
danielk197787c29a92008-01-18 11:33:16 +00005010** Move the page pPg to location pgno in the file.
danielk1977687566d2004-11-02 12:56:41 +00005011**
drh5e385312007-06-16 04:42:12 +00005012** There must be no references to the page previously located at
5013** pgno (which we call pPgOld) though that page is allowed to be
5014** in cache. If the page previous located at pgno is not already
5015** in the rollback journal, it is not put there by by this routine.
danielk1977687566d2004-11-02 12:56:41 +00005016**
drh5e385312007-06-16 04:42:12 +00005017** References to the page pPg remain valid. Updating any
5018** meta-data associated with pPg (i.e. data stored in the nExtra bytes
danielk1977687566d2004-11-02 12:56:41 +00005019** allocated along with the page) is the responsibility of the caller.
5020**
danielk19775fd057a2005-03-09 13:09:43 +00005021** A transaction must be active when this routine is called. It used to be
5022** required that a statement transaction was not active, but this restriction
5023** has been removed (CREATE INDEX needs to move a page when a statement
5024** transaction is active).
danielk1977687566d2004-11-02 12:56:41 +00005025*/
danielk19773b8a05f2007-03-19 17:44:26 +00005026int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno){
drh5e385312007-06-16 04:42:12 +00005027 PgHdr *pPgOld; /* The page being overwritten. */
danielk1977f5fdda82004-11-03 08:44:05 +00005028 int h;
danielk197794daf7f2004-11-08 09:26:09 +00005029 Pgno needSyncPgno = 0;
danielk1977687566d2004-11-02 12:56:41 +00005030
drh86f8c192007-08-22 00:39:19 +00005031 pagerEnter(pPager);
danielk1977687566d2004-11-02 12:56:41 +00005032 assert( pPg->nRef>0 );
5033
drh4f0c5872007-03-26 22:05:01 +00005034 PAGERTRACE5("MOVE %d page %d (needSync=%d) moves to %d\n",
danielk1977599fcba2004-11-08 07:13:13 +00005035 PAGERID(pPager), pPg->pgno, pPg->needSync, pgno);
drhb0603412007-02-28 04:47:26 +00005036 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
danielk1977ef73ee92004-11-06 12:26:07 +00005037
danielk1977b4626a32007-04-28 15:47:43 +00005038 pager_get_content(pPg);
danielk197794daf7f2004-11-08 09:26:09 +00005039 if( pPg->needSync ){
5040 needSyncPgno = pPg->pgno;
drh4f0aee42007-06-16 18:39:41 +00005041 assert( pPg->inJournal || (int)pgno>pPager->origDbSize );
danielk197794daf7f2004-11-08 09:26:09 +00005042 assert( pPg->dirty );
danielk1977ae825582004-11-23 09:06:55 +00005043 assert( pPager->needSync );
danielk197794daf7f2004-11-08 09:26:09 +00005044 }
5045
drh85b623f2007-12-13 21:54:09 +00005046 /* Unlink pPg from its hash-chain */
danielk1977f5fdda82004-11-03 08:44:05 +00005047 unlinkHashChain(pPager, pPg);
danielk1977687566d2004-11-02 12:56:41 +00005048
danielk1977ef73ee92004-11-06 12:26:07 +00005049 /* If the cache contains a page with page-number pgno, remove it
drh85b623f2007-12-13 21:54:09 +00005050 ** from its hash chain. Also, if the PgHdr.needSync was set for
danielk1977599fcba2004-11-08 07:13:13 +00005051 ** page pgno before the 'move' operation, it needs to be retained
5052 ** for the page moved there.
danielk1977f5fdda82004-11-03 08:44:05 +00005053 */
drh5e385312007-06-16 04:42:12 +00005054 pPg->needSync = 0;
danielk1977687566d2004-11-02 12:56:41 +00005055 pPgOld = pager_lookup(pPager, pgno);
5056 if( pPgOld ){
danielk1977f5fdda82004-11-03 08:44:05 +00005057 assert( pPgOld->nRef==0 );
5058 unlinkHashChain(pPager, pPgOld);
drh605ad8f2006-05-03 23:34:05 +00005059 makeClean(pPgOld);
drh5e385312007-06-16 04:42:12 +00005060 pPg->needSync = pPgOld->needSync;
5061 }else{
5062 pPg->needSync = 0;
5063 }
drhf5e7bb52008-02-18 14:47:33 +00005064 pPg->inJournal = sqlite3BitvecTest(pPager->pInJournal, pgno);
danielk1977687566d2004-11-02 12:56:41 +00005065
danielk1977f5fdda82004-11-03 08:44:05 +00005066 /* Change the page number for pPg and insert it into the new hash-chain. */
drh3765df42006-06-28 18:18:09 +00005067 assert( pgno!=0 );
danielk1977f5fdda82004-11-03 08:44:05 +00005068 pPg->pgno = pgno;
drh8ca0c722006-05-07 17:49:38 +00005069 h = pgno & (pPager->nHash-1);
danielk1977f5fdda82004-11-03 08:44:05 +00005070 if( pPager->aHash[h] ){
5071 assert( pPager->aHash[h]->pPrevHash==0 );
5072 pPager->aHash[h]->pPrevHash = pPg;
5073 }
5074 pPg->pNextHash = pPager->aHash[h];
5075 pPager->aHash[h] = pPg;
5076 pPg->pPrevHash = 0;
5077
drh605ad8f2006-05-03 23:34:05 +00005078 makeDirty(pPg);
danielk1977687566d2004-11-02 12:56:41 +00005079 pPager->dirtyCache = 1;
5080
danielk197794daf7f2004-11-08 09:26:09 +00005081 if( needSyncPgno ){
5082 /* If needSyncPgno is non-zero, then the journal file needs to be
5083 ** sync()ed before any data is written to database file page needSyncPgno.
5084 ** Currently, no such page exists in the page-cache and the
drhf5e7bb52008-02-18 14:47:33 +00005085 ** Pager.pInJournal bit has been set. This needs to be remedied by loading
danielk197794daf7f2004-11-08 09:26:09 +00005086 ** the page into the pager-cache and setting the PgHdr.needSync flag.
danielk1977ae825582004-11-23 09:06:55 +00005087 **
danielk1977a98d7b42008-01-18 13:42:54 +00005088 ** If the attempt to load the page into the page-cache fails, (due
drhf5e7bb52008-02-18 14:47:33 +00005089 ** to a malloc() or IO failure), clear the bit in the pInJournal[]
danielk1977a98d7b42008-01-18 13:42:54 +00005090 ** array. Otherwise, if the page is loaded and written again in
5091 ** this transaction, it may be written to the database file before
5092 ** it is synced into the journal file. This way, it may end up in
5093 ** the journal file twice, but that is not a problem.
5094 **
danielk19773b8a05f2007-03-19 17:44:26 +00005095 ** The sqlite3PagerGet() call may cause the journal to sync. So make
danielk1977ae825582004-11-23 09:06:55 +00005096 ** sure the Pager.needSync flag is set too.
danielk197794daf7f2004-11-08 09:26:09 +00005097 */
5098 int rc;
danielk19773b8a05f2007-03-19 17:44:26 +00005099 PgHdr *pPgHdr;
danielk1977ae825582004-11-23 09:06:55 +00005100 assert( pPager->needSync );
danielk19773b8a05f2007-03-19 17:44:26 +00005101 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
danielk197787c29a92008-01-18 11:33:16 +00005102 if( rc!=SQLITE_OK ){
drhf5e7bb52008-02-18 14:47:33 +00005103 if( pPager->pInJournal && (int)needSyncPgno<=pPager->origDbSize ){
5104 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno);
danielk1977a98d7b42008-01-18 13:42:54 +00005105 }
danielk197787c29a92008-01-18 11:33:16 +00005106 pagerLeave(pPager);
5107 return rc;
5108 }
danielk1977ae825582004-11-23 09:06:55 +00005109 pPager->needSync = 1;
danielk19773b8a05f2007-03-19 17:44:26 +00005110 pPgHdr->needSync = 1;
5111 pPgHdr->inJournal = 1;
5112 makeDirty(pPgHdr);
5113 sqlite3PagerUnref(pPgHdr);
danielk197794daf7f2004-11-08 09:26:09 +00005114 }
5115
drh86f8c192007-08-22 00:39:19 +00005116 pagerLeave(pPager);
danielk1977687566d2004-11-02 12:56:41 +00005117 return SQLITE_OK;
5118}
5119#endif
5120
danielk19773b8a05f2007-03-19 17:44:26 +00005121/*
5122** Return a pointer to the data for the specified page.
5123*/
5124void *sqlite3PagerGetData(DbPage *pPg){
5125 return PGHDR_TO_DATA(pPg);
5126}
5127
5128/*
5129** Return a pointer to the Pager.nExtra bytes of "extra" space
5130** allocated along with the specified page.
5131*/
5132void *sqlite3PagerGetExtra(DbPage *pPg){
5133 Pager *pPager = pPg->pPager;
5134 return (pPager?PGHDR_TO_EXTRA(pPg, pPager):0);
5135}
5136
danielk197741483462007-03-24 16:45:04 +00005137/*
5138** Get/set the locking-mode for this pager. Parameter eMode must be one
5139** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
5140** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
5141** the locking-mode is set to the value specified.
5142**
5143** The returned value is either PAGER_LOCKINGMODE_NORMAL or
5144** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
5145** locking-mode.
5146*/
5147int sqlite3PagerLockingMode(Pager *pPager, int eMode){
drh369339d2007-03-30 16:01:55 +00005148 assert( eMode==PAGER_LOCKINGMODE_QUERY
5149 || eMode==PAGER_LOCKINGMODE_NORMAL
5150 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
5151 assert( PAGER_LOCKINGMODE_QUERY<0 );
5152 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
5153 if( eMode>=0 && !pPager->tempFile ){
danielk197741483462007-03-24 16:45:04 +00005154 pPager->exclusiveMode = eMode;
5155 }
5156 return (int)pPager->exclusiveMode;
5157}
5158
drhd919fe12007-12-11 19:34:44 +00005159#ifdef SQLITE_TEST
drhdd793422001-06-28 01:54:48 +00005160/*
5161** Print a listing of all referenced pages and their ref count.
5162*/
danielk19773b8a05f2007-03-19 17:44:26 +00005163void sqlite3PagerRefdump(Pager *pPager){
drhdd793422001-06-28 01:54:48 +00005164 PgHdr *pPg;
5165 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
5166 if( pPg->nRef<=0 ) continue;
drhfe63d1c2004-09-08 20:13:04 +00005167 sqlite3DebugPrintf("PAGE %3d addr=%p nRef=%d\n",
5168 pPg->pgno, PGHDR_TO_DATA(pPg), pPg->nRef);
drhdd793422001-06-28 01:54:48 +00005169 }
5170}
5171#endif
drh2e66f0b2005-04-28 17:18:48 +00005172
5173#endif /* SQLITE_OMIT_DISKIO */