blob: 6200772939469862d0e9ff304277a52786508fe2 [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**
danielk1977a664f8e2008-04-22 14:31:48 +000021** @(#) $Id: pager.c,v 1.432 2008/04/22 14:31:48 danielk1977 Exp $
drhed7c8552001-04-11 14:29:21 +000022*/
drh2e66f0b2005-04-28 17:18:48 +000023#ifndef SQLITE_OMIT_DISKIO
drhd9b02572001-04-15 00:37:09 +000024#include "sqliteInt.h"
drhed7c8552001-04-11 14:29:21 +000025#include <assert.h>
drhd9b02572001-04-15 00:37:09 +000026#include <string.h>
drhed7c8552001-04-11 14:29:21 +000027
28/*
drhdb48ee02003-01-16 13:42:43 +000029** Macros for troubleshooting. Normally turned off
30*/
danielk1977466be562004-06-10 02:16:01 +000031#if 0
drhd3627af2006-12-18 18:34:51 +000032#define sqlite3DebugPrintf printf
drh4f0c5872007-03-26 22:05:01 +000033#define PAGERTRACE1(X) sqlite3DebugPrintf(X)
34#define PAGERTRACE2(X,Y) sqlite3DebugPrintf(X,Y)
35#define PAGERTRACE3(X,Y,Z) sqlite3DebugPrintf(X,Y,Z)
36#define PAGERTRACE4(X,Y,Z,W) sqlite3DebugPrintf(X,Y,Z,W)
37#define PAGERTRACE5(X,Y,Z,W,V) sqlite3DebugPrintf(X,Y,Z,W,V)
drhdb48ee02003-01-16 13:42:43 +000038#else
drh4f0c5872007-03-26 22:05:01 +000039#define PAGERTRACE1(X)
40#define PAGERTRACE2(X,Y)
41#define PAGERTRACE3(X,Y,Z)
42#define PAGERTRACE4(X,Y,Z,W)
43#define PAGERTRACE5(X,Y,Z,W,V)
drhdb48ee02003-01-16 13:42:43 +000044#endif
45
danielk1977599fcba2004-11-08 07:13:13 +000046/*
drh4f0c5872007-03-26 22:05:01 +000047** The following two macros are used within the PAGERTRACEX() macros above
drhd86959f2005-11-26 03:51:18 +000048** to print out file-descriptors.
danielk1977599fcba2004-11-08 07:13:13 +000049**
drh85b623f2007-12-13 21:54:09 +000050** PAGERID() takes a pointer to a Pager struct as its argument. The
danielk197762079062007-08-15 17:08:46 +000051** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
drh85b623f2007-12-13 21:54:09 +000052** struct as its argument.
danielk1977599fcba2004-11-08 07:13:13 +000053*/
drhc001c582006-03-06 18:23:16 +000054#define PAGERID(p) ((int)(p->fd))
55#define FILEHANDLEID(fd) ((int)fd)
drhdb48ee02003-01-16 13:42:43 +000056
57/*
drhed7c8552001-04-11 14:29:21 +000058** The page cache as a whole is always in one of the following
59** states:
60**
drha6abd042004-06-09 17:37:22 +000061** PAGER_UNLOCK The page cache is not currently reading or
drhed7c8552001-04-11 14:29:21 +000062** writing the database file. There is no
63** data held in memory. This is the initial
64** state.
65**
drha6abd042004-06-09 17:37:22 +000066** PAGER_SHARED The page cache is reading the database.
drhed7c8552001-04-11 14:29:21 +000067** Writing is not permitted. There can be
68** multiple readers accessing the same database
drh69688d52001-04-14 16:38:23 +000069** file at the same time.
drhed7c8552001-04-11 14:29:21 +000070**
drh726de592004-06-10 23:35:50 +000071** PAGER_RESERVED This process has reserved the database for writing
72** but has not yet made any changes. Only one process
73** at a time can reserve the database. The original
74** database file has not been modified so other
75** processes may still be reading the on-disk
drha6abd042004-06-09 17:37:22 +000076** database file.
77**
78** PAGER_EXCLUSIVE The page cache is writing the database.
drhed7c8552001-04-11 14:29:21 +000079** Access is exclusive. No other processes or
80** threads can be reading or writing while one
81** process is writing.
82**
danielk1977aa5ccdf2004-06-14 05:10:42 +000083** PAGER_SYNCED The pager moves to this state from PAGER_EXCLUSIVE
84** after all dirty pages have been written to the
85** database file and the file has been synced to
drh369339d2007-03-30 16:01:55 +000086** disk. All that remains to do is to remove or
87** truncate the journal file and the transaction
88** will be committed.
danielk1977aa5ccdf2004-06-14 05:10:42 +000089**
drha6abd042004-06-09 17:37:22 +000090** The page cache comes up in PAGER_UNLOCK. The first time a
danielk19773b8a05f2007-03-19 17:44:26 +000091** sqlite3PagerGet() occurs, the state transitions to PAGER_SHARED.
drhed7c8552001-04-11 14:29:21 +000092** After all pages have been released using sqlite_page_unref(),
drha6abd042004-06-09 17:37:22 +000093** the state transitions back to PAGER_UNLOCK. The first time
danielk19773b8a05f2007-03-19 17:44:26 +000094** that sqlite3PagerWrite() is called, the state transitions to
drh369339d2007-03-30 16:01:55 +000095** PAGER_RESERVED. (Note that sqlite3PagerWrite() can only be
drh306dc212001-05-21 13:45:10 +000096** called on an outstanding page which means that the pager must
drha6abd042004-06-09 17:37:22 +000097** be in PAGER_SHARED before it transitions to PAGER_RESERVED.)
drh369339d2007-03-30 16:01:55 +000098** PAGER_RESERVED means that there is an open rollback journal.
99** The transition to PAGER_EXCLUSIVE occurs before any changes
100** are made to the database file, though writes to the rollback
101** journal occurs with just PAGER_RESERVED. After an sqlite3PagerRollback()
102** or sqlite3PagerCommitPhaseTwo(), the state can go back to PAGER_SHARED,
103** or it can stay at PAGER_EXCLUSIVE if we are in exclusive access mode.
drhed7c8552001-04-11 14:29:21 +0000104*/
drha6abd042004-06-09 17:37:22 +0000105#define PAGER_UNLOCK 0
drh684917c2004-10-05 02:41:42 +0000106#define PAGER_SHARED 1 /* same as SHARED_LOCK */
107#define PAGER_RESERVED 2 /* same as RESERVED_LOCK */
108#define PAGER_EXCLUSIVE 4 /* same as EXCLUSIVE_LOCK */
109#define PAGER_SYNCED 5
drhed7c8552001-04-11 14:29:21 +0000110
drh684917c2004-10-05 02:41:42 +0000111/*
112** If the SQLITE_BUSY_RESERVED_LOCK macro is set to true at compile-time,
113** then failed attempts to get a reserved lock will invoke the busy callback.
114** This is off by default. To see why, consider the following scenario:
115**
116** Suppose thread A already has a shared lock and wants a reserved lock.
117** Thread B already has a reserved lock and wants an exclusive lock. If
118** both threads are using their busy callbacks, it might be a long time
119** be for one of the threads give up and allows the other to proceed.
120** But if the thread trying to get the reserved lock gives up quickly
121** (if it never invokes its busy callback) then the contention will be
122** resolved quickly.
123*/
124#ifndef SQLITE_BUSY_RESERVED_LOCK
125# define SQLITE_BUSY_RESERVED_LOCK 0
126#endif
drhd9b02572001-04-15 00:37:09 +0000127
drhed7c8552001-04-11 14:29:21 +0000128/*
drh887dc4c2004-10-22 16:22:57 +0000129** This macro rounds values up so that if the value is an address it
130** is guaranteed to be an address that is aligned to an 8-byte boundary.
131*/
132#define FORCE_ALIGNMENT(X) (((X)+7)&~7)
133
danielk19779f61c2f2007-08-27 17:27:49 +0000134typedef struct PgHdr PgHdr;
danielk197784f786f2007-08-28 08:00:17 +0000135
136/*
137** Each pager stores all currently unreferenced pages in a list sorted
138** in least-recently-used (LRU) order (i.e. the first item on the list has
139** not been referenced in a long time, the last item has been recently
140** used). An instance of this structure is included as part of each
141** pager structure for this purpose (variable Pager.lru).
142**
143** Additionally, if memory-management is enabled, all unreferenced pages
144** are stored in a global LRU list (global variable sqlite3LruPageList).
145**
146** In both cases, the PagerLruList.pFirstSynced variable points to
147** the first page in the corresponding list that does not require an
drh85b623f2007-12-13 21:54:09 +0000148** fsync() operation before its memory can be reclaimed. If no such
danielk197784f786f2007-08-28 08:00:17 +0000149** page exists, PagerLruList.pFirstSynced is set to NULL.
150*/
151typedef struct PagerLruList PagerLruList;
152struct PagerLruList {
153 PgHdr *pFirst; /* First page in LRU list */
154 PgHdr *pLast; /* Last page in LRU list (the most recently used) */
155 PgHdr *pFirstSynced; /* First page in list with PgHdr.needSync==0 */
156};
157
158/*
159** The following structure contains the next and previous pointers used
160** to link a PgHdr structure into a PagerLruList linked list.
161*/
danielk19779f61c2f2007-08-27 17:27:49 +0000162typedef struct PagerLruLink PagerLruLink;
163struct PagerLruLink {
164 PgHdr *pNext;
165 PgHdr *pPrev;
166};
167
drh887dc4c2004-10-22 16:22:57 +0000168/*
drhed7c8552001-04-11 14:29:21 +0000169** Each in-memory image of a page begins with the following header.
drhbd03cae2001-06-02 02:40:57 +0000170** This header is only visible to this pager module. The client
171** code that calls pager sees only the data that follows the header.
drhf6038712004-02-08 18:07:34 +0000172**
danielk19773b8a05f2007-03-19 17:44:26 +0000173** Client code should call sqlite3PagerWrite() on a page prior to making
174** any modifications to that page. The first time sqlite3PagerWrite()
drhf6038712004-02-08 18:07:34 +0000175** is called, the original page contents are written into the rollback
176** journal and PgHdr.inJournal and PgHdr.needSync are set. Later, once
177** the journal page has made it onto the disk surface, PgHdr.needSync
178** is cleared. The modified page cannot be written back into the original
179** database file until the journal pages has been synced to disk and the
180** PgHdr.needSync has been cleared.
181**
danielk19773b8a05f2007-03-19 17:44:26 +0000182** The PgHdr.dirty flag is set when sqlite3PagerWrite() is called and
drhf6038712004-02-08 18:07:34 +0000183** is cleared again when the page content is written back to the original
184** database file.
drh732c8172007-06-16 11:17:45 +0000185**
186** Details of important structure elements:
187**
188** needSync
189**
190** If this is true, this means that it is not safe to write the page
191** content to the database because the original content needed
192** for rollback has not by synced to the main rollback journal.
193** The original content may have been written to the rollback journal
194** but it has not yet been synced. So we cannot write to the database
195** file because power failure might cause the page in the journal file
196** to never reach the disk. It is as if the write to the journal file
197** does not occur until the journal file is synced.
198**
199** This flag is false if the page content exactly matches what
200** currently exists in the database file. The needSync flag is also
201** false if the original content has been written to the main rollback
202** journal and synced. If the page represents a new page that has
203** been added onto the end of the database during the current
204** transaction, the needSync flag is true until the original database
205** size in the journal header has been synced to disk.
206**
207** inJournal
208**
209** This is true if the original page has been written into the main
210** rollback journal. This is always false for new pages added to
211** the end of the database file during the current transaction.
212** And this flag says nothing about whether or not the journal
213** has been synced to disk. For pages that are in the original
214** database file, the following expression should always be true:
215**
drhf5e7bb52008-02-18 14:47:33 +0000216** inJournal = sqlite3BitvecTest(pPager->pInJournal, pgno)
drh732c8172007-06-16 11:17:45 +0000217**
drhf5e7bb52008-02-18 14:47:33 +0000218** The pPager->pInJournal object is only valid for the original
drh732c8172007-06-16 11:17:45 +0000219** pages of the database, not new pages that are added to the end
220** of the database, so obviously the above expression cannot be
221** valid for new pages. For new pages inJournal is always 0.
222**
223** dirty
224**
225** When true, this means that the content of the page has been
226** modified and needs to be written back to the database file.
227** If false, it means that either the content of the page is
228** unchanged or else the content is unimportant and we do not
229** care whether or not it is preserved.
230**
231** alwaysRollback
232**
233** This means that the sqlite3PagerDontRollback() API should be
234** ignored for this page. The DontRollback() API attempts to say
235** that the content of the page on disk is unimportant (it is an
236** unused page on the freelist) so that it is unnecessary to
237** rollback changes to this page because the content of the page
238** can change without changing the meaning of the database. This
239** flag overrides any DontRollback() attempt. This flag is set
240** when a page that originally contained valid data is added to
241** the freelist. Later in the same transaction, this page might
242** be pulled from the freelist and reused for something different
243** and at that point the DontRollback() API will be called because
244** pages taken from the freelist do not need to be protected by
245** the rollback journal. But this flag says that the page was
246** not originally part of the freelist so that it still needs to
247** be rolled back in spite of any subsequent DontRollback() calls.
248**
249** needRead
250**
251** This flag means (when true) that the content of the page has
252** not yet been loaded from disk. The in-memory content is just
253** garbage. (Actually, we zero the content, but you should not
254** make any assumptions about the content nevertheless.) If the
255** content is needed in the future, it should be read from the
256** original database file.
drhed7c8552001-04-11 14:29:21 +0000257*/
258struct PgHdr {
259 Pager *pPager; /* The pager to which this page belongs */
260 Pgno pgno; /* The page number for this page */
drh69688d52001-04-14 16:38:23 +0000261 PgHdr *pNextHash, *pPrevHash; /* Hash collision chain for PgHdr.pgno */
danielk19779f61c2f2007-08-27 17:27:49 +0000262 PagerLruLink free; /* Next and previous free pages */
drhac69b052004-05-12 13:30:07 +0000263 PgHdr *pNextAll; /* A list of all pages */
drh193a6b42002-07-07 16:52:46 +0000264 u8 inJournal; /* TRUE if has been written to journal */
drh193a6b42002-07-07 16:52:46 +0000265 u8 dirty; /* TRUE if we need to write back changes */
drhdb48ee02003-01-16 13:42:43 +0000266 u8 needSync; /* Sync journal before writing this page */
drh80e35f42007-03-30 14:06:34 +0000267 u8 alwaysRollback; /* Disable DontRollback() for this page */
drh538f5702007-04-13 02:14:30 +0000268 u8 needRead; /* Read content if PagerWrite() is called */
drhac69b052004-05-12 13:30:07 +0000269 short int nRef; /* Number of users of this page */
drhdc3e10b2006-06-15 14:31:06 +0000270 PgHdr *pDirty, *pPrevDirty; /* Dirty pages */
danielk19779f61c2f2007-08-27 17:27:49 +0000271#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
272 PagerLruLink gfree; /* Global list of nRef==0 pages */
273#endif
danielk19773c407372005-02-15 02:54:14 +0000274#ifdef SQLITE_CHECK_PAGES
275 u32 pageHash;
276#endif
drhbf4bca52007-09-06 22:19:14 +0000277 void *pData; /* Page data */
278 /* Pager.nExtra bytes of local data appended to this header */
drhed7c8552001-04-11 14:29:21 +0000279};
280
drhac69b052004-05-12 13:30:07 +0000281/*
282** For an in-memory only database, some extra information is recorded about
283** each page so that changes can be rolled back. (Journal files are not
284** used for in-memory databases.) The following information is added to
285** the end of every EXTRA block for in-memory databases.
286**
287** This information could have been added directly to the PgHdr structure.
288** But then it would take up an extra 8 bytes of storage on every PgHdr
289** even for disk-based databases. Splitting it out saves 8 bytes. This
290** is only a savings of 0.8% but those percentages add up.
291*/
292typedef struct PgHistory PgHistory;
293struct PgHistory {
294 u8 *pOrig; /* Original page text. Restore to this on a full rollback */
295 u8 *pStmt; /* Text as it was at the beginning of the current statement */
danielk1977f35843b2007-04-07 15:03:17 +0000296 PgHdr *pNextStmt, *pPrevStmt; /* List of pages in the statement journal */
297 u8 inStmt; /* TRUE if in the statement subjournal */
drhac69b052004-05-12 13:30:07 +0000298};
drh9eb9e262004-02-11 02:18:05 +0000299
300/*
301** A macro used for invoking the codec if there is one
302*/
303#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000304# define CODEC1(P,D,N,X) if( P->xCodec!=0 ){ P->xCodec(P->pCodecArg,D,N,X); }
305# define CODEC2(P,D,N,X) ((char*)(P->xCodec!=0?P->xCodec(P->pCodecArg,D,N,X):D))
drh9eb9e262004-02-11 02:18:05 +0000306#else
drhc001c582006-03-06 18:23:16 +0000307# define CODEC1(P,D,N,X) /* NO-OP */
308# define CODEC2(P,D,N,X) ((char*)D)
drh9eb9e262004-02-11 02:18:05 +0000309#endif
310
drhed7c8552001-04-11 14:29:21 +0000311/*
drh69688d52001-04-14 16:38:23 +0000312** Convert a pointer to a PgHdr into a pointer to its data
313** and back again.
drhed7c8552001-04-11 14:29:21 +0000314*/
drhbf4bca52007-09-06 22:19:14 +0000315#define PGHDR_TO_DATA(P) ((P)->pData)
316#define PGHDR_TO_EXTRA(G,P) ((void*)&((G)[1]))
drhac69b052004-05-12 13:30:07 +0000317#define PGHDR_TO_HIST(P,PGR) \
drhbf4bca52007-09-06 22:19:14 +0000318 ((PgHistory*)&((char*)(&(P)[1]))[(PGR)->nExtra])
drhed7c8552001-04-11 14:29:21 +0000319
320/*
drhed7c8552001-04-11 14:29:21 +0000321** A open page cache is an instance of the following structure.
danielk1977efaaf572006-01-16 11:29:19 +0000322**
drh4f0ee682007-03-30 20:43:40 +0000323** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or
danielk1977efaaf572006-01-16 11:29:19 +0000324** or SQLITE_FULL. Once one of the first three errors occurs, it persists
325** and is returned as the result of every major pager API call. The
326** SQLITE_FULL return code is slightly different. It persists only until the
327** next successful rollback is performed on the pager cache. Also,
danielk19773b8a05f2007-03-19 17:44:26 +0000328** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup()
danielk1977efaaf572006-01-16 11:29:19 +0000329** APIs, they may still be used successfully.
drhed7c8552001-04-11 14:29:21 +0000330*/
331struct Pager {
danielk1977b4b47412007-08-17 15:53:36 +0000332 sqlite3_vfs *pVfs; /* OS functions to use for IO */
drh603240c2002-03-05 01:11:12 +0000333 u8 journalOpen; /* True if journal file descriptors is valid */
drh34e79ce2004-02-08 06:05:46 +0000334 u8 journalStarted; /* True if header of journal is synced */
335 u8 useJournal; /* Use a rollback journal on this file */
drh7bec5052005-02-06 02:45:41 +0000336 u8 noReadlock; /* Do not bother to obtain readlocks */
drhac69b052004-05-12 13:30:07 +0000337 u8 stmtOpen; /* True if the statement subjournal is open */
338 u8 stmtInUse; /* True we are in a statement subtransaction */
339 u8 stmtAutoopen; /* Open stmt journal when main journal is opened*/
drh603240c2002-03-05 01:11:12 +0000340 u8 noSync; /* Do not sync the journal if true */
drh968af522003-02-11 14:55:40 +0000341 u8 fullSync; /* Do extra syncs of the journal for robustness */
danielk1977f036aef2007-08-20 05:36:51 +0000342 u8 sync_flags; /* One of SYNC_NORMAL or SYNC_FULL */
drha6abd042004-06-09 17:37:22 +0000343 u8 state; /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */
drh603240c2002-03-05 01:11:12 +0000344 u8 tempFile; /* zFilename is a temporary file */
345 u8 readOnly; /* True for a read-only database */
346 u8 needSync; /* True if an fsync() is needed on the journal */
drha6abd042004-06-09 17:37:22 +0000347 u8 dirtyCache; /* True if cached pages have changed */
drh80e35f42007-03-30 14:06:34 +0000348 u8 alwaysRollback; /* Disable DontRollback() for all pages */
drhac69b052004-05-12 13:30:07 +0000349 u8 memDb; /* True to inhibit all file I/O */
drh6d156e42005-05-20 20:11:20 +0000350 u8 setMaster; /* True if a m-j name has been written to jrnl */
drh80e35f42007-03-30 14:06:34 +0000351 u8 doNotSync; /* Boolean. While true, do not spill the cache */
352 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
drhfdc40e92008-04-17 20:59:37 +0000353 u8 journalMode; /* On of the PAGER_JOURNALMODE_* values */
drh80e35f42007-03-30 14:06:34 +0000354 u8 changeCountDone; /* Set after incrementing the change-counter */
drh33f4e022007-09-03 15:19:34 +0000355 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
drhe49f9822006-09-15 12:29:16 +0000356 int errCode; /* One of several kinds of errors */
drhfcd35c72005-05-21 02:48:08 +0000357 int dbSize; /* Number of pages in the file */
358 int origDbSize; /* dbSize before the current change */
359 int stmtSize; /* Size of database (in pages) at stmt_begin() */
360 int nRec; /* Number of pages written to the journal */
361 u32 cksumInit; /* Quasi-random value added to every checksum */
362 int stmtNRec; /* Number of records in stmt subjournal */
363 int nExtra; /* Add this many bytes to each in-memory page */
364 int pageSize; /* Number of bytes in a page */
365 int nPage; /* Total number of in-memory pages */
drhfcd35c72005-05-21 02:48:08 +0000366 int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */
367 int mxPage; /* Maximum number of pages to hold in cache */
drhf8e632b2007-05-08 14:51:36 +0000368 Pgno mxPgno; /* Maximum allowed size of the database */
drhf5e7bb52008-02-18 14:47:33 +0000369 Bitvec *pInJournal; /* One bit for each page in the database file */
370 Bitvec *pInStmt; /* One bit for each page in the database */
drhfcd35c72005-05-21 02:48:08 +0000371 char *zFilename; /* Name of the database file */
372 char *zJournal; /* Name of the journal file */
373 char *zDirectory; /* Directory hold database and journal files */
drh334b2992007-09-06 23:28:23 +0000374 char *zStmtJrnl; /* Name of the statement journal file */
danielk197762079062007-08-15 17:08:46 +0000375 sqlite3_file *fd, *jfd; /* File descriptors for database and journal */
376 sqlite3_file *stfd; /* File descriptor for the statement subjournal*/
drh726de592004-06-10 23:35:50 +0000377 BusyHandler *pBusyHandler; /* Pointer to sqlite.busyHandler */
danielk19779f61c2f2007-08-27 17:27:49 +0000378 PagerLruList lru; /* LRU list of free pages */
drhd9b02572001-04-15 00:37:09 +0000379 PgHdr *pAll; /* List of all pages */
drhac69b052004-05-12 13:30:07 +0000380 PgHdr *pStmt; /* List of pages in the statement subjournal */
drh605ad8f2006-05-03 23:34:05 +0000381 PgHdr *pDirty; /* List of all dirty pages */
drheb206252004-10-01 02:00:31 +0000382 i64 journalOff; /* Current byte offset in the journal file */
383 i64 journalHdr; /* Byte offset to previous journal header */
384 i64 stmtHdrOff; /* First journal header written this statement */
385 i64 stmtCksum; /* cksumInit when statement was started */
drh6d156e42005-05-20 20:11:20 +0000386 i64 stmtJSize; /* Size of journal at stmt_begin() */
danielk197776572402004-06-25 02:38:54 +0000387 int sectorSize; /* Assumed sector size during rollback */
drhfcd35c72005-05-21 02:48:08 +0000388#ifdef SQLITE_TEST
drh7c4ac0c2007-04-05 11:25:58 +0000389 int nHit, nMiss; /* Cache hits and missing */
390 int nRead, nWrite; /* Database pages read/written */
drhfcd35c72005-05-21 02:48:08 +0000391#endif
danielk19773b8a05f2007-03-19 17:44:26 +0000392 void (*xDestructor)(DbPage*,int); /* Call this routine when freeing pages */
393 void (*xReiniter)(DbPage*,int); /* Call this routine when reloading pages */
drh7c4ac0c2007-04-05 11:25:58 +0000394#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000395 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
drh6d156e42005-05-20 20:11:20 +0000396 void *pCodecArg; /* First argument to xCodec() */
drh7c4ac0c2007-04-05 11:25:58 +0000397#endif
drh8ca0c722006-05-07 17:49:38 +0000398 int nHash; /* Size of the pager hash table */
399 PgHdr **aHash; /* Hash table to map page number to PgHdr */
drh6f7adc82006-01-11 21:41:20 +0000400#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +0000401 Pager *pNext; /* Doubly linked list of pagers on which */
402 Pager *pPrev; /* sqlite3_release_memory() will work */
403 int iInUseMM; /* Non-zero if unavailable to MM */
404 int iInUseDB; /* Non-zero if in sqlite3_release_memory() */
danielk197713f72992005-12-18 08:51:22 +0000405#endif
danielk19778186df82007-03-06 13:45:59 +0000406 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
drh86a88112007-04-16 15:02:19 +0000407 char dbFileVers[16]; /* Changes whenever database file changes */
drhd9b02572001-04-15 00:37:09 +0000408};
409
410/*
drh538f5702007-04-13 02:14:30 +0000411** The following global variables hold counters used for
412** testing purposes only. These variables do not exist in
413** a non-testing build. These variables are not thread-safe.
drhfcd35c72005-05-21 02:48:08 +0000414*/
415#ifdef SQLITE_TEST
drh538f5702007-04-13 02:14:30 +0000416int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
417int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
418int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
419int sqlite3_pager_pgfree_count = 0; /* Number of cache pages freed */
420# define PAGER_INCR(v) v++
drhfcd35c72005-05-21 02:48:08 +0000421#else
drh538f5702007-04-13 02:14:30 +0000422# define PAGER_INCR(v)
drhfcd35c72005-05-21 02:48:08 +0000423#endif
424
drh86f8c192007-08-22 00:39:19 +0000425/*
426** The following variable points to the head of a double-linked list
427** of all pagers that are eligible for page stealing by the
428** sqlite3_release_memory() interface. Access to this list is
429** protected by the SQLITE_MUTEX_STATIC_MEM2 mutex.
430*/
431#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
432static Pager *sqlite3PagerList = 0;
danielk19779f61c2f2007-08-27 17:27:49 +0000433static PagerLruList sqlite3LruPageList = {0, 0, 0};
drh86f8c192007-08-22 00:39:19 +0000434#endif
drh538f5702007-04-13 02:14:30 +0000435
436
drhfcd35c72005-05-21 02:48:08 +0000437/*
drh5e00f6c2001-09-13 13:46:56 +0000438** Journal files begin with the following magic string. The data
439** was obtained from /dev/random. It is used only as a sanity check.
drh94f33312002-08-12 12:29:56 +0000440**
drhae2b40c2004-06-09 19:03:54 +0000441** Since version 2.8.0, the journal format contains additional sanity
442** checking information. If the power fails while the journal is begin
443** written, semi-random garbage data might appear in the journal
444** file after power is restored. If an attempt is then made
drh968af522003-02-11 14:55:40 +0000445** to roll the journal back, the database could be corrupted. The additional
446** sanity checking data is an attempt to discover the garbage in the
447** journal and ignore it.
448**
drhae2b40c2004-06-09 19:03:54 +0000449** The sanity checking information for the new journal format consists
drh968af522003-02-11 14:55:40 +0000450** of a 32-bit checksum on each page of data. The checksum covers both
drh90f5ecb2004-07-22 01:19:35 +0000451** the page number and the pPager->pageSize bytes of data for the page.
drh968af522003-02-11 14:55:40 +0000452** This cksum is initialized to a 32-bit random value that appears in the
453** journal file right after the header. The random initializer is important,
454** because garbage data that appears at the end of a journal is likely
455** data that was once in other files that have now been deleted. If the
456** garbage data came from an obsolete journal file, the checksums might
457** be correct. But by initializing the checksum to random value which
458** is different for every journal, we minimize that risk.
drhd9b02572001-04-15 00:37:09 +0000459*/
drhae2b40c2004-06-09 19:03:54 +0000460static const unsigned char aJournalMagic[] = {
461 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
drhed7c8552001-04-11 14:29:21 +0000462};
463
464/*
drh726de592004-06-10 23:35:50 +0000465** The size of the header and of each page in the journal is determined
466** by the following macros.
drh968af522003-02-11 14:55:40 +0000467*/
drhae2b40c2004-06-09 19:03:54 +0000468#define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
drh968af522003-02-11 14:55:40 +0000469
danielk197776572402004-06-25 02:38:54 +0000470/*
471** The journal header size for this pager. In the future, this could be
472** set to some value read from the disk controller. The important
473** characteristic is that it is the same size as a disk sector.
474*/
475#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
476
drhb7f91642004-10-31 02:22:47 +0000477/*
478** The macro MEMDB is true if we are dealing with an in-memory database.
479** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
480** the value of MEMDB will be a constant and the compiler will optimize
481** out code that would never execute.
482*/
483#ifdef SQLITE_OMIT_MEMORYDB
484# define MEMDB 0
485#else
486# define MEMDB pPager->memDb
487#endif
488
489/*
danielk197776572402004-06-25 02:38:54 +0000490** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
491** reserved for working around a windows/posix incompatibility). It is
492** used in the journal to signify that the remainder of the journal file
493** is devoted to storing a master journal name - there are no more pages to
494** roll back. See comments for function writeMasterJournal() for details.
495*/
danielk1977599fcba2004-11-08 07:13:13 +0000496/* #define PAGER_MJ_PGNO(x) (PENDING_BYTE/((x)->pageSize)) */
497#define PAGER_MJ_PGNO(x) ((PENDING_BYTE/((x)->pageSize))+1)
danielk197713adf8a2004-06-03 16:08:41 +0000498
drh968af522003-02-11 14:55:40 +0000499/*
danielk197726836652005-01-17 01:33:13 +0000500** The maximum legal page number is (2^31 - 1).
501*/
502#define PAGER_MAX_PGNO 2147483647
503
504/*
drh86f8c192007-08-22 00:39:19 +0000505** The pagerEnter() and pagerLeave() routines acquire and release
506** a mutex on each pager. The mutex is recursive.
507**
508** This is a special-purpose mutex. It only provides mutual exclusion
509** between the Btree and the Memory Management sqlite3_release_memory()
510** function. It does not prevent, for example, two Btrees from accessing
511** the same pager at the same time. Other general-purpose mutexes in
512** the btree layer handle that chore.
513*/
514#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
515 static void pagerEnter(Pager *p){
516 p->iInUseDB++;
517 if( p->iInUseMM && p->iInUseDB==1 ){
518 sqlite3_mutex *mutex;
519 mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
520 p->iInUseDB = 0;
521 sqlite3_mutex_enter(mutex);
522 p->iInUseDB = 1;
523 sqlite3_mutex_leave(mutex);
524 }
525 assert( p->iInUseMM==0 );
526 }
527 static void pagerLeave(Pager *p){
528 p->iInUseDB--;
529 assert( p->iInUseDB>=0 );
530 }
531#else
532# define pagerEnter(X)
533# define pagerLeave(X)
534#endif
535
536/*
danielk197784f786f2007-08-28 08:00:17 +0000537** Add page pPg to the end of the linked list managed by structure
538** pList (pPg becomes the last entry in the list - the most recently
539** used). Argument pLink should point to either pPg->free or pPg->gfree,
540** depending on whether pPg is being added to the pager-specific or
541** global LRU list.
542*/
danielk19779f61c2f2007-08-27 17:27:49 +0000543static void listAdd(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){
544 pLink->pNext = 0;
545 pLink->pPrev = pList->pLast;
546
danielk197784f786f2007-08-28 08:00:17 +0000547#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
548 assert(pLink==&pPg->free || pLink==&pPg->gfree);
549 assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList);
550#endif
551
danielk19779f61c2f2007-08-27 17:27:49 +0000552 if( pList->pLast ){
553 int iOff = (char *)pLink - (char *)pPg;
554 PagerLruLink *pLastLink = (PagerLruLink *)(&((u8 *)pList->pLast)[iOff]);
555 pLastLink->pNext = pPg;
556 }else{
557 assert(!pList->pFirst);
558 pList->pFirst = pPg;
559 }
560
561 pList->pLast = pPg;
562 if( !pList->pFirstSynced && pPg->needSync==0 ){
563 pList->pFirstSynced = pPg;
564 }
565}
566
danielk197784f786f2007-08-28 08:00:17 +0000567/*
568** Remove pPg from the list managed by the structure pointed to by pList.
569**
570** Argument pLink should point to either pPg->free or pPg->gfree, depending
571** on whether pPg is being added to the pager-specific or global LRU list.
572*/
danielk19779f61c2f2007-08-27 17:27:49 +0000573static void listRemove(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){
574 int iOff = (char *)pLink - (char *)pPg;
575
danielk197784f786f2007-08-28 08:00:17 +0000576#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
577 assert(pLink==&pPg->free || pLink==&pPg->gfree);
578 assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList);
579#endif
580
danielk19779f61c2f2007-08-27 17:27:49 +0000581 if( pPg==pList->pFirst ){
582 pList->pFirst = pLink->pNext;
583 }
584 if( pPg==pList->pLast ){
585 pList->pLast = pLink->pPrev;
586 }
587 if( pLink->pPrev ){
588 PagerLruLink *pPrevLink = (PagerLruLink *)(&((u8 *)pLink->pPrev)[iOff]);
589 pPrevLink->pNext = pLink->pNext;
590 }
591 if( pLink->pNext ){
592 PagerLruLink *pNextLink = (PagerLruLink *)(&((u8 *)pLink->pNext)[iOff]);
593 pNextLink->pPrev = pLink->pPrev;
594 }
595 if( pPg==pList->pFirstSynced ){
596 PgHdr *p = pLink->pNext;
597 while( p && p->needSync ){
598 PagerLruLink *pL = (PagerLruLink *)(&((u8 *)p)[iOff]);
599 p = pL->pNext;
600 }
601 pList->pFirstSynced = p;
602 }
603
604 pLink->pNext = pLink->pPrev = 0;
605}
606
607/*
danielk197784f786f2007-08-28 08:00:17 +0000608** Add page pPg to the list of free pages for the pager. If
609** memory-management is enabled, also add the page to the global
610** list of free pages.
danielk19779f61c2f2007-08-27 17:27:49 +0000611*/
612static void lruListAdd(PgHdr *pPg){
613 listAdd(&pPg->pPager->lru, &pPg->free, pPg);
614#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
615 if( !pPg->pPager->memDb ){
616 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
617 listAdd(&sqlite3LruPageList, &pPg->gfree, pPg);
618 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
619 }
620#endif
621}
622
623/*
danielk197784f786f2007-08-28 08:00:17 +0000624** Remove page pPg from the list of free pages for the associated pager.
625** If memory-management is enabled, also remove pPg from the global list
626** of free pages.
danielk19779f61c2f2007-08-27 17:27:49 +0000627*/
628static void lruListRemove(PgHdr *pPg){
629 listRemove(&pPg->pPager->lru, &pPg->free, pPg);
630#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
631 if( !pPg->pPager->memDb ){
632 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
633 listRemove(&sqlite3LruPageList, &pPg->gfree, pPg);
634 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
635 }
636#endif
637}
638
639/*
danielk197784f786f2007-08-28 08:00:17 +0000640** This function is called just after the needSync flag has been cleared
641** from all pages managed by pPager (usually because the journal file
642** has just been synced). It updates the pPager->lru.pFirstSynced variable
643** and, if memory-management is enabled, the sqlite3LruPageList.pFirstSynced
644** variable also.
danielk19779f61c2f2007-08-27 17:27:49 +0000645*/
646static void lruListSetFirstSynced(Pager *pPager){
647 pPager->lru.pFirstSynced = pPager->lru.pFirst;
648#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
649 if( !pPager->memDb ){
650 PgHdr *p;
651 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
652 for(p=sqlite3LruPageList.pFirst; p && p->needSync; p=p->gfree.pNext);
653 assert(p==pPager->lru.pFirstSynced || p==sqlite3LruPageList.pFirstSynced);
654 sqlite3LruPageList.pFirstSynced = p;
655 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
656 }
657#endif
658}
659
danielk1977f35843b2007-04-07 15:03:17 +0000660/*
661** Return true if page *pPg has already been written to the statement
662** journal (or statement snapshot has been created, if *pPg is part
663** of an in-memory database).
664*/
665static int pageInStatement(PgHdr *pPg){
666 Pager *pPager = pPg->pPager;
667 if( MEMDB ){
668 return PGHDR_TO_HIST(pPg, pPager)->inStmt;
669 }else{
drhf5e7bb52008-02-18 14:47:33 +0000670 return sqlite3BitvecTest(pPager->pInStmt, pPg->pgno);
danielk1977f35843b2007-04-07 15:03:17 +0000671 }
672}
drh8ca0c722006-05-07 17:49:38 +0000673
674/*
675** Change the size of the pager hash table to N. N must be a power
676** of two.
677*/
678static void pager_resize_hash_table(Pager *pPager, int N){
679 PgHdr **aHash, *pPg;
680 assert( N>0 && (N&(N-1))==0 );
drheee4c8c2008-02-18 22:24:57 +0000681#ifdef SQLITE_MALLOC_SOFT_LIMIT
682 if( N*sizeof(aHash[0])>SQLITE_MALLOC_SOFT_LIMIT ){
683 N = SQLITE_MALLOC_SOFT_LIMIT/sizeof(aHash[0]);
684 }
685 if( N==pPager->nHash ) return;
686#endif
drhed138fb2007-08-22 22:04:37 +0000687 pagerLeave(pPager);
drh643167f2008-01-22 21:30:53 +0000688 sqlite3FaultBenign(SQLITE_FAULTINJECTOR_MALLOC, pPager->aHash!=0);
drh17435752007-08-16 04:30:38 +0000689 aHash = sqlite3MallocZero( sizeof(aHash[0])*N );
drh643167f2008-01-22 21:30:53 +0000690 sqlite3FaultBenign(SQLITE_FAULTINJECTOR_MALLOC, 0);
drhed138fb2007-08-22 22:04:37 +0000691 pagerEnter(pPager);
drh8ca0c722006-05-07 17:49:38 +0000692 if( aHash==0 ){
693 /* Failure to rehash is not an error. It is only a performance hit. */
694 return;
695 }
drh17435752007-08-16 04:30:38 +0000696 sqlite3_free(pPager->aHash);
drh8ca0c722006-05-07 17:49:38 +0000697 pPager->nHash = N;
698 pPager->aHash = aHash;
699 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
drh3765df42006-06-28 18:18:09 +0000700 int h;
701 if( pPg->pgno==0 ){
702 assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
703 continue;
704 }
705 h = pPg->pgno & (N-1);
drh8ca0c722006-05-07 17:49:38 +0000706 pPg->pNextHash = aHash[h];
707 if( aHash[h] ){
708 aHash[h]->pPrevHash = pPg;
709 }
710 aHash[h] = pPg;
711 pPg->pPrevHash = 0;
712 }
713}
714
drhdd793422001-06-28 01:54:48 +0000715/*
drh34e79ce2004-02-08 06:05:46 +0000716** Read a 32-bit integer from the given file descriptor. Store the integer
717** that is read in *pRes. Return SQLITE_OK if everything worked, or an
718** error code is something goes wrong.
drh726de592004-06-10 23:35:50 +0000719**
720** All values are stored on disk as big-endian.
drh94f33312002-08-12 12:29:56 +0000721*/
danielk197762079062007-08-15 17:08:46 +0000722static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
drh3b59a5c2006-01-15 20:28:28 +0000723 unsigned char ac[4];
danielk197762079062007-08-15 17:08:46 +0000724 int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
drhae2b40c2004-06-09 19:03:54 +0000725 if( rc==SQLITE_OK ){
drha3152892007-05-05 11:48:52 +0000726 *pRes = sqlite3Get4byte(ac);
drh94f33312002-08-12 12:29:56 +0000727 }
drh94f33312002-08-12 12:29:56 +0000728 return rc;
729}
730
731/*
drh97b57482006-01-10 20:32:32 +0000732** Write a 32-bit integer into a string buffer in big-endian byte order.
733*/
drha3152892007-05-05 11:48:52 +0000734#define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
drh97b57482006-01-10 20:32:32 +0000735
736/*
drh34e79ce2004-02-08 06:05:46 +0000737** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
738** on success or an error code is something goes wrong.
drh94f33312002-08-12 12:29:56 +0000739*/
danielk197762079062007-08-15 17:08:46 +0000740static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
danielk1977bab45c62006-01-16 15:14:27 +0000741 char ac[4];
drh97b57482006-01-10 20:32:32 +0000742 put32bits(ac, val);
danielk197762079062007-08-15 17:08:46 +0000743 return sqlite3OsWrite(fd, ac, 4, offset);
drh94f33312002-08-12 12:29:56 +0000744}
745
drh2554f8b2003-01-22 01:26:44 +0000746/*
danielk19777a2b1ee2007-08-21 14:27:01 +0000747** If file pFd is open, call sqlite3OsUnlock() on it.
748*/
749static int osUnlock(sqlite3_file *pFd, int eLock){
750 if( !pFd->pMethods ){
751 return SQLITE_OK;
752 }
753 return sqlite3OsUnlock(pFd, eLock);
754}
755
756/*
danielk1977c7b60172007-08-22 11:22:03 +0000757** This function determines whether or not the atomic-write optimization
758** can be used with this pager. The optimization can be used if:
759**
760** (a) the value returned by OsDeviceCharacteristics() indicates that
761** a database page may be written atomically, and
762** (b) the value returned by OsSectorSize() is less than or equal
763** to the page size.
764**
765** If the optimization cannot be used, 0 is returned. If it can be used,
766** then the value returned is the size of the journal file when it
767** contains rollback data for exactly one page.
768*/
769#ifdef SQLITE_ENABLE_ATOMIC_WRITE
770static int jrnlBufferSize(Pager *pPager){
771 int dc; /* Device characteristics */
772 int nSector; /* Sector size */
773 int nPage; /* Page size */
774 sqlite3_file *fd = pPager->fd;
775
776 if( fd->pMethods ){
777 dc = sqlite3OsDeviceCharacteristics(fd);
778 nSector = sqlite3OsSectorSize(fd);
779 nPage = pPager->pageSize;
780 }
781
782 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
783 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
784
danielk1977f8940ae2007-08-23 11:07:10 +0000785 if( !fd->pMethods || (dc&(SQLITE_IOCAP_ATOMIC|(nPage>>8))&&nSector<=nPage) ){
danielk1977c7b60172007-08-22 11:22:03 +0000786 return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
787 }
788 return 0;
789}
790#endif
791
792/*
danielk1977aef0bf62005-12-30 16:28:01 +0000793** This function should be called when an error occurs within the pager
danielk1977a96a7102006-01-16 12:46:41 +0000794** code. The first argument is a pointer to the pager structure, the
795** second the error-code about to be returned by a pager API function.
796** The value returned is a copy of the second argument to this function.
797**
drh4f0ee682007-03-30 20:43:40 +0000798** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
danielk1977ae72d982007-10-03 08:46:44 +0000799** the error becomes persistent. Until the persisten error is cleared,
800** subsequent API calls on this Pager will immediately return the same
801** error code.
802**
803** A persistent error indicates that the contents of the pager-cache
804** cannot be trusted. This state can be cleared by completely discarding
805** the contents of the pager-cache. If a transaction was active when
806** the persistent error occured, then the rollback journal may need
807** to be replayed.
danielk1977aef0bf62005-12-30 16:28:01 +0000808*/
danielk1977ae72d982007-10-03 08:46:44 +0000809static void pager_unlock(Pager *pPager);
danielk1977aef0bf62005-12-30 16:28:01 +0000810static int pager_error(Pager *pPager, int rc){
drh4ac285a2006-09-15 07:28:50 +0000811 int rc2 = rc & 0xff;
drh34f56212007-08-10 23:56:35 +0000812 assert(
813 pPager->errCode==SQLITE_FULL ||
814 pPager->errCode==SQLITE_OK ||
815 (pPager->errCode & 0xff)==SQLITE_IOERR
816 );
danielk1977979f38e2007-03-27 16:19:51 +0000817 if(
drh4ac285a2006-09-15 07:28:50 +0000818 rc2==SQLITE_FULL ||
819 rc2==SQLITE_IOERR ||
drh4f0ee682007-03-30 20:43:40 +0000820 rc2==SQLITE_CORRUPT
danielk1977efaaf572006-01-16 11:29:19 +0000821 ){
822 pPager->errCode = rc;
danielk1977ae72d982007-10-03 08:46:44 +0000823 if( pPager->state==PAGER_UNLOCK && pPager->nRef==0 ){
824 /* If the pager is already unlocked, call pager_unlock() now to
825 ** clear the error state and ensure that the pager-cache is
826 ** completely empty.
827 */
828 pager_unlock(pPager);
829 }
danielk1977aef0bf62005-12-30 16:28:01 +0000830 }
831 return rc;
832}
833
drh477731b2007-06-16 03:06:27 +0000834/*
835** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
836** on the cache using a hash function. This is used for testing
837** and debugging only.
838*/
danielk19773c407372005-02-15 02:54:14 +0000839#ifdef SQLITE_CHECK_PAGES
840/*
841** Return a 32-bit hash of the page data for pPage.
842*/
drh477731b2007-06-16 03:06:27 +0000843static u32 pager_datahash(int nByte, unsigned char *pData){
danielk19773c407372005-02-15 02:54:14 +0000844 u32 hash = 0;
845 int i;
drh477731b2007-06-16 03:06:27 +0000846 for(i=0; i<nByte; i++){
847 hash = (hash*1039) + pData[i];
danielk19773c407372005-02-15 02:54:14 +0000848 }
849 return hash;
850}
drh477731b2007-06-16 03:06:27 +0000851static u32 pager_pagehash(PgHdr *pPage){
852 return pager_datahash(pPage->pPager->pageSize,
853 (unsigned char *)PGHDR_TO_DATA(pPage));
854}
danielk19773c407372005-02-15 02:54:14 +0000855
856/*
857** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
858** is defined, and NDEBUG is not defined, an assert() statement checks
859** that the page is either dirty or still matches the calculated page-hash.
860*/
861#define CHECK_PAGE(x) checkPage(x)
862static void checkPage(PgHdr *pPg){
863 Pager *pPager = pPg->pPager;
danielk1977efaaf572006-01-16 11:29:19 +0000864 assert( !pPg->pageHash || pPager->errCode || MEMDB || pPg->dirty ||
danielk19773c407372005-02-15 02:54:14 +0000865 pPg->pageHash==pager_pagehash(pPg) );
866}
867
868#else
drh8ffa8172007-06-18 17:25:17 +0000869#define pager_datahash(X,Y) 0
drh477731b2007-06-16 03:06:27 +0000870#define pager_pagehash(X) 0
danielk19773c407372005-02-15 02:54:14 +0000871#define CHECK_PAGE(x)
872#endif
873
drhed7c8552001-04-11 14:29:21 +0000874/*
danielk197776572402004-06-25 02:38:54 +0000875** When this is called the journal file for pager pPager must be open.
876** The master journal file name is read from the end of the file and
danielk197765839c62007-08-30 08:08:17 +0000877** written into memory supplied by the caller.
danielk197776572402004-06-25 02:38:54 +0000878**
danielk197765839c62007-08-30 08:08:17 +0000879** zMaster must point to a buffer of at least nMaster bytes allocated by
880** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
881** enough space to write the master journal name). If the master journal
882** name in the journal is longer than nMaster bytes (including a
883** nul-terminator), then this is handled as if no master journal name
884** were present in the journal.
885**
886** If no master journal file name is present zMaster[0] is set to 0 and
danielk197776572402004-06-25 02:38:54 +0000887** SQLITE_OK returned.
888*/
danielk197765839c62007-08-30 08:08:17 +0000889static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, int nMaster){
danielk197776572402004-06-25 02:38:54 +0000890 int rc;
891 u32 len;
drheb206252004-10-01 02:00:31 +0000892 i64 szJ;
danielk1977c3e8f5e2004-06-28 01:16:46 +0000893 u32 cksum;
danielk1977cafadba2004-06-25 11:11:54 +0000894 int i;
danielk197776572402004-06-25 02:38:54 +0000895 unsigned char aMagic[8]; /* A buffer to hold the magic header */
896
danielk197765839c62007-08-30 08:08:17 +0000897 zMaster[0] = '\0';
danielk197776572402004-06-25 02:38:54 +0000898
drh054889e2005-11-30 03:20:31 +0000899 rc = sqlite3OsFileSize(pJrnl, &szJ);
danielk1977cafadba2004-06-25 11:11:54 +0000900 if( rc!=SQLITE_OK || szJ<16 ) return rc;
danielk197776572402004-06-25 02:38:54 +0000901
danielk197762079062007-08-15 17:08:46 +0000902 rc = read32bits(pJrnl, szJ-16, &len);
danielk197776572402004-06-25 02:38:54 +0000903 if( rc!=SQLITE_OK ) return rc;
904
danielk197765839c62007-08-30 08:08:17 +0000905 if( len>=nMaster ){
906 return SQLITE_OK;
907 }
908
danielk197762079062007-08-15 17:08:46 +0000909 rc = read32bits(pJrnl, szJ-12, &cksum);
danielk1977cafadba2004-06-25 11:11:54 +0000910 if( rc!=SQLITE_OK ) return rc;
911
danielk197762079062007-08-15 17:08:46 +0000912 rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8);
danielk197776572402004-06-25 02:38:54 +0000913 if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc;
914
danielk197765839c62007-08-30 08:08:17 +0000915 rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len);
danielk197776572402004-06-25 02:38:54 +0000916 if( rc!=SQLITE_OK ){
danielk197776572402004-06-25 02:38:54 +0000917 return rc;
918 }
danielk197765839c62007-08-30 08:08:17 +0000919 zMaster[len] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000920
921 /* See if the checksum matches the master journal name */
922 for(i=0; i<len; i++){
danielk197765839c62007-08-30 08:08:17 +0000923 cksum -= zMaster[i];
924 }
danielk19778191bff2004-06-28 04:52:30 +0000925 if( cksum ){
926 /* If the checksum doesn't add up, then one or more of the disk sectors
927 ** containing the master journal filename is corrupted. This means
928 ** definitely roll back, so just return SQLITE_OK and report a (nul)
929 ** master-journal filename.
930 */
danielk197765839c62007-08-30 08:08:17 +0000931 zMaster[0] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000932 }
danielk197776572402004-06-25 02:38:54 +0000933
934 return SQLITE_OK;
935}
936
937/*
938** Seek the journal file descriptor to the next sector boundary where a
939** journal header may be read or written. Pager.journalOff is updated with
940** the new seek offset.
941**
942** i.e for a sector size of 512:
943**
944** Input Offset Output Offset
945** ---------------------------------------
946** 0 0
947** 512 512
948** 100 512
949** 2000 2048
950**
951*/
danielk197762079062007-08-15 17:08:46 +0000952static void seekJournalHdr(Pager *pPager){
drheb206252004-10-01 02:00:31 +0000953 i64 offset = 0;
954 i64 c = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000955 if( c ){
956 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
957 }
958 assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
959 assert( offset>=c );
960 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
961 pPager->journalOff = offset;
danielk197776572402004-06-25 02:38:54 +0000962}
963
964/*
drhf3a87622008-04-17 14:16:42 +0000965** Write zeros over the header of the journal file. This has the
966** effect of invalidating the journal file and committing the
967** transaction.
968*/
969static int zeroJournalHdr(Pager *pPager){
970 int rc;
971 static const char zeroHdr[28];
972
973 IOTRACE(("JZEROHDR %p\n", pPager))
974 rc = sqlite3OsWrite(pPager->jfd, zeroHdr, sizeof(zeroHdr), 0);
975 return rc;
976}
977
978/*
danielk197776572402004-06-25 02:38:54 +0000979** The journal file must be open when this routine is called. A journal
980** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
981** current location.
982**
983** The format for the journal header is as follows:
984** - 8 bytes: Magic identifying journal format.
985** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
986** - 4 bytes: Random number used for page hash.
987** - 4 bytes: Initial database page count.
988** - 4 bytes: Sector size used by the process that wrote this journal.
danielk197767c007b2008-03-20 04:45:49 +0000989** - 4 bytes: Database page size.
danielk197776572402004-06-25 02:38:54 +0000990**
danielk197767c007b2008-03-20 04:45:49 +0000991** Followed by (JOURNAL_HDR_SZ - 28) bytes of unused space.
danielk197776572402004-06-25 02:38:54 +0000992*/
993static int writeJournalHdr(Pager *pPager){
danielk1977a664f8e2008-04-22 14:31:48 +0000994 int rc = SQLITE_OK;
995 char *zHeader = pPager->pTmpSpace;
996 int nHeader = pPager->pageSize;
997 int nWrite;
998
999 if( nHeader>JOURNAL_HDR_SZ(pPager) ){
1000 nHeader = JOURNAL_HDR_SZ(pPager);
1001 }
danielk197776572402004-06-25 02:38:54 +00001002
danielk19774099f6e2007-03-19 11:25:20 +00001003 if( pPager->stmtHdrOff==0 ){
1004 pPager->stmtHdrOff = pPager->journalOff;
1005 }
1006
danielk197762079062007-08-15 17:08:46 +00001007 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +00001008 pPager->journalHdr = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +00001009
drh97b57482006-01-10 20:32:32 +00001010 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
danielk19774cd2cd52007-08-23 14:48:23 +00001011
1012 /*
1013 ** Write the nRec Field - the number of page records that follow this
1014 ** journal header. Normally, zero is written to this value at this time.
1015 ** After the records are added to the journal (and the journal synced,
1016 ** if in full-sync mode), the zero is overwritten with the true number
1017 ** of records (see syncJournal()).
1018 **
1019 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
1020 ** reading the journal this value tells SQLite to assume that the
1021 ** rest of the journal file contains valid page records. This assumption
1022 ** is dangerous, as if a failure occured whilst writing to the journal
1023 ** file it may contain some garbage data. There are two scenarios
1024 ** where this risk can be ignored:
1025 **
1026 ** * When the pager is in no-sync mode. Corruption can follow a
1027 ** power failure in this case anyway.
1028 **
1029 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
1030 ** that garbage data is never appended to the journal file.
1031 */
1032 assert(pPager->fd->pMethods||pPager->noSync);
1033 if( (pPager->noSync)
1034 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
1035 ){
1036 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
1037 }else{
1038 put32bits(&zHeader[sizeof(aJournalMagic)], 0);
1039 }
1040
drh97b57482006-01-10 20:32:32 +00001041 /* The random check-hash initialiser */
drh2fa18682008-03-19 14:15:34 +00001042 sqlite3_randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
drh97b57482006-01-10 20:32:32 +00001043 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
1044 /* The initial database size */
1045 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbSize);
1046 /* The assumed sector size for this process */
1047 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
danielk197767c007b2008-03-20 04:45:49 +00001048 if( pPager->journalHdr==0 ){
1049 /* The page size */
1050 put32bits(&zHeader[sizeof(aJournalMagic)+16], pPager->pageSize);
1051 }
danielk197776572402004-06-25 02:38:54 +00001052
danielk1977a664f8e2008-04-22 14:31:48 +00001053 for(nWrite=0; rc==SQLITE_OK&&nWrite<JOURNAL_HDR_SZ(pPager); nWrite+=nHeader){
1054 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, nHeader))
1055 rc = sqlite3OsWrite(pPager->jfd, zHeader, nHeader, pPager->journalOff);
1056 pPager->journalOff += nHeader;
danielk197776572402004-06-25 02:38:54 +00001057 }
danielk1977a664f8e2008-04-22 14:31:48 +00001058
danielk197776572402004-06-25 02:38:54 +00001059 return rc;
1060}
1061
1062/*
1063** The journal file must be open when this is called. A journal header file
1064** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
1065** file. See comments above function writeJournalHdr() for a description of
1066** the journal header format.
1067**
1068** If the header is read successfully, *nRec is set to the number of
1069** page records following this header and *dbSize is set to the size of the
1070** database before the transaction began, in pages. Also, pPager->cksumInit
1071** is set to the value read from the journal header. SQLITE_OK is returned
1072** in this case.
1073**
1074** If the journal header file appears to be corrupted, SQLITE_DONE is
1075** returned and *nRec and *dbSize are not set. If JOURNAL_HDR_SZ bytes
1076** cannot be read from the journal file an error code is returned.
1077*/
1078static int readJournalHdr(
1079 Pager *pPager,
drheb206252004-10-01 02:00:31 +00001080 i64 journalSize,
danielk197776572402004-06-25 02:38:54 +00001081 u32 *pNRec,
1082 u32 *pDbSize
1083){
1084 int rc;
1085 unsigned char aMagic[8]; /* A buffer to hold the magic header */
danielk197762079062007-08-15 17:08:46 +00001086 i64 jrnlOff;
danielk197767c007b2008-03-20 04:45:49 +00001087 int iPageSize;
danielk197776572402004-06-25 02:38:54 +00001088
danielk197762079062007-08-15 17:08:46 +00001089 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +00001090 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
1091 return SQLITE_DONE;
1092 }
danielk197762079062007-08-15 17:08:46 +00001093 jrnlOff = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +00001094
danielk197762079062007-08-15 17:08:46 +00001095 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff);
danielk197776572402004-06-25 02:38:54 +00001096 if( rc ) return rc;
danielk197762079062007-08-15 17:08:46 +00001097 jrnlOff += sizeof(aMagic);
danielk197776572402004-06-25 02:38:54 +00001098
1099 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
1100 return SQLITE_DONE;
1101 }
1102
danielk197762079062007-08-15 17:08:46 +00001103 rc = read32bits(pPager->jfd, jrnlOff, pNRec);
danielk197776572402004-06-25 02:38:54 +00001104 if( rc ) return rc;
1105
danielk197762079062007-08-15 17:08:46 +00001106 rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit);
danielk197776572402004-06-25 02:38:54 +00001107 if( rc ) return rc;
1108
danielk197762079062007-08-15 17:08:46 +00001109 rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize);
danielk197776572402004-06-25 02:38:54 +00001110 if( rc ) return rc;
1111
danielk197767c007b2008-03-20 04:45:49 +00001112 rc = read32bits(pPager->jfd, jrnlOff+16, (u32 *)&iPageSize);
1113 if( rc==SQLITE_OK
1114 && iPageSize>=512
1115 && iPageSize<=SQLITE_MAX_PAGE_SIZE
1116 && ((iPageSize-1)&iPageSize)==0
1117 ){
1118 u16 pagesize = iPageSize;
1119 rc = sqlite3PagerSetPagesize(pPager, &pagesize);
1120 }
1121 if( rc ) return rc;
1122
danielk197776572402004-06-25 02:38:54 +00001123 /* Update the assumed sector-size to match the value used by
1124 ** the process that created this journal. If this journal was
1125 ** created by a process other than this one, then this routine
1126 ** is being called from within pager_playback(). The local value
1127 ** of Pager.sectorSize is restored at the end of that routine.
1128 */
danielk197762079062007-08-15 17:08:46 +00001129 rc = read32bits(pPager->jfd, jrnlOff+12, (u32 *)&pPager->sectorSize);
danielk197776572402004-06-25 02:38:54 +00001130 if( rc ) return rc;
1131
1132 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk197762079062007-08-15 17:08:46 +00001133 return SQLITE_OK;
danielk197776572402004-06-25 02:38:54 +00001134}
1135
1136
1137/*
1138** Write the supplied master journal name into the journal file for pager
danielk1977cafadba2004-06-25 11:11:54 +00001139** pPager at the current location. The master journal name must be the last
1140** thing written to a journal file. If the pager is in full-sync mode, the
1141** journal file descriptor is advanced to the next sector boundary before
1142** anything is written. The format is:
1143**
1144** + 4 bytes: PAGER_MJ_PGNO.
1145** + N bytes: length of master journal name.
1146** + 4 bytes: N
1147** + 4 bytes: Master journal name checksum.
1148** + 8 bytes: aJournalMagic[].
1149**
1150** The master journal page checksum is the sum of the bytes in the master
1151** journal name.
danielk1977aef0bf62005-12-30 16:28:01 +00001152**
1153** If zMaster is a NULL pointer (occurs for a single database transaction),
1154** this call is a no-op.
danielk197776572402004-06-25 02:38:54 +00001155*/
1156static int writeMasterJournal(Pager *pPager, const char *zMaster){
1157 int rc;
1158 int len;
danielk1977cafadba2004-06-25 11:11:54 +00001159 int i;
danielk197762079062007-08-15 17:08:46 +00001160 i64 jrnlOff;
drh97b57482006-01-10 20:32:32 +00001161 u32 cksum = 0;
1162 char zBuf[sizeof(aJournalMagic)+2*4];
danielk197776572402004-06-25 02:38:54 +00001163
1164 if( !zMaster || pPager->setMaster) return SQLITE_OK;
1165 pPager->setMaster = 1;
1166
1167 len = strlen(zMaster);
danielk1977cafadba2004-06-25 11:11:54 +00001168 for(i=0; i<len; i++){
1169 cksum += zMaster[i];
1170 }
danielk197776572402004-06-25 02:38:54 +00001171
1172 /* If in full-sync mode, advance to the next disk sector before writing
1173 ** the master journal name. This is in case the previous page written to
1174 ** the journal has already been synced.
1175 */
1176 if( pPager->fullSync ){
danielk197762079062007-08-15 17:08:46 +00001177 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +00001178 }
danielk197762079062007-08-15 17:08:46 +00001179 jrnlOff = pPager->journalOff;
danielk1977cafadba2004-06-25 11:11:54 +00001180 pPager->journalOff += (len+20);
danielk197776572402004-06-25 02:38:54 +00001181
danielk197762079062007-08-15 17:08:46 +00001182 rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager));
danielk197776572402004-06-25 02:38:54 +00001183 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001184 jrnlOff += 4;
danielk197776572402004-06-25 02:38:54 +00001185
danielk197762079062007-08-15 17:08:46 +00001186 rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff);
danielk197776572402004-06-25 02:38:54 +00001187 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001188 jrnlOff += len;
danielk197776572402004-06-25 02:38:54 +00001189
drh97b57482006-01-10 20:32:32 +00001190 put32bits(zBuf, len);
1191 put32bits(&zBuf[4], cksum);
1192 memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic));
danielk197762079062007-08-15 17:08:46 +00001193 rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff);
drh2c8997b2005-08-27 16:36:48 +00001194 pPager->needSync = !pPager->noSync;
danielk197776572402004-06-25 02:38:54 +00001195 return rc;
1196}
1197
1198/*
drh03eb96a2002-11-10 23:32:56 +00001199** Add or remove a page from the list of all pages that are in the
drhac69b052004-05-12 13:30:07 +00001200** statement journal.
drh03eb96a2002-11-10 23:32:56 +00001201**
1202** The Pager keeps a separate list of pages that are currently in
danielk19773b8a05f2007-03-19 17:44:26 +00001203** the statement journal. This helps the sqlite3PagerStmtCommit()
drh03eb96a2002-11-10 23:32:56 +00001204** routine run MUCH faster for the common case where there are many
drhac69b052004-05-12 13:30:07 +00001205** pages in memory but only a few are in the statement journal.
drh03eb96a2002-11-10 23:32:56 +00001206*/
drh3aac2dd2004-04-26 14:10:20 +00001207static void page_add_to_stmt_list(PgHdr *pPg){
drh03eb96a2002-11-10 23:32:56 +00001208 Pager *pPager = pPg->pPager;
danielk1977f35843b2007-04-07 15:03:17 +00001209 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
1210 assert( MEMDB );
1211 if( !pHist->inStmt ){
1212 assert( pHist->pPrevStmt==0 && pHist->pNextStmt==0 );
1213 if( pPager->pStmt ){
1214 PGHDR_TO_HIST(pPager->pStmt, pPager)->pPrevStmt = pPg;
1215 }
1216 pHist->pNextStmt = pPager->pStmt;
1217 pPager->pStmt = pPg;
1218 pHist->inStmt = 1;
drh03eb96a2002-11-10 23:32:56 +00001219 }
drh03eb96a2002-11-10 23:32:56 +00001220}
1221
1222/*
drhed7c8552001-04-11 14:29:21 +00001223** Find a page in the hash table given its page number. Return
1224** a pointer to the page or NULL if not found.
1225*/
drhd9b02572001-04-15 00:37:09 +00001226static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drh8ca0c722006-05-07 17:49:38 +00001227 PgHdr *p;
1228 if( pPager->aHash==0 ) return 0;
1229 p = pPager->aHash[pgno & (pPager->nHash-1)];
drhed7c8552001-04-11 14:29:21 +00001230 while( p && p->pgno!=pgno ){
1231 p = p->pNextHash;
1232 }
1233 return p;
1234}
1235
1236/*
danielk1977e180dd92007-04-05 17:15:52 +00001237** Clear the in-memory cache. This routine
drhed7c8552001-04-11 14:29:21 +00001238** sets the state of the pager back to what it was when it was first
1239** opened. Any outstanding pages are invalidated and subsequent attempts
1240** to access those pages will likely result in a coredump.
1241*/
drhd9b02572001-04-15 00:37:09 +00001242static void pager_reset(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00001243 PgHdr *pPg, *pNext;
danielk1977efaaf572006-01-16 11:29:19 +00001244 if( pPager->errCode ) return;
drhd9b02572001-04-15 00:37:09 +00001245 for(pPg=pPager->pAll; pPg; pPg=pNext){
drh538f5702007-04-13 02:14:30 +00001246 IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
1247 PAGER_INCR(sqlite3_pager_pgfree_count);
drhd9b02572001-04-15 00:37:09 +00001248 pNext = pPg->pNextAll;
danielk19779f61c2f2007-08-27 17:27:49 +00001249 lruListRemove(pPg);
drh0d180202008-02-14 23:26:56 +00001250 sqlite3_free(pPg->pData);
drh17435752007-08-16 04:30:38 +00001251 sqlite3_free(pPg);
drhed7c8552001-04-11 14:29:21 +00001252 }
danielk19779f61c2f2007-08-27 17:27:49 +00001253 assert(pPager->lru.pFirst==0);
1254 assert(pPager->lru.pFirstSynced==0);
1255 assert(pPager->lru.pLast==0);
danielk1977b94bf852007-03-19 13:53:37 +00001256 pPager->pStmt = 0;
drhd9b02572001-04-15 00:37:09 +00001257 pPager->pAll = 0;
danielk1977ae72d982007-10-03 08:46:44 +00001258 pPager->pDirty = 0;
drh8ca0c722006-05-07 17:49:38 +00001259 pPager->nHash = 0;
drh17435752007-08-16 04:30:38 +00001260 sqlite3_free(pPager->aHash);
drhed7c8552001-04-11 14:29:21 +00001261 pPager->nPage = 0;
drh8ca0c722006-05-07 17:49:38 +00001262 pPager->aHash = 0;
drhed7c8552001-04-11 14:29:21 +00001263 pPager->nRef = 0;
danielk1977e277be02007-03-23 18:12:06 +00001264}
1265
1266/*
danielk1977ae72d982007-10-03 08:46:44 +00001267** Unlock the database file.
1268**
1269** If the pager is currently in error state, discard the contents of
1270** the cache and reset the Pager structure internal state. If there is
1271** an open journal-file, then the next time a shared-lock is obtained
1272** on the pager file (by this or any other process), it will be
1273** treated as a hot-journal and rolled back.
1274*/
1275static void pager_unlock(Pager *pPager){
1276 if( !pPager->exclusiveMode ){
1277 if( !MEMDB ){
drh1aa5af12008-03-07 19:51:14 +00001278 int rc = osUnlock(pPager->fd, NO_LOCK);
drh308aa322008-03-07 20:14:38 +00001279 if( rc ) pPager->errCode = rc;
danielk1977ae72d982007-10-03 08:46:44 +00001280 pPager->dbSize = -1;
1281 IOTRACE(("UNLOCK %p\n", pPager))
1282
drh16e45a42008-04-19 20:34:18 +00001283 /* Always close the journal file when dropping the database lock.
1284 ** Otherwise, another connection with journal_mode=delete might
1285 ** delete the file out from under us.
1286 */
1287 if( pPager->journalOpen ){
1288 sqlite3OsClose(pPager->jfd);
1289 pPager->journalOpen = 0;
1290 sqlite3BitvecDestroy(pPager->pInJournal);
1291 pPager->pInJournal = 0;
1292 }
1293
danielk1977ae72d982007-10-03 08:46:44 +00001294 /* If Pager.errCode is set, the contents of the pager cache cannot be
1295 ** trusted. Now that the pager file is unlocked, the contents of the
1296 ** cache can be discarded and the error code safely cleared.
1297 */
1298 if( pPager->errCode ){
drh1aa5af12008-03-07 19:51:14 +00001299 if( rc==SQLITE_OK ) pPager->errCode = SQLITE_OK;
danielk1977ae72d982007-10-03 08:46:44 +00001300 pager_reset(pPager);
1301 if( pPager->stmtOpen ){
1302 sqlite3OsClose(pPager->stfd);
drhf5e7bb52008-02-18 14:47:33 +00001303 sqlite3BitvecDestroy(pPager->pInStmt);
1304 pPager->pInStmt = 0;
danielk1977ae72d982007-10-03 08:46:44 +00001305 }
danielk1977ae72d982007-10-03 08:46:44 +00001306 pPager->stmtOpen = 0;
1307 pPager->stmtInUse = 0;
1308 pPager->journalOff = 0;
1309 pPager->journalStarted = 0;
1310 pPager->stmtAutoopen = 0;
1311 pPager->origDbSize = 0;
1312 }
1313 }
1314
1315 if( !MEMDB || pPager->errCode==SQLITE_OK ){
1316 pPager->state = PAGER_UNLOCK;
1317 pPager->changeCountDone = 0;
1318 }
1319 }
1320}
1321
1322/*
1323** Execute a rollback if a transaction is active and unlock the
1324** database file. If the pager has already entered the error state,
1325** do not attempt the rollback.
1326*/
1327static void pagerUnlockAndRollback(Pager *p){
drhfdc40e92008-04-17 20:59:37 +00001328 /* assert( p->state>=PAGER_RESERVED || p->journalOpen==0 ); */
danielk1977ae72d982007-10-03 08:46:44 +00001329 if( p->errCode==SQLITE_OK && p->state>=PAGER_RESERVED ){
1330 sqlite3PagerRollback(p);
1331 }
1332 pager_unlock(p);
drhfdc40e92008-04-17 20:59:37 +00001333#if 0
danielk1977ae72d982007-10-03 08:46:44 +00001334 assert( p->errCode || !p->journalOpen || (p->exclusiveMode&&!p->journalOff) );
1335 assert( p->errCode || !p->stmtOpen || p->exclusiveMode );
drhfdc40e92008-04-17 20:59:37 +00001336#endif
danielk1977ae72d982007-10-03 08:46:44 +00001337}
1338
1339/*
drh80e35f42007-03-30 14:06:34 +00001340** This routine ends a transaction. A transaction is ended by either
1341** a COMMIT or a ROLLBACK.
1342**
drhed7c8552001-04-11 14:29:21 +00001343** When this routine is called, the pager has the journal file open and
drh80e35f42007-03-30 14:06:34 +00001344** a RESERVED or EXCLUSIVE lock on the database. This routine will release
1345** the database lock and acquires a SHARED lock in its place if that is
1346** the appropriate thing to do. Release locks usually is appropriate,
1347** unless we are in exclusive access mode or unless this is a
1348** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation.
1349**
1350** The journal file is either deleted or truncated.
drh50457892003-09-06 01:10:47 +00001351**
1352** TODO: Consider keeping the journal file open for temporary databases.
1353** This might give a performance improvement on windows where opening
1354** a file is an expensive operation.
drhed7c8552001-04-11 14:29:21 +00001355*/
drh80e35f42007-03-30 14:06:34 +00001356static int pager_end_transaction(Pager *pPager){
drhd9b02572001-04-15 00:37:09 +00001357 PgHdr *pPg;
danielk197741483462007-03-24 16:45:04 +00001358 int rc = SQLITE_OK;
danielk1977979f38e2007-03-27 16:19:51 +00001359 int rc2 = SQLITE_OK;
drhb7f91642004-10-31 02:22:47 +00001360 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +00001361 if( pPager->state<PAGER_RESERVED ){
1362 return SQLITE_OK;
1363 }
danielk19773b8a05f2007-03-19 17:44:26 +00001364 sqlite3PagerStmtCommit(pPager);
danielk1977334cdb62007-03-26 08:05:12 +00001365 if( pPager->stmtOpen && !pPager->exclusiveMode ){
danielk1977b4b47412007-08-17 15:53:36 +00001366 sqlite3OsClose(pPager->stfd);
danielk1977979f38e2007-03-27 16:19:51 +00001367 pPager->stmtOpen = 0;
drh0f892532002-05-30 12:27:03 +00001368 }
drhda47d772002-12-02 04:25:19 +00001369 if( pPager->journalOpen ){
drhfdc40e92008-04-17 20:59:37 +00001370 if( (pPager->exclusiveMode ||
1371 pPager->journalMode==PAGER_JOURNALMODE_PERSIST)
1372 && (rc = zeroJournalHdr(pPager))==SQLITE_OK ){
danielk197741483462007-03-24 16:45:04 +00001373 pPager->journalOff = 0;
danielk1977334cdb62007-03-26 08:05:12 +00001374 pPager->journalStarted = 0;
danielk197741483462007-03-24 16:45:04 +00001375 }else{
danielk1977b4b47412007-08-17 15:53:36 +00001376 sqlite3OsClose(pPager->jfd);
danielk197741483462007-03-24 16:45:04 +00001377 pPager->journalOpen = 0;
drh01fa4c32007-04-02 11:22:22 +00001378 if( rc==SQLITE_OK ){
danielk1977fee2d252007-08-18 10:59:19 +00001379 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
danielk19777152de82007-03-29 17:28:14 +00001380 }
danielk197741483462007-03-24 16:45:04 +00001381 }
drhf5e7bb52008-02-18 14:47:33 +00001382 sqlite3BitvecDestroy(pPager->pInJournal);
1383 pPager->pInJournal = 0;
drhda47d772002-12-02 04:25:19 +00001384 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1385 pPg->inJournal = 0;
1386 pPg->dirty = 0;
drhdb48ee02003-01-16 13:42:43 +00001387 pPg->needSync = 0;
danielk197741483462007-03-24 16:45:04 +00001388 pPg->alwaysRollback = 0;
danielk19773c407372005-02-15 02:54:14 +00001389#ifdef SQLITE_CHECK_PAGES
1390 pPg->pageHash = pager_pagehash(pPg);
1391#endif
drhda47d772002-12-02 04:25:19 +00001392 }
drh605ad8f2006-05-03 23:34:05 +00001393 pPager->pDirty = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001394 pPager->dirtyCache = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001395 pPager->nRec = 0;
drhda47d772002-12-02 04:25:19 +00001396 }else{
drhf5e7bb52008-02-18 14:47:33 +00001397 assert( pPager->pInJournal==0 );
drhd9b02572001-04-15 00:37:09 +00001398 }
danielk1977979f38e2007-03-27 16:19:51 +00001399
danielk197741483462007-03-24 16:45:04 +00001400 if( !pPager->exclusiveMode ){
danielk19777a2b1ee2007-08-21 14:27:01 +00001401 rc2 = osUnlock(pPager->fd, SHARED_LOCK);
danielk197741483462007-03-24 16:45:04 +00001402 pPager->state = PAGER_SHARED;
danielk1977334cdb62007-03-26 08:05:12 +00001403 }else if( pPager->state==PAGER_SYNCED ){
1404 pPager->state = PAGER_EXCLUSIVE;
danielk197741483462007-03-24 16:45:04 +00001405 }
danielk1977334cdb62007-03-26 08:05:12 +00001406 pPager->origDbSize = 0;
danielk197776572402004-06-25 02:38:54 +00001407 pPager->setMaster = 0;
danielk1977c4da5b92006-01-21 12:08:54 +00001408 pPager->needSync = 0;
danielk19779f61c2f2007-08-27 17:27:49 +00001409 lruListSetFirstSynced(pPager);
drh588f5bc2007-01-02 18:41:54 +00001410 pPager->dbSize = -1;
danielk1977979f38e2007-03-27 16:19:51 +00001411
1412 return (rc==SQLITE_OK?rc2:rc);
drhed7c8552001-04-11 14:29:21 +00001413}
1414
drhed7c8552001-04-11 14:29:21 +00001415/*
drh968af522003-02-11 14:55:40 +00001416** Compute and return a checksum for the page of data.
drh34e79ce2004-02-08 06:05:46 +00001417**
1418** This is not a real checksum. It is really just the sum of the
drh726de592004-06-10 23:35:50 +00001419** random initial value and the page number. We experimented with
1420** a checksum of the entire data, but that was found to be too slow.
1421**
1422** Note that the page number is stored at the beginning of data and
1423** the checksum is stored at the end. This is important. If journal
1424** corruption occurs due to a power failure, the most likely scenario
1425** is that one end or the other of the record will be changed. It is
1426** much less likely that the two ends of the journal record will be
1427** correct and the middle be corrupt. Thus, this "checksum" scheme,
1428** though fast and simple, catches the mostly likely kind of corruption.
1429**
1430** FIX ME: Consider adding every 200th (or so) byte of the data to the
1431** checksum. That way if a single page spans 3 or more disk sectors and
1432** only the middle sector is corrupt, we will still have a reasonable
1433** chance of failing the checksum and thus detecting the problem.
drh968af522003-02-11 14:55:40 +00001434*/
drh74161702006-02-24 02:53:49 +00001435static u32 pager_cksum(Pager *pPager, const u8 *aData){
danielk1977ef317ab2004-06-23 10:43:10 +00001436 u32 cksum = pPager->cksumInit;
1437 int i = pPager->pageSize-200;
1438 while( i>0 ){
1439 cksum += aData[i];
1440 i -= 200;
1441 }
drh968af522003-02-11 14:55:40 +00001442 return cksum;
1443}
1444
drh605ad8f2006-05-03 23:34:05 +00001445/* Forward declaration */
1446static void makeClean(PgHdr*);
1447
drh968af522003-02-11 14:55:40 +00001448/*
drhfa86c412002-02-02 15:01:15 +00001449** Read a single page from the journal file opened on file descriptor
1450** jfd. Playback this one page.
drh968af522003-02-11 14:55:40 +00001451**
drh726de592004-06-10 23:35:50 +00001452** If useCksum==0 it means this journal does not use checksums. Checksums
1453** are not used in statement journals because statement journals do not
1454** need to survive power failures.
drhfa86c412002-02-02 15:01:15 +00001455*/
danielk197762079062007-08-15 17:08:46 +00001456static int pager_playback_one_page(
1457 Pager *pPager,
1458 sqlite3_file *jfd,
1459 i64 offset,
1460 int useCksum
1461){
drhfa86c412002-02-02 15:01:15 +00001462 int rc;
drhae2b40c2004-06-09 19:03:54 +00001463 PgHdr *pPg; /* An existing page in the cache */
1464 Pgno pgno; /* The page number of a page in journal */
1465 u32 cksum; /* Checksum used for sanity checking */
danielk19778186df82007-03-06 13:45:59 +00001466 u8 *aData = (u8 *)pPager->pTmpSpace; /* Temp storage for a page */
drhfa86c412002-02-02 15:01:15 +00001467
drh96362842005-03-20 22:47:56 +00001468 /* useCksum should be true for the main journal and false for
1469 ** statement journals. Verify that this is always the case
1470 */
drh9cbe6352005-11-29 03:13:21 +00001471 assert( jfd == (useCksum ? pPager->jfd : pPager->stfd) );
danielk19778186df82007-03-06 13:45:59 +00001472 assert( aData );
drh96362842005-03-20 22:47:56 +00001473
danielk197762079062007-08-15 17:08:46 +00001474 rc = read32bits(jfd, offset, &pgno);
drh99ee3602003-02-16 19:13:36 +00001475 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001476 rc = sqlite3OsRead(jfd, aData, pPager->pageSize, offset+4);
drh99ee3602003-02-16 19:13:36 +00001477 if( rc!=SQLITE_OK ) return rc;
danielk197776572402004-06-25 02:38:54 +00001478 pPager->journalOff += pPager->pageSize + 4;
drhfa86c412002-02-02 15:01:15 +00001479
drh968af522003-02-11 14:55:40 +00001480 /* Sanity checking on the page. This is more important that I originally
1481 ** thought. If a power failure occurs while the journal is being written,
1482 ** it could cause invalid data to be written into the journal. We need to
1483 ** detect this invalid data (with high probability) and ignore it.
1484 */
danielk197775edc162004-06-26 01:48:18 +00001485 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh968af522003-02-11 14:55:40 +00001486 return SQLITE_DONE;
1487 }
drhae2b40c2004-06-09 19:03:54 +00001488 if( pgno>(unsigned)pPager->dbSize ){
drh968af522003-02-11 14:55:40 +00001489 return SQLITE_OK;
1490 }
drhae2b40c2004-06-09 19:03:54 +00001491 if( useCksum ){
danielk197762079062007-08-15 17:08:46 +00001492 rc = read32bits(jfd, offset+pPager->pageSize+4, &cksum);
drh99ee3602003-02-16 19:13:36 +00001493 if( rc ) return rc;
danielk197776572402004-06-25 02:38:54 +00001494 pPager->journalOff += 4;
drh74161702006-02-24 02:53:49 +00001495 if( pager_cksum(pPager, aData)!=cksum ){
drh968af522003-02-11 14:55:40 +00001496 return SQLITE_DONE;
1497 }
1498 }
drhfa86c412002-02-02 15:01:15 +00001499
danielk1977aa5ccdf2004-06-14 05:10:42 +00001500 assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE );
danielk1977a3f3a5f2004-06-10 04:32:16 +00001501
1502 /* If the pager is in RESERVED state, then there must be a copy of this
1503 ** page in the pager cache. In this case just update the pager cache,
danielk19770de0bb32004-06-10 05:59:24 +00001504 ** not the database file. The page is left marked dirty in this case.
1505 **
danielk19772df71c72007-05-24 07:22:42 +00001506 ** An exception to the above rule: If the database is in no-sync mode
1507 ** and a page is moved during an incremental vacuum then the page may
danielk1977369f3a02007-05-24 09:41:20 +00001508 ** not be in the pager cache. Later: if a malloc() or IO error occurs
1509 ** during a Movepage() call, then the page may not be in the cache
1510 ** either. So the condition described in the above paragraph is not
1511 ** assert()able.
danielk19772df71c72007-05-24 07:22:42 +00001512 **
danielk1977a3f3a5f2004-06-10 04:32:16 +00001513 ** If in EXCLUSIVE state, then we update the pager cache if it exists
1514 ** and the main file. The page is then marked not dirty.
drh96362842005-03-20 22:47:56 +00001515 **
1516 ** Ticket #1171: The statement journal might contain page content that is
1517 ** different from the page content at the start of the transaction.
1518 ** This occurs when a page is changed prior to the start of a statement
1519 ** then changed again within the statement. When rolling back such a
1520 ** statement we must not write to the original database unless we know
drh5e385312007-06-16 04:42:12 +00001521 ** for certain that original page contents are synced into the main rollback
1522 ** journal. Otherwise, a power loss might leave modified data in the
1523 ** database file without an entry in the rollback journal that can
1524 ** restore the database to its original form. Two conditions must be
1525 ** met before writing to the database files. (1) the database must be
1526 ** locked. (2) we know that the original page content is fully synced
1527 ** in the main journal either because the page is not in cache or else
1528 ** the page is marked as needSync==0.
drh4c02a232008-04-14 23:13:45 +00001529 **
1530 ** 2008-04-14: When attempting to vacuum a corrupt database file, it
1531 ** is possible to fail a statement on a database that does not yet exist.
1532 ** Do not attempt to write if database file has never been opened.
drhfa86c412002-02-02 15:01:15 +00001533 */
drhae2b40c2004-06-09 19:03:54 +00001534 pPg = pager_lookup(pPager, pgno);
drh477731b2007-06-16 03:06:27 +00001535 PAGERTRACE4("PLAYBACK %d page %d hash(%08x)\n",
1536 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData));
drh4c02a232008-04-14 23:13:45 +00001537 if( pPager->state>=PAGER_EXCLUSIVE && (pPg==0 || pPg->needSync==0)
1538 && pPager->fd->pMethods ){
danielk197762079062007-08-15 17:08:46 +00001539 i64 offset = (pgno-1)*(i64)pPager->pageSize;
1540 rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, offset);
drh605ad8f2006-05-03 23:34:05 +00001541 if( pPg ){
1542 makeClean(pPg);
1543 }
danielk1977a3f3a5f2004-06-10 04:32:16 +00001544 }
drhfa86c412002-02-02 15:01:15 +00001545 if( pPg ){
danielk197728129562005-01-11 10:25:06 +00001546 /* No page should ever be explicitly rolled back that is in use, except
1547 ** for page 1 which is held in use in order to keep the lock on the
1548 ** database active. However such a page may be rolled back as a result
1549 ** of an internal error resulting in an automatic call to
danielk19773b8a05f2007-03-19 17:44:26 +00001550 ** sqlite3PagerRollback().
drhacf4ac92003-12-17 23:57:34 +00001551 */
drhb6f41482004-05-14 01:58:11 +00001552 void *pData;
danielk197728129562005-01-11 10:25:06 +00001553 /* assert( pPg->nRef==0 || pPg->pgno==1 ); */
drhb6f41482004-05-14 01:58:11 +00001554 pData = PGHDR_TO_DATA(pPg);
drhae2b40c2004-06-09 19:03:54 +00001555 memcpy(pData, aData, pPager->pageSize);
danielk19779038bb62007-04-09 11:20:54 +00001556 if( pPager->xReiniter ){
1557 pPager->xReiniter(pPg, pPager->pageSize);
drhde647132004-05-07 17:57:49 +00001558 }
danielk19773c407372005-02-15 02:54:14 +00001559#ifdef SQLITE_CHECK_PAGES
drh96362842005-03-20 22:47:56 +00001560 pPg->pageHash = pager_pagehash(pPg);
danielk19773c407372005-02-15 02:54:14 +00001561#endif
drh86a88112007-04-16 15:02:19 +00001562 /* If this was page 1, then restore the value of Pager.dbFileVers.
1563 ** Do this before any decoding. */
danielk197741483462007-03-24 16:45:04 +00001564 if( pgno==1 ){
drh86a88112007-04-16 15:02:19 +00001565 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
danielk197741483462007-03-24 16:45:04 +00001566 }
drh86a88112007-04-16 15:02:19 +00001567
1568 /* Decode the page just read from disk */
1569 CODEC1(pPager, pData, pPg->pgno, 3);
drhfa86c412002-02-02 15:01:15 +00001570 }
1571 return rc;
1572}
1573
1574/*
danielk197713adf8a2004-06-03 16:08:41 +00001575** Parameter zMaster is the name of a master journal file. A single journal
1576** file that referred to the master journal file has just been rolled back.
1577** This routine checks if it is possible to delete the master journal file,
1578** and does so if it is.
drh726de592004-06-10 23:35:50 +00001579**
danielk197765839c62007-08-30 08:08:17 +00001580** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not
1581** available for use within this function.
1582**
1583**
drh726de592004-06-10 23:35:50 +00001584** The master journal file contains the names of all child journals.
1585** To tell if a master journal can be deleted, check to each of the
1586** children. If all children are either missing or do not refer to
1587** a different master journal, then this master journal can be deleted.
danielk197713adf8a2004-06-03 16:08:41 +00001588*/
danielk1977b4b47412007-08-17 15:53:36 +00001589static int pager_delmaster(Pager *pPager, const char *zMaster){
1590 sqlite3_vfs *pVfs = pPager->pVfs;
danielk197713adf8a2004-06-03 16:08:41 +00001591 int rc;
1592 int master_open = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001593 sqlite3_file *pMaster;
1594 sqlite3_file *pJournal;
danielk197713adf8a2004-06-03 16:08:41 +00001595 char *zMasterJournal = 0; /* Contents of master journal file */
drheb206252004-10-01 02:00:31 +00001596 i64 nMasterJournal; /* Size of master journal file */
danielk197713adf8a2004-06-03 16:08:41 +00001597
1598 /* Open the master journal file exclusively in case some other process
1599 ** is running this routine also. Not that it makes too much difference.
1600 */
danielk1977b4b47412007-08-17 15:53:36 +00001601 pMaster = (sqlite3_file *)sqlite3_malloc(pVfs->szOsFile * 2);
danielk1977fee2d252007-08-18 10:59:19 +00001602 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
danielk1977b4b47412007-08-17 15:53:36 +00001603 if( !pMaster ){
1604 rc = SQLITE_NOMEM;
1605 }else{
danielk1977fee2d252007-08-18 10:59:19 +00001606 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
1607 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
danielk1977b4b47412007-08-17 15:53:36 +00001608 }
danielk197713adf8a2004-06-03 16:08:41 +00001609 if( rc!=SQLITE_OK ) goto delmaster_out;
1610 master_open = 1;
danielk1977b4b47412007-08-17 15:53:36 +00001611
1612 rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001613 if( rc!=SQLITE_OK ) goto delmaster_out;
1614
1615 if( nMasterJournal>0 ){
danielk19775865e3d2004-06-14 06:03:57 +00001616 char *zJournal;
danielk197776572402004-06-25 02:38:54 +00001617 char *zMasterPtr = 0;
danielk197765839c62007-08-30 08:08:17 +00001618 int nMasterPtr = pPager->pVfs->mxPathname+1;
danielk19775865e3d2004-06-14 06:03:57 +00001619
1620 /* Load the entire master journal file into space obtained from
drh17435752007-08-16 04:30:38 +00001621 ** sqlite3_malloc() and pointed to by zMasterJournal.
danielk19775865e3d2004-06-14 06:03:57 +00001622 */
danielk197765839c62007-08-30 08:08:17 +00001623 zMasterJournal = (char *)sqlite3_malloc(nMasterJournal + nMasterPtr);
danielk197713adf8a2004-06-03 16:08:41 +00001624 if( !zMasterJournal ){
1625 rc = SQLITE_NOMEM;
1626 goto delmaster_out;
1627 }
danielk197765839c62007-08-30 08:08:17 +00001628 zMasterPtr = &zMasterJournal[nMasterJournal];
danielk1977b4b47412007-08-17 15:53:36 +00001629 rc = sqlite3OsRead(pMaster, zMasterJournal, nMasterJournal, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001630 if( rc!=SQLITE_OK ) goto delmaster_out;
1631
danielk19775865e3d2004-06-14 06:03:57 +00001632 zJournal = zMasterJournal;
1633 while( (zJournal-zMasterJournal)<nMasterJournal ){
drh19db9352008-03-27 22:42:51 +00001634 rc = sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS);
1635 if( rc!=0 && rc!=1 ){
1636 rc = SQLITE_IOERR_NOMEM;
1637 goto delmaster_out;
1638 }
1639 if( rc==1 ){
danielk197713adf8a2004-06-03 16:08:41 +00001640 /* One of the journals pointed to by the master journal exists.
1641 ** Open it and check if it points at the master journal. If
1642 ** so, return without deleting the master journal file.
1643 */
drh3b7b78b2004-11-24 01:16:43 +00001644 int c;
danielk1977fee2d252007-08-18 10:59:19 +00001645 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
1646 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001647 if( rc!=SQLITE_OK ){
danielk197713adf8a2004-06-03 16:08:41 +00001648 goto delmaster_out;
1649 }
danielk197713adf8a2004-06-03 16:08:41 +00001650
danielk197765839c62007-08-30 08:08:17 +00001651 rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
danielk1977b4b47412007-08-17 15:53:36 +00001652 sqlite3OsClose(pJournal);
danielk19779eed5052004-06-04 10:38:30 +00001653 if( rc!=SQLITE_OK ){
danielk19779eed5052004-06-04 10:38:30 +00001654 goto delmaster_out;
1655 }
danielk197776572402004-06-25 02:38:54 +00001656
danielk197765839c62007-08-30 08:08:17 +00001657 c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
drh3b7b78b2004-11-24 01:16:43 +00001658 if( c ){
danielk197776572402004-06-25 02:38:54 +00001659 /* We have a match. Do not delete the master journal file. */
1660 goto delmaster_out;
danielk197713adf8a2004-06-03 16:08:41 +00001661 }
1662 }
danielk19775865e3d2004-06-14 06:03:57 +00001663 zJournal += (strlen(zJournal)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001664 }
1665 }
1666
danielk1977fee2d252007-08-18 10:59:19 +00001667 rc = sqlite3OsDelete(pVfs, zMaster, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001668
1669delmaster_out:
1670 if( zMasterJournal ){
drh17435752007-08-16 04:30:38 +00001671 sqlite3_free(zMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001672 }
1673 if( master_open ){
danielk1977b4b47412007-08-17 15:53:36 +00001674 sqlite3OsClose(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001675 }
danielk1977b4b47412007-08-17 15:53:36 +00001676 sqlite3_free(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001677 return rc;
1678}
1679
drha6abd042004-06-09 17:37:22 +00001680
danielk1977e180dd92007-04-05 17:15:52 +00001681static void pager_truncate_cache(Pager *pPager);
1682
drha6abd042004-06-09 17:37:22 +00001683/*
drhcb4c40b2004-08-18 19:09:43 +00001684** Truncate the main file of the given pager to the number of pages
danielk1977e180dd92007-04-05 17:15:52 +00001685** indicated. Also truncate the cached representation of the file.
drh7fe3f7e2007-11-29 18:44:27 +00001686**
1687** Might might be the case that the file on disk is smaller than nPage.
1688** This can happen, for example, if we are in the middle of a transaction
1689** which has extended the file size and the new pages are still all held
1690** in cache, then an INSERT or UPDATE does a statement rollback. Some
1691** operating system implementations can get confused if you try to
1692** truncate a file to some size that is larger than it currently is,
1693** so detect this case and do not do the truncation.
drhcb4c40b2004-08-18 19:09:43 +00001694*/
1695static int pager_truncate(Pager *pPager, int nPage){
danielk1977e180dd92007-04-05 17:15:52 +00001696 int rc = SQLITE_OK;
danielk19777a2b1ee2007-08-21 14:27:01 +00001697 if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){
drh7fe3f7e2007-11-29 18:44:27 +00001698 i64 currentSize, newSize;
1699 rc = sqlite3OsFileSize(pPager->fd, &currentSize);
1700 newSize = pPager->pageSize*(i64)nPage;
1701 if( rc==SQLITE_OK && currentSize>newSize ){
1702 rc = sqlite3OsTruncate(pPager->fd, newSize);
1703 }
danielk1977e180dd92007-04-05 17:15:52 +00001704 }
1705 if( rc==SQLITE_OK ){
1706 pPager->dbSize = nPage;
1707 pager_truncate_cache(pPager);
1708 }
1709 return rc;
drhcb4c40b2004-08-18 19:09:43 +00001710}
1711
1712/*
drhc80f0582007-05-01 16:59:48 +00001713** Set the sectorSize for the given pager.
1714**
drh334c80d2008-03-28 17:41:13 +00001715** The sector size is at least as big as the sector size reported
1716** by sqlite3OsSectorSize(). The minimum sector size is 512.
drhc80f0582007-05-01 16:59:48 +00001717*/
1718static void setSectorSize(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00001719 assert(pPager->fd->pMethods||pPager->tempFile);
1720 if( !pPager->tempFile ){
1721 /* Sector size doesn't matter for temporary files. Also, the file
1722 ** may not have been opened yet, in whcih case the OsSectorSize()
1723 ** call will segfault.
1724 */
1725 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
1726 }
drh334c80d2008-03-28 17:41:13 +00001727 if( pPager->sectorSize<512 ){
1728 pPager->sectorSize = 512;
drhc80f0582007-05-01 16:59:48 +00001729 }
1730}
1731
1732/*
drhed7c8552001-04-11 14:29:21 +00001733** Playback the journal and thus restore the database file to
1734** the state it was in before we started making changes.
1735**
drh34e79ce2004-02-08 06:05:46 +00001736** The journal file format is as follows:
1737**
drhae2b40c2004-06-09 19:03:54 +00001738** (1) 8 byte prefix. A copy of aJournalMagic[].
1739** (2) 4 byte big-endian integer which is the number of valid page records
drh34e79ce2004-02-08 06:05:46 +00001740** in the journal. If this value is 0xffffffff, then compute the
drhae2b40c2004-06-09 19:03:54 +00001741** number of page records from the journal size.
1742** (3) 4 byte big-endian integer which is the initial value for the
1743** sanity checksum.
1744** (4) 4 byte integer which is the number of pages to truncate the
drh34e79ce2004-02-08 06:05:46 +00001745** database to during a rollback.
drh334c80d2008-03-28 17:41:13 +00001746** (5) 4 byte big-endian integer which is the sector size. The header
1747** is this many bytes in size.
1748** (6) 4 byte big-endian integer which is the page case.
1749** (7) 4 byte integer which is the number of bytes in the master journal
drhae2b40c2004-06-09 19:03:54 +00001750** name. The value may be zero (indicate that there is no master
1751** journal.)
drh334c80d2008-03-28 17:41:13 +00001752** (8) N bytes of the master journal name. The name will be nul-terminated
drhae2b40c2004-06-09 19:03:54 +00001753** and might be shorter than the value read from (5). If the first byte
1754** of the name is \000 then there is no master journal. The master
1755** journal name is stored in UTF-8.
drh334c80d2008-03-28 17:41:13 +00001756** (9) Zero or more pages instances, each as follows:
drh34e79ce2004-02-08 06:05:46 +00001757** + 4 byte page number.
drhae2b40c2004-06-09 19:03:54 +00001758** + pPager->pageSize bytes of data.
1759** + 4 byte checksum
drh34e79ce2004-02-08 06:05:46 +00001760**
drh334c80d2008-03-28 17:41:13 +00001761** When we speak of the journal header, we mean the first 8 items above.
1762** Each entry in the journal is an instance of the 9th item.
drh34e79ce2004-02-08 06:05:46 +00001763**
1764** Call the value from the second bullet "nRec". nRec is the number of
1765** valid page entries in the journal. In most cases, you can compute the
1766** value of nRec from the size of the journal file. But if a power
1767** failure occurred while the journal was being written, it could be the
1768** case that the size of the journal file had already been increased but
1769** the extra entries had not yet made it safely to disk. In such a case,
1770** the value of nRec computed from the file size would be too large. For
1771** that reason, we always use the nRec value in the header.
1772**
1773** If the nRec value is 0xffffffff it means that nRec should be computed
1774** from the file size. This value is used when the user selects the
1775** no-sync option for the journal. A power failure could lead to corruption
1776** in this case. But for things like temporary table (which will be
1777** deleted when the power is restored) we don't care.
1778**
drhd9b02572001-04-15 00:37:09 +00001779** If the file opened as the journal file is not a well-formed
danielk1977ece80f12004-06-23 01:05:26 +00001780** journal file then all pages up to the first corrupted page are rolled
1781** back (or no pages if the journal header is corrupted). The journal file
1782** is then deleted and SQLITE_OK returned, just as if no corruption had
1783** been encountered.
1784**
1785** If an I/O or malloc() error occurs, the journal-file is not deleted
1786** and an error code is returned.
drhed7c8552001-04-11 14:29:21 +00001787*/
danielk1977e277be02007-03-23 18:12:06 +00001788static int pager_playback(Pager *pPager, int isHot){
danielk1977b4b47412007-08-17 15:53:36 +00001789 sqlite3_vfs *pVfs = pPager->pVfs;
drheb206252004-10-01 02:00:31 +00001790 i64 szJ; /* Size of the journal file in bytes */
danielk1977c3e8f5e2004-06-28 01:16:46 +00001791 u32 nRec; /* Number of Records in the journal */
drhd9b02572001-04-15 00:37:09 +00001792 int i; /* Loop counter */
1793 Pgno mxPg = 0; /* Size of the original file in pages */
drhae2b40c2004-06-09 19:03:54 +00001794 int rc; /* Result code of a subroutine */
danielk1977ce98bba2008-04-03 10:13:01 +00001795 int res = 0; /* Value returned by sqlite3OsAccess() */
danielk197713adf8a2004-06-03 16:08:41 +00001796 char *zMaster = 0; /* Name of master journal file if any */
drhed7c8552001-04-11 14:29:21 +00001797
drhc3a64ba2001-11-22 00:01:27 +00001798 /* Figure out how many records are in the journal. Abort early if
1799 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +00001800 */
drh8cfbf082001-09-19 13:22:39 +00001801 assert( pPager->journalOpen );
drh054889e2005-11-30 03:20:31 +00001802 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
danielk1977334cdb62007-03-26 08:05:12 +00001803 if( rc!=SQLITE_OK || szJ==0 ){
drhc3a64ba2001-11-22 00:01:27 +00001804 goto end_playback;
1805 }
drh240c5792004-02-08 00:40:52 +00001806
danielk197776572402004-06-25 02:38:54 +00001807 /* Read the master journal name from the journal, if it is present.
1808 ** If a master journal file name is specified, but the file is not
1809 ** present on disk, then the journal is not hot and does not need to be
1810 ** played back.
drh240c5792004-02-08 00:40:52 +00001811 */
danielk197765839c62007-08-30 08:08:17 +00001812 zMaster = pPager->pTmpSpace;
1813 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
danielk1977ce98bba2008-04-03 10:13:01 +00001814 if( rc!=SQLITE_OK || (zMaster[0]
1815 && (res=sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS))==0 )
danielk1977b4b47412007-08-17 15:53:36 +00001816 ){
danielk197776572402004-06-25 02:38:54 +00001817 zMaster = 0;
drhc3a64ba2001-11-22 00:01:27 +00001818 goto end_playback;
1819 }
danielk197765839c62007-08-30 08:08:17 +00001820 zMaster = 0;
danielk1977ce98bba2008-04-03 10:13:01 +00001821 if( res<0 ){
1822 rc = SQLITE_IOERR_NOMEM;
1823 goto end_playback;
1824 }
1825 pPager->journalOff = 0;
drhc3a64ba2001-11-22 00:01:27 +00001826
danielk197776572402004-06-25 02:38:54 +00001827 /* This loop terminates either when the readJournalHdr() call returns
1828 ** SQLITE_DONE or an IO error occurs. */
1829 while( 1 ){
drhae2b40c2004-06-09 19:03:54 +00001830
danielk197776572402004-06-25 02:38:54 +00001831 /* Read the next journal header from the journal file. If there are
1832 ** not enough bytes left in the journal file for a complete header, or
1833 ** it is corrupted, then a process must of failed while writing it.
1834 ** This indicates nothing more needs to be rolled back.
1835 */
1836 rc = readJournalHdr(pPager, szJ, &nRec, &mxPg);
1837 if( rc!=SQLITE_OK ){
drh968af522003-02-11 14:55:40 +00001838 if( rc==SQLITE_DONE ){
drh968af522003-02-11 14:55:40 +00001839 rc = SQLITE_OK;
1840 }
danielk197776572402004-06-25 02:38:54 +00001841 goto end_playback;
1842 }
1843
1844 /* If nRec is 0xffffffff, then this journal was created by a process
1845 ** working in no-sync mode. This means that the rest of the journal
1846 ** file consists of pages, there are no more journal headers. Compute
1847 ** the value of nRec based on this assumption.
1848 */
1849 if( nRec==0xffffffff ){
1850 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
1851 nRec = (szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager);
1852 }
1853
danielk1977e277be02007-03-23 18:12:06 +00001854 /* If nRec is 0 and this rollback is of a transaction created by this
drh8940f4e2007-08-11 00:26:20 +00001855 ** process and if this is the final header in the journal, then it means
1856 ** that this part of the journal was being filled but has not yet been
1857 ** synced to disk. Compute the number of pages based on the remaining
1858 ** size of the file.
1859 **
1860 ** The third term of the test was added to fix ticket #2565.
danielk1977e277be02007-03-23 18:12:06 +00001861 */
drh8940f4e2007-08-11 00:26:20 +00001862 if( nRec==0 && !isHot &&
1863 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
danielk1977e277be02007-03-23 18:12:06 +00001864 nRec = (szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager);
1865 }
1866
danielk197776572402004-06-25 02:38:54 +00001867 /* If this is the first header read from the journal, truncate the
drh85b623f2007-12-13 21:54:09 +00001868 ** database file back to its original size.
danielk197776572402004-06-25 02:38:54 +00001869 */
danielk1977e180dd92007-04-05 17:15:52 +00001870 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
drhcb4c40b2004-08-18 19:09:43 +00001871 rc = pager_truncate(pPager, mxPg);
danielk197776572402004-06-25 02:38:54 +00001872 if( rc!=SQLITE_OK ){
1873 goto end_playback;
1874 }
danielk197776572402004-06-25 02:38:54 +00001875 }
1876
danielk197776572402004-06-25 02:38:54 +00001877 /* Copy original pages out of the journal and back into the database file.
1878 */
1879 for(i=0; i<nRec; i++){
danielk197762079062007-08-15 17:08:46 +00001880 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001881 if( rc!=SQLITE_OK ){
1882 if( rc==SQLITE_DONE ){
1883 rc = SQLITE_OK;
1884 pPager->journalOff = szJ;
1885 break;
1886 }else{
1887 goto end_playback;
1888 }
1889 }
drh968af522003-02-11 14:55:40 +00001890 }
drhed7c8552001-04-11 14:29:21 +00001891 }
drh580eeaf2006-02-24 03:09:37 +00001892 /*NOTREACHED*/
1893 assert( 0 );
drh4a0681e2003-02-13 01:58:20 +00001894
1895end_playback:
danielk19778191bff2004-06-28 04:52:30 +00001896 if( rc==SQLITE_OK ){
danielk197765839c62007-08-30 08:08:17 +00001897 zMaster = pPager->pTmpSpace;
1898 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
1899 }
1900 if( rc==SQLITE_OK ){
drh80e35f42007-03-30 14:06:34 +00001901 rc = pager_end_transaction(pPager);
danielk19778191bff2004-06-28 04:52:30 +00001902 }
danielk197765839c62007-08-30 08:08:17 +00001903 if( rc==SQLITE_OK && zMaster[0] ){
danielk1977979f38e2007-03-27 16:19:51 +00001904 /* If there was a master journal and this routine will return success,
danielk197732554c12005-01-22 03:39:39 +00001905 ** see if it is possible to delete the master journal.
danielk197713adf8a2004-06-03 16:08:41 +00001906 */
danielk197765839c62007-08-30 08:08:17 +00001907 rc = pager_delmaster(pPager, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001908 }
danielk197776572402004-06-25 02:38:54 +00001909
1910 /* The Pager.sectorSize variable may have been updated while rolling
drh3ceeb752007-03-29 18:19:52 +00001911 ** back a journal created by a process with a different sector size
danielk197776572402004-06-25 02:38:54 +00001912 ** value. Reset it to the correct value for this process.
1913 */
drhc80f0582007-05-01 16:59:48 +00001914 setSectorSize(pPager);
drhd9b02572001-04-15 00:37:09 +00001915 return rc;
drhed7c8552001-04-11 14:29:21 +00001916}
1917
1918/*
drhac69b052004-05-12 13:30:07 +00001919** Playback the statement journal.
drhfa86c412002-02-02 15:01:15 +00001920**
1921** This is similar to playing back the transaction journal but with
1922** a few extra twists.
1923**
drh663fc632002-02-02 18:49:19 +00001924** (1) The number of pages in the database file at the start of
drhac69b052004-05-12 13:30:07 +00001925** the statement is stored in pPager->stmtSize, not in the
drh663fc632002-02-02 18:49:19 +00001926** journal file itself.
drhfa86c412002-02-02 15:01:15 +00001927**
drhac69b052004-05-12 13:30:07 +00001928** (2) In addition to playing back the statement journal, also
drhfa86c412002-02-02 15:01:15 +00001929** playback all pages of the transaction journal beginning
drhac69b052004-05-12 13:30:07 +00001930** at offset pPager->stmtJSize.
drhfa86c412002-02-02 15:01:15 +00001931*/
drh3aac2dd2004-04-26 14:10:20 +00001932static int pager_stmt_playback(Pager *pPager){
drheb206252004-10-01 02:00:31 +00001933 i64 szJ; /* Size of the full journal */
1934 i64 hdrOff;
drh968af522003-02-11 14:55:40 +00001935 int nRec; /* Number of Records */
drhfa86c412002-02-02 15:01:15 +00001936 int i; /* Loop counter */
1937 int rc;
1938
danielk197776572402004-06-25 02:38:54 +00001939 szJ = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +00001940
danielk19774099f6e2007-03-19 11:25:20 +00001941 /* Set hdrOff to be the offset just after the end of the last journal
1942 ** page written before the first journal-header for this statement
1943 ** transaction was written, or the end of the file if no journal
danielk197776572402004-06-25 02:38:54 +00001944 ** header was written.
1945 */
1946 hdrOff = pPager->stmtHdrOff;
1947 assert( pPager->fullSync || !hdrOff );
1948 if( !hdrOff ){
1949 hdrOff = szJ;
1950 }
1951
drhfa86c412002-02-02 15:01:15 +00001952 /* Truncate the database back to its original size.
1953 */
danielk1977e180dd92007-04-05 17:15:52 +00001954 rc = pager_truncate(pPager, pPager->stmtSize);
drh1aa2d8b2007-01-03 15:34:29 +00001955 assert( pPager->state>=PAGER_SHARED );
drhfa86c412002-02-02 15:01:15 +00001956
drhac69b052004-05-12 13:30:07 +00001957 /* Figure out how many records are in the statement journal.
drhfa86c412002-02-02 15:01:15 +00001958 */
drhac69b052004-05-12 13:30:07 +00001959 assert( pPager->stmtInUse && pPager->journalOpen );
drhac69b052004-05-12 13:30:07 +00001960 nRec = pPager->stmtNRec;
drhfa86c412002-02-02 15:01:15 +00001961
drhac69b052004-05-12 13:30:07 +00001962 /* Copy original pages out of the statement journal and back into the
drhae2b40c2004-06-09 19:03:54 +00001963 ** database file. Note that the statement journal omits checksums from
1964 ** each record since power-failure recovery is not important to statement
1965 ** journals.
drhfa86c412002-02-02 15:01:15 +00001966 */
danielk197762079062007-08-15 17:08:46 +00001967 for(i=0; i<nRec; i++){
1968 i64 offset = i*(4+pPager->pageSize);
1969 rc = pager_playback_one_page(pPager, pPager->stfd, offset, 0);
drh968af522003-02-11 14:55:40 +00001970 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001971 if( rc!=SQLITE_OK ) goto end_stmt_playback;
drhfa86c412002-02-02 15:01:15 +00001972 }
1973
danielk197776572402004-06-25 02:38:54 +00001974 /* Now roll some pages back from the transaction journal. Pager.stmtJSize
1975 ** was the size of the journal file when this statement was started, so
1976 ** everything after that needs to be rolled back, either into the
1977 ** database, the memory cache, or both.
1978 **
1979 ** If it is not zero, then Pager.stmtHdrOff is the offset to the start
1980 ** of the first journal header written during this statement transaction.
drhfa86c412002-02-02 15:01:15 +00001981 */
danielk197776572402004-06-25 02:38:54 +00001982 pPager->journalOff = pPager->stmtJSize;
danielk197775edc162004-06-26 01:48:18 +00001983 pPager->cksumInit = pPager->stmtCksum;
danielk19774099f6e2007-03-19 11:25:20 +00001984 while( pPager->journalOff < hdrOff ){
danielk197762079062007-08-15 17:08:46 +00001985 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001986 assert( rc!=SQLITE_DONE );
1987 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1988 }
1989
1990 while( pPager->journalOff < szJ ){
danielk1977f0113002006-01-24 12:09:17 +00001991 u32 nJRec; /* Number of Journal Records */
danielk197776572402004-06-25 02:38:54 +00001992 u32 dummy;
danielk1977f0113002006-01-24 12:09:17 +00001993 rc = readJournalHdr(pPager, szJ, &nJRec, &dummy);
drh968af522003-02-11 14:55:40 +00001994 if( rc!=SQLITE_OK ){
1995 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001996 goto end_stmt_playback;
drh968af522003-02-11 14:55:40 +00001997 }
danielk1977f0113002006-01-24 12:09:17 +00001998 if( nJRec==0 ){
1999 nJRec = (szJ - pPager->journalOff) / (pPager->pageSize+8);
danielk197775edc162004-06-26 01:48:18 +00002000 }
danielk1977f0113002006-01-24 12:09:17 +00002001 for(i=nJRec-1; i>=0 && pPager->journalOff < szJ; i--){
danielk197762079062007-08-15 17:08:46 +00002002 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00002003 assert( rc!=SQLITE_DONE );
2004 if( rc!=SQLITE_OK ) goto end_stmt_playback;
2005 }
drhfa86c412002-02-02 15:01:15 +00002006 }
danielk197776572402004-06-25 02:38:54 +00002007
2008 pPager->journalOff = szJ;
drhfa86c412002-02-02 15:01:15 +00002009
drh3aac2dd2004-04-26 14:10:20 +00002010end_stmt_playback:
danielk19778a7aea32006-01-23 15:25:48 +00002011 if( rc==SQLITE_OK) {
danielk197775edc162004-06-26 01:48:18 +00002012 pPager->journalOff = szJ;
2013 /* pager_reload_cache(pPager); */
drhfa86c412002-02-02 15:01:15 +00002014 }
2015 return rc;
2016}
2017
2018/*
drhf57b14a2001-09-14 18:54:08 +00002019** Change the maximum number of in-memory pages that are allowed.
2020*/
danielk19773b8a05f2007-03-19 17:44:26 +00002021void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
drhf57b14a2001-09-14 18:54:08 +00002022 if( mxPage>10 ){
2023 pPager->mxPage = mxPage;
danielk1977ef317ab2004-06-23 10:43:10 +00002024 }else{
2025 pPager->mxPage = 10;
drhf57b14a2001-09-14 18:54:08 +00002026 }
2027}
2028
2029/*
drh973b6e32003-02-12 14:09:42 +00002030** Adjust the robustness of the database to damage due to OS crashes
2031** or power failures by changing the number of syncs()s when writing
2032** the rollback journal. There are three levels:
2033**
drh054889e2005-11-30 03:20:31 +00002034** OFF sqlite3OsSync() is never called. This is the default
drh973b6e32003-02-12 14:09:42 +00002035** for temporary and transient files.
2036**
2037** NORMAL The journal is synced once before writes begin on the
2038** database. This is normally adequate protection, but
2039** it is theoretically possible, though very unlikely,
2040** that an inopertune power failure could leave the journal
2041** in a state which would cause damage to the database
2042** when it is rolled back.
2043**
2044** FULL The journal is synced twice before writes begin on the
drh34e79ce2004-02-08 06:05:46 +00002045** database (with some additional information - the nRec field
2046** of the journal header - being written in between the two
2047** syncs). If we assume that writing a
drh973b6e32003-02-12 14:09:42 +00002048** single disk sector is atomic, then this mode provides
2049** assurance that the journal will not be corrupted to the
2050** point of causing damage to the database during rollback.
2051**
2052** Numeric values associated with these states are OFF==1, NORMAL=2,
2053** and FULL=3.
2054*/
danielk197793758c82005-01-21 08:13:14 +00002055#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk19773b8a05f2007-03-19 17:44:26 +00002056void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int full_fsync){
drh973b6e32003-02-12 14:09:42 +00002057 pPager->noSync = level==1 || pPager->tempFile;
2058 pPager->fullSync = level==3 && !pPager->tempFile;
danielk1977f036aef2007-08-20 05:36:51 +00002059 pPager->sync_flags = (full_fsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL);
danielk19771d850a72004-05-31 08:26:49 +00002060 if( pPager->noSync ) pPager->needSync = 0;
drh973b6e32003-02-12 14:09:42 +00002061}
danielk197793758c82005-01-21 08:13:14 +00002062#endif
drh973b6e32003-02-12 14:09:42 +00002063
2064/*
drhaf6df112005-06-07 02:12:30 +00002065** The following global variable is incremented whenever the library
2066** attempts to open a temporary file. This information is used for
2067** testing and analysis only.
2068*/
drh0f7eb612006-08-08 13:51:43 +00002069#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00002070int sqlite3_opentemp_count = 0;
drh0f7eb612006-08-08 13:51:43 +00002071#endif
drhaf6df112005-06-07 02:12:30 +00002072
2073/*
drh3f56e6e2007-03-15 01:16:47 +00002074** Open a temporary file.
2075**
2076** Write the file descriptor into *fd. Return SQLITE_OK on success or some
danielk1977fee2d252007-08-18 10:59:19 +00002077** other error code if we fail. The OS will automatically delete the temporary
2078** file when it is closed.
drhfa86c412002-02-02 15:01:15 +00002079*/
danielk1977b4b47412007-08-17 15:53:36 +00002080static int sqlite3PagerOpentemp(
drh33f4e022007-09-03 15:19:34 +00002081 sqlite3_vfs *pVfs, /* The virtual file system layer */
2082 sqlite3_file *pFile, /* Write the file descriptor here */
2083 char *zFilename, /* Name of the file. Might be NULL */
2084 int vfsFlags /* Flags passed through to the VFS */
danielk1977b4b47412007-08-17 15:53:36 +00002085){
drhfa86c412002-02-02 15:01:15 +00002086 int rc;
drh334b2992007-09-06 23:28:23 +00002087 assert( zFilename!=0 );
drh3f56e6e2007-03-15 01:16:47 +00002088
drh0f7eb612006-08-08 13:51:43 +00002089#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00002090 sqlite3_opentemp_count++; /* Used for testing and analysis only */
drh0f7eb612006-08-08 13:51:43 +00002091#endif
danielk1977b4b47412007-08-17 15:53:36 +00002092
drh33f4e022007-09-03 15:19:34 +00002093 vfsFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
2094 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
2095 rc = sqlite3OsOpen(pVfs, zFilename, pFile, vfsFlags, 0);
drhae28c012007-08-24 16:29:23 +00002096 assert( rc!=SQLITE_OK || pFile->pMethods );
drhfa86c412002-02-02 15:01:15 +00002097 return rc;
2098}
2099
2100/*
drhed7c8552001-04-11 14:29:21 +00002101** Create a new page cache and put a pointer to the page cache in *ppPager.
drh5e00f6c2001-09-13 13:46:56 +00002102** The file to be cached need not exist. The file is not locked until
danielk19773b8a05f2007-03-19 17:44:26 +00002103** the first call to sqlite3PagerGet() and is only held open until the
2104** last page is released using sqlite3PagerUnref().
drh382c0242001-10-06 16:33:02 +00002105**
drh6446c4d2001-12-15 14:22:18 +00002106** If zFilename is NULL then a randomly-named temporary file is created
drh1cc8c442007-08-24 16:08:29 +00002107** and used as the file to be cached. The file will be deleted
drh6446c4d2001-12-15 14:22:18 +00002108** automatically when it is closed.
drh90f5ecb2004-07-22 01:19:35 +00002109**
2110** If zFilename is ":memory:" then all information is held in cache.
2111** It is never written to disk. This can be used to implement an
2112** in-memory database.
drhed7c8552001-04-11 14:29:21 +00002113*/
danielk19773b8a05f2007-03-19 17:44:26 +00002114int sqlite3PagerOpen(
drh86f8c192007-08-22 00:39:19 +00002115 sqlite3_vfs *pVfs, /* The virtual file system to use */
drh7e3b0a02001-04-28 16:52:40 +00002116 Pager **ppPager, /* Return the Pager structure here */
2117 const char *zFilename, /* Name of the database file to open */
drhda47d772002-12-02 04:25:19 +00002118 int nExtra, /* Extra bytes append to each in-memory page */
drh33f4e022007-09-03 15:19:34 +00002119 int flags, /* flags controlling this file */
2120 int vfsFlags /* flags passed through to sqlite3_vfs.xOpen() */
drh7e3b0a02001-04-28 16:52:40 +00002121){
danielk1977b4b47412007-08-17 15:53:36 +00002122 u8 *pPtr;
danielk1977aef0bf62005-12-30 16:28:01 +00002123 Pager *pPager = 0;
danielk1977cfe9a692004-06-16 12:00:29 +00002124 int rc = SQLITE_OK;
2125 int i;
danielk19778def5ea2004-06-16 10:39:52 +00002126 int tempFile = 0;
drhac69b052004-05-12 13:30:07 +00002127 int memDb = 0;
drh5e00f6c2001-09-13 13:46:56 +00002128 int readOnly = 0;
drh7bec5052005-02-06 02:45:41 +00002129 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0;
2130 int noReadlock = (flags & PAGER_NO_READLOCK)!=0;
danielk1977c7b60172007-08-22 11:22:03 +00002131 int journalFileSize = sqlite3JournalSize(pVfs);
drh1cc8c442007-08-24 16:08:29 +00002132 int nDefaultPage = SQLITE_DEFAULT_PAGE_SIZE;
2133 char *zPathname;
2134 int nPathname;
drh99b90c32008-03-17 13:50:58 +00002135 char *zStmtJrnl;
2136 int nStmtJrnl;
danielk1977b4b47412007-08-17 15:53:36 +00002137
drh86f8c192007-08-22 00:39:19 +00002138 /* The default return is a NULL pointer */
drhd9b02572001-04-15 00:37:09 +00002139 *ppPager = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00002140
drh1cc8c442007-08-24 16:08:29 +00002141 /* Compute the full pathname */
danielk1977adfb9b02007-09-17 07:02:56 +00002142 nPathname = pVfs->mxPathname+1;
drh99b90c32008-03-17 13:50:58 +00002143 zPathname = sqlite3_malloc(nPathname*2);
drh1cc8c442007-08-24 16:08:29 +00002144 if( zPathname==0 ){
2145 return SQLITE_NOMEM;
2146 }
2147 if( zFilename && zFilename[0] ){
2148#ifndef SQLITE_OMIT_MEMORYDB
2149 if( strcmp(zFilename,":memory:")==0 ){
2150 memDb = 1;
2151 zPathname[0] = 0;
2152 }else
2153#endif
2154 {
danielk1977adfb9b02007-09-17 07:02:56 +00002155 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
drh1cc8c442007-08-24 16:08:29 +00002156 }
2157 }else{
danielk1977adfb9b02007-09-17 07:02:56 +00002158 rc = sqlite3OsGetTempname(pVfs, nPathname, zPathname);
drhae28c012007-08-24 16:29:23 +00002159 }
2160 if( rc!=SQLITE_OK ){
2161 sqlite3_free(zPathname);
2162 return rc;
drh1cc8c442007-08-24 16:08:29 +00002163 }
2164 nPathname = strlen(zPathname);
2165
drh99b90c32008-03-17 13:50:58 +00002166 /* Put the statement journal in temporary disk space since this is
2167 ** sometimes RAM disk or other optimized storage. Unlikely the main
2168 ** main journal file, the statement journal does not need to be
2169 ** colocated with the database nor does it need to be persistent.
2170 */
2171 zStmtJrnl = &zPathname[nPathname+1];
2172 rc = sqlite3OsGetTempname(pVfs, pVfs->mxPathname+1, zStmtJrnl);
2173 if( rc!=SQLITE_OK ){
2174 sqlite3_free(zPathname);
2175 return rc;
2176 }
2177 nStmtJrnl = strlen(zStmtJrnl);
2178
danielk1977b4b47412007-08-17 15:53:36 +00002179 /* Allocate memory for the pager structure */
2180 pPager = sqlite3MallocZero(
2181 sizeof(*pPager) + /* Pager structure */
danielk1977c7b60172007-08-22 11:22:03 +00002182 journalFileSize + /* The journal file structure */
drh17c0ba22008-02-26 18:40:11 +00002183 pVfs->szOsFile * 3 + /* The main db and two journal files */
drh99b90c32008-03-17 13:50:58 +00002184 3*nPathname + 40 + /* zFilename, zDirectory, zJournal */
2185 nStmtJrnl /* zStmtJrnl */
danielk1977b4b47412007-08-17 15:53:36 +00002186 );
2187 if( !pPager ){
drh1cc8c442007-08-24 16:08:29 +00002188 sqlite3_free(zPathname);
danielk1977b4b47412007-08-17 15:53:36 +00002189 return SQLITE_NOMEM;
2190 }
2191 pPtr = (u8 *)&pPager[1];
drh33f4e022007-09-03 15:19:34 +00002192 pPager->vfsFlags = vfsFlags;
danielk1977b4b47412007-08-17 15:53:36 +00002193 pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0];
danielk1977c7b60172007-08-22 11:22:03 +00002194 pPager->stfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*1];
2195 pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*2];
2196 pPager->zFilename = (char*)&pPtr[pVfs->szOsFile*2+journalFileSize];
drh1cc8c442007-08-24 16:08:29 +00002197 pPager->zDirectory = &pPager->zFilename[nPathname+1];
2198 pPager->zJournal = &pPager->zDirectory[nPathname+1];
drh334b2992007-09-06 23:28:23 +00002199 pPager->zStmtJrnl = &pPager->zJournal[nPathname+10];
danielk1977b4b47412007-08-17 15:53:36 +00002200 pPager->pVfs = pVfs;
drh1cc8c442007-08-24 16:08:29 +00002201 memcpy(pPager->zFilename, zPathname, nPathname+1);
drh99b90c32008-03-17 13:50:58 +00002202 memcpy(pPager->zStmtJrnl, zStmtJrnl, nStmtJrnl+1);
drh1cc8c442007-08-24 16:08:29 +00002203 sqlite3_free(zPathname);
danielk1977b4b47412007-08-17 15:53:36 +00002204
drh153c62c2007-08-24 03:51:33 +00002205 /* Open the pager file.
danielk1977aef0bf62005-12-30 16:28:01 +00002206 */
drhae28c012007-08-24 16:29:23 +00002207 if( zFilename && zFilename[0] && !memDb ){
drh1cc8c442007-08-24 16:08:29 +00002208 if( nPathname>(pVfs->mxPathname - sizeof("-journal")) ){
2209 rc = SQLITE_CANTOPEN;
2210 }else{
drh1cc8c442007-08-24 16:08:29 +00002211 int fout = 0;
drh33f4e022007-09-03 15:19:34 +00002212 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd,
2213 pPager->vfsFlags, &fout);
drh1cc8c442007-08-24 16:08:29 +00002214 readOnly = (fout&SQLITE_OPEN_READONLY);
2215
2216 /* If the file was successfully opened for read/write access,
2217 ** choose a default page size in case we have to create the
2218 ** database file. The default page size is the maximum of:
2219 **
2220 ** + SQLITE_DEFAULT_PAGE_SIZE,
2221 ** + The value returned by sqlite3OsSectorSize()
2222 ** + The largest page size that can be written atomically.
2223 */
2224 if( rc==SQLITE_OK && !readOnly ){
2225 int iSectorSize = sqlite3OsSectorSize(pPager->fd);
2226 if( nDefaultPage<iSectorSize ){
2227 nDefaultPage = iSectorSize;
2228 }
danielk19779663b8f2007-08-24 11:52:28 +00002229#ifdef SQLITE_ENABLE_ATOMIC_WRITE
drh1cc8c442007-08-24 16:08:29 +00002230 {
2231 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
2232 int ii;
2233 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
2234 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
2235 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
2236 for(ii=nDefaultPage; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
2237 if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ) nDefaultPage = ii;
danielk19779663b8f2007-08-24 11:52:28 +00002238 }
danielk1977b4b47412007-08-17 15:53:36 +00002239 }
drh1cc8c442007-08-24 16:08:29 +00002240#endif
2241 if( nDefaultPage>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
2242 nDefaultPage = SQLITE_MAX_DEFAULT_PAGE_SIZE;
2243 }
danielk19778def5ea2004-06-16 10:39:52 +00002244 }
drhac69b052004-05-12 13:30:07 +00002245 }
drh1cc8c442007-08-24 16:08:29 +00002246 }else if( !memDb ){
danielk19777a2b1ee2007-08-21 14:27:01 +00002247 /* If a temporary file is requested, it is not opened immediately.
2248 ** In this case we accept the default page size and delay actually
2249 ** opening the file until the first call to OsWrite().
2250 */
danielk19777a2b1ee2007-08-21 14:27:01 +00002251 tempFile = 1;
2252 pPager->state = PAGER_EXCLUSIVE;
drh5e00f6c2001-09-13 13:46:56 +00002253 }
danielk1977aef0bf62005-12-30 16:28:01 +00002254
danielk1977b4b47412007-08-17 15:53:36 +00002255 if( pPager && rc==SQLITE_OK ){
danielk19779663b8f2007-08-24 11:52:28 +00002256 pPager->pTmpSpace = (char *)sqlite3_malloc(nDefaultPage);
drh3e7a6092002-12-07 21:45:14 +00002257 }
danielk1977aef0bf62005-12-30 16:28:01 +00002258
drh153c62c2007-08-24 03:51:33 +00002259 /* If an error occured in either of the blocks above.
2260 ** Free the Pager structure and close the file.
2261 ** Since the pager is not allocated there is no need to set
danielk1977aef0bf62005-12-30 16:28:01 +00002262 ** any Pager.errMask variables.
2263 */
danielk1977b4b47412007-08-17 15:53:36 +00002264 if( !pPager || !pPager->pTmpSpace ){
2265 sqlite3OsClose(pPager->fd);
drh17435752007-08-16 04:30:38 +00002266 sqlite3_free(pPager);
danielk1977aef0bf62005-12-30 16:28:01 +00002267 return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc);
drhed7c8552001-04-11 14:29:21 +00002268 }
danielk1977aef0bf62005-12-30 16:28:01 +00002269
drh153c62c2007-08-24 03:51:33 +00002270 PAGERTRACE3("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename);
2271 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
danielk1977aef0bf62005-12-30 16:28:01 +00002272
danielk1977b4b47412007-08-17 15:53:36 +00002273 /* Fill in Pager.zDirectory[] */
drh1cc8c442007-08-24 16:08:29 +00002274 memcpy(pPager->zDirectory, pPager->zFilename, nPathname+1);
danielk1977b4b47412007-08-17 15:53:36 +00002275 for(i=strlen(pPager->zDirectory); i>0 && pPager->zDirectory[i-1]!='/'; i--){}
drha76c82e2003-07-27 18:59:42 +00002276 if( i>0 ) pPager->zDirectory[i-1] = 0;
danielk1977b4b47412007-08-17 15:53:36 +00002277
drh99b90c32008-03-17 13:50:58 +00002278 /* Fill in Pager.zJournal[] */
drh1cc8c442007-08-24 16:08:29 +00002279 memcpy(pPager->zJournal, pPager->zFilename, nPathname);
2280 memcpy(&pPager->zJournal[nPathname], "-journal", 9);
danielk1977b4b47412007-08-17 15:53:36 +00002281
drh3b59a5c2006-01-15 20:28:28 +00002282 /* pPager->journalOpen = 0; */
drhac69b052004-05-12 13:30:07 +00002283 pPager->useJournal = useJournal && !memDb;
drh7bec5052005-02-06 02:45:41 +00002284 pPager->noReadlock = noReadlock && readOnly;
drh3b59a5c2006-01-15 20:28:28 +00002285 /* pPager->stmtOpen = 0; */
2286 /* pPager->stmtInUse = 0; */
2287 /* pPager->nRef = 0; */
drhac69b052004-05-12 13:30:07 +00002288 pPager->dbSize = memDb-1;
danielk19779663b8f2007-08-24 11:52:28 +00002289 pPager->pageSize = nDefaultPage;
drh3b59a5c2006-01-15 20:28:28 +00002290 /* pPager->stmtSize = 0; */
2291 /* pPager->stmtJSize = 0; */
2292 /* pPager->nPage = 0; */
drh90f5ecb2004-07-22 01:19:35 +00002293 pPager->mxPage = 100;
drhf8e632b2007-05-08 14:51:36 +00002294 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
drh3b59a5c2006-01-15 20:28:28 +00002295 /* pPager->state = PAGER_UNLOCK; */
drh1cc8c442007-08-24 16:08:29 +00002296 assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
drh3b59a5c2006-01-15 20:28:28 +00002297 /* pPager->errMask = 0; */
drh5e00f6c2001-09-13 13:46:56 +00002298 pPager->tempFile = tempFile;
drh369339d2007-03-30 16:01:55 +00002299 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
2300 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
2301 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
2302 pPager->exclusiveMode = tempFile;
drhac69b052004-05-12 13:30:07 +00002303 pPager->memDb = memDb;
drh5e00f6c2001-09-13 13:46:56 +00002304 pPager->readOnly = readOnly;
drh3b59a5c2006-01-15 20:28:28 +00002305 /* pPager->needSync = 0; */
drhda47d772002-12-02 04:25:19 +00002306 pPager->noSync = pPager->tempFile || !useJournal;
danielk197775edc162004-06-26 01:48:18 +00002307 pPager->fullSync = (pPager->noSync?0:1);
danielk1977f036aef2007-08-20 05:36:51 +00002308 pPager->sync_flags = SQLITE_SYNC_NORMAL;
drh3b59a5c2006-01-15 20:28:28 +00002309 /* pPager->pFirst = 0; */
2310 /* pPager->pFirstSynced = 0; */
2311 /* pPager->pLast = 0; */
drh887dc4c2004-10-22 16:22:57 +00002312 pPager->nExtra = FORCE_ALIGNMENT(nExtra);
danielk19777a2b1ee2007-08-21 14:27:01 +00002313 assert(pPager->fd->pMethods||memDb||tempFile);
danielk1977b4721172007-03-19 05:54:48 +00002314 if( !memDb ){
drhc80f0582007-05-01 16:59:48 +00002315 setSectorSize(pPager);
danielk1977b4721172007-03-19 05:54:48 +00002316 }
drh3b59a5c2006-01-15 20:28:28 +00002317 /* pPager->pBusyHandler = 0; */
2318 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
drhed7c8552001-04-11 14:29:21 +00002319 *ppPager = pPager;
drh6f7adc82006-01-11 21:41:20 +00002320#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +00002321 pPager->iInUseMM = 0;
2322 pPager->iInUseDB = 0;
2323 if( !memDb ){
2324 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
2325 sqlite3_mutex_enter(mutex);
2326 pPager->pNext = sqlite3PagerList;
2327 if( sqlite3PagerList ){
2328 assert( sqlite3PagerList->pPrev==0 );
2329 sqlite3PagerList->pPrev = pPager;
2330 }
2331 pPager->pPrev = 0;
2332 sqlite3PagerList = pPager;
2333 sqlite3_mutex_leave(mutex);
2334 }
danielk197752622822006-01-09 09:59:49 +00002335#endif
drhed7c8552001-04-11 14:29:21 +00002336 return SQLITE_OK;
2337}
2338
2339/*
drh90f5ecb2004-07-22 01:19:35 +00002340** Set the busy handler function.
2341*/
danielk19773b8a05f2007-03-19 17:44:26 +00002342void sqlite3PagerSetBusyhandler(Pager *pPager, BusyHandler *pBusyHandler){
drh90f5ecb2004-07-22 01:19:35 +00002343 pPager->pBusyHandler = pBusyHandler;
2344}
2345
2346/*
drh72f82862001-05-24 21:06:34 +00002347** Set the destructor for this pager. If not NULL, the destructor is called
drh5e00f6c2001-09-13 13:46:56 +00002348** when the reference count on each page reaches zero. The destructor can
2349** be used to clean up information in the extra segment appended to each page.
drh72f82862001-05-24 21:06:34 +00002350**
danielk19773b8a05f2007-03-19 17:44:26 +00002351** The destructor is not called as a result sqlite3PagerClose().
2352** Destructors are only called by sqlite3PagerUnref().
drh72f82862001-05-24 21:06:34 +00002353*/
danielk19773b8a05f2007-03-19 17:44:26 +00002354void sqlite3PagerSetDestructor(Pager *pPager, void (*xDesc)(DbPage*,int)){
drh72f82862001-05-24 21:06:34 +00002355 pPager->xDestructor = xDesc;
2356}
2357
2358/*
drha6abd042004-06-09 17:37:22 +00002359** Set the reinitializer for this pager. If not NULL, the reinitializer
2360** is called when the content of a page in cache is restored to its original
2361** value as a result of a rollback. The callback gives higher-level code
2362** an opportunity to restore the EXTRA section to agree with the restored
2363** page data.
2364*/
danielk19773b8a05f2007-03-19 17:44:26 +00002365void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*,int)){
drha6abd042004-06-09 17:37:22 +00002366 pPager->xReiniter = xReinit;
2367}
2368
2369/*
danielk1977a1644fd2007-08-29 12:31:25 +00002370** Set the page size to *pPageSize. If the suggest new page size is
2371** inappropriate, then an alternative page size is set to that
2372** value before returning.
drh90f5ecb2004-07-22 01:19:35 +00002373*/
danielk1977a1644fd2007-08-29 12:31:25 +00002374int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){
2375 int rc = SQLITE_OK;
2376 u16 pageSize = *pPageSize;
danielk19779663b8f2007-08-24 11:52:28 +00002377 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
danielk1977a1644fd2007-08-29 12:31:25 +00002378 if( pageSize && pageSize!=pPager->pageSize
2379 && !pPager->memDb && pPager->nRef==0
2380 ){
2381 char *pNew = (char *)sqlite3_malloc(pageSize);
2382 if( !pNew ){
2383 rc = SQLITE_NOMEM;
2384 }else{
2385 pagerEnter(pPager);
2386 pager_reset(pPager);
2387 pPager->pageSize = pageSize;
2388 setSectorSize(pPager);
2389 sqlite3_free(pPager->pTmpSpace);
2390 pPager->pTmpSpace = pNew;
2391 pagerLeave(pPager);
2392 }
drh1c7880e2005-05-20 20:01:55 +00002393 }
danielk1977a1644fd2007-08-29 12:31:25 +00002394 *pPageSize = pPager->pageSize;
2395 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002396}
2397
2398/*
drh26b79942007-11-28 16:19:56 +00002399** Return a pointer to the "temporary page" buffer held internally
2400** by the pager. This is a buffer that is big enough to hold the
2401** entire content of a database page. This buffer is used internally
2402** during rollback and will be overwritten whenever a rollback
2403** occurs. But other modules are free to use it too, as long as
2404** no rollbacks are happening.
2405*/
2406void *sqlite3PagerTempSpace(Pager *pPager){
2407 return pPager->pTmpSpace;
2408}
2409
2410/*
drhf8e632b2007-05-08 14:51:36 +00002411** Attempt to set the maximum database page count if mxPage is positive.
2412** Make no changes if mxPage is zero or negative. And never reduce the
2413** maximum page count below the current size of the database.
2414**
2415** Regardless of mxPage, return the current maximum page count.
2416*/
2417int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
2418 if( mxPage>0 ){
2419 pPager->mxPgno = mxPage;
2420 }
2421 sqlite3PagerPagecount(pPager);
2422 return pPager->mxPgno;
2423}
2424
2425/*
drhc9ac5ca2005-11-04 22:03:30 +00002426** The following set of routines are used to disable the simulated
2427** I/O error mechanism. These routines are used to avoid simulated
2428** errors in places where we do not care about errors.
2429**
2430** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
2431** and generate no code.
2432*/
2433#ifdef SQLITE_TEST
2434extern int sqlite3_io_error_pending;
2435extern int sqlite3_io_error_hit;
2436static int saved_cnt;
drhc9ac5ca2005-11-04 22:03:30 +00002437void disable_simulated_io_errors(void){
2438 saved_cnt = sqlite3_io_error_pending;
2439 sqlite3_io_error_pending = -1;
2440}
2441void enable_simulated_io_errors(void){
2442 sqlite3_io_error_pending = saved_cnt;
2443}
2444#else
drh152410f2005-11-05 15:11:22 +00002445# define disable_simulated_io_errors()
2446# define enable_simulated_io_errors()
drhc9ac5ca2005-11-04 22:03:30 +00002447#endif
2448
2449/*
drh90f5ecb2004-07-22 01:19:35 +00002450** Read the first N bytes from the beginning of the file into memory
danielk1977aef0bf62005-12-30 16:28:01 +00002451** that pDest points to.
2452**
2453** No error checking is done. The rational for this is that this function
2454** may be called even if the file does not exist or contain a header. In
2455** these cases sqlite3OsRead() will return an error, to which the correct
2456** response is to zero the memory at pDest and continue. A real IO error
2457** will presumably recur and be picked up later (Todo: Think about this).
drh90f5ecb2004-07-22 01:19:35 +00002458*/
danielk19773b8a05f2007-03-19 17:44:26 +00002459int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
drh551b7732006-11-06 21:20:25 +00002460 int rc = SQLITE_OK;
drh90f5ecb2004-07-22 01:19:35 +00002461 memset(pDest, 0, N);
danielk19777a2b1ee2007-08-21 14:27:01 +00002462 assert(MEMDB||pPager->fd->pMethods||pPager->tempFile);
2463 if( pPager->fd->pMethods ){
drhb0603412007-02-28 04:47:26 +00002464 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
danielk197762079062007-08-15 17:08:46 +00002465 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
drh551b7732006-11-06 21:20:25 +00002466 if( rc==SQLITE_IOERR_SHORT_READ ){
2467 rc = SQLITE_OK;
2468 }
drh90f5ecb2004-07-22 01:19:35 +00002469 }
drh551b7732006-11-06 21:20:25 +00002470 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002471}
2472
2473/*
drh5e00f6c2001-09-13 13:46:56 +00002474** Return the total number of pages in the disk file associated with
danielk197715f411d2005-09-16 10:18:45 +00002475** pPager.
2476**
2477** If the PENDING_BYTE lies on the page directly after the end of the
2478** file, then consider this page part of the file too. For example, if
2479** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the
2480** file is 4096 bytes, 5 is returned instead of 4.
drhed7c8552001-04-11 14:29:21 +00002481*/
danielk19773b8a05f2007-03-19 17:44:26 +00002482int sqlite3PagerPagecount(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00002483 i64 n = 0;
drhe49f9822006-09-15 12:29:16 +00002484 int rc;
drhd9b02572001-04-15 00:37:09 +00002485 assert( pPager!=0 );
drha7aea3d2007-03-15 12:51:16 +00002486 if( pPager->errCode ){
danielk197743468d92008-02-26 06:05:31 +00002487 return -1;
drha7aea3d2007-03-15 12:51:16 +00002488 }
drhed7c8552001-04-11 14:29:21 +00002489 if( pPager->dbSize>=0 ){
danielk197715f411d2005-09-16 10:18:45 +00002490 n = pPager->dbSize;
2491 } else {
danielk19777a2b1ee2007-08-21 14:27:01 +00002492 assert(pPager->fd->pMethods||pPager->tempFile);
2493 if( (pPager->fd->pMethods)
2494 && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){
danielk1977ae72d982007-10-03 08:46:44 +00002495 pPager->nRef++;
drhe49f9822006-09-15 12:29:16 +00002496 pager_error(pPager, rc);
danielk1977ae72d982007-10-03 08:46:44 +00002497 pPager->nRef--;
danielk197743468d92008-02-26 06:05:31 +00002498 return -1;
danielk197715f411d2005-09-16 10:18:45 +00002499 }
2500 if( n>0 && n<pPager->pageSize ){
2501 n = 1;
2502 }else{
2503 n /= pPager->pageSize;
2504 }
2505 if( pPager->state!=PAGER_UNLOCK ){
2506 pPager->dbSize = n;
2507 }
drhed7c8552001-04-11 14:29:21 +00002508 }
danielk197715f411d2005-09-16 10:18:45 +00002509 if( n==(PENDING_BYTE/pPager->pageSize) ){
drh1f595712004-06-15 01:40:29 +00002510 n++;
2511 }
drhf8e632b2007-05-08 14:51:36 +00002512 if( n>pPager->mxPgno ){
2513 pPager->mxPgno = n;
2514 }
drhed7c8552001-04-11 14:29:21 +00002515 return n;
2516}
2517
drhf07e7d52006-04-07 13:54:46 +00002518
2519#ifndef SQLITE_OMIT_MEMORYDB
2520/*
2521** Clear a PgHistory block
2522*/
2523static void clearHistory(PgHistory *pHist){
drh17435752007-08-16 04:30:38 +00002524 sqlite3_free(pHist->pOrig);
2525 sqlite3_free(pHist->pStmt);
drhf07e7d52006-04-07 13:54:46 +00002526 pHist->pOrig = 0;
2527 pHist->pStmt = 0;
2528}
2529#else
2530#define clearHistory(x)
2531#endif
2532
drhed7c8552001-04-11 14:29:21 +00002533/*
drhf7c57532003-04-25 13:22:51 +00002534** Forward declaration
2535*/
danielk197776572402004-06-25 02:38:54 +00002536static int syncJournal(Pager*);
drhac69b052004-05-12 13:30:07 +00002537
2538/*
drh85b623f2007-12-13 21:54:09 +00002539** Unlink pPg from its hash chain. Also set the page number to 0 to indicate
danielk1977f5fdda82004-11-03 08:44:05 +00002540** that the page is not part of any hash chain. This is required because the
danielk19773b8a05f2007-03-19 17:44:26 +00002541** sqlite3PagerMovepage() routine can leave a page in the
danielk1977f5fdda82004-11-03 08:44:05 +00002542** pNextFree/pPrevFree list that is not a part of any hash-chain.
2543*/
2544static void unlinkHashChain(Pager *pPager, PgHdr *pPg){
2545 if( pPg->pgno==0 ){
drh3765df42006-06-28 18:18:09 +00002546 assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
danielk1977f5fdda82004-11-03 08:44:05 +00002547 return;
2548 }
2549 if( pPg->pNextHash ){
2550 pPg->pNextHash->pPrevHash = pPg->pPrevHash;
2551 }
2552 if( pPg->pPrevHash ){
drh8ca0c722006-05-07 17:49:38 +00002553 assert( pPager->aHash[pPg->pgno & (pPager->nHash-1)]!=pPg );
danielk1977f5fdda82004-11-03 08:44:05 +00002554 pPg->pPrevHash->pNextHash = pPg->pNextHash;
2555 }else{
drh8ca0c722006-05-07 17:49:38 +00002556 int h = pPg->pgno & (pPager->nHash-1);
danielk1977f5fdda82004-11-03 08:44:05 +00002557 pPager->aHash[h] = pPg->pNextHash;
2558 }
drh5229ae42006-03-23 23:29:04 +00002559 if( MEMDB ){
drh5229ae42006-03-23 23:29:04 +00002560 clearHistory(PGHDR_TO_HIST(pPg, pPager));
2561 }
danielk1977f5fdda82004-11-03 08:44:05 +00002562 pPg->pgno = 0;
2563 pPg->pNextHash = pPg->pPrevHash = 0;
2564}
2565
2566/*
drhac69b052004-05-12 13:30:07 +00002567** Unlink a page from the free list (the list of all pages where nRef==0)
2568** and from its hash collision chain.
2569*/
2570static void unlinkPage(PgHdr *pPg){
2571 Pager *pPager = pPg->pPager;
2572
danielk19779f61c2f2007-08-27 17:27:49 +00002573 /* Unlink from free page list */
2574 lruListRemove(pPg);
drhac69b052004-05-12 13:30:07 +00002575
2576 /* Unlink from the pgno hash table */
danielk1977f5fdda82004-11-03 08:44:05 +00002577 unlinkHashChain(pPager, pPg);
drhac69b052004-05-12 13:30:07 +00002578}
2579
2580/*
danielk1977e180dd92007-04-05 17:15:52 +00002581** This routine is used to truncate the cache when a database
2582** is truncated. Drop from the cache all pages whose pgno is
2583** larger than pPager->dbSize and is unreferenced.
2584**
drhac69b052004-05-12 13:30:07 +00002585** Referenced pages larger than pPager->dbSize are zeroed.
danielk1977e180dd92007-04-05 17:15:52 +00002586**
2587** Actually, at the point this routine is called, it would be
2588** an error to have a referenced page. But rather than delete
2589** that page and guarantee a subsequent segfault, it seems better
2590** to zero it and hope that we error out sanely.
drhac69b052004-05-12 13:30:07 +00002591*/
danielk1977e180dd92007-04-05 17:15:52 +00002592static void pager_truncate_cache(Pager *pPager){
drhac69b052004-05-12 13:30:07 +00002593 PgHdr *pPg;
2594 PgHdr **ppPg;
2595 int dbSize = pPager->dbSize;
2596
2597 ppPg = &pPager->pAll;
2598 while( (pPg = *ppPg)!=0 ){
2599 if( pPg->pgno<=dbSize ){
2600 ppPg = &pPg->pNextAll;
2601 }else if( pPg->nRef>0 ){
2602 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
2603 ppPg = &pPg->pNextAll;
2604 }else{
2605 *ppPg = pPg->pNextAll;
drh538f5702007-04-13 02:14:30 +00002606 IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
2607 PAGER_INCR(sqlite3_pager_pgfree_count);
drhac69b052004-05-12 13:30:07 +00002608 unlinkPage(pPg);
drh605ad8f2006-05-03 23:34:05 +00002609 makeClean(pPg);
drh0d180202008-02-14 23:26:56 +00002610 sqlite3_free(pPg->pData);
drh17435752007-08-16 04:30:38 +00002611 sqlite3_free(pPg);
drhac69b052004-05-12 13:30:07 +00002612 pPager->nPage--;
2613 }
2614 }
2615}
2616
drhf7c57532003-04-25 13:22:51 +00002617/*
danielk197717221812005-02-15 03:38:05 +00002618** Try to obtain a lock on a file. Invoke the busy callback if the lock
drha4afb652005-07-09 02:16:02 +00002619** is currently not available. Repeat until the busy callback returns
danielk197717221812005-02-15 03:38:05 +00002620** false or until the lock succeeds.
2621**
2622** Return SQLITE_OK on success and an error code if we cannot obtain
2623** the lock.
2624*/
2625static int pager_wait_on_lock(Pager *pPager, int locktype){
2626 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002627
2628 /* The OS lock values must be the same as the Pager lock values */
danielk197717221812005-02-15 03:38:05 +00002629 assert( PAGER_SHARED==SHARED_LOCK );
2630 assert( PAGER_RESERVED==RESERVED_LOCK );
2631 assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
drh1aa2d8b2007-01-03 15:34:29 +00002632
2633 /* If the file is currently unlocked then the size must be unknown */
2634 assert( pPager->state>=PAGER_SHARED || pPager->dbSize<0 || MEMDB );
2635
danielk197717221812005-02-15 03:38:05 +00002636 if( pPager->state>=locktype ){
2637 rc = SQLITE_OK;
2638 }else{
drhbefdf832008-03-14 19:33:05 +00002639 if( pPager->pBusyHandler ) pPager->pBusyHandler->nBusy = 0;
danielk197717221812005-02-15 03:38:05 +00002640 do {
drh054889e2005-11-30 03:20:31 +00002641 rc = sqlite3OsLock(pPager->fd, locktype);
drha4afb652005-07-09 02:16:02 +00002642 }while( rc==SQLITE_BUSY && sqlite3InvokeBusyHandler(pPager->pBusyHandler) );
danielk197717221812005-02-15 03:38:05 +00002643 if( rc==SQLITE_OK ){
2644 pPager->state = locktype;
drhb0603412007-02-28 04:47:26 +00002645 IOTRACE(("LOCK %p %d\n", pPager, locktype))
danielk197717221812005-02-15 03:38:05 +00002646 }
2647 }
2648 return rc;
2649}
2650
2651/*
drhf7c57532003-04-25 13:22:51 +00002652** Truncate the file to the number of pages specified.
2653*/
danielk19773b8a05f2007-03-19 17:44:26 +00002654int sqlite3PagerTruncate(Pager *pPager, Pgno nPage){
drhf7c57532003-04-25 13:22:51 +00002655 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002656 assert( pPager->state>=PAGER_SHARED || MEMDB );
danielk19773b8a05f2007-03-19 17:44:26 +00002657 sqlite3PagerPagecount(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00002658 if( pPager->errCode ){
2659 rc = pPager->errCode;
drh2e6d11b2003-04-25 15:37:57 +00002660 return rc;
2661 }
drh7d02cb72003-06-04 16:24:39 +00002662 if( nPage>=(unsigned)pPager->dbSize ){
drhf7c57532003-04-25 13:22:51 +00002663 return SQLITE_OK;
2664 }
drhb7f91642004-10-31 02:22:47 +00002665 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00002666 pPager->dbSize = nPage;
danielk1977e180dd92007-04-05 17:15:52 +00002667 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00002668 return SQLITE_OK;
2669 }
drh86f8c192007-08-22 00:39:19 +00002670 pagerEnter(pPager);
danielk197776572402004-06-25 02:38:54 +00002671 rc = syncJournal(pPager);
drh86f8c192007-08-22 00:39:19 +00002672 pagerLeave(pPager);
danielk1977369f27e2004-06-15 11:40:04 +00002673 if( rc!=SQLITE_OK ){
2674 return rc;
2675 }
danielk197717221812005-02-15 03:38:05 +00002676
2677 /* Get an exclusive lock on the database before truncating. */
drh86f8c192007-08-22 00:39:19 +00002678 pagerEnter(pPager);
danielk197717221812005-02-15 03:38:05 +00002679 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
drh86f8c192007-08-22 00:39:19 +00002680 pagerLeave(pPager);
danielk197717221812005-02-15 03:38:05 +00002681 if( rc!=SQLITE_OK ){
2682 return rc;
2683 }
2684
drhcb4c40b2004-08-18 19:09:43 +00002685 rc = pager_truncate(pPager, nPage);
drhf7c57532003-04-25 13:22:51 +00002686 return rc;
2687}
2688
2689/*
drhed7c8552001-04-11 14:29:21 +00002690** Shutdown the page cache. Free all memory and close all files.
2691**
2692** If a transaction was in progress when this routine is called, that
2693** transaction is rolled back. All outstanding pages are invalidated
2694** and their memory is freed. Any attempt to use a page associated
2695** with this page cache after this function returns will likely
2696** result in a coredump.
danielk1977aef0bf62005-12-30 16:28:01 +00002697**
2698** This function always succeeds. If a transaction is active an attempt
2699** is made to roll it back. If an error occurs during the rollback
2700** a hot journal may be left in the filesystem but no error is returned
2701** to the caller.
drhed7c8552001-04-11 14:29:21 +00002702*/
danielk19773b8a05f2007-03-19 17:44:26 +00002703int sqlite3PagerClose(Pager *pPager){
drh6f7adc82006-01-11 21:41:20 +00002704#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +00002705 if( !MEMDB ){
2706 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
2707 sqlite3_mutex_enter(mutex);
2708 if( pPager->pPrev ){
2709 pPager->pPrev->pNext = pPager->pNext;
2710 }else{
2711 sqlite3PagerList = pPager->pNext;
2712 }
2713 if( pPager->pNext ){
2714 pPager->pNext->pPrev = pPager->pPrev;
2715 }
2716 sqlite3_mutex_leave(mutex);
2717 }
danielk197713f72992005-12-18 08:51:22 +00002718#endif
2719
drhbafda092007-01-03 23:36:22 +00002720 disable_simulated_io_errors();
drh16e45a42008-04-19 20:34:18 +00002721 sqlite3FaultBenign(-1, 1);
drhc2ee76c2007-01-04 14:58:14 +00002722 pPager->errCode = 0;
danielk197741483462007-03-24 16:45:04 +00002723 pPager->exclusiveMode = 0;
drhbafda092007-01-03 23:36:22 +00002724 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00002725 pagerUnlockAndRollback(pPager);
drhbafda092007-01-03 23:36:22 +00002726 enable_simulated_io_errors();
drh16e45a42008-04-19 20:34:18 +00002727 sqlite3FaultBenign(-1, 0);
drh4f0c5872007-03-26 22:05:01 +00002728 PAGERTRACE2("CLOSE %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002729 IOTRACE(("CLOSE %p\n", pPager))
danielk1977e94ddc92005-03-21 03:53:38 +00002730 if( pPager->journalOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002731 sqlite3OsClose(pPager->jfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002732 }
drhf5e7bb52008-02-18 14:47:33 +00002733 sqlite3BitvecDestroy(pPager->pInJournal);
danielk1977e94ddc92005-03-21 03:53:38 +00002734 if( pPager->stmtOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002735 sqlite3OsClose(pPager->stfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002736 }
danielk1977b4b47412007-08-17 15:53:36 +00002737 sqlite3OsClose(pPager->fd);
drh0f892532002-05-30 12:27:03 +00002738 /* Temp files are automatically deleted by the OS
2739 ** if( pPager->tempFile ){
drh66560ad2006-01-06 14:32:19 +00002740 ** sqlite3OsDelete(pPager->zFilename);
drh0f892532002-05-30 12:27:03 +00002741 ** }
2742 */
danielk1977aca790a2005-01-13 11:07:52 +00002743
drh17435752007-08-16 04:30:38 +00002744 sqlite3_free(pPager->aHash);
2745 sqlite3_free(pPager->pTmpSpace);
2746 sqlite3_free(pPager);
drhed7c8552001-04-11 14:29:21 +00002747 return SQLITE_OK;
2748}
2749
drh87cc3b32007-05-08 21:45:27 +00002750#if !defined(NDEBUG) || defined(SQLITE_TEST)
drhed7c8552001-04-11 14:29:21 +00002751/*
drh5e00f6c2001-09-13 13:46:56 +00002752** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +00002753*/
danielk19773b8a05f2007-03-19 17:44:26 +00002754Pgno sqlite3PagerPagenumber(DbPage *p){
drhed7c8552001-04-11 14:29:21 +00002755 return p->pgno;
2756}
drh87cc3b32007-05-08 21:45:27 +00002757#endif
drhed7c8552001-04-11 14:29:21 +00002758
2759/*
drhc8629a12004-05-08 20:07:40 +00002760** The page_ref() function increments the reference count for a page.
2761** If the page is currently on the freelist (the reference count is zero) then
drh7e3b0a02001-04-28 16:52:40 +00002762** remove it from the freelist.
drhc8629a12004-05-08 20:07:40 +00002763**
2764** For non-test systems, page_ref() is a macro that calls _page_ref()
2765** online of the reference count is zero. For test systems, page_ref()
2766** is a real function so that we can set breakpoints and trace it.
drh7e3b0a02001-04-28 16:52:40 +00002767*/
drh836faa42003-01-11 13:30:57 +00002768static void _page_ref(PgHdr *pPg){
drh7e3b0a02001-04-28 16:52:40 +00002769 if( pPg->nRef==0 ){
2770 /* The page is currently on the freelist. Remove it. */
danielk19779f61c2f2007-08-27 17:27:49 +00002771 lruListRemove(pPg);
drh7e3b0a02001-04-28 16:52:40 +00002772 pPg->pPager->nRef++;
2773 }
2774 pPg->nRef++;
drhdf0b3b02001-06-23 11:36:20 +00002775}
danielk1977aca790a2005-01-13 11:07:52 +00002776#ifdef SQLITE_DEBUG
drhc8629a12004-05-08 20:07:40 +00002777 static void page_ref(PgHdr *pPg){
2778 if( pPg->nRef==0 ){
2779 _page_ref(pPg);
2780 }else{
2781 pPg->nRef++;
drhc8629a12004-05-08 20:07:40 +00002782 }
2783 }
2784#else
2785# define page_ref(P) ((P)->nRef==0?_page_ref(P):(void)(P)->nRef++)
2786#endif
drhdf0b3b02001-06-23 11:36:20 +00002787
2788/*
2789** Increment the reference count for a page. The input pointer is
2790** a reference to the page data.
2791*/
danielk19773b8a05f2007-03-19 17:44:26 +00002792int sqlite3PagerRef(DbPage *pPg){
drh86f8c192007-08-22 00:39:19 +00002793 pagerEnter(pPg->pPager);
drhdf0b3b02001-06-23 11:36:20 +00002794 page_ref(pPg);
drh86f8c192007-08-22 00:39:19 +00002795 pagerLeave(pPg->pPager);
drh8c42ca92001-06-22 19:15:00 +00002796 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00002797}
2798
2799/*
drh34e79ce2004-02-08 06:05:46 +00002800** Sync the journal. In other words, make sure all the pages that have
2801** been written to the journal have actually reached the surface of the
2802** disk. It is not safe to modify the original database file until after
2803** the journal has been synced. If the original database is modified before
2804** the journal is synced and a power failure occurs, the unsynced journal
2805** data would be lost and we would be unable to completely rollback the
2806** database changes. Database corruption would occur.
2807**
2808** This routine also updates the nRec field in the header of the journal.
2809** (See comments on the pager_playback() routine for additional information.)
2810** If the sync mode is FULL, two syncs will occur. First the whole journal
2811** is synced, then the nRec field is updated, then a second sync occurs.
drhb19a2bc2001-09-16 00:13:26 +00002812**
drh34e79ce2004-02-08 06:05:46 +00002813** For temporary databases, we do not care if we are able to rollback
danielk19774cd2cd52007-08-23 14:48:23 +00002814** after a power failure, so no sync occurs.
2815**
2816** If the IOCAP_SEQUENTIAL flag is set for the persistent media on which
2817** the database is stored, then OsSync() is never called on the journal
2818** file. In this case all that is required is to update the nRec field in
2819** the journal header.
drhfa86c412002-02-02 15:01:15 +00002820**
drh34e79ce2004-02-08 06:05:46 +00002821** This routine clears the needSync field of every page current held in
2822** memory.
drh50e5dad2001-09-15 00:57:28 +00002823*/
danielk197776572402004-06-25 02:38:54 +00002824static int syncJournal(Pager *pPager){
drh50e5dad2001-09-15 00:57:28 +00002825 PgHdr *pPg;
2826 int rc = SQLITE_OK;
drh03eb96a2002-11-10 23:32:56 +00002827
danielk19774cd2cd52007-08-23 14:48:23 +00002828
drh03eb96a2002-11-10 23:32:56 +00002829 /* Sync the journal before modifying the main database
2830 ** (assuming there is a journal and it needs to be synced.)
2831 */
danielk197776572402004-06-25 02:38:54 +00002832 if( pPager->needSync ){
drhfa86c412002-02-02 15:01:15 +00002833 if( !pPager->tempFile ){
danielk19774cd2cd52007-08-23 14:48:23 +00002834 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
drhdb48ee02003-01-16 13:42:43 +00002835 assert( pPager->journalOpen );
danielk19774cd2cd52007-08-23 14:48:23 +00002836
danielk19774cd2cd52007-08-23 14:48:23 +00002837 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
danielk197776572402004-06-25 02:38:54 +00002838 /* Write the nRec value into the journal file header. If in
2839 ** full-synchronous mode, sync the journal first. This ensures that
2840 ** all data has really hit the disk before nRec is updated to mark
danielk19774cd2cd52007-08-23 14:48:23 +00002841 ** it as a candidate for rollback.
2842 **
2843 ** This is not required if the persistent media supports the
2844 ** SAFE_APPEND property. Because in this case it is not possible
2845 ** for garbage data to be appended to the file, the nRec field
2846 ** is populated with 0xFFFFFFFF when the journal header is written
2847 ** and never needs to be updated.
danielk197776572402004-06-25 02:38:54 +00002848 */
danielk19774cd2cd52007-08-23 14:48:23 +00002849 i64 jrnlOff;
2850 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
drh4f0c5872007-03-26 22:05:01 +00002851 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002852 IOTRACE(("JSYNC %p\n", pPager))
danielk1977f036aef2007-08-20 05:36:51 +00002853 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags);
drhd8d66e82003-02-12 02:10:15 +00002854 if( rc!=0 ) return rc;
2855 }
danielk197713adf8a2004-06-03 16:08:41 +00002856
danielk197762079062007-08-15 17:08:46 +00002857 jrnlOff = pPager->journalHdr + sizeof(aJournalMagic);
2858 IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4));
2859 rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec);
drhb4746b92005-09-09 01:32:06 +00002860 if( rc ) return rc;
drh968af522003-02-11 14:55:40 +00002861 }
danielk19774cd2cd52007-08-23 14:48:23 +00002862 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
2863 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
2864 IOTRACE(("JSYNC %p\n", pPager))
2865 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags|
2866 (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
2867 );
2868 if( rc!=0 ) return rc;
2869 }
drhdb48ee02003-01-16 13:42:43 +00002870 pPager->journalStarted = 1;
drhfa86c412002-02-02 15:01:15 +00002871 }
drh50e5dad2001-09-15 00:57:28 +00002872 pPager->needSync = 0;
drh341eae82003-01-21 02:39:36 +00002873
2874 /* Erase the needSync flag from every page.
2875 */
2876 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2877 pPg->needSync = 0;
2878 }
danielk19779f61c2f2007-08-27 17:27:49 +00002879 lruListSetFirstSynced(pPager);
drh50e5dad2001-09-15 00:57:28 +00002880 }
drh03eb96a2002-11-10 23:32:56 +00002881
drh341eae82003-01-21 02:39:36 +00002882#ifndef NDEBUG
2883 /* If the Pager.needSync flag is clear then the PgHdr.needSync
2884 ** flag must also be clear for all pages. Verify that this
2885 ** invariant is true.
drh03eb96a2002-11-10 23:32:56 +00002886 */
drh341eae82003-01-21 02:39:36 +00002887 else{
2888 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2889 assert( pPg->needSync==0 );
2890 }
danielk19779f61c2f2007-08-27 17:27:49 +00002891 assert( pPager->lru.pFirstSynced==pPager->lru.pFirst );
drh03eb96a2002-11-10 23:32:56 +00002892 }
drh341eae82003-01-21 02:39:36 +00002893#endif
drhdb48ee02003-01-16 13:42:43 +00002894
drh81a20f22001-10-12 17:30:04 +00002895 return rc;
drh50e5dad2001-09-15 00:57:28 +00002896}
2897
2898/*
drhdc3e10b2006-06-15 14:31:06 +00002899** Merge two lists of pages connected by pDirty and in pgno order.
2900** Do not both fixing the pPrevDirty pointers.
2901*/
2902static PgHdr *merge_pagelist(PgHdr *pA, PgHdr *pB){
2903 PgHdr result, *pTail;
2904 pTail = &result;
2905 while( pA && pB ){
2906 if( pA->pgno<pB->pgno ){
2907 pTail->pDirty = pA;
2908 pTail = pA;
2909 pA = pA->pDirty;
2910 }else{
2911 pTail->pDirty = pB;
2912 pTail = pB;
2913 pB = pB->pDirty;
2914 }
2915 }
2916 if( pA ){
2917 pTail->pDirty = pA;
2918 }else if( pB ){
2919 pTail->pDirty = pB;
2920 }else{
2921 pTail->pDirty = 0;
2922 }
2923 return result.pDirty;
2924}
2925
2926/*
2927** Sort the list of pages in accending order by pgno. Pages are
2928** connected by pDirty pointers. The pPrevDirty pointers are
2929** corrupted by this sort.
2930*/
danielk197724168722007-04-02 05:07:47 +00002931#define N_SORT_BUCKET_ALLOC 25
2932#define N_SORT_BUCKET 25
2933#ifdef SQLITE_TEST
2934 int sqlite3_pager_n_sort_bucket = 0;
2935 #undef N_SORT_BUCKET
2936 #define N_SORT_BUCKET \
2937 (sqlite3_pager_n_sort_bucket?sqlite3_pager_n_sort_bucket:N_SORT_BUCKET_ALLOC)
2938#endif
drhdc3e10b2006-06-15 14:31:06 +00002939static PgHdr *sort_pagelist(PgHdr *pIn){
danielk197724168722007-04-02 05:07:47 +00002940 PgHdr *a[N_SORT_BUCKET_ALLOC], *p;
drhdc3e10b2006-06-15 14:31:06 +00002941 int i;
2942 memset(a, 0, sizeof(a));
2943 while( pIn ){
2944 p = pIn;
2945 pIn = p->pDirty;
2946 p->pDirty = 0;
2947 for(i=0; i<N_SORT_BUCKET-1; i++){
2948 if( a[i]==0 ){
2949 a[i] = p;
2950 break;
2951 }else{
2952 p = merge_pagelist(a[i], p);
2953 a[i] = 0;
2954 }
2955 }
2956 if( i==N_SORT_BUCKET-1 ){
danielk197724168722007-04-02 05:07:47 +00002957 /* Coverage: To get here, there need to be 2^(N_SORT_BUCKET)
2958 ** elements in the input list. This is possible, but impractical.
2959 ** Testing this line is the point of global variable
2960 ** sqlite3_pager_n_sort_bucket.
2961 */
drhdc3e10b2006-06-15 14:31:06 +00002962 a[i] = merge_pagelist(a[i], p);
2963 }
2964 }
2965 p = a[0];
2966 for(i=1; i<N_SORT_BUCKET; i++){
2967 p = merge_pagelist(p, a[i]);
2968 }
2969 return p;
2970}
2971
2972/*
drh2554f8b2003-01-22 01:26:44 +00002973** Given a list of pages (connected by the PgHdr.pDirty pointer) write
2974** every one of those pages out to the database file and mark them all
2975** as clean.
2976*/
2977static int pager_write_pagelist(PgHdr *pList){
2978 Pager *pPager;
drh153c62c2007-08-24 03:51:33 +00002979 PgHdr *p;
drh2554f8b2003-01-22 01:26:44 +00002980 int rc;
2981
2982 if( pList==0 ) return SQLITE_OK;
2983 pPager = pList->pPager;
danielk19779eed5052004-06-04 10:38:30 +00002984
2985 /* At this point there may be either a RESERVED or EXCLUSIVE lock on the
2986 ** database file. If there is already an EXCLUSIVE lock, the following
drh054889e2005-11-30 03:20:31 +00002987 ** calls to sqlite3OsLock() are no-ops.
danielk19779eed5052004-06-04 10:38:30 +00002988 **
drha6abd042004-06-09 17:37:22 +00002989 ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
2990 ** through an intermediate state PENDING. A PENDING lock prevents new
2991 ** readers from attaching to the database but is unsufficient for us to
2992 ** write. The idea of a PENDING lock is to prevent new readers from
2993 ** coming in while we wait for existing readers to clear.
danielk19779eed5052004-06-04 10:38:30 +00002994 **
drha6abd042004-06-09 17:37:22 +00002995 ** While the pager is in the RESERVED state, the original database file
2996 ** is unchanged and we can rollback without having to playback the
2997 ** journal into the original database file. Once we transition to
2998 ** EXCLUSIVE, it means the database file has been changed and any rollback
2999 ** will require a journal playback.
danielk19779eed5052004-06-04 10:38:30 +00003000 */
drh684917c2004-10-05 02:41:42 +00003001 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
danielk19779eed5052004-06-04 10:38:30 +00003002 if( rc!=SQLITE_OK ){
3003 return rc;
3004 }
3005
drhdc3e10b2006-06-15 14:31:06 +00003006 pList = sort_pagelist(pList);
drh153c62c2007-08-24 03:51:33 +00003007 for(p=pList; p; p=p->pDirty){
3008 assert( p->dirty );
3009 p->dirty = 0;
3010 }
drh2554f8b2003-01-22 01:26:44 +00003011 while( pList ){
danielk19777a2b1ee2007-08-21 14:27:01 +00003012
3013 /* If the file has not yet been opened, open it now. */
3014 if( !pPager->fd->pMethods ){
3015 assert(pPager->tempFile);
drh33f4e022007-09-03 15:19:34 +00003016 rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->fd, pPager->zFilename,
3017 pPager->vfsFlags);
danielk19777a2b1ee2007-08-21 14:27:01 +00003018 if( rc ) return rc;
3019 }
3020
danielk1977687566d2004-11-02 12:56:41 +00003021 /* If there are dirty pages in the page cache with page numbers greater
danielk19773b8a05f2007-03-19 17:44:26 +00003022 ** than Pager.dbSize, this means sqlite3PagerTruncate() was called to
danielk1977687566d2004-11-02 12:56:41 +00003023 ** make the file smaller (presumably by auto-vacuum code). Do not write
3024 ** any such pages to the file.
3025 */
3026 if( pList->pgno<=pPager->dbSize ){
danielk197762079062007-08-15 17:08:46 +00003027 i64 offset = (pList->pgno-1)*(i64)pPager->pageSize;
drhc001c582006-03-06 18:23:16 +00003028 char *pData = CODEC2(pPager, PGHDR_TO_DATA(pList), pList->pgno, 6);
drh477731b2007-06-16 03:06:27 +00003029 PAGERTRACE4("STORE %d page %d hash(%08x)\n",
3030 PAGERID(pPager), pList->pgno, pager_pagehash(pList));
drh538f5702007-04-13 02:14:30 +00003031 IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno));
danielk197762079062007-08-15 17:08:46 +00003032 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00003033 PAGER_INCR(sqlite3_pager_writedb_count);
3034 PAGER_INCR(pPager->nWrite);
drh86a88112007-04-16 15:02:19 +00003035 if( pList->pgno==1 ){
3036 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
3037 }
danielk1977687566d2004-11-02 12:56:41 +00003038 }
3039#ifndef NDEBUG
3040 else{
drh4f0c5872007-03-26 22:05:01 +00003041 PAGERTRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno);
danielk1977687566d2004-11-02 12:56:41 +00003042 }
3043#endif
drh2554f8b2003-01-22 01:26:44 +00003044 if( rc ) return rc;
danielk19773c407372005-02-15 02:54:14 +00003045#ifdef SQLITE_CHECK_PAGES
3046 pList->pageHash = pager_pagehash(pList);
3047#endif
drh2554f8b2003-01-22 01:26:44 +00003048 pList = pList->pDirty;
3049 }
3050 return SQLITE_OK;
3051}
3052
3053/*
3054** Collect every dirty page into a dirty list and
3055** return a pointer to the head of that list. All pages are
3056** collected even if they are still in use.
3057*/
3058static PgHdr *pager_get_all_dirty_pages(Pager *pPager){
drh8f2e9a12008-02-26 14:46:04 +00003059
3060#ifndef NDEBUG
3061 /* Verify the sanity of the dirty list when we are running
3062 ** in debugging mode. This is expensive, so do not
3063 ** do this on a normal build. */
3064 int n1 = 0;
3065 int n2 = 0;
3066 PgHdr *p;
3067 for(p=pPager->pAll; p; p=p->pNextAll){ if( p->dirty ) n1++; }
3068 for(p=pPager->pDirty; p; p=p->pDirty){ n2++; }
3069 assert( n1==n2 );
3070#endif
3071
drh605ad8f2006-05-03 23:34:05 +00003072 return pPager->pDirty;
drh2554f8b2003-01-22 01:26:44 +00003073}
3074
3075/*
drh19db9352008-03-27 22:42:51 +00003076** Return 1 if there is a hot journal on the given pager.
drh165ffe92005-03-15 17:09:30 +00003077** A hot journal is one that needs to be played back.
3078**
3079** If the current size of the database file is 0 but a journal file
3080** exists, that is probably an old journal left over from a prior
3081** database with the same name. Just delete the journal.
drh19db9352008-03-27 22:42:51 +00003082**
3083** Return negative if unable to determine the status of the journal.
drh165ffe92005-03-15 17:09:30 +00003084*/
3085static int hasHotJournal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00003086 sqlite3_vfs *pVfs = pPager->pVfs;
drh19db9352008-03-27 22:42:51 +00003087 int rc;
drh165ffe92005-03-15 17:09:30 +00003088 if( !pPager->useJournal ) return 0;
drhd3cf2472007-11-27 16:55:07 +00003089 if( !pPager->fd->pMethods ) return 0;
drh19db9352008-03-27 22:42:51 +00003090 rc = sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS);
3091 if( rc<=0 ){
3092 return rc;
drhbb5f18d2007-04-06 18:23:17 +00003093 }
3094 if( sqlite3OsCheckReservedLock(pPager->fd) ){
3095 return 0;
3096 }
danielk19773b8a05f2007-03-19 17:44:26 +00003097 if( sqlite3PagerPagecount(pPager)==0 ){
danielk1977fee2d252007-08-18 10:59:19 +00003098 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
drh165ffe92005-03-15 17:09:30 +00003099 return 0;
3100 }else{
3101 return 1;
3102 }
3103}
3104
danielk19775591df52005-12-20 09:19:37 +00003105/*
danielk1977aef0bf62005-12-30 16:28:01 +00003106** Try to find a page in the cache that can be recycled.
3107**
3108** This routine may return SQLITE_IOERR, SQLITE_FULL or SQLITE_OK. It
danielk1977efaaf572006-01-16 11:29:19 +00003109** does not set the pPager->errCode variable.
danielk19775591df52005-12-20 09:19:37 +00003110*/
danielk1977843e65f2007-09-01 16:16:15 +00003111static int pager_recycle(Pager *pPager, PgHdr **ppPg){
danielk197713f72992005-12-18 08:51:22 +00003112 PgHdr *pPg;
3113 *ppPg = 0;
3114
danielk1977843e65f2007-09-01 16:16:15 +00003115 /* It is illegal to call this function unless the pager object
3116 ** pointed to by pPager has at least one free page (page with nRef==0).
3117 */
danielk1977c551edc2007-04-05 13:12:13 +00003118 assert(!MEMDB);
danielk1977843e65f2007-09-01 16:16:15 +00003119 assert(pPager->lru.pFirst);
danielk1977c551edc2007-04-05 13:12:13 +00003120
danielk197713f72992005-12-18 08:51:22 +00003121 /* Find a page to recycle. Try to locate a page that does not
3122 ** require us to do an fsync() on the journal.
3123 */
danielk19779f61c2f2007-08-27 17:27:49 +00003124 pPg = pPager->lru.pFirstSynced;
danielk197713f72992005-12-18 08:51:22 +00003125
3126 /* If we could not find a page that does not require an fsync()
3127 ** on the journal file then fsync the journal file. This is a
3128 ** very slow operation, so we work hard to avoid it. But sometimes
3129 ** it can't be helped.
3130 */
danielk1977843e65f2007-09-01 16:16:15 +00003131 if( pPg==0 && pPager->lru.pFirst){
danielk19774cd2cd52007-08-23 14:48:23 +00003132 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
danielk197713f72992005-12-18 08:51:22 +00003133 int rc = syncJournal(pPager);
3134 if( rc!=0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00003135 return rc;
danielk197713f72992005-12-18 08:51:22 +00003136 }
danielk19774cd2cd52007-08-23 14:48:23 +00003137 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
danielk197713f72992005-12-18 08:51:22 +00003138 /* If in full-sync mode, write a new journal header into the
3139 ** journal file. This is done to avoid ever modifying a journal
3140 ** header that is involved in the rollback of pages that have
3141 ** already been written to the database (in case the header is
3142 ** trashed when the nRec field is updated).
3143 */
3144 pPager->nRec = 0;
3145 assert( pPager->journalOff > 0 );
danielk19774099f6e2007-03-19 11:25:20 +00003146 assert( pPager->doNotSync==0 );
danielk197713f72992005-12-18 08:51:22 +00003147 rc = writeJournalHdr(pPager);
3148 if( rc!=0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00003149 return rc;
danielk197713f72992005-12-18 08:51:22 +00003150 }
3151 }
danielk19779f61c2f2007-08-27 17:27:49 +00003152 pPg = pPager->lru.pFirst;
danielk197713f72992005-12-18 08:51:22 +00003153 }
danielk197713f72992005-12-18 08:51:22 +00003154
3155 assert( pPg->nRef==0 );
3156
3157 /* Write the page to the database file if it is dirty.
3158 */
3159 if( pPg->dirty ){
3160 int rc;
3161 assert( pPg->needSync==0 );
drh605ad8f2006-05-03 23:34:05 +00003162 makeClean(pPg);
3163 pPg->dirty = 1;
danielk197713f72992005-12-18 08:51:22 +00003164 pPg->pDirty = 0;
3165 rc = pager_write_pagelist( pPg );
drh153c62c2007-08-24 03:51:33 +00003166 pPg->dirty = 0;
danielk197713f72992005-12-18 08:51:22 +00003167 if( rc!=SQLITE_OK ){
danielk1977aef0bf62005-12-30 16:28:01 +00003168 return rc;
danielk197713f72992005-12-18 08:51:22 +00003169 }
3170 }
3171 assert( pPg->dirty==0 );
3172
3173 /* If the page we are recycling is marked as alwaysRollback, then
3174 ** set the global alwaysRollback flag, thus disabling the
drh80e35f42007-03-30 14:06:34 +00003175 ** sqlite3PagerDontRollback() optimization for the rest of this transaction.
danielk197713f72992005-12-18 08:51:22 +00003176 ** It is necessary to do this because the page marked alwaysRollback
3177 ** might be reloaded at a later time but at that point we won't remember
3178 ** that is was marked alwaysRollback. This means that all pages must
3179 ** be marked as alwaysRollback from here on out.
3180 */
3181 if( pPg->alwaysRollback ){
drh602c2372007-03-01 00:29:13 +00003182 IOTRACE(("ALWAYS_ROLLBACK %p\n", pPager))
danielk197713f72992005-12-18 08:51:22 +00003183 pPager->alwaysRollback = 1;
3184 }
3185
3186 /* Unlink the old page from the free list and the hash table
3187 */
3188 unlinkPage(pPg);
drh7c4ac0c2007-04-05 11:25:58 +00003189 assert( pPg->pgno==0 );
danielk197713f72992005-12-18 08:51:22 +00003190
3191 *ppPg = pPg;
3192 return SQLITE_OK;
3193}
3194
drh86f8c192007-08-22 00:39:19 +00003195#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk197713f72992005-12-18 08:51:22 +00003196/*
3197** This function is called to free superfluous dynamically allocated memory
3198** held by the pager system. Memory in use by any SQLite pager allocated
drh17435752007-08-16 04:30:38 +00003199** by the current thread may be sqlite3_free()ed.
danielk197713f72992005-12-18 08:51:22 +00003200**
3201** nReq is the number of bytes of memory required. Once this much has
drh86f8c192007-08-22 00:39:19 +00003202** been released, the function returns. The return value is the total number
danielk197713f72992005-12-18 08:51:22 +00003203** of bytes of memory released.
3204*/
danielk19773b8a05f2007-03-19 17:44:26 +00003205int sqlite3PagerReleaseMemory(int nReq){
drh86f8c192007-08-22 00:39:19 +00003206 int nReleased = 0; /* Bytes of memory released so far */
3207 sqlite3_mutex *mutex; /* The MEM2 mutex */
3208 Pager *pPager; /* For looping over pagers */
drhe5fe6902007-12-07 18:55:28 +00003209 BusyHandler *savedBusy; /* Saved copy of the busy handler */
danielk19779f61c2f2007-08-27 17:27:49 +00003210 int rc = SQLITE_OK;
danielk197713f72992005-12-18 08:51:22 +00003211
drh86f8c192007-08-22 00:39:19 +00003212 /* Acquire the memory-management mutex
danielk1977441b09a2006-01-05 13:48:29 +00003213 */
drh86f8c192007-08-22 00:39:19 +00003214 mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
3215 sqlite3_mutex_enter(mutex);
3216
3217 /* Signal all database connections that memory management wants
3218 ** to have access to the pagers.
3219 */
3220 for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
3221 pPager->iInUseMM = 1;
danielk1977441b09a2006-01-05 13:48:29 +00003222 }
3223
danielk19779f61c2f2007-08-27 17:27:49 +00003224 while( rc==SQLITE_OK && (nReq<0 || nReleased<nReq) ){
3225 PgHdr *pPg;
3226 PgHdr *pRecycled;
3227
3228 /* Try to find a page to recycle that does not require a sync(). If
3229 ** this is not possible, find one that does require a sync().
3230 */
3231 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
3232 pPg = sqlite3LruPageList.pFirstSynced;
3233 while( pPg && (pPg->needSync || pPg->pPager->iInUseDB) ){
3234 pPg = pPg->gfree.pNext;
3235 }
3236 if( !pPg ){
3237 pPg = sqlite3LruPageList.pFirst;
3238 while( pPg && pPg->pPager->iInUseDB ){
3239 pPg = pPg->gfree.pNext;
3240 }
3241 }
3242 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
danielk197713f72992005-12-18 08:51:22 +00003243
danielk197784f786f2007-08-28 08:00:17 +00003244 /* If pPg==0, then the block above has failed to find a page to
3245 ** recycle. In this case return early - no further memory will
3246 ** be released.
3247 */
danielk19779f61c2f2007-08-27 17:27:49 +00003248 if( !pPg ) break;
danielk197713f72992005-12-18 08:51:22 +00003249
danielk19779f61c2f2007-08-27 17:27:49 +00003250 pPager = pPg->pPager;
3251 assert(!pPg->needSync || pPg==pPager->lru.pFirst);
3252 assert(pPg->needSync || pPg==pPager->lru.pFirstSynced);
3253
drhe5fe6902007-12-07 18:55:28 +00003254 savedBusy = pPager->pBusyHandler;
3255 pPager->pBusyHandler = 0;
danielk1977843e65f2007-09-01 16:16:15 +00003256 rc = pager_recycle(pPager, &pRecycled);
drhe5fe6902007-12-07 18:55:28 +00003257 pPager->pBusyHandler = savedBusy;
danielk19779f61c2f2007-08-27 17:27:49 +00003258 assert(pRecycled==pPg || rc!=SQLITE_OK);
3259 if( rc==SQLITE_OK ){
3260 /* We've found a page to free. At this point the page has been
3261 ** removed from the page hash-table, free-list and synced-list
3262 ** (pFirstSynced). It is still in the all pages (pAll) list.
3263 ** Remove it from this list before freeing.
3264 **
3265 ** Todo: Check the Pager.pStmt list to make sure this is Ok. It
3266 ** probably is though.
danielk197713f72992005-12-18 08:51:22 +00003267 */
danielk19779f61c2f2007-08-27 17:27:49 +00003268 PgHdr *pTmp;
3269 assert( pPg );
3270 if( pPg==pPager->pAll ){
3271 pPager->pAll = pPg->pNextAll;
3272 }else{
3273 for( pTmp=pPager->pAll; pTmp->pNextAll!=pPg; pTmp=pTmp->pNextAll ){}
3274 pTmp->pNextAll = pPg->pNextAll;
danielk197713f72992005-12-18 08:51:22 +00003275 }
danielk19779f61c2f2007-08-27 17:27:49 +00003276 nReleased += (
3277 sizeof(*pPg) + pPager->pageSize
3278 + sizeof(u32) + pPager->nExtra
3279 + MEMDB*sizeof(PgHistory)
3280 );
3281 IOTRACE(("PGFREE %p %d *\n", pPager, pPg->pgno));
3282 PAGER_INCR(sqlite3_pager_pgfree_count);
drh0d180202008-02-14 23:26:56 +00003283 sqlite3_free(pPg->pData);
danielk19779f61c2f2007-08-27 17:27:49 +00003284 sqlite3_free(pPg);
danielk197784f786f2007-08-28 08:00:17 +00003285 pPager->nPage--;
danielk19779f61c2f2007-08-27 17:27:49 +00003286 }else{
3287 /* An error occured whilst writing to the database file or
3288 ** journal in pager_recycle(). The error is not returned to the
3289 ** caller of this function. Instead, set the Pager.errCode variable.
3290 ** The error will be returned to the user (or users, in the case
3291 ** of a shared pager cache) of the pager for which the error occured.
3292 */
3293 assert(
3294 (rc&0xff)==SQLITE_IOERR ||
3295 rc==SQLITE_FULL ||
3296 rc==SQLITE_BUSY
3297 );
3298 assert( pPager->state>=PAGER_RESERVED );
3299 pager_error(pPager, rc);
danielk197713f72992005-12-18 08:51:22 +00003300 }
3301 }
danielk1977aef0bf62005-12-30 16:28:01 +00003302
drh86f8c192007-08-22 00:39:19 +00003303 /* Clear the memory management flags and release the mutex
3304 */
3305 for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
3306 pPager->iInUseMM = 0;
3307 }
3308 sqlite3_mutex_leave(mutex);
3309
3310 /* Return the number of bytes released
3311 */
danielk197713f72992005-12-18 08:51:22 +00003312 return nReleased;
3313}
drh86f8c192007-08-22 00:39:19 +00003314#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
danielk197713f72992005-12-18 08:51:22 +00003315
drh165ffe92005-03-15 17:09:30 +00003316/*
danielk1977e180dd92007-04-05 17:15:52 +00003317** Read the content of page pPg out of the database file.
3318*/
3319static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){
3320 int rc;
danielk197762079062007-08-15 17:08:46 +00003321 i64 offset;
danielk1977e180dd92007-04-05 17:15:52 +00003322 assert( MEMDB==0 );
danielk19777a2b1ee2007-08-21 14:27:01 +00003323 assert(pPager->fd->pMethods||pPager->tempFile);
3324 if( !pPager->fd->pMethods ){
3325 return SQLITE_IOERR_SHORT_READ;
3326 }
danielk197762079062007-08-15 17:08:46 +00003327 offset = (pgno-1)*(i64)pPager->pageSize;
3328 rc = sqlite3OsRead(pPager->fd, PGHDR_TO_DATA(pPg), pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00003329 PAGER_INCR(sqlite3_pager_readdb_count);
3330 PAGER_INCR(pPager->nRead);
3331 IOTRACE(("PGIN %p %d\n", pPager, pgno));
drh86a88112007-04-16 15:02:19 +00003332 if( pgno==1 ){
3333 memcpy(&pPager->dbFileVers, &((u8*)PGHDR_TO_DATA(pPg))[24],
3334 sizeof(pPager->dbFileVers));
3335 }
danielk1977e180dd92007-04-05 17:15:52 +00003336 CODEC1(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3);
drh477731b2007-06-16 03:06:27 +00003337 PAGERTRACE4("FETCH %d page %d hash(%08x)\n",
3338 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg));
danielk1977e180dd92007-04-05 17:15:52 +00003339 return rc;
3340}
3341
3342
3343/*
danielk1977e277be02007-03-23 18:12:06 +00003344** This function is called to obtain the shared lock required before
3345** data may be read from the pager cache. If the shared lock has already
3346** been obtained, this function is a no-op.
danielk1977393f0682007-03-31 10:00:48 +00003347**
3348** Immediately after obtaining the shared lock (if required), this function
3349** checks for a hot-journal file. If one is found, an emergency rollback
3350** is performed immediately.
danielk1977e277be02007-03-23 18:12:06 +00003351*/
3352static int pagerSharedLock(Pager *pPager){
3353 int rc = SQLITE_OK;
danielk1977ae72d982007-10-03 08:46:44 +00003354 int isHot = 0;
danielk1977e277be02007-03-23 18:12:06 +00003355
danielk1977ae72d982007-10-03 08:46:44 +00003356 /* If this database is opened for exclusive access, has no outstanding
3357 ** page references and is in an error-state, now is the chance to clear
3358 ** the error. Discard the contents of the pager-cache and treat any
3359 ** open journal file as a hot-journal.
3360 */
3361 if( !MEMDB && pPager->exclusiveMode && pPager->nRef==0 && pPager->errCode ){
3362 if( pPager->journalOpen ){
3363 isHot = 1;
3364 }
3365 pager_reset(pPager);
3366 pPager->errCode = SQLITE_OK;
3367 }
3368
3369 /* If the pager is still in an error state, do not proceed. The error
3370 ** state will be cleared at some point in the future when all page
3371 ** references are dropped and the cache can be discarded.
3372 */
3373 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
3374 return pPager->errCode;
3375 }
3376
3377 if( pPager->state==PAGER_UNLOCK || isHot ){
danielk1977b4b47412007-08-17 15:53:36 +00003378 sqlite3_vfs *pVfs = pPager->pVfs;
danielk1977e277be02007-03-23 18:12:06 +00003379 if( !MEMDB ){
3380 assert( pPager->nRef==0 );
3381 if( !pPager->noReadlock ){
3382 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
3383 if( rc!=SQLITE_OK ){
3384 return pager_error(pPager, rc);
3385 }
3386 assert( pPager->state>=SHARED_LOCK );
3387 }
3388
3389 /* If a journal file exists, and there is no RESERVED lock on the
3390 ** database file, then it either needs to be played back or deleted.
3391 */
drh19db9352008-03-27 22:42:51 +00003392 rc = hasHotJournal(pPager);
3393 if( rc<0 ){
danielk1977a3e52182008-04-14 16:37:10 +00003394 return SQLITE_IOERR_NOMEM;
drh19db9352008-03-27 22:42:51 +00003395 }
3396 if( rc==1 || isHot ){
danielk1977e277be02007-03-23 18:12:06 +00003397 /* Get an EXCLUSIVE lock on the database file. At this point it is
3398 ** important that a RESERVED lock is not obtained on the way to the
3399 ** EXCLUSIVE lock. If it were, another process might open the
3400 ** database file, detect the RESERVED lock, and conclude that the
3401 ** database is safe to read while this process is still rolling it
3402 ** back.
3403 **
3404 ** Because the intermediate RESERVED lock is not requested, the
3405 ** second process will get to this point in the code and fail to
drh85b623f2007-12-13 21:54:09 +00003406 ** obtain its own EXCLUSIVE lock on the database file.
danielk1977e277be02007-03-23 18:12:06 +00003407 */
danielk1977ae72d982007-10-03 08:46:44 +00003408 if( pPager->state<EXCLUSIVE_LOCK ){
3409 rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
3410 if( rc!=SQLITE_OK ){
3411 pager_unlock(pPager);
3412 return pager_error(pPager, rc);
3413 }
3414 pPager->state = PAGER_EXCLUSIVE;
danielk1977e277be02007-03-23 18:12:06 +00003415 }
danielk1977e277be02007-03-23 18:12:06 +00003416
drh16e45a42008-04-19 20:34:18 +00003417 /* Open the journal for read/write access. This is because in
drhfd131da2007-08-07 17:13:03 +00003418 ** exclusive-access mode the file descriptor will be kept open and
danielk1977979f38e2007-03-27 16:19:51 +00003419 ** possibly used for a transaction later on. On some systems, the
3420 ** OsTruncate() call used in exclusive-access mode also requires
3421 ** a read/write file handle.
danielk1977e277be02007-03-23 18:12:06 +00003422 */
drh16e45a42008-04-19 20:34:18 +00003423 if( !isHot && pPager->journalOpen==0 ){
danielk1977ce98bba2008-04-03 10:13:01 +00003424 int res = sqlite3OsAccess(pVfs,pPager->zJournal,SQLITE_ACCESS_EXISTS);
3425 if( res==1 ){
danielk1977ae72d982007-10-03 08:46:44 +00003426 int fout = 0;
3427 int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
3428 assert( !pPager->tempFile );
3429 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
3430 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
3431 if( fout&SQLITE_OPEN_READONLY ){
3432 rc = SQLITE_BUSY;
3433 sqlite3OsClose(pPager->jfd);
3434 }
drh16e45a42008-04-19 20:34:18 +00003435 }else if( res==0 ){
3436 /* If the journal does not exist, that means some other process
3437 ** has already rolled it back */
3438 rc = SQLITE_BUSY;
danielk1977ce98bba2008-04-03 10:13:01 +00003439 }else{
drh16e45a42008-04-19 20:34:18 +00003440 /* If sqlite3OsAccess() returns a negative value, that means it
3441 ** failed a memory allocation */
3442 rc = SQLITE_IOERR_NOMEM;
danielk1977979f38e2007-03-27 16:19:51 +00003443 }
3444 }
danielk1977e277be02007-03-23 18:12:06 +00003445 if( rc!=SQLITE_OK ){
3446 pager_unlock(pPager);
drh1aa5af12008-03-07 19:51:14 +00003447 switch( rc ){
3448 case SQLITE_NOMEM:
3449 case SQLITE_IOERR_UNLOCK:
3450 case SQLITE_IOERR_NOMEM:
3451 return rc;
3452 default:
3453 return SQLITE_BUSY;
3454 }
danielk1977e277be02007-03-23 18:12:06 +00003455 }
3456 pPager->journalOpen = 1;
3457 pPager->journalStarted = 0;
3458 pPager->journalOff = 0;
3459 pPager->setMaster = 0;
3460 pPager->journalHdr = 0;
3461
3462 /* Playback and delete the journal. Drop the database write
3463 ** lock and reacquire the read lock.
3464 */
3465 rc = pager_playback(pPager, 1);
3466 if( rc!=SQLITE_OK ){
3467 return pager_error(pPager, rc);
3468 }
danielk1977c5859712007-03-26 12:26:27 +00003469 assert(pPager->state==PAGER_SHARED ||
3470 (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
3471 );
danielk1977e277be02007-03-23 18:12:06 +00003472 }
3473
3474 if( pPager->pAll ){
danielk197724168722007-04-02 05:07:47 +00003475 /* The shared-lock has just been acquired on the database file
3476 ** and there are already pages in the cache (from a previous
drh86a88112007-04-16 15:02:19 +00003477 ** read or write transaction). Check to see if the database
3478 ** has been modified. If the database has changed, flush the
3479 ** cache.
3480 **
3481 ** Database changes is detected by looking at 15 bytes beginning
3482 ** at offset 24 into the file. The first 4 of these 16 bytes are
3483 ** a 32-bit counter that is incremented with each change. The
3484 ** other bytes change randomly with each file change when
3485 ** a codec is in use.
3486 **
3487 ** There is a vanishingly small chance that a change will not be
drh6fa51032007-05-09 20:35:31 +00003488 ** detected. The chance of an undetected change is so small that
drh86a88112007-04-16 15:02:19 +00003489 ** it can be neglected.
danielk197724168722007-04-02 05:07:47 +00003490 */
drh86a88112007-04-16 15:02:19 +00003491 char dbFileVers[sizeof(pPager->dbFileVers)];
danielk1977e180dd92007-04-05 17:15:52 +00003492 sqlite3PagerPagecount(pPager);
danielk197724168722007-04-02 05:07:47 +00003493
danielk1977e180dd92007-04-05 17:15:52 +00003494 if( pPager->errCode ){
3495 return pPager->errCode;
danielk1977e277be02007-03-23 18:12:06 +00003496 }
3497
danielk1977e180dd92007-04-05 17:15:52 +00003498 if( pPager->dbSize>0 ){
drhae5e4452007-05-03 17:18:36 +00003499 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
danielk197762079062007-08-15 17:08:46 +00003500 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
danielk1977e180dd92007-04-05 17:15:52 +00003501 if( rc!=SQLITE_OK ){
3502 return rc;
3503 }
drh86a88112007-04-16 15:02:19 +00003504 }else{
3505 memset(dbFileVers, 0, sizeof(dbFileVers));
danielk1977e180dd92007-04-05 17:15:52 +00003506 }
3507
drh86a88112007-04-16 15:02:19 +00003508 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
danielk1977e180dd92007-04-05 17:15:52 +00003509 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00003510 }
3511 }
3512 }
danielk1977c5859712007-03-26 12:26:27 +00003513 assert( pPager->exclusiveMode || pPager->state<=PAGER_SHARED );
3514 if( pPager->state==PAGER_UNLOCK ){
3515 pPager->state = PAGER_SHARED;
3516 }
danielk1977e277be02007-03-23 18:12:06 +00003517 }
3518
3519 return rc;
3520}
3521
3522/*
danielk1977e180dd92007-04-05 17:15:52 +00003523** Allocate a PgHdr object. Either create a new one or reuse
3524** an existing one that is not otherwise in use.
3525**
3526** A new PgHdr structure is created if any of the following are
3527** true:
3528**
3529** (1) We have not exceeded our maximum allocated cache size
3530** as set by the "PRAGMA cache_size" command.
3531**
3532** (2) There are no unused PgHdr objects available at this time.
3533**
3534** (3) This is an in-memory database.
3535**
3536** (4) There are no PgHdr objects that do not require a journal
3537** file sync and a sync of the journal file is currently
3538** prohibited.
3539**
3540** Otherwise, reuse an existing PgHdr. In other words, reuse an
3541** existing PgHdr if all of the following are true:
3542**
3543** (1) We have reached or exceeded the maximum cache size
3544** allowed by "PRAGMA cache_size".
3545**
3546** (2) There is a PgHdr available with PgHdr->nRef==0
3547**
3548** (3) We are not in an in-memory database
3549**
3550** (4) Either there is an available PgHdr that does not need
3551** to be synced to disk or else disk syncing is currently
3552** allowed.
danielk197724168722007-04-02 05:07:47 +00003553*/
3554static int pagerAllocatePage(Pager *pPager, PgHdr **ppPg){
3555 int rc = SQLITE_OK;
3556 PgHdr *pPg;
drh1e3af332007-10-20 13:17:54 +00003557 int nByteHdr;
danielk197724168722007-04-02 05:07:47 +00003558
danielk1977e180dd92007-04-05 17:15:52 +00003559 /* Create a new PgHdr if any of the four conditions defined
danielk19772a6bdf62007-08-20 16:07:00 +00003560 ** above are met: */
danielk1977e180dd92007-04-05 17:15:52 +00003561 if( pPager->nPage<pPager->mxPage
danielk19779f61c2f2007-08-27 17:27:49 +00003562 || pPager->lru.pFirst==0
danielk1977e180dd92007-04-05 17:15:52 +00003563 || MEMDB
danielk19779f61c2f2007-08-27 17:27:49 +00003564 || (pPager->lru.pFirstSynced==0 && pPager->doNotSync)
danielk1977e180dd92007-04-05 17:15:52 +00003565 ){
drh0d180202008-02-14 23:26:56 +00003566 void *pData;
danielk197724168722007-04-02 05:07:47 +00003567 if( pPager->nPage>=pPager->nHash ){
3568 pager_resize_hash_table(pPager,
3569 pPager->nHash<256 ? 256 : pPager->nHash*2);
3570 if( pPager->nHash==0 ){
3571 rc = SQLITE_NOMEM;
3572 goto pager_allocate_out;
3573 }
3574 }
drhed138fb2007-08-22 22:04:37 +00003575 pagerLeave(pPager);
drh1e3af332007-10-20 13:17:54 +00003576 nByteHdr = sizeof(*pPg) + sizeof(u32) + pPager->nExtra
3577 + MEMDB*sizeof(PgHistory);
drh0d180202008-02-14 23:26:56 +00003578 pPg = sqlite3_malloc( nByteHdr );
3579 if( pPg ){
3580 pData = sqlite3_malloc( pPager->pageSize );
3581 if( pData==0 ){
3582 sqlite3_free(pPg);
3583 pPg = 0;
3584 }
3585 }
drhed138fb2007-08-22 22:04:37 +00003586 pagerEnter(pPager);
danielk197724168722007-04-02 05:07:47 +00003587 if( pPg==0 ){
3588 rc = SQLITE_NOMEM;
3589 goto pager_allocate_out;
3590 }
drh1e3af332007-10-20 13:17:54 +00003591 memset(pPg, 0, nByteHdr);
drh0d180202008-02-14 23:26:56 +00003592 pPg->pData = pData;
danielk197724168722007-04-02 05:07:47 +00003593 pPg->pPager = pPager;
3594 pPg->pNextAll = pPager->pAll;
3595 pPager->pAll = pPg;
3596 pPager->nPage++;
danielk197724168722007-04-02 05:07:47 +00003597 }else{
3598 /* Recycle an existing page with a zero ref-count. */
danielk1977843e65f2007-09-01 16:16:15 +00003599 rc = pager_recycle(pPager, &pPg);
danielk1977e965ac72007-06-13 15:22:28 +00003600 if( rc==SQLITE_BUSY ){
3601 rc = SQLITE_IOERR_BLOCKED;
3602 }
danielk197724168722007-04-02 05:07:47 +00003603 if( rc!=SQLITE_OK ){
3604 goto pager_allocate_out;
3605 }
3606 assert( pPager->state>=SHARED_LOCK );
3607 assert(pPg);
3608 }
3609 *ppPg = pPg;
3610
3611pager_allocate_out:
3612 return rc;
3613}
3614
3615/*
drhd33d5a82007-04-26 12:11:28 +00003616** Make sure we have the content for a page. If the page was
3617** previously acquired with noContent==1, then the content was
3618** just initialized to zeros instead of being read from disk.
3619** But now we need the real data off of disk. So make sure we
3620** have it. Read it in if we do not have it already.
3621*/
3622static int pager_get_content(PgHdr *pPg){
3623 if( pPg->needRead ){
3624 int rc = readDbPage(pPg->pPager, pPg, pPg->pgno);
3625 if( rc==SQLITE_OK ){
3626 pPg->needRead = 0;
3627 }else{
3628 return rc;
3629 }
3630 }
3631 return SQLITE_OK;
3632}
3633
3634/*
drhd9b02572001-04-15 00:37:09 +00003635** Acquire a page.
3636**
drh58a11682001-11-10 13:51:08 +00003637** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00003638** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00003639**
drhd33d5a82007-04-26 12:11:28 +00003640** This routine works for any page number greater than 0. If the database
drh306dc212001-05-21 13:45:10 +00003641** file is smaller than the requested page, then no actual disk
3642** read occurs and the memory image of the page is initialized to
3643** all zeros. The extra data appended to a page is always initialized
3644** to zeros the first time a page is loaded into memory.
3645**
drhd9b02572001-04-15 00:37:09 +00003646** The acquisition might fail for several reasons. In all cases,
3647** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00003648**
drhd33d5a82007-04-26 12:11:28 +00003649** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
drh7e3b0a02001-04-28 16:52:40 +00003650** to find a page in the in-memory cache first. If the page is not already
drhd33d5a82007-04-26 12:11:28 +00003651** in memory, this routine goes to disk to read it in whereas Lookup()
drh7e3b0a02001-04-28 16:52:40 +00003652** just returns 0. This routine acquires a read-lock the first time it
3653** has to go to disk, and could also playback an old journal if necessary.
drhd33d5a82007-04-26 12:11:28 +00003654** Since Lookup() never goes to disk, it never has to deal with locks
drh7e3b0a02001-04-28 16:52:40 +00003655** or journal files.
drh0787db62007-03-04 13:15:27 +00003656**
drh538f5702007-04-13 02:14:30 +00003657** If noContent is false, the page contents are actually read from disk.
3658** If noContent is true, it means that we do not care about the contents
3659** of the page at this time, so do not do a disk read. Just fill in the
3660** page content with zeros. But mark the fact that we have not read the
3661** content by setting the PgHdr.needRead flag. Later on, if
drhd33d5a82007-04-26 12:11:28 +00003662** sqlite3PagerWrite() is called on this page or if this routine is
3663** called again with noContent==0, that means that the content is needed
3664** and the disk read should occur at that point.
drhed7c8552001-04-11 14:29:21 +00003665*/
drh86f8c192007-08-22 00:39:19 +00003666static int pagerAcquire(
drh538f5702007-04-13 02:14:30 +00003667 Pager *pPager, /* The pager open on the database file */
3668 Pgno pgno, /* Page number to fetch */
3669 DbPage **ppPage, /* Write a pointer to the page here */
3670 int noContent /* Do not bother reading content from disk if true */
3671){
drhed7c8552001-04-11 14:29:21 +00003672 PgHdr *pPg;
drh165ffe92005-03-15 17:09:30 +00003673 int rc;
drhed7c8552001-04-11 14:29:21 +00003674
danielk1977e277be02007-03-23 18:12:06 +00003675 assert( pPager->state==PAGER_UNLOCK || pPager->nRef>0 || pgno==1 );
3676
danielk197726836652005-01-17 01:33:13 +00003677 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
3678 ** number greater than this, or zero, is requested.
3679 */
drh267cb322005-09-16 17:16:52 +00003680 if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh49285702005-09-17 15:20:26 +00003681 return SQLITE_CORRUPT_BKPT;
danielk197726836652005-01-17 01:33:13 +00003682 }
3683
drhd9b02572001-04-15 00:37:09 +00003684 /* Make sure we have not hit any critical errors.
3685 */
drh836faa42003-01-11 13:30:57 +00003686 assert( pPager!=0 );
drh2e6d11b2003-04-25 15:37:57 +00003687 *ppPage = 0;
drhd9b02572001-04-15 00:37:09 +00003688
danielk197713adf8a2004-06-03 16:08:41 +00003689 /* If this is the first page accessed, then get a SHARED lock
danielk1977334cdb62007-03-26 08:05:12 +00003690 ** on the database file. pagerSharedLock() is a no-op if
3691 ** a database lock is already held.
drhed7c8552001-04-11 14:29:21 +00003692 */
danielk1977e277be02007-03-23 18:12:06 +00003693 rc = pagerSharedLock(pPager);
3694 if( rc!=SQLITE_OK ){
3695 return rc;
drhed7c8552001-04-11 14:29:21 +00003696 }
danielk1977e277be02007-03-23 18:12:06 +00003697 assert( pPager->state!=PAGER_UNLOCK );
3698
3699 pPg = pager_lookup(pPager, pgno);
drhed7c8552001-04-11 14:29:21 +00003700 if( pPg==0 ){
drhd9b02572001-04-15 00:37:09 +00003701 /* The requested page is not in the page cache. */
danielk1977979f38e2007-03-27 16:19:51 +00003702 int nMax;
drhed7c8552001-04-11 14:29:21 +00003703 int h;
drh538f5702007-04-13 02:14:30 +00003704 PAGER_INCR(pPager->nMiss);
danielk197724168722007-04-02 05:07:47 +00003705 rc = pagerAllocatePage(pPager, &pPg);
3706 if( rc!=SQLITE_OK ){
3707 return rc;
drhed7c8552001-04-11 14:29:21 +00003708 }
danielk197724168722007-04-02 05:07:47 +00003709
drhed7c8552001-04-11 14:29:21 +00003710 pPg->pgno = pgno;
danielk1977f35843b2007-04-07 15:03:17 +00003711 assert( !MEMDB || pgno>pPager->stmtSize );
drhf5e7bb52008-02-18 14:47:33 +00003712 pPg->inJournal = sqlite3BitvecTest(pPager->pInJournal, pgno);
3713 pPg->needSync = 0;
danielk1977f35843b2007-04-07 15:03:17 +00003714
drh605ad8f2006-05-03 23:34:05 +00003715 makeClean(pPg);
drhed7c8552001-04-11 14:29:21 +00003716 pPg->nRef = 1;
danielk197775bab7d2006-01-23 13:09:45 +00003717
drhd9b02572001-04-15 00:37:09 +00003718 pPager->nRef++;
drh2e6d11b2003-04-25 15:37:57 +00003719 if( pPager->nExtra>0 ){
drh90f5ecb2004-07-22 01:19:35 +00003720 memset(PGHDR_TO_EXTRA(pPg, pPager), 0, pPager->nExtra);
drh2e6d11b2003-04-25 15:37:57 +00003721 }
danielk1977979f38e2007-03-27 16:19:51 +00003722 nMax = sqlite3PagerPagecount(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00003723 if( pPager->errCode ){
danielk1977efaaf572006-01-16 11:29:19 +00003724 rc = pPager->errCode;
danielk1977ae72d982007-10-03 08:46:44 +00003725 sqlite3PagerUnref(pPg);
drh2e6d11b2003-04-25 15:37:57 +00003726 return rc;
3727 }
danielk197775bab7d2006-01-23 13:09:45 +00003728
3729 /* Populate the page with data, either by reading from the database
3730 ** file, or by setting the entire page to zero.
3731 */
drhd215acf2007-04-13 04:01:58 +00003732 if( nMax<(int)pgno || MEMDB || (noContent && !pPager->alwaysRollback) ){
drhf8e632b2007-05-08 14:51:36 +00003733 if( pgno>pPager->mxPgno ){
danielk1977de3bea72007-05-09 15:56:39 +00003734 sqlite3PagerUnref(pPg);
drhf8e632b2007-05-08 14:51:36 +00003735 return SQLITE_FULL;
3736 }
drh90f5ecb2004-07-22 01:19:35 +00003737 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
drhd215acf2007-04-13 04:01:58 +00003738 pPg->needRead = noContent && !pPager->alwaysRollback;
drh538f5702007-04-13 02:14:30 +00003739 IOTRACE(("ZERO %p %d\n", pPager, pgno));
drh306dc212001-05-21 13:45:10 +00003740 }else{
danielk1977e180dd92007-04-05 17:15:52 +00003741 rc = readDbPage(pPager, pPg, pgno);
drh551b7732006-11-06 21:20:25 +00003742 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
3743 pPg->pgno = 0;
danielk19773b8a05f2007-03-19 17:44:26 +00003744 sqlite3PagerUnref(pPg);
drh551b7732006-11-06 21:20:25 +00003745 return rc;
drh81a20f22001-10-12 17:30:04 +00003746 }
danielk1977b4626a32007-04-28 15:47:43 +00003747 pPg->needRead = 0;
drh306dc212001-05-21 13:45:10 +00003748 }
danielk197775bab7d2006-01-23 13:09:45 +00003749
3750 /* Link the page into the page hash table */
drh8ca0c722006-05-07 17:49:38 +00003751 h = pgno & (pPager->nHash-1);
drh3765df42006-06-28 18:18:09 +00003752 assert( pgno!=0 );
danielk197775bab7d2006-01-23 13:09:45 +00003753 pPg->pNextHash = pPager->aHash[h];
3754 pPager->aHash[h] = pPg;
3755 if( pPg->pNextHash ){
3756 assert( pPg->pNextHash->pPrevHash==0 );
3757 pPg->pNextHash->pPrevHash = pPg;
3758 }
3759
danielk19773c407372005-02-15 02:54:14 +00003760#ifdef SQLITE_CHECK_PAGES
3761 pPg->pageHash = pager_pagehash(pPg);
3762#endif
drhed7c8552001-04-11 14:29:21 +00003763 }else{
drhd9b02572001-04-15 00:37:09 +00003764 /* The requested page is in the page cache. */
danielk1977e277be02007-03-23 18:12:06 +00003765 assert(pPager->nRef>0 || pgno==1);
drh538f5702007-04-13 02:14:30 +00003766 PAGER_INCR(pPager->nHit);
drhd33d5a82007-04-26 12:11:28 +00003767 if( !noContent ){
3768 rc = pager_get_content(pPg);
3769 if( rc ){
3770 return rc;
3771 }
3772 }
drhdf0b3b02001-06-23 11:36:20 +00003773 page_ref(pPg);
drhed7c8552001-04-11 14:29:21 +00003774 }
danielk19773b8a05f2007-03-19 17:44:26 +00003775 *ppPage = pPg;
drhed7c8552001-04-11 14:29:21 +00003776 return SQLITE_OK;
3777}
drh86f8c192007-08-22 00:39:19 +00003778int sqlite3PagerAcquire(
3779 Pager *pPager, /* The pager open on the database file */
3780 Pgno pgno, /* Page number to fetch */
3781 DbPage **ppPage, /* Write a pointer to the page here */
3782 int noContent /* Do not bother reading content from disk if true */
3783){
3784 int rc;
3785 pagerEnter(pPager);
3786 rc = pagerAcquire(pPager, pgno, ppPage, noContent);
3787 pagerLeave(pPager);
3788 return rc;
3789}
3790
drhed7c8552001-04-11 14:29:21 +00003791
3792/*
drh7e3b0a02001-04-28 16:52:40 +00003793** Acquire a page if it is already in the in-memory cache. Do
3794** not read the page from disk. Return a pointer to the page,
3795** or 0 if the page is not in cache.
3796**
danielk19773b8a05f2007-03-19 17:44:26 +00003797** See also sqlite3PagerGet(). The difference between this routine
3798** and sqlite3PagerGet() is that _get() will go to the disk and read
drh7e3b0a02001-04-28 16:52:40 +00003799** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00003800** returns NULL if the page is not in cache or if a disk I/O error
3801** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00003802*/
danielk19773b8a05f2007-03-19 17:44:26 +00003803DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
drh86f8c192007-08-22 00:39:19 +00003804 PgHdr *pPg = 0;
drh7e3b0a02001-04-28 16:52:40 +00003805
drh836faa42003-01-11 13:30:57 +00003806 assert( pPager!=0 );
3807 assert( pgno!=0 );
danielk1977e277be02007-03-23 18:12:06 +00003808
drh86f8c192007-08-22 00:39:19 +00003809 pagerEnter(pPager);
danielk1977e277be02007-03-23 18:12:06 +00003810 if( pPager->state==PAGER_UNLOCK ){
danielk1977334cdb62007-03-26 08:05:12 +00003811 assert( !pPager->pAll || pPager->exclusiveMode );
drh86f8c192007-08-22 00:39:19 +00003812 }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
3813 /* Do nothing */
3814 }else if( (pPg = pager_lookup(pPager, pgno))!=0 ){
3815 page_ref(pPg);
danielk1977e277be02007-03-23 18:12:06 +00003816 }
drh86f8c192007-08-22 00:39:19 +00003817 pagerLeave(pPager);
danielk19773b8a05f2007-03-19 17:44:26 +00003818 return pPg;
drh7e3b0a02001-04-28 16:52:40 +00003819}
3820
3821/*
drhed7c8552001-04-11 14:29:21 +00003822** Release a page.
3823**
3824** If the number of references to the page drop to zero, then the
3825** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00003826** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00003827** removed.
3828*/
danielk19773b8a05f2007-03-19 17:44:26 +00003829int sqlite3PagerUnref(DbPage *pPg){
danielk1977ae72d982007-10-03 08:46:44 +00003830 Pager *pPager = pPg->pPager;
drhd9b02572001-04-15 00:37:09 +00003831
3832 /* Decrement the reference count for this page
3833 */
drhed7c8552001-04-11 14:29:21 +00003834 assert( pPg->nRef>0 );
drh86f8c192007-08-22 00:39:19 +00003835 pagerEnter(pPg->pPager);
drhed7c8552001-04-11 14:29:21 +00003836 pPg->nRef--;
drhd9b02572001-04-15 00:37:09 +00003837
danielk19773c407372005-02-15 02:54:14 +00003838 CHECK_PAGE(pPg);
3839
drh72f82862001-05-24 21:06:34 +00003840 /* When the number of references to a page reach 0, call the
3841 ** destructor and add the page to the freelist.
drhd9b02572001-04-15 00:37:09 +00003842 */
drhed7c8552001-04-11 14:29:21 +00003843 if( pPg->nRef==0 ){
danielk19779f61c2f2007-08-27 17:27:49 +00003844
3845 lruListAdd(pPg);
drh72f82862001-05-24 21:06:34 +00003846 if( pPager->xDestructor ){
danielk19773b8a05f2007-03-19 17:44:26 +00003847 pPager->xDestructor(pPg, pPager->pageSize);
drh72f82862001-05-24 21:06:34 +00003848 }
drhd9b02572001-04-15 00:37:09 +00003849
3850 /* When all pages reach the freelist, drop the read lock from
3851 ** the database file.
3852 */
3853 pPager->nRef--;
3854 assert( pPager->nRef>=0 );
danielk19773dedc192007-03-27 17:37:31 +00003855 if( pPager->nRef==0 && (!pPager->exclusiveMode || pPager->journalOff>0) ){
danielk1977e277be02007-03-23 18:12:06 +00003856 pagerUnlockAndRollback(pPager);
drhd9b02572001-04-15 00:37:09 +00003857 }
drhed7c8552001-04-11 14:29:21 +00003858 }
danielk1977ae72d982007-10-03 08:46:44 +00003859 pagerLeave(pPager);
drhd9b02572001-04-15 00:37:09 +00003860 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00003861}
3862
3863/*
drha6abd042004-06-09 17:37:22 +00003864** Create a journal file for pPager. There should already be a RESERVED
3865** or EXCLUSIVE lock on the database file when this routine is called.
drhda47d772002-12-02 04:25:19 +00003866**
3867** Return SQLITE_OK if everything. Return an error code and release the
3868** write lock if anything goes wrong.
3869*/
3870static int pager_open_journal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00003871 sqlite3_vfs *pVfs = pPager->pVfs;
3872 int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE);
3873
drhda47d772002-12-02 04:25:19 +00003874 int rc;
drhb7f91642004-10-31 02:22:47 +00003875 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +00003876 assert( pPager->state>=PAGER_RESERVED );
drhda47d772002-12-02 04:25:19 +00003877 assert( pPager->useJournal );
drhf5e7bb52008-02-18 14:47:33 +00003878 assert( pPager->pInJournal==0 );
danielk19773b8a05f2007-03-19 17:44:26 +00003879 sqlite3PagerPagecount(pPager);
drhed138fb2007-08-22 22:04:37 +00003880 pagerLeave(pPager);
drhf5e7bb52008-02-18 14:47:33 +00003881 pPager->pInJournal = sqlite3BitvecCreate(pPager->dbSize);
drhed138fb2007-08-22 22:04:37 +00003882 pagerEnter(pPager);
drhf5e7bb52008-02-18 14:47:33 +00003883 if( pPager->pInJournal==0 ){
drh9c105bb2004-10-02 20:38:28 +00003884 rc = SQLITE_NOMEM;
3885 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003886 }
danielk1977b4b47412007-08-17 15:53:36 +00003887
drhfdc40e92008-04-17 20:59:37 +00003888 if( pPager->journalOpen==0 ){
3889 if( pPager->tempFile ){
3890 flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
3891 }else{
3892 flags |= (SQLITE_OPEN_MAIN_JOURNAL);
drh600e46a2007-03-28 01:59:33 +00003893 }
drhfdc40e92008-04-17 20:59:37 +00003894#ifdef SQLITE_ENABLE_ATOMIC_WRITE
3895 rc = sqlite3JournalOpen(
3896 pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
3897 );
3898#else
3899 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
3900#endif
3901 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
3902 pPager->journalOff = 0;
3903 pPager->setMaster = 0;
3904 pPager->journalHdr = 0;
3905 if( rc!=SQLITE_OK ){
3906 if( rc==SQLITE_NOMEM ){
3907 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
3908 }
3909 goto failed_to_open_journal;
3910 }
drhda47d772002-12-02 04:25:19 +00003911 }
3912 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00003913 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00003914 pPager->needSync = 0;
3915 pPager->alwaysRollback = 0;
drh968af522003-02-11 14:55:40 +00003916 pPager->nRec = 0;
danielk1977efaaf572006-01-16 11:29:19 +00003917 if( pPager->errCode ){
3918 rc = pPager->errCode;
drhdd5b2fa2005-03-28 03:39:55 +00003919 goto failed_to_open_journal;
drh2e6d11b2003-04-25 15:37:57 +00003920 }
drhda47d772002-12-02 04:25:19 +00003921 pPager->origDbSize = pPager->dbSize;
drhae2b40c2004-06-09 19:03:54 +00003922
danielk197776572402004-06-25 02:38:54 +00003923 rc = writeJournalHdr(pPager);
3924
drhac69b052004-05-12 13:30:07 +00003925 if( pPager->stmtAutoopen && rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00003926 rc = sqlite3PagerStmtBegin(pPager);
drhda47d772002-12-02 04:25:19 +00003927 }
danielk1977ae72d982007-10-03 08:46:44 +00003928 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_NOMEM ){
drh80e35f42007-03-30 14:06:34 +00003929 rc = pager_end_transaction(pPager);
drhda47d772002-12-02 04:25:19 +00003930 if( rc==SQLITE_OK ){
3931 rc = SQLITE_FULL;
3932 }
3933 }
drh9c105bb2004-10-02 20:38:28 +00003934 return rc;
3935
3936failed_to_open_journal:
drhf5e7bb52008-02-18 14:47:33 +00003937 sqlite3BitvecDestroy(pPager->pInJournal);
3938 pPager->pInJournal = 0;
drh9c105bb2004-10-02 20:38:28 +00003939 return rc;
drhda47d772002-12-02 04:25:19 +00003940}
3941
3942/*
drh4b845d72002-03-05 12:41:19 +00003943** Acquire a write-lock on the database. The lock is removed when
3944** the any of the following happen:
3945**
drh80e35f42007-03-30 14:06:34 +00003946** * sqlite3PagerCommitPhaseTwo() is called.
danielk19773b8a05f2007-03-19 17:44:26 +00003947** * sqlite3PagerRollback() is called.
3948** * sqlite3PagerClose() is called.
3949** * sqlite3PagerUnref() is called to on every outstanding page.
drh4b845d72002-03-05 12:41:19 +00003950**
danielk197713adf8a2004-06-03 16:08:41 +00003951** The first parameter to this routine is a pointer to any open page of the
3952** database file. Nothing changes about the page - it is used merely to
3953** acquire a pointer to the Pager structure and as proof that there is
3954** already a read-lock on the database.
drh4b845d72002-03-05 12:41:19 +00003955**
danielk197713adf8a2004-06-03 16:08:41 +00003956** The second parameter indicates how much space in bytes to reserve for a
3957** master journal file-name at the start of the journal when it is created.
3958**
3959** A journal file is opened if this is not a temporary file. For temporary
3960** files, the opening of the journal file is deferred until there is an
3961** actual need to write to the journal.
drhda47d772002-12-02 04:25:19 +00003962**
drha6abd042004-06-09 17:37:22 +00003963** If the database is already reserved for writing, this routine is a no-op.
drh684917c2004-10-05 02:41:42 +00003964**
3965** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file
3966** immediately instead of waiting until we try to flush the cache. The
3967** exFlag is ignored if a transaction is already active.
drh4b845d72002-03-05 12:41:19 +00003968*/
danielk19773b8a05f2007-03-19 17:44:26 +00003969int sqlite3PagerBegin(DbPage *pPg, int exFlag){
drh4b845d72002-03-05 12:41:19 +00003970 Pager *pPager = pPg->pPager;
3971 int rc = SQLITE_OK;
drh86f8c192007-08-22 00:39:19 +00003972 pagerEnter(pPager);
drh4b845d72002-03-05 12:41:19 +00003973 assert( pPg->nRef>0 );
drha6abd042004-06-09 17:37:22 +00003974 assert( pPager->state!=PAGER_UNLOCK );
3975 if( pPager->state==PAGER_SHARED ){
drhf5e7bb52008-02-18 14:47:33 +00003976 assert( pPager->pInJournal==0 );
drhb7f91642004-10-31 02:22:47 +00003977 if( MEMDB ){
drha6abd042004-06-09 17:37:22 +00003978 pPager->state = PAGER_EXCLUSIVE;
drhac69b052004-05-12 13:30:07 +00003979 pPager->origDbSize = pPager->dbSize;
3980 }else{
drh054889e2005-11-30 03:20:31 +00003981 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
drh684917c2004-10-05 02:41:42 +00003982 if( rc==SQLITE_OK ){
3983 pPager->state = PAGER_RESERVED;
3984 if( exFlag ){
3985 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
3986 }
3987 }
danielk19779eed5052004-06-04 10:38:30 +00003988 if( rc!=SQLITE_OK ){
drh86f8c192007-08-22 00:39:19 +00003989 pagerLeave(pPager);
danielk19779eed5052004-06-04 10:38:30 +00003990 return rc;
drhac69b052004-05-12 13:30:07 +00003991 }
drha6abd042004-06-09 17:37:22 +00003992 pPager->dirtyCache = 0;
drh4f0c5872007-03-26 22:05:01 +00003993 PAGERTRACE2("TRANSACTION %d\n", PAGERID(pPager));
drhfdc40e92008-04-17 20:59:37 +00003994 if( pPager->useJournal && !pPager->tempFile
3995 && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
drhac69b052004-05-12 13:30:07 +00003996 rc = pager_open_journal(pPager);
3997 }
drh4b845d72002-03-05 12:41:19 +00003998 }
danielk1977334cdb62007-03-26 08:05:12 +00003999 }else if( pPager->journalOpen && pPager->journalOff==0 ){
4000 /* This happens when the pager was in exclusive-access mode last
4001 ** time a (read or write) transaction was successfully concluded
4002 ** by this connection. Instead of deleting the journal file it was
4003 ** kept open and truncated to 0 bytes.
4004 */
4005 assert( pPager->nRec==0 );
4006 assert( pPager->origDbSize==0 );
drhf5e7bb52008-02-18 14:47:33 +00004007 assert( pPager->pInJournal==0 );
danielk1977334cdb62007-03-26 08:05:12 +00004008 sqlite3PagerPagecount(pPager);
drhed138fb2007-08-22 22:04:37 +00004009 pagerLeave(pPager);
drhf5e7bb52008-02-18 14:47:33 +00004010 pPager->pInJournal = sqlite3BitvecCreate( pPager->dbSize );
drhed138fb2007-08-22 22:04:37 +00004011 pagerEnter(pPager);
drhf5e7bb52008-02-18 14:47:33 +00004012 if( !pPager->pInJournal ){
danielk1977334cdb62007-03-26 08:05:12 +00004013 rc = SQLITE_NOMEM;
4014 }else{
drh369339d2007-03-30 16:01:55 +00004015 pPager->origDbSize = pPager->dbSize;
danielk1977334cdb62007-03-26 08:05:12 +00004016 rc = writeJournalHdr(pPager);
4017 }
drh4b845d72002-03-05 12:41:19 +00004018 }
danielk1977334cdb62007-03-26 08:05:12 +00004019 assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK );
drh86f8c192007-08-22 00:39:19 +00004020 pagerLeave(pPager);
drh4b845d72002-03-05 12:41:19 +00004021 return rc;
4022}
4023
4024/*
drh605ad8f2006-05-03 23:34:05 +00004025** Make a page dirty. Set its dirty flag and add it to the dirty
4026** page list.
4027*/
4028static void makeDirty(PgHdr *pPg){
4029 if( pPg->dirty==0 ){
4030 Pager *pPager = pPg->pPager;
4031 pPg->dirty = 1;
4032 pPg->pDirty = pPager->pDirty;
4033 if( pPager->pDirty ){
4034 pPager->pDirty->pPrevDirty = pPg;
4035 }
4036 pPg->pPrevDirty = 0;
4037 pPager->pDirty = pPg;
4038 }
4039}
4040
4041/*
4042** Make a page clean. Clear its dirty bit and remove it from the
4043** dirty page list.
4044*/
4045static void makeClean(PgHdr *pPg){
4046 if( pPg->dirty ){
4047 pPg->dirty = 0;
4048 if( pPg->pDirty ){
drh153c62c2007-08-24 03:51:33 +00004049 assert( pPg->pDirty->pPrevDirty==pPg );
drh605ad8f2006-05-03 23:34:05 +00004050 pPg->pDirty->pPrevDirty = pPg->pPrevDirty;
4051 }
4052 if( pPg->pPrevDirty ){
drh153c62c2007-08-24 03:51:33 +00004053 assert( pPg->pPrevDirty->pDirty==pPg );
drh605ad8f2006-05-03 23:34:05 +00004054 pPg->pPrevDirty->pDirty = pPg->pDirty;
4055 }else{
drh153c62c2007-08-24 03:51:33 +00004056 assert( pPg->pPager->pDirty==pPg );
drh605ad8f2006-05-03 23:34:05 +00004057 pPg->pPager->pDirty = pPg->pDirty;
4058 }
4059 }
4060}
4061
4062
4063/*
drhed7c8552001-04-11 14:29:21 +00004064** Mark a data page as writeable. The page is written into the journal
4065** if it is not there already. This routine must be called before making
4066** changes to a page.
4067**
4068** The first time this routine is called, the pager creates a new
drha6abd042004-06-09 17:37:22 +00004069** journal and acquires a RESERVED lock on the database. If the RESERVED
drhed7c8552001-04-11 14:29:21 +00004070** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00004071** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00004072** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00004073**
4074** If the journal file could not be written because the disk is full,
4075** then this routine returns SQLITE_FULL and does an immediate rollback.
4076** All subsequent write attempts also return SQLITE_FULL until there
danielk19773b8a05f2007-03-19 17:44:26 +00004077** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to
drhd9b02572001-04-15 00:37:09 +00004078** reset.
drhed7c8552001-04-11 14:29:21 +00004079*/
danielk19773b8a05f2007-03-19 17:44:26 +00004080static int pager_write(PgHdr *pPg){
4081 void *pData = PGHDR_TO_DATA(pPg);
drh69688d52001-04-14 16:38:23 +00004082 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00004083 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00004084
drh6446c4d2001-12-15 14:22:18 +00004085 /* Check for errors
4086 */
danielk1977efaaf572006-01-16 11:29:19 +00004087 if( pPager->errCode ){
4088 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004089 }
drh5e00f6c2001-09-13 13:46:56 +00004090 if( pPager->readOnly ){
4091 return SQLITE_PERM;
4092 }
drh6446c4d2001-12-15 14:22:18 +00004093
danielk197776572402004-06-25 02:38:54 +00004094 assert( !pPager->setMaster );
4095
danielk19773c407372005-02-15 02:54:14 +00004096 CHECK_PAGE(pPg);
4097
drh538f5702007-04-13 02:14:30 +00004098 /* If this page was previously acquired with noContent==1, that means
4099 ** we didn't really read in the content of the page. This can happen
4100 ** (for example) when the page is being moved to the freelist. But
4101 ** now we are (perhaps) moving the page off of the freelist for
4102 ** reuse and we need to know its original content so that content
4103 ** can be stored in the rollback journal. So do the read at this
4104 ** time.
4105 */
drhd33d5a82007-04-26 12:11:28 +00004106 rc = pager_get_content(pPg);
4107 if( rc ){
4108 return rc;
drh538f5702007-04-13 02:14:30 +00004109 }
4110
drh6446c4d2001-12-15 14:22:18 +00004111 /* Mark the page as dirty. If the page has already been written
4112 ** to the journal then we can return right away.
4113 */
drh605ad8f2006-05-03 23:34:05 +00004114 makeDirty(pPg);
danielk1977f35843b2007-04-07 15:03:17 +00004115 if( pPg->inJournal && (pageInStatement(pPg) || pPager->stmtInUse==0) ){
drha6abd042004-06-09 17:37:22 +00004116 pPager->dirtyCache = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00004117 }else{
drh6446c4d2001-12-15 14:22:18 +00004118
danielk1977a0bf2652004-11-04 14:30:04 +00004119 /* If we get this far, it means that the page needs to be
4120 ** written to the transaction journal or the ckeckpoint journal
4121 ** or both.
4122 **
4123 ** First check to see that the transaction journal exists and
4124 ** create it if it does not.
4125 */
4126 assert( pPager->state!=PAGER_UNLOCK );
danielk19773b8a05f2007-03-19 17:44:26 +00004127 rc = sqlite3PagerBegin(pPg, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00004128 if( rc!=SQLITE_OK ){
4129 return rc;
4130 }
4131 assert( pPager->state>=PAGER_RESERVED );
drhfdc40e92008-04-17 20:59:37 +00004132 if( !pPager->journalOpen && pPager->useJournal
4133 && pPager->journalMode!=PAGER_JOURNALMODE_OFF ){
danielk1977a0bf2652004-11-04 14:30:04 +00004134 rc = pager_open_journal(pPager);
4135 if( rc!=SQLITE_OK ) return rc;
4136 }
danielk1977a0bf2652004-11-04 14:30:04 +00004137 pPager->dirtyCache = 1;
4138
4139 /* The transaction journal now exists and we have a RESERVED or an
4140 ** EXCLUSIVE lock on the main database file. Write the current page to
4141 ** the transaction journal if it is not there already.
4142 */
drhfdc40e92008-04-17 20:59:37 +00004143 if( !pPg->inJournal && (pPager->journalOpen || MEMDB) ){
danielk1977a0bf2652004-11-04 14:30:04 +00004144 if( (int)pPg->pgno <= pPager->origDbSize ){
danielk1977a0bf2652004-11-04 14:30:04 +00004145 if( MEMDB ){
4146 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
drh4f0c5872007-03-26 22:05:01 +00004147 PAGERTRACE3("JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00004148 assert( pHist->pOrig==0 );
drh17435752007-08-16 04:30:38 +00004149 pHist->pOrig = sqlite3_malloc( pPager->pageSize );
danielk1977662278e2007-11-05 15:30:12 +00004150 if( !pHist->pOrig ){
4151 return SQLITE_NOMEM;
danielk1977a0bf2652004-11-04 14:30:04 +00004152 }
danielk1977662278e2007-11-05 15:30:12 +00004153 memcpy(pHist->pOrig, PGHDR_TO_DATA(pPg), pPager->pageSize);
danielk1977a0bf2652004-11-04 14:30:04 +00004154 }else{
drhbf4bca52007-09-06 22:19:14 +00004155 u32 cksum;
4156 char *pData2;
danielk1977dd97a492007-08-22 18:54:32 +00004157
drh267cb322005-09-16 17:16:52 +00004158 /* We should never write to the journal file the page that
4159 ** contains the database locks. The following assert verifies
4160 ** that we do not. */
4161 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
drhc001c582006-03-06 18:23:16 +00004162 pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
drh37527852006-03-16 16:19:56 +00004163 cksum = pager_cksum(pPager, (u8*)pData2);
drhbf4bca52007-09-06 22:19:14 +00004164 rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno);
4165 if( rc==SQLITE_OK ){
4166 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize,
4167 pPager->journalOff + 4);
4168 pPager->journalOff += pPager->pageSize+4;
4169 }
4170 if( rc==SQLITE_OK ){
4171 rc = write32bits(pPager->jfd, pPager->journalOff, cksum);
4172 pPager->journalOff += 4;
4173 }
4174 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
danielk1977667a6c92007-09-10 06:12:04 +00004175 pPager->journalOff, pPager->pageSize));
drh538f5702007-04-13 02:14:30 +00004176 PAGER_INCR(sqlite3_pager_writej_count);
drh477731b2007-06-16 03:06:27 +00004177 PAGERTRACE5("JOURNAL %d page %d needSync=%d hash(%08x)\n",
4178 PAGERID(pPager), pPg->pgno, pPg->needSync, pager_pagehash(pPg));
danielk197707cb5602006-01-20 10:55:05 +00004179
drhfd131da2007-08-07 17:13:03 +00004180 /* An error has occured writing to the journal file. The
danielk197707cb5602006-01-20 10:55:05 +00004181 ** transaction will be rolled back by the layer above.
4182 */
danielk1977a0bf2652004-11-04 14:30:04 +00004183 if( rc!=SQLITE_OK ){
danielk1977a0bf2652004-11-04 14:30:04 +00004184 return rc;
4185 }
danielk197707cb5602006-01-20 10:55:05 +00004186
danielk1977a0bf2652004-11-04 14:30:04 +00004187 pPager->nRec++;
drhf5e7bb52008-02-18 14:47:33 +00004188 assert( pPager->pInJournal!=0 );
4189 sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00004190 pPg->needSync = !pPager->noSync;
4191 if( pPager->stmtInUse ){
drhf5e7bb52008-02-18 14:47:33 +00004192 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00004193 }
drhac69b052004-05-12 13:30:07 +00004194 }
danielk197713adf8a2004-06-03 16:08:41 +00004195 }else{
danielk1977a0bf2652004-11-04 14:30:04 +00004196 pPg->needSync = !pPager->journalStarted && !pPager->noSync;
drh4f0c5872007-03-26 22:05:01 +00004197 PAGERTRACE4("APPEND %d page %d needSync=%d\n",
danielk1977ef73ee92004-11-06 12:26:07 +00004198 PAGERID(pPager), pPg->pgno, pPg->needSync);
danielk1977a0bf2652004-11-04 14:30:04 +00004199 }
4200 if( pPg->needSync ){
4201 pPager->needSync = 1;
4202 }
4203 pPg->inJournal = 1;
4204 }
4205
4206 /* If the statement journal is open and the page is not in it,
4207 ** then write the current page to the statement journal. Note that
4208 ** the statement journal format differs from the standard journal format
4209 ** in that it omits the checksums and the header.
4210 */
danielk1977f35843b2007-04-07 15:03:17 +00004211 if( pPager->stmtInUse
4212 && !pageInStatement(pPg)
4213 && (int)pPg->pgno<=pPager->stmtSize
4214 ){
danielk1977a0bf2652004-11-04 14:30:04 +00004215 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
4216 if( MEMDB ){
4217 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4218 assert( pHist->pStmt==0 );
drh17435752007-08-16 04:30:38 +00004219 pHist->pStmt = sqlite3_malloc( pPager->pageSize );
danielk1977a0bf2652004-11-04 14:30:04 +00004220 if( pHist->pStmt ){
4221 memcpy(pHist->pStmt, PGHDR_TO_DATA(pPg), pPager->pageSize);
4222 }
drh4f0c5872007-03-26 22:05:01 +00004223 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk1977f35843b2007-04-07 15:03:17 +00004224 page_add_to_stmt_list(pPg);
danielk1977a0bf2652004-11-04 14:30:04 +00004225 }else{
danielk197762079062007-08-15 17:08:46 +00004226 i64 offset = pPager->stmtNRec*(4+pPager->pageSize);
drhbf4bca52007-09-06 22:19:14 +00004227 char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
4228 rc = write32bits(pPager->stfd, offset, pPg->pgno);
4229 if( rc==SQLITE_OK ){
4230 rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize, offset+4);
4231 }
drh4f0c5872007-03-26 22:05:01 +00004232 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
drhac69b052004-05-12 13:30:07 +00004233 if( rc!=SQLITE_OK ){
drhac69b052004-05-12 13:30:07 +00004234 return rc;
4235 }
danielk1977a0bf2652004-11-04 14:30:04 +00004236 pPager->stmtNRec++;
drhf5e7bb52008-02-18 14:47:33 +00004237 assert( pPager->pInStmt!=0 );
4238 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
drhdb48ee02003-01-16 13:42:43 +00004239 }
drhd9b02572001-04-15 00:37:09 +00004240 }
drhfa86c412002-02-02 15:01:15 +00004241 }
4242
4243 /* Update the database size and return.
4244 */
drh1aa2d8b2007-01-03 15:34:29 +00004245 assert( pPager->state>=PAGER_SHARED );
drh1ab43002002-01-14 09:28:19 +00004246 if( pPager->dbSize<(int)pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00004247 pPager->dbSize = pPg->pgno;
drhb7f91642004-10-31 02:22:47 +00004248 if( !MEMDB && pPager->dbSize==PENDING_BYTE/pPager->pageSize ){
drh1f595712004-06-15 01:40:29 +00004249 pPager->dbSize++;
4250 }
drh306dc212001-05-21 13:45:10 +00004251 }
drh69688d52001-04-14 16:38:23 +00004252 return rc;
drhed7c8552001-04-11 14:29:21 +00004253}
4254
4255/*
danielk19774099f6e2007-03-19 11:25:20 +00004256** This function is used to mark a data-page as writable. It uses
4257** pager_write() to open a journal file (if it is not already open)
4258** and write the page *pData to the journal.
4259**
4260** The difference between this function and pager_write() is that this
4261** function also deals with the special case where 2 or more pages
4262** fit on a single disk sector. In this case all co-resident pages
4263** must have been written to the journal file before returning.
4264*/
danielk19773b8a05f2007-03-19 17:44:26 +00004265int sqlite3PagerWrite(DbPage *pDbPage){
danielk19774099f6e2007-03-19 11:25:20 +00004266 int rc = SQLITE_OK;
4267
danielk19773b8a05f2007-03-19 17:44:26 +00004268 PgHdr *pPg = pDbPage;
danielk19774099f6e2007-03-19 11:25:20 +00004269 Pager *pPager = pPg->pPager;
4270 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
4271
drh86f8c192007-08-22 00:39:19 +00004272 pagerEnter(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00004273 if( !MEMDB && nPagePerSector>1 ){
4274 Pgno nPageCount; /* Total number of pages in database file */
4275 Pgno pg1; /* First page of the sector pPg is located on. */
4276 int nPage; /* Number of pages starting at pg1 to journal */
4277 int ii;
danielk1977dd97a492007-08-22 18:54:32 +00004278 int needSync = 0;
danielk19774099f6e2007-03-19 11:25:20 +00004279
4280 /* Set the doNotSync flag to 1. This is because we cannot allow a journal
4281 ** header to be written between the pages journaled by this function.
4282 */
4283 assert( pPager->doNotSync==0 );
4284 pPager->doNotSync = 1;
4285
4286 /* This trick assumes that both the page-size and sector-size are
4287 ** an integer power of 2. It sets variable pg1 to the identifier
4288 ** of the first page of the sector pPg is located on.
4289 */
4290 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
4291
danielk19773b8a05f2007-03-19 17:44:26 +00004292 nPageCount = sqlite3PagerPagecount(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00004293 if( pPg->pgno>nPageCount ){
4294 nPage = (pPg->pgno - pg1)+1;
4295 }else if( (pg1+nPagePerSector-1)>nPageCount ){
4296 nPage = nPageCount+1-pg1;
4297 }else{
4298 nPage = nPagePerSector;
4299 }
4300 assert(nPage>0);
4301 assert(pg1<=pPg->pgno);
4302 assert((pg1+nPage)>pPg->pgno);
4303
4304 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
4305 Pgno pg = pg1+ii;
danielk1977dd97a492007-08-22 18:54:32 +00004306 PgHdr *pPage;
drhf5e7bb52008-02-18 14:47:33 +00004307 if( pg==pPg->pgno || !sqlite3BitvecTest(pPager->pInJournal, pg) ){
danielk19774099f6e2007-03-19 11:25:20 +00004308 if( pg!=PAGER_MJ_PGNO(pPager) ){
danielk19773b8a05f2007-03-19 17:44:26 +00004309 rc = sqlite3PagerGet(pPager, pg, &pPage);
danielk19774099f6e2007-03-19 11:25:20 +00004310 if( rc==SQLITE_OK ){
4311 rc = pager_write(pPage);
danielk1977dd97a492007-08-22 18:54:32 +00004312 if( pPage->needSync ){
4313 needSync = 1;
4314 }
danielk19773b8a05f2007-03-19 17:44:26 +00004315 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00004316 }
4317 }
drhc81945e2008-03-10 14:12:53 +00004318 }else if( (pPage = pager_lookup(pPager, pg))!=0 ){
danielk1977dd97a492007-08-22 18:54:32 +00004319 if( pPage->needSync ){
4320 needSync = 1;
4321 }
danielk19774099f6e2007-03-19 11:25:20 +00004322 }
4323 }
4324
danielk1977dd97a492007-08-22 18:54:32 +00004325 /* If the PgHdr.needSync flag is set for any of the nPage pages
4326 ** starting at pg1, then it needs to be set for all of them. Because
4327 ** writing to any of these nPage pages may damage the others, the
4328 ** journal file must contain sync()ed copies of all of them
4329 ** before any of them can be written out to the database file.
4330 */
4331 if( needSync ){
4332 for(ii=0; ii<nPage && needSync; ii++){
4333 PgHdr *pPage = pager_lookup(pPager, pg1+ii);
4334 if( pPage ) pPage->needSync = 1;
4335 }
4336 assert(pPager->needSync);
4337 }
4338
danielk19774099f6e2007-03-19 11:25:20 +00004339 assert( pPager->doNotSync==1 );
4340 pPager->doNotSync = 0;
4341 }else{
danielk19773b8a05f2007-03-19 17:44:26 +00004342 rc = pager_write(pDbPage);
danielk19774099f6e2007-03-19 11:25:20 +00004343 }
drh86f8c192007-08-22 00:39:19 +00004344 pagerLeave(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00004345 return rc;
4346}
4347
4348/*
drhaacc5432002-01-06 17:07:40 +00004349** Return TRUE if the page given in the argument was previously passed
danielk19773b8a05f2007-03-19 17:44:26 +00004350** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
drh6019e162001-07-02 17:51:45 +00004351** to change the content of the page.
4352*/
danielk19777d3a6662006-01-23 16:21:05 +00004353#ifndef NDEBUG
danielk19773b8a05f2007-03-19 17:44:26 +00004354int sqlite3PagerIswriteable(DbPage *pPg){
drh6019e162001-07-02 17:51:45 +00004355 return pPg->dirty;
4356}
danielk19777d3a6662006-01-23 16:21:05 +00004357#endif
drh6019e162001-07-02 17:51:45 +00004358
danielk1977b84f96f2005-01-20 11:32:23 +00004359#ifndef SQLITE_OMIT_VACUUM
drh6019e162001-07-02 17:51:45 +00004360/*
drh001bbcb2003-03-19 03:14:00 +00004361** Replace the content of a single page with the information in the third
4362** argument.
4363*/
danielk19773b8a05f2007-03-19 17:44:26 +00004364int sqlite3PagerOverwrite(Pager *pPager, Pgno pgno, void *pData){
4365 PgHdr *pPg;
drh001bbcb2003-03-19 03:14:00 +00004366 int rc;
4367
drh86f8c192007-08-22 00:39:19 +00004368 pagerEnter(pPager);
danielk19773b8a05f2007-03-19 17:44:26 +00004369 rc = sqlite3PagerGet(pPager, pgno, &pPg);
drh001bbcb2003-03-19 03:14:00 +00004370 if( rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00004371 rc = sqlite3PagerWrite(pPg);
drh001bbcb2003-03-19 03:14:00 +00004372 if( rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00004373 memcpy(sqlite3PagerGetData(pPg), pData, pPager->pageSize);
drh001bbcb2003-03-19 03:14:00 +00004374 }
danielk19773b8a05f2007-03-19 17:44:26 +00004375 sqlite3PagerUnref(pPg);
drh001bbcb2003-03-19 03:14:00 +00004376 }
drh86f8c192007-08-22 00:39:19 +00004377 pagerLeave(pPager);
drh001bbcb2003-03-19 03:14:00 +00004378 return rc;
4379}
danielk1977b84f96f2005-01-20 11:32:23 +00004380#endif
drh001bbcb2003-03-19 03:14:00 +00004381
4382/*
drh30e58752002-03-02 20:41:57 +00004383** A call to this routine tells the pager that it is not necessary to
drh538f5702007-04-13 02:14:30 +00004384** write the information on page pPg back to the disk, even though
drh30e58752002-03-02 20:41:57 +00004385** that page might be marked as dirty.
4386**
4387** The overlying software layer calls this routine when all of the data
4388** on the given page is unused. The pager marks the page as clean so
4389** that it does not get written to disk.
4390**
4391** Tests show that this optimization, together with the
danielk19773b8a05f2007-03-19 17:44:26 +00004392** sqlite3PagerDontRollback() below, more than double the speed
drh30e58752002-03-02 20:41:57 +00004393** of large INSERT operations and quadruple the speed of large DELETEs.
drh8e298f92002-07-06 16:28:47 +00004394**
4395** When this routine is called, set the alwaysRollback flag to true.
danielk19773b8a05f2007-03-19 17:44:26 +00004396** Subsequent calls to sqlite3PagerDontRollback() for the same page
drh8e298f92002-07-06 16:28:47 +00004397** will thereafter be ignored. This is necessary to avoid a problem
4398** where a page with data is added to the freelist during one part of
4399** a transaction then removed from the freelist during a later part
4400** of the same transaction and reused for some other purpose. When it
4401** is first added to the freelist, this routine is called. When reused,
drh80e35f42007-03-30 14:06:34 +00004402** the sqlite3PagerDontRollback() routine is called. But because the
4403** page contains critical data, we still need to be sure it gets
4404** rolled back in spite of the sqlite3PagerDontRollback() call.
drh30e58752002-03-02 20:41:57 +00004405*/
drh538f5702007-04-13 02:14:30 +00004406void sqlite3PagerDontWrite(DbPage *pDbPage){
4407 PgHdr *pPg = pDbPage;
4408 Pager *pPager = pPg->pPager;
drh8e298f92002-07-06 16:28:47 +00004409
drhb7f91642004-10-31 02:22:47 +00004410 if( MEMDB ) return;
drh86f8c192007-08-22 00:39:19 +00004411 pagerEnter(pPager);
drh8e298f92002-07-06 16:28:47 +00004412 pPg->alwaysRollback = 1;
drh43617e92006-03-06 20:55:46 +00004413 if( pPg->dirty && !pPager->stmtInUse ){
drh1aa2d8b2007-01-03 15:34:29 +00004414 assert( pPager->state>=PAGER_SHARED );
drh8124a302002-06-25 14:43:57 +00004415 if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
4416 /* If this pages is the last page in the file and the file has grown
4417 ** during the current transaction, then do NOT mark the page as clean.
4418 ** When the database file grows, we must make sure that the last page
4419 ** gets written at least once so that the disk file will be the correct
4420 ** size. If you do not write this page and the size of the file
4421 ** on the disk ends up being too small, that can lead to database
4422 ** corruption during the next transaction.
4423 */
4424 }else{
drh538f5702007-04-13 02:14:30 +00004425 PAGERTRACE3("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager));
4426 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
drh605ad8f2006-05-03 23:34:05 +00004427 makeClean(pPg);
danielk19773c407372005-02-15 02:54:14 +00004428#ifdef SQLITE_CHECK_PAGES
4429 pPg->pageHash = pager_pagehash(pPg);
4430#endif
drh8124a302002-06-25 14:43:57 +00004431 }
drh30e58752002-03-02 20:41:57 +00004432 }
drh86f8c192007-08-22 00:39:19 +00004433 pagerLeave(pPager);
drh30e58752002-03-02 20:41:57 +00004434}
4435
4436/*
4437** A call to this routine tells the pager that if a rollback occurs,
4438** it is not necessary to restore the data on the given page. This
4439** means that the pager does not have to record the given page in the
4440** rollback journal.
drh538f5702007-04-13 02:14:30 +00004441**
4442** If we have not yet actually read the content of this page (if
4443** the PgHdr.needRead flag is set) then this routine acts as a promise
4444** that we will never need to read the page content in the future.
4445** so the needRead flag can be cleared at this point.
drh30e58752002-03-02 20:41:57 +00004446*/
danielk19773b8a05f2007-03-19 17:44:26 +00004447void sqlite3PagerDontRollback(DbPage *pPg){
drh30e58752002-03-02 20:41:57 +00004448 Pager *pPager = pPg->pPager;
4449
drh86f8c192007-08-22 00:39:19 +00004450 pagerEnter(pPager);
drhd3627af2006-12-18 18:34:51 +00004451 assert( pPager->state>=PAGER_RESERVED );
danielk1977a55e9352008-01-21 13:04:34 +00004452
4453 /* If the journal file is not open, or DontWrite() has been called on
4454 ** this page (DontWrite() sets the alwaysRollback flag), then this
4455 ** function is a no-op.
4456 */
4457 if( pPager->journalOpen==0 || pPg->alwaysRollback || pPager->alwaysRollback ){
danielk197787c29a92008-01-18 11:33:16 +00004458 pagerLeave(pPager);
4459 return;
4460 }
danielk1977a55e9352008-01-21 13:04:34 +00004461 assert( !MEMDB ); /* For a memdb, pPager->journalOpen is always 0 */
4462
drhc5d0bd92008-04-14 01:00:57 +00004463#ifdef SQLITE_SECURE_DELETE
4464 if( pPg->inJournal || (int)pPg->pgno > pPager->origDbSize ){
4465 return;
4466 }
4467#endif
4468
4469 /* If SECURE_DELETE is disabled, then there is no way that this
4470 ** routine can be called on a page for which sqlite3PagerDontWrite()
4471 ** has not been previously called during the same transaction.
4472 ** And if DontWrite() has previously been called, the following
4473 ** conditions must be met.
danielk1977a55e9352008-01-21 13:04:34 +00004474 */
4475 assert( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize );
4476
drhf5e7bb52008-02-18 14:47:33 +00004477 assert( pPager->pInJournal!=0 );
4478 sqlite3BitvecSet(pPager->pInJournal, pPg->pgno);
danielk1977a55e9352008-01-21 13:04:34 +00004479 pPg->inJournal = 1;
4480 pPg->needRead = 0;
4481 if( pPager->stmtInUse ){
drhc5d0bd92008-04-14 01:00:57 +00004482 assert( pPager->stmtSize >= pPager->origDbSize );
drhf5e7bb52008-02-18 14:47:33 +00004483 sqlite3BitvecSet(pPager->pInStmt, pPg->pgno);
drh30e58752002-03-02 20:41:57 +00004484 }
danielk1977a55e9352008-01-21 13:04:34 +00004485 PAGERTRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager));
4486 IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno))
drh86f8c192007-08-22 00:39:19 +00004487 pagerLeave(pPager);
drh30e58752002-03-02 20:41:57 +00004488}
4489
drhac69b052004-05-12 13:30:07 +00004490
drh30e58752002-03-02 20:41:57 +00004491/*
drh80e35f42007-03-30 14:06:34 +00004492** This routine is called to increment the database file change-counter,
4493** stored at byte 24 of the pager file.
4494*/
danielk1977c7b60172007-08-22 11:22:03 +00004495static int pager_incr_changecounter(Pager *pPager, int isDirect){
drh80e35f42007-03-30 14:06:34 +00004496 PgHdr *pPgHdr;
4497 u32 change_counter;
danielk1977c7b60172007-08-22 11:22:03 +00004498 int rc = SQLITE_OK;
drh80e35f42007-03-30 14:06:34 +00004499
4500 if( !pPager->changeCountDone ){
4501 /* Open page 1 of the file for writing. */
4502 rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
4503 if( rc!=SQLITE_OK ) return rc;
danielk1977c7b60172007-08-22 11:22:03 +00004504
4505 if( !isDirect ){
4506 rc = sqlite3PagerWrite(pPgHdr);
danielk1977ae72d982007-10-03 08:46:44 +00004507 if( rc!=SQLITE_OK ){
4508 sqlite3PagerUnref(pPgHdr);
4509 return rc;
4510 }
danielk1977c7b60172007-08-22 11:22:03 +00004511 }
4512
drh80e35f42007-03-30 14:06:34 +00004513 /* Increment the value just read and write it back to byte 24. */
drhb1003912007-07-20 00:33:36 +00004514 change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
drh80e35f42007-03-30 14:06:34 +00004515 change_counter++;
4516 put32bits(((char*)PGHDR_TO_DATA(pPgHdr))+24, change_counter);
danielk1977c7b60172007-08-22 11:22:03 +00004517
4518 if( isDirect && pPager->fd->pMethods ){
4519 const void *zBuf = PGHDR_TO_DATA(pPgHdr);
4520 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
4521 }
4522
drh80e35f42007-03-30 14:06:34 +00004523 /* Release the page reference. */
4524 sqlite3PagerUnref(pPgHdr);
4525 pPager->changeCountDone = 1;
4526 }
danielk1977c7b60172007-08-22 11:22:03 +00004527 return rc;
drh80e35f42007-03-30 14:06:34 +00004528}
4529
4530/*
danielk1977f653d782008-03-20 11:04:21 +00004531** Sync the pager file to disk.
4532*/
4533int sqlite3PagerSync(Pager *pPager){
4534 int rc;
4535 pagerEnter(pPager);
4536 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
4537 pagerLeave(pPager);
4538 return rc;
4539}
4540
4541/*
drh80e35f42007-03-30 14:06:34 +00004542** Sync the database file for the pager pPager. zMaster points to the name
4543** of a master journal file that should be written into the individual
4544** journal file. zMaster may be NULL, which is interpreted as no master
4545** journal (a single database transaction).
4546**
4547** This routine ensures that the journal is synced, all dirty pages written
4548** to the database file and the database file synced. The only thing that
4549** remains to commit the transaction is to delete the journal file (or
4550** master journal file if specified).
4551**
4552** Note that if zMaster==NULL, this does not overwrite a previous value
4553** passed to an sqlite3PagerCommitPhaseOne() call.
4554**
4555** If parameter nTrunc is non-zero, then the pager file is truncated to
4556** nTrunc pages (this is used by auto-vacuum databases).
danielk1977f653d782008-03-20 11:04:21 +00004557**
4558** If the final parameter - noSync - is true, then the database file itself
4559** is not synced. The caller must call sqlite3PagerSync() directly to
4560** sync the database file before calling CommitPhaseTwo() to delete the
4561** journal file in this case.
drh80e35f42007-03-30 14:06:34 +00004562*/
danielk1977f653d782008-03-20 11:04:21 +00004563int sqlite3PagerCommitPhaseOne(
4564 Pager *pPager,
4565 const char *zMaster,
4566 Pgno nTrunc,
4567 int noSync
4568){
drh80e35f42007-03-30 14:06:34 +00004569 int rc = SQLITE_OK;
4570
4571 PAGERTRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n",
4572 pPager->zFilename, zMaster, nTrunc);
drh86f8c192007-08-22 00:39:19 +00004573 pagerEnter(pPager);
drh80e35f42007-03-30 14:06:34 +00004574
4575 /* If this is an in-memory db, or no pages have been written to, or this
4576 ** function has already been called, it is a no-op.
4577 */
4578 if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){
4579 PgHdr *pPg;
drh80e35f42007-03-30 14:06:34 +00004580
danielk1977c7b60172007-08-22 11:22:03 +00004581#ifdef SQLITE_ENABLE_ATOMIC_WRITE
4582 /* The atomic-write optimization can be used if all of the
4583 ** following are true:
4584 **
4585 ** + The file-system supports the atomic-write property for
4586 ** blocks of size page-size, and
4587 ** + This commit is not part of a multi-file transaction, and
4588 ** + Exactly one page has been modified and store in the journal file.
4589 **
4590 ** If the optimization can be used, then the journal file will never
4591 ** be created for this transaction.
4592 */
danielk1977f55b8992007-08-24 08:15:53 +00004593 int useAtomicWrite = (
4594 !zMaster &&
4595 pPager->journalOff==jrnlBufferSize(pPager) &&
4596 nTrunc==0 &&
4597 (0==pPager->pDirty || 0==pPager->pDirty->pDirty)
4598 );
4599 if( useAtomicWrite ){
danielk1977c7b60172007-08-22 11:22:03 +00004600 /* Update the nRec field in the journal file. */
4601 int offset = pPager->journalHdr + sizeof(aJournalMagic);
4602 assert(pPager->nRec==1);
4603 rc = write32bits(pPager->jfd, offset, pPager->nRec);
4604
4605 /* Update the db file change counter. The following call will modify
4606 ** the in-memory representation of page 1 to include the updated
4607 ** change counter and then write page 1 directly to the database
4608 ** file. Because of the atomic-write property of the host file-system,
4609 ** this is safe.
4610 */
danielk1977ae72d982007-10-03 08:46:44 +00004611 if( rc==SQLITE_OK ){
4612 rc = pager_incr_changecounter(pPager, 1);
4613 }
danielk1977f55b8992007-08-24 08:15:53 +00004614 }else{
4615 rc = sqlite3JournalCreate(pPager->jfd);
danielk1977f55b8992007-08-24 08:15:53 +00004616 }
4617
danielk1977ae72d982007-10-03 08:46:44 +00004618 if( !useAtomicWrite && rc==SQLITE_OK )
danielk1977c7b60172007-08-22 11:22:03 +00004619#endif
4620
drh80e35f42007-03-30 14:06:34 +00004621 /* If a master journal file name has already been written to the
4622 ** journal file, then no sync is required. This happens when it is
4623 ** written, then the process fails to upgrade from a RESERVED to an
4624 ** EXCLUSIVE lock. The next time the process tries to commit the
4625 ** transaction the m-j name will have already been written.
4626 */
4627 if( !pPager->setMaster ){
danielk1977c7b60172007-08-22 11:22:03 +00004628 rc = pager_incr_changecounter(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00004629 if( rc!=SQLITE_OK ) goto sync_exit;
4630#ifndef SQLITE_OMIT_AUTOVACUUM
4631 if( nTrunc!=0 ){
4632 /* If this transaction has made the database smaller, then all pages
4633 ** being discarded by the truncation must be written to the journal
4634 ** file.
4635 */
4636 Pgno i;
4637 int iSkip = PAGER_MJ_PGNO(pPager);
4638 for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){
drhf5e7bb52008-02-18 14:47:33 +00004639 if( !sqlite3BitvecTest(pPager->pInJournal, i) && i!=iSkip ){
drh80e35f42007-03-30 14:06:34 +00004640 rc = sqlite3PagerGet(pPager, i, &pPg);
4641 if( rc!=SQLITE_OK ) goto sync_exit;
4642 rc = sqlite3PagerWrite(pPg);
4643 sqlite3PagerUnref(pPg);
4644 if( rc!=SQLITE_OK ) goto sync_exit;
4645 }
4646 }
4647 }
4648#endif
4649 rc = writeMasterJournal(pPager, zMaster);
4650 if( rc!=SQLITE_OK ) goto sync_exit;
4651 rc = syncJournal(pPager);
drh80e35f42007-03-30 14:06:34 +00004652 }
danielk1977c7b60172007-08-22 11:22:03 +00004653 if( rc!=SQLITE_OK ) goto sync_exit;
drh80e35f42007-03-30 14:06:34 +00004654
4655#ifndef SQLITE_OMIT_AUTOVACUUM
4656 if( nTrunc!=0 ){
4657 rc = sqlite3PagerTruncate(pPager, nTrunc);
4658 if( rc!=SQLITE_OK ) goto sync_exit;
4659 }
4660#endif
4661
4662 /* Write all dirty pages to the database file */
4663 pPg = pager_get_all_dirty_pages(pPager);
4664 rc = pager_write_pagelist(pPg);
drh153c62c2007-08-24 03:51:33 +00004665 if( rc!=SQLITE_OK ){
drh04c3a462008-02-26 16:16:45 +00004666 assert( rc!=SQLITE_IOERR_BLOCKED );
4667 /* The error might have left the dirty list all fouled up here,
4668 ** but that does not matter because if the if the dirty list did
4669 ** get corrupted, then the transaction will roll back and
4670 ** discard the dirty list. There is an assert in
4671 ** pager_get_all_dirty_pages() that verifies that no attempt
4672 ** is made to use an invalid dirty list.
4673 */
drh153c62c2007-08-24 03:51:33 +00004674 goto sync_exit;
4675 }
drh3cdd7d32007-03-30 14:46:01 +00004676 pPager->pDirty = 0;
drh80e35f42007-03-30 14:06:34 +00004677
4678 /* Sync the database file. */
danielk1977f653d782008-03-20 11:04:21 +00004679 if( !pPager->noSync && !noSync ){
danielk1977f036aef2007-08-20 05:36:51 +00004680 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
drh80e35f42007-03-30 14:06:34 +00004681 }
4682 IOTRACE(("DBSYNC %p\n", pPager))
4683
4684 pPager->state = PAGER_SYNCED;
4685 }else if( MEMDB && nTrunc!=0 ){
4686 rc = sqlite3PagerTruncate(pPager, nTrunc);
4687 }
4688
4689sync_exit:
danielk1977e965ac72007-06-13 15:22:28 +00004690 if( rc==SQLITE_IOERR_BLOCKED ){
4691 /* pager_incr_changecounter() may attempt to obtain an exclusive
4692 * lock to spill the cache and return IOERR_BLOCKED. But since
drh85b623f2007-12-13 21:54:09 +00004693 * there is no chance the cache is inconsistent, it is
danielk1977e965ac72007-06-13 15:22:28 +00004694 * better to return SQLITE_BUSY.
4695 */
4696 rc = SQLITE_BUSY;
4697 }
drh86f8c192007-08-22 00:39:19 +00004698 pagerLeave(pPager);
drh80e35f42007-03-30 14:06:34 +00004699 return rc;
4700}
4701
4702
4703/*
drhed7c8552001-04-11 14:29:21 +00004704** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00004705**
4706** If the commit fails for any reason, a rollback attempt is made
4707** and an error code is returned. If the commit worked, SQLITE_OK
4708** is returned.
drhed7c8552001-04-11 14:29:21 +00004709*/
drh80e35f42007-03-30 14:06:34 +00004710int sqlite3PagerCommitPhaseTwo(Pager *pPager){
drha1b351a2001-09-14 16:42:12 +00004711 int rc;
drhed7c8552001-04-11 14:29:21 +00004712 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +00004713
danielk1977efaaf572006-01-16 11:29:19 +00004714 if( pPager->errCode ){
danielk19777f7bc662006-01-23 13:47:47 +00004715 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004716 }
drha6abd042004-06-09 17:37:22 +00004717 if( pPager->state<PAGER_RESERVED ){
drhd9b02572001-04-15 00:37:09 +00004718 return SQLITE_ERROR;
4719 }
drh86f8c192007-08-22 00:39:19 +00004720 pagerEnter(pPager);
drh4f0c5872007-03-26 22:05:01 +00004721 PAGERTRACE2("COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004722 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004723 pPg = pager_get_all_dirty_pages(pPager);
4724 while( pPg ){
danielk1977f35843b2007-04-07 15:03:17 +00004725 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4726 clearHistory(pHist);
drhac69b052004-05-12 13:30:07 +00004727 pPg->dirty = 0;
4728 pPg->inJournal = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004729 pHist->inStmt = 0;
drh5229ae42006-03-23 23:29:04 +00004730 pPg->needSync = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004731 pHist->pPrevStmt = pHist->pNextStmt = 0;
drhac69b052004-05-12 13:30:07 +00004732 pPg = pPg->pDirty;
4733 }
drh605ad8f2006-05-03 23:34:05 +00004734 pPager->pDirty = 0;
danielk1977eac7a362004-06-16 07:45:24 +00004735#ifndef NDEBUG
4736 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
4737 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4738 assert( !pPg->alwaysRollback );
4739 assert( !pHist->pOrig );
4740 assert( !pHist->pStmt );
4741 }
4742#endif
drhac69b052004-05-12 13:30:07 +00004743 pPager->pStmt = 0;
drha6abd042004-06-09 17:37:22 +00004744 pPager->state = PAGER_SHARED;
danielk197787c29a92008-01-18 11:33:16 +00004745 pagerLeave(pPager);
drhac69b052004-05-12 13:30:07 +00004746 return SQLITE_OK;
4747 }
drh3cdd7d32007-03-30 14:46:01 +00004748 assert( pPager->state==PAGER_SYNCED || !pPager->dirtyCache );
4749 rc = pager_end_transaction(pPager);
drh86f8c192007-08-22 00:39:19 +00004750 rc = pager_error(pPager, rc);
4751 pagerLeave(pPager);
4752 return rc;
drhed7c8552001-04-11 14:29:21 +00004753}
4754
4755/*
drha6abd042004-06-09 17:37:22 +00004756** Rollback all changes. The database falls back to PAGER_SHARED mode.
drhed7c8552001-04-11 14:29:21 +00004757** All in-memory cache pages revert to their original data contents.
4758** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00004759**
4760** This routine cannot fail unless some other process is not following
drh4f0ee682007-03-30 20:43:40 +00004761** the correct locking protocol or unless some other
drhd9b02572001-04-15 00:37:09 +00004762** process is writing trash into the journal file (SQLITE_CORRUPT) or
4763** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
4764** codes are returned for all these occasions. Otherwise,
4765** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00004766*/
danielk19773b8a05f2007-03-19 17:44:26 +00004767int sqlite3PagerRollback(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00004768 int rc;
drh4f0c5872007-03-26 22:05:01 +00004769 PAGERTRACE2("ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004770 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004771 PgHdr *p;
4772 for(p=pPager->pAll; p; p=p->pNextAll){
4773 PgHistory *pHist;
danielk1977eac7a362004-06-16 07:45:24 +00004774 assert( !p->alwaysRollback );
4775 if( !p->dirty ){
4776 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pOrig );
4777 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pStmt );
4778 continue;
4779 }
4780
drhac69b052004-05-12 13:30:07 +00004781 pHist = PGHDR_TO_HIST(p, pPager);
4782 if( pHist->pOrig ){
4783 memcpy(PGHDR_TO_DATA(p), pHist->pOrig, pPager->pageSize);
drh4f0c5872007-03-26 22:05:01 +00004784 PAGERTRACE3("ROLLBACK-PAGE %d of %d\n", p->pgno, PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00004785 }else{
drh4f0c5872007-03-26 22:05:01 +00004786 PAGERTRACE3("PAGE %d is clean on %d\n", p->pgno, PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00004787 }
4788 clearHistory(pHist);
4789 p->dirty = 0;
4790 p->inJournal = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004791 pHist->inStmt = 0;
4792 pHist->pPrevStmt = pHist->pNextStmt = 0;
danielk1977369f27e2004-06-15 11:40:04 +00004793 if( pPager->xReiniter ){
danielk19773b8a05f2007-03-19 17:44:26 +00004794 pPager->xReiniter(p, pPager->pageSize);
danielk1977369f27e2004-06-15 11:40:04 +00004795 }
drhac69b052004-05-12 13:30:07 +00004796 }
drh605ad8f2006-05-03 23:34:05 +00004797 pPager->pDirty = 0;
drhac69b052004-05-12 13:30:07 +00004798 pPager->pStmt = 0;
4799 pPager->dbSize = pPager->origDbSize;
danielk1977e180dd92007-04-05 17:15:52 +00004800 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00004801 pPager->stmtInUse = 0;
drha6abd042004-06-09 17:37:22 +00004802 pPager->state = PAGER_SHARED;
drhac69b052004-05-12 13:30:07 +00004803 return SQLITE_OK;
4804 }
4805
drh86f8c192007-08-22 00:39:19 +00004806 pagerEnter(pPager);
drha6abd042004-06-09 17:37:22 +00004807 if( !pPager->dirtyCache || !pPager->journalOpen ){
drh80e35f42007-03-30 14:06:34 +00004808 rc = pager_end_transaction(pPager);
drh86f8c192007-08-22 00:39:19 +00004809 pagerLeave(pPager);
drhda47d772002-12-02 04:25:19 +00004810 return rc;
4811 }
drhdb48ee02003-01-16 13:42:43 +00004812
danielk1977efaaf572006-01-16 11:29:19 +00004813 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
drha6abd042004-06-09 17:37:22 +00004814 if( pPager->state>=PAGER_EXCLUSIVE ){
danielk1977e277be02007-03-23 18:12:06 +00004815 pager_playback(pPager, 0);
drh4b845d72002-03-05 12:41:19 +00004816 }
drh86f8c192007-08-22 00:39:19 +00004817 pagerLeave(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00004818 return pPager->errCode;
drhed7c8552001-04-11 14:29:21 +00004819 }
drha6abd042004-06-09 17:37:22 +00004820 if( pPager->state==PAGER_RESERVED ){
danielk197717221812005-02-15 03:38:05 +00004821 int rc2;
danielk1977e277be02007-03-23 18:12:06 +00004822 rc = pager_playback(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00004823 rc2 = pager_end_transaction(pPager);
drha6abd042004-06-09 17:37:22 +00004824 if( rc==SQLITE_OK ){
4825 rc = rc2;
4826 }
4827 }else{
danielk1977e277be02007-03-23 18:12:06 +00004828 rc = pager_playback(pPager, 0);
drhd9b02572001-04-15 00:37:09 +00004829 }
danielk1977e180dd92007-04-05 17:15:52 +00004830 /* pager_reset(pPager); */
drhd9b02572001-04-15 00:37:09 +00004831 pPager->dbSize = -1;
danielk197707cb5602006-01-20 10:55:05 +00004832
4833 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
4834 ** cache. So call pager_error() on the way out to make any error
4835 ** persistent.
4836 */
drh86f8c192007-08-22 00:39:19 +00004837 rc = pager_error(pPager, rc);
4838 pagerLeave(pPager);
4839 return rc;
drh98808ba2001-10-18 12:34:46 +00004840}
drhd9b02572001-04-15 00:37:09 +00004841
4842/*
drh5e00f6c2001-09-13 13:46:56 +00004843** Return TRUE if the database file is opened read-only. Return FALSE
4844** if the database is (in theory) writable.
4845*/
danielk19773b8a05f2007-03-19 17:44:26 +00004846int sqlite3PagerIsreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00004847 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00004848}
4849
4850/*
drh0f7eb612006-08-08 13:51:43 +00004851** Return the number of references to the pager.
4852*/
danielk19773b8a05f2007-03-19 17:44:26 +00004853int sqlite3PagerRefcount(Pager *pPager){
drh0f7eb612006-08-08 13:51:43 +00004854 return pPager->nRef;
4855}
4856
4857#ifdef SQLITE_TEST
4858/*
drhd9b02572001-04-15 00:37:09 +00004859** This routine is used for testing and analysis only.
4860*/
danielk19773b8a05f2007-03-19 17:44:26 +00004861int *sqlite3PagerStats(Pager *pPager){
danielk197742741be2005-01-08 12:42:39 +00004862 static int a[11];
drhd9b02572001-04-15 00:37:09 +00004863 a[0] = pPager->nRef;
4864 a[1] = pPager->nPage;
4865 a[2] = pPager->mxPage;
4866 a[3] = pPager->dbSize;
4867 a[4] = pPager->state;
danielk1977efaaf572006-01-16 11:29:19 +00004868 a[5] = pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004869 a[6] = pPager->nHit;
4870 a[7] = pPager->nMiss;
drh7c4ac0c2007-04-05 11:25:58 +00004871 a[8] = 0; /* Used to be pPager->nOvfl */
danielk197742741be2005-01-08 12:42:39 +00004872 a[9] = pPager->nRead;
4873 a[10] = pPager->nWrite;
drhd9b02572001-04-15 00:37:09 +00004874 return a;
4875}
drh0f7eb612006-08-08 13:51:43 +00004876#endif
drhdd793422001-06-28 01:54:48 +00004877
drhfa86c412002-02-02 15:01:15 +00004878/*
drhac69b052004-05-12 13:30:07 +00004879** Set the statement rollback point.
drhfa86c412002-02-02 15:01:15 +00004880**
4881** This routine should be called with the transaction journal already
drhac69b052004-05-12 13:30:07 +00004882** open. A new statement journal is created that can be used to rollback
drhaaab5722002-02-19 13:39:21 +00004883** changes of a single SQL command within a larger transaction.
drhfa86c412002-02-02 15:01:15 +00004884*/
drh86f8c192007-08-22 00:39:19 +00004885static int pagerStmtBegin(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00004886 int rc;
drhac69b052004-05-12 13:30:07 +00004887 assert( !pPager->stmtInUse );
drh1aa2d8b2007-01-03 15:34:29 +00004888 assert( pPager->state>=PAGER_SHARED );
drhdc3ff9c2004-08-18 02:10:15 +00004889 assert( pPager->dbSize>=0 );
drh4f0c5872007-03-26 22:05:01 +00004890 PAGERTRACE2("STMT-BEGIN %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004891 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004892 pPager->stmtInUse = 1;
4893 pPager->stmtSize = pPager->dbSize;
4894 return SQLITE_OK;
4895 }
drhda47d772002-12-02 04:25:19 +00004896 if( !pPager->journalOpen ){
drhac69b052004-05-12 13:30:07 +00004897 pPager->stmtAutoopen = 1;
drhda47d772002-12-02 04:25:19 +00004898 return SQLITE_OK;
4899 }
drhfa86c412002-02-02 15:01:15 +00004900 assert( pPager->journalOpen );
drhed138fb2007-08-22 22:04:37 +00004901 pagerLeave(pPager);
drhf5e7bb52008-02-18 14:47:33 +00004902 assert( pPager->pInStmt==0 );
4903 pPager->pInStmt = sqlite3BitvecCreate(pPager->dbSize);
drhed138fb2007-08-22 22:04:37 +00004904 pagerEnter(pPager);
drhf5e7bb52008-02-18 14:47:33 +00004905 if( pPager->pInStmt==0 ){
danielk1977261919c2005-12-06 12:52:59 +00004906 /* sqlite3OsLock(pPager->fd, SHARED_LOCK); */
drhfa86c412002-02-02 15:01:15 +00004907 return SQLITE_NOMEM;
4908 }
danielk197776572402004-06-25 02:38:54 +00004909 pPager->stmtJSize = pPager->journalOff;
drhac69b052004-05-12 13:30:07 +00004910 pPager->stmtSize = pPager->dbSize;
danielk197776572402004-06-25 02:38:54 +00004911 pPager->stmtHdrOff = 0;
danielk197775edc162004-06-26 01:48:18 +00004912 pPager->stmtCksum = pPager->cksumInit;
drhac69b052004-05-12 13:30:07 +00004913 if( !pPager->stmtOpen ){
drh334b2992007-09-06 23:28:23 +00004914 rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->stfd, pPager->zStmtJrnl,
drh33f4e022007-09-03 15:19:34 +00004915 SQLITE_OPEN_SUBJOURNAL);
drh334b2992007-09-06 23:28:23 +00004916 if( rc ){
4917 goto stmt_begin_failed;
4918 }
drhac69b052004-05-12 13:30:07 +00004919 pPager->stmtOpen = 1;
4920 pPager->stmtNRec = 0;
drh0f892532002-05-30 12:27:03 +00004921 }
drhac69b052004-05-12 13:30:07 +00004922 pPager->stmtInUse = 1;
drhfa86c412002-02-02 15:01:15 +00004923 return SQLITE_OK;
4924
drhac69b052004-05-12 13:30:07 +00004925stmt_begin_failed:
drhf5e7bb52008-02-18 14:47:33 +00004926 if( pPager->pInStmt ){
4927 sqlite3BitvecDestroy(pPager->pInStmt);
4928 pPager->pInStmt = 0;
drhfa86c412002-02-02 15:01:15 +00004929 }
4930 return rc;
4931}
drh86f8c192007-08-22 00:39:19 +00004932int sqlite3PagerStmtBegin(Pager *pPager){
4933 int rc;
4934 pagerEnter(pPager);
4935 rc = pagerStmtBegin(pPager);
4936 pagerLeave(pPager);
4937 return rc;
4938}
drhfa86c412002-02-02 15:01:15 +00004939
4940/*
drhac69b052004-05-12 13:30:07 +00004941** Commit a statement.
drhfa86c412002-02-02 15:01:15 +00004942*/
danielk19773b8a05f2007-03-19 17:44:26 +00004943int sqlite3PagerStmtCommit(Pager *pPager){
drh86f8c192007-08-22 00:39:19 +00004944 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00004945 if( pPager->stmtInUse ){
drh03eb96a2002-11-10 23:32:56 +00004946 PgHdr *pPg, *pNext;
drh4f0c5872007-03-26 22:05:01 +00004947 PAGERTRACE2("STMT-COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004948 if( !MEMDB ){
drh054889e2005-11-30 03:20:31 +00004949 /* sqlite3OsTruncate(pPager->stfd, 0); */
drhf5e7bb52008-02-18 14:47:33 +00004950 sqlite3BitvecDestroy(pPager->pInStmt);
4951 pPager->pInStmt = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004952 }else{
4953 for(pPg=pPager->pStmt; pPg; pPg=pNext){
drhac69b052004-05-12 13:30:07 +00004954 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
danielk1977f35843b2007-04-07 15:03:17 +00004955 pNext = pHist->pNextStmt;
4956 assert( pHist->inStmt );
4957 pHist->inStmt = 0;
4958 pHist->pPrevStmt = pHist->pNextStmt = 0;
drh17435752007-08-16 04:30:38 +00004959 sqlite3_free(pHist->pStmt);
drhac69b052004-05-12 13:30:07 +00004960 pHist->pStmt = 0;
4961 }
4962 }
4963 pPager->stmtNRec = 0;
4964 pPager->stmtInUse = 0;
4965 pPager->pStmt = 0;
drh663fc632002-02-02 18:49:19 +00004966 }
drhac69b052004-05-12 13:30:07 +00004967 pPager->stmtAutoopen = 0;
drh86f8c192007-08-22 00:39:19 +00004968 pagerLeave(pPager);
drhfa86c412002-02-02 15:01:15 +00004969 return SQLITE_OK;
4970}
4971
4972/*
drhac69b052004-05-12 13:30:07 +00004973** Rollback a statement.
drhfa86c412002-02-02 15:01:15 +00004974*/
danielk19773b8a05f2007-03-19 17:44:26 +00004975int sqlite3PagerStmtRollback(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00004976 int rc;
drh86f8c192007-08-22 00:39:19 +00004977 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00004978 if( pPager->stmtInUse ){
drh4f0c5872007-03-26 22:05:01 +00004979 PAGERTRACE2("STMT-ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004980 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004981 PgHdr *pPg;
danielk1977f35843b2007-04-07 15:03:17 +00004982 PgHistory *pHist;
4983 for(pPg=pPager->pStmt; pPg; pPg=pHist->pNextStmt){
4984 pHist = PGHDR_TO_HIST(pPg, pPager);
drhac69b052004-05-12 13:30:07 +00004985 if( pHist->pStmt ){
4986 memcpy(PGHDR_TO_DATA(pPg), pHist->pStmt, pPager->pageSize);
drh17435752007-08-16 04:30:38 +00004987 sqlite3_free(pHist->pStmt);
drhac69b052004-05-12 13:30:07 +00004988 pHist->pStmt = 0;
4989 }
4990 }
4991 pPager->dbSize = pPager->stmtSize;
danielk1977e180dd92007-04-05 17:15:52 +00004992 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00004993 rc = SQLITE_OK;
4994 }else{
4995 rc = pager_stmt_playback(pPager);
4996 }
danielk19773b8a05f2007-03-19 17:44:26 +00004997 sqlite3PagerStmtCommit(pPager);
drh663fc632002-02-02 18:49:19 +00004998 }else{
4999 rc = SQLITE_OK;
5000 }
drhac69b052004-05-12 13:30:07 +00005001 pPager->stmtAutoopen = 0;
drh86f8c192007-08-22 00:39:19 +00005002 pagerLeave(pPager);
drhfa86c412002-02-02 15:01:15 +00005003 return rc;
5004}
5005
drh73509ee2003-04-06 20:44:45 +00005006/*
5007** Return the full pathname of the database file.
5008*/
danielk19773b8a05f2007-03-19 17:44:26 +00005009const char *sqlite3PagerFilename(Pager *pPager){
drh73509ee2003-04-06 20:44:45 +00005010 return pPager->zFilename;
5011}
5012
drhb20ea9d2004-02-09 01:20:36 +00005013/*
drhd0679ed2007-08-28 22:24:34 +00005014** Return the VFS structure for the pager.
5015*/
5016const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
5017 return pPager->pVfs;
5018}
5019
5020/*
drhcc6bb3e2007-08-31 16:11:35 +00005021** Return the file handle for the database file associated
5022** with the pager. This might return NULL if the file has
5023** not yet been opened.
5024*/
5025sqlite3_file *sqlite3PagerFile(Pager *pPager){
5026 return pPager->fd;
5027}
5028
5029/*
danielk19775865e3d2004-06-14 06:03:57 +00005030** Return the directory of the database file.
5031*/
danielk19773b8a05f2007-03-19 17:44:26 +00005032const char *sqlite3PagerDirname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00005033 return pPager->zDirectory;
5034}
5035
5036/*
5037** Return the full pathname of the journal file.
5038*/
danielk19773b8a05f2007-03-19 17:44:26 +00005039const char *sqlite3PagerJournalname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00005040 return pPager->zJournal;
5041}
5042
5043/*
drh2c8997b2005-08-27 16:36:48 +00005044** Return true if fsync() calls are disabled for this pager. Return FALSE
5045** if fsync()s are executed normally.
5046*/
danielk19773b8a05f2007-03-19 17:44:26 +00005047int sqlite3PagerNosync(Pager *pPager){
drh2c8997b2005-08-27 16:36:48 +00005048 return pPager->noSync;
5049}
5050
drh7c4ac0c2007-04-05 11:25:58 +00005051#ifdef SQLITE_HAS_CODEC
drh2c8997b2005-08-27 16:36:48 +00005052/*
drhb20ea9d2004-02-09 01:20:36 +00005053** Set the codec for this pager
5054*/
danielk19773b8a05f2007-03-19 17:44:26 +00005055void sqlite3PagerSetCodec(
drhb20ea9d2004-02-09 01:20:36 +00005056 Pager *pPager,
drhc001c582006-03-06 18:23:16 +00005057 void *(*xCodec)(void*,void*,Pgno,int),
drhb20ea9d2004-02-09 01:20:36 +00005058 void *pCodecArg
5059){
5060 pPager->xCodec = xCodec;
5061 pPager->pCodecArg = pCodecArg;
5062}
drh7c4ac0c2007-04-05 11:25:58 +00005063#endif
drhb20ea9d2004-02-09 01:20:36 +00005064
danielk1977687566d2004-11-02 12:56:41 +00005065#ifndef SQLITE_OMIT_AUTOVACUUM
5066/*
danielk197787c29a92008-01-18 11:33:16 +00005067** Move the page pPg to location pgno in the file.
danielk1977687566d2004-11-02 12:56:41 +00005068**
drh5e385312007-06-16 04:42:12 +00005069** There must be no references to the page previously located at
5070** pgno (which we call pPgOld) though that page is allowed to be
5071** in cache. If the page previous located at pgno is not already
5072** in the rollback journal, it is not put there by by this routine.
danielk1977687566d2004-11-02 12:56:41 +00005073**
drh5e385312007-06-16 04:42:12 +00005074** References to the page pPg remain valid. Updating any
5075** meta-data associated with pPg (i.e. data stored in the nExtra bytes
danielk1977687566d2004-11-02 12:56:41 +00005076** allocated along with the page) is the responsibility of the caller.
5077**
danielk19775fd057a2005-03-09 13:09:43 +00005078** A transaction must be active when this routine is called. It used to be
5079** required that a statement transaction was not active, but this restriction
5080** has been removed (CREATE INDEX needs to move a page when a statement
5081** transaction is active).
danielk1977687566d2004-11-02 12:56:41 +00005082*/
danielk19773b8a05f2007-03-19 17:44:26 +00005083int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno){
drh5e385312007-06-16 04:42:12 +00005084 PgHdr *pPgOld; /* The page being overwritten. */
danielk1977f5fdda82004-11-03 08:44:05 +00005085 int h;
danielk197794daf7f2004-11-08 09:26:09 +00005086 Pgno needSyncPgno = 0;
danielk1977687566d2004-11-02 12:56:41 +00005087
drh86f8c192007-08-22 00:39:19 +00005088 pagerEnter(pPager);
danielk1977687566d2004-11-02 12:56:41 +00005089 assert( pPg->nRef>0 );
5090
drh4f0c5872007-03-26 22:05:01 +00005091 PAGERTRACE5("MOVE %d page %d (needSync=%d) moves to %d\n",
danielk1977599fcba2004-11-08 07:13:13 +00005092 PAGERID(pPager), pPg->pgno, pPg->needSync, pgno);
drhb0603412007-02-28 04:47:26 +00005093 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
danielk1977ef73ee92004-11-06 12:26:07 +00005094
danielk1977b4626a32007-04-28 15:47:43 +00005095 pager_get_content(pPg);
danielk197794daf7f2004-11-08 09:26:09 +00005096 if( pPg->needSync ){
5097 needSyncPgno = pPg->pgno;
drh4f0aee42007-06-16 18:39:41 +00005098 assert( pPg->inJournal || (int)pgno>pPager->origDbSize );
danielk197794daf7f2004-11-08 09:26:09 +00005099 assert( pPg->dirty );
danielk1977ae825582004-11-23 09:06:55 +00005100 assert( pPager->needSync );
danielk197794daf7f2004-11-08 09:26:09 +00005101 }
5102
drh85b623f2007-12-13 21:54:09 +00005103 /* Unlink pPg from its hash-chain */
danielk1977f5fdda82004-11-03 08:44:05 +00005104 unlinkHashChain(pPager, pPg);
danielk1977687566d2004-11-02 12:56:41 +00005105
danielk1977ef73ee92004-11-06 12:26:07 +00005106 /* If the cache contains a page with page-number pgno, remove it
drh85b623f2007-12-13 21:54:09 +00005107 ** from its hash chain. Also, if the PgHdr.needSync was set for
danielk1977599fcba2004-11-08 07:13:13 +00005108 ** page pgno before the 'move' operation, it needs to be retained
5109 ** for the page moved there.
danielk1977f5fdda82004-11-03 08:44:05 +00005110 */
drh5e385312007-06-16 04:42:12 +00005111 pPg->needSync = 0;
danielk1977687566d2004-11-02 12:56:41 +00005112 pPgOld = pager_lookup(pPager, pgno);
5113 if( pPgOld ){
danielk1977f5fdda82004-11-03 08:44:05 +00005114 assert( pPgOld->nRef==0 );
5115 unlinkHashChain(pPager, pPgOld);
drh605ad8f2006-05-03 23:34:05 +00005116 makeClean(pPgOld);
drh5e385312007-06-16 04:42:12 +00005117 pPg->needSync = pPgOld->needSync;
5118 }else{
5119 pPg->needSync = 0;
5120 }
drhf5e7bb52008-02-18 14:47:33 +00005121 pPg->inJournal = sqlite3BitvecTest(pPager->pInJournal, pgno);
danielk1977687566d2004-11-02 12:56:41 +00005122
danielk1977f5fdda82004-11-03 08:44:05 +00005123 /* Change the page number for pPg and insert it into the new hash-chain. */
drh3765df42006-06-28 18:18:09 +00005124 assert( pgno!=0 );
danielk1977f5fdda82004-11-03 08:44:05 +00005125 pPg->pgno = pgno;
drh8ca0c722006-05-07 17:49:38 +00005126 h = pgno & (pPager->nHash-1);
danielk1977f5fdda82004-11-03 08:44:05 +00005127 if( pPager->aHash[h] ){
5128 assert( pPager->aHash[h]->pPrevHash==0 );
5129 pPager->aHash[h]->pPrevHash = pPg;
5130 }
5131 pPg->pNextHash = pPager->aHash[h];
5132 pPager->aHash[h] = pPg;
5133 pPg->pPrevHash = 0;
5134
drh605ad8f2006-05-03 23:34:05 +00005135 makeDirty(pPg);
danielk1977687566d2004-11-02 12:56:41 +00005136 pPager->dirtyCache = 1;
5137
danielk197794daf7f2004-11-08 09:26:09 +00005138 if( needSyncPgno ){
5139 /* If needSyncPgno is non-zero, then the journal file needs to be
5140 ** sync()ed before any data is written to database file page needSyncPgno.
5141 ** Currently, no such page exists in the page-cache and the
drhf5e7bb52008-02-18 14:47:33 +00005142 ** Pager.pInJournal bit has been set. This needs to be remedied by loading
danielk197794daf7f2004-11-08 09:26:09 +00005143 ** the page into the pager-cache and setting the PgHdr.needSync flag.
danielk1977ae825582004-11-23 09:06:55 +00005144 **
danielk1977a98d7b42008-01-18 13:42:54 +00005145 ** If the attempt to load the page into the page-cache fails, (due
drhf5e7bb52008-02-18 14:47:33 +00005146 ** to a malloc() or IO failure), clear the bit in the pInJournal[]
danielk1977a98d7b42008-01-18 13:42:54 +00005147 ** array. Otherwise, if the page is loaded and written again in
5148 ** this transaction, it may be written to the database file before
5149 ** it is synced into the journal file. This way, it may end up in
5150 ** the journal file twice, but that is not a problem.
5151 **
danielk19773b8a05f2007-03-19 17:44:26 +00005152 ** The sqlite3PagerGet() call may cause the journal to sync. So make
danielk1977ae825582004-11-23 09:06:55 +00005153 ** sure the Pager.needSync flag is set too.
danielk197794daf7f2004-11-08 09:26:09 +00005154 */
5155 int rc;
danielk19773b8a05f2007-03-19 17:44:26 +00005156 PgHdr *pPgHdr;
danielk1977ae825582004-11-23 09:06:55 +00005157 assert( pPager->needSync );
danielk19773b8a05f2007-03-19 17:44:26 +00005158 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
danielk197787c29a92008-01-18 11:33:16 +00005159 if( rc!=SQLITE_OK ){
drhf5e7bb52008-02-18 14:47:33 +00005160 if( pPager->pInJournal && (int)needSyncPgno<=pPager->origDbSize ){
5161 sqlite3BitvecClear(pPager->pInJournal, needSyncPgno);
danielk1977a98d7b42008-01-18 13:42:54 +00005162 }
danielk197787c29a92008-01-18 11:33:16 +00005163 pagerLeave(pPager);
5164 return rc;
5165 }
danielk1977ae825582004-11-23 09:06:55 +00005166 pPager->needSync = 1;
danielk19773b8a05f2007-03-19 17:44:26 +00005167 pPgHdr->needSync = 1;
5168 pPgHdr->inJournal = 1;
5169 makeDirty(pPgHdr);
5170 sqlite3PagerUnref(pPgHdr);
danielk197794daf7f2004-11-08 09:26:09 +00005171 }
5172
drh86f8c192007-08-22 00:39:19 +00005173 pagerLeave(pPager);
danielk1977687566d2004-11-02 12:56:41 +00005174 return SQLITE_OK;
5175}
5176#endif
5177
danielk19773b8a05f2007-03-19 17:44:26 +00005178/*
5179** Return a pointer to the data for the specified page.
5180*/
5181void *sqlite3PagerGetData(DbPage *pPg){
5182 return PGHDR_TO_DATA(pPg);
5183}
5184
5185/*
5186** Return a pointer to the Pager.nExtra bytes of "extra" space
5187** allocated along with the specified page.
5188*/
5189void *sqlite3PagerGetExtra(DbPage *pPg){
5190 Pager *pPager = pPg->pPager;
5191 return (pPager?PGHDR_TO_EXTRA(pPg, pPager):0);
5192}
5193
danielk197741483462007-03-24 16:45:04 +00005194/*
5195** Get/set the locking-mode for this pager. Parameter eMode must be one
5196** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
5197** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
5198** the locking-mode is set to the value specified.
5199**
5200** The returned value is either PAGER_LOCKINGMODE_NORMAL or
5201** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
5202** locking-mode.
5203*/
5204int sqlite3PagerLockingMode(Pager *pPager, int eMode){
drh369339d2007-03-30 16:01:55 +00005205 assert( eMode==PAGER_LOCKINGMODE_QUERY
5206 || eMode==PAGER_LOCKINGMODE_NORMAL
5207 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
5208 assert( PAGER_LOCKINGMODE_QUERY<0 );
5209 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
5210 if( eMode>=0 && !pPager->tempFile ){
danielk197741483462007-03-24 16:45:04 +00005211 pPager->exclusiveMode = eMode;
5212 }
5213 return (int)pPager->exclusiveMode;
5214}
5215
drh3b020132008-04-17 17:02:01 +00005216/*
5217** Get/set the journal-mode for this pager. Parameter eMode must be one
5218** of PAGER_JOURNALMODE_QUERY, PAGER_JOURNALMODE_DELETE or
5219** PAGER_JOURNALMODE_PERSIST. If the parameter is not _QUERY, then
5220** the journal-mode is set to the value specified.
5221**
5222** The returned value is either PAGER_JOURNALMODE_DELETE or
5223** PAGER_JOURNALMODE_PERSIST, indicating the current (possibly updated)
5224** journal-mode.
5225*/
5226int sqlite3PagerJournalMode(Pager *pPager, int eMode){
5227 assert( eMode==PAGER_JOURNALMODE_QUERY
5228 || eMode==PAGER_JOURNALMODE_DELETE
drhfdc40e92008-04-17 20:59:37 +00005229 || eMode==PAGER_JOURNALMODE_PERSIST
5230 || eMode==PAGER_JOURNALMODE_OFF );
drh3b020132008-04-17 17:02:01 +00005231 assert( PAGER_JOURNALMODE_QUERY<0 );
5232 assert( PAGER_JOURNALMODE_DELETE>=0 && PAGER_JOURNALMODE_PERSIST>=0 );
drhfdc40e92008-04-17 20:59:37 +00005233 if( eMode>=0 ){
5234 pPager->journalMode = eMode;
drh3b020132008-04-17 17:02:01 +00005235 }
drhfdc40e92008-04-17 20:59:37 +00005236 return (int)pPager->journalMode;
drh3b020132008-04-17 17:02:01 +00005237}
5238
drhd919fe12007-12-11 19:34:44 +00005239#ifdef SQLITE_TEST
drhdd793422001-06-28 01:54:48 +00005240/*
5241** Print a listing of all referenced pages and their ref count.
5242*/
danielk19773b8a05f2007-03-19 17:44:26 +00005243void sqlite3PagerRefdump(Pager *pPager){
drhdd793422001-06-28 01:54:48 +00005244 PgHdr *pPg;
5245 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
5246 if( pPg->nRef<=0 ) continue;
drhfe63d1c2004-09-08 20:13:04 +00005247 sqlite3DebugPrintf("PAGE %3d addr=%p nRef=%d\n",
5248 pPg->pgno, PGHDR_TO_DATA(pPg), pPg->nRef);
drhdd793422001-06-28 01:54:48 +00005249 }
5250}
5251#endif
drh2e66f0b2005-04-28 17:18:48 +00005252
5253#endif /* SQLITE_OMIT_DISKIO */