blob: 80469db9f4e5d85bcff460e0b8e6775992e2ae76 [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**
drhf5e7bb52008-02-18 14:47:33 +000021** @(#) $Id: pager.c,v 1.407 2008/02/18 14:47:34 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 );
drhed138fb2007-08-22 22:04:37 +0000680 pagerLeave(pPager);
drh643167f2008-01-22 21:30:53 +0000681 sqlite3FaultBenign(SQLITE_FAULTINJECTOR_MALLOC, pPager->aHash!=0);
drh17435752007-08-16 04:30:38 +0000682 aHash = sqlite3MallocZero( sizeof(aHash[0])*N );
drh643167f2008-01-22 21:30:53 +0000683 sqlite3FaultBenign(SQLITE_FAULTINJECTOR_MALLOC, 0);
drhed138fb2007-08-22 22:04:37 +0000684 pagerEnter(pPager);
drh8ca0c722006-05-07 17:49:38 +0000685 if( aHash==0 ){
686 /* Failure to rehash is not an error. It is only a performance hit. */
687 return;
688 }
drh17435752007-08-16 04:30:38 +0000689 sqlite3_free(pPager->aHash);
drh8ca0c722006-05-07 17:49:38 +0000690 pPager->nHash = N;
691 pPager->aHash = aHash;
692 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
drh3765df42006-06-28 18:18:09 +0000693 int h;
694 if( pPg->pgno==0 ){
695 assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
696 continue;
697 }
698 h = pPg->pgno & (N-1);
drh8ca0c722006-05-07 17:49:38 +0000699 pPg->pNextHash = aHash[h];
700 if( aHash[h] ){
701 aHash[h]->pPrevHash = pPg;
702 }
703 aHash[h] = pPg;
704 pPg->pPrevHash = 0;
705 }
706}
707
drhdd793422001-06-28 01:54:48 +0000708/*
drh34e79ce2004-02-08 06:05:46 +0000709** Read a 32-bit integer from the given file descriptor. Store the integer
710** that is read in *pRes. Return SQLITE_OK if everything worked, or an
711** error code is something goes wrong.
drh726de592004-06-10 23:35:50 +0000712**
713** All values are stored on disk as big-endian.
drh94f33312002-08-12 12:29:56 +0000714*/
danielk197762079062007-08-15 17:08:46 +0000715static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
drh3b59a5c2006-01-15 20:28:28 +0000716 unsigned char ac[4];
danielk197762079062007-08-15 17:08:46 +0000717 int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
drhae2b40c2004-06-09 19:03:54 +0000718 if( rc==SQLITE_OK ){
drha3152892007-05-05 11:48:52 +0000719 *pRes = sqlite3Get4byte(ac);
drh94f33312002-08-12 12:29:56 +0000720 }
drh94f33312002-08-12 12:29:56 +0000721 return rc;
722}
723
724/*
drh97b57482006-01-10 20:32:32 +0000725** Write a 32-bit integer into a string buffer in big-endian byte order.
726*/
drha3152892007-05-05 11:48:52 +0000727#define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
drh97b57482006-01-10 20:32:32 +0000728
729/*
drh34e79ce2004-02-08 06:05:46 +0000730** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
731** on success or an error code is something goes wrong.
drh94f33312002-08-12 12:29:56 +0000732*/
danielk197762079062007-08-15 17:08:46 +0000733static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
danielk1977bab45c62006-01-16 15:14:27 +0000734 char ac[4];
drh97b57482006-01-10 20:32:32 +0000735 put32bits(ac, val);
danielk197762079062007-08-15 17:08:46 +0000736 return sqlite3OsWrite(fd, ac, 4, offset);
drh94f33312002-08-12 12:29:56 +0000737}
738
drh2554f8b2003-01-22 01:26:44 +0000739/*
danielk19777a2b1ee2007-08-21 14:27:01 +0000740** If file pFd is open, call sqlite3OsUnlock() on it.
741*/
742static int osUnlock(sqlite3_file *pFd, int eLock){
743 if( !pFd->pMethods ){
744 return SQLITE_OK;
745 }
746 return sqlite3OsUnlock(pFd, eLock);
747}
748
749/*
danielk1977c7b60172007-08-22 11:22:03 +0000750** This function determines whether or not the atomic-write optimization
751** can be used with this pager. The optimization can be used if:
752**
753** (a) the value returned by OsDeviceCharacteristics() indicates that
754** a database page may be written atomically, and
755** (b) the value returned by OsSectorSize() is less than or equal
756** to the page size.
757**
758** If the optimization cannot be used, 0 is returned. If it can be used,
759** then the value returned is the size of the journal file when it
760** contains rollback data for exactly one page.
761*/
762#ifdef SQLITE_ENABLE_ATOMIC_WRITE
763static int jrnlBufferSize(Pager *pPager){
764 int dc; /* Device characteristics */
765 int nSector; /* Sector size */
766 int nPage; /* Page size */
767 sqlite3_file *fd = pPager->fd;
768
769 if( fd->pMethods ){
770 dc = sqlite3OsDeviceCharacteristics(fd);
771 nSector = sqlite3OsSectorSize(fd);
772 nPage = pPager->pageSize;
773 }
774
775 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
776 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
777
danielk1977f8940ae2007-08-23 11:07:10 +0000778 if( !fd->pMethods || (dc&(SQLITE_IOCAP_ATOMIC|(nPage>>8))&&nSector<=nPage) ){
danielk1977c7b60172007-08-22 11:22:03 +0000779 return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
780 }
781 return 0;
782}
783#endif
784
785/*
danielk1977aef0bf62005-12-30 16:28:01 +0000786** This function should be called when an error occurs within the pager
danielk1977a96a7102006-01-16 12:46:41 +0000787** code. The first argument is a pointer to the pager structure, the
788** second the error-code about to be returned by a pager API function.
789** The value returned is a copy of the second argument to this function.
790**
drh4f0ee682007-03-30 20:43:40 +0000791** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
danielk1977ae72d982007-10-03 08:46:44 +0000792** the error becomes persistent. Until the persisten error is cleared,
793** subsequent API calls on this Pager will immediately return the same
794** error code.
795**
796** A persistent error indicates that the contents of the pager-cache
797** cannot be trusted. This state can be cleared by completely discarding
798** the contents of the pager-cache. If a transaction was active when
799** the persistent error occured, then the rollback journal may need
800** to be replayed.
danielk1977aef0bf62005-12-30 16:28:01 +0000801*/
danielk1977ae72d982007-10-03 08:46:44 +0000802static void pager_unlock(Pager *pPager);
danielk1977aef0bf62005-12-30 16:28:01 +0000803static int pager_error(Pager *pPager, int rc){
drh4ac285a2006-09-15 07:28:50 +0000804 int rc2 = rc & 0xff;
drh34f56212007-08-10 23:56:35 +0000805 assert(
806 pPager->errCode==SQLITE_FULL ||
807 pPager->errCode==SQLITE_OK ||
808 (pPager->errCode & 0xff)==SQLITE_IOERR
809 );
danielk1977979f38e2007-03-27 16:19:51 +0000810 if(
drh4ac285a2006-09-15 07:28:50 +0000811 rc2==SQLITE_FULL ||
812 rc2==SQLITE_IOERR ||
drh4f0ee682007-03-30 20:43:40 +0000813 rc2==SQLITE_CORRUPT
danielk1977efaaf572006-01-16 11:29:19 +0000814 ){
815 pPager->errCode = rc;
danielk1977ae72d982007-10-03 08:46:44 +0000816 if( pPager->state==PAGER_UNLOCK && pPager->nRef==0 ){
817 /* If the pager is already unlocked, call pager_unlock() now to
818 ** clear the error state and ensure that the pager-cache is
819 ** completely empty.
820 */
821 pager_unlock(pPager);
822 }
danielk1977aef0bf62005-12-30 16:28:01 +0000823 }
824 return rc;
825}
826
drh477731b2007-06-16 03:06:27 +0000827/*
828** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
829** on the cache using a hash function. This is used for testing
830** and debugging only.
831*/
danielk19773c407372005-02-15 02:54:14 +0000832#ifdef SQLITE_CHECK_PAGES
833/*
834** Return a 32-bit hash of the page data for pPage.
835*/
drh477731b2007-06-16 03:06:27 +0000836static u32 pager_datahash(int nByte, unsigned char *pData){
danielk19773c407372005-02-15 02:54:14 +0000837 u32 hash = 0;
838 int i;
drh477731b2007-06-16 03:06:27 +0000839 for(i=0; i<nByte; i++){
840 hash = (hash*1039) + pData[i];
danielk19773c407372005-02-15 02:54:14 +0000841 }
842 return hash;
843}
drh477731b2007-06-16 03:06:27 +0000844static u32 pager_pagehash(PgHdr *pPage){
845 return pager_datahash(pPage->pPager->pageSize,
846 (unsigned char *)PGHDR_TO_DATA(pPage));
847}
danielk19773c407372005-02-15 02:54:14 +0000848
849/*
850** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
851** is defined, and NDEBUG is not defined, an assert() statement checks
852** that the page is either dirty or still matches the calculated page-hash.
853*/
854#define CHECK_PAGE(x) checkPage(x)
855static void checkPage(PgHdr *pPg){
856 Pager *pPager = pPg->pPager;
danielk1977efaaf572006-01-16 11:29:19 +0000857 assert( !pPg->pageHash || pPager->errCode || MEMDB || pPg->dirty ||
danielk19773c407372005-02-15 02:54:14 +0000858 pPg->pageHash==pager_pagehash(pPg) );
859}
860
861#else
drh8ffa8172007-06-18 17:25:17 +0000862#define pager_datahash(X,Y) 0
drh477731b2007-06-16 03:06:27 +0000863#define pager_pagehash(X) 0
danielk19773c407372005-02-15 02:54:14 +0000864#define CHECK_PAGE(x)
865#endif
866
drhed7c8552001-04-11 14:29:21 +0000867/*
danielk197776572402004-06-25 02:38:54 +0000868** When this is called the journal file for pager pPager must be open.
869** The master journal file name is read from the end of the file and
danielk197765839c62007-08-30 08:08:17 +0000870** written into memory supplied by the caller.
danielk197776572402004-06-25 02:38:54 +0000871**
danielk197765839c62007-08-30 08:08:17 +0000872** zMaster must point to a buffer of at least nMaster bytes allocated by
873** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
874** enough space to write the master journal name). If the master journal
875** name in the journal is longer than nMaster bytes (including a
876** nul-terminator), then this is handled as if no master journal name
877** were present in the journal.
878**
879** If no master journal file name is present zMaster[0] is set to 0 and
danielk197776572402004-06-25 02:38:54 +0000880** SQLITE_OK returned.
881*/
danielk197765839c62007-08-30 08:08:17 +0000882static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, int nMaster){
danielk197776572402004-06-25 02:38:54 +0000883 int rc;
884 u32 len;
drheb206252004-10-01 02:00:31 +0000885 i64 szJ;
danielk1977c3e8f5e2004-06-28 01:16:46 +0000886 u32 cksum;
danielk1977cafadba2004-06-25 11:11:54 +0000887 int i;
danielk197776572402004-06-25 02:38:54 +0000888 unsigned char aMagic[8]; /* A buffer to hold the magic header */
889
danielk197765839c62007-08-30 08:08:17 +0000890 zMaster[0] = '\0';
danielk197776572402004-06-25 02:38:54 +0000891
drh054889e2005-11-30 03:20:31 +0000892 rc = sqlite3OsFileSize(pJrnl, &szJ);
danielk1977cafadba2004-06-25 11:11:54 +0000893 if( rc!=SQLITE_OK || szJ<16 ) return rc;
danielk197776572402004-06-25 02:38:54 +0000894
danielk197762079062007-08-15 17:08:46 +0000895 rc = read32bits(pJrnl, szJ-16, &len);
danielk197776572402004-06-25 02:38:54 +0000896 if( rc!=SQLITE_OK ) return rc;
897
danielk197765839c62007-08-30 08:08:17 +0000898 if( len>=nMaster ){
899 return SQLITE_OK;
900 }
901
danielk197762079062007-08-15 17:08:46 +0000902 rc = read32bits(pJrnl, szJ-12, &cksum);
danielk1977cafadba2004-06-25 11:11:54 +0000903 if( rc!=SQLITE_OK ) return rc;
904
danielk197762079062007-08-15 17:08:46 +0000905 rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8);
danielk197776572402004-06-25 02:38:54 +0000906 if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc;
907
danielk197765839c62007-08-30 08:08:17 +0000908 rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len);
danielk197776572402004-06-25 02:38:54 +0000909 if( rc!=SQLITE_OK ){
danielk197776572402004-06-25 02:38:54 +0000910 return rc;
911 }
danielk197765839c62007-08-30 08:08:17 +0000912 zMaster[len] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000913
914 /* See if the checksum matches the master journal name */
915 for(i=0; i<len; i++){
danielk197765839c62007-08-30 08:08:17 +0000916 cksum -= zMaster[i];
917 }
danielk19778191bff2004-06-28 04:52:30 +0000918 if( cksum ){
919 /* If the checksum doesn't add up, then one or more of the disk sectors
920 ** containing the master journal filename is corrupted. This means
921 ** definitely roll back, so just return SQLITE_OK and report a (nul)
922 ** master-journal filename.
923 */
danielk197765839c62007-08-30 08:08:17 +0000924 zMaster[0] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000925 }
danielk197776572402004-06-25 02:38:54 +0000926
927 return SQLITE_OK;
928}
929
930/*
931** Seek the journal file descriptor to the next sector boundary where a
932** journal header may be read or written. Pager.journalOff is updated with
933** the new seek offset.
934**
935** i.e for a sector size of 512:
936**
937** Input Offset Output Offset
938** ---------------------------------------
939** 0 0
940** 512 512
941** 100 512
942** 2000 2048
943**
944*/
danielk197762079062007-08-15 17:08:46 +0000945static void seekJournalHdr(Pager *pPager){
drheb206252004-10-01 02:00:31 +0000946 i64 offset = 0;
947 i64 c = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000948 if( c ){
949 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
950 }
951 assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
952 assert( offset>=c );
953 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
954 pPager->journalOff = offset;
danielk197776572402004-06-25 02:38:54 +0000955}
956
957/*
958** The journal file must be open when this routine is called. A journal
959** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
960** current location.
961**
962** The format for the journal header is as follows:
963** - 8 bytes: Magic identifying journal format.
964** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
965** - 4 bytes: Random number used for page hash.
966** - 4 bytes: Initial database page count.
967** - 4 bytes: Sector size used by the process that wrote this journal.
968**
danielk1977f42f25c2004-06-25 07:21:28 +0000969** Followed by (JOURNAL_HDR_SZ - 24) bytes of unused space.
danielk197776572402004-06-25 02:38:54 +0000970*/
971static int writeJournalHdr(Pager *pPager){
drh97b57482006-01-10 20:32:32 +0000972 char zHeader[sizeof(aJournalMagic)+16];
danielk19774099f6e2007-03-19 11:25:20 +0000973 int rc;
danielk197776572402004-06-25 02:38:54 +0000974
danielk19774099f6e2007-03-19 11:25:20 +0000975 if( pPager->stmtHdrOff==0 ){
976 pPager->stmtHdrOff = pPager->journalOff;
977 }
978
danielk197762079062007-08-15 17:08:46 +0000979 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000980 pPager->journalHdr = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000981
drh97b57482006-01-10 20:32:32 +0000982 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
danielk19774cd2cd52007-08-23 14:48:23 +0000983
984 /*
985 ** Write the nRec Field - the number of page records that follow this
986 ** journal header. Normally, zero is written to this value at this time.
987 ** After the records are added to the journal (and the journal synced,
988 ** if in full-sync mode), the zero is overwritten with the true number
989 ** of records (see syncJournal()).
990 **
991 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
992 ** reading the journal this value tells SQLite to assume that the
993 ** rest of the journal file contains valid page records. This assumption
994 ** is dangerous, as if a failure occured whilst writing to the journal
995 ** file it may contain some garbage data. There are two scenarios
996 ** where this risk can be ignored:
997 **
998 ** * When the pager is in no-sync mode. Corruption can follow a
999 ** power failure in this case anyway.
1000 **
1001 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
1002 ** that garbage data is never appended to the journal file.
1003 */
1004 assert(pPager->fd->pMethods||pPager->noSync);
1005 if( (pPager->noSync)
1006 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
1007 ){
1008 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
1009 }else{
1010 put32bits(&zHeader[sizeof(aJournalMagic)], 0);
1011 }
1012
drh97b57482006-01-10 20:32:32 +00001013 /* The random check-hash initialiser */
1014 sqlite3Randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
1015 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
1016 /* The initial database size */
1017 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbSize);
1018 /* The assumed sector size for this process */
1019 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
drhb0603412007-02-28 04:47:26 +00001020 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, sizeof(zHeader)))
danielk197762079062007-08-15 17:08:46 +00001021 rc = sqlite3OsWrite(pPager->jfd, zHeader, sizeof(zHeader),pPager->journalOff);
1022 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk197776572402004-06-25 02:38:54 +00001023
1024 /* The journal header has been written successfully. Seek the journal
1025 ** file descriptor to the end of the journal header sector.
1026 */
1027 if( rc==SQLITE_OK ){
drhb0603412007-02-28 04:47:26 +00001028 IOTRACE(("JTAIL %p %lld\n", pPager, pPager->journalOff-1))
danielk197762079062007-08-15 17:08:46 +00001029 rc = sqlite3OsWrite(pPager->jfd, "\000", 1, pPager->journalOff-1);
danielk197776572402004-06-25 02:38:54 +00001030 }
1031 return rc;
1032}
1033
1034/*
1035** The journal file must be open when this is called. A journal header file
1036** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
1037** file. See comments above function writeJournalHdr() for a description of
1038** the journal header format.
1039**
1040** If the header is read successfully, *nRec is set to the number of
1041** page records following this header and *dbSize is set to the size of the
1042** database before the transaction began, in pages. Also, pPager->cksumInit
1043** is set to the value read from the journal header. SQLITE_OK is returned
1044** in this case.
1045**
1046** If the journal header file appears to be corrupted, SQLITE_DONE is
1047** returned and *nRec and *dbSize are not set. If JOURNAL_HDR_SZ bytes
1048** cannot be read from the journal file an error code is returned.
1049*/
1050static int readJournalHdr(
1051 Pager *pPager,
drheb206252004-10-01 02:00:31 +00001052 i64 journalSize,
danielk197776572402004-06-25 02:38:54 +00001053 u32 *pNRec,
1054 u32 *pDbSize
1055){
1056 int rc;
1057 unsigned char aMagic[8]; /* A buffer to hold the magic header */
danielk197762079062007-08-15 17:08:46 +00001058 i64 jrnlOff;
danielk197776572402004-06-25 02:38:54 +00001059
danielk197762079062007-08-15 17:08:46 +00001060 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +00001061 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
1062 return SQLITE_DONE;
1063 }
danielk197762079062007-08-15 17:08:46 +00001064 jrnlOff = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +00001065
danielk197762079062007-08-15 17:08:46 +00001066 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff);
danielk197776572402004-06-25 02:38:54 +00001067 if( rc ) return rc;
danielk197762079062007-08-15 17:08:46 +00001068 jrnlOff += sizeof(aMagic);
danielk197776572402004-06-25 02:38:54 +00001069
1070 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
1071 return SQLITE_DONE;
1072 }
1073
danielk197762079062007-08-15 17:08:46 +00001074 rc = read32bits(pPager->jfd, jrnlOff, pNRec);
danielk197776572402004-06-25 02:38:54 +00001075 if( rc ) return rc;
1076
danielk197762079062007-08-15 17:08:46 +00001077 rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit);
danielk197776572402004-06-25 02:38:54 +00001078 if( rc ) return rc;
1079
danielk197762079062007-08-15 17:08:46 +00001080 rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize);
danielk197776572402004-06-25 02:38:54 +00001081 if( rc ) return rc;
1082
1083 /* Update the assumed sector-size to match the value used by
1084 ** the process that created this journal. If this journal was
1085 ** created by a process other than this one, then this routine
1086 ** is being called from within pager_playback(). The local value
1087 ** of Pager.sectorSize is restored at the end of that routine.
1088 */
danielk197762079062007-08-15 17:08:46 +00001089 rc = read32bits(pPager->jfd, jrnlOff+12, (u32 *)&pPager->sectorSize);
danielk197776572402004-06-25 02:38:54 +00001090 if( rc ) return rc;
1091
1092 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk197762079062007-08-15 17:08:46 +00001093 return SQLITE_OK;
danielk197776572402004-06-25 02:38:54 +00001094}
1095
1096
1097/*
1098** Write the supplied master journal name into the journal file for pager
danielk1977cafadba2004-06-25 11:11:54 +00001099** pPager at the current location. The master journal name must be the last
1100** thing written to a journal file. If the pager is in full-sync mode, the
1101** journal file descriptor is advanced to the next sector boundary before
1102** anything is written. The format is:
1103**
1104** + 4 bytes: PAGER_MJ_PGNO.
1105** + N bytes: length of master journal name.
1106** + 4 bytes: N
1107** + 4 bytes: Master journal name checksum.
1108** + 8 bytes: aJournalMagic[].
1109**
1110** The master journal page checksum is the sum of the bytes in the master
1111** journal name.
danielk1977aef0bf62005-12-30 16:28:01 +00001112**
1113** If zMaster is a NULL pointer (occurs for a single database transaction),
1114** this call is a no-op.
danielk197776572402004-06-25 02:38:54 +00001115*/
1116static int writeMasterJournal(Pager *pPager, const char *zMaster){
1117 int rc;
1118 int len;
danielk1977cafadba2004-06-25 11:11:54 +00001119 int i;
danielk197762079062007-08-15 17:08:46 +00001120 i64 jrnlOff;
drh97b57482006-01-10 20:32:32 +00001121 u32 cksum = 0;
1122 char zBuf[sizeof(aJournalMagic)+2*4];
danielk197776572402004-06-25 02:38:54 +00001123
1124 if( !zMaster || pPager->setMaster) return SQLITE_OK;
1125 pPager->setMaster = 1;
1126
1127 len = strlen(zMaster);
danielk1977cafadba2004-06-25 11:11:54 +00001128 for(i=0; i<len; i++){
1129 cksum += zMaster[i];
1130 }
danielk197776572402004-06-25 02:38:54 +00001131
1132 /* If in full-sync mode, advance to the next disk sector before writing
1133 ** the master journal name. This is in case the previous page written to
1134 ** the journal has already been synced.
1135 */
1136 if( pPager->fullSync ){
danielk197762079062007-08-15 17:08:46 +00001137 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +00001138 }
danielk197762079062007-08-15 17:08:46 +00001139 jrnlOff = pPager->journalOff;
danielk1977cafadba2004-06-25 11:11:54 +00001140 pPager->journalOff += (len+20);
danielk197776572402004-06-25 02:38:54 +00001141
danielk197762079062007-08-15 17:08:46 +00001142 rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager));
danielk197776572402004-06-25 02:38:54 +00001143 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001144 jrnlOff += 4;
danielk197776572402004-06-25 02:38:54 +00001145
danielk197762079062007-08-15 17:08:46 +00001146 rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff);
danielk197776572402004-06-25 02:38:54 +00001147 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001148 jrnlOff += len;
danielk197776572402004-06-25 02:38:54 +00001149
drh97b57482006-01-10 20:32:32 +00001150 put32bits(zBuf, len);
1151 put32bits(&zBuf[4], cksum);
1152 memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic));
danielk197762079062007-08-15 17:08:46 +00001153 rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff);
drh2c8997b2005-08-27 16:36:48 +00001154 pPager->needSync = !pPager->noSync;
danielk197776572402004-06-25 02:38:54 +00001155 return rc;
1156}
1157
1158/*
drh03eb96a2002-11-10 23:32:56 +00001159** Add or remove a page from the list of all pages that are in the
drhac69b052004-05-12 13:30:07 +00001160** statement journal.
drh03eb96a2002-11-10 23:32:56 +00001161**
1162** The Pager keeps a separate list of pages that are currently in
danielk19773b8a05f2007-03-19 17:44:26 +00001163** the statement journal. This helps the sqlite3PagerStmtCommit()
drh03eb96a2002-11-10 23:32:56 +00001164** routine run MUCH faster for the common case where there are many
drhac69b052004-05-12 13:30:07 +00001165** pages in memory but only a few are in the statement journal.
drh03eb96a2002-11-10 23:32:56 +00001166*/
drh3aac2dd2004-04-26 14:10:20 +00001167static void page_add_to_stmt_list(PgHdr *pPg){
drh03eb96a2002-11-10 23:32:56 +00001168 Pager *pPager = pPg->pPager;
danielk1977f35843b2007-04-07 15:03:17 +00001169 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
1170 assert( MEMDB );
1171 if( !pHist->inStmt ){
1172 assert( pHist->pPrevStmt==0 && pHist->pNextStmt==0 );
1173 if( pPager->pStmt ){
1174 PGHDR_TO_HIST(pPager->pStmt, pPager)->pPrevStmt = pPg;
1175 }
1176 pHist->pNextStmt = pPager->pStmt;
1177 pPager->pStmt = pPg;
1178 pHist->inStmt = 1;
drh03eb96a2002-11-10 23:32:56 +00001179 }
drh03eb96a2002-11-10 23:32:56 +00001180}
1181
1182/*
drhed7c8552001-04-11 14:29:21 +00001183** Find a page in the hash table given its page number. Return
1184** a pointer to the page or NULL if not found.
1185*/
drhd9b02572001-04-15 00:37:09 +00001186static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drh8ca0c722006-05-07 17:49:38 +00001187 PgHdr *p;
1188 if( pPager->aHash==0 ) return 0;
1189 p = pPager->aHash[pgno & (pPager->nHash-1)];
drhed7c8552001-04-11 14:29:21 +00001190 while( p && p->pgno!=pgno ){
1191 p = p->pNextHash;
1192 }
1193 return p;
1194}
1195
1196/*
danielk1977e180dd92007-04-05 17:15:52 +00001197** Clear the in-memory cache. This routine
drhed7c8552001-04-11 14:29:21 +00001198** sets the state of the pager back to what it was when it was first
1199** opened. Any outstanding pages are invalidated and subsequent attempts
1200** to access those pages will likely result in a coredump.
1201*/
drhd9b02572001-04-15 00:37:09 +00001202static void pager_reset(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00001203 PgHdr *pPg, *pNext;
danielk1977efaaf572006-01-16 11:29:19 +00001204 if( pPager->errCode ) return;
drhd9b02572001-04-15 00:37:09 +00001205 for(pPg=pPager->pAll; pPg; pPg=pNext){
drh538f5702007-04-13 02:14:30 +00001206 IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
1207 PAGER_INCR(sqlite3_pager_pgfree_count);
drhd9b02572001-04-15 00:37:09 +00001208 pNext = pPg->pNextAll;
danielk19779f61c2f2007-08-27 17:27:49 +00001209 lruListRemove(pPg);
drh0d180202008-02-14 23:26:56 +00001210 sqlite3_free(pPg->pData);
drh17435752007-08-16 04:30:38 +00001211 sqlite3_free(pPg);
drhed7c8552001-04-11 14:29:21 +00001212 }
danielk19779f61c2f2007-08-27 17:27:49 +00001213 assert(pPager->lru.pFirst==0);
1214 assert(pPager->lru.pFirstSynced==0);
1215 assert(pPager->lru.pLast==0);
danielk1977b94bf852007-03-19 13:53:37 +00001216 pPager->pStmt = 0;
drhd9b02572001-04-15 00:37:09 +00001217 pPager->pAll = 0;
danielk1977ae72d982007-10-03 08:46:44 +00001218 pPager->pDirty = 0;
drh8ca0c722006-05-07 17:49:38 +00001219 pPager->nHash = 0;
drh17435752007-08-16 04:30:38 +00001220 sqlite3_free(pPager->aHash);
drhed7c8552001-04-11 14:29:21 +00001221 pPager->nPage = 0;
drh8ca0c722006-05-07 17:49:38 +00001222 pPager->aHash = 0;
drhed7c8552001-04-11 14:29:21 +00001223 pPager->nRef = 0;
danielk1977e277be02007-03-23 18:12:06 +00001224}
1225
1226/*
danielk1977ae72d982007-10-03 08:46:44 +00001227** Unlock the database file.
1228**
1229** If the pager is currently in error state, discard the contents of
1230** the cache and reset the Pager structure internal state. If there is
1231** an open journal-file, then the next time a shared-lock is obtained
1232** on the pager file (by this or any other process), it will be
1233** treated as a hot-journal and rolled back.
1234*/
1235static void pager_unlock(Pager *pPager){
1236 if( !pPager->exclusiveMode ){
1237 if( !MEMDB ){
1238 if( pPager->fd->pMethods ){
1239 osUnlock(pPager->fd, NO_LOCK);
1240 }
1241 pPager->dbSize = -1;
1242 IOTRACE(("UNLOCK %p\n", pPager))
1243
1244 /* If Pager.errCode is set, the contents of the pager cache cannot be
1245 ** trusted. Now that the pager file is unlocked, the contents of the
1246 ** cache can be discarded and the error code safely cleared.
1247 */
1248 if( pPager->errCode ){
1249 pPager->errCode = SQLITE_OK;
1250 pager_reset(pPager);
1251 if( pPager->stmtOpen ){
1252 sqlite3OsClose(pPager->stfd);
drhf5e7bb52008-02-18 14:47:33 +00001253 sqlite3BitvecDestroy(pPager->pInStmt);
1254 pPager->pInStmt = 0;
danielk1977ae72d982007-10-03 08:46:44 +00001255 }
1256 if( pPager->journalOpen ){
1257 sqlite3OsClose(pPager->jfd);
1258 pPager->journalOpen = 0;
drhf5e7bb52008-02-18 14:47:33 +00001259 sqlite3BitvecDestroy(pPager->pInJournal);
1260 pPager->pInJournal = 0;
danielk1977ae72d982007-10-03 08:46:44 +00001261 }
1262 pPager->stmtOpen = 0;
1263 pPager->stmtInUse = 0;
1264 pPager->journalOff = 0;
1265 pPager->journalStarted = 0;
1266 pPager->stmtAutoopen = 0;
1267 pPager->origDbSize = 0;
1268 }
1269 }
1270
1271 if( !MEMDB || pPager->errCode==SQLITE_OK ){
1272 pPager->state = PAGER_UNLOCK;
1273 pPager->changeCountDone = 0;
1274 }
1275 }
1276}
1277
1278/*
1279** Execute a rollback if a transaction is active and unlock the
1280** database file. If the pager has already entered the error state,
1281** do not attempt the rollback.
1282*/
1283static void pagerUnlockAndRollback(Pager *p){
1284 assert( p->state>=PAGER_RESERVED || p->journalOpen==0 );
1285 if( p->errCode==SQLITE_OK && p->state>=PAGER_RESERVED ){
1286 sqlite3PagerRollback(p);
1287 }
1288 pager_unlock(p);
1289 assert( p->errCode || !p->journalOpen || (p->exclusiveMode&&!p->journalOff) );
1290 assert( p->errCode || !p->stmtOpen || p->exclusiveMode );
1291}
1292
1293/*
drh80e35f42007-03-30 14:06:34 +00001294** This routine ends a transaction. A transaction is ended by either
1295** a COMMIT or a ROLLBACK.
1296**
drhed7c8552001-04-11 14:29:21 +00001297** When this routine is called, the pager has the journal file open and
drh80e35f42007-03-30 14:06:34 +00001298** a RESERVED or EXCLUSIVE lock on the database. This routine will release
1299** the database lock and acquires a SHARED lock in its place if that is
1300** the appropriate thing to do. Release locks usually is appropriate,
1301** unless we are in exclusive access mode or unless this is a
1302** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation.
1303**
1304** The journal file is either deleted or truncated.
drh50457892003-09-06 01:10:47 +00001305**
1306** TODO: Consider keeping the journal file open for temporary databases.
1307** This might give a performance improvement on windows where opening
1308** a file is an expensive operation.
drhed7c8552001-04-11 14:29:21 +00001309*/
drh80e35f42007-03-30 14:06:34 +00001310static int pager_end_transaction(Pager *pPager){
drhd9b02572001-04-15 00:37:09 +00001311 PgHdr *pPg;
danielk197741483462007-03-24 16:45:04 +00001312 int rc = SQLITE_OK;
danielk1977979f38e2007-03-27 16:19:51 +00001313 int rc2 = SQLITE_OK;
drhb7f91642004-10-31 02:22:47 +00001314 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +00001315 if( pPager->state<PAGER_RESERVED ){
1316 return SQLITE_OK;
1317 }
danielk19773b8a05f2007-03-19 17:44:26 +00001318 sqlite3PagerStmtCommit(pPager);
danielk1977334cdb62007-03-26 08:05:12 +00001319 if( pPager->stmtOpen && !pPager->exclusiveMode ){
danielk1977b4b47412007-08-17 15:53:36 +00001320 sqlite3OsClose(pPager->stfd);
danielk1977979f38e2007-03-27 16:19:51 +00001321 pPager->stmtOpen = 0;
drh0f892532002-05-30 12:27:03 +00001322 }
drhda47d772002-12-02 04:25:19 +00001323 if( pPager->journalOpen ){
drh01fa4c32007-04-02 11:22:22 +00001324 if( pPager->exclusiveMode
1325 && (rc = sqlite3OsTruncate(pPager->jfd, 0))==SQLITE_OK ){;
danielk197741483462007-03-24 16:45:04 +00001326 pPager->journalOff = 0;
danielk1977334cdb62007-03-26 08:05:12 +00001327 pPager->journalStarted = 0;
danielk197741483462007-03-24 16:45:04 +00001328 }else{
danielk1977b4b47412007-08-17 15:53:36 +00001329 sqlite3OsClose(pPager->jfd);
danielk197741483462007-03-24 16:45:04 +00001330 pPager->journalOpen = 0;
drh01fa4c32007-04-02 11:22:22 +00001331 if( rc==SQLITE_OK ){
danielk1977fee2d252007-08-18 10:59:19 +00001332 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
danielk19777152de82007-03-29 17:28:14 +00001333 }
danielk197741483462007-03-24 16:45:04 +00001334 }
drhf5e7bb52008-02-18 14:47:33 +00001335 sqlite3BitvecDestroy(pPager->pInJournal);
1336 pPager->pInJournal = 0;
drhda47d772002-12-02 04:25:19 +00001337 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1338 pPg->inJournal = 0;
1339 pPg->dirty = 0;
drhdb48ee02003-01-16 13:42:43 +00001340 pPg->needSync = 0;
danielk197741483462007-03-24 16:45:04 +00001341 pPg->alwaysRollback = 0;
danielk19773c407372005-02-15 02:54:14 +00001342#ifdef SQLITE_CHECK_PAGES
1343 pPg->pageHash = pager_pagehash(pPg);
1344#endif
drhda47d772002-12-02 04:25:19 +00001345 }
drh605ad8f2006-05-03 23:34:05 +00001346 pPager->pDirty = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001347 pPager->dirtyCache = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001348 pPager->nRec = 0;
drhda47d772002-12-02 04:25:19 +00001349 }else{
drhf5e7bb52008-02-18 14:47:33 +00001350 assert( pPager->pInJournal==0 );
drha6abd042004-06-09 17:37:22 +00001351 assert( pPager->dirtyCache==0 || pPager->useJournal==0 );
drhd9b02572001-04-15 00:37:09 +00001352 }
danielk1977979f38e2007-03-27 16:19:51 +00001353
danielk197741483462007-03-24 16:45:04 +00001354 if( !pPager->exclusiveMode ){
danielk19777a2b1ee2007-08-21 14:27:01 +00001355 rc2 = osUnlock(pPager->fd, SHARED_LOCK);
danielk197741483462007-03-24 16:45:04 +00001356 pPager->state = PAGER_SHARED;
danielk1977334cdb62007-03-26 08:05:12 +00001357 }else if( pPager->state==PAGER_SYNCED ){
1358 pPager->state = PAGER_EXCLUSIVE;
danielk197741483462007-03-24 16:45:04 +00001359 }
danielk1977334cdb62007-03-26 08:05:12 +00001360 pPager->origDbSize = 0;
danielk197776572402004-06-25 02:38:54 +00001361 pPager->setMaster = 0;
danielk1977c4da5b92006-01-21 12:08:54 +00001362 pPager->needSync = 0;
danielk19779f61c2f2007-08-27 17:27:49 +00001363 lruListSetFirstSynced(pPager);
drh588f5bc2007-01-02 18:41:54 +00001364 pPager->dbSize = -1;
danielk1977979f38e2007-03-27 16:19:51 +00001365
1366 return (rc==SQLITE_OK?rc2:rc);
drhed7c8552001-04-11 14:29:21 +00001367}
1368
drhed7c8552001-04-11 14:29:21 +00001369/*
drh968af522003-02-11 14:55:40 +00001370** Compute and return a checksum for the page of data.
drh34e79ce2004-02-08 06:05:46 +00001371**
1372** This is not a real checksum. It is really just the sum of the
drh726de592004-06-10 23:35:50 +00001373** random initial value and the page number. We experimented with
1374** a checksum of the entire data, but that was found to be too slow.
1375**
1376** Note that the page number is stored at the beginning of data and
1377** the checksum is stored at the end. This is important. If journal
1378** corruption occurs due to a power failure, the most likely scenario
1379** is that one end or the other of the record will be changed. It is
1380** much less likely that the two ends of the journal record will be
1381** correct and the middle be corrupt. Thus, this "checksum" scheme,
1382** though fast and simple, catches the mostly likely kind of corruption.
1383**
1384** FIX ME: Consider adding every 200th (or so) byte of the data to the
1385** checksum. That way if a single page spans 3 or more disk sectors and
1386** only the middle sector is corrupt, we will still have a reasonable
1387** chance of failing the checksum and thus detecting the problem.
drh968af522003-02-11 14:55:40 +00001388*/
drh74161702006-02-24 02:53:49 +00001389static u32 pager_cksum(Pager *pPager, const u8 *aData){
danielk1977ef317ab2004-06-23 10:43:10 +00001390 u32 cksum = pPager->cksumInit;
1391 int i = pPager->pageSize-200;
1392 while( i>0 ){
1393 cksum += aData[i];
1394 i -= 200;
1395 }
drh968af522003-02-11 14:55:40 +00001396 return cksum;
1397}
1398
drh605ad8f2006-05-03 23:34:05 +00001399/* Forward declaration */
1400static void makeClean(PgHdr*);
1401
drh968af522003-02-11 14:55:40 +00001402/*
drhfa86c412002-02-02 15:01:15 +00001403** Read a single page from the journal file opened on file descriptor
1404** jfd. Playback this one page.
drh968af522003-02-11 14:55:40 +00001405**
drh726de592004-06-10 23:35:50 +00001406** If useCksum==0 it means this journal does not use checksums. Checksums
1407** are not used in statement journals because statement journals do not
1408** need to survive power failures.
drhfa86c412002-02-02 15:01:15 +00001409*/
danielk197762079062007-08-15 17:08:46 +00001410static int pager_playback_one_page(
1411 Pager *pPager,
1412 sqlite3_file *jfd,
1413 i64 offset,
1414 int useCksum
1415){
drhfa86c412002-02-02 15:01:15 +00001416 int rc;
drhae2b40c2004-06-09 19:03:54 +00001417 PgHdr *pPg; /* An existing page in the cache */
1418 Pgno pgno; /* The page number of a page in journal */
1419 u32 cksum; /* Checksum used for sanity checking */
danielk19778186df82007-03-06 13:45:59 +00001420 u8 *aData = (u8 *)pPager->pTmpSpace; /* Temp storage for a page */
drhfa86c412002-02-02 15:01:15 +00001421
drh96362842005-03-20 22:47:56 +00001422 /* useCksum should be true for the main journal and false for
1423 ** statement journals. Verify that this is always the case
1424 */
drh9cbe6352005-11-29 03:13:21 +00001425 assert( jfd == (useCksum ? pPager->jfd : pPager->stfd) );
danielk19778186df82007-03-06 13:45:59 +00001426 assert( aData );
drh96362842005-03-20 22:47:56 +00001427
danielk197762079062007-08-15 17:08:46 +00001428 rc = read32bits(jfd, offset, &pgno);
drh99ee3602003-02-16 19:13:36 +00001429 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001430 rc = sqlite3OsRead(jfd, aData, pPager->pageSize, offset+4);
drh99ee3602003-02-16 19:13:36 +00001431 if( rc!=SQLITE_OK ) return rc;
danielk197776572402004-06-25 02:38:54 +00001432 pPager->journalOff += pPager->pageSize + 4;
drhfa86c412002-02-02 15:01:15 +00001433
drh968af522003-02-11 14:55:40 +00001434 /* Sanity checking on the page. This is more important that I originally
1435 ** thought. If a power failure occurs while the journal is being written,
1436 ** it could cause invalid data to be written into the journal. We need to
1437 ** detect this invalid data (with high probability) and ignore it.
1438 */
danielk197775edc162004-06-26 01:48:18 +00001439 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh968af522003-02-11 14:55:40 +00001440 return SQLITE_DONE;
1441 }
drhae2b40c2004-06-09 19:03:54 +00001442 if( pgno>(unsigned)pPager->dbSize ){
drh968af522003-02-11 14:55:40 +00001443 return SQLITE_OK;
1444 }
drhae2b40c2004-06-09 19:03:54 +00001445 if( useCksum ){
danielk197762079062007-08-15 17:08:46 +00001446 rc = read32bits(jfd, offset+pPager->pageSize+4, &cksum);
drh99ee3602003-02-16 19:13:36 +00001447 if( rc ) return rc;
danielk197776572402004-06-25 02:38:54 +00001448 pPager->journalOff += 4;
drh74161702006-02-24 02:53:49 +00001449 if( pager_cksum(pPager, aData)!=cksum ){
drh968af522003-02-11 14:55:40 +00001450 return SQLITE_DONE;
1451 }
1452 }
drhfa86c412002-02-02 15:01:15 +00001453
danielk1977aa5ccdf2004-06-14 05:10:42 +00001454 assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE );
danielk1977a3f3a5f2004-06-10 04:32:16 +00001455
1456 /* If the pager is in RESERVED state, then there must be a copy of this
1457 ** page in the pager cache. In this case just update the pager cache,
danielk19770de0bb32004-06-10 05:59:24 +00001458 ** not the database file. The page is left marked dirty in this case.
1459 **
danielk19772df71c72007-05-24 07:22:42 +00001460 ** An exception to the above rule: If the database is in no-sync mode
1461 ** and a page is moved during an incremental vacuum then the page may
danielk1977369f3a02007-05-24 09:41:20 +00001462 ** not be in the pager cache. Later: if a malloc() or IO error occurs
1463 ** during a Movepage() call, then the page may not be in the cache
1464 ** either. So the condition described in the above paragraph is not
1465 ** assert()able.
danielk19772df71c72007-05-24 07:22:42 +00001466 **
danielk1977a3f3a5f2004-06-10 04:32:16 +00001467 ** If in EXCLUSIVE state, then we update the pager cache if it exists
1468 ** and the main file. The page is then marked not dirty.
drh96362842005-03-20 22:47:56 +00001469 **
1470 ** Ticket #1171: The statement journal might contain page content that is
1471 ** different from the page content at the start of the transaction.
1472 ** This occurs when a page is changed prior to the start of a statement
1473 ** then changed again within the statement. When rolling back such a
1474 ** statement we must not write to the original database unless we know
drh5e385312007-06-16 04:42:12 +00001475 ** for certain that original page contents are synced into the main rollback
1476 ** journal. Otherwise, a power loss might leave modified data in the
1477 ** database file without an entry in the rollback journal that can
1478 ** restore the database to its original form. Two conditions must be
1479 ** met before writing to the database files. (1) the database must be
1480 ** locked. (2) we know that the original page content is fully synced
1481 ** in the main journal either because the page is not in cache or else
1482 ** the page is marked as needSync==0.
drhfa86c412002-02-02 15:01:15 +00001483 */
drhae2b40c2004-06-09 19:03:54 +00001484 pPg = pager_lookup(pPager, pgno);
drh477731b2007-06-16 03:06:27 +00001485 PAGERTRACE4("PLAYBACK %d page %d hash(%08x)\n",
1486 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData));
drh96362842005-03-20 22:47:56 +00001487 if( pPager->state>=PAGER_EXCLUSIVE && (pPg==0 || pPg->needSync==0) ){
danielk197762079062007-08-15 17:08:46 +00001488 i64 offset = (pgno-1)*(i64)pPager->pageSize;
1489 rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, offset);
drh605ad8f2006-05-03 23:34:05 +00001490 if( pPg ){
1491 makeClean(pPg);
1492 }
danielk1977a3f3a5f2004-06-10 04:32:16 +00001493 }
drhfa86c412002-02-02 15:01:15 +00001494 if( pPg ){
danielk197728129562005-01-11 10:25:06 +00001495 /* No page should ever be explicitly rolled back that is in use, except
1496 ** for page 1 which is held in use in order to keep the lock on the
1497 ** database active. However such a page may be rolled back as a result
1498 ** of an internal error resulting in an automatic call to
danielk19773b8a05f2007-03-19 17:44:26 +00001499 ** sqlite3PagerRollback().
drhacf4ac92003-12-17 23:57:34 +00001500 */
drhb6f41482004-05-14 01:58:11 +00001501 void *pData;
danielk197728129562005-01-11 10:25:06 +00001502 /* assert( pPg->nRef==0 || pPg->pgno==1 ); */
drhb6f41482004-05-14 01:58:11 +00001503 pData = PGHDR_TO_DATA(pPg);
drhae2b40c2004-06-09 19:03:54 +00001504 memcpy(pData, aData, pPager->pageSize);
danielk19779038bb62007-04-09 11:20:54 +00001505 if( pPager->xReiniter ){
1506 pPager->xReiniter(pPg, pPager->pageSize);
drhde647132004-05-07 17:57:49 +00001507 }
danielk19773c407372005-02-15 02:54:14 +00001508#ifdef SQLITE_CHECK_PAGES
drh96362842005-03-20 22:47:56 +00001509 pPg->pageHash = pager_pagehash(pPg);
danielk19773c407372005-02-15 02:54:14 +00001510#endif
drh86a88112007-04-16 15:02:19 +00001511 /* If this was page 1, then restore the value of Pager.dbFileVers.
1512 ** Do this before any decoding. */
danielk197741483462007-03-24 16:45:04 +00001513 if( pgno==1 ){
drh86a88112007-04-16 15:02:19 +00001514 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
danielk197741483462007-03-24 16:45:04 +00001515 }
drh86a88112007-04-16 15:02:19 +00001516
1517 /* Decode the page just read from disk */
1518 CODEC1(pPager, pData, pPg->pgno, 3);
drhfa86c412002-02-02 15:01:15 +00001519 }
1520 return rc;
1521}
1522
1523/*
danielk197713adf8a2004-06-03 16:08:41 +00001524** Parameter zMaster is the name of a master journal file. A single journal
1525** file that referred to the master journal file has just been rolled back.
1526** This routine checks if it is possible to delete the master journal file,
1527** and does so if it is.
drh726de592004-06-10 23:35:50 +00001528**
danielk197765839c62007-08-30 08:08:17 +00001529** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not
1530** available for use within this function.
1531**
1532**
drh726de592004-06-10 23:35:50 +00001533** The master journal file contains the names of all child journals.
1534** To tell if a master journal can be deleted, check to each of the
1535** children. If all children are either missing or do not refer to
1536** a different master journal, then this master journal can be deleted.
danielk197713adf8a2004-06-03 16:08:41 +00001537*/
danielk1977b4b47412007-08-17 15:53:36 +00001538static int pager_delmaster(Pager *pPager, const char *zMaster){
1539 sqlite3_vfs *pVfs = pPager->pVfs;
danielk197713adf8a2004-06-03 16:08:41 +00001540 int rc;
1541 int master_open = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001542 sqlite3_file *pMaster;
1543 sqlite3_file *pJournal;
danielk197713adf8a2004-06-03 16:08:41 +00001544 char *zMasterJournal = 0; /* Contents of master journal file */
drheb206252004-10-01 02:00:31 +00001545 i64 nMasterJournal; /* Size of master journal file */
danielk197713adf8a2004-06-03 16:08:41 +00001546
1547 /* Open the master journal file exclusively in case some other process
1548 ** is running this routine also. Not that it makes too much difference.
1549 */
danielk1977b4b47412007-08-17 15:53:36 +00001550 pMaster = (sqlite3_file *)sqlite3_malloc(pVfs->szOsFile * 2);
danielk1977fee2d252007-08-18 10:59:19 +00001551 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
danielk1977b4b47412007-08-17 15:53:36 +00001552 if( !pMaster ){
1553 rc = SQLITE_NOMEM;
1554 }else{
danielk1977fee2d252007-08-18 10:59:19 +00001555 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
1556 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
danielk1977b4b47412007-08-17 15:53:36 +00001557 }
danielk197713adf8a2004-06-03 16:08:41 +00001558 if( rc!=SQLITE_OK ) goto delmaster_out;
1559 master_open = 1;
danielk1977b4b47412007-08-17 15:53:36 +00001560
1561 rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001562 if( rc!=SQLITE_OK ) goto delmaster_out;
1563
1564 if( nMasterJournal>0 ){
danielk19775865e3d2004-06-14 06:03:57 +00001565 char *zJournal;
danielk197776572402004-06-25 02:38:54 +00001566 char *zMasterPtr = 0;
danielk197765839c62007-08-30 08:08:17 +00001567 int nMasterPtr = pPager->pVfs->mxPathname+1;
danielk19775865e3d2004-06-14 06:03:57 +00001568
1569 /* Load the entire master journal file into space obtained from
drh17435752007-08-16 04:30:38 +00001570 ** sqlite3_malloc() and pointed to by zMasterJournal.
danielk19775865e3d2004-06-14 06:03:57 +00001571 */
danielk197765839c62007-08-30 08:08:17 +00001572 zMasterJournal = (char *)sqlite3_malloc(nMasterJournal + nMasterPtr);
danielk197713adf8a2004-06-03 16:08:41 +00001573 if( !zMasterJournal ){
1574 rc = SQLITE_NOMEM;
1575 goto delmaster_out;
1576 }
danielk197765839c62007-08-30 08:08:17 +00001577 zMasterPtr = &zMasterJournal[nMasterJournal];
danielk1977b4b47412007-08-17 15:53:36 +00001578 rc = sqlite3OsRead(pMaster, zMasterJournal, nMasterJournal, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001579 if( rc!=SQLITE_OK ) goto delmaster_out;
1580
danielk19775865e3d2004-06-14 06:03:57 +00001581 zJournal = zMasterJournal;
1582 while( (zJournal-zMasterJournal)<nMasterJournal ){
danielk1977b4b47412007-08-17 15:53:36 +00001583 if( sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS) ){
danielk197713adf8a2004-06-03 16:08:41 +00001584 /* One of the journals pointed to by the master journal exists.
1585 ** Open it and check if it points at the master journal. If
1586 ** so, return without deleting the master journal file.
1587 */
drh3b7b78b2004-11-24 01:16:43 +00001588 int c;
danielk1977fee2d252007-08-18 10:59:19 +00001589 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
1590 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001591 if( rc!=SQLITE_OK ){
danielk197713adf8a2004-06-03 16:08:41 +00001592 goto delmaster_out;
1593 }
danielk197713adf8a2004-06-03 16:08:41 +00001594
danielk197765839c62007-08-30 08:08:17 +00001595 rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
danielk1977b4b47412007-08-17 15:53:36 +00001596 sqlite3OsClose(pJournal);
danielk19779eed5052004-06-04 10:38:30 +00001597 if( rc!=SQLITE_OK ){
danielk19779eed5052004-06-04 10:38:30 +00001598 goto delmaster_out;
1599 }
danielk197776572402004-06-25 02:38:54 +00001600
danielk197765839c62007-08-30 08:08:17 +00001601 c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
drh3b7b78b2004-11-24 01:16:43 +00001602 if( c ){
danielk197776572402004-06-25 02:38:54 +00001603 /* We have a match. Do not delete the master journal file. */
1604 goto delmaster_out;
danielk197713adf8a2004-06-03 16:08:41 +00001605 }
1606 }
danielk19775865e3d2004-06-14 06:03:57 +00001607 zJournal += (strlen(zJournal)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001608 }
1609 }
1610
danielk1977fee2d252007-08-18 10:59:19 +00001611 rc = sqlite3OsDelete(pVfs, zMaster, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001612
1613delmaster_out:
1614 if( zMasterJournal ){
drh17435752007-08-16 04:30:38 +00001615 sqlite3_free(zMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001616 }
1617 if( master_open ){
danielk1977b4b47412007-08-17 15:53:36 +00001618 sqlite3OsClose(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001619 }
danielk1977b4b47412007-08-17 15:53:36 +00001620 sqlite3_free(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001621 return rc;
1622}
1623
drha6abd042004-06-09 17:37:22 +00001624
danielk1977e180dd92007-04-05 17:15:52 +00001625static void pager_truncate_cache(Pager *pPager);
1626
drha6abd042004-06-09 17:37:22 +00001627/*
drhcb4c40b2004-08-18 19:09:43 +00001628** Truncate the main file of the given pager to the number of pages
danielk1977e180dd92007-04-05 17:15:52 +00001629** indicated. Also truncate the cached representation of the file.
drh7fe3f7e2007-11-29 18:44:27 +00001630**
1631** Might might be the case that the file on disk is smaller than nPage.
1632** This can happen, for example, if we are in the middle of a transaction
1633** which has extended the file size and the new pages are still all held
1634** in cache, then an INSERT or UPDATE does a statement rollback. Some
1635** operating system implementations can get confused if you try to
1636** truncate a file to some size that is larger than it currently is,
1637** so detect this case and do not do the truncation.
drhcb4c40b2004-08-18 19:09:43 +00001638*/
1639static int pager_truncate(Pager *pPager, int nPage){
danielk1977e180dd92007-04-05 17:15:52 +00001640 int rc = SQLITE_OK;
danielk19777a2b1ee2007-08-21 14:27:01 +00001641 if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){
drh7fe3f7e2007-11-29 18:44:27 +00001642 i64 currentSize, newSize;
1643 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
1644 newSize = pPager->pageSize*(i64)nPage;
1645 if( rc==SQLITE_OK && currentSize>newSize ){
1646 rc = sqlite3OsTruncate(pPager->fd, newSize);
1647 }
danielk1977e180dd92007-04-05 17:15:52 +00001648 }
1649 if( rc==SQLITE_OK ){
1650 pPager->dbSize = nPage;
1651 pager_truncate_cache(pPager);
1652 }
1653 return rc;
drhcb4c40b2004-08-18 19:09:43 +00001654}
1655
1656/*
drhc80f0582007-05-01 16:59:48 +00001657** Set the sectorSize for the given pager.
1658**
1659** The sector size is the larger of the sector size reported
1660** by sqlite3OsSectorSize() and the pageSize.
1661*/
1662static void setSectorSize(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00001663 assert(pPager->fd->pMethods||pPager->tempFile);
1664 if( !pPager->tempFile ){
1665 /* Sector size doesn't matter for temporary files. Also, the file
1666 ** may not have been opened yet, in whcih case the OsSectorSize()
1667 ** call will segfault.
1668 */
1669 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
1670 }
drhc80f0582007-05-01 16:59:48 +00001671 if( pPager->sectorSize<pPager->pageSize ){
1672 pPager->sectorSize = pPager->pageSize;
1673 }
1674}
1675
1676/*
drhed7c8552001-04-11 14:29:21 +00001677** Playback the journal and thus restore the database file to
1678** the state it was in before we started making changes.
1679**
drh34e79ce2004-02-08 06:05:46 +00001680** The journal file format is as follows:
1681**
drhae2b40c2004-06-09 19:03:54 +00001682** (1) 8 byte prefix. A copy of aJournalMagic[].
1683** (2) 4 byte big-endian integer which is the number of valid page records
drh34e79ce2004-02-08 06:05:46 +00001684** in the journal. If this value is 0xffffffff, then compute the
drhae2b40c2004-06-09 19:03:54 +00001685** number of page records from the journal size.
1686** (3) 4 byte big-endian integer which is the initial value for the
1687** sanity checksum.
1688** (4) 4 byte integer which is the number of pages to truncate the
drh34e79ce2004-02-08 06:05:46 +00001689** database to during a rollback.
drhae2b40c2004-06-09 19:03:54 +00001690** (5) 4 byte integer which is the number of bytes in the master journal
1691** name. The value may be zero (indicate that there is no master
1692** journal.)
1693** (6) N bytes of the master journal name. The name will be nul-terminated
1694** and might be shorter than the value read from (5). If the first byte
1695** of the name is \000 then there is no master journal. The master
1696** journal name is stored in UTF-8.
1697** (7) Zero or more pages instances, each as follows:
drh34e79ce2004-02-08 06:05:46 +00001698** + 4 byte page number.
drhae2b40c2004-06-09 19:03:54 +00001699** + pPager->pageSize bytes of data.
1700** + 4 byte checksum
drh34e79ce2004-02-08 06:05:46 +00001701**
drhae2b40c2004-06-09 19:03:54 +00001702** When we speak of the journal header, we mean the first 6 items above.
1703** Each entry in the journal is an instance of the 7th item.
drh34e79ce2004-02-08 06:05:46 +00001704**
1705** Call the value from the second bullet "nRec". nRec is the number of
1706** valid page entries in the journal. In most cases, you can compute the
1707** value of nRec from the size of the journal file. But if a power
1708** failure occurred while the journal was being written, it could be the
1709** case that the size of the journal file had already been increased but
1710** the extra entries had not yet made it safely to disk. In such a case,
1711** the value of nRec computed from the file size would be too large. For
1712** that reason, we always use the nRec value in the header.
1713**
1714** If the nRec value is 0xffffffff it means that nRec should be computed
1715** from the file size. This value is used when the user selects the
1716** no-sync option for the journal. A power failure could lead to corruption
1717** in this case. But for things like temporary table (which will be
1718** deleted when the power is restored) we don't care.
1719**
drhd9b02572001-04-15 00:37:09 +00001720** If the file opened as the journal file is not a well-formed
danielk1977ece80f12004-06-23 01:05:26 +00001721** journal file then all pages up to the first corrupted page are rolled
1722** back (or no pages if the journal header is corrupted). The journal file
1723** is then deleted and SQLITE_OK returned, just as if no corruption had
1724** been encountered.
1725**
1726** If an I/O or malloc() error occurs, the journal-file is not deleted
1727** and an error code is returned.
drhed7c8552001-04-11 14:29:21 +00001728*/
danielk1977e277be02007-03-23 18:12:06 +00001729static int pager_playback(Pager *pPager, int isHot){
danielk1977b4b47412007-08-17 15:53:36 +00001730 sqlite3_vfs *pVfs = pPager->pVfs;
drheb206252004-10-01 02:00:31 +00001731 i64 szJ; /* Size of the journal file in bytes */
danielk1977c3e8f5e2004-06-28 01:16:46 +00001732 u32 nRec; /* Number of Records in the journal */
drhd9b02572001-04-15 00:37:09 +00001733 int i; /* Loop counter */
1734 Pgno mxPg = 0; /* Size of the original file in pages */
drhae2b40c2004-06-09 19:03:54 +00001735 int rc; /* Result code of a subroutine */
danielk197713adf8a2004-06-03 16:08:41 +00001736 char *zMaster = 0; /* Name of master journal file if any */
drhed7c8552001-04-11 14:29:21 +00001737
drhc3a64ba2001-11-22 00:01:27 +00001738 /* Figure out how many records are in the journal. Abort early if
1739 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +00001740 */
drh8cfbf082001-09-19 13:22:39 +00001741 assert( pPager->journalOpen );
drh054889e2005-11-30 03:20:31 +00001742 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
danielk1977334cdb62007-03-26 08:05:12 +00001743 if( rc!=SQLITE_OK || szJ==0 ){
drhc3a64ba2001-11-22 00:01:27 +00001744 goto end_playback;
1745 }
drh240c5792004-02-08 00:40:52 +00001746
danielk197776572402004-06-25 02:38:54 +00001747 /* Read the master journal name from the journal, if it is present.
1748 ** If a master journal file name is specified, but the file is not
1749 ** present on disk, then the journal is not hot and does not need to be
1750 ** played back.
drh240c5792004-02-08 00:40:52 +00001751 */
danielk197765839c62007-08-30 08:08:17 +00001752 zMaster = pPager->pTmpSpace;
1753 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
danielk197776572402004-06-25 02:38:54 +00001754 assert( rc!=SQLITE_DONE );
danielk1977b4b47412007-08-17 15:53:36 +00001755 if( rc!=SQLITE_OK
danielk197765839c62007-08-30 08:08:17 +00001756 || (zMaster[0] && !sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS))
danielk1977b4b47412007-08-17 15:53:36 +00001757 ){
danielk197776572402004-06-25 02:38:54 +00001758 zMaster = 0;
1759 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
drhc3a64ba2001-11-22 00:01:27 +00001760 goto end_playback;
1761 }
danielk197776572402004-06-25 02:38:54 +00001762 pPager->journalOff = 0;
danielk197765839c62007-08-30 08:08:17 +00001763 zMaster = 0;
drhc3a64ba2001-11-22 00:01:27 +00001764
danielk197776572402004-06-25 02:38:54 +00001765 /* This loop terminates either when the readJournalHdr() call returns
1766 ** SQLITE_DONE or an IO error occurs. */
1767 while( 1 ){
drhae2b40c2004-06-09 19:03:54 +00001768
danielk197776572402004-06-25 02:38:54 +00001769 /* Read the next journal header from the journal file. If there are
1770 ** not enough bytes left in the journal file for a complete header, or
1771 ** it is corrupted, then a process must of failed while writing it.
1772 ** This indicates nothing more needs to be rolled back.
1773 */
1774 rc = readJournalHdr(pPager, szJ, &nRec, &mxPg);
1775 if( rc!=SQLITE_OK ){
drh968af522003-02-11 14:55:40 +00001776 if( rc==SQLITE_DONE ){
drh968af522003-02-11 14:55:40 +00001777 rc = SQLITE_OK;
1778 }
danielk197776572402004-06-25 02:38:54 +00001779 goto end_playback;
1780 }
1781
1782 /* If nRec is 0xffffffff, then this journal was created by a process
1783 ** working in no-sync mode. This means that the rest of the journal
1784 ** file consists of pages, there are no more journal headers. Compute
1785 ** the value of nRec based on this assumption.
1786 */
1787 if( nRec==0xffffffff ){
1788 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
1789 nRec = (szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager);
1790 }
1791
danielk1977e277be02007-03-23 18:12:06 +00001792 /* If nRec is 0 and this rollback is of a transaction created by this
drh8940f4e2007-08-11 00:26:20 +00001793 ** process and if this is the final header in the journal, then it means
1794 ** that this part of the journal was being filled but has not yet been
1795 ** synced to disk. Compute the number of pages based on the remaining
1796 ** size of the file.
1797 **
1798 ** The third term of the test was added to fix ticket #2565.
danielk1977e277be02007-03-23 18:12:06 +00001799 */
drh8940f4e2007-08-11 00:26:20 +00001800 if( nRec==0 && !isHot &&
1801 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
danielk1977e277be02007-03-23 18:12:06 +00001802 nRec = (szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager);
1803 }
1804
danielk197776572402004-06-25 02:38:54 +00001805 /* If this is the first header read from the journal, truncate the
drh85b623f2007-12-13 21:54:09 +00001806 ** database file back to its original size.
danielk197776572402004-06-25 02:38:54 +00001807 */
danielk1977e180dd92007-04-05 17:15:52 +00001808 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
drhcb4c40b2004-08-18 19:09:43 +00001809 rc = pager_truncate(pPager, mxPg);
danielk197776572402004-06-25 02:38:54 +00001810 if( rc!=SQLITE_OK ){
1811 goto end_playback;
1812 }
danielk197776572402004-06-25 02:38:54 +00001813 }
1814
danielk197776572402004-06-25 02:38:54 +00001815 /* Copy original pages out of the journal and back into the database file.
1816 */
1817 for(i=0; i<nRec; i++){
danielk197762079062007-08-15 17:08:46 +00001818 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001819 if( rc!=SQLITE_OK ){
1820 if( rc==SQLITE_DONE ){
1821 rc = SQLITE_OK;
1822 pPager->journalOff = szJ;
1823 break;
1824 }else{
1825 goto end_playback;
1826 }
1827 }
drh968af522003-02-11 14:55:40 +00001828 }
drhed7c8552001-04-11 14:29:21 +00001829 }
drh580eeaf2006-02-24 03:09:37 +00001830 /*NOTREACHED*/
1831 assert( 0 );
drh4a0681e2003-02-13 01:58:20 +00001832
1833end_playback:
danielk19778191bff2004-06-28 04:52:30 +00001834 if( rc==SQLITE_OK ){
danielk197765839c62007-08-30 08:08:17 +00001835 zMaster = pPager->pTmpSpace;
1836 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
1837 }
1838 if( rc==SQLITE_OK ){
drh80e35f42007-03-30 14:06:34 +00001839 rc = pager_end_transaction(pPager);
danielk19778191bff2004-06-28 04:52:30 +00001840 }
danielk197765839c62007-08-30 08:08:17 +00001841 if( rc==SQLITE_OK && zMaster[0] ){
danielk1977979f38e2007-03-27 16:19:51 +00001842 /* If there was a master journal and this routine will return success,
danielk197732554c12005-01-22 03:39:39 +00001843 ** see if it is possible to delete the master journal.
danielk197713adf8a2004-06-03 16:08:41 +00001844 */
danielk197765839c62007-08-30 08:08:17 +00001845 rc = pager_delmaster(pPager, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001846 }
danielk197776572402004-06-25 02:38:54 +00001847
1848 /* The Pager.sectorSize variable may have been updated while rolling
drh3ceeb752007-03-29 18:19:52 +00001849 ** back a journal created by a process with a different sector size
danielk197776572402004-06-25 02:38:54 +00001850 ** value. Reset it to the correct value for this process.
1851 */
drhc80f0582007-05-01 16:59:48 +00001852 setSectorSize(pPager);
drhd9b02572001-04-15 00:37:09 +00001853 return rc;
drhed7c8552001-04-11 14:29:21 +00001854}
1855
1856/*
drhac69b052004-05-12 13:30:07 +00001857** Playback the statement journal.
drhfa86c412002-02-02 15:01:15 +00001858**
1859** This is similar to playing back the transaction journal but with
1860** a few extra twists.
1861**
drh663fc632002-02-02 18:49:19 +00001862** (1) The number of pages in the database file at the start of
drhac69b052004-05-12 13:30:07 +00001863** the statement is stored in pPager->stmtSize, not in the
drh663fc632002-02-02 18:49:19 +00001864** journal file itself.
drhfa86c412002-02-02 15:01:15 +00001865**
drhac69b052004-05-12 13:30:07 +00001866** (2) In addition to playing back the statement journal, also
drhfa86c412002-02-02 15:01:15 +00001867** playback all pages of the transaction journal beginning
drhac69b052004-05-12 13:30:07 +00001868** at offset pPager->stmtJSize.
drhfa86c412002-02-02 15:01:15 +00001869*/
drh3aac2dd2004-04-26 14:10:20 +00001870static int pager_stmt_playback(Pager *pPager){
drheb206252004-10-01 02:00:31 +00001871 i64 szJ; /* Size of the full journal */
1872 i64 hdrOff;
drh968af522003-02-11 14:55:40 +00001873 int nRec; /* Number of Records */
drhfa86c412002-02-02 15:01:15 +00001874 int i; /* Loop counter */
1875 int rc;
1876
danielk197776572402004-06-25 02:38:54 +00001877 szJ = pPager->journalOff;
1878#ifndef NDEBUG
1879 {
drheb206252004-10-01 02:00:31 +00001880 i64 os_szJ;
drh054889e2005-11-30 03:20:31 +00001881 rc = sqlite3OsFileSize(pPager->jfd, &os_szJ);
danielk197776572402004-06-25 02:38:54 +00001882 if( rc!=SQLITE_OK ) return rc;
1883 assert( szJ==os_szJ );
1884 }
1885#endif
1886
danielk19774099f6e2007-03-19 11:25:20 +00001887 /* Set hdrOff to be the offset just after the end of the last journal
1888 ** page written before the first journal-header for this statement
1889 ** transaction was written, or the end of the file if no journal
danielk197776572402004-06-25 02:38:54 +00001890 ** header was written.
1891 */
1892 hdrOff = pPager->stmtHdrOff;
1893 assert( pPager->fullSync || !hdrOff );
1894 if( !hdrOff ){
1895 hdrOff = szJ;
1896 }
1897
drhfa86c412002-02-02 15:01:15 +00001898 /* Truncate the database back to its original size.
1899 */
danielk1977e180dd92007-04-05 17:15:52 +00001900 rc = pager_truncate(pPager, pPager->stmtSize);
drh1aa2d8b2007-01-03 15:34:29 +00001901 assert( pPager->state>=PAGER_SHARED );
drhfa86c412002-02-02 15:01:15 +00001902
drhac69b052004-05-12 13:30:07 +00001903 /* Figure out how many records are in the statement journal.
drhfa86c412002-02-02 15:01:15 +00001904 */
drhac69b052004-05-12 13:30:07 +00001905 assert( pPager->stmtInUse && pPager->journalOpen );
drhac69b052004-05-12 13:30:07 +00001906 nRec = pPager->stmtNRec;
drhfa86c412002-02-02 15:01:15 +00001907
drhac69b052004-05-12 13:30:07 +00001908 /* Copy original pages out of the statement journal and back into the
drhae2b40c2004-06-09 19:03:54 +00001909 ** database file. Note that the statement journal omits checksums from
1910 ** each record since power-failure recovery is not important to statement
1911 ** journals.
drhfa86c412002-02-02 15:01:15 +00001912 */
danielk197762079062007-08-15 17:08:46 +00001913 for(i=0; i<nRec; i++){
1914 i64 offset = i*(4+pPager->pageSize);
1915 rc = pager_playback_one_page(pPager, pPager->stfd, offset, 0);
drh968af522003-02-11 14:55:40 +00001916 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001917 if( rc!=SQLITE_OK ) goto end_stmt_playback;
drhfa86c412002-02-02 15:01:15 +00001918 }
1919
danielk197776572402004-06-25 02:38:54 +00001920 /* Now roll some pages back from the transaction journal. Pager.stmtJSize
1921 ** was the size of the journal file when this statement was started, so
1922 ** everything after that needs to be rolled back, either into the
1923 ** database, the memory cache, or both.
1924 **
1925 ** If it is not zero, then Pager.stmtHdrOff is the offset to the start
1926 ** of the first journal header written during this statement transaction.
drhfa86c412002-02-02 15:01:15 +00001927 */
danielk197776572402004-06-25 02:38:54 +00001928 pPager->journalOff = pPager->stmtJSize;
danielk197775edc162004-06-26 01:48:18 +00001929 pPager->cksumInit = pPager->stmtCksum;
danielk19774099f6e2007-03-19 11:25:20 +00001930 while( pPager->journalOff < hdrOff ){
danielk197762079062007-08-15 17:08:46 +00001931 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001932 assert( rc!=SQLITE_DONE );
1933 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1934 }
1935
1936 while( pPager->journalOff < szJ ){
danielk1977f0113002006-01-24 12:09:17 +00001937 u32 nJRec; /* Number of Journal Records */
danielk197776572402004-06-25 02:38:54 +00001938 u32 dummy;
danielk1977f0113002006-01-24 12:09:17 +00001939 rc = readJournalHdr(pPager, szJ, &nJRec, &dummy);
drh968af522003-02-11 14:55:40 +00001940 if( rc!=SQLITE_OK ){
1941 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001942 goto end_stmt_playback;
drh968af522003-02-11 14:55:40 +00001943 }
danielk1977f0113002006-01-24 12:09:17 +00001944 if( nJRec==0 ){
1945 nJRec = (szJ - pPager->journalOff) / (pPager->pageSize+8);
danielk197775edc162004-06-26 01:48:18 +00001946 }
danielk1977f0113002006-01-24 12:09:17 +00001947 for(i=nJRec-1; i>=0 && pPager->journalOff < szJ; i--){
danielk197762079062007-08-15 17:08:46 +00001948 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001949 assert( rc!=SQLITE_DONE );
1950 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1951 }
drhfa86c412002-02-02 15:01:15 +00001952 }
danielk197776572402004-06-25 02:38:54 +00001953
1954 pPager->journalOff = szJ;
drhfa86c412002-02-02 15:01:15 +00001955
drh3aac2dd2004-04-26 14:10:20 +00001956end_stmt_playback:
danielk19778a7aea32006-01-23 15:25:48 +00001957 if( rc==SQLITE_OK) {
danielk197775edc162004-06-26 01:48:18 +00001958 pPager->journalOff = szJ;
1959 /* pager_reload_cache(pPager); */
drhfa86c412002-02-02 15:01:15 +00001960 }
1961 return rc;
1962}
1963
1964/*
drhf57b14a2001-09-14 18:54:08 +00001965** Change the maximum number of in-memory pages that are allowed.
1966*/
danielk19773b8a05f2007-03-19 17:44:26 +00001967void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
drhf57b14a2001-09-14 18:54:08 +00001968 if( mxPage>10 ){
1969 pPager->mxPage = mxPage;
danielk1977ef317ab2004-06-23 10:43:10 +00001970 }else{
1971 pPager->mxPage = 10;
drhf57b14a2001-09-14 18:54:08 +00001972 }
1973}
1974
1975/*
drh973b6e32003-02-12 14:09:42 +00001976** Adjust the robustness of the database to damage due to OS crashes
1977** or power failures by changing the number of syncs()s when writing
1978** the rollback journal. There are three levels:
1979**
drh054889e2005-11-30 03:20:31 +00001980** OFF sqlite3OsSync() is never called. This is the default
drh973b6e32003-02-12 14:09:42 +00001981** for temporary and transient files.
1982**
1983** NORMAL The journal is synced once before writes begin on the
1984** database. This is normally adequate protection, but
1985** it is theoretically possible, though very unlikely,
1986** that an inopertune power failure could leave the journal
1987** in a state which would cause damage to the database
1988** when it is rolled back.
1989**
1990** FULL The journal is synced twice before writes begin on the
drh34e79ce2004-02-08 06:05:46 +00001991** database (with some additional information - the nRec field
1992** of the journal header - being written in between the two
1993** syncs). If we assume that writing a
drh973b6e32003-02-12 14:09:42 +00001994** single disk sector is atomic, then this mode provides
1995** assurance that the journal will not be corrupted to the
1996** point of causing damage to the database during rollback.
1997**
1998** Numeric values associated with these states are OFF==1, NORMAL=2,
1999** and FULL=3.
2000*/
danielk197793758c82005-01-21 08:13:14 +00002001#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk19773b8a05f2007-03-19 17:44:26 +00002002void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int full_fsync){
drh973b6e32003-02-12 14:09:42 +00002003 pPager->noSync = level==1 || pPager->tempFile;
2004 pPager->fullSync = level==3 && !pPager->tempFile;
danielk1977f036aef2007-08-20 05:36:51 +00002005 pPager->sync_flags = (full_fsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL);
danielk19771d850a72004-05-31 08:26:49 +00002006 if( pPager->noSync ) pPager->needSync = 0;
drh973b6e32003-02-12 14:09:42 +00002007}
danielk197793758c82005-01-21 08:13:14 +00002008#endif
drh973b6e32003-02-12 14:09:42 +00002009
2010/*
drhaf6df112005-06-07 02:12:30 +00002011** The following global variable is incremented whenever the library
2012** attempts to open a temporary file. This information is used for
2013** testing and analysis only.
2014*/
drh0f7eb612006-08-08 13:51:43 +00002015#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00002016int sqlite3_opentemp_count = 0;
drh0f7eb612006-08-08 13:51:43 +00002017#endif
drhaf6df112005-06-07 02:12:30 +00002018
2019/*
drh3f56e6e2007-03-15 01:16:47 +00002020** Open a temporary file.
2021**
2022** Write the file descriptor into *fd. Return SQLITE_OK on success or some
danielk1977fee2d252007-08-18 10:59:19 +00002023** other error code if we fail. The OS will automatically delete the temporary
2024** file when it is closed.
drhfa86c412002-02-02 15:01:15 +00002025*/
danielk1977b4b47412007-08-17 15:53:36 +00002026static int sqlite3PagerOpentemp(
drh33f4e022007-09-03 15:19:34 +00002027 sqlite3_vfs *pVfs, /* The virtual file system layer */
2028 sqlite3_file *pFile, /* Write the file descriptor here */
2029 char *zFilename, /* Name of the file. Might be NULL */
2030 int vfsFlags /* Flags passed through to the VFS */
danielk1977b4b47412007-08-17 15:53:36 +00002031){
drhfa86c412002-02-02 15:01:15 +00002032 int rc;
drh334b2992007-09-06 23:28:23 +00002033 assert( zFilename!=0 );
drh3f56e6e2007-03-15 01:16:47 +00002034
drh0f7eb612006-08-08 13:51:43 +00002035#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00002036 sqlite3_opentemp_count++; /* Used for testing and analysis only */
drh0f7eb612006-08-08 13:51:43 +00002037#endif
danielk1977b4b47412007-08-17 15:53:36 +00002038
drh33f4e022007-09-03 15:19:34 +00002039 vfsFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
2040 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
2041 rc = sqlite3OsOpen(pVfs, zFilename, pFile, vfsFlags, 0);
drhae28c012007-08-24 16:29:23 +00002042 assert( rc!=SQLITE_OK || pFile->pMethods );
drhfa86c412002-02-02 15:01:15 +00002043 return rc;
2044}
2045
2046/*
drhed7c8552001-04-11 14:29:21 +00002047** Create a new page cache and put a pointer to the page cache in *ppPager.
drh5e00f6c2001-09-13 13:46:56 +00002048** The file to be cached need not exist. The file is not locked until
danielk19773b8a05f2007-03-19 17:44:26 +00002049** the first call to sqlite3PagerGet() and is only held open until the
2050** last page is released using sqlite3PagerUnref().
drh382c0242001-10-06 16:33:02 +00002051**
drh6446c4d2001-12-15 14:22:18 +00002052** If zFilename is NULL then a randomly-named temporary file is created
drh1cc8c442007-08-24 16:08:29 +00002053** and used as the file to be cached. The file will be deleted
drh6446c4d2001-12-15 14:22:18 +00002054** automatically when it is closed.
drh90f5ecb2004-07-22 01:19:35 +00002055**
2056** If zFilename is ":memory:" then all information is held in cache.
2057** It is never written to disk. This can be used to implement an
2058** in-memory database.
drhed7c8552001-04-11 14:29:21 +00002059*/
danielk19773b8a05f2007-03-19 17:44:26 +00002060int sqlite3PagerOpen(
drh86f8c192007-08-22 00:39:19 +00002061 sqlite3_vfs *pVfs, /* The virtual file system to use */
drh7e3b0a02001-04-28 16:52:40 +00002062 Pager **ppPager, /* Return the Pager structure here */
2063 const char *zFilename, /* Name of the database file to open */
drhda47d772002-12-02 04:25:19 +00002064 int nExtra, /* Extra bytes append to each in-memory page */
drh33f4e022007-09-03 15:19:34 +00002065 int flags, /* flags controlling this file */
2066 int vfsFlags /* flags passed through to sqlite3_vfs.xOpen() */
drh7e3b0a02001-04-28 16:52:40 +00002067){
danielk1977b4b47412007-08-17 15:53:36 +00002068 u8 *pPtr;
danielk1977aef0bf62005-12-30 16:28:01 +00002069 Pager *pPager = 0;
danielk1977cfe9a692004-06-16 12:00:29 +00002070 int rc = SQLITE_OK;
2071 int i;
danielk19778def5ea2004-06-16 10:39:52 +00002072 int tempFile = 0;
drhac69b052004-05-12 13:30:07 +00002073 int memDb = 0;
drh5e00f6c2001-09-13 13:46:56 +00002074 int readOnly = 0;
drh7bec5052005-02-06 02:45:41 +00002075 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0;
2076 int noReadlock = (flags & PAGER_NO_READLOCK)!=0;
danielk1977c7b60172007-08-22 11:22:03 +00002077 int journalFileSize = sqlite3JournalSize(pVfs);
drh1cc8c442007-08-24 16:08:29 +00002078 int nDefaultPage = SQLITE_DEFAULT_PAGE_SIZE;
2079 char *zPathname;
2080 int nPathname;
danielk1977b4b47412007-08-17 15:53:36 +00002081
drh86f8c192007-08-22 00:39:19 +00002082 /* The default return is a NULL pointer */
drhd9b02572001-04-15 00:37:09 +00002083 *ppPager = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00002084
drh1cc8c442007-08-24 16:08:29 +00002085 /* Compute the full pathname */
danielk1977adfb9b02007-09-17 07:02:56 +00002086 nPathname = pVfs->mxPathname+1;
2087 zPathname = sqlite3_malloc(nPathname);
drh1cc8c442007-08-24 16:08:29 +00002088 if( zPathname==0 ){
2089 return SQLITE_NOMEM;
2090 }
2091 if( zFilename && zFilename[0] ){
2092#ifndef SQLITE_OMIT_MEMORYDB
2093 if( strcmp(zFilename,":memory:")==0 ){
2094 memDb = 1;
2095 zPathname[0] = 0;
2096 }else
2097#endif
2098 {
danielk1977adfb9b02007-09-17 07:02:56 +00002099 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
drh1cc8c442007-08-24 16:08:29 +00002100 }
2101 }else{
danielk1977adfb9b02007-09-17 07:02:56 +00002102 rc = sqlite3OsGetTempname(pVfs, nPathname, zPathname);
drhae28c012007-08-24 16:29:23 +00002103 }
2104 if( rc!=SQLITE_OK ){
2105 sqlite3_free(zPathname);
2106 return rc;
drh1cc8c442007-08-24 16:08:29 +00002107 }
2108 nPathname = strlen(zPathname);
2109
danielk1977b4b47412007-08-17 15:53:36 +00002110 /* Allocate memory for the pager structure */
2111 pPager = sqlite3MallocZero(
2112 sizeof(*pPager) + /* Pager structure */
danielk1977c7b60172007-08-22 11:22:03 +00002113 journalFileSize + /* The journal file structure */
2114 pVfs->szOsFile * 2 + /* The db and stmt journal files */
drh334b2992007-09-06 23:28:23 +00002115 4*nPathname + 40 /* zFilename, zDirectory, zJournal, zStmtJrnl */
danielk1977b4b47412007-08-17 15:53:36 +00002116 );
2117 if( !pPager ){
drh1cc8c442007-08-24 16:08:29 +00002118 sqlite3_free(zPathname);
danielk1977b4b47412007-08-17 15:53:36 +00002119 return SQLITE_NOMEM;
2120 }
2121 pPtr = (u8 *)&pPager[1];
drh33f4e022007-09-03 15:19:34 +00002122 pPager->vfsFlags = vfsFlags;
danielk1977b4b47412007-08-17 15:53:36 +00002123 pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0];
danielk1977c7b60172007-08-22 11:22:03 +00002124 pPager->stfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*1];
2125 pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*2];
2126 pPager->zFilename = (char*)&pPtr[pVfs->szOsFile*2+journalFileSize];
drh1cc8c442007-08-24 16:08:29 +00002127 pPager->zDirectory = &pPager->zFilename[nPathname+1];
2128 pPager->zJournal = &pPager->zDirectory[nPathname+1];
drh334b2992007-09-06 23:28:23 +00002129 pPager->zStmtJrnl = &pPager->zJournal[nPathname+10];
danielk1977b4b47412007-08-17 15:53:36 +00002130 pPager->pVfs = pVfs;
drh1cc8c442007-08-24 16:08:29 +00002131 memcpy(pPager->zFilename, zPathname, nPathname+1);
2132 sqlite3_free(zPathname);
danielk1977b4b47412007-08-17 15:53:36 +00002133
drh153c62c2007-08-24 03:51:33 +00002134 /* Open the pager file.
danielk1977aef0bf62005-12-30 16:28:01 +00002135 */
drhae28c012007-08-24 16:29:23 +00002136 if( zFilename && zFilename[0] && !memDb ){
drh1cc8c442007-08-24 16:08:29 +00002137 if( nPathname>(pVfs->mxPathname - sizeof("-journal")) ){
2138 rc = SQLITE_CANTOPEN;
2139 }else{
drh1cc8c442007-08-24 16:08:29 +00002140 int fout = 0;
drh33f4e022007-09-03 15:19:34 +00002141 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd,
2142 pPager->vfsFlags, &fout);
drh1cc8c442007-08-24 16:08:29 +00002143 readOnly = (fout&SQLITE_OPEN_READONLY);
2144
2145 /* If the file was successfully opened for read/write access,
2146 ** choose a default page size in case we have to create the
2147 ** database file. The default page size is the maximum of:
2148 **
2149 ** + SQLITE_DEFAULT_PAGE_SIZE,
2150 ** + The value returned by sqlite3OsSectorSize()
2151 ** + The largest page size that can be written atomically.
2152 */
2153 if( rc==SQLITE_OK && !readOnly ){
2154 int iSectorSize = sqlite3OsSectorSize(pPager->fd);
2155 if( nDefaultPage<iSectorSize ){
2156 nDefaultPage = iSectorSize;
2157 }
danielk19779663b8f2007-08-24 11:52:28 +00002158#ifdef SQLITE_ENABLE_ATOMIC_WRITE
drh1cc8c442007-08-24 16:08:29 +00002159 {
2160 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
2161 int ii;
2162 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
2163 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
2164 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
2165 for(ii=nDefaultPage; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
2166 if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ) nDefaultPage = ii;
danielk19779663b8f2007-08-24 11:52:28 +00002167 }
danielk1977b4b47412007-08-17 15:53:36 +00002168 }
drh1cc8c442007-08-24 16:08:29 +00002169#endif
2170 if( nDefaultPage>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
2171 nDefaultPage = SQLITE_MAX_DEFAULT_PAGE_SIZE;
2172 }
danielk19778def5ea2004-06-16 10:39:52 +00002173 }
drhac69b052004-05-12 13:30:07 +00002174 }
drh1cc8c442007-08-24 16:08:29 +00002175 }else if( !memDb ){
danielk19777a2b1ee2007-08-21 14:27:01 +00002176 /* If a temporary file is requested, it is not opened immediately.
2177 ** In this case we accept the default page size and delay actually
2178 ** opening the file until the first call to OsWrite().
2179 */
danielk19777a2b1ee2007-08-21 14:27:01 +00002180 tempFile = 1;
2181 pPager->state = PAGER_EXCLUSIVE;
drh5e00f6c2001-09-13 13:46:56 +00002182 }
danielk1977aef0bf62005-12-30 16:28:01 +00002183
danielk1977b4b47412007-08-17 15:53:36 +00002184 if( pPager && rc==SQLITE_OK ){
danielk19779663b8f2007-08-24 11:52:28 +00002185 pPager->pTmpSpace = (char *)sqlite3_malloc(nDefaultPage);
drh3e7a6092002-12-07 21:45:14 +00002186 }
danielk1977aef0bf62005-12-30 16:28:01 +00002187
drh153c62c2007-08-24 03:51:33 +00002188 /* If an error occured in either of the blocks above.
2189 ** Free the Pager structure and close the file.
2190 ** Since the pager is not allocated there is no need to set
danielk1977aef0bf62005-12-30 16:28:01 +00002191 ** any Pager.errMask variables.
2192 */
danielk1977b4b47412007-08-17 15:53:36 +00002193 if( !pPager || !pPager->pTmpSpace ){
2194 sqlite3OsClose(pPager->fd);
drh17435752007-08-16 04:30:38 +00002195 sqlite3_free(pPager);
danielk1977aef0bf62005-12-30 16:28:01 +00002196 return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc);
drhed7c8552001-04-11 14:29:21 +00002197 }
danielk1977aef0bf62005-12-30 16:28:01 +00002198
drh153c62c2007-08-24 03:51:33 +00002199 PAGERTRACE3("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename);
2200 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
danielk1977aef0bf62005-12-30 16:28:01 +00002201
danielk1977b4b47412007-08-17 15:53:36 +00002202 /* Fill in Pager.zDirectory[] */
drh1cc8c442007-08-24 16:08:29 +00002203 memcpy(pPager->zDirectory, pPager->zFilename, nPathname+1);
danielk1977b4b47412007-08-17 15:53:36 +00002204 for(i=strlen(pPager->zDirectory); i>0 && pPager->zDirectory[i-1]!='/'; i--){}
drha76c82e2003-07-27 18:59:42 +00002205 if( i>0 ) pPager->zDirectory[i-1] = 0;
danielk1977b4b47412007-08-17 15:53:36 +00002206
drh334b2992007-09-06 23:28:23 +00002207 /* Fill in Pager.zJournal[] and Pager.zStmtJrnl[] */
drh1cc8c442007-08-24 16:08:29 +00002208 memcpy(pPager->zJournal, pPager->zFilename, nPathname);
2209 memcpy(&pPager->zJournal[nPathname], "-journal", 9);
drh334b2992007-09-06 23:28:23 +00002210 memcpy(pPager->zStmtJrnl, pPager->zFilename, nPathname);
2211 memcpy(&pPager->zStmtJrnl[nPathname], "-stmtjrnl", 10);
danielk1977b4b47412007-08-17 15:53:36 +00002212
drh3b59a5c2006-01-15 20:28:28 +00002213 /* pPager->journalOpen = 0; */
drhac69b052004-05-12 13:30:07 +00002214 pPager->useJournal = useJournal && !memDb;
drh7bec5052005-02-06 02:45:41 +00002215 pPager->noReadlock = noReadlock && readOnly;
drh3b59a5c2006-01-15 20:28:28 +00002216 /* pPager->stmtOpen = 0; */
2217 /* pPager->stmtInUse = 0; */
2218 /* pPager->nRef = 0; */
drhac69b052004-05-12 13:30:07 +00002219 pPager->dbSize = memDb-1;
danielk19779663b8f2007-08-24 11:52:28 +00002220 pPager->pageSize = nDefaultPage;
drh3b59a5c2006-01-15 20:28:28 +00002221 /* pPager->stmtSize = 0; */
2222 /* pPager->stmtJSize = 0; */
2223 /* pPager->nPage = 0; */
drh90f5ecb2004-07-22 01:19:35 +00002224 pPager->mxPage = 100;
drhf8e632b2007-05-08 14:51:36 +00002225 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
drh3b59a5c2006-01-15 20:28:28 +00002226 /* pPager->state = PAGER_UNLOCK; */
drh1cc8c442007-08-24 16:08:29 +00002227 assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
drh3b59a5c2006-01-15 20:28:28 +00002228 /* pPager->errMask = 0; */
drh5e00f6c2001-09-13 13:46:56 +00002229 pPager->tempFile = tempFile;
drh369339d2007-03-30 16:01:55 +00002230 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
2231 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
2232 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
2233 pPager->exclusiveMode = tempFile;
drhac69b052004-05-12 13:30:07 +00002234 pPager->memDb = memDb;
drh5e00f6c2001-09-13 13:46:56 +00002235 pPager->readOnly = readOnly;
drh3b59a5c2006-01-15 20:28:28 +00002236 /* pPager->needSync = 0; */
drhda47d772002-12-02 04:25:19 +00002237 pPager->noSync = pPager->tempFile || !useJournal;
danielk197775edc162004-06-26 01:48:18 +00002238 pPager->fullSync = (pPager->noSync?0:1);
danielk1977f036aef2007-08-20 05:36:51 +00002239 pPager->sync_flags = SQLITE_SYNC_NORMAL;
drh3b59a5c2006-01-15 20:28:28 +00002240 /* pPager->pFirst = 0; */
2241 /* pPager->pFirstSynced = 0; */
2242 /* pPager->pLast = 0; */
drh887dc4c2004-10-22 16:22:57 +00002243 pPager->nExtra = FORCE_ALIGNMENT(nExtra);
danielk19777a2b1ee2007-08-21 14:27:01 +00002244 assert(pPager->fd->pMethods||memDb||tempFile);
danielk1977b4721172007-03-19 05:54:48 +00002245 if( !memDb ){
drhc80f0582007-05-01 16:59:48 +00002246 setSectorSize(pPager);
danielk1977b4721172007-03-19 05:54:48 +00002247 }
drh3b59a5c2006-01-15 20:28:28 +00002248 /* pPager->pBusyHandler = 0; */
2249 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
drhed7c8552001-04-11 14:29:21 +00002250 *ppPager = pPager;
drh6f7adc82006-01-11 21:41:20 +00002251#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +00002252 pPager->iInUseMM = 0;
2253 pPager->iInUseDB = 0;
2254 if( !memDb ){
2255 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
2256 sqlite3_mutex_enter(mutex);
2257 pPager->pNext = sqlite3PagerList;
2258 if( sqlite3PagerList ){
2259 assert( sqlite3PagerList->pPrev==0 );
2260 sqlite3PagerList->pPrev = pPager;
2261 }
2262 pPager->pPrev = 0;
2263 sqlite3PagerList = pPager;
2264 sqlite3_mutex_leave(mutex);
2265 }
danielk197752622822006-01-09 09:59:49 +00002266#endif
drhed7c8552001-04-11 14:29:21 +00002267 return SQLITE_OK;
2268}
2269
2270/*
drh90f5ecb2004-07-22 01:19:35 +00002271** Set the busy handler function.
2272*/
danielk19773b8a05f2007-03-19 17:44:26 +00002273void sqlite3PagerSetBusyhandler(Pager *pPager, BusyHandler *pBusyHandler){
drh90f5ecb2004-07-22 01:19:35 +00002274 pPager->pBusyHandler = pBusyHandler;
2275}
2276
2277/*
drh72f82862001-05-24 21:06:34 +00002278** Set the destructor for this pager. If not NULL, the destructor is called
drh5e00f6c2001-09-13 13:46:56 +00002279** when the reference count on each page reaches zero. The destructor can
2280** be used to clean up information in the extra segment appended to each page.
drh72f82862001-05-24 21:06:34 +00002281**
danielk19773b8a05f2007-03-19 17:44:26 +00002282** The destructor is not called as a result sqlite3PagerClose().
2283** Destructors are only called by sqlite3PagerUnref().
drh72f82862001-05-24 21:06:34 +00002284*/
danielk19773b8a05f2007-03-19 17:44:26 +00002285void sqlite3PagerSetDestructor(Pager *pPager, void (*xDesc)(DbPage*,int)){
drh72f82862001-05-24 21:06:34 +00002286 pPager->xDestructor = xDesc;
2287}
2288
2289/*
drha6abd042004-06-09 17:37:22 +00002290** Set the reinitializer for this pager. If not NULL, the reinitializer
2291** is called when the content of a page in cache is restored to its original
2292** value as a result of a rollback. The callback gives higher-level code
2293** an opportunity to restore the EXTRA section to agree with the restored
2294** page data.
2295*/
danielk19773b8a05f2007-03-19 17:44:26 +00002296void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*,int)){
drha6abd042004-06-09 17:37:22 +00002297 pPager->xReiniter = xReinit;
2298}
2299
2300/*
danielk1977a1644fd2007-08-29 12:31:25 +00002301** Set the page size to *pPageSize. If the suggest new page size is
2302** inappropriate, then an alternative page size is set to that
2303** value before returning.
drh90f5ecb2004-07-22 01:19:35 +00002304*/
danielk1977a1644fd2007-08-29 12:31:25 +00002305int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){
2306 int rc = SQLITE_OK;
2307 u16 pageSize = *pPageSize;
danielk19779663b8f2007-08-24 11:52:28 +00002308 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
danielk1977a1644fd2007-08-29 12:31:25 +00002309 if( pageSize && pageSize!=pPager->pageSize
2310 && !pPager->memDb && pPager->nRef==0
2311 ){
2312 char *pNew = (char *)sqlite3_malloc(pageSize);
2313 if( !pNew ){
2314 rc = SQLITE_NOMEM;
2315 }else{
2316 pagerEnter(pPager);
2317 pager_reset(pPager);
2318 pPager->pageSize = pageSize;
2319 setSectorSize(pPager);
2320 sqlite3_free(pPager->pTmpSpace);
2321 pPager->pTmpSpace = pNew;
2322 pagerLeave(pPager);
2323 }
drh1c7880e2005-05-20 20:01:55 +00002324 }
danielk1977a1644fd2007-08-29 12:31:25 +00002325 *pPageSize = pPager->pageSize;
2326 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002327}
2328
2329/*
drh26b79942007-11-28 16:19:56 +00002330** Return a pointer to the "temporary page" buffer held internally
2331** by the pager. This is a buffer that is big enough to hold the
2332** entire content of a database page. This buffer is used internally
2333** during rollback and will be overwritten whenever a rollback
2334** occurs. But other modules are free to use it too, as long as
2335** no rollbacks are happening.
2336*/
2337void *sqlite3PagerTempSpace(Pager *pPager){
2338 return pPager->pTmpSpace;
2339}
2340
2341/*
drhf8e632b2007-05-08 14:51:36 +00002342** Attempt to set the maximum database page count if mxPage is positive.
2343** Make no changes if mxPage is zero or negative. And never reduce the
2344** maximum page count below the current size of the database.
2345**
2346** Regardless of mxPage, return the current maximum page count.
2347*/
2348int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
2349 if( mxPage>0 ){
2350 pPager->mxPgno = mxPage;
2351 }
2352 sqlite3PagerPagecount(pPager);
2353 return pPager->mxPgno;
2354}
2355
2356/*
drhc9ac5ca2005-11-04 22:03:30 +00002357** The following set of routines are used to disable the simulated
2358** I/O error mechanism. These routines are used to avoid simulated
2359** errors in places where we do not care about errors.
2360**
2361** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
2362** and generate no code.
2363*/
2364#ifdef SQLITE_TEST
2365extern int sqlite3_io_error_pending;
2366extern int sqlite3_io_error_hit;
2367static int saved_cnt;
drhc9ac5ca2005-11-04 22:03:30 +00002368void disable_simulated_io_errors(void){
2369 saved_cnt = sqlite3_io_error_pending;
2370 sqlite3_io_error_pending = -1;
2371}
2372void enable_simulated_io_errors(void){
2373 sqlite3_io_error_pending = saved_cnt;
2374}
2375#else
drh152410f2005-11-05 15:11:22 +00002376# define disable_simulated_io_errors()
2377# define enable_simulated_io_errors()
drhc9ac5ca2005-11-04 22:03:30 +00002378#endif
2379
2380/*
drh90f5ecb2004-07-22 01:19:35 +00002381** Read the first N bytes from the beginning of the file into memory
danielk1977aef0bf62005-12-30 16:28:01 +00002382** that pDest points to.
2383**
2384** No error checking is done. The rational for this is that this function
2385** may be called even if the file does not exist or contain a header. In
2386** these cases sqlite3OsRead() will return an error, to which the correct
2387** response is to zero the memory at pDest and continue. A real IO error
2388** will presumably recur and be picked up later (Todo: Think about this).
drh90f5ecb2004-07-22 01:19:35 +00002389*/
danielk19773b8a05f2007-03-19 17:44:26 +00002390int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
drh551b7732006-11-06 21:20:25 +00002391 int rc = SQLITE_OK;
drh90f5ecb2004-07-22 01:19:35 +00002392 memset(pDest, 0, N);
danielk19777a2b1ee2007-08-21 14:27:01 +00002393 assert(MEMDB||pPager->fd->pMethods||pPager->tempFile);
2394 if( pPager->fd->pMethods ){
drhb0603412007-02-28 04:47:26 +00002395 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
danielk197762079062007-08-15 17:08:46 +00002396 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
drh551b7732006-11-06 21:20:25 +00002397 if( rc==SQLITE_IOERR_SHORT_READ ){
2398 rc = SQLITE_OK;
2399 }
drh90f5ecb2004-07-22 01:19:35 +00002400 }
drh551b7732006-11-06 21:20:25 +00002401 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002402}
2403
2404/*
drh5e00f6c2001-09-13 13:46:56 +00002405** Return the total number of pages in the disk file associated with
danielk197715f411d2005-09-16 10:18:45 +00002406** pPager.
2407**
2408** If the PENDING_BYTE lies on the page directly after the end of the
2409** file, then consider this page part of the file too. For example, if
2410** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the
2411** file is 4096 bytes, 5 is returned instead of 4.
drhed7c8552001-04-11 14:29:21 +00002412*/
danielk19773b8a05f2007-03-19 17:44:26 +00002413int sqlite3PagerPagecount(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00002414 i64 n = 0;
drhe49f9822006-09-15 12:29:16 +00002415 int rc;
drhd9b02572001-04-15 00:37:09 +00002416 assert( pPager!=0 );
drha7aea3d2007-03-15 12:51:16 +00002417 if( pPager->errCode ){
2418 return 0;
2419 }
drhed7c8552001-04-11 14:29:21 +00002420 if( pPager->dbSize>=0 ){
danielk197715f411d2005-09-16 10:18:45 +00002421 n = pPager->dbSize;
2422 } else {
danielk19777a2b1ee2007-08-21 14:27:01 +00002423 assert(pPager->fd->pMethods||pPager->tempFile);
2424 if( (pPager->fd->pMethods)
2425 && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){
danielk1977ae72d982007-10-03 08:46:44 +00002426 pPager->nRef++;
drhe49f9822006-09-15 12:29:16 +00002427 pager_error(pPager, rc);
danielk1977ae72d982007-10-03 08:46:44 +00002428 pPager->nRef--;
danielk197715f411d2005-09-16 10:18:45 +00002429 return 0;
2430 }
2431 if( n>0 && n<pPager->pageSize ){
2432 n = 1;
2433 }else{
2434 n /= pPager->pageSize;
2435 }
2436 if( pPager->state!=PAGER_UNLOCK ){
2437 pPager->dbSize = n;
2438 }
drhed7c8552001-04-11 14:29:21 +00002439 }
danielk197715f411d2005-09-16 10:18:45 +00002440 if( n==(PENDING_BYTE/pPager->pageSize) ){
drh1f595712004-06-15 01:40:29 +00002441 n++;
2442 }
drhf8e632b2007-05-08 14:51:36 +00002443 if( n>pPager->mxPgno ){
2444 pPager->mxPgno = n;
2445 }
drhed7c8552001-04-11 14:29:21 +00002446 return n;
2447}
2448
drhf07e7d52006-04-07 13:54:46 +00002449
2450#ifndef SQLITE_OMIT_MEMORYDB
2451/*
2452** Clear a PgHistory block
2453*/
2454static void clearHistory(PgHistory *pHist){
drh17435752007-08-16 04:30:38 +00002455 sqlite3_free(pHist->pOrig);
2456 sqlite3_free(pHist->pStmt);
drhf07e7d52006-04-07 13:54:46 +00002457 pHist->pOrig = 0;
2458 pHist->pStmt = 0;
2459}
2460#else
2461#define clearHistory(x)
2462#endif
2463
drhed7c8552001-04-11 14:29:21 +00002464/*
drhf7c57532003-04-25 13:22:51 +00002465** Forward declaration
2466*/
danielk197776572402004-06-25 02:38:54 +00002467static int syncJournal(Pager*);
drhac69b052004-05-12 13:30:07 +00002468
2469/*
drh85b623f2007-12-13 21:54:09 +00002470** Unlink pPg from its hash chain. Also set the page number to 0 to indicate
danielk1977f5fdda82004-11-03 08:44:05 +00002471** that the page is not part of any hash chain. This is required because the
danielk19773b8a05f2007-03-19 17:44:26 +00002472** sqlite3PagerMovepage() routine can leave a page in the
danielk1977f5fdda82004-11-03 08:44:05 +00002473** pNextFree/pPrevFree list that is not a part of any hash-chain.
2474*/
2475static void unlinkHashChain(Pager *pPager, PgHdr *pPg){
2476 if( pPg->pgno==0 ){
drh3765df42006-06-28 18:18:09 +00002477 assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
danielk1977f5fdda82004-11-03 08:44:05 +00002478 return;
2479 }
2480 if( pPg->pNextHash ){
2481 pPg->pNextHash->pPrevHash = pPg->pPrevHash;
2482 }
2483 if( pPg->pPrevHash ){
drh8ca0c722006-05-07 17:49:38 +00002484 assert( pPager->aHash[pPg->pgno & (pPager->nHash-1)]!=pPg );
danielk1977f5fdda82004-11-03 08:44:05 +00002485 pPg->pPrevHash->pNextHash = pPg->pNextHash;
2486 }else{
drh8ca0c722006-05-07 17:49:38 +00002487 int h = pPg->pgno & (pPager->nHash-1);
danielk1977f5fdda82004-11-03 08:44:05 +00002488 pPager->aHash[h] = pPg->pNextHash;
2489 }
drh5229ae42006-03-23 23:29:04 +00002490 if( MEMDB ){
drh5229ae42006-03-23 23:29:04 +00002491 clearHistory(PGHDR_TO_HIST(pPg, pPager));
2492 }
danielk1977f5fdda82004-11-03 08:44:05 +00002493 pPg->pgno = 0;
2494 pPg->pNextHash = pPg->pPrevHash = 0;
2495}
2496
2497/*
drhac69b052004-05-12 13:30:07 +00002498** Unlink a page from the free list (the list of all pages where nRef==0)
2499** and from its hash collision chain.
2500*/
2501static void unlinkPage(PgHdr *pPg){
2502 Pager *pPager = pPg->pPager;
2503
danielk19779f61c2f2007-08-27 17:27:49 +00002504 /* Unlink from free page list */
2505 lruListRemove(pPg);
drhac69b052004-05-12 13:30:07 +00002506
2507 /* Unlink from the pgno hash table */
danielk1977f5fdda82004-11-03 08:44:05 +00002508 unlinkHashChain(pPager, pPg);
drhac69b052004-05-12 13:30:07 +00002509}
2510
2511/*
danielk1977e180dd92007-04-05 17:15:52 +00002512** This routine is used to truncate the cache when a database
2513** is truncated. Drop from the cache all pages whose pgno is
2514** larger than pPager->dbSize and is unreferenced.
2515**
drhac69b052004-05-12 13:30:07 +00002516** Referenced pages larger than pPager->dbSize are zeroed.
danielk1977e180dd92007-04-05 17:15:52 +00002517**
2518** Actually, at the point this routine is called, it would be
2519** an error to have a referenced page. But rather than delete
2520** that page and guarantee a subsequent segfault, it seems better
2521** to zero it and hope that we error out sanely.
drhac69b052004-05-12 13:30:07 +00002522*/
danielk1977e180dd92007-04-05 17:15:52 +00002523static void pager_truncate_cache(Pager *pPager){
drhac69b052004-05-12 13:30:07 +00002524 PgHdr *pPg;
2525 PgHdr **ppPg;
2526 int dbSize = pPager->dbSize;
2527
2528 ppPg = &pPager->pAll;
2529 while( (pPg = *ppPg)!=0 ){
2530 if( pPg->pgno<=dbSize ){
2531 ppPg = &pPg->pNextAll;
2532 }else if( pPg->nRef>0 ){
2533 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
2534 ppPg = &pPg->pNextAll;
2535 }else{
2536 *ppPg = pPg->pNextAll;
drh538f5702007-04-13 02:14:30 +00002537 IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
2538 PAGER_INCR(sqlite3_pager_pgfree_count);
drhac69b052004-05-12 13:30:07 +00002539 unlinkPage(pPg);
drh605ad8f2006-05-03 23:34:05 +00002540 makeClean(pPg);
drh0d180202008-02-14 23:26:56 +00002541 sqlite3_free(pPg->pData);
drh17435752007-08-16 04:30:38 +00002542 sqlite3_free(pPg);
drhac69b052004-05-12 13:30:07 +00002543 pPager->nPage--;
2544 }
2545 }
2546}
2547
drhf7c57532003-04-25 13:22:51 +00002548/*
danielk197717221812005-02-15 03:38:05 +00002549** Try to obtain a lock on a file. Invoke the busy callback if the lock
drha4afb652005-07-09 02:16:02 +00002550** is currently not available. Repeat until the busy callback returns
danielk197717221812005-02-15 03:38:05 +00002551** false or until the lock succeeds.
2552**
2553** Return SQLITE_OK on success and an error code if we cannot obtain
2554** the lock.
2555*/
2556static int pager_wait_on_lock(Pager *pPager, int locktype){
2557 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002558
2559 /* The OS lock values must be the same as the Pager lock values */
danielk197717221812005-02-15 03:38:05 +00002560 assert( PAGER_SHARED==SHARED_LOCK );
2561 assert( PAGER_RESERVED==RESERVED_LOCK );
2562 assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
drh1aa2d8b2007-01-03 15:34:29 +00002563
2564 /* If the file is currently unlocked then the size must be unknown */
2565 assert( pPager->state>=PAGER_SHARED || pPager->dbSize<0 || MEMDB );
2566
danielk197717221812005-02-15 03:38:05 +00002567 if( pPager->state>=locktype ){
2568 rc = SQLITE_OK;
2569 }else{
danielk197717221812005-02-15 03:38:05 +00002570 do {
drh054889e2005-11-30 03:20:31 +00002571 rc = sqlite3OsLock(pPager->fd, locktype);
drha4afb652005-07-09 02:16:02 +00002572 }while( rc==SQLITE_BUSY && sqlite3InvokeBusyHandler(pPager->pBusyHandler) );
danielk197717221812005-02-15 03:38:05 +00002573 if( rc==SQLITE_OK ){
2574 pPager->state = locktype;
drhb0603412007-02-28 04:47:26 +00002575 IOTRACE(("LOCK %p %d\n", pPager, locktype))
danielk197717221812005-02-15 03:38:05 +00002576 }
2577 }
2578 return rc;
2579}
2580
2581/*
drhf7c57532003-04-25 13:22:51 +00002582** Truncate the file to the number of pages specified.
2583*/
danielk19773b8a05f2007-03-19 17:44:26 +00002584int sqlite3PagerTruncate(Pager *pPager, Pgno nPage){
drhf7c57532003-04-25 13:22:51 +00002585 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002586 assert( pPager->state>=PAGER_SHARED || MEMDB );
danielk19773b8a05f2007-03-19 17:44:26 +00002587 sqlite3PagerPagecount(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00002588 if( pPager->errCode ){
2589 rc = pPager->errCode;
drh2e6d11b2003-04-25 15:37:57 +00002590 return rc;
2591 }
drh7d02cb72003-06-04 16:24:39 +00002592 if( nPage>=(unsigned)pPager->dbSize ){
drhf7c57532003-04-25 13:22:51 +00002593 return SQLITE_OK;
2594 }
drhb7f91642004-10-31 02:22:47 +00002595 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00002596 pPager->dbSize = nPage;
danielk1977e180dd92007-04-05 17:15:52 +00002597 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00002598 return SQLITE_OK;
2599 }
drh86f8c192007-08-22 00:39:19 +00002600 pagerEnter(pPager);
danielk197776572402004-06-25 02:38:54 +00002601 rc = syncJournal(pPager);
drh86f8c192007-08-22 00:39:19 +00002602 pagerLeave(pPager);
danielk1977369f27e2004-06-15 11:40:04 +00002603 if( rc!=SQLITE_OK ){
2604 return rc;
2605 }
danielk197717221812005-02-15 03:38:05 +00002606
2607 /* Get an exclusive lock on the database before truncating. */
drh86f8c192007-08-22 00:39:19 +00002608 pagerEnter(pPager);
danielk197717221812005-02-15 03:38:05 +00002609 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
drh86f8c192007-08-22 00:39:19 +00002610 pagerLeave(pPager);
danielk197717221812005-02-15 03:38:05 +00002611 if( rc!=SQLITE_OK ){
2612 return rc;
2613 }
2614
drhcb4c40b2004-08-18 19:09:43 +00002615 rc = pager_truncate(pPager, nPage);
drhf7c57532003-04-25 13:22:51 +00002616 return rc;
2617}
2618
2619/*
drhed7c8552001-04-11 14:29:21 +00002620** Shutdown the page cache. Free all memory and close all files.
2621**
2622** If a transaction was in progress when this routine is called, that
2623** transaction is rolled back. All outstanding pages are invalidated
2624** and their memory is freed. Any attempt to use a page associated
2625** with this page cache after this function returns will likely
2626** result in a coredump.
danielk1977aef0bf62005-12-30 16:28:01 +00002627**
2628** This function always succeeds. If a transaction is active an attempt
2629** is made to roll it back. If an error occurs during the rollback
2630** a hot journal may be left in the filesystem but no error is returned
2631** to the caller.
drhed7c8552001-04-11 14:29:21 +00002632*/
danielk19773b8a05f2007-03-19 17:44:26 +00002633int sqlite3PagerClose(Pager *pPager){
drh6f7adc82006-01-11 21:41:20 +00002634#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +00002635 if( !MEMDB ){
2636 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
2637 sqlite3_mutex_enter(mutex);
2638 if( pPager->pPrev ){
2639 pPager->pPrev->pNext = pPager->pNext;
2640 }else{
2641 sqlite3PagerList = pPager->pNext;
2642 }
2643 if( pPager->pNext ){
2644 pPager->pNext->pPrev = pPager->pPrev;
2645 }
2646 sqlite3_mutex_leave(mutex);
2647 }
danielk197713f72992005-12-18 08:51:22 +00002648#endif
2649
drhbafda092007-01-03 23:36:22 +00002650 disable_simulated_io_errors();
drhc2ee76c2007-01-04 14:58:14 +00002651 pPager->errCode = 0;
danielk197741483462007-03-24 16:45:04 +00002652 pPager->exclusiveMode = 0;
drhbafda092007-01-03 23:36:22 +00002653 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00002654 pagerUnlockAndRollback(pPager);
drhbafda092007-01-03 23:36:22 +00002655 enable_simulated_io_errors();
drh4f0c5872007-03-26 22:05:01 +00002656 PAGERTRACE2("CLOSE %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002657 IOTRACE(("CLOSE %p\n", pPager))
danielk1977efaaf572006-01-16 11:29:19 +00002658 assert( pPager->errCode || (pPager->journalOpen==0 && pPager->stmtOpen==0) );
danielk1977e94ddc92005-03-21 03:53:38 +00002659 if( pPager->journalOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002660 sqlite3OsClose(pPager->jfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002661 }
drhf5e7bb52008-02-18 14:47:33 +00002662 sqlite3BitvecDestroy(pPager->pInJournal);
danielk1977e94ddc92005-03-21 03:53:38 +00002663 if( pPager->stmtOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002664 sqlite3OsClose(pPager->stfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002665 }
danielk1977b4b47412007-08-17 15:53:36 +00002666 sqlite3OsClose(pPager->fd);
drh0f892532002-05-30 12:27:03 +00002667 /* Temp files are automatically deleted by the OS
2668 ** if( pPager->tempFile ){
drh66560ad2006-01-06 14:32:19 +00002669 ** sqlite3OsDelete(pPager->zFilename);
drh0f892532002-05-30 12:27:03 +00002670 ** }
2671 */
danielk1977aca790a2005-01-13 11:07:52 +00002672
drh17435752007-08-16 04:30:38 +00002673 sqlite3_free(pPager->aHash);
2674 sqlite3_free(pPager->pTmpSpace);
2675 sqlite3_free(pPager);
drhed7c8552001-04-11 14:29:21 +00002676 return SQLITE_OK;
2677}
2678
drh87cc3b32007-05-08 21:45:27 +00002679#if !defined(NDEBUG) || defined(SQLITE_TEST)
drhed7c8552001-04-11 14:29:21 +00002680/*
drh5e00f6c2001-09-13 13:46:56 +00002681** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +00002682*/
danielk19773b8a05f2007-03-19 17:44:26 +00002683Pgno sqlite3PagerPagenumber(DbPage *p){
drhed7c8552001-04-11 14:29:21 +00002684 return p->pgno;
2685}
drh87cc3b32007-05-08 21:45:27 +00002686#endif
drhed7c8552001-04-11 14:29:21 +00002687
2688/*
drhc8629a12004-05-08 20:07:40 +00002689** The page_ref() function increments the reference count for a page.
2690** If the page is currently on the freelist (the reference count is zero) then
drh7e3b0a02001-04-28 16:52:40 +00002691** remove it from the freelist.
drhc8629a12004-05-08 20:07:40 +00002692**
2693** For non-test systems, page_ref() is a macro that calls _page_ref()
2694** online of the reference count is zero. For test systems, page_ref()
2695** is a real function so that we can set breakpoints and trace it.
drh7e3b0a02001-04-28 16:52:40 +00002696*/
drh836faa42003-01-11 13:30:57 +00002697static void _page_ref(PgHdr *pPg){
drh7e3b0a02001-04-28 16:52:40 +00002698 if( pPg->nRef==0 ){
2699 /* The page is currently on the freelist. Remove it. */
danielk19779f61c2f2007-08-27 17:27:49 +00002700 lruListRemove(pPg);
drh7e3b0a02001-04-28 16:52:40 +00002701 pPg->pPager->nRef++;
2702 }
2703 pPg->nRef++;
drhdf0b3b02001-06-23 11:36:20 +00002704}
danielk1977aca790a2005-01-13 11:07:52 +00002705#ifdef SQLITE_DEBUG
drhc8629a12004-05-08 20:07:40 +00002706 static void page_ref(PgHdr *pPg){
2707 if( pPg->nRef==0 ){
2708 _page_ref(pPg);
2709 }else{
2710 pPg->nRef++;
drhc8629a12004-05-08 20:07:40 +00002711 }
2712 }
2713#else
2714# define page_ref(P) ((P)->nRef==0?_page_ref(P):(void)(P)->nRef++)
2715#endif
drhdf0b3b02001-06-23 11:36:20 +00002716
2717/*
2718** Increment the reference count for a page. The input pointer is
2719** a reference to the page data.
2720*/
danielk19773b8a05f2007-03-19 17:44:26 +00002721int sqlite3PagerRef(DbPage *pPg){
drh86f8c192007-08-22 00:39:19 +00002722 pagerEnter(pPg->pPager);
drhdf0b3b02001-06-23 11:36:20 +00002723 page_ref(pPg);
drh86f8c192007-08-22 00:39:19 +00002724 pagerLeave(pPg->pPager);
drh8c42ca92001-06-22 19:15:00 +00002725 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00002726}
2727
2728/*
drh34e79ce2004-02-08 06:05:46 +00002729** Sync the journal. In other words, make sure all the pages that have
2730** been written to the journal have actually reached the surface of the
2731** disk. It is not safe to modify the original database file until after
2732** the journal has been synced. If the original database is modified before
2733** the journal is synced and a power failure occurs, the unsynced journal
2734** data would be lost and we would be unable to completely rollback the
2735** database changes. Database corruption would occur.
2736**
2737** This routine also updates the nRec field in the header of the journal.
2738** (See comments on the pager_playback() routine for additional information.)
2739** If the sync mode is FULL, two syncs will occur. First the whole journal
2740** is synced, then the nRec field is updated, then a second sync occurs.
drhb19a2bc2001-09-16 00:13:26 +00002741**
drh34e79ce2004-02-08 06:05:46 +00002742** For temporary databases, we do not care if we are able to rollback
danielk19774cd2cd52007-08-23 14:48:23 +00002743** after a power failure, so no sync occurs.
2744**
2745** If the IOCAP_SEQUENTIAL flag is set for the persistent media on which
2746** the database is stored, then OsSync() is never called on the journal
2747** file. In this case all that is required is to update the nRec field in
2748** the journal header.
drhfa86c412002-02-02 15:01:15 +00002749**
drh34e79ce2004-02-08 06:05:46 +00002750** This routine clears the needSync field of every page current held in
2751** memory.
drh50e5dad2001-09-15 00:57:28 +00002752*/
danielk197776572402004-06-25 02:38:54 +00002753static int syncJournal(Pager *pPager){
drh50e5dad2001-09-15 00:57:28 +00002754 PgHdr *pPg;
2755 int rc = SQLITE_OK;
drh03eb96a2002-11-10 23:32:56 +00002756
danielk19774cd2cd52007-08-23 14:48:23 +00002757
drh03eb96a2002-11-10 23:32:56 +00002758 /* Sync the journal before modifying the main database
2759 ** (assuming there is a journal and it needs to be synced.)
2760 */
danielk197776572402004-06-25 02:38:54 +00002761 if( pPager->needSync ){
drhfa86c412002-02-02 15:01:15 +00002762 if( !pPager->tempFile ){
danielk19774cd2cd52007-08-23 14:48:23 +00002763 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
drhdb48ee02003-01-16 13:42:43 +00002764 assert( pPager->journalOpen );
danielk19774cd2cd52007-08-23 14:48:23 +00002765
drh946966f2004-02-25 02:20:41 +00002766 /* assert( !pPager->noSync ); // noSync might be set if synchronous
2767 ** was turned off after the transaction was started. Ticket #615 */
drh968af522003-02-11 14:55:40 +00002768#ifndef NDEBUG
2769 {
drh34e79ce2004-02-08 06:05:46 +00002770 /* Make sure the pPager->nRec counter we are keeping agrees
2771 ** with the nRec computed from the size of the journal file.
2772 */
drheb206252004-10-01 02:00:31 +00002773 i64 jSz;
drh054889e2005-11-30 03:20:31 +00002774 rc = sqlite3OsFileSize(pPager->jfd, &jSz);
drh968af522003-02-11 14:55:40 +00002775 if( rc!=0 ) return rc;
danielk197776572402004-06-25 02:38:54 +00002776 assert( pPager->journalOff==jSz );
drh968af522003-02-11 14:55:40 +00002777 }
2778#endif
danielk19774cd2cd52007-08-23 14:48:23 +00002779 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
danielk197776572402004-06-25 02:38:54 +00002780 /* Write the nRec value into the journal file header. If in
2781 ** full-synchronous mode, sync the journal first. This ensures that
2782 ** all data has really hit the disk before nRec is updated to mark
danielk19774cd2cd52007-08-23 14:48:23 +00002783 ** it as a candidate for rollback.
2784 **
2785 ** This is not required if the persistent media supports the
2786 ** SAFE_APPEND property. Because in this case it is not possible
2787 ** for garbage data to be appended to the file, the nRec field
2788 ** is populated with 0xFFFFFFFF when the journal header is written
2789 ** and never needs to be updated.
danielk197776572402004-06-25 02:38:54 +00002790 */
danielk19774cd2cd52007-08-23 14:48:23 +00002791 i64 jrnlOff;
2792 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
drh4f0c5872007-03-26 22:05:01 +00002793 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002794 IOTRACE(("JSYNC %p\n", pPager))
danielk1977f036aef2007-08-20 05:36:51 +00002795 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags);
drhd8d66e82003-02-12 02:10:15 +00002796 if( rc!=0 ) return rc;
2797 }
danielk197713adf8a2004-06-03 16:08:41 +00002798
danielk197762079062007-08-15 17:08:46 +00002799 jrnlOff = pPager->journalHdr + sizeof(aJournalMagic);
2800 IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4));
2801 rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec);
drhb4746b92005-09-09 01:32:06 +00002802 if( rc ) return rc;
drh968af522003-02-11 14:55:40 +00002803 }
danielk19774cd2cd52007-08-23 14:48:23 +00002804 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
2805 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
2806 IOTRACE(("JSYNC %p\n", pPager))
2807 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags|
2808 (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
2809 );
2810 if( rc!=0 ) return rc;
2811 }
drhdb48ee02003-01-16 13:42:43 +00002812 pPager->journalStarted = 1;
drhfa86c412002-02-02 15:01:15 +00002813 }
drh50e5dad2001-09-15 00:57:28 +00002814 pPager->needSync = 0;
drh341eae82003-01-21 02:39:36 +00002815
2816 /* Erase the needSync flag from every page.
2817 */
2818 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2819 pPg->needSync = 0;
2820 }
danielk19779f61c2f2007-08-27 17:27:49 +00002821 lruListSetFirstSynced(pPager);
drh50e5dad2001-09-15 00:57:28 +00002822 }
drh03eb96a2002-11-10 23:32:56 +00002823
drh341eae82003-01-21 02:39:36 +00002824#ifndef NDEBUG
2825 /* If the Pager.needSync flag is clear then the PgHdr.needSync
2826 ** flag must also be clear for all pages. Verify that this
2827 ** invariant is true.
drh03eb96a2002-11-10 23:32:56 +00002828 */
drh341eae82003-01-21 02:39:36 +00002829 else{
2830 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2831 assert( pPg->needSync==0 );
2832 }
danielk19779f61c2f2007-08-27 17:27:49 +00002833 assert( pPager->lru.pFirstSynced==pPager->lru.pFirst );
drh03eb96a2002-11-10 23:32:56 +00002834 }
drh341eae82003-01-21 02:39:36 +00002835#endif
drhdb48ee02003-01-16 13:42:43 +00002836
drh81a20f22001-10-12 17:30:04 +00002837 return rc;
drh50e5dad2001-09-15 00:57:28 +00002838}
2839
2840/*
drhdc3e10b2006-06-15 14:31:06 +00002841** Merge two lists of pages connected by pDirty and in pgno order.
2842** Do not both fixing the pPrevDirty pointers.
2843*/
2844static PgHdr *merge_pagelist(PgHdr *pA, PgHdr *pB){
2845 PgHdr result, *pTail;
2846 pTail = &result;
2847 while( pA && pB ){
2848 if( pA->pgno<pB->pgno ){
2849 pTail->pDirty = pA;
2850 pTail = pA;
2851 pA = pA->pDirty;
2852 }else{
2853 pTail->pDirty = pB;
2854 pTail = pB;
2855 pB = pB->pDirty;
2856 }
2857 }
2858 if( pA ){
2859 pTail->pDirty = pA;
2860 }else if( pB ){
2861 pTail->pDirty = pB;
2862 }else{
2863 pTail->pDirty = 0;
2864 }
2865 return result.pDirty;
2866}
2867
2868/*
2869** Sort the list of pages in accending order by pgno. Pages are
2870** connected by pDirty pointers. The pPrevDirty pointers are
2871** corrupted by this sort.
2872*/
danielk197724168722007-04-02 05:07:47 +00002873#define N_SORT_BUCKET_ALLOC 25
2874#define N_SORT_BUCKET 25
2875#ifdef SQLITE_TEST
2876 int sqlite3_pager_n_sort_bucket = 0;
2877 #undef N_SORT_BUCKET
2878 #define N_SORT_BUCKET \
2879 (sqlite3_pager_n_sort_bucket?sqlite3_pager_n_sort_bucket:N_SORT_BUCKET_ALLOC)
2880#endif
drhdc3e10b2006-06-15 14:31:06 +00002881static PgHdr *sort_pagelist(PgHdr *pIn){
danielk197724168722007-04-02 05:07:47 +00002882 PgHdr *a[N_SORT_BUCKET_ALLOC], *p;
drhdc3e10b2006-06-15 14:31:06 +00002883 int i;
2884 memset(a, 0, sizeof(a));
2885 while( pIn ){
2886 p = pIn;
2887 pIn = p->pDirty;
2888 p->pDirty = 0;
2889 for(i=0; i<N_SORT_BUCKET-1; i++){
2890 if( a[i]==0 ){
2891 a[i] = p;
2892 break;
2893 }else{
2894 p = merge_pagelist(a[i], p);
2895 a[i] = 0;
2896 }
2897 }
2898 if( i==N_SORT_BUCKET-1 ){
danielk197724168722007-04-02 05:07:47 +00002899 /* Coverage: To get here, there need to be 2^(N_SORT_BUCKET)
2900 ** elements in the input list. This is possible, but impractical.
2901 ** Testing this line is the point of global variable
2902 ** sqlite3_pager_n_sort_bucket.
2903 */
drhdc3e10b2006-06-15 14:31:06 +00002904 a[i] = merge_pagelist(a[i], p);
2905 }
2906 }
2907 p = a[0];
2908 for(i=1; i<N_SORT_BUCKET; i++){
2909 p = merge_pagelist(p, a[i]);
2910 }
2911 return p;
2912}
2913
2914/*
drh2554f8b2003-01-22 01:26:44 +00002915** Given a list of pages (connected by the PgHdr.pDirty pointer) write
2916** every one of those pages out to the database file and mark them all
2917** as clean.
2918*/
2919static int pager_write_pagelist(PgHdr *pList){
2920 Pager *pPager;
drh153c62c2007-08-24 03:51:33 +00002921 PgHdr *p;
drh2554f8b2003-01-22 01:26:44 +00002922 int rc;
2923
2924 if( pList==0 ) return SQLITE_OK;
2925 pPager = pList->pPager;
danielk19779eed5052004-06-04 10:38:30 +00002926
2927 /* At this point there may be either a RESERVED or EXCLUSIVE lock on the
2928 ** database file. If there is already an EXCLUSIVE lock, the following
drh054889e2005-11-30 03:20:31 +00002929 ** calls to sqlite3OsLock() are no-ops.
danielk19779eed5052004-06-04 10:38:30 +00002930 **
drha6abd042004-06-09 17:37:22 +00002931 ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
2932 ** through an intermediate state PENDING. A PENDING lock prevents new
2933 ** readers from attaching to the database but is unsufficient for us to
2934 ** write. The idea of a PENDING lock is to prevent new readers from
2935 ** coming in while we wait for existing readers to clear.
danielk19779eed5052004-06-04 10:38:30 +00002936 **
drha6abd042004-06-09 17:37:22 +00002937 ** While the pager is in the RESERVED state, the original database file
2938 ** is unchanged and we can rollback without having to playback the
2939 ** journal into the original database file. Once we transition to
2940 ** EXCLUSIVE, it means the database file has been changed and any rollback
2941 ** will require a journal playback.
danielk19779eed5052004-06-04 10:38:30 +00002942 */
drh684917c2004-10-05 02:41:42 +00002943 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
danielk19779eed5052004-06-04 10:38:30 +00002944 if( rc!=SQLITE_OK ){
2945 return rc;
2946 }
2947
drhdc3e10b2006-06-15 14:31:06 +00002948 pList = sort_pagelist(pList);
drh153c62c2007-08-24 03:51:33 +00002949 for(p=pList; p; p=p->pDirty){
2950 assert( p->dirty );
2951 p->dirty = 0;
2952 }
drh2554f8b2003-01-22 01:26:44 +00002953 while( pList ){
danielk19777a2b1ee2007-08-21 14:27:01 +00002954
2955 /* If the file has not yet been opened, open it now. */
2956 if( !pPager->fd->pMethods ){
2957 assert(pPager->tempFile);
drh33f4e022007-09-03 15:19:34 +00002958 rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->fd, pPager->zFilename,
2959 pPager->vfsFlags);
danielk19777a2b1ee2007-08-21 14:27:01 +00002960 if( rc ) return rc;
2961 }
2962
danielk1977687566d2004-11-02 12:56:41 +00002963 /* If there are dirty pages in the page cache with page numbers greater
danielk19773b8a05f2007-03-19 17:44:26 +00002964 ** than Pager.dbSize, this means sqlite3PagerTruncate() was called to
danielk1977687566d2004-11-02 12:56:41 +00002965 ** make the file smaller (presumably by auto-vacuum code). Do not write
2966 ** any such pages to the file.
2967 */
2968 if( pList->pgno<=pPager->dbSize ){
danielk197762079062007-08-15 17:08:46 +00002969 i64 offset = (pList->pgno-1)*(i64)pPager->pageSize;
drhc001c582006-03-06 18:23:16 +00002970 char *pData = CODEC2(pPager, PGHDR_TO_DATA(pList), pList->pgno, 6);
drh477731b2007-06-16 03:06:27 +00002971 PAGERTRACE4("STORE %d page %d hash(%08x)\n",
2972 PAGERID(pPager), pList->pgno, pager_pagehash(pList));
drh538f5702007-04-13 02:14:30 +00002973 IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno));
danielk197762079062007-08-15 17:08:46 +00002974 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00002975 PAGER_INCR(sqlite3_pager_writedb_count);
2976 PAGER_INCR(pPager->nWrite);
drh86a88112007-04-16 15:02:19 +00002977 if( pList->pgno==1 ){
2978 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
2979 }
danielk1977687566d2004-11-02 12:56:41 +00002980 }
2981#ifndef NDEBUG
2982 else{
drh4f0c5872007-03-26 22:05:01 +00002983 PAGERTRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno);
danielk1977687566d2004-11-02 12:56:41 +00002984 }
2985#endif
drh2554f8b2003-01-22 01:26:44 +00002986 if( rc ) return rc;
danielk19773c407372005-02-15 02:54:14 +00002987#ifdef SQLITE_CHECK_PAGES
2988 pList->pageHash = pager_pagehash(pList);
2989#endif
drh2554f8b2003-01-22 01:26:44 +00002990 pList = pList->pDirty;
2991 }
2992 return SQLITE_OK;
2993}
2994
2995/*
2996** Collect every dirty page into a dirty list and
2997** return a pointer to the head of that list. All pages are
2998** collected even if they are still in use.
2999*/
3000static PgHdr *pager_get_all_dirty_pages(Pager *pPager){
drh605ad8f2006-05-03 23:34:05 +00003001 return pPager->pDirty;
drh2554f8b2003-01-22 01:26:44 +00003002}
3003
3004/*
drh165ffe92005-03-15 17:09:30 +00003005** Return TRUE if there is a hot journal on the given pager.
3006** A hot journal is one that needs to be played back.
3007**
3008** If the current size of the database file is 0 but a journal file
3009** exists, that is probably an old journal left over from a prior
3010** database with the same name. Just delete the journal.
3011*/
3012static int hasHotJournal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00003013 sqlite3_vfs *pVfs = pPager->pVfs;
drh165ffe92005-03-15 17:09:30 +00003014 if( !pPager->useJournal ) return 0;
drhd3cf2472007-11-27 16:55:07 +00003015 if( !pPager->fd->pMethods ) return 0;
danielk1977b4b47412007-08-17 15:53:36 +00003016 if( !sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS) ){
drhbb5f18d2007-04-06 18:23:17 +00003017 return 0;
3018 }
3019 if( sqlite3OsCheckReservedLock(pPager->fd) ){
3020 return 0;
3021 }
danielk19773b8a05f2007-03-19 17:44:26 +00003022 if( sqlite3PagerPagecount(pPager)==0 ){
danielk1977fee2d252007-08-18 10:59:19 +00003023 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
drh165ffe92005-03-15 17:09:30 +00003024 return 0;
3025 }else{
3026 return 1;
3027 }
3028}
3029
danielk19775591df52005-12-20 09:19:37 +00003030/*
danielk1977aef0bf62005-12-30 16:28:01 +00003031** Try to find a page in the cache that can be recycled.
3032**
3033** This routine may return SQLITE_IOERR, SQLITE_FULL or SQLITE_OK. It
danielk1977efaaf572006-01-16 11:29:19 +00003034** does not set the pPager->errCode variable.
danielk19775591df52005-12-20 09:19:37 +00003035*/
danielk1977843e65f2007-09-01 16:16:15 +00003036static int pager_recycle(Pager *pPager, PgHdr **ppPg){
danielk197713f72992005-12-18 08:51:22 +00003037 PgHdr *pPg;
3038 *ppPg = 0;
3039
danielk1977843e65f2007-09-01 16:16:15 +00003040 /* It is illegal to call this function unless the pager object
3041 ** pointed to by pPager has at least one free page (page with nRef==0).
3042 */
danielk1977c551edc2007-04-05 13:12:13 +00003043 assert(!MEMDB);
danielk1977843e65f2007-09-01 16:16:15 +00003044 assert(pPager->lru.pFirst);
danielk1977c551edc2007-04-05 13:12:13 +00003045
danielk197713f72992005-12-18 08:51:22 +00003046 /* Find a page to recycle. Try to locate a page that does not
3047 ** require us to do an fsync() on the journal.
3048 */
danielk19779f61c2f2007-08-27 17:27:49 +00003049 pPg = pPager->lru.pFirstSynced;
danielk197713f72992005-12-18 08:51:22 +00003050
3051 /* If we could not find a page that does not require an fsync()
3052 ** on the journal file then fsync the journal file. This is a
3053 ** very slow operation, so we work hard to avoid it. But sometimes
3054 ** it can't be helped.
3055 */
danielk1977843e65f2007-09-01 16:16:15 +00003056 if( pPg==0 && pPager->lru.pFirst){
danielk19774cd2cd52007-08-23 14:48:23 +00003057 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
danielk197713f72992005-12-18 08:51:22 +00003058 int rc = syncJournal(pPager);
3059 if( rc!=0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00003060 return rc;
danielk197713f72992005-12-18 08:51:22 +00003061 }
danielk19774cd2cd52007-08-23 14:48:23 +00003062 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
danielk197713f72992005-12-18 08:51:22 +00003063 /* If in full-sync mode, write a new journal header into the
3064 ** journal file. This is done to avoid ever modifying a journal
3065 ** header that is involved in the rollback of pages that have
3066 ** already been written to the database (in case the header is
3067 ** trashed when the nRec field is updated).
3068 */
3069 pPager->nRec = 0;
3070 assert( pPager->journalOff > 0 );
danielk19774099f6e2007-03-19 11:25:20 +00003071 assert( pPager->doNotSync==0 );
danielk197713f72992005-12-18 08:51:22 +00003072 rc = writeJournalHdr(pPager);
3073 if( rc!=0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00003074 return rc;
danielk197713f72992005-12-18 08:51:22 +00003075 }
3076 }
danielk19779f61c2f2007-08-27 17:27:49 +00003077 pPg = pPager->lru.pFirst;
danielk197713f72992005-12-18 08:51:22 +00003078 }
danielk197713f72992005-12-18 08:51:22 +00003079
3080 assert( pPg->nRef==0 );
3081
3082 /* Write the page to the database file if it is dirty.
3083 */
3084 if( pPg->dirty ){
3085 int rc;
3086 assert( pPg->needSync==0 );
drh605ad8f2006-05-03 23:34:05 +00003087 makeClean(pPg);
3088 pPg->dirty = 1;
danielk197713f72992005-12-18 08:51:22 +00003089 pPg->pDirty = 0;
3090 rc = pager_write_pagelist( pPg );
drh153c62c2007-08-24 03:51:33 +00003091 pPg->dirty = 0;
danielk197713f72992005-12-18 08:51:22 +00003092 if( rc!=SQLITE_OK ){
danielk1977aef0bf62005-12-30 16:28:01 +00003093 return rc;
danielk197713f72992005-12-18 08:51:22 +00003094 }
3095 }
3096 assert( pPg->dirty==0 );
3097
3098 /* If the page we are recycling is marked as alwaysRollback, then
3099 ** set the global alwaysRollback flag, thus disabling the
drh80e35f42007-03-30 14:06:34 +00003100 ** sqlite3PagerDontRollback() optimization for the rest of this transaction.
danielk197713f72992005-12-18 08:51:22 +00003101 ** It is necessary to do this because the page marked alwaysRollback
3102 ** might be reloaded at a later time but at that point we won't remember
3103 ** that is was marked alwaysRollback. This means that all pages must
3104 ** be marked as alwaysRollback from here on out.
3105 */
3106 if( pPg->alwaysRollback ){
drh602c2372007-03-01 00:29:13 +00003107 IOTRACE(("ALWAYS_ROLLBACK %p\n", pPager))
danielk197713f72992005-12-18 08:51:22 +00003108 pPager->alwaysRollback = 1;
3109 }
3110
3111 /* Unlink the old page from the free list and the hash table
3112 */
3113 unlinkPage(pPg);
drh7c4ac0c2007-04-05 11:25:58 +00003114 assert( pPg->pgno==0 );
danielk197713f72992005-12-18 08:51:22 +00003115
3116 *ppPg = pPg;
3117 return SQLITE_OK;
3118}
3119
drh86f8c192007-08-22 00:39:19 +00003120#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk197713f72992005-12-18 08:51:22 +00003121/*
3122** This function is called to free superfluous dynamically allocated memory
3123** held by the pager system. Memory in use by any SQLite pager allocated
drh17435752007-08-16 04:30:38 +00003124** by the current thread may be sqlite3_free()ed.
danielk197713f72992005-12-18 08:51:22 +00003125**
3126** nReq is the number of bytes of memory required. Once this much has
drh86f8c192007-08-22 00:39:19 +00003127** been released, the function returns. The return value is the total number
danielk197713f72992005-12-18 08:51:22 +00003128** of bytes of memory released.
3129*/
danielk19773b8a05f2007-03-19 17:44:26 +00003130int sqlite3PagerReleaseMemory(int nReq){
drh86f8c192007-08-22 00:39:19 +00003131 int nReleased = 0; /* Bytes of memory released so far */
3132 sqlite3_mutex *mutex; /* The MEM2 mutex */
3133 Pager *pPager; /* For looping over pagers */
drhe5fe6902007-12-07 18:55:28 +00003134 BusyHandler *savedBusy; /* Saved copy of the busy handler */
danielk19779f61c2f2007-08-27 17:27:49 +00003135 int rc = SQLITE_OK;
danielk197713f72992005-12-18 08:51:22 +00003136
drh86f8c192007-08-22 00:39:19 +00003137 /* Acquire the memory-management mutex
danielk1977441b09a2006-01-05 13:48:29 +00003138 */
drh86f8c192007-08-22 00:39:19 +00003139 mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
3140 sqlite3_mutex_enter(mutex);
3141
3142 /* Signal all database connections that memory management wants
3143 ** to have access to the pagers.
3144 */
3145 for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
3146 pPager->iInUseMM = 1;
danielk1977441b09a2006-01-05 13:48:29 +00003147 }
3148
danielk19779f61c2f2007-08-27 17:27:49 +00003149 while( rc==SQLITE_OK && (nReq<0 || nReleased<nReq) ){
3150 PgHdr *pPg;
3151 PgHdr *pRecycled;
3152
3153 /* Try to find a page to recycle that does not require a sync(). If
3154 ** this is not possible, find one that does require a sync().
3155 */
3156 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
3157 pPg = sqlite3LruPageList.pFirstSynced;
3158 while( pPg && (pPg->needSync || pPg->pPager->iInUseDB) ){
3159 pPg = pPg->gfree.pNext;
3160 }
3161 if( !pPg ){
3162 pPg = sqlite3LruPageList.pFirst;
3163 while( pPg && pPg->pPager->iInUseDB ){
3164 pPg = pPg->gfree.pNext;
3165 }
3166 }
3167 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
danielk197713f72992005-12-18 08:51:22 +00003168
danielk197784f786f2007-08-28 08:00:17 +00003169 /* If pPg==0, then the block above has failed to find a page to
3170 ** recycle. In this case return early - no further memory will
3171 ** be released.
3172 */
danielk19779f61c2f2007-08-27 17:27:49 +00003173 if( !pPg ) break;
danielk197713f72992005-12-18 08:51:22 +00003174
danielk19779f61c2f2007-08-27 17:27:49 +00003175 pPager = pPg->pPager;
3176 assert(!pPg->needSync || pPg==pPager->lru.pFirst);
3177 assert(pPg->needSync || pPg==pPager->lru.pFirstSynced);
3178
drhe5fe6902007-12-07 18:55:28 +00003179 savedBusy = pPager->pBusyHandler;
3180 pPager->pBusyHandler = 0;
danielk1977843e65f2007-09-01 16:16:15 +00003181 rc = pager_recycle(pPager, &pRecycled);
drhe5fe6902007-12-07 18:55:28 +00003182 pPager->pBusyHandler = savedBusy;
danielk19779f61c2f2007-08-27 17:27:49 +00003183 assert(pRecycled==pPg || rc!=SQLITE_OK);
3184 if( rc==SQLITE_OK ){
3185 /* We've found a page to free. At this point the page has been
3186 ** removed from the page hash-table, free-list and synced-list
3187 ** (pFirstSynced). It is still in the all pages (pAll) list.
3188 ** Remove it from this list before freeing.
3189 **
3190 ** Todo: Check the Pager.pStmt list to make sure this is Ok. It
3191 ** probably is though.
danielk197713f72992005-12-18 08:51:22 +00003192 */
danielk19779f61c2f2007-08-27 17:27:49 +00003193 PgHdr *pTmp;
3194 assert( pPg );
3195 if( pPg==pPager->pAll ){
3196 pPager->pAll = pPg->pNextAll;
3197 }else{
3198 for( pTmp=pPager->pAll; pTmp->pNextAll!=pPg; pTmp=pTmp->pNextAll ){}
3199 pTmp->pNextAll = pPg->pNextAll;
danielk197713f72992005-12-18 08:51:22 +00003200 }
danielk19779f61c2f2007-08-27 17:27:49 +00003201 nReleased += (
3202 sizeof(*pPg) + pPager->pageSize
3203 + sizeof(u32) + pPager->nExtra
3204 + MEMDB*sizeof(PgHistory)
3205 );
3206 IOTRACE(("PGFREE %p %d *\n", pPager, pPg->pgno));
3207 PAGER_INCR(sqlite3_pager_pgfree_count);
drh0d180202008-02-14 23:26:56 +00003208 sqlite3_free(pPg->pData);
danielk19779f61c2f2007-08-27 17:27:49 +00003209 sqlite3_free(pPg);
danielk197784f786f2007-08-28 08:00:17 +00003210 pPager->nPage--;
danielk19779f61c2f2007-08-27 17:27:49 +00003211 }else{
3212 /* An error occured whilst writing to the database file or
3213 ** journal in pager_recycle(). The error is not returned to the
3214 ** caller of this function. Instead, set the Pager.errCode variable.
3215 ** The error will be returned to the user (or users, in the case
3216 ** of a shared pager cache) of the pager for which the error occured.
3217 */
3218 assert(
3219 (rc&0xff)==SQLITE_IOERR ||
3220 rc==SQLITE_FULL ||
3221 rc==SQLITE_BUSY
3222 );
3223 assert( pPager->state>=PAGER_RESERVED );
3224 pager_error(pPager, rc);
danielk197713f72992005-12-18 08:51:22 +00003225 }
3226 }
danielk1977aef0bf62005-12-30 16:28:01 +00003227
drh86f8c192007-08-22 00:39:19 +00003228 /* Clear the memory management flags and release the mutex
3229 */
3230 for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
3231 pPager->iInUseMM = 0;
3232 }
3233 sqlite3_mutex_leave(mutex);
3234
3235 /* Return the number of bytes released
3236 */
danielk197713f72992005-12-18 08:51:22 +00003237 return nReleased;
3238}
drh86f8c192007-08-22 00:39:19 +00003239#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
danielk197713f72992005-12-18 08:51:22 +00003240
drh165ffe92005-03-15 17:09:30 +00003241/*
danielk1977e180dd92007-04-05 17:15:52 +00003242** Read the content of page pPg out of the database file.
3243*/
3244static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){
3245 int rc;
danielk197762079062007-08-15 17:08:46 +00003246 i64 offset;
danielk1977e180dd92007-04-05 17:15:52 +00003247 assert( MEMDB==0 );
danielk19777a2b1ee2007-08-21 14:27:01 +00003248 assert(pPager->fd->pMethods||pPager->tempFile);
3249 if( !pPager->fd->pMethods ){
3250 return SQLITE_IOERR_SHORT_READ;
3251 }
danielk197762079062007-08-15 17:08:46 +00003252 offset = (pgno-1)*(i64)pPager->pageSize;
3253 rc = sqlite3OsRead(pPager->fd, PGHDR_TO_DATA(pPg), pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00003254 PAGER_INCR(sqlite3_pager_readdb_count);
3255 PAGER_INCR(pPager->nRead);
3256 IOTRACE(("PGIN %p %d\n", pPager, pgno));
drh86a88112007-04-16 15:02:19 +00003257 if( pgno==1 ){
3258 memcpy(&pPager->dbFileVers, &((u8*)PGHDR_TO_DATA(pPg))[24],
3259 sizeof(pPager->dbFileVers));
3260 }
danielk1977e180dd92007-04-05 17:15:52 +00003261 CODEC1(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3);
drh477731b2007-06-16 03:06:27 +00003262 PAGERTRACE4("FETCH %d page %d hash(%08x)\n",
3263 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg));
danielk1977e180dd92007-04-05 17:15:52 +00003264 return rc;
3265}
3266
3267
3268/*
danielk1977e277be02007-03-23 18:12:06 +00003269** This function is called to obtain the shared lock required before
3270** data may be read from the pager cache. If the shared lock has already
3271** been obtained, this function is a no-op.
danielk1977393f0682007-03-31 10:00:48 +00003272**
3273** Immediately after obtaining the shared lock (if required), this function
3274** checks for a hot-journal file. If one is found, an emergency rollback
3275** is performed immediately.
danielk1977e277be02007-03-23 18:12:06 +00003276*/
3277static int pagerSharedLock(Pager *pPager){
3278 int rc = SQLITE_OK;
danielk1977ae72d982007-10-03 08:46:44 +00003279 int isHot = 0;
danielk1977e277be02007-03-23 18:12:06 +00003280
danielk1977ae72d982007-10-03 08:46:44 +00003281 /* If this database is opened for exclusive access, has no outstanding
3282 ** page references and is in an error-state, now is the chance to clear
3283 ** the error. Discard the contents of the pager-cache and treat any
3284 ** open journal file as a hot-journal.
3285 */
3286 if( !MEMDB && pPager->exclusiveMode && pPager->nRef==0 && pPager->errCode ){
3287 if( pPager->journalOpen ){
3288 isHot = 1;
3289 }
3290 pager_reset(pPager);
3291 pPager->errCode = SQLITE_OK;
3292 }
3293
3294 /* If the pager is still in an error state, do not proceed. The error
3295 ** state will be cleared at some point in the future when all page
3296 ** references are dropped and the cache can be discarded.
3297 */
3298 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
3299 return pPager->errCode;
3300 }
3301
3302 if( pPager->state==PAGER_UNLOCK || isHot ){
danielk1977b4b47412007-08-17 15:53:36 +00003303 sqlite3_vfs *pVfs = pPager->pVfs;
danielk1977e277be02007-03-23 18:12:06 +00003304 if( !MEMDB ){
3305 assert( pPager->nRef==0 );
3306 if( !pPager->noReadlock ){
3307 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
3308 if( rc!=SQLITE_OK ){
3309 return pager_error(pPager, rc);
3310 }
3311 assert( pPager->state>=SHARED_LOCK );
3312 }
3313
3314 /* If a journal file exists, and there is no RESERVED lock on the
3315 ** database file, then it either needs to be played back or deleted.
3316 */
danielk1977ae72d982007-10-03 08:46:44 +00003317 if( hasHotJournal(pPager) || isHot ){
danielk1977e277be02007-03-23 18:12:06 +00003318 /* Get an EXCLUSIVE lock on the database file. At this point it is
3319 ** important that a RESERVED lock is not obtained on the way to the
3320 ** EXCLUSIVE lock. If it were, another process might open the
3321 ** database file, detect the RESERVED lock, and conclude that the
3322 ** database is safe to read while this process is still rolling it
3323 ** back.
3324 **
3325 ** Because the intermediate RESERVED lock is not requested, the
3326 ** second process will get to this point in the code and fail to
drh85b623f2007-12-13 21:54:09 +00003327 ** obtain its own EXCLUSIVE lock on the database file.
danielk1977e277be02007-03-23 18:12:06 +00003328 */
danielk1977ae72d982007-10-03 08:46:44 +00003329 if( pPager->state<EXCLUSIVE_LOCK ){
3330 rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
3331 if( rc!=SQLITE_OK ){
3332 pager_unlock(pPager);
3333 return pager_error(pPager, rc);
3334 }
3335 pPager->state = PAGER_EXCLUSIVE;
danielk1977e277be02007-03-23 18:12:06 +00003336 }
danielk1977e277be02007-03-23 18:12:06 +00003337
3338 /* Open the journal for reading only. Return SQLITE_BUSY if
3339 ** we are unable to open the journal file.
3340 **
3341 ** The journal file does not need to be locked itself. The
3342 ** journal file is never open unless the main database file holds
3343 ** a write lock, so there is never any chance of two or more
3344 ** processes opening the journal at the same time.
danielk1977979f38e2007-03-27 16:19:51 +00003345 **
drhfd131da2007-08-07 17:13:03 +00003346 ** Open the journal for read/write access. This is because in
3347 ** exclusive-access mode the file descriptor will be kept open and
danielk1977979f38e2007-03-27 16:19:51 +00003348 ** possibly used for a transaction later on. On some systems, the
3349 ** OsTruncate() call used in exclusive-access mode also requires
3350 ** a read/write file handle.
danielk1977e277be02007-03-23 18:12:06 +00003351 */
danielk1977ae72d982007-10-03 08:46:44 +00003352 if( !isHot ){
3353 rc = SQLITE_BUSY;
3354 if( sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS) ){
3355 int fout = 0;
3356 int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
3357 assert( !pPager->tempFile );
3358 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
3359 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
3360 if( fout&SQLITE_OPEN_READONLY ){
3361 rc = SQLITE_BUSY;
3362 sqlite3OsClose(pPager->jfd);
3363 }
danielk1977979f38e2007-03-27 16:19:51 +00003364 }
3365 }
danielk1977e277be02007-03-23 18:12:06 +00003366 if( rc!=SQLITE_OK ){
3367 pager_unlock(pPager);
danielk1977ae72d982007-10-03 08:46:44 +00003368 return ((rc==SQLITE_NOMEM||rc==SQLITE_IOERR_NOMEM)?rc:SQLITE_BUSY);
danielk1977e277be02007-03-23 18:12:06 +00003369 }
3370 pPager->journalOpen = 1;
3371 pPager->journalStarted = 0;
3372 pPager->journalOff = 0;
3373 pPager->setMaster = 0;
3374 pPager->journalHdr = 0;
3375
3376 /* Playback and delete the journal. Drop the database write
3377 ** lock and reacquire the read lock.
3378 */
3379 rc = pager_playback(pPager, 1);
3380 if( rc!=SQLITE_OK ){
3381 return pager_error(pPager, rc);
3382 }
danielk1977c5859712007-03-26 12:26:27 +00003383 assert(pPager->state==PAGER_SHARED ||
3384 (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
3385 );
danielk1977e277be02007-03-23 18:12:06 +00003386 }
3387
3388 if( pPager->pAll ){
danielk197724168722007-04-02 05:07:47 +00003389 /* The shared-lock has just been acquired on the database file
3390 ** and there are already pages in the cache (from a previous
drh86a88112007-04-16 15:02:19 +00003391 ** read or write transaction). Check to see if the database
3392 ** has been modified. If the database has changed, flush the
3393 ** cache.
3394 **
3395 ** Database changes is detected by looking at 15 bytes beginning
3396 ** at offset 24 into the file. The first 4 of these 16 bytes are
3397 ** a 32-bit counter that is incremented with each change. The
3398 ** other bytes change randomly with each file change when
3399 ** a codec is in use.
3400 **
3401 ** There is a vanishingly small chance that a change will not be
drh6fa51032007-05-09 20:35:31 +00003402 ** detected. The chance of an undetected change is so small that
drh86a88112007-04-16 15:02:19 +00003403 ** it can be neglected.
danielk197724168722007-04-02 05:07:47 +00003404 */
drh86a88112007-04-16 15:02:19 +00003405 char dbFileVers[sizeof(pPager->dbFileVers)];
danielk1977e180dd92007-04-05 17:15:52 +00003406 sqlite3PagerPagecount(pPager);
danielk197724168722007-04-02 05:07:47 +00003407
danielk1977e180dd92007-04-05 17:15:52 +00003408 if( pPager->errCode ){
3409 return pPager->errCode;
danielk1977e277be02007-03-23 18:12:06 +00003410 }
3411
danielk1977e180dd92007-04-05 17:15:52 +00003412 if( pPager->dbSize>0 ){
drhae5e4452007-05-03 17:18:36 +00003413 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
danielk197762079062007-08-15 17:08:46 +00003414 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
danielk1977e180dd92007-04-05 17:15:52 +00003415 if( rc!=SQLITE_OK ){
3416 return rc;
3417 }
drh86a88112007-04-16 15:02:19 +00003418 }else{
3419 memset(dbFileVers, 0, sizeof(dbFileVers));
danielk1977e180dd92007-04-05 17:15:52 +00003420 }
3421
drh86a88112007-04-16 15:02:19 +00003422 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
danielk1977e180dd92007-04-05 17:15:52 +00003423 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00003424 }
3425 }
3426 }
danielk1977c5859712007-03-26 12:26:27 +00003427 assert( pPager->exclusiveMode || pPager->state<=PAGER_SHARED );
3428 if( pPager->state==PAGER_UNLOCK ){
3429 pPager->state = PAGER_SHARED;
3430 }
danielk1977e277be02007-03-23 18:12:06 +00003431 }
3432
3433 return rc;
3434}
3435
3436/*
danielk1977e180dd92007-04-05 17:15:52 +00003437** Allocate a PgHdr object. Either create a new one or reuse
3438** an existing one that is not otherwise in use.
3439**
3440** A new PgHdr structure is created if any of the following are
3441** true:
3442**
3443** (1) We have not exceeded our maximum allocated cache size
3444** as set by the "PRAGMA cache_size" command.
3445**
3446** (2) There are no unused PgHdr objects available at this time.
3447**
3448** (3) This is an in-memory database.
3449**
3450** (4) There are no PgHdr objects that do not require a journal
3451** file sync and a sync of the journal file is currently
3452** prohibited.
3453**
3454** Otherwise, reuse an existing PgHdr. In other words, reuse an
3455** existing PgHdr if all of the following are true:
3456**
3457** (1) We have reached or exceeded the maximum cache size
3458** allowed by "PRAGMA cache_size".
3459**
3460** (2) There is a PgHdr available with PgHdr->nRef==0
3461**
3462** (3) We are not in an in-memory database
3463**
3464** (4) Either there is an available PgHdr that does not need
3465** to be synced to disk or else disk syncing is currently
3466** allowed.
danielk197724168722007-04-02 05:07:47 +00003467*/
3468static int pagerAllocatePage(Pager *pPager, PgHdr **ppPg){
3469 int rc = SQLITE_OK;
3470 PgHdr *pPg;
drh1e3af332007-10-20 13:17:54 +00003471 int nByteHdr;
danielk197724168722007-04-02 05:07:47 +00003472
danielk1977e180dd92007-04-05 17:15:52 +00003473 /* Create a new PgHdr if any of the four conditions defined
danielk19772a6bdf62007-08-20 16:07:00 +00003474 ** above are met: */
danielk1977e180dd92007-04-05 17:15:52 +00003475 if( pPager->nPage<pPager->mxPage
danielk19779f61c2f2007-08-27 17:27:49 +00003476 || pPager->lru.pFirst==0
danielk1977e180dd92007-04-05 17:15:52 +00003477 || MEMDB
danielk19779f61c2f2007-08-27 17:27:49 +00003478 || (pPager->lru.pFirstSynced==0 && pPager->doNotSync)
danielk1977e180dd92007-04-05 17:15:52 +00003479 ){
drh0d180202008-02-14 23:26:56 +00003480 void *pData;
danielk197724168722007-04-02 05:07:47 +00003481 if( pPager->nPage>=pPager->nHash ){
3482 pager_resize_hash_table(pPager,
3483 pPager->nHash<256 ? 256 : pPager->nHash*2);
3484 if( pPager->nHash==0 ){
3485 rc = SQLITE_NOMEM;
3486 goto pager_allocate_out;
3487 }
3488 }
drhed138fb2007-08-22 22:04:37 +00003489 pagerLeave(pPager);
drh1e3af332007-10-20 13:17:54 +00003490 nByteHdr = sizeof(*pPg) + sizeof(u32) + pPager->nExtra
3491 + MEMDB*sizeof(PgHistory);
drh0d180202008-02-14 23:26:56 +00003492 pPg = sqlite3_malloc( nByteHdr );
3493 if( pPg ){
3494 pData = sqlite3_malloc( pPager->pageSize );
3495 if( pData==0 ){
3496 sqlite3_free(pPg);
3497 pPg = 0;
3498 }
3499 }
drhed138fb2007-08-22 22:04:37 +00003500 pagerEnter(pPager);
danielk197724168722007-04-02 05:07:47 +00003501 if( pPg==0 ){
3502 rc = SQLITE_NOMEM;
3503 goto pager_allocate_out;
3504 }
drh1e3af332007-10-20 13:17:54 +00003505 memset(pPg, 0, nByteHdr);
drh0d180202008-02-14 23:26:56 +00003506 pPg->pData = pData;
danielk197724168722007-04-02 05:07:47 +00003507 pPg->pPager = pPager;
3508 pPg->pNextAll = pPager->pAll;
3509 pPager->pAll = pPg;
3510 pPager->nPage++;
danielk197724168722007-04-02 05:07:47 +00003511 }else{
3512 /* Recycle an existing page with a zero ref-count. */
danielk1977843e65f2007-09-01 16:16:15 +00003513 rc = pager_recycle(pPager, &pPg);
danielk1977e965ac72007-06-13 15:22:28 +00003514 if( rc==SQLITE_BUSY ){
3515 rc = SQLITE_IOERR_BLOCKED;
3516 }
danielk197724168722007-04-02 05:07:47 +00003517 if( rc!=SQLITE_OK ){
3518 goto pager_allocate_out;
3519 }
3520 assert( pPager->state>=SHARED_LOCK );
3521 assert(pPg);
3522 }
3523 *ppPg = pPg;
3524
3525pager_allocate_out:
3526 return rc;
3527}
3528
3529/*
drhd33d5a82007-04-26 12:11:28 +00003530** Make sure we have the content for a page. If the page was
3531** previously acquired with noContent==1, then the content was
3532** just initialized to zeros instead of being read from disk.
3533** But now we need the real data off of disk. So make sure we
3534** have it. Read it in if we do not have it already.
3535*/
3536static int pager_get_content(PgHdr *pPg){
3537 if( pPg->needRead ){
3538 int rc = readDbPage(pPg->pPager, pPg, pPg->pgno);
3539 if( rc==SQLITE_OK ){
3540 pPg->needRead = 0;
3541 }else{
3542 return rc;
3543 }
3544 }
3545 return SQLITE_OK;
3546}
3547
3548/*
drhd9b02572001-04-15 00:37:09 +00003549** Acquire a page.
3550**
drh58a11682001-11-10 13:51:08 +00003551** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00003552** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00003553**
drhd33d5a82007-04-26 12:11:28 +00003554** This routine works for any page number greater than 0. If the database
drh306dc212001-05-21 13:45:10 +00003555** file is smaller than the requested page, then no actual disk
3556** read occurs and the memory image of the page is initialized to
3557** all zeros. The extra data appended to a page is always initialized
3558** to zeros the first time a page is loaded into memory.
3559**
drhd9b02572001-04-15 00:37:09 +00003560** The acquisition might fail for several reasons. In all cases,
3561** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00003562**
drhd33d5a82007-04-26 12:11:28 +00003563** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
drh7e3b0a02001-04-28 16:52:40 +00003564** to find a page in the in-memory cache first. If the page is not already
drhd33d5a82007-04-26 12:11:28 +00003565** in memory, this routine goes to disk to read it in whereas Lookup()
drh7e3b0a02001-04-28 16:52:40 +00003566** just returns 0. This routine acquires a read-lock the first time it
3567** has to go to disk, and could also playback an old journal if necessary.
drhd33d5a82007-04-26 12:11:28 +00003568** Since Lookup() never goes to disk, it never has to deal with locks
drh7e3b0a02001-04-28 16:52:40 +00003569** or journal files.
drh0787db62007-03-04 13:15:27 +00003570**
drh538f5702007-04-13 02:14:30 +00003571** If noContent is false, the page contents are actually read from disk.
3572** If noContent is true, it means that we do not care about the contents
3573** of the page at this time, so do not do a disk read. Just fill in the
3574** page content with zeros. But mark the fact that we have not read the
3575** content by setting the PgHdr.needRead flag. Later on, if
drhd33d5a82007-04-26 12:11:28 +00003576** sqlite3PagerWrite() is called on this page or if this routine is
3577** called again with noContent==0, that means that the content is needed
3578** and the disk read should occur at that point.
drhed7c8552001-04-11 14:29:21 +00003579*/
drh86f8c192007-08-22 00:39:19 +00003580static int pagerAcquire(
drh538f5702007-04-13 02:14:30 +00003581 Pager *pPager, /* The pager open on the database file */
3582 Pgno pgno, /* Page number to fetch */
3583 DbPage **ppPage, /* Write a pointer to the page here */
3584 int noContent /* Do not bother reading content from disk if true */
3585){
drhed7c8552001-04-11 14:29:21 +00003586 PgHdr *pPg;
drh165ffe92005-03-15 17:09:30 +00003587 int rc;
drhed7c8552001-04-11 14:29:21 +00003588
danielk1977e277be02007-03-23 18:12:06 +00003589 assert( pPager->state==PAGER_UNLOCK || pPager->nRef>0 || pgno==1 );
3590
danielk197726836652005-01-17 01:33:13 +00003591 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
3592 ** number greater than this, or zero, is requested.
3593 */
drh267cb322005-09-16 17:16:52 +00003594 if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh49285702005-09-17 15:20:26 +00003595 return SQLITE_CORRUPT_BKPT;
danielk197726836652005-01-17 01:33:13 +00003596 }
3597
drhd9b02572001-04-15 00:37:09 +00003598 /* Make sure we have not hit any critical errors.
3599 */
drh836faa42003-01-11 13:30:57 +00003600 assert( pPager!=0 );
drh2e6d11b2003-04-25 15:37:57 +00003601 *ppPage = 0;
drhd9b02572001-04-15 00:37:09 +00003602
danielk197713adf8a2004-06-03 16:08:41 +00003603 /* If this is the first page accessed, then get a SHARED lock
danielk1977334cdb62007-03-26 08:05:12 +00003604 ** on the database file. pagerSharedLock() is a no-op if
3605 ** a database lock is already held.
drhed7c8552001-04-11 14:29:21 +00003606 */
danielk1977e277be02007-03-23 18:12:06 +00003607 rc = pagerSharedLock(pPager);
3608 if( rc!=SQLITE_OK ){
3609 return rc;
drhed7c8552001-04-11 14:29:21 +00003610 }
danielk1977e277be02007-03-23 18:12:06 +00003611 assert( pPager->state!=PAGER_UNLOCK );
3612
3613 pPg = pager_lookup(pPager, pgno);
drhed7c8552001-04-11 14:29:21 +00003614 if( pPg==0 ){
drhd9b02572001-04-15 00:37:09 +00003615 /* The requested page is not in the page cache. */
danielk1977979f38e2007-03-27 16:19:51 +00003616 int nMax;
drhed7c8552001-04-11 14:29:21 +00003617 int h;
drh538f5702007-04-13 02:14:30 +00003618 PAGER_INCR(pPager->nMiss);
danielk197724168722007-04-02 05:07:47 +00003619 rc = pagerAllocatePage(pPager, &pPg);
3620 if( rc!=SQLITE_OK ){
3621 return rc;
drhed7c8552001-04-11 14:29:21 +00003622 }
danielk197724168722007-04-02 05:07:47 +00003623
drhed7c8552001-04-11 14:29:21 +00003624 pPg->pgno = pgno;
danielk1977f35843b2007-04-07 15:03:17 +00003625 assert( !MEMDB || pgno>pPager->stmtSize );
drhf5e7bb52008-02-18 14:47:33 +00003626 pPg->inJournal = sqlite3BitvecTest(pPager->pInJournal, pgno);
3627 pPg->needSync = 0;
danielk1977f35843b2007-04-07 15:03:17 +00003628
drh605ad8f2006-05-03 23:34:05 +00003629 makeClean(pPg);
drhed7c8552001-04-11 14:29:21 +00003630 pPg->nRef = 1;
danielk197775bab7d2006-01-23 13:09:45 +00003631
drhd9b02572001-04-15 00:37:09 +00003632 pPager->nRef++;
drh2e6d11b2003-04-25 15:37:57 +00003633 if( pPager->nExtra>0 ){
drh90f5ecb2004-07-22 01:19:35 +00003634 memset(PGHDR_TO_EXTRA(pPg, pPager), 0, pPager->nExtra);
drh2e6d11b2003-04-25 15:37:57 +00003635 }
danielk1977979f38e2007-03-27 16:19:51 +00003636 nMax = sqlite3PagerPagecount(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00003637 if( pPager->errCode ){
danielk1977efaaf572006-01-16 11:29:19 +00003638 rc = pPager->errCode;
danielk1977ae72d982007-10-03 08:46:44 +00003639 sqlite3PagerUnref(pPg);
drh2e6d11b2003-04-25 15:37:57 +00003640 return rc;
3641 }
danielk197775bab7d2006-01-23 13:09:45 +00003642
3643 /* Populate the page with data, either by reading from the database
3644 ** file, or by setting the entire page to zero.
3645 */
drhd215acf2007-04-13 04:01:58 +00003646 if( nMax<(int)pgno || MEMDB || (noContent && !pPager->alwaysRollback) ){
drhf8e632b2007-05-08 14:51:36 +00003647 if( pgno>pPager->mxPgno ){
danielk1977de3bea72007-05-09 15:56:39 +00003648 sqlite3PagerUnref(pPg);
drhf8e632b2007-05-08 14:51:36 +00003649 return SQLITE_FULL;
3650 }
drh90f5ecb2004-07-22 01:19:35 +00003651 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
drhd215acf2007-04-13 04:01:58 +00003652 pPg->needRead = noContent && !pPager->alwaysRollback;
drh538f5702007-04-13 02:14:30 +00003653 IOTRACE(("ZERO %p %d\n", pPager, pgno));
drh306dc212001-05-21 13:45:10 +00003654 }else{
danielk1977e180dd92007-04-05 17:15:52 +00003655 rc = readDbPage(pPager, pPg, pgno);
drh551b7732006-11-06 21:20:25 +00003656 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
3657 pPg->pgno = 0;
danielk19773b8a05f2007-03-19 17:44:26 +00003658 sqlite3PagerUnref(pPg);
drh551b7732006-11-06 21:20:25 +00003659 return rc;
drh81a20f22001-10-12 17:30:04 +00003660 }
danielk1977b4626a32007-04-28 15:47:43 +00003661 pPg->needRead = 0;
drh306dc212001-05-21 13:45:10 +00003662 }
danielk197775bab7d2006-01-23 13:09:45 +00003663
3664 /* Link the page into the page hash table */
drh8ca0c722006-05-07 17:49:38 +00003665 h = pgno & (pPager->nHash-1);
drh3765df42006-06-28 18:18:09 +00003666 assert( pgno!=0 );
danielk197775bab7d2006-01-23 13:09:45 +00003667 pPg->pNextHash = pPager->aHash[h];
3668 pPager->aHash[h] = pPg;
3669 if( pPg->pNextHash ){
3670 assert( pPg->pNextHash->pPrevHash==0 );
3671 pPg->pNextHash->pPrevHash = pPg;
3672 }
3673
danielk19773c407372005-02-15 02:54:14 +00003674#ifdef SQLITE_CHECK_PAGES
3675 pPg->pageHash = pager_pagehash(pPg);
3676#endif
drhed7c8552001-04-11 14:29:21 +00003677 }else{
drhd9b02572001-04-15 00:37:09 +00003678 /* The requested page is in the page cache. */
danielk1977e277be02007-03-23 18:12:06 +00003679 assert(pPager->nRef>0 || pgno==1);
drh538f5702007-04-13 02:14:30 +00003680 PAGER_INCR(pPager->nHit);
drhd33d5a82007-04-26 12:11:28 +00003681 if( !noContent ){
3682 rc = pager_get_content(pPg);
3683 if( rc ){
3684 return rc;
3685 }
3686 }
drhdf0b3b02001-06-23 11:36:20 +00003687 page_ref(pPg);
drhed7c8552001-04-11 14:29:21 +00003688 }
danielk19773b8a05f2007-03-19 17:44:26 +00003689 *ppPage = pPg;
drhed7c8552001-04-11 14:29:21 +00003690 return SQLITE_OK;
3691}
drh86f8c192007-08-22 00:39:19 +00003692int sqlite3PagerAcquire(
3693 Pager *pPager, /* The pager open on the database file */
3694 Pgno pgno, /* Page number to fetch */
3695 DbPage **ppPage, /* Write a pointer to the page here */
3696 int noContent /* Do not bother reading content from disk if true */
3697){
3698 int rc;
3699 pagerEnter(pPager);
3700 rc = pagerAcquire(pPager, pgno, ppPage, noContent);
3701 pagerLeave(pPager);
3702 return rc;
3703}
3704
drhed7c8552001-04-11 14:29:21 +00003705
3706/*
drh7e3b0a02001-04-28 16:52:40 +00003707** Acquire a page if it is already in the in-memory cache. Do
3708** not read the page from disk. Return a pointer to the page,
3709** or 0 if the page is not in cache.
3710**
danielk19773b8a05f2007-03-19 17:44:26 +00003711** See also sqlite3PagerGet(). The difference between this routine
3712** and sqlite3PagerGet() is that _get() will go to the disk and read
drh7e3b0a02001-04-28 16:52:40 +00003713** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00003714** returns NULL if the page is not in cache or if a disk I/O error
3715** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00003716*/
danielk19773b8a05f2007-03-19 17:44:26 +00003717DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
drh86f8c192007-08-22 00:39:19 +00003718 PgHdr *pPg = 0;
drh7e3b0a02001-04-28 16:52:40 +00003719
drh836faa42003-01-11 13:30:57 +00003720 assert( pPager!=0 );
3721 assert( pgno!=0 );
danielk1977e277be02007-03-23 18:12:06 +00003722
drh86f8c192007-08-22 00:39:19 +00003723 pagerEnter(pPager);
danielk1977e277be02007-03-23 18:12:06 +00003724 if( pPager->state==PAGER_UNLOCK ){
danielk1977334cdb62007-03-26 08:05:12 +00003725 assert( !pPager->pAll || pPager->exclusiveMode );
drh86f8c192007-08-22 00:39:19 +00003726 }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
3727 /* Do nothing */
3728 }else if( (pPg = pager_lookup(pPager, pgno))!=0 ){
3729 page_ref(pPg);
danielk1977e277be02007-03-23 18:12:06 +00003730 }
drh86f8c192007-08-22 00:39:19 +00003731 pagerLeave(pPager);
danielk19773b8a05f2007-03-19 17:44:26 +00003732 return pPg;
drh7e3b0a02001-04-28 16:52:40 +00003733}
3734
3735/*
drhed7c8552001-04-11 14:29:21 +00003736** Release a page.
3737**
3738** If the number of references to the page drop to zero, then the
3739** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00003740** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00003741** removed.
3742*/
danielk19773b8a05f2007-03-19 17:44:26 +00003743int sqlite3PagerUnref(DbPage *pPg){
danielk1977ae72d982007-10-03 08:46:44 +00003744 Pager *pPager = pPg->pPager;
drhd9b02572001-04-15 00:37:09 +00003745
3746 /* Decrement the reference count for this page
3747 */
drhed7c8552001-04-11 14:29:21 +00003748 assert( pPg->nRef>0 );
drh86f8c192007-08-22 00:39:19 +00003749 pagerEnter(pPg->pPager);
drhed7c8552001-04-11 14:29:21 +00003750 pPg->nRef--;
drhd9b02572001-04-15 00:37:09 +00003751
danielk19773c407372005-02-15 02:54:14 +00003752 CHECK_PAGE(pPg);
3753
drh72f82862001-05-24 21:06:34 +00003754 /* When the number of references to a page reach 0, call the
3755 ** destructor and add the page to the freelist.
drhd9b02572001-04-15 00:37:09 +00003756 */
drhed7c8552001-04-11 14:29:21 +00003757 if( pPg->nRef==0 ){
danielk19779f61c2f2007-08-27 17:27:49 +00003758
3759 lruListAdd(pPg);
drh72f82862001-05-24 21:06:34 +00003760 if( pPager->xDestructor ){
danielk19773b8a05f2007-03-19 17:44:26 +00003761 pPager->xDestructor(pPg, pPager->pageSize);
drh72f82862001-05-24 21:06:34 +00003762 }
drhd9b02572001-04-15 00:37:09 +00003763
3764 /* When all pages reach the freelist, drop the read lock from
3765 ** the database file.
3766 */
3767 pPager->nRef--;
3768 assert( pPager->nRef>=0 );
danielk19773dedc192007-03-27 17:37:31 +00003769 if( pPager->nRef==0 && (!pPager->exclusiveMode || pPager->journalOff>0) ){
danielk1977e277be02007-03-23 18:12:06 +00003770 pagerUnlockAndRollback(pPager);
drhd9b02572001-04-15 00:37:09 +00003771 }
drhed7c8552001-04-11 14:29:21 +00003772 }
danielk1977ae72d982007-10-03 08:46:44 +00003773 pagerLeave(pPager);
drhd9b02572001-04-15 00:37:09 +00003774 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00003775}
3776
3777/*
drha6abd042004-06-09 17:37:22 +00003778** Create a journal file for pPager. There should already be a RESERVED
3779** or EXCLUSIVE lock on the database file when this routine is called.
drhda47d772002-12-02 04:25:19 +00003780**
3781** Return SQLITE_OK if everything. Return an error code and release the
3782** write lock if anything goes wrong.
3783*/
3784static int pager_open_journal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00003785 sqlite3_vfs *pVfs = pPager->pVfs;
3786 int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE);
3787
drhda47d772002-12-02 04:25:19 +00003788 int rc;
drhb7f91642004-10-31 02:22:47 +00003789 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +00003790 assert( pPager->state>=PAGER_RESERVED );
drhda47d772002-12-02 04:25:19 +00003791 assert( pPager->journalOpen==0 );
3792 assert( pPager->useJournal );
drhf5e7bb52008-02-18 14:47:33 +00003793 assert( pPager->pInJournal==0 );
danielk19773b8a05f2007-03-19 17:44:26 +00003794 sqlite3PagerPagecount(pPager);
drhed138fb2007-08-22 22:04:37 +00003795 pagerLeave(pPager);
drhf5e7bb52008-02-18 14:47:33 +00003796 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
drhed138fb2007-08-22 22:04:37 +00003797 pagerEnter(pPager);
drhf5e7bb52008-02-18 14:47:33 +00003798 if( pPager->pInJournal==0 ){
drh9c105bb2004-10-02 20:38:28 +00003799 rc = SQLITE_NOMEM;
3800 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003801 }
danielk1977b4b47412007-08-17 15:53:36 +00003802
3803 if( pPager->tempFile ){
danielk1977fee2d252007-08-18 10:59:19 +00003804 flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
3805 }else{
3806 flags |= (SQLITE_OPEN_MAIN_JOURNAL);
danielk1977b4b47412007-08-17 15:53:36 +00003807 }
danielk1977c7b60172007-08-22 11:22:03 +00003808#ifdef SQLITE_ENABLE_ATOMIC_WRITE
3809 rc = sqlite3JournalOpen(
3810 pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
3811 );
3812#else
danielk1977b4b47412007-08-17 15:53:36 +00003813 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
danielk1977c7b60172007-08-22 11:22:03 +00003814#endif
danielk1977b4b47412007-08-17 15:53:36 +00003815 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
danielk197776572402004-06-25 02:38:54 +00003816 pPager->journalOff = 0;
3817 pPager->setMaster = 0;
3818 pPager->journalHdr = 0;
drhda47d772002-12-02 04:25:19 +00003819 if( rc!=SQLITE_OK ){
drh600e46a2007-03-28 01:59:33 +00003820 if( rc==SQLITE_NOMEM ){
danielk1977fee2d252007-08-18 10:59:19 +00003821 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
drh600e46a2007-03-28 01:59:33 +00003822 }
drh9c105bb2004-10-02 20:38:28 +00003823 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003824 }
3825 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00003826 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00003827 pPager->needSync = 0;
3828 pPager->alwaysRollback = 0;
drh968af522003-02-11 14:55:40 +00003829 pPager->nRec = 0;
danielk1977efaaf572006-01-16 11:29:19 +00003830 if( pPager->errCode ){
3831 rc = pPager->errCode;
drhdd5b2fa2005-03-28 03:39:55 +00003832 goto failed_to_open_journal;
drh2e6d11b2003-04-25 15:37:57 +00003833 }
drhda47d772002-12-02 04:25:19 +00003834 pPager->origDbSize = pPager->dbSize;
drhae2b40c2004-06-09 19:03:54 +00003835
danielk197776572402004-06-25 02:38:54 +00003836 rc = writeJournalHdr(pPager);
3837
drhac69b052004-05-12 13:30:07 +00003838 if( pPager->stmtAutoopen && rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00003839 rc = sqlite3PagerStmtBegin(pPager);
drhda47d772002-12-02 04:25:19 +00003840 }
danielk1977ae72d982007-10-03 08:46:44 +00003841 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_NOMEM ){
drh80e35f42007-03-30 14:06:34 +00003842 rc = pager_end_transaction(pPager);
drhda47d772002-12-02 04:25:19 +00003843 if( rc==SQLITE_OK ){
3844 rc = SQLITE_FULL;
3845 }
3846 }
drh9c105bb2004-10-02 20:38:28 +00003847 return rc;
3848
3849failed_to_open_journal:
drhf5e7bb52008-02-18 14:47:33 +00003850 sqlite3BitvecDestroy(pPager->pInJournal);
3851 pPager->pInJournal = 0;
drh9c105bb2004-10-02 20:38:28 +00003852 return rc;
drhda47d772002-12-02 04:25:19 +00003853}
3854
3855/*
drh4b845d72002-03-05 12:41:19 +00003856** Acquire a write-lock on the database. The lock is removed when
3857** the any of the following happen:
3858**
drh80e35f42007-03-30 14:06:34 +00003859** * sqlite3PagerCommitPhaseTwo() is called.
danielk19773b8a05f2007-03-19 17:44:26 +00003860** * sqlite3PagerRollback() is called.
3861** * sqlite3PagerClose() is called.
3862** * sqlite3PagerUnref() is called to on every outstanding page.
drh4b845d72002-03-05 12:41:19 +00003863**
danielk197713adf8a2004-06-03 16:08:41 +00003864** The first parameter to this routine is a pointer to any open page of the
3865** database file. Nothing changes about the page - it is used merely to
3866** acquire a pointer to the Pager structure and as proof that there is
3867** already a read-lock on the database.
drh4b845d72002-03-05 12:41:19 +00003868**
danielk197713adf8a2004-06-03 16:08:41 +00003869** The second parameter indicates how much space in bytes to reserve for a
3870** master journal file-name at the start of the journal when it is created.
3871**
3872** A journal file is opened if this is not a temporary file. For temporary
3873** files, the opening of the journal file is deferred until there is an
3874** actual need to write to the journal.
drhda47d772002-12-02 04:25:19 +00003875**
drha6abd042004-06-09 17:37:22 +00003876** If the database is already reserved for writing, this routine is a no-op.
drh684917c2004-10-05 02:41:42 +00003877**
3878** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file
3879** immediately instead of waiting until we try to flush the cache. The
3880** exFlag is ignored if a transaction is already active.
drh4b845d72002-03-05 12:41:19 +00003881*/
danielk19773b8a05f2007-03-19 17:44:26 +00003882int sqlite3PagerBegin(DbPage *pPg, int exFlag){
drh4b845d72002-03-05 12:41:19 +00003883 Pager *pPager = pPg->pPager;
3884 int rc = SQLITE_OK;
drh86f8c192007-08-22 00:39:19 +00003885 pagerEnter(pPager);
drh4b845d72002-03-05 12:41:19 +00003886 assert( pPg->nRef>0 );
drha6abd042004-06-09 17:37:22 +00003887 assert( pPager->state!=PAGER_UNLOCK );
3888 if( pPager->state==PAGER_SHARED ){
drhf5e7bb52008-02-18 14:47:33 +00003889 assert( pPager->pInJournal==0 );
drhb7f91642004-10-31 02:22:47 +00003890 if( MEMDB ){
drha6abd042004-06-09 17:37:22 +00003891 pPager->state = PAGER_EXCLUSIVE;
drhac69b052004-05-12 13:30:07 +00003892 pPager->origDbSize = pPager->dbSize;
3893 }else{
drh054889e2005-11-30 03:20:31 +00003894 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
drh684917c2004-10-05 02:41:42 +00003895 if( rc==SQLITE_OK ){
3896 pPager->state = PAGER_RESERVED;
3897 if( exFlag ){
3898 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
3899 }
3900 }
danielk19779eed5052004-06-04 10:38:30 +00003901 if( rc!=SQLITE_OK ){
drh86f8c192007-08-22 00:39:19 +00003902 pagerLeave(pPager);
danielk19779eed5052004-06-04 10:38:30 +00003903 return rc;
drhac69b052004-05-12 13:30:07 +00003904 }
drha6abd042004-06-09 17:37:22 +00003905 pPager->dirtyCache = 0;
drh4f0c5872007-03-26 22:05:01 +00003906 PAGERTRACE2("TRANSACTION %d\n", PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00003907 if( pPager->useJournal && !pPager->tempFile ){
3908 rc = pager_open_journal(pPager);
3909 }
drh4b845d72002-03-05 12:41:19 +00003910 }
danielk1977334cdb62007-03-26 08:05:12 +00003911 }else if( pPager->journalOpen && pPager->journalOff==0 ){
3912 /* This happens when the pager was in exclusive-access mode last
3913 ** time a (read or write) transaction was successfully concluded
3914 ** by this connection. Instead of deleting the journal file it was
3915 ** kept open and truncated to 0 bytes.
3916 */
3917 assert( pPager->nRec==0 );
3918 assert( pPager->origDbSize==0 );
drhf5e7bb52008-02-18 14:47:33 +00003919 assert( pPager->pInJournal==0 );
danielk1977334cdb62007-03-26 08:05:12 +00003920 sqlite3PagerPagecount(pPager);
drhed138fb2007-08-22 22:04:37 +00003921 pagerLeave(pPager);
drhf5e7bb52008-02-18 14:47:33 +00003922 pPager->pInJournal = sqlite3BitvecCreate( pPager->dbSize );
drhed138fb2007-08-22 22:04:37 +00003923 pagerEnter(pPager);
drhf5e7bb52008-02-18 14:47:33 +00003924 if( !pPager->pInJournal ){
danielk1977334cdb62007-03-26 08:05:12 +00003925 rc = SQLITE_NOMEM;
3926 }else{
drh369339d2007-03-30 16:01:55 +00003927 pPager->origDbSize = pPager->dbSize;
danielk1977334cdb62007-03-26 08:05:12 +00003928 rc = writeJournalHdr(pPager);
3929 }
drh4b845d72002-03-05 12:41:19 +00003930 }
danielk1977334cdb62007-03-26 08:05:12 +00003931 assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK );
drh86f8c192007-08-22 00:39:19 +00003932 pagerLeave(pPager);
drh4b845d72002-03-05 12:41:19 +00003933 return rc;
3934}
3935
3936/*
drh605ad8f2006-05-03 23:34:05 +00003937** Make a page dirty. Set its dirty flag and add it to the dirty
3938** page list.
3939*/
3940static void makeDirty(PgHdr *pPg){
3941 if( pPg->dirty==0 ){
3942 Pager *pPager = pPg->pPager;
3943 pPg->dirty = 1;
3944 pPg->pDirty = pPager->pDirty;
3945 if( pPager->pDirty ){
3946 pPager->pDirty->pPrevDirty = pPg;
3947 }
3948 pPg->pPrevDirty = 0;
3949 pPager->pDirty = pPg;
3950 }
3951}
3952
3953/*
3954** Make a page clean. Clear its dirty bit and remove it from the
3955** dirty page list.
3956*/
3957static void makeClean(PgHdr *pPg){
3958 if( pPg->dirty ){
3959 pPg->dirty = 0;
3960 if( pPg->pDirty ){
drh153c62c2007-08-24 03:51:33 +00003961 assert( pPg->pDirty->pPrevDirty==pPg );
drh605ad8f2006-05-03 23:34:05 +00003962 pPg->pDirty->pPrevDirty = pPg->pPrevDirty;
3963 }
3964 if( pPg->pPrevDirty ){
drh153c62c2007-08-24 03:51:33 +00003965 assert( pPg->pPrevDirty->pDirty==pPg );
drh605ad8f2006-05-03 23:34:05 +00003966 pPg->pPrevDirty->pDirty = pPg->pDirty;
3967 }else{
drh153c62c2007-08-24 03:51:33 +00003968 assert( pPg->pPager->pDirty==pPg );
drh605ad8f2006-05-03 23:34:05 +00003969 pPg->pPager->pDirty = pPg->pDirty;
3970 }
3971 }
3972}
3973
3974
3975/*
drhed7c8552001-04-11 14:29:21 +00003976** Mark a data page as writeable. The page is written into the journal
3977** if it is not there already. This routine must be called before making
3978** changes to a page.
3979**
3980** The first time this routine is called, the pager creates a new
drha6abd042004-06-09 17:37:22 +00003981** journal and acquires a RESERVED lock on the database. If the RESERVED
drhed7c8552001-04-11 14:29:21 +00003982** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00003983** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00003984** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00003985**
3986** If the journal file could not be written because the disk is full,
3987** then this routine returns SQLITE_FULL and does an immediate rollback.
3988** All subsequent write attempts also return SQLITE_FULL until there
danielk19773b8a05f2007-03-19 17:44:26 +00003989** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to
drhd9b02572001-04-15 00:37:09 +00003990** reset.
drhed7c8552001-04-11 14:29:21 +00003991*/
danielk19773b8a05f2007-03-19 17:44:26 +00003992static int pager_write(PgHdr *pPg){
3993 void *pData = PGHDR_TO_DATA(pPg);
drh69688d52001-04-14 16:38:23 +00003994 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00003995 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00003996
drh6446c4d2001-12-15 14:22:18 +00003997 /* Check for errors
3998 */
danielk1977efaaf572006-01-16 11:29:19 +00003999 if( pPager->errCode ){
4000 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004001 }
drh5e00f6c2001-09-13 13:46:56 +00004002 if( pPager->readOnly ){
4003 return SQLITE_PERM;
4004 }
drh6446c4d2001-12-15 14:22:18 +00004005
danielk197776572402004-06-25 02:38:54 +00004006 assert( !pPager->setMaster );
4007
danielk19773c407372005-02-15 02:54:14 +00004008 CHECK_PAGE(pPg);
4009
drh538f5702007-04-13 02:14:30 +00004010 /* If this page was previously acquired with noContent==1, that means
4011 ** we didn't really read in the content of the page. This can happen
4012 ** (for example) when the page is being moved to the freelist. But
4013 ** now we are (perhaps) moving the page off of the freelist for
4014 ** reuse and we need to know its original content so that content
4015 ** can be stored in the rollback journal. So do the read at this
4016 ** time.
4017 */
drhd33d5a82007-04-26 12:11:28 +00004018 rc = pager_get_content(pPg);
4019 if( rc ){
4020 return rc;
drh538f5702007-04-13 02:14:30 +00004021 }
4022
drh6446c4d2001-12-15 14:22:18 +00004023 /* Mark the page as dirty. If the page has already been written
4024 ** to the journal then we can return right away.
4025 */
drh605ad8f2006-05-03 23:34:05 +00004026 makeDirty(pPg);
danielk1977f35843b2007-04-07 15:03:17 +00004027 if( pPg->inJournal && (pageInStatement(pPg) || pPager->stmtInUse==0) ){
drha6abd042004-06-09 17:37:22 +00004028 pPager->dirtyCache = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00004029 }else{
drh6446c4d2001-12-15 14:22:18 +00004030
danielk1977a0bf2652004-11-04 14:30:04 +00004031 /* If we get this far, it means that the page needs to be
4032 ** written to the transaction journal or the ckeckpoint journal
4033 ** or both.
4034 **
4035 ** First check to see that the transaction journal exists and
4036 ** create it if it does not.
4037 */
4038 assert( pPager->state!=PAGER_UNLOCK );
danielk19773b8a05f2007-03-19 17:44:26 +00004039 rc = sqlite3PagerBegin(pPg, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00004040 if( rc!=SQLITE_OK ){
4041 return rc;
4042 }
4043 assert( pPager->state>=PAGER_RESERVED );
4044 if( !pPager->journalOpen && pPager->useJournal ){
4045 rc = pager_open_journal(pPager);
4046 if( rc!=SQLITE_OK ) return rc;
4047 }
4048 assert( pPager->journalOpen || !pPager->useJournal );
4049 pPager->dirtyCache = 1;
4050
4051 /* The transaction journal now exists and we have a RESERVED or an
4052 ** EXCLUSIVE lock on the main database file. Write the current page to
4053 ** the transaction journal if it is not there already.
4054 */
4055 if( !pPg->inJournal && (pPager->useJournal || MEMDB) ){
4056 if( (int)pPg->pgno <= pPager->origDbSize ){
danielk1977a0bf2652004-11-04 14:30:04 +00004057 if( MEMDB ){
4058 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
drh4f0c5872007-03-26 22:05:01 +00004059 PAGERTRACE3("JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00004060 assert( pHist->pOrig==0 );
drh17435752007-08-16 04:30:38 +00004061 pHist->pOrig = sqlite3_malloc( pPager->pageSize );
danielk1977662278e2007-11-05 15:30:12 +00004062 if( !pHist->pOrig ){
4063 return SQLITE_NOMEM;
danielk1977a0bf2652004-11-04 14:30:04 +00004064 }
danielk1977662278e2007-11-05 15:30:12 +00004065 memcpy(pHist->pOrig, PGHDR_TO_DATA(pPg), pPager->pageSize);
danielk1977a0bf2652004-11-04 14:30:04 +00004066 }else{
drhbf4bca52007-09-06 22:19:14 +00004067 u32 cksum;
4068 char *pData2;
danielk1977dd97a492007-08-22 18:54:32 +00004069
drh267cb322005-09-16 17:16:52 +00004070 /* We should never write to the journal file the page that
4071 ** contains the database locks. The following assert verifies
4072 ** that we do not. */
4073 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
drhc001c582006-03-06 18:23:16 +00004074 pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
drh37527852006-03-16 16:19:56 +00004075 cksum = pager_cksum(pPager, (u8*)pData2);
drhbf4bca52007-09-06 22:19:14 +00004076 rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno);
4077 if( rc==SQLITE_OK ){
4078 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize,
4079 pPager->journalOff + 4);
4080 pPager->journalOff += pPager->pageSize+4;
4081 }
4082 if( rc==SQLITE_OK ){
4083 rc = write32bits(pPager->jfd, pPager->journalOff, cksum);
4084 pPager->journalOff += 4;
4085 }
4086 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
danielk1977667a6c92007-09-10 06:12:04 +00004087 pPager->journalOff, pPager->pageSize));
drh538f5702007-04-13 02:14:30 +00004088 PAGER_INCR(sqlite3_pager_writej_count);
drh477731b2007-06-16 03:06:27 +00004089 PAGERTRACE5("JOURNAL %d page %d needSync=%d hash(%08x)\n",
4090 PAGERID(pPager), pPg->pgno, pPg->needSync, pager_pagehash(pPg));
danielk197707cb5602006-01-20 10:55:05 +00004091
drhfd131da2007-08-07 17:13:03 +00004092 /* An error has occured writing to the journal file. The
danielk197707cb5602006-01-20 10:55:05 +00004093 ** transaction will be rolled back by the layer above.
4094 */
danielk1977a0bf2652004-11-04 14:30:04 +00004095 if( rc!=SQLITE_OK ){
danielk1977a0bf2652004-11-04 14:30:04 +00004096 return rc;
4097 }
danielk197707cb5602006-01-20 10:55:05 +00004098
danielk1977a0bf2652004-11-04 14:30:04 +00004099 pPager->nRec++;
drhf5e7bb52008-02-18 14:47:33 +00004100 assert( pPager->pInJournal!=0 );
4101 sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00004102 pPg->needSync = !pPager->noSync;
4103 if( pPager->stmtInUse ){
drhf5e7bb52008-02-18 14:47:33 +00004104 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00004105 }
drhac69b052004-05-12 13:30:07 +00004106 }
danielk197713adf8a2004-06-03 16:08:41 +00004107 }else{
danielk1977a0bf2652004-11-04 14:30:04 +00004108 pPg->needSync = !pPager->journalStarted && !pPager->noSync;
drh4f0c5872007-03-26 22:05:01 +00004109 PAGERTRACE4("APPEND %d page %d needSync=%d\n",
danielk1977ef73ee92004-11-06 12:26:07 +00004110 PAGERID(pPager), pPg->pgno, pPg->needSync);
danielk1977a0bf2652004-11-04 14:30:04 +00004111 }
4112 if( pPg->needSync ){
4113 pPager->needSync = 1;
4114 }
4115 pPg->inJournal = 1;
4116 }
4117
4118 /* If the statement journal is open and the page is not in it,
4119 ** then write the current page to the statement journal. Note that
4120 ** the statement journal format differs from the standard journal format
4121 ** in that it omits the checksums and the header.
4122 */
danielk1977f35843b2007-04-07 15:03:17 +00004123 if( pPager->stmtInUse
4124 && !pageInStatement(pPg)
4125 && (int)pPg->pgno<=pPager->stmtSize
4126 ){
danielk1977a0bf2652004-11-04 14:30:04 +00004127 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
4128 if( MEMDB ){
4129 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4130 assert( pHist->pStmt==0 );
drh17435752007-08-16 04:30:38 +00004131 pHist->pStmt = sqlite3_malloc( pPager->pageSize );
danielk1977a0bf2652004-11-04 14:30:04 +00004132 if( pHist->pStmt ){
4133 memcpy(pHist->pStmt, PGHDR_TO_DATA(pPg), pPager->pageSize);
4134 }
drh4f0c5872007-03-26 22:05:01 +00004135 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk1977f35843b2007-04-07 15:03:17 +00004136 page_add_to_stmt_list(pPg);
danielk1977a0bf2652004-11-04 14:30:04 +00004137 }else{
danielk197762079062007-08-15 17:08:46 +00004138 i64 offset = pPager->stmtNRec*(4+pPager->pageSize);
drhbf4bca52007-09-06 22:19:14 +00004139 char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
4140 rc = write32bits(pPager->stfd, offset, pPg->pgno);
4141 if( rc==SQLITE_OK ){
4142 rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize, offset+4);
4143 }
drh4f0c5872007-03-26 22:05:01 +00004144 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
drhac69b052004-05-12 13:30:07 +00004145 if( rc!=SQLITE_OK ){
drhac69b052004-05-12 13:30:07 +00004146 return rc;
4147 }
danielk1977a0bf2652004-11-04 14:30:04 +00004148 pPager->stmtNRec++;
drhf5e7bb52008-02-18 14:47:33 +00004149 assert( pPager->pInStmt!=0 );
4150 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
drhdb48ee02003-01-16 13:42:43 +00004151 }
drhd9b02572001-04-15 00:37:09 +00004152 }
drhfa86c412002-02-02 15:01:15 +00004153 }
4154
4155 /* Update the database size and return.
4156 */
drh1aa2d8b2007-01-03 15:34:29 +00004157 assert( pPager->state>=PAGER_SHARED );
drh1ab43002002-01-14 09:28:19 +00004158 if( pPager->dbSize<(int)pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00004159 pPager->dbSize = pPg->pgno;
drhb7f91642004-10-31 02:22:47 +00004160 if( !MEMDB && pPager->dbSize==PENDING_BYTE/pPager->pageSize ){
drh1f595712004-06-15 01:40:29 +00004161 pPager->dbSize++;
4162 }
drh306dc212001-05-21 13:45:10 +00004163 }
drh69688d52001-04-14 16:38:23 +00004164 return rc;
drhed7c8552001-04-11 14:29:21 +00004165}
4166
4167/*
danielk19774099f6e2007-03-19 11:25:20 +00004168** This function is used to mark a data-page as writable. It uses
4169** pager_write() to open a journal file (if it is not already open)
4170** and write the page *pData to the journal.
4171**
4172** The difference between this function and pager_write() is that this
4173** function also deals with the special case where 2 or more pages
4174** fit on a single disk sector. In this case all co-resident pages
4175** must have been written to the journal file before returning.
4176*/
danielk19773b8a05f2007-03-19 17:44:26 +00004177int sqlite3PagerWrite(DbPage *pDbPage){
danielk19774099f6e2007-03-19 11:25:20 +00004178 int rc = SQLITE_OK;
4179
danielk19773b8a05f2007-03-19 17:44:26 +00004180 PgHdr *pPg = pDbPage;
danielk19774099f6e2007-03-19 11:25:20 +00004181 Pager *pPager = pPg->pPager;
4182 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
4183
drh86f8c192007-08-22 00:39:19 +00004184 pagerEnter(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00004185 if( !MEMDB && nPagePerSector>1 ){
4186 Pgno nPageCount; /* Total number of pages in database file */
4187 Pgno pg1; /* First page of the sector pPg is located on. */
4188 int nPage; /* Number of pages starting at pg1 to journal */
4189 int ii;
danielk1977dd97a492007-08-22 18:54:32 +00004190 int needSync = 0;
danielk19774099f6e2007-03-19 11:25:20 +00004191
4192 /* Set the doNotSync flag to 1. This is because we cannot allow a journal
4193 ** header to be written between the pages journaled by this function.
4194 */
4195 assert( pPager->doNotSync==0 );
4196 pPager->doNotSync = 1;
4197
4198 /* This trick assumes that both the page-size and sector-size are
4199 ** an integer power of 2. It sets variable pg1 to the identifier
4200 ** of the first page of the sector pPg is located on.
4201 */
4202 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
4203
danielk19773b8a05f2007-03-19 17:44:26 +00004204 nPageCount = sqlite3PagerPagecount(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00004205 if( pPg->pgno>nPageCount ){
4206 nPage = (pPg->pgno - pg1)+1;
4207 }else if( (pg1+nPagePerSector-1)>nPageCount ){
4208 nPage = nPageCount+1-pg1;
4209 }else{
4210 nPage = nPagePerSector;
4211 }
4212 assert(nPage>0);
4213 assert(pg1<=pPg->pgno);
4214 assert((pg1+nPage)>pPg->pgno);
4215
4216 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
4217 Pgno pg = pg1+ii;
danielk1977dd97a492007-08-22 18:54:32 +00004218 PgHdr *pPage;
drhf5e7bb52008-02-18 14:47:33 +00004219 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
danielk19774099f6e2007-03-19 11:25:20 +00004220 if( pg!=PAGER_MJ_PGNO(pPager) ){
danielk19773b8a05f2007-03-19 17:44:26 +00004221 rc = sqlite3PagerGet(pPager, pg, &pPage);
danielk19774099f6e2007-03-19 11:25:20 +00004222 if( rc==SQLITE_OK ){
4223 rc = pager_write(pPage);
danielk1977dd97a492007-08-22 18:54:32 +00004224 if( pPage->needSync ){
4225 needSync = 1;
4226 }
danielk19773b8a05f2007-03-19 17:44:26 +00004227 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00004228 }
4229 }
danielk1977dd97a492007-08-22 18:54:32 +00004230 }else if( (pPage = pager_lookup(pPager, pg)) ){
4231 if( pPage->needSync ){
4232 needSync = 1;
4233 }
danielk19774099f6e2007-03-19 11:25:20 +00004234 }
4235 }
4236
danielk1977dd97a492007-08-22 18:54:32 +00004237 /* If the PgHdr.needSync flag is set for any of the nPage pages
4238 ** starting at pg1, then it needs to be set for all of them. Because
4239 ** writing to any of these nPage pages may damage the others, the
4240 ** journal file must contain sync()ed copies of all of them
4241 ** before any of them can be written out to the database file.
4242 */
4243 if( needSync ){
4244 for(ii=0; ii<nPage && needSync; ii++){
4245 PgHdr *pPage = pager_lookup(pPager, pg1+ii);
4246 if( pPage ) pPage->needSync = 1;
4247 }
4248 assert(pPager->needSync);
4249 }
4250
danielk19774099f6e2007-03-19 11:25:20 +00004251 assert( pPager->doNotSync==1 );
4252 pPager->doNotSync = 0;
4253 }else{
danielk19773b8a05f2007-03-19 17:44:26 +00004254 rc = pager_write(pDbPage);
danielk19774099f6e2007-03-19 11:25:20 +00004255 }
drh86f8c192007-08-22 00:39:19 +00004256 pagerLeave(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00004257 return rc;
4258}
4259
4260/*
drhaacc5432002-01-06 17:07:40 +00004261** Return TRUE if the page given in the argument was previously passed
danielk19773b8a05f2007-03-19 17:44:26 +00004262** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
drh6019e162001-07-02 17:51:45 +00004263** to change the content of the page.
4264*/
danielk19777d3a6662006-01-23 16:21:05 +00004265#ifndef NDEBUG
danielk19773b8a05f2007-03-19 17:44:26 +00004266int sqlite3PagerIswriteable(DbPage *pPg){
drh6019e162001-07-02 17:51:45 +00004267 return pPg->dirty;
4268}
danielk19777d3a6662006-01-23 16:21:05 +00004269#endif
drh6019e162001-07-02 17:51:45 +00004270
danielk1977b84f96f2005-01-20 11:32:23 +00004271#ifndef SQLITE_OMIT_VACUUM
drh6019e162001-07-02 17:51:45 +00004272/*
drh001bbcb2003-03-19 03:14:00 +00004273** Replace the content of a single page with the information in the third
4274** argument.
4275*/
danielk19773b8a05f2007-03-19 17:44:26 +00004276int sqlite3PagerOverwrite(Pager *pPager, Pgno pgno, void *pData){
4277 PgHdr *pPg;
drh001bbcb2003-03-19 03:14:00 +00004278 int rc;
4279
drh86f8c192007-08-22 00:39:19 +00004280 pagerEnter(pPager);
danielk19773b8a05f2007-03-19 17:44:26 +00004281 rc = sqlite3PagerGet(pPager, pgno, &pPg);
drh001bbcb2003-03-19 03:14:00 +00004282 if( rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00004283 rc = sqlite3PagerWrite(pPg);
drh001bbcb2003-03-19 03:14:00 +00004284 if( rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00004285 memcpy(sqlite3PagerGetData(pPg), pData, pPager->pageSize);
drh001bbcb2003-03-19 03:14:00 +00004286 }
danielk19773b8a05f2007-03-19 17:44:26 +00004287 sqlite3PagerUnref(pPg);
drh001bbcb2003-03-19 03:14:00 +00004288 }
drh86f8c192007-08-22 00:39:19 +00004289 pagerLeave(pPager);
drh001bbcb2003-03-19 03:14:00 +00004290 return rc;
4291}
danielk1977b84f96f2005-01-20 11:32:23 +00004292#endif
drh001bbcb2003-03-19 03:14:00 +00004293
4294/*
drh30e58752002-03-02 20:41:57 +00004295** A call to this routine tells the pager that it is not necessary to
drh538f5702007-04-13 02:14:30 +00004296** write the information on page pPg back to the disk, even though
drh30e58752002-03-02 20:41:57 +00004297** that page might be marked as dirty.
4298**
4299** The overlying software layer calls this routine when all of the data
4300** on the given page is unused. The pager marks the page as clean so
4301** that it does not get written to disk.
4302**
4303** Tests show that this optimization, together with the
danielk19773b8a05f2007-03-19 17:44:26 +00004304** sqlite3PagerDontRollback() below, more than double the speed
drh30e58752002-03-02 20:41:57 +00004305** of large INSERT operations and quadruple the speed of large DELETEs.
drh8e298f92002-07-06 16:28:47 +00004306**
4307** When this routine is called, set the alwaysRollback flag to true.
danielk19773b8a05f2007-03-19 17:44:26 +00004308** Subsequent calls to sqlite3PagerDontRollback() for the same page
drh8e298f92002-07-06 16:28:47 +00004309** will thereafter be ignored. This is necessary to avoid a problem
4310** where a page with data is added to the freelist during one part of
4311** a transaction then removed from the freelist during a later part
4312** of the same transaction and reused for some other purpose. When it
4313** is first added to the freelist, this routine is called. When reused,
drh80e35f42007-03-30 14:06:34 +00004314** the sqlite3PagerDontRollback() routine is called. But because the
4315** page contains critical data, we still need to be sure it gets
4316** rolled back in spite of the sqlite3PagerDontRollback() call.
drh30e58752002-03-02 20:41:57 +00004317*/
drh538f5702007-04-13 02:14:30 +00004318void sqlite3PagerDontWrite(DbPage *pDbPage){
4319 PgHdr *pPg = pDbPage;
4320 Pager *pPager = pPg->pPager;
drh8e298f92002-07-06 16:28:47 +00004321
drhb7f91642004-10-31 02:22:47 +00004322 if( MEMDB ) return;
drh86f8c192007-08-22 00:39:19 +00004323 pagerEnter(pPager);
drh8e298f92002-07-06 16:28:47 +00004324 pPg->alwaysRollback = 1;
drh43617e92006-03-06 20:55:46 +00004325 if( pPg->dirty && !pPager->stmtInUse ){
drh1aa2d8b2007-01-03 15:34:29 +00004326 assert( pPager->state>=PAGER_SHARED );
drh8124a302002-06-25 14:43:57 +00004327 if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
4328 /* If this pages is the last page in the file and the file has grown
4329 ** during the current transaction, then do NOT mark the page as clean.
4330 ** When the database file grows, we must make sure that the last page
4331 ** gets written at least once so that the disk file will be the correct
4332 ** size. If you do not write this page and the size of the file
4333 ** on the disk ends up being too small, that can lead to database
4334 ** corruption during the next transaction.
4335 */
4336 }else{
drh538f5702007-04-13 02:14:30 +00004337 PAGERTRACE3("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager));
4338 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
drh605ad8f2006-05-03 23:34:05 +00004339 makeClean(pPg);
danielk19773c407372005-02-15 02:54:14 +00004340#ifdef SQLITE_CHECK_PAGES
4341 pPg->pageHash = pager_pagehash(pPg);
4342#endif
drh8124a302002-06-25 14:43:57 +00004343 }
drh30e58752002-03-02 20:41:57 +00004344 }
drh86f8c192007-08-22 00:39:19 +00004345 pagerLeave(pPager);
drh30e58752002-03-02 20:41:57 +00004346}
4347
4348/*
4349** A call to this routine tells the pager that if a rollback occurs,
4350** it is not necessary to restore the data on the given page. This
4351** means that the pager does not have to record the given page in the
4352** rollback journal.
drh538f5702007-04-13 02:14:30 +00004353**
4354** If we have not yet actually read the content of this page (if
4355** the PgHdr.needRead flag is set) then this routine acts as a promise
4356** that we will never need to read the page content in the future.
4357** so the needRead flag can be cleared at this point.
danielk1977a55e9352008-01-21 13:04:34 +00004358**
4359** This routine is only called from a single place in the sqlite btree
4360** code (when a leaf is removed from the free-list). This allows the
4361** following assumptions to be made about pPg:
4362**
4363** 1. PagerDontWrite() has been called on the page, OR
4364** PagerWrite() has not yet been called on the page.
4365**
4366** 2. The page existed when the transaction was started.
4367**
4368** Details: DontRollback() (this routine) is only called when a leaf is
4369** removed from the free list. DontWrite() is called whenever a page
4370** becomes a free-list leaf.
drh30e58752002-03-02 20:41:57 +00004371*/
danielk19773b8a05f2007-03-19 17:44:26 +00004372void sqlite3PagerDontRollback(DbPage *pPg){
drh30e58752002-03-02 20:41:57 +00004373 Pager *pPager = pPg->pPager;
4374
drh86f8c192007-08-22 00:39:19 +00004375 pagerEnter(pPager);
drhd3627af2006-12-18 18:34:51 +00004376 assert( pPager->state>=PAGER_RESERVED );
danielk1977a55e9352008-01-21 13:04:34 +00004377
4378 /* If the journal file is not open, or DontWrite() has been called on
4379 ** this page (DontWrite() sets the alwaysRollback flag), then this
4380 ** function is a no-op.
4381 */
4382 if( pPager->journalOpen==0 || pPg->alwaysRollback || pPager->alwaysRollback ){
danielk197787c29a92008-01-18 11:33:16 +00004383 pagerLeave(pPager);
4384 return;
4385 }
danielk1977a55e9352008-01-21 13:04:34 +00004386 assert( !MEMDB ); /* For a memdb, pPager->journalOpen is always 0 */
4387
4388 /* Check that PagerWrite() has not yet been called on this page, and
4389 ** that the page existed when the transaction started.
4390 */
4391 assert( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize );
4392
drhf5e7bb52008-02-18 14:47:33 +00004393 assert( pPager->pInJournal!=0 );
4394 sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
danielk1977a55e9352008-01-21 13:04:34 +00004395 pPg->inJournal = 1;
4396 pPg->needRead = 0;
4397 if( pPager->stmtInUse ){
4398 assert( pPager->stmtSize <= pPager->origDbSize );
drhf5e7bb52008-02-18 14:47:33 +00004399 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
drh30e58752002-03-02 20:41:57 +00004400 }
danielk1977a55e9352008-01-21 13:04:34 +00004401 PAGERTRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager));
4402 IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno))
drh86f8c192007-08-22 00:39:19 +00004403 pagerLeave(pPager);
drh30e58752002-03-02 20:41:57 +00004404}
4405
drhac69b052004-05-12 13:30:07 +00004406
drh30e58752002-03-02 20:41:57 +00004407/*
drh80e35f42007-03-30 14:06:34 +00004408** This routine is called to increment the database file change-counter,
4409** stored at byte 24 of the pager file.
4410*/
danielk1977c7b60172007-08-22 11:22:03 +00004411static int pager_incr_changecounter(Pager *pPager, int isDirect){
drh80e35f42007-03-30 14:06:34 +00004412 PgHdr *pPgHdr;
4413 u32 change_counter;
danielk1977c7b60172007-08-22 11:22:03 +00004414 int rc = SQLITE_OK;
drh80e35f42007-03-30 14:06:34 +00004415
4416 if( !pPager->changeCountDone ){
4417 /* Open page 1 of the file for writing. */
4418 rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
4419 if( rc!=SQLITE_OK ) return rc;
danielk1977c7b60172007-08-22 11:22:03 +00004420
4421 if( !isDirect ){
4422 rc = sqlite3PagerWrite(pPgHdr);
danielk1977ae72d982007-10-03 08:46:44 +00004423 if( rc!=SQLITE_OK ){
4424 sqlite3PagerUnref(pPgHdr);
4425 return rc;
4426 }
danielk1977c7b60172007-08-22 11:22:03 +00004427 }
4428
drh80e35f42007-03-30 14:06:34 +00004429 /* Increment the value just read and write it back to byte 24. */
drhb1003912007-07-20 00:33:36 +00004430 change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
drh80e35f42007-03-30 14:06:34 +00004431 change_counter++;
4432 put32bits(((char*)PGHDR_TO_DATA(pPgHdr))+24, change_counter);
danielk1977c7b60172007-08-22 11:22:03 +00004433
4434 if( isDirect && pPager->fd->pMethods ){
4435 const void *zBuf = PGHDR_TO_DATA(pPgHdr);
4436 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
4437 }
4438
drh80e35f42007-03-30 14:06:34 +00004439 /* Release the page reference. */
4440 sqlite3PagerUnref(pPgHdr);
4441 pPager->changeCountDone = 1;
4442 }
danielk1977c7b60172007-08-22 11:22:03 +00004443 return rc;
drh80e35f42007-03-30 14:06:34 +00004444}
4445
4446/*
4447** Sync the database file for the pager pPager. zMaster points to the name
4448** of a master journal file that should be written into the individual
4449** journal file. zMaster may be NULL, which is interpreted as no master
4450** journal (a single database transaction).
4451**
4452** This routine ensures that the journal is synced, all dirty pages written
4453** to the database file and the database file synced. The only thing that
4454** remains to commit the transaction is to delete the journal file (or
4455** master journal file if specified).
4456**
4457** Note that if zMaster==NULL, this does not overwrite a previous value
4458** passed to an sqlite3PagerCommitPhaseOne() call.
4459**
4460** If parameter nTrunc is non-zero, then the pager file is truncated to
4461** nTrunc pages (this is used by auto-vacuum databases).
4462*/
4463int sqlite3PagerCommitPhaseOne(Pager *pPager, const char *zMaster, Pgno nTrunc){
4464 int rc = SQLITE_OK;
4465
4466 PAGERTRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n",
4467 pPager->zFilename, zMaster, nTrunc);
drh86f8c192007-08-22 00:39:19 +00004468 pagerEnter(pPager);
drh80e35f42007-03-30 14:06:34 +00004469
4470 /* If this is an in-memory db, or no pages have been written to, or this
4471 ** function has already been called, it is a no-op.
4472 */
4473 if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){
4474 PgHdr *pPg;
drh80e35f42007-03-30 14:06:34 +00004475
danielk1977c7b60172007-08-22 11:22:03 +00004476#ifdef SQLITE_ENABLE_ATOMIC_WRITE
4477 /* The atomic-write optimization can be used if all of the
4478 ** following are true:
4479 **
4480 ** + The file-system supports the atomic-write property for
4481 ** blocks of size page-size, and
4482 ** + This commit is not part of a multi-file transaction, and
4483 ** + Exactly one page has been modified and store in the journal file.
4484 **
4485 ** If the optimization can be used, then the journal file will never
4486 ** be created for this transaction.
4487 */
danielk1977f55b8992007-08-24 08:15:53 +00004488 int useAtomicWrite = (
4489 !zMaster &&
4490 pPager->journalOff==jrnlBufferSize(pPager) &&
4491 nTrunc==0 &&
4492 (0==pPager->pDirty || 0==pPager->pDirty->pDirty)
4493 );
4494 if( useAtomicWrite ){
danielk1977c7b60172007-08-22 11:22:03 +00004495 /* Update the nRec field in the journal file. */
4496 int offset = pPager->journalHdr + sizeof(aJournalMagic);
4497 assert(pPager->nRec==1);
4498 rc = write32bits(pPager->jfd, offset, pPager->nRec);
4499
4500 /* Update the db file change counter. The following call will modify
4501 ** the in-memory representation of page 1 to include the updated
4502 ** change counter and then write page 1 directly to the database
4503 ** file. Because of the atomic-write property of the host file-system,
4504 ** this is safe.
4505 */
danielk1977ae72d982007-10-03 08:46:44 +00004506 if( rc==SQLITE_OK ){
4507 rc = pager_incr_changecounter(pPager, 1);
4508 }
danielk1977f55b8992007-08-24 08:15:53 +00004509 }else{
4510 rc = sqlite3JournalCreate(pPager->jfd);
danielk1977f55b8992007-08-24 08:15:53 +00004511 }
4512
danielk1977ae72d982007-10-03 08:46:44 +00004513 if( !useAtomicWrite && rc==SQLITE_OK )
danielk1977c7b60172007-08-22 11:22:03 +00004514#endif
4515
drh80e35f42007-03-30 14:06:34 +00004516 /* If a master journal file name has already been written to the
4517 ** journal file, then no sync is required. This happens when it is
4518 ** written, then the process fails to upgrade from a RESERVED to an
4519 ** EXCLUSIVE lock. The next time the process tries to commit the
4520 ** transaction the m-j name will have already been written.
4521 */
4522 if( !pPager->setMaster ){
danielk1977f55b8992007-08-24 08:15:53 +00004523 assert( pPager->journalOpen );
danielk1977c7b60172007-08-22 11:22:03 +00004524 rc = pager_incr_changecounter(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00004525 if( rc!=SQLITE_OK ) goto sync_exit;
4526#ifndef SQLITE_OMIT_AUTOVACUUM
4527 if( nTrunc!=0 ){
4528 /* If this transaction has made the database smaller, then all pages
4529 ** being discarded by the truncation must be written to the journal
4530 ** file.
4531 */
4532 Pgno i;
4533 int iSkip = PAGER_MJ_PGNO(pPager);
4534 for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){
drhf5e7bb52008-02-18 14:47:33 +00004535 if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
drh80e35f42007-03-30 14:06:34 +00004536 rc = sqlite3PagerGet(pPager, i, &pPg);
4537 if( rc!=SQLITE_OK ) goto sync_exit;
4538 rc = sqlite3PagerWrite(pPg);
4539 sqlite3PagerUnref(pPg);
4540 if( rc!=SQLITE_OK ) goto sync_exit;
4541 }
4542 }
4543 }
4544#endif
4545 rc = writeMasterJournal(pPager, zMaster);
4546 if( rc!=SQLITE_OK ) goto sync_exit;
4547 rc = syncJournal(pPager);
drh80e35f42007-03-30 14:06:34 +00004548 }
danielk1977c7b60172007-08-22 11:22:03 +00004549 if( rc!=SQLITE_OK ) goto sync_exit;
drh80e35f42007-03-30 14:06:34 +00004550
4551#ifndef SQLITE_OMIT_AUTOVACUUM
4552 if( nTrunc!=0 ){
4553 rc = sqlite3PagerTruncate(pPager, nTrunc);
4554 if( rc!=SQLITE_OK ) goto sync_exit;
4555 }
4556#endif
4557
4558 /* Write all dirty pages to the database file */
4559 pPg = pager_get_all_dirty_pages(pPager);
4560 rc = pager_write_pagelist(pPg);
drh153c62c2007-08-24 03:51:33 +00004561 if( rc!=SQLITE_OK ){
4562 while( pPg && !pPg->dirty ){ pPg = pPg->pDirty; }
4563 pPager->pDirty = pPg;
4564 goto sync_exit;
4565 }
drh3cdd7d32007-03-30 14:46:01 +00004566 pPager->pDirty = 0;
drh80e35f42007-03-30 14:06:34 +00004567
4568 /* Sync the database file. */
drh1cc8c442007-08-24 16:08:29 +00004569 if( !pPager->noSync ){
danielk1977f036aef2007-08-20 05:36:51 +00004570 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
drh80e35f42007-03-30 14:06:34 +00004571 }
4572 IOTRACE(("DBSYNC %p\n", pPager))
4573
4574 pPager->state = PAGER_SYNCED;
4575 }else if( MEMDB && nTrunc!=0 ){
4576 rc = sqlite3PagerTruncate(pPager, nTrunc);
4577 }
4578
4579sync_exit:
danielk1977e965ac72007-06-13 15:22:28 +00004580 if( rc==SQLITE_IOERR_BLOCKED ){
4581 /* pager_incr_changecounter() may attempt to obtain an exclusive
4582 * lock to spill the cache and return IOERR_BLOCKED. But since
drh85b623f2007-12-13 21:54:09 +00004583 * there is no chance the cache is inconsistent, it is
danielk1977e965ac72007-06-13 15:22:28 +00004584 * better to return SQLITE_BUSY.
4585 */
4586 rc = SQLITE_BUSY;
4587 }
drh86f8c192007-08-22 00:39:19 +00004588 pagerLeave(pPager);
drh80e35f42007-03-30 14:06:34 +00004589 return rc;
4590}
4591
4592
4593/*
drhed7c8552001-04-11 14:29:21 +00004594** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00004595**
4596** If the commit fails for any reason, a rollback attempt is made
4597** and an error code is returned. If the commit worked, SQLITE_OK
4598** is returned.
drhed7c8552001-04-11 14:29:21 +00004599*/
drh80e35f42007-03-30 14:06:34 +00004600int sqlite3PagerCommitPhaseTwo(Pager *pPager){
drha1b351a2001-09-14 16:42:12 +00004601 int rc;
drhed7c8552001-04-11 14:29:21 +00004602 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +00004603
danielk1977efaaf572006-01-16 11:29:19 +00004604 if( pPager->errCode ){
danielk19777f7bc662006-01-23 13:47:47 +00004605 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004606 }
drha6abd042004-06-09 17:37:22 +00004607 if( pPager->state<PAGER_RESERVED ){
drhd9b02572001-04-15 00:37:09 +00004608 return SQLITE_ERROR;
4609 }
drh86f8c192007-08-22 00:39:19 +00004610 pagerEnter(pPager);
drh4f0c5872007-03-26 22:05:01 +00004611 PAGERTRACE2("COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004612 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004613 pPg = pager_get_all_dirty_pages(pPager);
4614 while( pPg ){
danielk1977f35843b2007-04-07 15:03:17 +00004615 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4616 clearHistory(pHist);
drhac69b052004-05-12 13:30:07 +00004617 pPg->dirty = 0;
4618 pPg->inJournal = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004619 pHist->inStmt = 0;
drh5229ae42006-03-23 23:29:04 +00004620 pPg->needSync = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004621 pHist->pPrevStmt = pHist->pNextStmt = 0;
drhac69b052004-05-12 13:30:07 +00004622 pPg = pPg->pDirty;
4623 }
drh605ad8f2006-05-03 23:34:05 +00004624 pPager->pDirty = 0;
danielk1977eac7a362004-06-16 07:45:24 +00004625#ifndef NDEBUG
4626 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
4627 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4628 assert( !pPg->alwaysRollback );
4629 assert( !pHist->pOrig );
4630 assert( !pHist->pStmt );
4631 }
4632#endif
drhac69b052004-05-12 13:30:07 +00004633 pPager->pStmt = 0;
drha6abd042004-06-09 17:37:22 +00004634 pPager->state = PAGER_SHARED;
danielk197787c29a92008-01-18 11:33:16 +00004635 pagerLeave(pPager);
drhac69b052004-05-12 13:30:07 +00004636 return SQLITE_OK;
4637 }
drh3cdd7d32007-03-30 14:46:01 +00004638 assert( pPager->journalOpen || !pPager->dirtyCache );
4639 assert( pPager->state==PAGER_SYNCED || !pPager->dirtyCache );
4640 rc = pager_end_transaction(pPager);
drh86f8c192007-08-22 00:39:19 +00004641 rc = pager_error(pPager, rc);
4642 pagerLeave(pPager);
4643 return rc;
drhed7c8552001-04-11 14:29:21 +00004644}
4645
4646/*
drha6abd042004-06-09 17:37:22 +00004647** Rollback all changes. The database falls back to PAGER_SHARED mode.
drhed7c8552001-04-11 14:29:21 +00004648** All in-memory cache pages revert to their original data contents.
4649** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00004650**
4651** This routine cannot fail unless some other process is not following
drh4f0ee682007-03-30 20:43:40 +00004652** the correct locking protocol or unless some other
drhd9b02572001-04-15 00:37:09 +00004653** process is writing trash into the journal file (SQLITE_CORRUPT) or
4654** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
4655** codes are returned for all these occasions. Otherwise,
4656** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00004657*/
danielk19773b8a05f2007-03-19 17:44:26 +00004658int sqlite3PagerRollback(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00004659 int rc;
drh4f0c5872007-03-26 22:05:01 +00004660 PAGERTRACE2("ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004661 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004662 PgHdr *p;
4663 for(p=pPager->pAll; p; p=p->pNextAll){
4664 PgHistory *pHist;
danielk1977eac7a362004-06-16 07:45:24 +00004665 assert( !p->alwaysRollback );
4666 if( !p->dirty ){
4667 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pOrig );
4668 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pStmt );
4669 continue;
4670 }
4671
drhac69b052004-05-12 13:30:07 +00004672 pHist = PGHDR_TO_HIST(p, pPager);
4673 if( pHist->pOrig ){
4674 memcpy(PGHDR_TO_DATA(p), pHist->pOrig, pPager->pageSize);
drh4f0c5872007-03-26 22:05:01 +00004675 PAGERTRACE3("ROLLBACK-PAGE %d of %d\n", p->pgno, PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00004676 }else{
drh4f0c5872007-03-26 22:05:01 +00004677 PAGERTRACE3("PAGE %d is clean on %d\n", p->pgno, PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00004678 }
4679 clearHistory(pHist);
4680 p->dirty = 0;
4681 p->inJournal = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004682 pHist->inStmt = 0;
4683 pHist->pPrevStmt = pHist->pNextStmt = 0;
danielk1977369f27e2004-06-15 11:40:04 +00004684 if( pPager->xReiniter ){
danielk19773b8a05f2007-03-19 17:44:26 +00004685 pPager->xReiniter(p, pPager->pageSize);
danielk1977369f27e2004-06-15 11:40:04 +00004686 }
drhac69b052004-05-12 13:30:07 +00004687 }
drh605ad8f2006-05-03 23:34:05 +00004688 pPager->pDirty = 0;
drhac69b052004-05-12 13:30:07 +00004689 pPager->pStmt = 0;
4690 pPager->dbSize = pPager->origDbSize;
danielk1977e180dd92007-04-05 17:15:52 +00004691 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00004692 pPager->stmtInUse = 0;
drha6abd042004-06-09 17:37:22 +00004693 pPager->state = PAGER_SHARED;
drhac69b052004-05-12 13:30:07 +00004694 return SQLITE_OK;
4695 }
4696
drh86f8c192007-08-22 00:39:19 +00004697 pagerEnter(pPager);
drha6abd042004-06-09 17:37:22 +00004698 if( !pPager->dirtyCache || !pPager->journalOpen ){
drh80e35f42007-03-30 14:06:34 +00004699 rc = pager_end_transaction(pPager);
drh86f8c192007-08-22 00:39:19 +00004700 pagerLeave(pPager);
drhda47d772002-12-02 04:25:19 +00004701 return rc;
4702 }
drhdb48ee02003-01-16 13:42:43 +00004703
danielk1977efaaf572006-01-16 11:29:19 +00004704 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
drha6abd042004-06-09 17:37:22 +00004705 if( pPager->state>=PAGER_EXCLUSIVE ){
danielk1977e277be02007-03-23 18:12:06 +00004706 pager_playback(pPager, 0);
drh4b845d72002-03-05 12:41:19 +00004707 }
drh86f8c192007-08-22 00:39:19 +00004708 pagerLeave(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00004709 return pPager->errCode;
drhed7c8552001-04-11 14:29:21 +00004710 }
drha6abd042004-06-09 17:37:22 +00004711 if( pPager->state==PAGER_RESERVED ){
danielk197717221812005-02-15 03:38:05 +00004712 int rc2;
danielk1977e277be02007-03-23 18:12:06 +00004713 rc = pager_playback(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00004714 rc2 = pager_end_transaction(pPager);
drha6abd042004-06-09 17:37:22 +00004715 if( rc==SQLITE_OK ){
4716 rc = rc2;
4717 }
4718 }else{
danielk1977e277be02007-03-23 18:12:06 +00004719 rc = pager_playback(pPager, 0);
drhd9b02572001-04-15 00:37:09 +00004720 }
danielk1977e180dd92007-04-05 17:15:52 +00004721 /* pager_reset(pPager); */
drhd9b02572001-04-15 00:37:09 +00004722 pPager->dbSize = -1;
danielk197707cb5602006-01-20 10:55:05 +00004723
4724 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
4725 ** cache. So call pager_error() on the way out to make any error
4726 ** persistent.
4727 */
drh86f8c192007-08-22 00:39:19 +00004728 rc = pager_error(pPager, rc);
4729 pagerLeave(pPager);
4730 return rc;
drh98808ba2001-10-18 12:34:46 +00004731}
drhd9b02572001-04-15 00:37:09 +00004732
4733/*
drh5e00f6c2001-09-13 13:46:56 +00004734** Return TRUE if the database file is opened read-only. Return FALSE
4735** if the database is (in theory) writable.
4736*/
danielk19773b8a05f2007-03-19 17:44:26 +00004737int sqlite3PagerIsreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00004738 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00004739}
4740
4741/*
drh0f7eb612006-08-08 13:51:43 +00004742** Return the number of references to the pager.
4743*/
danielk19773b8a05f2007-03-19 17:44:26 +00004744int sqlite3PagerRefcount(Pager *pPager){
drh0f7eb612006-08-08 13:51:43 +00004745 return pPager->nRef;
4746}
4747
4748#ifdef SQLITE_TEST
4749/*
drhd9b02572001-04-15 00:37:09 +00004750** This routine is used for testing and analysis only.
4751*/
danielk19773b8a05f2007-03-19 17:44:26 +00004752int *sqlite3PagerStats(Pager *pPager){
danielk197742741be2005-01-08 12:42:39 +00004753 static int a[11];
drhd9b02572001-04-15 00:37:09 +00004754 a[0] = pPager->nRef;
4755 a[1] = pPager->nPage;
4756 a[2] = pPager->mxPage;
4757 a[3] = pPager->dbSize;
4758 a[4] = pPager->state;
danielk1977efaaf572006-01-16 11:29:19 +00004759 a[5] = pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004760 a[6] = pPager->nHit;
4761 a[7] = pPager->nMiss;
drh7c4ac0c2007-04-05 11:25:58 +00004762 a[8] = 0; /* Used to be pPager->nOvfl */
danielk197742741be2005-01-08 12:42:39 +00004763 a[9] = pPager->nRead;
4764 a[10] = pPager->nWrite;
drhd9b02572001-04-15 00:37:09 +00004765 return a;
4766}
drh0f7eb612006-08-08 13:51:43 +00004767#endif
drhdd793422001-06-28 01:54:48 +00004768
drhfa86c412002-02-02 15:01:15 +00004769/*
drhac69b052004-05-12 13:30:07 +00004770** Set the statement rollback point.
drhfa86c412002-02-02 15:01:15 +00004771**
4772** This routine should be called with the transaction journal already
drhac69b052004-05-12 13:30:07 +00004773** open. A new statement journal is created that can be used to rollback
drhaaab5722002-02-19 13:39:21 +00004774** changes of a single SQL command within a larger transaction.
drhfa86c412002-02-02 15:01:15 +00004775*/
drh86f8c192007-08-22 00:39:19 +00004776static int pagerStmtBegin(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00004777 int rc;
drhac69b052004-05-12 13:30:07 +00004778 assert( !pPager->stmtInUse );
drh1aa2d8b2007-01-03 15:34:29 +00004779 assert( pPager->state>=PAGER_SHARED );
drhdc3ff9c2004-08-18 02:10:15 +00004780 assert( pPager->dbSize>=0 );
drh4f0c5872007-03-26 22:05:01 +00004781 PAGERTRACE2("STMT-BEGIN %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004782 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004783 pPager->stmtInUse = 1;
4784 pPager->stmtSize = pPager->dbSize;
4785 return SQLITE_OK;
4786 }
drhda47d772002-12-02 04:25:19 +00004787 if( !pPager->journalOpen ){
drhac69b052004-05-12 13:30:07 +00004788 pPager->stmtAutoopen = 1;
drhda47d772002-12-02 04:25:19 +00004789 return SQLITE_OK;
4790 }
drhfa86c412002-02-02 15:01:15 +00004791 assert( pPager->journalOpen );
drhed138fb2007-08-22 22:04:37 +00004792 pagerLeave(pPager);
drhf5e7bb52008-02-18 14:47:33 +00004793 assert( pPager->pInStmt==0 );
4794 pPager->pInStmt = sqlite3BitvecCreate(pPager->dbSize);
drhed138fb2007-08-22 22:04:37 +00004795 pagerEnter(pPager);
drhf5e7bb52008-02-18 14:47:33 +00004796 if( pPager->pInStmt==0 ){
danielk1977261919c2005-12-06 12:52:59 +00004797 /* sqlite3OsLock(pPager->fd, SHARED_LOCK); */
drhfa86c412002-02-02 15:01:15 +00004798 return SQLITE_NOMEM;
4799 }
drh968af522003-02-11 14:55:40 +00004800#ifndef NDEBUG
drh054889e2005-11-30 03:20:31 +00004801 rc = sqlite3OsFileSize(pPager->jfd, &pPager->stmtJSize);
drhac69b052004-05-12 13:30:07 +00004802 if( rc ) goto stmt_begin_failed;
danielk197776572402004-06-25 02:38:54 +00004803 assert( pPager->stmtJSize == pPager->journalOff );
drh968af522003-02-11 14:55:40 +00004804#endif
danielk197776572402004-06-25 02:38:54 +00004805 pPager->stmtJSize = pPager->journalOff;
drhac69b052004-05-12 13:30:07 +00004806 pPager->stmtSize = pPager->dbSize;
danielk197776572402004-06-25 02:38:54 +00004807 pPager->stmtHdrOff = 0;
danielk197775edc162004-06-26 01:48:18 +00004808 pPager->stmtCksum = pPager->cksumInit;
drhac69b052004-05-12 13:30:07 +00004809 if( !pPager->stmtOpen ){
drh334b2992007-09-06 23:28:23 +00004810 rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->stfd, pPager->zStmtJrnl,
drh33f4e022007-09-03 15:19:34 +00004811 SQLITE_OPEN_SUBJOURNAL);
drh334b2992007-09-06 23:28:23 +00004812 if( rc ){
4813 goto stmt_begin_failed;
4814 }
drhac69b052004-05-12 13:30:07 +00004815 pPager->stmtOpen = 1;
4816 pPager->stmtNRec = 0;
drh0f892532002-05-30 12:27:03 +00004817 }
drhac69b052004-05-12 13:30:07 +00004818 pPager->stmtInUse = 1;
drhfa86c412002-02-02 15:01:15 +00004819 return SQLITE_OK;
4820
drhac69b052004-05-12 13:30:07 +00004821stmt_begin_failed:
drhf5e7bb52008-02-18 14:47:33 +00004822 if( pPager->pInStmt ){
4823 sqlite3BitvecDestroy(pPager->pInStmt);
4824 pPager->pInStmt = 0;
drhfa86c412002-02-02 15:01:15 +00004825 }
4826 return rc;
4827}
drh86f8c192007-08-22 00:39:19 +00004828int sqlite3PagerStmtBegin(Pager *pPager){
4829 int rc;
4830 pagerEnter(pPager);
4831 rc = pagerStmtBegin(pPager);
4832 pagerLeave(pPager);
4833 return rc;
4834}
drhfa86c412002-02-02 15:01:15 +00004835
4836/*
drhac69b052004-05-12 13:30:07 +00004837** Commit a statement.
drhfa86c412002-02-02 15:01:15 +00004838*/
danielk19773b8a05f2007-03-19 17:44:26 +00004839int sqlite3PagerStmtCommit(Pager *pPager){
drh86f8c192007-08-22 00:39:19 +00004840 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00004841 if( pPager->stmtInUse ){
drh03eb96a2002-11-10 23:32:56 +00004842 PgHdr *pPg, *pNext;
drh4f0c5872007-03-26 22:05:01 +00004843 PAGERTRACE2("STMT-COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004844 if( !MEMDB ){
drh054889e2005-11-30 03:20:31 +00004845 /* sqlite3OsTruncate(pPager->stfd, 0); */
drhf5e7bb52008-02-18 14:47:33 +00004846 sqlite3BitvecDestroy(pPager->pInStmt);
4847 pPager->pInStmt = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004848 }else{
4849 for(pPg=pPager->pStmt; pPg; pPg=pNext){
drhac69b052004-05-12 13:30:07 +00004850 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
danielk1977f35843b2007-04-07 15:03:17 +00004851 pNext = pHist->pNextStmt;
4852 assert( pHist->inStmt );
4853 pHist->inStmt = 0;
4854 pHist->pPrevStmt = pHist->pNextStmt = 0;
drh17435752007-08-16 04:30:38 +00004855 sqlite3_free(pHist->pStmt);
drhac69b052004-05-12 13:30:07 +00004856 pHist->pStmt = 0;
4857 }
4858 }
4859 pPager->stmtNRec = 0;
4860 pPager->stmtInUse = 0;
4861 pPager->pStmt = 0;
drh663fc632002-02-02 18:49:19 +00004862 }
drhac69b052004-05-12 13:30:07 +00004863 pPager->stmtAutoopen = 0;
drh86f8c192007-08-22 00:39:19 +00004864 pagerLeave(pPager);
drhfa86c412002-02-02 15:01:15 +00004865 return SQLITE_OK;
4866}
4867
4868/*
drhac69b052004-05-12 13:30:07 +00004869** Rollback a statement.
drhfa86c412002-02-02 15:01:15 +00004870*/
danielk19773b8a05f2007-03-19 17:44:26 +00004871int sqlite3PagerStmtRollback(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00004872 int rc;
drh86f8c192007-08-22 00:39:19 +00004873 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00004874 if( pPager->stmtInUse ){
drh4f0c5872007-03-26 22:05:01 +00004875 PAGERTRACE2("STMT-ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004876 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004877 PgHdr *pPg;
danielk1977f35843b2007-04-07 15:03:17 +00004878 PgHistory *pHist;
4879 for(pPg=pPager->pStmt; pPg; pPg=pHist->pNextStmt){
4880 pHist = PGHDR_TO_HIST(pPg, pPager);
drhac69b052004-05-12 13:30:07 +00004881 if( pHist->pStmt ){
4882 memcpy(PGHDR_TO_DATA(pPg), pHist->pStmt, pPager->pageSize);
drh17435752007-08-16 04:30:38 +00004883 sqlite3_free(pHist->pStmt);
drhac69b052004-05-12 13:30:07 +00004884 pHist->pStmt = 0;
4885 }
4886 }
4887 pPager->dbSize = pPager->stmtSize;
danielk1977e180dd92007-04-05 17:15:52 +00004888 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00004889 rc = SQLITE_OK;
4890 }else{
4891 rc = pager_stmt_playback(pPager);
4892 }
danielk19773b8a05f2007-03-19 17:44:26 +00004893 sqlite3PagerStmtCommit(pPager);
drh663fc632002-02-02 18:49:19 +00004894 }else{
4895 rc = SQLITE_OK;
4896 }
drhac69b052004-05-12 13:30:07 +00004897 pPager->stmtAutoopen = 0;
drh86f8c192007-08-22 00:39:19 +00004898 pagerLeave(pPager);
drhfa86c412002-02-02 15:01:15 +00004899 return rc;
4900}
4901
drh73509ee2003-04-06 20:44:45 +00004902/*
4903** Return the full pathname of the database file.
4904*/
danielk19773b8a05f2007-03-19 17:44:26 +00004905const char *sqlite3PagerFilename(Pager *pPager){
drh73509ee2003-04-06 20:44:45 +00004906 return pPager->zFilename;
4907}
4908
drhb20ea9d2004-02-09 01:20:36 +00004909/*
drhd0679ed2007-08-28 22:24:34 +00004910** Return the VFS structure for the pager.
4911*/
4912const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
4913 return pPager->pVfs;
4914}
4915
4916/*
drhcc6bb3e2007-08-31 16:11:35 +00004917** Return the file handle for the database file associated
4918** with the pager. This might return NULL if the file has
4919** not yet been opened.
4920*/
4921sqlite3_file *sqlite3PagerFile(Pager *pPager){
4922 return pPager->fd;
4923}
4924
4925/*
danielk19775865e3d2004-06-14 06:03:57 +00004926** Return the directory of the database file.
4927*/
danielk19773b8a05f2007-03-19 17:44:26 +00004928const char *sqlite3PagerDirname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004929 return pPager->zDirectory;
4930}
4931
4932/*
4933** Return the full pathname of the journal file.
4934*/
danielk19773b8a05f2007-03-19 17:44:26 +00004935const char *sqlite3PagerJournalname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004936 return pPager->zJournal;
4937}
4938
4939/*
drh2c8997b2005-08-27 16:36:48 +00004940** Return true if fsync() calls are disabled for this pager. Return FALSE
4941** if fsync()s are executed normally.
4942*/
danielk19773b8a05f2007-03-19 17:44:26 +00004943int sqlite3PagerNosync(Pager *pPager){
drh2c8997b2005-08-27 16:36:48 +00004944 return pPager->noSync;
4945}
4946
drh7c4ac0c2007-04-05 11:25:58 +00004947#ifdef SQLITE_HAS_CODEC
drh2c8997b2005-08-27 16:36:48 +00004948/*
drhb20ea9d2004-02-09 01:20:36 +00004949** Set the codec for this pager
4950*/
danielk19773b8a05f2007-03-19 17:44:26 +00004951void sqlite3PagerSetCodec(
drhb20ea9d2004-02-09 01:20:36 +00004952 Pager *pPager,
drhc001c582006-03-06 18:23:16 +00004953 void *(*xCodec)(void*,void*,Pgno,int),
drhb20ea9d2004-02-09 01:20:36 +00004954 void *pCodecArg
4955){
4956 pPager->xCodec = xCodec;
4957 pPager->pCodecArg = pCodecArg;
4958}
drh7c4ac0c2007-04-05 11:25:58 +00004959#endif
drhb20ea9d2004-02-09 01:20:36 +00004960
danielk1977687566d2004-11-02 12:56:41 +00004961#ifndef SQLITE_OMIT_AUTOVACUUM
4962/*
danielk197787c29a92008-01-18 11:33:16 +00004963** Move the page pPg to location pgno in the file.
danielk1977687566d2004-11-02 12:56:41 +00004964**
drh5e385312007-06-16 04:42:12 +00004965** There must be no references to the page previously located at
4966** pgno (which we call pPgOld) though that page is allowed to be
4967** in cache. If the page previous located at pgno is not already
4968** in the rollback journal, it is not put there by by this routine.
danielk1977687566d2004-11-02 12:56:41 +00004969**
drh5e385312007-06-16 04:42:12 +00004970** References to the page pPg remain valid. Updating any
4971** meta-data associated with pPg (i.e. data stored in the nExtra bytes
danielk1977687566d2004-11-02 12:56:41 +00004972** allocated along with the page) is the responsibility of the caller.
4973**
danielk19775fd057a2005-03-09 13:09:43 +00004974** A transaction must be active when this routine is called. It used to be
4975** required that a statement transaction was not active, but this restriction
4976** has been removed (CREATE INDEX needs to move a page when a statement
4977** transaction is active).
danielk1977687566d2004-11-02 12:56:41 +00004978*/
danielk19773b8a05f2007-03-19 17:44:26 +00004979int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno){
drh5e385312007-06-16 04:42:12 +00004980 PgHdr *pPgOld; /* The page being overwritten. */
danielk1977f5fdda82004-11-03 08:44:05 +00004981 int h;
danielk197794daf7f2004-11-08 09:26:09 +00004982 Pgno needSyncPgno = 0;
danielk1977687566d2004-11-02 12:56:41 +00004983
drh86f8c192007-08-22 00:39:19 +00004984 pagerEnter(pPager);
danielk1977687566d2004-11-02 12:56:41 +00004985 assert( pPg->nRef>0 );
4986
drh4f0c5872007-03-26 22:05:01 +00004987 PAGERTRACE5("MOVE %d page %d (needSync=%d) moves to %d\n",
danielk1977599fcba2004-11-08 07:13:13 +00004988 PAGERID(pPager), pPg->pgno, pPg->needSync, pgno);
drhb0603412007-02-28 04:47:26 +00004989 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
danielk1977ef73ee92004-11-06 12:26:07 +00004990
danielk1977b4626a32007-04-28 15:47:43 +00004991 pager_get_content(pPg);
danielk197794daf7f2004-11-08 09:26:09 +00004992 if( pPg->needSync ){
4993 needSyncPgno = pPg->pgno;
drh4f0aee42007-06-16 18:39:41 +00004994 assert( pPg->inJournal || (int)pgno>pPager->origDbSize );
danielk197794daf7f2004-11-08 09:26:09 +00004995 assert( pPg->dirty );
danielk1977ae825582004-11-23 09:06:55 +00004996 assert( pPager->needSync );
danielk197794daf7f2004-11-08 09:26:09 +00004997 }
4998
drh85b623f2007-12-13 21:54:09 +00004999 /* Unlink pPg from its hash-chain */
danielk1977f5fdda82004-11-03 08:44:05 +00005000 unlinkHashChain(pPager, pPg);
danielk1977687566d2004-11-02 12:56:41 +00005001
danielk1977ef73ee92004-11-06 12:26:07 +00005002 /* If the cache contains a page with page-number pgno, remove it
drh85b623f2007-12-13 21:54:09 +00005003 ** from its hash chain. Also, if the PgHdr.needSync was set for
danielk1977599fcba2004-11-08 07:13:13 +00005004 ** page pgno before the 'move' operation, it needs to be retained
5005 ** for the page moved there.
danielk1977f5fdda82004-11-03 08:44:05 +00005006 */
drh5e385312007-06-16 04:42:12 +00005007 pPg->needSync = 0;
danielk1977687566d2004-11-02 12:56:41 +00005008 pPgOld = pager_lookup(pPager, pgno);
5009 if( pPgOld ){
danielk1977f5fdda82004-11-03 08:44:05 +00005010 assert( pPgOld->nRef==0 );
5011 unlinkHashChain(pPager, pPgOld);
drh605ad8f2006-05-03 23:34:05 +00005012 makeClean(pPgOld);
drh5e385312007-06-16 04:42:12 +00005013 pPg->needSync = pPgOld->needSync;
5014 }else{
5015 pPg->needSync = 0;
5016 }
drhf5e7bb52008-02-18 14:47:33 +00005017 pPg->inJournal = sqlite3BitvecTest(pPager->pInJournal, pgno);
danielk1977687566d2004-11-02 12:56:41 +00005018
danielk1977f5fdda82004-11-03 08:44:05 +00005019 /* Change the page number for pPg and insert it into the new hash-chain. */
drh3765df42006-06-28 18:18:09 +00005020 assert( pgno!=0 );
danielk1977f5fdda82004-11-03 08:44:05 +00005021 pPg->pgno = pgno;
drh8ca0c722006-05-07 17:49:38 +00005022 h = pgno & (pPager->nHash-1);
danielk1977f5fdda82004-11-03 08:44:05 +00005023 if( pPager->aHash[h] ){
5024 assert( pPager->aHash[h]->pPrevHash==0 );
5025 pPager->aHash[h]->pPrevHash = pPg;
5026 }
5027 pPg->pNextHash = pPager->aHash[h];
5028 pPager->aHash[h] = pPg;
5029 pPg->pPrevHash = 0;
5030
drh605ad8f2006-05-03 23:34:05 +00005031 makeDirty(pPg);
danielk1977687566d2004-11-02 12:56:41 +00005032 pPager->dirtyCache = 1;
5033
danielk197794daf7f2004-11-08 09:26:09 +00005034 if( needSyncPgno ){
5035 /* If needSyncPgno is non-zero, then the journal file needs to be
5036 ** sync()ed before any data is written to database file page needSyncPgno.
5037 ** Currently, no such page exists in the page-cache and the
drhf5e7bb52008-02-18 14:47:33 +00005038 ** Pager.pInJournal bit has been set. This needs to be remedied by loading
danielk197794daf7f2004-11-08 09:26:09 +00005039 ** the page into the pager-cache and setting the PgHdr.needSync flag.
danielk1977ae825582004-11-23 09:06:55 +00005040 **
danielk1977a98d7b42008-01-18 13:42:54 +00005041 ** If the attempt to load the page into the page-cache fails, (due
drhf5e7bb52008-02-18 14:47:33 +00005042 ** to a malloc() or IO failure), clear the bit in the pInJournal[]
danielk1977a98d7b42008-01-18 13:42:54 +00005043 ** array. Otherwise, if the page is loaded and written again in
5044 ** this transaction, it may be written to the database file before
5045 ** it is synced into the journal file. This way, it may end up in
5046 ** the journal file twice, but that is not a problem.
5047 **
danielk19773b8a05f2007-03-19 17:44:26 +00005048 ** The sqlite3PagerGet() call may cause the journal to sync. So make
danielk1977ae825582004-11-23 09:06:55 +00005049 ** sure the Pager.needSync flag is set too.
danielk197794daf7f2004-11-08 09:26:09 +00005050 */
5051 int rc;
danielk19773b8a05f2007-03-19 17:44:26 +00005052 PgHdr *pPgHdr;
danielk1977ae825582004-11-23 09:06:55 +00005053 assert( pPager->needSync );
danielk19773b8a05f2007-03-19 17:44:26 +00005054 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
danielk197787c29a92008-01-18 11:33:16 +00005055 if( rc!=SQLITE_OK ){
drhf5e7bb52008-02-18 14:47:33 +00005056 if( pPager->pInJournal && (int)needSyncPgno<=pPager->origDbSize ){
5057 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno);
danielk1977a98d7b42008-01-18 13:42:54 +00005058 }
danielk197787c29a92008-01-18 11:33:16 +00005059 pagerLeave(pPager);
5060 return rc;
5061 }
danielk1977ae825582004-11-23 09:06:55 +00005062 pPager->needSync = 1;
danielk19773b8a05f2007-03-19 17:44:26 +00005063 pPgHdr->needSync = 1;
5064 pPgHdr->inJournal = 1;
5065 makeDirty(pPgHdr);
5066 sqlite3PagerUnref(pPgHdr);
danielk197794daf7f2004-11-08 09:26:09 +00005067 }
5068
drh86f8c192007-08-22 00:39:19 +00005069 pagerLeave(pPager);
danielk1977687566d2004-11-02 12:56:41 +00005070 return SQLITE_OK;
5071}
5072#endif
5073
danielk19773b8a05f2007-03-19 17:44:26 +00005074/*
5075** Return a pointer to the data for the specified page.
5076*/
5077void *sqlite3PagerGetData(DbPage *pPg){
5078 return PGHDR_TO_DATA(pPg);
5079}
5080
5081/*
5082** Return a pointer to the Pager.nExtra bytes of "extra" space
5083** allocated along with the specified page.
5084*/
5085void *sqlite3PagerGetExtra(DbPage *pPg){
5086 Pager *pPager = pPg->pPager;
5087 return (pPager?PGHDR_TO_EXTRA(pPg, pPager):0);
5088}
5089
danielk197741483462007-03-24 16:45:04 +00005090/*
5091** Get/set the locking-mode for this pager. Parameter eMode must be one
5092** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
5093** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
5094** the locking-mode is set to the value specified.
5095**
5096** The returned value is either PAGER_LOCKINGMODE_NORMAL or
5097** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
5098** locking-mode.
5099*/
5100int sqlite3PagerLockingMode(Pager *pPager, int eMode){
drh369339d2007-03-30 16:01:55 +00005101 assert( eMode==PAGER_LOCKINGMODE_QUERY
5102 || eMode==PAGER_LOCKINGMODE_NORMAL
5103 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
5104 assert( PAGER_LOCKINGMODE_QUERY<0 );
5105 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
5106 if( eMode>=0 && !pPager->tempFile ){
danielk197741483462007-03-24 16:45:04 +00005107 pPager->exclusiveMode = eMode;
5108 }
5109 return (int)pPager->exclusiveMode;
5110}
5111
drhd919fe12007-12-11 19:34:44 +00005112#ifdef SQLITE_TEST
drhdd793422001-06-28 01:54:48 +00005113/*
5114** Print a listing of all referenced pages and their ref count.
5115*/
danielk19773b8a05f2007-03-19 17:44:26 +00005116void sqlite3PagerRefdump(Pager *pPager){
drhdd793422001-06-28 01:54:48 +00005117 PgHdr *pPg;
5118 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
5119 if( pPg->nRef<=0 ) continue;
drhfe63d1c2004-09-08 20:13:04 +00005120 sqlite3DebugPrintf("PAGE %3d addr=%p nRef=%d\n",
5121 pPg->pgno, PGHDR_TO_DATA(pPg), pPg->nRef);
drhdd793422001-06-28 01:54:48 +00005122 }
5123}
5124#endif
drh2e66f0b2005-04-28 17:18:48 +00005125
5126#endif /* SQLITE_OMIT_DISKIO */