blob: c442365e4bb1605571b6cec067f2769434fd90f4 [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**
danielk1977adfb9b02007-09-17 07:02:56 +000021** @(#) $Id: pager.c,v 1.390 2007/09/17 07:02:57 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
danielk19773c407372005-02-15 02:54:14 +0000274#ifdef SQLITE_CHECK_PAGES
275 u32 pageHash;
276#endif
drhbf4bca52007-09-06 22:19:14 +0000277 void *pData; /* Page data */
278 /* Pager.nExtra bytes of local data appended to this header */
drhed7c8552001-04-11 14:29:21 +0000279};
280
drhac69b052004-05-12 13:30:07 +0000281/*
282** For an in-memory only database, some extra information is recorded about
283** each page so that changes can be rolled back. (Journal files are not
284** used for in-memory databases.) The following information is added to
285** the end of every EXTRA block for in-memory databases.
286**
287** This information could have been added directly to the PgHdr structure.
288** But then it would take up an extra 8 bytes of storage on every PgHdr
289** even for disk-based databases. Splitting it out saves 8 bytes. This
290** is only a savings of 0.8% but those percentages add up.
291*/
292typedef struct PgHistory PgHistory;
293struct PgHistory {
294 u8 *pOrig; /* Original page text. Restore to this on a full rollback */
295 u8 *pStmt; /* Text as it was at the beginning of the current statement */
danielk1977f35843b2007-04-07 15:03:17 +0000296 PgHdr *pNextStmt, *pPrevStmt; /* List of pages in the statement journal */
297 u8 inStmt; /* TRUE if in the statement subjournal */
drhac69b052004-05-12 13:30:07 +0000298};
drh9eb9e262004-02-11 02:18:05 +0000299
300/*
301** A macro used for invoking the codec if there is one
302*/
303#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000304# define CODEC1(P,D,N,X) if( P->xCodec!=0 ){ P->xCodec(P->pCodecArg,D,N,X); }
305# define CODEC2(P,D,N,X) ((char*)(P->xCodec!=0?P->xCodec(P->pCodecArg,D,N,X):D))
drh9eb9e262004-02-11 02:18:05 +0000306#else
drhc001c582006-03-06 18:23:16 +0000307# define CODEC1(P,D,N,X) /* NO-OP */
308# define CODEC2(P,D,N,X) ((char*)D)
drh9eb9e262004-02-11 02:18:05 +0000309#endif
310
drhed7c8552001-04-11 14:29:21 +0000311/*
drh69688d52001-04-14 16:38:23 +0000312** Convert a pointer to a PgHdr into a pointer to its data
313** and back again.
drhed7c8552001-04-11 14:29:21 +0000314*/
drhbf4bca52007-09-06 22:19:14 +0000315#define PGHDR_TO_DATA(P) ((P)->pData)
316#define PGHDR_TO_EXTRA(G,P) ((void*)&((G)[1]))
drhac69b052004-05-12 13:30:07 +0000317#define PGHDR_TO_HIST(P,PGR) \
drhbf4bca52007-09-06 22:19:14 +0000318 ((PgHistory*)&((char*)(&(P)[1]))[(PGR)->nExtra])
drhed7c8552001-04-11 14:29:21 +0000319
320/*
drhed7c8552001-04-11 14:29:21 +0000321** A open page cache is an instance of the following structure.
danielk1977efaaf572006-01-16 11:29:19 +0000322**
drh4f0ee682007-03-30 20:43:40 +0000323** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or
danielk1977efaaf572006-01-16 11:29:19 +0000324** or SQLITE_FULL. Once one of the first three errors occurs, it persists
325** and is returned as the result of every major pager API call. The
326** SQLITE_FULL return code is slightly different. It persists only until the
327** next successful rollback is performed on the pager cache. Also,
danielk19773b8a05f2007-03-19 17:44:26 +0000328** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup()
danielk1977efaaf572006-01-16 11:29:19 +0000329** APIs, they may still be used successfully.
drhed7c8552001-04-11 14:29:21 +0000330*/
331struct Pager {
danielk1977b4b47412007-08-17 15:53:36 +0000332 sqlite3_vfs *pVfs; /* OS functions to use for IO */
drh603240c2002-03-05 01:11:12 +0000333 u8 journalOpen; /* True if journal file descriptors is valid */
drh34e79ce2004-02-08 06:05:46 +0000334 u8 journalStarted; /* True if header of journal is synced */
335 u8 useJournal; /* Use a rollback journal on this file */
drh7bec5052005-02-06 02:45:41 +0000336 u8 noReadlock; /* Do not bother to obtain readlocks */
drhac69b052004-05-12 13:30:07 +0000337 u8 stmtOpen; /* True if the statement subjournal is open */
338 u8 stmtInUse; /* True we are in a statement subtransaction */
339 u8 stmtAutoopen; /* Open stmt journal when main journal is opened*/
drh603240c2002-03-05 01:11:12 +0000340 u8 noSync; /* Do not sync the journal if true */
drh968af522003-02-11 14:55:40 +0000341 u8 fullSync; /* Do extra syncs of the journal for robustness */
danielk1977f036aef2007-08-20 05:36:51 +0000342 u8 sync_flags; /* One of SYNC_NORMAL or SYNC_FULL */
drha6abd042004-06-09 17:37:22 +0000343 u8 state; /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */
drh603240c2002-03-05 01:11:12 +0000344 u8 tempFile; /* zFilename is a temporary file */
345 u8 readOnly; /* True for a read-only database */
346 u8 needSync; /* True if an fsync() is needed on the journal */
drha6abd042004-06-09 17:37:22 +0000347 u8 dirtyCache; /* True if cached pages have changed */
drh80e35f42007-03-30 14:06:34 +0000348 u8 alwaysRollback; /* Disable DontRollback() for all pages */
drhac69b052004-05-12 13:30:07 +0000349 u8 memDb; /* True to inhibit all file I/O */
drh6d156e42005-05-20 20:11:20 +0000350 u8 setMaster; /* True if a m-j name has been written to jrnl */
drh80e35f42007-03-30 14:06:34 +0000351 u8 doNotSync; /* Boolean. While true, do not spill the cache */
352 u8 exclusiveMode; /* Boolean. True if locking_mode==EXCLUSIVE */
353 u8 changeCountDone; /* Set after incrementing the change-counter */
drh33f4e022007-09-03 15:19:34 +0000354 u32 vfsFlags; /* Flags for sqlite3_vfs.xOpen() */
drhe49f9822006-09-15 12:29:16 +0000355 int errCode; /* One of several kinds of errors */
drhfcd35c72005-05-21 02:48:08 +0000356 int dbSize; /* Number of pages in the file */
357 int origDbSize; /* dbSize before the current change */
358 int stmtSize; /* Size of database (in pages) at stmt_begin() */
359 int nRec; /* Number of pages written to the journal */
360 u32 cksumInit; /* Quasi-random value added to every checksum */
361 int stmtNRec; /* Number of records in stmt subjournal */
362 int nExtra; /* Add this many bytes to each in-memory page */
363 int pageSize; /* Number of bytes in a page */
364 int nPage; /* Total number of in-memory pages */
drhfcd35c72005-05-21 02:48:08 +0000365 int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */
366 int mxPage; /* Maximum number of pages to hold in cache */
drhf8e632b2007-05-08 14:51:36 +0000367 Pgno mxPgno; /* Maximum allowed size of the database */
drh603240c2002-03-05 01:11:12 +0000368 u8 *aInJournal; /* One bit for each page in the database file */
drhac69b052004-05-12 13:30:07 +0000369 u8 *aInStmt; /* One bit for each page in the database */
drhfcd35c72005-05-21 02:48:08 +0000370 char *zFilename; /* Name of the database file */
371 char *zJournal; /* Name of the journal file */
372 char *zDirectory; /* Directory hold database and journal files */
drh334b2992007-09-06 23:28:23 +0000373 char *zStmtJrnl; /* Name of the statement journal file */
danielk197762079062007-08-15 17:08:46 +0000374 sqlite3_file *fd, *jfd; /* File descriptors for database and journal */
375 sqlite3_file *stfd; /* File descriptor for the statement subjournal*/
drh726de592004-06-10 23:35:50 +0000376 BusyHandler *pBusyHandler; /* Pointer to sqlite.busyHandler */
danielk19779f61c2f2007-08-27 17:27:49 +0000377 PagerLruList lru; /* LRU list of free pages */
drhd9b02572001-04-15 00:37:09 +0000378 PgHdr *pAll; /* List of all pages */
drhac69b052004-05-12 13:30:07 +0000379 PgHdr *pStmt; /* List of pages in the statement subjournal */
drh605ad8f2006-05-03 23:34:05 +0000380 PgHdr *pDirty; /* List of all dirty pages */
drheb206252004-10-01 02:00:31 +0000381 i64 journalOff; /* Current byte offset in the journal file */
382 i64 journalHdr; /* Byte offset to previous journal header */
383 i64 stmtHdrOff; /* First journal header written this statement */
384 i64 stmtCksum; /* cksumInit when statement was started */
drh6d156e42005-05-20 20:11:20 +0000385 i64 stmtJSize; /* Size of journal at stmt_begin() */
danielk197776572402004-06-25 02:38:54 +0000386 int sectorSize; /* Assumed sector size during rollback */
drhfcd35c72005-05-21 02:48:08 +0000387#ifdef SQLITE_TEST
drh7c4ac0c2007-04-05 11:25:58 +0000388 int nHit, nMiss; /* Cache hits and missing */
389 int nRead, nWrite; /* Database pages read/written */
drhfcd35c72005-05-21 02:48:08 +0000390#endif
danielk19773b8a05f2007-03-19 17:44:26 +0000391 void (*xDestructor)(DbPage*,int); /* Call this routine when freeing pages */
392 void (*xReiniter)(DbPage*,int); /* Call this routine when reloading pages */
drh7c4ac0c2007-04-05 11:25:58 +0000393#ifdef SQLITE_HAS_CODEC
drhc001c582006-03-06 18:23:16 +0000394 void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
drh6d156e42005-05-20 20:11:20 +0000395 void *pCodecArg; /* First argument to xCodec() */
drh7c4ac0c2007-04-05 11:25:58 +0000396#endif
drh8ca0c722006-05-07 17:49:38 +0000397 int nHash; /* Size of the pager hash table */
398 PgHdr **aHash; /* Hash table to map page number to PgHdr */
drh6f7adc82006-01-11 21:41:20 +0000399#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +0000400 Pager *pNext; /* Doubly linked list of pagers on which */
401 Pager *pPrev; /* sqlite3_release_memory() will work */
402 int iInUseMM; /* Non-zero if unavailable to MM */
403 int iInUseDB; /* Non-zero if in sqlite3_release_memory() */
danielk197713f72992005-12-18 08:51:22 +0000404#endif
danielk19778186df82007-03-06 13:45:59 +0000405 char *pTmpSpace; /* Pager.pageSize bytes of space for tmp use */
drh86a88112007-04-16 15:02:19 +0000406 char dbFileVers[16]; /* Changes whenever database file changes */
drhd9b02572001-04-15 00:37:09 +0000407};
408
409/*
drh538f5702007-04-13 02:14:30 +0000410** The following global variables hold counters used for
411** testing purposes only. These variables do not exist in
412** a non-testing build. These variables are not thread-safe.
drhfcd35c72005-05-21 02:48:08 +0000413*/
414#ifdef SQLITE_TEST
drh538f5702007-04-13 02:14:30 +0000415int sqlite3_pager_readdb_count = 0; /* Number of full pages read from DB */
416int sqlite3_pager_writedb_count = 0; /* Number of full pages written to DB */
417int sqlite3_pager_writej_count = 0; /* Number of pages written to journal */
418int sqlite3_pager_pgfree_count = 0; /* Number of cache pages freed */
419# define PAGER_INCR(v) v++
drhfcd35c72005-05-21 02:48:08 +0000420#else
drh538f5702007-04-13 02:14:30 +0000421# define PAGER_INCR(v)
drhfcd35c72005-05-21 02:48:08 +0000422#endif
423
drh86f8c192007-08-22 00:39:19 +0000424/*
425** The following variable points to the head of a double-linked list
426** of all pagers that are eligible for page stealing by the
427** sqlite3_release_memory() interface. Access to this list is
428** protected by the SQLITE_MUTEX_STATIC_MEM2 mutex.
429*/
430#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
431static Pager *sqlite3PagerList = 0;
danielk19779f61c2f2007-08-27 17:27:49 +0000432static PagerLruList sqlite3LruPageList = {0, 0, 0};
drh86f8c192007-08-22 00:39:19 +0000433#endif
drh538f5702007-04-13 02:14:30 +0000434
435
drhfcd35c72005-05-21 02:48:08 +0000436/*
drh5e00f6c2001-09-13 13:46:56 +0000437** Journal files begin with the following magic string. The data
438** was obtained from /dev/random. It is used only as a sanity check.
drh94f33312002-08-12 12:29:56 +0000439**
drhae2b40c2004-06-09 19:03:54 +0000440** Since version 2.8.0, the journal format contains additional sanity
441** checking information. If the power fails while the journal is begin
442** written, semi-random garbage data might appear in the journal
443** file after power is restored. If an attempt is then made
drh968af522003-02-11 14:55:40 +0000444** to roll the journal back, the database could be corrupted. The additional
445** sanity checking data is an attempt to discover the garbage in the
446** journal and ignore it.
447**
drhae2b40c2004-06-09 19:03:54 +0000448** The sanity checking information for the new journal format consists
drh968af522003-02-11 14:55:40 +0000449** of a 32-bit checksum on each page of data. The checksum covers both
drh90f5ecb2004-07-22 01:19:35 +0000450** the page number and the pPager->pageSize bytes of data for the page.
drh968af522003-02-11 14:55:40 +0000451** This cksum is initialized to a 32-bit random value that appears in the
452** journal file right after the header. The random initializer is important,
453** because garbage data that appears at the end of a journal is likely
454** data that was once in other files that have now been deleted. If the
455** garbage data came from an obsolete journal file, the checksums might
456** be correct. But by initializing the checksum to random value which
457** is different for every journal, we minimize that risk.
drhd9b02572001-04-15 00:37:09 +0000458*/
drhae2b40c2004-06-09 19:03:54 +0000459static const unsigned char aJournalMagic[] = {
460 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
drhed7c8552001-04-11 14:29:21 +0000461};
462
463/*
drh726de592004-06-10 23:35:50 +0000464** The size of the header and of each page in the journal is determined
465** by the following macros.
drh968af522003-02-11 14:55:40 +0000466*/
drhae2b40c2004-06-09 19:03:54 +0000467#define JOURNAL_PG_SZ(pPager) ((pPager->pageSize) + 8)
drh968af522003-02-11 14:55:40 +0000468
danielk197776572402004-06-25 02:38:54 +0000469/*
470** The journal header size for this pager. In the future, this could be
471** set to some value read from the disk controller. The important
472** characteristic is that it is the same size as a disk sector.
473*/
474#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
475
drhb7f91642004-10-31 02:22:47 +0000476/*
477** The macro MEMDB is true if we are dealing with an in-memory database.
478** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
479** the value of MEMDB will be a constant and the compiler will optimize
480** out code that would never execute.
481*/
482#ifdef SQLITE_OMIT_MEMORYDB
483# define MEMDB 0
484#else
485# define MEMDB pPager->memDb
486#endif
487
488/*
danielk197776572402004-06-25 02:38:54 +0000489** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
490** reserved for working around a windows/posix incompatibility). It is
491** used in the journal to signify that the remainder of the journal file
492** is devoted to storing a master journal name - there are no more pages to
493** roll back. See comments for function writeMasterJournal() for details.
494*/
danielk1977599fcba2004-11-08 07:13:13 +0000495/* #define PAGER_MJ_PGNO(x) (PENDING_BYTE/((x)->pageSize)) */
496#define PAGER_MJ_PGNO(x) ((PENDING_BYTE/((x)->pageSize))+1)
danielk197713adf8a2004-06-03 16:08:41 +0000497
drh968af522003-02-11 14:55:40 +0000498/*
danielk197726836652005-01-17 01:33:13 +0000499** The maximum legal page number is (2^31 - 1).
500*/
501#define PAGER_MAX_PGNO 2147483647
502
503/*
drh86f8c192007-08-22 00:39:19 +0000504** The pagerEnter() and pagerLeave() routines acquire and release
505** a mutex on each pager. The mutex is recursive.
506**
507** This is a special-purpose mutex. It only provides mutual exclusion
508** between the Btree and the Memory Management sqlite3_release_memory()
509** function. It does not prevent, for example, two Btrees from accessing
510** the same pager at the same time. Other general-purpose mutexes in
511** the btree layer handle that chore.
512*/
513#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
514 static void pagerEnter(Pager *p){
515 p->iInUseDB++;
516 if( p->iInUseMM && p->iInUseDB==1 ){
517 sqlite3_mutex *mutex;
518 mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
519 p->iInUseDB = 0;
520 sqlite3_mutex_enter(mutex);
521 p->iInUseDB = 1;
522 sqlite3_mutex_leave(mutex);
523 }
524 assert( p->iInUseMM==0 );
525 }
526 static void pagerLeave(Pager *p){
527 p->iInUseDB--;
528 assert( p->iInUseDB>=0 );
529 }
530#else
531# define pagerEnter(X)
532# define pagerLeave(X)
533#endif
534
535/*
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
danielk197765839c62007-08-30 08:08:17 +0000875** written into memory supplied by the caller.
danielk197776572402004-06-25 02:38:54 +0000876**
danielk197765839c62007-08-30 08:08:17 +0000877** zMaster must point to a buffer of at least nMaster bytes allocated by
878** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
879** enough space to write the master journal name). If the master journal
880** name in the journal is longer than nMaster bytes (including a
881** nul-terminator), then this is handled as if no master journal name
882** were present in the journal.
883**
884** If no master journal file name is present zMaster[0] is set to 0 and
danielk197776572402004-06-25 02:38:54 +0000885** SQLITE_OK returned.
886*/
danielk197765839c62007-08-30 08:08:17 +0000887static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, int nMaster){
danielk197776572402004-06-25 02:38:54 +0000888 int rc;
889 u32 len;
drheb206252004-10-01 02:00:31 +0000890 i64 szJ;
danielk1977c3e8f5e2004-06-28 01:16:46 +0000891 u32 cksum;
danielk1977cafadba2004-06-25 11:11:54 +0000892 int i;
danielk197776572402004-06-25 02:38:54 +0000893 unsigned char aMagic[8]; /* A buffer to hold the magic header */
894
danielk197765839c62007-08-30 08:08:17 +0000895 zMaster[0] = '\0';
danielk197776572402004-06-25 02:38:54 +0000896
drh054889e2005-11-30 03:20:31 +0000897 rc = sqlite3OsFileSize(pJrnl, &szJ);
danielk1977cafadba2004-06-25 11:11:54 +0000898 if( rc!=SQLITE_OK || szJ<16 ) return rc;
danielk197776572402004-06-25 02:38:54 +0000899
danielk197762079062007-08-15 17:08:46 +0000900 rc = read32bits(pJrnl, szJ-16, &len);
danielk197776572402004-06-25 02:38:54 +0000901 if( rc!=SQLITE_OK ) return rc;
902
danielk197765839c62007-08-30 08:08:17 +0000903 if( len>=nMaster ){
904 return SQLITE_OK;
905 }
906
danielk197762079062007-08-15 17:08:46 +0000907 rc = read32bits(pJrnl, szJ-12, &cksum);
danielk1977cafadba2004-06-25 11:11:54 +0000908 if( rc!=SQLITE_OK ) return rc;
909
danielk197762079062007-08-15 17:08:46 +0000910 rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8);
danielk197776572402004-06-25 02:38:54 +0000911 if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc;
912
danielk197765839c62007-08-30 08:08:17 +0000913 rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len);
danielk197776572402004-06-25 02:38:54 +0000914 if( rc!=SQLITE_OK ){
danielk197776572402004-06-25 02:38:54 +0000915 return rc;
916 }
danielk197765839c62007-08-30 08:08:17 +0000917 zMaster[len] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000918
919 /* See if the checksum matches the master journal name */
920 for(i=0; i<len; i++){
danielk197765839c62007-08-30 08:08:17 +0000921 cksum -= zMaster[i];
922 }
danielk19778191bff2004-06-28 04:52:30 +0000923 if( cksum ){
924 /* If the checksum doesn't add up, then one or more of the disk sectors
925 ** containing the master journal filename is corrupted. This means
926 ** definitely roll back, so just return SQLITE_OK and report a (nul)
927 ** master-journal filename.
928 */
danielk197765839c62007-08-30 08:08:17 +0000929 zMaster[0] = '\0';
danielk1977cafadba2004-06-25 11:11:54 +0000930 }
danielk197776572402004-06-25 02:38:54 +0000931
932 return SQLITE_OK;
933}
934
935/*
936** Seek the journal file descriptor to the next sector boundary where a
937** journal header may be read or written. Pager.journalOff is updated with
938** the new seek offset.
939**
940** i.e for a sector size of 512:
941**
942** Input Offset Output Offset
943** ---------------------------------------
944** 0 0
945** 512 512
946** 100 512
947** 2000 2048
948**
949*/
danielk197762079062007-08-15 17:08:46 +0000950static void seekJournalHdr(Pager *pPager){
drheb206252004-10-01 02:00:31 +0000951 i64 offset = 0;
952 i64 c = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000953 if( c ){
954 offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
955 }
956 assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
957 assert( offset>=c );
958 assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
959 pPager->journalOff = offset;
danielk197776572402004-06-25 02:38:54 +0000960}
961
962/*
963** The journal file must be open when this routine is called. A journal
964** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
965** current location.
966**
967** The format for the journal header is as follows:
968** - 8 bytes: Magic identifying journal format.
969** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
970** - 4 bytes: Random number used for page hash.
971** - 4 bytes: Initial database page count.
972** - 4 bytes: Sector size used by the process that wrote this journal.
973**
danielk1977f42f25c2004-06-25 07:21:28 +0000974** Followed by (JOURNAL_HDR_SZ - 24) bytes of unused space.
danielk197776572402004-06-25 02:38:54 +0000975*/
976static int writeJournalHdr(Pager *pPager){
drh97b57482006-01-10 20:32:32 +0000977 char zHeader[sizeof(aJournalMagic)+16];
danielk19774099f6e2007-03-19 11:25:20 +0000978 int rc;
danielk197776572402004-06-25 02:38:54 +0000979
danielk19774099f6e2007-03-19 11:25:20 +0000980 if( pPager->stmtHdrOff==0 ){
981 pPager->stmtHdrOff = pPager->journalOff;
982 }
983
danielk197762079062007-08-15 17:08:46 +0000984 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +0000985 pPager->journalHdr = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +0000986
drh97b57482006-01-10 20:32:32 +0000987 memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
danielk19774cd2cd52007-08-23 14:48:23 +0000988
989 /*
990 ** Write the nRec Field - the number of page records that follow this
991 ** journal header. Normally, zero is written to this value at this time.
992 ** After the records are added to the journal (and the journal synced,
993 ** if in full-sync mode), the zero is overwritten with the true number
994 ** of records (see syncJournal()).
995 **
996 ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
997 ** reading the journal this value tells SQLite to assume that the
998 ** rest of the journal file contains valid page records. This assumption
999 ** is dangerous, as if a failure occured whilst writing to the journal
1000 ** file it may contain some garbage data. There are two scenarios
1001 ** where this risk can be ignored:
1002 **
1003 ** * When the pager is in no-sync mode. Corruption can follow a
1004 ** power failure in this case anyway.
1005 **
1006 ** * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
1007 ** that garbage data is never appended to the journal file.
1008 */
1009 assert(pPager->fd->pMethods||pPager->noSync);
1010 if( (pPager->noSync)
1011 || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND)
1012 ){
1013 put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
1014 }else{
1015 put32bits(&zHeader[sizeof(aJournalMagic)], 0);
1016 }
1017
drh97b57482006-01-10 20:32:32 +00001018 /* The random check-hash initialiser */
1019 sqlite3Randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
1020 put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
1021 /* The initial database size */
1022 put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbSize);
1023 /* The assumed sector size for this process */
1024 put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
drhb0603412007-02-28 04:47:26 +00001025 IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, sizeof(zHeader)))
danielk197762079062007-08-15 17:08:46 +00001026 rc = sqlite3OsWrite(pPager->jfd, zHeader, sizeof(zHeader),pPager->journalOff);
1027 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk197776572402004-06-25 02:38:54 +00001028
1029 /* The journal header has been written successfully. Seek the journal
1030 ** file descriptor to the end of the journal header sector.
1031 */
1032 if( rc==SQLITE_OK ){
drhb0603412007-02-28 04:47:26 +00001033 IOTRACE(("JTAIL %p %lld\n", pPager, pPager->journalOff-1))
danielk197762079062007-08-15 17:08:46 +00001034 rc = sqlite3OsWrite(pPager->jfd, "\000", 1, pPager->journalOff-1);
danielk197776572402004-06-25 02:38:54 +00001035 }
1036 return rc;
1037}
1038
1039/*
1040** The journal file must be open when this is called. A journal header file
1041** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
1042** file. See comments above function writeJournalHdr() for a description of
1043** the journal header format.
1044**
1045** If the header is read successfully, *nRec is set to the number of
1046** page records following this header and *dbSize is set to the size of the
1047** database before the transaction began, in pages. Also, pPager->cksumInit
1048** is set to the value read from the journal header. SQLITE_OK is returned
1049** in this case.
1050**
1051** If the journal header file appears to be corrupted, SQLITE_DONE is
1052** returned and *nRec and *dbSize are not set. If JOURNAL_HDR_SZ bytes
1053** cannot be read from the journal file an error code is returned.
1054*/
1055static int readJournalHdr(
1056 Pager *pPager,
drheb206252004-10-01 02:00:31 +00001057 i64 journalSize,
danielk197776572402004-06-25 02:38:54 +00001058 u32 *pNRec,
1059 u32 *pDbSize
1060){
1061 int rc;
1062 unsigned char aMagic[8]; /* A buffer to hold the magic header */
danielk197762079062007-08-15 17:08:46 +00001063 i64 jrnlOff;
danielk197776572402004-06-25 02:38:54 +00001064
danielk197762079062007-08-15 17:08:46 +00001065 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +00001066 if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
1067 return SQLITE_DONE;
1068 }
danielk197762079062007-08-15 17:08:46 +00001069 jrnlOff = pPager->journalOff;
danielk197776572402004-06-25 02:38:54 +00001070
danielk197762079062007-08-15 17:08:46 +00001071 rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff);
danielk197776572402004-06-25 02:38:54 +00001072 if( rc ) return rc;
danielk197762079062007-08-15 17:08:46 +00001073 jrnlOff += sizeof(aMagic);
danielk197776572402004-06-25 02:38:54 +00001074
1075 if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
1076 return SQLITE_DONE;
1077 }
1078
danielk197762079062007-08-15 17:08:46 +00001079 rc = read32bits(pPager->jfd, jrnlOff, pNRec);
danielk197776572402004-06-25 02:38:54 +00001080 if( rc ) return rc;
1081
danielk197762079062007-08-15 17:08:46 +00001082 rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit);
danielk197776572402004-06-25 02:38:54 +00001083 if( rc ) return rc;
1084
danielk197762079062007-08-15 17:08:46 +00001085 rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize);
danielk197776572402004-06-25 02:38:54 +00001086 if( rc ) return rc;
1087
1088 /* Update the assumed sector-size to match the value used by
1089 ** the process that created this journal. If this journal was
1090 ** created by a process other than this one, then this routine
1091 ** is being called from within pager_playback(). The local value
1092 ** of Pager.sectorSize is restored at the end of that routine.
1093 */
danielk197762079062007-08-15 17:08:46 +00001094 rc = read32bits(pPager->jfd, jrnlOff+12, (u32 *)&pPager->sectorSize);
danielk197776572402004-06-25 02:38:54 +00001095 if( rc ) return rc;
1096
1097 pPager->journalOff += JOURNAL_HDR_SZ(pPager);
danielk197762079062007-08-15 17:08:46 +00001098 return SQLITE_OK;
danielk197776572402004-06-25 02:38:54 +00001099}
1100
1101
1102/*
1103** Write the supplied master journal name into the journal file for pager
danielk1977cafadba2004-06-25 11:11:54 +00001104** pPager at the current location. The master journal name must be the last
1105** thing written to a journal file. If the pager is in full-sync mode, the
1106** journal file descriptor is advanced to the next sector boundary before
1107** anything is written. The format is:
1108**
1109** + 4 bytes: PAGER_MJ_PGNO.
1110** + N bytes: length of master journal name.
1111** + 4 bytes: N
1112** + 4 bytes: Master journal name checksum.
1113** + 8 bytes: aJournalMagic[].
1114**
1115** The master journal page checksum is the sum of the bytes in the master
1116** journal name.
danielk1977aef0bf62005-12-30 16:28:01 +00001117**
1118** If zMaster is a NULL pointer (occurs for a single database transaction),
1119** this call is a no-op.
danielk197776572402004-06-25 02:38:54 +00001120*/
1121static int writeMasterJournal(Pager *pPager, const char *zMaster){
1122 int rc;
1123 int len;
danielk1977cafadba2004-06-25 11:11:54 +00001124 int i;
danielk197762079062007-08-15 17:08:46 +00001125 i64 jrnlOff;
drh97b57482006-01-10 20:32:32 +00001126 u32 cksum = 0;
1127 char zBuf[sizeof(aJournalMagic)+2*4];
danielk197776572402004-06-25 02:38:54 +00001128
1129 if( !zMaster || pPager->setMaster) return SQLITE_OK;
1130 pPager->setMaster = 1;
1131
1132 len = strlen(zMaster);
danielk1977cafadba2004-06-25 11:11:54 +00001133 for(i=0; i<len; i++){
1134 cksum += zMaster[i];
1135 }
danielk197776572402004-06-25 02:38:54 +00001136
1137 /* If in full-sync mode, advance to the next disk sector before writing
1138 ** the master journal name. This is in case the previous page written to
1139 ** the journal has already been synced.
1140 */
1141 if( pPager->fullSync ){
danielk197762079062007-08-15 17:08:46 +00001142 seekJournalHdr(pPager);
danielk197776572402004-06-25 02:38:54 +00001143 }
danielk197762079062007-08-15 17:08:46 +00001144 jrnlOff = pPager->journalOff;
danielk1977cafadba2004-06-25 11:11:54 +00001145 pPager->journalOff += (len+20);
danielk197776572402004-06-25 02:38:54 +00001146
danielk197762079062007-08-15 17:08:46 +00001147 rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager));
danielk197776572402004-06-25 02:38:54 +00001148 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001149 jrnlOff += 4;
danielk197776572402004-06-25 02:38:54 +00001150
danielk197762079062007-08-15 17:08:46 +00001151 rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff);
danielk197776572402004-06-25 02:38:54 +00001152 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001153 jrnlOff += len;
danielk197776572402004-06-25 02:38:54 +00001154
drh97b57482006-01-10 20:32:32 +00001155 put32bits(zBuf, len);
1156 put32bits(&zBuf[4], cksum);
1157 memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic));
danielk197762079062007-08-15 17:08:46 +00001158 rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff);
drh2c8997b2005-08-27 16:36:48 +00001159 pPager->needSync = !pPager->noSync;
danielk197776572402004-06-25 02:38:54 +00001160 return rc;
1161}
1162
1163/*
drh03eb96a2002-11-10 23:32:56 +00001164** Add or remove a page from the list of all pages that are in the
drhac69b052004-05-12 13:30:07 +00001165** statement journal.
drh03eb96a2002-11-10 23:32:56 +00001166**
1167** The Pager keeps a separate list of pages that are currently in
danielk19773b8a05f2007-03-19 17:44:26 +00001168** the statement journal. This helps the sqlite3PagerStmtCommit()
drh03eb96a2002-11-10 23:32:56 +00001169** routine run MUCH faster for the common case where there are many
drhac69b052004-05-12 13:30:07 +00001170** pages in memory but only a few are in the statement journal.
drh03eb96a2002-11-10 23:32:56 +00001171*/
drh3aac2dd2004-04-26 14:10:20 +00001172static void page_add_to_stmt_list(PgHdr *pPg){
drh03eb96a2002-11-10 23:32:56 +00001173 Pager *pPager = pPg->pPager;
danielk1977f35843b2007-04-07 15:03:17 +00001174 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
1175 assert( MEMDB );
1176 if( !pHist->inStmt ){
1177 assert( pHist->pPrevStmt==0 && pHist->pNextStmt==0 );
1178 if( pPager->pStmt ){
1179 PGHDR_TO_HIST(pPager->pStmt, pPager)->pPrevStmt = pPg;
1180 }
1181 pHist->pNextStmt = pPager->pStmt;
1182 pPager->pStmt = pPg;
1183 pHist->inStmt = 1;
drh03eb96a2002-11-10 23:32:56 +00001184 }
drh03eb96a2002-11-10 23:32:56 +00001185}
1186
1187/*
drhed7c8552001-04-11 14:29:21 +00001188** Find a page in the hash table given its page number. Return
1189** a pointer to the page or NULL if not found.
1190*/
drhd9b02572001-04-15 00:37:09 +00001191static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drh8ca0c722006-05-07 17:49:38 +00001192 PgHdr *p;
1193 if( pPager->aHash==0 ) return 0;
1194 p = pPager->aHash[pgno & (pPager->nHash-1)];
drhed7c8552001-04-11 14:29:21 +00001195 while( p && p->pgno!=pgno ){
1196 p = p->pNextHash;
1197 }
1198 return p;
1199}
1200
1201/*
drh1aa2d8b2007-01-03 15:34:29 +00001202** Unlock the database file.
drh1aa2d8b2007-01-03 15:34:29 +00001203*/
1204static void pager_unlock(Pager *pPager){
danielk197741483462007-03-24 16:45:04 +00001205 if( !pPager->exclusiveMode ){
1206 if( !MEMDB ){
danielk19777a2b1ee2007-08-21 14:27:01 +00001207 osUnlock(pPager->fd, NO_LOCK);
danielk197741483462007-03-24 16:45:04 +00001208 pPager->dbSize = -1;
1209 IOTRACE(("UNLOCK %p\n", pPager))
1210 }
1211 pPager->state = PAGER_UNLOCK;
1212 pPager->changeCountDone = 0;
drh1aa2d8b2007-01-03 15:34:29 +00001213 }
danielk1977e277be02007-03-23 18:12:06 +00001214}
1215
1216/*
1217** Execute a rollback if a transaction is active and unlock the
1218** database file. This is a no-op if the pager has already entered
1219** the error-state.
1220*/
danielk1977334cdb62007-03-26 08:05:12 +00001221static void pagerUnlockAndRollback(Pager *p){
1222 if( p->errCode ) return;
danielk1977c5859712007-03-26 12:26:27 +00001223 assert( p->state>=PAGER_RESERVED || p->journalOpen==0 );
danielk1977334cdb62007-03-26 08:05:12 +00001224 if( p->state>=PAGER_RESERVED ){
1225 sqlite3PagerRollback(p);
danielk1977e277be02007-03-23 18:12:06 +00001226 }
danielk1977334cdb62007-03-26 08:05:12 +00001227 pager_unlock(p);
1228 assert( p->errCode || !p->journalOpen || (p->exclusiveMode&&!p->journalOff) );
1229 assert( p->errCode || !p->stmtOpen || p->exclusiveMode );
drh1aa2d8b2007-01-03 15:34:29 +00001230}
1231
1232
1233/*
danielk1977e180dd92007-04-05 17:15:52 +00001234** Clear the in-memory cache. This routine
drhed7c8552001-04-11 14:29:21 +00001235** sets the state of the pager back to what it was when it was first
1236** opened. Any outstanding pages are invalidated and subsequent attempts
1237** to access those pages will likely result in a coredump.
1238*/
drhd9b02572001-04-15 00:37:09 +00001239static void pager_reset(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00001240 PgHdr *pPg, *pNext;
danielk1977efaaf572006-01-16 11:29:19 +00001241 if( pPager->errCode ) return;
drhd9b02572001-04-15 00:37:09 +00001242 for(pPg=pPager->pAll; pPg; pPg=pNext){
drh538f5702007-04-13 02:14:30 +00001243 IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
1244 PAGER_INCR(sqlite3_pager_pgfree_count);
drhd9b02572001-04-15 00:37:09 +00001245 pNext = pPg->pNextAll;
danielk19779f61c2f2007-08-27 17:27:49 +00001246 lruListRemove(pPg);
drhbf4bca52007-09-06 22:19:14 +00001247 sqlite3_free(pPg->pData);
drh17435752007-08-16 04:30:38 +00001248 sqlite3_free(pPg);
drhed7c8552001-04-11 14:29:21 +00001249 }
danielk19779f61c2f2007-08-27 17:27:49 +00001250 assert(pPager->lru.pFirst==0);
1251 assert(pPager->lru.pFirstSynced==0);
1252 assert(pPager->lru.pLast==0);
danielk1977b94bf852007-03-19 13:53:37 +00001253 pPager->pStmt = 0;
drhd9b02572001-04-15 00:37:09 +00001254 pPager->pAll = 0;
drh8ca0c722006-05-07 17:49:38 +00001255 pPager->nHash = 0;
drh17435752007-08-16 04:30:38 +00001256 sqlite3_free(pPager->aHash);
drhed7c8552001-04-11 14:29:21 +00001257 pPager->nPage = 0;
drh8ca0c722006-05-07 17:49:38 +00001258 pPager->aHash = 0;
drhed7c8552001-04-11 14:29:21 +00001259 pPager->nRef = 0;
danielk1977e277be02007-03-23 18:12:06 +00001260}
1261
1262/*
drh80e35f42007-03-30 14:06:34 +00001263** This routine ends a transaction. A transaction is ended by either
1264** a COMMIT or a ROLLBACK.
1265**
drhed7c8552001-04-11 14:29:21 +00001266** When this routine is called, the pager has the journal file open and
drh80e35f42007-03-30 14:06:34 +00001267** a RESERVED or EXCLUSIVE lock on the database. This routine will release
1268** the database lock and acquires a SHARED lock in its place if that is
1269** the appropriate thing to do. Release locks usually is appropriate,
1270** unless we are in exclusive access mode or unless this is a
1271** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation.
1272**
1273** The journal file is either deleted or truncated.
drh50457892003-09-06 01:10:47 +00001274**
1275** TODO: Consider keeping the journal file open for temporary databases.
1276** This might give a performance improvement on windows where opening
1277** a file is an expensive operation.
drhed7c8552001-04-11 14:29:21 +00001278*/
drh80e35f42007-03-30 14:06:34 +00001279static int pager_end_transaction(Pager *pPager){
drhd9b02572001-04-15 00:37:09 +00001280 PgHdr *pPg;
danielk197741483462007-03-24 16:45:04 +00001281 int rc = SQLITE_OK;
danielk1977979f38e2007-03-27 16:19:51 +00001282 int rc2 = SQLITE_OK;
drhb7f91642004-10-31 02:22:47 +00001283 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +00001284 if( pPager->state<PAGER_RESERVED ){
1285 return SQLITE_OK;
1286 }
danielk19773b8a05f2007-03-19 17:44:26 +00001287 sqlite3PagerStmtCommit(pPager);
danielk1977334cdb62007-03-26 08:05:12 +00001288 if( pPager->stmtOpen && !pPager->exclusiveMode ){
danielk1977b4b47412007-08-17 15:53:36 +00001289 sqlite3OsClose(pPager->stfd);
danielk1977979f38e2007-03-27 16:19:51 +00001290 pPager->stmtOpen = 0;
drh0f892532002-05-30 12:27:03 +00001291 }
drhda47d772002-12-02 04:25:19 +00001292 if( pPager->journalOpen ){
drh01fa4c32007-04-02 11:22:22 +00001293 if( pPager->exclusiveMode
1294 && (rc = sqlite3OsTruncate(pPager->jfd, 0))==SQLITE_OK ){;
danielk197741483462007-03-24 16:45:04 +00001295 pPager->journalOff = 0;
danielk1977334cdb62007-03-26 08:05:12 +00001296 pPager->journalStarted = 0;
danielk197741483462007-03-24 16:45:04 +00001297 }else{
danielk1977b4b47412007-08-17 15:53:36 +00001298 sqlite3OsClose(pPager->jfd);
danielk197741483462007-03-24 16:45:04 +00001299 pPager->journalOpen = 0;
drh01fa4c32007-04-02 11:22:22 +00001300 if( rc==SQLITE_OK ){
danielk1977fee2d252007-08-18 10:59:19 +00001301 rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
danielk19777152de82007-03-29 17:28:14 +00001302 }
danielk197741483462007-03-24 16:45:04 +00001303 }
drh17435752007-08-16 04:30:38 +00001304 sqlite3_free( pPager->aInJournal );
drhda47d772002-12-02 04:25:19 +00001305 pPager->aInJournal = 0;
1306 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1307 pPg->inJournal = 0;
1308 pPg->dirty = 0;
drhdb48ee02003-01-16 13:42:43 +00001309 pPg->needSync = 0;
danielk197741483462007-03-24 16:45:04 +00001310 pPg->alwaysRollback = 0;
danielk19773c407372005-02-15 02:54:14 +00001311#ifdef SQLITE_CHECK_PAGES
1312 pPg->pageHash = pager_pagehash(pPg);
1313#endif
drhda47d772002-12-02 04:25:19 +00001314 }
drh605ad8f2006-05-03 23:34:05 +00001315 pPager->pDirty = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001316 pPager->dirtyCache = 0;
danielk1977ef317ab2004-06-23 10:43:10 +00001317 pPager->nRec = 0;
drhda47d772002-12-02 04:25:19 +00001318 }else{
drh88b01a12005-03-28 18:04:27 +00001319 assert( pPager->aInJournal==0 );
drha6abd042004-06-09 17:37:22 +00001320 assert( pPager->dirtyCache==0 || pPager->useJournal==0 );
drhd9b02572001-04-15 00:37:09 +00001321 }
danielk1977979f38e2007-03-27 16:19:51 +00001322
danielk197741483462007-03-24 16:45:04 +00001323 if( !pPager->exclusiveMode ){
danielk19777a2b1ee2007-08-21 14:27:01 +00001324 rc2 = osUnlock(pPager->fd, SHARED_LOCK);
danielk197741483462007-03-24 16:45:04 +00001325 pPager->state = PAGER_SHARED;
danielk1977334cdb62007-03-26 08:05:12 +00001326 }else if( pPager->state==PAGER_SYNCED ){
1327 pPager->state = PAGER_EXCLUSIVE;
danielk197741483462007-03-24 16:45:04 +00001328 }
danielk1977334cdb62007-03-26 08:05:12 +00001329 pPager->origDbSize = 0;
danielk197776572402004-06-25 02:38:54 +00001330 pPager->setMaster = 0;
danielk1977c4da5b92006-01-21 12:08:54 +00001331 pPager->needSync = 0;
danielk19779f61c2f2007-08-27 17:27:49 +00001332 lruListSetFirstSynced(pPager);
drh588f5bc2007-01-02 18:41:54 +00001333 pPager->dbSize = -1;
danielk1977979f38e2007-03-27 16:19:51 +00001334
1335 return (rc==SQLITE_OK?rc2:rc);
drhed7c8552001-04-11 14:29:21 +00001336}
1337
drhed7c8552001-04-11 14:29:21 +00001338/*
drh968af522003-02-11 14:55:40 +00001339** Compute and return a checksum for the page of data.
drh34e79ce2004-02-08 06:05:46 +00001340**
1341** This is not a real checksum. It is really just the sum of the
drh726de592004-06-10 23:35:50 +00001342** random initial value and the page number. We experimented with
1343** a checksum of the entire data, but that was found to be too slow.
1344**
1345** Note that the page number is stored at the beginning of data and
1346** the checksum is stored at the end. This is important. If journal
1347** corruption occurs due to a power failure, the most likely scenario
1348** is that one end or the other of the record will be changed. It is
1349** much less likely that the two ends of the journal record will be
1350** correct and the middle be corrupt. Thus, this "checksum" scheme,
1351** though fast and simple, catches the mostly likely kind of corruption.
1352**
1353** FIX ME: Consider adding every 200th (or so) byte of the data to the
1354** checksum. That way if a single page spans 3 or more disk sectors and
1355** only the middle sector is corrupt, we will still have a reasonable
1356** chance of failing the checksum and thus detecting the problem.
drh968af522003-02-11 14:55:40 +00001357*/
drh74161702006-02-24 02:53:49 +00001358static u32 pager_cksum(Pager *pPager, const u8 *aData){
danielk1977ef317ab2004-06-23 10:43:10 +00001359 u32 cksum = pPager->cksumInit;
1360 int i = pPager->pageSize-200;
1361 while( i>0 ){
1362 cksum += aData[i];
1363 i -= 200;
1364 }
drh968af522003-02-11 14:55:40 +00001365 return cksum;
1366}
1367
drh605ad8f2006-05-03 23:34:05 +00001368/* Forward declaration */
1369static void makeClean(PgHdr*);
1370
drh968af522003-02-11 14:55:40 +00001371/*
drhfa86c412002-02-02 15:01:15 +00001372** Read a single page from the journal file opened on file descriptor
1373** jfd. Playback this one page.
drh968af522003-02-11 14:55:40 +00001374**
drh726de592004-06-10 23:35:50 +00001375** If useCksum==0 it means this journal does not use checksums. Checksums
1376** are not used in statement journals because statement journals do not
1377** need to survive power failures.
drhfa86c412002-02-02 15:01:15 +00001378*/
danielk197762079062007-08-15 17:08:46 +00001379static int pager_playback_one_page(
1380 Pager *pPager,
1381 sqlite3_file *jfd,
1382 i64 offset,
1383 int useCksum
1384){
drhfa86c412002-02-02 15:01:15 +00001385 int rc;
drhae2b40c2004-06-09 19:03:54 +00001386 PgHdr *pPg; /* An existing page in the cache */
1387 Pgno pgno; /* The page number of a page in journal */
1388 u32 cksum; /* Checksum used for sanity checking */
danielk19778186df82007-03-06 13:45:59 +00001389 u8 *aData = (u8 *)pPager->pTmpSpace; /* Temp storage for a page */
drhfa86c412002-02-02 15:01:15 +00001390
drh96362842005-03-20 22:47:56 +00001391 /* useCksum should be true for the main journal and false for
1392 ** statement journals. Verify that this is always the case
1393 */
drh9cbe6352005-11-29 03:13:21 +00001394 assert( jfd == (useCksum ? pPager->jfd : pPager->stfd) );
danielk19778186df82007-03-06 13:45:59 +00001395 assert( aData );
drh96362842005-03-20 22:47:56 +00001396
danielk197762079062007-08-15 17:08:46 +00001397 rc = read32bits(jfd, offset, &pgno);
drh99ee3602003-02-16 19:13:36 +00001398 if( rc!=SQLITE_OK ) return rc;
danielk197762079062007-08-15 17:08:46 +00001399 rc = sqlite3OsRead(jfd, aData, pPager->pageSize, offset+4);
drh99ee3602003-02-16 19:13:36 +00001400 if( rc!=SQLITE_OK ) return rc;
danielk197776572402004-06-25 02:38:54 +00001401 pPager->journalOff += pPager->pageSize + 4;
drhfa86c412002-02-02 15:01:15 +00001402
drh968af522003-02-11 14:55:40 +00001403 /* Sanity checking on the page. This is more important that I originally
1404 ** thought. If a power failure occurs while the journal is being written,
1405 ** it could cause invalid data to be written into the journal. We need to
1406 ** detect this invalid data (with high probability) and ignore it.
1407 */
danielk197775edc162004-06-26 01:48:18 +00001408 if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh968af522003-02-11 14:55:40 +00001409 return SQLITE_DONE;
1410 }
drhae2b40c2004-06-09 19:03:54 +00001411 if( pgno>(unsigned)pPager->dbSize ){
drh968af522003-02-11 14:55:40 +00001412 return SQLITE_OK;
1413 }
drhae2b40c2004-06-09 19:03:54 +00001414 if( useCksum ){
danielk197762079062007-08-15 17:08:46 +00001415 rc = read32bits(jfd, offset+pPager->pageSize+4, &cksum);
drh99ee3602003-02-16 19:13:36 +00001416 if( rc ) return rc;
danielk197776572402004-06-25 02:38:54 +00001417 pPager->journalOff += 4;
drh74161702006-02-24 02:53:49 +00001418 if( pager_cksum(pPager, aData)!=cksum ){
drh968af522003-02-11 14:55:40 +00001419 return SQLITE_DONE;
1420 }
1421 }
drhfa86c412002-02-02 15:01:15 +00001422
danielk1977aa5ccdf2004-06-14 05:10:42 +00001423 assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE );
danielk1977a3f3a5f2004-06-10 04:32:16 +00001424
1425 /* If the pager is in RESERVED state, then there must be a copy of this
1426 ** page in the pager cache. In this case just update the pager cache,
danielk19770de0bb32004-06-10 05:59:24 +00001427 ** not the database file. The page is left marked dirty in this case.
1428 **
danielk19772df71c72007-05-24 07:22:42 +00001429 ** An exception to the above rule: If the database is in no-sync mode
1430 ** and a page is moved during an incremental vacuum then the page may
danielk1977369f3a02007-05-24 09:41:20 +00001431 ** not be in the pager cache. Later: if a malloc() or IO error occurs
1432 ** during a Movepage() call, then the page may not be in the cache
1433 ** either. So the condition described in the above paragraph is not
1434 ** assert()able.
danielk19772df71c72007-05-24 07:22:42 +00001435 **
danielk1977a3f3a5f2004-06-10 04:32:16 +00001436 ** If in EXCLUSIVE state, then we update the pager cache if it exists
1437 ** and the main file. The page is then marked not dirty.
drh96362842005-03-20 22:47:56 +00001438 **
1439 ** Ticket #1171: The statement journal might contain page content that is
1440 ** different from the page content at the start of the transaction.
1441 ** This occurs when a page is changed prior to the start of a statement
1442 ** then changed again within the statement. When rolling back such a
1443 ** statement we must not write to the original database unless we know
drh5e385312007-06-16 04:42:12 +00001444 ** for certain that original page contents are synced into the main rollback
1445 ** journal. Otherwise, a power loss might leave modified data in the
1446 ** database file without an entry in the rollback journal that can
1447 ** restore the database to its original form. Two conditions must be
1448 ** met before writing to the database files. (1) the database must be
1449 ** locked. (2) we know that the original page content is fully synced
1450 ** in the main journal either because the page is not in cache or else
1451 ** the page is marked as needSync==0.
drhfa86c412002-02-02 15:01:15 +00001452 */
drhae2b40c2004-06-09 19:03:54 +00001453 pPg = pager_lookup(pPager, pgno);
drh477731b2007-06-16 03:06:27 +00001454 PAGERTRACE4("PLAYBACK %d page %d hash(%08x)\n",
1455 PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData));
drh96362842005-03-20 22:47:56 +00001456 if( pPager->state>=PAGER_EXCLUSIVE && (pPg==0 || pPg->needSync==0) ){
danielk197762079062007-08-15 17:08:46 +00001457 i64 offset = (pgno-1)*(i64)pPager->pageSize;
1458 rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, offset);
drh605ad8f2006-05-03 23:34:05 +00001459 if( pPg ){
1460 makeClean(pPg);
1461 }
danielk1977a3f3a5f2004-06-10 04:32:16 +00001462 }
drhfa86c412002-02-02 15:01:15 +00001463 if( pPg ){
danielk197728129562005-01-11 10:25:06 +00001464 /* No page should ever be explicitly rolled back that is in use, except
1465 ** for page 1 which is held in use in order to keep the lock on the
1466 ** database active. However such a page may be rolled back as a result
1467 ** of an internal error resulting in an automatic call to
danielk19773b8a05f2007-03-19 17:44:26 +00001468 ** sqlite3PagerRollback().
drhacf4ac92003-12-17 23:57:34 +00001469 */
drhb6f41482004-05-14 01:58:11 +00001470 void *pData;
danielk197728129562005-01-11 10:25:06 +00001471 /* assert( pPg->nRef==0 || pPg->pgno==1 ); */
drhb6f41482004-05-14 01:58:11 +00001472 pData = PGHDR_TO_DATA(pPg);
drhae2b40c2004-06-09 19:03:54 +00001473 memcpy(pData, aData, pPager->pageSize);
danielk19779038bb62007-04-09 11:20:54 +00001474 if( pPager->xReiniter ){
1475 pPager->xReiniter(pPg, pPager->pageSize);
drhde647132004-05-07 17:57:49 +00001476 }
danielk19773c407372005-02-15 02:54:14 +00001477#ifdef SQLITE_CHECK_PAGES
drh96362842005-03-20 22:47:56 +00001478 pPg->pageHash = pager_pagehash(pPg);
danielk19773c407372005-02-15 02:54:14 +00001479#endif
drh86a88112007-04-16 15:02:19 +00001480 /* If this was page 1, then restore the value of Pager.dbFileVers.
1481 ** Do this before any decoding. */
danielk197741483462007-03-24 16:45:04 +00001482 if( pgno==1 ){
drh86a88112007-04-16 15:02:19 +00001483 memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
danielk197741483462007-03-24 16:45:04 +00001484 }
drh86a88112007-04-16 15:02:19 +00001485
1486 /* Decode the page just read from disk */
1487 CODEC1(pPager, pData, pPg->pgno, 3);
drhfa86c412002-02-02 15:01:15 +00001488 }
1489 return rc;
1490}
1491
1492/*
danielk197713adf8a2004-06-03 16:08:41 +00001493** Parameter zMaster is the name of a master journal file. A single journal
1494** file that referred to the master journal file has just been rolled back.
1495** This routine checks if it is possible to delete the master journal file,
1496** and does so if it is.
drh726de592004-06-10 23:35:50 +00001497**
danielk197765839c62007-08-30 08:08:17 +00001498** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not
1499** available for use within this function.
1500**
1501**
drh726de592004-06-10 23:35:50 +00001502** The master journal file contains the names of all child journals.
1503** To tell if a master journal can be deleted, check to each of the
1504** children. If all children are either missing or do not refer to
1505** a different master journal, then this master journal can be deleted.
danielk197713adf8a2004-06-03 16:08:41 +00001506*/
danielk1977b4b47412007-08-17 15:53:36 +00001507static int pager_delmaster(Pager *pPager, const char *zMaster){
1508 sqlite3_vfs *pVfs = pPager->pVfs;
danielk197713adf8a2004-06-03 16:08:41 +00001509 int rc;
1510 int master_open = 0;
danielk1977b4b47412007-08-17 15:53:36 +00001511 sqlite3_file *pMaster;
1512 sqlite3_file *pJournal;
danielk197713adf8a2004-06-03 16:08:41 +00001513 char *zMasterJournal = 0; /* Contents of master journal file */
drheb206252004-10-01 02:00:31 +00001514 i64 nMasterJournal; /* Size of master journal file */
danielk197713adf8a2004-06-03 16:08:41 +00001515
1516 /* Open the master journal file exclusively in case some other process
1517 ** is running this routine also. Not that it makes too much difference.
1518 */
danielk1977b4b47412007-08-17 15:53:36 +00001519 pMaster = (sqlite3_file *)sqlite3_malloc(pVfs->szOsFile * 2);
danielk1977fee2d252007-08-18 10:59:19 +00001520 pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
danielk1977b4b47412007-08-17 15:53:36 +00001521 if( !pMaster ){
1522 rc = SQLITE_NOMEM;
1523 }else{
danielk1977fee2d252007-08-18 10:59:19 +00001524 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
1525 rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
danielk1977b4b47412007-08-17 15:53:36 +00001526 }
danielk197713adf8a2004-06-03 16:08:41 +00001527 if( rc!=SQLITE_OK ) goto delmaster_out;
1528 master_open = 1;
danielk1977b4b47412007-08-17 15:53:36 +00001529
1530 rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001531 if( rc!=SQLITE_OK ) goto delmaster_out;
1532
1533 if( nMasterJournal>0 ){
danielk19775865e3d2004-06-14 06:03:57 +00001534 char *zJournal;
danielk197776572402004-06-25 02:38:54 +00001535 char *zMasterPtr = 0;
danielk197765839c62007-08-30 08:08:17 +00001536 int nMasterPtr = pPager->pVfs->mxPathname+1;
danielk19775865e3d2004-06-14 06:03:57 +00001537
1538 /* Load the entire master journal file into space obtained from
drh17435752007-08-16 04:30:38 +00001539 ** sqlite3_malloc() and pointed to by zMasterJournal.
danielk19775865e3d2004-06-14 06:03:57 +00001540 */
danielk197765839c62007-08-30 08:08:17 +00001541 zMasterJournal = (char *)sqlite3_malloc(nMasterJournal + nMasterPtr);
danielk197713adf8a2004-06-03 16:08:41 +00001542 if( !zMasterJournal ){
1543 rc = SQLITE_NOMEM;
1544 goto delmaster_out;
1545 }
danielk197765839c62007-08-30 08:08:17 +00001546 zMasterPtr = &zMasterJournal[nMasterJournal];
danielk1977b4b47412007-08-17 15:53:36 +00001547 rc = sqlite3OsRead(pMaster, zMasterJournal, nMasterJournal, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001548 if( rc!=SQLITE_OK ) goto delmaster_out;
1549
danielk19775865e3d2004-06-14 06:03:57 +00001550 zJournal = zMasterJournal;
1551 while( (zJournal-zMasterJournal)<nMasterJournal ){
danielk1977b4b47412007-08-17 15:53:36 +00001552 if( sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS) ){
danielk197713adf8a2004-06-03 16:08:41 +00001553 /* One of the journals pointed to by the master journal exists.
1554 ** Open it and check if it points at the master journal. If
1555 ** so, return without deleting the master journal file.
1556 */
drh3b7b78b2004-11-24 01:16:43 +00001557 int c;
danielk1977fee2d252007-08-18 10:59:19 +00001558 int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
1559 rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001560 if( rc!=SQLITE_OK ){
danielk197713adf8a2004-06-03 16:08:41 +00001561 goto delmaster_out;
1562 }
danielk197713adf8a2004-06-03 16:08:41 +00001563
danielk197765839c62007-08-30 08:08:17 +00001564 rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
danielk1977b4b47412007-08-17 15:53:36 +00001565 sqlite3OsClose(pJournal);
danielk19779eed5052004-06-04 10:38:30 +00001566 if( rc!=SQLITE_OK ){
danielk19779eed5052004-06-04 10:38:30 +00001567 goto delmaster_out;
1568 }
danielk197776572402004-06-25 02:38:54 +00001569
danielk197765839c62007-08-30 08:08:17 +00001570 c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
drh3b7b78b2004-11-24 01:16:43 +00001571 if( c ){
danielk197776572402004-06-25 02:38:54 +00001572 /* We have a match. Do not delete the master journal file. */
1573 goto delmaster_out;
danielk197713adf8a2004-06-03 16:08:41 +00001574 }
1575 }
danielk19775865e3d2004-06-14 06:03:57 +00001576 zJournal += (strlen(zJournal)+1);
danielk197713adf8a2004-06-03 16:08:41 +00001577 }
1578 }
1579
danielk1977fee2d252007-08-18 10:59:19 +00001580 rc = sqlite3OsDelete(pVfs, zMaster, 0);
danielk197713adf8a2004-06-03 16:08:41 +00001581
1582delmaster_out:
1583 if( zMasterJournal ){
drh17435752007-08-16 04:30:38 +00001584 sqlite3_free(zMasterJournal);
danielk197713adf8a2004-06-03 16:08:41 +00001585 }
1586 if( master_open ){
danielk1977b4b47412007-08-17 15:53:36 +00001587 sqlite3OsClose(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001588 }
danielk1977b4b47412007-08-17 15:53:36 +00001589 sqlite3_free(pMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001590 return rc;
1591}
1592
drha6abd042004-06-09 17:37:22 +00001593
danielk1977e180dd92007-04-05 17:15:52 +00001594static void pager_truncate_cache(Pager *pPager);
1595
drha6abd042004-06-09 17:37:22 +00001596/*
drhcb4c40b2004-08-18 19:09:43 +00001597** Truncate the main file of the given pager to the number of pages
danielk1977e180dd92007-04-05 17:15:52 +00001598** indicated. Also truncate the cached representation of the file.
drhcb4c40b2004-08-18 19:09:43 +00001599*/
1600static int pager_truncate(Pager *pPager, int nPage){
danielk1977e180dd92007-04-05 17:15:52 +00001601 int rc = SQLITE_OK;
danielk19777a2b1ee2007-08-21 14:27:01 +00001602 if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){
danielk1977e180dd92007-04-05 17:15:52 +00001603 rc = sqlite3OsTruncate(pPager->fd, pPager->pageSize*(i64)nPage);
1604 }
1605 if( rc==SQLITE_OK ){
1606 pPager->dbSize = nPage;
1607 pager_truncate_cache(pPager);
1608 }
1609 return rc;
drhcb4c40b2004-08-18 19:09:43 +00001610}
1611
1612/*
drhc80f0582007-05-01 16:59:48 +00001613** Set the sectorSize for the given pager.
1614**
1615** The sector size is the larger of the sector size reported
1616** by sqlite3OsSectorSize() and the pageSize.
1617*/
1618static void setSectorSize(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00001619 assert(pPager->fd->pMethods||pPager->tempFile);
1620 if( !pPager->tempFile ){
1621 /* Sector size doesn't matter for temporary files. Also, the file
1622 ** may not have been opened yet, in whcih case the OsSectorSize()
1623 ** call will segfault.
1624 */
1625 pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
1626 }
drhc80f0582007-05-01 16:59:48 +00001627 if( pPager->sectorSize<pPager->pageSize ){
1628 pPager->sectorSize = pPager->pageSize;
1629 }
1630}
1631
1632/*
drhed7c8552001-04-11 14:29:21 +00001633** Playback the journal and thus restore the database file to
1634** the state it was in before we started making changes.
1635**
drh34e79ce2004-02-08 06:05:46 +00001636** The journal file format is as follows:
1637**
drhae2b40c2004-06-09 19:03:54 +00001638** (1) 8 byte prefix. A copy of aJournalMagic[].
1639** (2) 4 byte big-endian integer which is the number of valid page records
drh34e79ce2004-02-08 06:05:46 +00001640** in the journal. If this value is 0xffffffff, then compute the
drhae2b40c2004-06-09 19:03:54 +00001641** number of page records from the journal size.
1642** (3) 4 byte big-endian integer which is the initial value for the
1643** sanity checksum.
1644** (4) 4 byte integer which is the number of pages to truncate the
drh34e79ce2004-02-08 06:05:46 +00001645** database to during a rollback.
drhae2b40c2004-06-09 19:03:54 +00001646** (5) 4 byte integer which is the number of bytes in the master journal
1647** name. The value may be zero (indicate that there is no master
1648** journal.)
1649** (6) N bytes of the master journal name. The name will be nul-terminated
1650** and might be shorter than the value read from (5). If the first byte
1651** of the name is \000 then there is no master journal. The master
1652** journal name is stored in UTF-8.
1653** (7) Zero or more pages instances, each as follows:
drh34e79ce2004-02-08 06:05:46 +00001654** + 4 byte page number.
drhae2b40c2004-06-09 19:03:54 +00001655** + pPager->pageSize bytes of data.
1656** + 4 byte checksum
drh34e79ce2004-02-08 06:05:46 +00001657**
drhae2b40c2004-06-09 19:03:54 +00001658** When we speak of the journal header, we mean the first 6 items above.
1659** Each entry in the journal is an instance of the 7th item.
drh34e79ce2004-02-08 06:05:46 +00001660**
1661** Call the value from the second bullet "nRec". nRec is the number of
1662** valid page entries in the journal. In most cases, you can compute the
1663** value of nRec from the size of the journal file. But if a power
1664** failure occurred while the journal was being written, it could be the
1665** case that the size of the journal file had already been increased but
1666** the extra entries had not yet made it safely to disk. In such a case,
1667** the value of nRec computed from the file size would be too large. For
1668** that reason, we always use the nRec value in the header.
1669**
1670** If the nRec value is 0xffffffff it means that nRec should be computed
1671** from the file size. This value is used when the user selects the
1672** no-sync option for the journal. A power failure could lead to corruption
1673** in this case. But for things like temporary table (which will be
1674** deleted when the power is restored) we don't care.
1675**
drhd9b02572001-04-15 00:37:09 +00001676** If the file opened as the journal file is not a well-formed
danielk1977ece80f12004-06-23 01:05:26 +00001677** journal file then all pages up to the first corrupted page are rolled
1678** back (or no pages if the journal header is corrupted). The journal file
1679** is then deleted and SQLITE_OK returned, just as if no corruption had
1680** been encountered.
1681**
1682** If an I/O or malloc() error occurs, the journal-file is not deleted
1683** and an error code is returned.
drhed7c8552001-04-11 14:29:21 +00001684*/
danielk1977e277be02007-03-23 18:12:06 +00001685static int pager_playback(Pager *pPager, int isHot){
danielk1977b4b47412007-08-17 15:53:36 +00001686 sqlite3_vfs *pVfs = pPager->pVfs;
drheb206252004-10-01 02:00:31 +00001687 i64 szJ; /* Size of the journal file in bytes */
danielk1977c3e8f5e2004-06-28 01:16:46 +00001688 u32 nRec; /* Number of Records in the journal */
drhd9b02572001-04-15 00:37:09 +00001689 int i; /* Loop counter */
1690 Pgno mxPg = 0; /* Size of the original file in pages */
drhae2b40c2004-06-09 19:03:54 +00001691 int rc; /* Result code of a subroutine */
danielk197713adf8a2004-06-03 16:08:41 +00001692 char *zMaster = 0; /* Name of master journal file if any */
drhed7c8552001-04-11 14:29:21 +00001693
drhc3a64ba2001-11-22 00:01:27 +00001694 /* Figure out how many records are in the journal. Abort early if
1695 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +00001696 */
drh8cfbf082001-09-19 13:22:39 +00001697 assert( pPager->journalOpen );
drh054889e2005-11-30 03:20:31 +00001698 rc = sqlite3OsFileSize(pPager->jfd, &szJ);
danielk1977334cdb62007-03-26 08:05:12 +00001699 if( rc!=SQLITE_OK || szJ==0 ){
drhc3a64ba2001-11-22 00:01:27 +00001700 goto end_playback;
1701 }
drh240c5792004-02-08 00:40:52 +00001702
danielk197776572402004-06-25 02:38:54 +00001703 /* Read the master journal name from the journal, if it is present.
1704 ** If a master journal file name is specified, but the file is not
1705 ** present on disk, then the journal is not hot and does not need to be
1706 ** played back.
drh240c5792004-02-08 00:40:52 +00001707 */
danielk197765839c62007-08-30 08:08:17 +00001708 zMaster = pPager->pTmpSpace;
1709 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
danielk197776572402004-06-25 02:38:54 +00001710 assert( rc!=SQLITE_DONE );
danielk1977b4b47412007-08-17 15:53:36 +00001711 if( rc!=SQLITE_OK
danielk197765839c62007-08-30 08:08:17 +00001712 || (zMaster[0] && !sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS))
danielk1977b4b47412007-08-17 15:53:36 +00001713 ){
danielk197776572402004-06-25 02:38:54 +00001714 zMaster = 0;
1715 if( rc==SQLITE_DONE ) rc = SQLITE_OK;
drhc3a64ba2001-11-22 00:01:27 +00001716 goto end_playback;
1717 }
danielk197776572402004-06-25 02:38:54 +00001718 pPager->journalOff = 0;
danielk197765839c62007-08-30 08:08:17 +00001719 zMaster = 0;
drhc3a64ba2001-11-22 00:01:27 +00001720
danielk197776572402004-06-25 02:38:54 +00001721 /* This loop terminates either when the readJournalHdr() call returns
1722 ** SQLITE_DONE or an IO error occurs. */
1723 while( 1 ){
drhae2b40c2004-06-09 19:03:54 +00001724
danielk197776572402004-06-25 02:38:54 +00001725 /* Read the next journal header from the journal file. If there are
1726 ** not enough bytes left in the journal file for a complete header, or
1727 ** it is corrupted, then a process must of failed while writing it.
1728 ** This indicates nothing more needs to be rolled back.
1729 */
1730 rc = readJournalHdr(pPager, szJ, &nRec, &mxPg);
1731 if( rc!=SQLITE_OK ){
drh968af522003-02-11 14:55:40 +00001732 if( rc==SQLITE_DONE ){
drh968af522003-02-11 14:55:40 +00001733 rc = SQLITE_OK;
1734 }
danielk197776572402004-06-25 02:38:54 +00001735 goto end_playback;
1736 }
1737
1738 /* If nRec is 0xffffffff, then this journal was created by a process
1739 ** working in no-sync mode. This means that the rest of the journal
1740 ** file consists of pages, there are no more journal headers. Compute
1741 ** the value of nRec based on this assumption.
1742 */
1743 if( nRec==0xffffffff ){
1744 assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
1745 nRec = (szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager);
1746 }
1747
danielk1977e277be02007-03-23 18:12:06 +00001748 /* If nRec is 0 and this rollback is of a transaction created by this
drh8940f4e2007-08-11 00:26:20 +00001749 ** process and if this is the final header in the journal, then it means
1750 ** that this part of the journal was being filled but has not yet been
1751 ** synced to disk. Compute the number of pages based on the remaining
1752 ** size of the file.
1753 **
1754 ** The third term of the test was added to fix ticket #2565.
danielk1977e277be02007-03-23 18:12:06 +00001755 */
drh8940f4e2007-08-11 00:26:20 +00001756 if( nRec==0 && !isHot &&
1757 pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
danielk1977e277be02007-03-23 18:12:06 +00001758 nRec = (szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager);
1759 }
1760
danielk197776572402004-06-25 02:38:54 +00001761 /* If this is the first header read from the journal, truncate the
1762 ** database file back to it's original size.
1763 */
danielk1977e180dd92007-04-05 17:15:52 +00001764 if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
drhcb4c40b2004-08-18 19:09:43 +00001765 rc = pager_truncate(pPager, mxPg);
danielk197776572402004-06-25 02:38:54 +00001766 if( rc!=SQLITE_OK ){
1767 goto end_playback;
1768 }
danielk197776572402004-06-25 02:38:54 +00001769 }
1770
danielk197776572402004-06-25 02:38:54 +00001771 /* Copy original pages out of the journal and back into the database file.
1772 */
1773 for(i=0; i<nRec; i++){
danielk197762079062007-08-15 17:08:46 +00001774 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001775 if( rc!=SQLITE_OK ){
1776 if( rc==SQLITE_DONE ){
1777 rc = SQLITE_OK;
1778 pPager->journalOff = szJ;
1779 break;
1780 }else{
1781 goto end_playback;
1782 }
1783 }
drh968af522003-02-11 14:55:40 +00001784 }
drhed7c8552001-04-11 14:29:21 +00001785 }
drh580eeaf2006-02-24 03:09:37 +00001786 /*NOTREACHED*/
1787 assert( 0 );
drh4a0681e2003-02-13 01:58:20 +00001788
1789end_playback:
danielk19778191bff2004-06-28 04:52:30 +00001790 if( rc==SQLITE_OK ){
danielk197765839c62007-08-30 08:08:17 +00001791 zMaster = pPager->pTmpSpace;
1792 rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
1793 }
1794 if( rc==SQLITE_OK ){
drh80e35f42007-03-30 14:06:34 +00001795 rc = pager_end_transaction(pPager);
danielk19778191bff2004-06-28 04:52:30 +00001796 }
danielk197765839c62007-08-30 08:08:17 +00001797 if( rc==SQLITE_OK && zMaster[0] ){
danielk1977979f38e2007-03-27 16:19:51 +00001798 /* If there was a master journal and this routine will return success,
danielk197732554c12005-01-22 03:39:39 +00001799 ** see if it is possible to delete the master journal.
danielk197713adf8a2004-06-03 16:08:41 +00001800 */
danielk197765839c62007-08-30 08:08:17 +00001801 rc = pager_delmaster(pPager, zMaster);
danielk197713adf8a2004-06-03 16:08:41 +00001802 }
danielk197776572402004-06-25 02:38:54 +00001803
1804 /* The Pager.sectorSize variable may have been updated while rolling
drh3ceeb752007-03-29 18:19:52 +00001805 ** back a journal created by a process with a different sector size
danielk197776572402004-06-25 02:38:54 +00001806 ** value. Reset it to the correct value for this process.
1807 */
drhc80f0582007-05-01 16:59:48 +00001808 setSectorSize(pPager);
drhd9b02572001-04-15 00:37:09 +00001809 return rc;
drhed7c8552001-04-11 14:29:21 +00001810}
1811
1812/*
drhac69b052004-05-12 13:30:07 +00001813** Playback the statement journal.
drhfa86c412002-02-02 15:01:15 +00001814**
1815** This is similar to playing back the transaction journal but with
1816** a few extra twists.
1817**
drh663fc632002-02-02 18:49:19 +00001818** (1) The number of pages in the database file at the start of
drhac69b052004-05-12 13:30:07 +00001819** the statement is stored in pPager->stmtSize, not in the
drh663fc632002-02-02 18:49:19 +00001820** journal file itself.
drhfa86c412002-02-02 15:01:15 +00001821**
drhac69b052004-05-12 13:30:07 +00001822** (2) In addition to playing back the statement journal, also
drhfa86c412002-02-02 15:01:15 +00001823** playback all pages of the transaction journal beginning
drhac69b052004-05-12 13:30:07 +00001824** at offset pPager->stmtJSize.
drhfa86c412002-02-02 15:01:15 +00001825*/
drh3aac2dd2004-04-26 14:10:20 +00001826static int pager_stmt_playback(Pager *pPager){
drheb206252004-10-01 02:00:31 +00001827 i64 szJ; /* Size of the full journal */
1828 i64 hdrOff;
drh968af522003-02-11 14:55:40 +00001829 int nRec; /* Number of Records */
drhfa86c412002-02-02 15:01:15 +00001830 int i; /* Loop counter */
1831 int rc;
1832
danielk197776572402004-06-25 02:38:54 +00001833 szJ = pPager->journalOff;
1834#ifndef NDEBUG
1835 {
drheb206252004-10-01 02:00:31 +00001836 i64 os_szJ;
drh054889e2005-11-30 03:20:31 +00001837 rc = sqlite3OsFileSize(pPager->jfd, &os_szJ);
danielk197776572402004-06-25 02:38:54 +00001838 if( rc!=SQLITE_OK ) return rc;
1839 assert( szJ==os_szJ );
1840 }
1841#endif
1842
danielk19774099f6e2007-03-19 11:25:20 +00001843 /* Set hdrOff to be the offset just after the end of the last journal
1844 ** page written before the first journal-header for this statement
1845 ** transaction was written, or the end of the file if no journal
danielk197776572402004-06-25 02:38:54 +00001846 ** header was written.
1847 */
1848 hdrOff = pPager->stmtHdrOff;
1849 assert( pPager->fullSync || !hdrOff );
1850 if( !hdrOff ){
1851 hdrOff = szJ;
1852 }
1853
drhfa86c412002-02-02 15:01:15 +00001854 /* Truncate the database back to its original size.
1855 */
danielk1977e180dd92007-04-05 17:15:52 +00001856 rc = pager_truncate(pPager, pPager->stmtSize);
drh1aa2d8b2007-01-03 15:34:29 +00001857 assert( pPager->state>=PAGER_SHARED );
drhfa86c412002-02-02 15:01:15 +00001858
drhac69b052004-05-12 13:30:07 +00001859 /* Figure out how many records are in the statement journal.
drhfa86c412002-02-02 15:01:15 +00001860 */
drhac69b052004-05-12 13:30:07 +00001861 assert( pPager->stmtInUse && pPager->journalOpen );
drhac69b052004-05-12 13:30:07 +00001862 nRec = pPager->stmtNRec;
drhfa86c412002-02-02 15:01:15 +00001863
drhac69b052004-05-12 13:30:07 +00001864 /* Copy original pages out of the statement journal and back into the
drhae2b40c2004-06-09 19:03:54 +00001865 ** database file. Note that the statement journal omits checksums from
1866 ** each record since power-failure recovery is not important to statement
1867 ** journals.
drhfa86c412002-02-02 15:01:15 +00001868 */
danielk197762079062007-08-15 17:08:46 +00001869 for(i=0; i<nRec; i++){
1870 i64 offset = i*(4+pPager->pageSize);
1871 rc = pager_playback_one_page(pPager, pPager->stfd, offset, 0);
drh968af522003-02-11 14:55:40 +00001872 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001873 if( rc!=SQLITE_OK ) goto end_stmt_playback;
drhfa86c412002-02-02 15:01:15 +00001874 }
1875
danielk197776572402004-06-25 02:38:54 +00001876 /* Now roll some pages back from the transaction journal. Pager.stmtJSize
1877 ** was the size of the journal file when this statement was started, so
1878 ** everything after that needs to be rolled back, either into the
1879 ** database, the memory cache, or both.
1880 **
1881 ** If it is not zero, then Pager.stmtHdrOff is the offset to the start
1882 ** of the first journal header written during this statement transaction.
drhfa86c412002-02-02 15:01:15 +00001883 */
danielk197776572402004-06-25 02:38:54 +00001884 pPager->journalOff = pPager->stmtJSize;
danielk197775edc162004-06-26 01:48:18 +00001885 pPager->cksumInit = pPager->stmtCksum;
danielk19774099f6e2007-03-19 11:25:20 +00001886 while( pPager->journalOff < hdrOff ){
danielk197762079062007-08-15 17:08:46 +00001887 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001888 assert( rc!=SQLITE_DONE );
1889 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1890 }
1891
1892 while( pPager->journalOff < szJ ){
danielk1977f0113002006-01-24 12:09:17 +00001893 u32 nJRec; /* Number of Journal Records */
danielk197776572402004-06-25 02:38:54 +00001894 u32 dummy;
danielk1977f0113002006-01-24 12:09:17 +00001895 rc = readJournalHdr(pPager, szJ, &nJRec, &dummy);
drh968af522003-02-11 14:55:40 +00001896 if( rc!=SQLITE_OK ){
1897 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001898 goto end_stmt_playback;
drh968af522003-02-11 14:55:40 +00001899 }
danielk1977f0113002006-01-24 12:09:17 +00001900 if( nJRec==0 ){
1901 nJRec = (szJ - pPager->journalOff) / (pPager->pageSize+8);
danielk197775edc162004-06-26 01:48:18 +00001902 }
danielk1977f0113002006-01-24 12:09:17 +00001903 for(i=nJRec-1; i>=0 && pPager->journalOff < szJ; i--){
danielk197762079062007-08-15 17:08:46 +00001904 rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
danielk197776572402004-06-25 02:38:54 +00001905 assert( rc!=SQLITE_DONE );
1906 if( rc!=SQLITE_OK ) goto end_stmt_playback;
1907 }
drhfa86c412002-02-02 15:01:15 +00001908 }
danielk197776572402004-06-25 02:38:54 +00001909
1910 pPager->journalOff = szJ;
drhfa86c412002-02-02 15:01:15 +00001911
drh3aac2dd2004-04-26 14:10:20 +00001912end_stmt_playback:
danielk19778a7aea32006-01-23 15:25:48 +00001913 if( rc==SQLITE_OK) {
danielk197775edc162004-06-26 01:48:18 +00001914 pPager->journalOff = szJ;
1915 /* pager_reload_cache(pPager); */
drhfa86c412002-02-02 15:01:15 +00001916 }
1917 return rc;
1918}
1919
1920/*
drhf57b14a2001-09-14 18:54:08 +00001921** Change the maximum number of in-memory pages that are allowed.
1922*/
danielk19773b8a05f2007-03-19 17:44:26 +00001923void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
drhf57b14a2001-09-14 18:54:08 +00001924 if( mxPage>10 ){
1925 pPager->mxPage = mxPage;
danielk1977ef317ab2004-06-23 10:43:10 +00001926 }else{
1927 pPager->mxPage = 10;
drhf57b14a2001-09-14 18:54:08 +00001928 }
1929}
1930
1931/*
drh973b6e32003-02-12 14:09:42 +00001932** Adjust the robustness of the database to damage due to OS crashes
1933** or power failures by changing the number of syncs()s when writing
1934** the rollback journal. There are three levels:
1935**
drh054889e2005-11-30 03:20:31 +00001936** OFF sqlite3OsSync() is never called. This is the default
drh973b6e32003-02-12 14:09:42 +00001937** for temporary and transient files.
1938**
1939** NORMAL The journal is synced once before writes begin on the
1940** database. This is normally adequate protection, but
1941** it is theoretically possible, though very unlikely,
1942** that an inopertune power failure could leave the journal
1943** in a state which would cause damage to the database
1944** when it is rolled back.
1945**
1946** FULL The journal is synced twice before writes begin on the
drh34e79ce2004-02-08 06:05:46 +00001947** database (with some additional information - the nRec field
1948** of the journal header - being written in between the two
1949** syncs). If we assume that writing a
drh973b6e32003-02-12 14:09:42 +00001950** single disk sector is atomic, then this mode provides
1951** assurance that the journal will not be corrupted to the
1952** point of causing damage to the database during rollback.
1953**
1954** Numeric values associated with these states are OFF==1, NORMAL=2,
1955** and FULL=3.
1956*/
danielk197793758c82005-01-21 08:13:14 +00001957#ifndef SQLITE_OMIT_PAGER_PRAGMAS
danielk19773b8a05f2007-03-19 17:44:26 +00001958void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int full_fsync){
drh973b6e32003-02-12 14:09:42 +00001959 pPager->noSync = level==1 || pPager->tempFile;
1960 pPager->fullSync = level==3 && !pPager->tempFile;
danielk1977f036aef2007-08-20 05:36:51 +00001961 pPager->sync_flags = (full_fsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL);
danielk19771d850a72004-05-31 08:26:49 +00001962 if( pPager->noSync ) pPager->needSync = 0;
drh973b6e32003-02-12 14:09:42 +00001963}
danielk197793758c82005-01-21 08:13:14 +00001964#endif
drh973b6e32003-02-12 14:09:42 +00001965
1966/*
drhaf6df112005-06-07 02:12:30 +00001967** The following global variable is incremented whenever the library
1968** attempts to open a temporary file. This information is used for
1969** testing and analysis only.
1970*/
drh0f7eb612006-08-08 13:51:43 +00001971#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00001972int sqlite3_opentemp_count = 0;
drh0f7eb612006-08-08 13:51:43 +00001973#endif
drhaf6df112005-06-07 02:12:30 +00001974
1975/*
drh3f56e6e2007-03-15 01:16:47 +00001976** Open a temporary file.
1977**
1978** Write the file descriptor into *fd. Return SQLITE_OK on success or some
danielk1977fee2d252007-08-18 10:59:19 +00001979** other error code if we fail. The OS will automatically delete the temporary
1980** file when it is closed.
drhfa86c412002-02-02 15:01:15 +00001981*/
danielk1977b4b47412007-08-17 15:53:36 +00001982static int sqlite3PagerOpentemp(
drh33f4e022007-09-03 15:19:34 +00001983 sqlite3_vfs *pVfs, /* The virtual file system layer */
1984 sqlite3_file *pFile, /* Write the file descriptor here */
1985 char *zFilename, /* Name of the file. Might be NULL */
1986 int vfsFlags /* Flags passed through to the VFS */
danielk1977b4b47412007-08-17 15:53:36 +00001987){
drhfa86c412002-02-02 15:01:15 +00001988 int rc;
drh334b2992007-09-06 23:28:23 +00001989 assert( zFilename!=0 );
drh3f56e6e2007-03-15 01:16:47 +00001990
drh0f7eb612006-08-08 13:51:43 +00001991#ifdef SQLITE_TEST
drhaf6df112005-06-07 02:12:30 +00001992 sqlite3_opentemp_count++; /* Used for testing and analysis only */
drh0f7eb612006-08-08 13:51:43 +00001993#endif
danielk1977b4b47412007-08-17 15:53:36 +00001994
drh33f4e022007-09-03 15:19:34 +00001995 vfsFlags |= SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
1996 SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
1997 rc = sqlite3OsOpen(pVfs, zFilename, pFile, vfsFlags, 0);
drhae28c012007-08-24 16:29:23 +00001998 assert( rc!=SQLITE_OK || pFile->pMethods );
drhfa86c412002-02-02 15:01:15 +00001999 return rc;
2000}
2001
2002/*
drhed7c8552001-04-11 14:29:21 +00002003** Create a new page cache and put a pointer to the page cache in *ppPager.
drh5e00f6c2001-09-13 13:46:56 +00002004** The file to be cached need not exist. The file is not locked until
danielk19773b8a05f2007-03-19 17:44:26 +00002005** the first call to sqlite3PagerGet() and is only held open until the
2006** last page is released using sqlite3PagerUnref().
drh382c0242001-10-06 16:33:02 +00002007**
drh6446c4d2001-12-15 14:22:18 +00002008** If zFilename is NULL then a randomly-named temporary file is created
drh1cc8c442007-08-24 16:08:29 +00002009** and used as the file to be cached. The file will be deleted
drh6446c4d2001-12-15 14:22:18 +00002010** automatically when it is closed.
drh90f5ecb2004-07-22 01:19:35 +00002011**
2012** If zFilename is ":memory:" then all information is held in cache.
2013** It is never written to disk. This can be used to implement an
2014** in-memory database.
drhed7c8552001-04-11 14:29:21 +00002015*/
danielk19773b8a05f2007-03-19 17:44:26 +00002016int sqlite3PagerOpen(
drh86f8c192007-08-22 00:39:19 +00002017 sqlite3_vfs *pVfs, /* The virtual file system to use */
drh7e3b0a02001-04-28 16:52:40 +00002018 Pager **ppPager, /* Return the Pager structure here */
2019 const char *zFilename, /* Name of the database file to open */
drhda47d772002-12-02 04:25:19 +00002020 int nExtra, /* Extra bytes append to each in-memory page */
drh33f4e022007-09-03 15:19:34 +00002021 int flags, /* flags controlling this file */
2022 int vfsFlags /* flags passed through to sqlite3_vfs.xOpen() */
drh7e3b0a02001-04-28 16:52:40 +00002023){
danielk1977b4b47412007-08-17 15:53:36 +00002024 u8 *pPtr;
danielk1977aef0bf62005-12-30 16:28:01 +00002025 Pager *pPager = 0;
danielk1977cfe9a692004-06-16 12:00:29 +00002026 int rc = SQLITE_OK;
2027 int i;
danielk19778def5ea2004-06-16 10:39:52 +00002028 int tempFile = 0;
drhac69b052004-05-12 13:30:07 +00002029 int memDb = 0;
drh5e00f6c2001-09-13 13:46:56 +00002030 int readOnly = 0;
drh7bec5052005-02-06 02:45:41 +00002031 int useJournal = (flags & PAGER_OMIT_JOURNAL)==0;
2032 int noReadlock = (flags & PAGER_NO_READLOCK)!=0;
danielk1977c7b60172007-08-22 11:22:03 +00002033 int journalFileSize = sqlite3JournalSize(pVfs);
drh1cc8c442007-08-24 16:08:29 +00002034 int nDefaultPage = SQLITE_DEFAULT_PAGE_SIZE;
2035 char *zPathname;
2036 int nPathname;
danielk1977b4b47412007-08-17 15:53:36 +00002037
drh86f8c192007-08-22 00:39:19 +00002038 /* The default return is a NULL pointer */
drhd9b02572001-04-15 00:37:09 +00002039 *ppPager = 0;
danielk1977aef0bf62005-12-30 16:28:01 +00002040
drh1cc8c442007-08-24 16:08:29 +00002041 /* Compute the full pathname */
danielk1977adfb9b02007-09-17 07:02:56 +00002042 nPathname = pVfs->mxPathname+1;
2043 zPathname = sqlite3_malloc(nPathname);
drh1cc8c442007-08-24 16:08:29 +00002044 if( zPathname==0 ){
2045 return SQLITE_NOMEM;
2046 }
2047 if( zFilename && zFilename[0] ){
2048#ifndef SQLITE_OMIT_MEMORYDB
2049 if( strcmp(zFilename,":memory:")==0 ){
2050 memDb = 1;
2051 zPathname[0] = 0;
2052 }else
2053#endif
2054 {
danielk1977adfb9b02007-09-17 07:02:56 +00002055 rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
drh1cc8c442007-08-24 16:08:29 +00002056 }
2057 }else{
danielk1977adfb9b02007-09-17 07:02:56 +00002058 rc = sqlite3OsGetTempname(pVfs, nPathname, zPathname);
drhae28c012007-08-24 16:29:23 +00002059 }
2060 if( rc!=SQLITE_OK ){
2061 sqlite3_free(zPathname);
2062 return rc;
drh1cc8c442007-08-24 16:08:29 +00002063 }
2064 nPathname = strlen(zPathname);
2065
danielk1977b4b47412007-08-17 15:53:36 +00002066 /* Allocate memory for the pager structure */
2067 pPager = sqlite3MallocZero(
2068 sizeof(*pPager) + /* Pager structure */
danielk1977c7b60172007-08-22 11:22:03 +00002069 journalFileSize + /* The journal file structure */
2070 pVfs->szOsFile * 2 + /* The db and stmt journal files */
drh334b2992007-09-06 23:28:23 +00002071 4*nPathname + 40 /* zFilename, zDirectory, zJournal, zStmtJrnl */
danielk1977b4b47412007-08-17 15:53:36 +00002072 );
2073 if( !pPager ){
drh1cc8c442007-08-24 16:08:29 +00002074 sqlite3_free(zPathname);
danielk1977b4b47412007-08-17 15:53:36 +00002075 return SQLITE_NOMEM;
2076 }
2077 pPtr = (u8 *)&pPager[1];
drh33f4e022007-09-03 15:19:34 +00002078 pPager->vfsFlags = vfsFlags;
danielk1977b4b47412007-08-17 15:53:36 +00002079 pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0];
danielk1977c7b60172007-08-22 11:22:03 +00002080 pPager->stfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*1];
2081 pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*2];
2082 pPager->zFilename = (char*)&pPtr[pVfs->szOsFile*2+journalFileSize];
drh1cc8c442007-08-24 16:08:29 +00002083 pPager->zDirectory = &pPager->zFilename[nPathname+1];
2084 pPager->zJournal = &pPager->zDirectory[nPathname+1];
drh334b2992007-09-06 23:28:23 +00002085 pPager->zStmtJrnl = &pPager->zJournal[nPathname+10];
danielk1977b4b47412007-08-17 15:53:36 +00002086 pPager->pVfs = pVfs;
drh1cc8c442007-08-24 16:08:29 +00002087 memcpy(pPager->zFilename, zPathname, nPathname+1);
2088 sqlite3_free(zPathname);
danielk1977b4b47412007-08-17 15:53:36 +00002089
drh153c62c2007-08-24 03:51:33 +00002090 /* Open the pager file.
danielk1977aef0bf62005-12-30 16:28:01 +00002091 */
drhae28c012007-08-24 16:29:23 +00002092 if( zFilename && zFilename[0] && !memDb ){
drh1cc8c442007-08-24 16:08:29 +00002093 if( nPathname>(pVfs->mxPathname - sizeof("-journal")) ){
2094 rc = SQLITE_CANTOPEN;
2095 }else{
drh1cc8c442007-08-24 16:08:29 +00002096 int fout = 0;
drh33f4e022007-09-03 15:19:34 +00002097 rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd,
2098 pPager->vfsFlags, &fout);
drh1cc8c442007-08-24 16:08:29 +00002099 readOnly = (fout&SQLITE_OPEN_READONLY);
2100
2101 /* If the file was successfully opened for read/write access,
2102 ** choose a default page size in case we have to create the
2103 ** database file. The default page size is the maximum of:
2104 **
2105 ** + SQLITE_DEFAULT_PAGE_SIZE,
2106 ** + The value returned by sqlite3OsSectorSize()
2107 ** + The largest page size that can be written atomically.
2108 */
2109 if( rc==SQLITE_OK && !readOnly ){
2110 int iSectorSize = sqlite3OsSectorSize(pPager->fd);
2111 if( nDefaultPage<iSectorSize ){
2112 nDefaultPage = iSectorSize;
2113 }
danielk19779663b8f2007-08-24 11:52:28 +00002114#ifdef SQLITE_ENABLE_ATOMIC_WRITE
drh1cc8c442007-08-24 16:08:29 +00002115 {
2116 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
2117 int ii;
2118 assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
2119 assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
2120 assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
2121 for(ii=nDefaultPage; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
2122 if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ) nDefaultPage = ii;
danielk19779663b8f2007-08-24 11:52:28 +00002123 }
danielk1977b4b47412007-08-17 15:53:36 +00002124 }
drh1cc8c442007-08-24 16:08:29 +00002125#endif
2126 if( nDefaultPage>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
2127 nDefaultPage = SQLITE_MAX_DEFAULT_PAGE_SIZE;
2128 }
danielk19778def5ea2004-06-16 10:39:52 +00002129 }
drhac69b052004-05-12 13:30:07 +00002130 }
drh1cc8c442007-08-24 16:08:29 +00002131 }else if( !memDb ){
danielk19777a2b1ee2007-08-21 14:27:01 +00002132 /* If a temporary file is requested, it is not opened immediately.
2133 ** In this case we accept the default page size and delay actually
2134 ** opening the file until the first call to OsWrite().
2135 */
danielk19777a2b1ee2007-08-21 14:27:01 +00002136 tempFile = 1;
2137 pPager->state = PAGER_EXCLUSIVE;
drh5e00f6c2001-09-13 13:46:56 +00002138 }
danielk1977aef0bf62005-12-30 16:28:01 +00002139
danielk1977b4b47412007-08-17 15:53:36 +00002140 if( pPager && rc==SQLITE_OK ){
danielk19779663b8f2007-08-24 11:52:28 +00002141 pPager->pTmpSpace = (char *)sqlite3_malloc(nDefaultPage);
drh3e7a6092002-12-07 21:45:14 +00002142 }
danielk1977aef0bf62005-12-30 16:28:01 +00002143
drh153c62c2007-08-24 03:51:33 +00002144 /* If an error occured in either of the blocks above.
2145 ** Free the Pager structure and close the file.
2146 ** Since the pager is not allocated there is no need to set
danielk1977aef0bf62005-12-30 16:28:01 +00002147 ** any Pager.errMask variables.
2148 */
danielk1977b4b47412007-08-17 15:53:36 +00002149 if( !pPager || !pPager->pTmpSpace ){
2150 sqlite3OsClose(pPager->fd);
drh17435752007-08-16 04:30:38 +00002151 sqlite3_free(pPager);
danielk1977aef0bf62005-12-30 16:28:01 +00002152 return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc);
drhed7c8552001-04-11 14:29:21 +00002153 }
danielk1977aef0bf62005-12-30 16:28:01 +00002154
drh153c62c2007-08-24 03:51:33 +00002155 PAGERTRACE3("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename);
2156 IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
danielk1977aef0bf62005-12-30 16:28:01 +00002157
danielk1977b4b47412007-08-17 15:53:36 +00002158 /* Fill in Pager.zDirectory[] */
drh1cc8c442007-08-24 16:08:29 +00002159 memcpy(pPager->zDirectory, pPager->zFilename, nPathname+1);
danielk1977b4b47412007-08-17 15:53:36 +00002160 for(i=strlen(pPager->zDirectory); i>0 && pPager->zDirectory[i-1]!='/'; i--){}
drha76c82e2003-07-27 18:59:42 +00002161 if( i>0 ) pPager->zDirectory[i-1] = 0;
danielk1977b4b47412007-08-17 15:53:36 +00002162
drh334b2992007-09-06 23:28:23 +00002163 /* Fill in Pager.zJournal[] and Pager.zStmtJrnl[] */
drh1cc8c442007-08-24 16:08:29 +00002164 memcpy(pPager->zJournal, pPager->zFilename, nPathname);
2165 memcpy(&pPager->zJournal[nPathname], "-journal", 9);
drh334b2992007-09-06 23:28:23 +00002166 memcpy(pPager->zStmtJrnl, pPager->zFilename, nPathname);
2167 memcpy(&pPager->zStmtJrnl[nPathname], "-stmtjrnl", 10);
danielk1977b4b47412007-08-17 15:53:36 +00002168
drh3b59a5c2006-01-15 20:28:28 +00002169 /* pPager->journalOpen = 0; */
drhac69b052004-05-12 13:30:07 +00002170 pPager->useJournal = useJournal && !memDb;
drh7bec5052005-02-06 02:45:41 +00002171 pPager->noReadlock = noReadlock && readOnly;
drh3b59a5c2006-01-15 20:28:28 +00002172 /* pPager->stmtOpen = 0; */
2173 /* pPager->stmtInUse = 0; */
2174 /* pPager->nRef = 0; */
drhac69b052004-05-12 13:30:07 +00002175 pPager->dbSize = memDb-1;
danielk19779663b8f2007-08-24 11:52:28 +00002176 pPager->pageSize = nDefaultPage;
drh3b59a5c2006-01-15 20:28:28 +00002177 /* pPager->stmtSize = 0; */
2178 /* pPager->stmtJSize = 0; */
2179 /* pPager->nPage = 0; */
drh90f5ecb2004-07-22 01:19:35 +00002180 pPager->mxPage = 100;
drhf8e632b2007-05-08 14:51:36 +00002181 pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
drh3b59a5c2006-01-15 20:28:28 +00002182 /* pPager->state = PAGER_UNLOCK; */
drh1cc8c442007-08-24 16:08:29 +00002183 assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
drh3b59a5c2006-01-15 20:28:28 +00002184 /* pPager->errMask = 0; */
drh5e00f6c2001-09-13 13:46:56 +00002185 pPager->tempFile = tempFile;
drh369339d2007-03-30 16:01:55 +00002186 assert( tempFile==PAGER_LOCKINGMODE_NORMAL
2187 || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
2188 assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
2189 pPager->exclusiveMode = tempFile;
drhac69b052004-05-12 13:30:07 +00002190 pPager->memDb = memDb;
drh5e00f6c2001-09-13 13:46:56 +00002191 pPager->readOnly = readOnly;
drh3b59a5c2006-01-15 20:28:28 +00002192 /* pPager->needSync = 0; */
drhda47d772002-12-02 04:25:19 +00002193 pPager->noSync = pPager->tempFile || !useJournal;
danielk197775edc162004-06-26 01:48:18 +00002194 pPager->fullSync = (pPager->noSync?0:1);
danielk1977f036aef2007-08-20 05:36:51 +00002195 pPager->sync_flags = SQLITE_SYNC_NORMAL;
drh3b59a5c2006-01-15 20:28:28 +00002196 /* pPager->pFirst = 0; */
2197 /* pPager->pFirstSynced = 0; */
2198 /* pPager->pLast = 0; */
drh887dc4c2004-10-22 16:22:57 +00002199 pPager->nExtra = FORCE_ALIGNMENT(nExtra);
danielk19777a2b1ee2007-08-21 14:27:01 +00002200 assert(pPager->fd->pMethods||memDb||tempFile);
danielk1977b4721172007-03-19 05:54:48 +00002201 if( !memDb ){
drhc80f0582007-05-01 16:59:48 +00002202 setSectorSize(pPager);
danielk1977b4721172007-03-19 05:54:48 +00002203 }
drh3b59a5c2006-01-15 20:28:28 +00002204 /* pPager->pBusyHandler = 0; */
2205 /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
drhed7c8552001-04-11 14:29:21 +00002206 *ppPager = pPager;
drh6f7adc82006-01-11 21:41:20 +00002207#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +00002208 pPager->iInUseMM = 0;
2209 pPager->iInUseDB = 0;
2210 if( !memDb ){
2211 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
2212 sqlite3_mutex_enter(mutex);
2213 pPager->pNext = sqlite3PagerList;
2214 if( sqlite3PagerList ){
2215 assert( sqlite3PagerList->pPrev==0 );
2216 sqlite3PagerList->pPrev = pPager;
2217 }
2218 pPager->pPrev = 0;
2219 sqlite3PagerList = pPager;
2220 sqlite3_mutex_leave(mutex);
2221 }
danielk197752622822006-01-09 09:59:49 +00002222#endif
drhed7c8552001-04-11 14:29:21 +00002223 return SQLITE_OK;
2224}
2225
2226/*
drh90f5ecb2004-07-22 01:19:35 +00002227** Set the busy handler function.
2228*/
danielk19773b8a05f2007-03-19 17:44:26 +00002229void sqlite3PagerSetBusyhandler(Pager *pPager, BusyHandler *pBusyHandler){
drh90f5ecb2004-07-22 01:19:35 +00002230 pPager->pBusyHandler = pBusyHandler;
2231}
2232
2233/*
drh72f82862001-05-24 21:06:34 +00002234** Set the destructor for this pager. If not NULL, the destructor is called
drh5e00f6c2001-09-13 13:46:56 +00002235** when the reference count on each page reaches zero. The destructor can
2236** be used to clean up information in the extra segment appended to each page.
drh72f82862001-05-24 21:06:34 +00002237**
danielk19773b8a05f2007-03-19 17:44:26 +00002238** The destructor is not called as a result sqlite3PagerClose().
2239** Destructors are only called by sqlite3PagerUnref().
drh72f82862001-05-24 21:06:34 +00002240*/
danielk19773b8a05f2007-03-19 17:44:26 +00002241void sqlite3PagerSetDestructor(Pager *pPager, void (*xDesc)(DbPage*,int)){
drh72f82862001-05-24 21:06:34 +00002242 pPager->xDestructor = xDesc;
2243}
2244
2245/*
drha6abd042004-06-09 17:37:22 +00002246** Set the reinitializer for this pager. If not NULL, the reinitializer
2247** is called when the content of a page in cache is restored to its original
2248** value as a result of a rollback. The callback gives higher-level code
2249** an opportunity to restore the EXTRA section to agree with the restored
2250** page data.
2251*/
danielk19773b8a05f2007-03-19 17:44:26 +00002252void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*,int)){
drha6abd042004-06-09 17:37:22 +00002253 pPager->xReiniter = xReinit;
2254}
2255
2256/*
danielk1977a1644fd2007-08-29 12:31:25 +00002257** Set the page size to *pPageSize. If the suggest new page size is
2258** inappropriate, then an alternative page size is set to that
2259** value before returning.
drh90f5ecb2004-07-22 01:19:35 +00002260*/
danielk1977a1644fd2007-08-29 12:31:25 +00002261int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){
2262 int rc = SQLITE_OK;
2263 u16 pageSize = *pPageSize;
danielk19779663b8f2007-08-24 11:52:28 +00002264 assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
danielk1977a1644fd2007-08-29 12:31:25 +00002265 if( pageSize && pageSize!=pPager->pageSize
2266 && !pPager->memDb && pPager->nRef==0
2267 ){
2268 char *pNew = (char *)sqlite3_malloc(pageSize);
2269 if( !pNew ){
2270 rc = SQLITE_NOMEM;
2271 }else{
2272 pagerEnter(pPager);
2273 pager_reset(pPager);
2274 pPager->pageSize = pageSize;
2275 setSectorSize(pPager);
2276 sqlite3_free(pPager->pTmpSpace);
2277 pPager->pTmpSpace = pNew;
2278 pagerLeave(pPager);
2279 }
drh1c7880e2005-05-20 20:01:55 +00002280 }
danielk1977a1644fd2007-08-29 12:31:25 +00002281 *pPageSize = pPager->pageSize;
2282 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002283}
2284
2285/*
drhf8e632b2007-05-08 14:51:36 +00002286** Attempt to set the maximum database page count if mxPage is positive.
2287** Make no changes if mxPage is zero or negative. And never reduce the
2288** maximum page count below the current size of the database.
2289**
2290** Regardless of mxPage, return the current maximum page count.
2291*/
2292int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
2293 if( mxPage>0 ){
2294 pPager->mxPgno = mxPage;
2295 }
2296 sqlite3PagerPagecount(pPager);
2297 return pPager->mxPgno;
2298}
2299
2300/*
drhc9ac5ca2005-11-04 22:03:30 +00002301** The following set of routines are used to disable the simulated
2302** I/O error mechanism. These routines are used to avoid simulated
2303** errors in places where we do not care about errors.
2304**
2305** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
2306** and generate no code.
2307*/
2308#ifdef SQLITE_TEST
2309extern int sqlite3_io_error_pending;
2310extern int sqlite3_io_error_hit;
2311static int saved_cnt;
drhc9ac5ca2005-11-04 22:03:30 +00002312void disable_simulated_io_errors(void){
2313 saved_cnt = sqlite3_io_error_pending;
2314 sqlite3_io_error_pending = -1;
2315}
2316void enable_simulated_io_errors(void){
2317 sqlite3_io_error_pending = saved_cnt;
2318}
2319#else
drh152410f2005-11-05 15:11:22 +00002320# define disable_simulated_io_errors()
2321# define enable_simulated_io_errors()
drhc9ac5ca2005-11-04 22:03:30 +00002322#endif
2323
2324/*
drh90f5ecb2004-07-22 01:19:35 +00002325** Read the first N bytes from the beginning of the file into memory
danielk1977aef0bf62005-12-30 16:28:01 +00002326** that pDest points to.
2327**
2328** No error checking is done. The rational for this is that this function
2329** may be called even if the file does not exist or contain a header. In
2330** these cases sqlite3OsRead() will return an error, to which the correct
2331** response is to zero the memory at pDest and continue. A real IO error
2332** will presumably recur and be picked up later (Todo: Think about this).
drh90f5ecb2004-07-22 01:19:35 +00002333*/
danielk19773b8a05f2007-03-19 17:44:26 +00002334int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
drh551b7732006-11-06 21:20:25 +00002335 int rc = SQLITE_OK;
drh90f5ecb2004-07-22 01:19:35 +00002336 memset(pDest, 0, N);
danielk19777a2b1ee2007-08-21 14:27:01 +00002337 assert(MEMDB||pPager->fd->pMethods||pPager->tempFile);
2338 if( pPager->fd->pMethods ){
drhb0603412007-02-28 04:47:26 +00002339 IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
danielk197762079062007-08-15 17:08:46 +00002340 rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
drh551b7732006-11-06 21:20:25 +00002341 if( rc==SQLITE_IOERR_SHORT_READ ){
2342 rc = SQLITE_OK;
2343 }
drh90f5ecb2004-07-22 01:19:35 +00002344 }
drh551b7732006-11-06 21:20:25 +00002345 return rc;
drh90f5ecb2004-07-22 01:19:35 +00002346}
2347
2348/*
drh5e00f6c2001-09-13 13:46:56 +00002349** Return the total number of pages in the disk file associated with
danielk197715f411d2005-09-16 10:18:45 +00002350** pPager.
2351**
2352** If the PENDING_BYTE lies on the page directly after the end of the
2353** file, then consider this page part of the file too. For example, if
2354** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the
2355** file is 4096 bytes, 5 is returned instead of 4.
drhed7c8552001-04-11 14:29:21 +00002356*/
danielk19773b8a05f2007-03-19 17:44:26 +00002357int sqlite3PagerPagecount(Pager *pPager){
danielk19777a2b1ee2007-08-21 14:27:01 +00002358 i64 n = 0;
drhe49f9822006-09-15 12:29:16 +00002359 int rc;
drhd9b02572001-04-15 00:37:09 +00002360 assert( pPager!=0 );
drha7aea3d2007-03-15 12:51:16 +00002361 if( pPager->errCode ){
2362 return 0;
2363 }
drhed7c8552001-04-11 14:29:21 +00002364 if( pPager->dbSize>=0 ){
danielk197715f411d2005-09-16 10:18:45 +00002365 n = pPager->dbSize;
2366 } else {
danielk19777a2b1ee2007-08-21 14:27:01 +00002367 assert(pPager->fd->pMethods||pPager->tempFile);
2368 if( (pPager->fd->pMethods)
2369 && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){
drhe49f9822006-09-15 12:29:16 +00002370 pager_error(pPager, rc);
danielk197715f411d2005-09-16 10:18:45 +00002371 return 0;
2372 }
2373 if( n>0 && n<pPager->pageSize ){
2374 n = 1;
2375 }else{
2376 n /= pPager->pageSize;
2377 }
2378 if( pPager->state!=PAGER_UNLOCK ){
2379 pPager->dbSize = n;
2380 }
drhed7c8552001-04-11 14:29:21 +00002381 }
danielk197715f411d2005-09-16 10:18:45 +00002382 if( n==(PENDING_BYTE/pPager->pageSize) ){
drh1f595712004-06-15 01:40:29 +00002383 n++;
2384 }
drhf8e632b2007-05-08 14:51:36 +00002385 if( n>pPager->mxPgno ){
2386 pPager->mxPgno = n;
2387 }
drhed7c8552001-04-11 14:29:21 +00002388 return n;
2389}
2390
drhf07e7d52006-04-07 13:54:46 +00002391
2392#ifndef SQLITE_OMIT_MEMORYDB
2393/*
2394** Clear a PgHistory block
2395*/
2396static void clearHistory(PgHistory *pHist){
drh17435752007-08-16 04:30:38 +00002397 sqlite3_free(pHist->pOrig);
2398 sqlite3_free(pHist->pStmt);
drhf07e7d52006-04-07 13:54:46 +00002399 pHist->pOrig = 0;
2400 pHist->pStmt = 0;
2401}
2402#else
2403#define clearHistory(x)
2404#endif
2405
drhed7c8552001-04-11 14:29:21 +00002406/*
drhf7c57532003-04-25 13:22:51 +00002407** Forward declaration
2408*/
danielk197776572402004-06-25 02:38:54 +00002409static int syncJournal(Pager*);
drhac69b052004-05-12 13:30:07 +00002410
2411/*
danielk1977f5fdda82004-11-03 08:44:05 +00002412** Unlink pPg from it's hash chain. Also set the page number to 0 to indicate
2413** that the page is not part of any hash chain. This is required because the
danielk19773b8a05f2007-03-19 17:44:26 +00002414** sqlite3PagerMovepage() routine can leave a page in the
danielk1977f5fdda82004-11-03 08:44:05 +00002415** pNextFree/pPrevFree list that is not a part of any hash-chain.
2416*/
2417static void unlinkHashChain(Pager *pPager, PgHdr *pPg){
2418 if( pPg->pgno==0 ){
drh3765df42006-06-28 18:18:09 +00002419 assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
danielk1977f5fdda82004-11-03 08:44:05 +00002420 return;
2421 }
2422 if( pPg->pNextHash ){
2423 pPg->pNextHash->pPrevHash = pPg->pPrevHash;
2424 }
2425 if( pPg->pPrevHash ){
drh8ca0c722006-05-07 17:49:38 +00002426 assert( pPager->aHash[pPg->pgno & (pPager->nHash-1)]!=pPg );
danielk1977f5fdda82004-11-03 08:44:05 +00002427 pPg->pPrevHash->pNextHash = pPg->pNextHash;
2428 }else{
drh8ca0c722006-05-07 17:49:38 +00002429 int h = pPg->pgno & (pPager->nHash-1);
danielk1977f5fdda82004-11-03 08:44:05 +00002430 pPager->aHash[h] = pPg->pNextHash;
2431 }
drh5229ae42006-03-23 23:29:04 +00002432 if( MEMDB ){
drh5229ae42006-03-23 23:29:04 +00002433 clearHistory(PGHDR_TO_HIST(pPg, pPager));
2434 }
danielk1977f5fdda82004-11-03 08:44:05 +00002435 pPg->pgno = 0;
2436 pPg->pNextHash = pPg->pPrevHash = 0;
2437}
2438
2439/*
drhac69b052004-05-12 13:30:07 +00002440** Unlink a page from the free list (the list of all pages where nRef==0)
2441** and from its hash collision chain.
2442*/
2443static void unlinkPage(PgHdr *pPg){
2444 Pager *pPager = pPg->pPager;
2445
danielk19779f61c2f2007-08-27 17:27:49 +00002446 /* Unlink from free page list */
2447 lruListRemove(pPg);
drhac69b052004-05-12 13:30:07 +00002448
2449 /* Unlink from the pgno hash table */
danielk1977f5fdda82004-11-03 08:44:05 +00002450 unlinkHashChain(pPager, pPg);
drhac69b052004-05-12 13:30:07 +00002451}
2452
2453/*
danielk1977e180dd92007-04-05 17:15:52 +00002454** This routine is used to truncate the cache when a database
2455** is truncated. Drop from the cache all pages whose pgno is
2456** larger than pPager->dbSize and is unreferenced.
2457**
drhac69b052004-05-12 13:30:07 +00002458** Referenced pages larger than pPager->dbSize are zeroed.
danielk1977e180dd92007-04-05 17:15:52 +00002459**
2460** Actually, at the point this routine is called, it would be
2461** an error to have a referenced page. But rather than delete
2462** that page and guarantee a subsequent segfault, it seems better
2463** to zero it and hope that we error out sanely.
drhac69b052004-05-12 13:30:07 +00002464*/
danielk1977e180dd92007-04-05 17:15:52 +00002465static void pager_truncate_cache(Pager *pPager){
drhac69b052004-05-12 13:30:07 +00002466 PgHdr *pPg;
2467 PgHdr **ppPg;
2468 int dbSize = pPager->dbSize;
2469
2470 ppPg = &pPager->pAll;
2471 while( (pPg = *ppPg)!=0 ){
2472 if( pPg->pgno<=dbSize ){
2473 ppPg = &pPg->pNextAll;
2474 }else if( pPg->nRef>0 ){
2475 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
2476 ppPg = &pPg->pNextAll;
2477 }else{
2478 *ppPg = pPg->pNextAll;
drh538f5702007-04-13 02:14:30 +00002479 IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
2480 PAGER_INCR(sqlite3_pager_pgfree_count);
drhac69b052004-05-12 13:30:07 +00002481 unlinkPage(pPg);
drh605ad8f2006-05-03 23:34:05 +00002482 makeClean(pPg);
drhbf4bca52007-09-06 22:19:14 +00002483 sqlite3_free(pPg->pData);
drh17435752007-08-16 04:30:38 +00002484 sqlite3_free(pPg);
drhac69b052004-05-12 13:30:07 +00002485 pPager->nPage--;
2486 }
2487 }
2488}
2489
drhf7c57532003-04-25 13:22:51 +00002490/*
danielk197717221812005-02-15 03:38:05 +00002491** Try to obtain a lock on a file. Invoke the busy callback if the lock
drha4afb652005-07-09 02:16:02 +00002492** is currently not available. Repeat until the busy callback returns
danielk197717221812005-02-15 03:38:05 +00002493** false or until the lock succeeds.
2494**
2495** Return SQLITE_OK on success and an error code if we cannot obtain
2496** the lock.
2497*/
2498static int pager_wait_on_lock(Pager *pPager, int locktype){
2499 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002500
2501 /* The OS lock values must be the same as the Pager lock values */
danielk197717221812005-02-15 03:38:05 +00002502 assert( PAGER_SHARED==SHARED_LOCK );
2503 assert( PAGER_RESERVED==RESERVED_LOCK );
2504 assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
drh1aa2d8b2007-01-03 15:34:29 +00002505
2506 /* If the file is currently unlocked then the size must be unknown */
2507 assert( pPager->state>=PAGER_SHARED || pPager->dbSize<0 || MEMDB );
2508
danielk197717221812005-02-15 03:38:05 +00002509 if( pPager->state>=locktype ){
2510 rc = SQLITE_OK;
2511 }else{
danielk197717221812005-02-15 03:38:05 +00002512 do {
drh054889e2005-11-30 03:20:31 +00002513 rc = sqlite3OsLock(pPager->fd, locktype);
drha4afb652005-07-09 02:16:02 +00002514 }while( rc==SQLITE_BUSY && sqlite3InvokeBusyHandler(pPager->pBusyHandler) );
danielk197717221812005-02-15 03:38:05 +00002515 if( rc==SQLITE_OK ){
2516 pPager->state = locktype;
drhb0603412007-02-28 04:47:26 +00002517 IOTRACE(("LOCK %p %d\n", pPager, locktype))
danielk197717221812005-02-15 03:38:05 +00002518 }
2519 }
2520 return rc;
2521}
2522
2523/*
drhf7c57532003-04-25 13:22:51 +00002524** Truncate the file to the number of pages specified.
2525*/
danielk19773b8a05f2007-03-19 17:44:26 +00002526int sqlite3PagerTruncate(Pager *pPager, Pgno nPage){
drhf7c57532003-04-25 13:22:51 +00002527 int rc;
drh1aa2d8b2007-01-03 15:34:29 +00002528 assert( pPager->state>=PAGER_SHARED || MEMDB );
danielk19773b8a05f2007-03-19 17:44:26 +00002529 sqlite3PagerPagecount(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00002530 if( pPager->errCode ){
2531 rc = pPager->errCode;
drh2e6d11b2003-04-25 15:37:57 +00002532 return rc;
2533 }
drh7d02cb72003-06-04 16:24:39 +00002534 if( nPage>=(unsigned)pPager->dbSize ){
drhf7c57532003-04-25 13:22:51 +00002535 return SQLITE_OK;
2536 }
drhb7f91642004-10-31 02:22:47 +00002537 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00002538 pPager->dbSize = nPage;
danielk1977e180dd92007-04-05 17:15:52 +00002539 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00002540 return SQLITE_OK;
2541 }
drh86f8c192007-08-22 00:39:19 +00002542 pagerEnter(pPager);
danielk197776572402004-06-25 02:38:54 +00002543 rc = syncJournal(pPager);
drh86f8c192007-08-22 00:39:19 +00002544 pagerLeave(pPager);
danielk1977369f27e2004-06-15 11:40:04 +00002545 if( rc!=SQLITE_OK ){
2546 return rc;
2547 }
danielk197717221812005-02-15 03:38:05 +00002548
2549 /* Get an exclusive lock on the database before truncating. */
drh86f8c192007-08-22 00:39:19 +00002550 pagerEnter(pPager);
danielk197717221812005-02-15 03:38:05 +00002551 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
drh86f8c192007-08-22 00:39:19 +00002552 pagerLeave(pPager);
danielk197717221812005-02-15 03:38:05 +00002553 if( rc!=SQLITE_OK ){
2554 return rc;
2555 }
2556
drhcb4c40b2004-08-18 19:09:43 +00002557 rc = pager_truncate(pPager, nPage);
drhf7c57532003-04-25 13:22:51 +00002558 return rc;
2559}
2560
2561/*
drhed7c8552001-04-11 14:29:21 +00002562** Shutdown the page cache. Free all memory and close all files.
2563**
2564** If a transaction was in progress when this routine is called, that
2565** transaction is rolled back. All outstanding pages are invalidated
2566** and their memory is freed. Any attempt to use a page associated
2567** with this page cache after this function returns will likely
2568** result in a coredump.
danielk1977aef0bf62005-12-30 16:28:01 +00002569**
2570** This function always succeeds. If a transaction is active an attempt
2571** is made to roll it back. If an error occurs during the rollback
2572** a hot journal may be left in the filesystem but no error is returned
2573** to the caller.
drhed7c8552001-04-11 14:29:21 +00002574*/
danielk19773b8a05f2007-03-19 17:44:26 +00002575int sqlite3PagerClose(Pager *pPager){
drh6f7adc82006-01-11 21:41:20 +00002576#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
drh86f8c192007-08-22 00:39:19 +00002577 if( !MEMDB ){
2578 sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
2579 sqlite3_mutex_enter(mutex);
2580 if( pPager->pPrev ){
2581 pPager->pPrev->pNext = pPager->pNext;
2582 }else{
2583 sqlite3PagerList = pPager->pNext;
2584 }
2585 if( pPager->pNext ){
2586 pPager->pNext->pPrev = pPager->pPrev;
2587 }
2588 sqlite3_mutex_leave(mutex);
2589 }
danielk197713f72992005-12-18 08:51:22 +00002590#endif
2591
drhbafda092007-01-03 23:36:22 +00002592 disable_simulated_io_errors();
drhc2ee76c2007-01-04 14:58:14 +00002593 pPager->errCode = 0;
danielk197741483462007-03-24 16:45:04 +00002594 pPager->exclusiveMode = 0;
drhbafda092007-01-03 23:36:22 +00002595 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00002596 pagerUnlockAndRollback(pPager);
drhbafda092007-01-03 23:36:22 +00002597 enable_simulated_io_errors();
drh4f0c5872007-03-26 22:05:01 +00002598 PAGERTRACE2("CLOSE %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002599 IOTRACE(("CLOSE %p\n", pPager))
danielk1977efaaf572006-01-16 11:29:19 +00002600 assert( pPager->errCode || (pPager->journalOpen==0 && pPager->stmtOpen==0) );
danielk1977e94ddc92005-03-21 03:53:38 +00002601 if( pPager->journalOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002602 sqlite3OsClose(pPager->jfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002603 }
drh17435752007-08-16 04:30:38 +00002604 sqlite3_free(pPager->aInJournal);
danielk1977e94ddc92005-03-21 03:53:38 +00002605 if( pPager->stmtOpen ){
danielk1977b4b47412007-08-17 15:53:36 +00002606 sqlite3OsClose(pPager->stfd);
danielk1977e94ddc92005-03-21 03:53:38 +00002607 }
danielk1977b4b47412007-08-17 15:53:36 +00002608 sqlite3OsClose(pPager->fd);
drh0f892532002-05-30 12:27:03 +00002609 /* Temp files are automatically deleted by the OS
2610 ** if( pPager->tempFile ){
drh66560ad2006-01-06 14:32:19 +00002611 ** sqlite3OsDelete(pPager->zFilename);
drh0f892532002-05-30 12:27:03 +00002612 ** }
2613 */
danielk1977aca790a2005-01-13 11:07:52 +00002614
drh17435752007-08-16 04:30:38 +00002615 sqlite3_free(pPager->aHash);
2616 sqlite3_free(pPager->pTmpSpace);
2617 sqlite3_free(pPager);
drhed7c8552001-04-11 14:29:21 +00002618 return SQLITE_OK;
2619}
2620
drh87cc3b32007-05-08 21:45:27 +00002621#if !defined(NDEBUG) || defined(SQLITE_TEST)
drhed7c8552001-04-11 14:29:21 +00002622/*
drh5e00f6c2001-09-13 13:46:56 +00002623** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +00002624*/
danielk19773b8a05f2007-03-19 17:44:26 +00002625Pgno sqlite3PagerPagenumber(DbPage *p){
drhed7c8552001-04-11 14:29:21 +00002626 return p->pgno;
2627}
drh87cc3b32007-05-08 21:45:27 +00002628#endif
drhed7c8552001-04-11 14:29:21 +00002629
2630/*
drhc8629a12004-05-08 20:07:40 +00002631** The page_ref() function increments the reference count for a page.
2632** If the page is currently on the freelist (the reference count is zero) then
drh7e3b0a02001-04-28 16:52:40 +00002633** remove it from the freelist.
drhc8629a12004-05-08 20:07:40 +00002634**
2635** For non-test systems, page_ref() is a macro that calls _page_ref()
2636** online of the reference count is zero. For test systems, page_ref()
2637** is a real function so that we can set breakpoints and trace it.
drh7e3b0a02001-04-28 16:52:40 +00002638*/
drh836faa42003-01-11 13:30:57 +00002639static void _page_ref(PgHdr *pPg){
drh7e3b0a02001-04-28 16:52:40 +00002640 if( pPg->nRef==0 ){
2641 /* The page is currently on the freelist. Remove it. */
danielk19779f61c2f2007-08-27 17:27:49 +00002642 lruListRemove(pPg);
drh7e3b0a02001-04-28 16:52:40 +00002643 pPg->pPager->nRef++;
2644 }
2645 pPg->nRef++;
drhdd793422001-06-28 01:54:48 +00002646 REFINFO(pPg);
drhdf0b3b02001-06-23 11:36:20 +00002647}
danielk1977aca790a2005-01-13 11:07:52 +00002648#ifdef SQLITE_DEBUG
drhc8629a12004-05-08 20:07:40 +00002649 static void page_ref(PgHdr *pPg){
2650 if( pPg->nRef==0 ){
2651 _page_ref(pPg);
2652 }else{
2653 pPg->nRef++;
2654 REFINFO(pPg);
2655 }
2656 }
2657#else
2658# define page_ref(P) ((P)->nRef==0?_page_ref(P):(void)(P)->nRef++)
2659#endif
drhdf0b3b02001-06-23 11:36:20 +00002660
2661/*
2662** Increment the reference count for a page. The input pointer is
2663** a reference to the page data.
2664*/
danielk19773b8a05f2007-03-19 17:44:26 +00002665int sqlite3PagerRef(DbPage *pPg){
drh86f8c192007-08-22 00:39:19 +00002666 pagerEnter(pPg->pPager);
drhdf0b3b02001-06-23 11:36:20 +00002667 page_ref(pPg);
drh86f8c192007-08-22 00:39:19 +00002668 pagerLeave(pPg->pPager);
drh8c42ca92001-06-22 19:15:00 +00002669 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00002670}
2671
2672/*
drh34e79ce2004-02-08 06:05:46 +00002673** Sync the journal. In other words, make sure all the pages that have
2674** been written to the journal have actually reached the surface of the
2675** disk. It is not safe to modify the original database file until after
2676** the journal has been synced. If the original database is modified before
2677** the journal is synced and a power failure occurs, the unsynced journal
2678** data would be lost and we would be unable to completely rollback the
2679** database changes. Database corruption would occur.
2680**
2681** This routine also updates the nRec field in the header of the journal.
2682** (See comments on the pager_playback() routine for additional information.)
2683** If the sync mode is FULL, two syncs will occur. First the whole journal
2684** is synced, then the nRec field is updated, then a second sync occurs.
drhb19a2bc2001-09-16 00:13:26 +00002685**
drh34e79ce2004-02-08 06:05:46 +00002686** For temporary databases, we do not care if we are able to rollback
danielk19774cd2cd52007-08-23 14:48:23 +00002687** after a power failure, so no sync occurs.
2688**
2689** If the IOCAP_SEQUENTIAL flag is set for the persistent media on which
2690** the database is stored, then OsSync() is never called on the journal
2691** file. In this case all that is required is to update the nRec field in
2692** the journal header.
drhfa86c412002-02-02 15:01:15 +00002693**
drh34e79ce2004-02-08 06:05:46 +00002694** This routine clears the needSync field of every page current held in
2695** memory.
drh50e5dad2001-09-15 00:57:28 +00002696*/
danielk197776572402004-06-25 02:38:54 +00002697static int syncJournal(Pager *pPager){
drh50e5dad2001-09-15 00:57:28 +00002698 PgHdr *pPg;
2699 int rc = SQLITE_OK;
drh03eb96a2002-11-10 23:32:56 +00002700
danielk19774cd2cd52007-08-23 14:48:23 +00002701
drh03eb96a2002-11-10 23:32:56 +00002702 /* Sync the journal before modifying the main database
2703 ** (assuming there is a journal and it needs to be synced.)
2704 */
danielk197776572402004-06-25 02:38:54 +00002705 if( pPager->needSync ){
drhfa86c412002-02-02 15:01:15 +00002706 if( !pPager->tempFile ){
danielk19774cd2cd52007-08-23 14:48:23 +00002707 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
drhdb48ee02003-01-16 13:42:43 +00002708 assert( pPager->journalOpen );
danielk19774cd2cd52007-08-23 14:48:23 +00002709
drh946966f2004-02-25 02:20:41 +00002710 /* assert( !pPager->noSync ); // noSync might be set if synchronous
2711 ** was turned off after the transaction was started. Ticket #615 */
drh968af522003-02-11 14:55:40 +00002712#ifndef NDEBUG
2713 {
drh34e79ce2004-02-08 06:05:46 +00002714 /* Make sure the pPager->nRec counter we are keeping agrees
2715 ** with the nRec computed from the size of the journal file.
2716 */
drheb206252004-10-01 02:00:31 +00002717 i64 jSz;
drh054889e2005-11-30 03:20:31 +00002718 rc = sqlite3OsFileSize(pPager->jfd, &jSz);
drh968af522003-02-11 14:55:40 +00002719 if( rc!=0 ) return rc;
danielk197776572402004-06-25 02:38:54 +00002720 assert( pPager->journalOff==jSz );
drh968af522003-02-11 14:55:40 +00002721 }
2722#endif
danielk19774cd2cd52007-08-23 14:48:23 +00002723 if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
danielk197776572402004-06-25 02:38:54 +00002724 /* Write the nRec value into the journal file header. If in
2725 ** full-synchronous mode, sync the journal first. This ensures that
2726 ** all data has really hit the disk before nRec is updated to mark
danielk19774cd2cd52007-08-23 14:48:23 +00002727 ** it as a candidate for rollback.
2728 **
2729 ** This is not required if the persistent media supports the
2730 ** SAFE_APPEND property. Because in this case it is not possible
2731 ** for garbage data to be appended to the file, the nRec field
2732 ** is populated with 0xFFFFFFFF when the journal header is written
2733 ** and never needs to be updated.
danielk197776572402004-06-25 02:38:54 +00002734 */
danielk19774cd2cd52007-08-23 14:48:23 +00002735 i64 jrnlOff;
2736 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
drh4f0c5872007-03-26 22:05:01 +00002737 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00002738 IOTRACE(("JSYNC %p\n", pPager))
danielk1977f036aef2007-08-20 05:36:51 +00002739 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags);
drhd8d66e82003-02-12 02:10:15 +00002740 if( rc!=0 ) return rc;
2741 }
danielk197713adf8a2004-06-03 16:08:41 +00002742
danielk197762079062007-08-15 17:08:46 +00002743 jrnlOff = pPager->journalHdr + sizeof(aJournalMagic);
2744 IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4));
2745 rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec);
drhb4746b92005-09-09 01:32:06 +00002746 if( rc ) return rc;
drh968af522003-02-11 14:55:40 +00002747 }
danielk19774cd2cd52007-08-23 14:48:23 +00002748 if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
2749 PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
2750 IOTRACE(("JSYNC %p\n", pPager))
2751 rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags|
2752 (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
2753 );
2754 if( rc!=0 ) return rc;
2755 }
drhdb48ee02003-01-16 13:42:43 +00002756 pPager->journalStarted = 1;
drhfa86c412002-02-02 15:01:15 +00002757 }
drh50e5dad2001-09-15 00:57:28 +00002758 pPager->needSync = 0;
drh341eae82003-01-21 02:39:36 +00002759
2760 /* Erase the needSync flag from every page.
2761 */
2762 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2763 pPg->needSync = 0;
2764 }
danielk19779f61c2f2007-08-27 17:27:49 +00002765 lruListSetFirstSynced(pPager);
drh50e5dad2001-09-15 00:57:28 +00002766 }
drh03eb96a2002-11-10 23:32:56 +00002767
drh341eae82003-01-21 02:39:36 +00002768#ifndef NDEBUG
2769 /* If the Pager.needSync flag is clear then the PgHdr.needSync
2770 ** flag must also be clear for all pages. Verify that this
2771 ** invariant is true.
drh03eb96a2002-11-10 23:32:56 +00002772 */
drh341eae82003-01-21 02:39:36 +00002773 else{
2774 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2775 assert( pPg->needSync==0 );
2776 }
danielk19779f61c2f2007-08-27 17:27:49 +00002777 assert( pPager->lru.pFirstSynced==pPager->lru.pFirst );
drh03eb96a2002-11-10 23:32:56 +00002778 }
drh341eae82003-01-21 02:39:36 +00002779#endif
drhdb48ee02003-01-16 13:42:43 +00002780
drh81a20f22001-10-12 17:30:04 +00002781 return rc;
drh50e5dad2001-09-15 00:57:28 +00002782}
2783
2784/*
drhdc3e10b2006-06-15 14:31:06 +00002785** Merge two lists of pages connected by pDirty and in pgno order.
2786** Do not both fixing the pPrevDirty pointers.
2787*/
2788static PgHdr *merge_pagelist(PgHdr *pA, PgHdr *pB){
2789 PgHdr result, *pTail;
2790 pTail = &result;
2791 while( pA && pB ){
2792 if( pA->pgno<pB->pgno ){
2793 pTail->pDirty = pA;
2794 pTail = pA;
2795 pA = pA->pDirty;
2796 }else{
2797 pTail->pDirty = pB;
2798 pTail = pB;
2799 pB = pB->pDirty;
2800 }
2801 }
2802 if( pA ){
2803 pTail->pDirty = pA;
2804 }else if( pB ){
2805 pTail->pDirty = pB;
2806 }else{
2807 pTail->pDirty = 0;
2808 }
2809 return result.pDirty;
2810}
2811
2812/*
2813** Sort the list of pages in accending order by pgno. Pages are
2814** connected by pDirty pointers. The pPrevDirty pointers are
2815** corrupted by this sort.
2816*/
danielk197724168722007-04-02 05:07:47 +00002817#define N_SORT_BUCKET_ALLOC 25
2818#define N_SORT_BUCKET 25
2819#ifdef SQLITE_TEST
2820 int sqlite3_pager_n_sort_bucket = 0;
2821 #undef N_SORT_BUCKET
2822 #define N_SORT_BUCKET \
2823 (sqlite3_pager_n_sort_bucket?sqlite3_pager_n_sort_bucket:N_SORT_BUCKET_ALLOC)
2824#endif
drhdc3e10b2006-06-15 14:31:06 +00002825static PgHdr *sort_pagelist(PgHdr *pIn){
danielk197724168722007-04-02 05:07:47 +00002826 PgHdr *a[N_SORT_BUCKET_ALLOC], *p;
drhdc3e10b2006-06-15 14:31:06 +00002827 int i;
2828 memset(a, 0, sizeof(a));
2829 while( pIn ){
2830 p = pIn;
2831 pIn = p->pDirty;
2832 p->pDirty = 0;
2833 for(i=0; i<N_SORT_BUCKET-1; i++){
2834 if( a[i]==0 ){
2835 a[i] = p;
2836 break;
2837 }else{
2838 p = merge_pagelist(a[i], p);
2839 a[i] = 0;
2840 }
2841 }
2842 if( i==N_SORT_BUCKET-1 ){
danielk197724168722007-04-02 05:07:47 +00002843 /* Coverage: To get here, there need to be 2^(N_SORT_BUCKET)
2844 ** elements in the input list. This is possible, but impractical.
2845 ** Testing this line is the point of global variable
2846 ** sqlite3_pager_n_sort_bucket.
2847 */
drhdc3e10b2006-06-15 14:31:06 +00002848 a[i] = merge_pagelist(a[i], p);
2849 }
2850 }
2851 p = a[0];
2852 for(i=1; i<N_SORT_BUCKET; i++){
2853 p = merge_pagelist(p, a[i]);
2854 }
2855 return p;
2856}
2857
2858/*
drh2554f8b2003-01-22 01:26:44 +00002859** Given a list of pages (connected by the PgHdr.pDirty pointer) write
2860** every one of those pages out to the database file and mark them all
2861** as clean.
2862*/
2863static int pager_write_pagelist(PgHdr *pList){
2864 Pager *pPager;
drh153c62c2007-08-24 03:51:33 +00002865 PgHdr *p;
drh2554f8b2003-01-22 01:26:44 +00002866 int rc;
2867
2868 if( pList==0 ) return SQLITE_OK;
2869 pPager = pList->pPager;
danielk19779eed5052004-06-04 10:38:30 +00002870
2871 /* At this point there may be either a RESERVED or EXCLUSIVE lock on the
2872 ** database file. If there is already an EXCLUSIVE lock, the following
drh054889e2005-11-30 03:20:31 +00002873 ** calls to sqlite3OsLock() are no-ops.
danielk19779eed5052004-06-04 10:38:30 +00002874 **
drha6abd042004-06-09 17:37:22 +00002875 ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
2876 ** through an intermediate state PENDING. A PENDING lock prevents new
2877 ** readers from attaching to the database but is unsufficient for us to
2878 ** write. The idea of a PENDING lock is to prevent new readers from
2879 ** coming in while we wait for existing readers to clear.
danielk19779eed5052004-06-04 10:38:30 +00002880 **
drha6abd042004-06-09 17:37:22 +00002881 ** While the pager is in the RESERVED state, the original database file
2882 ** is unchanged and we can rollback without having to playback the
2883 ** journal into the original database file. Once we transition to
2884 ** EXCLUSIVE, it means the database file has been changed and any rollback
2885 ** will require a journal playback.
danielk19779eed5052004-06-04 10:38:30 +00002886 */
drh684917c2004-10-05 02:41:42 +00002887 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
danielk19779eed5052004-06-04 10:38:30 +00002888 if( rc!=SQLITE_OK ){
2889 return rc;
2890 }
2891
drhdc3e10b2006-06-15 14:31:06 +00002892 pList = sort_pagelist(pList);
drh153c62c2007-08-24 03:51:33 +00002893 for(p=pList; p; p=p->pDirty){
2894 assert( p->dirty );
2895 p->dirty = 0;
2896 }
drh2554f8b2003-01-22 01:26:44 +00002897 while( pList ){
danielk19777a2b1ee2007-08-21 14:27:01 +00002898
2899 /* If the file has not yet been opened, open it now. */
2900 if( !pPager->fd->pMethods ){
2901 assert(pPager->tempFile);
drh33f4e022007-09-03 15:19:34 +00002902 rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->fd, pPager->zFilename,
2903 pPager->vfsFlags);
danielk19777a2b1ee2007-08-21 14:27:01 +00002904 if( rc ) return rc;
2905 }
2906
danielk1977687566d2004-11-02 12:56:41 +00002907 /* If there are dirty pages in the page cache with page numbers greater
danielk19773b8a05f2007-03-19 17:44:26 +00002908 ** than Pager.dbSize, this means sqlite3PagerTruncate() was called to
danielk1977687566d2004-11-02 12:56:41 +00002909 ** make the file smaller (presumably by auto-vacuum code). Do not write
2910 ** any such pages to the file.
2911 */
2912 if( pList->pgno<=pPager->dbSize ){
danielk197762079062007-08-15 17:08:46 +00002913 i64 offset = (pList->pgno-1)*(i64)pPager->pageSize;
drhc001c582006-03-06 18:23:16 +00002914 char *pData = CODEC2(pPager, PGHDR_TO_DATA(pList), pList->pgno, 6);
drh477731b2007-06-16 03:06:27 +00002915 PAGERTRACE4("STORE %d page %d hash(%08x)\n",
2916 PAGERID(pPager), pList->pgno, pager_pagehash(pList));
drh538f5702007-04-13 02:14:30 +00002917 IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno));
danielk197762079062007-08-15 17:08:46 +00002918 rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00002919 PAGER_INCR(sqlite3_pager_writedb_count);
2920 PAGER_INCR(pPager->nWrite);
drh86a88112007-04-16 15:02:19 +00002921 if( pList->pgno==1 ){
2922 memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
2923 }
danielk1977687566d2004-11-02 12:56:41 +00002924 }
2925#ifndef NDEBUG
2926 else{
drh4f0c5872007-03-26 22:05:01 +00002927 PAGERTRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno);
danielk1977687566d2004-11-02 12:56:41 +00002928 }
2929#endif
drh2554f8b2003-01-22 01:26:44 +00002930 if( rc ) return rc;
danielk19773c407372005-02-15 02:54:14 +00002931#ifdef SQLITE_CHECK_PAGES
2932 pList->pageHash = pager_pagehash(pList);
2933#endif
drh2554f8b2003-01-22 01:26:44 +00002934 pList = pList->pDirty;
2935 }
2936 return SQLITE_OK;
2937}
2938
2939/*
2940** Collect every dirty page into a dirty list and
2941** return a pointer to the head of that list. All pages are
2942** collected even if they are still in use.
2943*/
2944static PgHdr *pager_get_all_dirty_pages(Pager *pPager){
drh605ad8f2006-05-03 23:34:05 +00002945 return pPager->pDirty;
drh2554f8b2003-01-22 01:26:44 +00002946}
2947
2948/*
drh165ffe92005-03-15 17:09:30 +00002949** Return TRUE if there is a hot journal on the given pager.
2950** A hot journal is one that needs to be played back.
2951**
2952** If the current size of the database file is 0 but a journal file
2953** exists, that is probably an old journal left over from a prior
2954** database with the same name. Just delete the journal.
2955*/
2956static int hasHotJournal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00002957 sqlite3_vfs *pVfs = pPager->pVfs;
drh165ffe92005-03-15 17:09:30 +00002958 if( !pPager->useJournal ) return 0;
danielk1977b4b47412007-08-17 15:53:36 +00002959 if( !sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS) ){
drhbb5f18d2007-04-06 18:23:17 +00002960 return 0;
2961 }
2962 if( sqlite3OsCheckReservedLock(pPager->fd) ){
2963 return 0;
2964 }
danielk19773b8a05f2007-03-19 17:44:26 +00002965 if( sqlite3PagerPagecount(pPager)==0 ){
danielk1977fee2d252007-08-18 10:59:19 +00002966 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
drh165ffe92005-03-15 17:09:30 +00002967 return 0;
2968 }else{
2969 return 1;
2970 }
2971}
2972
danielk19775591df52005-12-20 09:19:37 +00002973/*
danielk1977aef0bf62005-12-30 16:28:01 +00002974** Try to find a page in the cache that can be recycled.
2975**
2976** This routine may return SQLITE_IOERR, SQLITE_FULL or SQLITE_OK. It
danielk1977efaaf572006-01-16 11:29:19 +00002977** does not set the pPager->errCode variable.
danielk19775591df52005-12-20 09:19:37 +00002978*/
danielk1977843e65f2007-09-01 16:16:15 +00002979static int pager_recycle(Pager *pPager, PgHdr **ppPg){
danielk197713f72992005-12-18 08:51:22 +00002980 PgHdr *pPg;
2981 *ppPg = 0;
2982
danielk1977843e65f2007-09-01 16:16:15 +00002983 /* It is illegal to call this function unless the pager object
2984 ** pointed to by pPager has at least one free page (page with nRef==0).
2985 */
danielk1977c551edc2007-04-05 13:12:13 +00002986 assert(!MEMDB);
danielk1977843e65f2007-09-01 16:16:15 +00002987 assert(pPager->lru.pFirst);
danielk1977c551edc2007-04-05 13:12:13 +00002988
danielk197713f72992005-12-18 08:51:22 +00002989 /* Find a page to recycle. Try to locate a page that does not
2990 ** require us to do an fsync() on the journal.
2991 */
danielk19779f61c2f2007-08-27 17:27:49 +00002992 pPg = pPager->lru.pFirstSynced;
danielk197713f72992005-12-18 08:51:22 +00002993
2994 /* If we could not find a page that does not require an fsync()
2995 ** on the journal file then fsync the journal file. This is a
2996 ** very slow operation, so we work hard to avoid it. But sometimes
2997 ** it can't be helped.
2998 */
danielk1977843e65f2007-09-01 16:16:15 +00002999 if( pPg==0 && pPager->lru.pFirst){
danielk19774cd2cd52007-08-23 14:48:23 +00003000 int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
danielk197713f72992005-12-18 08:51:22 +00003001 int rc = syncJournal(pPager);
3002 if( rc!=0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00003003 return rc;
danielk197713f72992005-12-18 08:51:22 +00003004 }
danielk19774cd2cd52007-08-23 14:48:23 +00003005 if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
danielk197713f72992005-12-18 08:51:22 +00003006 /* If in full-sync mode, write a new journal header into the
3007 ** journal file. This is done to avoid ever modifying a journal
3008 ** header that is involved in the rollback of pages that have
3009 ** already been written to the database (in case the header is
3010 ** trashed when the nRec field is updated).
3011 */
3012 pPager->nRec = 0;
3013 assert( pPager->journalOff > 0 );
danielk19774099f6e2007-03-19 11:25:20 +00003014 assert( pPager->doNotSync==0 );
danielk197713f72992005-12-18 08:51:22 +00003015 rc = writeJournalHdr(pPager);
3016 if( rc!=0 ){
danielk1977aef0bf62005-12-30 16:28:01 +00003017 return rc;
danielk197713f72992005-12-18 08:51:22 +00003018 }
3019 }
danielk19779f61c2f2007-08-27 17:27:49 +00003020 pPg = pPager->lru.pFirst;
danielk197713f72992005-12-18 08:51:22 +00003021 }
danielk197713f72992005-12-18 08:51:22 +00003022
3023 assert( pPg->nRef==0 );
3024
3025 /* Write the page to the database file if it is dirty.
3026 */
3027 if( pPg->dirty ){
3028 int rc;
3029 assert( pPg->needSync==0 );
drh605ad8f2006-05-03 23:34:05 +00003030 makeClean(pPg);
3031 pPg->dirty = 1;
danielk197713f72992005-12-18 08:51:22 +00003032 pPg->pDirty = 0;
3033 rc = pager_write_pagelist( pPg );
drh153c62c2007-08-24 03:51:33 +00003034 pPg->dirty = 0;
danielk197713f72992005-12-18 08:51:22 +00003035 if( rc!=SQLITE_OK ){
danielk1977aef0bf62005-12-30 16:28:01 +00003036 return rc;
danielk197713f72992005-12-18 08:51:22 +00003037 }
3038 }
3039 assert( pPg->dirty==0 );
3040
3041 /* If the page we are recycling is marked as alwaysRollback, then
3042 ** set the global alwaysRollback flag, thus disabling the
drh80e35f42007-03-30 14:06:34 +00003043 ** sqlite3PagerDontRollback() optimization for the rest of this transaction.
danielk197713f72992005-12-18 08:51:22 +00003044 ** It is necessary to do this because the page marked alwaysRollback
3045 ** might be reloaded at a later time but at that point we won't remember
3046 ** that is was marked alwaysRollback. This means that all pages must
3047 ** be marked as alwaysRollback from here on out.
3048 */
3049 if( pPg->alwaysRollback ){
drh602c2372007-03-01 00:29:13 +00003050 IOTRACE(("ALWAYS_ROLLBACK %p\n", pPager))
danielk197713f72992005-12-18 08:51:22 +00003051 pPager->alwaysRollback = 1;
3052 }
3053
3054 /* Unlink the old page from the free list and the hash table
3055 */
3056 unlinkPage(pPg);
drh7c4ac0c2007-04-05 11:25:58 +00003057 assert( pPg->pgno==0 );
danielk197713f72992005-12-18 08:51:22 +00003058
3059 *ppPg = pPg;
3060 return SQLITE_OK;
3061}
3062
drh86f8c192007-08-22 00:39:19 +00003063#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
danielk197713f72992005-12-18 08:51:22 +00003064/*
3065** This function is called to free superfluous dynamically allocated memory
3066** held by the pager system. Memory in use by any SQLite pager allocated
drh17435752007-08-16 04:30:38 +00003067** by the current thread may be sqlite3_free()ed.
danielk197713f72992005-12-18 08:51:22 +00003068**
3069** nReq is the number of bytes of memory required. Once this much has
drh86f8c192007-08-22 00:39:19 +00003070** been released, the function returns. The return value is the total number
danielk197713f72992005-12-18 08:51:22 +00003071** of bytes of memory released.
3072*/
danielk19773b8a05f2007-03-19 17:44:26 +00003073int sqlite3PagerReleaseMemory(int nReq){
drh86f8c192007-08-22 00:39:19 +00003074 int nReleased = 0; /* Bytes of memory released so far */
3075 sqlite3_mutex *mutex; /* The MEM2 mutex */
3076 Pager *pPager; /* For looping over pagers */
danielk19779f61c2f2007-08-27 17:27:49 +00003077 int rc = SQLITE_OK;
danielk197713f72992005-12-18 08:51:22 +00003078
drh86f8c192007-08-22 00:39:19 +00003079 /* Acquire the memory-management mutex
danielk1977441b09a2006-01-05 13:48:29 +00003080 */
drh86f8c192007-08-22 00:39:19 +00003081 mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
3082 sqlite3_mutex_enter(mutex);
3083
3084 /* Signal all database connections that memory management wants
3085 ** to have access to the pagers.
3086 */
3087 for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
3088 pPager->iInUseMM = 1;
danielk1977441b09a2006-01-05 13:48:29 +00003089 }
3090
danielk19779f61c2f2007-08-27 17:27:49 +00003091 while( rc==SQLITE_OK && (nReq<0 || nReleased<nReq) ){
3092 PgHdr *pPg;
3093 PgHdr *pRecycled;
3094
3095 /* Try to find a page to recycle that does not require a sync(). If
3096 ** this is not possible, find one that does require a sync().
3097 */
3098 sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
3099 pPg = sqlite3LruPageList.pFirstSynced;
3100 while( pPg && (pPg->needSync || pPg->pPager->iInUseDB) ){
3101 pPg = pPg->gfree.pNext;
3102 }
3103 if( !pPg ){
3104 pPg = sqlite3LruPageList.pFirst;
3105 while( pPg && pPg->pPager->iInUseDB ){
3106 pPg = pPg->gfree.pNext;
3107 }
3108 }
3109 sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
danielk197713f72992005-12-18 08:51:22 +00003110
danielk197784f786f2007-08-28 08:00:17 +00003111 /* If pPg==0, then the block above has failed to find a page to
3112 ** recycle. In this case return early - no further memory will
3113 ** be released.
3114 */
danielk19779f61c2f2007-08-27 17:27:49 +00003115 if( !pPg ) break;
danielk197713f72992005-12-18 08:51:22 +00003116
danielk19779f61c2f2007-08-27 17:27:49 +00003117 pPager = pPg->pPager;
3118 assert(!pPg->needSync || pPg==pPager->lru.pFirst);
3119 assert(pPg->needSync || pPg==pPager->lru.pFirstSynced);
3120
danielk1977843e65f2007-09-01 16:16:15 +00003121 rc = pager_recycle(pPager, &pRecycled);
danielk19779f61c2f2007-08-27 17:27:49 +00003122 assert(pRecycled==pPg || rc!=SQLITE_OK);
3123 if( rc==SQLITE_OK ){
3124 /* We've found a page to free. At this point the page has been
3125 ** removed from the page hash-table, free-list and synced-list
3126 ** (pFirstSynced). It is still in the all pages (pAll) list.
3127 ** Remove it from this list before freeing.
3128 **
3129 ** Todo: Check the Pager.pStmt list to make sure this is Ok. It
3130 ** probably is though.
danielk197713f72992005-12-18 08:51:22 +00003131 */
danielk19779f61c2f2007-08-27 17:27:49 +00003132 PgHdr *pTmp;
3133 assert( pPg );
3134 if( pPg==pPager->pAll ){
3135 pPager->pAll = pPg->pNextAll;
3136 }else{
3137 for( pTmp=pPager->pAll; pTmp->pNextAll!=pPg; pTmp=pTmp->pNextAll ){}
3138 pTmp->pNextAll = pPg->pNextAll;
danielk197713f72992005-12-18 08:51:22 +00003139 }
danielk19779f61c2f2007-08-27 17:27:49 +00003140 nReleased += (
3141 sizeof(*pPg) + pPager->pageSize
3142 + sizeof(u32) + pPager->nExtra
3143 + MEMDB*sizeof(PgHistory)
3144 );
3145 IOTRACE(("PGFREE %p %d *\n", pPager, pPg->pgno));
3146 PAGER_INCR(sqlite3_pager_pgfree_count);
drhbf4bca52007-09-06 22:19:14 +00003147 sqlite3_free(pPg->pData);
danielk19779f61c2f2007-08-27 17:27:49 +00003148 sqlite3_free(pPg);
danielk197784f786f2007-08-28 08:00:17 +00003149 pPager->nPage--;
danielk19779f61c2f2007-08-27 17:27:49 +00003150 }else{
3151 /* An error occured whilst writing to the database file or
3152 ** journal in pager_recycle(). The error is not returned to the
3153 ** caller of this function. Instead, set the Pager.errCode variable.
3154 ** The error will be returned to the user (or users, in the case
3155 ** of a shared pager cache) of the pager for which the error occured.
3156 */
3157 assert(
3158 (rc&0xff)==SQLITE_IOERR ||
3159 rc==SQLITE_FULL ||
3160 rc==SQLITE_BUSY
3161 );
3162 assert( pPager->state>=PAGER_RESERVED );
3163 pager_error(pPager, rc);
danielk197713f72992005-12-18 08:51:22 +00003164 }
3165 }
danielk1977aef0bf62005-12-30 16:28:01 +00003166
drh86f8c192007-08-22 00:39:19 +00003167 /* Clear the memory management flags and release the mutex
3168 */
3169 for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
3170 pPager->iInUseMM = 0;
3171 }
3172 sqlite3_mutex_leave(mutex);
3173
3174 /* Return the number of bytes released
3175 */
danielk197713f72992005-12-18 08:51:22 +00003176 return nReleased;
3177}
drh86f8c192007-08-22 00:39:19 +00003178#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
danielk197713f72992005-12-18 08:51:22 +00003179
drh165ffe92005-03-15 17:09:30 +00003180/*
danielk1977e180dd92007-04-05 17:15:52 +00003181** Read the content of page pPg out of the database file.
3182*/
3183static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){
3184 int rc;
danielk197762079062007-08-15 17:08:46 +00003185 i64 offset;
danielk1977e180dd92007-04-05 17:15:52 +00003186 assert( MEMDB==0 );
danielk19777a2b1ee2007-08-21 14:27:01 +00003187 assert(pPager->fd->pMethods||pPager->tempFile);
3188 if( !pPager->fd->pMethods ){
3189 return SQLITE_IOERR_SHORT_READ;
3190 }
danielk197762079062007-08-15 17:08:46 +00003191 offset = (pgno-1)*(i64)pPager->pageSize;
3192 rc = sqlite3OsRead(pPager->fd, PGHDR_TO_DATA(pPg), pPager->pageSize, offset);
drh538f5702007-04-13 02:14:30 +00003193 PAGER_INCR(sqlite3_pager_readdb_count);
3194 PAGER_INCR(pPager->nRead);
3195 IOTRACE(("PGIN %p %d\n", pPager, pgno));
drh86a88112007-04-16 15:02:19 +00003196 if( pgno==1 ){
3197 memcpy(&pPager->dbFileVers, &((u8*)PGHDR_TO_DATA(pPg))[24],
3198 sizeof(pPager->dbFileVers));
3199 }
danielk1977e180dd92007-04-05 17:15:52 +00003200 CODEC1(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3);
drh477731b2007-06-16 03:06:27 +00003201 PAGERTRACE4("FETCH %d page %d hash(%08x)\n",
3202 PAGERID(pPager), pPg->pgno, pager_pagehash(pPg));
danielk1977e180dd92007-04-05 17:15:52 +00003203 return rc;
3204}
3205
3206
3207/*
danielk1977e277be02007-03-23 18:12:06 +00003208** This function is called to obtain the shared lock required before
3209** data may be read from the pager cache. If the shared lock has already
3210** been obtained, this function is a no-op.
danielk1977393f0682007-03-31 10:00:48 +00003211**
3212** Immediately after obtaining the shared lock (if required), this function
3213** checks for a hot-journal file. If one is found, an emergency rollback
3214** is performed immediately.
danielk1977e277be02007-03-23 18:12:06 +00003215*/
3216static int pagerSharedLock(Pager *pPager){
3217 int rc = SQLITE_OK;
3218
3219 if( pPager->state==PAGER_UNLOCK ){
danielk1977b4b47412007-08-17 15:53:36 +00003220 sqlite3_vfs *pVfs = pPager->pVfs;
danielk1977e277be02007-03-23 18:12:06 +00003221 if( !MEMDB ){
3222 assert( pPager->nRef==0 );
3223 if( !pPager->noReadlock ){
3224 rc = pager_wait_on_lock(pPager, SHARED_LOCK);
3225 if( rc!=SQLITE_OK ){
3226 return pager_error(pPager, rc);
3227 }
3228 assert( pPager->state>=SHARED_LOCK );
3229 }
3230
3231 /* If a journal file exists, and there is no RESERVED lock on the
3232 ** database file, then it either needs to be played back or deleted.
3233 */
3234 if( hasHotJournal(pPager) ){
3235 /* Get an EXCLUSIVE lock on the database file. At this point it is
3236 ** important that a RESERVED lock is not obtained on the way to the
3237 ** EXCLUSIVE lock. If it were, another process might open the
3238 ** database file, detect the RESERVED lock, and conclude that the
3239 ** database is safe to read while this process is still rolling it
3240 ** back.
3241 **
3242 ** Because the intermediate RESERVED lock is not requested, the
3243 ** second process will get to this point in the code and fail to
3244 ** obtain it's own EXCLUSIVE lock on the database file.
3245 */
3246 rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
3247 if( rc!=SQLITE_OK ){
3248 pager_unlock(pPager);
3249 return pager_error(pPager, rc);
3250 }
3251 pPager->state = PAGER_EXCLUSIVE;
3252
3253 /* Open the journal for reading only. Return SQLITE_BUSY if
3254 ** we are unable to open the journal file.
3255 **
3256 ** The journal file does not need to be locked itself. The
3257 ** journal file is never open unless the main database file holds
3258 ** a write lock, so there is never any chance of two or more
3259 ** processes opening the journal at the same time.
danielk1977979f38e2007-03-27 16:19:51 +00003260 **
drhfd131da2007-08-07 17:13:03 +00003261 ** Open the journal for read/write access. This is because in
3262 ** exclusive-access mode the file descriptor will be kept open and
danielk1977979f38e2007-03-27 16:19:51 +00003263 ** possibly used for a transaction later on. On some systems, the
3264 ** OsTruncate() call used in exclusive-access mode also requires
3265 ** a read/write file handle.
danielk1977e277be02007-03-23 18:12:06 +00003266 */
danielk1977979f38e2007-03-27 16:19:51 +00003267 rc = SQLITE_BUSY;
danielk1977b4b47412007-08-17 15:53:36 +00003268 if( sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS) ){
3269 int fout = 0;
danielk1977fee2d252007-08-18 10:59:19 +00003270 int flags = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
danielk19777152de82007-03-29 17:28:14 +00003271 assert( !pPager->tempFile );
danielk1977a1644fd2007-08-29 12:31:25 +00003272 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, &fout);
danielk1977b4b47412007-08-17 15:53:36 +00003273 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
3274 if( fout&SQLITE_OPEN_READONLY ){
danielk1977979f38e2007-03-27 16:19:51 +00003275 rc = SQLITE_BUSY;
danielk1977b4b47412007-08-17 15:53:36 +00003276 sqlite3OsClose(pPager->jfd);
danielk1977979f38e2007-03-27 16:19:51 +00003277 }
3278 }
danielk1977e277be02007-03-23 18:12:06 +00003279 if( rc!=SQLITE_OK ){
3280 pager_unlock(pPager);
danielk1977a1644fd2007-08-29 12:31:25 +00003281 return (rc==SQLITE_NOMEM?rc:SQLITE_BUSY);
danielk1977e277be02007-03-23 18:12:06 +00003282 }
3283 pPager->journalOpen = 1;
3284 pPager->journalStarted = 0;
3285 pPager->journalOff = 0;
3286 pPager->setMaster = 0;
3287 pPager->journalHdr = 0;
3288
3289 /* Playback and delete the journal. Drop the database write
3290 ** lock and reacquire the read lock.
3291 */
3292 rc = pager_playback(pPager, 1);
3293 if( rc!=SQLITE_OK ){
3294 return pager_error(pPager, rc);
3295 }
danielk1977c5859712007-03-26 12:26:27 +00003296 assert(pPager->state==PAGER_SHARED ||
3297 (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
3298 );
danielk1977e277be02007-03-23 18:12:06 +00003299 }
3300
3301 if( pPager->pAll ){
danielk197724168722007-04-02 05:07:47 +00003302 /* The shared-lock has just been acquired on the database file
3303 ** and there are already pages in the cache (from a previous
drh86a88112007-04-16 15:02:19 +00003304 ** read or write transaction). Check to see if the database
3305 ** has been modified. If the database has changed, flush the
3306 ** cache.
3307 **
3308 ** Database changes is detected by looking at 15 bytes beginning
3309 ** at offset 24 into the file. The first 4 of these 16 bytes are
3310 ** a 32-bit counter that is incremented with each change. The
3311 ** other bytes change randomly with each file change when
3312 ** a codec is in use.
3313 **
3314 ** There is a vanishingly small chance that a change will not be
drh6fa51032007-05-09 20:35:31 +00003315 ** detected. The chance of an undetected change is so small that
drh86a88112007-04-16 15:02:19 +00003316 ** it can be neglected.
danielk197724168722007-04-02 05:07:47 +00003317 */
drh86a88112007-04-16 15:02:19 +00003318 char dbFileVers[sizeof(pPager->dbFileVers)];
danielk1977e180dd92007-04-05 17:15:52 +00003319 sqlite3PagerPagecount(pPager);
danielk197724168722007-04-02 05:07:47 +00003320
danielk1977e180dd92007-04-05 17:15:52 +00003321 if( pPager->errCode ){
3322 return pPager->errCode;
danielk1977e277be02007-03-23 18:12:06 +00003323 }
3324
danielk1977e180dd92007-04-05 17:15:52 +00003325 if( pPager->dbSize>0 ){
drhae5e4452007-05-03 17:18:36 +00003326 IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
danielk197762079062007-08-15 17:08:46 +00003327 rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
danielk1977e180dd92007-04-05 17:15:52 +00003328 if( rc!=SQLITE_OK ){
3329 return rc;
3330 }
drh86a88112007-04-16 15:02:19 +00003331 }else{
3332 memset(dbFileVers, 0, sizeof(dbFileVers));
danielk1977e180dd92007-04-05 17:15:52 +00003333 }
3334
drh86a88112007-04-16 15:02:19 +00003335 if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
danielk1977e180dd92007-04-05 17:15:52 +00003336 pager_reset(pPager);
danielk1977e277be02007-03-23 18:12:06 +00003337 }
3338 }
3339 }
danielk1977c5859712007-03-26 12:26:27 +00003340 assert( pPager->exclusiveMode || pPager->state<=PAGER_SHARED );
3341 if( pPager->state==PAGER_UNLOCK ){
3342 pPager->state = PAGER_SHARED;
3343 }
danielk1977e277be02007-03-23 18:12:06 +00003344 }
3345
3346 return rc;
3347}
3348
3349/*
danielk1977e180dd92007-04-05 17:15:52 +00003350** Allocate a PgHdr object. Either create a new one or reuse
3351** an existing one that is not otherwise in use.
3352**
3353** A new PgHdr structure is created if any of the following are
3354** true:
3355**
3356** (1) We have not exceeded our maximum allocated cache size
3357** as set by the "PRAGMA cache_size" command.
3358**
3359** (2) There are no unused PgHdr objects available at this time.
3360**
3361** (3) This is an in-memory database.
3362**
3363** (4) There are no PgHdr objects that do not require a journal
3364** file sync and a sync of the journal file is currently
3365** prohibited.
3366**
3367** Otherwise, reuse an existing PgHdr. In other words, reuse an
3368** existing PgHdr if all of the following are true:
3369**
3370** (1) We have reached or exceeded the maximum cache size
3371** allowed by "PRAGMA cache_size".
3372**
3373** (2) There is a PgHdr available with PgHdr->nRef==0
3374**
3375** (3) We are not in an in-memory database
3376**
3377** (4) Either there is an available PgHdr that does not need
3378** to be synced to disk or else disk syncing is currently
3379** allowed.
danielk197724168722007-04-02 05:07:47 +00003380*/
3381static int pagerAllocatePage(Pager *pPager, PgHdr **ppPg){
3382 int rc = SQLITE_OK;
3383 PgHdr *pPg;
drhbf4bca52007-09-06 22:19:14 +00003384 void *pData;
danielk197724168722007-04-02 05:07:47 +00003385
danielk1977e180dd92007-04-05 17:15:52 +00003386 /* Create a new PgHdr if any of the four conditions defined
danielk19772a6bdf62007-08-20 16:07:00 +00003387 ** above are met: */
danielk1977e180dd92007-04-05 17:15:52 +00003388 if( pPager->nPage<pPager->mxPage
danielk19779f61c2f2007-08-27 17:27:49 +00003389 || pPager->lru.pFirst==0
danielk1977e180dd92007-04-05 17:15:52 +00003390 || MEMDB
danielk19779f61c2f2007-08-27 17:27:49 +00003391 || (pPager->lru.pFirstSynced==0 && pPager->doNotSync)
danielk1977e180dd92007-04-05 17:15:52 +00003392 ){
danielk197724168722007-04-02 05:07:47 +00003393 if( pPager->nPage>=pPager->nHash ){
3394 pager_resize_hash_table(pPager,
3395 pPager->nHash<256 ? 256 : pPager->nHash*2);
3396 if( pPager->nHash==0 ){
3397 rc = SQLITE_NOMEM;
3398 goto pager_allocate_out;
3399 }
3400 }
drhed138fb2007-08-22 22:04:37 +00003401 pagerLeave(pPager);
drhbf4bca52007-09-06 22:19:14 +00003402 pPg = sqlite3_malloc( sizeof(*pPg) + sizeof(u32) + pPager->nExtra
danielk197724168722007-04-02 05:07:47 +00003403 + MEMDB*sizeof(PgHistory) );
drhbf4bca52007-09-06 22:19:14 +00003404 if( pPg ){
3405 pData = sqlite3_malloc( pPager->pageSize );
3406 if( pData==0 ){
3407 sqlite3_free(pPg);
3408 pPg = 0;
3409 }
3410 }
drhed138fb2007-08-22 22:04:37 +00003411 pagerEnter(pPager);
danielk197724168722007-04-02 05:07:47 +00003412 if( pPg==0 ){
3413 rc = SQLITE_NOMEM;
3414 goto pager_allocate_out;
3415 }
3416 memset(pPg, 0, sizeof(*pPg));
3417 if( MEMDB ){
3418 memset(PGHDR_TO_HIST(pPg, pPager), 0, sizeof(PgHistory));
3419 }
drhbf4bca52007-09-06 22:19:14 +00003420 pPg->pData = pData;
danielk197724168722007-04-02 05:07:47 +00003421 pPg->pPager = pPager;
3422 pPg->pNextAll = pPager->pAll;
3423 pPager->pAll = pPg;
3424 pPager->nPage++;
danielk197724168722007-04-02 05:07:47 +00003425 }else{
3426 /* Recycle an existing page with a zero ref-count. */
danielk1977843e65f2007-09-01 16:16:15 +00003427 rc = pager_recycle(pPager, &pPg);
danielk1977e965ac72007-06-13 15:22:28 +00003428 if( rc==SQLITE_BUSY ){
3429 rc = SQLITE_IOERR_BLOCKED;
3430 }
danielk197724168722007-04-02 05:07:47 +00003431 if( rc!=SQLITE_OK ){
3432 goto pager_allocate_out;
3433 }
3434 assert( pPager->state>=SHARED_LOCK );
3435 assert(pPg);
3436 }
3437 *ppPg = pPg;
3438
3439pager_allocate_out:
3440 return rc;
3441}
3442
3443/*
drhd33d5a82007-04-26 12:11:28 +00003444** Make sure we have the content for a page. If the page was
3445** previously acquired with noContent==1, then the content was
3446** just initialized to zeros instead of being read from disk.
3447** But now we need the real data off of disk. So make sure we
3448** have it. Read it in if we do not have it already.
3449*/
3450static int pager_get_content(PgHdr *pPg){
3451 if( pPg->needRead ){
3452 int rc = readDbPage(pPg->pPager, pPg, pPg->pgno);
3453 if( rc==SQLITE_OK ){
3454 pPg->needRead = 0;
3455 }else{
3456 return rc;
3457 }
3458 }
3459 return SQLITE_OK;
3460}
3461
3462/*
drhd9b02572001-04-15 00:37:09 +00003463** Acquire a page.
3464**
drh58a11682001-11-10 13:51:08 +00003465** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00003466** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00003467**
drhd33d5a82007-04-26 12:11:28 +00003468** This routine works for any page number greater than 0. If the database
drh306dc212001-05-21 13:45:10 +00003469** file is smaller than the requested page, then no actual disk
3470** read occurs and the memory image of the page is initialized to
3471** all zeros. The extra data appended to a page is always initialized
3472** to zeros the first time a page is loaded into memory.
3473**
drhd9b02572001-04-15 00:37:09 +00003474** The acquisition might fail for several reasons. In all cases,
3475** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00003476**
drhd33d5a82007-04-26 12:11:28 +00003477** See also sqlite3PagerLookup(). Both this routine and Lookup() attempt
drh7e3b0a02001-04-28 16:52:40 +00003478** to find a page in the in-memory cache first. If the page is not already
drhd33d5a82007-04-26 12:11:28 +00003479** in memory, this routine goes to disk to read it in whereas Lookup()
drh7e3b0a02001-04-28 16:52:40 +00003480** just returns 0. This routine acquires a read-lock the first time it
3481** has to go to disk, and could also playback an old journal if necessary.
drhd33d5a82007-04-26 12:11:28 +00003482** Since Lookup() never goes to disk, it never has to deal with locks
drh7e3b0a02001-04-28 16:52:40 +00003483** or journal files.
drh0787db62007-03-04 13:15:27 +00003484**
drh538f5702007-04-13 02:14:30 +00003485** If noContent is false, the page contents are actually read from disk.
3486** If noContent is true, it means that we do not care about the contents
3487** of the page at this time, so do not do a disk read. Just fill in the
3488** page content with zeros. But mark the fact that we have not read the
3489** content by setting the PgHdr.needRead flag. Later on, if
drhd33d5a82007-04-26 12:11:28 +00003490** sqlite3PagerWrite() is called on this page or if this routine is
3491** called again with noContent==0, that means that the content is needed
3492** and the disk read should occur at that point.
drhed7c8552001-04-11 14:29:21 +00003493*/
drh86f8c192007-08-22 00:39:19 +00003494static int pagerAcquire(
drh538f5702007-04-13 02:14:30 +00003495 Pager *pPager, /* The pager open on the database file */
3496 Pgno pgno, /* Page number to fetch */
3497 DbPage **ppPage, /* Write a pointer to the page here */
3498 int noContent /* Do not bother reading content from disk if true */
3499){
drhed7c8552001-04-11 14:29:21 +00003500 PgHdr *pPg;
drh165ffe92005-03-15 17:09:30 +00003501 int rc;
drhed7c8552001-04-11 14:29:21 +00003502
danielk1977e277be02007-03-23 18:12:06 +00003503 assert( pPager->state==PAGER_UNLOCK || pPager->nRef>0 || pgno==1 );
3504
danielk197726836652005-01-17 01:33:13 +00003505 /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
3506 ** number greater than this, or zero, is requested.
3507 */
drh267cb322005-09-16 17:16:52 +00003508 if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
drh49285702005-09-17 15:20:26 +00003509 return SQLITE_CORRUPT_BKPT;
danielk197726836652005-01-17 01:33:13 +00003510 }
3511
drhd9b02572001-04-15 00:37:09 +00003512 /* Make sure we have not hit any critical errors.
3513 */
drh836faa42003-01-11 13:30:57 +00003514 assert( pPager!=0 );
drh2e6d11b2003-04-25 15:37:57 +00003515 *ppPage = 0;
danielk1977efaaf572006-01-16 11:29:19 +00003516 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
3517 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003518 }
3519
danielk197713adf8a2004-06-03 16:08:41 +00003520 /* If this is the first page accessed, then get a SHARED lock
danielk1977334cdb62007-03-26 08:05:12 +00003521 ** on the database file. pagerSharedLock() is a no-op if
3522 ** a database lock is already held.
drhed7c8552001-04-11 14:29:21 +00003523 */
danielk1977e277be02007-03-23 18:12:06 +00003524 rc = pagerSharedLock(pPager);
3525 if( rc!=SQLITE_OK ){
3526 return rc;
drhed7c8552001-04-11 14:29:21 +00003527 }
danielk1977e277be02007-03-23 18:12:06 +00003528 assert( pPager->state!=PAGER_UNLOCK );
3529
3530 pPg = pager_lookup(pPager, pgno);
drhed7c8552001-04-11 14:29:21 +00003531 if( pPg==0 ){
drhd9b02572001-04-15 00:37:09 +00003532 /* The requested page is not in the page cache. */
danielk1977979f38e2007-03-27 16:19:51 +00003533 int nMax;
drhed7c8552001-04-11 14:29:21 +00003534 int h;
drh538f5702007-04-13 02:14:30 +00003535 PAGER_INCR(pPager->nMiss);
danielk197724168722007-04-02 05:07:47 +00003536 rc = pagerAllocatePage(pPager, &pPg);
3537 if( rc!=SQLITE_OK ){
3538 return rc;
drhed7c8552001-04-11 14:29:21 +00003539 }
danielk197724168722007-04-02 05:07:47 +00003540
drhed7c8552001-04-11 14:29:21 +00003541 pPg->pgno = pgno;
danielk1977f35843b2007-04-07 15:03:17 +00003542 assert( !MEMDB || pgno>pPager->stmtSize );
drh1ab43002002-01-14 09:28:19 +00003543 if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){
danielk19771e536952007-08-16 10:09:01 +00003544#if 0
danielk19774adee202004-05-08 08:23:19 +00003545 sqlite3CheckMemory(pPager->aInJournal, pgno/8);
danielk19771e536952007-08-16 10:09:01 +00003546#endif
drhdb48ee02003-01-16 13:42:43 +00003547 assert( pPager->journalOpen );
drh6019e162001-07-02 17:51:45 +00003548 pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0;
drhdb48ee02003-01-16 13:42:43 +00003549 pPg->needSync = 0;
drh6019e162001-07-02 17:51:45 +00003550 }else{
3551 pPg->inJournal = 0;
drhdb48ee02003-01-16 13:42:43 +00003552 pPg->needSync = 0;
drh6019e162001-07-02 17:51:45 +00003553 }
danielk1977f35843b2007-04-07 15:03:17 +00003554
drh605ad8f2006-05-03 23:34:05 +00003555 makeClean(pPg);
drhed7c8552001-04-11 14:29:21 +00003556 pPg->nRef = 1;
drhdd793422001-06-28 01:54:48 +00003557 REFINFO(pPg);
danielk197775bab7d2006-01-23 13:09:45 +00003558
drhd9b02572001-04-15 00:37:09 +00003559 pPager->nRef++;
drh2e6d11b2003-04-25 15:37:57 +00003560 if( pPager->nExtra>0 ){
drh90f5ecb2004-07-22 01:19:35 +00003561 memset(PGHDR_TO_EXTRA(pPg, pPager), 0, pPager->nExtra);
drh2e6d11b2003-04-25 15:37:57 +00003562 }
danielk1977979f38e2007-03-27 16:19:51 +00003563 nMax = sqlite3PagerPagecount(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00003564 if( pPager->errCode ){
danielk19773b8a05f2007-03-19 17:44:26 +00003565 sqlite3PagerUnref(pPg);
danielk1977efaaf572006-01-16 11:29:19 +00003566 rc = pPager->errCode;
drh2e6d11b2003-04-25 15:37:57 +00003567 return rc;
3568 }
danielk197775bab7d2006-01-23 13:09:45 +00003569
3570 /* Populate the page with data, either by reading from the database
3571 ** file, or by setting the entire page to zero.
3572 */
drhd215acf2007-04-13 04:01:58 +00003573 if( nMax<(int)pgno || MEMDB || (noContent && !pPager->alwaysRollback) ){
drhf8e632b2007-05-08 14:51:36 +00003574 if( pgno>pPager->mxPgno ){
danielk1977de3bea72007-05-09 15:56:39 +00003575 sqlite3PagerUnref(pPg);
drhf8e632b2007-05-08 14:51:36 +00003576 return SQLITE_FULL;
3577 }
drh90f5ecb2004-07-22 01:19:35 +00003578 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
drhd215acf2007-04-13 04:01:58 +00003579 pPg->needRead = noContent && !pPager->alwaysRollback;
drh538f5702007-04-13 02:14:30 +00003580 IOTRACE(("ZERO %p %d\n", pPager, pgno));
drh306dc212001-05-21 13:45:10 +00003581 }else{
danielk1977e180dd92007-04-05 17:15:52 +00003582 rc = readDbPage(pPager, pPg, pgno);
drh551b7732006-11-06 21:20:25 +00003583 if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
3584 pPg->pgno = 0;
danielk19773b8a05f2007-03-19 17:44:26 +00003585 sqlite3PagerUnref(pPg);
drh551b7732006-11-06 21:20:25 +00003586 return rc;
drh81a20f22001-10-12 17:30:04 +00003587 }
danielk1977b4626a32007-04-28 15:47:43 +00003588 pPg->needRead = 0;
drh306dc212001-05-21 13:45:10 +00003589 }
danielk197775bab7d2006-01-23 13:09:45 +00003590
3591 /* Link the page into the page hash table */
drh8ca0c722006-05-07 17:49:38 +00003592 h = pgno & (pPager->nHash-1);
drh3765df42006-06-28 18:18:09 +00003593 assert( pgno!=0 );
danielk197775bab7d2006-01-23 13:09:45 +00003594 pPg->pNextHash = pPager->aHash[h];
3595 pPager->aHash[h] = pPg;
3596 if( pPg->pNextHash ){
3597 assert( pPg->pNextHash->pPrevHash==0 );
3598 pPg->pNextHash->pPrevHash = pPg;
3599 }
3600
danielk19773c407372005-02-15 02:54:14 +00003601#ifdef SQLITE_CHECK_PAGES
3602 pPg->pageHash = pager_pagehash(pPg);
3603#endif
drhed7c8552001-04-11 14:29:21 +00003604 }else{
drhd9b02572001-04-15 00:37:09 +00003605 /* The requested page is in the page cache. */
danielk1977e277be02007-03-23 18:12:06 +00003606 assert(pPager->nRef>0 || pgno==1);
drh538f5702007-04-13 02:14:30 +00003607 PAGER_INCR(pPager->nHit);
drhd33d5a82007-04-26 12:11:28 +00003608 if( !noContent ){
3609 rc = pager_get_content(pPg);
3610 if( rc ){
3611 return rc;
3612 }
3613 }
drhdf0b3b02001-06-23 11:36:20 +00003614 page_ref(pPg);
drhed7c8552001-04-11 14:29:21 +00003615 }
danielk19773b8a05f2007-03-19 17:44:26 +00003616 *ppPage = pPg;
drhed7c8552001-04-11 14:29:21 +00003617 return SQLITE_OK;
3618}
drh86f8c192007-08-22 00:39:19 +00003619int sqlite3PagerAcquire(
3620 Pager *pPager, /* The pager open on the database file */
3621 Pgno pgno, /* Page number to fetch */
3622 DbPage **ppPage, /* Write a pointer to the page here */
3623 int noContent /* Do not bother reading content from disk if true */
3624){
3625 int rc;
3626 pagerEnter(pPager);
3627 rc = pagerAcquire(pPager, pgno, ppPage, noContent);
3628 pagerLeave(pPager);
3629 return rc;
3630}
3631
drhed7c8552001-04-11 14:29:21 +00003632
3633/*
drh7e3b0a02001-04-28 16:52:40 +00003634** Acquire a page if it is already in the in-memory cache. Do
3635** not read the page from disk. Return a pointer to the page,
3636** or 0 if the page is not in cache.
3637**
danielk19773b8a05f2007-03-19 17:44:26 +00003638** See also sqlite3PagerGet(). The difference between this routine
3639** and sqlite3PagerGet() is that _get() will go to the disk and read
drh7e3b0a02001-04-28 16:52:40 +00003640** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00003641** returns NULL if the page is not in cache or if a disk I/O error
3642** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00003643*/
danielk19773b8a05f2007-03-19 17:44:26 +00003644DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
drh86f8c192007-08-22 00:39:19 +00003645 PgHdr *pPg = 0;
drh7e3b0a02001-04-28 16:52:40 +00003646
drh836faa42003-01-11 13:30:57 +00003647 assert( pPager!=0 );
3648 assert( pgno!=0 );
danielk1977e277be02007-03-23 18:12:06 +00003649
drh86f8c192007-08-22 00:39:19 +00003650 pagerEnter(pPager);
danielk1977e277be02007-03-23 18:12:06 +00003651 if( pPager->state==PAGER_UNLOCK ){
danielk1977334cdb62007-03-26 08:05:12 +00003652 assert( !pPager->pAll || pPager->exclusiveMode );
drh86f8c192007-08-22 00:39:19 +00003653 }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
3654 /* Do nothing */
3655 }else if( (pPg = pager_lookup(pPager, pgno))!=0 ){
3656 page_ref(pPg);
danielk1977e277be02007-03-23 18:12:06 +00003657 }
drh86f8c192007-08-22 00:39:19 +00003658 pagerLeave(pPager);
danielk19773b8a05f2007-03-19 17:44:26 +00003659 return pPg;
drh7e3b0a02001-04-28 16:52:40 +00003660}
3661
3662/*
drhed7c8552001-04-11 14:29:21 +00003663** Release a page.
3664**
3665** If the number of references to the page drop to zero, then the
3666** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00003667** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00003668** removed.
3669*/
danielk19773b8a05f2007-03-19 17:44:26 +00003670int sqlite3PagerUnref(DbPage *pPg){
drhd9b02572001-04-15 00:37:09 +00003671
3672 /* Decrement the reference count for this page
3673 */
drhed7c8552001-04-11 14:29:21 +00003674 assert( pPg->nRef>0 );
drh86f8c192007-08-22 00:39:19 +00003675 pagerEnter(pPg->pPager);
drhed7c8552001-04-11 14:29:21 +00003676 pPg->nRef--;
drhdd793422001-06-28 01:54:48 +00003677 REFINFO(pPg);
drhd9b02572001-04-15 00:37:09 +00003678
danielk19773c407372005-02-15 02:54:14 +00003679 CHECK_PAGE(pPg);
3680
drh72f82862001-05-24 21:06:34 +00003681 /* When the number of references to a page reach 0, call the
3682 ** destructor and add the page to the freelist.
drhd9b02572001-04-15 00:37:09 +00003683 */
drhed7c8552001-04-11 14:29:21 +00003684 if( pPg->nRef==0 ){
danielk19779f61c2f2007-08-27 17:27:49 +00003685 Pager *pPager = pPg->pPager;
3686
3687 lruListAdd(pPg);
drh72f82862001-05-24 21:06:34 +00003688 if( pPager->xDestructor ){
danielk19773b8a05f2007-03-19 17:44:26 +00003689 pPager->xDestructor(pPg, pPager->pageSize);
drh72f82862001-05-24 21:06:34 +00003690 }
drhd9b02572001-04-15 00:37:09 +00003691
3692 /* When all pages reach the freelist, drop the read lock from
3693 ** the database file.
3694 */
3695 pPager->nRef--;
3696 assert( pPager->nRef>=0 );
danielk19773dedc192007-03-27 17:37:31 +00003697 if( pPager->nRef==0 && (!pPager->exclusiveMode || pPager->journalOff>0) ){
danielk1977e277be02007-03-23 18:12:06 +00003698 pagerUnlockAndRollback(pPager);
drhd9b02572001-04-15 00:37:09 +00003699 }
drhed7c8552001-04-11 14:29:21 +00003700 }
drh86f8c192007-08-22 00:39:19 +00003701 pagerLeave(pPg->pPager);
drhd9b02572001-04-15 00:37:09 +00003702 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00003703}
3704
3705/*
drha6abd042004-06-09 17:37:22 +00003706** Create a journal file for pPager. There should already be a RESERVED
3707** or EXCLUSIVE lock on the database file when this routine is called.
drhda47d772002-12-02 04:25:19 +00003708**
3709** Return SQLITE_OK if everything. Return an error code and release the
3710** write lock if anything goes wrong.
3711*/
3712static int pager_open_journal(Pager *pPager){
danielk1977b4b47412007-08-17 15:53:36 +00003713 sqlite3_vfs *pVfs = pPager->pVfs;
3714 int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE);
3715
drhda47d772002-12-02 04:25:19 +00003716 int rc;
drhb7f91642004-10-31 02:22:47 +00003717 assert( !MEMDB );
drha6abd042004-06-09 17:37:22 +00003718 assert( pPager->state>=PAGER_RESERVED );
drhda47d772002-12-02 04:25:19 +00003719 assert( pPager->journalOpen==0 );
3720 assert( pPager->useJournal );
drh88b01a12005-03-28 18:04:27 +00003721 assert( pPager->aInJournal==0 );
danielk19773b8a05f2007-03-19 17:44:26 +00003722 sqlite3PagerPagecount(pPager);
drhed138fb2007-08-22 22:04:37 +00003723 pagerLeave(pPager);
drh17435752007-08-16 04:30:38 +00003724 pPager->aInJournal = sqlite3MallocZero( pPager->dbSize/8 + 1 );
drhed138fb2007-08-22 22:04:37 +00003725 pagerEnter(pPager);
drhda47d772002-12-02 04:25:19 +00003726 if( pPager->aInJournal==0 ){
drh9c105bb2004-10-02 20:38:28 +00003727 rc = SQLITE_NOMEM;
3728 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003729 }
danielk1977b4b47412007-08-17 15:53:36 +00003730
3731 if( pPager->tempFile ){
danielk1977fee2d252007-08-18 10:59:19 +00003732 flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
3733 }else{
3734 flags |= (SQLITE_OPEN_MAIN_JOURNAL);
danielk1977b4b47412007-08-17 15:53:36 +00003735 }
danielk1977c7b60172007-08-22 11:22:03 +00003736#ifdef SQLITE_ENABLE_ATOMIC_WRITE
3737 rc = sqlite3JournalOpen(
3738 pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
3739 );
3740#else
danielk1977b4b47412007-08-17 15:53:36 +00003741 rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
danielk1977c7b60172007-08-22 11:22:03 +00003742#endif
danielk1977b4b47412007-08-17 15:53:36 +00003743 assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
danielk197776572402004-06-25 02:38:54 +00003744 pPager->journalOff = 0;
3745 pPager->setMaster = 0;
3746 pPager->journalHdr = 0;
drhda47d772002-12-02 04:25:19 +00003747 if( rc!=SQLITE_OK ){
drh600e46a2007-03-28 01:59:33 +00003748 if( rc==SQLITE_NOMEM ){
danielk1977fee2d252007-08-18 10:59:19 +00003749 sqlite3OsDelete(pVfs, pPager->zJournal, 0);
drh600e46a2007-03-28 01:59:33 +00003750 }
drh9c105bb2004-10-02 20:38:28 +00003751 goto failed_to_open_journal;
drhda47d772002-12-02 04:25:19 +00003752 }
3753 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00003754 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00003755 pPager->needSync = 0;
3756 pPager->alwaysRollback = 0;
drh968af522003-02-11 14:55:40 +00003757 pPager->nRec = 0;
danielk1977efaaf572006-01-16 11:29:19 +00003758 if( pPager->errCode ){
3759 rc = pPager->errCode;
drhdd5b2fa2005-03-28 03:39:55 +00003760 goto failed_to_open_journal;
drh2e6d11b2003-04-25 15:37:57 +00003761 }
drhda47d772002-12-02 04:25:19 +00003762 pPager->origDbSize = pPager->dbSize;
drhae2b40c2004-06-09 19:03:54 +00003763
danielk197776572402004-06-25 02:38:54 +00003764 rc = writeJournalHdr(pPager);
3765
drhac69b052004-05-12 13:30:07 +00003766 if( pPager->stmtAutoopen && rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00003767 rc = sqlite3PagerStmtBegin(pPager);
drhda47d772002-12-02 04:25:19 +00003768 }
danielk1977261919c2005-12-06 12:52:59 +00003769 if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM ){
drh80e35f42007-03-30 14:06:34 +00003770 rc = pager_end_transaction(pPager);
drhda47d772002-12-02 04:25:19 +00003771 if( rc==SQLITE_OK ){
3772 rc = SQLITE_FULL;
3773 }
3774 }
drh9c105bb2004-10-02 20:38:28 +00003775 return rc;
3776
3777failed_to_open_journal:
drh17435752007-08-16 04:30:38 +00003778 sqlite3_free(pPager->aInJournal);
drh9c105bb2004-10-02 20:38:28 +00003779 pPager->aInJournal = 0;
drh9c105bb2004-10-02 20:38:28 +00003780 return rc;
drhda47d772002-12-02 04:25:19 +00003781}
3782
3783/*
drh4b845d72002-03-05 12:41:19 +00003784** Acquire a write-lock on the database. The lock is removed when
3785** the any of the following happen:
3786**
drh80e35f42007-03-30 14:06:34 +00003787** * sqlite3PagerCommitPhaseTwo() is called.
danielk19773b8a05f2007-03-19 17:44:26 +00003788** * sqlite3PagerRollback() is called.
3789** * sqlite3PagerClose() is called.
3790** * sqlite3PagerUnref() is called to on every outstanding page.
drh4b845d72002-03-05 12:41:19 +00003791**
danielk197713adf8a2004-06-03 16:08:41 +00003792** The first parameter to this routine is a pointer to any open page of the
3793** database file. Nothing changes about the page - it is used merely to
3794** acquire a pointer to the Pager structure and as proof that there is
3795** already a read-lock on the database.
drh4b845d72002-03-05 12:41:19 +00003796**
danielk197713adf8a2004-06-03 16:08:41 +00003797** The second parameter indicates how much space in bytes to reserve for a
3798** master journal file-name at the start of the journal when it is created.
3799**
3800** A journal file is opened if this is not a temporary file. For temporary
3801** files, the opening of the journal file is deferred until there is an
3802** actual need to write to the journal.
drhda47d772002-12-02 04:25:19 +00003803**
drha6abd042004-06-09 17:37:22 +00003804** If the database is already reserved for writing, this routine is a no-op.
drh684917c2004-10-05 02:41:42 +00003805**
3806** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file
3807** immediately instead of waiting until we try to flush the cache. The
3808** exFlag is ignored if a transaction is already active.
drh4b845d72002-03-05 12:41:19 +00003809*/
danielk19773b8a05f2007-03-19 17:44:26 +00003810int sqlite3PagerBegin(DbPage *pPg, int exFlag){
drh4b845d72002-03-05 12:41:19 +00003811 Pager *pPager = pPg->pPager;
3812 int rc = SQLITE_OK;
drh86f8c192007-08-22 00:39:19 +00003813 pagerEnter(pPager);
drh4b845d72002-03-05 12:41:19 +00003814 assert( pPg->nRef>0 );
drha6abd042004-06-09 17:37:22 +00003815 assert( pPager->state!=PAGER_UNLOCK );
3816 if( pPager->state==PAGER_SHARED ){
drh4b845d72002-03-05 12:41:19 +00003817 assert( pPager->aInJournal==0 );
drhb7f91642004-10-31 02:22:47 +00003818 if( MEMDB ){
drha6abd042004-06-09 17:37:22 +00003819 pPager->state = PAGER_EXCLUSIVE;
drhac69b052004-05-12 13:30:07 +00003820 pPager->origDbSize = pPager->dbSize;
3821 }else{
drh054889e2005-11-30 03:20:31 +00003822 rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
drh684917c2004-10-05 02:41:42 +00003823 if( rc==SQLITE_OK ){
3824 pPager->state = PAGER_RESERVED;
3825 if( exFlag ){
3826 rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
3827 }
3828 }
danielk19779eed5052004-06-04 10:38:30 +00003829 if( rc!=SQLITE_OK ){
drh86f8c192007-08-22 00:39:19 +00003830 pagerLeave(pPager);
danielk19779eed5052004-06-04 10:38:30 +00003831 return rc;
drhac69b052004-05-12 13:30:07 +00003832 }
drha6abd042004-06-09 17:37:22 +00003833 pPager->dirtyCache = 0;
drh4f0c5872007-03-26 22:05:01 +00003834 PAGERTRACE2("TRANSACTION %d\n", PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00003835 if( pPager->useJournal && !pPager->tempFile ){
3836 rc = pager_open_journal(pPager);
3837 }
drh4b845d72002-03-05 12:41:19 +00003838 }
danielk1977334cdb62007-03-26 08:05:12 +00003839 }else if( pPager->journalOpen && pPager->journalOff==0 ){
3840 /* This happens when the pager was in exclusive-access mode last
3841 ** time a (read or write) transaction was successfully concluded
3842 ** by this connection. Instead of deleting the journal file it was
3843 ** kept open and truncated to 0 bytes.
3844 */
3845 assert( pPager->nRec==0 );
3846 assert( pPager->origDbSize==0 );
drh369339d2007-03-30 16:01:55 +00003847 assert( pPager->aInJournal==0 );
danielk1977334cdb62007-03-26 08:05:12 +00003848 sqlite3PagerPagecount(pPager);
drhed138fb2007-08-22 22:04:37 +00003849 pagerLeave(pPager);
drh17435752007-08-16 04:30:38 +00003850 pPager->aInJournal = sqlite3MallocZero( pPager->dbSize/8 + 1 );
drhed138fb2007-08-22 22:04:37 +00003851 pagerEnter(pPager);
danielk1977334cdb62007-03-26 08:05:12 +00003852 if( !pPager->aInJournal ){
3853 rc = SQLITE_NOMEM;
3854 }else{
drh369339d2007-03-30 16:01:55 +00003855 pPager->origDbSize = pPager->dbSize;
danielk1977334cdb62007-03-26 08:05:12 +00003856 rc = writeJournalHdr(pPager);
3857 }
drh4b845d72002-03-05 12:41:19 +00003858 }
danielk1977334cdb62007-03-26 08:05:12 +00003859 assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK );
drh86f8c192007-08-22 00:39:19 +00003860 pagerLeave(pPager);
drh4b845d72002-03-05 12:41:19 +00003861 return rc;
3862}
3863
3864/*
drh605ad8f2006-05-03 23:34:05 +00003865** Make a page dirty. Set its dirty flag and add it to the dirty
3866** page list.
3867*/
3868static void makeDirty(PgHdr *pPg){
3869 if( pPg->dirty==0 ){
3870 Pager *pPager = pPg->pPager;
3871 pPg->dirty = 1;
3872 pPg->pDirty = pPager->pDirty;
3873 if( pPager->pDirty ){
3874 pPager->pDirty->pPrevDirty = pPg;
3875 }
3876 pPg->pPrevDirty = 0;
3877 pPager->pDirty = pPg;
3878 }
3879}
3880
3881/*
3882** Make a page clean. Clear its dirty bit and remove it from the
3883** dirty page list.
3884*/
3885static void makeClean(PgHdr *pPg){
3886 if( pPg->dirty ){
3887 pPg->dirty = 0;
3888 if( pPg->pDirty ){
drh153c62c2007-08-24 03:51:33 +00003889 assert( pPg->pDirty->pPrevDirty==pPg );
drh605ad8f2006-05-03 23:34:05 +00003890 pPg->pDirty->pPrevDirty = pPg->pPrevDirty;
3891 }
3892 if( pPg->pPrevDirty ){
drh153c62c2007-08-24 03:51:33 +00003893 assert( pPg->pPrevDirty->pDirty==pPg );
drh605ad8f2006-05-03 23:34:05 +00003894 pPg->pPrevDirty->pDirty = pPg->pDirty;
3895 }else{
drh153c62c2007-08-24 03:51:33 +00003896 assert( pPg->pPager->pDirty==pPg );
drh605ad8f2006-05-03 23:34:05 +00003897 pPg->pPager->pDirty = pPg->pDirty;
3898 }
3899 }
3900}
3901
3902
3903/*
drhed7c8552001-04-11 14:29:21 +00003904** Mark a data page as writeable. The page is written into the journal
3905** if it is not there already. This routine must be called before making
3906** changes to a page.
3907**
3908** The first time this routine is called, the pager creates a new
drha6abd042004-06-09 17:37:22 +00003909** journal and acquires a RESERVED lock on the database. If the RESERVED
drhed7c8552001-04-11 14:29:21 +00003910** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00003911** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00003912** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00003913**
3914** If the journal file could not be written because the disk is full,
3915** then this routine returns SQLITE_FULL and does an immediate rollback.
3916** All subsequent write attempts also return SQLITE_FULL until there
danielk19773b8a05f2007-03-19 17:44:26 +00003917** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to
drhd9b02572001-04-15 00:37:09 +00003918** reset.
drhed7c8552001-04-11 14:29:21 +00003919*/
danielk19773b8a05f2007-03-19 17:44:26 +00003920static int pager_write(PgHdr *pPg){
3921 void *pData = PGHDR_TO_DATA(pPg);
drh69688d52001-04-14 16:38:23 +00003922 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00003923 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00003924
drh6446c4d2001-12-15 14:22:18 +00003925 /* Check for errors
3926 */
danielk1977efaaf572006-01-16 11:29:19 +00003927 if( pPager->errCode ){
3928 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00003929 }
drh5e00f6c2001-09-13 13:46:56 +00003930 if( pPager->readOnly ){
3931 return SQLITE_PERM;
3932 }
drh6446c4d2001-12-15 14:22:18 +00003933
danielk197776572402004-06-25 02:38:54 +00003934 assert( !pPager->setMaster );
3935
danielk19773c407372005-02-15 02:54:14 +00003936 CHECK_PAGE(pPg);
3937
drh538f5702007-04-13 02:14:30 +00003938 /* If this page was previously acquired with noContent==1, that means
3939 ** we didn't really read in the content of the page. This can happen
3940 ** (for example) when the page is being moved to the freelist. But
3941 ** now we are (perhaps) moving the page off of the freelist for
3942 ** reuse and we need to know its original content so that content
3943 ** can be stored in the rollback journal. So do the read at this
3944 ** time.
3945 */
drhd33d5a82007-04-26 12:11:28 +00003946 rc = pager_get_content(pPg);
3947 if( rc ){
3948 return rc;
drh538f5702007-04-13 02:14:30 +00003949 }
3950
drh6446c4d2001-12-15 14:22:18 +00003951 /* Mark the page as dirty. If the page has already been written
3952 ** to the journal then we can return right away.
3953 */
drh605ad8f2006-05-03 23:34:05 +00003954 makeDirty(pPg);
danielk1977f35843b2007-04-07 15:03:17 +00003955 if( pPg->inJournal && (pageInStatement(pPg) || pPager->stmtInUse==0) ){
drha6abd042004-06-09 17:37:22 +00003956 pPager->dirtyCache = 1;
danielk1977a0bf2652004-11-04 14:30:04 +00003957 }else{
drh6446c4d2001-12-15 14:22:18 +00003958
danielk1977a0bf2652004-11-04 14:30:04 +00003959 /* If we get this far, it means that the page needs to be
3960 ** written to the transaction journal or the ckeckpoint journal
3961 ** or both.
3962 **
3963 ** First check to see that the transaction journal exists and
3964 ** create it if it does not.
3965 */
3966 assert( pPager->state!=PAGER_UNLOCK );
danielk19773b8a05f2007-03-19 17:44:26 +00003967 rc = sqlite3PagerBegin(pPg, 0);
danielk1977a0bf2652004-11-04 14:30:04 +00003968 if( rc!=SQLITE_OK ){
3969 return rc;
3970 }
3971 assert( pPager->state>=PAGER_RESERVED );
3972 if( !pPager->journalOpen && pPager->useJournal ){
3973 rc = pager_open_journal(pPager);
3974 if( rc!=SQLITE_OK ) return rc;
3975 }
3976 assert( pPager->journalOpen || !pPager->useJournal );
3977 pPager->dirtyCache = 1;
3978
3979 /* The transaction journal now exists and we have a RESERVED or an
3980 ** EXCLUSIVE lock on the main database file. Write the current page to
3981 ** the transaction journal if it is not there already.
3982 */
3983 if( !pPg->inJournal && (pPager->useJournal || MEMDB) ){
3984 if( (int)pPg->pgno <= pPager->origDbSize ){
danielk1977a0bf2652004-11-04 14:30:04 +00003985 if( MEMDB ){
3986 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
drh4f0c5872007-03-26 22:05:01 +00003987 PAGERTRACE3("JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk1977a0bf2652004-11-04 14:30:04 +00003988 assert( pHist->pOrig==0 );
drh17435752007-08-16 04:30:38 +00003989 pHist->pOrig = sqlite3_malloc( pPager->pageSize );
danielk1977a0bf2652004-11-04 14:30:04 +00003990 if( pHist->pOrig ){
3991 memcpy(pHist->pOrig, PGHDR_TO_DATA(pPg), pPager->pageSize);
3992 }
3993 }else{
drhbf4bca52007-09-06 22:19:14 +00003994 u32 cksum;
3995 char *pData2;
danielk1977dd97a492007-08-22 18:54:32 +00003996
drh267cb322005-09-16 17:16:52 +00003997 /* We should never write to the journal file the page that
3998 ** contains the database locks. The following assert verifies
3999 ** that we do not. */
4000 assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
drhc001c582006-03-06 18:23:16 +00004001 pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
drh37527852006-03-16 16:19:56 +00004002 cksum = pager_cksum(pPager, (u8*)pData2);
drhbf4bca52007-09-06 22:19:14 +00004003 rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno);
4004 if( rc==SQLITE_OK ){
4005 rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize,
4006 pPager->journalOff + 4);
4007 pPager->journalOff += pPager->pageSize+4;
4008 }
4009 if( rc==SQLITE_OK ){
4010 rc = write32bits(pPager->jfd, pPager->journalOff, cksum);
4011 pPager->journalOff += 4;
4012 }
4013 IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno,
danielk1977667a6c92007-09-10 06:12:04 +00004014 pPager->journalOff, pPager->pageSize));
drh538f5702007-04-13 02:14:30 +00004015 PAGER_INCR(sqlite3_pager_writej_count);
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));
danielk197707cb5602006-01-20 10:55:05 +00004018
drhfd131da2007-08-07 17:13:03 +00004019 /* An error has occured writing to the journal file. The
danielk197707cb5602006-01-20 10:55:05 +00004020 ** transaction will be rolled back by the layer above.
4021 */
danielk1977a0bf2652004-11-04 14:30:04 +00004022 if( rc!=SQLITE_OK ){
danielk1977a0bf2652004-11-04 14:30:04 +00004023 return rc;
4024 }
danielk197707cb5602006-01-20 10:55:05 +00004025
danielk1977a0bf2652004-11-04 14:30:04 +00004026 pPager->nRec++;
4027 assert( pPager->aInJournal!=0 );
4028 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
4029 pPg->needSync = !pPager->noSync;
4030 if( pPager->stmtInUse ){
4031 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
danielk1977a0bf2652004-11-04 14:30:04 +00004032 }
drhac69b052004-05-12 13:30:07 +00004033 }
danielk197713adf8a2004-06-03 16:08:41 +00004034 }else{
danielk1977a0bf2652004-11-04 14:30:04 +00004035 pPg->needSync = !pPager->journalStarted && !pPager->noSync;
drh4f0c5872007-03-26 22:05:01 +00004036 PAGERTRACE4("APPEND %d page %d needSync=%d\n",
danielk1977ef73ee92004-11-06 12:26:07 +00004037 PAGERID(pPager), pPg->pgno, pPg->needSync);
danielk1977a0bf2652004-11-04 14:30:04 +00004038 }
4039 if( pPg->needSync ){
4040 pPager->needSync = 1;
4041 }
4042 pPg->inJournal = 1;
4043 }
4044
4045 /* If the statement journal is open and the page is not in it,
4046 ** then write the current page to the statement journal. Note that
4047 ** the statement journal format differs from the standard journal format
4048 ** in that it omits the checksums and the header.
4049 */
danielk1977f35843b2007-04-07 15:03:17 +00004050 if( pPager->stmtInUse
4051 && !pageInStatement(pPg)
4052 && (int)pPg->pgno<=pPager->stmtSize
4053 ){
danielk1977a0bf2652004-11-04 14:30:04 +00004054 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
4055 if( MEMDB ){
4056 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4057 assert( pHist->pStmt==0 );
drh17435752007-08-16 04:30:38 +00004058 pHist->pStmt = sqlite3_malloc( pPager->pageSize );
danielk1977a0bf2652004-11-04 14:30:04 +00004059 if( pHist->pStmt ){
4060 memcpy(pHist->pStmt, PGHDR_TO_DATA(pPg), pPager->pageSize);
4061 }
drh4f0c5872007-03-26 22:05:01 +00004062 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
danielk1977f35843b2007-04-07 15:03:17 +00004063 page_add_to_stmt_list(pPg);
danielk1977a0bf2652004-11-04 14:30:04 +00004064 }else{
danielk197762079062007-08-15 17:08:46 +00004065 i64 offset = pPager->stmtNRec*(4+pPager->pageSize);
drhbf4bca52007-09-06 22:19:14 +00004066 char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
4067 rc = write32bits(pPager->stfd, offset, pPg->pgno);
4068 if( rc==SQLITE_OK ){
4069 rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize, offset+4);
4070 }
drh4f0c5872007-03-26 22:05:01 +00004071 PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
drhac69b052004-05-12 13:30:07 +00004072 if( rc!=SQLITE_OK ){
drhac69b052004-05-12 13:30:07 +00004073 return rc;
4074 }
danielk1977a0bf2652004-11-04 14:30:04 +00004075 pPager->stmtNRec++;
4076 assert( pPager->aInStmt!=0 );
4077 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drhdb48ee02003-01-16 13:42:43 +00004078 }
drhd9b02572001-04-15 00:37:09 +00004079 }
drhfa86c412002-02-02 15:01:15 +00004080 }
4081
4082 /* Update the database size and return.
4083 */
drh1aa2d8b2007-01-03 15:34:29 +00004084 assert( pPager->state>=PAGER_SHARED );
drh1ab43002002-01-14 09:28:19 +00004085 if( pPager->dbSize<(int)pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00004086 pPager->dbSize = pPg->pgno;
drhb7f91642004-10-31 02:22:47 +00004087 if( !MEMDB && pPager->dbSize==PENDING_BYTE/pPager->pageSize ){
drh1f595712004-06-15 01:40:29 +00004088 pPager->dbSize++;
4089 }
drh306dc212001-05-21 13:45:10 +00004090 }
drh69688d52001-04-14 16:38:23 +00004091 return rc;
drhed7c8552001-04-11 14:29:21 +00004092}
4093
4094/*
danielk19774099f6e2007-03-19 11:25:20 +00004095** This function is used to mark a data-page as writable. It uses
4096** pager_write() to open a journal file (if it is not already open)
4097** and write the page *pData to the journal.
4098**
4099** The difference between this function and pager_write() is that this
4100** function also deals with the special case where 2 or more pages
4101** fit on a single disk sector. In this case all co-resident pages
4102** must have been written to the journal file before returning.
4103*/
danielk19773b8a05f2007-03-19 17:44:26 +00004104int sqlite3PagerWrite(DbPage *pDbPage){
danielk19774099f6e2007-03-19 11:25:20 +00004105 int rc = SQLITE_OK;
4106
danielk19773b8a05f2007-03-19 17:44:26 +00004107 PgHdr *pPg = pDbPage;
danielk19774099f6e2007-03-19 11:25:20 +00004108 Pager *pPager = pPg->pPager;
4109 Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
4110
drh86f8c192007-08-22 00:39:19 +00004111 pagerEnter(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00004112 if( !MEMDB && nPagePerSector>1 ){
4113 Pgno nPageCount; /* Total number of pages in database file */
4114 Pgno pg1; /* First page of the sector pPg is located on. */
4115 int nPage; /* Number of pages starting at pg1 to journal */
4116 int ii;
danielk1977dd97a492007-08-22 18:54:32 +00004117 int needSync = 0;
danielk19774099f6e2007-03-19 11:25:20 +00004118
4119 /* Set the doNotSync flag to 1. This is because we cannot allow a journal
4120 ** header to be written between the pages journaled by this function.
4121 */
4122 assert( pPager->doNotSync==0 );
4123 pPager->doNotSync = 1;
4124
4125 /* This trick assumes that both the page-size and sector-size are
4126 ** an integer power of 2. It sets variable pg1 to the identifier
4127 ** of the first page of the sector pPg is located on.
4128 */
4129 pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
4130
danielk19773b8a05f2007-03-19 17:44:26 +00004131 nPageCount = sqlite3PagerPagecount(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00004132 if( pPg->pgno>nPageCount ){
4133 nPage = (pPg->pgno - pg1)+1;
4134 }else if( (pg1+nPagePerSector-1)>nPageCount ){
4135 nPage = nPageCount+1-pg1;
4136 }else{
4137 nPage = nPagePerSector;
4138 }
4139 assert(nPage>0);
4140 assert(pg1<=pPg->pgno);
4141 assert((pg1+nPage)>pPg->pgno);
4142
4143 for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
4144 Pgno pg = pg1+ii;
danielk1977dd97a492007-08-22 18:54:32 +00004145 PgHdr *pPage;
danielk19774099f6e2007-03-19 11:25:20 +00004146 if( !pPager->aInJournal || pg==pPg->pgno ||
4147 pg>pPager->origDbSize || !(pPager->aInJournal[pg/8]&(1<<(pg&7)))
4148 ) {
4149 if( pg!=PAGER_MJ_PGNO(pPager) ){
danielk19773b8a05f2007-03-19 17:44:26 +00004150 rc = sqlite3PagerGet(pPager, pg, &pPage);
danielk19774099f6e2007-03-19 11:25:20 +00004151 if( rc==SQLITE_OK ){
4152 rc = pager_write(pPage);
danielk1977dd97a492007-08-22 18:54:32 +00004153 if( pPage->needSync ){
4154 needSync = 1;
4155 }
danielk19773b8a05f2007-03-19 17:44:26 +00004156 sqlite3PagerUnref(pPage);
danielk19774099f6e2007-03-19 11:25:20 +00004157 }
4158 }
danielk1977dd97a492007-08-22 18:54:32 +00004159 }else if( (pPage = pager_lookup(pPager, pg)) ){
4160 if( pPage->needSync ){
4161 needSync = 1;
4162 }
danielk19774099f6e2007-03-19 11:25:20 +00004163 }
4164 }
4165
danielk1977dd97a492007-08-22 18:54:32 +00004166 /* If the PgHdr.needSync flag is set for any of the nPage pages
4167 ** starting at pg1, then it needs to be set for all of them. Because
4168 ** writing to any of these nPage pages may damage the others, the
4169 ** journal file must contain sync()ed copies of all of them
4170 ** before any of them can be written out to the database file.
4171 */
4172 if( needSync ){
4173 for(ii=0; ii<nPage && needSync; ii++){
4174 PgHdr *pPage = pager_lookup(pPager, pg1+ii);
4175 if( pPage ) pPage->needSync = 1;
4176 }
4177 assert(pPager->needSync);
4178 }
4179
danielk19774099f6e2007-03-19 11:25:20 +00004180 assert( pPager->doNotSync==1 );
4181 pPager->doNotSync = 0;
4182 }else{
danielk19773b8a05f2007-03-19 17:44:26 +00004183 rc = pager_write(pDbPage);
danielk19774099f6e2007-03-19 11:25:20 +00004184 }
drh86f8c192007-08-22 00:39:19 +00004185 pagerLeave(pPager);
danielk19774099f6e2007-03-19 11:25:20 +00004186 return rc;
4187}
4188
4189/*
drhaacc5432002-01-06 17:07:40 +00004190** Return TRUE if the page given in the argument was previously passed
danielk19773b8a05f2007-03-19 17:44:26 +00004191** to sqlite3PagerWrite(). In other words, return TRUE if it is ok
drh6019e162001-07-02 17:51:45 +00004192** to change the content of the page.
4193*/
danielk19777d3a6662006-01-23 16:21:05 +00004194#ifndef NDEBUG
danielk19773b8a05f2007-03-19 17:44:26 +00004195int sqlite3PagerIswriteable(DbPage *pPg){
drh6019e162001-07-02 17:51:45 +00004196 return pPg->dirty;
4197}
danielk19777d3a6662006-01-23 16:21:05 +00004198#endif
drh6019e162001-07-02 17:51:45 +00004199
danielk1977b84f96f2005-01-20 11:32:23 +00004200#ifndef SQLITE_OMIT_VACUUM
drh6019e162001-07-02 17:51:45 +00004201/*
drh001bbcb2003-03-19 03:14:00 +00004202** Replace the content of a single page with the information in the third
4203** argument.
4204*/
danielk19773b8a05f2007-03-19 17:44:26 +00004205int sqlite3PagerOverwrite(Pager *pPager, Pgno pgno, void *pData){
4206 PgHdr *pPg;
drh001bbcb2003-03-19 03:14:00 +00004207 int rc;
4208
drh86f8c192007-08-22 00:39:19 +00004209 pagerEnter(pPager);
danielk19773b8a05f2007-03-19 17:44:26 +00004210 rc = sqlite3PagerGet(pPager, pgno, &pPg);
drh001bbcb2003-03-19 03:14:00 +00004211 if( rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00004212 rc = sqlite3PagerWrite(pPg);
drh001bbcb2003-03-19 03:14:00 +00004213 if( rc==SQLITE_OK ){
danielk19773b8a05f2007-03-19 17:44:26 +00004214 memcpy(sqlite3PagerGetData(pPg), pData, pPager->pageSize);
drh001bbcb2003-03-19 03:14:00 +00004215 }
danielk19773b8a05f2007-03-19 17:44:26 +00004216 sqlite3PagerUnref(pPg);
drh001bbcb2003-03-19 03:14:00 +00004217 }
drh86f8c192007-08-22 00:39:19 +00004218 pagerLeave(pPager);
drh001bbcb2003-03-19 03:14:00 +00004219 return rc;
4220}
danielk1977b84f96f2005-01-20 11:32:23 +00004221#endif
drh001bbcb2003-03-19 03:14:00 +00004222
4223/*
drh30e58752002-03-02 20:41:57 +00004224** A call to this routine tells the pager that it is not necessary to
drh538f5702007-04-13 02:14:30 +00004225** write the information on page pPg back to the disk, even though
drh30e58752002-03-02 20:41:57 +00004226** that page might be marked as dirty.
4227**
4228** The overlying software layer calls this routine when all of the data
4229** on the given page is unused. The pager marks the page as clean so
4230** that it does not get written to disk.
4231**
4232** Tests show that this optimization, together with the
danielk19773b8a05f2007-03-19 17:44:26 +00004233** sqlite3PagerDontRollback() below, more than double the speed
drh30e58752002-03-02 20:41:57 +00004234** of large INSERT operations and quadruple the speed of large DELETEs.
drh8e298f92002-07-06 16:28:47 +00004235**
4236** When this routine is called, set the alwaysRollback flag to true.
danielk19773b8a05f2007-03-19 17:44:26 +00004237** Subsequent calls to sqlite3PagerDontRollback() for the same page
drh8e298f92002-07-06 16:28:47 +00004238** will thereafter be ignored. This is necessary to avoid a problem
4239** where a page with data is added to the freelist during one part of
4240** a transaction then removed from the freelist during a later part
4241** of the same transaction and reused for some other purpose. When it
4242** is first added to the freelist, this routine is called. When reused,
drh80e35f42007-03-30 14:06:34 +00004243** the sqlite3PagerDontRollback() routine is called. But because the
4244** page contains critical data, we still need to be sure it gets
4245** rolled back in spite of the sqlite3PagerDontRollback() call.
drh30e58752002-03-02 20:41:57 +00004246*/
drh538f5702007-04-13 02:14:30 +00004247void sqlite3PagerDontWrite(DbPage *pDbPage){
4248 PgHdr *pPg = pDbPage;
4249 Pager *pPager = pPg->pPager;
drh8e298f92002-07-06 16:28:47 +00004250
drhb7f91642004-10-31 02:22:47 +00004251 if( MEMDB ) return;
drh86f8c192007-08-22 00:39:19 +00004252 pagerEnter(pPager);
drh8e298f92002-07-06 16:28:47 +00004253 pPg->alwaysRollback = 1;
drh43617e92006-03-06 20:55:46 +00004254 if( pPg->dirty && !pPager->stmtInUse ){
drh1aa2d8b2007-01-03 15:34:29 +00004255 assert( pPager->state>=PAGER_SHARED );
drh8124a302002-06-25 14:43:57 +00004256 if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
4257 /* If this pages is the last page in the file and the file has grown
4258 ** during the current transaction, then do NOT mark the page as clean.
4259 ** When the database file grows, we must make sure that the last page
4260 ** gets written at least once so that the disk file will be the correct
4261 ** size. If you do not write this page and the size of the file
4262 ** on the disk ends up being too small, that can lead to database
4263 ** corruption during the next transaction.
4264 */
4265 }else{
drh538f5702007-04-13 02:14:30 +00004266 PAGERTRACE3("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager));
4267 IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
drh605ad8f2006-05-03 23:34:05 +00004268 makeClean(pPg);
danielk19773c407372005-02-15 02:54:14 +00004269#ifdef SQLITE_CHECK_PAGES
4270 pPg->pageHash = pager_pagehash(pPg);
4271#endif
drh8124a302002-06-25 14:43:57 +00004272 }
drh30e58752002-03-02 20:41:57 +00004273 }
drh86f8c192007-08-22 00:39:19 +00004274 pagerLeave(pPager);
drh30e58752002-03-02 20:41:57 +00004275}
4276
4277/*
4278** A call to this routine tells the pager that if a rollback occurs,
4279** it is not necessary to restore the data on the given page. This
4280** means that the pager does not have to record the given page in the
4281** rollback journal.
drh538f5702007-04-13 02:14:30 +00004282**
4283** If we have not yet actually read the content of this page (if
4284** the PgHdr.needRead flag is set) then this routine acts as a promise
4285** that we will never need to read the page content in the future.
4286** so the needRead flag can be cleared at this point.
drh30e58752002-03-02 20:41:57 +00004287*/
danielk19773b8a05f2007-03-19 17:44:26 +00004288void sqlite3PagerDontRollback(DbPage *pPg){
drh30e58752002-03-02 20:41:57 +00004289 Pager *pPager = pPg->pPager;
4290
drh86f8c192007-08-22 00:39:19 +00004291 pagerEnter(pPager);
drhd3627af2006-12-18 18:34:51 +00004292 assert( pPager->state>=PAGER_RESERVED );
4293 if( pPager->journalOpen==0 ) return;
drhb7f91642004-10-31 02:22:47 +00004294 if( pPg->alwaysRollback || pPager->alwaysRollback || MEMDB ) return;
drh30e58752002-03-02 20:41:57 +00004295 if( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ){
4296 assert( pPager->aInJournal!=0 );
4297 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
4298 pPg->inJournal = 1;
drh538f5702007-04-13 02:14:30 +00004299 pPg->needRead = 0;
drhac69b052004-05-12 13:30:07 +00004300 if( pPager->stmtInUse ){
4301 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh30e58752002-03-02 20:41:57 +00004302 }
drh4f0c5872007-03-26 22:05:01 +00004303 PAGERTRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager));
drhb0603412007-02-28 04:47:26 +00004304 IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno))
drh30e58752002-03-02 20:41:57 +00004305 }
danielk1977f35843b2007-04-07 15:03:17 +00004306 if( pPager->stmtInUse
4307 && !pageInStatement(pPg)
4308 && (int)pPg->pgno<=pPager->stmtSize
4309 ){
drh30e58752002-03-02 20:41:57 +00004310 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
drhac69b052004-05-12 13:30:07 +00004311 assert( pPager->aInStmt!=0 );
4312 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh30e58752002-03-02 20:41:57 +00004313 }
drh86f8c192007-08-22 00:39:19 +00004314 pagerLeave(pPager);
drh30e58752002-03-02 20:41:57 +00004315}
4316
drhac69b052004-05-12 13:30:07 +00004317
drh30e58752002-03-02 20:41:57 +00004318/*
drh80e35f42007-03-30 14:06:34 +00004319** This routine is called to increment the database file change-counter,
4320** stored at byte 24 of the pager file.
4321*/
danielk1977c7b60172007-08-22 11:22:03 +00004322static int pager_incr_changecounter(Pager *pPager, int isDirect){
drh80e35f42007-03-30 14:06:34 +00004323 PgHdr *pPgHdr;
4324 u32 change_counter;
danielk1977c7b60172007-08-22 11:22:03 +00004325 int rc = SQLITE_OK;
drh80e35f42007-03-30 14:06:34 +00004326
4327 if( !pPager->changeCountDone ){
4328 /* Open page 1 of the file for writing. */
4329 rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
4330 if( rc!=SQLITE_OK ) return rc;
danielk1977c7b60172007-08-22 11:22:03 +00004331
4332 if( !isDirect ){
4333 rc = sqlite3PagerWrite(pPgHdr);
4334 if( rc!=SQLITE_OK ) return rc;
4335 }
4336
drh80e35f42007-03-30 14:06:34 +00004337 /* Increment the value just read and write it back to byte 24. */
drhb1003912007-07-20 00:33:36 +00004338 change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
drh80e35f42007-03-30 14:06:34 +00004339 change_counter++;
4340 put32bits(((char*)PGHDR_TO_DATA(pPgHdr))+24, change_counter);
danielk1977c7b60172007-08-22 11:22:03 +00004341
4342 if( isDirect && pPager->fd->pMethods ){
4343 const void *zBuf = PGHDR_TO_DATA(pPgHdr);
4344 rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
4345 }
4346
drh80e35f42007-03-30 14:06:34 +00004347 /* Release the page reference. */
4348 sqlite3PagerUnref(pPgHdr);
4349 pPager->changeCountDone = 1;
4350 }
danielk1977c7b60172007-08-22 11:22:03 +00004351 return rc;
drh80e35f42007-03-30 14:06:34 +00004352}
4353
4354/*
4355** Sync the database file for the pager pPager. zMaster points to the name
4356** of a master journal file that should be written into the individual
4357** journal file. zMaster may be NULL, which is interpreted as no master
4358** journal (a single database transaction).
4359**
4360** This routine ensures that the journal is synced, all dirty pages written
4361** to the database file and the database file synced. The only thing that
4362** remains to commit the transaction is to delete the journal file (or
4363** master journal file if specified).
4364**
4365** Note that if zMaster==NULL, this does not overwrite a previous value
4366** passed to an sqlite3PagerCommitPhaseOne() call.
4367**
4368** If parameter nTrunc is non-zero, then the pager file is truncated to
4369** nTrunc pages (this is used by auto-vacuum databases).
4370*/
4371int sqlite3PagerCommitPhaseOne(Pager *pPager, const char *zMaster, Pgno nTrunc){
4372 int rc = SQLITE_OK;
4373
4374 PAGERTRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n",
4375 pPager->zFilename, zMaster, nTrunc);
drh86f8c192007-08-22 00:39:19 +00004376 pagerEnter(pPager);
drh80e35f42007-03-30 14:06:34 +00004377
4378 /* If this is an in-memory db, or no pages have been written to, or this
4379 ** function has already been called, it is a no-op.
4380 */
4381 if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){
4382 PgHdr *pPg;
drh80e35f42007-03-30 14:06:34 +00004383
danielk1977c7b60172007-08-22 11:22:03 +00004384#ifdef SQLITE_ENABLE_ATOMIC_WRITE
4385 /* The atomic-write optimization can be used if all of the
4386 ** following are true:
4387 **
4388 ** + The file-system supports the atomic-write property for
4389 ** blocks of size page-size, and
4390 ** + This commit is not part of a multi-file transaction, and
4391 ** + Exactly one page has been modified and store in the journal file.
4392 **
4393 ** If the optimization can be used, then the journal file will never
4394 ** be created for this transaction.
4395 */
danielk1977f55b8992007-08-24 08:15:53 +00004396 int useAtomicWrite = (
4397 !zMaster &&
4398 pPager->journalOff==jrnlBufferSize(pPager) &&
4399 nTrunc==0 &&
4400 (0==pPager->pDirty || 0==pPager->pDirty->pDirty)
4401 );
4402 if( useAtomicWrite ){
danielk1977c7b60172007-08-22 11:22:03 +00004403 /* Update the nRec field in the journal file. */
4404 int offset = pPager->journalHdr + sizeof(aJournalMagic);
4405 assert(pPager->nRec==1);
4406 rc = write32bits(pPager->jfd, offset, pPager->nRec);
4407
4408 /* Update the db file change counter. The following call will modify
4409 ** the in-memory representation of page 1 to include the updated
4410 ** change counter and then write page 1 directly to the database
4411 ** file. Because of the atomic-write property of the host file-system,
4412 ** this is safe.
4413 */
4414 rc = pager_incr_changecounter(pPager, 1);
danielk1977f55b8992007-08-24 08:15:53 +00004415 }else{
4416 rc = sqlite3JournalCreate(pPager->jfd);
4417 if( rc!=SQLITE_OK ) goto sync_exit;
4418 }
4419
4420 if( !useAtomicWrite )
danielk1977c7b60172007-08-22 11:22:03 +00004421#endif
4422
drh80e35f42007-03-30 14:06:34 +00004423 /* If a master journal file name has already been written to the
4424 ** journal file, then no sync is required. This happens when it is
4425 ** written, then the process fails to upgrade from a RESERVED to an
4426 ** EXCLUSIVE lock. The next time the process tries to commit the
4427 ** transaction the m-j name will have already been written.
4428 */
4429 if( !pPager->setMaster ){
danielk1977f55b8992007-08-24 08:15:53 +00004430 assert( pPager->journalOpen );
danielk1977c7b60172007-08-22 11:22:03 +00004431 rc = pager_incr_changecounter(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00004432 if( rc!=SQLITE_OK ) goto sync_exit;
4433#ifndef SQLITE_OMIT_AUTOVACUUM
4434 if( nTrunc!=0 ){
4435 /* If this transaction has made the database smaller, then all pages
4436 ** being discarded by the truncation must be written to the journal
4437 ** file.
4438 */
4439 Pgno i;
4440 int iSkip = PAGER_MJ_PGNO(pPager);
4441 for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){
4442 if( !(pPager->aInJournal[i/8] & (1<<(i&7))) && i!=iSkip ){
4443 rc = sqlite3PagerGet(pPager, i, &pPg);
4444 if( rc!=SQLITE_OK ) goto sync_exit;
4445 rc = sqlite3PagerWrite(pPg);
4446 sqlite3PagerUnref(pPg);
4447 if( rc!=SQLITE_OK ) goto sync_exit;
4448 }
4449 }
4450 }
4451#endif
4452 rc = writeMasterJournal(pPager, zMaster);
4453 if( rc!=SQLITE_OK ) goto sync_exit;
4454 rc = syncJournal(pPager);
drh80e35f42007-03-30 14:06:34 +00004455 }
danielk1977c7b60172007-08-22 11:22:03 +00004456 if( rc!=SQLITE_OK ) goto sync_exit;
drh80e35f42007-03-30 14:06:34 +00004457
4458#ifndef SQLITE_OMIT_AUTOVACUUM
4459 if( nTrunc!=0 ){
4460 rc = sqlite3PagerTruncate(pPager, nTrunc);
4461 if( rc!=SQLITE_OK ) goto sync_exit;
4462 }
4463#endif
4464
4465 /* Write all dirty pages to the database file */
4466 pPg = pager_get_all_dirty_pages(pPager);
4467 rc = pager_write_pagelist(pPg);
drh153c62c2007-08-24 03:51:33 +00004468 if( rc!=SQLITE_OK ){
4469 while( pPg && !pPg->dirty ){ pPg = pPg->pDirty; }
4470 pPager->pDirty = pPg;
4471 goto sync_exit;
4472 }
drh3cdd7d32007-03-30 14:46:01 +00004473 pPager->pDirty = 0;
drh80e35f42007-03-30 14:06:34 +00004474
4475 /* Sync the database file. */
drh1cc8c442007-08-24 16:08:29 +00004476 if( !pPager->noSync ){
danielk1977f036aef2007-08-20 05:36:51 +00004477 rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
drh80e35f42007-03-30 14:06:34 +00004478 }
4479 IOTRACE(("DBSYNC %p\n", pPager))
4480
4481 pPager->state = PAGER_SYNCED;
4482 }else if( MEMDB && nTrunc!=0 ){
4483 rc = sqlite3PagerTruncate(pPager, nTrunc);
4484 }
4485
4486sync_exit:
danielk1977e965ac72007-06-13 15:22:28 +00004487 if( rc==SQLITE_IOERR_BLOCKED ){
4488 /* pager_incr_changecounter() may attempt to obtain an exclusive
4489 * lock to spill the cache and return IOERR_BLOCKED. But since
4490 * there is no chance the cache is inconsistent, it's
4491 * better to return SQLITE_BUSY.
4492 */
4493 rc = SQLITE_BUSY;
4494 }
drh86f8c192007-08-22 00:39:19 +00004495 pagerLeave(pPager);
drh80e35f42007-03-30 14:06:34 +00004496 return rc;
4497}
4498
4499
4500/*
drhed7c8552001-04-11 14:29:21 +00004501** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00004502**
4503** If the commit fails for any reason, a rollback attempt is made
4504** and an error code is returned. If the commit worked, SQLITE_OK
4505** is returned.
drhed7c8552001-04-11 14:29:21 +00004506*/
drh80e35f42007-03-30 14:06:34 +00004507int sqlite3PagerCommitPhaseTwo(Pager *pPager){
drha1b351a2001-09-14 16:42:12 +00004508 int rc;
drhed7c8552001-04-11 14:29:21 +00004509 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +00004510
danielk1977efaaf572006-01-16 11:29:19 +00004511 if( pPager->errCode ){
danielk19777f7bc662006-01-23 13:47:47 +00004512 return pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004513 }
drha6abd042004-06-09 17:37:22 +00004514 if( pPager->state<PAGER_RESERVED ){
drhd9b02572001-04-15 00:37:09 +00004515 return SQLITE_ERROR;
4516 }
drh86f8c192007-08-22 00:39:19 +00004517 pagerEnter(pPager);
drh4f0c5872007-03-26 22:05:01 +00004518 PAGERTRACE2("COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004519 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004520 pPg = pager_get_all_dirty_pages(pPager);
4521 while( pPg ){
danielk1977f35843b2007-04-07 15:03:17 +00004522 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4523 clearHistory(pHist);
drhac69b052004-05-12 13:30:07 +00004524 pPg->dirty = 0;
4525 pPg->inJournal = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004526 pHist->inStmt = 0;
drh5229ae42006-03-23 23:29:04 +00004527 pPg->needSync = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004528 pHist->pPrevStmt = pHist->pNextStmt = 0;
drhac69b052004-05-12 13:30:07 +00004529 pPg = pPg->pDirty;
4530 }
drh605ad8f2006-05-03 23:34:05 +00004531 pPager->pDirty = 0;
danielk1977eac7a362004-06-16 07:45:24 +00004532#ifndef NDEBUG
4533 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
4534 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
4535 assert( !pPg->alwaysRollback );
4536 assert( !pHist->pOrig );
4537 assert( !pHist->pStmt );
4538 }
4539#endif
drhac69b052004-05-12 13:30:07 +00004540 pPager->pStmt = 0;
drha6abd042004-06-09 17:37:22 +00004541 pPager->state = PAGER_SHARED;
drhac69b052004-05-12 13:30:07 +00004542 return SQLITE_OK;
4543 }
drh3cdd7d32007-03-30 14:46:01 +00004544 assert( pPager->journalOpen || !pPager->dirtyCache );
4545 assert( pPager->state==PAGER_SYNCED || !pPager->dirtyCache );
4546 rc = pager_end_transaction(pPager);
drh86f8c192007-08-22 00:39:19 +00004547 rc = pager_error(pPager, rc);
4548 pagerLeave(pPager);
4549 return rc;
drhed7c8552001-04-11 14:29:21 +00004550}
4551
4552/*
drha6abd042004-06-09 17:37:22 +00004553** Rollback all changes. The database falls back to PAGER_SHARED mode.
drhed7c8552001-04-11 14:29:21 +00004554** All in-memory cache pages revert to their original data contents.
4555** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00004556**
4557** This routine cannot fail unless some other process is not following
drh4f0ee682007-03-30 20:43:40 +00004558** the correct locking protocol or unless some other
drhd9b02572001-04-15 00:37:09 +00004559** process is writing trash into the journal file (SQLITE_CORRUPT) or
4560** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
4561** codes are returned for all these occasions. Otherwise,
4562** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00004563*/
danielk19773b8a05f2007-03-19 17:44:26 +00004564int sqlite3PagerRollback(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00004565 int rc;
drh4f0c5872007-03-26 22:05:01 +00004566 PAGERTRACE2("ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004567 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004568 PgHdr *p;
4569 for(p=pPager->pAll; p; p=p->pNextAll){
4570 PgHistory *pHist;
danielk1977eac7a362004-06-16 07:45:24 +00004571 assert( !p->alwaysRollback );
4572 if( !p->dirty ){
4573 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pOrig );
4574 assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pStmt );
4575 continue;
4576 }
4577
drhac69b052004-05-12 13:30:07 +00004578 pHist = PGHDR_TO_HIST(p, pPager);
4579 if( pHist->pOrig ){
4580 memcpy(PGHDR_TO_DATA(p), pHist->pOrig, pPager->pageSize);
drh4f0c5872007-03-26 22:05:01 +00004581 PAGERTRACE3("ROLLBACK-PAGE %d of %d\n", p->pgno, PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00004582 }else{
drh4f0c5872007-03-26 22:05:01 +00004583 PAGERTRACE3("PAGE %d is clean on %d\n", p->pgno, PAGERID(pPager));
drhac69b052004-05-12 13:30:07 +00004584 }
4585 clearHistory(pHist);
4586 p->dirty = 0;
4587 p->inJournal = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004588 pHist->inStmt = 0;
4589 pHist->pPrevStmt = pHist->pNextStmt = 0;
danielk1977369f27e2004-06-15 11:40:04 +00004590 if( pPager->xReiniter ){
danielk19773b8a05f2007-03-19 17:44:26 +00004591 pPager->xReiniter(p, pPager->pageSize);
danielk1977369f27e2004-06-15 11:40:04 +00004592 }
drhac69b052004-05-12 13:30:07 +00004593 }
drh605ad8f2006-05-03 23:34:05 +00004594 pPager->pDirty = 0;
drhac69b052004-05-12 13:30:07 +00004595 pPager->pStmt = 0;
4596 pPager->dbSize = pPager->origDbSize;
danielk1977e180dd92007-04-05 17:15:52 +00004597 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00004598 pPager->stmtInUse = 0;
drha6abd042004-06-09 17:37:22 +00004599 pPager->state = PAGER_SHARED;
drhac69b052004-05-12 13:30:07 +00004600 return SQLITE_OK;
4601 }
4602
drh86f8c192007-08-22 00:39:19 +00004603 pagerEnter(pPager);
drha6abd042004-06-09 17:37:22 +00004604 if( !pPager->dirtyCache || !pPager->journalOpen ){
drh80e35f42007-03-30 14:06:34 +00004605 rc = pager_end_transaction(pPager);
drh86f8c192007-08-22 00:39:19 +00004606 pagerLeave(pPager);
drhda47d772002-12-02 04:25:19 +00004607 return rc;
4608 }
drhdb48ee02003-01-16 13:42:43 +00004609
danielk1977efaaf572006-01-16 11:29:19 +00004610 if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
drha6abd042004-06-09 17:37:22 +00004611 if( pPager->state>=PAGER_EXCLUSIVE ){
danielk1977e277be02007-03-23 18:12:06 +00004612 pager_playback(pPager, 0);
drh4b845d72002-03-05 12:41:19 +00004613 }
drh86f8c192007-08-22 00:39:19 +00004614 pagerLeave(pPager);
danielk1977efaaf572006-01-16 11:29:19 +00004615 return pPager->errCode;
drhed7c8552001-04-11 14:29:21 +00004616 }
drha6abd042004-06-09 17:37:22 +00004617 if( pPager->state==PAGER_RESERVED ){
danielk197717221812005-02-15 03:38:05 +00004618 int rc2;
danielk1977e277be02007-03-23 18:12:06 +00004619 rc = pager_playback(pPager, 0);
drh80e35f42007-03-30 14:06:34 +00004620 rc2 = pager_end_transaction(pPager);
drha6abd042004-06-09 17:37:22 +00004621 if( rc==SQLITE_OK ){
4622 rc = rc2;
4623 }
4624 }else{
danielk1977e277be02007-03-23 18:12:06 +00004625 rc = pager_playback(pPager, 0);
drhd9b02572001-04-15 00:37:09 +00004626 }
danielk1977e180dd92007-04-05 17:15:52 +00004627 /* pager_reset(pPager); */
drhd9b02572001-04-15 00:37:09 +00004628 pPager->dbSize = -1;
danielk197707cb5602006-01-20 10:55:05 +00004629
4630 /* If an error occurs during a ROLLBACK, we can no longer trust the pager
4631 ** cache. So call pager_error() on the way out to make any error
4632 ** persistent.
4633 */
drh86f8c192007-08-22 00:39:19 +00004634 rc = pager_error(pPager, rc);
4635 pagerLeave(pPager);
4636 return rc;
drh98808ba2001-10-18 12:34:46 +00004637}
drhd9b02572001-04-15 00:37:09 +00004638
4639/*
drh5e00f6c2001-09-13 13:46:56 +00004640** Return TRUE if the database file is opened read-only. Return FALSE
4641** if the database is (in theory) writable.
4642*/
danielk19773b8a05f2007-03-19 17:44:26 +00004643int sqlite3PagerIsreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00004644 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00004645}
4646
4647/*
drh0f7eb612006-08-08 13:51:43 +00004648** Return the number of references to the pager.
4649*/
danielk19773b8a05f2007-03-19 17:44:26 +00004650int sqlite3PagerRefcount(Pager *pPager){
drh0f7eb612006-08-08 13:51:43 +00004651 return pPager->nRef;
4652}
4653
4654#ifdef SQLITE_TEST
4655/*
drhd9b02572001-04-15 00:37:09 +00004656** This routine is used for testing and analysis only.
4657*/
danielk19773b8a05f2007-03-19 17:44:26 +00004658int *sqlite3PagerStats(Pager *pPager){
danielk197742741be2005-01-08 12:42:39 +00004659 static int a[11];
drhd9b02572001-04-15 00:37:09 +00004660 a[0] = pPager->nRef;
4661 a[1] = pPager->nPage;
4662 a[2] = pPager->mxPage;
4663 a[3] = pPager->dbSize;
4664 a[4] = pPager->state;
danielk1977efaaf572006-01-16 11:29:19 +00004665 a[5] = pPager->errCode;
drhd9b02572001-04-15 00:37:09 +00004666 a[6] = pPager->nHit;
4667 a[7] = pPager->nMiss;
drh7c4ac0c2007-04-05 11:25:58 +00004668 a[8] = 0; /* Used to be pPager->nOvfl */
danielk197742741be2005-01-08 12:42:39 +00004669 a[9] = pPager->nRead;
4670 a[10] = pPager->nWrite;
drhd9b02572001-04-15 00:37:09 +00004671 return a;
4672}
drh0f7eb612006-08-08 13:51:43 +00004673#endif
drhdd793422001-06-28 01:54:48 +00004674
drhfa86c412002-02-02 15:01:15 +00004675/*
drhac69b052004-05-12 13:30:07 +00004676** Set the statement rollback point.
drhfa86c412002-02-02 15:01:15 +00004677**
4678** This routine should be called with the transaction journal already
drhac69b052004-05-12 13:30:07 +00004679** open. A new statement journal is created that can be used to rollback
drhaaab5722002-02-19 13:39:21 +00004680** changes of a single SQL command within a larger transaction.
drhfa86c412002-02-02 15:01:15 +00004681*/
drh86f8c192007-08-22 00:39:19 +00004682static int pagerStmtBegin(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00004683 int rc;
drhac69b052004-05-12 13:30:07 +00004684 assert( !pPager->stmtInUse );
drh1aa2d8b2007-01-03 15:34:29 +00004685 assert( pPager->state>=PAGER_SHARED );
drhdc3ff9c2004-08-18 02:10:15 +00004686 assert( pPager->dbSize>=0 );
drh4f0c5872007-03-26 22:05:01 +00004687 PAGERTRACE2("STMT-BEGIN %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004688 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004689 pPager->stmtInUse = 1;
4690 pPager->stmtSize = pPager->dbSize;
4691 return SQLITE_OK;
4692 }
drhda47d772002-12-02 04:25:19 +00004693 if( !pPager->journalOpen ){
drhac69b052004-05-12 13:30:07 +00004694 pPager->stmtAutoopen = 1;
drhda47d772002-12-02 04:25:19 +00004695 return SQLITE_OK;
4696 }
drhfa86c412002-02-02 15:01:15 +00004697 assert( pPager->journalOpen );
drhed138fb2007-08-22 22:04:37 +00004698 pagerLeave(pPager);
drh17435752007-08-16 04:30:38 +00004699 pPager->aInStmt = sqlite3MallocZero( pPager->dbSize/8 + 1 );
drhed138fb2007-08-22 22:04:37 +00004700 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00004701 if( pPager->aInStmt==0 ){
danielk1977261919c2005-12-06 12:52:59 +00004702 /* sqlite3OsLock(pPager->fd, SHARED_LOCK); */
drhfa86c412002-02-02 15:01:15 +00004703 return SQLITE_NOMEM;
4704 }
drh968af522003-02-11 14:55:40 +00004705#ifndef NDEBUG
drh054889e2005-11-30 03:20:31 +00004706 rc = sqlite3OsFileSize(pPager->jfd, &pPager->stmtJSize);
drhac69b052004-05-12 13:30:07 +00004707 if( rc ) goto stmt_begin_failed;
danielk197776572402004-06-25 02:38:54 +00004708 assert( pPager->stmtJSize == pPager->journalOff );
drh968af522003-02-11 14:55:40 +00004709#endif
danielk197776572402004-06-25 02:38:54 +00004710 pPager->stmtJSize = pPager->journalOff;
drhac69b052004-05-12 13:30:07 +00004711 pPager->stmtSize = pPager->dbSize;
danielk197776572402004-06-25 02:38:54 +00004712 pPager->stmtHdrOff = 0;
danielk197775edc162004-06-26 01:48:18 +00004713 pPager->stmtCksum = pPager->cksumInit;
drhac69b052004-05-12 13:30:07 +00004714 if( !pPager->stmtOpen ){
drh334b2992007-09-06 23:28:23 +00004715 rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->stfd, pPager->zStmtJrnl,
drh33f4e022007-09-03 15:19:34 +00004716 SQLITE_OPEN_SUBJOURNAL);
drh334b2992007-09-06 23:28:23 +00004717 if( rc ){
4718 goto stmt_begin_failed;
4719 }
drhac69b052004-05-12 13:30:07 +00004720 pPager->stmtOpen = 1;
4721 pPager->stmtNRec = 0;
drh0f892532002-05-30 12:27:03 +00004722 }
drhac69b052004-05-12 13:30:07 +00004723 pPager->stmtInUse = 1;
drhfa86c412002-02-02 15:01:15 +00004724 return SQLITE_OK;
4725
drhac69b052004-05-12 13:30:07 +00004726stmt_begin_failed:
4727 if( pPager->aInStmt ){
drh17435752007-08-16 04:30:38 +00004728 sqlite3_free(pPager->aInStmt);
drhac69b052004-05-12 13:30:07 +00004729 pPager->aInStmt = 0;
drhfa86c412002-02-02 15:01:15 +00004730 }
4731 return rc;
4732}
drh86f8c192007-08-22 00:39:19 +00004733int sqlite3PagerStmtBegin(Pager *pPager){
4734 int rc;
4735 pagerEnter(pPager);
4736 rc = pagerStmtBegin(pPager);
4737 pagerLeave(pPager);
4738 return rc;
4739}
drhfa86c412002-02-02 15:01:15 +00004740
4741/*
drhac69b052004-05-12 13:30:07 +00004742** Commit a statement.
drhfa86c412002-02-02 15:01:15 +00004743*/
danielk19773b8a05f2007-03-19 17:44:26 +00004744int sqlite3PagerStmtCommit(Pager *pPager){
drh86f8c192007-08-22 00:39:19 +00004745 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00004746 if( pPager->stmtInUse ){
drh03eb96a2002-11-10 23:32:56 +00004747 PgHdr *pPg, *pNext;
drh4f0c5872007-03-26 22:05:01 +00004748 PAGERTRACE2("STMT-COMMIT %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004749 if( !MEMDB ){
drh054889e2005-11-30 03:20:31 +00004750 /* sqlite3OsTruncate(pPager->stfd, 0); */
drh17435752007-08-16 04:30:38 +00004751 sqlite3_free( pPager->aInStmt );
drhac69b052004-05-12 13:30:07 +00004752 pPager->aInStmt = 0;
danielk1977f35843b2007-04-07 15:03:17 +00004753 }else{
4754 for(pPg=pPager->pStmt; pPg; pPg=pNext){
drhac69b052004-05-12 13:30:07 +00004755 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
danielk1977f35843b2007-04-07 15:03:17 +00004756 pNext = pHist->pNextStmt;
4757 assert( pHist->inStmt );
4758 pHist->inStmt = 0;
4759 pHist->pPrevStmt = pHist->pNextStmt = 0;
drh17435752007-08-16 04:30:38 +00004760 sqlite3_free(pHist->pStmt);
drhac69b052004-05-12 13:30:07 +00004761 pHist->pStmt = 0;
4762 }
4763 }
4764 pPager->stmtNRec = 0;
4765 pPager->stmtInUse = 0;
4766 pPager->pStmt = 0;
drh663fc632002-02-02 18:49:19 +00004767 }
drhac69b052004-05-12 13:30:07 +00004768 pPager->stmtAutoopen = 0;
drh86f8c192007-08-22 00:39:19 +00004769 pagerLeave(pPager);
drhfa86c412002-02-02 15:01:15 +00004770 return SQLITE_OK;
4771}
4772
4773/*
drhac69b052004-05-12 13:30:07 +00004774** Rollback a statement.
drhfa86c412002-02-02 15:01:15 +00004775*/
danielk19773b8a05f2007-03-19 17:44:26 +00004776int sqlite3PagerStmtRollback(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00004777 int rc;
drh86f8c192007-08-22 00:39:19 +00004778 pagerEnter(pPager);
drhac69b052004-05-12 13:30:07 +00004779 if( pPager->stmtInUse ){
drh4f0c5872007-03-26 22:05:01 +00004780 PAGERTRACE2("STMT-ROLLBACK %d\n", PAGERID(pPager));
drhb7f91642004-10-31 02:22:47 +00004781 if( MEMDB ){
drhac69b052004-05-12 13:30:07 +00004782 PgHdr *pPg;
danielk1977f35843b2007-04-07 15:03:17 +00004783 PgHistory *pHist;
4784 for(pPg=pPager->pStmt; pPg; pPg=pHist->pNextStmt){
4785 pHist = PGHDR_TO_HIST(pPg, pPager);
drhac69b052004-05-12 13:30:07 +00004786 if( pHist->pStmt ){
4787 memcpy(PGHDR_TO_DATA(pPg), pHist->pStmt, pPager->pageSize);
drh17435752007-08-16 04:30:38 +00004788 sqlite3_free(pHist->pStmt);
drhac69b052004-05-12 13:30:07 +00004789 pHist->pStmt = 0;
4790 }
4791 }
4792 pPager->dbSize = pPager->stmtSize;
danielk1977e180dd92007-04-05 17:15:52 +00004793 pager_truncate_cache(pPager);
drhac69b052004-05-12 13:30:07 +00004794 rc = SQLITE_OK;
4795 }else{
4796 rc = pager_stmt_playback(pPager);
4797 }
danielk19773b8a05f2007-03-19 17:44:26 +00004798 sqlite3PagerStmtCommit(pPager);
drh663fc632002-02-02 18:49:19 +00004799 }else{
4800 rc = SQLITE_OK;
4801 }
drhac69b052004-05-12 13:30:07 +00004802 pPager->stmtAutoopen = 0;
drh86f8c192007-08-22 00:39:19 +00004803 pagerLeave(pPager);
drhfa86c412002-02-02 15:01:15 +00004804 return rc;
4805}
4806
drh73509ee2003-04-06 20:44:45 +00004807/*
4808** Return the full pathname of the database file.
4809*/
danielk19773b8a05f2007-03-19 17:44:26 +00004810const char *sqlite3PagerFilename(Pager *pPager){
drh73509ee2003-04-06 20:44:45 +00004811 return pPager->zFilename;
4812}
4813
drhb20ea9d2004-02-09 01:20:36 +00004814/*
drhd0679ed2007-08-28 22:24:34 +00004815** Return the VFS structure for the pager.
4816*/
4817const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
4818 return pPager->pVfs;
4819}
4820
4821/*
drhcc6bb3e2007-08-31 16:11:35 +00004822** Return the file handle for the database file associated
4823** with the pager. This might return NULL if the file has
4824** not yet been opened.
4825*/
4826sqlite3_file *sqlite3PagerFile(Pager *pPager){
4827 return pPager->fd;
4828}
4829
4830/*
danielk19775865e3d2004-06-14 06:03:57 +00004831** Return the directory of the database file.
4832*/
danielk19773b8a05f2007-03-19 17:44:26 +00004833const char *sqlite3PagerDirname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004834 return pPager->zDirectory;
4835}
4836
4837/*
4838** Return the full pathname of the journal file.
4839*/
danielk19773b8a05f2007-03-19 17:44:26 +00004840const char *sqlite3PagerJournalname(Pager *pPager){
danielk19775865e3d2004-06-14 06:03:57 +00004841 return pPager->zJournal;
4842}
4843
4844/*
drh2c8997b2005-08-27 16:36:48 +00004845** Return true if fsync() calls are disabled for this pager. Return FALSE
4846** if fsync()s are executed normally.
4847*/
danielk19773b8a05f2007-03-19 17:44:26 +00004848int sqlite3PagerNosync(Pager *pPager){
drh2c8997b2005-08-27 16:36:48 +00004849 return pPager->noSync;
4850}
4851
drh7c4ac0c2007-04-05 11:25:58 +00004852#ifdef SQLITE_HAS_CODEC
drh2c8997b2005-08-27 16:36:48 +00004853/*
drhb20ea9d2004-02-09 01:20:36 +00004854** Set the codec for this pager
4855*/
danielk19773b8a05f2007-03-19 17:44:26 +00004856void sqlite3PagerSetCodec(
drhb20ea9d2004-02-09 01:20:36 +00004857 Pager *pPager,
drhc001c582006-03-06 18:23:16 +00004858 void *(*xCodec)(void*,void*,Pgno,int),
drhb20ea9d2004-02-09 01:20:36 +00004859 void *pCodecArg
4860){
4861 pPager->xCodec = xCodec;
4862 pPager->pCodecArg = pCodecArg;
4863}
drh7c4ac0c2007-04-05 11:25:58 +00004864#endif
drhb20ea9d2004-02-09 01:20:36 +00004865
danielk1977687566d2004-11-02 12:56:41 +00004866#ifndef SQLITE_OMIT_AUTOVACUUM
4867/*
drh5e385312007-06-16 04:42:12 +00004868** Move the page pPg to location pgno in the file.
danielk1977687566d2004-11-02 12:56:41 +00004869**
drh5e385312007-06-16 04:42:12 +00004870** There must be no references to the page previously located at
4871** pgno (which we call pPgOld) though that page is allowed to be
4872** in cache. If the page previous located at pgno is not already
4873** in the rollback journal, it is not put there by by this routine.
danielk1977687566d2004-11-02 12:56:41 +00004874**
drh5e385312007-06-16 04:42:12 +00004875** References to the page pPg remain valid. Updating any
4876** meta-data associated with pPg (i.e. data stored in the nExtra bytes
danielk1977687566d2004-11-02 12:56:41 +00004877** allocated along with the page) is the responsibility of the caller.
4878**
danielk19775fd057a2005-03-09 13:09:43 +00004879** A transaction must be active when this routine is called. It used to be
4880** required that a statement transaction was not active, but this restriction
4881** has been removed (CREATE INDEX needs to move a page when a statement
4882** transaction is active).
danielk1977687566d2004-11-02 12:56:41 +00004883*/
danielk19773b8a05f2007-03-19 17:44:26 +00004884int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno){
drh5e385312007-06-16 04:42:12 +00004885 PgHdr *pPgOld; /* The page being overwritten. */
danielk1977f5fdda82004-11-03 08:44:05 +00004886 int h;
danielk197794daf7f2004-11-08 09:26:09 +00004887 Pgno needSyncPgno = 0;
danielk1977687566d2004-11-02 12:56:41 +00004888
drh86f8c192007-08-22 00:39:19 +00004889 pagerEnter(pPager);
danielk1977687566d2004-11-02 12:56:41 +00004890 assert( pPg->nRef>0 );
4891
drh4f0c5872007-03-26 22:05:01 +00004892 PAGERTRACE5("MOVE %d page %d (needSync=%d) moves to %d\n",
danielk1977599fcba2004-11-08 07:13:13 +00004893 PAGERID(pPager), pPg->pgno, pPg->needSync, pgno);
drhb0603412007-02-28 04:47:26 +00004894 IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
danielk1977ef73ee92004-11-06 12:26:07 +00004895
danielk1977b4626a32007-04-28 15:47:43 +00004896 pager_get_content(pPg);
danielk197794daf7f2004-11-08 09:26:09 +00004897 if( pPg->needSync ){
4898 needSyncPgno = pPg->pgno;
drh4f0aee42007-06-16 18:39:41 +00004899 assert( pPg->inJournal || (int)pgno>pPager->origDbSize );
danielk197794daf7f2004-11-08 09:26:09 +00004900 assert( pPg->dirty );
danielk1977ae825582004-11-23 09:06:55 +00004901 assert( pPager->needSync );
danielk197794daf7f2004-11-08 09:26:09 +00004902 }
4903
danielk1977687566d2004-11-02 12:56:41 +00004904 /* Unlink pPg from it's hash-chain */
danielk1977f5fdda82004-11-03 08:44:05 +00004905 unlinkHashChain(pPager, pPg);
danielk1977687566d2004-11-02 12:56:41 +00004906
danielk1977ef73ee92004-11-06 12:26:07 +00004907 /* If the cache contains a page with page-number pgno, remove it
danielk1977599fcba2004-11-08 07:13:13 +00004908 ** from it's hash chain. Also, if the PgHdr.needSync was set for
4909 ** page pgno before the 'move' operation, it needs to be retained
4910 ** for the page moved there.
danielk1977f5fdda82004-11-03 08:44:05 +00004911 */
drh5e385312007-06-16 04:42:12 +00004912 pPg->needSync = 0;
danielk1977687566d2004-11-02 12:56:41 +00004913 pPgOld = pager_lookup(pPager, pgno);
4914 if( pPgOld ){
danielk1977f5fdda82004-11-03 08:44:05 +00004915 assert( pPgOld->nRef==0 );
4916 unlinkHashChain(pPager, pPgOld);
drh605ad8f2006-05-03 23:34:05 +00004917 makeClean(pPgOld);
drh5e385312007-06-16 04:42:12 +00004918 pPg->needSync = pPgOld->needSync;
4919 }else{
4920 pPg->needSync = 0;
4921 }
4922 if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){
4923 pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0;
drh5e385312007-06-16 04:42:12 +00004924 }else{
4925 pPg->inJournal = 0;
drh732c8172007-06-16 11:17:45 +00004926 assert( pPg->needSync==0 || (int)pgno>pPager->origDbSize );
danielk1977687566d2004-11-02 12:56:41 +00004927 }
4928
danielk1977f5fdda82004-11-03 08:44:05 +00004929 /* Change the page number for pPg and insert it into the new hash-chain. */
drh3765df42006-06-28 18:18:09 +00004930 assert( pgno!=0 );
danielk1977f5fdda82004-11-03 08:44:05 +00004931 pPg->pgno = pgno;
drh8ca0c722006-05-07 17:49:38 +00004932 h = pgno & (pPager->nHash-1);
danielk1977f5fdda82004-11-03 08:44:05 +00004933 if( pPager->aHash[h] ){
4934 assert( pPager->aHash[h]->pPrevHash==0 );
4935 pPager->aHash[h]->pPrevHash = pPg;
4936 }
4937 pPg->pNextHash = pPager->aHash[h];
4938 pPager->aHash[h] = pPg;
4939 pPg->pPrevHash = 0;
4940
drh605ad8f2006-05-03 23:34:05 +00004941 makeDirty(pPg);
danielk1977687566d2004-11-02 12:56:41 +00004942 pPager->dirtyCache = 1;
4943
danielk197794daf7f2004-11-08 09:26:09 +00004944 if( needSyncPgno ){
4945 /* If needSyncPgno is non-zero, then the journal file needs to be
4946 ** sync()ed before any data is written to database file page needSyncPgno.
4947 ** Currently, no such page exists in the page-cache and the
4948 ** Pager.aInJournal bit has been set. This needs to be remedied by loading
4949 ** the page into the pager-cache and setting the PgHdr.needSync flag.
danielk1977ae825582004-11-23 09:06:55 +00004950 **
danielk19773b8a05f2007-03-19 17:44:26 +00004951 ** The sqlite3PagerGet() call may cause the journal to sync. So make
danielk1977ae825582004-11-23 09:06:55 +00004952 ** sure the Pager.needSync flag is set too.
danielk197794daf7f2004-11-08 09:26:09 +00004953 */
4954 int rc;
danielk19773b8a05f2007-03-19 17:44:26 +00004955 PgHdr *pPgHdr;
danielk1977ae825582004-11-23 09:06:55 +00004956 assert( pPager->needSync );
danielk19773b8a05f2007-03-19 17:44:26 +00004957 rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
danielk197794daf7f2004-11-08 09:26:09 +00004958 if( rc!=SQLITE_OK ) return rc;
danielk1977ae825582004-11-23 09:06:55 +00004959 pPager->needSync = 1;
danielk19773b8a05f2007-03-19 17:44:26 +00004960 pPgHdr->needSync = 1;
4961 pPgHdr->inJournal = 1;
4962 makeDirty(pPgHdr);
4963 sqlite3PagerUnref(pPgHdr);
danielk197794daf7f2004-11-08 09:26:09 +00004964 }
4965
drh86f8c192007-08-22 00:39:19 +00004966 pagerLeave(pPager);
danielk1977687566d2004-11-02 12:56:41 +00004967 return SQLITE_OK;
4968}
4969#endif
4970
danielk19773b8a05f2007-03-19 17:44:26 +00004971/*
4972** Return a pointer to the data for the specified page.
4973*/
4974void *sqlite3PagerGetData(DbPage *pPg){
4975 return PGHDR_TO_DATA(pPg);
4976}
4977
4978/*
4979** Return a pointer to the Pager.nExtra bytes of "extra" space
4980** allocated along with the specified page.
4981*/
4982void *sqlite3PagerGetExtra(DbPage *pPg){
4983 Pager *pPager = pPg->pPager;
4984 return (pPager?PGHDR_TO_EXTRA(pPg, pPager):0);
4985}
4986
danielk197741483462007-03-24 16:45:04 +00004987/*
4988** Get/set the locking-mode for this pager. Parameter eMode must be one
4989** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or
4990** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
4991** the locking-mode is set to the value specified.
4992**
4993** The returned value is either PAGER_LOCKINGMODE_NORMAL or
4994** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
4995** locking-mode.
4996*/
4997int sqlite3PagerLockingMode(Pager *pPager, int eMode){
drh369339d2007-03-30 16:01:55 +00004998 assert( eMode==PAGER_LOCKINGMODE_QUERY
4999 || eMode==PAGER_LOCKINGMODE_NORMAL
5000 || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
5001 assert( PAGER_LOCKINGMODE_QUERY<0 );
5002 assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
5003 if( eMode>=0 && !pPager->tempFile ){
danielk197741483462007-03-24 16:45:04 +00005004 pPager->exclusiveMode = eMode;
5005 }
5006 return (int)pPager->exclusiveMode;
5007}
5008
danielk197793758c82005-01-21 08:13:14 +00005009#ifdef SQLITE_DEBUG
drhdd793422001-06-28 01:54:48 +00005010/*
5011** Print a listing of all referenced pages and their ref count.
5012*/
danielk19773b8a05f2007-03-19 17:44:26 +00005013void sqlite3PagerRefdump(Pager *pPager){
drhdd793422001-06-28 01:54:48 +00005014 PgHdr *pPg;
5015 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
5016 if( pPg->nRef<=0 ) continue;
drhfe63d1c2004-09-08 20:13:04 +00005017 sqlite3DebugPrintf("PAGE %3d addr=%p nRef=%d\n",
5018 pPg->pgno, PGHDR_TO_DATA(pPg), pPg->nRef);
drhdd793422001-06-28 01:54:48 +00005019 }
5020}
5021#endif
drh2e66f0b2005-04-28 17:18:48 +00005022
5023#endif /* SQLITE_OMIT_DISKIO */