blob: 5901ca1ee9f7675f3205536d0b965091785eec58 [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**
danielk1977a1644fd2007-08-29 12:31:25 +000021** @(#) $Id: pager.c,v 1.380 2007/08/29 12:31:27 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**
50** PAGERID() takes a pointer to a Pager struct as it's argument. The
danielk197762079062007-08-15 17:08:46 +000051** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
danielk1977599fcba2004-11-08 07:13:13 +000052** struct as it's argument.
53*/
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
148** fsync() operation before it's memory can be reclaimed. If no such
149** 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**
216** inJournal = (pPager->aInJournal[(pgno-1)/8] & (1<<((pgno-1)%8))!=0
217**
218** The pPager->aInJournal[] array is only valid for the original
219** 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
drh605ad8f2006-05-03 23:34:05 +0000274 u32 notUsed; /* Buffer space */
danielk19773c407372005-02-15 02:54:14 +0000275#ifdef SQLITE_CHECK_PAGES
276 u32 pageHash;
277#endif
drh1c7880e2005-05-20 20:01:55 +0000278 /* pPager->pageSize bytes of page data follow this header */
drh973b6e32003-02-12 14:09:42 +0000279 /* Pager.nExtra bytes of local data follow the page data */
drhed7c8552001-04-11 14:29:21 +0000280};
281
drhac69b052004-05-12 13:30:07 +0000282/*
283** For an in-memory only database, some extra information is recorded about
284** each page so that changes can be rolled back. (Journal files are not
285** used for in-memory databases.) The following information is added to
286** the end of every EXTRA block for in-memory databases.
287**
288** This information could have been added directly to the PgHdr structure.
289** But then it would take up an extra 8 bytes of storage on every PgHdr
290** even for disk-based databases. Splitting it out saves 8 bytes. This
291** is only a savings of 0.8% but those percentages add up.
292*/
293typedef struct PgHistory PgHistory;
294struct PgHistory {
295 u8 *pOrig; /* Original page text. Restore to this on a full rollback */
296 u8 *pStmt; /* Text as it was at the beginning of the current statement */
danielk1977f35843b2007-04-07 15:03:17 +0000297 PgHdr *pNextStmt, *pPrevStmt; /* List of pages in the statement journal */
298 u8 inStmt; /* TRUE if in the statement subjournal */
drhac69b052004-05-12 13:30:07 +0000299};
drh9eb9e262004-02-11 02:18:05 +0000300
301/*
302** A macro used for invoking the codec if there is one
303*/
304#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000305# define CODEC1(P,D,N,X) if( P->xCodec!=0 ){ P->xCodec(P->pCodecArg,D,N,X); }
306# define CODEC2(P,D,N,X) ((char*)(P->xCodec!=0?P->xCodec(P->pCodecArg,D,N,X):D))
drh9eb9e262004-02-11 02:18:05 +0000307#else
drhc001c582006-03-06 18:23:16 +0000308# define CODEC1(P,D,N,X) /* NO-OP */
309# define CODEC2(P,D,N,X) ((char*)D)
drh9eb9e262004-02-11 02:18:05 +0000310#endif
311
drhed7c8552001-04-11 14:29:21 +0000312/*
drh69688d52001-04-14 16:38:23 +0000313** Convert a pointer to a PgHdr into a pointer to its data
314** and back again.
drhed7c8552001-04-11 14:29:21 +0000315*/
316#define PGHDR_TO_DATA(P) ((void*)(&(P)[1]))
317#define DATA_TO_PGHDR(D) (&((PgHdr*)(D))[-1])
drh1c7880e2005-05-20 20:01:55 +0000318#define PGHDR_TO_EXTRA(G,P) ((void*)&((char*)(&(G)[1]))[(P)->pageSize])
drhac69b052004-05-12 13:30:07 +0000319#define PGHDR_TO_HIST(P,PGR) \
drh1c7880e2005-05-20 20:01:55 +0000320 ((PgHistory*)&((char*)(&(P)[1]))[(PGR)->pageSize+(PGR)->nExtra])
drhed7c8552001-04-11 14:29:21 +0000321
322/*
drhed7c8552001-04-11 14:29:21 +0000323** A open page cache is an instance of the following structure.
danielk1977efaaf572006-01-16 11:29:19 +0000324**
drh4f0ee682007-03-30 20:43:40 +0000325** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or
danielk1977efaaf572006-01-16 11:29:19 +0000326** or SQLITE_FULL. Once one of the first three errors occurs, it persists
327** and is returned as the result of every major pager API call. The
328** SQLITE_FULL return code is slightly different. It persists only until the
329** next successful rollback is performed on the pager cache. Also,
danielk19773b8a05f2007-03-19 17:44:26 +0000330** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup()
danielk1977efaaf572006-01-16 11:29:19 +0000331** APIs, they may still be used successfully.
drhed7c8552001-04-11 14:29:21 +0000332*/
333struct Pager {
danielk1977b4b47412007-08-17 15:53:36 +0000334 sqlite3_vfs *pVfs; /* OS functions to use for IO */
drh603240c2002-03-05 01:11:12 +0000335 u8 journalOpen; /* True if journal file descriptors is valid */
drh34e79ce2004-02-08 06:05:46 +0000336 u8 journalStarted; /* True if header of journal is synced */
337 u8 useJournal; /* Use a rollback journal on this file */
drh7bec5052005-02-06 02:45:41 +0000338 u8 noReadlock; /* Do not bother to obtain readlocks */
drhac69b052004-05-12 13:30:07 +0000339 u8 stmtOpen; /* True if the statement subjournal is open */
340 u8 stmtInUse; /* True we are in a statement subtransaction */
341 u8 stmtAutoopen; /* Open stmt journal when main journal is opened*/
drh603240c2002-03-05 01:11:12 +0000342 u8 noSync; /* Do not sync the journal if true */
drh968af522003-02-11 14:55:40 +0000343 u8 fullSync; /* Do extra syncs of the journal for robustness */
danielk1977f036aef2007-08-20 05:36:51 +0000344 u8 sync_flags; /* One of SYNC_NORMAL or SYNC_FULL */
drha6abd042004-06-09 17:37:22 +0000345 u8 state; /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */
drh603240c2002-03-05 01:11:12 +0000346 u8 tempFile; /* zFilename is a temporary file */
347 u8 readOnly; /* True for a read-only database */
348 u8 needSync; /* True if an fsync() is needed on the journal */
drha6abd042004-06-09 17:37:22 +0000349 u8 dirtyCache; /* True if cached pages have changed */
drh80e35f42007-03-30 14:06:34 +0000350 u8 alwaysRollback; /* Disable DontRollback() for all pages */
drhac69b052004-05-12 13:30:07 +0000351 u8 memDb; /* True to inhibit all file I/O */
drh6d156e42005-05-20 20:11:20 +0000352 u8 setMaster; /* True if a m-j name has been written to jrnl */
drh80e35f42007-03-30 14:06:34 +0000353 u8 doNotSync; /* Boolean. While true, do not spill the cache */
354 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
355 u8 changeCountDone; /* Set after incrementing the change-counter */
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 */
drh603240c2002-03-05 01:11:12 +0000369 u8 *aInJournal; /* One bit for each page in the database file */
drhac69b052004-05-12 13:30:07 +0000370 u8 *aInStmt; /* 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 */
danielk197762079062007-08-15 17:08:46 +0000374 sqlite3_file *fd, *jfd; /* File descriptors for database and journal */
375 sqlite3_file *stfd; /* File descriptor for the statement subjournal*/
drh726de592004-06-10 23:35:50 +0000376 BusyHandler *pBusyHandler; /* Pointer to sqlite.busyHandler */
danielk19779f61c2f2007-08-27 17:27:49 +0000377 PagerLruList lru; /* LRU list of free pages */
drhd9b02572001-04-15 00:37:09 +0000378 PgHdr *pAll; /* List of all pages */
drhac69b052004-05-12 13:30:07 +0000379 PgHdr *pStmt; /* List of pages in the statement subjournal */
drh605ad8f2006-05-03 23:34:05 +0000380 PgHdr *pDirty; /* List of all dirty pages */
drheb206252004-10-01 02:00:31 +0000381 i64 journalOff; /* Current byte offset in the journal file */
382 i64 journalHdr; /* Byte offset to previous journal header */
383 i64 stmtHdrOff; /* First journal header written this statement */
384 i64 stmtCksum; /* cksumInit when statement was started */
drh6d156e42005-05-20 20:11:20 +0000385 i64 stmtJSize; /* Size of journal at stmt_begin() */
danielk197776572402004-06-25 02:38:54 +0000386 int sectorSize; /* Assumed sector size during rollback */
drhfcd35c72005-05-21 02:48:08 +0000387#ifdef SQLITE_TEST
drh7c4ac0c2007-04-05 11:25:58 +0000388 int nHit, nMiss; /* Cache hits and missing */
389 int nRead, nWrite; /* Database pages read/written */
drhfcd35c72005-05-21 02:48:08 +0000390#endif
danielk19773b8a05f2007-03-19 17:44:26 +0000391 void (*xDestructor)(DbPage*,int); /* Call this routine when freeing pages */
392 void (*xReiniter)(DbPage*,int); /* Call this routine when reloading pages */
drh7c4ac0c2007-04-05 11:25:58 +0000393#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000394 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
drh6d156e42005-05-20 20:11:20 +0000395 void *pCodecArg; /* First argument to xCodec() */
drh7c4ac0c2007-04-05 11:25:58 +0000396#endif
drh8ca0c722006-05-07 17:49:38 +0000397 int nHash; /* Size of the pager hash table */
398 PgHdr **aHash; /* Hash table to map page number to PgHdr */
drh6f7adc82006-01-11 21:41:20 +0000399#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +0000400 Pager *pNext; /* Doubly linked list of pagers on which */
401 Pager *pPrev; /* sqlite3_release_memory() will work */
402 int iInUseMM; /* Non-zero if unavailable to MM */
403 int iInUseDB; /* Non-zero if in sqlite3_release_memory() */
danielk197713f72992005-12-18 08:51:22 +0000404#endif
danielk19778186df82007-03-06 13:45:59 +0000405 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
drh86a88112007-04-16 15:02:19 +0000406 char dbFileVers[16]; /* Changes whenever database file changes */
drhd9b02572001-04-15 00:37:09 +0000407};
408
409/*
drh538f5702007-04-13 02:14:30 +0000410** The following global variables hold counters used for
411** testing purposes only. These variables do not exist in
412** a non-testing build. These variables are not thread-safe.
drhfcd35c72005-05-21 02:48:08 +0000413*/
414#ifdef SQLITE_TEST
drh538f5702007-04-13 02:14:30 +0000415int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
416int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
417int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
418int sqlite3_pager_pgfree_count = 0; /* Number of cache pages freed */
419# define PAGER_INCR(v) v++
drhfcd35c72005-05-21 02:48:08 +0000420#else
drh538f5702007-04-13 02:14:30 +0000421# define PAGER_INCR(v)
drhfcd35c72005-05-21 02:48:08 +0000422#endif
423
drh86f8c192007-08-22 00:39:19 +0000424/*
425** The following variable points to the head of a double-linked list
426** of all pagers that are eligible for page stealing by the
427** sqlite3_release_memory() interface. Access to this list is
428** protected by the SQLITE_MUTEX_STATIC_MEM2 mutex.
429*/
430#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
431static Pager *sqlite3PagerList = 0;
danielk19779f61c2f2007-08-27 17:27:49 +0000432static PagerLruList sqlite3LruPageList = {0, 0, 0};
drh86f8c192007-08-22 00:39:19 +0000433#endif
drh538f5702007-04-13 02:14:30 +0000434
435
drhfcd35c72005-05-21 02:48:08 +0000436/*
drh5e00f6c2001-09-13 13:46:56 +0000437** Journal files begin with the following magic string. The data
438** was obtained from /dev/random. It is used only as a sanity check.
drh94f33312002-08-12 12:29:56 +0000439**
drhae2b40c2004-06-09 19:03:54 +0000440** Since version 2.8.0, the journal format contains additional sanity
441** checking information. If the power fails while the journal is begin
442** written, semi-random garbage data might appear in the journal
443** file after power is restored. If an attempt is then made
drh968af522003-02-11 14:55:40 +0000444** to roll the journal back, the database could be corrupted. The additional
445** sanity checking data is an attempt to discover the garbage in the
446** journal and ignore it.
447**
drhae2b40c2004-06-09 19:03:54 +0000448** The sanity checking information for the new journal format consists
drh968af522003-02-11 14:55:40 +0000449** of a 32-bit checksum on each page of data. The checksum covers both
drh90f5ecb2004-07-22 01:19:35 +0000450** the page number and the pPager->pageSize bytes of data for the page.
drh968af522003-02-11 14:55:40 +0000451** This cksum is initialized to a 32-bit random value that appears in the
452** journal file right after the header. The random initializer is important,
453** because garbage data that appears at the end of a journal is likely
454** data that was once in other files that have now been deleted. If the
455** garbage data came from an obsolete journal file, the checksums might
456** be correct. But by initializing the checksum to random value which
457** is different for every journal, we minimize that risk.
drhd9b02572001-04-15 00:37:09 +0000458*/
drhae2b40c2004-06-09 19:03:54 +0000459static const unsigned char aJournalMagic[] = {
460 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
drhed7c8552001-04-11 14:29:21 +0000461};
462
463/*
drh726de592004-06-10 23:35:50 +0000464** The size of the header and of each page in the journal is determined
465** by the following macros.
drh968af522003-02-11 14:55:40 +0000466*/
drhae2b40c2004-06-09 19:03:54 +0000467#define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
drh968af522003-02-11 14:55:40 +0000468
danielk197776572402004-06-25 02:38:54 +0000469/*
470** The journal header size for this pager. In the future, this could be
471** set to some value read from the disk controller. The important
472** characteristic is that it is the same size as a disk sector.
473*/
474#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
475
drhb7f91642004-10-31 02:22:47 +0000476/*
477** The macro MEMDB is true if we are dealing with an in-memory database.
478** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
479** the value of MEMDB will be a constant and the compiler will optimize
480** out code that would never execute.
481*/
482#ifdef SQLITE_OMIT_MEMORYDB
483# define MEMDB 0
484#else
485# define MEMDB pPager->memDb
486#endif
487
488/*
danielk197776572402004-06-25 02:38:54 +0000489** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
490** reserved for working around a windows/posix incompatibility). It is
491** used in the journal to signify that the remainder of the journal file
492** is devoted to storing a master journal name - there are no more pages to
493** roll back. See comments for function writeMasterJournal() for details.
494*/
danielk1977599fcba2004-11-08 07:13:13 +0000495/* #define PAGER_MJ_PGNO(x) (PENDING_BYTE/((x)->pageSize)) */
496#define PAGER_MJ_PGNO(x) ((PENDING_BYTE/((x)->pageSize))+1)
danielk197713adf8a2004-06-03 16:08:41 +0000497
drh968af522003-02-11 14:55:40 +0000498/*
danielk197726836652005-01-17 01:33:13 +0000499** The maximum legal page number is (2^31 - 1).
500*/
501#define PAGER_MAX_PGNO 2147483647
502
503/*
drh86f8c192007-08-22 00:39:19 +0000504** The pagerEnter() and pagerLeave() routines acquire and release
505** a mutex on each pager. The mutex is recursive.
506**
507** This is a special-purpose mutex. It only provides mutual exclusion
508** between the Btree and the Memory Management sqlite3_release_memory()
509** function. It does not prevent, for example, two Btrees from accessing
510** the same pager at the same time. Other general-purpose mutexes in
511** the btree layer handle that chore.
512*/
513#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
514 static void pagerEnter(Pager *p){
515 p->iInUseDB++;
516 if( p->iInUseMM && p->iInUseDB==1 ){
517 sqlite3_mutex *mutex;
518 mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
519 p->iInUseDB = 0;
520 sqlite3_mutex_enter(mutex);
521 p->iInUseDB = 1;
522 sqlite3_mutex_leave(mutex);
523 }
524 assert( p->iInUseMM==0 );
525 }
526 static void pagerLeave(Pager *p){
527 p->iInUseDB--;
528 assert( p->iInUseDB>=0 );
529 }
530#else
531# define pagerEnter(X)
532# define pagerLeave(X)
533#endif
534
535/*
drh726de592004-06-10 23:35:50 +0000536** Enable reference count tracking (for debugging) here:
drhdd793422001-06-28 01:54:48 +0000537*/
drh7c4ac0c2007-04-05 11:25:58 +0000538#ifdef SQLITE_DEBUG
drh3aac2dd2004-04-26 14:10:20 +0000539 int pager3_refinfo_enable = 0;
drhdd793422001-06-28 01:54:48 +0000540 static void pager_refinfo(PgHdr *p){
541 static int cnt = 0;
drh3aac2dd2004-04-26 14:10:20 +0000542 if( !pager3_refinfo_enable ) return;
drhfe63d1c2004-09-08 20:13:04 +0000543 sqlite3DebugPrintf(
drh24c9a2e2007-01-05 02:00:47 +0000544 "REFCNT: %4d addr=%p nRef=%-3d total=%d\n",
545 p->pgno, PGHDR_TO_DATA(p), p->nRef, p->pPager->nRef
drhdd793422001-06-28 01:54:48 +0000546 );
547 cnt++; /* Something to set a breakpoint on */
548 }
549# define REFINFO(X) pager_refinfo(X)
550#else
551# define REFINFO(X)
552#endif
553
danielk197784f786f2007-08-28 08:00:17 +0000554/*
555** Add page pPg to the end of the linked list managed by structure
556** pList (pPg becomes the last entry in the list - the most recently
557** used). Argument pLink should point to either pPg->free or pPg->gfree,
558** depending on whether pPg is being added to the pager-specific or
559** global LRU list.
560*/
danielk19779f61c2f2007-08-27 17:27:49 +0000561static void listAdd(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){
562 pLink->pNext = 0;
563 pLink->pPrev = pList->pLast;
564
danielk197784f786f2007-08-28 08:00:17 +0000565#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
566 assert(pLink==&pPg->free || pLink==&pPg->gfree);
567 assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList);
568#endif
569
danielk19779f61c2f2007-08-27 17:27:49 +0000570 if( pList->pLast ){
571 int iOff = (char *)pLink - (char *)pPg;
572 PagerLruLink *pLastLink = (PagerLruLink *)(&((u8 *)pList->pLast)[iOff]);
573 pLastLink->pNext = pPg;
574 }else{
575 assert(!pList->pFirst);
576 pList->pFirst = pPg;
577 }
578
579 pList->pLast = pPg;
580 if( !pList->pFirstSynced && pPg->needSync==0 ){
581 pList->pFirstSynced = pPg;
582 }
583}
584
danielk197784f786f2007-08-28 08:00:17 +0000585/*
586** Remove pPg from the list managed by the structure pointed to by pList.
587**
588** Argument pLink should point to either pPg->free or pPg->gfree, depending
589** on whether pPg is being added to the pager-specific or global LRU list.
590*/
danielk19779f61c2f2007-08-27 17:27:49 +0000591static void listRemove(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){
592 int iOff = (char *)pLink - (char *)pPg;
593
danielk197784f786f2007-08-28 08:00:17 +0000594#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
595 assert(pLink==&pPg->free || pLink==&pPg->gfree);
596 assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList);
597#endif
598
danielk19779f61c2f2007-08-27 17:27:49 +0000599 if( pPg==pList->pFirst ){
600 pList->pFirst = pLink->pNext;
601 }
602 if( pPg==pList->pLast ){
603 pList->pLast = pLink->pPrev;
604 }
605 if( pLink->pPrev ){
606 PagerLruLink *pPrevLink = (PagerLruLink *)(&((u8 *)pLink->pPrev)[iOff]);
607 pPrevLink->pNext = pLink->pNext;
608 }
609 if( pLink->pNext ){
610 PagerLruLink *pNextLink = (PagerLruLink *)(&((u8 *)pLink->pNext)[iOff]);
611 pNextLink->pPrev = pLink->pPrev;
612 }
613 if( pPg==pList->pFirstSynced ){
614 PgHdr *p = pLink->pNext;
615 while( p && p->needSync ){
616 PagerLruLink *pL = (PagerLruLink *)(&((u8 *)p)[iOff]);
617 p = pL->pNext;
618 }
619 pList->pFirstSynced = p;
620 }
621
622 pLink->pNext = pLink->pPrev = 0;
623}
624
625/*
danielk197784f786f2007-08-28 08:00:17 +0000626** Add page pPg to the list of free pages for the pager. If
627** memory-management is enabled, also add the page to the global
628** list of free pages.
danielk19779f61c2f2007-08-27 17:27:49 +0000629*/
630static void lruListAdd(PgHdr *pPg){
631 listAdd(&pPg->pPager->lru, &pPg->free, pPg);
632#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
633 if( !pPg->pPager->memDb ){
634 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
635 listAdd(&sqlite3LruPageList, &pPg->gfree, pPg);
636 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
637 }
638#endif
639}
640
641/*
danielk197784f786f2007-08-28 08:00:17 +0000642** Remove page pPg from the list of free pages for the associated pager.
643** If memory-management is enabled, also remove pPg from the global list
644** of free pages.
danielk19779f61c2f2007-08-27 17:27:49 +0000645*/
646static void lruListRemove(PgHdr *pPg){
647 listRemove(&pPg->pPager->lru, &pPg->free, pPg);
648#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
649 if( !pPg->pPager->memDb ){
650 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
651 listRemove(&sqlite3LruPageList, &pPg->gfree, pPg);
652 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
653 }
654#endif
655}
656
657/*
danielk197784f786f2007-08-28 08:00:17 +0000658** This function is called just after the needSync flag has been cleared
659** from all pages managed by pPager (usually because the journal file
660** has just been synced). It updates the pPager->lru.pFirstSynced variable
661** and, if memory-management is enabled, the sqlite3LruPageList.pFirstSynced
662** variable also.
danielk19779f61c2f2007-08-27 17:27:49 +0000663*/
664static void lruListSetFirstSynced(Pager *pPager){
665 pPager->lru.pFirstSynced = pPager->lru.pFirst;
666#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
667 if( !pPager->memDb ){
668 PgHdr *p;
669 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
670 for(p=sqlite3LruPageList.pFirst; p && p->needSync; p=p->gfree.pNext);
671 assert(p==pPager->lru.pFirstSynced || p==sqlite3LruPageList.pFirstSynced);
672 sqlite3LruPageList.pFirstSynced = p;
673 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
674 }
675#endif
676}
677
danielk1977f35843b2007-04-07 15:03:17 +0000678/*
679** Return true if page *pPg has already been written to the statement
680** journal (or statement snapshot has been created, if *pPg is part
681** of an in-memory database).
682*/
683static int pageInStatement(PgHdr *pPg){
684 Pager *pPager = pPg->pPager;
685 if( MEMDB ){
686 return PGHDR_TO_HIST(pPg, pPager)->inStmt;
687 }else{
688 Pgno pgno = pPg->pgno;
689 u8 *a = pPager->aInStmt;
690 return (a && (int)pgno<=pPager->stmtSize && (a[pgno/8] & (1<<(pgno&7))));
691 }
692}
drh8ca0c722006-05-07 17:49:38 +0000693
694/*
695** Change the size of the pager hash table to N. N must be a power
696** of two.
697*/
698static void pager_resize_hash_table(Pager *pPager, int N){
699 PgHdr **aHash, *pPg;
700 assert( N>0 && (N&(N-1))==0 );
drhed138fb2007-08-22 22:04:37 +0000701 pagerLeave(pPager);
danielk1977a1644fd2007-08-29 12:31:25 +0000702 sqlite3MallocBenignFailure((int)pPager->aHash);
drh17435752007-08-16 04:30:38 +0000703 aHash = sqlite3MallocZero( sizeof(aHash[0])*N );
drhed138fb2007-08-22 22:04:37 +0000704 pagerEnter(pPager);
drh8ca0c722006-05-07 17:49:38 +0000705 if( aHash==0 ){
706 /* Failure to rehash is not an error. It is only a performance hit. */
707 return;
708 }
drh17435752007-08-16 04:30:38 +0000709 sqlite3_free(pPager->aHash);
drh8ca0c722006-05-07 17:49:38 +0000710 pPager->nHash = N;
711 pPager->aHash = aHash;
712 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
drh3765df42006-06-28 18:18:09 +0000713 int h;
714 if( pPg->pgno==0 ){
715 assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
716 continue;
717 }
718 h = pPg->pgno & (N-1);
drh8ca0c722006-05-07 17:49:38 +0000719 pPg->pNextHash = aHash[h];
720 if( aHash[h] ){
721 aHash[h]->pPrevHash = pPg;
722 }
723 aHash[h] = pPg;
724 pPg->pPrevHash = 0;
725 }
726}
727
drhdd793422001-06-28 01:54:48 +0000728/*
drh34e79ce2004-02-08 06:05:46 +0000729** Read a 32-bit integer from the given file descriptor. Store the integer
730** that is read in *pRes. Return SQLITE_OK if everything worked, or an
731** error code is something goes wrong.
drh726de592004-06-10 23:35:50 +0000732**
733** All values are stored on disk as big-endian.
drh94f33312002-08-12 12:29:56 +0000734*/
danielk197762079062007-08-15 17:08:46 +0000735static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
drh3b59a5c2006-01-15 20:28:28 +0000736 unsigned char ac[4];
danielk197762079062007-08-15 17:08:46 +0000737 int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
drhae2b40c2004-06-09 19:03:54 +0000738 if( rc==SQLITE_OK ){
drha3152892007-05-05 11:48:52 +0000739 *pRes = sqlite3Get4byte(ac);
drh94f33312002-08-12 12:29:56 +0000740 }
drh94f33312002-08-12 12:29:56 +0000741 return rc;
742}
743
744/*
drh97b57482006-01-10 20:32:32 +0000745** Write a 32-bit integer into a string buffer in big-endian byte order.
746*/
drha3152892007-05-05 11:48:52 +0000747#define put32bits(A,B) sqlite3Put4byte((u8*)A,B)
drh97b57482006-01-10 20:32:32 +0000748
749/*
drh34e79ce2004-02-08 06:05:46 +0000750** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
751** on success or an error code is something goes wrong.
drh94f33312002-08-12 12:29:56 +0000752*/
danielk197762079062007-08-15 17:08:46 +0000753static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
danielk1977bab45c62006-01-16 15:14:27 +0000754 char ac[4];
drh97b57482006-01-10 20:32:32 +0000755 put32bits(ac, val);
danielk197762079062007-08-15 17:08:46 +0000756 return sqlite3OsWrite(fd, ac, 4, offset);
drh94f33312002-08-12 12:29:56 +0000757}
758
drh2554f8b2003-01-22 01:26:44 +0000759/*
danielk19777a2b1ee2007-08-21 14:27:01 +0000760** If file pFd is open, call sqlite3OsUnlock() on it.
761*/
762static int osUnlock(sqlite3_file *pFd, int eLock){
763 if( !pFd->pMethods ){
764 return SQLITE_OK;
765 }
766 return sqlite3OsUnlock(pFd, eLock);
767}
768
769/*
danielk1977c7b60172007-08-22 11:22:03 +0000770** This function determines whether or not the atomic-write optimization
771** can be used with this pager. The optimization can be used if:
772**
773** (a) the value returned by OsDeviceCharacteristics() indicates that
774** a database page may be written atomically, and
775** (b) the value returned by OsSectorSize() is less than or equal
776** to the page size.
777**
778** If the optimization cannot be used, 0 is returned. If it can be used,
779** then the value returned is the size of the journal file when it
780** contains rollback data for exactly one page.
781*/
782#ifdef SQLITE_ENABLE_ATOMIC_WRITE
783static int jrnlBufferSize(Pager *pPager){
784 int dc; /* Device characteristics */
785 int nSector; /* Sector size */
786 int nPage; /* Page size */
787 sqlite3_file *fd = pPager->fd;
788
789 if( fd->pMethods ){
790 dc = sqlite3OsDeviceCharacteristics(fd);
791 nSector = sqlite3OsSectorSize(fd);
792 nPage = pPager->pageSize;
793 }
794
795 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
796 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
797
danielk1977f8940ae2007-08-23 11:07:10 +0000798 if( !fd->pMethods || (dc&(SQLITE_IOCAP_ATOMIC|(nPage>>8))&&nSector<=nPage) ){
danielk1977c7b60172007-08-22 11:22:03 +0000799 return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
800 }
801 return 0;
802}
803#endif
804
805/*
danielk1977aef0bf62005-12-30 16:28:01 +0000806** This function should be called when an error occurs within the pager
danielk1977a96a7102006-01-16 12:46:41 +0000807** code. The first argument is a pointer to the pager structure, the
808** second the error-code about to be returned by a pager API function.
809** The value returned is a copy of the second argument to this function.
810**
drh4f0ee682007-03-30 20:43:40 +0000811** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
danielk1977a96a7102006-01-16 12:46:41 +0000812** the error becomes persistent. All subsequent API calls on this Pager
813** will immediately return the same error code.
danielk1977aef0bf62005-12-30 16:28:01 +0000814*/
815static int pager_error(Pager *pPager, int rc){
drh4ac285a2006-09-15 07:28:50 +0000816 int rc2 = rc & 0xff;
drh34f56212007-08-10 23:56:35 +0000817 assert(
818 pPager->errCode==SQLITE_FULL ||
819 pPager->errCode==SQLITE_OK ||
820 (pPager->errCode & 0xff)==SQLITE_IOERR
821 );
danielk1977979f38e2007-03-27 16:19:51 +0000822 if(
drh4ac285a2006-09-15 07:28:50 +0000823 rc2==SQLITE_FULL ||
824 rc2==SQLITE_IOERR ||
drh4f0ee682007-03-30 20:43:40 +0000825 rc2==SQLITE_CORRUPT
danielk1977efaaf572006-01-16 11:29:19 +0000826 ){
827 pPager->errCode = rc;
danielk1977aef0bf62005-12-30 16:28:01 +0000828 }
829 return rc;
830}
831
drh477731b2007-06-16 03:06:27 +0000832/*
833** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
834** on the cache using a hash function. This is used for testing
835** and debugging only.
836*/
danielk19773c407372005-02-15 02:54:14 +0000837#ifdef SQLITE_CHECK_PAGES
838/*
839** Return a 32-bit hash of the page data for pPage.
840*/
drh477731b2007-06-16 03:06:27 +0000841static u32 pager_datahash(int nByte, unsigned char *pData){
danielk19773c407372005-02-15 02:54:14 +0000842 u32 hash = 0;
843 int i;
drh477731b2007-06-16 03:06:27 +0000844 for(i=0; i<nByte; i++){
845 hash = (hash*1039) + pData[i];
danielk19773c407372005-02-15 02:54:14 +0000846 }
847 return hash;
848}
drh477731b2007-06-16 03:06:27 +0000849static u32 pager_pagehash(PgHdr *pPage){
850 return pager_datahash(pPage->pPager->pageSize,
851 (unsigned char *)PGHDR_TO_DATA(pPage));
852}
danielk19773c407372005-02-15 02:54:14 +0000853
854/*
855** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
856** is defined, and NDEBUG is not defined, an assert() statement checks
857** that the page is either dirty or still matches the calculated page-hash.
858*/
859#define CHECK_PAGE(x) checkPage(x)
860static void checkPage(PgHdr *pPg){
861 Pager *pPager = pPg->pPager;
danielk1977efaaf572006-01-16 11:29:19 +0000862 assert( !pPg->pageHash || pPager->errCode || MEMDB || pPg->dirty ||
danielk19773c407372005-02-15 02:54:14 +0000863 pPg->pageHash==pager_pagehash(pPg) );
864}
865
866#else
drh8ffa8172007-06-18 17:25:17 +0000867#define pager_datahash(X,Y) 0
drh477731b2007-06-16 03:06:27 +0000868#define pager_pagehash(X) 0
danielk19773c407372005-02-15 02:54:14 +0000869#define CHECK_PAGE(x)
870#endif
871
drhed7c8552001-04-11 14:29:21 +0000872/*
danielk197776572402004-06-25 02:38:54 +0000873** When this is called the journal file for pager pPager must be open.
874** The master journal file name is read from the end of the file and
drh17435752007-08-16 04:30:38 +0000875** written into memory obtained from sqlite3_malloc(). *pzMaster is
danielk197776572402004-06-25 02:38:54 +0000876** set to point at the memory and SQLITE_OK returned. The caller must
drh17435752007-08-16 04:30:38 +0000877** sqlite3_free() *pzMaster.
danielk197776572402004-06-25 02:38:54 +0000878**
879** If no master journal file name is present *pzMaster is set to 0 and
880** SQLITE_OK returned.
881*/
danielk197762079062007-08-15 17:08:46 +0000882static int readMasterJournal(sqlite3_file *pJrnl, char **pzMaster){
danielk197776572402004-06-25 02:38:54 +0000883 int rc;
884 u32 len;
drheb206252004-10-01 02:00:31 +0000885 i64 szJ;
danielk1977c3e8f5e2004-06-28 01:16:46 +0000886 u32 cksum;
danielk1977cafadba2004-06-25 11:11:54 +0000887 int i;
danielk197776572402004-06-25 02:38:54 +0000888 unsigned char aMagic[8]; /* A buffer to hold the magic header */
889
890 *pzMaster = 0;
891
drh054889e2005-11-30 03:20:31 +0000892 rc = sqlite3OsFileSize(pJrnl, &szJ);
danielk1977cafadba2004-06-25 11:11:54 +0000893 if( rc!=SQLITE_OK || szJ<16 ) return rc;
danielk197776572402004-06-25 02:38:54 +0000894
danielk197762079062007-08-15 17:08:46 +0000895 rc = read32bits(pJrnl, szJ-16, &len);
danielk197776572402004-06-25 02:38:54 +0000896 if( rc!=SQLITE_OK ) return rc;
897
danielk197762079062007-08-15 17:08:46 +0000898 rc = read32bits(pJrnl, szJ-12, &cksum);
danielk1977cafadba2004-06-25 11:11:54 +0000899 if( rc!=SQLITE_OK ) return rc;
900
danielk197762079062007-08-15 17:08:46 +0000901 rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8);
danielk197776572402004-06-25 02:38:54 +0000902 if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc;
903
drh17435752007-08-16 04:30:38 +0000904 *pzMaster = (char *)sqlite3MallocZero(len+1);
danielk197776572402004-06-25 02:38:54 +0000905 if( !*pzMaster ){
906 return SQLITE_NOMEM;
907 }
danielk197762079062007-08-15 17:08:46 +0000908 rc = sqlite3OsRead(pJrnl, *pzMaster, len, szJ-16-len);
danielk197776572402004-06-25 02:38:54 +0000909 if( rc!=SQLITE_OK ){
drh17435752007-08-16 04:30:38 +0000910 sqlite3_free(*pzMaster);
danielk197776572402004-06-25 02:38:54 +0000911 *pzMaster = 0;
912 return rc;
913 }
danielk1977cafadba2004-06-25 11:11:54 +0000914
915 /* See if the checksum matches the master journal name */
916 for(i=0; i<len; i++){
danielk19778191bff2004-06-28 04:52:30 +0000917 cksum -= (*pzMaster)[i];
danielk1977cafadba2004-06-25 11:11:54 +0000918 }
danielk19778191bff2004-06-28 04:52:30 +0000919 if( cksum ){
920 /* If the checksum doesn't add up, then one or more of the disk sectors
921 ** containing the master journal filename is corrupted. This means
922 ** definitely roll back, so just return SQLITE_OK and report a (nul)
923 ** master-journal filename.
924 */
drh17435752007-08-16 04:30:38 +0000925 sqlite3_free(*pzMaster);
danielk1977cafadba2004-06-25 11:11:54 +0000926 *pzMaster = 0;
danielk1977aca790a2005-01-13 11:07:52 +0000927 }else{
928 (*pzMaster)[len] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000929 }
danielk197776572402004-06-25 02:38:54 +0000930
931 return SQLITE_OK;
932}
933
934/*
935** Seek the journal file descriptor to the next sector boundary where a
936** journal header may be read or written. Pager.journalOff is updated with
937** the new seek offset.
938**
939** i.e for a sector size of 512:
940**
941** Input Offset Output Offset
942** ---------------------------------------
943** 0 0
944** 512 512
945** 100 512
946** 2000 2048
947**
948*/
danielk197762079062007-08-15 17:08:46 +0000949static void seekJournalHdr(Pager *pPager){
drheb206252004-10-01 02:00:31 +0000950 i64 offset = 0;
951 i64 c = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000952 if( c ){
953 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
954 }
955 assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
956 assert( offset>=c );
957 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
958 pPager->journalOff = offset;
danielk197776572402004-06-25 02:38:54 +0000959}
960
961/*
962** The journal file must be open when this routine is called. A journal
963** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
964** current location.
965**
966** The format for the journal header is as follows:
967** - 8 bytes: Magic identifying journal format.
968** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
969** - 4 bytes: Random number used for page hash.
970** - 4 bytes: Initial database page count.
971** - 4 bytes: Sector size used by the process that wrote this journal.
972**
danielk1977f42f25c2004-06-25 07:21:28 +0000973** Followed by (JOURNAL_HDR_SZ - 24) bytes of unused space.
danielk197776572402004-06-25 02:38:54 +0000974*/
975static int writeJournalHdr(Pager *pPager){
drh97b57482006-01-10 20:32:32 +0000976 char zHeader[sizeof(aJournalMagic)+16];
danielk19774099f6e2007-03-19 11:25:20 +0000977 int rc;
danielk197776572402004-06-25 02:38:54 +0000978
danielk19774099f6e2007-03-19 11:25:20 +0000979 if( pPager->stmtHdrOff==0 ){
980 pPager->stmtHdrOff = pPager->journalOff;
981 }
982
danielk197762079062007-08-15 17:08:46 +0000983 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000984 pPager->journalHdr = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000985
drh97b57482006-01-10 20:32:32 +0000986 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
danielk19774cd2cd52007-08-23 14:48:23 +0000987
988 /*
989 ** Write the nRec Field - the number of page records that follow this
990 ** journal header. Normally, zero is written to this value at this time.
991 ** After the records are added to the journal (and the journal synced,
992 ** if in full-sync mode), the zero is overwritten with the true number
993 ** of records (see syncJournal()).
994 **
995 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
996 ** reading the journal this value tells SQLite to assume that the
997 ** rest of the journal file contains valid page records. This assumption
998 ** is dangerous, as if a failure occured whilst writing to the journal
999 ** file it may contain some garbage data. There are two scenarios
1000 ** where this risk can be ignored:
1001 **
1002 ** * When the pager is in no-sync mode. Corruption can follow a
1003 ** power failure in this case anyway.
1004 **
1005 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
1006 ** that garbage data is never appended to the journal file.
1007 */
1008 assert(pPager->fd->pMethods||pPager->noSync);
1009 if( (pPager->noSync)
1010 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
1011 ){
1012 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
1013 }else{
1014 put32bits(&zHeader[sizeof(aJournalMagic)], 0);
1015 }
1016
drh97b57482006-01-10 20:32:32 +00001017 /* The random check-hash initialiser */
1018 sqlite3Randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
1019 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
1020 /* The initial database size */
1021 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbSize);
1022 /* The assumed sector size for this process */
1023 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
drhb0603412007-02-28 04:47:26 +00001024 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, sizeof(zHeader)))
danielk197762079062007-08-15 17:08:46 +00001025 rc = sqlite3OsWrite(pPager->jfd, zHeader, sizeof(zHeader),pPager->journalOff);
1026 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk197776572402004-06-25 02:38:54 +00001027
1028 /* The journal header has been written successfully. Seek the journal
1029 ** file descriptor to the end of the journal header sector.
1030 */
1031 if( rc==SQLITE_OK ){
drhb0603412007-02-28 04:47:26 +00001032 IOTRACE(("JTAIL %p %lld\n", pPager, pPager->journalOff-1))
danielk197762079062007-08-15 17:08:46 +00001033 rc = sqlite3OsWrite(pPager->jfd, "\000", 1, pPager->journalOff-1);
danielk197776572402004-06-25 02:38:54 +00001034 }
1035 return rc;
1036}
1037
1038/*
1039** The journal file must be open when this is called. A journal header file
1040** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
1041** file. See comments above function writeJournalHdr() for a description of
1042** the journal header format.
1043**
1044** If the header is read successfully, *nRec is set to the number of
1045** page records following this header and *dbSize is set to the size of the
1046** database before the transaction began, in pages. Also, pPager->cksumInit
1047** is set to the value read from the journal header. SQLITE_OK is returned
1048** in this case.
1049**
1050** If the journal header file appears to be corrupted, SQLITE_DONE is
1051** returned and *nRec and *dbSize are not set. If JOURNAL_HDR_SZ bytes
1052** cannot be read from the journal file an error code is returned.
1053*/
1054static int readJournalHdr(
1055 Pager *pPager,
drheb206252004-10-01 02:00:31 +00001056 i64 journalSize,
danielk197776572402004-06-25 02:38:54 +00001057 u32 *pNRec,
1058 u32 *pDbSize
1059){
1060 int rc;
1061 unsigned char aMagic[8]; /* A buffer to hold the magic header */
danielk197762079062007-08-15 17:08:46 +00001062 i64 jrnlOff;
danielk197776572402004-06-25 02:38:54 +00001063
danielk197762079062007-08-15 17:08:46 +00001064 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +00001065 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
1066 return SQLITE_DONE;
1067 }
danielk197762079062007-08-15 17:08:46 +00001068 jrnlOff = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +00001069
danielk197762079062007-08-15 17:08:46 +00001070 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff);
danielk197776572402004-06-25 02:38:54 +00001071 if( rc ) return rc;
danielk197762079062007-08-15 17:08:46 +00001072 jrnlOff += sizeof(aMagic);
danielk197776572402004-06-25 02:38:54 +00001073
1074 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
1075 return SQLITE_DONE;
1076 }
1077
danielk197762079062007-08-15 17:08:46 +00001078 rc = read32bits(pPager->jfd, jrnlOff, pNRec);
danielk197776572402004-06-25 02:38:54 +00001079 if( rc ) return rc;
1080
danielk197762079062007-08-15 17:08:46 +00001081 rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit);
danielk197776572402004-06-25 02:38:54 +00001082 if( rc ) return rc;
1083
danielk197762079062007-08-15 17:08:46 +00001084 rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize);
danielk197776572402004-06-25 02:38:54 +00001085 if( rc ) return rc;
1086
1087 /* Update the assumed sector-size to match the value used by
1088 ** the process that created this journal. If this journal was
1089 ** created by a process other than this one, then this routine
1090 ** is being called from within pager_playback(). The local value
1091 ** of Pager.sectorSize is restored at the end of that routine.
1092 */
danielk197762079062007-08-15 17:08:46 +00001093 rc = read32bits(pPager->jfd, jrnlOff+12, (u32 *)&pPager->sectorSize);
danielk197776572402004-06-25 02:38:54 +00001094 if( rc ) return rc;
1095
1096 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk197762079062007-08-15 17:08:46 +00001097 return SQLITE_OK;
danielk197776572402004-06-25 02:38:54 +00001098}
1099
1100
1101/*
1102** Write the supplied master journal name into the journal file for pager
danielk1977cafadba2004-06-25 11:11:54 +00001103** pPager at the current location. The master journal name must be the last
1104** thing written to a journal file. If the pager is in full-sync mode, the
1105** journal file descriptor is advanced to the next sector boundary before
1106** anything is written. The format is:
1107**
1108** + 4 bytes: PAGER_MJ_PGNO.
1109** + N bytes: length of master journal name.
1110** + 4 bytes: N
1111** + 4 bytes: Master journal name checksum.
1112** + 8 bytes: aJournalMagic[].
1113**
1114** The master journal page checksum is the sum of the bytes in the master
1115** journal name.
danielk1977aef0bf62005-12-30 16:28:01 +00001116**
1117** If zMaster is a NULL pointer (occurs for a single database transaction),
1118** this call is a no-op.
danielk197776572402004-06-25 02:38:54 +00001119*/
1120static int writeMasterJournal(Pager *pPager, const char *zMaster){
1121 int rc;
1122 int len;
danielk1977cafadba2004-06-25 11:11:54 +00001123 int i;
danielk197762079062007-08-15 17:08:46 +00001124 i64 jrnlOff;
drh97b57482006-01-10 20:32:32 +00001125 u32 cksum = 0;
1126 char zBuf[sizeof(aJournalMagic)+2*4];
danielk197776572402004-06-25 02:38:54 +00001127
1128 if( !zMaster || pPager->setMaster) return SQLITE_OK;
1129 pPager->setMaster = 1;
1130
1131 len = strlen(zMaster);
danielk1977cafadba2004-06-25 11:11:54 +00001132 for(i=0; i<len; i++){
1133 cksum += zMaster[i];
1134 }
danielk197776572402004-06-25 02:38:54 +00001135
1136 /* If in full-sync mode, advance to the next disk sector before writing
1137 ** the master journal name. This is in case the previous page written to
1138 ** the journal has already been synced.
1139 */
1140 if( pPager->fullSync ){
danielk197762079062007-08-15 17:08:46 +00001141 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +00001142 }
danielk197762079062007-08-15 17:08:46 +00001143 jrnlOff = pPager->journalOff;
danielk1977cafadba2004-06-25 11:11:54 +00001144 pPager->journalOff += (len+20);
danielk197776572402004-06-25 02:38:54 +00001145
danielk197762079062007-08-15 17:08:46 +00001146 rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager));
danielk197776572402004-06-25 02:38:54 +00001147 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001148 jrnlOff += 4;
danielk197776572402004-06-25 02:38:54 +00001149
danielk197762079062007-08-15 17:08:46 +00001150 rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff);
danielk197776572402004-06-25 02:38:54 +00001151 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001152 jrnlOff += len;
danielk197776572402004-06-25 02:38:54 +00001153
drh97b57482006-01-10 20:32:32 +00001154 put32bits(zBuf, len);
1155 put32bits(&zBuf[4], cksum);
1156 memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic));
danielk197762079062007-08-15 17:08:46 +00001157 rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff);
drh2c8997b2005-08-27 16:36:48 +00001158 pPager->needSync = !pPager->noSync;
danielk197776572402004-06-25 02:38:54 +00001159 return rc;
1160}
1161
1162/*
drh03eb96a2002-11-10 23:32:56 +00001163** Add or remove a page from the list of all pages that are in the
drhac69b052004-05-12 13:30:07 +00001164** statement journal.
drh03eb96a2002-11-10 23:32:56 +00001165**
1166** The Pager keeps a separate list of pages that are currently in
danielk19773b8a05f2007-03-19 17:44:26 +00001167** the statement journal. This helps the sqlite3PagerStmtCommit()
drh03eb96a2002-11-10 23:32:56 +00001168** routine run MUCH faster for the common case where there are many
drhac69b052004-05-12 13:30:07 +00001169** pages in memory but only a few are in the statement journal.
drh03eb96a2002-11-10 23:32:56 +00001170*/
drh3aac2dd2004-04-26 14:10:20 +00001171static void page_add_to_stmt_list(PgHdr *pPg){
drh03eb96a2002-11-10 23:32:56 +00001172 Pager *pPager = pPg->pPager;
danielk1977f35843b2007-04-07 15:03:17 +00001173 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
1174 assert( MEMDB );
1175 if( !pHist->inStmt ){
1176 assert( pHist->pPrevStmt==0 && pHist->pNextStmt==0 );
1177 if( pPager->pStmt ){
1178 PGHDR_TO_HIST(pPager->pStmt, pPager)->pPrevStmt = pPg;
1179 }
1180 pHist->pNextStmt = pPager->pStmt;
1181 pPager->pStmt = pPg;
1182 pHist->inStmt = 1;
drh03eb96a2002-11-10 23:32:56 +00001183 }
drh03eb96a2002-11-10 23:32:56 +00001184}
1185
1186/*
drhed7c8552001-04-11 14:29:21 +00001187** Find a page in the hash table given its page number. Return
1188** a pointer to the page or NULL if not found.
1189*/
drhd9b02572001-04-15 00:37:09 +00001190static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drh8ca0c722006-05-07 17:49:38 +00001191 PgHdr *p;
1192 if( pPager->aHash==0 ) return 0;
1193 p = pPager->aHash[pgno & (pPager->nHash-1)];
drhed7c8552001-04-11 14:29:21 +00001194 while( p && p->pgno!=pgno ){
1195 p = p->pNextHash;
1196 }
1197 return p;
1198}
1199
1200/*
drh1aa2d8b2007-01-03 15:34:29 +00001201** Unlock the database file.
drh1aa2d8b2007-01-03 15:34:29 +00001202*/
1203static void pager_unlock(Pager *pPager){
danielk197741483462007-03-24 16:45:04 +00001204 if( !pPager->exclusiveMode ){
1205 if( !MEMDB ){
danielk19777a2b1ee2007-08-21 14:27:01 +00001206 osUnlock(pPager->fd, NO_LOCK);
danielk197741483462007-03-24 16:45:04 +00001207 pPager->dbSize = -1;
1208 IOTRACE(("UNLOCK %p\n", pPager))
1209 }
1210 pPager->state = PAGER_UNLOCK;
1211 pPager->changeCountDone = 0;
drh1aa2d8b2007-01-03 15:34:29 +00001212 }
danielk1977e277be02007-03-23 18:12:06 +00001213}
1214
1215/*
1216** Execute a rollback if a transaction is active and unlock the
1217** database file. This is a no-op if the pager has already entered
1218** the error-state.
1219*/
danielk1977334cdb62007-03-26 08:05:12 +00001220static void pagerUnlockAndRollback(Pager *p){
1221 if( p->errCode ) return;
danielk1977c5859712007-03-26 12:26:27 +00001222 assert( p->state>=PAGER_RESERVED || p->journalOpen==0 );
danielk1977334cdb62007-03-26 08:05:12 +00001223 if( p->state>=PAGER_RESERVED ){
1224 sqlite3PagerRollback(p);
danielk1977e277be02007-03-23 18:12:06 +00001225 }
danielk1977334cdb62007-03-26 08:05:12 +00001226 pager_unlock(p);
1227 assert( p->errCode || !p->journalOpen || (p->exclusiveMode&&!p->journalOff) );
1228 assert( p->errCode || !p->stmtOpen || p->exclusiveMode );
drh1aa2d8b2007-01-03 15:34:29 +00001229}
1230
1231
1232/*
danielk1977e180dd92007-04-05 17:15:52 +00001233** Clear the in-memory cache. This routine
drhed7c8552001-04-11 14:29:21 +00001234** sets the state of the pager back to what it was when it was first
1235** opened. Any outstanding pages are invalidated and subsequent attempts
1236** to access those pages will likely result in a coredump.
1237*/
drhd9b02572001-04-15 00:37:09 +00001238static void pager_reset(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00001239 PgHdr *pPg, *pNext;
danielk1977efaaf572006-01-16 11:29:19 +00001240 if( pPager->errCode ) return;
drhd9b02572001-04-15 00:37:09 +00001241 for(pPg=pPager->pAll; pPg; pPg=pNext){
drh538f5702007-04-13 02:14:30 +00001242 IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
1243 PAGER_INCR(sqlite3_pager_pgfree_count);
drhd9b02572001-04-15 00:37:09 +00001244 pNext = pPg->pNextAll;
danielk19779f61c2f2007-08-27 17:27:49 +00001245 lruListRemove(pPg);
drh17435752007-08-16 04:30:38 +00001246 sqlite3_free(pPg);
drhed7c8552001-04-11 14:29:21 +00001247 }
danielk19779f61c2f2007-08-27 17:27:49 +00001248 assert(pPager->lru.pFirst==0);
1249 assert(pPager->lru.pFirstSynced==0);
1250 assert(pPager->lru.pLast==0);
danielk1977b94bf852007-03-19 13:53:37 +00001251 pPager->pStmt = 0;
drhd9b02572001-04-15 00:37:09 +00001252 pPager->pAll = 0;
drh8ca0c722006-05-07 17:49:38 +00001253 pPager->nHash = 0;
drh17435752007-08-16 04:30:38 +00001254 sqlite3_free(pPager->aHash);
drhed7c8552001-04-11 14:29:21 +00001255 pPager->nPage = 0;
drh8ca0c722006-05-07 17:49:38 +00001256 pPager->aHash = 0;
drhed7c8552001-04-11 14:29:21 +00001257 pPager->nRef = 0;
danielk1977e277be02007-03-23 18:12:06 +00001258}
1259
1260/*
drh80e35f42007-03-30 14:06:34 +00001261** This routine ends a transaction. A transaction is ended by either
1262** a COMMIT or a ROLLBACK.
1263**
drhed7c8552001-04-11 14:29:21 +00001264** When this routine is called, the pager has the journal file open and
drh80e35f42007-03-30 14:06:34 +00001265** a RESERVED or EXCLUSIVE lock on the database. This routine will release
1266** the database lock and acquires a SHARED lock in its place if that is
1267** the appropriate thing to do. Release locks usually is appropriate,
1268** unless we are in exclusive access mode or unless this is a
1269** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation.
1270**
1271** The journal file is either deleted or truncated.
drh50457892003-09-06 01:10:47 +00001272**
1273** TODO: Consider keeping the journal file open for temporary databases.
1274** This might give a performance improvement on windows where opening
1275** a file is an expensive operation.
drhed7c8552001-04-11 14:29:21 +00001276*/
drh80e35f42007-03-30 14:06:34 +00001277static int pager_end_transaction(Pager *pPager){
drhd9b02572001-04-15 00:37:09 +00001278 PgHdr *pPg;
danielk197741483462007-03-24 16:45:04 +00001279 int rc = SQLITE_OK;
danielk1977979f38e2007-03-27 16:19:51 +00001280 int rc2 = SQLITE_OK;
drhb7f91642004-10-31 02:22:47 +00001281 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +00001282 if( pPager->state<PAGER_RESERVED ){
1283 return SQLITE_OK;
1284 }
danielk19773b8a05f2007-03-19 17:44:26 +00001285 sqlite3PagerStmtCommit(pPager);
danielk1977334cdb62007-03-26 08:05:12 +00001286 if( pPager->stmtOpen && !pPager->exclusiveMode ){
danielk1977b4b47412007-08-17 15:53:36 +00001287 sqlite3OsClose(pPager->stfd);
danielk1977979f38e2007-03-27 16:19:51 +00001288 pPager->stmtOpen = 0;
drh0f892532002-05-30 12:27:03 +00001289 }
drhda47d772002-12-02 04:25:19 +00001290 if( pPager->journalOpen ){
drh01fa4c32007-04-02 11:22:22 +00001291 if( pPager->exclusiveMode
1292 && (rc = sqlite3OsTruncate(pPager->jfd, 0))==SQLITE_OK ){;
danielk197741483462007-03-24 16:45:04 +00001293 pPager->journalOff = 0;
danielk1977334cdb62007-03-26 08:05:12 +00001294 pPager->journalStarted = 0;
danielk197741483462007-03-24 16:45:04 +00001295 }else{
danielk1977b4b47412007-08-17 15:53:36 +00001296 sqlite3OsClose(pPager->jfd);
danielk197741483462007-03-24 16:45:04 +00001297 pPager->journalOpen = 0;
drh01fa4c32007-04-02 11:22:22 +00001298 if( rc==SQLITE_OK ){
danielk1977fee2d252007-08-18 10:59:19 +00001299 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
danielk19777152de82007-03-29 17:28:14 +00001300 }
danielk197741483462007-03-24 16:45:04 +00001301 }
drh17435752007-08-16 04:30:38 +00001302 sqlite3_free( pPager->aInJournal );
drhda47d772002-12-02 04:25:19 +00001303 pPager->aInJournal = 0;
1304 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1305 pPg->inJournal = 0;
1306 pPg->dirty = 0;
drhdb48ee02003-01-16 13:42:43 +00001307 pPg->needSync = 0;
danielk197741483462007-03-24 16:45:04 +00001308 pPg->alwaysRollback = 0;
danielk19773c407372005-02-15 02:54:14 +00001309#ifdef SQLITE_CHECK_PAGES
1310 pPg->pageHash = pager_pagehash(pPg);
1311#endif
drhda47d772002-12-02 04:25:19 +00001312 }
drh605ad8f2006-05-03 23:34:05 +00001313 pPager->pDirty = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001314 pPager->dirtyCache = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001315 pPager->nRec = 0;
drhda47d772002-12-02 04:25:19 +00001316 }else{
drh88b01a12005-03-28 18:04:27 +00001317 assert( pPager->aInJournal==0 );
drha6abd042004-06-09 17:37:22 +00001318 assert( pPager->dirtyCache==0 || pPager->useJournal==0 );
drhd9b02572001-04-15 00:37:09 +00001319 }
danielk1977979f38e2007-03-27 16:19:51 +00001320
danielk197741483462007-03-24 16:45:04 +00001321 if( !pPager->exclusiveMode ){
danielk19777a2b1ee2007-08-21 14:27:01 +00001322 rc2 = osUnlock(pPager->fd, SHARED_LOCK);
danielk197741483462007-03-24 16:45:04 +00001323 pPager->state = PAGER_SHARED;
danielk1977334cdb62007-03-26 08:05:12 +00001324 }else if( pPager->state==PAGER_SYNCED ){
1325 pPager->state = PAGER_EXCLUSIVE;
danielk197741483462007-03-24 16:45:04 +00001326 }
danielk1977334cdb62007-03-26 08:05:12 +00001327 pPager->origDbSize = 0;
danielk197776572402004-06-25 02:38:54 +00001328 pPager->setMaster = 0;
danielk1977c4da5b92006-01-21 12:08:54 +00001329 pPager->needSync = 0;
danielk19779f61c2f2007-08-27 17:27:49 +00001330 lruListSetFirstSynced(pPager);
drh588f5bc2007-01-02 18:41:54 +00001331 pPager->dbSize = -1;
danielk1977979f38e2007-03-27 16:19:51 +00001332
1333 return (rc==SQLITE_OK?rc2:rc);
drhed7c8552001-04-11 14:29:21 +00001334}
1335
drhed7c8552001-04-11 14:29:21 +00001336/*
drh968af522003-02-11 14:55:40 +00001337** Compute and return a checksum for the page of data.
drh34e79ce2004-02-08 06:05:46 +00001338**
1339** This is not a real checksum. It is really just the sum of the
drh726de592004-06-10 23:35:50 +00001340** random initial value and the page number. We experimented with
1341** a checksum of the entire data, but that was found to be too slow.
1342**
1343** Note that the page number is stored at the beginning of data and
1344** the checksum is stored at the end. This is important. If journal
1345** corruption occurs due to a power failure, the most likely scenario
1346** is that one end or the other of the record will be changed. It is
1347** much less likely that the two ends of the journal record will be
1348** correct and the middle be corrupt. Thus, this "checksum" scheme,
1349** though fast and simple, catches the mostly likely kind of corruption.
1350**
1351** FIX ME: Consider adding every 200th (or so) byte of the data to the
1352** checksum. That way if a single page spans 3 or more disk sectors and
1353** only the middle sector is corrupt, we will still have a reasonable
1354** chance of failing the checksum and thus detecting the problem.
drh968af522003-02-11 14:55:40 +00001355*/
drh74161702006-02-24 02:53:49 +00001356static u32 pager_cksum(Pager *pPager, const u8 *aData){
danielk1977ef317ab2004-06-23 10:43:10 +00001357 u32 cksum = pPager->cksumInit;
1358 int i = pPager->pageSize-200;
1359 while( i>0 ){
1360 cksum += aData[i];
1361 i -= 200;
1362 }
drh968af522003-02-11 14:55:40 +00001363 return cksum;
1364}
1365
drh605ad8f2006-05-03 23:34:05 +00001366/* Forward declaration */
1367static void makeClean(PgHdr*);
1368
drh968af522003-02-11 14:55:40 +00001369/*
drhfa86c412002-02-02 15:01:15 +00001370** Read a single page from the journal file opened on file descriptor
1371** jfd. Playback this one page.
drh968af522003-02-11 14:55:40 +00001372**
drh726de592004-06-10 23:35:50 +00001373** If useCksum==0 it means this journal does not use checksums. Checksums
1374** are not used in statement journals because statement journals do not
1375** need to survive power failures.
drhfa86c412002-02-02 15:01:15 +00001376*/
danielk197762079062007-08-15 17:08:46 +00001377static int pager_playback_one_page(
1378 Pager *pPager,
1379 sqlite3_file *jfd,
1380 i64 offset,
1381 int useCksum
1382){
drhfa86c412002-02-02 15:01:15 +00001383 int rc;
drhae2b40c2004-06-09 19:03:54 +00001384 PgHdr *pPg; /* An existing page in the cache */
1385 Pgno pgno; /* The page number of a page in journal */
1386 u32 cksum; /* Checksum used for sanity checking */
danielk19778186df82007-03-06 13:45:59 +00001387 u8 *aData = (u8 *)pPager->pTmpSpace; /* Temp storage for a page */
drhfa86c412002-02-02 15:01:15 +00001388
drh96362842005-03-20 22:47:56 +00001389 /* useCksum should be true for the main journal and false for
1390 ** statement journals. Verify that this is always the case
1391 */
drh9cbe6352005-11-29 03:13:21 +00001392 assert( jfd == (useCksum ? pPager->jfd : pPager->stfd) );
danielk19778186df82007-03-06 13:45:59 +00001393 assert( aData );
drh96362842005-03-20 22:47:56 +00001394
danielk197762079062007-08-15 17:08:46 +00001395 rc = read32bits(jfd, offset, &pgno);
drh99ee3602003-02-16 19:13:36 +00001396 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001397 rc = sqlite3OsRead(jfd, aData, pPager->pageSize, offset+4);
drh99ee3602003-02-16 19:13:36 +00001398 if( rc!=SQLITE_OK ) return rc;
danielk197776572402004-06-25 02:38:54 +00001399 pPager->journalOff += pPager->pageSize + 4;
drhfa86c412002-02-02 15:01:15 +00001400
drh968af522003-02-11 14:55:40 +00001401 /* Sanity checking on the page. This is more important that I originally
1402 ** thought. If a power failure occurs while the journal is being written,
1403 ** it could cause invalid data to be written into the journal. We need to
1404 ** detect this invalid data (with high probability) and ignore it.
1405 */
danielk197775edc162004-06-26 01:48:18 +00001406 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh968af522003-02-11 14:55:40 +00001407 return SQLITE_DONE;
1408 }
drhae2b40c2004-06-09 19:03:54 +00001409 if( pgno>(unsigned)pPager->dbSize ){
drh968af522003-02-11 14:55:40 +00001410 return SQLITE_OK;
1411 }
drhae2b40c2004-06-09 19:03:54 +00001412 if( useCksum ){
danielk197762079062007-08-15 17:08:46 +00001413 rc = read32bits(jfd, offset+pPager->pageSize+4, &cksum);
drh99ee3602003-02-16 19:13:36 +00001414 if( rc ) return rc;
danielk197776572402004-06-25 02:38:54 +00001415 pPager->journalOff += 4;
drh74161702006-02-24 02:53:49 +00001416 if( pager_cksum(pPager, aData)!=cksum ){
drh968af522003-02-11 14:55:40 +00001417 return SQLITE_DONE;
1418 }
1419 }
drhfa86c412002-02-02 15:01:15 +00001420
danielk1977aa5ccdf2004-06-14 05:10:42 +00001421 assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE );
danielk1977a3f3a5f2004-06-10 04:32:16 +00001422
1423 /* If the pager is in RESERVED state, then there must be a copy of this
1424 ** page in the pager cache. In this case just update the pager cache,
danielk19770de0bb32004-06-10 05:59:24 +00001425 ** not the database file. The page is left marked dirty in this case.
1426 **
danielk19772df71c72007-05-24 07:22:42 +00001427 ** An exception to the above rule: If the database is in no-sync mode
1428 ** and a page is moved during an incremental vacuum then the page may
danielk1977369f3a02007-05-24 09:41:20 +00001429 ** not be in the pager cache. Later: if a malloc() or IO error occurs
1430 ** during a Movepage() call, then the page may not be in the cache
1431 ** either. So the condition described in the above paragraph is not
1432 ** assert()able.
danielk19772df71c72007-05-24 07:22:42 +00001433 **
danielk1977a3f3a5f2004-06-10 04:32:16 +00001434 ** If in EXCLUSIVE state, then we update the pager cache if it exists
1435 ** and the main file. The page is then marked not dirty.
drh96362842005-03-20 22:47:56 +00001436 **
1437 ** Ticket #1171: The statement journal might contain page content that is
1438 ** different from the page content at the start of the transaction.
1439 ** This occurs when a page is changed prior to the start of a statement
1440 ** then changed again within the statement. When rolling back such a
1441 ** statement we must not write to the original database unless we know
drh5e385312007-06-16 04:42:12 +00001442 ** for certain that original page contents are synced into the main rollback
1443 ** journal. Otherwise, a power loss might leave modified data in the
1444 ** database file without an entry in the rollback journal that can
1445 ** restore the database to its original form. Two conditions must be
1446 ** met before writing to the database files. (1) the database must be
1447 ** locked. (2) we know that the original page content is fully synced
1448 ** in the main journal either because the page is not in cache or else
1449 ** the page is marked as needSync==0.
drhfa86c412002-02-02 15:01:15 +00001450 */
drhae2b40c2004-06-09 19:03:54 +00001451 pPg = pager_lookup(pPager, pgno);
drh477731b2007-06-16 03:06:27 +00001452 PAGERTRACE4("PLAYBACK %d page %d hash(%08x)\n",
1453 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData));
drh96362842005-03-20 22:47:56 +00001454 if( pPager->state>=PAGER_EXCLUSIVE && (pPg==0 || pPg->needSync==0) ){
danielk197762079062007-08-15 17:08:46 +00001455 i64 offset = (pgno-1)*(i64)pPager->pageSize;
1456 rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, offset);
drh605ad8f2006-05-03 23:34:05 +00001457 if( pPg ){
1458 makeClean(pPg);
1459 }
danielk1977a3f3a5f2004-06-10 04:32:16 +00001460 }
drhfa86c412002-02-02 15:01:15 +00001461 if( pPg ){
danielk197728129562005-01-11 10:25:06 +00001462 /* No page should ever be explicitly rolled back that is in use, except
1463 ** for page 1 which is held in use in order to keep the lock on the
1464 ** database active. However such a page may be rolled back as a result
1465 ** of an internal error resulting in an automatic call to
danielk19773b8a05f2007-03-19 17:44:26 +00001466 ** sqlite3PagerRollback().
drhacf4ac92003-12-17 23:57:34 +00001467 */
drhb6f41482004-05-14 01:58:11 +00001468 void *pData;
danielk197728129562005-01-11 10:25:06 +00001469 /* assert( pPg->nRef==0 || pPg->pgno==1 ); */
drhb6f41482004-05-14 01:58:11 +00001470 pData = PGHDR_TO_DATA(pPg);
drhae2b40c2004-06-09 19:03:54 +00001471 memcpy(pData, aData, pPager->pageSize);
danielk19779038bb62007-04-09 11:20:54 +00001472 if( pPager->xReiniter ){
1473 pPager->xReiniter(pPg, pPager->pageSize);
drhde647132004-05-07 17:57:49 +00001474 }
danielk19773c407372005-02-15 02:54:14 +00001475#ifdef SQLITE_CHECK_PAGES
drh96362842005-03-20 22:47:56 +00001476 pPg->pageHash = pager_pagehash(pPg);
danielk19773c407372005-02-15 02:54:14 +00001477#endif
drh86a88112007-04-16 15:02:19 +00001478 /* If this was page 1, then restore the value of Pager.dbFileVers.
1479 ** Do this before any decoding. */
danielk197741483462007-03-24 16:45:04 +00001480 if( pgno==1 ){
drh86a88112007-04-16 15:02:19 +00001481 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
danielk197741483462007-03-24 16:45:04 +00001482 }
drh86a88112007-04-16 15:02:19 +00001483
1484 /* Decode the page just read from disk */
1485 CODEC1(pPager, pData, pPg->pgno, 3);
drhfa86c412002-02-02 15:01:15 +00001486 }
1487 return rc;
1488}
1489
1490/*
danielk197713adf8a2004-06-03 16:08:41 +00001491** Parameter zMaster is the name of a master journal file. A single journal
1492** file that referred to the master journal file has just been rolled back.
1493** This routine checks if it is possible to delete the master journal file,
1494** and does so if it is.
drh726de592004-06-10 23:35:50 +00001495**
1496** The master journal file contains the names of all child journals.
1497** To tell if a master journal can be deleted, check to each of the
1498** children. If all children are either missing or do not refer to
1499** a different master journal, then this master journal can be deleted.
danielk197713adf8a2004-06-03 16:08:41 +00001500*/
danielk1977b4b47412007-08-17 15:53:36 +00001501static int pager_delmaster(Pager *pPager, const char *zMaster){
1502 sqlite3_vfs *pVfs = pPager->pVfs;
danielk197713adf8a2004-06-03 16:08:41 +00001503 int rc;
1504 int master_open = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001505 sqlite3_file *pMaster;
1506 sqlite3_file *pJournal;
danielk197713adf8a2004-06-03 16:08:41 +00001507 char *zMasterJournal = 0; /* Contents of master journal file */
drheb206252004-10-01 02:00:31 +00001508 i64 nMasterJournal; /* Size of master journal file */
danielk197713adf8a2004-06-03 16:08:41 +00001509
1510 /* Open the master journal file exclusively in case some other process
1511 ** is running this routine also. Not that it makes too much difference.
1512 */
danielk1977b4b47412007-08-17 15:53:36 +00001513 pMaster = (sqlite3_file *)sqlite3_malloc(pVfs->szOsFile * 2);
danielk1977fee2d252007-08-18 10:59:19 +00001514 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
danielk1977b4b47412007-08-17 15:53:36 +00001515 if( !pMaster ){
1516 rc = SQLITE_NOMEM;
1517 }else{
danielk1977fee2d252007-08-18 10:59:19 +00001518 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
1519 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
danielk1977b4b47412007-08-17 15:53:36 +00001520 }
danielk197713adf8a2004-06-03 16:08:41 +00001521 if( rc!=SQLITE_OK ) goto delmaster_out;
1522 master_open = 1;
danielk1977b4b47412007-08-17 15:53:36 +00001523
1524 rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001525 if( rc!=SQLITE_OK ) goto delmaster_out;
1526
1527 if( nMasterJournal>0 ){
danielk19775865e3d2004-06-14 06:03:57 +00001528 char *zJournal;
danielk197776572402004-06-25 02:38:54 +00001529 char *zMasterPtr = 0;
danielk19775865e3d2004-06-14 06:03:57 +00001530
1531 /* Load the entire master journal file into space obtained from
drh17435752007-08-16 04:30:38 +00001532 ** sqlite3_malloc() and pointed to by zMasterJournal.
danielk19775865e3d2004-06-14 06:03:57 +00001533 */
drh17435752007-08-16 04:30:38 +00001534 zMasterJournal = (char *)sqlite3_malloc(nMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001535 if( !zMasterJournal ){
1536 rc = SQLITE_NOMEM;
1537 goto delmaster_out;
1538 }
danielk1977b4b47412007-08-17 15:53:36 +00001539 rc = sqlite3OsRead(pMaster, zMasterJournal, nMasterJournal, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001540 if( rc!=SQLITE_OK ) goto delmaster_out;
1541
danielk19775865e3d2004-06-14 06:03:57 +00001542 zJournal = zMasterJournal;
1543 while( (zJournal-zMasterJournal)<nMasterJournal ){
danielk1977b4b47412007-08-17 15:53:36 +00001544 if( sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS) ){
danielk197713adf8a2004-06-03 16:08:41 +00001545 /* One of the journals pointed to by the master journal exists.
1546 ** Open it and check if it points at the master journal. If
1547 ** so, return without deleting the master journal file.
1548 */
drh3b7b78b2004-11-24 01:16:43 +00001549 int c;
danielk1977fee2d252007-08-18 10:59:19 +00001550 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
1551 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001552 if( rc!=SQLITE_OK ){
danielk197713adf8a2004-06-03 16:08:41 +00001553 goto delmaster_out;
1554 }
danielk197713adf8a2004-06-03 16:08:41 +00001555
danielk1977b4b47412007-08-17 15:53:36 +00001556 rc = readMasterJournal(pJournal, &zMasterPtr);
1557 sqlite3OsClose(pJournal);
danielk19779eed5052004-06-04 10:38:30 +00001558 if( rc!=SQLITE_OK ){
danielk19779eed5052004-06-04 10:38:30 +00001559 goto delmaster_out;
1560 }
danielk197776572402004-06-25 02:38:54 +00001561
drh3b7b78b2004-11-24 01:16:43 +00001562 c = zMasterPtr!=0 && strcmp(zMasterPtr, zMaster)==0;
drh17435752007-08-16 04:30:38 +00001563 sqlite3_free(zMasterPtr);
drh3b7b78b2004-11-24 01:16:43 +00001564 if( c ){
danielk197776572402004-06-25 02:38:54 +00001565 /* We have a match. Do not delete the master journal file. */
1566 goto delmaster_out;
danielk197713adf8a2004-06-03 16:08:41 +00001567 }
1568 }
danielk19775865e3d2004-06-14 06:03:57 +00001569 zJournal += (strlen(zJournal)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001570 }
1571 }
1572
danielk1977fee2d252007-08-18 10:59:19 +00001573 rc = sqlite3OsDelete(pVfs, zMaster, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001574
1575delmaster_out:
1576 if( zMasterJournal ){
drh17435752007-08-16 04:30:38 +00001577 sqlite3_free(zMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001578 }
1579 if( master_open ){
danielk1977b4b47412007-08-17 15:53:36 +00001580 sqlite3OsClose(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001581 }
danielk1977b4b47412007-08-17 15:53:36 +00001582 sqlite3_free(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001583 return rc;
1584}
1585
drha6abd042004-06-09 17:37:22 +00001586
danielk1977e180dd92007-04-05 17:15:52 +00001587static void pager_truncate_cache(Pager *pPager);
1588
drha6abd042004-06-09 17:37:22 +00001589/*
drhcb4c40b2004-08-18 19:09:43 +00001590** Truncate the main file of the given pager to the number of pages
danielk1977e180dd92007-04-05 17:15:52 +00001591** indicated. Also truncate the cached representation of the file.
drhcb4c40b2004-08-18 19:09:43 +00001592*/
1593static int pager_truncate(Pager *pPager, int nPage){
danielk1977e180dd92007-04-05 17:15:52 +00001594 int rc = SQLITE_OK;
danielk19777a2b1ee2007-08-21 14:27:01 +00001595 if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){
danielk1977e180dd92007-04-05 17:15:52 +00001596 rc = sqlite3OsTruncate(pPager->fd, pPager->pageSize*(i64)nPage);
1597 }
1598 if( rc==SQLITE_OK ){
1599 pPager->dbSize = nPage;
1600 pager_truncate_cache(pPager);
1601 }
1602 return rc;
drhcb4c40b2004-08-18 19:09:43 +00001603}
1604
1605/*
drhc80f0582007-05-01 16:59:48 +00001606** Set the sectorSize for the given pager.
1607**
1608** The sector size is the larger of the sector size reported
1609** by sqlite3OsSectorSize() and the pageSize.
1610*/
1611static void setSectorSize(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00001612 assert(pPager->fd->pMethods||pPager->tempFile);
1613 if( !pPager->tempFile ){
1614 /* Sector size doesn't matter for temporary files. Also, the file
1615 ** may not have been opened yet, in whcih case the OsSectorSize()
1616 ** call will segfault.
1617 */
1618 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
1619 }
drhc80f0582007-05-01 16:59:48 +00001620 if( pPager->sectorSize<pPager->pageSize ){
1621 pPager->sectorSize = pPager->pageSize;
1622 }
1623}
1624
1625/*
drhed7c8552001-04-11 14:29:21 +00001626** Playback the journal and thus restore the database file to
1627** the state it was in before we started making changes.
1628**
drh34e79ce2004-02-08 06:05:46 +00001629** The journal file format is as follows:
1630**
drhae2b40c2004-06-09 19:03:54 +00001631** (1) 8 byte prefix. A copy of aJournalMagic[].
1632** (2) 4 byte big-endian integer which is the number of valid page records
drh34e79ce2004-02-08 06:05:46 +00001633** in the journal. If this value is 0xffffffff, then compute the
drhae2b40c2004-06-09 19:03:54 +00001634** number of page records from the journal size.
1635** (3) 4 byte big-endian integer which is the initial value for the
1636** sanity checksum.
1637** (4) 4 byte integer which is the number of pages to truncate the
drh34e79ce2004-02-08 06:05:46 +00001638** database to during a rollback.
drhae2b40c2004-06-09 19:03:54 +00001639** (5) 4 byte integer which is the number of bytes in the master journal
1640** name. The value may be zero (indicate that there is no master
1641** journal.)
1642** (6) N bytes of the master journal name. The name will be nul-terminated
1643** and might be shorter than the value read from (5). If the first byte
1644** of the name is \000 then there is no master journal. The master
1645** journal name is stored in UTF-8.
1646** (7) Zero or more pages instances, each as follows:
drh34e79ce2004-02-08 06:05:46 +00001647** + 4 byte page number.
drhae2b40c2004-06-09 19:03:54 +00001648** + pPager->pageSize bytes of data.
1649** + 4 byte checksum
drh34e79ce2004-02-08 06:05:46 +00001650**
drhae2b40c2004-06-09 19:03:54 +00001651** When we speak of the journal header, we mean the first 6 items above.
1652** Each entry in the journal is an instance of the 7th item.
drh34e79ce2004-02-08 06:05:46 +00001653**
1654** Call the value from the second bullet "nRec". nRec is the number of
1655** valid page entries in the journal. In most cases, you can compute the
1656** value of nRec from the size of the journal file. But if a power
1657** failure occurred while the journal was being written, it could be the
1658** case that the size of the journal file had already been increased but
1659** the extra entries had not yet made it safely to disk. In such a case,
1660** the value of nRec computed from the file size would be too large. For
1661** that reason, we always use the nRec value in the header.
1662**
1663** If the nRec value is 0xffffffff it means that nRec should be computed
1664** from the file size. This value is used when the user selects the
1665** no-sync option for the journal. A power failure could lead to corruption
1666** in this case. But for things like temporary table (which will be
1667** deleted when the power is restored) we don't care.
1668**
drhd9b02572001-04-15 00:37:09 +00001669** If the file opened as the journal file is not a well-formed
danielk1977ece80f12004-06-23 01:05:26 +00001670** journal file then all pages up to the first corrupted page are rolled
1671** back (or no pages if the journal header is corrupted). The journal file
1672** is then deleted and SQLITE_OK returned, just as if no corruption had
1673** been encountered.
1674**
1675** If an I/O or malloc() error occurs, the journal-file is not deleted
1676** and an error code is returned.
drhed7c8552001-04-11 14:29:21 +00001677*/
danielk1977e277be02007-03-23 18:12:06 +00001678static int pager_playback(Pager *pPager, int isHot){
danielk1977b4b47412007-08-17 15:53:36 +00001679 sqlite3_vfs *pVfs = pPager->pVfs;
drheb206252004-10-01 02:00:31 +00001680 i64 szJ; /* Size of the journal file in bytes */
danielk1977c3e8f5e2004-06-28 01:16:46 +00001681 u32 nRec; /* Number of Records in the journal */
drhd9b02572001-04-15 00:37:09 +00001682 int i; /* Loop counter */
1683 Pgno mxPg = 0; /* Size of the original file in pages */
drhae2b40c2004-06-09 19:03:54 +00001684 int rc; /* Result code of a subroutine */
danielk197713adf8a2004-06-03 16:08:41 +00001685 char *zMaster = 0; /* Name of master journal file if any */
drhed7c8552001-04-11 14:29:21 +00001686
drhc3a64ba2001-11-22 00:01:27 +00001687 /* Figure out how many records are in the journal. Abort early if
1688 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +00001689 */
drh8cfbf082001-09-19 13:22:39 +00001690 assert( pPager->journalOpen );
drh054889e2005-11-30 03:20:31 +00001691 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
danielk1977334cdb62007-03-26 08:05:12 +00001692 if( rc!=SQLITE_OK || szJ==0 ){
drhc3a64ba2001-11-22 00:01:27 +00001693 goto end_playback;
1694 }
drh240c5792004-02-08 00:40:52 +00001695
danielk197776572402004-06-25 02:38:54 +00001696 /* Read the master journal name from the journal, if it is present.
1697 ** If a master journal file name is specified, but the file is not
1698 ** present on disk, then the journal is not hot and does not need to be
1699 ** played back.
drh240c5792004-02-08 00:40:52 +00001700 */
drh9cbe6352005-11-29 03:13:21 +00001701 rc = readMasterJournal(pPager->jfd, &zMaster);
danielk197776572402004-06-25 02:38:54 +00001702 assert( rc!=SQLITE_DONE );
danielk1977b4b47412007-08-17 15:53:36 +00001703 if( rc!=SQLITE_OK
1704 || (zMaster && !sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS))
1705 ){
drh17435752007-08-16 04:30:38 +00001706 sqlite3_free(zMaster);
danielk197776572402004-06-25 02:38:54 +00001707 zMaster = 0;
1708 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
drhc3a64ba2001-11-22 00:01:27 +00001709 goto end_playback;
1710 }
danielk197776572402004-06-25 02:38:54 +00001711 pPager->journalOff = 0;
drhc3a64ba2001-11-22 00:01:27 +00001712
danielk197776572402004-06-25 02:38:54 +00001713 /* This loop terminates either when the readJournalHdr() call returns
1714 ** SQLITE_DONE or an IO error occurs. */
1715 while( 1 ){
drhae2b40c2004-06-09 19:03:54 +00001716
danielk197776572402004-06-25 02:38:54 +00001717 /* Read the next journal header from the journal file. If there are
1718 ** not enough bytes left in the journal file for a complete header, or
1719 ** it is corrupted, then a process must of failed while writing it.
1720 ** This indicates nothing more needs to be rolled back.
1721 */
1722 rc = readJournalHdr(pPager, szJ, &nRec, &mxPg);
1723 if( rc!=SQLITE_OK ){
drh968af522003-02-11 14:55:40 +00001724 if( rc==SQLITE_DONE ){
drh968af522003-02-11 14:55:40 +00001725 rc = SQLITE_OK;
1726 }
danielk197776572402004-06-25 02:38:54 +00001727 goto end_playback;
1728 }
1729
1730 /* If nRec is 0xffffffff, then this journal was created by a process
1731 ** working in no-sync mode. This means that the rest of the journal
1732 ** file consists of pages, there are no more journal headers. Compute
1733 ** the value of nRec based on this assumption.
1734 */
1735 if( nRec==0xffffffff ){
1736 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
1737 nRec = (szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager);
1738 }
1739
danielk1977e277be02007-03-23 18:12:06 +00001740 /* If nRec is 0 and this rollback is of a transaction created by this
drh8940f4e2007-08-11 00:26:20 +00001741 ** process and if this is the final header in the journal, then it means
1742 ** that this part of the journal was being filled but has not yet been
1743 ** synced to disk. Compute the number of pages based on the remaining
1744 ** size of the file.
1745 **
1746 ** The third term of the test was added to fix ticket #2565.
danielk1977e277be02007-03-23 18:12:06 +00001747 */
drh8940f4e2007-08-11 00:26:20 +00001748 if( nRec==0 && !isHot &&
1749 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
danielk1977e277be02007-03-23 18:12:06 +00001750 nRec = (szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager);
1751 }
1752
danielk197776572402004-06-25 02:38:54 +00001753 /* If this is the first header read from the journal, truncate the
1754 ** database file back to it's original size.
1755 */
danielk1977e180dd92007-04-05 17:15:52 +00001756 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
drhcb4c40b2004-08-18 19:09:43 +00001757 rc = pager_truncate(pPager, mxPg);
danielk197776572402004-06-25 02:38:54 +00001758 if( rc!=SQLITE_OK ){
1759 goto end_playback;
1760 }
danielk197776572402004-06-25 02:38:54 +00001761 }
1762
danielk197776572402004-06-25 02:38:54 +00001763 /* Copy original pages out of the journal and back into the database file.
1764 */
1765 for(i=0; i<nRec; i++){
danielk197762079062007-08-15 17:08:46 +00001766 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001767 if( rc!=SQLITE_OK ){
1768 if( rc==SQLITE_DONE ){
1769 rc = SQLITE_OK;
1770 pPager->journalOff = szJ;
1771 break;
1772 }else{
1773 goto end_playback;
1774 }
1775 }
drh968af522003-02-11 14:55:40 +00001776 }
drhed7c8552001-04-11 14:29:21 +00001777 }
drh580eeaf2006-02-24 03:09:37 +00001778 /*NOTREACHED*/
1779 assert( 0 );
drh4a0681e2003-02-13 01:58:20 +00001780
1781end_playback:
danielk19778191bff2004-06-28 04:52:30 +00001782 if( rc==SQLITE_OK ){
drh80e35f42007-03-30 14:06:34 +00001783 rc = pager_end_transaction(pPager);
danielk19778191bff2004-06-28 04:52:30 +00001784 }
danielk197713adf8a2004-06-03 16:08:41 +00001785 if( zMaster ){
danielk1977979f38e2007-03-27 16:19:51 +00001786 /* If there was a master journal and this routine will return success,
danielk197732554c12005-01-22 03:39:39 +00001787 ** see if it is possible to delete the master journal.
danielk197713adf8a2004-06-03 16:08:41 +00001788 */
1789 if( rc==SQLITE_OK ){
danielk1977b4b47412007-08-17 15:53:36 +00001790 rc = pager_delmaster(pPager, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001791 }
drh17435752007-08-16 04:30:38 +00001792 sqlite3_free(zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001793 }
danielk197776572402004-06-25 02:38:54 +00001794
1795 /* The Pager.sectorSize variable may have been updated while rolling
drh3ceeb752007-03-29 18:19:52 +00001796 ** back a journal created by a process with a different sector size
danielk197776572402004-06-25 02:38:54 +00001797 ** value. Reset it to the correct value for this process.
1798 */
drhc80f0582007-05-01 16:59:48 +00001799 setSectorSize(pPager);
drhd9b02572001-04-15 00:37:09 +00001800 return rc;
drhed7c8552001-04-11 14:29:21 +00001801}
1802
1803/*
drhac69b052004-05-12 13:30:07 +00001804** Playback the statement journal.
drhfa86c412002-02-02 15:01:15 +00001805**
1806** This is similar to playing back the transaction journal but with
1807** a few extra twists.
1808**
drh663fc632002-02-02 18:49:19 +00001809** (1) The number of pages in the database file at the start of
drhac69b052004-05-12 13:30:07 +00001810** the statement is stored in pPager->stmtSize, not in the
drh663fc632002-02-02 18:49:19 +00001811** journal file itself.
drhfa86c412002-02-02 15:01:15 +00001812**
drhac69b052004-05-12 13:30:07 +00001813** (2) In addition to playing back the statement journal, also
drhfa86c412002-02-02 15:01:15 +00001814** playback all pages of the transaction journal beginning
drhac69b052004-05-12 13:30:07 +00001815** at offset pPager->stmtJSize.
drhfa86c412002-02-02 15:01:15 +00001816*/
drh3aac2dd2004-04-26 14:10:20 +00001817static int pager_stmt_playback(Pager *pPager){
drheb206252004-10-01 02:00:31 +00001818 i64 szJ; /* Size of the full journal */
1819 i64 hdrOff;
drh968af522003-02-11 14:55:40 +00001820 int nRec; /* Number of Records */
drhfa86c412002-02-02 15:01:15 +00001821 int i; /* Loop counter */
1822 int rc;
1823
danielk197776572402004-06-25 02:38:54 +00001824 szJ = pPager->journalOff;
1825#ifndef NDEBUG
1826 {
drheb206252004-10-01 02:00:31 +00001827 i64 os_szJ;
drh054889e2005-11-30 03:20:31 +00001828 rc = sqlite3OsFileSize(pPager->jfd, &os_szJ);
danielk197776572402004-06-25 02:38:54 +00001829 if( rc!=SQLITE_OK ) return rc;
1830 assert( szJ==os_szJ );
1831 }
1832#endif
1833
danielk19774099f6e2007-03-19 11:25:20 +00001834 /* Set hdrOff to be the offset just after the end of the last journal
1835 ** page written before the first journal-header for this statement
1836 ** transaction was written, or the end of the file if no journal
danielk197776572402004-06-25 02:38:54 +00001837 ** header was written.
1838 */
1839 hdrOff = pPager->stmtHdrOff;
1840 assert( pPager->fullSync || !hdrOff );
1841 if( !hdrOff ){
1842 hdrOff = szJ;
1843 }
1844
drhfa86c412002-02-02 15:01:15 +00001845 /* Truncate the database back to its original size.
1846 */
danielk1977e180dd92007-04-05 17:15:52 +00001847 rc = pager_truncate(pPager, pPager->stmtSize);
drh1aa2d8b2007-01-03 15:34:29 +00001848 assert( pPager->state>=PAGER_SHARED );
drhfa86c412002-02-02 15:01:15 +00001849
drhac69b052004-05-12 13:30:07 +00001850 /* Figure out how many records are in the statement journal.
drhfa86c412002-02-02 15:01:15 +00001851 */
drhac69b052004-05-12 13:30:07 +00001852 assert( pPager->stmtInUse && pPager->journalOpen );
drhac69b052004-05-12 13:30:07 +00001853 nRec = pPager->stmtNRec;
drhfa86c412002-02-02 15:01:15 +00001854
drhac69b052004-05-12 13:30:07 +00001855 /* Copy original pages out of the statement journal and back into the
drhae2b40c2004-06-09 19:03:54 +00001856 ** database file. Note that the statement journal omits checksums from
1857 ** each record since power-failure recovery is not important to statement
1858 ** journals.
drhfa86c412002-02-02 15:01:15 +00001859 */
danielk197762079062007-08-15 17:08:46 +00001860 for(i=0; i<nRec; i++){
1861 i64 offset = i*(4+pPager->pageSize);
1862 rc = pager_playback_one_page(pPager, pPager->stfd, offset, 0);
drh968af522003-02-11 14:55:40 +00001863 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001864 if( rc!=SQLITE_OK ) goto end_stmt_playback;
drhfa86c412002-02-02 15:01:15 +00001865 }
1866
danielk197776572402004-06-25 02:38:54 +00001867 /* Now roll some pages back from the transaction journal. Pager.stmtJSize
1868 ** was the size of the journal file when this statement was started, so
1869 ** everything after that needs to be rolled back, either into the
1870 ** database, the memory cache, or both.
1871 **
1872 ** If it is not zero, then Pager.stmtHdrOff is the offset to the start
1873 ** of the first journal header written during this statement transaction.
drhfa86c412002-02-02 15:01:15 +00001874 */
danielk197776572402004-06-25 02:38:54 +00001875 pPager->journalOff = pPager->stmtJSize;
danielk197775edc162004-06-26 01:48:18 +00001876 pPager->cksumInit = pPager->stmtCksum;
danielk19774099f6e2007-03-19 11:25:20 +00001877 while( pPager->journalOff < hdrOff ){
danielk197762079062007-08-15 17:08:46 +00001878 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001879 assert( rc!=SQLITE_DONE );
1880 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1881 }
1882
1883 while( pPager->journalOff < szJ ){
danielk1977f0113002006-01-24 12:09:17 +00001884 u32 nJRec; /* Number of Journal Records */
danielk197776572402004-06-25 02:38:54 +00001885 u32 dummy;
danielk1977f0113002006-01-24 12:09:17 +00001886 rc = readJournalHdr(pPager, szJ, &nJRec, &dummy);
drh968af522003-02-11 14:55:40 +00001887 if( rc!=SQLITE_OK ){
1888 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001889 goto end_stmt_playback;
drh968af522003-02-11 14:55:40 +00001890 }
danielk1977f0113002006-01-24 12:09:17 +00001891 if( nJRec==0 ){
1892 nJRec = (szJ - pPager->journalOff) / (pPager->pageSize+8);
danielk197775edc162004-06-26 01:48:18 +00001893 }
danielk1977f0113002006-01-24 12:09:17 +00001894 for(i=nJRec-1; i>=0 && pPager->journalOff < szJ; i--){
danielk197762079062007-08-15 17:08:46 +00001895 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001896 assert( rc!=SQLITE_DONE );
1897 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1898 }
drhfa86c412002-02-02 15:01:15 +00001899 }
danielk197776572402004-06-25 02:38:54 +00001900
1901 pPager->journalOff = szJ;
drhfa86c412002-02-02 15:01:15 +00001902
drh3aac2dd2004-04-26 14:10:20 +00001903end_stmt_playback:
danielk19778a7aea32006-01-23 15:25:48 +00001904 if( rc==SQLITE_OK) {
danielk197775edc162004-06-26 01:48:18 +00001905 pPager->journalOff = szJ;
1906 /* pager_reload_cache(pPager); */
drhfa86c412002-02-02 15:01:15 +00001907 }
1908 return rc;
1909}
1910
1911/*
drhf57b14a2001-09-14 18:54:08 +00001912** Change the maximum number of in-memory pages that are allowed.
1913*/
danielk19773b8a05f2007-03-19 17:44:26 +00001914void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
drhf57b14a2001-09-14 18:54:08 +00001915 if( mxPage>10 ){
1916 pPager->mxPage = mxPage;
danielk1977ef317ab2004-06-23 10:43:10 +00001917 }else{
1918 pPager->mxPage = 10;
drhf57b14a2001-09-14 18:54:08 +00001919 }
1920}
1921
1922/*
drh973b6e32003-02-12 14:09:42 +00001923** Adjust the robustness of the database to damage due to OS crashes
1924** or power failures by changing the number of syncs()s when writing
1925** the rollback journal. There are three levels:
1926**
drh054889e2005-11-30 03:20:31 +00001927** OFF sqlite3OsSync() is never called. This is the default
drh973b6e32003-02-12 14:09:42 +00001928** for temporary and transient files.
1929**
1930** NORMAL The journal is synced once before writes begin on the
1931** database. This is normally adequate protection, but
1932** it is theoretically possible, though very unlikely,
1933** that an inopertune power failure could leave the journal
1934** in a state which would cause damage to the database
1935** when it is rolled back.
1936**
1937** FULL The journal is synced twice before writes begin on the
drh34e79ce2004-02-08 06:05:46 +00001938** database (with some additional information - the nRec field
1939** of the journal header - being written in between the two
1940** syncs). If we assume that writing a
drh973b6e32003-02-12 14:09:42 +00001941** single disk sector is atomic, then this mode provides
1942** assurance that the journal will not be corrupted to the
1943** point of causing damage to the database during rollback.
1944**
1945** Numeric values associated with these states are OFF==1, NORMAL=2,
1946** and FULL=3.
1947*/
danielk197793758c82005-01-21 08:13:14 +00001948#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk19773b8a05f2007-03-19 17:44:26 +00001949void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int full_fsync){
drh973b6e32003-02-12 14:09:42 +00001950 pPager->noSync = level==1 || pPager->tempFile;
1951 pPager->fullSync = level==3 && !pPager->tempFile;
danielk1977f036aef2007-08-20 05:36:51 +00001952 pPager->sync_flags = (full_fsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL);
danielk19771d850a72004-05-31 08:26:49 +00001953 if( pPager->noSync ) pPager->needSync = 0;
drh973b6e32003-02-12 14:09:42 +00001954}
danielk197793758c82005-01-21 08:13:14 +00001955#endif
drh973b6e32003-02-12 14:09:42 +00001956
1957/*
drhaf6df112005-06-07 02:12:30 +00001958** The following global variable is incremented whenever the library
1959** attempts to open a temporary file. This information is used for
1960** testing and analysis only.
1961*/
drh0f7eb612006-08-08 13:51:43 +00001962#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00001963int sqlite3_opentemp_count = 0;
drh0f7eb612006-08-08 13:51:43 +00001964#endif
drhaf6df112005-06-07 02:12:30 +00001965
1966/*
drh3f56e6e2007-03-15 01:16:47 +00001967** Open a temporary file.
1968**
1969** Write the file descriptor into *fd. Return SQLITE_OK on success or some
danielk1977fee2d252007-08-18 10:59:19 +00001970** other error code if we fail. The OS will automatically delete the temporary
1971** file when it is closed.
drhfa86c412002-02-02 15:01:15 +00001972**
drhae28c012007-08-24 16:29:23 +00001973** If zFilename is 0, then an appropriate temporary filename is
1974** generated automatically and SQLITE_OPEN_SUBJOURNAL is passed to
1975** the OS layer as the file type.
1976**
1977** If zFilename is not 0, SQLITE_OPEN_TEMP_DB is passed as the file type.
drhfa86c412002-02-02 15:01:15 +00001978*/
danielk1977b4b47412007-08-17 15:53:36 +00001979static int sqlite3PagerOpentemp(
1980 sqlite3_vfs *pVfs,
1981 sqlite3_file *pFile,
drhae28c012007-08-24 16:29:23 +00001982 char *zFilename
danielk1977b4b47412007-08-17 15:53:36 +00001983){
drhfa86c412002-02-02 15:01:15 +00001984 int rc;
danielk1977863c0f92007-08-23 11:47:59 +00001985 int flags = (
1986 SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|
1987 SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_DELETEONCLOSE
1988 );
danielk1977b4b47412007-08-17 15:53:36 +00001989
1990 char *zFree = 0;
drhae28c012007-08-24 16:29:23 +00001991 if( zFilename==0 ){
danielk1977b4b47412007-08-17 15:53:36 +00001992 zFree = (char *)sqlite3_malloc(pVfs->mxPathname);
1993 if( !zFree ){
1994 return SQLITE_NOMEM;
1995 }
drhae28c012007-08-24 16:29:23 +00001996 zFilename = zFree;
danielk1977fee2d252007-08-18 10:59:19 +00001997 flags |= SQLITE_OPEN_SUBJOURNAL;
drhae28c012007-08-24 16:29:23 +00001998 rc = sqlite3OsGetTempName(pVfs, zFilename);
1999 if( rc!=SQLITE_OK ){
2000 sqlite3_free(zFree);
2001 return rc;
2002 }
danielk1977fee2d252007-08-18 10:59:19 +00002003 }else{
2004 flags |= SQLITE_OPEN_TEMP_DB;
danielk1977b4b47412007-08-17 15:53:36 +00002005 }
drh3f56e6e2007-03-15 01:16:47 +00002006
drh0f7eb612006-08-08 13:51:43 +00002007#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00002008 sqlite3_opentemp_count++; /* Used for testing and analysis only */
drh0f7eb612006-08-08 13:51:43 +00002009#endif
danielk1977b4b47412007-08-17 15:53:36 +00002010
drhae28c012007-08-24 16:29:23 +00002011 rc = sqlite3OsOpen(pVfs, zFilename, pFile, flags, 0);
2012 assert( rc!=SQLITE_OK || pFile->pMethods );
danielk1977b4b47412007-08-17 15:53:36 +00002013 sqlite3_free(zFree);
drhfa86c412002-02-02 15:01:15 +00002014 return rc;
2015}
2016
2017/*
drhed7c8552001-04-11 14:29:21 +00002018** Create a new page cache and put a pointer to the page cache in *ppPager.
drh5e00f6c2001-09-13 13:46:56 +00002019** The file to be cached need not exist. The file is not locked until
danielk19773b8a05f2007-03-19 17:44:26 +00002020** the first call to sqlite3PagerGet() and is only held open until the
2021** last page is released using sqlite3PagerUnref().
drh382c0242001-10-06 16:33:02 +00002022**
drh6446c4d2001-12-15 14:22:18 +00002023** If zFilename is NULL then a randomly-named temporary file is created
drh1cc8c442007-08-24 16:08:29 +00002024** and used as the file to be cached. The file will be deleted
drh6446c4d2001-12-15 14:22:18 +00002025** automatically when it is closed.
drh90f5ecb2004-07-22 01:19:35 +00002026**
2027** If zFilename is ":memory:" then all information is held in cache.
2028** It is never written to disk. This can be used to implement an
2029** in-memory database.
drhed7c8552001-04-11 14:29:21 +00002030*/
danielk19773b8a05f2007-03-19 17:44:26 +00002031int sqlite3PagerOpen(
drh86f8c192007-08-22 00:39:19 +00002032 sqlite3_vfs *pVfs, /* The virtual file system to use */
drh7e3b0a02001-04-28 16:52:40 +00002033 Pager **ppPager, /* Return the Pager structure here */
2034 const char *zFilename, /* Name of the database file to open */
drhda47d772002-12-02 04:25:19 +00002035 int nExtra, /* Extra bytes append to each in-memory page */
drh7bec5052005-02-06 02:45:41 +00002036 int flags /* flags controlling this file */
drh7e3b0a02001-04-28 16:52:40 +00002037){
danielk1977b4b47412007-08-17 15:53:36 +00002038 u8 *pPtr;
danielk1977aef0bf62005-12-30 16:28:01 +00002039 Pager *pPager = 0;
danielk1977cfe9a692004-06-16 12:00:29 +00002040 int rc = SQLITE_OK;
2041 int i;
danielk19778def5ea2004-06-16 10:39:52 +00002042 int tempFile = 0;
drhac69b052004-05-12 13:30:07 +00002043 int memDb = 0;
drh5e00f6c2001-09-13 13:46:56 +00002044 int readOnly = 0;
drh7bec5052005-02-06 02:45:41 +00002045 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0;
2046 int noReadlock = (flags & PAGER_NO_READLOCK)!=0;
danielk1977c7b60172007-08-22 11:22:03 +00002047 int journalFileSize = sqlite3JournalSize(pVfs);
drh1cc8c442007-08-24 16:08:29 +00002048 int nDefaultPage = SQLITE_DEFAULT_PAGE_SIZE;
2049 char *zPathname;
2050 int nPathname;
danielk1977b4b47412007-08-17 15:53:36 +00002051
drh86f8c192007-08-22 00:39:19 +00002052 /* The default return is a NULL pointer */
drhd9b02572001-04-15 00:37:09 +00002053 *ppPager = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00002054
drh1cc8c442007-08-24 16:08:29 +00002055 /* Compute the full pathname */
2056 zPathname = sqlite3_malloc(pVfs->mxPathname+1);
2057 if( zPathname==0 ){
2058 return SQLITE_NOMEM;
2059 }
2060 if( zFilename && zFilename[0] ){
2061#ifndef SQLITE_OMIT_MEMORYDB
2062 if( strcmp(zFilename,":memory:")==0 ){
2063 memDb = 1;
2064 zPathname[0] = 0;
2065 }else
2066#endif
2067 {
2068 rc = sqlite3OsFullPathname(pVfs, zFilename, zPathname);
drh1cc8c442007-08-24 16:08:29 +00002069 }
2070 }else{
drhae28c012007-08-24 16:29:23 +00002071 rc = sqlite3OsGetTempName(pVfs, zPathname);
2072 }
2073 if( rc!=SQLITE_OK ){
2074 sqlite3_free(zPathname);
2075 return rc;
drh1cc8c442007-08-24 16:08:29 +00002076 }
2077 nPathname = strlen(zPathname);
2078
danielk1977b4b47412007-08-17 15:53:36 +00002079 /* Allocate memory for the pager structure */
2080 pPager = sqlite3MallocZero(
2081 sizeof(*pPager) + /* Pager structure */
danielk1977c7b60172007-08-22 11:22:03 +00002082 journalFileSize + /* The journal file structure */
2083 pVfs->szOsFile * 2 + /* The db and stmt journal files */
drh1cc8c442007-08-24 16:08:29 +00002084 nPathname * 3 + 30 /* zFilename, zDirectory, zJournal */
danielk1977b4b47412007-08-17 15:53:36 +00002085 );
2086 if( !pPager ){
drh1cc8c442007-08-24 16:08:29 +00002087 sqlite3_free(zPathname);
danielk1977b4b47412007-08-17 15:53:36 +00002088 return SQLITE_NOMEM;
2089 }
2090 pPtr = (u8 *)&pPager[1];
2091 pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0];
danielk1977c7b60172007-08-22 11:22:03 +00002092 pPager->stfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*1];
2093 pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*2];
2094 pPager->zFilename = (char*)&pPtr[pVfs->szOsFile*2+journalFileSize];
drh1cc8c442007-08-24 16:08:29 +00002095 pPager->zDirectory = &pPager->zFilename[nPathname+1];
2096 pPager->zJournal = &pPager->zDirectory[nPathname+1];
danielk1977b4b47412007-08-17 15:53:36 +00002097 pPager->pVfs = pVfs;
drh1cc8c442007-08-24 16:08:29 +00002098 memcpy(pPager->zFilename, zPathname, nPathname+1);
2099 sqlite3_free(zPathname);
danielk1977b4b47412007-08-17 15:53:36 +00002100
drh153c62c2007-08-24 03:51:33 +00002101 /* Open the pager file.
danielk1977aef0bf62005-12-30 16:28:01 +00002102 */
drhae28c012007-08-24 16:29:23 +00002103 if( zFilename && zFilename[0] && !memDb ){
drh1cc8c442007-08-24 16:08:29 +00002104 if( nPathname>(pVfs->mxPathname - sizeof("-journal")) ){
2105 rc = SQLITE_CANTOPEN;
2106 }else{
2107/*** FIXME: Might need to be SQLITE_OPEN_TEMP_DB. Need to pass in
2108**** a flag from higher up.
2109****/
2110 int oflag =
2111 (SQLITE_OPEN_READWRITE|SQLITE_OPEN_CREATE|SQLITE_OPEN_MAIN_DB);
2112 int fout = 0;
2113 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd, oflag, &fout);
2114 readOnly = (fout&SQLITE_OPEN_READONLY);
2115
2116 /* If the file was successfully opened for read/write access,
2117 ** choose a default page size in case we have to create the
2118 ** database file. The default page size is the maximum of:
2119 **
2120 ** + SQLITE_DEFAULT_PAGE_SIZE,
2121 ** + The value returned by sqlite3OsSectorSize()
2122 ** + The largest page size that can be written atomically.
2123 */
2124 if( rc==SQLITE_OK && !readOnly ){
2125 int iSectorSize = sqlite3OsSectorSize(pPager->fd);
2126 if( nDefaultPage<iSectorSize ){
2127 nDefaultPage = iSectorSize;
2128 }
danielk19779663b8f2007-08-24 11:52:28 +00002129#ifdef SQLITE_ENABLE_ATOMIC_WRITE
drh1cc8c442007-08-24 16:08:29 +00002130 {
2131 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
2132 int ii;
2133 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
2134 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
2135 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
2136 for(ii=nDefaultPage; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
2137 if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ) nDefaultPage = ii;
danielk19779663b8f2007-08-24 11:52:28 +00002138 }
danielk1977b4b47412007-08-17 15:53:36 +00002139 }
drh1cc8c442007-08-24 16:08:29 +00002140#endif
2141 if( nDefaultPage>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
2142 nDefaultPage = SQLITE_MAX_DEFAULT_PAGE_SIZE;
2143 }
danielk19778def5ea2004-06-16 10:39:52 +00002144 }
drhac69b052004-05-12 13:30:07 +00002145 }
drh1cc8c442007-08-24 16:08:29 +00002146 }else if( !memDb ){
danielk19777a2b1ee2007-08-21 14:27:01 +00002147 /* If a temporary file is requested, it is not opened immediately.
2148 ** In this case we accept the default page size and delay actually
2149 ** opening the file until the first call to OsWrite().
2150 */
danielk19777a2b1ee2007-08-21 14:27:01 +00002151 tempFile = 1;
2152 pPager->state = PAGER_EXCLUSIVE;
drh5e00f6c2001-09-13 13:46:56 +00002153 }
danielk1977aef0bf62005-12-30 16:28:01 +00002154
danielk1977b4b47412007-08-17 15:53:36 +00002155 if( pPager && rc==SQLITE_OK ){
danielk19779663b8f2007-08-24 11:52:28 +00002156 pPager->pTmpSpace = (char *)sqlite3_malloc(nDefaultPage);
drh3e7a6092002-12-07 21:45:14 +00002157 }
danielk1977aef0bf62005-12-30 16:28:01 +00002158
drh153c62c2007-08-24 03:51:33 +00002159 /* If an error occured in either of the blocks above.
2160 ** Free the Pager structure and close the file.
2161 ** Since the pager is not allocated there is no need to set
danielk1977aef0bf62005-12-30 16:28:01 +00002162 ** any Pager.errMask variables.
2163 */
danielk1977b4b47412007-08-17 15:53:36 +00002164 if( !pPager || !pPager->pTmpSpace ){
2165 sqlite3OsClose(pPager->fd);
drh17435752007-08-16 04:30:38 +00002166 sqlite3_free(pPager);
danielk1977aef0bf62005-12-30 16:28:01 +00002167 return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc);
drhed7c8552001-04-11 14:29:21 +00002168 }
danielk1977aef0bf62005-12-30 16:28:01 +00002169
drh153c62c2007-08-24 03:51:33 +00002170 PAGERTRACE3("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename);
2171 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
danielk1977aef0bf62005-12-30 16:28:01 +00002172
danielk1977b4b47412007-08-17 15:53:36 +00002173 /* Fill in Pager.zDirectory[] */
drh1cc8c442007-08-24 16:08:29 +00002174 memcpy(pPager->zDirectory, pPager->zFilename, nPathname+1);
danielk1977b4b47412007-08-17 15:53:36 +00002175 for(i=strlen(pPager->zDirectory); i>0 && pPager->zDirectory[i-1]!='/'; i--){}
drha76c82e2003-07-27 18:59:42 +00002176 if( i>0 ) pPager->zDirectory[i-1] = 0;
danielk1977b4b47412007-08-17 15:53:36 +00002177
2178 /* Fill in Pager.zJournal[] */
drh1cc8c442007-08-24 16:08:29 +00002179 memcpy(pPager->zJournal, pPager->zFilename, nPathname);
2180 memcpy(&pPager->zJournal[nPathname], "-journal", 9);
danielk1977b4b47412007-08-17 15:53:36 +00002181
drh3b59a5c2006-01-15 20:28:28 +00002182 /* pPager->journalOpen = 0; */
drhac69b052004-05-12 13:30:07 +00002183 pPager->useJournal = useJournal && !memDb;
drh7bec5052005-02-06 02:45:41 +00002184 pPager->noReadlock = noReadlock && readOnly;
drh3b59a5c2006-01-15 20:28:28 +00002185 /* pPager->stmtOpen = 0; */
2186 /* pPager->stmtInUse = 0; */
2187 /* pPager->nRef = 0; */
drhac69b052004-05-12 13:30:07 +00002188 pPager->dbSize = memDb-1;
danielk19779663b8f2007-08-24 11:52:28 +00002189 pPager->pageSize = nDefaultPage;
drh3b59a5c2006-01-15 20:28:28 +00002190 /* pPager->stmtSize = 0; */
2191 /* pPager->stmtJSize = 0; */
2192 /* pPager->nPage = 0; */
drh90f5ecb2004-07-22 01:19:35 +00002193 pPager->mxPage = 100;
drhf8e632b2007-05-08 14:51:36 +00002194 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
drh3b59a5c2006-01-15 20:28:28 +00002195 /* pPager->state = PAGER_UNLOCK; */
drh1cc8c442007-08-24 16:08:29 +00002196 assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
drh3b59a5c2006-01-15 20:28:28 +00002197 /* pPager->errMask = 0; */
drh5e00f6c2001-09-13 13:46:56 +00002198 pPager->tempFile = tempFile;
drh369339d2007-03-30 16:01:55 +00002199 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
2200 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
2201 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
2202 pPager->exclusiveMode = tempFile;
drhac69b052004-05-12 13:30:07 +00002203 pPager->memDb = memDb;
drh5e00f6c2001-09-13 13:46:56 +00002204 pPager->readOnly = readOnly;
drh3b59a5c2006-01-15 20:28:28 +00002205 /* pPager->needSync = 0; */
drhda47d772002-12-02 04:25:19 +00002206 pPager->noSync = pPager->tempFile || !useJournal;
danielk197775edc162004-06-26 01:48:18 +00002207 pPager->fullSync = (pPager->noSync?0:1);
danielk1977f036aef2007-08-20 05:36:51 +00002208 pPager->sync_flags = SQLITE_SYNC_NORMAL;
drh3b59a5c2006-01-15 20:28:28 +00002209 /* pPager->pFirst = 0; */
2210 /* pPager->pFirstSynced = 0; */
2211 /* pPager->pLast = 0; */
drh887dc4c2004-10-22 16:22:57 +00002212 pPager->nExtra = FORCE_ALIGNMENT(nExtra);
danielk19777a2b1ee2007-08-21 14:27:01 +00002213 assert(pPager->fd->pMethods||memDb||tempFile);
danielk1977b4721172007-03-19 05:54:48 +00002214 if( !memDb ){
drhc80f0582007-05-01 16:59:48 +00002215 setSectorSize(pPager);
danielk1977b4721172007-03-19 05:54:48 +00002216 }
drh3b59a5c2006-01-15 20:28:28 +00002217 /* pPager->pBusyHandler = 0; */
2218 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
drhed7c8552001-04-11 14:29:21 +00002219 *ppPager = pPager;
drh6f7adc82006-01-11 21:41:20 +00002220#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +00002221 pPager->iInUseMM = 0;
2222 pPager->iInUseDB = 0;
2223 if( !memDb ){
2224 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
2225 sqlite3_mutex_enter(mutex);
2226 pPager->pNext = sqlite3PagerList;
2227 if( sqlite3PagerList ){
2228 assert( sqlite3PagerList->pPrev==0 );
2229 sqlite3PagerList->pPrev = pPager;
2230 }
2231 pPager->pPrev = 0;
2232 sqlite3PagerList = pPager;
2233 sqlite3_mutex_leave(mutex);
2234 }
danielk197752622822006-01-09 09:59:49 +00002235#endif
drhed7c8552001-04-11 14:29:21 +00002236 return SQLITE_OK;
2237}
2238
2239/*
drh90f5ecb2004-07-22 01:19:35 +00002240** Set the busy handler function.
2241*/
danielk19773b8a05f2007-03-19 17:44:26 +00002242void sqlite3PagerSetBusyhandler(Pager *pPager, BusyHandler *pBusyHandler){
drh90f5ecb2004-07-22 01:19:35 +00002243 pPager->pBusyHandler = pBusyHandler;
2244}
2245
2246/*
drh72f82862001-05-24 21:06:34 +00002247** Set the destructor for this pager. If not NULL, the destructor is called
drh5e00f6c2001-09-13 13:46:56 +00002248** when the reference count on each page reaches zero. The destructor can
2249** be used to clean up information in the extra segment appended to each page.
drh72f82862001-05-24 21:06:34 +00002250**
danielk19773b8a05f2007-03-19 17:44:26 +00002251** The destructor is not called as a result sqlite3PagerClose().
2252** Destructors are only called by sqlite3PagerUnref().
drh72f82862001-05-24 21:06:34 +00002253*/
danielk19773b8a05f2007-03-19 17:44:26 +00002254void sqlite3PagerSetDestructor(Pager *pPager, void (*xDesc)(DbPage*,int)){
drh72f82862001-05-24 21:06:34 +00002255 pPager->xDestructor = xDesc;
2256}
2257
2258/*
drha6abd042004-06-09 17:37:22 +00002259** Set the reinitializer for this pager. If not NULL, the reinitializer
2260** is called when the content of a page in cache is restored to its original
2261** value as a result of a rollback. The callback gives higher-level code
2262** an opportunity to restore the EXTRA section to agree with the restored
2263** page data.
2264*/
danielk19773b8a05f2007-03-19 17:44:26 +00002265void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*,int)){
drha6abd042004-06-09 17:37:22 +00002266 pPager->xReiniter = xReinit;
2267}
2268
2269/*
danielk1977a1644fd2007-08-29 12:31:25 +00002270** Set the page size to *pPageSize. If the suggest new page size is
2271** inappropriate, then an alternative page size is set to that
2272** value before returning.
drh90f5ecb2004-07-22 01:19:35 +00002273*/
danielk1977a1644fd2007-08-29 12:31:25 +00002274int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){
2275 int rc = SQLITE_OK;
2276 u16 pageSize = *pPageSize;
danielk19779663b8f2007-08-24 11:52:28 +00002277 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
danielk1977a1644fd2007-08-29 12:31:25 +00002278 if( pageSize && pageSize!=pPager->pageSize
2279 && !pPager->memDb && pPager->nRef==0
2280 ){
2281 char *pNew = (char *)sqlite3_malloc(pageSize);
2282 if( !pNew ){
2283 rc = SQLITE_NOMEM;
2284 }else{
2285 pagerEnter(pPager);
2286 pager_reset(pPager);
2287 pPager->pageSize = pageSize;
2288 setSectorSize(pPager);
2289 sqlite3_free(pPager->pTmpSpace);
2290 pPager->pTmpSpace = pNew;
2291 pagerLeave(pPager);
2292 }
drh1c7880e2005-05-20 20:01:55 +00002293 }
danielk1977a1644fd2007-08-29 12:31:25 +00002294 *pPageSize = pPager->pageSize;
2295 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002296}
2297
2298/*
drhf8e632b2007-05-08 14:51:36 +00002299** Attempt to set the maximum database page count if mxPage is positive.
2300** Make no changes if mxPage is zero or negative. And never reduce the
2301** maximum page count below the current size of the database.
2302**
2303** Regardless of mxPage, return the current maximum page count.
2304*/
2305int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
2306 if( mxPage>0 ){
2307 pPager->mxPgno = mxPage;
2308 }
2309 sqlite3PagerPagecount(pPager);
2310 return pPager->mxPgno;
2311}
2312
2313/*
drhc9ac5ca2005-11-04 22:03:30 +00002314** The following set of routines are used to disable the simulated
2315** I/O error mechanism. These routines are used to avoid simulated
2316** errors in places where we do not care about errors.
2317**
2318** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
2319** and generate no code.
2320*/
2321#ifdef SQLITE_TEST
2322extern int sqlite3_io_error_pending;
2323extern int sqlite3_io_error_hit;
2324static int saved_cnt;
drhc9ac5ca2005-11-04 22:03:30 +00002325void disable_simulated_io_errors(void){
2326 saved_cnt = sqlite3_io_error_pending;
2327 sqlite3_io_error_pending = -1;
2328}
2329void enable_simulated_io_errors(void){
2330 sqlite3_io_error_pending = saved_cnt;
2331}
2332#else
drh152410f2005-11-05 15:11:22 +00002333# define disable_simulated_io_errors()
2334# define enable_simulated_io_errors()
drhc9ac5ca2005-11-04 22:03:30 +00002335#endif
2336
2337/*
drh90f5ecb2004-07-22 01:19:35 +00002338** Read the first N bytes from the beginning of the file into memory
danielk1977aef0bf62005-12-30 16:28:01 +00002339** that pDest points to.
2340**
2341** No error checking is done. The rational for this is that this function
2342** may be called even if the file does not exist or contain a header. In
2343** these cases sqlite3OsRead() will return an error, to which the correct
2344** response is to zero the memory at pDest and continue. A real IO error
2345** will presumably recur and be picked up later (Todo: Think about this).
drh90f5ecb2004-07-22 01:19:35 +00002346*/
danielk19773b8a05f2007-03-19 17:44:26 +00002347int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
drh551b7732006-11-06 21:20:25 +00002348 int rc = SQLITE_OK;
drh90f5ecb2004-07-22 01:19:35 +00002349 memset(pDest, 0, N);
danielk19777a2b1ee2007-08-21 14:27:01 +00002350 assert(MEMDB||pPager->fd->pMethods||pPager->tempFile);
2351 if( pPager->fd->pMethods ){
drhb0603412007-02-28 04:47:26 +00002352 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
danielk197762079062007-08-15 17:08:46 +00002353 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
drh551b7732006-11-06 21:20:25 +00002354 if( rc==SQLITE_IOERR_SHORT_READ ){
2355 rc = SQLITE_OK;
2356 }
drh90f5ecb2004-07-22 01:19:35 +00002357 }
drh551b7732006-11-06 21:20:25 +00002358 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002359}
2360
2361/*
drh5e00f6c2001-09-13 13:46:56 +00002362** Return the total number of pages in the disk file associated with
danielk197715f411d2005-09-16 10:18:45 +00002363** pPager.
2364**
2365** If the PENDING_BYTE lies on the page directly after the end of the
2366** file, then consider this page part of the file too. For example, if
2367** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the
2368** file is 4096 bytes, 5 is returned instead of 4.
drhed7c8552001-04-11 14:29:21 +00002369*/
danielk19773b8a05f2007-03-19 17:44:26 +00002370int sqlite3PagerPagecount(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00002371 i64 n = 0;
drhe49f9822006-09-15 12:29:16 +00002372 int rc;
drhd9b02572001-04-15 00:37:09 +00002373 assert( pPager!=0 );
drha7aea3d2007-03-15 12:51:16 +00002374 if( pPager->errCode ){
2375 return 0;
2376 }
drhed7c8552001-04-11 14:29:21 +00002377 if( pPager->dbSize>=0 ){
danielk197715f411d2005-09-16 10:18:45 +00002378 n = pPager->dbSize;
2379 } else {
danielk19777a2b1ee2007-08-21 14:27:01 +00002380 assert(pPager->fd->pMethods||pPager->tempFile);
2381 if( (pPager->fd->pMethods)
2382 && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){
drhe49f9822006-09-15 12:29:16 +00002383 pager_error(pPager, rc);
danielk197715f411d2005-09-16 10:18:45 +00002384 return 0;
2385 }
2386 if( n>0 && n<pPager->pageSize ){
2387 n = 1;
2388 }else{
2389 n /= pPager->pageSize;
2390 }
2391 if( pPager->state!=PAGER_UNLOCK ){
2392 pPager->dbSize = n;
2393 }
drhed7c8552001-04-11 14:29:21 +00002394 }
danielk197715f411d2005-09-16 10:18:45 +00002395 if( n==(PENDING_BYTE/pPager->pageSize) ){
drh1f595712004-06-15 01:40:29 +00002396 n++;
2397 }
drhf8e632b2007-05-08 14:51:36 +00002398 if( n>pPager->mxPgno ){
2399 pPager->mxPgno = n;
2400 }
drhed7c8552001-04-11 14:29:21 +00002401 return n;
2402}
2403
drhf07e7d52006-04-07 13:54:46 +00002404
2405#ifndef SQLITE_OMIT_MEMORYDB
2406/*
2407** Clear a PgHistory block
2408*/
2409static void clearHistory(PgHistory *pHist){
drh17435752007-08-16 04:30:38 +00002410 sqlite3_free(pHist->pOrig);
2411 sqlite3_free(pHist->pStmt);
drhf07e7d52006-04-07 13:54:46 +00002412 pHist->pOrig = 0;
2413 pHist->pStmt = 0;
2414}
2415#else
2416#define clearHistory(x)
2417#endif
2418
drhed7c8552001-04-11 14:29:21 +00002419/*
drhf7c57532003-04-25 13:22:51 +00002420** Forward declaration
2421*/
danielk197776572402004-06-25 02:38:54 +00002422static int syncJournal(Pager*);
drhac69b052004-05-12 13:30:07 +00002423
2424/*
danielk1977f5fdda82004-11-03 08:44:05 +00002425** Unlink pPg from it's hash chain. Also set the page number to 0 to indicate
2426** that the page is not part of any hash chain. This is required because the
danielk19773b8a05f2007-03-19 17:44:26 +00002427** sqlite3PagerMovepage() routine can leave a page in the
danielk1977f5fdda82004-11-03 08:44:05 +00002428** pNextFree/pPrevFree list that is not a part of any hash-chain.
2429*/
2430static void unlinkHashChain(Pager *pPager, PgHdr *pPg){
2431 if( pPg->pgno==0 ){
drh3765df42006-06-28 18:18:09 +00002432 assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
danielk1977f5fdda82004-11-03 08:44:05 +00002433 return;
2434 }
2435 if( pPg->pNextHash ){
2436 pPg->pNextHash->pPrevHash = pPg->pPrevHash;
2437 }
2438 if( pPg->pPrevHash ){
drh8ca0c722006-05-07 17:49:38 +00002439 assert( pPager->aHash[pPg->pgno & (pPager->nHash-1)]!=pPg );
danielk1977f5fdda82004-11-03 08:44:05 +00002440 pPg->pPrevHash->pNextHash = pPg->pNextHash;
2441 }else{
drh8ca0c722006-05-07 17:49:38 +00002442 int h = pPg->pgno & (pPager->nHash-1);
danielk1977f5fdda82004-11-03 08:44:05 +00002443 pPager->aHash[h] = pPg->pNextHash;
2444 }
drh5229ae42006-03-23 23:29:04 +00002445 if( MEMDB ){
drh5229ae42006-03-23 23:29:04 +00002446 clearHistory(PGHDR_TO_HIST(pPg, pPager));
2447 }
danielk1977f5fdda82004-11-03 08:44:05 +00002448 pPg->pgno = 0;
2449 pPg->pNextHash = pPg->pPrevHash = 0;
2450}
2451
2452/*
drhac69b052004-05-12 13:30:07 +00002453** Unlink a page from the free list (the list of all pages where nRef==0)
2454** and from its hash collision chain.
2455*/
2456static void unlinkPage(PgHdr *pPg){
2457 Pager *pPager = pPg->pPager;
2458
danielk19779f61c2f2007-08-27 17:27:49 +00002459 /* Unlink from free page list */
2460 lruListRemove(pPg);
drhac69b052004-05-12 13:30:07 +00002461
2462 /* Unlink from the pgno hash table */
danielk1977f5fdda82004-11-03 08:44:05 +00002463 unlinkHashChain(pPager, pPg);
drhac69b052004-05-12 13:30:07 +00002464}
2465
2466/*
danielk1977e180dd92007-04-05 17:15:52 +00002467** This routine is used to truncate the cache when a database
2468** is truncated. Drop from the cache all pages whose pgno is
2469** larger than pPager->dbSize and is unreferenced.
2470**
drhac69b052004-05-12 13:30:07 +00002471** Referenced pages larger than pPager->dbSize are zeroed.
danielk1977e180dd92007-04-05 17:15:52 +00002472**
2473** Actually, at the point this routine is called, it would be
2474** an error to have a referenced page. But rather than delete
2475** that page and guarantee a subsequent segfault, it seems better
2476** to zero it and hope that we error out sanely.
drhac69b052004-05-12 13:30:07 +00002477*/
danielk1977e180dd92007-04-05 17:15:52 +00002478static void pager_truncate_cache(Pager *pPager){
drhac69b052004-05-12 13:30:07 +00002479 PgHdr *pPg;
2480 PgHdr **ppPg;
2481 int dbSize = pPager->dbSize;
2482
2483 ppPg = &pPager->pAll;
2484 while( (pPg = *ppPg)!=0 ){
2485 if( pPg->pgno<=dbSize ){
2486 ppPg = &pPg->pNextAll;
2487 }else if( pPg->nRef>0 ){
2488 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
2489 ppPg = &pPg->pNextAll;
2490 }else{
2491 *ppPg = pPg->pNextAll;
drh538f5702007-04-13 02:14:30 +00002492 IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
2493 PAGER_INCR(sqlite3_pager_pgfree_count);
drhac69b052004-05-12 13:30:07 +00002494 unlinkPage(pPg);
drh605ad8f2006-05-03 23:34:05 +00002495 makeClean(pPg);
drh17435752007-08-16 04:30:38 +00002496 sqlite3_free(pPg);
drhac69b052004-05-12 13:30:07 +00002497 pPager->nPage--;
2498 }
2499 }
2500}
2501
drhf7c57532003-04-25 13:22:51 +00002502/*
danielk197717221812005-02-15 03:38:05 +00002503** Try to obtain a lock on a file. Invoke the busy callback if the lock
drha4afb652005-07-09 02:16:02 +00002504** is currently not available. Repeat until the busy callback returns
danielk197717221812005-02-15 03:38:05 +00002505** false or until the lock succeeds.
2506**
2507** Return SQLITE_OK on success and an error code if we cannot obtain
2508** the lock.
2509*/
2510static int pager_wait_on_lock(Pager *pPager, int locktype){
2511 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002512
2513 /* The OS lock values must be the same as the Pager lock values */
danielk197717221812005-02-15 03:38:05 +00002514 assert( PAGER_SHARED==SHARED_LOCK );
2515 assert( PAGER_RESERVED==RESERVED_LOCK );
2516 assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
drh1aa2d8b2007-01-03 15:34:29 +00002517
2518 /* If the file is currently unlocked then the size must be unknown */
2519 assert( pPager->state>=PAGER_SHARED || pPager->dbSize<0 || MEMDB );
2520
danielk197717221812005-02-15 03:38:05 +00002521 if( pPager->state>=locktype ){
2522 rc = SQLITE_OK;
2523 }else{
danielk197717221812005-02-15 03:38:05 +00002524 do {
drh054889e2005-11-30 03:20:31 +00002525 rc = sqlite3OsLock(pPager->fd, locktype);
drha4afb652005-07-09 02:16:02 +00002526 }while( rc==SQLITE_BUSY && sqlite3InvokeBusyHandler(pPager->pBusyHandler) );
danielk197717221812005-02-15 03:38:05 +00002527 if( rc==SQLITE_OK ){
2528 pPager->state = locktype;
drhb0603412007-02-28 04:47:26 +00002529 IOTRACE(("LOCK %p %d\n", pPager, locktype))
danielk197717221812005-02-15 03:38:05 +00002530 }
2531 }
2532 return rc;
2533}
2534
2535/*
drhf7c57532003-04-25 13:22:51 +00002536** Truncate the file to the number of pages specified.
2537*/
danielk19773b8a05f2007-03-19 17:44:26 +00002538int sqlite3PagerTruncate(Pager *pPager, Pgno nPage){
drhf7c57532003-04-25 13:22:51 +00002539 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002540 assert( pPager->state>=PAGER_SHARED || MEMDB );
danielk19773b8a05f2007-03-19 17:44:26 +00002541 sqlite3PagerPagecount(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00002542 if( pPager->errCode ){
2543 rc = pPager->errCode;
drh2e6d11b2003-04-25 15:37:57 +00002544 return rc;
2545 }
drh7d02cb72003-06-04 16:24:39 +00002546 if( nPage>=(unsigned)pPager->dbSize ){
drhf7c57532003-04-25 13:22:51 +00002547 return SQLITE_OK;
2548 }
drhb7f91642004-10-31 02:22:47 +00002549 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00002550 pPager->dbSize = nPage;
danielk1977e180dd92007-04-05 17:15:52 +00002551 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00002552 return SQLITE_OK;
2553 }
drh86f8c192007-08-22 00:39:19 +00002554 pagerEnter(pPager);
danielk197776572402004-06-25 02:38:54 +00002555 rc = syncJournal(pPager);
drh86f8c192007-08-22 00:39:19 +00002556 pagerLeave(pPager);
danielk1977369f27e2004-06-15 11:40:04 +00002557 if( rc!=SQLITE_OK ){
2558 return rc;
2559 }
danielk197717221812005-02-15 03:38:05 +00002560
2561 /* Get an exclusive lock on the database before truncating. */
drh86f8c192007-08-22 00:39:19 +00002562 pagerEnter(pPager);
danielk197717221812005-02-15 03:38:05 +00002563 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
drh86f8c192007-08-22 00:39:19 +00002564 pagerLeave(pPager);
danielk197717221812005-02-15 03:38:05 +00002565 if( rc!=SQLITE_OK ){
2566 return rc;
2567 }
2568
drhcb4c40b2004-08-18 19:09:43 +00002569 rc = pager_truncate(pPager, nPage);
drhf7c57532003-04-25 13:22:51 +00002570 return rc;
2571}
2572
2573/*
drhed7c8552001-04-11 14:29:21 +00002574** Shutdown the page cache. Free all memory and close all files.
2575**
2576** If a transaction was in progress when this routine is called, that
2577** transaction is rolled back. All outstanding pages are invalidated
2578** and their memory is freed. Any attempt to use a page associated
2579** with this page cache after this function returns will likely
2580** result in a coredump.
danielk1977aef0bf62005-12-30 16:28:01 +00002581**
2582** This function always succeeds. If a transaction is active an attempt
2583** is made to roll it back. If an error occurs during the rollback
2584** a hot journal may be left in the filesystem but no error is returned
2585** to the caller.
drhed7c8552001-04-11 14:29:21 +00002586*/
danielk19773b8a05f2007-03-19 17:44:26 +00002587int sqlite3PagerClose(Pager *pPager){
drh6f7adc82006-01-11 21:41:20 +00002588#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +00002589 if( !MEMDB ){
2590 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
2591 sqlite3_mutex_enter(mutex);
2592 if( pPager->pPrev ){
2593 pPager->pPrev->pNext = pPager->pNext;
2594 }else{
2595 sqlite3PagerList = pPager->pNext;
2596 }
2597 if( pPager->pNext ){
2598 pPager->pNext->pPrev = pPager->pPrev;
2599 }
2600 sqlite3_mutex_leave(mutex);
2601 }
danielk197713f72992005-12-18 08:51:22 +00002602#endif
2603
drhbafda092007-01-03 23:36:22 +00002604 disable_simulated_io_errors();
drhc2ee76c2007-01-04 14:58:14 +00002605 pPager->errCode = 0;
danielk197741483462007-03-24 16:45:04 +00002606 pPager->exclusiveMode = 0;
drhbafda092007-01-03 23:36:22 +00002607 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00002608 pagerUnlockAndRollback(pPager);
drhbafda092007-01-03 23:36:22 +00002609 enable_simulated_io_errors();
drh4f0c5872007-03-26 22:05:01 +00002610 PAGERTRACE2("CLOSE %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002611 IOTRACE(("CLOSE %p\n", pPager))
danielk1977efaaf572006-01-16 11:29:19 +00002612 assert( pPager->errCode || (pPager->journalOpen==0 && pPager->stmtOpen==0) );
danielk1977e94ddc92005-03-21 03:53:38 +00002613 if( pPager->journalOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002614 sqlite3OsClose(pPager->jfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002615 }
drh17435752007-08-16 04:30:38 +00002616 sqlite3_free(pPager->aInJournal);
danielk1977e94ddc92005-03-21 03:53:38 +00002617 if( pPager->stmtOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002618 sqlite3OsClose(pPager->stfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002619 }
danielk1977b4b47412007-08-17 15:53:36 +00002620 sqlite3OsClose(pPager->fd);
drh0f892532002-05-30 12:27:03 +00002621 /* Temp files are automatically deleted by the OS
2622 ** if( pPager->tempFile ){
drh66560ad2006-01-06 14:32:19 +00002623 ** sqlite3OsDelete(pPager->zFilename);
drh0f892532002-05-30 12:27:03 +00002624 ** }
2625 */
danielk1977aca790a2005-01-13 11:07:52 +00002626
drh17435752007-08-16 04:30:38 +00002627 sqlite3_free(pPager->aHash);
2628 sqlite3_free(pPager->pTmpSpace);
2629 sqlite3_free(pPager);
drhed7c8552001-04-11 14:29:21 +00002630 return SQLITE_OK;
2631}
2632
drh87cc3b32007-05-08 21:45:27 +00002633#if !defined(NDEBUG) || defined(SQLITE_TEST)
drhed7c8552001-04-11 14:29:21 +00002634/*
drh5e00f6c2001-09-13 13:46:56 +00002635** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +00002636*/
danielk19773b8a05f2007-03-19 17:44:26 +00002637Pgno sqlite3PagerPagenumber(DbPage *p){
drhed7c8552001-04-11 14:29:21 +00002638 return p->pgno;
2639}
drh87cc3b32007-05-08 21:45:27 +00002640#endif
drhed7c8552001-04-11 14:29:21 +00002641
2642/*
drhc8629a12004-05-08 20:07:40 +00002643** The page_ref() function increments the reference count for a page.
2644** If the page is currently on the freelist (the reference count is zero) then
drh7e3b0a02001-04-28 16:52:40 +00002645** remove it from the freelist.
drhc8629a12004-05-08 20:07:40 +00002646**
2647** For non-test systems, page_ref() is a macro that calls _page_ref()
2648** online of the reference count is zero. For test systems, page_ref()
2649** is a real function so that we can set breakpoints and trace it.
drh7e3b0a02001-04-28 16:52:40 +00002650*/
drh836faa42003-01-11 13:30:57 +00002651static void _page_ref(PgHdr *pPg){
drh7e3b0a02001-04-28 16:52:40 +00002652 if( pPg->nRef==0 ){
2653 /* The page is currently on the freelist. Remove it. */
danielk19779f61c2f2007-08-27 17:27:49 +00002654 lruListRemove(pPg);
drh7e3b0a02001-04-28 16:52:40 +00002655 pPg->pPager->nRef++;
2656 }
2657 pPg->nRef++;
drhdd793422001-06-28 01:54:48 +00002658 REFINFO(pPg);
drhdf0b3b02001-06-23 11:36:20 +00002659}
danielk1977aca790a2005-01-13 11:07:52 +00002660#ifdef SQLITE_DEBUG
drhc8629a12004-05-08 20:07:40 +00002661 static void page_ref(PgHdr *pPg){
2662 if( pPg->nRef==0 ){
2663 _page_ref(pPg);
2664 }else{
2665 pPg->nRef++;
2666 REFINFO(pPg);
2667 }
2668 }
2669#else
2670# define page_ref(P) ((P)->nRef==0?_page_ref(P):(void)(P)->nRef++)
2671#endif
drhdf0b3b02001-06-23 11:36:20 +00002672
2673/*
2674** Increment the reference count for a page. The input pointer is
2675** a reference to the page data.
2676*/
danielk19773b8a05f2007-03-19 17:44:26 +00002677int sqlite3PagerRef(DbPage *pPg){
drh86f8c192007-08-22 00:39:19 +00002678 pagerEnter(pPg->pPager);
drhdf0b3b02001-06-23 11:36:20 +00002679 page_ref(pPg);
drh86f8c192007-08-22 00:39:19 +00002680 pagerLeave(pPg->pPager);
drh8c42ca92001-06-22 19:15:00 +00002681 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00002682}
2683
2684/*
drh34e79ce2004-02-08 06:05:46 +00002685** Sync the journal. In other words, make sure all the pages that have
2686** been written to the journal have actually reached the surface of the
2687** disk. It is not safe to modify the original database file until after
2688** the journal has been synced. If the original database is modified before
2689** the journal is synced and a power failure occurs, the unsynced journal
2690** data would be lost and we would be unable to completely rollback the
2691** database changes. Database corruption would occur.
2692**
2693** This routine also updates the nRec field in the header of the journal.
2694** (See comments on the pager_playback() routine for additional information.)
2695** If the sync mode is FULL, two syncs will occur. First the whole journal
2696** is synced, then the nRec field is updated, then a second sync occurs.
drhb19a2bc2001-09-16 00:13:26 +00002697**
drh34e79ce2004-02-08 06:05:46 +00002698** For temporary databases, we do not care if we are able to rollback
danielk19774cd2cd52007-08-23 14:48:23 +00002699** after a power failure, so no sync occurs.
2700**
2701** If the IOCAP_SEQUENTIAL flag is set for the persistent media on which
2702** the database is stored, then OsSync() is never called on the journal
2703** file. In this case all that is required is to update the nRec field in
2704** the journal header.
drhfa86c412002-02-02 15:01:15 +00002705**
drh34e79ce2004-02-08 06:05:46 +00002706** This routine clears the needSync field of every page current held in
2707** memory.
drh50e5dad2001-09-15 00:57:28 +00002708*/
danielk197776572402004-06-25 02:38:54 +00002709static int syncJournal(Pager *pPager){
drh50e5dad2001-09-15 00:57:28 +00002710 PgHdr *pPg;
2711 int rc = SQLITE_OK;
drh03eb96a2002-11-10 23:32:56 +00002712
danielk19774cd2cd52007-08-23 14:48:23 +00002713
drh03eb96a2002-11-10 23:32:56 +00002714 /* Sync the journal before modifying the main database
2715 ** (assuming there is a journal and it needs to be synced.)
2716 */
danielk197776572402004-06-25 02:38:54 +00002717 if( pPager->needSync ){
drhfa86c412002-02-02 15:01:15 +00002718 if( !pPager->tempFile ){
danielk19774cd2cd52007-08-23 14:48:23 +00002719 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
drhdb48ee02003-01-16 13:42:43 +00002720 assert( pPager->journalOpen );
danielk19774cd2cd52007-08-23 14:48:23 +00002721
drh946966f2004-02-25 02:20:41 +00002722 /* assert( !pPager->noSync ); // noSync might be set if synchronous
2723 ** was turned off after the transaction was started. Ticket #615 */
drh968af522003-02-11 14:55:40 +00002724#ifndef NDEBUG
2725 {
drh34e79ce2004-02-08 06:05:46 +00002726 /* Make sure the pPager->nRec counter we are keeping agrees
2727 ** with the nRec computed from the size of the journal file.
2728 */
drheb206252004-10-01 02:00:31 +00002729 i64 jSz;
drh054889e2005-11-30 03:20:31 +00002730 rc = sqlite3OsFileSize(pPager->jfd, &jSz);
drh968af522003-02-11 14:55:40 +00002731 if( rc!=0 ) return rc;
danielk197776572402004-06-25 02:38:54 +00002732 assert( pPager->journalOff==jSz );
drh968af522003-02-11 14:55:40 +00002733 }
2734#endif
danielk19774cd2cd52007-08-23 14:48:23 +00002735 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
danielk197776572402004-06-25 02:38:54 +00002736 /* Write the nRec value into the journal file header. If in
2737 ** full-synchronous mode, sync the journal first. This ensures that
2738 ** all data has really hit the disk before nRec is updated to mark
danielk19774cd2cd52007-08-23 14:48:23 +00002739 ** it as a candidate for rollback.
2740 **
2741 ** This is not required if the persistent media supports the
2742 ** SAFE_APPEND property. Because in this case it is not possible
2743 ** for garbage data to be appended to the file, the nRec field
2744 ** is populated with 0xFFFFFFFF when the journal header is written
2745 ** and never needs to be updated.
danielk197776572402004-06-25 02:38:54 +00002746 */
danielk19774cd2cd52007-08-23 14:48:23 +00002747 i64 jrnlOff;
2748 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
drh4f0c5872007-03-26 22:05:01 +00002749 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002750 IOTRACE(("JSYNC %p\n", pPager))
danielk1977f036aef2007-08-20 05:36:51 +00002751 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags);
drhd8d66e82003-02-12 02:10:15 +00002752 if( rc!=0 ) return rc;
2753 }
danielk197713adf8a2004-06-03 16:08:41 +00002754
danielk197762079062007-08-15 17:08:46 +00002755 jrnlOff = pPager->journalHdr + sizeof(aJournalMagic);
2756 IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4));
2757 rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec);
drhb4746b92005-09-09 01:32:06 +00002758 if( rc ) return rc;
drh968af522003-02-11 14:55:40 +00002759 }
danielk19774cd2cd52007-08-23 14:48:23 +00002760 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
2761 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
2762 IOTRACE(("JSYNC %p\n", pPager))
2763 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags|
2764 (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
2765 );
2766 if( rc!=0 ) return rc;
2767 }
drhdb48ee02003-01-16 13:42:43 +00002768 pPager->journalStarted = 1;
drhfa86c412002-02-02 15:01:15 +00002769 }
drh50e5dad2001-09-15 00:57:28 +00002770 pPager->needSync = 0;
drh341eae82003-01-21 02:39:36 +00002771
2772 /* Erase the needSync flag from every page.
2773 */
2774 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2775 pPg->needSync = 0;
2776 }
danielk19779f61c2f2007-08-27 17:27:49 +00002777 lruListSetFirstSynced(pPager);
drh50e5dad2001-09-15 00:57:28 +00002778 }
drh03eb96a2002-11-10 23:32:56 +00002779
drh341eae82003-01-21 02:39:36 +00002780#ifndef NDEBUG
2781 /* If the Pager.needSync flag is clear then the PgHdr.needSync
2782 ** flag must also be clear for all pages. Verify that this
2783 ** invariant is true.
drh03eb96a2002-11-10 23:32:56 +00002784 */
drh341eae82003-01-21 02:39:36 +00002785 else{
2786 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2787 assert( pPg->needSync==0 );
2788 }
danielk19779f61c2f2007-08-27 17:27:49 +00002789 assert( pPager->lru.pFirstSynced==pPager->lru.pFirst );
drh03eb96a2002-11-10 23:32:56 +00002790 }
drh341eae82003-01-21 02:39:36 +00002791#endif
drhdb48ee02003-01-16 13:42:43 +00002792
drh81a20f22001-10-12 17:30:04 +00002793 return rc;
drh50e5dad2001-09-15 00:57:28 +00002794}
2795
2796/*
drhdc3e10b2006-06-15 14:31:06 +00002797** Merge two lists of pages connected by pDirty and in pgno order.
2798** Do not both fixing the pPrevDirty pointers.
2799*/
2800static PgHdr *merge_pagelist(PgHdr *pA, PgHdr *pB){
2801 PgHdr result, *pTail;
2802 pTail = &result;
2803 while( pA && pB ){
2804 if( pA->pgno<pB->pgno ){
2805 pTail->pDirty = pA;
2806 pTail = pA;
2807 pA = pA->pDirty;
2808 }else{
2809 pTail->pDirty = pB;
2810 pTail = pB;
2811 pB = pB->pDirty;
2812 }
2813 }
2814 if( pA ){
2815 pTail->pDirty = pA;
2816 }else if( pB ){
2817 pTail->pDirty = pB;
2818 }else{
2819 pTail->pDirty = 0;
2820 }
2821 return result.pDirty;
2822}
2823
2824/*
2825** Sort the list of pages in accending order by pgno. Pages are
2826** connected by pDirty pointers. The pPrevDirty pointers are
2827** corrupted by this sort.
2828*/
danielk197724168722007-04-02 05:07:47 +00002829#define N_SORT_BUCKET_ALLOC 25
2830#define N_SORT_BUCKET 25
2831#ifdef SQLITE_TEST
2832 int sqlite3_pager_n_sort_bucket = 0;
2833 #undef N_SORT_BUCKET
2834 #define N_SORT_BUCKET \
2835 (sqlite3_pager_n_sort_bucket?sqlite3_pager_n_sort_bucket:N_SORT_BUCKET_ALLOC)
2836#endif
drhdc3e10b2006-06-15 14:31:06 +00002837static PgHdr *sort_pagelist(PgHdr *pIn){
danielk197724168722007-04-02 05:07:47 +00002838 PgHdr *a[N_SORT_BUCKET_ALLOC], *p;
drhdc3e10b2006-06-15 14:31:06 +00002839 int i;
2840 memset(a, 0, sizeof(a));
2841 while( pIn ){
2842 p = pIn;
2843 pIn = p->pDirty;
2844 p->pDirty = 0;
2845 for(i=0; i<N_SORT_BUCKET-1; i++){
2846 if( a[i]==0 ){
2847 a[i] = p;
2848 break;
2849 }else{
2850 p = merge_pagelist(a[i], p);
2851 a[i] = 0;
2852 }
2853 }
2854 if( i==N_SORT_BUCKET-1 ){
danielk197724168722007-04-02 05:07:47 +00002855 /* Coverage: To get here, there need to be 2^(N_SORT_BUCKET)
2856 ** elements in the input list. This is possible, but impractical.
2857 ** Testing this line is the point of global variable
2858 ** sqlite3_pager_n_sort_bucket.
2859 */
drhdc3e10b2006-06-15 14:31:06 +00002860 a[i] = merge_pagelist(a[i], p);
2861 }
2862 }
2863 p = a[0];
2864 for(i=1; i<N_SORT_BUCKET; i++){
2865 p = merge_pagelist(p, a[i]);
2866 }
2867 return p;
2868}
2869
2870/*
drh2554f8b2003-01-22 01:26:44 +00002871** Given a list of pages (connected by the PgHdr.pDirty pointer) write
2872** every one of those pages out to the database file and mark them all
2873** as clean.
2874*/
2875static int pager_write_pagelist(PgHdr *pList){
2876 Pager *pPager;
drh153c62c2007-08-24 03:51:33 +00002877 PgHdr *p;
drh2554f8b2003-01-22 01:26:44 +00002878 int rc;
2879
2880 if( pList==0 ) return SQLITE_OK;
2881 pPager = pList->pPager;
danielk19779eed5052004-06-04 10:38:30 +00002882
2883 /* At this point there may be either a RESERVED or EXCLUSIVE lock on the
2884 ** database file. If there is already an EXCLUSIVE lock, the following
drh054889e2005-11-30 03:20:31 +00002885 ** calls to sqlite3OsLock() are no-ops.
danielk19779eed5052004-06-04 10:38:30 +00002886 **
drha6abd042004-06-09 17:37:22 +00002887 ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
2888 ** through an intermediate state PENDING. A PENDING lock prevents new
2889 ** readers from attaching to the database but is unsufficient for us to
2890 ** write. The idea of a PENDING lock is to prevent new readers from
2891 ** coming in while we wait for existing readers to clear.
danielk19779eed5052004-06-04 10:38:30 +00002892 **
drha6abd042004-06-09 17:37:22 +00002893 ** While the pager is in the RESERVED state, the original database file
2894 ** is unchanged and we can rollback without having to playback the
2895 ** journal into the original database file. Once we transition to
2896 ** EXCLUSIVE, it means the database file has been changed and any rollback
2897 ** will require a journal playback.
danielk19779eed5052004-06-04 10:38:30 +00002898 */
drh684917c2004-10-05 02:41:42 +00002899 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
danielk19779eed5052004-06-04 10:38:30 +00002900 if( rc!=SQLITE_OK ){
2901 return rc;
2902 }
2903
drhdc3e10b2006-06-15 14:31:06 +00002904 pList = sort_pagelist(pList);
drh153c62c2007-08-24 03:51:33 +00002905 for(p=pList; p; p=p->pDirty){
2906 assert( p->dirty );
2907 p->dirty = 0;
2908 }
drh2554f8b2003-01-22 01:26:44 +00002909 while( pList ){
danielk19777a2b1ee2007-08-21 14:27:01 +00002910
2911 /* If the file has not yet been opened, open it now. */
2912 if( !pPager->fd->pMethods ){
2913 assert(pPager->tempFile);
2914 rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->fd, pPager->zFilename);
2915 if( rc ) return rc;
2916 }
2917
danielk1977687566d2004-11-02 12:56:41 +00002918 /* If there are dirty pages in the page cache with page numbers greater
danielk19773b8a05f2007-03-19 17:44:26 +00002919 ** than Pager.dbSize, this means sqlite3PagerTruncate() was called to
danielk1977687566d2004-11-02 12:56:41 +00002920 ** make the file smaller (presumably by auto-vacuum code). Do not write
2921 ** any such pages to the file.
2922 */
2923 if( pList->pgno<=pPager->dbSize ){
danielk197762079062007-08-15 17:08:46 +00002924 i64 offset = (pList->pgno-1)*(i64)pPager->pageSize;
drhc001c582006-03-06 18:23:16 +00002925 char *pData = CODEC2(pPager, PGHDR_TO_DATA(pList), pList->pgno, 6);
drh477731b2007-06-16 03:06:27 +00002926 PAGERTRACE4("STORE %d page %d hash(%08x)\n",
2927 PAGERID(pPager), pList->pgno, pager_pagehash(pList));
drh538f5702007-04-13 02:14:30 +00002928 IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno));
danielk197762079062007-08-15 17:08:46 +00002929 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00002930 PAGER_INCR(sqlite3_pager_writedb_count);
2931 PAGER_INCR(pPager->nWrite);
drh86a88112007-04-16 15:02:19 +00002932 if( pList->pgno==1 ){
2933 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
2934 }
danielk1977687566d2004-11-02 12:56:41 +00002935 }
2936#ifndef NDEBUG
2937 else{
drh4f0c5872007-03-26 22:05:01 +00002938 PAGERTRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno);
danielk1977687566d2004-11-02 12:56:41 +00002939 }
2940#endif
drh2554f8b2003-01-22 01:26:44 +00002941 if( rc ) return rc;
danielk19773c407372005-02-15 02:54:14 +00002942#ifdef SQLITE_CHECK_PAGES
2943 pList->pageHash = pager_pagehash(pList);
2944#endif
drh2554f8b2003-01-22 01:26:44 +00002945 pList = pList->pDirty;
2946 }
2947 return SQLITE_OK;
2948}
2949
2950/*
2951** Collect every dirty page into a dirty list and
2952** return a pointer to the head of that list. All pages are
2953** collected even if they are still in use.
2954*/
2955static PgHdr *pager_get_all_dirty_pages(Pager *pPager){
drh605ad8f2006-05-03 23:34:05 +00002956 return pPager->pDirty;
drh2554f8b2003-01-22 01:26:44 +00002957}
2958
2959/*
drh165ffe92005-03-15 17:09:30 +00002960** Return TRUE if there is a hot journal on the given pager.
2961** A hot journal is one that needs to be played back.
2962**
2963** If the current size of the database file is 0 but a journal file
2964** exists, that is probably an old journal left over from a prior
2965** database with the same name. Just delete the journal.
2966*/
2967static int hasHotJournal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00002968 sqlite3_vfs *pVfs = pPager->pVfs;
drh165ffe92005-03-15 17:09:30 +00002969 if( !pPager->useJournal ) return 0;
danielk1977b4b47412007-08-17 15:53:36 +00002970 if( !sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS) ){
drhbb5f18d2007-04-06 18:23:17 +00002971 return 0;
2972 }
2973 if( sqlite3OsCheckReservedLock(pPager->fd) ){
2974 return 0;
2975 }
danielk19773b8a05f2007-03-19 17:44:26 +00002976 if( sqlite3PagerPagecount(pPager)==0 ){
danielk1977fee2d252007-08-18 10:59:19 +00002977 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
drh165ffe92005-03-15 17:09:30 +00002978 return 0;
2979 }else{
2980 return 1;
2981 }
2982}
2983
danielk19775591df52005-12-20 09:19:37 +00002984/*
danielk1977aef0bf62005-12-30 16:28:01 +00002985** Try to find a page in the cache that can be recycled.
2986**
2987** This routine may return SQLITE_IOERR, SQLITE_FULL or SQLITE_OK. It
danielk1977efaaf572006-01-16 11:29:19 +00002988** does not set the pPager->errCode variable.
danielk19775591df52005-12-20 09:19:37 +00002989*/
danielk197713f72992005-12-18 08:51:22 +00002990static int pager_recycle(Pager *pPager, int syncOk, PgHdr **ppPg){
2991 PgHdr *pPg;
2992 *ppPg = 0;
2993
danielk1977c551edc2007-04-05 13:12:13 +00002994 assert(!MEMDB);
2995
danielk197713f72992005-12-18 08:51:22 +00002996 /* Find a page to recycle. Try to locate a page that does not
2997 ** require us to do an fsync() on the journal.
2998 */
danielk19779f61c2f2007-08-27 17:27:49 +00002999 pPg = pPager->lru.pFirstSynced;
danielk197713f72992005-12-18 08:51:22 +00003000
3001 /* If we could not find a page that does not require an fsync()
3002 ** on the journal file then fsync the journal file. This is a
3003 ** very slow operation, so we work hard to avoid it. But sometimes
3004 ** it can't be helped.
3005 */
danielk19779f61c2f2007-08-27 17:27:49 +00003006 if( pPg==0 && pPager->lru.pFirst && syncOk && !MEMDB){
danielk19774cd2cd52007-08-23 14:48:23 +00003007 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
danielk197713f72992005-12-18 08:51:22 +00003008 int rc = syncJournal(pPager);
3009 if( rc!=0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00003010 return rc;
danielk197713f72992005-12-18 08:51:22 +00003011 }
danielk19774cd2cd52007-08-23 14:48:23 +00003012 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
danielk197713f72992005-12-18 08:51:22 +00003013 /* If in full-sync mode, write a new journal header into the
3014 ** journal file. This is done to avoid ever modifying a journal
3015 ** header that is involved in the rollback of pages that have
3016 ** already been written to the database (in case the header is
3017 ** trashed when the nRec field is updated).
3018 */
3019 pPager->nRec = 0;
3020 assert( pPager->journalOff > 0 );
danielk19774099f6e2007-03-19 11:25:20 +00003021 assert( pPager->doNotSync==0 );
danielk197713f72992005-12-18 08:51:22 +00003022 rc = writeJournalHdr(pPager);
3023 if( rc!=0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00003024 return rc;
danielk197713f72992005-12-18 08:51:22 +00003025 }
3026 }
danielk19779f61c2f2007-08-27 17:27:49 +00003027 pPg = pPager->lru.pFirst;
danielk197713f72992005-12-18 08:51:22 +00003028 }
3029 if( pPg==0 ){
3030 return SQLITE_OK;
3031 }
3032
3033 assert( pPg->nRef==0 );
3034
3035 /* Write the page to the database file if it is dirty.
3036 */
3037 if( pPg->dirty ){
3038 int rc;
3039 assert( pPg->needSync==0 );
drh605ad8f2006-05-03 23:34:05 +00003040 makeClean(pPg);
3041 pPg->dirty = 1;
danielk197713f72992005-12-18 08:51:22 +00003042 pPg->pDirty = 0;
3043 rc = pager_write_pagelist( pPg );
drh153c62c2007-08-24 03:51:33 +00003044 pPg->dirty = 0;
danielk197713f72992005-12-18 08:51:22 +00003045 if( rc!=SQLITE_OK ){
danielk1977aef0bf62005-12-30 16:28:01 +00003046 return rc;
danielk197713f72992005-12-18 08:51:22 +00003047 }
3048 }
3049 assert( pPg->dirty==0 );
3050
3051 /* If the page we are recycling is marked as alwaysRollback, then
3052 ** set the global alwaysRollback flag, thus disabling the
drh80e35f42007-03-30 14:06:34 +00003053 ** sqlite3PagerDontRollback() optimization for the rest of this transaction.
danielk197713f72992005-12-18 08:51:22 +00003054 ** It is necessary to do this because the page marked alwaysRollback
3055 ** might be reloaded at a later time but at that point we won't remember
3056 ** that is was marked alwaysRollback. This means that all pages must
3057 ** be marked as alwaysRollback from here on out.
3058 */
3059 if( pPg->alwaysRollback ){
drh602c2372007-03-01 00:29:13 +00003060 IOTRACE(("ALWAYS_ROLLBACK %p\n", pPager))
danielk197713f72992005-12-18 08:51:22 +00003061 pPager->alwaysRollback = 1;
3062 }
3063
3064 /* Unlink the old page from the free list and the hash table
3065 */
3066 unlinkPage(pPg);
drh7c4ac0c2007-04-05 11:25:58 +00003067 assert( pPg->pgno==0 );
danielk197713f72992005-12-18 08:51:22 +00003068
3069 *ppPg = pPg;
3070 return SQLITE_OK;
3071}
3072
drh86f8c192007-08-22 00:39:19 +00003073#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk197713f72992005-12-18 08:51:22 +00003074/*
3075** This function is called to free superfluous dynamically allocated memory
3076** held by the pager system. Memory in use by any SQLite pager allocated
drh17435752007-08-16 04:30:38 +00003077** by the current thread may be sqlite3_free()ed.
danielk197713f72992005-12-18 08:51:22 +00003078**
3079** nReq is the number of bytes of memory required. Once this much has
drh86f8c192007-08-22 00:39:19 +00003080** been released, the function returns. The return value is the total number
danielk197713f72992005-12-18 08:51:22 +00003081** of bytes of memory released.
3082*/
danielk19773b8a05f2007-03-19 17:44:26 +00003083int sqlite3PagerReleaseMemory(int nReq){
drh86f8c192007-08-22 00:39:19 +00003084 int nReleased = 0; /* Bytes of memory released so far */
3085 sqlite3_mutex *mutex; /* The MEM2 mutex */
3086 Pager *pPager; /* For looping over pagers */
danielk19779f61c2f2007-08-27 17:27:49 +00003087 int rc = SQLITE_OK;
danielk197713f72992005-12-18 08:51:22 +00003088
drh86f8c192007-08-22 00:39:19 +00003089 /* Acquire the memory-management mutex
danielk1977441b09a2006-01-05 13:48:29 +00003090 */
drh86f8c192007-08-22 00:39:19 +00003091 mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
3092 sqlite3_mutex_enter(mutex);
3093
3094 /* Signal all database connections that memory management wants
3095 ** to have access to the pagers.
3096 */
3097 for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
3098 pPager->iInUseMM = 1;
danielk1977441b09a2006-01-05 13:48:29 +00003099 }
3100
danielk19779f61c2f2007-08-27 17:27:49 +00003101 while( rc==SQLITE_OK && (nReq<0 || nReleased<nReq) ){
3102 PgHdr *pPg;
3103 PgHdr *pRecycled;
3104
3105 /* Try to find a page to recycle that does not require a sync(). If
3106 ** this is not possible, find one that does require a sync().
3107 */
3108 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
3109 pPg = sqlite3LruPageList.pFirstSynced;
3110 while( pPg && (pPg->needSync || pPg->pPager->iInUseDB) ){
3111 pPg = pPg->gfree.pNext;
3112 }
3113 if( !pPg ){
3114 pPg = sqlite3LruPageList.pFirst;
3115 while( pPg && pPg->pPager->iInUseDB ){
3116 pPg = pPg->gfree.pNext;
3117 }
3118 }
3119 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
danielk197713f72992005-12-18 08:51:22 +00003120
danielk197784f786f2007-08-28 08:00:17 +00003121 /* If pPg==0, then the block above has failed to find a page to
3122 ** recycle. In this case return early - no further memory will
3123 ** be released.
3124 */
danielk19779f61c2f2007-08-27 17:27:49 +00003125 if( !pPg ) break;
danielk197713f72992005-12-18 08:51:22 +00003126
danielk19779f61c2f2007-08-27 17:27:49 +00003127 pPager = pPg->pPager;
3128 assert(!pPg->needSync || pPg==pPager->lru.pFirst);
3129 assert(pPg->needSync || pPg==pPager->lru.pFirstSynced);
3130
3131 rc = pager_recycle(pPager, 1, &pRecycled);
3132 assert(pRecycled==pPg || rc!=SQLITE_OK);
3133 if( rc==SQLITE_OK ){
3134 /* We've found a page to free. At this point the page has been
3135 ** removed from the page hash-table, free-list and synced-list
3136 ** (pFirstSynced). It is still in the all pages (pAll) list.
3137 ** Remove it from this list before freeing.
3138 **
3139 ** Todo: Check the Pager.pStmt list to make sure this is Ok. It
3140 ** probably is though.
danielk197713f72992005-12-18 08:51:22 +00003141 */
danielk19779f61c2f2007-08-27 17:27:49 +00003142 PgHdr *pTmp;
3143 assert( pPg );
3144 if( pPg==pPager->pAll ){
3145 pPager->pAll = pPg->pNextAll;
3146 }else{
3147 for( pTmp=pPager->pAll; pTmp->pNextAll!=pPg; pTmp=pTmp->pNextAll ){}
3148 pTmp->pNextAll = pPg->pNextAll;
danielk197713f72992005-12-18 08:51:22 +00003149 }
danielk19779f61c2f2007-08-27 17:27:49 +00003150 nReleased += (
3151 sizeof(*pPg) + pPager->pageSize
3152 + sizeof(u32) + pPager->nExtra
3153 + MEMDB*sizeof(PgHistory)
3154 );
3155 IOTRACE(("PGFREE %p %d *\n", pPager, pPg->pgno));
3156 PAGER_INCR(sqlite3_pager_pgfree_count);
3157 sqlite3_free(pPg);
danielk197784f786f2007-08-28 08:00:17 +00003158 pPager->nPage--;
danielk19779f61c2f2007-08-27 17:27:49 +00003159 }else{
3160 /* An error occured whilst writing to the database file or
3161 ** journal in pager_recycle(). The error is not returned to the
3162 ** caller of this function. Instead, set the Pager.errCode variable.
3163 ** The error will be returned to the user (or users, in the case
3164 ** of a shared pager cache) of the pager for which the error occured.
3165 */
3166 assert(
3167 (rc&0xff)==SQLITE_IOERR ||
3168 rc==SQLITE_FULL ||
3169 rc==SQLITE_BUSY
3170 );
3171 assert( pPager->state>=PAGER_RESERVED );
3172 pager_error(pPager, rc);
danielk197713f72992005-12-18 08:51:22 +00003173 }
3174 }
danielk1977aef0bf62005-12-30 16:28:01 +00003175
drh86f8c192007-08-22 00:39:19 +00003176 /* Clear the memory management flags and release the mutex
3177 */
3178 for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
3179 pPager->iInUseMM = 0;
3180 }
3181 sqlite3_mutex_leave(mutex);
3182
3183 /* Return the number of bytes released
3184 */
danielk197713f72992005-12-18 08:51:22 +00003185 return nReleased;
3186}
drh86f8c192007-08-22 00:39:19 +00003187#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
danielk197713f72992005-12-18 08:51:22 +00003188
drh165ffe92005-03-15 17:09:30 +00003189/*
danielk1977e180dd92007-04-05 17:15:52 +00003190** Read the content of page pPg out of the database file.
3191*/
3192static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){
3193 int rc;
danielk197762079062007-08-15 17:08:46 +00003194 i64 offset;
danielk1977e180dd92007-04-05 17:15:52 +00003195 assert( MEMDB==0 );
danielk19777a2b1ee2007-08-21 14:27:01 +00003196 assert(pPager->fd->pMethods||pPager->tempFile);
3197 if( !pPager->fd->pMethods ){
3198 return SQLITE_IOERR_SHORT_READ;
3199 }
danielk197762079062007-08-15 17:08:46 +00003200 offset = (pgno-1)*(i64)pPager->pageSize;
3201 rc = sqlite3OsRead(pPager->fd, PGHDR_TO_DATA(pPg), pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00003202 PAGER_INCR(sqlite3_pager_readdb_count);
3203 PAGER_INCR(pPager->nRead);
3204 IOTRACE(("PGIN %p %d\n", pPager, pgno));
drh86a88112007-04-16 15:02:19 +00003205 if( pgno==1 ){
3206 memcpy(&pPager->dbFileVers, &((u8*)PGHDR_TO_DATA(pPg))[24],
3207 sizeof(pPager->dbFileVers));
3208 }
danielk1977e180dd92007-04-05 17:15:52 +00003209 CODEC1(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3);
drh477731b2007-06-16 03:06:27 +00003210 PAGERTRACE4("FETCH %d page %d hash(%08x)\n",
3211 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg));
danielk1977e180dd92007-04-05 17:15:52 +00003212 return rc;
3213}
3214
3215
3216/*
danielk1977e277be02007-03-23 18:12:06 +00003217** This function is called to obtain the shared lock required before
3218** data may be read from the pager cache. If the shared lock has already
3219** been obtained, this function is a no-op.
danielk1977393f0682007-03-31 10:00:48 +00003220**
3221** Immediately after obtaining the shared lock (if required), this function
3222** checks for a hot-journal file. If one is found, an emergency rollback
3223** is performed immediately.
danielk1977e277be02007-03-23 18:12:06 +00003224*/
3225static int pagerSharedLock(Pager *pPager){
3226 int rc = SQLITE_OK;
3227
3228 if( pPager->state==PAGER_UNLOCK ){
danielk1977b4b47412007-08-17 15:53:36 +00003229 sqlite3_vfs *pVfs = pPager->pVfs;
danielk1977e277be02007-03-23 18:12:06 +00003230 if( !MEMDB ){
3231 assert( pPager->nRef==0 );
3232 if( !pPager->noReadlock ){
3233 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
3234 if( rc!=SQLITE_OK ){
3235 return pager_error(pPager, rc);
3236 }
3237 assert( pPager->state>=SHARED_LOCK );
3238 }
3239
3240 /* If a journal file exists, and there is no RESERVED lock on the
3241 ** database file, then it either needs to be played back or deleted.
3242 */
3243 if( hasHotJournal(pPager) ){
3244 /* Get an EXCLUSIVE lock on the database file. At this point it is
3245 ** important that a RESERVED lock is not obtained on the way to the
3246 ** EXCLUSIVE lock. If it were, another process might open the
3247 ** database file, detect the RESERVED lock, and conclude that the
3248 ** database is safe to read while this process is still rolling it
3249 ** back.
3250 **
3251 ** Because the intermediate RESERVED lock is not requested, the
3252 ** second process will get to this point in the code and fail to
3253 ** obtain it's own EXCLUSIVE lock on the database file.
3254 */
3255 rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
3256 if( rc!=SQLITE_OK ){
3257 pager_unlock(pPager);
3258 return pager_error(pPager, rc);
3259 }
3260 pPager->state = PAGER_EXCLUSIVE;
3261
3262 /* Open the journal for reading only. Return SQLITE_BUSY if
3263 ** we are unable to open the journal file.
3264 **
3265 ** The journal file does not need to be locked itself. The
3266 ** journal file is never open unless the main database file holds
3267 ** a write lock, so there is never any chance of two or more
3268 ** processes opening the journal at the same time.
danielk1977979f38e2007-03-27 16:19:51 +00003269 **
drhfd131da2007-08-07 17:13:03 +00003270 ** Open the journal for read/write access. This is because in
3271 ** exclusive-access mode the file descriptor will be kept open and
danielk1977979f38e2007-03-27 16:19:51 +00003272 ** possibly used for a transaction later on. On some systems, the
3273 ** OsTruncate() call used in exclusive-access mode also requires
3274 ** a read/write file handle.
danielk1977e277be02007-03-23 18:12:06 +00003275 */
danielk1977979f38e2007-03-27 16:19:51 +00003276 rc = SQLITE_BUSY;
danielk1977b4b47412007-08-17 15:53:36 +00003277 if( sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS) ){
3278 int fout = 0;
danielk1977fee2d252007-08-18 10:59:19 +00003279 int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
danielk19777152de82007-03-29 17:28:14 +00003280 assert( !pPager->tempFile );
danielk1977a1644fd2007-08-29 12:31:25 +00003281 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, &fout);
danielk1977b4b47412007-08-17 15:53:36 +00003282 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
3283 if( fout&SQLITE_OPEN_READONLY ){
danielk1977979f38e2007-03-27 16:19:51 +00003284 rc = SQLITE_BUSY;
danielk1977b4b47412007-08-17 15:53:36 +00003285 sqlite3OsClose(pPager->jfd);
danielk1977979f38e2007-03-27 16:19:51 +00003286 }
3287 }
danielk1977e277be02007-03-23 18:12:06 +00003288 if( rc!=SQLITE_OK ){
3289 pager_unlock(pPager);
danielk1977a1644fd2007-08-29 12:31:25 +00003290 return (rc==SQLITE_NOMEM?rc:SQLITE_BUSY);
danielk1977e277be02007-03-23 18:12:06 +00003291 }
3292 pPager->journalOpen = 1;
3293 pPager->journalStarted = 0;
3294 pPager->journalOff = 0;
3295 pPager->setMaster = 0;
3296 pPager->journalHdr = 0;
3297
3298 /* Playback and delete the journal. Drop the database write
3299 ** lock and reacquire the read lock.
3300 */
3301 rc = pager_playback(pPager, 1);
3302 if( rc!=SQLITE_OK ){
3303 return pager_error(pPager, rc);
3304 }
danielk1977c5859712007-03-26 12:26:27 +00003305 assert(pPager->state==PAGER_SHARED ||
3306 (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
3307 );
danielk1977e277be02007-03-23 18:12:06 +00003308 }
3309
3310 if( pPager->pAll ){
danielk197724168722007-04-02 05:07:47 +00003311 /* The shared-lock has just been acquired on the database file
3312 ** and there are already pages in the cache (from a previous
drh86a88112007-04-16 15:02:19 +00003313 ** read or write transaction). Check to see if the database
3314 ** has been modified. If the database has changed, flush the
3315 ** cache.
3316 **
3317 ** Database changes is detected by looking at 15 bytes beginning
3318 ** at offset 24 into the file. The first 4 of these 16 bytes are
3319 ** a 32-bit counter that is incremented with each change. The
3320 ** other bytes change randomly with each file change when
3321 ** a codec is in use.
3322 **
3323 ** There is a vanishingly small chance that a change will not be
drh6fa51032007-05-09 20:35:31 +00003324 ** detected. The chance of an undetected change is so small that
drh86a88112007-04-16 15:02:19 +00003325 ** it can be neglected.
danielk197724168722007-04-02 05:07:47 +00003326 */
drh86a88112007-04-16 15:02:19 +00003327 char dbFileVers[sizeof(pPager->dbFileVers)];
danielk1977e180dd92007-04-05 17:15:52 +00003328 sqlite3PagerPagecount(pPager);
danielk197724168722007-04-02 05:07:47 +00003329
danielk1977e180dd92007-04-05 17:15:52 +00003330 if( pPager->errCode ){
3331 return pPager->errCode;
danielk1977e277be02007-03-23 18:12:06 +00003332 }
3333
danielk1977e180dd92007-04-05 17:15:52 +00003334 if( pPager->dbSize>0 ){
drhae5e4452007-05-03 17:18:36 +00003335 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
danielk197762079062007-08-15 17:08:46 +00003336 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
danielk1977e180dd92007-04-05 17:15:52 +00003337 if( rc!=SQLITE_OK ){
3338 return rc;
3339 }
drh86a88112007-04-16 15:02:19 +00003340 }else{
3341 memset(dbFileVers, 0, sizeof(dbFileVers));
danielk1977e180dd92007-04-05 17:15:52 +00003342 }
3343
drh86a88112007-04-16 15:02:19 +00003344 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
danielk1977e180dd92007-04-05 17:15:52 +00003345 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00003346 }
3347 }
3348 }
danielk1977c5859712007-03-26 12:26:27 +00003349 assert( pPager->exclusiveMode || pPager->state<=PAGER_SHARED );
3350 if( pPager->state==PAGER_UNLOCK ){
3351 pPager->state = PAGER_SHARED;
3352 }
danielk1977e277be02007-03-23 18:12:06 +00003353 }
3354
3355 return rc;
3356}
3357
3358/*
danielk1977e180dd92007-04-05 17:15:52 +00003359** Allocate a PgHdr object. Either create a new one or reuse
3360** an existing one that is not otherwise in use.
3361**
3362** A new PgHdr structure is created if any of the following are
3363** true:
3364**
3365** (1) We have not exceeded our maximum allocated cache size
3366** as set by the "PRAGMA cache_size" command.
3367**
3368** (2) There are no unused PgHdr objects available at this time.
3369**
3370** (3) This is an in-memory database.
3371**
3372** (4) There are no PgHdr objects that do not require a journal
3373** file sync and a sync of the journal file is currently
3374** prohibited.
3375**
3376** Otherwise, reuse an existing PgHdr. In other words, reuse an
3377** existing PgHdr if all of the following are true:
3378**
3379** (1) We have reached or exceeded the maximum cache size
3380** allowed by "PRAGMA cache_size".
3381**
3382** (2) There is a PgHdr available with PgHdr->nRef==0
3383**
3384** (3) We are not in an in-memory database
3385**
3386** (4) Either there is an available PgHdr that does not need
3387** to be synced to disk or else disk syncing is currently
3388** allowed.
danielk197724168722007-04-02 05:07:47 +00003389*/
3390static int pagerAllocatePage(Pager *pPager, PgHdr **ppPg){
3391 int rc = SQLITE_OK;
3392 PgHdr *pPg;
3393
danielk1977e180dd92007-04-05 17:15:52 +00003394 /* Create a new PgHdr if any of the four conditions defined
danielk19772a6bdf62007-08-20 16:07:00 +00003395 ** above are met: */
danielk1977e180dd92007-04-05 17:15:52 +00003396 if( pPager->nPage<pPager->mxPage
danielk19779f61c2f2007-08-27 17:27:49 +00003397 || pPager->lru.pFirst==0
danielk1977e180dd92007-04-05 17:15:52 +00003398 || MEMDB
danielk19779f61c2f2007-08-27 17:27:49 +00003399 || (pPager->lru.pFirstSynced==0 && pPager->doNotSync)
danielk1977e180dd92007-04-05 17:15:52 +00003400 ){
danielk197724168722007-04-02 05:07:47 +00003401 if( pPager->nPage>=pPager->nHash ){
3402 pager_resize_hash_table(pPager,
3403 pPager->nHash<256 ? 256 : pPager->nHash*2);
3404 if( pPager->nHash==0 ){
3405 rc = SQLITE_NOMEM;
3406 goto pager_allocate_out;
3407 }
3408 }
drhed138fb2007-08-22 22:04:37 +00003409 pagerLeave(pPager);
drh17435752007-08-16 04:30:38 +00003410 pPg = sqlite3_malloc( sizeof(*pPg) + pPager->pageSize
danielk197724168722007-04-02 05:07:47 +00003411 + sizeof(u32) + pPager->nExtra
3412 + MEMDB*sizeof(PgHistory) );
drhed138fb2007-08-22 22:04:37 +00003413 pagerEnter(pPager);
danielk197724168722007-04-02 05:07:47 +00003414 if( pPg==0 ){
3415 rc = SQLITE_NOMEM;
3416 goto pager_allocate_out;
3417 }
3418 memset(pPg, 0, sizeof(*pPg));
3419 if( MEMDB ){
3420 memset(PGHDR_TO_HIST(pPg, pPager), 0, sizeof(PgHistory));
3421 }
3422 pPg->pPager = pPager;
3423 pPg->pNextAll = pPager->pAll;
3424 pPager->pAll = pPg;
3425 pPager->nPage++;
danielk197724168722007-04-02 05:07:47 +00003426 }else{
3427 /* Recycle an existing page with a zero ref-count. */
3428 rc = pager_recycle(pPager, 1, &pPg);
danielk1977e965ac72007-06-13 15:22:28 +00003429 if( rc==SQLITE_BUSY ){
3430 rc = SQLITE_IOERR_BLOCKED;
3431 }
danielk197724168722007-04-02 05:07:47 +00003432 if( rc!=SQLITE_OK ){
3433 goto pager_allocate_out;
3434 }
3435 assert( pPager->state>=SHARED_LOCK );
3436 assert(pPg);
3437 }
3438 *ppPg = pPg;
3439
3440pager_allocate_out:
3441 return rc;
3442}
3443
3444/*
drhd33d5a82007-04-26 12:11:28 +00003445** Make sure we have the content for a page. If the page was
3446** previously acquired with noContent==1, then the content was
3447** just initialized to zeros instead of being read from disk.
3448** But now we need the real data off of disk. So make sure we
3449** have it. Read it in if we do not have it already.
3450*/
3451static int pager_get_content(PgHdr *pPg){
3452 if( pPg->needRead ){
3453 int rc = readDbPage(pPg->pPager, pPg, pPg->pgno);
3454 if( rc==SQLITE_OK ){
3455 pPg->needRead = 0;
3456 }else{
3457 return rc;
3458 }
3459 }
3460 return SQLITE_OK;
3461}
3462
3463/*
drhd9b02572001-04-15 00:37:09 +00003464** Acquire a page.
3465**
drh58a11682001-11-10 13:51:08 +00003466** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00003467** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00003468**
drhd33d5a82007-04-26 12:11:28 +00003469** This routine works for any page number greater than 0. If the database
drh306dc212001-05-21 13:45:10 +00003470** file is smaller than the requested page, then no actual disk
3471** read occurs and the memory image of the page is initialized to
3472** all zeros. The extra data appended to a page is always initialized
3473** to zeros the first time a page is loaded into memory.
3474**
drhd9b02572001-04-15 00:37:09 +00003475** The acquisition might fail for several reasons. In all cases,
3476** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00003477**
drhd33d5a82007-04-26 12:11:28 +00003478** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
drh7e3b0a02001-04-28 16:52:40 +00003479** to find a page in the in-memory cache first. If the page is not already
drhd33d5a82007-04-26 12:11:28 +00003480** in memory, this routine goes to disk to read it in whereas Lookup()
drh7e3b0a02001-04-28 16:52:40 +00003481** just returns 0. This routine acquires a read-lock the first time it
3482** has to go to disk, and could also playback an old journal if necessary.
drhd33d5a82007-04-26 12:11:28 +00003483** Since Lookup() never goes to disk, it never has to deal with locks
drh7e3b0a02001-04-28 16:52:40 +00003484** or journal files.
drh0787db62007-03-04 13:15:27 +00003485**
drh538f5702007-04-13 02:14:30 +00003486** If noContent is false, the page contents are actually read from disk.
3487** If noContent is true, it means that we do not care about the contents
3488** of the page at this time, so do not do a disk read. Just fill in the
3489** page content with zeros. But mark the fact that we have not read the
3490** content by setting the PgHdr.needRead flag. Later on, if
drhd33d5a82007-04-26 12:11:28 +00003491** sqlite3PagerWrite() is called on this page or if this routine is
3492** called again with noContent==0, that means that the content is needed
3493** and the disk read should occur at that point.
drhed7c8552001-04-11 14:29:21 +00003494*/
drh86f8c192007-08-22 00:39:19 +00003495static int pagerAcquire(
drh538f5702007-04-13 02:14:30 +00003496 Pager *pPager, /* The pager open on the database file */
3497 Pgno pgno, /* Page number to fetch */
3498 DbPage **ppPage, /* Write a pointer to the page here */
3499 int noContent /* Do not bother reading content from disk if true */
3500){
drhed7c8552001-04-11 14:29:21 +00003501 PgHdr *pPg;
drh165ffe92005-03-15 17:09:30 +00003502 int rc;
drhed7c8552001-04-11 14:29:21 +00003503
danielk1977e277be02007-03-23 18:12:06 +00003504 assert( pPager->state==PAGER_UNLOCK || pPager->nRef>0 || pgno==1 );
3505
danielk197726836652005-01-17 01:33:13 +00003506 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
3507 ** number greater than this, or zero, is requested.
3508 */
drh267cb322005-09-16 17:16:52 +00003509 if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh49285702005-09-17 15:20:26 +00003510 return SQLITE_CORRUPT_BKPT;
danielk197726836652005-01-17 01:33:13 +00003511 }
3512
drhd9b02572001-04-15 00:37:09 +00003513 /* Make sure we have not hit any critical errors.
3514 */
drh836faa42003-01-11 13:30:57 +00003515 assert( pPager!=0 );
drh2e6d11b2003-04-25 15:37:57 +00003516 *ppPage = 0;
danielk1977efaaf572006-01-16 11:29:19 +00003517 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
3518 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003519 }
3520
danielk197713adf8a2004-06-03 16:08:41 +00003521 /* If this is the first page accessed, then get a SHARED lock
danielk1977334cdb62007-03-26 08:05:12 +00003522 ** on the database file. pagerSharedLock() is a no-op if
3523 ** a database lock is already held.
drhed7c8552001-04-11 14:29:21 +00003524 */
danielk1977e277be02007-03-23 18:12:06 +00003525 rc = pagerSharedLock(pPager);
3526 if( rc!=SQLITE_OK ){
3527 return rc;
drhed7c8552001-04-11 14:29:21 +00003528 }
danielk1977e277be02007-03-23 18:12:06 +00003529 assert( pPager->state!=PAGER_UNLOCK );
3530
3531 pPg = pager_lookup(pPager, pgno);
drhed7c8552001-04-11 14:29:21 +00003532 if( pPg==0 ){
drhd9b02572001-04-15 00:37:09 +00003533 /* The requested page is not in the page cache. */
danielk1977979f38e2007-03-27 16:19:51 +00003534 int nMax;
drhed7c8552001-04-11 14:29:21 +00003535 int h;
drh538f5702007-04-13 02:14:30 +00003536 PAGER_INCR(pPager->nMiss);
danielk197724168722007-04-02 05:07:47 +00003537 rc = pagerAllocatePage(pPager, &pPg);
3538 if( rc!=SQLITE_OK ){
3539 return rc;
drhed7c8552001-04-11 14:29:21 +00003540 }
danielk197724168722007-04-02 05:07:47 +00003541
drhed7c8552001-04-11 14:29:21 +00003542 pPg->pgno = pgno;
danielk1977f35843b2007-04-07 15:03:17 +00003543 assert( !MEMDB || pgno>pPager->stmtSize );
drh1ab43002002-01-14 09:28:19 +00003544 if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){
danielk19771e536952007-08-16 10:09:01 +00003545#if 0
danielk19774adee202004-05-08 08:23:19 +00003546 sqlite3CheckMemory(pPager->aInJournal, pgno/8);
danielk19771e536952007-08-16 10:09:01 +00003547#endif
drhdb48ee02003-01-16 13:42:43 +00003548 assert( pPager->journalOpen );
drh6019e162001-07-02 17:51:45 +00003549 pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0;
drhdb48ee02003-01-16 13:42:43 +00003550 pPg->needSync = 0;
drh6019e162001-07-02 17:51:45 +00003551 }else{
3552 pPg->inJournal = 0;
drhdb48ee02003-01-16 13:42:43 +00003553 pPg->needSync = 0;
drh6019e162001-07-02 17:51:45 +00003554 }
danielk1977f35843b2007-04-07 15:03:17 +00003555
drh605ad8f2006-05-03 23:34:05 +00003556 makeClean(pPg);
drhed7c8552001-04-11 14:29:21 +00003557 pPg->nRef = 1;
drhdd793422001-06-28 01:54:48 +00003558 REFINFO(pPg);
danielk197775bab7d2006-01-23 13:09:45 +00003559
drhd9b02572001-04-15 00:37:09 +00003560 pPager->nRef++;
drh2e6d11b2003-04-25 15:37:57 +00003561 if( pPager->nExtra>0 ){
drh90f5ecb2004-07-22 01:19:35 +00003562 memset(PGHDR_TO_EXTRA(pPg, pPager), 0, pPager->nExtra);
drh2e6d11b2003-04-25 15:37:57 +00003563 }
danielk1977979f38e2007-03-27 16:19:51 +00003564 nMax = sqlite3PagerPagecount(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00003565 if( pPager->errCode ){
danielk19773b8a05f2007-03-19 17:44:26 +00003566 sqlite3PagerUnref(pPg);
danielk1977efaaf572006-01-16 11:29:19 +00003567 rc = pPager->errCode;
drh2e6d11b2003-04-25 15:37:57 +00003568 return rc;
3569 }
danielk197775bab7d2006-01-23 13:09:45 +00003570
3571 /* Populate the page with data, either by reading from the database
3572 ** file, or by setting the entire page to zero.
3573 */
drhd215acf2007-04-13 04:01:58 +00003574 if( nMax<(int)pgno || MEMDB || (noContent && !pPager->alwaysRollback) ){
drhf8e632b2007-05-08 14:51:36 +00003575 if( pgno>pPager->mxPgno ){
danielk1977de3bea72007-05-09 15:56:39 +00003576 sqlite3PagerUnref(pPg);
drhf8e632b2007-05-08 14:51:36 +00003577 return SQLITE_FULL;
3578 }
drh90f5ecb2004-07-22 01:19:35 +00003579 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
drhd215acf2007-04-13 04:01:58 +00003580 pPg->needRead = noContent && !pPager->alwaysRollback;
drh538f5702007-04-13 02:14:30 +00003581 IOTRACE(("ZERO %p %d\n", pPager, pgno));
drh306dc212001-05-21 13:45:10 +00003582 }else{
danielk1977e180dd92007-04-05 17:15:52 +00003583 rc = readDbPage(pPager, pPg, pgno);
drh551b7732006-11-06 21:20:25 +00003584 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
3585 pPg->pgno = 0;
danielk19773b8a05f2007-03-19 17:44:26 +00003586 sqlite3PagerUnref(pPg);
drh551b7732006-11-06 21:20:25 +00003587 return rc;
drh81a20f22001-10-12 17:30:04 +00003588 }
danielk1977b4626a32007-04-28 15:47:43 +00003589 pPg->needRead = 0;
drh306dc212001-05-21 13:45:10 +00003590 }
danielk197775bab7d2006-01-23 13:09:45 +00003591
3592 /* Link the page into the page hash table */
drh8ca0c722006-05-07 17:49:38 +00003593 h = pgno & (pPager->nHash-1);
drh3765df42006-06-28 18:18:09 +00003594 assert( pgno!=0 );
danielk197775bab7d2006-01-23 13:09:45 +00003595 pPg->pNextHash = pPager->aHash[h];
3596 pPager->aHash[h] = pPg;
3597 if( pPg->pNextHash ){
3598 assert( pPg->pNextHash->pPrevHash==0 );
3599 pPg->pNextHash->pPrevHash = pPg;
3600 }
3601
danielk19773c407372005-02-15 02:54:14 +00003602#ifdef SQLITE_CHECK_PAGES
3603 pPg->pageHash = pager_pagehash(pPg);
3604#endif
drhed7c8552001-04-11 14:29:21 +00003605 }else{
drhd9b02572001-04-15 00:37:09 +00003606 /* The requested page is in the page cache. */
danielk1977e277be02007-03-23 18:12:06 +00003607 assert(pPager->nRef>0 || pgno==1);
drh538f5702007-04-13 02:14:30 +00003608 PAGER_INCR(pPager->nHit);
drhd33d5a82007-04-26 12:11:28 +00003609 if( !noContent ){
3610 rc = pager_get_content(pPg);
3611 if( rc ){
3612 return rc;
3613 }
3614 }
drhdf0b3b02001-06-23 11:36:20 +00003615 page_ref(pPg);
drhed7c8552001-04-11 14:29:21 +00003616 }
danielk19773b8a05f2007-03-19 17:44:26 +00003617 *ppPage = pPg;
drhed7c8552001-04-11 14:29:21 +00003618 return SQLITE_OK;
3619}
drh86f8c192007-08-22 00:39:19 +00003620int sqlite3PagerAcquire(
3621 Pager *pPager, /* The pager open on the database file */
3622 Pgno pgno, /* Page number to fetch */
3623 DbPage **ppPage, /* Write a pointer to the page here */
3624 int noContent /* Do not bother reading content from disk if true */
3625){
3626 int rc;
3627 pagerEnter(pPager);
3628 rc = pagerAcquire(pPager, pgno, ppPage, noContent);
3629 pagerLeave(pPager);
3630 return rc;
3631}
3632
drhed7c8552001-04-11 14:29:21 +00003633
3634/*
drh7e3b0a02001-04-28 16:52:40 +00003635** Acquire a page if it is already in the in-memory cache. Do
3636** not read the page from disk. Return a pointer to the page,
3637** or 0 if the page is not in cache.
3638**
danielk19773b8a05f2007-03-19 17:44:26 +00003639** See also sqlite3PagerGet(). The difference between this routine
3640** and sqlite3PagerGet() is that _get() will go to the disk and read
drh7e3b0a02001-04-28 16:52:40 +00003641** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00003642** returns NULL if the page is not in cache or if a disk I/O error
3643** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00003644*/
danielk19773b8a05f2007-03-19 17:44:26 +00003645DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
drh86f8c192007-08-22 00:39:19 +00003646 PgHdr *pPg = 0;
drh7e3b0a02001-04-28 16:52:40 +00003647
drh836faa42003-01-11 13:30:57 +00003648 assert( pPager!=0 );
3649 assert( pgno!=0 );
danielk1977e277be02007-03-23 18:12:06 +00003650
drh86f8c192007-08-22 00:39:19 +00003651 pagerEnter(pPager);
danielk1977e277be02007-03-23 18:12:06 +00003652 if( pPager->state==PAGER_UNLOCK ){
danielk1977334cdb62007-03-26 08:05:12 +00003653 assert( !pPager->pAll || pPager->exclusiveMode );
drh86f8c192007-08-22 00:39:19 +00003654 }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
3655 /* Do nothing */
3656 }else if( (pPg = pager_lookup(pPager, pgno))!=0 ){
3657 page_ref(pPg);
danielk1977e277be02007-03-23 18:12:06 +00003658 }
drh86f8c192007-08-22 00:39:19 +00003659 pagerLeave(pPager);
danielk19773b8a05f2007-03-19 17:44:26 +00003660 return pPg;
drh7e3b0a02001-04-28 16:52:40 +00003661}
3662
3663/*
drhed7c8552001-04-11 14:29:21 +00003664** Release a page.
3665**
3666** If the number of references to the page drop to zero, then the
3667** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00003668** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00003669** removed.
3670*/
danielk19773b8a05f2007-03-19 17:44:26 +00003671int sqlite3PagerUnref(DbPage *pPg){
drhd9b02572001-04-15 00:37:09 +00003672
3673 /* Decrement the reference count for this page
3674 */
drhed7c8552001-04-11 14:29:21 +00003675 assert( pPg->nRef>0 );
drh86f8c192007-08-22 00:39:19 +00003676 pagerEnter(pPg->pPager);
drhed7c8552001-04-11 14:29:21 +00003677 pPg->nRef--;
drhdd793422001-06-28 01:54:48 +00003678 REFINFO(pPg);
drhd9b02572001-04-15 00:37:09 +00003679
danielk19773c407372005-02-15 02:54:14 +00003680 CHECK_PAGE(pPg);
3681
drh72f82862001-05-24 21:06:34 +00003682 /* When the number of references to a page reach 0, call the
3683 ** destructor and add the page to the freelist.
drhd9b02572001-04-15 00:37:09 +00003684 */
drhed7c8552001-04-11 14:29:21 +00003685 if( pPg->nRef==0 ){
danielk19779f61c2f2007-08-27 17:27:49 +00003686 Pager *pPager = pPg->pPager;
3687
3688 lruListAdd(pPg);
drh72f82862001-05-24 21:06:34 +00003689 if( pPager->xDestructor ){
danielk19773b8a05f2007-03-19 17:44:26 +00003690 pPager->xDestructor(pPg, pPager->pageSize);
drh72f82862001-05-24 21:06:34 +00003691 }
drhd9b02572001-04-15 00:37:09 +00003692
3693 /* When all pages reach the freelist, drop the read lock from
3694 ** the database file.
3695 */
3696 pPager->nRef--;
3697 assert( pPager->nRef>=0 );
danielk19773dedc192007-03-27 17:37:31 +00003698 if( pPager->nRef==0 && (!pPager->exclusiveMode || pPager->journalOff>0) ){
danielk1977e277be02007-03-23 18:12:06 +00003699 pagerUnlockAndRollback(pPager);
drhd9b02572001-04-15 00:37:09 +00003700 }
drhed7c8552001-04-11 14:29:21 +00003701 }
drh86f8c192007-08-22 00:39:19 +00003702 pagerLeave(pPg->pPager);
drhd9b02572001-04-15 00:37:09 +00003703 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00003704}
3705
3706/*
drha6abd042004-06-09 17:37:22 +00003707** Create a journal file for pPager. There should already be a RESERVED
3708** or EXCLUSIVE lock on the database file when this routine is called.
drhda47d772002-12-02 04:25:19 +00003709**
3710** Return SQLITE_OK if everything. Return an error code and release the
3711** write lock if anything goes wrong.
3712*/
3713static int pager_open_journal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00003714 sqlite3_vfs *pVfs = pPager->pVfs;
3715 int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE);
3716
drhda47d772002-12-02 04:25:19 +00003717 int rc;
drhb7f91642004-10-31 02:22:47 +00003718 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +00003719 assert( pPager->state>=PAGER_RESERVED );
drhda47d772002-12-02 04:25:19 +00003720 assert( pPager->journalOpen==0 );
3721 assert( pPager->useJournal );
drh88b01a12005-03-28 18:04:27 +00003722 assert( pPager->aInJournal==0 );
danielk19773b8a05f2007-03-19 17:44:26 +00003723 sqlite3PagerPagecount(pPager);
drhed138fb2007-08-22 22:04:37 +00003724 pagerLeave(pPager);
drh17435752007-08-16 04:30:38 +00003725 pPager->aInJournal = sqlite3MallocZero( pPager->dbSize/8 + 1 );
drhed138fb2007-08-22 22:04:37 +00003726 pagerEnter(pPager);
drhda47d772002-12-02 04:25:19 +00003727 if( pPager->aInJournal==0 ){
drh9c105bb2004-10-02 20:38:28 +00003728 rc = SQLITE_NOMEM;
3729 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003730 }
danielk1977b4b47412007-08-17 15:53:36 +00003731
3732 if( pPager->tempFile ){
danielk1977fee2d252007-08-18 10:59:19 +00003733 flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
3734 }else{
3735 flags |= (SQLITE_OPEN_MAIN_JOURNAL);
danielk1977b4b47412007-08-17 15:53:36 +00003736 }
danielk1977c7b60172007-08-22 11:22:03 +00003737#ifdef SQLITE_ENABLE_ATOMIC_WRITE
3738 rc = sqlite3JournalOpen(
3739 pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
3740 );
3741#else
danielk1977b4b47412007-08-17 15:53:36 +00003742 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
danielk1977c7b60172007-08-22 11:22:03 +00003743#endif
danielk1977b4b47412007-08-17 15:53:36 +00003744 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
danielk197776572402004-06-25 02:38:54 +00003745 pPager->journalOff = 0;
3746 pPager->setMaster = 0;
3747 pPager->journalHdr = 0;
drhda47d772002-12-02 04:25:19 +00003748 if( rc!=SQLITE_OK ){
drh600e46a2007-03-28 01:59:33 +00003749 if( rc==SQLITE_NOMEM ){
danielk1977fee2d252007-08-18 10:59:19 +00003750 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
drh600e46a2007-03-28 01:59:33 +00003751 }
drh9c105bb2004-10-02 20:38:28 +00003752 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003753 }
3754 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00003755 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00003756 pPager->needSync = 0;
3757 pPager->alwaysRollback = 0;
drh968af522003-02-11 14:55:40 +00003758 pPager->nRec = 0;
danielk1977efaaf572006-01-16 11:29:19 +00003759 if( pPager->errCode ){
3760 rc = pPager->errCode;
drhdd5b2fa2005-03-28 03:39:55 +00003761 goto failed_to_open_journal;
drh2e6d11b2003-04-25 15:37:57 +00003762 }
drhda47d772002-12-02 04:25:19 +00003763 pPager->origDbSize = pPager->dbSize;
drhae2b40c2004-06-09 19:03:54 +00003764
danielk197776572402004-06-25 02:38:54 +00003765 rc = writeJournalHdr(pPager);
3766
drhac69b052004-05-12 13:30:07 +00003767 if( pPager->stmtAutoopen && rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00003768 rc = sqlite3PagerStmtBegin(pPager);
drhda47d772002-12-02 04:25:19 +00003769 }
danielk1977261919c2005-12-06 12:52:59 +00003770 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM ){
drh80e35f42007-03-30 14:06:34 +00003771 rc = pager_end_transaction(pPager);
drhda47d772002-12-02 04:25:19 +00003772 if( rc==SQLITE_OK ){
3773 rc = SQLITE_FULL;
3774 }
3775 }
drh9c105bb2004-10-02 20:38:28 +00003776 return rc;
3777
3778failed_to_open_journal:
drh17435752007-08-16 04:30:38 +00003779 sqlite3_free(pPager->aInJournal);
drh9c105bb2004-10-02 20:38:28 +00003780 pPager->aInJournal = 0;
drh9c105bb2004-10-02 20:38:28 +00003781 return rc;
drhda47d772002-12-02 04:25:19 +00003782}
3783
3784/*
drh4b845d72002-03-05 12:41:19 +00003785** Acquire a write-lock on the database. The lock is removed when
3786** the any of the following happen:
3787**
drh80e35f42007-03-30 14:06:34 +00003788** * sqlite3PagerCommitPhaseTwo() is called.
danielk19773b8a05f2007-03-19 17:44:26 +00003789** * sqlite3PagerRollback() is called.
3790** * sqlite3PagerClose() is called.
3791** * sqlite3PagerUnref() is called to on every outstanding page.
drh4b845d72002-03-05 12:41:19 +00003792**
danielk197713adf8a2004-06-03 16:08:41 +00003793** The first parameter to this routine is a pointer to any open page of the
3794** database file. Nothing changes about the page - it is used merely to
3795** acquire a pointer to the Pager structure and as proof that there is
3796** already a read-lock on the database.
drh4b845d72002-03-05 12:41:19 +00003797**
danielk197713adf8a2004-06-03 16:08:41 +00003798** The second parameter indicates how much space in bytes to reserve for a
3799** master journal file-name at the start of the journal when it is created.
3800**
3801** A journal file is opened if this is not a temporary file. For temporary
3802** files, the opening of the journal file is deferred until there is an
3803** actual need to write to the journal.
drhda47d772002-12-02 04:25:19 +00003804**
drha6abd042004-06-09 17:37:22 +00003805** If the database is already reserved for writing, this routine is a no-op.
drh684917c2004-10-05 02:41:42 +00003806**
3807** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file
3808** immediately instead of waiting until we try to flush the cache. The
3809** exFlag is ignored if a transaction is already active.
drh4b845d72002-03-05 12:41:19 +00003810*/
danielk19773b8a05f2007-03-19 17:44:26 +00003811int sqlite3PagerBegin(DbPage *pPg, int exFlag){
drh4b845d72002-03-05 12:41:19 +00003812 Pager *pPager = pPg->pPager;
3813 int rc = SQLITE_OK;
drh86f8c192007-08-22 00:39:19 +00003814 pagerEnter(pPager);
drh4b845d72002-03-05 12:41:19 +00003815 assert( pPg->nRef>0 );
drha6abd042004-06-09 17:37:22 +00003816 assert( pPager->state!=PAGER_UNLOCK );
3817 if( pPager->state==PAGER_SHARED ){
drh4b845d72002-03-05 12:41:19 +00003818 assert( pPager->aInJournal==0 );
drhb7f91642004-10-31 02:22:47 +00003819 if( MEMDB ){
drha6abd042004-06-09 17:37:22 +00003820 pPager->state = PAGER_EXCLUSIVE;
drhac69b052004-05-12 13:30:07 +00003821 pPager->origDbSize = pPager->dbSize;
3822 }else{
drh054889e2005-11-30 03:20:31 +00003823 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
drh684917c2004-10-05 02:41:42 +00003824 if( rc==SQLITE_OK ){
3825 pPager->state = PAGER_RESERVED;
3826 if( exFlag ){
3827 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
3828 }
3829 }
danielk19779eed5052004-06-04 10:38:30 +00003830 if( rc!=SQLITE_OK ){
drh86f8c192007-08-22 00:39:19 +00003831 pagerLeave(pPager);
danielk19779eed5052004-06-04 10:38:30 +00003832 return rc;
drhac69b052004-05-12 13:30:07 +00003833 }
drha6abd042004-06-09 17:37:22 +00003834 pPager->dirtyCache = 0;
drh4f0c5872007-03-26 22:05:01 +00003835 PAGERTRACE2("TRANSACTION %d\n", PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00003836 if( pPager->useJournal && !pPager->tempFile ){
3837 rc = pager_open_journal(pPager);
3838 }
drh4b845d72002-03-05 12:41:19 +00003839 }
danielk1977334cdb62007-03-26 08:05:12 +00003840 }else if( pPager->journalOpen && pPager->journalOff==0 ){
3841 /* This happens when the pager was in exclusive-access mode last
3842 ** time a (read or write) transaction was successfully concluded
3843 ** by this connection. Instead of deleting the journal file it was
3844 ** kept open and truncated to 0 bytes.
3845 */
3846 assert( pPager->nRec==0 );
3847 assert( pPager->origDbSize==0 );
drh369339d2007-03-30 16:01:55 +00003848 assert( pPager->aInJournal==0 );
danielk1977334cdb62007-03-26 08:05:12 +00003849 sqlite3PagerPagecount(pPager);
drhed138fb2007-08-22 22:04:37 +00003850 pagerLeave(pPager);
drh17435752007-08-16 04:30:38 +00003851 pPager->aInJournal = sqlite3MallocZero( pPager->dbSize/8 + 1 );
drhed138fb2007-08-22 22:04:37 +00003852 pagerEnter(pPager);
danielk1977334cdb62007-03-26 08:05:12 +00003853 if( !pPager->aInJournal ){
3854 rc = SQLITE_NOMEM;
3855 }else{
drh369339d2007-03-30 16:01:55 +00003856 pPager->origDbSize = pPager->dbSize;
danielk1977334cdb62007-03-26 08:05:12 +00003857 rc = writeJournalHdr(pPager);
3858 }
drh4b845d72002-03-05 12:41:19 +00003859 }
danielk1977334cdb62007-03-26 08:05:12 +00003860 assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK );
drh86f8c192007-08-22 00:39:19 +00003861 pagerLeave(pPager);
drh4b845d72002-03-05 12:41:19 +00003862 return rc;
3863}
3864
3865/*
drh605ad8f2006-05-03 23:34:05 +00003866** Make a page dirty. Set its dirty flag and add it to the dirty
3867** page list.
3868*/
3869static void makeDirty(PgHdr *pPg){
3870 if( pPg->dirty==0 ){
3871 Pager *pPager = pPg->pPager;
3872 pPg->dirty = 1;
3873 pPg->pDirty = pPager->pDirty;
3874 if( pPager->pDirty ){
3875 pPager->pDirty->pPrevDirty = pPg;
3876 }
3877 pPg->pPrevDirty = 0;
3878 pPager->pDirty = pPg;
3879 }
3880}
3881
3882/*
3883** Make a page clean. Clear its dirty bit and remove it from the
3884** dirty page list.
3885*/
3886static void makeClean(PgHdr *pPg){
3887 if( pPg->dirty ){
3888 pPg->dirty = 0;
3889 if( pPg->pDirty ){
drh153c62c2007-08-24 03:51:33 +00003890 assert( pPg->pDirty->pPrevDirty==pPg );
drh605ad8f2006-05-03 23:34:05 +00003891 pPg->pDirty->pPrevDirty = pPg->pPrevDirty;
3892 }
3893 if( pPg->pPrevDirty ){
drh153c62c2007-08-24 03:51:33 +00003894 assert( pPg->pPrevDirty->pDirty==pPg );
drh605ad8f2006-05-03 23:34:05 +00003895 pPg->pPrevDirty->pDirty = pPg->pDirty;
3896 }else{
drh153c62c2007-08-24 03:51:33 +00003897 assert( pPg->pPager->pDirty==pPg );
drh605ad8f2006-05-03 23:34:05 +00003898 pPg->pPager->pDirty = pPg->pDirty;
3899 }
3900 }
3901}
3902
3903
3904/*
drhed7c8552001-04-11 14:29:21 +00003905** Mark a data page as writeable. The page is written into the journal
3906** if it is not there already. This routine must be called before making
3907** changes to a page.
3908**
3909** The first time this routine is called, the pager creates a new
drha6abd042004-06-09 17:37:22 +00003910** journal and acquires a RESERVED lock on the database. If the RESERVED
drhed7c8552001-04-11 14:29:21 +00003911** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00003912** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00003913** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00003914**
3915** If the journal file could not be written because the disk is full,
3916** then this routine returns SQLITE_FULL and does an immediate rollback.
3917** All subsequent write attempts also return SQLITE_FULL until there
danielk19773b8a05f2007-03-19 17:44:26 +00003918** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to
drhd9b02572001-04-15 00:37:09 +00003919** reset.
drhed7c8552001-04-11 14:29:21 +00003920*/
danielk19773b8a05f2007-03-19 17:44:26 +00003921static int pager_write(PgHdr *pPg){
3922 void *pData = PGHDR_TO_DATA(pPg);
drh69688d52001-04-14 16:38:23 +00003923 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00003924 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00003925
drh6446c4d2001-12-15 14:22:18 +00003926 /* Check for errors
3927 */
danielk1977efaaf572006-01-16 11:29:19 +00003928 if( pPager->errCode ){
3929 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003930 }
drh5e00f6c2001-09-13 13:46:56 +00003931 if( pPager->readOnly ){
3932 return SQLITE_PERM;
3933 }
drh6446c4d2001-12-15 14:22:18 +00003934
danielk197776572402004-06-25 02:38:54 +00003935 assert( !pPager->setMaster );
3936
danielk19773c407372005-02-15 02:54:14 +00003937 CHECK_PAGE(pPg);
3938
drh538f5702007-04-13 02:14:30 +00003939 /* If this page was previously acquired with noContent==1, that means
3940 ** we didn't really read in the content of the page. This can happen
3941 ** (for example) when the page is being moved to the freelist. But
3942 ** now we are (perhaps) moving the page off of the freelist for
3943 ** reuse and we need to know its original content so that content
3944 ** can be stored in the rollback journal. So do the read at this
3945 ** time.
3946 */
drhd33d5a82007-04-26 12:11:28 +00003947 rc = pager_get_content(pPg);
3948 if( rc ){
3949 return rc;
drh538f5702007-04-13 02:14:30 +00003950 }
3951
drh6446c4d2001-12-15 14:22:18 +00003952 /* Mark the page as dirty. If the page has already been written
3953 ** to the journal then we can return right away.
3954 */
drh605ad8f2006-05-03 23:34:05 +00003955 makeDirty(pPg);
danielk1977f35843b2007-04-07 15:03:17 +00003956 if( pPg->inJournal && (pageInStatement(pPg) || pPager->stmtInUse==0) ){
drha6abd042004-06-09 17:37:22 +00003957 pPager->dirtyCache = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00003958 }else{
drh6446c4d2001-12-15 14:22:18 +00003959
danielk1977a0bf2652004-11-04 14:30:04 +00003960 /* If we get this far, it means that the page needs to be
3961 ** written to the transaction journal or the ckeckpoint journal
3962 ** or both.
3963 **
3964 ** First check to see that the transaction journal exists and
3965 ** create it if it does not.
3966 */
3967 assert( pPager->state!=PAGER_UNLOCK );
danielk19773b8a05f2007-03-19 17:44:26 +00003968 rc = sqlite3PagerBegin(pPg, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00003969 if( rc!=SQLITE_OK ){
3970 return rc;
3971 }
3972 assert( pPager->state>=PAGER_RESERVED );
3973 if( !pPager->journalOpen && pPager->useJournal ){
3974 rc = pager_open_journal(pPager);
3975 if( rc!=SQLITE_OK ) return rc;
3976 }
3977 assert( pPager->journalOpen || !pPager->useJournal );
3978 pPager->dirtyCache = 1;
3979
3980 /* The transaction journal now exists and we have a RESERVED or an
3981 ** EXCLUSIVE lock on the main database file. Write the current page to
3982 ** the transaction journal if it is not there already.
3983 */
3984 if( !pPg->inJournal && (pPager->useJournal || MEMDB) ){
3985 if( (int)pPg->pgno <= pPager->origDbSize ){
3986 int szPg;
danielk1977a0bf2652004-11-04 14:30:04 +00003987 if( MEMDB ){
3988 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
drh4f0c5872007-03-26 22:05:01 +00003989 PAGERTRACE3("JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00003990 assert( pHist->pOrig==0 );
drh17435752007-08-16 04:30:38 +00003991 pHist->pOrig = sqlite3_malloc( pPager->pageSize );
danielk1977a0bf2652004-11-04 14:30:04 +00003992 if( pHist->pOrig ){
3993 memcpy(pHist->pOrig, PGHDR_TO_DATA(pPg), pPager->pageSize);
3994 }
3995 }else{
drhc001c582006-03-06 18:23:16 +00003996 u32 cksum, saved;
3997 char *pData2, *pEnd;
danielk1977dd97a492007-08-22 18:54:32 +00003998
drh267cb322005-09-16 17:16:52 +00003999 /* We should never write to the journal file the page that
4000 ** contains the database locks. The following assert verifies
4001 ** that we do not. */
4002 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
drhc001c582006-03-06 18:23:16 +00004003 pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
drh37527852006-03-16 16:19:56 +00004004 cksum = pager_cksum(pPager, (u8*)pData2);
drhc001c582006-03-06 18:23:16 +00004005 pEnd = pData2 + pPager->pageSize;
4006 pData2 -= 4;
4007 saved = *(u32*)pEnd;
4008 put32bits(pEnd, cksum);
danielk1977a0bf2652004-11-04 14:30:04 +00004009 szPg = pPager->pageSize+8;
drhc001c582006-03-06 18:23:16 +00004010 put32bits(pData2, pPg->pgno);
danielk197762079062007-08-15 17:08:46 +00004011 rc = sqlite3OsWrite(pPager->jfd, pData2, szPg, pPager->journalOff);
drhb0603412007-02-28 04:47:26 +00004012 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
drh538f5702007-04-13 02:14:30 +00004013 pPager->journalOff, szPg));
4014 PAGER_INCR(sqlite3_pager_writej_count);
danielk1977a0bf2652004-11-04 14:30:04 +00004015 pPager->journalOff += szPg;
drh477731b2007-06-16 03:06:27 +00004016 PAGERTRACE5("JOURNAL %d page %d needSync=%d hash(%08x)\n",
4017 PAGERID(pPager), pPg->pgno, pPg->needSync, pager_pagehash(pPg));
drhc001c582006-03-06 18:23:16 +00004018 *(u32*)pEnd = saved;
danielk197707cb5602006-01-20 10:55:05 +00004019
drhfd131da2007-08-07 17:13:03 +00004020 /* An error has occured writing to the journal file. The
danielk197707cb5602006-01-20 10:55:05 +00004021 ** transaction will be rolled back by the layer above.
4022 */
danielk1977a0bf2652004-11-04 14:30:04 +00004023 if( rc!=SQLITE_OK ){
danielk1977a0bf2652004-11-04 14:30:04 +00004024 return rc;
4025 }
danielk197707cb5602006-01-20 10:55:05 +00004026
danielk1977a0bf2652004-11-04 14:30:04 +00004027 pPager->nRec++;
4028 assert( pPager->aInJournal!=0 );
4029 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
4030 pPg->needSync = !pPager->noSync;
4031 if( pPager->stmtInUse ){
4032 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
danielk1977a0bf2652004-11-04 14:30:04 +00004033 }
drhac69b052004-05-12 13:30:07 +00004034 }
danielk197713adf8a2004-06-03 16:08:41 +00004035 }else{
danielk1977a0bf2652004-11-04 14:30:04 +00004036 pPg->needSync = !pPager->journalStarted && !pPager->noSync;
drh4f0c5872007-03-26 22:05:01 +00004037 PAGERTRACE4("APPEND %d page %d needSync=%d\n",
danielk1977ef73ee92004-11-06 12:26:07 +00004038 PAGERID(pPager), pPg->pgno, pPg->needSync);
danielk1977a0bf2652004-11-04 14:30:04 +00004039 }
4040 if( pPg->needSync ){
4041 pPager->needSync = 1;
4042 }
4043 pPg->inJournal = 1;
4044 }
4045
4046 /* If the statement journal is open and the page is not in it,
4047 ** then write the current page to the statement journal. Note that
4048 ** the statement journal format differs from the standard journal format
4049 ** in that it omits the checksums and the header.
4050 */
danielk1977f35843b2007-04-07 15:03:17 +00004051 if( pPager->stmtInUse
4052 && !pageInStatement(pPg)
4053 && (int)pPg->pgno<=pPager->stmtSize
4054 ){
danielk1977a0bf2652004-11-04 14:30:04 +00004055 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
4056 if( MEMDB ){
4057 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4058 assert( pHist->pStmt==0 );
drh17435752007-08-16 04:30:38 +00004059 pHist->pStmt = sqlite3_malloc( pPager->pageSize );
danielk1977a0bf2652004-11-04 14:30:04 +00004060 if( pHist->pStmt ){
4061 memcpy(pHist->pStmt, PGHDR_TO_DATA(pPg), pPager->pageSize);
4062 }
drh4f0c5872007-03-26 22:05:01 +00004063 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk1977f35843b2007-04-07 15:03:17 +00004064 page_add_to_stmt_list(pPg);
danielk1977a0bf2652004-11-04 14:30:04 +00004065 }else{
danielk197762079062007-08-15 17:08:46 +00004066 i64 offset = pPager->stmtNRec*(4+pPager->pageSize);
drhc001c582006-03-06 18:23:16 +00004067 char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7)-4;
4068 put32bits(pData2, pPg->pgno);
danielk197762079062007-08-15 17:08:46 +00004069 rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize+4, offset);
drh4f0c5872007-03-26 22:05:01 +00004070 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
drhac69b052004-05-12 13:30:07 +00004071 if( rc!=SQLITE_OK ){
drhac69b052004-05-12 13:30:07 +00004072 return rc;
4073 }
danielk1977a0bf2652004-11-04 14:30:04 +00004074 pPager->stmtNRec++;
4075 assert( pPager->aInStmt!=0 );
4076 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drhdb48ee02003-01-16 13:42:43 +00004077 }
drhd9b02572001-04-15 00:37:09 +00004078 }
drhfa86c412002-02-02 15:01:15 +00004079 }
4080
4081 /* Update the database size and return.
4082 */
drh1aa2d8b2007-01-03 15:34:29 +00004083 assert( pPager->state>=PAGER_SHARED );
drh1ab43002002-01-14 09:28:19 +00004084 if( pPager->dbSize<(int)pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00004085 pPager->dbSize = pPg->pgno;
drhb7f91642004-10-31 02:22:47 +00004086 if( !MEMDB && pPager->dbSize==PENDING_BYTE/pPager->pageSize ){
drh1f595712004-06-15 01:40:29 +00004087 pPager->dbSize++;
4088 }
drh306dc212001-05-21 13:45:10 +00004089 }
drh69688d52001-04-14 16:38:23 +00004090 return rc;
drhed7c8552001-04-11 14:29:21 +00004091}
4092
4093/*
danielk19774099f6e2007-03-19 11:25:20 +00004094** This function is used to mark a data-page as writable. It uses
4095** pager_write() to open a journal file (if it is not already open)
4096** and write the page *pData to the journal.
4097**
4098** The difference between this function and pager_write() is that this
4099** function also deals with the special case where 2 or more pages
4100** fit on a single disk sector. In this case all co-resident pages
4101** must have been written to the journal file before returning.
4102*/
danielk19773b8a05f2007-03-19 17:44:26 +00004103int sqlite3PagerWrite(DbPage *pDbPage){
danielk19774099f6e2007-03-19 11:25:20 +00004104 int rc = SQLITE_OK;
4105
danielk19773b8a05f2007-03-19 17:44:26 +00004106 PgHdr *pPg = pDbPage;
danielk19774099f6e2007-03-19 11:25:20 +00004107 Pager *pPager = pPg->pPager;
4108 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
4109
drh86f8c192007-08-22 00:39:19 +00004110 pagerEnter(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00004111 if( !MEMDB && nPagePerSector>1 ){
4112 Pgno nPageCount; /* Total number of pages in database file */
4113 Pgno pg1; /* First page of the sector pPg is located on. */
4114 int nPage; /* Number of pages starting at pg1 to journal */
4115 int ii;
danielk1977dd97a492007-08-22 18:54:32 +00004116 int needSync = 0;
danielk19774099f6e2007-03-19 11:25:20 +00004117
4118 /* Set the doNotSync flag to 1. This is because we cannot allow a journal
4119 ** header to be written between the pages journaled by this function.
4120 */
4121 assert( pPager->doNotSync==0 );
4122 pPager->doNotSync = 1;
4123
4124 /* This trick assumes that both the page-size and sector-size are
4125 ** an integer power of 2. It sets variable pg1 to the identifier
4126 ** of the first page of the sector pPg is located on.
4127 */
4128 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
4129
danielk19773b8a05f2007-03-19 17:44:26 +00004130 nPageCount = sqlite3PagerPagecount(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00004131 if( pPg->pgno>nPageCount ){
4132 nPage = (pPg->pgno - pg1)+1;
4133 }else if( (pg1+nPagePerSector-1)>nPageCount ){
4134 nPage = nPageCount+1-pg1;
4135 }else{
4136 nPage = nPagePerSector;
4137 }
4138 assert(nPage>0);
4139 assert(pg1<=pPg->pgno);
4140 assert((pg1+nPage)>pPg->pgno);
4141
4142 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
4143 Pgno pg = pg1+ii;
danielk1977dd97a492007-08-22 18:54:32 +00004144 PgHdr *pPage;
danielk19774099f6e2007-03-19 11:25:20 +00004145 if( !pPager->aInJournal || pg==pPg->pgno ||
4146 pg>pPager->origDbSize || !(pPager->aInJournal[pg/8]&(1<<(pg&7)))
4147 ) {
4148 if( pg!=PAGER_MJ_PGNO(pPager) ){
danielk19773b8a05f2007-03-19 17:44:26 +00004149 rc = sqlite3PagerGet(pPager, pg, &pPage);
danielk19774099f6e2007-03-19 11:25:20 +00004150 if( rc==SQLITE_OK ){
4151 rc = pager_write(pPage);
danielk1977dd97a492007-08-22 18:54:32 +00004152 if( pPage->needSync ){
4153 needSync = 1;
4154 }
danielk19773b8a05f2007-03-19 17:44:26 +00004155 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00004156 }
4157 }
danielk1977dd97a492007-08-22 18:54:32 +00004158 }else if( (pPage = pager_lookup(pPager, pg)) ){
4159 if( pPage->needSync ){
4160 needSync = 1;
4161 }
danielk19774099f6e2007-03-19 11:25:20 +00004162 }
4163 }
4164
danielk1977dd97a492007-08-22 18:54:32 +00004165 /* If the PgHdr.needSync flag is set for any of the nPage pages
4166 ** starting at pg1, then it needs to be set for all of them. Because
4167 ** writing to any of these nPage pages may damage the others, the
4168 ** journal file must contain sync()ed copies of all of them
4169 ** before any of them can be written out to the database file.
4170 */
4171 if( needSync ){
4172 for(ii=0; ii<nPage && needSync; ii++){
4173 PgHdr *pPage = pager_lookup(pPager, pg1+ii);
4174 if( pPage ) pPage->needSync = 1;
4175 }
4176 assert(pPager->needSync);
4177 }
4178
danielk19774099f6e2007-03-19 11:25:20 +00004179 assert( pPager->doNotSync==1 );
4180 pPager->doNotSync = 0;
4181 }else{
danielk19773b8a05f2007-03-19 17:44:26 +00004182 rc = pager_write(pDbPage);
danielk19774099f6e2007-03-19 11:25:20 +00004183 }
drh86f8c192007-08-22 00:39:19 +00004184 pagerLeave(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00004185 return rc;
4186}
4187
4188/*
drhaacc5432002-01-06 17:07:40 +00004189** Return TRUE if the page given in the argument was previously passed
danielk19773b8a05f2007-03-19 17:44:26 +00004190** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
drh6019e162001-07-02 17:51:45 +00004191** to change the content of the page.
4192*/
danielk19777d3a6662006-01-23 16:21:05 +00004193#ifndef NDEBUG
danielk19773b8a05f2007-03-19 17:44:26 +00004194int sqlite3PagerIswriteable(DbPage *pPg){
drh6019e162001-07-02 17:51:45 +00004195 return pPg->dirty;
4196}
danielk19777d3a6662006-01-23 16:21:05 +00004197#endif
drh6019e162001-07-02 17:51:45 +00004198
danielk1977b84f96f2005-01-20 11:32:23 +00004199#ifndef SQLITE_OMIT_VACUUM
drh6019e162001-07-02 17:51:45 +00004200/*
drh001bbcb2003-03-19 03:14:00 +00004201** Replace the content of a single page with the information in the third
4202** argument.
4203*/
danielk19773b8a05f2007-03-19 17:44:26 +00004204int sqlite3PagerOverwrite(Pager *pPager, Pgno pgno, void *pData){
4205 PgHdr *pPg;
drh001bbcb2003-03-19 03:14:00 +00004206 int rc;
4207
drh86f8c192007-08-22 00:39:19 +00004208 pagerEnter(pPager);
danielk19773b8a05f2007-03-19 17:44:26 +00004209 rc = sqlite3PagerGet(pPager, pgno, &pPg);
drh001bbcb2003-03-19 03:14:00 +00004210 if( rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00004211 rc = sqlite3PagerWrite(pPg);
drh001bbcb2003-03-19 03:14:00 +00004212 if( rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00004213 memcpy(sqlite3PagerGetData(pPg), pData, pPager->pageSize);
drh001bbcb2003-03-19 03:14:00 +00004214 }
danielk19773b8a05f2007-03-19 17:44:26 +00004215 sqlite3PagerUnref(pPg);
drh001bbcb2003-03-19 03:14:00 +00004216 }
drh86f8c192007-08-22 00:39:19 +00004217 pagerLeave(pPager);
drh001bbcb2003-03-19 03:14:00 +00004218 return rc;
4219}
danielk1977b84f96f2005-01-20 11:32:23 +00004220#endif
drh001bbcb2003-03-19 03:14:00 +00004221
4222/*
drh30e58752002-03-02 20:41:57 +00004223** A call to this routine tells the pager that it is not necessary to
drh538f5702007-04-13 02:14:30 +00004224** write the information on page pPg back to the disk, even though
drh30e58752002-03-02 20:41:57 +00004225** that page might be marked as dirty.
4226**
4227** The overlying software layer calls this routine when all of the data
4228** on the given page is unused. The pager marks the page as clean so
4229** that it does not get written to disk.
4230**
4231** Tests show that this optimization, together with the
danielk19773b8a05f2007-03-19 17:44:26 +00004232** sqlite3PagerDontRollback() below, more than double the speed
drh30e58752002-03-02 20:41:57 +00004233** of large INSERT operations and quadruple the speed of large DELETEs.
drh8e298f92002-07-06 16:28:47 +00004234**
4235** When this routine is called, set the alwaysRollback flag to true.
danielk19773b8a05f2007-03-19 17:44:26 +00004236** Subsequent calls to sqlite3PagerDontRollback() for the same page
drh8e298f92002-07-06 16:28:47 +00004237** will thereafter be ignored. This is necessary to avoid a problem
4238** where a page with data is added to the freelist during one part of
4239** a transaction then removed from the freelist during a later part
4240** of the same transaction and reused for some other purpose. When it
4241** is first added to the freelist, this routine is called. When reused,
drh80e35f42007-03-30 14:06:34 +00004242** the sqlite3PagerDontRollback() routine is called. But because the
4243** page contains critical data, we still need to be sure it gets
4244** rolled back in spite of the sqlite3PagerDontRollback() call.
drh30e58752002-03-02 20:41:57 +00004245*/
drh538f5702007-04-13 02:14:30 +00004246void sqlite3PagerDontWrite(DbPage *pDbPage){
4247 PgHdr *pPg = pDbPage;
4248 Pager *pPager = pPg->pPager;
drh8e298f92002-07-06 16:28:47 +00004249
drhb7f91642004-10-31 02:22:47 +00004250 if( MEMDB ) return;
drh86f8c192007-08-22 00:39:19 +00004251 pagerEnter(pPager);
drh8e298f92002-07-06 16:28:47 +00004252 pPg->alwaysRollback = 1;
drh43617e92006-03-06 20:55:46 +00004253 if( pPg->dirty && !pPager->stmtInUse ){
drh1aa2d8b2007-01-03 15:34:29 +00004254 assert( pPager->state>=PAGER_SHARED );
drh8124a302002-06-25 14:43:57 +00004255 if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
4256 /* If this pages is the last page in the file and the file has grown
4257 ** during the current transaction, then do NOT mark the page as clean.
4258 ** When the database file grows, we must make sure that the last page
4259 ** gets written at least once so that the disk file will be the correct
4260 ** size. If you do not write this page and the size of the file
4261 ** on the disk ends up being too small, that can lead to database
4262 ** corruption during the next transaction.
4263 */
4264 }else{
drh538f5702007-04-13 02:14:30 +00004265 PAGERTRACE3("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager));
4266 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
drh605ad8f2006-05-03 23:34:05 +00004267 makeClean(pPg);
danielk19773c407372005-02-15 02:54:14 +00004268#ifdef SQLITE_CHECK_PAGES
4269 pPg->pageHash = pager_pagehash(pPg);
4270#endif
drh8124a302002-06-25 14:43:57 +00004271 }
drh30e58752002-03-02 20:41:57 +00004272 }
drh86f8c192007-08-22 00:39:19 +00004273 pagerLeave(pPager);
drh30e58752002-03-02 20:41:57 +00004274}
4275
4276/*
4277** A call to this routine tells the pager that if a rollback occurs,
4278** it is not necessary to restore the data on the given page. This
4279** means that the pager does not have to record the given page in the
4280** rollback journal.
drh538f5702007-04-13 02:14:30 +00004281**
4282** If we have not yet actually read the content of this page (if
4283** the PgHdr.needRead flag is set) then this routine acts as a promise
4284** that we will never need to read the page content in the future.
4285** so the needRead flag can be cleared at this point.
drh30e58752002-03-02 20:41:57 +00004286*/
danielk19773b8a05f2007-03-19 17:44:26 +00004287void sqlite3PagerDontRollback(DbPage *pPg){
drh30e58752002-03-02 20:41:57 +00004288 Pager *pPager = pPg->pPager;
4289
drh86f8c192007-08-22 00:39:19 +00004290 pagerEnter(pPager);
drhd3627af2006-12-18 18:34:51 +00004291 assert( pPager->state>=PAGER_RESERVED );
4292 if( pPager->journalOpen==0 ) return;
drhb7f91642004-10-31 02:22:47 +00004293 if( pPg->alwaysRollback || pPager->alwaysRollback || MEMDB ) return;
drh30e58752002-03-02 20:41:57 +00004294 if( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ){
4295 assert( pPager->aInJournal!=0 );
4296 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
4297 pPg->inJournal = 1;
drh538f5702007-04-13 02:14:30 +00004298 pPg->needRead = 0;
drhac69b052004-05-12 13:30:07 +00004299 if( pPager->stmtInUse ){
4300 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh30e58752002-03-02 20:41:57 +00004301 }
drh4f0c5872007-03-26 22:05:01 +00004302 PAGERTRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00004303 IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno))
drh30e58752002-03-02 20:41:57 +00004304 }
danielk1977f35843b2007-04-07 15:03:17 +00004305 if( pPager->stmtInUse
4306 && !pageInStatement(pPg)
4307 && (int)pPg->pgno<=pPager->stmtSize
4308 ){
drh30e58752002-03-02 20:41:57 +00004309 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
drhac69b052004-05-12 13:30:07 +00004310 assert( pPager->aInStmt!=0 );
4311 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh30e58752002-03-02 20:41:57 +00004312 }
drh86f8c192007-08-22 00:39:19 +00004313 pagerLeave(pPager);
drh30e58752002-03-02 20:41:57 +00004314}
4315
drhac69b052004-05-12 13:30:07 +00004316
drh30e58752002-03-02 20:41:57 +00004317/*
drh80e35f42007-03-30 14:06:34 +00004318** This routine is called to increment the database file change-counter,
4319** stored at byte 24 of the pager file.
4320*/
danielk1977c7b60172007-08-22 11:22:03 +00004321static int pager_incr_changecounter(Pager *pPager, int isDirect){
drh80e35f42007-03-30 14:06:34 +00004322 PgHdr *pPgHdr;
4323 u32 change_counter;
danielk1977c7b60172007-08-22 11:22:03 +00004324 int rc = SQLITE_OK;
drh80e35f42007-03-30 14:06:34 +00004325
4326 if( !pPager->changeCountDone ){
4327 /* Open page 1 of the file for writing. */
4328 rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
4329 if( rc!=SQLITE_OK ) return rc;
danielk1977c7b60172007-08-22 11:22:03 +00004330
4331 if( !isDirect ){
4332 rc = sqlite3PagerWrite(pPgHdr);
4333 if( rc!=SQLITE_OK ) return rc;
4334 }
4335
drh80e35f42007-03-30 14:06:34 +00004336 /* Increment the value just read and write it back to byte 24. */
drhb1003912007-07-20 00:33:36 +00004337 change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
drh80e35f42007-03-30 14:06:34 +00004338 change_counter++;
4339 put32bits(((char*)PGHDR_TO_DATA(pPgHdr))+24, change_counter);
danielk1977c7b60172007-08-22 11:22:03 +00004340
4341 if( isDirect && pPager->fd->pMethods ){
4342 const void *zBuf = PGHDR_TO_DATA(pPgHdr);
4343 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
4344 }
4345
drh80e35f42007-03-30 14:06:34 +00004346 /* Release the page reference. */
4347 sqlite3PagerUnref(pPgHdr);
4348 pPager->changeCountDone = 1;
4349 }
danielk1977c7b60172007-08-22 11:22:03 +00004350 return rc;
drh80e35f42007-03-30 14:06:34 +00004351}
4352
4353/*
4354** Sync the database file for the pager pPager. zMaster points to the name
4355** of a master journal file that should be written into the individual
4356** journal file. zMaster may be NULL, which is interpreted as no master
4357** journal (a single database transaction).
4358**
4359** This routine ensures that the journal is synced, all dirty pages written
4360** to the database file and the database file synced. The only thing that
4361** remains to commit the transaction is to delete the journal file (or
4362** master journal file if specified).
4363**
4364** Note that if zMaster==NULL, this does not overwrite a previous value
4365** passed to an sqlite3PagerCommitPhaseOne() call.
4366**
4367** If parameter nTrunc is non-zero, then the pager file is truncated to
4368** nTrunc pages (this is used by auto-vacuum databases).
4369*/
4370int sqlite3PagerCommitPhaseOne(Pager *pPager, const char *zMaster, Pgno nTrunc){
4371 int rc = SQLITE_OK;
4372
4373 PAGERTRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n",
4374 pPager->zFilename, zMaster, nTrunc);
drh86f8c192007-08-22 00:39:19 +00004375 pagerEnter(pPager);
drh80e35f42007-03-30 14:06:34 +00004376
4377 /* If this is an in-memory db, or no pages have been written to, or this
4378 ** function has already been called, it is a no-op.
4379 */
4380 if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){
4381 PgHdr *pPg;
drh80e35f42007-03-30 14:06:34 +00004382
danielk1977c7b60172007-08-22 11:22:03 +00004383#ifdef SQLITE_ENABLE_ATOMIC_WRITE
4384 /* The atomic-write optimization can be used if all of the
4385 ** following are true:
4386 **
4387 ** + The file-system supports the atomic-write property for
4388 ** blocks of size page-size, and
4389 ** + This commit is not part of a multi-file transaction, and
4390 ** + Exactly one page has been modified and store in the journal file.
4391 **
4392 ** If the optimization can be used, then the journal file will never
4393 ** be created for this transaction.
4394 */
danielk1977f55b8992007-08-24 08:15:53 +00004395 int useAtomicWrite = (
4396 !zMaster &&
4397 pPager->journalOff==jrnlBufferSize(pPager) &&
4398 nTrunc==0 &&
4399 (0==pPager->pDirty || 0==pPager->pDirty->pDirty)
4400 );
4401 if( useAtomicWrite ){
danielk1977c7b60172007-08-22 11:22:03 +00004402 /* Update the nRec field in the journal file. */
4403 int offset = pPager->journalHdr + sizeof(aJournalMagic);
4404 assert(pPager->nRec==1);
4405 rc = write32bits(pPager->jfd, offset, pPager->nRec);
4406
4407 /* Update the db file change counter. The following call will modify
4408 ** the in-memory representation of page 1 to include the updated
4409 ** change counter and then write page 1 directly to the database
4410 ** file. Because of the atomic-write property of the host file-system,
4411 ** this is safe.
4412 */
4413 rc = pager_incr_changecounter(pPager, 1);
danielk1977f55b8992007-08-24 08:15:53 +00004414 }else{
4415 rc = sqlite3JournalCreate(pPager->jfd);
4416 if( rc!=SQLITE_OK ) goto sync_exit;
4417 }
4418
4419 if( !useAtomicWrite )
danielk1977c7b60172007-08-22 11:22:03 +00004420#endif
4421
drh80e35f42007-03-30 14:06:34 +00004422 /* If a master journal file name has already been written to the
4423 ** journal file, then no sync is required. This happens when it is
4424 ** written, then the process fails to upgrade from a RESERVED to an
4425 ** EXCLUSIVE lock. The next time the process tries to commit the
4426 ** transaction the m-j name will have already been written.
4427 */
4428 if( !pPager->setMaster ){
danielk1977f55b8992007-08-24 08:15:53 +00004429 assert( pPager->journalOpen );
danielk1977c7b60172007-08-22 11:22:03 +00004430 rc = pager_incr_changecounter(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00004431 if( rc!=SQLITE_OK ) goto sync_exit;
4432#ifndef SQLITE_OMIT_AUTOVACUUM
4433 if( nTrunc!=0 ){
4434 /* If this transaction has made the database smaller, then all pages
4435 ** being discarded by the truncation must be written to the journal
4436 ** file.
4437 */
4438 Pgno i;
4439 int iSkip = PAGER_MJ_PGNO(pPager);
4440 for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){
4441 if( !(pPager->aInJournal[i/8] & (1<<(i&7))) && i!=iSkip ){
4442 rc = sqlite3PagerGet(pPager, i, &pPg);
4443 if( rc!=SQLITE_OK ) goto sync_exit;
4444 rc = sqlite3PagerWrite(pPg);
4445 sqlite3PagerUnref(pPg);
4446 if( rc!=SQLITE_OK ) goto sync_exit;
4447 }
4448 }
4449 }
4450#endif
4451 rc = writeMasterJournal(pPager, zMaster);
4452 if( rc!=SQLITE_OK ) goto sync_exit;
4453 rc = syncJournal(pPager);
drh80e35f42007-03-30 14:06:34 +00004454 }
danielk1977c7b60172007-08-22 11:22:03 +00004455 if( rc!=SQLITE_OK ) goto sync_exit;
drh80e35f42007-03-30 14:06:34 +00004456
4457#ifndef SQLITE_OMIT_AUTOVACUUM
4458 if( nTrunc!=0 ){
4459 rc = sqlite3PagerTruncate(pPager, nTrunc);
4460 if( rc!=SQLITE_OK ) goto sync_exit;
4461 }
4462#endif
4463
4464 /* Write all dirty pages to the database file */
4465 pPg = pager_get_all_dirty_pages(pPager);
4466 rc = pager_write_pagelist(pPg);
drh153c62c2007-08-24 03:51:33 +00004467 if( rc!=SQLITE_OK ){
4468 while( pPg && !pPg->dirty ){ pPg = pPg->pDirty; }
4469 pPager->pDirty = pPg;
4470 goto sync_exit;
4471 }
drh3cdd7d32007-03-30 14:46:01 +00004472 pPager->pDirty = 0;
drh80e35f42007-03-30 14:06:34 +00004473
4474 /* Sync the database file. */
drh1cc8c442007-08-24 16:08:29 +00004475 if( !pPager->noSync ){
danielk1977f036aef2007-08-20 05:36:51 +00004476 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
drh80e35f42007-03-30 14:06:34 +00004477 }
4478 IOTRACE(("DBSYNC %p\n", pPager))
4479
4480 pPager->state = PAGER_SYNCED;
4481 }else if( MEMDB && nTrunc!=0 ){
4482 rc = sqlite3PagerTruncate(pPager, nTrunc);
4483 }
4484
4485sync_exit:
danielk1977e965ac72007-06-13 15:22:28 +00004486 if( rc==SQLITE_IOERR_BLOCKED ){
4487 /* pager_incr_changecounter() may attempt to obtain an exclusive
4488 * lock to spill the cache and return IOERR_BLOCKED. But since
4489 * there is no chance the cache is inconsistent, it's
4490 * better to return SQLITE_BUSY.
4491 */
4492 rc = SQLITE_BUSY;
4493 }
drh86f8c192007-08-22 00:39:19 +00004494 pagerLeave(pPager);
drh80e35f42007-03-30 14:06:34 +00004495 return rc;
4496}
4497
4498
4499/*
drhed7c8552001-04-11 14:29:21 +00004500** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00004501**
4502** If the commit fails for any reason, a rollback attempt is made
4503** and an error code is returned. If the commit worked, SQLITE_OK
4504** is returned.
drhed7c8552001-04-11 14:29:21 +00004505*/
drh80e35f42007-03-30 14:06:34 +00004506int sqlite3PagerCommitPhaseTwo(Pager *pPager){
drha1b351a2001-09-14 16:42:12 +00004507 int rc;
drhed7c8552001-04-11 14:29:21 +00004508 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +00004509
danielk1977efaaf572006-01-16 11:29:19 +00004510 if( pPager->errCode ){
danielk19777f7bc662006-01-23 13:47:47 +00004511 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004512 }
drha6abd042004-06-09 17:37:22 +00004513 if( pPager->state<PAGER_RESERVED ){
drhd9b02572001-04-15 00:37:09 +00004514 return SQLITE_ERROR;
4515 }
drh86f8c192007-08-22 00:39:19 +00004516 pagerEnter(pPager);
drh4f0c5872007-03-26 22:05:01 +00004517 PAGERTRACE2("COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004518 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004519 pPg = pager_get_all_dirty_pages(pPager);
4520 while( pPg ){
danielk1977f35843b2007-04-07 15:03:17 +00004521 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4522 clearHistory(pHist);
drhac69b052004-05-12 13:30:07 +00004523 pPg->dirty = 0;
4524 pPg->inJournal = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004525 pHist->inStmt = 0;
drh5229ae42006-03-23 23:29:04 +00004526 pPg->needSync = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004527 pHist->pPrevStmt = pHist->pNextStmt = 0;
drhac69b052004-05-12 13:30:07 +00004528 pPg = pPg->pDirty;
4529 }
drh605ad8f2006-05-03 23:34:05 +00004530 pPager->pDirty = 0;
danielk1977eac7a362004-06-16 07:45:24 +00004531#ifndef NDEBUG
4532 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
4533 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4534 assert( !pPg->alwaysRollback );
4535 assert( !pHist->pOrig );
4536 assert( !pHist->pStmt );
4537 }
4538#endif
drhac69b052004-05-12 13:30:07 +00004539 pPager->pStmt = 0;
drha6abd042004-06-09 17:37:22 +00004540 pPager->state = PAGER_SHARED;
drhac69b052004-05-12 13:30:07 +00004541 return SQLITE_OK;
4542 }
drh3cdd7d32007-03-30 14:46:01 +00004543 assert( pPager->journalOpen || !pPager->dirtyCache );
4544 assert( pPager->state==PAGER_SYNCED || !pPager->dirtyCache );
4545 rc = pager_end_transaction(pPager);
drh86f8c192007-08-22 00:39:19 +00004546 rc = pager_error(pPager, rc);
4547 pagerLeave(pPager);
4548 return rc;
drhed7c8552001-04-11 14:29:21 +00004549}
4550
4551/*
drha6abd042004-06-09 17:37:22 +00004552** Rollback all changes. The database falls back to PAGER_SHARED mode.
drhed7c8552001-04-11 14:29:21 +00004553** All in-memory cache pages revert to their original data contents.
4554** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00004555**
4556** This routine cannot fail unless some other process is not following
drh4f0ee682007-03-30 20:43:40 +00004557** the correct locking protocol or unless some other
drhd9b02572001-04-15 00:37:09 +00004558** process is writing trash into the journal file (SQLITE_CORRUPT) or
4559** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
4560** codes are returned for all these occasions. Otherwise,
4561** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00004562*/
danielk19773b8a05f2007-03-19 17:44:26 +00004563int sqlite3PagerRollback(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00004564 int rc;
drh4f0c5872007-03-26 22:05:01 +00004565 PAGERTRACE2("ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004566 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004567 PgHdr *p;
4568 for(p=pPager->pAll; p; p=p->pNextAll){
4569 PgHistory *pHist;
danielk1977eac7a362004-06-16 07:45:24 +00004570 assert( !p->alwaysRollback );
4571 if( !p->dirty ){
4572 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pOrig );
4573 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pStmt );
4574 continue;
4575 }
4576
drhac69b052004-05-12 13:30:07 +00004577 pHist = PGHDR_TO_HIST(p, pPager);
4578 if( pHist->pOrig ){
4579 memcpy(PGHDR_TO_DATA(p), pHist->pOrig, pPager->pageSize);
drh4f0c5872007-03-26 22:05:01 +00004580 PAGERTRACE3("ROLLBACK-PAGE %d of %d\n", p->pgno, PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00004581 }else{
drh4f0c5872007-03-26 22:05:01 +00004582 PAGERTRACE3("PAGE %d is clean on %d\n", p->pgno, PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00004583 }
4584 clearHistory(pHist);
4585 p->dirty = 0;
4586 p->inJournal = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004587 pHist->inStmt = 0;
4588 pHist->pPrevStmt = pHist->pNextStmt = 0;
danielk1977369f27e2004-06-15 11:40:04 +00004589 if( pPager->xReiniter ){
danielk19773b8a05f2007-03-19 17:44:26 +00004590 pPager->xReiniter(p, pPager->pageSize);
danielk1977369f27e2004-06-15 11:40:04 +00004591 }
drhac69b052004-05-12 13:30:07 +00004592 }
drh605ad8f2006-05-03 23:34:05 +00004593 pPager->pDirty = 0;
drhac69b052004-05-12 13:30:07 +00004594 pPager->pStmt = 0;
4595 pPager->dbSize = pPager->origDbSize;
danielk1977e180dd92007-04-05 17:15:52 +00004596 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00004597 pPager->stmtInUse = 0;
drha6abd042004-06-09 17:37:22 +00004598 pPager->state = PAGER_SHARED;
drhac69b052004-05-12 13:30:07 +00004599 return SQLITE_OK;
4600 }
4601
drh86f8c192007-08-22 00:39:19 +00004602 pagerEnter(pPager);
drha6abd042004-06-09 17:37:22 +00004603 if( !pPager->dirtyCache || !pPager->journalOpen ){
drh80e35f42007-03-30 14:06:34 +00004604 rc = pager_end_transaction(pPager);
drh86f8c192007-08-22 00:39:19 +00004605 pagerLeave(pPager);
drhda47d772002-12-02 04:25:19 +00004606 return rc;
4607 }
drhdb48ee02003-01-16 13:42:43 +00004608
danielk1977efaaf572006-01-16 11:29:19 +00004609 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
drha6abd042004-06-09 17:37:22 +00004610 if( pPager->state>=PAGER_EXCLUSIVE ){
danielk1977e277be02007-03-23 18:12:06 +00004611 pager_playback(pPager, 0);
drh4b845d72002-03-05 12:41:19 +00004612 }
drh86f8c192007-08-22 00:39:19 +00004613 pagerLeave(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00004614 return pPager->errCode;
drhed7c8552001-04-11 14:29:21 +00004615 }
drha6abd042004-06-09 17:37:22 +00004616 if( pPager->state==PAGER_RESERVED ){
danielk197717221812005-02-15 03:38:05 +00004617 int rc2;
danielk1977e277be02007-03-23 18:12:06 +00004618 rc = pager_playback(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00004619 rc2 = pager_end_transaction(pPager);
drha6abd042004-06-09 17:37:22 +00004620 if( rc==SQLITE_OK ){
4621 rc = rc2;
4622 }
4623 }else{
danielk1977e277be02007-03-23 18:12:06 +00004624 rc = pager_playback(pPager, 0);
drhd9b02572001-04-15 00:37:09 +00004625 }
danielk1977e180dd92007-04-05 17:15:52 +00004626 /* pager_reset(pPager); */
drhd9b02572001-04-15 00:37:09 +00004627 pPager->dbSize = -1;
danielk197707cb5602006-01-20 10:55:05 +00004628
4629 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
4630 ** cache. So call pager_error() on the way out to make any error
4631 ** persistent.
4632 */
drh86f8c192007-08-22 00:39:19 +00004633 rc = pager_error(pPager, rc);
4634 pagerLeave(pPager);
4635 return rc;
drh98808ba2001-10-18 12:34:46 +00004636}
drhd9b02572001-04-15 00:37:09 +00004637
4638/*
drh5e00f6c2001-09-13 13:46:56 +00004639** Return TRUE if the database file is opened read-only. Return FALSE
4640** if the database is (in theory) writable.
4641*/
danielk19773b8a05f2007-03-19 17:44:26 +00004642int sqlite3PagerIsreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00004643 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00004644}
4645
4646/*
drh0f7eb612006-08-08 13:51:43 +00004647** Return the number of references to the pager.
4648*/
danielk19773b8a05f2007-03-19 17:44:26 +00004649int sqlite3PagerRefcount(Pager *pPager){
drh0f7eb612006-08-08 13:51:43 +00004650 return pPager->nRef;
4651}
4652
4653#ifdef SQLITE_TEST
4654/*
drhd9b02572001-04-15 00:37:09 +00004655** This routine is used for testing and analysis only.
4656*/
danielk19773b8a05f2007-03-19 17:44:26 +00004657int *sqlite3PagerStats(Pager *pPager){
danielk197742741be2005-01-08 12:42:39 +00004658 static int a[11];
drhd9b02572001-04-15 00:37:09 +00004659 a[0] = pPager->nRef;
4660 a[1] = pPager->nPage;
4661 a[2] = pPager->mxPage;
4662 a[3] = pPager->dbSize;
4663 a[4] = pPager->state;
danielk1977efaaf572006-01-16 11:29:19 +00004664 a[5] = pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004665 a[6] = pPager->nHit;
4666 a[7] = pPager->nMiss;
drh7c4ac0c2007-04-05 11:25:58 +00004667 a[8] = 0; /* Used to be pPager->nOvfl */
danielk197742741be2005-01-08 12:42:39 +00004668 a[9] = pPager->nRead;
4669 a[10] = pPager->nWrite;
drhd9b02572001-04-15 00:37:09 +00004670 return a;
4671}
drh0f7eb612006-08-08 13:51:43 +00004672#endif
drhdd793422001-06-28 01:54:48 +00004673
drhfa86c412002-02-02 15:01:15 +00004674/*
drhac69b052004-05-12 13:30:07 +00004675** Set the statement rollback point.
drhfa86c412002-02-02 15:01:15 +00004676**
4677** This routine should be called with the transaction journal already
drhac69b052004-05-12 13:30:07 +00004678** open. A new statement journal is created that can be used to rollback
drhaaab5722002-02-19 13:39:21 +00004679** changes of a single SQL command within a larger transaction.
drhfa86c412002-02-02 15:01:15 +00004680*/
drh86f8c192007-08-22 00:39:19 +00004681static int pagerStmtBegin(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00004682 int rc;
drhac69b052004-05-12 13:30:07 +00004683 assert( !pPager->stmtInUse );
drh1aa2d8b2007-01-03 15:34:29 +00004684 assert( pPager->state>=PAGER_SHARED );
drhdc3ff9c2004-08-18 02:10:15 +00004685 assert( pPager->dbSize>=0 );
drh4f0c5872007-03-26 22:05:01 +00004686 PAGERTRACE2("STMT-BEGIN %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004687 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004688 pPager->stmtInUse = 1;
4689 pPager->stmtSize = pPager->dbSize;
4690 return SQLITE_OK;
4691 }
drhda47d772002-12-02 04:25:19 +00004692 if( !pPager->journalOpen ){
drhac69b052004-05-12 13:30:07 +00004693 pPager->stmtAutoopen = 1;
drhda47d772002-12-02 04:25:19 +00004694 return SQLITE_OK;
4695 }
drhfa86c412002-02-02 15:01:15 +00004696 assert( pPager->journalOpen );
drhed138fb2007-08-22 22:04:37 +00004697 pagerLeave(pPager);
drh17435752007-08-16 04:30:38 +00004698 pPager->aInStmt = sqlite3MallocZero( pPager->dbSize/8 + 1 );
drhed138fb2007-08-22 22:04:37 +00004699 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00004700 if( pPager->aInStmt==0 ){
danielk1977261919c2005-12-06 12:52:59 +00004701 /* sqlite3OsLock(pPager->fd, SHARED_LOCK); */
drhfa86c412002-02-02 15:01:15 +00004702 return SQLITE_NOMEM;
4703 }
drh968af522003-02-11 14:55:40 +00004704#ifndef NDEBUG
drh054889e2005-11-30 03:20:31 +00004705 rc = sqlite3OsFileSize(pPager->jfd, &pPager->stmtJSize);
drhac69b052004-05-12 13:30:07 +00004706 if( rc ) goto stmt_begin_failed;
danielk197776572402004-06-25 02:38:54 +00004707 assert( pPager->stmtJSize == pPager->journalOff );
drh968af522003-02-11 14:55:40 +00004708#endif
danielk197776572402004-06-25 02:38:54 +00004709 pPager->stmtJSize = pPager->journalOff;
drhac69b052004-05-12 13:30:07 +00004710 pPager->stmtSize = pPager->dbSize;
danielk197776572402004-06-25 02:38:54 +00004711 pPager->stmtHdrOff = 0;
danielk197775edc162004-06-26 01:48:18 +00004712 pPager->stmtCksum = pPager->cksumInit;
drhac69b052004-05-12 13:30:07 +00004713 if( !pPager->stmtOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00004714 rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->stfd, 0);
drhac69b052004-05-12 13:30:07 +00004715 if( rc ) goto stmt_begin_failed;
4716 pPager->stmtOpen = 1;
4717 pPager->stmtNRec = 0;
drh0f892532002-05-30 12:27:03 +00004718 }
drhac69b052004-05-12 13:30:07 +00004719 pPager->stmtInUse = 1;
drhfa86c412002-02-02 15:01:15 +00004720 return SQLITE_OK;
4721
drhac69b052004-05-12 13:30:07 +00004722stmt_begin_failed:
4723 if( pPager->aInStmt ){
drh17435752007-08-16 04:30:38 +00004724 sqlite3_free(pPager->aInStmt);
drhac69b052004-05-12 13:30:07 +00004725 pPager->aInStmt = 0;
drhfa86c412002-02-02 15:01:15 +00004726 }
4727 return rc;
4728}
drh86f8c192007-08-22 00:39:19 +00004729int sqlite3PagerStmtBegin(Pager *pPager){
4730 int rc;
4731 pagerEnter(pPager);
4732 rc = pagerStmtBegin(pPager);
4733 pagerLeave(pPager);
4734 return rc;
4735}
drhfa86c412002-02-02 15:01:15 +00004736
4737/*
drhac69b052004-05-12 13:30:07 +00004738** Commit a statement.
drhfa86c412002-02-02 15:01:15 +00004739*/
danielk19773b8a05f2007-03-19 17:44:26 +00004740int sqlite3PagerStmtCommit(Pager *pPager){
drh86f8c192007-08-22 00:39:19 +00004741 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00004742 if( pPager->stmtInUse ){
drh03eb96a2002-11-10 23:32:56 +00004743 PgHdr *pPg, *pNext;
drh4f0c5872007-03-26 22:05:01 +00004744 PAGERTRACE2("STMT-COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004745 if( !MEMDB ){
drh054889e2005-11-30 03:20:31 +00004746 /* sqlite3OsTruncate(pPager->stfd, 0); */
drh17435752007-08-16 04:30:38 +00004747 sqlite3_free( pPager->aInStmt );
drhac69b052004-05-12 13:30:07 +00004748 pPager->aInStmt = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004749 }else{
4750 for(pPg=pPager->pStmt; pPg; pPg=pNext){
drhac69b052004-05-12 13:30:07 +00004751 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
danielk1977f35843b2007-04-07 15:03:17 +00004752 pNext = pHist->pNextStmt;
4753 assert( pHist->inStmt );
4754 pHist->inStmt = 0;
4755 pHist->pPrevStmt = pHist->pNextStmt = 0;
drh17435752007-08-16 04:30:38 +00004756 sqlite3_free(pHist->pStmt);
drhac69b052004-05-12 13:30:07 +00004757 pHist->pStmt = 0;
4758 }
4759 }
4760 pPager->stmtNRec = 0;
4761 pPager->stmtInUse = 0;
4762 pPager->pStmt = 0;
drh663fc632002-02-02 18:49:19 +00004763 }
drhac69b052004-05-12 13:30:07 +00004764 pPager->stmtAutoopen = 0;
drh86f8c192007-08-22 00:39:19 +00004765 pagerLeave(pPager);
drhfa86c412002-02-02 15:01:15 +00004766 return SQLITE_OK;
4767}
4768
4769/*
drhac69b052004-05-12 13:30:07 +00004770** Rollback a statement.
drhfa86c412002-02-02 15:01:15 +00004771*/
danielk19773b8a05f2007-03-19 17:44:26 +00004772int sqlite3PagerStmtRollback(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00004773 int rc;
drh86f8c192007-08-22 00:39:19 +00004774 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00004775 if( pPager->stmtInUse ){
drh4f0c5872007-03-26 22:05:01 +00004776 PAGERTRACE2("STMT-ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004777 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004778 PgHdr *pPg;
danielk1977f35843b2007-04-07 15:03:17 +00004779 PgHistory *pHist;
4780 for(pPg=pPager->pStmt; pPg; pPg=pHist->pNextStmt){
4781 pHist = PGHDR_TO_HIST(pPg, pPager);
drhac69b052004-05-12 13:30:07 +00004782 if( pHist->pStmt ){
4783 memcpy(PGHDR_TO_DATA(pPg), pHist->pStmt, pPager->pageSize);
drh17435752007-08-16 04:30:38 +00004784 sqlite3_free(pHist->pStmt);
drhac69b052004-05-12 13:30:07 +00004785 pHist->pStmt = 0;
4786 }
4787 }
4788 pPager->dbSize = pPager->stmtSize;
danielk1977e180dd92007-04-05 17:15:52 +00004789 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00004790 rc = SQLITE_OK;
4791 }else{
4792 rc = pager_stmt_playback(pPager);
4793 }
danielk19773b8a05f2007-03-19 17:44:26 +00004794 sqlite3PagerStmtCommit(pPager);
drh663fc632002-02-02 18:49:19 +00004795 }else{
4796 rc = SQLITE_OK;
4797 }
drhac69b052004-05-12 13:30:07 +00004798 pPager->stmtAutoopen = 0;
drh86f8c192007-08-22 00:39:19 +00004799 pagerLeave(pPager);
drhfa86c412002-02-02 15:01:15 +00004800 return rc;
4801}
4802
drh73509ee2003-04-06 20:44:45 +00004803/*
4804** Return the full pathname of the database file.
4805*/
danielk19773b8a05f2007-03-19 17:44:26 +00004806const char *sqlite3PagerFilename(Pager *pPager){
drh73509ee2003-04-06 20:44:45 +00004807 return pPager->zFilename;
4808}
4809
drhb20ea9d2004-02-09 01:20:36 +00004810/*
drhd0679ed2007-08-28 22:24:34 +00004811** Return the VFS structure for the pager.
4812*/
4813const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
4814 return pPager->pVfs;
4815}
4816
4817/*
danielk19775865e3d2004-06-14 06:03:57 +00004818** Return the directory of the database file.
4819*/
danielk19773b8a05f2007-03-19 17:44:26 +00004820const char *sqlite3PagerDirname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004821 return pPager->zDirectory;
4822}
4823
4824/*
4825** Return the full pathname of the journal file.
4826*/
danielk19773b8a05f2007-03-19 17:44:26 +00004827const char *sqlite3PagerJournalname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004828 return pPager->zJournal;
4829}
4830
4831/*
drh2c8997b2005-08-27 16:36:48 +00004832** Return true if fsync() calls are disabled for this pager. Return FALSE
4833** if fsync()s are executed normally.
4834*/
danielk19773b8a05f2007-03-19 17:44:26 +00004835int sqlite3PagerNosync(Pager *pPager){
drh2c8997b2005-08-27 16:36:48 +00004836 return pPager->noSync;
4837}
4838
drh7c4ac0c2007-04-05 11:25:58 +00004839#ifdef SQLITE_HAS_CODEC
drh2c8997b2005-08-27 16:36:48 +00004840/*
drhb20ea9d2004-02-09 01:20:36 +00004841** Set the codec for this pager
4842*/
danielk19773b8a05f2007-03-19 17:44:26 +00004843void sqlite3PagerSetCodec(
drhb20ea9d2004-02-09 01:20:36 +00004844 Pager *pPager,
drhc001c582006-03-06 18:23:16 +00004845 void *(*xCodec)(void*,void*,Pgno,int),
drhb20ea9d2004-02-09 01:20:36 +00004846 void *pCodecArg
4847){
4848 pPager->xCodec = xCodec;
4849 pPager->pCodecArg = pCodecArg;
4850}
drh7c4ac0c2007-04-05 11:25:58 +00004851#endif
drhb20ea9d2004-02-09 01:20:36 +00004852
danielk1977687566d2004-11-02 12:56:41 +00004853#ifndef SQLITE_OMIT_AUTOVACUUM
4854/*
drh5e385312007-06-16 04:42:12 +00004855** Move the page pPg to location pgno in the file.
danielk1977687566d2004-11-02 12:56:41 +00004856**
drh5e385312007-06-16 04:42:12 +00004857** There must be no references to the page previously located at
4858** pgno (which we call pPgOld) though that page is allowed to be
4859** in cache. If the page previous located at pgno is not already
4860** in the rollback journal, it is not put there by by this routine.
danielk1977687566d2004-11-02 12:56:41 +00004861**
drh5e385312007-06-16 04:42:12 +00004862** References to the page pPg remain valid. Updating any
4863** meta-data associated with pPg (i.e. data stored in the nExtra bytes
danielk1977687566d2004-11-02 12:56:41 +00004864** allocated along with the page) is the responsibility of the caller.
4865**
danielk19775fd057a2005-03-09 13:09:43 +00004866** A transaction must be active when this routine is called. It used to be
4867** required that a statement transaction was not active, but this restriction
4868** has been removed (CREATE INDEX needs to move a page when a statement
4869** transaction is active).
danielk1977687566d2004-11-02 12:56:41 +00004870*/
danielk19773b8a05f2007-03-19 17:44:26 +00004871int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno){
drh5e385312007-06-16 04:42:12 +00004872 PgHdr *pPgOld; /* The page being overwritten. */
danielk1977f5fdda82004-11-03 08:44:05 +00004873 int h;
danielk197794daf7f2004-11-08 09:26:09 +00004874 Pgno needSyncPgno = 0;
danielk1977687566d2004-11-02 12:56:41 +00004875
drh86f8c192007-08-22 00:39:19 +00004876 pagerEnter(pPager);
danielk1977687566d2004-11-02 12:56:41 +00004877 assert( pPg->nRef>0 );
4878
drh4f0c5872007-03-26 22:05:01 +00004879 PAGERTRACE5("MOVE %d page %d (needSync=%d) moves to %d\n",
danielk1977599fcba2004-11-08 07:13:13 +00004880 PAGERID(pPager), pPg->pgno, pPg->needSync, pgno);
drhb0603412007-02-28 04:47:26 +00004881 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
danielk1977ef73ee92004-11-06 12:26:07 +00004882
danielk1977b4626a32007-04-28 15:47:43 +00004883 pager_get_content(pPg);
danielk197794daf7f2004-11-08 09:26:09 +00004884 if( pPg->needSync ){
4885 needSyncPgno = pPg->pgno;
drh4f0aee42007-06-16 18:39:41 +00004886 assert( pPg->inJournal || (int)pgno>pPager->origDbSize );
danielk197794daf7f2004-11-08 09:26:09 +00004887 assert( pPg->dirty );
danielk1977ae825582004-11-23 09:06:55 +00004888 assert( pPager->needSync );
danielk197794daf7f2004-11-08 09:26:09 +00004889 }
4890
danielk1977687566d2004-11-02 12:56:41 +00004891 /* Unlink pPg from it's hash-chain */
danielk1977f5fdda82004-11-03 08:44:05 +00004892 unlinkHashChain(pPager, pPg);
danielk1977687566d2004-11-02 12:56:41 +00004893
danielk1977ef73ee92004-11-06 12:26:07 +00004894 /* If the cache contains a page with page-number pgno, remove it
danielk1977599fcba2004-11-08 07:13:13 +00004895 ** from it's hash chain. Also, if the PgHdr.needSync was set for
4896 ** page pgno before the 'move' operation, it needs to be retained
4897 ** for the page moved there.
danielk1977f5fdda82004-11-03 08:44:05 +00004898 */
drh5e385312007-06-16 04:42:12 +00004899 pPg->needSync = 0;
danielk1977687566d2004-11-02 12:56:41 +00004900 pPgOld = pager_lookup(pPager, pgno);
4901 if( pPgOld ){
danielk1977f5fdda82004-11-03 08:44:05 +00004902 assert( pPgOld->nRef==0 );
4903 unlinkHashChain(pPager, pPgOld);
drh605ad8f2006-05-03 23:34:05 +00004904 makeClean(pPgOld);
drh5e385312007-06-16 04:42:12 +00004905 pPg->needSync = pPgOld->needSync;
4906 }else{
4907 pPg->needSync = 0;
4908 }
4909 if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){
4910 pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0;
drh5e385312007-06-16 04:42:12 +00004911 }else{
4912 pPg->inJournal = 0;
drh732c8172007-06-16 11:17:45 +00004913 assert( pPg->needSync==0 || (int)pgno>pPager->origDbSize );
danielk1977687566d2004-11-02 12:56:41 +00004914 }
4915
danielk1977f5fdda82004-11-03 08:44:05 +00004916 /* Change the page number for pPg and insert it into the new hash-chain. */
drh3765df42006-06-28 18:18:09 +00004917 assert( pgno!=0 );
danielk1977f5fdda82004-11-03 08:44:05 +00004918 pPg->pgno = pgno;
drh8ca0c722006-05-07 17:49:38 +00004919 h = pgno & (pPager->nHash-1);
danielk1977f5fdda82004-11-03 08:44:05 +00004920 if( pPager->aHash[h] ){
4921 assert( pPager->aHash[h]->pPrevHash==0 );
4922 pPager->aHash[h]->pPrevHash = pPg;
4923 }
4924 pPg->pNextHash = pPager->aHash[h];
4925 pPager->aHash[h] = pPg;
4926 pPg->pPrevHash = 0;
4927
drh605ad8f2006-05-03 23:34:05 +00004928 makeDirty(pPg);
danielk1977687566d2004-11-02 12:56:41 +00004929 pPager->dirtyCache = 1;
4930
danielk197794daf7f2004-11-08 09:26:09 +00004931 if( needSyncPgno ){
4932 /* If needSyncPgno is non-zero, then the journal file needs to be
4933 ** sync()ed before any data is written to database file page needSyncPgno.
4934 ** Currently, no such page exists in the page-cache and the
4935 ** Pager.aInJournal bit has been set. This needs to be remedied by loading
4936 ** the page into the pager-cache and setting the PgHdr.needSync flag.
danielk1977ae825582004-11-23 09:06:55 +00004937 **
danielk19773b8a05f2007-03-19 17:44:26 +00004938 ** The sqlite3PagerGet() call may cause the journal to sync. So make
danielk1977ae825582004-11-23 09:06:55 +00004939 ** sure the Pager.needSync flag is set too.
danielk197794daf7f2004-11-08 09:26:09 +00004940 */
4941 int rc;
danielk19773b8a05f2007-03-19 17:44:26 +00004942 PgHdr *pPgHdr;
danielk1977ae825582004-11-23 09:06:55 +00004943 assert( pPager->needSync );
danielk19773b8a05f2007-03-19 17:44:26 +00004944 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
danielk197794daf7f2004-11-08 09:26:09 +00004945 if( rc!=SQLITE_OK ) return rc;
danielk1977ae825582004-11-23 09:06:55 +00004946 pPager->needSync = 1;
danielk19773b8a05f2007-03-19 17:44:26 +00004947 pPgHdr->needSync = 1;
4948 pPgHdr->inJournal = 1;
4949 makeDirty(pPgHdr);
4950 sqlite3PagerUnref(pPgHdr);
danielk197794daf7f2004-11-08 09:26:09 +00004951 }
4952
drh86f8c192007-08-22 00:39:19 +00004953 pagerLeave(pPager);
danielk1977687566d2004-11-02 12:56:41 +00004954 return SQLITE_OK;
4955}
4956#endif
4957
danielk19773b8a05f2007-03-19 17:44:26 +00004958/*
4959** Return a pointer to the data for the specified page.
4960*/
4961void *sqlite3PagerGetData(DbPage *pPg){
4962 return PGHDR_TO_DATA(pPg);
4963}
4964
4965/*
4966** Return a pointer to the Pager.nExtra bytes of "extra" space
4967** allocated along with the specified page.
4968*/
4969void *sqlite3PagerGetExtra(DbPage *pPg){
4970 Pager *pPager = pPg->pPager;
4971 return (pPager?PGHDR_TO_EXTRA(pPg, pPager):0);
4972}
4973
danielk197741483462007-03-24 16:45:04 +00004974/*
4975** Get/set the locking-mode for this pager. Parameter eMode must be one
4976** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
4977** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
4978** the locking-mode is set to the value specified.
4979**
4980** The returned value is either PAGER_LOCKINGMODE_NORMAL or
4981** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
4982** locking-mode.
4983*/
4984int sqlite3PagerLockingMode(Pager *pPager, int eMode){
drh369339d2007-03-30 16:01:55 +00004985 assert( eMode==PAGER_LOCKINGMODE_QUERY
4986 || eMode==PAGER_LOCKINGMODE_NORMAL
4987 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
4988 assert( PAGER_LOCKINGMODE_QUERY<0 );
4989 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
4990 if( eMode>=0 && !pPager->tempFile ){
danielk197741483462007-03-24 16:45:04 +00004991 pPager->exclusiveMode = eMode;
4992 }
4993 return (int)pPager->exclusiveMode;
4994}
4995
dougcurrie81c95ef2004-06-18 23:21:47 +00004996#if defined(SQLITE_DEBUG) || defined(SQLITE_TEST)
drh89ac8c12004-06-09 14:17:20 +00004997/*
4998** Return the current state of the file lock for the given pager.
4999** The return value is one of NO_LOCK, SHARED_LOCK, RESERVED_LOCK,
5000** PENDING_LOCK, or EXCLUSIVE_LOCK.
5001*/
danielk19773b8a05f2007-03-19 17:44:26 +00005002int sqlite3PagerLockstate(Pager *pPager){
drh054889e2005-11-30 03:20:31 +00005003 return sqlite3OsLockState(pPager->fd);
drh89ac8c12004-06-09 14:17:20 +00005004}
5005#endif
5006
danielk197793758c82005-01-21 08:13:14 +00005007#ifdef SQLITE_DEBUG
drhdd793422001-06-28 01:54:48 +00005008/*
5009** Print a listing of all referenced pages and their ref count.
5010*/
danielk19773b8a05f2007-03-19 17:44:26 +00005011void sqlite3PagerRefdump(Pager *pPager){
drhdd793422001-06-28 01:54:48 +00005012 PgHdr *pPg;
5013 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
5014 if( pPg->nRef<=0 ) continue;
drhfe63d1c2004-09-08 20:13:04 +00005015 sqlite3DebugPrintf("PAGE %3d addr=%p nRef=%d\n",
5016 pPg->pgno, PGHDR_TO_DATA(pPg), pPg->nRef);
drhdd793422001-06-28 01:54:48 +00005017 }
5018}
5019#endif
drh2e66f0b2005-04-28 17:18:48 +00005020
5021#endif /* SQLITE_OMIT_DISKIO */