blob: 1f95e72e6a9c8466f0b5b995541533a19876bc75 [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**
danielk197724162fe2004-06-04 06:22:00 +000021** @(#) $Id: pager.c,v 1.111 2004/06/04 06:22:01 danielk1977 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**
52** SQLITE_UNLOCK The page cache is not currently reading or
53** writing the database file. There is no
54** data held in memory. This is the initial
55** state.
56**
57** SQLITE_READLOCK The page cache is reading the database.
58** 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**
62** SQLITE_WRITELOCK The page cache is writing the database.
63** Access is exclusive. No other processes or
64** threads can be reading or writing while one
65** process is writing.
66**
drh306dc212001-05-21 13:45:10 +000067** The page cache comes up in SQLITE_UNLOCK. The first time a
68** sqlite_page_get() occurs, the state transitions to SQLITE_READLOCK.
drhed7c8552001-04-11 14:29:21 +000069** After all pages have been released using sqlite_page_unref(),
drh306dc212001-05-21 13:45:10 +000070** the state transitions back to SQLITE_UNLOCK. The first time
drhed7c8552001-04-11 14:29:21 +000071** that sqlite_page_write() is called, the state transitions to
drh306dc212001-05-21 13:45:10 +000072** SQLITE_WRITELOCK. (Note that sqlite_page_write() can only be
73** called on an outstanding page which means that the pager must
74** be in SQLITE_READLOCK before it transitions to SQLITE_WRITELOCK.)
75** The sqlite_page_rollback() and sqlite_page_commit() functions
76** transition the state from SQLITE_WRITELOCK back to SQLITE_READLOCK.
drhed7c8552001-04-11 14:29:21 +000077*/
78#define SQLITE_UNLOCK 0
79#define SQLITE_READLOCK 1
80#define SQLITE_WRITELOCK 2
81
drhd9b02572001-04-15 00:37:09 +000082
drhed7c8552001-04-11 14:29:21 +000083/*
84** Each in-memory image of a page begins with the following header.
drhbd03cae2001-06-02 02:40:57 +000085** This header is only visible to this pager module. The client
86** code that calls pager sees only the data that follows the header.
drhf6038712004-02-08 18:07:34 +000087**
drh3aac2dd2004-04-26 14:10:20 +000088** Client code should call sqlite3pager_write() on a page prior to making
89** any modifications to that page. The first time sqlite3pager_write()
drhf6038712004-02-08 18:07:34 +000090** is called, the original page contents are written into the rollback
91** journal and PgHdr.inJournal and PgHdr.needSync are set. Later, once
92** the journal page has made it onto the disk surface, PgHdr.needSync
93** is cleared. The modified page cannot be written back into the original
94** database file until the journal pages has been synced to disk and the
95** PgHdr.needSync has been cleared.
96**
drh3aac2dd2004-04-26 14:10:20 +000097** The PgHdr.dirty flag is set when sqlite3pager_write() is called and
drhf6038712004-02-08 18:07:34 +000098** is cleared again when the page content is written back to the original
99** database file.
drhed7c8552001-04-11 14:29:21 +0000100*/
drhd9b02572001-04-15 00:37:09 +0000101typedef struct PgHdr PgHdr;
drhed7c8552001-04-11 14:29:21 +0000102struct PgHdr {
103 Pager *pPager; /* The pager to which this page belongs */
104 Pgno pgno; /* The page number for this page */
drh69688d52001-04-14 16:38:23 +0000105 PgHdr *pNextHash, *pPrevHash; /* Hash collision chain for PgHdr.pgno */
drhd9b02572001-04-15 00:37:09 +0000106 PgHdr *pNextFree, *pPrevFree; /* Freelist of pages where nRef==0 */
drhac69b052004-05-12 13:30:07 +0000107 PgHdr *pNextAll; /* A list of all pages */
108 PgHdr *pNextStmt, *pPrevStmt; /* List of pages in the statement journal */
drh193a6b42002-07-07 16:52:46 +0000109 u8 inJournal; /* TRUE if has been written to journal */
drhac69b052004-05-12 13:30:07 +0000110 u8 inStmt; /* TRUE if in the statement subjournal */
drh193a6b42002-07-07 16:52:46 +0000111 u8 dirty; /* TRUE if we need to write back changes */
drhdb48ee02003-01-16 13:42:43 +0000112 u8 needSync; /* Sync journal before writing this page */
drh193a6b42002-07-07 16:52:46 +0000113 u8 alwaysRollback; /* Disable dont_rollback() for this page */
drhac69b052004-05-12 13:30:07 +0000114 short int nRef; /* Number of users of this page */
drh2554f8b2003-01-22 01:26:44 +0000115 PgHdr *pDirty; /* Dirty pages sorted by PgHdr.pgno */
drhd0ba1932004-02-10 01:54:28 +0000116 /* SQLITE_PAGE_SIZE bytes of page data follow this header */
drh973b6e32003-02-12 14:09:42 +0000117 /* Pager.nExtra bytes of local data follow the page data */
drhed7c8552001-04-11 14:29:21 +0000118};
119
drhac69b052004-05-12 13:30:07 +0000120/*
121** For an in-memory only database, some extra information is recorded about
122** each page so that changes can be rolled back. (Journal files are not
123** used for in-memory databases.) The following information is added to
124** the end of every EXTRA block for in-memory databases.
125**
126** This information could have been added directly to the PgHdr structure.
127** But then it would take up an extra 8 bytes of storage on every PgHdr
128** even for disk-based databases. Splitting it out saves 8 bytes. This
129** is only a savings of 0.8% but those percentages add up.
130*/
131typedef struct PgHistory PgHistory;
132struct PgHistory {
133 u8 *pOrig; /* Original page text. Restore to this on a full rollback */
134 u8 *pStmt; /* Text as it was at the beginning of the current statement */
135};
drh9eb9e262004-02-11 02:18:05 +0000136
137/*
138** A macro used for invoking the codec if there is one
139*/
140#ifdef SQLITE_HAS_CODEC
141# define CODEC(P,D,N,X) if( P->xCodec ){ P->xCodec(P->pCodecArg,D,N,X); }
142#else
143# define CODEC(P,D,N,X)
144#endif
145
drhed7c8552001-04-11 14:29:21 +0000146/*
drh69688d52001-04-14 16:38:23 +0000147** Convert a pointer to a PgHdr into a pointer to its data
148** and back again.
drhed7c8552001-04-11 14:29:21 +0000149*/
150#define PGHDR_TO_DATA(P) ((void*)(&(P)[1]))
151#define DATA_TO_PGHDR(D) (&((PgHdr*)(D))[-1])
drhd0ba1932004-02-10 01:54:28 +0000152#define PGHDR_TO_EXTRA(P) ((void*)&((char*)(&(P)[1]))[SQLITE_PAGE_SIZE])
drhac69b052004-05-12 13:30:07 +0000153#define PGHDR_TO_HIST(P,PGR) \
154 ((PgHistory*)&((char*)(&(P)[1]))[(PGR)->pageSize+(PGR)->nExtra])
drhed7c8552001-04-11 14:29:21 +0000155
156/*
drhed7c8552001-04-11 14:29:21 +0000157** How big to make the hash table used for locating in-memory pages
drh836faa42003-01-11 13:30:57 +0000158** by page number.
drhed7c8552001-04-11 14:29:21 +0000159*/
drh836faa42003-01-11 13:30:57 +0000160#define N_PG_HASH 2048
161
162/*
163** Hash a page number
164*/
165#define pager_hash(PN) ((PN)&(N_PG_HASH-1))
drhed7c8552001-04-11 14:29:21 +0000166
167/*
168** A open page cache is an instance of the following structure.
169*/
170struct Pager {
171 char *zFilename; /* Name of the database file */
172 char *zJournal; /* Name of the journal file */
drha76c82e2003-07-27 18:59:42 +0000173 char *zDirectory; /* Directory hold database and journal files */
drh8cfbf082001-09-19 13:22:39 +0000174 OsFile fd, jfd; /* File descriptors for database and journal */
drhac69b052004-05-12 13:30:07 +0000175 OsFile stfd; /* File descriptor for the statement subjournal*/
drhed7c8552001-04-11 14:29:21 +0000176 int dbSize; /* Number of pages in the file */
drh69688d52001-04-14 16:38:23 +0000177 int origDbSize; /* dbSize before the current change */
drhac69b052004-05-12 13:30:07 +0000178 int stmtSize; /* Size of database (in pages) at stmt_begin() */
179 off_t stmtJSize; /* Size of journal at stmt_begin() */
drh968af522003-02-11 14:55:40 +0000180 int nRec; /* Number of pages written to the journal */
181 u32 cksumInit; /* Quasi-random value added to every checksum */
drhac69b052004-05-12 13:30:07 +0000182 int stmtNRec; /* Number of records in stmt subjournal */
drh7e3b0a02001-04-28 16:52:40 +0000183 int nExtra; /* Add this many bytes to each in-memory page */
drhb6f41482004-05-14 01:58:11 +0000184 void (*xDestructor)(void*,int); /* Call this routine when freeing pages */
185 int pageSize; /* Number of bytes in a page */
drhed7c8552001-04-11 14:29:21 +0000186 int nPage; /* Total number of in-memory pages */
drhd9b02572001-04-15 00:37:09 +0000187 int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */
drhed7c8552001-04-11 14:29:21 +0000188 int mxPage; /* Maximum number of pages to hold in cache */
drhd9b02572001-04-15 00:37:09 +0000189 int nHit, nMiss, nOvfl; /* Cache hits, missing, and LRU overflows */
drh9eb9e262004-02-11 02:18:05 +0000190 void (*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
drhb20ea9d2004-02-09 01:20:36 +0000191 void *pCodecArg; /* First argument to xCodec() */
drh603240c2002-03-05 01:11:12 +0000192 u8 journalOpen; /* True if journal file descriptors is valid */
drh34e79ce2004-02-08 06:05:46 +0000193 u8 journalStarted; /* True if header of journal is synced */
194 u8 useJournal; /* Use a rollback journal on this file */
drhac69b052004-05-12 13:30:07 +0000195 u8 stmtOpen; /* True if the statement subjournal is open */
196 u8 stmtInUse; /* True we are in a statement subtransaction */
197 u8 stmtAutoopen; /* Open stmt journal when main journal is opened*/
drh603240c2002-03-05 01:11:12 +0000198 u8 noSync; /* Do not sync the journal if true */
drh968af522003-02-11 14:55:40 +0000199 u8 fullSync; /* Do extra syncs of the journal for robustness */
drh603240c2002-03-05 01:11:12 +0000200 u8 state; /* SQLITE_UNLOCK, _READLOCK or _WRITELOCK */
201 u8 errMask; /* One of several kinds of errors */
202 u8 tempFile; /* zFilename is a temporary file */
203 u8 readOnly; /* True for a read-only database */
204 u8 needSync; /* True if an fsync() is needed on the journal */
drha1680452002-04-18 01:56:57 +0000205 u8 dirtyFile; /* True if database file has changed in any way */
drh193a6b42002-07-07 16:52:46 +0000206 u8 alwaysRollback; /* Disable dont_rollback() for all pages */
drhac69b052004-05-12 13:30:07 +0000207 u8 memDb; /* True to inhibit all file I/O */
drh603240c2002-03-05 01:11:12 +0000208 u8 *aInJournal; /* One bit for each page in the database file */
drhac69b052004-05-12 13:30:07 +0000209 u8 *aInStmt; /* One bit for each page in the database */
drhed7c8552001-04-11 14:29:21 +0000210 PgHdr *pFirst, *pLast; /* List of free pages */
drh341eae82003-01-21 02:39:36 +0000211 PgHdr *pFirstSynced; /* First free page with PgHdr.needSync==0 */
drhd9b02572001-04-15 00:37:09 +0000212 PgHdr *pAll; /* List of all pages */
drhac69b052004-05-12 13:30:07 +0000213 PgHdr *pStmt; /* List of pages in the statement subjournal */
drhed7c8552001-04-11 14:29:21 +0000214 PgHdr *aHash[N_PG_HASH]; /* Hash table to map page number of PgHdr */
danielk197713adf8a2004-06-03 16:08:41 +0000215 int nMaster; /* Number of bytes to reserve for master j.p */
danielk197724162fe2004-06-04 06:22:00 +0000216 BusyHandler *pBusyHandler; /* Pointer to sqlite.busyHandler */
drhd9b02572001-04-15 00:37:09 +0000217};
218
219/*
220** These are bits that can be set in Pager.errMask.
221*/
222#define PAGER_ERR_FULL 0x01 /* a write() failed */
223#define PAGER_ERR_MEM 0x02 /* malloc() failed */
224#define PAGER_ERR_LOCK 0x04 /* error in the locking protocol */
225#define PAGER_ERR_CORRUPT 0x08 /* database or journal corruption */
drh81a20f22001-10-12 17:30:04 +0000226#define PAGER_ERR_DISK 0x10 /* general disk I/O error - bad hard drive? */
drhd9b02572001-04-15 00:37:09 +0000227
228/*
229** The journal file contains page records in the following
230** format.
drh968af522003-02-11 14:55:40 +0000231**
232** Actually, this structure is the complete page record for pager
233** formats less than 3. Beginning with format 3, this record is surrounded
234** by two checksums.
drhd9b02572001-04-15 00:37:09 +0000235*/
236typedef struct PageRecord PageRecord;
237struct PageRecord {
drhb20ea9d2004-02-09 01:20:36 +0000238 Pgno pgno; /* The page number */
drhd0ba1932004-02-10 01:54:28 +0000239 char aData[SQLITE_PAGE_SIZE]; /* Original data for page pgno */
drhd9b02572001-04-15 00:37:09 +0000240};
241
242/*
drh5e00f6c2001-09-13 13:46:56 +0000243** Journal files begin with the following magic string. The data
244** was obtained from /dev/random. It is used only as a sanity check.
drh94f33312002-08-12 12:29:56 +0000245**
drh968af522003-02-11 14:55:40 +0000246** There are three journal formats (so far). The 1st journal format writes
247** 32-bit integers in the byte-order of the host machine. New
248** formats writes integers as big-endian. All new journals use the
drh94f33312002-08-12 12:29:56 +0000249** new format, but we have to be able to read an older journal in order
drh968af522003-02-11 14:55:40 +0000250** to rollback journals created by older versions of the library.
251**
252** The 3rd journal format (added for 2.8.0) adds additional sanity
253** checking information to the journal. If the power fails while the
254** journal is being written, semi-random garbage data might appear in
255** the journal file after power is restored. If an attempt is then made
256** to roll the journal back, the database could be corrupted. The additional
257** sanity checking data is an attempt to discover the garbage in the
258** journal and ignore it.
259**
260** The sanity checking information for the 3rd journal format consists
261** of a 32-bit checksum on each page of data. The checksum covers both
drhd0ba1932004-02-10 01:54:28 +0000262** the page number and the SQLITE_PAGE_SIZE bytes of data for the page.
drh968af522003-02-11 14:55:40 +0000263** This cksum is initialized to a 32-bit random value that appears in the
264** journal file right after the header. The random initializer is important,
265** because garbage data that appears at the end of a journal is likely
266** data that was once in other files that have now been deleted. If the
267** garbage data came from an obsolete journal file, the checksums might
268** be correct. But by initializing the checksum to random value which
269** is different for every journal, we minimize that risk.
drhd9b02572001-04-15 00:37:09 +0000270*/
drh968af522003-02-11 14:55:40 +0000271static const unsigned char aJournalMagic1[] = {
drhd9b02572001-04-15 00:37:09 +0000272 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd4,
drhed7c8552001-04-11 14:29:21 +0000273};
drh968af522003-02-11 14:55:40 +0000274static const unsigned char aJournalMagic2[] = {
drh94f33312002-08-12 12:29:56 +0000275 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd5,
276};
drh968af522003-02-11 14:55:40 +0000277static const unsigned char aJournalMagic3[] = {
278 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd6,
279};
280#define JOURNAL_FORMAT_1 1
281#define JOURNAL_FORMAT_2 2
282#define JOURNAL_FORMAT_3 3
drh94f33312002-08-12 12:29:56 +0000283
284/*
drh968af522003-02-11 14:55:40 +0000285** The following integer determines what format to use when creating
286** new primary journal files. By default we always use format 3.
287** When testing, we can set this value to older journal formats in order to
288** make sure that newer versions of the library are able to rollback older
289** journal files.
290**
drhac69b052004-05-12 13:30:07 +0000291** Note that statement journals always use format 2 and omit the header.
drh94f33312002-08-12 12:29:56 +0000292*/
293#ifdef SQLITE_TEST
drh968af522003-02-11 14:55:40 +0000294int journal_format = 3;
drh74587e52002-08-13 00:01:16 +0000295#else
drh968af522003-02-11 14:55:40 +0000296# define journal_format 3
drh94f33312002-08-12 12:29:56 +0000297#endif
drhed7c8552001-04-11 14:29:21 +0000298
299/*
drh968af522003-02-11 14:55:40 +0000300** The size of the header and of each page in the journal varies according
301** to which journal format is being used. The following macros figure out
302** the sizes based on format numbers.
303*/
danielk197713adf8a2004-06-03 16:08:41 +0000304/*
drh968af522003-02-11 14:55:40 +0000305#define JOURNAL_HDR_SZ(X) \
306 (sizeof(aJournalMagic1) + sizeof(Pgno) + ((X)>=3)*2*sizeof(u32))
danielk197713adf8a2004-06-03 16:08:41 +0000307*/
308#define JOURNAL_HDR_SZ(pPager, X) (\
309 sizeof(aJournalMagic1) + \
310 sizeof(Pgno) + \
311 ((X)>=3?3*sizeof(u32)+(pPager)->nMaster:0) )
drh968af522003-02-11 14:55:40 +0000312#define JOURNAL_PG_SZ(X) \
drhd0ba1932004-02-10 01:54:28 +0000313 (SQLITE_PAGE_SIZE + sizeof(Pgno) + ((X)>=3)*sizeof(u32))
drh968af522003-02-11 14:55:40 +0000314
danielk197713adf8a2004-06-03 16:08:41 +0000315
drh968af522003-02-11 14:55:40 +0000316/*
drhdd793422001-06-28 01:54:48 +0000317** Enable reference count tracking here:
318*/
drh74587e52002-08-13 00:01:16 +0000319#ifdef SQLITE_TEST
drh3aac2dd2004-04-26 14:10:20 +0000320 int pager3_refinfo_enable = 0;
drhdd793422001-06-28 01:54:48 +0000321 static void pager_refinfo(PgHdr *p){
322 static int cnt = 0;
drh3aac2dd2004-04-26 14:10:20 +0000323 if( !pager3_refinfo_enable ) return;
drhdd793422001-06-28 01:54:48 +0000324 printf(
325 "REFCNT: %4d addr=0x%08x nRef=%d\n",
326 p->pgno, (int)PGHDR_TO_DATA(p), p->nRef
327 );
328 cnt++; /* Something to set a breakpoint on */
329 }
330# define REFINFO(X) pager_refinfo(X)
331#else
332# define REFINFO(X)
333#endif
334
335/*
drh34e79ce2004-02-08 06:05:46 +0000336** Read a 32-bit integer from the given file descriptor. Store the integer
337** that is read in *pRes. Return SQLITE_OK if everything worked, or an
338** error code is something goes wrong.
339**
340** If the journal format is 2 or 3, read a big-endian integer. If the
341** journal format is 1, read an integer in the native byte-order of the
342** host machine.
drh94f33312002-08-12 12:29:56 +0000343*/
drh968af522003-02-11 14:55:40 +0000344static int read32bits(int format, OsFile *fd, u32 *pRes){
drh94f33312002-08-12 12:29:56 +0000345 u32 res;
346 int rc;
danielk19774adee202004-05-08 08:23:19 +0000347 rc = sqlite3OsRead(fd, &res, sizeof(res));
drh968af522003-02-11 14:55:40 +0000348 if( rc==SQLITE_OK && format>JOURNAL_FORMAT_1 ){
drh94f33312002-08-12 12:29:56 +0000349 unsigned char ac[4];
350 memcpy(ac, &res, 4);
351 res = (ac[0]<<24) | (ac[1]<<16) | (ac[2]<<8) | ac[3];
352 }
353 *pRes = res;
354 return rc;
355}
356
357/*
drh34e79ce2004-02-08 06:05:46 +0000358** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
359** on success or an error code is something goes wrong.
360**
361** If the journal format is 2 or 3, write the integer as 4 big-endian
362** bytes. If the journal format is 1, write the integer in the native
363** byte order. In normal operation, only formats 2 and 3 are used.
364** Journal format 1 is only used for testing.
drh94f33312002-08-12 12:29:56 +0000365*/
366static int write32bits(OsFile *fd, u32 val){
367 unsigned char ac[4];
drh968af522003-02-11 14:55:40 +0000368 if( journal_format<=1 ){
danielk19774adee202004-05-08 08:23:19 +0000369 return sqlite3OsWrite(fd, &val, 4);
drh94f33312002-08-12 12:29:56 +0000370 }
drh94f33312002-08-12 12:29:56 +0000371 ac[0] = (val>>24) & 0xff;
372 ac[1] = (val>>16) & 0xff;
373 ac[2] = (val>>8) & 0xff;
374 ac[3] = val & 0xff;
danielk19774adee202004-05-08 08:23:19 +0000375 return sqlite3OsWrite(fd, ac, 4);
drh94f33312002-08-12 12:29:56 +0000376}
377
drh2554f8b2003-01-22 01:26:44 +0000378/*
379** Write a 32-bit integer into a page header right before the
380** page data. This will overwrite the PgHdr.pDirty pointer.
drh34e79ce2004-02-08 06:05:46 +0000381**
382** The integer is big-endian for formats 2 and 3 and native byte order
383** for journal format 1.
drh2554f8b2003-01-22 01:26:44 +0000384*/
drh968af522003-02-11 14:55:40 +0000385static void store32bits(u32 val, PgHdr *p, int offset){
drh2554f8b2003-01-22 01:26:44 +0000386 unsigned char *ac;
drhec1bd0b2003-08-26 11:41:27 +0000387 ac = &((unsigned char*)PGHDR_TO_DATA(p))[offset];
drh968af522003-02-11 14:55:40 +0000388 if( journal_format<=1 ){
drh2554f8b2003-01-22 01:26:44 +0000389 memcpy(ac, &val, 4);
390 }else{
391 ac[0] = (val>>24) & 0xff;
392 ac[1] = (val>>16) & 0xff;
393 ac[2] = (val>>8) & 0xff;
394 ac[3] = val & 0xff;
395 }
396}
397
drh94f33312002-08-12 12:29:56 +0000398
399/*
drhd9b02572001-04-15 00:37:09 +0000400** Convert the bits in the pPager->errMask into an approprate
401** return code.
402*/
403static int pager_errcode(Pager *pPager){
404 int rc = SQLITE_OK;
405 if( pPager->errMask & PAGER_ERR_LOCK ) rc = SQLITE_PROTOCOL;
drh81a20f22001-10-12 17:30:04 +0000406 if( pPager->errMask & PAGER_ERR_DISK ) rc = SQLITE_IOERR;
drhd9b02572001-04-15 00:37:09 +0000407 if( pPager->errMask & PAGER_ERR_FULL ) rc = SQLITE_FULL;
408 if( pPager->errMask & PAGER_ERR_MEM ) rc = SQLITE_NOMEM;
409 if( pPager->errMask & PAGER_ERR_CORRUPT ) rc = SQLITE_CORRUPT;
410 return rc;
drhed7c8552001-04-11 14:29:21 +0000411}
412
413/*
drh03eb96a2002-11-10 23:32:56 +0000414** Add or remove a page from the list of all pages that are in the
drhac69b052004-05-12 13:30:07 +0000415** statement journal.
drh03eb96a2002-11-10 23:32:56 +0000416**
417** The Pager keeps a separate list of pages that are currently in
drhac69b052004-05-12 13:30:07 +0000418** the statement journal. This helps the sqlite3pager_stmt_commit()
drh03eb96a2002-11-10 23:32:56 +0000419** routine run MUCH faster for the common case where there are many
drhac69b052004-05-12 13:30:07 +0000420** pages in memory but only a few are in the statement journal.
drh03eb96a2002-11-10 23:32:56 +0000421*/
drh3aac2dd2004-04-26 14:10:20 +0000422static void page_add_to_stmt_list(PgHdr *pPg){
drh03eb96a2002-11-10 23:32:56 +0000423 Pager *pPager = pPg->pPager;
drhac69b052004-05-12 13:30:07 +0000424 if( pPg->inStmt ) return;
425 assert( pPg->pPrevStmt==0 && pPg->pNextStmt==0 );
426 pPg->pPrevStmt = 0;
427 if( pPager->pStmt ){
428 pPager->pStmt->pPrevStmt = pPg;
drh03eb96a2002-11-10 23:32:56 +0000429 }
drhac69b052004-05-12 13:30:07 +0000430 pPg->pNextStmt = pPager->pStmt;
431 pPager->pStmt = pPg;
432 pPg->inStmt = 1;
drh03eb96a2002-11-10 23:32:56 +0000433}
drh3aac2dd2004-04-26 14:10:20 +0000434static void page_remove_from_stmt_list(PgHdr *pPg){
drhac69b052004-05-12 13:30:07 +0000435 if( !pPg->inStmt ) return;
436 if( pPg->pPrevStmt ){
437 assert( pPg->pPrevStmt->pNextStmt==pPg );
438 pPg->pPrevStmt->pNextStmt = pPg->pNextStmt;
drh03eb96a2002-11-10 23:32:56 +0000439 }else{
drhac69b052004-05-12 13:30:07 +0000440 assert( pPg->pPager->pStmt==pPg );
441 pPg->pPager->pStmt = pPg->pNextStmt;
drh03eb96a2002-11-10 23:32:56 +0000442 }
drhac69b052004-05-12 13:30:07 +0000443 if( pPg->pNextStmt ){
444 assert( pPg->pNextStmt->pPrevStmt==pPg );
445 pPg->pNextStmt->pPrevStmt = pPg->pPrevStmt;
drh03eb96a2002-11-10 23:32:56 +0000446 }
drhac69b052004-05-12 13:30:07 +0000447 pPg->pNextStmt = 0;
448 pPg->pPrevStmt = 0;
449 pPg->inStmt = 0;
drh03eb96a2002-11-10 23:32:56 +0000450}
451
452/*
drhed7c8552001-04-11 14:29:21 +0000453** Find a page in the hash table given its page number. Return
454** a pointer to the page or NULL if not found.
455*/
drhd9b02572001-04-15 00:37:09 +0000456static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drh836faa42003-01-11 13:30:57 +0000457 PgHdr *p = pPager->aHash[pager_hash(pgno)];
drhed7c8552001-04-11 14:29:21 +0000458 while( p && p->pgno!=pgno ){
459 p = p->pNextHash;
460 }
461 return p;
462}
463
464/*
465** Unlock the database and clear the in-memory cache. This routine
466** sets the state of the pager back to what it was when it was first
467** opened. Any outstanding pages are invalidated and subsequent attempts
468** to access those pages will likely result in a coredump.
469*/
drhd9b02572001-04-15 00:37:09 +0000470static void pager_reset(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +0000471 PgHdr *pPg, *pNext;
drhd9b02572001-04-15 00:37:09 +0000472 for(pPg=pPager->pAll; pPg; pPg=pNext){
473 pNext = pPg->pNextAll;
474 sqliteFree(pPg);
drhed7c8552001-04-11 14:29:21 +0000475 }
476 pPager->pFirst = 0;
drh341eae82003-01-21 02:39:36 +0000477 pPager->pFirstSynced = 0;
drhd9b02572001-04-15 00:37:09 +0000478 pPager->pLast = 0;
479 pPager->pAll = 0;
drhed7c8552001-04-11 14:29:21 +0000480 memset(pPager->aHash, 0, sizeof(pPager->aHash));
481 pPager->nPage = 0;
drhfa86c412002-02-02 15:01:15 +0000482 if( pPager->state>=SQLITE_WRITELOCK ){
drh3aac2dd2004-04-26 14:10:20 +0000483 sqlite3pager_rollback(pPager);
drhed7c8552001-04-11 14:29:21 +0000484 }
danielk19774adee202004-05-08 08:23:19 +0000485 sqlite3OsUnlock(&pPager->fd);
drhed7c8552001-04-11 14:29:21 +0000486 pPager->state = SQLITE_UNLOCK;
drhd9b02572001-04-15 00:37:09 +0000487 pPager->dbSize = -1;
drhed7c8552001-04-11 14:29:21 +0000488 pPager->nRef = 0;
drh8cfbf082001-09-19 13:22:39 +0000489 assert( pPager->journalOpen==0 );
drhed7c8552001-04-11 14:29:21 +0000490}
491
492/*
493** When this routine is called, the pager has the journal file open and
494** a write lock on the database. This routine releases the database
495** write lock and acquires a read lock in its place. The journal file
496** is deleted and closed.
drh50457892003-09-06 01:10:47 +0000497**
498** TODO: Consider keeping the journal file open for temporary databases.
499** This might give a performance improvement on windows where opening
500** a file is an expensive operation.
drhed7c8552001-04-11 14:29:21 +0000501*/
drhd9b02572001-04-15 00:37:09 +0000502static int pager_unwritelock(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +0000503 int rc;
drhd9b02572001-04-15 00:37:09 +0000504 PgHdr *pPg;
drhfa86c412002-02-02 15:01:15 +0000505 if( pPager->state<SQLITE_WRITELOCK ) return SQLITE_OK;
drh3aac2dd2004-04-26 14:10:20 +0000506 sqlite3pager_stmt_commit(pPager);
drhac69b052004-05-12 13:30:07 +0000507 if( pPager->stmtOpen ){
508 sqlite3OsClose(&pPager->stfd);
509 pPager->stmtOpen = 0;
drh0f892532002-05-30 12:27:03 +0000510 }
drhda47d772002-12-02 04:25:19 +0000511 if( pPager->journalOpen ){
danielk19774adee202004-05-08 08:23:19 +0000512 sqlite3OsClose(&pPager->jfd);
drhda47d772002-12-02 04:25:19 +0000513 pPager->journalOpen = 0;
danielk19774adee202004-05-08 08:23:19 +0000514 sqlite3OsDelete(pPager->zJournal);
drhda47d772002-12-02 04:25:19 +0000515 sqliteFree( pPager->aInJournal );
516 pPager->aInJournal = 0;
517 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
518 pPg->inJournal = 0;
519 pPg->dirty = 0;
drhdb48ee02003-01-16 13:42:43 +0000520 pPg->needSync = 0;
drhda47d772002-12-02 04:25:19 +0000521 }
522 }else{
523 assert( pPager->dirtyFile==0 || pPager->useJournal==0 );
drhd9b02572001-04-15 00:37:09 +0000524 }
danielk19774adee202004-05-08 08:23:19 +0000525 rc = sqlite3OsReadLock(&pPager->fd);
drh8e298f92002-07-06 16:28:47 +0000526 if( rc==SQLITE_OK ){
527 pPager->state = SQLITE_READLOCK;
528 }else{
529 /* This can only happen if a process does a BEGIN, then forks and the
530 ** child process does the COMMIT. Because of the semantics of unix
531 ** file locking, the unlock will fail.
532 */
533 pPager->state = SQLITE_UNLOCK;
534 }
drhed7c8552001-04-11 14:29:21 +0000535 return rc;
536}
537
drhed7c8552001-04-11 14:29:21 +0000538/*
drh968af522003-02-11 14:55:40 +0000539** Compute and return a checksum for the page of data.
drh34e79ce2004-02-08 06:05:46 +0000540**
541** This is not a real checksum. It is really just the sum of the
542** random initial value and the page number. We considered do a checksum
543** of the database, but that was found to be too slow.
drh968af522003-02-11 14:55:40 +0000544*/
545static u32 pager_cksum(Pager *pPager, Pgno pgno, const char *aData){
546 u32 cksum = pPager->cksumInit + pgno;
drh968af522003-02-11 14:55:40 +0000547 return cksum;
548}
549
550/*
drhfa86c412002-02-02 15:01:15 +0000551** Read a single page from the journal file opened on file descriptor
552** jfd. Playback this one page.
drh968af522003-02-11 14:55:40 +0000553**
554** There are three different journal formats. The format parameter determines
555** which format is used by the journal that is played back.
drhfa86c412002-02-02 15:01:15 +0000556*/
drh968af522003-02-11 14:55:40 +0000557static int pager_playback_one_page(Pager *pPager, OsFile *jfd, int format){
drhfa86c412002-02-02 15:01:15 +0000558 int rc;
559 PgHdr *pPg; /* An existing page in the cache */
560 PageRecord pgRec;
drh968af522003-02-11 14:55:40 +0000561 u32 cksum;
drhfa86c412002-02-02 15:01:15 +0000562
drh968af522003-02-11 14:55:40 +0000563 rc = read32bits(format, jfd, &pgRec.pgno);
drh99ee3602003-02-16 19:13:36 +0000564 if( rc!=SQLITE_OK ) return rc;
danielk19774adee202004-05-08 08:23:19 +0000565 rc = sqlite3OsRead(jfd, &pgRec.aData, sizeof(pgRec.aData));
drh99ee3602003-02-16 19:13:36 +0000566 if( rc!=SQLITE_OK ) return rc;
drhfa86c412002-02-02 15:01:15 +0000567
drh968af522003-02-11 14:55:40 +0000568 /* Sanity checking on the page. This is more important that I originally
569 ** thought. If a power failure occurs while the journal is being written,
570 ** it could cause invalid data to be written into the journal. We need to
571 ** detect this invalid data (with high probability) and ignore it.
572 */
573 if( pgRec.pgno==0 ){
574 return SQLITE_DONE;
575 }
drh7d02cb72003-06-04 16:24:39 +0000576 if( pgRec.pgno>(unsigned)pPager->dbSize ){
drh968af522003-02-11 14:55:40 +0000577 return SQLITE_OK;
578 }
579 if( format>=JOURNAL_FORMAT_3 ){
580 rc = read32bits(format, jfd, &cksum);
drh99ee3602003-02-16 19:13:36 +0000581 if( rc ) return rc;
drh968af522003-02-11 14:55:40 +0000582 if( pager_cksum(pPager, pgRec.pgno, pgRec.aData)!=cksum ){
583 return SQLITE_DONE;
584 }
585 }
drhfa86c412002-02-02 15:01:15 +0000586
587 /* Playback the page. Update the in-memory copy of the page
588 ** at the same time, if there is one.
589 */
590 pPg = pager_lookup(pPager, pgRec.pgno);
drh99ee3602003-02-16 19:13:36 +0000591 TRACE2("PLAYBACK %d\n", pgRec.pgno);
danielk19774adee202004-05-08 08:23:19 +0000592 sqlite3OsSeek(&pPager->fd, (pgRec.pgno-1)*(off_t)SQLITE_PAGE_SIZE);
593 rc = sqlite3OsWrite(&pPager->fd, pgRec.aData, SQLITE_PAGE_SIZE);
drhfa86c412002-02-02 15:01:15 +0000594 if( pPg ){
drhacf4ac92003-12-17 23:57:34 +0000595 /* No page should ever be rolled back that is in use, except for page
596 ** 1 which is held in use in order to keep the lock on the database
597 ** active.
598 */
drhb6f41482004-05-14 01:58:11 +0000599 void *pData;
drhacf4ac92003-12-17 23:57:34 +0000600 assert( pPg->nRef==0 || pPg->pgno==1 );
drhb6f41482004-05-14 01:58:11 +0000601 pData = PGHDR_TO_DATA(pPg);
602 memcpy(pData, pgRec.aData, pPager->pageSize);
drhde647132004-05-07 17:57:49 +0000603 if( pPager->xDestructor ){
drhb6f41482004-05-14 01:58:11 +0000604 pPager->xDestructor(pData, pPager->pageSize);
drhde647132004-05-07 17:57:49 +0000605 }
drhdb48ee02003-01-16 13:42:43 +0000606 pPg->dirty = 0;
607 pPg->needSync = 0;
drhb6f41482004-05-14 01:58:11 +0000608 CODEC(pPager, pData, pPg->pgno, 3);
drhfa86c412002-02-02 15:01:15 +0000609 }
610 return rc;
611}
612
613/*
danielk197713adf8a2004-06-03 16:08:41 +0000614** Parameter zMaster is the name of a master journal file. A single journal
615** file that referred to the master journal file has just been rolled back.
616** This routine checks if it is possible to delete the master journal file,
617** and does so if it is.
618*/
619static int pager_delmaster(const char *zMaster){
620 int rc;
621 int master_open = 0;
622 OsFile master;
623 char *zMasterJournal = 0; /* Contents of master journal file */
624 off_t nMasterJournal; /* Size of master journal file */
625
626 /* Open the master journal file exclusively in case some other process
627 ** is running this routine also. Not that it makes too much difference.
628 */
629 rc = sqlite3OsOpenExclusive(zMaster, &master, 0);
630 if( rc!=SQLITE_OK ) goto delmaster_out;
631 master_open = 1;
632
633 rc = sqlite3OsFileSize(&master, &nMasterJournal);
634 if( rc!=SQLITE_OK ) goto delmaster_out;
635
636 if( nMasterJournal>0 ){
637 char *zDb;
638 zMasterJournal = (char *)sqliteMalloc(nMasterJournal);
639 if( !zMasterJournal ){
640 rc = SQLITE_NOMEM;
641 goto delmaster_out;
642 }
643 rc = sqlite3OsRead(&master, zMasterJournal, nMasterJournal);
644 if( rc!=SQLITE_OK ) goto delmaster_out;
645
646 zDb = zMasterJournal;
647 while( (zDb-zMasterJournal)<nMasterJournal ){
648 char *zJournal = 0;
649 sqlite3SetString(&zJournal, zDb, "-journal", 0);
650 if( !zJournal ){
651 rc = SQLITE_NOMEM;
652 goto delmaster_out;
653 }
654 if( sqlite3OsFileExists(zJournal) ){
655 /* One of the journals pointed to by the master journal exists.
656 ** Open it and check if it points at the master journal. If
657 ** so, return without deleting the master journal file.
658 */
659 OsFile journal;
660 int nMaster;
661
662 rc = sqlite3OsOpenReadOnly(zJournal, &journal);
663 if( rc!=SQLITE_OK ){
664 sqlite3OsClose(&journal);
665 sqliteFree(zJournal);
666 goto delmaster_out;
667 }
668 sqlite3OsClose(&journal);
669
670 /* Seek to the point in the journal where the master journal name
671 ** is stored. Read the master journal name into memory obtained
672 ** from malloc.
673 */
674 rc = sqlite3OsSeek(&journal, sizeof(aJournalMagic3)+2*sizeof(u32));
675 if( rc!=SQLITE_OK ) goto delmaster_out;
676 rc = read32bits(3, &journal, (u32 *)&nMaster);
677 if( rc!=SQLITE_OK ) goto delmaster_out;
678 if( nMaster>0 && nMaster==strlen(zMaster)+1 ){
679 char *zMasterPtr = (char *)sqliteMalloc(nMaster);
680 if( !zMasterPtr ){
681 rc = SQLITE_NOMEM;
682 }
683 rc = sqlite3OsRead(&journal, zMasterPtr, nMaster);
684 if( rc!=SQLITE_OK ){
685 sqliteFree(zMasterPtr);
686 goto delmaster_out;
687 }
688 if( 0==strncmp(zMasterPtr, zMaster, nMaster) ){
689 /* We have a match. Do not delete the master journal file. */
690 sqliteFree(zMasterPtr);
691 goto delmaster_out;
692 }
693 }
694 }
695 zDb += (strlen(zDb)+1);
696 }
697 }
698
699 sqlite3OsDelete(zMaster);
700
701delmaster_out:
702 if( zMasterJournal ){
703 sqliteFree(zMasterJournal);
704 }
705 if( master_open ){
706 sqlite3OsClose(&master);
707 }
708 return rc;
709}
710
711/*
drhed7c8552001-04-11 14:29:21 +0000712** Playback the journal and thus restore the database file to
713** the state it was in before we started making changes.
714**
drh34e79ce2004-02-08 06:05:46 +0000715** The journal file format is as follows:
716**
717** * 8 byte prefix. One of the aJournalMagic123 vectors defined
718** above. The format of the journal file is determined by which
719** of the three prefix vectors is seen.
720** * 4 byte big-endian integer which is the number of valid page records
721** in the journal. If this value is 0xffffffff, then compute the
722** number of page records from the journal size. This field appears
723** in format 3 only.
724** * 4 byte big-endian integer which is the initial value for the
725** sanity checksum. This field appears in format 3 only.
726** * 4 byte integer which is the number of pages to truncate the
727** database to during a rollback.
728** * Zero or more pages instances, each as follows:
729** + 4 byte page number.
drhd0ba1932004-02-10 01:54:28 +0000730** + SQLITE_PAGE_SIZE bytes of data.
drh34e79ce2004-02-08 06:05:46 +0000731** + 4 byte checksum (format 3 only)
732**
733** When we speak of the journal header, we mean the first 4 bullets above.
734** Each entry in the journal is an instance of the 5th bullet. Note that
735** bullets 2 and 3 only appear in format-3 journals.
736**
737** Call the value from the second bullet "nRec". nRec is the number of
738** valid page entries in the journal. In most cases, you can compute the
739** value of nRec from the size of the journal file. But if a power
740** failure occurred while the journal was being written, it could be the
741** case that the size of the journal file had already been increased but
742** the extra entries had not yet made it safely to disk. In such a case,
743** the value of nRec computed from the file size would be too large. For
744** that reason, we always use the nRec value in the header.
745**
746** If the nRec value is 0xffffffff it means that nRec should be computed
747** from the file size. This value is used when the user selects the
748** no-sync option for the journal. A power failure could lead to corruption
749** in this case. But for things like temporary table (which will be
750** deleted when the power is restored) we don't care.
751**
752** Journal formats 1 and 2 do not have an nRec value in the header so we
753** have to compute nRec from the file size. This has risks (as described
754** above) which is why all persistent tables have been changed to use
755** format 3.
drhed7c8552001-04-11 14:29:21 +0000756**
drhd9b02572001-04-15 00:37:09 +0000757** If the file opened as the journal file is not a well-formed
drh34e79ce2004-02-08 06:05:46 +0000758** journal file then the database will likely already be
759** corrupted, so the PAGER_ERR_CORRUPT bit is set in pPager->errMask
760** and SQLITE_CORRUPT is returned. If it all works, then this routine
761** returns SQLITE_OK.
drhed7c8552001-04-11 14:29:21 +0000762*/
drh99ee3602003-02-16 19:13:36 +0000763static int pager_playback(Pager *pPager, int useJournalSize){
drh968af522003-02-11 14:55:40 +0000764 off_t szJ; /* Size of the journal file in bytes */
765 int nRec; /* Number of Records in the journal */
drhd9b02572001-04-15 00:37:09 +0000766 int i; /* Loop counter */
767 Pgno mxPg = 0; /* Size of the original file in pages */
drh968af522003-02-11 14:55:40 +0000768 int format; /* Format of the journal file. */
769 unsigned char aMagic[sizeof(aJournalMagic1)];
drhed7c8552001-04-11 14:29:21 +0000770 int rc;
danielk197713adf8a2004-06-03 16:08:41 +0000771 char *zMaster = 0; /* Name of master journal file if any */
drhed7c8552001-04-11 14:29:21 +0000772
drhc3a64ba2001-11-22 00:01:27 +0000773 /* Figure out how many records are in the journal. Abort early if
774 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +0000775 */
drh8cfbf082001-09-19 13:22:39 +0000776 assert( pPager->journalOpen );
danielk19774adee202004-05-08 08:23:19 +0000777 sqlite3OsSeek(&pPager->jfd, 0);
778 rc = sqlite3OsFileSize(&pPager->jfd, &szJ);
drhc3a64ba2001-11-22 00:01:27 +0000779 if( rc!=SQLITE_OK ){
780 goto end_playback;
781 }
drh240c5792004-02-08 00:40:52 +0000782
783 /* If the journal file is too small to contain a complete header,
drh34e79ce2004-02-08 06:05:46 +0000784 ** it must mean that the process that created the journal was just
785 ** beginning to write the journal file when it died. In that case,
786 ** the database file should have still been completely unchanged.
787 ** Nothing needs to be rolled back. We can safely ignore this journal.
drh240c5792004-02-08 00:40:52 +0000788 */
drh968af522003-02-11 14:55:40 +0000789 if( szJ < sizeof(aMagic)+sizeof(Pgno) ){
drhc3a64ba2001-11-22 00:01:27 +0000790 goto end_playback;
791 }
792
793 /* Read the beginning of the journal and truncate the
794 ** database file back to its original size.
795 */
danielk19774adee202004-05-08 08:23:19 +0000796 rc = sqlite3OsRead(&pPager->jfd, aMagic, sizeof(aMagic));
drh94f33312002-08-12 12:29:56 +0000797 if( rc!=SQLITE_OK ){
drh81a20f22001-10-12 17:30:04 +0000798 rc = SQLITE_PROTOCOL;
799 goto end_playback;
drhd9b02572001-04-15 00:37:09 +0000800 }
drh968af522003-02-11 14:55:40 +0000801 if( memcmp(aMagic, aJournalMagic3, sizeof(aMagic))==0 ){
802 format = JOURNAL_FORMAT_3;
803 }else if( memcmp(aMagic, aJournalMagic2, sizeof(aMagic))==0 ){
804 format = JOURNAL_FORMAT_2;
805 }else if( memcmp(aMagic, aJournalMagic1, sizeof(aMagic))==0 ){
806 format = JOURNAL_FORMAT_1;
drh94f33312002-08-12 12:29:56 +0000807 }else{
808 rc = SQLITE_PROTOCOL;
809 goto end_playback;
810 }
drh968af522003-02-11 14:55:40 +0000811 if( format>=JOURNAL_FORMAT_3 ){
danielk197713adf8a2004-06-03 16:08:41 +0000812 if( szJ < sizeof(aMagic) + 4*sizeof(u32) ){
drh240c5792004-02-08 00:40:52 +0000813 /* Ignore the journal if it is too small to contain a complete
814 ** header. We already did this test once above, but at the prior
815 ** test, we did not know the journal format and so we had to assume
816 ** the smallest possible header. Now we know the header is bigger
drh34e79ce2004-02-08 06:05:46 +0000817 ** than the minimum so we test again.
drh240c5792004-02-08 00:40:52 +0000818 */
819 goto end_playback;
820 }
drh133cdf62004-01-07 02:52:07 +0000821 rc = read32bits(format, &pPager->jfd, (u32*)&nRec);
drh968af522003-02-11 14:55:40 +0000822 if( rc ) goto end_playback;
823 rc = read32bits(format, &pPager->jfd, &pPager->cksumInit);
824 if( rc ) goto end_playback;
drh99ee3602003-02-16 19:13:36 +0000825 if( nRec==0xffffffff || useJournalSize ){
danielk197713adf8a2004-06-03 16:08:41 +0000826 nRec = (szJ - JOURNAL_HDR_SZ(pPager, 3))/JOURNAL_PG_SZ(3);
827 }
828
829 /* Check if a master journal file is specified. If one is specified,
830 ** only proceed with the playback if it still exists.
831 */
832 rc = read32bits(format, &pPager->jfd, &pPager->nMaster);
833 if( rc ) goto end_playback;
834 if( pPager->nMaster>0 ){
835 zMaster = sqliteMalloc(pPager->nMaster);
836 if( !zMaster ){
837 rc = SQLITE_NOMEM;
838 goto end_playback;
839 }
840 rc = sqlite3OsRead(&pPager->jfd, zMaster, pPager->nMaster);
841 if( rc!=SQLITE_OK || (strlen(zMaster) && !sqlite3OsFileExists(zMaster)) ){
842 goto end_playback;
843 }
drh968af522003-02-11 14:55:40 +0000844 }
845 }else{
danielk197713adf8a2004-06-03 16:08:41 +0000846 nRec = (szJ - JOURNAL_HDR_SZ(pPager, 2))/JOURNAL_PG_SZ(2);
847 assert( nRec*JOURNAL_PG_SZ(2)+JOURNAL_HDR_SZ(pPager, 2)==szJ );
drh968af522003-02-11 14:55:40 +0000848 }
849 rc = read32bits(format, &pPager->jfd, &mxPg);
drhd9b02572001-04-15 00:37:09 +0000850 if( rc!=SQLITE_OK ){
drh81a20f22001-10-12 17:30:04 +0000851 goto end_playback;
drhd9b02572001-04-15 00:37:09 +0000852 }
drhd8d66e82003-02-12 02:10:15 +0000853 assert( pPager->origDbSize==0 || pPager->origDbSize==mxPg );
danielk19774adee202004-05-08 08:23:19 +0000854 rc = sqlite3OsTruncate(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)mxPg);
drh81a20f22001-10-12 17:30:04 +0000855 if( rc!=SQLITE_OK ){
856 goto end_playback;
857 }
drhd9b02572001-04-15 00:37:09 +0000858 pPager->dbSize = mxPg;
859
drhfa86c412002-02-02 15:01:15 +0000860 /* Copy original pages out of the journal and back into the database file.
drhed7c8552001-04-11 14:29:21 +0000861 */
drh968af522003-02-11 14:55:40 +0000862 for(i=0; i<nRec; i++){
863 rc = pager_playback_one_page(pPager, &pPager->jfd, format);
864 if( rc!=SQLITE_OK ){
865 if( rc==SQLITE_DONE ){
drh968af522003-02-11 14:55:40 +0000866 rc = SQLITE_OK;
867 }
868 break;
869 }
drhed7c8552001-04-11 14:29:21 +0000870 }
drh81a20f22001-10-12 17:30:04 +0000871
drh4a0681e2003-02-13 01:58:20 +0000872 /* Pages that have been written to the journal but never synced
873 ** where not restored by the loop above. We have to restore those
drh240c5792004-02-08 00:40:52 +0000874 ** pages by reading them back from the original database.
drhdb48ee02003-01-16 13:42:43 +0000875 */
876 if( rc==SQLITE_OK ){
877 PgHdr *pPg;
878 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
drhd0ba1932004-02-10 01:54:28 +0000879 char zBuf[SQLITE_PAGE_SIZE];
drh4a0681e2003-02-13 01:58:20 +0000880 if( !pPg->dirty ) continue;
drhdb48ee02003-01-16 13:42:43 +0000881 if( (int)pPg->pgno <= pPager->origDbSize ){
danielk19774adee202004-05-08 08:23:19 +0000882 sqlite3OsSeek(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)(pPg->pgno-1));
883 rc = sqlite3OsRead(&pPager->fd, zBuf, SQLITE_PAGE_SIZE);
drh9eb9e262004-02-11 02:18:05 +0000884 TRACE2("REFETCH %d\n", pPg->pgno);
885 CODEC(pPager, zBuf, pPg->pgno, 2);
drhdb48ee02003-01-16 13:42:43 +0000886 if( rc ) break;
887 }else{
drhd0ba1932004-02-10 01:54:28 +0000888 memset(zBuf, 0, SQLITE_PAGE_SIZE);
drhdb48ee02003-01-16 13:42:43 +0000889 }
drhd0ba1932004-02-10 01:54:28 +0000890 if( pPg->nRef==0 || memcmp(zBuf, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE) ){
891 memcpy(PGHDR_TO_DATA(pPg), zBuf, SQLITE_PAGE_SIZE);
drh3a840692003-01-29 22:58:26 +0000892 memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra);
893 }
drhdb48ee02003-01-16 13:42:43 +0000894 pPg->needSync = 0;
895 pPg->dirty = 0;
896 }
897 }
drh4a0681e2003-02-13 01:58:20 +0000898
899end_playback:
danielk197713adf8a2004-06-03 16:08:41 +0000900 if( zMaster ){
901 /* If there was a master journal and this routine will return true,
902 ** see if it is possible to delete the master journal. If errors
903 ** occur during this process, ignore them.
904 */
905 if( rc==SQLITE_OK ){
906 pager_delmaster(zMaster);
907 }
908 sqliteFree(zMaster);
909 }
drhd9b02572001-04-15 00:37:09 +0000910 if( rc!=SQLITE_OK ){
danielk197713adf8a2004-06-03 16:08:41 +0000911 /* FIX ME: We shouldn't delete the journal if an error occured during
912 ** rollback. It may have been a transient error and the rollback may
913 ** succeed next time it is attempted.
914 */
drhd9b02572001-04-15 00:37:09 +0000915 pager_unwritelock(pPager);
916 pPager->errMask |= PAGER_ERR_CORRUPT;
917 rc = SQLITE_CORRUPT;
918 }else{
919 rc = pager_unwritelock(pPager);
drhed7c8552001-04-11 14:29:21 +0000920 }
drhd9b02572001-04-15 00:37:09 +0000921 return rc;
drhed7c8552001-04-11 14:29:21 +0000922}
923
924/*
drhac69b052004-05-12 13:30:07 +0000925** Playback the statement journal.
drhfa86c412002-02-02 15:01:15 +0000926**
927** This is similar to playing back the transaction journal but with
928** a few extra twists.
929**
drh663fc632002-02-02 18:49:19 +0000930** (1) The number of pages in the database file at the start of
drhac69b052004-05-12 13:30:07 +0000931** the statement is stored in pPager->stmtSize, not in the
drh663fc632002-02-02 18:49:19 +0000932** journal file itself.
drhfa86c412002-02-02 15:01:15 +0000933**
drhac69b052004-05-12 13:30:07 +0000934** (2) In addition to playing back the statement journal, also
drhfa86c412002-02-02 15:01:15 +0000935** playback all pages of the transaction journal beginning
drhac69b052004-05-12 13:30:07 +0000936** at offset pPager->stmtJSize.
drhfa86c412002-02-02 15:01:15 +0000937*/
drh3aac2dd2004-04-26 14:10:20 +0000938static int pager_stmt_playback(Pager *pPager){
drh968af522003-02-11 14:55:40 +0000939 off_t szJ; /* Size of the full journal */
940 int nRec; /* Number of Records */
drhfa86c412002-02-02 15:01:15 +0000941 int i; /* Loop counter */
942 int rc;
943
944 /* Truncate the database back to its original size.
945 */
drhac69b052004-05-12 13:30:07 +0000946 rc = sqlite3OsTruncate(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)pPager->stmtSize);
947 pPager->dbSize = pPager->stmtSize;
drhfa86c412002-02-02 15:01:15 +0000948
drhac69b052004-05-12 13:30:07 +0000949 /* Figure out how many records are in the statement journal.
drhfa86c412002-02-02 15:01:15 +0000950 */
drhac69b052004-05-12 13:30:07 +0000951 assert( pPager->stmtInUse && pPager->journalOpen );
952 sqlite3OsSeek(&pPager->stfd, 0);
953 nRec = pPager->stmtNRec;
drhfa86c412002-02-02 15:01:15 +0000954
drhac69b052004-05-12 13:30:07 +0000955 /* Copy original pages out of the statement journal and back into the
956 ** database file. Note that the statement journal always uses format
drh968af522003-02-11 14:55:40 +0000957 ** 2 instead of format 3 since it does not need to be concerned with
958 ** power failures corrupting the journal and can thus omit the checksums.
drhfa86c412002-02-02 15:01:15 +0000959 */
960 for(i=nRec-1; i>=0; i--){
drhac69b052004-05-12 13:30:07 +0000961 rc = pager_playback_one_page(pPager, &pPager->stfd, 2);
drh968af522003-02-11 14:55:40 +0000962 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +0000963 if( rc!=SQLITE_OK ) goto end_stmt_playback;
drhfa86c412002-02-02 15:01:15 +0000964 }
965
966 /* Figure out how many pages need to be copied out of the transaction
967 ** journal.
968 */
drhac69b052004-05-12 13:30:07 +0000969 rc = sqlite3OsSeek(&pPager->jfd, pPager->stmtJSize);
drhfa86c412002-02-02 15:01:15 +0000970 if( rc!=SQLITE_OK ){
drh3aac2dd2004-04-26 14:10:20 +0000971 goto end_stmt_playback;
drhfa86c412002-02-02 15:01:15 +0000972 }
danielk19774adee202004-05-08 08:23:19 +0000973 rc = sqlite3OsFileSize(&pPager->jfd, &szJ);
drhfa86c412002-02-02 15:01:15 +0000974 if( rc!=SQLITE_OK ){
drh3aac2dd2004-04-26 14:10:20 +0000975 goto end_stmt_playback;
drhfa86c412002-02-02 15:01:15 +0000976 }
drhac69b052004-05-12 13:30:07 +0000977 nRec = (szJ - pPager->stmtJSize)/JOURNAL_PG_SZ(journal_format);
drhfa86c412002-02-02 15:01:15 +0000978 for(i=nRec-1; i>=0; i--){
drh968af522003-02-11 14:55:40 +0000979 rc = pager_playback_one_page(pPager, &pPager->jfd, journal_format);
980 if( rc!=SQLITE_OK ){
981 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +0000982 goto end_stmt_playback;
drh968af522003-02-11 14:55:40 +0000983 }
drhfa86c412002-02-02 15:01:15 +0000984 }
985
drh3aac2dd2004-04-26 14:10:20 +0000986end_stmt_playback:
drhfa86c412002-02-02 15:01:15 +0000987 if( rc!=SQLITE_OK ){
drhfa86c412002-02-02 15:01:15 +0000988 pPager->errMask |= PAGER_ERR_CORRUPT;
989 rc = SQLITE_CORRUPT;
drhfa86c412002-02-02 15:01:15 +0000990 }
991 return rc;
992}
993
994/*
drhf57b14a2001-09-14 18:54:08 +0000995** Change the maximum number of in-memory pages that are allowed.
drhcd61c282002-03-06 22:01:34 +0000996**
997** The maximum number is the absolute value of the mxPage parameter.
998** If mxPage is negative, the noSync flag is also set. noSync bypasses
danielk19774adee202004-05-08 08:23:19 +0000999** calls to sqlite3OsSync(). The pager runs much faster with noSync on,
drhcd61c282002-03-06 22:01:34 +00001000** but if the operating system crashes or there is an abrupt power
1001** failure, the database file might be left in an inconsistent and
1002** unrepairable state.
drhf57b14a2001-09-14 18:54:08 +00001003*/
drh3aac2dd2004-04-26 14:10:20 +00001004void sqlite3pager_set_cachesize(Pager *pPager, int mxPage){
drh603240c2002-03-05 01:11:12 +00001005 if( mxPage>=0 ){
drha1680452002-04-18 01:56:57 +00001006 pPager->noSync = pPager->tempFile;
danielk19771d850a72004-05-31 08:26:49 +00001007 if( pPager->noSync ) pPager->needSync = 0;
drh603240c2002-03-05 01:11:12 +00001008 }else{
1009 pPager->noSync = 1;
1010 mxPage = -mxPage;
1011 }
drhf57b14a2001-09-14 18:54:08 +00001012 if( mxPage>10 ){
1013 pPager->mxPage = mxPage;
1014 }
1015}
1016
1017/*
drh973b6e32003-02-12 14:09:42 +00001018** Adjust the robustness of the database to damage due to OS crashes
1019** or power failures by changing the number of syncs()s when writing
1020** the rollback journal. There are three levels:
1021**
danielk19774adee202004-05-08 08:23:19 +00001022** OFF sqlite3OsSync() is never called. This is the default
drh973b6e32003-02-12 14:09:42 +00001023** for temporary and transient files.
1024**
1025** NORMAL The journal is synced once before writes begin on the
1026** database. This is normally adequate protection, but
1027** it is theoretically possible, though very unlikely,
1028** that an inopertune power failure could leave the journal
1029** in a state which would cause damage to the database
1030** when it is rolled back.
1031**
1032** FULL The journal is synced twice before writes begin on the
drh34e79ce2004-02-08 06:05:46 +00001033** database (with some additional information - the nRec field
1034** of the journal header - being written in between the two
1035** syncs). If we assume that writing a
drh973b6e32003-02-12 14:09:42 +00001036** single disk sector is atomic, then this mode provides
1037** assurance that the journal will not be corrupted to the
1038** point of causing damage to the database during rollback.
1039**
1040** Numeric values associated with these states are OFF==1, NORMAL=2,
1041** and FULL=3.
1042*/
drh3aac2dd2004-04-26 14:10:20 +00001043void sqlite3pager_set_safety_level(Pager *pPager, int level){
drh973b6e32003-02-12 14:09:42 +00001044 pPager->noSync = level==1 || pPager->tempFile;
1045 pPager->fullSync = level==3 && !pPager->tempFile;
danielk19771d850a72004-05-31 08:26:49 +00001046 if( pPager->noSync ) pPager->needSync = 0;
drh973b6e32003-02-12 14:09:42 +00001047}
1048
1049/*
drhfa86c412002-02-02 15:01:15 +00001050** Open a temporary file. Write the name of the file into zName
1051** (zName must be at least SQLITE_TEMPNAME_SIZE bytes long.) Write
1052** the file descriptor into *fd. Return SQLITE_OK on success or some
1053** other error code if we fail.
1054**
1055** The OS will automatically delete the temporary file when it is
1056** closed.
1057*/
drh3aac2dd2004-04-26 14:10:20 +00001058static int sqlite3pager_opentemp(char *zFile, OsFile *fd){
drhfa86c412002-02-02 15:01:15 +00001059 int cnt = 8;
1060 int rc;
1061 do{
1062 cnt--;
danielk19774adee202004-05-08 08:23:19 +00001063 sqlite3OsTempFileName(zFile);
1064 rc = sqlite3OsOpenExclusive(zFile, fd, 1);
drhfa86c412002-02-02 15:01:15 +00001065 }while( cnt>0 && rc!=SQLITE_OK );
1066 return rc;
1067}
1068
1069/*
drhed7c8552001-04-11 14:29:21 +00001070** Create a new page cache and put a pointer to the page cache in *ppPager.
drh5e00f6c2001-09-13 13:46:56 +00001071** The file to be cached need not exist. The file is not locked until
drh3aac2dd2004-04-26 14:10:20 +00001072** the first call to sqlite3pager_get() and is only held open until the
1073** last page is released using sqlite3pager_unref().
drh382c0242001-10-06 16:33:02 +00001074**
drh6446c4d2001-12-15 14:22:18 +00001075** If zFilename is NULL then a randomly-named temporary file is created
1076** and used as the file to be cached. The file will be deleted
1077** automatically when it is closed.
drhed7c8552001-04-11 14:29:21 +00001078*/
drh3aac2dd2004-04-26 14:10:20 +00001079int sqlite3pager_open(
drh7e3b0a02001-04-28 16:52:40 +00001080 Pager **ppPager, /* Return the Pager structure here */
1081 const char *zFilename, /* Name of the database file to open */
1082 int mxPage, /* Max number of in-memory cache pages */
drhda47d772002-12-02 04:25:19 +00001083 int nExtra, /* Extra bytes append to each in-memory page */
danielk197724162fe2004-06-04 06:22:00 +00001084 int useJournal, /* TRUE to use a rollback journal on this file */
1085 void *pBusyHandler /* Busy callback */
drh7e3b0a02001-04-28 16:52:40 +00001086){
drhed7c8552001-04-11 14:29:21 +00001087 Pager *pPager;
drh3e7a6092002-12-07 21:45:14 +00001088 char *zFullPathname;
drhed7c8552001-04-11 14:29:21 +00001089 int nameLen;
drh8cfbf082001-09-19 13:22:39 +00001090 OsFile fd;
drha76c82e2003-07-27 18:59:42 +00001091 int rc, i;
drh5e00f6c2001-09-13 13:46:56 +00001092 int tempFile;
drhac69b052004-05-12 13:30:07 +00001093 int memDb = 0;
drh5e00f6c2001-09-13 13:46:56 +00001094 int readOnly = 0;
drh8cfbf082001-09-19 13:22:39 +00001095 char zTemp[SQLITE_TEMPNAME_SIZE];
drhed7c8552001-04-11 14:29:21 +00001096
drhd9b02572001-04-15 00:37:09 +00001097 *ppPager = 0;
danielk19776f8a5032004-05-10 10:34:51 +00001098 if( sqlite3_malloc_failed ){
drhd9b02572001-04-15 00:37:09 +00001099 return SQLITE_NOMEM;
1100 }
drh901afd42003-08-26 11:25:58 +00001101 if( zFilename && zFilename[0] ){
drhac69b052004-05-12 13:30:07 +00001102 if( strcmp(zFilename,":memory:")==0 ){
1103 memDb = 1;
1104 zFullPathname = sqliteMalloc(4);
1105 if( zFullPathname ) strcpy(zFullPathname, "nil");
1106 rc = SQLITE_OK;
1107 }else{
1108 zFullPathname = sqlite3OsFullPathname(zFilename);
1109 rc = sqlite3OsOpenReadWrite(zFullPathname, &fd, &readOnly);
1110 tempFile = 0;
1111 }
drh5e00f6c2001-09-13 13:46:56 +00001112 }else{
drh3aac2dd2004-04-26 14:10:20 +00001113 rc = sqlite3pager_opentemp(zTemp, &fd);
drh5e00f6c2001-09-13 13:46:56 +00001114 zFilename = zTemp;
danielk19774adee202004-05-08 08:23:19 +00001115 zFullPathname = sqlite3OsFullPathname(zFilename);
drh5e00f6c2001-09-13 13:46:56 +00001116 tempFile = 1;
1117 }
danielk19776f8a5032004-05-10 10:34:51 +00001118 if( sqlite3_malloc_failed ){
drh3e7a6092002-12-07 21:45:14 +00001119 return SQLITE_NOMEM;
1120 }
drh8cfbf082001-09-19 13:22:39 +00001121 if( rc!=SQLITE_OK ){
drh3e7a6092002-12-07 21:45:14 +00001122 sqliteFree(zFullPathname);
drhed7c8552001-04-11 14:29:21 +00001123 return SQLITE_CANTOPEN;
1124 }
drh3e7a6092002-12-07 21:45:14 +00001125 nameLen = strlen(zFullPathname);
drha76c82e2003-07-27 18:59:42 +00001126 pPager = sqliteMalloc( sizeof(*pPager) + nameLen*3 + 30 );
drhd9b02572001-04-15 00:37:09 +00001127 if( pPager==0 ){
danielk19774adee202004-05-08 08:23:19 +00001128 sqlite3OsClose(&fd);
drh3e7a6092002-12-07 21:45:14 +00001129 sqliteFree(zFullPathname);
drhd9b02572001-04-15 00:37:09 +00001130 return SQLITE_NOMEM;
1131 }
drhdb48ee02003-01-16 13:42:43 +00001132 SET_PAGER(pPager);
drhed7c8552001-04-11 14:29:21 +00001133 pPager->zFilename = (char*)&pPager[1];
drha76c82e2003-07-27 18:59:42 +00001134 pPager->zDirectory = &pPager->zFilename[nameLen+1];
1135 pPager->zJournal = &pPager->zDirectory[nameLen+1];
drh3e7a6092002-12-07 21:45:14 +00001136 strcpy(pPager->zFilename, zFullPathname);
drha76c82e2003-07-27 18:59:42 +00001137 strcpy(pPager->zDirectory, zFullPathname);
1138 for(i=nameLen; i>0 && pPager->zDirectory[i-1]!='/'; i--){}
1139 if( i>0 ) pPager->zDirectory[i-1] = 0;
drh3e7a6092002-12-07 21:45:14 +00001140 strcpy(pPager->zJournal, zFullPathname);
1141 sqliteFree(zFullPathname);
drhed7c8552001-04-11 14:29:21 +00001142 strcpy(&pPager->zJournal[nameLen], "-journal");
1143 pPager->fd = fd;
drh8cfbf082001-09-19 13:22:39 +00001144 pPager->journalOpen = 0;
drhac69b052004-05-12 13:30:07 +00001145 pPager->useJournal = useJournal && !memDb;
1146 pPager->stmtOpen = 0;
1147 pPager->stmtInUse = 0;
drhed7c8552001-04-11 14:29:21 +00001148 pPager->nRef = 0;
drhac69b052004-05-12 13:30:07 +00001149 pPager->dbSize = memDb-1;
1150 pPager->pageSize = SQLITE_PAGE_SIZE;
1151 pPager->stmtSize = 0;
1152 pPager->stmtJSize = 0;
drhed7c8552001-04-11 14:29:21 +00001153 pPager->nPage = 0;
drhd79caeb2001-04-15 02:27:24 +00001154 pPager->mxPage = mxPage>5 ? mxPage : 10;
drhed7c8552001-04-11 14:29:21 +00001155 pPager->state = SQLITE_UNLOCK;
drhd9b02572001-04-15 00:37:09 +00001156 pPager->errMask = 0;
drh5e00f6c2001-09-13 13:46:56 +00001157 pPager->tempFile = tempFile;
drhac69b052004-05-12 13:30:07 +00001158 pPager->memDb = memDb;
drh5e00f6c2001-09-13 13:46:56 +00001159 pPager->readOnly = readOnly;
drhf57b14a2001-09-14 18:54:08 +00001160 pPager->needSync = 0;
drhda47d772002-12-02 04:25:19 +00001161 pPager->noSync = pPager->tempFile || !useJournal;
drhed7c8552001-04-11 14:29:21 +00001162 pPager->pFirst = 0;
drh341eae82003-01-21 02:39:36 +00001163 pPager->pFirstSynced = 0;
drhed7c8552001-04-11 14:29:21 +00001164 pPager->pLast = 0;
drh7c717f72001-06-24 20:39:41 +00001165 pPager->nExtra = nExtra;
danielk197724162fe2004-06-04 06:22:00 +00001166 pPager->pBusyHandler = (BusyHandler *)pBusyHandler;
drhed7c8552001-04-11 14:29:21 +00001167 memset(pPager->aHash, 0, sizeof(pPager->aHash));
1168 *ppPager = pPager;
1169 return SQLITE_OK;
1170}
1171
1172/*
drh72f82862001-05-24 21:06:34 +00001173** Set the destructor for this pager. If not NULL, the destructor is called
drh5e00f6c2001-09-13 13:46:56 +00001174** when the reference count on each page reaches zero. The destructor can
1175** be used to clean up information in the extra segment appended to each page.
drh72f82862001-05-24 21:06:34 +00001176**
drh3aac2dd2004-04-26 14:10:20 +00001177** The destructor is not called as a result sqlite3pager_close().
1178** Destructors are only called by sqlite3pager_unref().
drh72f82862001-05-24 21:06:34 +00001179*/
drhb6f41482004-05-14 01:58:11 +00001180void sqlite3pager_set_destructor(Pager *pPager, void (*xDesc)(void*,int)){
drh72f82862001-05-24 21:06:34 +00001181 pPager->xDestructor = xDesc;
1182}
1183
1184/*
drh5e00f6c2001-09-13 13:46:56 +00001185** Return the total number of pages in the disk file associated with
1186** pPager.
drhed7c8552001-04-11 14:29:21 +00001187*/
drh3aac2dd2004-04-26 14:10:20 +00001188int sqlite3pager_pagecount(Pager *pPager){
drh28be87c2002-11-05 23:03:02 +00001189 off_t n;
drhd9b02572001-04-15 00:37:09 +00001190 assert( pPager!=0 );
drhed7c8552001-04-11 14:29:21 +00001191 if( pPager->dbSize>=0 ){
1192 return pPager->dbSize;
1193 }
danielk19774adee202004-05-08 08:23:19 +00001194 if( sqlite3OsFileSize(&pPager->fd, &n)!=SQLITE_OK ){
drh81a20f22001-10-12 17:30:04 +00001195 pPager->errMask |= PAGER_ERR_DISK;
drh8cfbf082001-09-19 13:22:39 +00001196 return 0;
drhed7c8552001-04-11 14:29:21 +00001197 }
drhd0ba1932004-02-10 01:54:28 +00001198 n /= SQLITE_PAGE_SIZE;
drhd9b02572001-04-15 00:37:09 +00001199 if( pPager->state!=SQLITE_UNLOCK ){
drhed7c8552001-04-11 14:29:21 +00001200 pPager->dbSize = n;
1201 }
1202 return n;
1203}
1204
1205/*
drhf7c57532003-04-25 13:22:51 +00001206** Forward declaration
1207*/
danielk197713adf8a2004-06-03 16:08:41 +00001208static int syncJournal(Pager*, const char*);
drhf7c57532003-04-25 13:22:51 +00001209
drhac69b052004-05-12 13:30:07 +00001210
1211/*
1212** Unlink a page from the free list (the list of all pages where nRef==0)
1213** and from its hash collision chain.
1214*/
1215static void unlinkPage(PgHdr *pPg){
1216 Pager *pPager = pPg->pPager;
1217
1218 /* Keep the pFirstSynced pointer pointing at the first synchronized page */
1219 if( pPg==pPager->pFirstSynced ){
1220 PgHdr *p = pPg->pNextFree;
1221 while( p && p->needSync ){ p = p->pNextFree; }
1222 pPager->pFirstSynced = p;
1223 }
1224
1225 /* Unlink from the freelist */
1226 if( pPg->pPrevFree ){
1227 pPg->pPrevFree->pNextFree = pPg->pNextFree;
1228 }else{
1229 assert( pPager->pFirst==pPg );
1230 pPager->pFirst = pPg->pNextFree;
1231 }
1232 if( pPg->pNextFree ){
1233 pPg->pNextFree->pPrevFree = pPg->pPrevFree;
1234 }else{
1235 assert( pPager->pLast==pPg );
1236 pPager->pLast = pPg->pPrevFree;
1237 }
1238 pPg->pNextFree = pPg->pPrevFree = 0;
1239
1240 /* Unlink from the pgno hash table */
1241 if( pPg->pNextHash ){
1242 pPg->pNextHash->pPrevHash = pPg->pPrevHash;
1243 }
1244 if( pPg->pPrevHash ){
1245 pPg->pPrevHash->pNextHash = pPg->pNextHash;
1246 }else{
1247 int h = pager_hash(pPg->pgno);
1248 assert( pPager->aHash[h]==pPg );
1249 pPager->aHash[h] = pPg->pNextHash;
1250 }
1251 pPg->pNextHash = pPg->pPrevHash = 0;
1252}
1253
1254/*
1255** This routine is used to truncate an in-memory database. Delete
1256** every pages whose pgno is larger than pPager->dbSize and is unreferenced.
1257** Referenced pages larger than pPager->dbSize are zeroed.
1258*/
1259static void memoryTruncate(Pager *pPager){
1260 PgHdr *pPg;
1261 PgHdr **ppPg;
1262 int dbSize = pPager->dbSize;
1263
1264 ppPg = &pPager->pAll;
1265 while( (pPg = *ppPg)!=0 ){
1266 if( pPg->pgno<=dbSize ){
1267 ppPg = &pPg->pNextAll;
1268 }else if( pPg->nRef>0 ){
1269 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
1270 ppPg = &pPg->pNextAll;
1271 }else{
1272 *ppPg = pPg->pNextAll;
1273 unlinkPage(pPg);
1274 sqliteFree(pPg);
1275 pPager->nPage--;
1276 }
1277 }
1278}
1279
drhf7c57532003-04-25 13:22:51 +00001280/*
1281** Truncate the file to the number of pages specified.
1282*/
drh3aac2dd2004-04-26 14:10:20 +00001283int sqlite3pager_truncate(Pager *pPager, Pgno nPage){
drhf7c57532003-04-25 13:22:51 +00001284 int rc;
drh2e6d11b2003-04-25 15:37:57 +00001285 if( pPager->dbSize<0 ){
drh3aac2dd2004-04-26 14:10:20 +00001286 sqlite3pager_pagecount(pPager);
drh2e6d11b2003-04-25 15:37:57 +00001287 }
1288 if( pPager->errMask!=0 ){
1289 rc = pager_errcode(pPager);
1290 return rc;
1291 }
drh7d02cb72003-06-04 16:24:39 +00001292 if( nPage>=(unsigned)pPager->dbSize ){
drhf7c57532003-04-25 13:22:51 +00001293 return SQLITE_OK;
1294 }
drhac69b052004-05-12 13:30:07 +00001295 if( pPager->memDb ){
1296 pPager->dbSize = nPage;
1297 memoryTruncate(pPager);
1298 return SQLITE_OK;
1299 }
danielk197713adf8a2004-06-03 16:08:41 +00001300 syncJournal(pPager, 0);
danielk19774adee202004-05-08 08:23:19 +00001301 rc = sqlite3OsTruncate(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)nPage);
drhf7c57532003-04-25 13:22:51 +00001302 if( rc==SQLITE_OK ){
1303 pPager->dbSize = nPage;
1304 }
1305 return rc;
1306}
1307
1308/*
drhed7c8552001-04-11 14:29:21 +00001309** Shutdown the page cache. Free all memory and close all files.
1310**
1311** If a transaction was in progress when this routine is called, that
1312** transaction is rolled back. All outstanding pages are invalidated
1313** and their memory is freed. Any attempt to use a page associated
1314** with this page cache after this function returns will likely
1315** result in a coredump.
1316*/
drh3aac2dd2004-04-26 14:10:20 +00001317int sqlite3pager_close(Pager *pPager){
drhd9b02572001-04-15 00:37:09 +00001318 PgHdr *pPg, *pNext;
drhed7c8552001-04-11 14:29:21 +00001319 switch( pPager->state ){
1320 case SQLITE_WRITELOCK: {
drh3aac2dd2004-04-26 14:10:20 +00001321 sqlite3pager_rollback(pPager);
drhac69b052004-05-12 13:30:07 +00001322 if( !pPager->memDb ){
1323 sqlite3OsUnlock(&pPager->fd);
1324 }
drh8cfbf082001-09-19 13:22:39 +00001325 assert( pPager->journalOpen==0 );
drhed7c8552001-04-11 14:29:21 +00001326 break;
1327 }
1328 case SQLITE_READLOCK: {
drhac69b052004-05-12 13:30:07 +00001329 if( !pPager->memDb ){
1330 sqlite3OsUnlock(&pPager->fd);
1331 }
drhed7c8552001-04-11 14:29:21 +00001332 break;
1333 }
1334 default: {
1335 /* Do nothing */
1336 break;
1337 }
1338 }
drhd9b02572001-04-15 00:37:09 +00001339 for(pPg=pPager->pAll; pPg; pPg=pNext){
1340 pNext = pPg->pNextAll;
1341 sqliteFree(pPg);
drhed7c8552001-04-11 14:29:21 +00001342 }
drhac69b052004-05-12 13:30:07 +00001343 if( !pPager->memDb ){
1344 sqlite3OsClose(&pPager->fd);
1345 }
drh8cfbf082001-09-19 13:22:39 +00001346 assert( pPager->journalOpen==0 );
drh0f892532002-05-30 12:27:03 +00001347 /* Temp files are automatically deleted by the OS
1348 ** if( pPager->tempFile ){
danielk19774adee202004-05-08 08:23:19 +00001349 ** sqlite3OsDelete(pPager->zFilename);
drh0f892532002-05-30 12:27:03 +00001350 ** }
1351 */
drhdb48ee02003-01-16 13:42:43 +00001352 CLR_PAGER(pPager);
drh73509ee2003-04-06 20:44:45 +00001353 if( pPager->zFilename!=(char*)&pPager[1] ){
drha76c82e2003-07-27 18:59:42 +00001354 assert( 0 ); /* Cannot happen */
drh73509ee2003-04-06 20:44:45 +00001355 sqliteFree(pPager->zFilename);
1356 sqliteFree(pPager->zJournal);
drha76c82e2003-07-27 18:59:42 +00001357 sqliteFree(pPager->zDirectory);
drh73509ee2003-04-06 20:44:45 +00001358 }
drhed7c8552001-04-11 14:29:21 +00001359 sqliteFree(pPager);
1360 return SQLITE_OK;
1361}
1362
1363/*
drh5e00f6c2001-09-13 13:46:56 +00001364** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +00001365*/
drh3aac2dd2004-04-26 14:10:20 +00001366Pgno sqlite3pager_pagenumber(void *pData){
drhed7c8552001-04-11 14:29:21 +00001367 PgHdr *p = DATA_TO_PGHDR(pData);
1368 return p->pgno;
1369}
1370
1371/*
drhc8629a12004-05-08 20:07:40 +00001372** The page_ref() function increments the reference count for a page.
1373** If the page is currently on the freelist (the reference count is zero) then
drh7e3b0a02001-04-28 16:52:40 +00001374** remove it from the freelist.
drhc8629a12004-05-08 20:07:40 +00001375**
1376** For non-test systems, page_ref() is a macro that calls _page_ref()
1377** online of the reference count is zero. For test systems, page_ref()
1378** is a real function so that we can set breakpoints and trace it.
drh7e3b0a02001-04-28 16:52:40 +00001379*/
drh836faa42003-01-11 13:30:57 +00001380static void _page_ref(PgHdr *pPg){
drh7e3b0a02001-04-28 16:52:40 +00001381 if( pPg->nRef==0 ){
1382 /* The page is currently on the freelist. Remove it. */
drh341eae82003-01-21 02:39:36 +00001383 if( pPg==pPg->pPager->pFirstSynced ){
1384 PgHdr *p = pPg->pNextFree;
1385 while( p && p->needSync ){ p = p->pNextFree; }
1386 pPg->pPager->pFirstSynced = p;
1387 }
drh7e3b0a02001-04-28 16:52:40 +00001388 if( pPg->pPrevFree ){
1389 pPg->pPrevFree->pNextFree = pPg->pNextFree;
1390 }else{
1391 pPg->pPager->pFirst = pPg->pNextFree;
1392 }
1393 if( pPg->pNextFree ){
1394 pPg->pNextFree->pPrevFree = pPg->pPrevFree;
1395 }else{
1396 pPg->pPager->pLast = pPg->pPrevFree;
1397 }
1398 pPg->pPager->nRef++;
1399 }
1400 pPg->nRef++;
drhdd793422001-06-28 01:54:48 +00001401 REFINFO(pPg);
drhdf0b3b02001-06-23 11:36:20 +00001402}
drhc8629a12004-05-08 20:07:40 +00001403#ifdef SQLITE_TEST
1404 static void page_ref(PgHdr *pPg){
1405 if( pPg->nRef==0 ){
1406 _page_ref(pPg);
1407 }else{
1408 pPg->nRef++;
1409 REFINFO(pPg);
1410 }
1411 }
1412#else
1413# define page_ref(P) ((P)->nRef==0?_page_ref(P):(void)(P)->nRef++)
1414#endif
drhdf0b3b02001-06-23 11:36:20 +00001415
1416/*
1417** Increment the reference count for a page. The input pointer is
1418** a reference to the page data.
1419*/
drh3aac2dd2004-04-26 14:10:20 +00001420int sqlite3pager_ref(void *pData){
drhdf0b3b02001-06-23 11:36:20 +00001421 PgHdr *pPg = DATA_TO_PGHDR(pData);
1422 page_ref(pPg);
drh8c42ca92001-06-22 19:15:00 +00001423 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00001424}
1425
1426/*
drh34e79ce2004-02-08 06:05:46 +00001427** Sync the journal. In other words, make sure all the pages that have
1428** been written to the journal have actually reached the surface of the
1429** disk. It is not safe to modify the original database file until after
1430** the journal has been synced. If the original database is modified before
1431** the journal is synced and a power failure occurs, the unsynced journal
1432** data would be lost and we would be unable to completely rollback the
1433** database changes. Database corruption would occur.
1434**
1435** This routine also updates the nRec field in the header of the journal.
1436** (See comments on the pager_playback() routine for additional information.)
1437** If the sync mode is FULL, two syncs will occur. First the whole journal
1438** is synced, then the nRec field is updated, then a second sync occurs.
drhb19a2bc2001-09-16 00:13:26 +00001439**
drh34e79ce2004-02-08 06:05:46 +00001440** For temporary databases, we do not care if we are able to rollback
1441** after a power failure, so sync occurs.
drhfa86c412002-02-02 15:01:15 +00001442**
drh34e79ce2004-02-08 06:05:46 +00001443** This routine clears the needSync field of every page current held in
1444** memory.
drh50e5dad2001-09-15 00:57:28 +00001445*/
danielk197713adf8a2004-06-03 16:08:41 +00001446static int syncJournal(Pager *pPager, const char *zMaster){
drh50e5dad2001-09-15 00:57:28 +00001447 PgHdr *pPg;
1448 int rc = SQLITE_OK;
drh03eb96a2002-11-10 23:32:56 +00001449
1450 /* Sync the journal before modifying the main database
1451 ** (assuming there is a journal and it needs to be synced.)
1452 */
danielk197713adf8a2004-06-03 16:08:41 +00001453 if( pPager->needSync || zMaster ){
drhfa86c412002-02-02 15:01:15 +00001454 if( !pPager->tempFile ){
drhdb48ee02003-01-16 13:42:43 +00001455 assert( pPager->journalOpen );
drh946966f2004-02-25 02:20:41 +00001456 /* assert( !pPager->noSync ); // noSync might be set if synchronous
1457 ** was turned off after the transaction was started. Ticket #615 */
drh968af522003-02-11 14:55:40 +00001458#ifndef NDEBUG
1459 {
drh34e79ce2004-02-08 06:05:46 +00001460 /* Make sure the pPager->nRec counter we are keeping agrees
1461 ** with the nRec computed from the size of the journal file.
1462 */
drh4a0681e2003-02-13 01:58:20 +00001463 off_t hdrSz, pgSz, jSz;
danielk197713adf8a2004-06-03 16:08:41 +00001464 hdrSz = JOURNAL_HDR_SZ(pPager, journal_format);
drh968af522003-02-11 14:55:40 +00001465 pgSz = JOURNAL_PG_SZ(journal_format);
danielk19774adee202004-05-08 08:23:19 +00001466 rc = sqlite3OsFileSize(&pPager->jfd, &jSz);
drh968af522003-02-11 14:55:40 +00001467 if( rc!=0 ) return rc;
drh4a0681e2003-02-13 01:58:20 +00001468 assert( pPager->nRec*pgSz+hdrSz==jSz );
drh968af522003-02-11 14:55:40 +00001469 }
1470#endif
drhd8d66e82003-02-12 02:10:15 +00001471 if( journal_format>=3 ){
drh34e79ce2004-02-08 06:05:46 +00001472 /* Write the nRec value into the journal file header */
drhd8d66e82003-02-12 02:10:15 +00001473 off_t szJ;
1474 if( pPager->fullSync ){
1475 TRACE1("SYNC\n");
danielk19774adee202004-05-08 08:23:19 +00001476 rc = sqlite3OsSync(&pPager->jfd);
drhd8d66e82003-02-12 02:10:15 +00001477 if( rc!=0 ) return rc;
1478 }
danielk19774adee202004-05-08 08:23:19 +00001479 sqlite3OsSeek(&pPager->jfd, sizeof(aJournalMagic1));
drh99ee3602003-02-16 19:13:36 +00001480 rc = write32bits(&pPager->jfd, pPager->nRec);
1481 if( rc ) return rc;
danielk197713adf8a2004-06-03 16:08:41 +00001482
1483 /* Write the name of the master journal file if one is specified */
1484 if( zMaster ){
1485 assert( strlen(zMaster)<pPager->nMaster );
1486 rc = sqlite3OsSeek(&pPager->jfd, sizeof(aJournalMagic3) + 3*4);
1487 if( rc ) return rc;
1488 rc = sqlite3OsWrite(&pPager->jfd, zMaster, strlen(zMaster)+1);
1489 if( rc ) return rc;
1490 }
1491
1492 szJ = JOURNAL_HDR_SZ(pPager, journal_format) +
drhd8d66e82003-02-12 02:10:15 +00001493 pPager->nRec*JOURNAL_PG_SZ(journal_format);
danielk19774adee202004-05-08 08:23:19 +00001494 sqlite3OsSeek(&pPager->jfd, szJ);
drh968af522003-02-11 14:55:40 +00001495 }
drhdb48ee02003-01-16 13:42:43 +00001496 TRACE1("SYNC\n");
danielk19774adee202004-05-08 08:23:19 +00001497 rc = sqlite3OsSync(&pPager->jfd);
drhfa86c412002-02-02 15:01:15 +00001498 if( rc!=0 ) return rc;
drhdb48ee02003-01-16 13:42:43 +00001499 pPager->journalStarted = 1;
drhfa86c412002-02-02 15:01:15 +00001500 }
drh50e5dad2001-09-15 00:57:28 +00001501 pPager->needSync = 0;
drh341eae82003-01-21 02:39:36 +00001502
1503 /* Erase the needSync flag from every page.
1504 */
1505 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1506 pPg->needSync = 0;
1507 }
1508 pPager->pFirstSynced = pPager->pFirst;
drh50e5dad2001-09-15 00:57:28 +00001509 }
drh03eb96a2002-11-10 23:32:56 +00001510
drh341eae82003-01-21 02:39:36 +00001511#ifndef NDEBUG
1512 /* If the Pager.needSync flag is clear then the PgHdr.needSync
1513 ** flag must also be clear for all pages. Verify that this
1514 ** invariant is true.
drh03eb96a2002-11-10 23:32:56 +00001515 */
drh341eae82003-01-21 02:39:36 +00001516 else{
1517 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1518 assert( pPg->needSync==0 );
1519 }
1520 assert( pPager->pFirstSynced==pPager->pFirst );
drh03eb96a2002-11-10 23:32:56 +00001521 }
drh341eae82003-01-21 02:39:36 +00001522#endif
drhdb48ee02003-01-16 13:42:43 +00001523
drh81a20f22001-10-12 17:30:04 +00001524 return rc;
drh50e5dad2001-09-15 00:57:28 +00001525}
1526
1527/*
drh2554f8b2003-01-22 01:26:44 +00001528** Given a list of pages (connected by the PgHdr.pDirty pointer) write
1529** every one of those pages out to the database file and mark them all
1530** as clean.
1531*/
1532static int pager_write_pagelist(PgHdr *pList){
1533 Pager *pPager;
1534 int rc;
1535
1536 if( pList==0 ) return SQLITE_OK;
1537 pPager = pList->pPager;
1538 while( pList ){
1539 assert( pList->dirty );
danielk19774adee202004-05-08 08:23:19 +00001540 sqlite3OsSeek(&pPager->fd, (pList->pgno-1)*(off_t)SQLITE_PAGE_SIZE);
drh9eb9e262004-02-11 02:18:05 +00001541 CODEC(pPager, PGHDR_TO_DATA(pList), pList->pgno, 6);
1542 TRACE2("STORE %d\n", pList->pgno);
danielk19774adee202004-05-08 08:23:19 +00001543 rc = sqlite3OsWrite(&pPager->fd, PGHDR_TO_DATA(pList), SQLITE_PAGE_SIZE);
drh9eb9e262004-02-11 02:18:05 +00001544 CODEC(pPager, PGHDR_TO_DATA(pList), pList->pgno, 0);
drh2554f8b2003-01-22 01:26:44 +00001545 if( rc ) return rc;
1546 pList->dirty = 0;
1547 pList = pList->pDirty;
1548 }
1549 return SQLITE_OK;
1550}
1551
1552/*
1553** Collect every dirty page into a dirty list and
1554** return a pointer to the head of that list. All pages are
1555** collected even if they are still in use.
1556*/
1557static PgHdr *pager_get_all_dirty_pages(Pager *pPager){
1558 PgHdr *p, *pList;
1559 pList = 0;
1560 for(p=pPager->pAll; p; p=p->pNextAll){
1561 if( p->dirty ){
1562 p->pDirty = pList;
1563 pList = p;
1564 }
1565 }
1566 return pList;
1567}
1568
1569/*
drhd9b02572001-04-15 00:37:09 +00001570** Acquire a page.
1571**
drh58a11682001-11-10 13:51:08 +00001572** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00001573** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00001574**
drh306dc212001-05-21 13:45:10 +00001575** A _get works for any page number greater than 0. If the database
1576** file is smaller than the requested page, then no actual disk
1577** read occurs and the memory image of the page is initialized to
1578** all zeros. The extra data appended to a page is always initialized
1579** to zeros the first time a page is loaded into memory.
1580**
drhd9b02572001-04-15 00:37:09 +00001581** The acquisition might fail for several reasons. In all cases,
1582** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00001583**
drh3aac2dd2004-04-26 14:10:20 +00001584** See also sqlite3pager_lookup(). Both this routine and _lookup() attempt
drh7e3b0a02001-04-28 16:52:40 +00001585** to find a page in the in-memory cache first. If the page is not already
drh5e00f6c2001-09-13 13:46:56 +00001586** in memory, this routine goes to disk to read it in whereas _lookup()
drh7e3b0a02001-04-28 16:52:40 +00001587** just returns 0. This routine acquires a read-lock the first time it
1588** has to go to disk, and could also playback an old journal if necessary.
1589** Since _lookup() never goes to disk, it never has to deal with locks
1590** or journal files.
drhed7c8552001-04-11 14:29:21 +00001591*/
drh3aac2dd2004-04-26 14:10:20 +00001592int sqlite3pager_get(Pager *pPager, Pgno pgno, void **ppPage){
drhed7c8552001-04-11 14:29:21 +00001593 PgHdr *pPg;
drh8766c342002-11-09 00:33:15 +00001594 int rc;
drhed7c8552001-04-11 14:29:21 +00001595
drhd9b02572001-04-15 00:37:09 +00001596 /* Make sure we have not hit any critical errors.
1597 */
drh836faa42003-01-11 13:30:57 +00001598 assert( pPager!=0 );
1599 assert( pgno!=0 );
drh2e6d11b2003-04-25 15:37:57 +00001600 *ppPage = 0;
drhd9b02572001-04-15 00:37:09 +00001601 if( pPager->errMask & ~(PAGER_ERR_FULL) ){
1602 return pager_errcode(pPager);
1603 }
1604
danielk197713adf8a2004-06-03 16:08:41 +00001605 /* If this is the first page accessed, then get a SHARED lock
drhed7c8552001-04-11 14:29:21 +00001606 ** on the database file.
1607 */
drhac69b052004-05-12 13:30:07 +00001608 if( pPager->nRef==0 && !pPager->memDb ){
danielk197724162fe2004-06-04 06:22:00 +00001609 int busy = 1;
1610 while( busy ){
1611 rc = sqlite3OsReadLock(&pPager->fd);
1612 if( rc==SQLITE_BUSY &&
1613 pPager->pBusyHandler &&
1614 pPager->pBusyHandler->xFunc &&
1615 pPager->pBusyHandler->xFunc(pPager->pBusyHandler->pArg, "", busy++)
1616 ){
1617 rc = SQLITE_OK;
1618 }else{
1619 busy = 0;
1620 }
1621 if( rc!=SQLITE_OK ){
1622 return rc;
1623 }
drhed7c8552001-04-11 14:29:21 +00001624 }
drhd9b02572001-04-15 00:37:09 +00001625 pPager->state = SQLITE_READLOCK;
drhed7c8552001-04-11 14:29:21 +00001626
danielk197713adf8a2004-06-03 16:08:41 +00001627 /* If a journal file exists, and there is no RESERVED lock on the
1628 ** database file, then it either needs to be played back or deleted.
drhed7c8552001-04-11 14:29:21 +00001629 */
danielk197713adf8a2004-06-03 16:08:41 +00001630 if( pPager->useJournal &&
1631 sqlite3OsFileExists(pPager->zJournal) &&
1632 !sqlite3OsCheckWriteLock(&pPager->fd)
1633 ){
drhe2227f02003-06-14 11:42:57 +00001634 int rc;
drhed7c8552001-04-11 14:29:21 +00001635
danielk197713adf8a2004-06-03 16:08:41 +00001636 /* Get an EXCLUSIVE lock on the database file. */
1637 rc = sqlite3OsLock(&pPager->fd, EXCLUSIVE_LOCK);
drha7fcb052001-12-14 15:09:55 +00001638 if( rc!=SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00001639 if( sqlite3OsUnlock(&pPager->fd)!=SQLITE_OK ){
drh8766c342002-11-09 00:33:15 +00001640 /* This should never happen! */
1641 rc = SQLITE_INTERNAL;
1642 }
drh8766c342002-11-09 00:33:15 +00001643 return rc;
drha7fcb052001-12-14 15:09:55 +00001644 }
1645 pPager->state = SQLITE_WRITELOCK;
1646
drhe2227f02003-06-14 11:42:57 +00001647 /* Open the journal for reading only. Return SQLITE_BUSY if
1648 ** we are unable to open the journal file.
drhf57b3392001-10-08 13:22:32 +00001649 **
drhe2227f02003-06-14 11:42:57 +00001650 ** The journal file does not need to be locked itself. The
1651 ** journal file is never open unless the main database file holds
1652 ** a write lock, so there is never any chance of two or more
1653 ** processes opening the journal at the same time.
drhed7c8552001-04-11 14:29:21 +00001654 */
danielk19774adee202004-05-08 08:23:19 +00001655 rc = sqlite3OsOpenReadOnly(pPager->zJournal, &pPager->jfd);
drha7fcb052001-12-14 15:09:55 +00001656 if( rc!=SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00001657 rc = sqlite3OsUnlock(&pPager->fd);
drha7fcb052001-12-14 15:09:55 +00001658 assert( rc==SQLITE_OK );
drhed7c8552001-04-11 14:29:21 +00001659 return SQLITE_BUSY;
1660 }
drha7fcb052001-12-14 15:09:55 +00001661 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00001662 pPager->journalStarted = 0;
drhed7c8552001-04-11 14:29:21 +00001663
1664 /* Playback and delete the journal. Drop the database write
1665 ** lock and reacquire the read lock.
1666 */
drh99ee3602003-02-16 19:13:36 +00001667 rc = pager_playback(pPager, 0);
drhd9b02572001-04-15 00:37:09 +00001668 if( rc!=SQLITE_OK ){
1669 return rc;
1670 }
drhed7c8552001-04-11 14:29:21 +00001671 }
1672 pPg = 0;
1673 }else{
1674 /* Search for page in cache */
drhd9b02572001-04-15 00:37:09 +00001675 pPg = pager_lookup(pPager, pgno);
drhac69b052004-05-12 13:30:07 +00001676 if( pPager->memDb && pPager->state==SQLITE_UNLOCK ){
1677 pPager->state = SQLITE_READLOCK;
1678 }
drhed7c8552001-04-11 14:29:21 +00001679 }
1680 if( pPg==0 ){
drhd9b02572001-04-15 00:37:09 +00001681 /* The requested page is not in the page cache. */
drhed7c8552001-04-11 14:29:21 +00001682 int h;
drh7e3b0a02001-04-28 16:52:40 +00001683 pPager->nMiss++;
drhac69b052004-05-12 13:30:07 +00001684 if( pPager->nPage<pPager->mxPage || pPager->pFirst==0 || pPager->memDb ){
drhed7c8552001-04-11 14:29:21 +00001685 /* Create a new page */
drhd0ba1932004-02-10 01:54:28 +00001686 pPg = sqliteMallocRaw( sizeof(*pPg) + SQLITE_PAGE_SIZE
drhac69b052004-05-12 13:30:07 +00001687 + sizeof(u32) + pPager->nExtra
1688 + pPager->memDb*sizeof(PgHistory) );
drhd9b02572001-04-15 00:37:09 +00001689 if( pPg==0 ){
drhd9b02572001-04-15 00:37:09 +00001690 pager_unwritelock(pPager);
1691 pPager->errMask |= PAGER_ERR_MEM;
1692 return SQLITE_NOMEM;
1693 }
drh8c1238a2003-01-02 14:43:55 +00001694 memset(pPg, 0, sizeof(*pPg));
drhac69b052004-05-12 13:30:07 +00001695 if( pPager->memDb ){
1696 memset(PGHDR_TO_HIST(pPg, pPager), 0, sizeof(PgHistory));
1697 }
drhed7c8552001-04-11 14:29:21 +00001698 pPg->pPager = pPager;
drhd9b02572001-04-15 00:37:09 +00001699 pPg->pNextAll = pPager->pAll;
drhd79caeb2001-04-15 02:27:24 +00001700 pPager->pAll = pPg;
drhd9b02572001-04-15 00:37:09 +00001701 pPager->nPage++;
drhed7c8552001-04-11 14:29:21 +00001702 }else{
drhdb48ee02003-01-16 13:42:43 +00001703 /* Find a page to recycle. Try to locate a page that does not
1704 ** require us to do an fsync() on the journal.
1705 */
drh341eae82003-01-21 02:39:36 +00001706 pPg = pPager->pFirstSynced;
drhb19a2bc2001-09-16 00:13:26 +00001707
drhdb48ee02003-01-16 13:42:43 +00001708 /* If we could not find a page that does not require an fsync()
1709 ** on the journal file then fsync the journal file. This is a
1710 ** very slow operation, so we work hard to avoid it. But sometimes
1711 ** it can't be helped.
drhb19a2bc2001-09-16 00:13:26 +00001712 */
drh603240c2002-03-05 01:11:12 +00001713 if( pPg==0 ){
danielk197713adf8a2004-06-03 16:08:41 +00001714 int rc = syncJournal(pPager, 0);
drh50e5dad2001-09-15 00:57:28 +00001715 if( rc!=0 ){
drh3aac2dd2004-04-26 14:10:20 +00001716 sqlite3pager_rollback(pPager);
drh50e5dad2001-09-15 00:57:28 +00001717 return SQLITE_IOERR;
1718 }
1719 pPg = pPager->pFirst;
1720 }
drhd9b02572001-04-15 00:37:09 +00001721 assert( pPg->nRef==0 );
drhdb48ee02003-01-16 13:42:43 +00001722
1723 /* Write the page to the database file if it is dirty.
1724 */
1725 if( pPg->dirty ){
1726 assert( pPg->needSync==0 );
drh2554f8b2003-01-22 01:26:44 +00001727 pPg->pDirty = 0;
1728 rc = pager_write_pagelist( pPg );
drhdb48ee02003-01-16 13:42:43 +00001729 if( rc!=SQLITE_OK ){
drh3aac2dd2004-04-26 14:10:20 +00001730 sqlite3pager_rollback(pPager);
drhdb48ee02003-01-16 13:42:43 +00001731 return SQLITE_IOERR;
1732 }
drhdb48ee02003-01-16 13:42:43 +00001733 }
drh50e5dad2001-09-15 00:57:28 +00001734 assert( pPg->dirty==0 );
drhd9b02572001-04-15 00:37:09 +00001735
drhdb48ee02003-01-16 13:42:43 +00001736 /* If the page we are recycling is marked as alwaysRollback, then
drh193a6b42002-07-07 16:52:46 +00001737 ** set the global alwaysRollback flag, thus disabling the
1738 ** sqlite_dont_rollback() optimization for the rest of this transaction.
1739 ** It is necessary to do this because the page marked alwaysRollback
1740 ** might be reloaded at a later time but at that point we won't remember
1741 ** that is was marked alwaysRollback. This means that all pages must
1742 ** be marked as alwaysRollback from here on out.
1743 */
1744 if( pPg->alwaysRollback ){
1745 pPager->alwaysRollback = 1;
1746 }
1747
drhd9b02572001-04-15 00:37:09 +00001748 /* Unlink the old page from the free list and the hash table
1749 */
drhac69b052004-05-12 13:30:07 +00001750 unlinkPage(pPg);
drhd9b02572001-04-15 00:37:09 +00001751 pPager->nOvfl++;
drhed7c8552001-04-11 14:29:21 +00001752 }
1753 pPg->pgno = pgno;
drh1ab43002002-01-14 09:28:19 +00001754 if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){
danielk19774adee202004-05-08 08:23:19 +00001755 sqlite3CheckMemory(pPager->aInJournal, pgno/8);
drhdb48ee02003-01-16 13:42:43 +00001756 assert( pPager->journalOpen );
drh6019e162001-07-02 17:51:45 +00001757 pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0;
drhdb48ee02003-01-16 13:42:43 +00001758 pPg->needSync = 0;
drh6019e162001-07-02 17:51:45 +00001759 }else{
1760 pPg->inJournal = 0;
drhdb48ee02003-01-16 13:42:43 +00001761 pPg->needSync = 0;
drh6019e162001-07-02 17:51:45 +00001762 }
drhac69b052004-05-12 13:30:07 +00001763 if( pPager->aInStmt && (int)pgno<=pPager->stmtSize
1764 && (pPager->aInStmt[pgno/8] & (1<<(pgno&7)))!=0 ){
drh3aac2dd2004-04-26 14:10:20 +00001765 page_add_to_stmt_list(pPg);
drhfa86c412002-02-02 15:01:15 +00001766 }else{
drh3aac2dd2004-04-26 14:10:20 +00001767 page_remove_from_stmt_list(pPg);
drhfa86c412002-02-02 15:01:15 +00001768 }
drhed7c8552001-04-11 14:29:21 +00001769 pPg->dirty = 0;
1770 pPg->nRef = 1;
drhdd793422001-06-28 01:54:48 +00001771 REFINFO(pPg);
drhd9b02572001-04-15 00:37:09 +00001772 pPager->nRef++;
1773 h = pager_hash(pgno);
drhed7c8552001-04-11 14:29:21 +00001774 pPg->pNextHash = pPager->aHash[h];
1775 pPager->aHash[h] = pPg;
1776 if( pPg->pNextHash ){
1777 assert( pPg->pNextHash->pPrevHash==0 );
1778 pPg->pNextHash->pPrevHash = pPg;
1779 }
drh2e6d11b2003-04-25 15:37:57 +00001780 if( pPager->nExtra>0 ){
1781 memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra);
1782 }
drh3aac2dd2004-04-26 14:10:20 +00001783 if( pPager->dbSize<0 ) sqlite3pager_pagecount(pPager);
drh2e6d11b2003-04-25 15:37:57 +00001784 if( pPager->errMask!=0 ){
drh3aac2dd2004-04-26 14:10:20 +00001785 sqlite3pager_unref(PGHDR_TO_DATA(pPg));
drh2e6d11b2003-04-25 15:37:57 +00001786 rc = pager_errcode(pPager);
1787 return rc;
1788 }
drh1ab43002002-01-14 09:28:19 +00001789 if( pPager->dbSize<(int)pgno ){
drhd0ba1932004-02-10 01:54:28 +00001790 memset(PGHDR_TO_DATA(pPg), 0, SQLITE_PAGE_SIZE);
drh306dc212001-05-21 13:45:10 +00001791 }else{
drh81a20f22001-10-12 17:30:04 +00001792 int rc;
drhac69b052004-05-12 13:30:07 +00001793 assert( pPager->memDb==0 );
danielk19774adee202004-05-08 08:23:19 +00001794 sqlite3OsSeek(&pPager->fd, (pgno-1)*(off_t)SQLITE_PAGE_SIZE);
1795 rc = sqlite3OsRead(&pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE);
drh9eb9e262004-02-11 02:18:05 +00001796 TRACE2("FETCH %d\n", pPg->pgno);
1797 CODEC(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3);
drh81a20f22001-10-12 17:30:04 +00001798 if( rc!=SQLITE_OK ){
drh28be87c2002-11-05 23:03:02 +00001799 off_t fileSize;
danielk19774adee202004-05-08 08:23:19 +00001800 if( sqlite3OsFileSize(&pPager->fd,&fileSize)!=SQLITE_OK
drhd0ba1932004-02-10 01:54:28 +00001801 || fileSize>=pgno*SQLITE_PAGE_SIZE ){
drh3aac2dd2004-04-26 14:10:20 +00001802 sqlite3pager_unref(PGHDR_TO_DATA(pPg));
drh4e371ee2002-09-05 16:08:27 +00001803 return rc;
1804 }else{
drhd0ba1932004-02-10 01:54:28 +00001805 memset(PGHDR_TO_DATA(pPg), 0, SQLITE_PAGE_SIZE);
drh4e371ee2002-09-05 16:08:27 +00001806 }
drh81a20f22001-10-12 17:30:04 +00001807 }
drh306dc212001-05-21 13:45:10 +00001808 }
drhed7c8552001-04-11 14:29:21 +00001809 }else{
drhd9b02572001-04-15 00:37:09 +00001810 /* The requested page is in the page cache. */
drh7e3b0a02001-04-28 16:52:40 +00001811 pPager->nHit++;
drhdf0b3b02001-06-23 11:36:20 +00001812 page_ref(pPg);
drhed7c8552001-04-11 14:29:21 +00001813 }
1814 *ppPage = PGHDR_TO_DATA(pPg);
1815 return SQLITE_OK;
1816}
1817
1818/*
drh7e3b0a02001-04-28 16:52:40 +00001819** Acquire a page if it is already in the in-memory cache. Do
1820** not read the page from disk. Return a pointer to the page,
1821** or 0 if the page is not in cache.
1822**
drh3aac2dd2004-04-26 14:10:20 +00001823** See also sqlite3pager_get(). The difference between this routine
1824** and sqlite3pager_get() is that _get() will go to the disk and read
drh7e3b0a02001-04-28 16:52:40 +00001825** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00001826** returns NULL if the page is not in cache or if a disk I/O error
1827** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00001828*/
drh3aac2dd2004-04-26 14:10:20 +00001829void *sqlite3pager_lookup(Pager *pPager, Pgno pgno){
drh7e3b0a02001-04-28 16:52:40 +00001830 PgHdr *pPg;
1831
drh836faa42003-01-11 13:30:57 +00001832 assert( pPager!=0 );
1833 assert( pgno!=0 );
drh7e3b0a02001-04-28 16:52:40 +00001834 if( pPager->errMask & ~(PAGER_ERR_FULL) ){
1835 return 0;
1836 }
drh7e3b0a02001-04-28 16:52:40 +00001837 pPg = pager_lookup(pPager, pgno);
1838 if( pPg==0 ) return 0;
drhdf0b3b02001-06-23 11:36:20 +00001839 page_ref(pPg);
drh7e3b0a02001-04-28 16:52:40 +00001840 return PGHDR_TO_DATA(pPg);
1841}
1842
1843/*
drhed7c8552001-04-11 14:29:21 +00001844** Release a page.
1845**
1846** If the number of references to the page drop to zero, then the
1847** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00001848** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00001849** removed.
1850*/
drh3aac2dd2004-04-26 14:10:20 +00001851int sqlite3pager_unref(void *pData){
drhed7c8552001-04-11 14:29:21 +00001852 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +00001853
1854 /* Decrement the reference count for this page
1855 */
drhed7c8552001-04-11 14:29:21 +00001856 pPg = DATA_TO_PGHDR(pData);
1857 assert( pPg->nRef>0 );
drhed7c8552001-04-11 14:29:21 +00001858 pPg->nRef--;
drhdd793422001-06-28 01:54:48 +00001859 REFINFO(pPg);
drhd9b02572001-04-15 00:37:09 +00001860
drh72f82862001-05-24 21:06:34 +00001861 /* When the number of references to a page reach 0, call the
1862 ** destructor and add the page to the freelist.
drhd9b02572001-04-15 00:37:09 +00001863 */
drhed7c8552001-04-11 14:29:21 +00001864 if( pPg->nRef==0 ){
drh1eaa2692001-09-18 02:02:23 +00001865 Pager *pPager;
1866 pPager = pPg->pPager;
drhd9b02572001-04-15 00:37:09 +00001867 pPg->pNextFree = 0;
1868 pPg->pPrevFree = pPager->pLast;
drhed7c8552001-04-11 14:29:21 +00001869 pPager->pLast = pPg;
drhd9b02572001-04-15 00:37:09 +00001870 if( pPg->pPrevFree ){
1871 pPg->pPrevFree->pNextFree = pPg;
drhed7c8552001-04-11 14:29:21 +00001872 }else{
1873 pPager->pFirst = pPg;
1874 }
drh341eae82003-01-21 02:39:36 +00001875 if( pPg->needSync==0 && pPager->pFirstSynced==0 ){
1876 pPager->pFirstSynced = pPg;
1877 }
drh72f82862001-05-24 21:06:34 +00001878 if( pPager->xDestructor ){
drhb6f41482004-05-14 01:58:11 +00001879 pPager->xDestructor(pData, pPager->pageSize);
drh72f82862001-05-24 21:06:34 +00001880 }
drhd9b02572001-04-15 00:37:09 +00001881
1882 /* When all pages reach the freelist, drop the read lock from
1883 ** the database file.
1884 */
1885 pPager->nRef--;
1886 assert( pPager->nRef>=0 );
drhac69b052004-05-12 13:30:07 +00001887 if( pPager->nRef==0 && !pPager->memDb ){
drhd9b02572001-04-15 00:37:09 +00001888 pager_reset(pPager);
1889 }
drhed7c8552001-04-11 14:29:21 +00001890 }
drhd9b02572001-04-15 00:37:09 +00001891 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00001892}
1893
1894/*
drhda47d772002-12-02 04:25:19 +00001895** Create a journal file for pPager. There should already be a write
1896** lock on the database file when this routine is called.
1897**
1898** Return SQLITE_OK if everything. Return an error code and release the
1899** write lock if anything goes wrong.
1900*/
1901static int pager_open_journal(Pager *pPager){
1902 int rc;
1903 assert( pPager->state==SQLITE_WRITELOCK );
1904 assert( pPager->journalOpen==0 );
1905 assert( pPager->useJournal );
drh3aac2dd2004-04-26 14:10:20 +00001906 sqlite3pager_pagecount(pPager);
drhda47d772002-12-02 04:25:19 +00001907 pPager->aInJournal = sqliteMalloc( pPager->dbSize/8 + 1 );
1908 if( pPager->aInJournal==0 ){
danielk19774adee202004-05-08 08:23:19 +00001909 sqlite3OsReadLock(&pPager->fd);
drhda47d772002-12-02 04:25:19 +00001910 pPager->state = SQLITE_READLOCK;
1911 return SQLITE_NOMEM;
1912 }
danielk19774adee202004-05-08 08:23:19 +00001913 rc = sqlite3OsOpenExclusive(pPager->zJournal, &pPager->jfd,pPager->tempFile);
drhda47d772002-12-02 04:25:19 +00001914 if( rc!=SQLITE_OK ){
1915 sqliteFree(pPager->aInJournal);
1916 pPager->aInJournal = 0;
danielk19774adee202004-05-08 08:23:19 +00001917 sqlite3OsReadLock(&pPager->fd);
drhda47d772002-12-02 04:25:19 +00001918 pPager->state = SQLITE_READLOCK;
1919 return SQLITE_CANTOPEN;
1920 }
danielk19774adee202004-05-08 08:23:19 +00001921 sqlite3OsOpenDirectory(pPager->zDirectory, &pPager->jfd);
drhda47d772002-12-02 04:25:19 +00001922 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00001923 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00001924 pPager->needSync = 0;
1925 pPager->alwaysRollback = 0;
drh968af522003-02-11 14:55:40 +00001926 pPager->nRec = 0;
drh2e6d11b2003-04-25 15:37:57 +00001927 if( pPager->errMask!=0 ){
1928 rc = pager_errcode(pPager);
1929 return rc;
1930 }
drhda47d772002-12-02 04:25:19 +00001931 pPager->origDbSize = pPager->dbSize;
drh968af522003-02-11 14:55:40 +00001932 if( journal_format==JOURNAL_FORMAT_3 ){
danielk197713adf8a2004-06-03 16:08:41 +00001933 /* Create the header for a format 3 journal:
1934 ** - 8 bytes: Magic identifying journal format 3.
1935 ** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
1936 ** - 4 bytes: Magic used for page checksums.
1937 ** - 4 bytes: Number of bytes reserved for master journal ptr (nMaster)
1938 ** - nMaster bytes: Space for a master journal pointer.
1939 ** - 4 bytes: Initial database page count.
1940 */
danielk19774adee202004-05-08 08:23:19 +00001941 rc = sqlite3OsWrite(&pPager->jfd, aJournalMagic3, sizeof(aJournalMagic3));
drh968af522003-02-11 14:55:40 +00001942 if( rc==SQLITE_OK ){
drh4303fee2003-02-15 23:09:17 +00001943 rc = write32bits(&pPager->jfd, pPager->noSync ? 0xffffffff : 0);
drh968af522003-02-11 14:55:40 +00001944 }
1945 if( rc==SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00001946 sqlite3Randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
drh968af522003-02-11 14:55:40 +00001947 rc = write32bits(&pPager->jfd, pPager->cksumInit);
1948 }
danielk197713adf8a2004-06-03 16:08:41 +00001949 if( rc==SQLITE_OK ){
1950 rc = write32bits(&pPager->jfd, pPager->nMaster);
1951 }
1952
1953 /* Unless the size reserved for the master-journal pointer is 0, set
1954 ** the first byte of the master journal pointer to 0x00. Either way,
1955 ** this is interpreted as 'no master journal' in the event of a
1956 ** rollback after a crash.
1957 */
1958 if( rc==SQLITE_OK && pPager->nMaster>0 ){
1959 rc = sqlite3OsWrite(&pPager->jfd, "", 1);
1960 }
1961 if( rc==SQLITE_OK ){
1962 rc = sqlite3OsSeek(&pPager->jfd,
1963 sizeof(aJournalMagic3) + 3*4 + pPager->nMaster);
1964 }
drh968af522003-02-11 14:55:40 +00001965 }else if( journal_format==JOURNAL_FORMAT_2 ){
danielk19774adee202004-05-08 08:23:19 +00001966 rc = sqlite3OsWrite(&pPager->jfd, aJournalMagic2, sizeof(aJournalMagic2));
drhda47d772002-12-02 04:25:19 +00001967 }else{
drh968af522003-02-11 14:55:40 +00001968 assert( journal_format==JOURNAL_FORMAT_1 );
danielk19774adee202004-05-08 08:23:19 +00001969 rc = sqlite3OsWrite(&pPager->jfd, aJournalMagic1, sizeof(aJournalMagic1));
drhda47d772002-12-02 04:25:19 +00001970 }
1971 if( rc==SQLITE_OK ){
1972 rc = write32bits(&pPager->jfd, pPager->dbSize);
1973 }
drhac69b052004-05-12 13:30:07 +00001974 if( pPager->stmtAutoopen && rc==SQLITE_OK ){
drh3aac2dd2004-04-26 14:10:20 +00001975 rc = sqlite3pager_stmt_begin(pPager);
drhda47d772002-12-02 04:25:19 +00001976 }
1977 if( rc!=SQLITE_OK ){
1978 rc = pager_unwritelock(pPager);
1979 if( rc==SQLITE_OK ){
1980 rc = SQLITE_FULL;
1981 }
1982 }
1983 return rc;
1984}
1985
1986/*
drh4b845d72002-03-05 12:41:19 +00001987** Acquire a write-lock on the database. The lock is removed when
1988** the any of the following happen:
1989**
drh3aac2dd2004-04-26 14:10:20 +00001990** * sqlite3pager_commit() is called.
1991** * sqlite3pager_rollback() is called.
1992** * sqlite3pager_close() is called.
1993** * sqlite3pager_unref() is called to on every outstanding page.
drh4b845d72002-03-05 12:41:19 +00001994**
danielk197713adf8a2004-06-03 16:08:41 +00001995** The first parameter to this routine is a pointer to any open page of the
1996** database file. Nothing changes about the page - it is used merely to
1997** acquire a pointer to the Pager structure and as proof that there is
1998** already a read-lock on the database.
drh4b845d72002-03-05 12:41:19 +00001999**
danielk197713adf8a2004-06-03 16:08:41 +00002000** The second parameter indicates how much space in bytes to reserve for a
2001** master journal file-name at the start of the journal when it is created.
2002**
2003** A journal file is opened if this is not a temporary file. For temporary
2004** files, the opening of the journal file is deferred until there is an
2005** actual need to write to the journal.
drhda47d772002-12-02 04:25:19 +00002006**
drh4b845d72002-03-05 12:41:19 +00002007** If the database is already write-locked, this routine is a no-op.
2008*/
danielk197713adf8a2004-06-03 16:08:41 +00002009int sqlite3pager_begin(void *pData, int nMaster){
drh4b845d72002-03-05 12:41:19 +00002010 PgHdr *pPg = DATA_TO_PGHDR(pData);
2011 Pager *pPager = pPg->pPager;
2012 int rc = SQLITE_OK;
2013 assert( pPg->nRef>0 );
danielk197713adf8a2004-06-03 16:08:41 +00002014 assert( nMaster>=0 );
drh4b845d72002-03-05 12:41:19 +00002015 assert( pPager->state!=SQLITE_UNLOCK );
2016 if( pPager->state==SQLITE_READLOCK ){
2017 assert( pPager->aInJournal==0 );
drhac69b052004-05-12 13:30:07 +00002018 if( pPager->memDb ){
2019 pPager->state = SQLITE_WRITELOCK;
2020 pPager->origDbSize = pPager->dbSize;
2021 }else{
danielk197724162fe2004-06-04 06:22:00 +00002022 int busy = 1;
2023 while( busy ){
2024 rc = sqlite3OsWriteLock(&pPager->fd);
2025 if( rc==SQLITE_BUSY &&
2026 pPager->pBusyHandler &&
2027 pPager->pBusyHandler->xFunc &&
2028 pPager->pBusyHandler->xFunc(pPager->pBusyHandler->pArg, "", busy++)
2029 ){
2030 rc = SQLITE_OK;
2031 }else{
2032 busy = 0;
2033 }
2034 if( rc!=SQLITE_OK ){
2035 return rc;
2036 }
drhac69b052004-05-12 13:30:07 +00002037 }
danielk197713adf8a2004-06-03 16:08:41 +00002038 pPager->nMaster = nMaster;
drhac69b052004-05-12 13:30:07 +00002039 pPager->state = SQLITE_WRITELOCK;
2040 pPager->dirtyFile = 0;
2041 TRACE1("TRANSACTION\n");
2042 if( pPager->useJournal && !pPager->tempFile ){
2043 rc = pager_open_journal(pPager);
2044 }
drh4b845d72002-03-05 12:41:19 +00002045 }
2046 }
2047 return rc;
2048}
2049
2050/*
drhed7c8552001-04-11 14:29:21 +00002051** Mark a data page as writeable. The page is written into the journal
2052** if it is not there already. This routine must be called before making
2053** changes to a page.
2054**
2055** The first time this routine is called, the pager creates a new
2056** journal and acquires a write lock on the database. If the write
2057** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00002058** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00002059** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00002060**
2061** If the journal file could not be written because the disk is full,
2062** then this routine returns SQLITE_FULL and does an immediate rollback.
2063** All subsequent write attempts also return SQLITE_FULL until there
drh3aac2dd2004-04-26 14:10:20 +00002064** is a call to sqlite3pager_commit() or sqlite3pager_rollback() to
drhd9b02572001-04-15 00:37:09 +00002065** reset.
drhed7c8552001-04-11 14:29:21 +00002066*/
drh3aac2dd2004-04-26 14:10:20 +00002067int sqlite3pager_write(void *pData){
drh69688d52001-04-14 16:38:23 +00002068 PgHdr *pPg = DATA_TO_PGHDR(pData);
2069 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00002070 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00002071
drh6446c4d2001-12-15 14:22:18 +00002072 /* Check for errors
2073 */
drhd9b02572001-04-15 00:37:09 +00002074 if( pPager->errMask ){
2075 return pager_errcode(pPager);
2076 }
drh5e00f6c2001-09-13 13:46:56 +00002077 if( pPager->readOnly ){
2078 return SQLITE_PERM;
2079 }
drh6446c4d2001-12-15 14:22:18 +00002080
2081 /* Mark the page as dirty. If the page has already been written
2082 ** to the journal then we can return right away.
2083 */
drhd9b02572001-04-15 00:37:09 +00002084 pPg->dirty = 1;
drhac69b052004-05-12 13:30:07 +00002085 if( pPg->inJournal && (pPg->inStmt || pPager->stmtInUse==0) ){
drha1680452002-04-18 01:56:57 +00002086 pPager->dirtyFile = 1;
drhfa86c412002-02-02 15:01:15 +00002087 return SQLITE_OK;
2088 }
drh6446c4d2001-12-15 14:22:18 +00002089
2090 /* If we get this far, it means that the page needs to be
drhfa86c412002-02-02 15:01:15 +00002091 ** written to the transaction journal or the ckeckpoint journal
2092 ** or both.
2093 **
2094 ** First check to see that the transaction journal exists and
2095 ** create it if it does not.
drh6446c4d2001-12-15 14:22:18 +00002096 */
drhd9b02572001-04-15 00:37:09 +00002097 assert( pPager->state!=SQLITE_UNLOCK );
danielk197713adf8a2004-06-03 16:08:41 +00002098 rc = sqlite3pager_begin(pData, 0);
drhda47d772002-12-02 04:25:19 +00002099 if( rc!=SQLITE_OK ){
2100 return rc;
2101 }
drhd9b02572001-04-15 00:37:09 +00002102 assert( pPager->state==SQLITE_WRITELOCK );
drhda47d772002-12-02 04:25:19 +00002103 if( !pPager->journalOpen && pPager->useJournal ){
2104 rc = pager_open_journal(pPager);
2105 if( rc!=SQLITE_OK ) return rc;
2106 }
2107 assert( pPager->journalOpen || !pPager->useJournal );
2108 pPager->dirtyFile = 1;
drh6446c4d2001-12-15 14:22:18 +00002109
drhfa86c412002-02-02 15:01:15 +00002110 /* The transaction journal now exists and we have a write lock on the
2111 ** main database file. Write the current page to the transaction
2112 ** journal if it is not there already.
drh6446c4d2001-12-15 14:22:18 +00002113 */
drhac69b052004-05-12 13:30:07 +00002114 if( !pPg->inJournal && (pPager->useJournal || pPager->memDb) ){
drhdb48ee02003-01-16 13:42:43 +00002115 if( (int)pPg->pgno <= pPager->origDbSize ){
drh968af522003-02-11 14:55:40 +00002116 int szPg;
2117 u32 saved;
drhac69b052004-05-12 13:30:07 +00002118 if( pPager->memDb ){
2119 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
2120 TRACE2("JOURNAL %d\n", pPg->pgno);
2121 assert( pHist->pOrig==0 );
2122 pHist->pOrig = sqliteMallocRaw( pPager->pageSize );
2123 if( pHist->pOrig ){
2124 memcpy(pHist->pOrig, PGHDR_TO_DATA(pPg), pPager->pageSize);
2125 }
2126 pPg->inJournal = 1;
danielk197713adf8a2004-06-03 16:08:41 +00002127 }else{
drhac69b052004-05-12 13:30:07 +00002128 if( journal_format>=JOURNAL_FORMAT_3 ){
2129 u32 cksum = pager_cksum(pPager, pPg->pgno, pData);
2130 saved = *(u32*)PGHDR_TO_EXTRA(pPg);
2131 store32bits(cksum, pPg, SQLITE_PAGE_SIZE);
2132 szPg = SQLITE_PAGE_SIZE+8;
2133 }else{
2134 szPg = SQLITE_PAGE_SIZE+4;
2135 }
2136 store32bits(pPg->pgno, pPg, -4);
2137 CODEC(pPager, pData, pPg->pgno, 7);
2138 rc = sqlite3OsWrite(&pPager->jfd, &((char*)pData)[-4], szPg);
2139 TRACE3("JOURNAL %d %d\n", pPg->pgno, pPg->needSync);
2140 CODEC(pPager, pData, pPg->pgno, 0);
2141 if( journal_format>=JOURNAL_FORMAT_3 ){
2142 *(u32*)PGHDR_TO_EXTRA(pPg) = saved;
2143 }
2144 if( rc!=SQLITE_OK ){
2145 sqlite3pager_rollback(pPager);
2146 pPager->errMask |= PAGER_ERR_FULL;
2147 return rc;
2148 }
2149 pPager->nRec++;
2150 assert( pPager->aInJournal!=0 );
2151 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
2152 pPg->needSync = !pPager->noSync;
2153 pPg->inJournal = 1;
2154 if( pPager->stmtInUse ){
2155 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
2156 page_add_to_stmt_list(pPg);
2157 }
drhdb48ee02003-01-16 13:42:43 +00002158 }
drhdb48ee02003-01-16 13:42:43 +00002159 }else{
2160 pPg->needSync = !pPager->journalStarted && !pPager->noSync;
2161 TRACE3("APPEND %d %d\n", pPg->pgno, pPg->needSync);
drhd9b02572001-04-15 00:37:09 +00002162 }
drhdb48ee02003-01-16 13:42:43 +00002163 if( pPg->needSync ){
2164 pPager->needSync = 1;
drhfa86c412002-02-02 15:01:15 +00002165 }
drh69688d52001-04-14 16:38:23 +00002166 }
drh6446c4d2001-12-15 14:22:18 +00002167
drhac69b052004-05-12 13:30:07 +00002168 /* If the statement journal is open and the page is not in it,
2169 ** then write the current page to the statement journal. Note that
2170 ** the statement journal always uses the simplier format 2 that lacks
2171 ** checksums. The header is also omitted from the statement journal.
drh6446c4d2001-12-15 14:22:18 +00002172 */
drhac69b052004-05-12 13:30:07 +00002173 if( pPager->stmtInUse && !pPg->inStmt && (int)pPg->pgno<=pPager->stmtSize ){
drh1e336b42002-02-14 12:50:33 +00002174 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
drhac69b052004-05-12 13:30:07 +00002175 if( pPager->memDb ){
2176 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
2177 assert( pHist->pStmt==0 );
2178 pHist->pStmt = sqliteMallocRaw( pPager->pageSize );
2179 if( pHist->pStmt ){
2180 memcpy(pHist->pStmt, PGHDR_TO_DATA(pPg), pPager->pageSize);
2181 }
2182 TRACE2("STMT-JOURNAL %d\n", pPg->pgno);
2183 }else{
2184 store32bits(pPg->pgno, pPg, -4);
2185 CODEC(pPager, pData, pPg->pgno, 7);
2186 rc = sqlite3OsWrite(&pPager->stfd, ((char*)pData)-4, SQLITE_PAGE_SIZE+4);
2187 TRACE2("STMT-JOURNAL %d\n", pPg->pgno);
2188 CODEC(pPager, pData, pPg->pgno, 0);
2189 if( rc!=SQLITE_OK ){
2190 sqlite3pager_rollback(pPager);
2191 pPager->errMask |= PAGER_ERR_FULL;
2192 return rc;
2193 }
2194 pPager->stmtNRec++;
2195 assert( pPager->aInStmt!=0 );
2196 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drhfa86c412002-02-02 15:01:15 +00002197 }
drh3aac2dd2004-04-26 14:10:20 +00002198 page_add_to_stmt_list(pPg);
drhfa86c412002-02-02 15:01:15 +00002199 }
2200
2201 /* Update the database size and return.
2202 */
drh1ab43002002-01-14 09:28:19 +00002203 if( pPager->dbSize<(int)pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00002204 pPager->dbSize = pPg->pgno;
2205 }
drh69688d52001-04-14 16:38:23 +00002206 return rc;
drhed7c8552001-04-11 14:29:21 +00002207}
2208
2209/*
drhaacc5432002-01-06 17:07:40 +00002210** Return TRUE if the page given in the argument was previously passed
drh3aac2dd2004-04-26 14:10:20 +00002211** to sqlite3pager_write(). In other words, return TRUE if it is ok
drh6019e162001-07-02 17:51:45 +00002212** to change the content of the page.
2213*/
drh3aac2dd2004-04-26 14:10:20 +00002214int sqlite3pager_iswriteable(void *pData){
drh6019e162001-07-02 17:51:45 +00002215 PgHdr *pPg = DATA_TO_PGHDR(pData);
2216 return pPg->dirty;
2217}
2218
2219/*
drh001bbcb2003-03-19 03:14:00 +00002220** Replace the content of a single page with the information in the third
2221** argument.
2222*/
drh3aac2dd2004-04-26 14:10:20 +00002223int sqlite3pager_overwrite(Pager *pPager, Pgno pgno, void *pData){
drh001bbcb2003-03-19 03:14:00 +00002224 void *pPage;
2225 int rc;
2226
drh3aac2dd2004-04-26 14:10:20 +00002227 rc = sqlite3pager_get(pPager, pgno, &pPage);
drh001bbcb2003-03-19 03:14:00 +00002228 if( rc==SQLITE_OK ){
drh3aac2dd2004-04-26 14:10:20 +00002229 rc = sqlite3pager_write(pPage);
drh001bbcb2003-03-19 03:14:00 +00002230 if( rc==SQLITE_OK ){
drhd0ba1932004-02-10 01:54:28 +00002231 memcpy(pPage, pData, SQLITE_PAGE_SIZE);
drh001bbcb2003-03-19 03:14:00 +00002232 }
drh3aac2dd2004-04-26 14:10:20 +00002233 sqlite3pager_unref(pPage);
drh001bbcb2003-03-19 03:14:00 +00002234 }
2235 return rc;
2236}
2237
2238/*
drh30e58752002-03-02 20:41:57 +00002239** A call to this routine tells the pager that it is not necessary to
2240** write the information on page "pgno" back to the disk, even though
2241** that page might be marked as dirty.
2242**
2243** The overlying software layer calls this routine when all of the data
2244** on the given page is unused. The pager marks the page as clean so
2245** that it does not get written to disk.
2246**
2247** Tests show that this optimization, together with the
drh3aac2dd2004-04-26 14:10:20 +00002248** sqlite3pager_dont_rollback() below, more than double the speed
drh30e58752002-03-02 20:41:57 +00002249** of large INSERT operations and quadruple the speed of large DELETEs.
drh8e298f92002-07-06 16:28:47 +00002250**
2251** When this routine is called, set the alwaysRollback flag to true.
drh3aac2dd2004-04-26 14:10:20 +00002252** Subsequent calls to sqlite3pager_dont_rollback() for the same page
drh8e298f92002-07-06 16:28:47 +00002253** will thereafter be ignored. This is necessary to avoid a problem
2254** where a page with data is added to the freelist during one part of
2255** a transaction then removed from the freelist during a later part
2256** of the same transaction and reused for some other purpose. When it
2257** is first added to the freelist, this routine is called. When reused,
2258** the dont_rollback() routine is called. But because the page contains
2259** critical data, we still need to be sure it gets rolled back in spite
2260** of the dont_rollback() call.
drh30e58752002-03-02 20:41:57 +00002261*/
drh3aac2dd2004-04-26 14:10:20 +00002262void sqlite3pager_dont_write(Pager *pPager, Pgno pgno){
drh30e58752002-03-02 20:41:57 +00002263 PgHdr *pPg;
drh8e298f92002-07-06 16:28:47 +00002264
drh30e58752002-03-02 20:41:57 +00002265 pPg = pager_lookup(pPager, pgno);
drh8e298f92002-07-06 16:28:47 +00002266 pPg->alwaysRollback = 1;
drh30e58752002-03-02 20:41:57 +00002267 if( pPg && pPg->dirty ){
drh8124a302002-06-25 14:43:57 +00002268 if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
2269 /* If this pages is the last page in the file and the file has grown
2270 ** during the current transaction, then do NOT mark the page as clean.
2271 ** When the database file grows, we must make sure that the last page
2272 ** gets written at least once so that the disk file will be the correct
2273 ** size. If you do not write this page and the size of the file
2274 ** on the disk ends up being too small, that can lead to database
2275 ** corruption during the next transaction.
2276 */
2277 }else{
drhdb48ee02003-01-16 13:42:43 +00002278 TRACE2("DONT_WRITE %d\n", pgno);
drh8124a302002-06-25 14:43:57 +00002279 pPg->dirty = 0;
2280 }
drh30e58752002-03-02 20:41:57 +00002281 }
2282}
2283
2284/*
2285** A call to this routine tells the pager that if a rollback occurs,
2286** it is not necessary to restore the data on the given page. This
2287** means that the pager does not have to record the given page in the
2288** rollback journal.
2289*/
drh3aac2dd2004-04-26 14:10:20 +00002290void sqlite3pager_dont_rollback(void *pData){
drh30e58752002-03-02 20:41:57 +00002291 PgHdr *pPg = DATA_TO_PGHDR(pData);
2292 Pager *pPager = pPg->pPager;
2293
2294 if( pPager->state!=SQLITE_WRITELOCK || pPager->journalOpen==0 ) return;
drhac69b052004-05-12 13:30:07 +00002295 if( pPg->alwaysRollback || pPager->alwaysRollback || pPager->memDb ) return;
drh30e58752002-03-02 20:41:57 +00002296 if( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ){
2297 assert( pPager->aInJournal!=0 );
2298 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
2299 pPg->inJournal = 1;
drhac69b052004-05-12 13:30:07 +00002300 if( pPager->stmtInUse ){
2301 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh3aac2dd2004-04-26 14:10:20 +00002302 page_add_to_stmt_list(pPg);
drh30e58752002-03-02 20:41:57 +00002303 }
drhdb48ee02003-01-16 13:42:43 +00002304 TRACE2("DONT_ROLLBACK %d\n", pPg->pgno);
drh30e58752002-03-02 20:41:57 +00002305 }
drhac69b052004-05-12 13:30:07 +00002306 if( pPager->stmtInUse && !pPg->inStmt && (int)pPg->pgno<=pPager->stmtSize ){
drh30e58752002-03-02 20:41:57 +00002307 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
drhac69b052004-05-12 13:30:07 +00002308 assert( pPager->aInStmt!=0 );
2309 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh3aac2dd2004-04-26 14:10:20 +00002310 page_add_to_stmt_list(pPg);
drh30e58752002-03-02 20:41:57 +00002311 }
2312}
2313
drhac69b052004-05-12 13:30:07 +00002314
2315/*
2316** Clear a PgHistory block
2317*/
2318static void clearHistory(PgHistory *pHist){
2319 sqliteFree(pHist->pOrig);
2320 sqliteFree(pHist->pStmt);
2321 pHist->pOrig = 0;
2322 pHist->pStmt = 0;
2323}
2324
drh30e58752002-03-02 20:41:57 +00002325/*
drhed7c8552001-04-11 14:29:21 +00002326** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00002327**
2328** If the commit fails for any reason, a rollback attempt is made
2329** and an error code is returned. If the commit worked, SQLITE_OK
2330** is returned.
drhed7c8552001-04-11 14:29:21 +00002331*/
drh3aac2dd2004-04-26 14:10:20 +00002332int sqlite3pager_commit(Pager *pPager){
drha1b351a2001-09-14 16:42:12 +00002333 int rc;
drhed7c8552001-04-11 14:29:21 +00002334 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +00002335
2336 if( pPager->errMask==PAGER_ERR_FULL ){
drh3aac2dd2004-04-26 14:10:20 +00002337 rc = sqlite3pager_rollback(pPager);
drh4e371ee2002-09-05 16:08:27 +00002338 if( rc==SQLITE_OK ){
2339 rc = SQLITE_FULL;
2340 }
drhd9b02572001-04-15 00:37:09 +00002341 return rc;
2342 }
2343 if( pPager->errMask!=0 ){
2344 rc = pager_errcode(pPager);
2345 return rc;
2346 }
2347 if( pPager->state!=SQLITE_WRITELOCK ){
2348 return SQLITE_ERROR;
2349 }
drhdb48ee02003-01-16 13:42:43 +00002350 TRACE1("COMMIT\n");
drhac69b052004-05-12 13:30:07 +00002351 if( pPager->memDb ){
2352 pPg = pager_get_all_dirty_pages(pPager);
2353 while( pPg ){
2354 clearHistory(PGHDR_TO_HIST(pPg, pPager));
2355 pPg->dirty = 0;
2356 pPg->inJournal = 0;
2357 pPg->inStmt = 0;
2358 pPg->pPrevStmt = pPg->pNextStmt = 0;
2359 pPg = pPg->pDirty;
2360 }
2361 pPager->pStmt = 0;
2362 pPager->state = SQLITE_READLOCK;
2363 return SQLITE_OK;
2364 }
danielk197713adf8a2004-06-03 16:08:41 +00002365#if 0
drha1680452002-04-18 01:56:57 +00002366 if( pPager->dirtyFile==0 ){
danielk19774adee202004-05-08 08:23:19 +00002367 /* Exit early (without doing the time-consuming sqlite3OsSync() calls)
drha1680452002-04-18 01:56:57 +00002368 ** if there have been no changes to the database file. */
drh341eae82003-01-21 02:39:36 +00002369 assert( pPager->needSync==0 );
drha1680452002-04-18 01:56:57 +00002370 rc = pager_unwritelock(pPager);
2371 pPager->dbSize = -1;
2372 return rc;
2373 }
drhda47d772002-12-02 04:25:19 +00002374 assert( pPager->journalOpen );
danielk197713adf8a2004-06-03 16:08:41 +00002375 rc = syncJournal(pPager, 0);
drh240c5792004-02-08 00:40:52 +00002376 if( rc!=SQLITE_OK ){
drhd9b02572001-04-15 00:37:09 +00002377 goto commit_abort;
drhed7c8552001-04-11 14:29:21 +00002378 }
drh2554f8b2003-01-22 01:26:44 +00002379 pPg = pager_get_all_dirty_pages(pPager);
2380 if( pPg ){
2381 rc = pager_write_pagelist(pPg);
danielk19774adee202004-05-08 08:23:19 +00002382 if( rc || (!pPager->noSync && sqlite3OsSync(&pPager->fd)!=SQLITE_OK) ){
drh2554f8b2003-01-22 01:26:44 +00002383 goto commit_abort;
2384 }
drh603240c2002-03-05 01:11:12 +00002385 }
danielk197713adf8a2004-06-03 16:08:41 +00002386#endif
2387 rc = sqlite3pager_sync(pPager, 0);
2388 if( rc!=SQLITE_OK ) goto commit_abort;
2389
drhd9b02572001-04-15 00:37:09 +00002390 rc = pager_unwritelock(pPager);
2391 pPager->dbSize = -1;
2392 return rc;
2393
2394 /* Jump here if anything goes wrong during the commit process.
2395 */
2396commit_abort:
drh3aac2dd2004-04-26 14:10:20 +00002397 rc = sqlite3pager_rollback(pPager);
drhd9b02572001-04-15 00:37:09 +00002398 if( rc==SQLITE_OK ){
2399 rc = SQLITE_FULL;
drhed7c8552001-04-11 14:29:21 +00002400 }
drhed7c8552001-04-11 14:29:21 +00002401 return rc;
2402}
2403
2404/*
2405** Rollback all changes. The database falls back to read-only mode.
2406** All in-memory cache pages revert to their original data contents.
2407** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00002408**
2409** This routine cannot fail unless some other process is not following
2410** the correct locking protocol (SQLITE_PROTOCOL) or unless some other
2411** process is writing trash into the journal file (SQLITE_CORRUPT) or
2412** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
2413** codes are returned for all these occasions. Otherwise,
2414** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00002415*/
drh3aac2dd2004-04-26 14:10:20 +00002416int sqlite3pager_rollback(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00002417 int rc;
drhdb48ee02003-01-16 13:42:43 +00002418 TRACE1("ROLLBACK\n");
drhac69b052004-05-12 13:30:07 +00002419 if( pPager->memDb ){
2420 PgHdr *p;
2421 for(p=pPager->pAll; p; p=p->pNextAll){
2422 PgHistory *pHist;
2423 if( !p->dirty ) continue;
2424 pHist = PGHDR_TO_HIST(p, pPager);
2425 if( pHist->pOrig ){
2426 memcpy(PGHDR_TO_DATA(p), pHist->pOrig, pPager->pageSize);
2427 TRACE2("ROLLBACK-PAGE %d\n", p->pgno);
2428 }else{
2429 TRACE2("PAGE %d is clean\n", p->pgno);
2430 }
2431 clearHistory(pHist);
2432 p->dirty = 0;
2433 p->inJournal = 0;
2434 p->inStmt = 0;
2435 p->pPrevStmt = p->pNextStmt = 0;
2436 }
2437 pPager->pStmt = 0;
2438 pPager->dbSize = pPager->origDbSize;
2439 memoryTruncate(pPager);
2440 pPager->stmtInUse = 0;
2441 pPager->state = SQLITE_READLOCK;
2442 return SQLITE_OK;
2443 }
2444
drhda47d772002-12-02 04:25:19 +00002445 if( !pPager->dirtyFile || !pPager->journalOpen ){
2446 rc = pager_unwritelock(pPager);
2447 pPager->dbSize = -1;
2448 return rc;
2449 }
drhdb48ee02003-01-16 13:42:43 +00002450
drhd9b02572001-04-15 00:37:09 +00002451 if( pPager->errMask!=0 && pPager->errMask!=PAGER_ERR_FULL ){
drh4b845d72002-03-05 12:41:19 +00002452 if( pPager->state>=SQLITE_WRITELOCK ){
drh99ee3602003-02-16 19:13:36 +00002453 pager_playback(pPager, 1);
drh4b845d72002-03-05 12:41:19 +00002454 }
drhd9b02572001-04-15 00:37:09 +00002455 return pager_errcode(pPager);
drhed7c8552001-04-11 14:29:21 +00002456 }
drhd9b02572001-04-15 00:37:09 +00002457 if( pPager->state!=SQLITE_WRITELOCK ){
2458 return SQLITE_OK;
2459 }
drh99ee3602003-02-16 19:13:36 +00002460 rc = pager_playback(pPager, 1);
drhd9b02572001-04-15 00:37:09 +00002461 if( rc!=SQLITE_OK ){
2462 rc = SQLITE_CORRUPT;
2463 pPager->errMask |= PAGER_ERR_CORRUPT;
2464 }
2465 pPager->dbSize = -1;
drhed7c8552001-04-11 14:29:21 +00002466 return rc;
drh98808ba2001-10-18 12:34:46 +00002467}
drhd9b02572001-04-15 00:37:09 +00002468
2469/*
drh5e00f6c2001-09-13 13:46:56 +00002470** Return TRUE if the database file is opened read-only. Return FALSE
2471** if the database is (in theory) writable.
2472*/
drh3aac2dd2004-04-26 14:10:20 +00002473int sqlite3pager_isreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00002474 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00002475}
2476
2477/*
drhd9b02572001-04-15 00:37:09 +00002478** This routine is used for testing and analysis only.
2479*/
drh3aac2dd2004-04-26 14:10:20 +00002480int *sqlite3pager_stats(Pager *pPager){
drhd9b02572001-04-15 00:37:09 +00002481 static int a[9];
2482 a[0] = pPager->nRef;
2483 a[1] = pPager->nPage;
2484 a[2] = pPager->mxPage;
2485 a[3] = pPager->dbSize;
2486 a[4] = pPager->state;
2487 a[5] = pPager->errMask;
2488 a[6] = pPager->nHit;
2489 a[7] = pPager->nMiss;
2490 a[8] = pPager->nOvfl;
2491 return a;
2492}
drhdd793422001-06-28 01:54:48 +00002493
drhfa86c412002-02-02 15:01:15 +00002494/*
drhac69b052004-05-12 13:30:07 +00002495** Set the statement rollback point.
drhfa86c412002-02-02 15:01:15 +00002496**
2497** This routine should be called with the transaction journal already
drhac69b052004-05-12 13:30:07 +00002498** open. A new statement journal is created that can be used to rollback
drhaaab5722002-02-19 13:39:21 +00002499** changes of a single SQL command within a larger transaction.
drhfa86c412002-02-02 15:01:15 +00002500*/
drh3aac2dd2004-04-26 14:10:20 +00002501int sqlite3pager_stmt_begin(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00002502 int rc;
2503 char zTemp[SQLITE_TEMPNAME_SIZE];
drhac69b052004-05-12 13:30:07 +00002504 assert( !pPager->stmtInUse );
2505 TRACE1("STMT-BEGIN\n");
2506 if( pPager->memDb ){
2507 pPager->stmtInUse = 1;
2508 pPager->stmtSize = pPager->dbSize;
2509 return SQLITE_OK;
2510 }
drhda47d772002-12-02 04:25:19 +00002511 if( !pPager->journalOpen ){
drhac69b052004-05-12 13:30:07 +00002512 pPager->stmtAutoopen = 1;
drhda47d772002-12-02 04:25:19 +00002513 return SQLITE_OK;
2514 }
drhfa86c412002-02-02 15:01:15 +00002515 assert( pPager->journalOpen );
drhac69b052004-05-12 13:30:07 +00002516 pPager->aInStmt = sqliteMalloc( pPager->dbSize/8 + 1 );
2517 if( pPager->aInStmt==0 ){
danielk19774adee202004-05-08 08:23:19 +00002518 sqlite3OsReadLock(&pPager->fd);
drhfa86c412002-02-02 15:01:15 +00002519 return SQLITE_NOMEM;
2520 }
drh968af522003-02-11 14:55:40 +00002521#ifndef NDEBUG
drhac69b052004-05-12 13:30:07 +00002522 rc = sqlite3OsFileSize(&pPager->jfd, &pPager->stmtJSize);
2523 if( rc ) goto stmt_begin_failed;
2524 assert( pPager->stmtJSize ==
danielk197713adf8a2004-06-03 16:08:41 +00002525 pPager->nRec*JOURNAL_PG_SZ(journal_format) +
2526 JOURNAL_HDR_SZ(pPager, journal_format) );
drh968af522003-02-11 14:55:40 +00002527#endif
drhac69b052004-05-12 13:30:07 +00002528 pPager->stmtJSize = pPager->nRec*JOURNAL_PG_SZ(journal_format)
danielk197713adf8a2004-06-03 16:08:41 +00002529 + JOURNAL_HDR_SZ(pPager, journal_format);
drhac69b052004-05-12 13:30:07 +00002530 pPager->stmtSize = pPager->dbSize;
2531 if( !pPager->stmtOpen ){
2532 rc = sqlite3pager_opentemp(zTemp, &pPager->stfd);
2533 if( rc ) goto stmt_begin_failed;
2534 pPager->stmtOpen = 1;
2535 pPager->stmtNRec = 0;
drh0f892532002-05-30 12:27:03 +00002536 }
drhac69b052004-05-12 13:30:07 +00002537 pPager->stmtInUse = 1;
drhfa86c412002-02-02 15:01:15 +00002538 return SQLITE_OK;
2539
drhac69b052004-05-12 13:30:07 +00002540stmt_begin_failed:
2541 if( pPager->aInStmt ){
2542 sqliteFree(pPager->aInStmt);
2543 pPager->aInStmt = 0;
drhfa86c412002-02-02 15:01:15 +00002544 }
2545 return rc;
2546}
2547
2548/*
drhac69b052004-05-12 13:30:07 +00002549** Commit a statement.
drhfa86c412002-02-02 15:01:15 +00002550*/
drh3aac2dd2004-04-26 14:10:20 +00002551int sqlite3pager_stmt_commit(Pager *pPager){
drhac69b052004-05-12 13:30:07 +00002552 if( pPager->stmtInUse ){
drh03eb96a2002-11-10 23:32:56 +00002553 PgHdr *pPg, *pNext;
drhac69b052004-05-12 13:30:07 +00002554 TRACE1("STMT-COMMIT\n");
2555 if( !pPager->memDb ){
2556 sqlite3OsSeek(&pPager->stfd, 0);
2557 /* sqlite3OsTruncate(&pPager->stfd, 0); */
2558 sqliteFree( pPager->aInStmt );
2559 pPager->aInStmt = 0;
drh663fc632002-02-02 18:49:19 +00002560 }
drhac69b052004-05-12 13:30:07 +00002561 for(pPg=pPager->pStmt; pPg; pPg=pNext){
2562 pNext = pPg->pNextStmt;
2563 assert( pPg->inStmt );
2564 pPg->inStmt = 0;
2565 pPg->pPrevStmt = pPg->pNextStmt = 0;
2566 if( pPager->memDb ){
2567 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
2568 sqliteFree(pHist->pStmt);
2569 pHist->pStmt = 0;
2570 }
2571 }
2572 pPager->stmtNRec = 0;
2573 pPager->stmtInUse = 0;
2574 pPager->pStmt = 0;
drh663fc632002-02-02 18:49:19 +00002575 }
drhac69b052004-05-12 13:30:07 +00002576 pPager->stmtAutoopen = 0;
drhfa86c412002-02-02 15:01:15 +00002577 return SQLITE_OK;
2578}
2579
2580/*
drhac69b052004-05-12 13:30:07 +00002581** Rollback a statement.
drhfa86c412002-02-02 15:01:15 +00002582*/
drh3aac2dd2004-04-26 14:10:20 +00002583int sqlite3pager_stmt_rollback(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00002584 int rc;
drhac69b052004-05-12 13:30:07 +00002585 if( pPager->stmtInUse ){
2586 TRACE1("STMT-ROLLBACK\n");
2587 if( pPager->memDb ){
2588 PgHdr *pPg;
2589 for(pPg=pPager->pStmt; pPg; pPg=pPg->pNextStmt){
2590 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
2591 if( pHist->pStmt ){
2592 memcpy(PGHDR_TO_DATA(pPg), pHist->pStmt, pPager->pageSize);
2593 sqliteFree(pHist->pStmt);
2594 pHist->pStmt = 0;
2595 }
2596 }
2597 pPager->dbSize = pPager->stmtSize;
2598 memoryTruncate(pPager);
2599 rc = SQLITE_OK;
2600 }else{
2601 rc = pager_stmt_playback(pPager);
2602 }
drh3aac2dd2004-04-26 14:10:20 +00002603 sqlite3pager_stmt_commit(pPager);
drh663fc632002-02-02 18:49:19 +00002604 }else{
2605 rc = SQLITE_OK;
2606 }
drhac69b052004-05-12 13:30:07 +00002607 pPager->stmtAutoopen = 0;
drhfa86c412002-02-02 15:01:15 +00002608 return rc;
2609}
2610
drh73509ee2003-04-06 20:44:45 +00002611/*
2612** Return the full pathname of the database file.
2613*/
drh3aac2dd2004-04-26 14:10:20 +00002614const char *sqlite3pager_filename(Pager *pPager){
drh73509ee2003-04-06 20:44:45 +00002615 return pPager->zFilename;
2616}
2617
drhb20ea9d2004-02-09 01:20:36 +00002618/*
2619** Set the codec for this pager
2620*/
drh3aac2dd2004-04-26 14:10:20 +00002621void sqlite3pager_set_codec(
drhb20ea9d2004-02-09 01:20:36 +00002622 Pager *pPager,
drh9eb9e262004-02-11 02:18:05 +00002623 void (*xCodec)(void*,void*,Pgno,int),
drhb20ea9d2004-02-09 01:20:36 +00002624 void *pCodecArg
2625){
2626 pPager->xCodec = xCodec;
2627 pPager->pCodecArg = pCodecArg;
2628}
2629
danielk197713adf8a2004-06-03 16:08:41 +00002630/*
2631** Sync the database file for the pager pPager. zMaster points to the name
2632** of a master journal file that should be written into the individual
2633** journal file. zMaster may be NULL, which is interpreted as no master
2634** journal (a single database transaction).
2635**
2636** This routine ensures that the journal is synced, all dirty pages written
2637** to the database file and the database file synced. The only thing that
2638** remains to commit the transaction is to delete the journal file (or
2639** master journal file if specified).
2640**
2641** Note that if zMaster==NULL, this does not overwrite a previous value
2642** passed to an sqlite3pager_sync() call.
2643*/
2644int sqlite3pager_sync(Pager *pPager, const char *zMaster){
2645 int rc = SQLITE_OK;
2646
2647 /* If this is an in-memory db, or no pages have been written to, this
2648 ** function is a no-op.
2649 */
2650 if( !pPager->memDb && pPager->dirtyFile ){
2651 PgHdr *pPg;
2652 assert( pPager->journalOpen );
2653
2654 /* Sync the journal file */
2655 rc = syncJournal(pPager, zMaster);
2656 if( rc!=SQLITE_OK ) goto sync_exit;
2657
2658 /* Write all dirty pages to the database file */
2659 pPg = pager_get_all_dirty_pages(pPager);
2660 rc = pager_write_pagelist(pPg);
2661 if( rc!=SQLITE_OK ) goto sync_exit;
2662
2663 /* If any pages were actually written, sync the database file */
2664 if( pPg && !pPager->noSync ){
2665 rc = sqlite3OsSync(&pPager->fd);
2666 }
2667 }
2668
2669sync_exit:
2670 return rc;
2671}
2672
drh74587e52002-08-13 00:01:16 +00002673#ifdef SQLITE_TEST
drhdd793422001-06-28 01:54:48 +00002674/*
2675** Print a listing of all referenced pages and their ref count.
2676*/
drh3aac2dd2004-04-26 14:10:20 +00002677void sqlite3pager_refdump(Pager *pPager){
drhdd793422001-06-28 01:54:48 +00002678 PgHdr *pPg;
2679 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2680 if( pPg->nRef<=0 ) continue;
2681 printf("PAGE %3d addr=0x%08x nRef=%d\n",
2682 pPg->pgno, (int)PGHDR_TO_DATA(pPg), pPg->nRef);
2683 }
2684}
2685#endif