blob: 4dabfca2911d909e11f8285f85d8d15c8bac4843 [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**
drha6abd042004-06-09 17:37:22 +000021** @(#) $Id: pager.c,v 1.115 2004/06/09 17:37:28 drh Exp $
drhed7c8552001-04-11 14:29:21 +000022*/
drh829e8022002-11-06 14:08:11 +000023#include "os.h" /* Must be first to enable large file support */
drhd9b02572001-04-15 00:37:09 +000024#include "sqliteInt.h"
drhed7c8552001-04-11 14:29:21 +000025#include "pager.h"
drhed7c8552001-04-11 14:29:21 +000026#include <assert.h>
drhd9b02572001-04-15 00:37:09 +000027#include <string.h>
drhed7c8552001-04-11 14:29:21 +000028
29/*
drhdb48ee02003-01-16 13:42:43 +000030** Macros for troubleshooting. Normally turned off
31*/
32#if 0
33static Pager *mainPager = 0;
34#define SET_PAGER(X) if( mainPager==0 ) mainPager = (X)
35#define CLR_PAGER(X) if( mainPager==(X) ) mainPager = 0
36#define TRACE1(X) if( pPager==mainPager ) fprintf(stderr,X)
37#define TRACE2(X,Y) if( pPager==mainPager ) fprintf(stderr,X,Y)
38#define TRACE3(X,Y,Z) if( pPager==mainPager ) fprintf(stderr,X,Y,Z)
39#else
40#define SET_PAGER(X)
41#define CLR_PAGER(X)
42#define TRACE1(X)
43#define TRACE2(X,Y)
44#define TRACE3(X,Y,Z)
45#endif
46
47
48/*
drhed7c8552001-04-11 14:29:21 +000049** The page cache as a whole is always in one of the following
50** states:
51**
drha6abd042004-06-09 17:37:22 +000052** PAGER_UNLOCK The page cache is not currently reading or
drhed7c8552001-04-11 14:29:21 +000053** writing the database file. There is no
54** data held in memory. This is the initial
55** state.
56**
drha6abd042004-06-09 17:37:22 +000057** PAGER_SHARED The page cache is reading the database.
drhed7c8552001-04-11 14:29:21 +000058** Writing is not permitted. There can be
59** multiple readers accessing the same database
drh69688d52001-04-14 16:38:23 +000060** file at the same time.
drhed7c8552001-04-11 14:29:21 +000061**
drha6abd042004-06-09 17:37:22 +000062** PAGER_RESERVED Writing is permitted to the page cache only.
63** The original database file has not been modified.
64** Other processes may still be reading the on-disk
65** database file.
66**
67** PAGER_EXCLUSIVE The page cache is writing the database.
drhed7c8552001-04-11 14:29:21 +000068** Access is exclusive. No other processes or
69** threads can be reading or writing while one
70** process is writing.
71**
drha6abd042004-06-09 17:37:22 +000072** The page cache comes up in PAGER_UNLOCK. The first time a
73** sqlite_page_get() occurs, the state transitions to PAGER_SHARED.
drhed7c8552001-04-11 14:29:21 +000074** After all pages have been released using sqlite_page_unref(),
drha6abd042004-06-09 17:37:22 +000075** the state transitions back to PAGER_UNLOCK. The first time
drhed7c8552001-04-11 14:29:21 +000076** that sqlite_page_write() is called, the state transitions to
drha6abd042004-06-09 17:37:22 +000077** PAGER_RESERVED. (Note that sqlite_page_write() can only be
drh306dc212001-05-21 13:45:10 +000078** called on an outstanding page which means that the pager must
drha6abd042004-06-09 17:37:22 +000079** be in PAGER_SHARED before it transitions to PAGER_RESERVED.)
drh306dc212001-05-21 13:45:10 +000080** The sqlite_page_rollback() and sqlite_page_commit() functions
drha6abd042004-06-09 17:37:22 +000081** transition the state from PAGER_RESERVED to PAGER_EXCLUSIVE to
82** PAGER_SHARED.
drhed7c8552001-04-11 14:29:21 +000083*/
drha6abd042004-06-09 17:37:22 +000084#define PAGER_UNLOCK 0
85#define PAGER_SHARED 1
86#define PAGER_RESERVED 2
87#define PAGER_EXCLUSIVE 3
drhed7c8552001-04-11 14:29:21 +000088
drhd9b02572001-04-15 00:37:09 +000089
drhed7c8552001-04-11 14:29:21 +000090/*
91** Each in-memory image of a page begins with the following header.
drhbd03cae2001-06-02 02:40:57 +000092** This header is only visible to this pager module. The client
93** code that calls pager sees only the data that follows the header.
drhf6038712004-02-08 18:07:34 +000094**
drh3aac2dd2004-04-26 14:10:20 +000095** Client code should call sqlite3pager_write() on a page prior to making
96** any modifications to that page. The first time sqlite3pager_write()
drhf6038712004-02-08 18:07:34 +000097** is called, the original page contents are written into the rollback
98** journal and PgHdr.inJournal and PgHdr.needSync are set. Later, once
99** the journal page has made it onto the disk surface, PgHdr.needSync
100** is cleared. The modified page cannot be written back into the original
101** database file until the journal pages has been synced to disk and the
102** PgHdr.needSync has been cleared.
103**
drh3aac2dd2004-04-26 14:10:20 +0000104** The PgHdr.dirty flag is set when sqlite3pager_write() is called and
drhf6038712004-02-08 18:07:34 +0000105** is cleared again when the page content is written back to the original
106** database file.
drhed7c8552001-04-11 14:29:21 +0000107*/
drhd9b02572001-04-15 00:37:09 +0000108typedef struct PgHdr PgHdr;
drhed7c8552001-04-11 14:29:21 +0000109struct PgHdr {
110 Pager *pPager; /* The pager to which this page belongs */
111 Pgno pgno; /* The page number for this page */
drh69688d52001-04-14 16:38:23 +0000112 PgHdr *pNextHash, *pPrevHash; /* Hash collision chain for PgHdr.pgno */
drhd9b02572001-04-15 00:37:09 +0000113 PgHdr *pNextFree, *pPrevFree; /* Freelist of pages where nRef==0 */
drhac69b052004-05-12 13:30:07 +0000114 PgHdr *pNextAll; /* A list of all pages */
115 PgHdr *pNextStmt, *pPrevStmt; /* List of pages in the statement journal */
drh193a6b42002-07-07 16:52:46 +0000116 u8 inJournal; /* TRUE if has been written to journal */
drhac69b052004-05-12 13:30:07 +0000117 u8 inStmt; /* TRUE if in the statement subjournal */
drh193a6b42002-07-07 16:52:46 +0000118 u8 dirty; /* TRUE if we need to write back changes */
drhdb48ee02003-01-16 13:42:43 +0000119 u8 needSync; /* Sync journal before writing this page */
drh193a6b42002-07-07 16:52:46 +0000120 u8 alwaysRollback; /* Disable dont_rollback() for this page */
drhac69b052004-05-12 13:30:07 +0000121 short int nRef; /* Number of users of this page */
drh2554f8b2003-01-22 01:26:44 +0000122 PgHdr *pDirty; /* Dirty pages sorted by PgHdr.pgno */
drhd0ba1932004-02-10 01:54:28 +0000123 /* SQLITE_PAGE_SIZE bytes of page data follow this header */
drh973b6e32003-02-12 14:09:42 +0000124 /* Pager.nExtra bytes of local data follow the page data */
drhed7c8552001-04-11 14:29:21 +0000125};
126
drhac69b052004-05-12 13:30:07 +0000127/*
128** For an in-memory only database, some extra information is recorded about
129** each page so that changes can be rolled back. (Journal files are not
130** used for in-memory databases.) The following information is added to
131** the end of every EXTRA block for in-memory databases.
132**
133** This information could have been added directly to the PgHdr structure.
134** But then it would take up an extra 8 bytes of storage on every PgHdr
135** even for disk-based databases. Splitting it out saves 8 bytes. This
136** is only a savings of 0.8% but those percentages add up.
137*/
138typedef struct PgHistory PgHistory;
139struct PgHistory {
140 u8 *pOrig; /* Original page text. Restore to this on a full rollback */
141 u8 *pStmt; /* Text as it was at the beginning of the current statement */
142};
drh9eb9e262004-02-11 02:18:05 +0000143
144/*
145** A macro used for invoking the codec if there is one
146*/
147#ifdef SQLITE_HAS_CODEC
148# define CODEC(P,D,N,X) if( P->xCodec ){ P->xCodec(P->pCodecArg,D,N,X); }
149#else
150# define CODEC(P,D,N,X)
151#endif
152
drhed7c8552001-04-11 14:29:21 +0000153/*
drh69688d52001-04-14 16:38:23 +0000154** Convert a pointer to a PgHdr into a pointer to its data
155** and back again.
drhed7c8552001-04-11 14:29:21 +0000156*/
157#define PGHDR_TO_DATA(P) ((void*)(&(P)[1]))
158#define DATA_TO_PGHDR(D) (&((PgHdr*)(D))[-1])
drhd0ba1932004-02-10 01:54:28 +0000159#define PGHDR_TO_EXTRA(P) ((void*)&((char*)(&(P)[1]))[SQLITE_PAGE_SIZE])
drhac69b052004-05-12 13:30:07 +0000160#define PGHDR_TO_HIST(P,PGR) \
161 ((PgHistory*)&((char*)(&(P)[1]))[(PGR)->pageSize+(PGR)->nExtra])
drhed7c8552001-04-11 14:29:21 +0000162
163/*
drhed7c8552001-04-11 14:29:21 +0000164** How big to make the hash table used for locating in-memory pages
drh836faa42003-01-11 13:30:57 +0000165** by page number.
drhed7c8552001-04-11 14:29:21 +0000166*/
drh836faa42003-01-11 13:30:57 +0000167#define N_PG_HASH 2048
168
169/*
170** Hash a page number
171*/
172#define pager_hash(PN) ((PN)&(N_PG_HASH-1))
drhed7c8552001-04-11 14:29:21 +0000173
174/*
175** A open page cache is an instance of the following structure.
176*/
177struct Pager {
178 char *zFilename; /* Name of the database file */
179 char *zJournal; /* Name of the journal file */
drha76c82e2003-07-27 18:59:42 +0000180 char *zDirectory; /* Directory hold database and journal files */
drh8cfbf082001-09-19 13:22:39 +0000181 OsFile fd, jfd; /* File descriptors for database and journal */
drhac69b052004-05-12 13:30:07 +0000182 OsFile stfd; /* File descriptor for the statement subjournal*/
drhed7c8552001-04-11 14:29:21 +0000183 int dbSize; /* Number of pages in the file */
drh69688d52001-04-14 16:38:23 +0000184 int origDbSize; /* dbSize before the current change */
drhac69b052004-05-12 13:30:07 +0000185 int stmtSize; /* Size of database (in pages) at stmt_begin() */
186 off_t stmtJSize; /* Size of journal at stmt_begin() */
drh968af522003-02-11 14:55:40 +0000187 int nRec; /* Number of pages written to the journal */
188 u32 cksumInit; /* Quasi-random value added to every checksum */
drhac69b052004-05-12 13:30:07 +0000189 int stmtNRec; /* Number of records in stmt subjournal */
drh7e3b0a02001-04-28 16:52:40 +0000190 int nExtra; /* Add this many bytes to each in-memory page */
drhb6f41482004-05-14 01:58:11 +0000191 void (*xDestructor)(void*,int); /* Call this routine when freeing pages */
drha6abd042004-06-09 17:37:22 +0000192 void (*xReiniter)(void*,int); /* Call this routine when reloading pages */
drhb6f41482004-05-14 01:58:11 +0000193 int pageSize; /* Number of bytes in a page */
drhed7c8552001-04-11 14:29:21 +0000194 int nPage; /* Total number of in-memory pages */
drhd9b02572001-04-15 00:37:09 +0000195 int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */
drhed7c8552001-04-11 14:29:21 +0000196 int mxPage; /* Maximum number of pages to hold in cache */
drhd9b02572001-04-15 00:37:09 +0000197 int nHit, nMiss, nOvfl; /* Cache hits, missing, and LRU overflows */
drh9eb9e262004-02-11 02:18:05 +0000198 void (*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
drhb20ea9d2004-02-09 01:20:36 +0000199 void *pCodecArg; /* First argument to xCodec() */
drh603240c2002-03-05 01:11:12 +0000200 u8 journalOpen; /* True if journal file descriptors is valid */
drh34e79ce2004-02-08 06:05:46 +0000201 u8 journalStarted; /* True if header of journal is synced */
202 u8 useJournal; /* Use a rollback journal on this file */
drhac69b052004-05-12 13:30:07 +0000203 u8 stmtOpen; /* True if the statement subjournal is open */
204 u8 stmtInUse; /* True we are in a statement subtransaction */
205 u8 stmtAutoopen; /* Open stmt journal when main journal is opened*/
drh603240c2002-03-05 01:11:12 +0000206 u8 noSync; /* Do not sync the journal if true */
drh968af522003-02-11 14:55:40 +0000207 u8 fullSync; /* Do extra syncs of the journal for robustness */
drha6abd042004-06-09 17:37:22 +0000208 u8 state; /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */
drh603240c2002-03-05 01:11:12 +0000209 u8 errMask; /* One of several kinds of errors */
210 u8 tempFile; /* zFilename is a temporary file */
211 u8 readOnly; /* True for a read-only database */
212 u8 needSync; /* True if an fsync() is needed on the journal */
drha6abd042004-06-09 17:37:22 +0000213 u8 dirtyCache; /* True if cached pages have changed */
drh193a6b42002-07-07 16:52:46 +0000214 u8 alwaysRollback; /* Disable dont_rollback() for all pages */
drhac69b052004-05-12 13:30:07 +0000215 u8 memDb; /* True to inhibit all file I/O */
drh603240c2002-03-05 01:11:12 +0000216 u8 *aInJournal; /* One bit for each page in the database file */
drhac69b052004-05-12 13:30:07 +0000217 u8 *aInStmt; /* One bit for each page in the database */
drhed7c8552001-04-11 14:29:21 +0000218 PgHdr *pFirst, *pLast; /* List of free pages */
drh341eae82003-01-21 02:39:36 +0000219 PgHdr *pFirstSynced; /* First free page with PgHdr.needSync==0 */
drhd9b02572001-04-15 00:37:09 +0000220 PgHdr *pAll; /* List of all pages */
drhac69b052004-05-12 13:30:07 +0000221 PgHdr *pStmt; /* List of pages in the statement subjournal */
drhed7c8552001-04-11 14:29:21 +0000222 PgHdr *aHash[N_PG_HASH]; /* Hash table to map page number of PgHdr */
danielk197713adf8a2004-06-03 16:08:41 +0000223 int nMaster; /* Number of bytes to reserve for master j.p */
danielk197724162fe2004-06-04 06:22:00 +0000224 BusyHandler *pBusyHandler; /* Pointer to sqlite.busyHandler */
drhd9b02572001-04-15 00:37:09 +0000225};
226
227/*
228** These are bits that can be set in Pager.errMask.
229*/
230#define PAGER_ERR_FULL 0x01 /* a write() failed */
231#define PAGER_ERR_MEM 0x02 /* malloc() failed */
232#define PAGER_ERR_LOCK 0x04 /* error in the locking protocol */
233#define PAGER_ERR_CORRUPT 0x08 /* database or journal corruption */
drh81a20f22001-10-12 17:30:04 +0000234#define PAGER_ERR_DISK 0x10 /* general disk I/O error - bad hard drive? */
drhd9b02572001-04-15 00:37:09 +0000235
236/*
237** The journal file contains page records in the following
238** format.
drh968af522003-02-11 14:55:40 +0000239**
240** Actually, this structure is the complete page record for pager
241** formats less than 3. Beginning with format 3, this record is surrounded
242** by two checksums.
drhd9b02572001-04-15 00:37:09 +0000243*/
244typedef struct PageRecord PageRecord;
245struct PageRecord {
drhb20ea9d2004-02-09 01:20:36 +0000246 Pgno pgno; /* The page number */
drhd0ba1932004-02-10 01:54:28 +0000247 char aData[SQLITE_PAGE_SIZE]; /* Original data for page pgno */
drhd9b02572001-04-15 00:37:09 +0000248};
249
250/*
drh5e00f6c2001-09-13 13:46:56 +0000251** Journal files begin with the following magic string. The data
252** was obtained from /dev/random. It is used only as a sanity check.
drh94f33312002-08-12 12:29:56 +0000253**
drh968af522003-02-11 14:55:40 +0000254** There are three journal formats (so far). The 1st journal format writes
255** 32-bit integers in the byte-order of the host machine. New
256** formats writes integers as big-endian. All new journals use the
drh94f33312002-08-12 12:29:56 +0000257** new format, but we have to be able to read an older journal in order
drh968af522003-02-11 14:55:40 +0000258** to rollback journals created by older versions of the library.
259**
260** The 3rd journal format (added for 2.8.0) adds additional sanity
261** checking information to the journal. If the power fails while the
262** journal is being written, semi-random garbage data might appear in
263** the journal file after power is restored. If an attempt is then made
264** to roll the journal back, the database could be corrupted. The additional
265** sanity checking data is an attempt to discover the garbage in the
266** journal and ignore it.
267**
268** The sanity checking information for the 3rd journal format consists
269** of a 32-bit checksum on each page of data. The checksum covers both
drhd0ba1932004-02-10 01:54:28 +0000270** the page number and the SQLITE_PAGE_SIZE bytes of data for the page.
drh968af522003-02-11 14:55:40 +0000271** This cksum is initialized to a 32-bit random value that appears in the
272** journal file right after the header. The random initializer is important,
273** because garbage data that appears at the end of a journal is likely
274** data that was once in other files that have now been deleted. If the
275** garbage data came from an obsolete journal file, the checksums might
276** be correct. But by initializing the checksum to random value which
277** is different for every journal, we minimize that risk.
drhd9b02572001-04-15 00:37:09 +0000278*/
drh968af522003-02-11 14:55:40 +0000279static const unsigned char aJournalMagic1[] = {
drhd9b02572001-04-15 00:37:09 +0000280 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd4,
drhed7c8552001-04-11 14:29:21 +0000281};
drh968af522003-02-11 14:55:40 +0000282static const unsigned char aJournalMagic2[] = {
drh94f33312002-08-12 12:29:56 +0000283 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd5,
284};
drh968af522003-02-11 14:55:40 +0000285static const unsigned char aJournalMagic3[] = {
286 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd6,
287};
288#define JOURNAL_FORMAT_1 1
289#define JOURNAL_FORMAT_2 2
290#define JOURNAL_FORMAT_3 3
drh94f33312002-08-12 12:29:56 +0000291
292/*
drh968af522003-02-11 14:55:40 +0000293** The following integer determines what format to use when creating
294** new primary journal files. By default we always use format 3.
295** When testing, we can set this value to older journal formats in order to
296** make sure that newer versions of the library are able to rollback older
297** journal files.
298**
drhac69b052004-05-12 13:30:07 +0000299** Note that statement journals always use format 2 and omit the header.
drh94f33312002-08-12 12:29:56 +0000300*/
301#ifdef SQLITE_TEST
drh968af522003-02-11 14:55:40 +0000302int journal_format = 3;
drh74587e52002-08-13 00:01:16 +0000303#else
drh968af522003-02-11 14:55:40 +0000304# define journal_format 3
drh94f33312002-08-12 12:29:56 +0000305#endif
drhed7c8552001-04-11 14:29:21 +0000306
307/*
drh968af522003-02-11 14:55:40 +0000308** The size of the header and of each page in the journal varies according
309** to which journal format is being used. The following macros figure out
310** the sizes based on format numbers.
311*/
danielk197713adf8a2004-06-03 16:08:41 +0000312/*
drh968af522003-02-11 14:55:40 +0000313#define JOURNAL_HDR_SZ(X) \
314 (sizeof(aJournalMagic1) + sizeof(Pgno) + ((X)>=3)*2*sizeof(u32))
danielk197713adf8a2004-06-03 16:08:41 +0000315*/
316#define JOURNAL_HDR_SZ(pPager, X) (\
317 sizeof(aJournalMagic1) + \
318 sizeof(Pgno) + \
319 ((X)>=3?3*sizeof(u32)+(pPager)->nMaster:0) )
drh968af522003-02-11 14:55:40 +0000320#define JOURNAL_PG_SZ(X) \
drhd0ba1932004-02-10 01:54:28 +0000321 (SQLITE_PAGE_SIZE + sizeof(Pgno) + ((X)>=3)*sizeof(u32))
drh968af522003-02-11 14:55:40 +0000322
danielk197713adf8a2004-06-03 16:08:41 +0000323
drh968af522003-02-11 14:55:40 +0000324/*
drhdd793422001-06-28 01:54:48 +0000325** Enable reference count tracking here:
326*/
drh74587e52002-08-13 00:01:16 +0000327#ifdef SQLITE_TEST
drh3aac2dd2004-04-26 14:10:20 +0000328 int pager3_refinfo_enable = 0;
drhdd793422001-06-28 01:54:48 +0000329 static void pager_refinfo(PgHdr *p){
330 static int cnt = 0;
drh3aac2dd2004-04-26 14:10:20 +0000331 if( !pager3_refinfo_enable ) return;
drhdd793422001-06-28 01:54:48 +0000332 printf(
333 "REFCNT: %4d addr=0x%08x nRef=%d\n",
334 p->pgno, (int)PGHDR_TO_DATA(p), p->nRef
335 );
336 cnt++; /* Something to set a breakpoint on */
337 }
338# define REFINFO(X) pager_refinfo(X)
339#else
340# define REFINFO(X)
341#endif
342
343/*
drh34e79ce2004-02-08 06:05:46 +0000344** Read a 32-bit integer from the given file descriptor. Store the integer
345** that is read in *pRes. Return SQLITE_OK if everything worked, or an
346** error code is something goes wrong.
347**
348** If the journal format is 2 or 3, read a big-endian integer. If the
349** journal format is 1, read an integer in the native byte-order of the
350** host machine.
drh94f33312002-08-12 12:29:56 +0000351*/
drh968af522003-02-11 14:55:40 +0000352static int read32bits(int format, OsFile *fd, u32 *pRes){
drh94f33312002-08-12 12:29:56 +0000353 u32 res;
354 int rc;
danielk19774adee202004-05-08 08:23:19 +0000355 rc = sqlite3OsRead(fd, &res, sizeof(res));
drh968af522003-02-11 14:55:40 +0000356 if( rc==SQLITE_OK && format>JOURNAL_FORMAT_1 ){
drh94f33312002-08-12 12:29:56 +0000357 unsigned char ac[4];
358 memcpy(ac, &res, 4);
359 res = (ac[0]<<24) | (ac[1]<<16) | (ac[2]<<8) | ac[3];
360 }
361 *pRes = res;
362 return rc;
363}
364
365/*
drh34e79ce2004-02-08 06:05:46 +0000366** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
367** on success or an error code is something goes wrong.
368**
369** If the journal format is 2 or 3, write the integer as 4 big-endian
370** bytes. If the journal format is 1, write the integer in the native
371** byte order. In normal operation, only formats 2 and 3 are used.
372** Journal format 1 is only used for testing.
drh94f33312002-08-12 12:29:56 +0000373*/
374static int write32bits(OsFile *fd, u32 val){
375 unsigned char ac[4];
drh968af522003-02-11 14:55:40 +0000376 if( journal_format<=1 ){
danielk19774adee202004-05-08 08:23:19 +0000377 return sqlite3OsWrite(fd, &val, 4);
drh94f33312002-08-12 12:29:56 +0000378 }
drh94f33312002-08-12 12:29:56 +0000379 ac[0] = (val>>24) & 0xff;
380 ac[1] = (val>>16) & 0xff;
381 ac[2] = (val>>8) & 0xff;
382 ac[3] = val & 0xff;
danielk19774adee202004-05-08 08:23:19 +0000383 return sqlite3OsWrite(fd, ac, 4);
drh94f33312002-08-12 12:29:56 +0000384}
385
drh2554f8b2003-01-22 01:26:44 +0000386/*
387** Write a 32-bit integer into a page header right before the
388** page data. This will overwrite the PgHdr.pDirty pointer.
drh34e79ce2004-02-08 06:05:46 +0000389**
390** The integer is big-endian for formats 2 and 3 and native byte order
391** for journal format 1.
drh2554f8b2003-01-22 01:26:44 +0000392*/
drh968af522003-02-11 14:55:40 +0000393static void store32bits(u32 val, PgHdr *p, int offset){
drh2554f8b2003-01-22 01:26:44 +0000394 unsigned char *ac;
drhec1bd0b2003-08-26 11:41:27 +0000395 ac = &((unsigned char*)PGHDR_TO_DATA(p))[offset];
drh968af522003-02-11 14:55:40 +0000396 if( journal_format<=1 ){
drh2554f8b2003-01-22 01:26:44 +0000397 memcpy(ac, &val, 4);
398 }else{
399 ac[0] = (val>>24) & 0xff;
400 ac[1] = (val>>16) & 0xff;
401 ac[2] = (val>>8) & 0xff;
402 ac[3] = val & 0xff;
403 }
404}
405
drh94f33312002-08-12 12:29:56 +0000406
407/*
drhd9b02572001-04-15 00:37:09 +0000408** Convert the bits in the pPager->errMask into an approprate
409** return code.
410*/
411static int pager_errcode(Pager *pPager){
412 int rc = SQLITE_OK;
413 if( pPager->errMask & PAGER_ERR_LOCK ) rc = SQLITE_PROTOCOL;
drh81a20f22001-10-12 17:30:04 +0000414 if( pPager->errMask & PAGER_ERR_DISK ) rc = SQLITE_IOERR;
drhd9b02572001-04-15 00:37:09 +0000415 if( pPager->errMask & PAGER_ERR_FULL ) rc = SQLITE_FULL;
416 if( pPager->errMask & PAGER_ERR_MEM ) rc = SQLITE_NOMEM;
417 if( pPager->errMask & PAGER_ERR_CORRUPT ) rc = SQLITE_CORRUPT;
418 return rc;
drhed7c8552001-04-11 14:29:21 +0000419}
420
421/*
drh03eb96a2002-11-10 23:32:56 +0000422** Add or remove a page from the list of all pages that are in the
drhac69b052004-05-12 13:30:07 +0000423** statement journal.
drh03eb96a2002-11-10 23:32:56 +0000424**
425** The Pager keeps a separate list of pages that are currently in
drhac69b052004-05-12 13:30:07 +0000426** the statement journal. This helps the sqlite3pager_stmt_commit()
drh03eb96a2002-11-10 23:32:56 +0000427** routine run MUCH faster for the common case where there are many
drhac69b052004-05-12 13:30:07 +0000428** pages in memory but only a few are in the statement journal.
drh03eb96a2002-11-10 23:32:56 +0000429*/
drh3aac2dd2004-04-26 14:10:20 +0000430static void page_add_to_stmt_list(PgHdr *pPg){
drh03eb96a2002-11-10 23:32:56 +0000431 Pager *pPager = pPg->pPager;
drhac69b052004-05-12 13:30:07 +0000432 if( pPg->inStmt ) return;
433 assert( pPg->pPrevStmt==0 && pPg->pNextStmt==0 );
434 pPg->pPrevStmt = 0;
435 if( pPager->pStmt ){
436 pPager->pStmt->pPrevStmt = pPg;
drh03eb96a2002-11-10 23:32:56 +0000437 }
drhac69b052004-05-12 13:30:07 +0000438 pPg->pNextStmt = pPager->pStmt;
439 pPager->pStmt = pPg;
440 pPg->inStmt = 1;
drh03eb96a2002-11-10 23:32:56 +0000441}
drh3aac2dd2004-04-26 14:10:20 +0000442static void page_remove_from_stmt_list(PgHdr *pPg){
drhac69b052004-05-12 13:30:07 +0000443 if( !pPg->inStmt ) return;
444 if( pPg->pPrevStmt ){
445 assert( pPg->pPrevStmt->pNextStmt==pPg );
446 pPg->pPrevStmt->pNextStmt = pPg->pNextStmt;
drh03eb96a2002-11-10 23:32:56 +0000447 }else{
drhac69b052004-05-12 13:30:07 +0000448 assert( pPg->pPager->pStmt==pPg );
449 pPg->pPager->pStmt = pPg->pNextStmt;
drh03eb96a2002-11-10 23:32:56 +0000450 }
drhac69b052004-05-12 13:30:07 +0000451 if( pPg->pNextStmt ){
452 assert( pPg->pNextStmt->pPrevStmt==pPg );
453 pPg->pNextStmt->pPrevStmt = pPg->pPrevStmt;
drh03eb96a2002-11-10 23:32:56 +0000454 }
drhac69b052004-05-12 13:30:07 +0000455 pPg->pNextStmt = 0;
456 pPg->pPrevStmt = 0;
457 pPg->inStmt = 0;
drh03eb96a2002-11-10 23:32:56 +0000458}
459
460/*
drhed7c8552001-04-11 14:29:21 +0000461** Find a page in the hash table given its page number. Return
462** a pointer to the page or NULL if not found.
463*/
drhd9b02572001-04-15 00:37:09 +0000464static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drh836faa42003-01-11 13:30:57 +0000465 PgHdr *p = pPager->aHash[pager_hash(pgno)];
drhed7c8552001-04-11 14:29:21 +0000466 while( p && p->pgno!=pgno ){
467 p = p->pNextHash;
468 }
469 return p;
470}
471
472/*
473** Unlock the database and clear the in-memory cache. This routine
474** sets the state of the pager back to what it was when it was first
475** opened. Any outstanding pages are invalidated and subsequent attempts
476** to access those pages will likely result in a coredump.
477*/
drhd9b02572001-04-15 00:37:09 +0000478static void pager_reset(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +0000479 PgHdr *pPg, *pNext;
drhd9b02572001-04-15 00:37:09 +0000480 for(pPg=pPager->pAll; pPg; pPg=pNext){
481 pNext = pPg->pNextAll;
482 sqliteFree(pPg);
drhed7c8552001-04-11 14:29:21 +0000483 }
484 pPager->pFirst = 0;
drh341eae82003-01-21 02:39:36 +0000485 pPager->pFirstSynced = 0;
drhd9b02572001-04-15 00:37:09 +0000486 pPager->pLast = 0;
487 pPager->pAll = 0;
drhed7c8552001-04-11 14:29:21 +0000488 memset(pPager->aHash, 0, sizeof(pPager->aHash));
489 pPager->nPage = 0;
drha6abd042004-06-09 17:37:22 +0000490 if( pPager->state>=PAGER_RESERVED ){
drh3aac2dd2004-04-26 14:10:20 +0000491 sqlite3pager_rollback(pPager);
drhed7c8552001-04-11 14:29:21 +0000492 }
drha6abd042004-06-09 17:37:22 +0000493 sqlite3OsUnlock(&pPager->fd, NO_LOCK);
494 pPager->state = PAGER_UNLOCK;
drhd9b02572001-04-15 00:37:09 +0000495 pPager->dbSize = -1;
drhed7c8552001-04-11 14:29:21 +0000496 pPager->nRef = 0;
drh8cfbf082001-09-19 13:22:39 +0000497 assert( pPager->journalOpen==0 );
drhed7c8552001-04-11 14:29:21 +0000498}
499
500/*
501** When this routine is called, the pager has the journal file open and
drha6abd042004-06-09 17:37:22 +0000502** a RESERVED or EXCLUSIVE lock on the database. This routine releases
503** the database lock and acquires a SHARED lock in its place. The journal
504** file is deleted and closed.
drh50457892003-09-06 01:10:47 +0000505**
506** TODO: Consider keeping the journal file open for temporary databases.
507** This might give a performance improvement on windows where opening
508** a file is an expensive operation.
drhed7c8552001-04-11 14:29:21 +0000509*/
drhd9b02572001-04-15 00:37:09 +0000510static int pager_unwritelock(Pager *pPager){
drhd9b02572001-04-15 00:37:09 +0000511 PgHdr *pPg;
drha6abd042004-06-09 17:37:22 +0000512 if( pPager->state<PAGER_RESERVED ){
513 return SQLITE_OK;
514 }
drh3aac2dd2004-04-26 14:10:20 +0000515 sqlite3pager_stmt_commit(pPager);
drhac69b052004-05-12 13:30:07 +0000516 if( pPager->stmtOpen ){
517 sqlite3OsClose(&pPager->stfd);
518 pPager->stmtOpen = 0;
drh0f892532002-05-30 12:27:03 +0000519 }
drhda47d772002-12-02 04:25:19 +0000520 if( pPager->journalOpen ){
danielk19774adee202004-05-08 08:23:19 +0000521 sqlite3OsClose(&pPager->jfd);
drhda47d772002-12-02 04:25:19 +0000522 pPager->journalOpen = 0;
danielk19774adee202004-05-08 08:23:19 +0000523 sqlite3OsDelete(pPager->zJournal);
drhda47d772002-12-02 04:25:19 +0000524 sqliteFree( pPager->aInJournal );
525 pPager->aInJournal = 0;
526 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
527 pPg->inJournal = 0;
528 pPg->dirty = 0;
drhdb48ee02003-01-16 13:42:43 +0000529 pPg->needSync = 0;
drhda47d772002-12-02 04:25:19 +0000530 }
531 }else{
drha6abd042004-06-09 17:37:22 +0000532 assert( pPager->dirtyCache==0 || pPager->useJournal==0 );
drhd9b02572001-04-15 00:37:09 +0000533 }
drha6abd042004-06-09 17:37:22 +0000534 sqlite3OsUnlock(&pPager->fd, SHARED_LOCK);
535 pPager->state = PAGER_SHARED;
536 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +0000537}
538
drhed7c8552001-04-11 14:29:21 +0000539/*
drh968af522003-02-11 14:55:40 +0000540** Compute and return a checksum for the page of data.
drh34e79ce2004-02-08 06:05:46 +0000541**
542** This is not a real checksum. It is really just the sum of the
543** random initial value and the page number. We considered do a checksum
544** of the database, but that was found to be too slow.
drh968af522003-02-11 14:55:40 +0000545*/
546static u32 pager_cksum(Pager *pPager, Pgno pgno, const char *aData){
547 u32 cksum = pPager->cksumInit + pgno;
drh968af522003-02-11 14:55:40 +0000548 return cksum;
549}
550
551/*
drhfa86c412002-02-02 15:01:15 +0000552** Read a single page from the journal file opened on file descriptor
553** jfd. Playback this one page.
drh968af522003-02-11 14:55:40 +0000554**
555** There are three different journal formats. The format parameter determines
556** which format is used by the journal that is played back.
drhfa86c412002-02-02 15:01:15 +0000557*/
drh968af522003-02-11 14:55:40 +0000558static int pager_playback_one_page(Pager *pPager, OsFile *jfd, int format){
drhfa86c412002-02-02 15:01:15 +0000559 int rc;
560 PgHdr *pPg; /* An existing page in the cache */
561 PageRecord pgRec;
drh968af522003-02-11 14:55:40 +0000562 u32 cksum;
drhfa86c412002-02-02 15:01:15 +0000563
drh968af522003-02-11 14:55:40 +0000564 rc = read32bits(format, jfd, &pgRec.pgno);
drh99ee3602003-02-16 19:13:36 +0000565 if( rc!=SQLITE_OK ) return rc;
danielk19774adee202004-05-08 08:23:19 +0000566 rc = sqlite3OsRead(jfd, &pgRec.aData, sizeof(pgRec.aData));
drh99ee3602003-02-16 19:13:36 +0000567 if( rc!=SQLITE_OK ) return rc;
drhfa86c412002-02-02 15:01:15 +0000568
drh968af522003-02-11 14:55:40 +0000569 /* Sanity checking on the page. This is more important that I originally
570 ** thought. If a power failure occurs while the journal is being written,
571 ** it could cause invalid data to be written into the journal. We need to
572 ** detect this invalid data (with high probability) and ignore it.
573 */
574 if( pgRec.pgno==0 ){
575 return SQLITE_DONE;
576 }
drh7d02cb72003-06-04 16:24:39 +0000577 if( pgRec.pgno>(unsigned)pPager->dbSize ){
drh968af522003-02-11 14:55:40 +0000578 return SQLITE_OK;
579 }
580 if( format>=JOURNAL_FORMAT_3 ){
581 rc = read32bits(format, jfd, &cksum);
drh99ee3602003-02-16 19:13:36 +0000582 if( rc ) return rc;
drh968af522003-02-11 14:55:40 +0000583 if( pager_cksum(pPager, pgRec.pgno, pgRec.aData)!=cksum ){
584 return SQLITE_DONE;
585 }
586 }
drhfa86c412002-02-02 15:01:15 +0000587
588 /* Playback the page. Update the in-memory copy of the page
589 ** at the same time, if there is one.
590 */
591 pPg = pager_lookup(pPager, pgRec.pgno);
drha6abd042004-06-09 17:37:22 +0000592 TRACE2("PLAYBACK page %d\n", pgRec.pgno);
danielk19774adee202004-05-08 08:23:19 +0000593 sqlite3OsSeek(&pPager->fd, (pgRec.pgno-1)*(off_t)SQLITE_PAGE_SIZE);
594 rc = sqlite3OsWrite(&pPager->fd, pgRec.aData, SQLITE_PAGE_SIZE);
drhfa86c412002-02-02 15:01:15 +0000595 if( pPg ){
drhacf4ac92003-12-17 23:57:34 +0000596 /* No page should ever be rolled back that is in use, except for page
597 ** 1 which is held in use in order to keep the lock on the database
598 ** active.
599 */
drhb6f41482004-05-14 01:58:11 +0000600 void *pData;
drhacf4ac92003-12-17 23:57:34 +0000601 assert( pPg->nRef==0 || pPg->pgno==1 );
drhb6f41482004-05-14 01:58:11 +0000602 pData = PGHDR_TO_DATA(pPg);
603 memcpy(pData, pgRec.aData, pPager->pageSize);
drhde647132004-05-07 17:57:49 +0000604 if( pPager->xDestructor ){
drhb6f41482004-05-14 01:58:11 +0000605 pPager->xDestructor(pData, pPager->pageSize);
drhde647132004-05-07 17:57:49 +0000606 }
drhdb48ee02003-01-16 13:42:43 +0000607 pPg->dirty = 0;
608 pPg->needSync = 0;
drhb6f41482004-05-14 01:58:11 +0000609 CODEC(pPager, pData, pPg->pgno, 3);
drhfa86c412002-02-02 15:01:15 +0000610 }
611 return rc;
612}
613
614/*
danielk197713adf8a2004-06-03 16:08:41 +0000615** Parameter zMaster is the name of a master journal file. A single journal
616** file that referred to the master journal file has just been rolled back.
617** This routine checks if it is possible to delete the master journal file,
618** and does so if it is.
619*/
620static int pager_delmaster(const char *zMaster){
621 int rc;
622 int master_open = 0;
623 OsFile master;
624 char *zMasterJournal = 0; /* Contents of master journal file */
625 off_t nMasterJournal; /* Size of master journal file */
626
627 /* Open the master journal file exclusively in case some other process
628 ** is running this routine also. Not that it makes too much difference.
629 */
630 rc = sqlite3OsOpenExclusive(zMaster, &master, 0);
631 if( rc!=SQLITE_OK ) goto delmaster_out;
632 master_open = 1;
633
634 rc = sqlite3OsFileSize(&master, &nMasterJournal);
635 if( rc!=SQLITE_OK ) goto delmaster_out;
636
637 if( nMasterJournal>0 ){
638 char *zDb;
639 zMasterJournal = (char *)sqliteMalloc(nMasterJournal);
640 if( !zMasterJournal ){
641 rc = SQLITE_NOMEM;
642 goto delmaster_out;
643 }
644 rc = sqlite3OsRead(&master, zMasterJournal, nMasterJournal);
645 if( rc!=SQLITE_OK ) goto delmaster_out;
646
647 zDb = zMasterJournal;
648 while( (zDb-zMasterJournal)<nMasterJournal ){
649 char *zJournal = 0;
650 sqlite3SetString(&zJournal, zDb, "-journal", 0);
651 if( !zJournal ){
652 rc = SQLITE_NOMEM;
653 goto delmaster_out;
654 }
655 if( sqlite3OsFileExists(zJournal) ){
656 /* One of the journals pointed to by the master journal exists.
657 ** Open it and check if it points at the master journal. If
658 ** so, return without deleting the master journal file.
659 */
660 OsFile journal;
661 int nMaster;
danielk19779eed5052004-06-04 10:38:30 +0000662 off_t jsz;
danielk197713adf8a2004-06-03 16:08:41 +0000663
664 rc = sqlite3OsOpenReadOnly(zJournal, &journal);
danielk19779eed5052004-06-04 10:38:30 +0000665 sqliteFree(zJournal);
danielk197713adf8a2004-06-03 16:08:41 +0000666 if( rc!=SQLITE_OK ){
667 sqlite3OsClose(&journal);
danielk197713adf8a2004-06-03 16:08:41 +0000668 goto delmaster_out;
669 }
danielk197713adf8a2004-06-03 16:08:41 +0000670
danielk19779eed5052004-06-04 10:38:30 +0000671 /* Check if the file is big enough to be a format 3 journal file
672 ** with the required master journal name. If not, ignore it.
673 */
674 rc = sqlite3OsFileSize(&journal, &jsz);
675 if( rc!=SQLITE_OK ){
676 sqlite3OsClose(&journal);
677 goto delmaster_out;
678 }
679 if( jsz<(sizeof(aJournalMagic3)+4*sizeof(u32)+strlen(zMaster)+1) ){
680 sqlite3OsClose(&journal);
681 continue;
682 }
683
danielk197713adf8a2004-06-03 16:08:41 +0000684 /* Seek to the point in the journal where the master journal name
685 ** is stored. Read the master journal name into memory obtained
686 ** from malloc.
687 */
688 rc = sqlite3OsSeek(&journal, sizeof(aJournalMagic3)+2*sizeof(u32));
689 if( rc!=SQLITE_OK ) goto delmaster_out;
690 rc = read32bits(3, &journal, (u32 *)&nMaster);
691 if( rc!=SQLITE_OK ) goto delmaster_out;
692 if( nMaster>0 && nMaster==strlen(zMaster)+1 ){
693 char *zMasterPtr = (char *)sqliteMalloc(nMaster);
694 if( !zMasterPtr ){
695 rc = SQLITE_NOMEM;
696 }
697 rc = sqlite3OsRead(&journal, zMasterPtr, nMaster);
698 if( rc!=SQLITE_OK ){
699 sqliteFree(zMasterPtr);
700 goto delmaster_out;
701 }
702 if( 0==strncmp(zMasterPtr, zMaster, nMaster) ){
703 /* We have a match. Do not delete the master journal file. */
704 sqliteFree(zMasterPtr);
705 goto delmaster_out;
706 }
707 }
708 }
709 zDb += (strlen(zDb)+1);
710 }
711 }
712
713 sqlite3OsDelete(zMaster);
714
715delmaster_out:
716 if( zMasterJournal ){
717 sqliteFree(zMasterJournal);
718 }
719 if( master_open ){
720 sqlite3OsClose(&master);
721 }
722 return rc;
723}
724
725/*
drha6abd042004-06-09 17:37:22 +0000726** Make every page in the cache agree with what is on disk. In other words,
727** reread the disk to reset the state of the cache.
728**
729** This routine is called after a rollback in which some of the dirty cache
730** pages had never been written out to disk. We need to roll back the
731** cache content and the easiest way to do that is to reread the old content
732** back from the disk.
733*/
734static int pager_reload_cache(Pager *pPager){
735 PgHdr *pPg;
736 int rc = SQLITE_OK;
737 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
738 char zBuf[SQLITE_PAGE_SIZE];
739 if( !pPg->dirty ) continue;
740 if( (int)pPg->pgno <= pPager->origDbSize ){
741 sqlite3OsSeek(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)(pPg->pgno-1));
742 rc = sqlite3OsRead(&pPager->fd, zBuf, SQLITE_PAGE_SIZE);
743 TRACE2("REFETCH page %d\n", pPg->pgno);
744 CODEC(pPager, zBuf, pPg->pgno, 2);
745 if( rc ) break;
746 }else{
747 memset(zBuf, 0, SQLITE_PAGE_SIZE);
748 }
749 if( pPg->nRef==0 || memcmp(zBuf, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE) ){
750 memcpy(PGHDR_TO_DATA(pPg), zBuf, SQLITE_PAGE_SIZE);
751 if( pPager->xReiniter ){
752 pPager->xReiniter(PGHDR_TO_DATA(pPg), pPager->pageSize);
753 }else{
754 memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra);
755 }
756 }
757 pPg->needSync = 0;
758 pPg->dirty = 0;
759 }
760 return rc;
761}
762
763
764/*
drhed7c8552001-04-11 14:29:21 +0000765** Playback the journal and thus restore the database file to
766** the state it was in before we started making changes.
767**
drh34e79ce2004-02-08 06:05:46 +0000768** The journal file format is as follows:
769**
770** * 8 byte prefix. One of the aJournalMagic123 vectors defined
771** above. The format of the journal file is determined by which
772** of the three prefix vectors is seen.
773** * 4 byte big-endian integer which is the number of valid page records
774** in the journal. If this value is 0xffffffff, then compute the
775** number of page records from the journal size. This field appears
776** in format 3 only.
777** * 4 byte big-endian integer which is the initial value for the
778** sanity checksum. This field appears in format 3 only.
779** * 4 byte integer which is the number of pages to truncate the
780** database to during a rollback.
781** * Zero or more pages instances, each as follows:
782** + 4 byte page number.
drhd0ba1932004-02-10 01:54:28 +0000783** + SQLITE_PAGE_SIZE bytes of data.
drh34e79ce2004-02-08 06:05:46 +0000784** + 4 byte checksum (format 3 only)
785**
786** When we speak of the journal header, we mean the first 4 bullets above.
787** Each entry in the journal is an instance of the 5th bullet. Note that
788** bullets 2 and 3 only appear in format-3 journals.
789**
790** Call the value from the second bullet "nRec". nRec is the number of
791** valid page entries in the journal. In most cases, you can compute the
792** value of nRec from the size of the journal file. But if a power
793** failure occurred while the journal was being written, it could be the
794** case that the size of the journal file had already been increased but
795** the extra entries had not yet made it safely to disk. In such a case,
796** the value of nRec computed from the file size would be too large. For
797** that reason, we always use the nRec value in the header.
798**
799** If the nRec value is 0xffffffff it means that nRec should be computed
800** from the file size. This value is used when the user selects the
801** no-sync option for the journal. A power failure could lead to corruption
802** in this case. But for things like temporary table (which will be
803** deleted when the power is restored) we don't care.
804**
805** Journal formats 1 and 2 do not have an nRec value in the header so we
806** have to compute nRec from the file size. This has risks (as described
807** above) which is why all persistent tables have been changed to use
808** format 3.
drhed7c8552001-04-11 14:29:21 +0000809**
drhd9b02572001-04-15 00:37:09 +0000810** If the file opened as the journal file is not a well-formed
drh34e79ce2004-02-08 06:05:46 +0000811** journal file then the database will likely already be
812** corrupted, so the PAGER_ERR_CORRUPT bit is set in pPager->errMask
813** and SQLITE_CORRUPT is returned. If it all works, then this routine
814** returns SQLITE_OK.
drhed7c8552001-04-11 14:29:21 +0000815*/
drh99ee3602003-02-16 19:13:36 +0000816static int pager_playback(Pager *pPager, int useJournalSize){
drh968af522003-02-11 14:55:40 +0000817 off_t szJ; /* Size of the journal file in bytes */
818 int nRec; /* Number of Records in the journal */
drhd9b02572001-04-15 00:37:09 +0000819 int i; /* Loop counter */
820 Pgno mxPg = 0; /* Size of the original file in pages */
drh968af522003-02-11 14:55:40 +0000821 int format; /* Format of the journal file. */
822 unsigned char aMagic[sizeof(aJournalMagic1)];
drhed7c8552001-04-11 14:29:21 +0000823 int rc;
danielk197713adf8a2004-06-03 16:08:41 +0000824 char *zMaster = 0; /* Name of master journal file if any */
drhed7c8552001-04-11 14:29:21 +0000825
drhc3a64ba2001-11-22 00:01:27 +0000826 /* Figure out how many records are in the journal. Abort early if
827 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +0000828 */
drh8cfbf082001-09-19 13:22:39 +0000829 assert( pPager->journalOpen );
danielk19774adee202004-05-08 08:23:19 +0000830 sqlite3OsSeek(&pPager->jfd, 0);
831 rc = sqlite3OsFileSize(&pPager->jfd, &szJ);
drhc3a64ba2001-11-22 00:01:27 +0000832 if( rc!=SQLITE_OK ){
833 goto end_playback;
834 }
drh240c5792004-02-08 00:40:52 +0000835
836 /* If the journal file is too small to contain a complete header,
drh34e79ce2004-02-08 06:05:46 +0000837 ** it must mean that the process that created the journal was just
838 ** beginning to write the journal file when it died. In that case,
839 ** the database file should have still been completely unchanged.
840 ** Nothing needs to be rolled back. We can safely ignore this journal.
drh240c5792004-02-08 00:40:52 +0000841 */
drh968af522003-02-11 14:55:40 +0000842 if( szJ < sizeof(aMagic)+sizeof(Pgno) ){
drhc3a64ba2001-11-22 00:01:27 +0000843 goto end_playback;
844 }
845
846 /* Read the beginning of the journal and truncate the
847 ** database file back to its original size.
848 */
danielk19774adee202004-05-08 08:23:19 +0000849 rc = sqlite3OsRead(&pPager->jfd, aMagic, sizeof(aMagic));
drh94f33312002-08-12 12:29:56 +0000850 if( rc!=SQLITE_OK ){
drh81a20f22001-10-12 17:30:04 +0000851 rc = SQLITE_PROTOCOL;
852 goto end_playback;
drhd9b02572001-04-15 00:37:09 +0000853 }
drh968af522003-02-11 14:55:40 +0000854 if( memcmp(aMagic, aJournalMagic3, sizeof(aMagic))==0 ){
855 format = JOURNAL_FORMAT_3;
856 }else if( memcmp(aMagic, aJournalMagic2, sizeof(aMagic))==0 ){
857 format = JOURNAL_FORMAT_2;
858 }else if( memcmp(aMagic, aJournalMagic1, sizeof(aMagic))==0 ){
859 format = JOURNAL_FORMAT_1;
drh94f33312002-08-12 12:29:56 +0000860 }else{
861 rc = SQLITE_PROTOCOL;
862 goto end_playback;
863 }
drh968af522003-02-11 14:55:40 +0000864 if( format>=JOURNAL_FORMAT_3 ){
danielk197713adf8a2004-06-03 16:08:41 +0000865 if( szJ < sizeof(aMagic) + 4*sizeof(u32) ){
drh240c5792004-02-08 00:40:52 +0000866 /* Ignore the journal if it is too small to contain a complete
867 ** header. We already did this test once above, but at the prior
868 ** test, we did not know the journal format and so we had to assume
869 ** the smallest possible header. Now we know the header is bigger
drh34e79ce2004-02-08 06:05:46 +0000870 ** than the minimum so we test again.
drh240c5792004-02-08 00:40:52 +0000871 */
872 goto end_playback;
873 }
drh133cdf62004-01-07 02:52:07 +0000874 rc = read32bits(format, &pPager->jfd, (u32*)&nRec);
drh968af522003-02-11 14:55:40 +0000875 if( rc ) goto end_playback;
876 rc = read32bits(format, &pPager->jfd, &pPager->cksumInit);
877 if( rc ) goto end_playback;
drh99ee3602003-02-16 19:13:36 +0000878 if( nRec==0xffffffff || useJournalSize ){
danielk197713adf8a2004-06-03 16:08:41 +0000879 nRec = (szJ - JOURNAL_HDR_SZ(pPager, 3))/JOURNAL_PG_SZ(3);
880 }
881
882 /* Check if a master journal file is specified. If one is specified,
883 ** only proceed with the playback if it still exists.
884 */
885 rc = read32bits(format, &pPager->jfd, &pPager->nMaster);
886 if( rc ) goto end_playback;
887 if( pPager->nMaster>0 ){
888 zMaster = sqliteMalloc(pPager->nMaster);
889 if( !zMaster ){
890 rc = SQLITE_NOMEM;
891 goto end_playback;
892 }
893 rc = sqlite3OsRead(&pPager->jfd, zMaster, pPager->nMaster);
894 if( rc!=SQLITE_OK || (strlen(zMaster) && !sqlite3OsFileExists(zMaster)) ){
895 goto end_playback;
896 }
drh968af522003-02-11 14:55:40 +0000897 }
898 }else{
danielk197713adf8a2004-06-03 16:08:41 +0000899 nRec = (szJ - JOURNAL_HDR_SZ(pPager, 2))/JOURNAL_PG_SZ(2);
900 assert( nRec*JOURNAL_PG_SZ(2)+JOURNAL_HDR_SZ(pPager, 2)==szJ );
drh968af522003-02-11 14:55:40 +0000901 }
902 rc = read32bits(format, &pPager->jfd, &mxPg);
drhd9b02572001-04-15 00:37:09 +0000903 if( rc!=SQLITE_OK ){
drh81a20f22001-10-12 17:30:04 +0000904 goto end_playback;
drhd9b02572001-04-15 00:37:09 +0000905 }
drhd8d66e82003-02-12 02:10:15 +0000906 assert( pPager->origDbSize==0 || pPager->origDbSize==mxPg );
danielk19774adee202004-05-08 08:23:19 +0000907 rc = sqlite3OsTruncate(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)mxPg);
drh81a20f22001-10-12 17:30:04 +0000908 if( rc!=SQLITE_OK ){
909 goto end_playback;
910 }
drhd9b02572001-04-15 00:37:09 +0000911 pPager->dbSize = mxPg;
912
drhfa86c412002-02-02 15:01:15 +0000913 /* Copy original pages out of the journal and back into the database file.
drhed7c8552001-04-11 14:29:21 +0000914 */
drh968af522003-02-11 14:55:40 +0000915 for(i=0; i<nRec; i++){
916 rc = pager_playback_one_page(pPager, &pPager->jfd, format);
917 if( rc!=SQLITE_OK ){
918 if( rc==SQLITE_DONE ){
drh968af522003-02-11 14:55:40 +0000919 rc = SQLITE_OK;
920 }
921 break;
922 }
drhed7c8552001-04-11 14:29:21 +0000923 }
drh81a20f22001-10-12 17:30:04 +0000924
drh4a0681e2003-02-13 01:58:20 +0000925 /* Pages that have been written to the journal but never synced
926 ** where not restored by the loop above. We have to restore those
drh240c5792004-02-08 00:40:52 +0000927 ** pages by reading them back from the original database.
drhdb48ee02003-01-16 13:42:43 +0000928 */
929 if( rc==SQLITE_OK ){
drha6abd042004-06-09 17:37:22 +0000930 pager_reload_cache(pPager);
drhdb48ee02003-01-16 13:42:43 +0000931 }
drh4a0681e2003-02-13 01:58:20 +0000932
933end_playback:
danielk197713adf8a2004-06-03 16:08:41 +0000934 if( zMaster ){
935 /* If there was a master journal and this routine will return true,
936 ** see if it is possible to delete the master journal. If errors
937 ** occur during this process, ignore them.
938 */
939 if( rc==SQLITE_OK ){
940 pager_delmaster(zMaster);
941 }
942 sqliteFree(zMaster);
943 }
drhd9b02572001-04-15 00:37:09 +0000944 if( rc!=SQLITE_OK ){
danielk197713adf8a2004-06-03 16:08:41 +0000945 /* FIX ME: We shouldn't delete the journal if an error occured during
946 ** rollback. It may have been a transient error and the rollback may
947 ** succeed next time it is attempted.
948 */
drhd9b02572001-04-15 00:37:09 +0000949 pager_unwritelock(pPager);
950 pPager->errMask |= PAGER_ERR_CORRUPT;
951 rc = SQLITE_CORRUPT;
952 }else{
953 rc = pager_unwritelock(pPager);
drhed7c8552001-04-11 14:29:21 +0000954 }
drhd9b02572001-04-15 00:37:09 +0000955 return rc;
drhed7c8552001-04-11 14:29:21 +0000956}
957
958/*
drhac69b052004-05-12 13:30:07 +0000959** Playback the statement journal.
drhfa86c412002-02-02 15:01:15 +0000960**
961** This is similar to playing back the transaction journal but with
962** a few extra twists.
963**
drh663fc632002-02-02 18:49:19 +0000964** (1) The number of pages in the database file at the start of
drhac69b052004-05-12 13:30:07 +0000965** the statement is stored in pPager->stmtSize, not in the
drh663fc632002-02-02 18:49:19 +0000966** journal file itself.
drhfa86c412002-02-02 15:01:15 +0000967**
drhac69b052004-05-12 13:30:07 +0000968** (2) In addition to playing back the statement journal, also
drhfa86c412002-02-02 15:01:15 +0000969** playback all pages of the transaction journal beginning
drhac69b052004-05-12 13:30:07 +0000970** at offset pPager->stmtJSize.
drhfa86c412002-02-02 15:01:15 +0000971*/
drh3aac2dd2004-04-26 14:10:20 +0000972static int pager_stmt_playback(Pager *pPager){
drh968af522003-02-11 14:55:40 +0000973 off_t szJ; /* Size of the full journal */
974 int nRec; /* Number of Records */
drhfa86c412002-02-02 15:01:15 +0000975 int i; /* Loop counter */
976 int rc;
977
978 /* Truncate the database back to its original size.
979 */
drhac69b052004-05-12 13:30:07 +0000980 rc = sqlite3OsTruncate(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)pPager->stmtSize);
981 pPager->dbSize = pPager->stmtSize;
drhfa86c412002-02-02 15:01:15 +0000982
drhac69b052004-05-12 13:30:07 +0000983 /* Figure out how many records are in the statement journal.
drhfa86c412002-02-02 15:01:15 +0000984 */
drhac69b052004-05-12 13:30:07 +0000985 assert( pPager->stmtInUse && pPager->journalOpen );
986 sqlite3OsSeek(&pPager->stfd, 0);
987 nRec = pPager->stmtNRec;
drhfa86c412002-02-02 15:01:15 +0000988
drhac69b052004-05-12 13:30:07 +0000989 /* Copy original pages out of the statement journal and back into the
990 ** database file. Note that the statement journal always uses format
drh968af522003-02-11 14:55:40 +0000991 ** 2 instead of format 3 since it does not need to be concerned with
992 ** power failures corrupting the journal and can thus omit the checksums.
drhfa86c412002-02-02 15:01:15 +0000993 */
994 for(i=nRec-1; i>=0; i--){
drhac69b052004-05-12 13:30:07 +0000995 rc = pager_playback_one_page(pPager, &pPager->stfd, 2);
drh968af522003-02-11 14:55:40 +0000996 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +0000997 if( rc!=SQLITE_OK ) goto end_stmt_playback;
drhfa86c412002-02-02 15:01:15 +0000998 }
999
1000 /* Figure out how many pages need to be copied out of the transaction
1001 ** journal.
1002 */
drhac69b052004-05-12 13:30:07 +00001003 rc = sqlite3OsSeek(&pPager->jfd, pPager->stmtJSize);
drhfa86c412002-02-02 15:01:15 +00001004 if( rc!=SQLITE_OK ){
drh3aac2dd2004-04-26 14:10:20 +00001005 goto end_stmt_playback;
drhfa86c412002-02-02 15:01:15 +00001006 }
danielk19774adee202004-05-08 08:23:19 +00001007 rc = sqlite3OsFileSize(&pPager->jfd, &szJ);
drhfa86c412002-02-02 15:01:15 +00001008 if( rc!=SQLITE_OK ){
drh3aac2dd2004-04-26 14:10:20 +00001009 goto end_stmt_playback;
drhfa86c412002-02-02 15:01:15 +00001010 }
drhac69b052004-05-12 13:30:07 +00001011 nRec = (szJ - pPager->stmtJSize)/JOURNAL_PG_SZ(journal_format);
drhfa86c412002-02-02 15:01:15 +00001012 for(i=nRec-1; i>=0; i--){
drh968af522003-02-11 14:55:40 +00001013 rc = pager_playback_one_page(pPager, &pPager->jfd, journal_format);
1014 if( rc!=SQLITE_OK ){
1015 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +00001016 goto end_stmt_playback;
drh968af522003-02-11 14:55:40 +00001017 }
drhfa86c412002-02-02 15:01:15 +00001018 }
1019
drh3aac2dd2004-04-26 14:10:20 +00001020end_stmt_playback:
drhfa86c412002-02-02 15:01:15 +00001021 if( rc!=SQLITE_OK ){
drhfa86c412002-02-02 15:01:15 +00001022 pPager->errMask |= PAGER_ERR_CORRUPT;
1023 rc = SQLITE_CORRUPT;
drhfa86c412002-02-02 15:01:15 +00001024 }
1025 return rc;
1026}
1027
1028/*
drhf57b14a2001-09-14 18:54:08 +00001029** Change the maximum number of in-memory pages that are allowed.
drhcd61c282002-03-06 22:01:34 +00001030**
1031** The maximum number is the absolute value of the mxPage parameter.
1032** If mxPage is negative, the noSync flag is also set. noSync bypasses
danielk19774adee202004-05-08 08:23:19 +00001033** calls to sqlite3OsSync(). The pager runs much faster with noSync on,
drhcd61c282002-03-06 22:01:34 +00001034** but if the operating system crashes or there is an abrupt power
1035** failure, the database file might be left in an inconsistent and
1036** unrepairable state.
drhf57b14a2001-09-14 18:54:08 +00001037*/
drh3aac2dd2004-04-26 14:10:20 +00001038void sqlite3pager_set_cachesize(Pager *pPager, int mxPage){
drh603240c2002-03-05 01:11:12 +00001039 if( mxPage>=0 ){
drha1680452002-04-18 01:56:57 +00001040 pPager->noSync = pPager->tempFile;
danielk19771d850a72004-05-31 08:26:49 +00001041 if( pPager->noSync ) pPager->needSync = 0;
drh603240c2002-03-05 01:11:12 +00001042 }else{
1043 pPager->noSync = 1;
1044 mxPage = -mxPage;
1045 }
drhf57b14a2001-09-14 18:54:08 +00001046 if( mxPage>10 ){
1047 pPager->mxPage = mxPage;
1048 }
1049}
1050
1051/*
drh973b6e32003-02-12 14:09:42 +00001052** Adjust the robustness of the database to damage due to OS crashes
1053** or power failures by changing the number of syncs()s when writing
1054** the rollback journal. There are three levels:
1055**
danielk19774adee202004-05-08 08:23:19 +00001056** OFF sqlite3OsSync() is never called. This is the default
drh973b6e32003-02-12 14:09:42 +00001057** for temporary and transient files.
1058**
1059** NORMAL The journal is synced once before writes begin on the
1060** database. This is normally adequate protection, but
1061** it is theoretically possible, though very unlikely,
1062** that an inopertune power failure could leave the journal
1063** in a state which would cause damage to the database
1064** when it is rolled back.
1065**
1066** FULL The journal is synced twice before writes begin on the
drh34e79ce2004-02-08 06:05:46 +00001067** database (with some additional information - the nRec field
1068** of the journal header - being written in between the two
1069** syncs). If we assume that writing a
drh973b6e32003-02-12 14:09:42 +00001070** single disk sector is atomic, then this mode provides
1071** assurance that the journal will not be corrupted to the
1072** point of causing damage to the database during rollback.
1073**
1074** Numeric values associated with these states are OFF==1, NORMAL=2,
1075** and FULL=3.
1076*/
drh3aac2dd2004-04-26 14:10:20 +00001077void sqlite3pager_set_safety_level(Pager *pPager, int level){
drh973b6e32003-02-12 14:09:42 +00001078 pPager->noSync = level==1 || pPager->tempFile;
1079 pPager->fullSync = level==3 && !pPager->tempFile;
danielk19771d850a72004-05-31 08:26:49 +00001080 if( pPager->noSync ) pPager->needSync = 0;
drh973b6e32003-02-12 14:09:42 +00001081}
1082
1083/*
drhfa86c412002-02-02 15:01:15 +00001084** Open a temporary file. Write the name of the file into zName
1085** (zName must be at least SQLITE_TEMPNAME_SIZE bytes long.) Write
1086** the file descriptor into *fd. Return SQLITE_OK on success or some
1087** other error code if we fail.
1088**
1089** The OS will automatically delete the temporary file when it is
1090** closed.
1091*/
drh3aac2dd2004-04-26 14:10:20 +00001092static int sqlite3pager_opentemp(char *zFile, OsFile *fd){
drhfa86c412002-02-02 15:01:15 +00001093 int cnt = 8;
1094 int rc;
1095 do{
1096 cnt--;
danielk19774adee202004-05-08 08:23:19 +00001097 sqlite3OsTempFileName(zFile);
1098 rc = sqlite3OsOpenExclusive(zFile, fd, 1);
drhfa86c412002-02-02 15:01:15 +00001099 }while( cnt>0 && rc!=SQLITE_OK );
1100 return rc;
1101}
1102
1103/*
drhed7c8552001-04-11 14:29:21 +00001104** Create a new page cache and put a pointer to the page cache in *ppPager.
drh5e00f6c2001-09-13 13:46:56 +00001105** The file to be cached need not exist. The file is not locked until
drh3aac2dd2004-04-26 14:10:20 +00001106** the first call to sqlite3pager_get() and is only held open until the
1107** last page is released using sqlite3pager_unref().
drh382c0242001-10-06 16:33:02 +00001108**
drh6446c4d2001-12-15 14:22:18 +00001109** If zFilename is NULL then a randomly-named temporary file is created
1110** and used as the file to be cached. The file will be deleted
1111** automatically when it is closed.
drhed7c8552001-04-11 14:29:21 +00001112*/
drh3aac2dd2004-04-26 14:10:20 +00001113int sqlite3pager_open(
drh7e3b0a02001-04-28 16:52:40 +00001114 Pager **ppPager, /* Return the Pager structure here */
1115 const char *zFilename, /* Name of the database file to open */
1116 int mxPage, /* Max number of in-memory cache pages */
drhda47d772002-12-02 04:25:19 +00001117 int nExtra, /* Extra bytes append to each in-memory page */
danielk197724162fe2004-06-04 06:22:00 +00001118 int useJournal, /* TRUE to use a rollback journal on this file */
1119 void *pBusyHandler /* Busy callback */
drh7e3b0a02001-04-28 16:52:40 +00001120){
drhed7c8552001-04-11 14:29:21 +00001121 Pager *pPager;
drh3e7a6092002-12-07 21:45:14 +00001122 char *zFullPathname;
drhed7c8552001-04-11 14:29:21 +00001123 int nameLen;
drh8cfbf082001-09-19 13:22:39 +00001124 OsFile fd;
drha76c82e2003-07-27 18:59:42 +00001125 int rc, i;
drh5e00f6c2001-09-13 13:46:56 +00001126 int tempFile;
drhac69b052004-05-12 13:30:07 +00001127 int memDb = 0;
drh5e00f6c2001-09-13 13:46:56 +00001128 int readOnly = 0;
drh8cfbf082001-09-19 13:22:39 +00001129 char zTemp[SQLITE_TEMPNAME_SIZE];
drhed7c8552001-04-11 14:29:21 +00001130
drhd9b02572001-04-15 00:37:09 +00001131 *ppPager = 0;
danielk19776f8a5032004-05-10 10:34:51 +00001132 if( sqlite3_malloc_failed ){
drhd9b02572001-04-15 00:37:09 +00001133 return SQLITE_NOMEM;
1134 }
drh901afd42003-08-26 11:25:58 +00001135 if( zFilename && zFilename[0] ){
drhac69b052004-05-12 13:30:07 +00001136 if( strcmp(zFilename,":memory:")==0 ){
1137 memDb = 1;
1138 zFullPathname = sqliteMalloc(4);
1139 if( zFullPathname ) strcpy(zFullPathname, "nil");
1140 rc = SQLITE_OK;
1141 }else{
1142 zFullPathname = sqlite3OsFullPathname(zFilename);
1143 rc = sqlite3OsOpenReadWrite(zFullPathname, &fd, &readOnly);
1144 tempFile = 0;
1145 }
drh5e00f6c2001-09-13 13:46:56 +00001146 }else{
drh3aac2dd2004-04-26 14:10:20 +00001147 rc = sqlite3pager_opentemp(zTemp, &fd);
drh5e00f6c2001-09-13 13:46:56 +00001148 zFilename = zTemp;
danielk19774adee202004-05-08 08:23:19 +00001149 zFullPathname = sqlite3OsFullPathname(zFilename);
drh5e00f6c2001-09-13 13:46:56 +00001150 tempFile = 1;
1151 }
danielk19776f8a5032004-05-10 10:34:51 +00001152 if( sqlite3_malloc_failed ){
drh3e7a6092002-12-07 21:45:14 +00001153 return SQLITE_NOMEM;
1154 }
drh8cfbf082001-09-19 13:22:39 +00001155 if( rc!=SQLITE_OK ){
drh3e7a6092002-12-07 21:45:14 +00001156 sqliteFree(zFullPathname);
drhed7c8552001-04-11 14:29:21 +00001157 return SQLITE_CANTOPEN;
1158 }
drh3e7a6092002-12-07 21:45:14 +00001159 nameLen = strlen(zFullPathname);
drha76c82e2003-07-27 18:59:42 +00001160 pPager = sqliteMalloc( sizeof(*pPager) + nameLen*3 + 30 );
drhd9b02572001-04-15 00:37:09 +00001161 if( pPager==0 ){
danielk19774adee202004-05-08 08:23:19 +00001162 sqlite3OsClose(&fd);
drh3e7a6092002-12-07 21:45:14 +00001163 sqliteFree(zFullPathname);
drhd9b02572001-04-15 00:37:09 +00001164 return SQLITE_NOMEM;
1165 }
drhdb48ee02003-01-16 13:42:43 +00001166 SET_PAGER(pPager);
drhed7c8552001-04-11 14:29:21 +00001167 pPager->zFilename = (char*)&pPager[1];
drha76c82e2003-07-27 18:59:42 +00001168 pPager->zDirectory = &pPager->zFilename[nameLen+1];
1169 pPager->zJournal = &pPager->zDirectory[nameLen+1];
drh3e7a6092002-12-07 21:45:14 +00001170 strcpy(pPager->zFilename, zFullPathname);
drha76c82e2003-07-27 18:59:42 +00001171 strcpy(pPager->zDirectory, zFullPathname);
1172 for(i=nameLen; i>0 && pPager->zDirectory[i-1]!='/'; i--){}
1173 if( i>0 ) pPager->zDirectory[i-1] = 0;
drh3e7a6092002-12-07 21:45:14 +00001174 strcpy(pPager->zJournal, zFullPathname);
1175 sqliteFree(zFullPathname);
drhed7c8552001-04-11 14:29:21 +00001176 strcpy(&pPager->zJournal[nameLen], "-journal");
1177 pPager->fd = fd;
drh8cfbf082001-09-19 13:22:39 +00001178 pPager->journalOpen = 0;
drhac69b052004-05-12 13:30:07 +00001179 pPager->useJournal = useJournal && !memDb;
1180 pPager->stmtOpen = 0;
1181 pPager->stmtInUse = 0;
drhed7c8552001-04-11 14:29:21 +00001182 pPager->nRef = 0;
drhac69b052004-05-12 13:30:07 +00001183 pPager->dbSize = memDb-1;
1184 pPager->pageSize = SQLITE_PAGE_SIZE;
1185 pPager->stmtSize = 0;
1186 pPager->stmtJSize = 0;
drhed7c8552001-04-11 14:29:21 +00001187 pPager->nPage = 0;
drhd79caeb2001-04-15 02:27:24 +00001188 pPager->mxPage = mxPage>5 ? mxPage : 10;
drha6abd042004-06-09 17:37:22 +00001189 pPager->state = PAGER_UNLOCK;
drhd9b02572001-04-15 00:37:09 +00001190 pPager->errMask = 0;
drh5e00f6c2001-09-13 13:46:56 +00001191 pPager->tempFile = tempFile;
drhac69b052004-05-12 13:30:07 +00001192 pPager->memDb = memDb;
drh5e00f6c2001-09-13 13:46:56 +00001193 pPager->readOnly = readOnly;
drhf57b14a2001-09-14 18:54:08 +00001194 pPager->needSync = 0;
drhda47d772002-12-02 04:25:19 +00001195 pPager->noSync = pPager->tempFile || !useJournal;
drhed7c8552001-04-11 14:29:21 +00001196 pPager->pFirst = 0;
drh341eae82003-01-21 02:39:36 +00001197 pPager->pFirstSynced = 0;
drhed7c8552001-04-11 14:29:21 +00001198 pPager->pLast = 0;
drh7c717f72001-06-24 20:39:41 +00001199 pPager->nExtra = nExtra;
danielk197724162fe2004-06-04 06:22:00 +00001200 pPager->pBusyHandler = (BusyHandler *)pBusyHandler;
drhed7c8552001-04-11 14:29:21 +00001201 memset(pPager->aHash, 0, sizeof(pPager->aHash));
1202 *ppPager = pPager;
1203 return SQLITE_OK;
1204}
1205
1206/*
drh72f82862001-05-24 21:06:34 +00001207** Set the destructor for this pager. If not NULL, the destructor is called
drh5e00f6c2001-09-13 13:46:56 +00001208** when the reference count on each page reaches zero. The destructor can
1209** be used to clean up information in the extra segment appended to each page.
drh72f82862001-05-24 21:06:34 +00001210**
drh3aac2dd2004-04-26 14:10:20 +00001211** The destructor is not called as a result sqlite3pager_close().
1212** Destructors are only called by sqlite3pager_unref().
drh72f82862001-05-24 21:06:34 +00001213*/
drhb6f41482004-05-14 01:58:11 +00001214void sqlite3pager_set_destructor(Pager *pPager, void (*xDesc)(void*,int)){
drh72f82862001-05-24 21:06:34 +00001215 pPager->xDestructor = xDesc;
1216}
1217
1218/*
drha6abd042004-06-09 17:37:22 +00001219** Set the reinitializer for this pager. If not NULL, the reinitializer
1220** is called when the content of a page in cache is restored to its original
1221** value as a result of a rollback. The callback gives higher-level code
1222** an opportunity to restore the EXTRA section to agree with the restored
1223** page data.
1224*/
1225void sqlite3pager_set_reiniter(Pager *pPager, void (*xReinit)(void*,int)){
1226 pPager->xReiniter = xReinit;
1227}
1228
1229/*
drh5e00f6c2001-09-13 13:46:56 +00001230** Return the total number of pages in the disk file associated with
1231** pPager.
drhed7c8552001-04-11 14:29:21 +00001232*/
drh3aac2dd2004-04-26 14:10:20 +00001233int sqlite3pager_pagecount(Pager *pPager){
drh28be87c2002-11-05 23:03:02 +00001234 off_t n;
drhd9b02572001-04-15 00:37:09 +00001235 assert( pPager!=0 );
drhed7c8552001-04-11 14:29:21 +00001236 if( pPager->dbSize>=0 ){
1237 return pPager->dbSize;
1238 }
danielk19774adee202004-05-08 08:23:19 +00001239 if( sqlite3OsFileSize(&pPager->fd, &n)!=SQLITE_OK ){
drh81a20f22001-10-12 17:30:04 +00001240 pPager->errMask |= PAGER_ERR_DISK;
drh8cfbf082001-09-19 13:22:39 +00001241 return 0;
drhed7c8552001-04-11 14:29:21 +00001242 }
drhd0ba1932004-02-10 01:54:28 +00001243 n /= SQLITE_PAGE_SIZE;
drha6abd042004-06-09 17:37:22 +00001244 if( pPager->state!=PAGER_UNLOCK ){
drhed7c8552001-04-11 14:29:21 +00001245 pPager->dbSize = n;
1246 }
1247 return n;
1248}
1249
1250/*
drhf7c57532003-04-25 13:22:51 +00001251** Forward declaration
1252*/
danielk197713adf8a2004-06-03 16:08:41 +00001253static int syncJournal(Pager*, const char*);
drhf7c57532003-04-25 13:22:51 +00001254
drhac69b052004-05-12 13:30:07 +00001255
1256/*
1257** Unlink a page from the free list (the list of all pages where nRef==0)
1258** and from its hash collision chain.
1259*/
1260static void unlinkPage(PgHdr *pPg){
1261 Pager *pPager = pPg->pPager;
1262
1263 /* Keep the pFirstSynced pointer pointing at the first synchronized page */
1264 if( pPg==pPager->pFirstSynced ){
1265 PgHdr *p = pPg->pNextFree;
1266 while( p && p->needSync ){ p = p->pNextFree; }
1267 pPager->pFirstSynced = p;
1268 }
1269
1270 /* Unlink from the freelist */
1271 if( pPg->pPrevFree ){
1272 pPg->pPrevFree->pNextFree = pPg->pNextFree;
1273 }else{
1274 assert( pPager->pFirst==pPg );
1275 pPager->pFirst = pPg->pNextFree;
1276 }
1277 if( pPg->pNextFree ){
1278 pPg->pNextFree->pPrevFree = pPg->pPrevFree;
1279 }else{
1280 assert( pPager->pLast==pPg );
1281 pPager->pLast = pPg->pPrevFree;
1282 }
1283 pPg->pNextFree = pPg->pPrevFree = 0;
1284
1285 /* Unlink from the pgno hash table */
1286 if( pPg->pNextHash ){
1287 pPg->pNextHash->pPrevHash = pPg->pPrevHash;
1288 }
1289 if( pPg->pPrevHash ){
1290 pPg->pPrevHash->pNextHash = pPg->pNextHash;
1291 }else{
1292 int h = pager_hash(pPg->pgno);
1293 assert( pPager->aHash[h]==pPg );
1294 pPager->aHash[h] = pPg->pNextHash;
1295 }
1296 pPg->pNextHash = pPg->pPrevHash = 0;
1297}
1298
1299/*
1300** This routine is used to truncate an in-memory database. Delete
1301** every pages whose pgno is larger than pPager->dbSize and is unreferenced.
1302** Referenced pages larger than pPager->dbSize are zeroed.
1303*/
1304static void memoryTruncate(Pager *pPager){
1305 PgHdr *pPg;
1306 PgHdr **ppPg;
1307 int dbSize = pPager->dbSize;
1308
1309 ppPg = &pPager->pAll;
1310 while( (pPg = *ppPg)!=0 ){
1311 if( pPg->pgno<=dbSize ){
1312 ppPg = &pPg->pNextAll;
1313 }else if( pPg->nRef>0 ){
1314 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
1315 ppPg = &pPg->pNextAll;
1316 }else{
1317 *ppPg = pPg->pNextAll;
1318 unlinkPage(pPg);
1319 sqliteFree(pPg);
1320 pPager->nPage--;
1321 }
1322 }
1323}
1324
drhf7c57532003-04-25 13:22:51 +00001325/*
1326** Truncate the file to the number of pages specified.
1327*/
drh3aac2dd2004-04-26 14:10:20 +00001328int sqlite3pager_truncate(Pager *pPager, Pgno nPage){
drhf7c57532003-04-25 13:22:51 +00001329 int rc;
drh2e6d11b2003-04-25 15:37:57 +00001330 if( pPager->dbSize<0 ){
drh3aac2dd2004-04-26 14:10:20 +00001331 sqlite3pager_pagecount(pPager);
drh2e6d11b2003-04-25 15:37:57 +00001332 }
1333 if( pPager->errMask!=0 ){
1334 rc = pager_errcode(pPager);
1335 return rc;
1336 }
drh7d02cb72003-06-04 16:24:39 +00001337 if( nPage>=(unsigned)pPager->dbSize ){
drhf7c57532003-04-25 13:22:51 +00001338 return SQLITE_OK;
1339 }
drhac69b052004-05-12 13:30:07 +00001340 if( pPager->memDb ){
1341 pPager->dbSize = nPage;
1342 memoryTruncate(pPager);
1343 return SQLITE_OK;
1344 }
danielk197713adf8a2004-06-03 16:08:41 +00001345 syncJournal(pPager, 0);
danielk19774adee202004-05-08 08:23:19 +00001346 rc = sqlite3OsTruncate(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)nPage);
drhf7c57532003-04-25 13:22:51 +00001347 if( rc==SQLITE_OK ){
1348 pPager->dbSize = nPage;
1349 }
1350 return rc;
1351}
1352
1353/*
drhed7c8552001-04-11 14:29:21 +00001354** Shutdown the page cache. Free all memory and close all files.
1355**
1356** If a transaction was in progress when this routine is called, that
1357** transaction is rolled back. All outstanding pages are invalidated
1358** and their memory is freed. Any attempt to use a page associated
1359** with this page cache after this function returns will likely
1360** result in a coredump.
1361*/
drh3aac2dd2004-04-26 14:10:20 +00001362int sqlite3pager_close(Pager *pPager){
drhd9b02572001-04-15 00:37:09 +00001363 PgHdr *pPg, *pNext;
drhed7c8552001-04-11 14:29:21 +00001364 switch( pPager->state ){
drha6abd042004-06-09 17:37:22 +00001365 case PAGER_RESERVED:
1366 case PAGER_EXCLUSIVE: {
drh3aac2dd2004-04-26 14:10:20 +00001367 sqlite3pager_rollback(pPager);
drhac69b052004-05-12 13:30:07 +00001368 if( !pPager->memDb ){
drha6abd042004-06-09 17:37:22 +00001369 sqlite3OsUnlock(&pPager->fd, NO_LOCK);
drhac69b052004-05-12 13:30:07 +00001370 }
drh8cfbf082001-09-19 13:22:39 +00001371 assert( pPager->journalOpen==0 );
drhed7c8552001-04-11 14:29:21 +00001372 break;
1373 }
drha6abd042004-06-09 17:37:22 +00001374 case PAGER_SHARED: {
drhac69b052004-05-12 13:30:07 +00001375 if( !pPager->memDb ){
drha6abd042004-06-09 17:37:22 +00001376 sqlite3OsUnlock(&pPager->fd, NO_LOCK);
drhac69b052004-05-12 13:30:07 +00001377 }
drhed7c8552001-04-11 14:29:21 +00001378 break;
1379 }
1380 default: {
1381 /* Do nothing */
1382 break;
1383 }
1384 }
drhd9b02572001-04-15 00:37:09 +00001385 for(pPg=pPager->pAll; pPg; pPg=pNext){
1386 pNext = pPg->pNextAll;
1387 sqliteFree(pPg);
drhed7c8552001-04-11 14:29:21 +00001388 }
drhac69b052004-05-12 13:30:07 +00001389 if( !pPager->memDb ){
1390 sqlite3OsClose(&pPager->fd);
1391 }
drh8cfbf082001-09-19 13:22:39 +00001392 assert( pPager->journalOpen==0 );
drh0f892532002-05-30 12:27:03 +00001393 /* Temp files are automatically deleted by the OS
1394 ** if( pPager->tempFile ){
danielk19774adee202004-05-08 08:23:19 +00001395 ** sqlite3OsDelete(pPager->zFilename);
drh0f892532002-05-30 12:27:03 +00001396 ** }
1397 */
drhdb48ee02003-01-16 13:42:43 +00001398 CLR_PAGER(pPager);
drh73509ee2003-04-06 20:44:45 +00001399 if( pPager->zFilename!=(char*)&pPager[1] ){
drha76c82e2003-07-27 18:59:42 +00001400 assert( 0 ); /* Cannot happen */
drh73509ee2003-04-06 20:44:45 +00001401 sqliteFree(pPager->zFilename);
1402 sqliteFree(pPager->zJournal);
drha76c82e2003-07-27 18:59:42 +00001403 sqliteFree(pPager->zDirectory);
drh73509ee2003-04-06 20:44:45 +00001404 }
drhed7c8552001-04-11 14:29:21 +00001405 sqliteFree(pPager);
1406 return SQLITE_OK;
1407}
1408
1409/*
drh5e00f6c2001-09-13 13:46:56 +00001410** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +00001411*/
drh3aac2dd2004-04-26 14:10:20 +00001412Pgno sqlite3pager_pagenumber(void *pData){
drhed7c8552001-04-11 14:29:21 +00001413 PgHdr *p = DATA_TO_PGHDR(pData);
1414 return p->pgno;
1415}
1416
1417/*
drhc8629a12004-05-08 20:07:40 +00001418** The page_ref() function increments the reference count for a page.
1419** If the page is currently on the freelist (the reference count is zero) then
drh7e3b0a02001-04-28 16:52:40 +00001420** remove it from the freelist.
drhc8629a12004-05-08 20:07:40 +00001421**
1422** For non-test systems, page_ref() is a macro that calls _page_ref()
1423** online of the reference count is zero. For test systems, page_ref()
1424** is a real function so that we can set breakpoints and trace it.
drh7e3b0a02001-04-28 16:52:40 +00001425*/
drh836faa42003-01-11 13:30:57 +00001426static void _page_ref(PgHdr *pPg){
drh7e3b0a02001-04-28 16:52:40 +00001427 if( pPg->nRef==0 ){
1428 /* The page is currently on the freelist. Remove it. */
drh341eae82003-01-21 02:39:36 +00001429 if( pPg==pPg->pPager->pFirstSynced ){
1430 PgHdr *p = pPg->pNextFree;
1431 while( p && p->needSync ){ p = p->pNextFree; }
1432 pPg->pPager->pFirstSynced = p;
1433 }
drh7e3b0a02001-04-28 16:52:40 +00001434 if( pPg->pPrevFree ){
1435 pPg->pPrevFree->pNextFree = pPg->pNextFree;
1436 }else{
1437 pPg->pPager->pFirst = pPg->pNextFree;
1438 }
1439 if( pPg->pNextFree ){
1440 pPg->pNextFree->pPrevFree = pPg->pPrevFree;
1441 }else{
1442 pPg->pPager->pLast = pPg->pPrevFree;
1443 }
1444 pPg->pPager->nRef++;
1445 }
1446 pPg->nRef++;
drhdd793422001-06-28 01:54:48 +00001447 REFINFO(pPg);
drhdf0b3b02001-06-23 11:36:20 +00001448}
drhc8629a12004-05-08 20:07:40 +00001449#ifdef SQLITE_TEST
1450 static void page_ref(PgHdr *pPg){
1451 if( pPg->nRef==0 ){
1452 _page_ref(pPg);
1453 }else{
1454 pPg->nRef++;
1455 REFINFO(pPg);
1456 }
1457 }
1458#else
1459# define page_ref(P) ((P)->nRef==0?_page_ref(P):(void)(P)->nRef++)
1460#endif
drhdf0b3b02001-06-23 11:36:20 +00001461
1462/*
1463** Increment the reference count for a page. The input pointer is
1464** a reference to the page data.
1465*/
drh3aac2dd2004-04-26 14:10:20 +00001466int sqlite3pager_ref(void *pData){
drhdf0b3b02001-06-23 11:36:20 +00001467 PgHdr *pPg = DATA_TO_PGHDR(pData);
1468 page_ref(pPg);
drh8c42ca92001-06-22 19:15:00 +00001469 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00001470}
1471
1472/*
drh34e79ce2004-02-08 06:05:46 +00001473** Sync the journal. In other words, make sure all the pages that have
1474** been written to the journal have actually reached the surface of the
1475** disk. It is not safe to modify the original database file until after
1476** the journal has been synced. If the original database is modified before
1477** the journal is synced and a power failure occurs, the unsynced journal
1478** data would be lost and we would be unable to completely rollback the
1479** database changes. Database corruption would occur.
1480**
1481** This routine also updates the nRec field in the header of the journal.
1482** (See comments on the pager_playback() routine for additional information.)
1483** If the sync mode is FULL, two syncs will occur. First the whole journal
1484** is synced, then the nRec field is updated, then a second sync occurs.
drhb19a2bc2001-09-16 00:13:26 +00001485**
drh34e79ce2004-02-08 06:05:46 +00001486** For temporary databases, we do not care if we are able to rollback
1487** after a power failure, so sync occurs.
drhfa86c412002-02-02 15:01:15 +00001488**
drh34e79ce2004-02-08 06:05:46 +00001489** This routine clears the needSync field of every page current held in
1490** memory.
drh50e5dad2001-09-15 00:57:28 +00001491*/
danielk197713adf8a2004-06-03 16:08:41 +00001492static int syncJournal(Pager *pPager, const char *zMaster){
drh50e5dad2001-09-15 00:57:28 +00001493 PgHdr *pPg;
1494 int rc = SQLITE_OK;
drh03eb96a2002-11-10 23:32:56 +00001495
1496 /* Sync the journal before modifying the main database
1497 ** (assuming there is a journal and it needs to be synced.)
1498 */
danielk197713adf8a2004-06-03 16:08:41 +00001499 if( pPager->needSync || zMaster ){
drhfa86c412002-02-02 15:01:15 +00001500 if( !pPager->tempFile ){
drhdb48ee02003-01-16 13:42:43 +00001501 assert( pPager->journalOpen );
drh946966f2004-02-25 02:20:41 +00001502 /* assert( !pPager->noSync ); // noSync might be set if synchronous
1503 ** was turned off after the transaction was started. Ticket #615 */
drh968af522003-02-11 14:55:40 +00001504#ifndef NDEBUG
1505 {
drh34e79ce2004-02-08 06:05:46 +00001506 /* Make sure the pPager->nRec counter we are keeping agrees
1507 ** with the nRec computed from the size of the journal file.
1508 */
drh4a0681e2003-02-13 01:58:20 +00001509 off_t hdrSz, pgSz, jSz;
danielk197713adf8a2004-06-03 16:08:41 +00001510 hdrSz = JOURNAL_HDR_SZ(pPager, journal_format);
drh968af522003-02-11 14:55:40 +00001511 pgSz = JOURNAL_PG_SZ(journal_format);
danielk19774adee202004-05-08 08:23:19 +00001512 rc = sqlite3OsFileSize(&pPager->jfd, &jSz);
drh968af522003-02-11 14:55:40 +00001513 if( rc!=0 ) return rc;
drh4a0681e2003-02-13 01:58:20 +00001514 assert( pPager->nRec*pgSz+hdrSz==jSz );
drh968af522003-02-11 14:55:40 +00001515 }
1516#endif
drhd8d66e82003-02-12 02:10:15 +00001517 if( journal_format>=3 ){
drh34e79ce2004-02-08 06:05:46 +00001518 /* Write the nRec value into the journal file header */
drhd8d66e82003-02-12 02:10:15 +00001519 off_t szJ;
1520 if( pPager->fullSync ){
drha6abd042004-06-09 17:37:22 +00001521 TRACE2("SYNC journal of %d\n", pPager->fd.h);
danielk19774adee202004-05-08 08:23:19 +00001522 rc = sqlite3OsSync(&pPager->jfd);
drhd8d66e82003-02-12 02:10:15 +00001523 if( rc!=0 ) return rc;
1524 }
danielk19774adee202004-05-08 08:23:19 +00001525 sqlite3OsSeek(&pPager->jfd, sizeof(aJournalMagic1));
drh99ee3602003-02-16 19:13:36 +00001526 rc = write32bits(&pPager->jfd, pPager->nRec);
1527 if( rc ) return rc;
danielk197713adf8a2004-06-03 16:08:41 +00001528
1529 /* Write the name of the master journal file if one is specified */
1530 if( zMaster ){
1531 assert( strlen(zMaster)<pPager->nMaster );
1532 rc = sqlite3OsSeek(&pPager->jfd, sizeof(aJournalMagic3) + 3*4);
1533 if( rc ) return rc;
1534 rc = sqlite3OsWrite(&pPager->jfd, zMaster, strlen(zMaster)+1);
1535 if( rc ) return rc;
1536 }
1537
1538 szJ = JOURNAL_HDR_SZ(pPager, journal_format) +
drhd8d66e82003-02-12 02:10:15 +00001539 pPager->nRec*JOURNAL_PG_SZ(journal_format);
danielk19774adee202004-05-08 08:23:19 +00001540 sqlite3OsSeek(&pPager->jfd, szJ);
drh968af522003-02-11 14:55:40 +00001541 }
drha6abd042004-06-09 17:37:22 +00001542 TRACE2("SYNC journal of %d\n", pPager->fd.h);
danielk19774adee202004-05-08 08:23:19 +00001543 rc = sqlite3OsSync(&pPager->jfd);
drhfa86c412002-02-02 15:01:15 +00001544 if( rc!=0 ) return rc;
drhdb48ee02003-01-16 13:42:43 +00001545 pPager->journalStarted = 1;
drhfa86c412002-02-02 15:01:15 +00001546 }
drh50e5dad2001-09-15 00:57:28 +00001547 pPager->needSync = 0;
drh341eae82003-01-21 02:39:36 +00001548
1549 /* Erase the needSync flag from every page.
1550 */
1551 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1552 pPg->needSync = 0;
1553 }
1554 pPager->pFirstSynced = pPager->pFirst;
drh50e5dad2001-09-15 00:57:28 +00001555 }
drh03eb96a2002-11-10 23:32:56 +00001556
drh341eae82003-01-21 02:39:36 +00001557#ifndef NDEBUG
1558 /* If the Pager.needSync flag is clear then the PgHdr.needSync
1559 ** flag must also be clear for all pages. Verify that this
1560 ** invariant is true.
drh03eb96a2002-11-10 23:32:56 +00001561 */
drh341eae82003-01-21 02:39:36 +00001562 else{
1563 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1564 assert( pPg->needSync==0 );
1565 }
1566 assert( pPager->pFirstSynced==pPager->pFirst );
drh03eb96a2002-11-10 23:32:56 +00001567 }
drh341eae82003-01-21 02:39:36 +00001568#endif
drhdb48ee02003-01-16 13:42:43 +00001569
drh81a20f22001-10-12 17:30:04 +00001570 return rc;
drh50e5dad2001-09-15 00:57:28 +00001571}
1572
1573/*
drh2554f8b2003-01-22 01:26:44 +00001574** Given a list of pages (connected by the PgHdr.pDirty pointer) write
1575** every one of those pages out to the database file and mark them all
1576** as clean.
1577*/
1578static int pager_write_pagelist(PgHdr *pList){
1579 Pager *pPager;
1580 int rc;
danielk19779eed5052004-06-04 10:38:30 +00001581 int busy = 1;
drh2554f8b2003-01-22 01:26:44 +00001582
1583 if( pList==0 ) return SQLITE_OK;
1584 pPager = pList->pPager;
danielk19779eed5052004-06-04 10:38:30 +00001585
1586 /* At this point there may be either a RESERVED or EXCLUSIVE lock on the
1587 ** database file. If there is already an EXCLUSIVE lock, the following
1588 ** calls to sqlite3OsLock() are no-ops.
1589 **
drha6abd042004-06-09 17:37:22 +00001590 ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
1591 ** through an intermediate state PENDING. A PENDING lock prevents new
1592 ** readers from attaching to the database but is unsufficient for us to
1593 ** write. The idea of a PENDING lock is to prevent new readers from
1594 ** coming in while we wait for existing readers to clear.
danielk19779eed5052004-06-04 10:38:30 +00001595 **
drha6abd042004-06-09 17:37:22 +00001596 ** While the pager is in the RESERVED state, the original database file
1597 ** is unchanged and we can rollback without having to playback the
1598 ** journal into the original database file. Once we transition to
1599 ** EXCLUSIVE, it means the database file has been changed and any rollback
1600 ** will require a journal playback.
danielk19779eed5052004-06-04 10:38:30 +00001601 */
danielk19779eed5052004-06-04 10:38:30 +00001602 do {
1603 rc = sqlite3OsLock(&pPager->fd, EXCLUSIVE_LOCK);
1604 }while( rc==SQLITE_BUSY &&
1605 pPager->pBusyHandler &&
1606 pPager->pBusyHandler->xFunc &&
1607 pPager->pBusyHandler->xFunc(pPager->pBusyHandler->pArg, "", busy++)
1608 );
1609 if( rc!=SQLITE_OK ){
1610 return rc;
1611 }
drha6abd042004-06-09 17:37:22 +00001612 pPager->state = PAGER_EXCLUSIVE;
danielk19779eed5052004-06-04 10:38:30 +00001613
drh2554f8b2003-01-22 01:26:44 +00001614 while( pList ){
1615 assert( pList->dirty );
danielk19774adee202004-05-08 08:23:19 +00001616 sqlite3OsSeek(&pPager->fd, (pList->pgno-1)*(off_t)SQLITE_PAGE_SIZE);
drh9eb9e262004-02-11 02:18:05 +00001617 CODEC(pPager, PGHDR_TO_DATA(pList), pList->pgno, 6);
drha6abd042004-06-09 17:37:22 +00001618 TRACE2("STORE page %d\n", pList->pgno);
danielk19774adee202004-05-08 08:23:19 +00001619 rc = sqlite3OsWrite(&pPager->fd, PGHDR_TO_DATA(pList), SQLITE_PAGE_SIZE);
drh9eb9e262004-02-11 02:18:05 +00001620 CODEC(pPager, PGHDR_TO_DATA(pList), pList->pgno, 0);
drh2554f8b2003-01-22 01:26:44 +00001621 if( rc ) return rc;
1622 pList->dirty = 0;
1623 pList = pList->pDirty;
1624 }
1625 return SQLITE_OK;
1626}
1627
1628/*
1629** Collect every dirty page into a dirty list and
1630** return a pointer to the head of that list. All pages are
1631** collected even if they are still in use.
1632*/
1633static PgHdr *pager_get_all_dirty_pages(Pager *pPager){
1634 PgHdr *p, *pList;
1635 pList = 0;
1636 for(p=pPager->pAll; p; p=p->pNextAll){
1637 if( p->dirty ){
1638 p->pDirty = pList;
1639 pList = p;
1640 }
1641 }
1642 return pList;
1643}
1644
1645/*
drhd9b02572001-04-15 00:37:09 +00001646** Acquire a page.
1647**
drh58a11682001-11-10 13:51:08 +00001648** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00001649** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00001650**
drh306dc212001-05-21 13:45:10 +00001651** A _get works for any page number greater than 0. If the database
1652** file is smaller than the requested page, then no actual disk
1653** read occurs and the memory image of the page is initialized to
1654** all zeros. The extra data appended to a page is always initialized
1655** to zeros the first time a page is loaded into memory.
1656**
drhd9b02572001-04-15 00:37:09 +00001657** The acquisition might fail for several reasons. In all cases,
1658** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00001659**
drh3aac2dd2004-04-26 14:10:20 +00001660** See also sqlite3pager_lookup(). Both this routine and _lookup() attempt
drh7e3b0a02001-04-28 16:52:40 +00001661** to find a page in the in-memory cache first. If the page is not already
drh5e00f6c2001-09-13 13:46:56 +00001662** in memory, this routine goes to disk to read it in whereas _lookup()
drh7e3b0a02001-04-28 16:52:40 +00001663** just returns 0. This routine acquires a read-lock the first time it
1664** has to go to disk, and could also playback an old journal if necessary.
1665** Since _lookup() never goes to disk, it never has to deal with locks
1666** or journal files.
drhed7c8552001-04-11 14:29:21 +00001667*/
drh3aac2dd2004-04-26 14:10:20 +00001668int sqlite3pager_get(Pager *pPager, Pgno pgno, void **ppPage){
drhed7c8552001-04-11 14:29:21 +00001669 PgHdr *pPg;
drh8766c342002-11-09 00:33:15 +00001670 int rc;
drhed7c8552001-04-11 14:29:21 +00001671
drhd9b02572001-04-15 00:37:09 +00001672 /* Make sure we have not hit any critical errors.
1673 */
drh836faa42003-01-11 13:30:57 +00001674 assert( pPager!=0 );
1675 assert( pgno!=0 );
drh2e6d11b2003-04-25 15:37:57 +00001676 *ppPage = 0;
drhd9b02572001-04-15 00:37:09 +00001677 if( pPager->errMask & ~(PAGER_ERR_FULL) ){
1678 return pager_errcode(pPager);
1679 }
1680
danielk197713adf8a2004-06-03 16:08:41 +00001681 /* If this is the first page accessed, then get a SHARED lock
drhed7c8552001-04-11 14:29:21 +00001682 ** on the database file.
1683 */
drhac69b052004-05-12 13:30:07 +00001684 if( pPager->nRef==0 && !pPager->memDb ){
danielk197724162fe2004-06-04 06:22:00 +00001685 int busy = 1;
danielk19779eed5052004-06-04 10:38:30 +00001686 do {
1687 rc = sqlite3OsLock(&pPager->fd, SHARED_LOCK);
1688 }while( rc==SQLITE_BUSY &&
1689 pPager->pBusyHandler &&
1690 pPager->pBusyHandler->xFunc &&
1691 pPager->pBusyHandler->xFunc(pPager->pBusyHandler->pArg, "", busy++)
1692 );
1693 if( rc!=SQLITE_OK ){
1694 return rc;
drhed7c8552001-04-11 14:29:21 +00001695 }
drha6abd042004-06-09 17:37:22 +00001696 pPager->state = PAGER_SHARED;
drhed7c8552001-04-11 14:29:21 +00001697
danielk197713adf8a2004-06-03 16:08:41 +00001698 /* If a journal file exists, and there is no RESERVED lock on the
1699 ** database file, then it either needs to be played back or deleted.
drhed7c8552001-04-11 14:29:21 +00001700 */
danielk197713adf8a2004-06-03 16:08:41 +00001701 if( pPager->useJournal &&
1702 sqlite3OsFileExists(pPager->zJournal) &&
drha6abd042004-06-09 17:37:22 +00001703 !sqlite3OsCheckReservedLock(&pPager->fd)
danielk197713adf8a2004-06-03 16:08:41 +00001704 ){
drhe2227f02003-06-14 11:42:57 +00001705 int rc;
drhed7c8552001-04-11 14:29:21 +00001706
danielk197713adf8a2004-06-03 16:08:41 +00001707 /* Get an EXCLUSIVE lock on the database file. */
1708 rc = sqlite3OsLock(&pPager->fd, EXCLUSIVE_LOCK);
drha7fcb052001-12-14 15:09:55 +00001709 if( rc!=SQLITE_OK ){
drha6abd042004-06-09 17:37:22 +00001710 sqlite3OsUnlock(&pPager->fd, NO_LOCK);
1711 pPager->state = PAGER_UNLOCK;
drh8766c342002-11-09 00:33:15 +00001712 return rc;
drha7fcb052001-12-14 15:09:55 +00001713 }
drha6abd042004-06-09 17:37:22 +00001714 pPager->state = PAGER_EXCLUSIVE;
drha7fcb052001-12-14 15:09:55 +00001715
drhe2227f02003-06-14 11:42:57 +00001716 /* Open the journal for reading only. Return SQLITE_BUSY if
1717 ** we are unable to open the journal file.
drhf57b3392001-10-08 13:22:32 +00001718 **
drhe2227f02003-06-14 11:42:57 +00001719 ** The journal file does not need to be locked itself. The
1720 ** journal file is never open unless the main database file holds
1721 ** a write lock, so there is never any chance of two or more
1722 ** processes opening the journal at the same time.
drhed7c8552001-04-11 14:29:21 +00001723 */
danielk19774adee202004-05-08 08:23:19 +00001724 rc = sqlite3OsOpenReadOnly(pPager->zJournal, &pPager->jfd);
drha7fcb052001-12-14 15:09:55 +00001725 if( rc!=SQLITE_OK ){
drha6abd042004-06-09 17:37:22 +00001726 sqlite3OsUnlock(&pPager->fd, NO_LOCK);
1727 pPager->state = PAGER_UNLOCK;
drhed7c8552001-04-11 14:29:21 +00001728 return SQLITE_BUSY;
1729 }
drha7fcb052001-12-14 15:09:55 +00001730 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00001731 pPager->journalStarted = 0;
drhed7c8552001-04-11 14:29:21 +00001732
1733 /* Playback and delete the journal. Drop the database write
1734 ** lock and reacquire the read lock.
1735 */
drh99ee3602003-02-16 19:13:36 +00001736 rc = pager_playback(pPager, 0);
drhd9b02572001-04-15 00:37:09 +00001737 if( rc!=SQLITE_OK ){
1738 return rc;
1739 }
drhed7c8552001-04-11 14:29:21 +00001740 }
1741 pPg = 0;
1742 }else{
1743 /* Search for page in cache */
drhd9b02572001-04-15 00:37:09 +00001744 pPg = pager_lookup(pPager, pgno);
drha6abd042004-06-09 17:37:22 +00001745 if( pPager->memDb && pPager->state==PAGER_UNLOCK ){
1746 pPager->state = PAGER_SHARED;
drhac69b052004-05-12 13:30:07 +00001747 }
drhed7c8552001-04-11 14:29:21 +00001748 }
1749 if( pPg==0 ){
drhd9b02572001-04-15 00:37:09 +00001750 /* The requested page is not in the page cache. */
drhed7c8552001-04-11 14:29:21 +00001751 int h;
drh7e3b0a02001-04-28 16:52:40 +00001752 pPager->nMiss++;
drhac69b052004-05-12 13:30:07 +00001753 if( pPager->nPage<pPager->mxPage || pPager->pFirst==0 || pPager->memDb ){
drhed7c8552001-04-11 14:29:21 +00001754 /* Create a new page */
drhd0ba1932004-02-10 01:54:28 +00001755 pPg = sqliteMallocRaw( sizeof(*pPg) + SQLITE_PAGE_SIZE
drhac69b052004-05-12 13:30:07 +00001756 + sizeof(u32) + pPager->nExtra
1757 + pPager->memDb*sizeof(PgHistory) );
drhd9b02572001-04-15 00:37:09 +00001758 if( pPg==0 ){
drhd9b02572001-04-15 00:37:09 +00001759 pager_unwritelock(pPager);
1760 pPager->errMask |= PAGER_ERR_MEM;
1761 return SQLITE_NOMEM;
1762 }
drh8c1238a2003-01-02 14:43:55 +00001763 memset(pPg, 0, sizeof(*pPg));
drhac69b052004-05-12 13:30:07 +00001764 if( pPager->memDb ){
1765 memset(PGHDR_TO_HIST(pPg, pPager), 0, sizeof(PgHistory));
1766 }
drhed7c8552001-04-11 14:29:21 +00001767 pPg->pPager = pPager;
drhd9b02572001-04-15 00:37:09 +00001768 pPg->pNextAll = pPager->pAll;
drhd79caeb2001-04-15 02:27:24 +00001769 pPager->pAll = pPg;
drhd9b02572001-04-15 00:37:09 +00001770 pPager->nPage++;
drhed7c8552001-04-11 14:29:21 +00001771 }else{
drhdb48ee02003-01-16 13:42:43 +00001772 /* Find a page to recycle. Try to locate a page that does not
1773 ** require us to do an fsync() on the journal.
1774 */
drh341eae82003-01-21 02:39:36 +00001775 pPg = pPager->pFirstSynced;
drhb19a2bc2001-09-16 00:13:26 +00001776
drhdb48ee02003-01-16 13:42:43 +00001777 /* If we could not find a page that does not require an fsync()
1778 ** on the journal file then fsync the journal file. This is a
1779 ** very slow operation, so we work hard to avoid it. But sometimes
1780 ** it can't be helped.
drhb19a2bc2001-09-16 00:13:26 +00001781 */
drh603240c2002-03-05 01:11:12 +00001782 if( pPg==0 ){
danielk197713adf8a2004-06-03 16:08:41 +00001783 int rc = syncJournal(pPager, 0);
drh50e5dad2001-09-15 00:57:28 +00001784 if( rc!=0 ){
drh3aac2dd2004-04-26 14:10:20 +00001785 sqlite3pager_rollback(pPager);
drh50e5dad2001-09-15 00:57:28 +00001786 return SQLITE_IOERR;
1787 }
1788 pPg = pPager->pFirst;
1789 }
drhd9b02572001-04-15 00:37:09 +00001790 assert( pPg->nRef==0 );
drhdb48ee02003-01-16 13:42:43 +00001791
1792 /* Write the page to the database file if it is dirty.
1793 */
1794 if( pPg->dirty ){
1795 assert( pPg->needSync==0 );
drh2554f8b2003-01-22 01:26:44 +00001796 pPg->pDirty = 0;
1797 rc = pager_write_pagelist( pPg );
drhdb48ee02003-01-16 13:42:43 +00001798 if( rc!=SQLITE_OK ){
drh3aac2dd2004-04-26 14:10:20 +00001799 sqlite3pager_rollback(pPager);
drhdb48ee02003-01-16 13:42:43 +00001800 return SQLITE_IOERR;
1801 }
drhdb48ee02003-01-16 13:42:43 +00001802 }
drh50e5dad2001-09-15 00:57:28 +00001803 assert( pPg->dirty==0 );
drhd9b02572001-04-15 00:37:09 +00001804
drhdb48ee02003-01-16 13:42:43 +00001805 /* If the page we are recycling is marked as alwaysRollback, then
drh193a6b42002-07-07 16:52:46 +00001806 ** set the global alwaysRollback flag, thus disabling the
1807 ** sqlite_dont_rollback() optimization for the rest of this transaction.
1808 ** It is necessary to do this because the page marked alwaysRollback
1809 ** might be reloaded at a later time but at that point we won't remember
1810 ** that is was marked alwaysRollback. This means that all pages must
1811 ** be marked as alwaysRollback from here on out.
1812 */
1813 if( pPg->alwaysRollback ){
1814 pPager->alwaysRollback = 1;
1815 }
1816
drhd9b02572001-04-15 00:37:09 +00001817 /* Unlink the old page from the free list and the hash table
1818 */
drhac69b052004-05-12 13:30:07 +00001819 unlinkPage(pPg);
drhd9b02572001-04-15 00:37:09 +00001820 pPager->nOvfl++;
drhed7c8552001-04-11 14:29:21 +00001821 }
1822 pPg->pgno = pgno;
drh1ab43002002-01-14 09:28:19 +00001823 if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){
danielk19774adee202004-05-08 08:23:19 +00001824 sqlite3CheckMemory(pPager->aInJournal, pgno/8);
drhdb48ee02003-01-16 13:42:43 +00001825 assert( pPager->journalOpen );
drh6019e162001-07-02 17:51:45 +00001826 pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0;
drhdb48ee02003-01-16 13:42:43 +00001827 pPg->needSync = 0;
drh6019e162001-07-02 17:51:45 +00001828 }else{
1829 pPg->inJournal = 0;
drhdb48ee02003-01-16 13:42:43 +00001830 pPg->needSync = 0;
drh6019e162001-07-02 17:51:45 +00001831 }
drhac69b052004-05-12 13:30:07 +00001832 if( pPager->aInStmt && (int)pgno<=pPager->stmtSize
1833 && (pPager->aInStmt[pgno/8] & (1<<(pgno&7)))!=0 ){
drh3aac2dd2004-04-26 14:10:20 +00001834 page_add_to_stmt_list(pPg);
drhfa86c412002-02-02 15:01:15 +00001835 }else{
drh3aac2dd2004-04-26 14:10:20 +00001836 page_remove_from_stmt_list(pPg);
drhfa86c412002-02-02 15:01:15 +00001837 }
drhed7c8552001-04-11 14:29:21 +00001838 pPg->dirty = 0;
1839 pPg->nRef = 1;
drhdd793422001-06-28 01:54:48 +00001840 REFINFO(pPg);
drhd9b02572001-04-15 00:37:09 +00001841 pPager->nRef++;
1842 h = pager_hash(pgno);
drhed7c8552001-04-11 14:29:21 +00001843 pPg->pNextHash = pPager->aHash[h];
1844 pPager->aHash[h] = pPg;
1845 if( pPg->pNextHash ){
1846 assert( pPg->pNextHash->pPrevHash==0 );
1847 pPg->pNextHash->pPrevHash = pPg;
1848 }
drh2e6d11b2003-04-25 15:37:57 +00001849 if( pPager->nExtra>0 ){
1850 memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra);
1851 }
drh3aac2dd2004-04-26 14:10:20 +00001852 if( pPager->dbSize<0 ) sqlite3pager_pagecount(pPager);
drh2e6d11b2003-04-25 15:37:57 +00001853 if( pPager->errMask!=0 ){
drh3aac2dd2004-04-26 14:10:20 +00001854 sqlite3pager_unref(PGHDR_TO_DATA(pPg));
drh2e6d11b2003-04-25 15:37:57 +00001855 rc = pager_errcode(pPager);
1856 return rc;
1857 }
drh1ab43002002-01-14 09:28:19 +00001858 if( pPager->dbSize<(int)pgno ){
drhd0ba1932004-02-10 01:54:28 +00001859 memset(PGHDR_TO_DATA(pPg), 0, SQLITE_PAGE_SIZE);
drh306dc212001-05-21 13:45:10 +00001860 }else{
drh81a20f22001-10-12 17:30:04 +00001861 int rc;
drhac69b052004-05-12 13:30:07 +00001862 assert( pPager->memDb==0 );
danielk19774adee202004-05-08 08:23:19 +00001863 sqlite3OsSeek(&pPager->fd, (pgno-1)*(off_t)SQLITE_PAGE_SIZE);
1864 rc = sqlite3OsRead(&pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE);
drha6abd042004-06-09 17:37:22 +00001865 TRACE2("FETCH page %d\n", pPg->pgno);
drh9eb9e262004-02-11 02:18:05 +00001866 CODEC(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3);
drh81a20f22001-10-12 17:30:04 +00001867 if( rc!=SQLITE_OK ){
drh28be87c2002-11-05 23:03:02 +00001868 off_t fileSize;
danielk19774adee202004-05-08 08:23:19 +00001869 if( sqlite3OsFileSize(&pPager->fd,&fileSize)!=SQLITE_OK
drhd0ba1932004-02-10 01:54:28 +00001870 || fileSize>=pgno*SQLITE_PAGE_SIZE ){
drh3aac2dd2004-04-26 14:10:20 +00001871 sqlite3pager_unref(PGHDR_TO_DATA(pPg));
drh4e371ee2002-09-05 16:08:27 +00001872 return rc;
1873 }else{
drhd0ba1932004-02-10 01:54:28 +00001874 memset(PGHDR_TO_DATA(pPg), 0, SQLITE_PAGE_SIZE);
drh4e371ee2002-09-05 16:08:27 +00001875 }
drh81a20f22001-10-12 17:30:04 +00001876 }
drh306dc212001-05-21 13:45:10 +00001877 }
drhed7c8552001-04-11 14:29:21 +00001878 }else{
drhd9b02572001-04-15 00:37:09 +00001879 /* The requested page is in the page cache. */
drh7e3b0a02001-04-28 16:52:40 +00001880 pPager->nHit++;
drhdf0b3b02001-06-23 11:36:20 +00001881 page_ref(pPg);
drhed7c8552001-04-11 14:29:21 +00001882 }
1883 *ppPage = PGHDR_TO_DATA(pPg);
1884 return SQLITE_OK;
1885}
1886
1887/*
drh7e3b0a02001-04-28 16:52:40 +00001888** Acquire a page if it is already in the in-memory cache. Do
1889** not read the page from disk. Return a pointer to the page,
1890** or 0 if the page is not in cache.
1891**
drh3aac2dd2004-04-26 14:10:20 +00001892** See also sqlite3pager_get(). The difference between this routine
1893** and sqlite3pager_get() is that _get() will go to the disk and read
drh7e3b0a02001-04-28 16:52:40 +00001894** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00001895** returns NULL if the page is not in cache or if a disk I/O error
1896** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00001897*/
drh3aac2dd2004-04-26 14:10:20 +00001898void *sqlite3pager_lookup(Pager *pPager, Pgno pgno){
drh7e3b0a02001-04-28 16:52:40 +00001899 PgHdr *pPg;
1900
drh836faa42003-01-11 13:30:57 +00001901 assert( pPager!=0 );
1902 assert( pgno!=0 );
drh7e3b0a02001-04-28 16:52:40 +00001903 if( pPager->errMask & ~(PAGER_ERR_FULL) ){
1904 return 0;
1905 }
drh7e3b0a02001-04-28 16:52:40 +00001906 pPg = pager_lookup(pPager, pgno);
1907 if( pPg==0 ) return 0;
drhdf0b3b02001-06-23 11:36:20 +00001908 page_ref(pPg);
drh7e3b0a02001-04-28 16:52:40 +00001909 return PGHDR_TO_DATA(pPg);
1910}
1911
1912/*
drhed7c8552001-04-11 14:29:21 +00001913** Release a page.
1914**
1915** If the number of references to the page drop to zero, then the
1916** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00001917** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00001918** removed.
1919*/
drh3aac2dd2004-04-26 14:10:20 +00001920int sqlite3pager_unref(void *pData){
drhed7c8552001-04-11 14:29:21 +00001921 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +00001922
1923 /* Decrement the reference count for this page
1924 */
drhed7c8552001-04-11 14:29:21 +00001925 pPg = DATA_TO_PGHDR(pData);
1926 assert( pPg->nRef>0 );
drhed7c8552001-04-11 14:29:21 +00001927 pPg->nRef--;
drhdd793422001-06-28 01:54:48 +00001928 REFINFO(pPg);
drhd9b02572001-04-15 00:37:09 +00001929
drh72f82862001-05-24 21:06:34 +00001930 /* When the number of references to a page reach 0, call the
1931 ** destructor and add the page to the freelist.
drhd9b02572001-04-15 00:37:09 +00001932 */
drhed7c8552001-04-11 14:29:21 +00001933 if( pPg->nRef==0 ){
drh1eaa2692001-09-18 02:02:23 +00001934 Pager *pPager;
1935 pPager = pPg->pPager;
drhd9b02572001-04-15 00:37:09 +00001936 pPg->pNextFree = 0;
1937 pPg->pPrevFree = pPager->pLast;
drhed7c8552001-04-11 14:29:21 +00001938 pPager->pLast = pPg;
drhd9b02572001-04-15 00:37:09 +00001939 if( pPg->pPrevFree ){
1940 pPg->pPrevFree->pNextFree = pPg;
drhed7c8552001-04-11 14:29:21 +00001941 }else{
1942 pPager->pFirst = pPg;
1943 }
drh341eae82003-01-21 02:39:36 +00001944 if( pPg->needSync==0 && pPager->pFirstSynced==0 ){
1945 pPager->pFirstSynced = pPg;
1946 }
drh72f82862001-05-24 21:06:34 +00001947 if( pPager->xDestructor ){
drhb6f41482004-05-14 01:58:11 +00001948 pPager->xDestructor(pData, pPager->pageSize);
drh72f82862001-05-24 21:06:34 +00001949 }
drhd9b02572001-04-15 00:37:09 +00001950
1951 /* When all pages reach the freelist, drop the read lock from
1952 ** the database file.
1953 */
1954 pPager->nRef--;
1955 assert( pPager->nRef>=0 );
drhac69b052004-05-12 13:30:07 +00001956 if( pPager->nRef==0 && !pPager->memDb ){
drhd9b02572001-04-15 00:37:09 +00001957 pager_reset(pPager);
1958 }
drhed7c8552001-04-11 14:29:21 +00001959 }
drhd9b02572001-04-15 00:37:09 +00001960 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00001961}
1962
1963/*
drha6abd042004-06-09 17:37:22 +00001964** Create a journal file for pPager. There should already be a RESERVED
1965** or EXCLUSIVE lock on the database file when this routine is called.
drhda47d772002-12-02 04:25:19 +00001966**
1967** Return SQLITE_OK if everything. Return an error code and release the
1968** write lock if anything goes wrong.
1969*/
1970static int pager_open_journal(Pager *pPager){
1971 int rc;
drha6abd042004-06-09 17:37:22 +00001972 assert( pPager->state>=PAGER_RESERVED );
drhda47d772002-12-02 04:25:19 +00001973 assert( pPager->journalOpen==0 );
1974 assert( pPager->useJournal );
drh3aac2dd2004-04-26 14:10:20 +00001975 sqlite3pager_pagecount(pPager);
drhda47d772002-12-02 04:25:19 +00001976 pPager->aInJournal = sqliteMalloc( pPager->dbSize/8 + 1 );
1977 if( pPager->aInJournal==0 ){
drha6abd042004-06-09 17:37:22 +00001978 sqlite3OsUnlock(&pPager->fd, SHARED_LOCK);
1979 pPager->state = PAGER_SHARED;
drhda47d772002-12-02 04:25:19 +00001980 return SQLITE_NOMEM;
1981 }
danielk19774adee202004-05-08 08:23:19 +00001982 rc = sqlite3OsOpenExclusive(pPager->zJournal, &pPager->jfd,pPager->tempFile);
drhda47d772002-12-02 04:25:19 +00001983 if( rc!=SQLITE_OK ){
1984 sqliteFree(pPager->aInJournal);
1985 pPager->aInJournal = 0;
drha6abd042004-06-09 17:37:22 +00001986 sqlite3OsUnlock(&pPager->fd, SHARED_LOCK);
1987 pPager->state = PAGER_SHARED;
drhda47d772002-12-02 04:25:19 +00001988 return SQLITE_CANTOPEN;
1989 }
danielk19774adee202004-05-08 08:23:19 +00001990 sqlite3OsOpenDirectory(pPager->zDirectory, &pPager->jfd);
drhda47d772002-12-02 04:25:19 +00001991 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00001992 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00001993 pPager->needSync = 0;
1994 pPager->alwaysRollback = 0;
drh968af522003-02-11 14:55:40 +00001995 pPager->nRec = 0;
drh2e6d11b2003-04-25 15:37:57 +00001996 if( pPager->errMask!=0 ){
1997 rc = pager_errcode(pPager);
1998 return rc;
1999 }
drhda47d772002-12-02 04:25:19 +00002000 pPager->origDbSize = pPager->dbSize;
drh968af522003-02-11 14:55:40 +00002001 if( journal_format==JOURNAL_FORMAT_3 ){
danielk197713adf8a2004-06-03 16:08:41 +00002002 /* Create the header for a format 3 journal:
2003 ** - 8 bytes: Magic identifying journal format 3.
2004 ** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
2005 ** - 4 bytes: Magic used for page checksums.
2006 ** - 4 bytes: Number of bytes reserved for master journal ptr (nMaster)
2007 ** - nMaster bytes: Space for a master journal pointer.
2008 ** - 4 bytes: Initial database page count.
2009 */
danielk19774adee202004-05-08 08:23:19 +00002010 rc = sqlite3OsWrite(&pPager->jfd, aJournalMagic3, sizeof(aJournalMagic3));
drh968af522003-02-11 14:55:40 +00002011 if( rc==SQLITE_OK ){
drh4303fee2003-02-15 23:09:17 +00002012 rc = write32bits(&pPager->jfd, pPager->noSync ? 0xffffffff : 0);
drh968af522003-02-11 14:55:40 +00002013 }
2014 if( rc==SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00002015 sqlite3Randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
drh968af522003-02-11 14:55:40 +00002016 rc = write32bits(&pPager->jfd, pPager->cksumInit);
2017 }
danielk197713adf8a2004-06-03 16:08:41 +00002018 if( rc==SQLITE_OK ){
2019 rc = write32bits(&pPager->jfd, pPager->nMaster);
2020 }
2021
2022 /* Unless the size reserved for the master-journal pointer is 0, set
2023 ** the first byte of the master journal pointer to 0x00. Either way,
2024 ** this is interpreted as 'no master journal' in the event of a
2025 ** rollback after a crash.
2026 */
2027 if( rc==SQLITE_OK && pPager->nMaster>0 ){
2028 rc = sqlite3OsWrite(&pPager->jfd, "", 1);
2029 }
2030 if( rc==SQLITE_OK ){
2031 rc = sqlite3OsSeek(&pPager->jfd,
2032 sizeof(aJournalMagic3) + 3*4 + pPager->nMaster);
2033 }
drh968af522003-02-11 14:55:40 +00002034 }else if( journal_format==JOURNAL_FORMAT_2 ){
danielk19774adee202004-05-08 08:23:19 +00002035 rc = sqlite3OsWrite(&pPager->jfd, aJournalMagic2, sizeof(aJournalMagic2));
drhda47d772002-12-02 04:25:19 +00002036 }else{
drh968af522003-02-11 14:55:40 +00002037 assert( journal_format==JOURNAL_FORMAT_1 );
danielk19774adee202004-05-08 08:23:19 +00002038 rc = sqlite3OsWrite(&pPager->jfd, aJournalMagic1, sizeof(aJournalMagic1));
drhda47d772002-12-02 04:25:19 +00002039 }
2040 if( rc==SQLITE_OK ){
2041 rc = write32bits(&pPager->jfd, pPager->dbSize);
2042 }
drhac69b052004-05-12 13:30:07 +00002043 if( pPager->stmtAutoopen && rc==SQLITE_OK ){
drh3aac2dd2004-04-26 14:10:20 +00002044 rc = sqlite3pager_stmt_begin(pPager);
drhda47d772002-12-02 04:25:19 +00002045 }
2046 if( rc!=SQLITE_OK ){
2047 rc = pager_unwritelock(pPager);
2048 if( rc==SQLITE_OK ){
2049 rc = SQLITE_FULL;
2050 }
2051 }
2052 return rc;
2053}
2054
2055/*
drh4b845d72002-03-05 12:41:19 +00002056** Acquire a write-lock on the database. The lock is removed when
2057** the any of the following happen:
2058**
drh3aac2dd2004-04-26 14:10:20 +00002059** * sqlite3pager_commit() is called.
2060** * sqlite3pager_rollback() is called.
2061** * sqlite3pager_close() is called.
2062** * sqlite3pager_unref() is called to on every outstanding page.
drh4b845d72002-03-05 12:41:19 +00002063**
danielk197713adf8a2004-06-03 16:08:41 +00002064** The first parameter to this routine is a pointer to any open page of the
2065** database file. Nothing changes about the page - it is used merely to
2066** acquire a pointer to the Pager structure and as proof that there is
2067** already a read-lock on the database.
drh4b845d72002-03-05 12:41:19 +00002068**
danielk197713adf8a2004-06-03 16:08:41 +00002069** The second parameter indicates how much space in bytes to reserve for a
2070** master journal file-name at the start of the journal when it is created.
2071**
2072** A journal file is opened if this is not a temporary file. For temporary
2073** files, the opening of the journal file is deferred until there is an
2074** actual need to write to the journal.
drhda47d772002-12-02 04:25:19 +00002075**
drha6abd042004-06-09 17:37:22 +00002076** If the database is already reserved for writing, this routine is a no-op.
drh4b845d72002-03-05 12:41:19 +00002077*/
danielk197713adf8a2004-06-03 16:08:41 +00002078int sqlite3pager_begin(void *pData, int nMaster){
drh4b845d72002-03-05 12:41:19 +00002079 PgHdr *pPg = DATA_TO_PGHDR(pData);
2080 Pager *pPager = pPg->pPager;
2081 int rc = SQLITE_OK;
2082 assert( pPg->nRef>0 );
danielk197713adf8a2004-06-03 16:08:41 +00002083 assert( nMaster>=0 );
drha6abd042004-06-09 17:37:22 +00002084 assert( pPager->state!=PAGER_UNLOCK );
2085 if( pPager->state==PAGER_SHARED ){
drh4b845d72002-03-05 12:41:19 +00002086 assert( pPager->aInJournal==0 );
drhac69b052004-05-12 13:30:07 +00002087 if( pPager->memDb ){
drha6abd042004-06-09 17:37:22 +00002088 pPager->state = PAGER_EXCLUSIVE;
drhac69b052004-05-12 13:30:07 +00002089 pPager->origDbSize = pPager->dbSize;
2090 }else{
danielk197724162fe2004-06-04 06:22:00 +00002091 int busy = 1;
danielk19779eed5052004-06-04 10:38:30 +00002092 do {
danielk19779eed5052004-06-04 10:38:30 +00002093 rc = sqlite3OsLock(&pPager->fd, RESERVED_LOCK);
2094 }while( rc==SQLITE_BUSY &&
2095 pPager->pBusyHandler &&
2096 pPager->pBusyHandler->xFunc &&
2097 pPager->pBusyHandler->xFunc(pPager->pBusyHandler->pArg, "", busy++)
2098 );
2099 if( rc!=SQLITE_OK ){
2100 return rc;
drhac69b052004-05-12 13:30:07 +00002101 }
danielk197713adf8a2004-06-03 16:08:41 +00002102 pPager->nMaster = nMaster;
drha6abd042004-06-09 17:37:22 +00002103 pPager->state = PAGER_RESERVED;
2104 pPager->dirtyCache = 0;
2105 TRACE3("TRANSACTION %d nMaster=%d\n", pPager->fd.h, nMaster);
drhac69b052004-05-12 13:30:07 +00002106 if( pPager->useJournal && !pPager->tempFile ){
2107 rc = pager_open_journal(pPager);
2108 }
drh4b845d72002-03-05 12:41:19 +00002109 }
2110 }
2111 return rc;
2112}
2113
2114/*
drhed7c8552001-04-11 14:29:21 +00002115** Mark a data page as writeable. The page is written into the journal
2116** if it is not there already. This routine must be called before making
2117** changes to a page.
2118**
2119** The first time this routine is called, the pager creates a new
drha6abd042004-06-09 17:37:22 +00002120** journal and acquires a RESERVED lock on the database. If the RESERVED
drhed7c8552001-04-11 14:29:21 +00002121** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00002122** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00002123** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00002124**
2125** If the journal file could not be written because the disk is full,
2126** then this routine returns SQLITE_FULL and does an immediate rollback.
2127** All subsequent write attempts also return SQLITE_FULL until there
drh3aac2dd2004-04-26 14:10:20 +00002128** is a call to sqlite3pager_commit() or sqlite3pager_rollback() to
drhd9b02572001-04-15 00:37:09 +00002129** reset.
drhed7c8552001-04-11 14:29:21 +00002130*/
drh3aac2dd2004-04-26 14:10:20 +00002131int sqlite3pager_write(void *pData){
drh69688d52001-04-14 16:38:23 +00002132 PgHdr *pPg = DATA_TO_PGHDR(pData);
2133 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00002134 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00002135
drh6446c4d2001-12-15 14:22:18 +00002136 /* Check for errors
2137 */
drhd9b02572001-04-15 00:37:09 +00002138 if( pPager->errMask ){
2139 return pager_errcode(pPager);
2140 }
drh5e00f6c2001-09-13 13:46:56 +00002141 if( pPager->readOnly ){
2142 return SQLITE_PERM;
2143 }
drh6446c4d2001-12-15 14:22:18 +00002144
2145 /* Mark the page as dirty. If the page has already been written
2146 ** to the journal then we can return right away.
2147 */
drhd9b02572001-04-15 00:37:09 +00002148 pPg->dirty = 1;
drhac69b052004-05-12 13:30:07 +00002149 if( pPg->inJournal && (pPg->inStmt || pPager->stmtInUse==0) ){
drha6abd042004-06-09 17:37:22 +00002150 pPager->dirtyCache = 1;
drhfa86c412002-02-02 15:01:15 +00002151 return SQLITE_OK;
2152 }
drh6446c4d2001-12-15 14:22:18 +00002153
2154 /* If we get this far, it means that the page needs to be
drhfa86c412002-02-02 15:01:15 +00002155 ** written to the transaction journal or the ckeckpoint journal
2156 ** or both.
2157 **
2158 ** First check to see that the transaction journal exists and
2159 ** create it if it does not.
drh6446c4d2001-12-15 14:22:18 +00002160 */
drha6abd042004-06-09 17:37:22 +00002161 assert( pPager->state!=PAGER_UNLOCK );
danielk197713adf8a2004-06-03 16:08:41 +00002162 rc = sqlite3pager_begin(pData, 0);
drhda47d772002-12-02 04:25:19 +00002163 if( rc!=SQLITE_OK ){
2164 return rc;
2165 }
drha6abd042004-06-09 17:37:22 +00002166 assert( pPager->state>=PAGER_RESERVED );
drhda47d772002-12-02 04:25:19 +00002167 if( !pPager->journalOpen && pPager->useJournal ){
2168 rc = pager_open_journal(pPager);
2169 if( rc!=SQLITE_OK ) return rc;
2170 }
2171 assert( pPager->journalOpen || !pPager->useJournal );
drha6abd042004-06-09 17:37:22 +00002172 pPager->dirtyCache = 1;
drh6446c4d2001-12-15 14:22:18 +00002173
drha6abd042004-06-09 17:37:22 +00002174 /* The transaction journal now exists and we have a RESERVED or an
2175 ** EXCLUSIVE lock on the main database file. Write the current page to
2176 ** the transaction journal if it is not there already.
drh6446c4d2001-12-15 14:22:18 +00002177 */
drhac69b052004-05-12 13:30:07 +00002178 if( !pPg->inJournal && (pPager->useJournal || pPager->memDb) ){
drhdb48ee02003-01-16 13:42:43 +00002179 if( (int)pPg->pgno <= pPager->origDbSize ){
drh968af522003-02-11 14:55:40 +00002180 int szPg;
2181 u32 saved;
drhac69b052004-05-12 13:30:07 +00002182 if( pPager->memDb ){
2183 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
drha6abd042004-06-09 17:37:22 +00002184 TRACE2("JOURNAL page %d\n", pPg->pgno);
drhac69b052004-05-12 13:30:07 +00002185 assert( pHist->pOrig==0 );
2186 pHist->pOrig = sqliteMallocRaw( pPager->pageSize );
2187 if( pHist->pOrig ){
2188 memcpy(pHist->pOrig, PGHDR_TO_DATA(pPg), pPager->pageSize);
2189 }
2190 pPg->inJournal = 1;
danielk197713adf8a2004-06-03 16:08:41 +00002191 }else{
drhac69b052004-05-12 13:30:07 +00002192 if( journal_format>=JOURNAL_FORMAT_3 ){
2193 u32 cksum = pager_cksum(pPager, pPg->pgno, pData);
2194 saved = *(u32*)PGHDR_TO_EXTRA(pPg);
2195 store32bits(cksum, pPg, SQLITE_PAGE_SIZE);
2196 szPg = SQLITE_PAGE_SIZE+8;
2197 }else{
2198 szPg = SQLITE_PAGE_SIZE+4;
2199 }
2200 store32bits(pPg->pgno, pPg, -4);
2201 CODEC(pPager, pData, pPg->pgno, 7);
2202 rc = sqlite3OsWrite(&pPager->jfd, &((char*)pData)[-4], szPg);
drha6abd042004-06-09 17:37:22 +00002203 TRACE3("JOURNAL page %d needSync=%d\n", pPg->pgno, pPg->needSync);
drhac69b052004-05-12 13:30:07 +00002204 CODEC(pPager, pData, pPg->pgno, 0);
2205 if( journal_format>=JOURNAL_FORMAT_3 ){
2206 *(u32*)PGHDR_TO_EXTRA(pPg) = saved;
2207 }
2208 if( rc!=SQLITE_OK ){
2209 sqlite3pager_rollback(pPager);
2210 pPager->errMask |= PAGER_ERR_FULL;
2211 return rc;
2212 }
2213 pPager->nRec++;
2214 assert( pPager->aInJournal!=0 );
2215 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
2216 pPg->needSync = !pPager->noSync;
2217 pPg->inJournal = 1;
2218 if( pPager->stmtInUse ){
2219 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
2220 page_add_to_stmt_list(pPg);
2221 }
drhdb48ee02003-01-16 13:42:43 +00002222 }
drhdb48ee02003-01-16 13:42:43 +00002223 }else{
2224 pPg->needSync = !pPager->journalStarted && !pPager->noSync;
drha6abd042004-06-09 17:37:22 +00002225 TRACE3("APPEND page %d needSync=%d\n", pPg->pgno, pPg->needSync);
drhd9b02572001-04-15 00:37:09 +00002226 }
drhdb48ee02003-01-16 13:42:43 +00002227 if( pPg->needSync ){
2228 pPager->needSync = 1;
drhfa86c412002-02-02 15:01:15 +00002229 }
drh69688d52001-04-14 16:38:23 +00002230 }
drh6446c4d2001-12-15 14:22:18 +00002231
drhac69b052004-05-12 13:30:07 +00002232 /* If the statement journal is open and the page is not in it,
2233 ** then write the current page to the statement journal. Note that
2234 ** the statement journal always uses the simplier format 2 that lacks
2235 ** checksums. The header is also omitted from the statement journal.
drh6446c4d2001-12-15 14:22:18 +00002236 */
drhac69b052004-05-12 13:30:07 +00002237 if( pPager->stmtInUse && !pPg->inStmt && (int)pPg->pgno<=pPager->stmtSize ){
drh1e336b42002-02-14 12:50:33 +00002238 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
drhac69b052004-05-12 13:30:07 +00002239 if( pPager->memDb ){
2240 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
2241 assert( pHist->pStmt==0 );
2242 pHist->pStmt = sqliteMallocRaw( pPager->pageSize );
2243 if( pHist->pStmt ){
2244 memcpy(pHist->pStmt, PGHDR_TO_DATA(pPg), pPager->pageSize);
2245 }
drha6abd042004-06-09 17:37:22 +00002246 TRACE2("STMT-JOURNAL page %d\n", pPg->pgno);
drhac69b052004-05-12 13:30:07 +00002247 }else{
2248 store32bits(pPg->pgno, pPg, -4);
2249 CODEC(pPager, pData, pPg->pgno, 7);
2250 rc = sqlite3OsWrite(&pPager->stfd, ((char*)pData)-4, SQLITE_PAGE_SIZE+4);
drha6abd042004-06-09 17:37:22 +00002251 TRACE2("STMT-JOURNAL page %d\n", pPg->pgno);
drhac69b052004-05-12 13:30:07 +00002252 CODEC(pPager, pData, pPg->pgno, 0);
2253 if( rc!=SQLITE_OK ){
2254 sqlite3pager_rollback(pPager);
2255 pPager->errMask |= PAGER_ERR_FULL;
2256 return rc;
2257 }
2258 pPager->stmtNRec++;
2259 assert( pPager->aInStmt!=0 );
2260 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drhfa86c412002-02-02 15:01:15 +00002261 }
drh3aac2dd2004-04-26 14:10:20 +00002262 page_add_to_stmt_list(pPg);
drhfa86c412002-02-02 15:01:15 +00002263 }
2264
2265 /* Update the database size and return.
2266 */
drh1ab43002002-01-14 09:28:19 +00002267 if( pPager->dbSize<(int)pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00002268 pPager->dbSize = pPg->pgno;
2269 }
drh69688d52001-04-14 16:38:23 +00002270 return rc;
drhed7c8552001-04-11 14:29:21 +00002271}
2272
2273/*
drhaacc5432002-01-06 17:07:40 +00002274** Return TRUE if the page given in the argument was previously passed
drh3aac2dd2004-04-26 14:10:20 +00002275** to sqlite3pager_write(). In other words, return TRUE if it is ok
drh6019e162001-07-02 17:51:45 +00002276** to change the content of the page.
2277*/
drh3aac2dd2004-04-26 14:10:20 +00002278int sqlite3pager_iswriteable(void *pData){
drh6019e162001-07-02 17:51:45 +00002279 PgHdr *pPg = DATA_TO_PGHDR(pData);
2280 return pPg->dirty;
2281}
2282
2283/*
drh001bbcb2003-03-19 03:14:00 +00002284** Replace the content of a single page with the information in the third
2285** argument.
2286*/
drh3aac2dd2004-04-26 14:10:20 +00002287int sqlite3pager_overwrite(Pager *pPager, Pgno pgno, void *pData){
drh001bbcb2003-03-19 03:14:00 +00002288 void *pPage;
2289 int rc;
2290
drh3aac2dd2004-04-26 14:10:20 +00002291 rc = sqlite3pager_get(pPager, pgno, &pPage);
drh001bbcb2003-03-19 03:14:00 +00002292 if( rc==SQLITE_OK ){
drh3aac2dd2004-04-26 14:10:20 +00002293 rc = sqlite3pager_write(pPage);
drh001bbcb2003-03-19 03:14:00 +00002294 if( rc==SQLITE_OK ){
drhd0ba1932004-02-10 01:54:28 +00002295 memcpy(pPage, pData, SQLITE_PAGE_SIZE);
drh001bbcb2003-03-19 03:14:00 +00002296 }
drh3aac2dd2004-04-26 14:10:20 +00002297 sqlite3pager_unref(pPage);
drh001bbcb2003-03-19 03:14:00 +00002298 }
2299 return rc;
2300}
2301
2302/*
drh30e58752002-03-02 20:41:57 +00002303** A call to this routine tells the pager that it is not necessary to
2304** write the information on page "pgno" back to the disk, even though
2305** that page might be marked as dirty.
2306**
2307** The overlying software layer calls this routine when all of the data
2308** on the given page is unused. The pager marks the page as clean so
2309** that it does not get written to disk.
2310**
2311** Tests show that this optimization, together with the
drh3aac2dd2004-04-26 14:10:20 +00002312** sqlite3pager_dont_rollback() below, more than double the speed
drh30e58752002-03-02 20:41:57 +00002313** of large INSERT operations and quadruple the speed of large DELETEs.
drh8e298f92002-07-06 16:28:47 +00002314**
2315** When this routine is called, set the alwaysRollback flag to true.
drh3aac2dd2004-04-26 14:10:20 +00002316** Subsequent calls to sqlite3pager_dont_rollback() for the same page
drh8e298f92002-07-06 16:28:47 +00002317** will thereafter be ignored. This is necessary to avoid a problem
2318** where a page with data is added to the freelist during one part of
2319** a transaction then removed from the freelist during a later part
2320** of the same transaction and reused for some other purpose. When it
2321** is first added to the freelist, this routine is called. When reused,
2322** the dont_rollback() routine is called. But because the page contains
2323** critical data, we still need to be sure it gets rolled back in spite
2324** of the dont_rollback() call.
drh30e58752002-03-02 20:41:57 +00002325*/
drh3aac2dd2004-04-26 14:10:20 +00002326void sqlite3pager_dont_write(Pager *pPager, Pgno pgno){
drh30e58752002-03-02 20:41:57 +00002327 PgHdr *pPg;
drh8e298f92002-07-06 16:28:47 +00002328
drh30e58752002-03-02 20:41:57 +00002329 pPg = pager_lookup(pPager, pgno);
drh8e298f92002-07-06 16:28:47 +00002330 pPg->alwaysRollback = 1;
drh30e58752002-03-02 20:41:57 +00002331 if( pPg && pPg->dirty ){
drh8124a302002-06-25 14:43:57 +00002332 if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
2333 /* If this pages is the last page in the file and the file has grown
2334 ** during the current transaction, then do NOT mark the page as clean.
2335 ** When the database file grows, we must make sure that the last page
2336 ** gets written at least once so that the disk file will be the correct
2337 ** size. If you do not write this page and the size of the file
2338 ** on the disk ends up being too small, that can lead to database
2339 ** corruption during the next transaction.
2340 */
2341 }else{
drha6abd042004-06-09 17:37:22 +00002342 TRACE3("DONT_WRITE page %d of %d\n", pgno, pPager->fd.h);
drh8124a302002-06-25 14:43:57 +00002343 pPg->dirty = 0;
2344 }
drh30e58752002-03-02 20:41:57 +00002345 }
2346}
2347
2348/*
2349** A call to this routine tells the pager that if a rollback occurs,
2350** it is not necessary to restore the data on the given page. This
2351** means that the pager does not have to record the given page in the
2352** rollback journal.
2353*/
drh3aac2dd2004-04-26 14:10:20 +00002354void sqlite3pager_dont_rollback(void *pData){
drh30e58752002-03-02 20:41:57 +00002355 PgHdr *pPg = DATA_TO_PGHDR(pData);
2356 Pager *pPager = pPg->pPager;
2357
drha6abd042004-06-09 17:37:22 +00002358 if( pPager->state!=PAGER_EXCLUSIVE || pPager->journalOpen==0 ) return;
drhac69b052004-05-12 13:30:07 +00002359 if( pPg->alwaysRollback || pPager->alwaysRollback || pPager->memDb ) return;
drh30e58752002-03-02 20:41:57 +00002360 if( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ){
2361 assert( pPager->aInJournal!=0 );
2362 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
2363 pPg->inJournal = 1;
drhac69b052004-05-12 13:30:07 +00002364 if( pPager->stmtInUse ){
2365 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh3aac2dd2004-04-26 14:10:20 +00002366 page_add_to_stmt_list(pPg);
drh30e58752002-03-02 20:41:57 +00002367 }
drha6abd042004-06-09 17:37:22 +00002368 TRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, pPager->fd.h);
drh30e58752002-03-02 20:41:57 +00002369 }
drhac69b052004-05-12 13:30:07 +00002370 if( pPager->stmtInUse && !pPg->inStmt && (int)pPg->pgno<=pPager->stmtSize ){
drh30e58752002-03-02 20:41:57 +00002371 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
drhac69b052004-05-12 13:30:07 +00002372 assert( pPager->aInStmt!=0 );
2373 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh3aac2dd2004-04-26 14:10:20 +00002374 page_add_to_stmt_list(pPg);
drh30e58752002-03-02 20:41:57 +00002375 }
2376}
2377
drhac69b052004-05-12 13:30:07 +00002378
2379/*
2380** Clear a PgHistory block
2381*/
2382static void clearHistory(PgHistory *pHist){
2383 sqliteFree(pHist->pOrig);
2384 sqliteFree(pHist->pStmt);
2385 pHist->pOrig = 0;
2386 pHist->pStmt = 0;
2387}
2388
drh30e58752002-03-02 20:41:57 +00002389/*
drhed7c8552001-04-11 14:29:21 +00002390** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00002391**
2392** If the commit fails for any reason, a rollback attempt is made
2393** and an error code is returned. If the commit worked, SQLITE_OK
2394** is returned.
drhed7c8552001-04-11 14:29:21 +00002395*/
drh3aac2dd2004-04-26 14:10:20 +00002396int sqlite3pager_commit(Pager *pPager){
drha1b351a2001-09-14 16:42:12 +00002397 int rc;
drhed7c8552001-04-11 14:29:21 +00002398 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +00002399
2400 if( pPager->errMask==PAGER_ERR_FULL ){
drh3aac2dd2004-04-26 14:10:20 +00002401 rc = sqlite3pager_rollback(pPager);
drh4e371ee2002-09-05 16:08:27 +00002402 if( rc==SQLITE_OK ){
2403 rc = SQLITE_FULL;
2404 }
drhd9b02572001-04-15 00:37:09 +00002405 return rc;
2406 }
2407 if( pPager->errMask!=0 ){
2408 rc = pager_errcode(pPager);
2409 return rc;
2410 }
drha6abd042004-06-09 17:37:22 +00002411 if( pPager->state<PAGER_RESERVED ){
drhd9b02572001-04-15 00:37:09 +00002412 return SQLITE_ERROR;
2413 }
drha6abd042004-06-09 17:37:22 +00002414 TRACE2("COMMIT %d\n", pPager->fd.h);
drhac69b052004-05-12 13:30:07 +00002415 if( pPager->memDb ){
2416 pPg = pager_get_all_dirty_pages(pPager);
2417 while( pPg ){
2418 clearHistory(PGHDR_TO_HIST(pPg, pPager));
2419 pPg->dirty = 0;
2420 pPg->inJournal = 0;
2421 pPg->inStmt = 0;
2422 pPg->pPrevStmt = pPg->pNextStmt = 0;
2423 pPg = pPg->pDirty;
2424 }
2425 pPager->pStmt = 0;
drha6abd042004-06-09 17:37:22 +00002426 pPager->state = PAGER_SHARED;
drhac69b052004-05-12 13:30:07 +00002427 return SQLITE_OK;
2428 }
drha6abd042004-06-09 17:37:22 +00002429 if( pPager->dirtyCache==0 ){
danielk19774adee202004-05-08 08:23:19 +00002430 /* Exit early (without doing the time-consuming sqlite3OsSync() calls)
drha1680452002-04-18 01:56:57 +00002431 ** if there have been no changes to the database file. */
drh341eae82003-01-21 02:39:36 +00002432 assert( pPager->needSync==0 );
drha1680452002-04-18 01:56:57 +00002433 rc = pager_unwritelock(pPager);
2434 pPager->dbSize = -1;
2435 return rc;
2436 }
drhda47d772002-12-02 04:25:19 +00002437 assert( pPager->journalOpen );
drha6abd042004-06-09 17:37:22 +00002438#if 0
danielk197713adf8a2004-06-03 16:08:41 +00002439 rc = syncJournal(pPager, 0);
drh240c5792004-02-08 00:40:52 +00002440 if( rc!=SQLITE_OK ){
drhd9b02572001-04-15 00:37:09 +00002441 goto commit_abort;
drhed7c8552001-04-11 14:29:21 +00002442 }
drh2554f8b2003-01-22 01:26:44 +00002443 pPg = pager_get_all_dirty_pages(pPager);
2444 if( pPg ){
2445 rc = pager_write_pagelist(pPg);
danielk19774adee202004-05-08 08:23:19 +00002446 if( rc || (!pPager->noSync && sqlite3OsSync(&pPager->fd)!=SQLITE_OK) ){
drh2554f8b2003-01-22 01:26:44 +00002447 goto commit_abort;
2448 }
drh603240c2002-03-05 01:11:12 +00002449 }
danielk197713adf8a2004-06-03 16:08:41 +00002450#endif
2451 rc = sqlite3pager_sync(pPager, 0);
drha6abd042004-06-09 17:37:22 +00002452 if( rc!=SQLITE_OK ){
2453 goto commit_abort;
2454 }
drhd9b02572001-04-15 00:37:09 +00002455 rc = pager_unwritelock(pPager);
2456 pPager->dbSize = -1;
2457 return rc;
2458
2459 /* Jump here if anything goes wrong during the commit process.
2460 */
2461commit_abort:
drh3aac2dd2004-04-26 14:10:20 +00002462 rc = sqlite3pager_rollback(pPager);
drhd9b02572001-04-15 00:37:09 +00002463 if( rc==SQLITE_OK ){
2464 rc = SQLITE_FULL;
drhed7c8552001-04-11 14:29:21 +00002465 }
drhed7c8552001-04-11 14:29:21 +00002466 return rc;
2467}
2468
2469/*
drha6abd042004-06-09 17:37:22 +00002470** Rollback all changes. The database falls back to PAGER_SHARED mode.
drhed7c8552001-04-11 14:29:21 +00002471** All in-memory cache pages revert to their original data contents.
2472** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00002473**
2474** This routine cannot fail unless some other process is not following
2475** the correct locking protocol (SQLITE_PROTOCOL) or unless some other
2476** process is writing trash into the journal file (SQLITE_CORRUPT) or
2477** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
2478** codes are returned for all these occasions. Otherwise,
2479** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00002480*/
drh3aac2dd2004-04-26 14:10:20 +00002481int sqlite3pager_rollback(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00002482 int rc;
drha6abd042004-06-09 17:37:22 +00002483 TRACE2("ROLLBACK %d\n", pPager->fd.h);
drhac69b052004-05-12 13:30:07 +00002484 if( pPager->memDb ){
2485 PgHdr *p;
2486 for(p=pPager->pAll; p; p=p->pNextAll){
2487 PgHistory *pHist;
2488 if( !p->dirty ) continue;
2489 pHist = PGHDR_TO_HIST(p, pPager);
2490 if( pHist->pOrig ){
2491 memcpy(PGHDR_TO_DATA(p), pHist->pOrig, pPager->pageSize);
2492 TRACE2("ROLLBACK-PAGE %d\n", p->pgno);
2493 }else{
2494 TRACE2("PAGE %d is clean\n", p->pgno);
2495 }
2496 clearHistory(pHist);
2497 p->dirty = 0;
2498 p->inJournal = 0;
2499 p->inStmt = 0;
2500 p->pPrevStmt = p->pNextStmt = 0;
2501 }
2502 pPager->pStmt = 0;
2503 pPager->dbSize = pPager->origDbSize;
2504 memoryTruncate(pPager);
2505 pPager->stmtInUse = 0;
drha6abd042004-06-09 17:37:22 +00002506 pPager->state = PAGER_SHARED;
drhac69b052004-05-12 13:30:07 +00002507 return SQLITE_OK;
2508 }
2509
drha6abd042004-06-09 17:37:22 +00002510 if( !pPager->dirtyCache || !pPager->journalOpen ){
drhda47d772002-12-02 04:25:19 +00002511 rc = pager_unwritelock(pPager);
2512 pPager->dbSize = -1;
2513 return rc;
2514 }
drhdb48ee02003-01-16 13:42:43 +00002515
drhd9b02572001-04-15 00:37:09 +00002516 if( pPager->errMask!=0 && pPager->errMask!=PAGER_ERR_FULL ){
drha6abd042004-06-09 17:37:22 +00002517 if( pPager->state>=PAGER_EXCLUSIVE ){
drh99ee3602003-02-16 19:13:36 +00002518 pager_playback(pPager, 1);
drh4b845d72002-03-05 12:41:19 +00002519 }
drhd9b02572001-04-15 00:37:09 +00002520 return pager_errcode(pPager);
drhed7c8552001-04-11 14:29:21 +00002521 }
drha6abd042004-06-09 17:37:22 +00002522 if( pPager->state==PAGER_RESERVED ){
2523 int rc2;
2524 rc = pager_reload_cache(pPager);
2525 rc2 = pager_unwritelock(pPager);
2526 if( rc==SQLITE_OK ){
2527 rc = rc2;
2528 }
2529 }else{
2530 rc = pager_playback(pPager, 1);
drhd9b02572001-04-15 00:37:09 +00002531 }
drhd9b02572001-04-15 00:37:09 +00002532 if( rc!=SQLITE_OK ){
2533 rc = SQLITE_CORRUPT;
2534 pPager->errMask |= PAGER_ERR_CORRUPT;
2535 }
2536 pPager->dbSize = -1;
drhed7c8552001-04-11 14:29:21 +00002537 return rc;
drh98808ba2001-10-18 12:34:46 +00002538}
drhd9b02572001-04-15 00:37:09 +00002539
2540/*
drh5e00f6c2001-09-13 13:46:56 +00002541** Return TRUE if the database file is opened read-only. Return FALSE
2542** if the database is (in theory) writable.
2543*/
drh3aac2dd2004-04-26 14:10:20 +00002544int sqlite3pager_isreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00002545 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00002546}
2547
2548/*
drhd9b02572001-04-15 00:37:09 +00002549** This routine is used for testing and analysis only.
2550*/
drh3aac2dd2004-04-26 14:10:20 +00002551int *sqlite3pager_stats(Pager *pPager){
drhd9b02572001-04-15 00:37:09 +00002552 static int a[9];
2553 a[0] = pPager->nRef;
2554 a[1] = pPager->nPage;
2555 a[2] = pPager->mxPage;
2556 a[3] = pPager->dbSize;
2557 a[4] = pPager->state;
2558 a[5] = pPager->errMask;
2559 a[6] = pPager->nHit;
2560 a[7] = pPager->nMiss;
2561 a[8] = pPager->nOvfl;
2562 return a;
2563}
drhdd793422001-06-28 01:54:48 +00002564
drhfa86c412002-02-02 15:01:15 +00002565/*
drhac69b052004-05-12 13:30:07 +00002566** Set the statement rollback point.
drhfa86c412002-02-02 15:01:15 +00002567**
2568** This routine should be called with the transaction journal already
drhac69b052004-05-12 13:30:07 +00002569** open. A new statement journal is created that can be used to rollback
drhaaab5722002-02-19 13:39:21 +00002570** changes of a single SQL command within a larger transaction.
drhfa86c412002-02-02 15:01:15 +00002571*/
drh3aac2dd2004-04-26 14:10:20 +00002572int sqlite3pager_stmt_begin(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00002573 int rc;
2574 char zTemp[SQLITE_TEMPNAME_SIZE];
drhac69b052004-05-12 13:30:07 +00002575 assert( !pPager->stmtInUse );
drha6abd042004-06-09 17:37:22 +00002576 TRACE2("STMT-BEGIN %d\n", pPager->fd.h);
drhac69b052004-05-12 13:30:07 +00002577 if( pPager->memDb ){
2578 pPager->stmtInUse = 1;
2579 pPager->stmtSize = pPager->dbSize;
2580 return SQLITE_OK;
2581 }
drhda47d772002-12-02 04:25:19 +00002582 if( !pPager->journalOpen ){
drhac69b052004-05-12 13:30:07 +00002583 pPager->stmtAutoopen = 1;
drhda47d772002-12-02 04:25:19 +00002584 return SQLITE_OK;
2585 }
drhfa86c412002-02-02 15:01:15 +00002586 assert( pPager->journalOpen );
drhac69b052004-05-12 13:30:07 +00002587 pPager->aInStmt = sqliteMalloc( pPager->dbSize/8 + 1 );
2588 if( pPager->aInStmt==0 ){
danielk19779eed5052004-06-04 10:38:30 +00002589 sqlite3OsLock(&pPager->fd, SHARED_LOCK);
drhfa86c412002-02-02 15:01:15 +00002590 return SQLITE_NOMEM;
2591 }
drh968af522003-02-11 14:55:40 +00002592#ifndef NDEBUG
drhac69b052004-05-12 13:30:07 +00002593 rc = sqlite3OsFileSize(&pPager->jfd, &pPager->stmtJSize);
2594 if( rc ) goto stmt_begin_failed;
2595 assert( pPager->stmtJSize ==
danielk197713adf8a2004-06-03 16:08:41 +00002596 pPager->nRec*JOURNAL_PG_SZ(journal_format) +
2597 JOURNAL_HDR_SZ(pPager, journal_format) );
drh968af522003-02-11 14:55:40 +00002598#endif
drhac69b052004-05-12 13:30:07 +00002599 pPager->stmtJSize = pPager->nRec*JOURNAL_PG_SZ(journal_format)
danielk197713adf8a2004-06-03 16:08:41 +00002600 + JOURNAL_HDR_SZ(pPager, journal_format);
drhac69b052004-05-12 13:30:07 +00002601 pPager->stmtSize = pPager->dbSize;
2602 if( !pPager->stmtOpen ){
2603 rc = sqlite3pager_opentemp(zTemp, &pPager->stfd);
2604 if( rc ) goto stmt_begin_failed;
2605 pPager->stmtOpen = 1;
2606 pPager->stmtNRec = 0;
drh0f892532002-05-30 12:27:03 +00002607 }
drhac69b052004-05-12 13:30:07 +00002608 pPager->stmtInUse = 1;
drhfa86c412002-02-02 15:01:15 +00002609 return SQLITE_OK;
2610
drhac69b052004-05-12 13:30:07 +00002611stmt_begin_failed:
2612 if( pPager->aInStmt ){
2613 sqliteFree(pPager->aInStmt);
2614 pPager->aInStmt = 0;
drhfa86c412002-02-02 15:01:15 +00002615 }
2616 return rc;
2617}
2618
2619/*
drhac69b052004-05-12 13:30:07 +00002620** Commit a statement.
drhfa86c412002-02-02 15:01:15 +00002621*/
drh3aac2dd2004-04-26 14:10:20 +00002622int sqlite3pager_stmt_commit(Pager *pPager){
drhac69b052004-05-12 13:30:07 +00002623 if( pPager->stmtInUse ){
drh03eb96a2002-11-10 23:32:56 +00002624 PgHdr *pPg, *pNext;
drha6abd042004-06-09 17:37:22 +00002625 TRACE2("STMT-COMMIT %d\n", pPager->fd.h);
drhac69b052004-05-12 13:30:07 +00002626 if( !pPager->memDb ){
2627 sqlite3OsSeek(&pPager->stfd, 0);
2628 /* sqlite3OsTruncate(&pPager->stfd, 0); */
2629 sqliteFree( pPager->aInStmt );
2630 pPager->aInStmt = 0;
drh663fc632002-02-02 18:49:19 +00002631 }
drhac69b052004-05-12 13:30:07 +00002632 for(pPg=pPager->pStmt; pPg; pPg=pNext){
2633 pNext = pPg->pNextStmt;
2634 assert( pPg->inStmt );
2635 pPg->inStmt = 0;
2636 pPg->pPrevStmt = pPg->pNextStmt = 0;
2637 if( pPager->memDb ){
2638 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
2639 sqliteFree(pHist->pStmt);
2640 pHist->pStmt = 0;
2641 }
2642 }
2643 pPager->stmtNRec = 0;
2644 pPager->stmtInUse = 0;
2645 pPager->pStmt = 0;
drh663fc632002-02-02 18:49:19 +00002646 }
drhac69b052004-05-12 13:30:07 +00002647 pPager->stmtAutoopen = 0;
drhfa86c412002-02-02 15:01:15 +00002648 return SQLITE_OK;
2649}
2650
2651/*
drhac69b052004-05-12 13:30:07 +00002652** Rollback a statement.
drhfa86c412002-02-02 15:01:15 +00002653*/
drh3aac2dd2004-04-26 14:10:20 +00002654int sqlite3pager_stmt_rollback(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00002655 int rc;
drhac69b052004-05-12 13:30:07 +00002656 if( pPager->stmtInUse ){
drha6abd042004-06-09 17:37:22 +00002657 TRACE2("STMT-ROLLBACK %d\n", pPager->fd.h);
drhac69b052004-05-12 13:30:07 +00002658 if( pPager->memDb ){
2659 PgHdr *pPg;
2660 for(pPg=pPager->pStmt; pPg; pPg=pPg->pNextStmt){
2661 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
2662 if( pHist->pStmt ){
2663 memcpy(PGHDR_TO_DATA(pPg), pHist->pStmt, pPager->pageSize);
2664 sqliteFree(pHist->pStmt);
2665 pHist->pStmt = 0;
2666 }
2667 }
2668 pPager->dbSize = pPager->stmtSize;
2669 memoryTruncate(pPager);
2670 rc = SQLITE_OK;
2671 }else{
2672 rc = pager_stmt_playback(pPager);
2673 }
drh3aac2dd2004-04-26 14:10:20 +00002674 sqlite3pager_stmt_commit(pPager);
drh663fc632002-02-02 18:49:19 +00002675 }else{
2676 rc = SQLITE_OK;
2677 }
drhac69b052004-05-12 13:30:07 +00002678 pPager->stmtAutoopen = 0;
drhfa86c412002-02-02 15:01:15 +00002679 return rc;
2680}
2681
drh73509ee2003-04-06 20:44:45 +00002682/*
2683** Return the full pathname of the database file.
2684*/
drh3aac2dd2004-04-26 14:10:20 +00002685const char *sqlite3pager_filename(Pager *pPager){
drh73509ee2003-04-06 20:44:45 +00002686 return pPager->zFilename;
2687}
2688
drhb20ea9d2004-02-09 01:20:36 +00002689/*
2690** Set the codec for this pager
2691*/
drh3aac2dd2004-04-26 14:10:20 +00002692void sqlite3pager_set_codec(
drhb20ea9d2004-02-09 01:20:36 +00002693 Pager *pPager,
drh9eb9e262004-02-11 02:18:05 +00002694 void (*xCodec)(void*,void*,Pgno,int),
drhb20ea9d2004-02-09 01:20:36 +00002695 void *pCodecArg
2696){
2697 pPager->xCodec = xCodec;
2698 pPager->pCodecArg = pCodecArg;
2699}
2700
danielk197713adf8a2004-06-03 16:08:41 +00002701/*
2702** Sync the database file for the pager pPager. zMaster points to the name
2703** of a master journal file that should be written into the individual
2704** journal file. zMaster may be NULL, which is interpreted as no master
2705** journal (a single database transaction).
2706**
2707** This routine ensures that the journal is synced, all dirty pages written
2708** to the database file and the database file synced. The only thing that
2709** remains to commit the transaction is to delete the journal file (or
2710** master journal file if specified).
2711**
2712** Note that if zMaster==NULL, this does not overwrite a previous value
2713** passed to an sqlite3pager_sync() call.
2714*/
2715int sqlite3pager_sync(Pager *pPager, const char *zMaster){
2716 int rc = SQLITE_OK;
2717
2718 /* If this is an in-memory db, or no pages have been written to, this
2719 ** function is a no-op.
2720 */
drha6abd042004-06-09 17:37:22 +00002721 if( !pPager->memDb && pPager->dirtyCache ){
danielk197713adf8a2004-06-03 16:08:41 +00002722 PgHdr *pPg;
2723 assert( pPager->journalOpen );
2724
2725 /* Sync the journal file */
2726 rc = syncJournal(pPager, zMaster);
2727 if( rc!=SQLITE_OK ) goto sync_exit;
2728
2729 /* Write all dirty pages to the database file */
2730 pPg = pager_get_all_dirty_pages(pPager);
2731 rc = pager_write_pagelist(pPg);
2732 if( rc!=SQLITE_OK ) goto sync_exit;
2733
2734 /* If any pages were actually written, sync the database file */
2735 if( pPg && !pPager->noSync ){
2736 rc = sqlite3OsSync(&pPager->fd);
2737 }
2738 }
2739
2740sync_exit:
2741 return rc;
2742}
2743
drh89ac8c12004-06-09 14:17:20 +00002744#ifdef SQLITE_DEBUG
2745/*
2746** Return the current state of the file lock for the given pager.
2747** The return value is one of NO_LOCK, SHARED_LOCK, RESERVED_LOCK,
2748** PENDING_LOCK, or EXCLUSIVE_LOCK.
2749*/
2750int sqlite3pager_lockstate(Pager *pPager){
2751 return pPager->fd.locktype;
2752}
2753#endif
2754
drh74587e52002-08-13 00:01:16 +00002755#ifdef SQLITE_TEST
drhdd793422001-06-28 01:54:48 +00002756/*
2757** Print a listing of all referenced pages and their ref count.
2758*/
drh3aac2dd2004-04-26 14:10:20 +00002759void sqlite3pager_refdump(Pager *pPager){
drhdd793422001-06-28 01:54:48 +00002760 PgHdr *pPg;
2761 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2762 if( pPg->nRef<=0 ) continue;
2763 printf("PAGE %3d addr=0x%08x nRef=%d\n",
2764 pPg->pgno, (int)PGHDR_TO_DATA(pPg), pPg->nRef);
2765 }
2766}
2767#endif