blob: 29db8640d28d01157b1ea78a7511accf8353d3c2 [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**
drhac69b052004-05-12 13:30:07 +000021** @(#) $Id: pager.c,v 1.107 2004/05/12 13:30:08 drh Exp $
drhed7c8552001-04-11 14:29:21 +000022*/
drh829e8022002-11-06 14:08:11 +000023#include "os.h" /* Must be first to enable large file support */
drhd9b02572001-04-15 00:37:09 +000024#include "sqliteInt.h"
drhed7c8552001-04-11 14:29:21 +000025#include "pager.h"
drhed7c8552001-04-11 14:29:21 +000026#include <assert.h>
drhd9b02572001-04-15 00:37:09 +000027#include <string.h>
drhed7c8552001-04-11 14:29:21 +000028
29/*
drhdb48ee02003-01-16 13:42:43 +000030** Macros for troubleshooting. Normally turned off
31*/
32#if 0
33static Pager *mainPager = 0;
34#define SET_PAGER(X) if( mainPager==0 ) mainPager = (X)
35#define CLR_PAGER(X) if( mainPager==(X) ) mainPager = 0
36#define TRACE1(X) if( pPager==mainPager ) fprintf(stderr,X)
37#define TRACE2(X,Y) if( pPager==mainPager ) fprintf(stderr,X,Y)
38#define TRACE3(X,Y,Z) if( pPager==mainPager ) fprintf(stderr,X,Y,Z)
39#else
40#define SET_PAGER(X)
41#define CLR_PAGER(X)
42#define TRACE1(X)
43#define TRACE2(X,Y)
44#define TRACE3(X,Y,Z)
45#endif
46
47
48/*
drhed7c8552001-04-11 14:29:21 +000049** The page cache as a whole is always in one of the following
50** states:
51**
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 */
drh72f82862001-05-24 21:06:34 +0000184 void (*xDestructor)(void*); /* Call this routine when freeing pages */
drhed7c8552001-04-11 14:29:21 +0000185 int nPage; /* Total number of in-memory pages */
drhd9b02572001-04-15 00:37:09 +0000186 int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */
drhed7c8552001-04-11 14:29:21 +0000187 int mxPage; /* Maximum number of pages to hold in cache */
drhd9b02572001-04-15 00:37:09 +0000188 int nHit, nMiss, nOvfl; /* Cache hits, missing, and LRU overflows */
drh9eb9e262004-02-11 02:18:05 +0000189 void (*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
drhb20ea9d2004-02-09 01:20:36 +0000190 void *pCodecArg; /* First argument to xCodec() */
drhac69b052004-05-12 13:30:07 +0000191 int pageSize; /* Page size in bytes */
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 */
drhd9b02572001-04-15 00:37:09 +0000215};
216
217/*
218** These are bits that can be set in Pager.errMask.
219*/
220#define PAGER_ERR_FULL 0x01 /* a write() failed */
221#define PAGER_ERR_MEM 0x02 /* malloc() failed */
222#define PAGER_ERR_LOCK 0x04 /* error in the locking protocol */
223#define PAGER_ERR_CORRUPT 0x08 /* database or journal corruption */
drh81a20f22001-10-12 17:30:04 +0000224#define PAGER_ERR_DISK 0x10 /* general disk I/O error - bad hard drive? */
drhd9b02572001-04-15 00:37:09 +0000225
226/*
227** The journal file contains page records in the following
228** format.
drh968af522003-02-11 14:55:40 +0000229**
230** Actually, this structure is the complete page record for pager
231** formats less than 3. Beginning with format 3, this record is surrounded
232** by two checksums.
drhd9b02572001-04-15 00:37:09 +0000233*/
234typedef struct PageRecord PageRecord;
235struct PageRecord {
drhb20ea9d2004-02-09 01:20:36 +0000236 Pgno pgno; /* The page number */
drhd0ba1932004-02-10 01:54:28 +0000237 char aData[SQLITE_PAGE_SIZE]; /* Original data for page pgno */
drhd9b02572001-04-15 00:37:09 +0000238};
239
240/*
drh5e00f6c2001-09-13 13:46:56 +0000241** Journal files begin with the following magic string. The data
242** was obtained from /dev/random. It is used only as a sanity check.
drh94f33312002-08-12 12:29:56 +0000243**
drh968af522003-02-11 14:55:40 +0000244** There are three journal formats (so far). The 1st journal format writes
245** 32-bit integers in the byte-order of the host machine. New
246** formats writes integers as big-endian. All new journals use the
drh94f33312002-08-12 12:29:56 +0000247** new format, but we have to be able to read an older journal in order
drh968af522003-02-11 14:55:40 +0000248** to rollback journals created by older versions of the library.
249**
250** The 3rd journal format (added for 2.8.0) adds additional sanity
251** checking information to the journal. If the power fails while the
252** journal is being written, semi-random garbage data might appear in
253** the journal file after power is restored. If an attempt is then made
254** to roll the journal back, the database could be corrupted. The additional
255** sanity checking data is an attempt to discover the garbage in the
256** journal and ignore it.
257**
258** The sanity checking information for the 3rd journal format consists
259** of a 32-bit checksum on each page of data. The checksum covers both
drhd0ba1932004-02-10 01:54:28 +0000260** the page number and the SQLITE_PAGE_SIZE bytes of data for the page.
drh968af522003-02-11 14:55:40 +0000261** This cksum is initialized to a 32-bit random value that appears in the
262** journal file right after the header. The random initializer is important,
263** because garbage data that appears at the end of a journal is likely
264** data that was once in other files that have now been deleted. If the
265** garbage data came from an obsolete journal file, the checksums might
266** be correct. But by initializing the checksum to random value which
267** is different for every journal, we minimize that risk.
drhd9b02572001-04-15 00:37:09 +0000268*/
drh968af522003-02-11 14:55:40 +0000269static const unsigned char aJournalMagic1[] = {
drhd9b02572001-04-15 00:37:09 +0000270 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd4,
drhed7c8552001-04-11 14:29:21 +0000271};
drh968af522003-02-11 14:55:40 +0000272static const unsigned char aJournalMagic2[] = {
drh94f33312002-08-12 12:29:56 +0000273 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd5,
274};
drh968af522003-02-11 14:55:40 +0000275static const unsigned char aJournalMagic3[] = {
276 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd6,
277};
278#define JOURNAL_FORMAT_1 1
279#define JOURNAL_FORMAT_2 2
280#define JOURNAL_FORMAT_3 3
drh94f33312002-08-12 12:29:56 +0000281
282/*
drh968af522003-02-11 14:55:40 +0000283** The following integer determines what format to use when creating
284** new primary journal files. By default we always use format 3.
285** When testing, we can set this value to older journal formats in order to
286** make sure that newer versions of the library are able to rollback older
287** journal files.
288**
drhac69b052004-05-12 13:30:07 +0000289** Note that statement journals always use format 2 and omit the header.
drh94f33312002-08-12 12:29:56 +0000290*/
291#ifdef SQLITE_TEST
drh968af522003-02-11 14:55:40 +0000292int journal_format = 3;
drh74587e52002-08-13 00:01:16 +0000293#else
drh968af522003-02-11 14:55:40 +0000294# define journal_format 3
drh94f33312002-08-12 12:29:56 +0000295#endif
drhed7c8552001-04-11 14:29:21 +0000296
297/*
drh968af522003-02-11 14:55:40 +0000298** The size of the header and of each page in the journal varies according
299** to which journal format is being used. The following macros figure out
300** the sizes based on format numbers.
301*/
302#define JOURNAL_HDR_SZ(X) \
303 (sizeof(aJournalMagic1) + sizeof(Pgno) + ((X)>=3)*2*sizeof(u32))
304#define JOURNAL_PG_SZ(X) \
drhd0ba1932004-02-10 01:54:28 +0000305 (SQLITE_PAGE_SIZE + sizeof(Pgno) + ((X)>=3)*sizeof(u32))
drh968af522003-02-11 14:55:40 +0000306
307/*
drhdd793422001-06-28 01:54:48 +0000308** Enable reference count tracking here:
309*/
drh74587e52002-08-13 00:01:16 +0000310#ifdef SQLITE_TEST
drh3aac2dd2004-04-26 14:10:20 +0000311 int pager3_refinfo_enable = 0;
drhdd793422001-06-28 01:54:48 +0000312 static void pager_refinfo(PgHdr *p){
313 static int cnt = 0;
drh3aac2dd2004-04-26 14:10:20 +0000314 if( !pager3_refinfo_enable ) return;
drhdd793422001-06-28 01:54:48 +0000315 printf(
316 "REFCNT: %4d addr=0x%08x nRef=%d\n",
317 p->pgno, (int)PGHDR_TO_DATA(p), p->nRef
318 );
319 cnt++; /* Something to set a breakpoint on */
320 }
321# define REFINFO(X) pager_refinfo(X)
322#else
323# define REFINFO(X)
324#endif
325
326/*
drh34e79ce2004-02-08 06:05:46 +0000327** Read a 32-bit integer from the given file descriptor. Store the integer
328** that is read in *pRes. Return SQLITE_OK if everything worked, or an
329** error code is something goes wrong.
330**
331** If the journal format is 2 or 3, read a big-endian integer. If the
332** journal format is 1, read an integer in the native byte-order of the
333** host machine.
drh94f33312002-08-12 12:29:56 +0000334*/
drh968af522003-02-11 14:55:40 +0000335static int read32bits(int format, OsFile *fd, u32 *pRes){
drh94f33312002-08-12 12:29:56 +0000336 u32 res;
337 int rc;
danielk19774adee202004-05-08 08:23:19 +0000338 rc = sqlite3OsRead(fd, &res, sizeof(res));
drh968af522003-02-11 14:55:40 +0000339 if( rc==SQLITE_OK && format>JOURNAL_FORMAT_1 ){
drh94f33312002-08-12 12:29:56 +0000340 unsigned char ac[4];
341 memcpy(ac, &res, 4);
342 res = (ac[0]<<24) | (ac[1]<<16) | (ac[2]<<8) | ac[3];
343 }
344 *pRes = res;
345 return rc;
346}
347
348/*
drh34e79ce2004-02-08 06:05:46 +0000349** Write a 32-bit integer into the given file descriptor. Return SQLITE_OK
350** on success or an error code is something goes wrong.
351**
352** If the journal format is 2 or 3, write the integer as 4 big-endian
353** bytes. If the journal format is 1, write the integer in the native
354** byte order. In normal operation, only formats 2 and 3 are used.
355** Journal format 1 is only used for testing.
drh94f33312002-08-12 12:29:56 +0000356*/
357static int write32bits(OsFile *fd, u32 val){
358 unsigned char ac[4];
drh968af522003-02-11 14:55:40 +0000359 if( journal_format<=1 ){
danielk19774adee202004-05-08 08:23:19 +0000360 return sqlite3OsWrite(fd, &val, 4);
drh94f33312002-08-12 12:29:56 +0000361 }
drh94f33312002-08-12 12:29:56 +0000362 ac[0] = (val>>24) & 0xff;
363 ac[1] = (val>>16) & 0xff;
364 ac[2] = (val>>8) & 0xff;
365 ac[3] = val & 0xff;
danielk19774adee202004-05-08 08:23:19 +0000366 return sqlite3OsWrite(fd, ac, 4);
drh94f33312002-08-12 12:29:56 +0000367}
368
drh2554f8b2003-01-22 01:26:44 +0000369/*
370** Write a 32-bit integer into a page header right before the
371** page data. This will overwrite the PgHdr.pDirty pointer.
drh34e79ce2004-02-08 06:05:46 +0000372**
373** The integer is big-endian for formats 2 and 3 and native byte order
374** for journal format 1.
drh2554f8b2003-01-22 01:26:44 +0000375*/
drh968af522003-02-11 14:55:40 +0000376static void store32bits(u32 val, PgHdr *p, int offset){
drh2554f8b2003-01-22 01:26:44 +0000377 unsigned char *ac;
drhec1bd0b2003-08-26 11:41:27 +0000378 ac = &((unsigned char*)PGHDR_TO_DATA(p))[offset];
drh968af522003-02-11 14:55:40 +0000379 if( journal_format<=1 ){
drh2554f8b2003-01-22 01:26:44 +0000380 memcpy(ac, &val, 4);
381 }else{
382 ac[0] = (val>>24) & 0xff;
383 ac[1] = (val>>16) & 0xff;
384 ac[2] = (val>>8) & 0xff;
385 ac[3] = val & 0xff;
386 }
387}
388
drh94f33312002-08-12 12:29:56 +0000389
390/*
drhd9b02572001-04-15 00:37:09 +0000391** Convert the bits in the pPager->errMask into an approprate
392** return code.
393*/
394static int pager_errcode(Pager *pPager){
395 int rc = SQLITE_OK;
396 if( pPager->errMask & PAGER_ERR_LOCK ) rc = SQLITE_PROTOCOL;
drh81a20f22001-10-12 17:30:04 +0000397 if( pPager->errMask & PAGER_ERR_DISK ) rc = SQLITE_IOERR;
drhd9b02572001-04-15 00:37:09 +0000398 if( pPager->errMask & PAGER_ERR_FULL ) rc = SQLITE_FULL;
399 if( pPager->errMask & PAGER_ERR_MEM ) rc = SQLITE_NOMEM;
400 if( pPager->errMask & PAGER_ERR_CORRUPT ) rc = SQLITE_CORRUPT;
401 return rc;
drhed7c8552001-04-11 14:29:21 +0000402}
403
404/*
drh03eb96a2002-11-10 23:32:56 +0000405** Add or remove a page from the list of all pages that are in the
drhac69b052004-05-12 13:30:07 +0000406** statement journal.
drh03eb96a2002-11-10 23:32:56 +0000407**
408** The Pager keeps a separate list of pages that are currently in
drhac69b052004-05-12 13:30:07 +0000409** the statement journal. This helps the sqlite3pager_stmt_commit()
drh03eb96a2002-11-10 23:32:56 +0000410** routine run MUCH faster for the common case where there are many
drhac69b052004-05-12 13:30:07 +0000411** pages in memory but only a few are in the statement journal.
drh03eb96a2002-11-10 23:32:56 +0000412*/
drh3aac2dd2004-04-26 14:10:20 +0000413static void page_add_to_stmt_list(PgHdr *pPg){
drh03eb96a2002-11-10 23:32:56 +0000414 Pager *pPager = pPg->pPager;
drhac69b052004-05-12 13:30:07 +0000415 if( pPg->inStmt ) return;
416 assert( pPg->pPrevStmt==0 && pPg->pNextStmt==0 );
417 pPg->pPrevStmt = 0;
418 if( pPager->pStmt ){
419 pPager->pStmt->pPrevStmt = pPg;
drh03eb96a2002-11-10 23:32:56 +0000420 }
drhac69b052004-05-12 13:30:07 +0000421 pPg->pNextStmt = pPager->pStmt;
422 pPager->pStmt = pPg;
423 pPg->inStmt = 1;
drh03eb96a2002-11-10 23:32:56 +0000424}
drh3aac2dd2004-04-26 14:10:20 +0000425static void page_remove_from_stmt_list(PgHdr *pPg){
drhac69b052004-05-12 13:30:07 +0000426 if( !pPg->inStmt ) return;
427 if( pPg->pPrevStmt ){
428 assert( pPg->pPrevStmt->pNextStmt==pPg );
429 pPg->pPrevStmt->pNextStmt = pPg->pNextStmt;
drh03eb96a2002-11-10 23:32:56 +0000430 }else{
drhac69b052004-05-12 13:30:07 +0000431 assert( pPg->pPager->pStmt==pPg );
432 pPg->pPager->pStmt = pPg->pNextStmt;
drh03eb96a2002-11-10 23:32:56 +0000433 }
drhac69b052004-05-12 13:30:07 +0000434 if( pPg->pNextStmt ){
435 assert( pPg->pNextStmt->pPrevStmt==pPg );
436 pPg->pNextStmt->pPrevStmt = pPg->pPrevStmt;
drh03eb96a2002-11-10 23:32:56 +0000437 }
drhac69b052004-05-12 13:30:07 +0000438 pPg->pNextStmt = 0;
439 pPg->pPrevStmt = 0;
440 pPg->inStmt = 0;
drh03eb96a2002-11-10 23:32:56 +0000441}
442
443/*
drhed7c8552001-04-11 14:29:21 +0000444** Find a page in the hash table given its page number. Return
445** a pointer to the page or NULL if not found.
446*/
drhd9b02572001-04-15 00:37:09 +0000447static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drh836faa42003-01-11 13:30:57 +0000448 PgHdr *p = pPager->aHash[pager_hash(pgno)];
drhed7c8552001-04-11 14:29:21 +0000449 while( p && p->pgno!=pgno ){
450 p = p->pNextHash;
451 }
452 return p;
453}
454
455/*
456** Unlock the database and clear the in-memory cache. This routine
457** sets the state of the pager back to what it was when it was first
458** opened. Any outstanding pages are invalidated and subsequent attempts
459** to access those pages will likely result in a coredump.
460*/
drhd9b02572001-04-15 00:37:09 +0000461static void pager_reset(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +0000462 PgHdr *pPg, *pNext;
drhd9b02572001-04-15 00:37:09 +0000463 for(pPg=pPager->pAll; pPg; pPg=pNext){
464 pNext = pPg->pNextAll;
465 sqliteFree(pPg);
drhed7c8552001-04-11 14:29:21 +0000466 }
467 pPager->pFirst = 0;
drh341eae82003-01-21 02:39:36 +0000468 pPager->pFirstSynced = 0;
drhd9b02572001-04-15 00:37:09 +0000469 pPager->pLast = 0;
470 pPager->pAll = 0;
drhed7c8552001-04-11 14:29:21 +0000471 memset(pPager->aHash, 0, sizeof(pPager->aHash));
472 pPager->nPage = 0;
drhfa86c412002-02-02 15:01:15 +0000473 if( pPager->state>=SQLITE_WRITELOCK ){
drh3aac2dd2004-04-26 14:10:20 +0000474 sqlite3pager_rollback(pPager);
drhed7c8552001-04-11 14:29:21 +0000475 }
danielk19774adee202004-05-08 08:23:19 +0000476 sqlite3OsUnlock(&pPager->fd);
drhed7c8552001-04-11 14:29:21 +0000477 pPager->state = SQLITE_UNLOCK;
drhd9b02572001-04-15 00:37:09 +0000478 pPager->dbSize = -1;
drhed7c8552001-04-11 14:29:21 +0000479 pPager->nRef = 0;
drh8cfbf082001-09-19 13:22:39 +0000480 assert( pPager->journalOpen==0 );
drhed7c8552001-04-11 14:29:21 +0000481}
482
483/*
484** When this routine is called, the pager has the journal file open and
485** a write lock on the database. This routine releases the database
486** write lock and acquires a read lock in its place. The journal file
487** is deleted and closed.
drh50457892003-09-06 01:10:47 +0000488**
489** TODO: Consider keeping the journal file open for temporary databases.
490** This might give a performance improvement on windows where opening
491** a file is an expensive operation.
drhed7c8552001-04-11 14:29:21 +0000492*/
drhd9b02572001-04-15 00:37:09 +0000493static int pager_unwritelock(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +0000494 int rc;
drhd9b02572001-04-15 00:37:09 +0000495 PgHdr *pPg;
drhfa86c412002-02-02 15:01:15 +0000496 if( pPager->state<SQLITE_WRITELOCK ) return SQLITE_OK;
drh3aac2dd2004-04-26 14:10:20 +0000497 sqlite3pager_stmt_commit(pPager);
drhac69b052004-05-12 13:30:07 +0000498 if( pPager->stmtOpen ){
499 sqlite3OsClose(&pPager->stfd);
500 pPager->stmtOpen = 0;
drh0f892532002-05-30 12:27:03 +0000501 }
drhda47d772002-12-02 04:25:19 +0000502 if( pPager->journalOpen ){
danielk19774adee202004-05-08 08:23:19 +0000503 sqlite3OsClose(&pPager->jfd);
drhda47d772002-12-02 04:25:19 +0000504 pPager->journalOpen = 0;
danielk19774adee202004-05-08 08:23:19 +0000505 sqlite3OsDelete(pPager->zJournal);
drhda47d772002-12-02 04:25:19 +0000506 sqliteFree( pPager->aInJournal );
507 pPager->aInJournal = 0;
508 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
509 pPg->inJournal = 0;
510 pPg->dirty = 0;
drhdb48ee02003-01-16 13:42:43 +0000511 pPg->needSync = 0;
drhda47d772002-12-02 04:25:19 +0000512 }
513 }else{
514 assert( pPager->dirtyFile==0 || pPager->useJournal==0 );
drhd9b02572001-04-15 00:37:09 +0000515 }
danielk19774adee202004-05-08 08:23:19 +0000516 rc = sqlite3OsReadLock(&pPager->fd);
drh8e298f92002-07-06 16:28:47 +0000517 if( rc==SQLITE_OK ){
518 pPager->state = SQLITE_READLOCK;
519 }else{
520 /* This can only happen if a process does a BEGIN, then forks and the
521 ** child process does the COMMIT. Because of the semantics of unix
522 ** file locking, the unlock will fail.
523 */
524 pPager->state = SQLITE_UNLOCK;
525 }
drhed7c8552001-04-11 14:29:21 +0000526 return rc;
527}
528
drhed7c8552001-04-11 14:29:21 +0000529/*
drh968af522003-02-11 14:55:40 +0000530** Compute and return a checksum for the page of data.
drh34e79ce2004-02-08 06:05:46 +0000531**
532** This is not a real checksum. It is really just the sum of the
533** random initial value and the page number. We considered do a checksum
534** of the database, but that was found to be too slow.
drh968af522003-02-11 14:55:40 +0000535*/
536static u32 pager_cksum(Pager *pPager, Pgno pgno, const char *aData){
537 u32 cksum = pPager->cksumInit + pgno;
drh968af522003-02-11 14:55:40 +0000538 return cksum;
539}
540
541/*
drhfa86c412002-02-02 15:01:15 +0000542** Read a single page from the journal file opened on file descriptor
543** jfd. Playback this one page.
drh968af522003-02-11 14:55:40 +0000544**
545** There are three different journal formats. The format parameter determines
546** which format is used by the journal that is played back.
drhfa86c412002-02-02 15:01:15 +0000547*/
drh968af522003-02-11 14:55:40 +0000548static int pager_playback_one_page(Pager *pPager, OsFile *jfd, int format){
drhfa86c412002-02-02 15:01:15 +0000549 int rc;
550 PgHdr *pPg; /* An existing page in the cache */
551 PageRecord pgRec;
drh968af522003-02-11 14:55:40 +0000552 u32 cksum;
drhfa86c412002-02-02 15:01:15 +0000553
drh968af522003-02-11 14:55:40 +0000554 rc = read32bits(format, jfd, &pgRec.pgno);
drh99ee3602003-02-16 19:13:36 +0000555 if( rc!=SQLITE_OK ) return rc;
danielk19774adee202004-05-08 08:23:19 +0000556 rc = sqlite3OsRead(jfd, &pgRec.aData, sizeof(pgRec.aData));
drh99ee3602003-02-16 19:13:36 +0000557 if( rc!=SQLITE_OK ) return rc;
drhfa86c412002-02-02 15:01:15 +0000558
drh968af522003-02-11 14:55:40 +0000559 /* Sanity checking on the page. This is more important that I originally
560 ** thought. If a power failure occurs while the journal is being written,
561 ** it could cause invalid data to be written into the journal. We need to
562 ** detect this invalid data (with high probability) and ignore it.
563 */
564 if( pgRec.pgno==0 ){
565 return SQLITE_DONE;
566 }
drh7d02cb72003-06-04 16:24:39 +0000567 if( pgRec.pgno>(unsigned)pPager->dbSize ){
drh968af522003-02-11 14:55:40 +0000568 return SQLITE_OK;
569 }
570 if( format>=JOURNAL_FORMAT_3 ){
571 rc = read32bits(format, jfd, &cksum);
drh99ee3602003-02-16 19:13:36 +0000572 if( rc ) return rc;
drh968af522003-02-11 14:55:40 +0000573 if( pager_cksum(pPager, pgRec.pgno, pgRec.aData)!=cksum ){
574 return SQLITE_DONE;
575 }
576 }
drhfa86c412002-02-02 15:01:15 +0000577
578 /* Playback the page. Update the in-memory copy of the page
579 ** at the same time, if there is one.
580 */
581 pPg = pager_lookup(pPager, pgRec.pgno);
drh99ee3602003-02-16 19:13:36 +0000582 TRACE2("PLAYBACK %d\n", pgRec.pgno);
danielk19774adee202004-05-08 08:23:19 +0000583 sqlite3OsSeek(&pPager->fd, (pgRec.pgno-1)*(off_t)SQLITE_PAGE_SIZE);
584 rc = sqlite3OsWrite(&pPager->fd, pgRec.aData, SQLITE_PAGE_SIZE);
drhfa86c412002-02-02 15:01:15 +0000585 if( pPg ){
drhacf4ac92003-12-17 23:57:34 +0000586 /* No page should ever be rolled back that is in use, except for page
587 ** 1 which is held in use in order to keep the lock on the database
588 ** active.
589 */
590 assert( pPg->nRef==0 || pPg->pgno==1 );
drhd0ba1932004-02-10 01:54:28 +0000591 memcpy(PGHDR_TO_DATA(pPg), pgRec.aData, SQLITE_PAGE_SIZE);
drhde647132004-05-07 17:57:49 +0000592 if( pPager->xDestructor ){
593 pPager->xDestructor(PGHDR_TO_DATA(pPg));
594 }
drhdb48ee02003-01-16 13:42:43 +0000595 pPg->dirty = 0;
596 pPg->needSync = 0;
drh9eb9e262004-02-11 02:18:05 +0000597 CODEC(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3);
drhfa86c412002-02-02 15:01:15 +0000598 }
599 return rc;
600}
601
602/*
drhed7c8552001-04-11 14:29:21 +0000603** Playback the journal and thus restore the database file to
604** the state it was in before we started making changes.
605**
drh34e79ce2004-02-08 06:05:46 +0000606** The journal file format is as follows:
607**
608** * 8 byte prefix. One of the aJournalMagic123 vectors defined
609** above. The format of the journal file is determined by which
610** of the three prefix vectors is seen.
611** * 4 byte big-endian integer which is the number of valid page records
612** in the journal. If this value is 0xffffffff, then compute the
613** number of page records from the journal size. This field appears
614** in format 3 only.
615** * 4 byte big-endian integer which is the initial value for the
616** sanity checksum. This field appears in format 3 only.
617** * 4 byte integer which is the number of pages to truncate the
618** database to during a rollback.
619** * Zero or more pages instances, each as follows:
620** + 4 byte page number.
drhd0ba1932004-02-10 01:54:28 +0000621** + SQLITE_PAGE_SIZE bytes of data.
drh34e79ce2004-02-08 06:05:46 +0000622** + 4 byte checksum (format 3 only)
623**
624** When we speak of the journal header, we mean the first 4 bullets above.
625** Each entry in the journal is an instance of the 5th bullet. Note that
626** bullets 2 and 3 only appear in format-3 journals.
627**
628** Call the value from the second bullet "nRec". nRec is the number of
629** valid page entries in the journal. In most cases, you can compute the
630** value of nRec from the size of the journal file. But if a power
631** failure occurred while the journal was being written, it could be the
632** case that the size of the journal file had already been increased but
633** the extra entries had not yet made it safely to disk. In such a case,
634** the value of nRec computed from the file size would be too large. For
635** that reason, we always use the nRec value in the header.
636**
637** If the nRec value is 0xffffffff it means that nRec should be computed
638** from the file size. This value is used when the user selects the
639** no-sync option for the journal. A power failure could lead to corruption
640** in this case. But for things like temporary table (which will be
641** deleted when the power is restored) we don't care.
642**
643** Journal formats 1 and 2 do not have an nRec value in the header so we
644** have to compute nRec from the file size. This has risks (as described
645** above) which is why all persistent tables have been changed to use
646** format 3.
drhed7c8552001-04-11 14:29:21 +0000647**
drhd9b02572001-04-15 00:37:09 +0000648** If the file opened as the journal file is not a well-formed
drh34e79ce2004-02-08 06:05:46 +0000649** journal file then the database will likely already be
650** corrupted, so the PAGER_ERR_CORRUPT bit is set in pPager->errMask
651** and SQLITE_CORRUPT is returned. If it all works, then this routine
652** returns SQLITE_OK.
drhed7c8552001-04-11 14:29:21 +0000653*/
drh99ee3602003-02-16 19:13:36 +0000654static int pager_playback(Pager *pPager, int useJournalSize){
drh968af522003-02-11 14:55:40 +0000655 off_t szJ; /* Size of the journal file in bytes */
656 int nRec; /* Number of Records in the journal */
drhd9b02572001-04-15 00:37:09 +0000657 int i; /* Loop counter */
658 Pgno mxPg = 0; /* Size of the original file in pages */
drh968af522003-02-11 14:55:40 +0000659 int format; /* Format of the journal file. */
660 unsigned char aMagic[sizeof(aJournalMagic1)];
drhed7c8552001-04-11 14:29:21 +0000661 int rc;
662
drhc3a64ba2001-11-22 00:01:27 +0000663 /* Figure out how many records are in the journal. Abort early if
664 ** the journal is empty.
drhed7c8552001-04-11 14:29:21 +0000665 */
drh8cfbf082001-09-19 13:22:39 +0000666 assert( pPager->journalOpen );
danielk19774adee202004-05-08 08:23:19 +0000667 sqlite3OsSeek(&pPager->jfd, 0);
668 rc = sqlite3OsFileSize(&pPager->jfd, &szJ);
drhc3a64ba2001-11-22 00:01:27 +0000669 if( rc!=SQLITE_OK ){
670 goto end_playback;
671 }
drh240c5792004-02-08 00:40:52 +0000672
673 /* If the journal file is too small to contain a complete header,
drh34e79ce2004-02-08 06:05:46 +0000674 ** it must mean that the process that created the journal was just
675 ** beginning to write the journal file when it died. In that case,
676 ** the database file should have still been completely unchanged.
677 ** Nothing needs to be rolled back. We can safely ignore this journal.
drh240c5792004-02-08 00:40:52 +0000678 */
drh968af522003-02-11 14:55:40 +0000679 if( szJ < sizeof(aMagic)+sizeof(Pgno) ){
drhc3a64ba2001-11-22 00:01:27 +0000680 goto end_playback;
681 }
682
683 /* Read the beginning of the journal and truncate the
684 ** database file back to its original size.
685 */
danielk19774adee202004-05-08 08:23:19 +0000686 rc = sqlite3OsRead(&pPager->jfd, aMagic, sizeof(aMagic));
drh94f33312002-08-12 12:29:56 +0000687 if( rc!=SQLITE_OK ){
drh81a20f22001-10-12 17:30:04 +0000688 rc = SQLITE_PROTOCOL;
689 goto end_playback;
drhd9b02572001-04-15 00:37:09 +0000690 }
drh968af522003-02-11 14:55:40 +0000691 if( memcmp(aMagic, aJournalMagic3, sizeof(aMagic))==0 ){
692 format = JOURNAL_FORMAT_3;
693 }else if( memcmp(aMagic, aJournalMagic2, sizeof(aMagic))==0 ){
694 format = JOURNAL_FORMAT_2;
695 }else if( memcmp(aMagic, aJournalMagic1, sizeof(aMagic))==0 ){
696 format = JOURNAL_FORMAT_1;
drh94f33312002-08-12 12:29:56 +0000697 }else{
698 rc = SQLITE_PROTOCOL;
699 goto end_playback;
700 }
drh968af522003-02-11 14:55:40 +0000701 if( format>=JOURNAL_FORMAT_3 ){
drh240c5792004-02-08 00:40:52 +0000702 if( szJ < sizeof(aMagic) + 3*sizeof(u32) ){
703 /* Ignore the journal if it is too small to contain a complete
704 ** header. We already did this test once above, but at the prior
705 ** test, we did not know the journal format and so we had to assume
706 ** the smallest possible header. Now we know the header is bigger
drh34e79ce2004-02-08 06:05:46 +0000707 ** than the minimum so we test again.
drh240c5792004-02-08 00:40:52 +0000708 */
709 goto end_playback;
710 }
drh133cdf62004-01-07 02:52:07 +0000711 rc = read32bits(format, &pPager->jfd, (u32*)&nRec);
drh968af522003-02-11 14:55:40 +0000712 if( rc ) goto end_playback;
713 rc = read32bits(format, &pPager->jfd, &pPager->cksumInit);
714 if( rc ) goto end_playback;
drh99ee3602003-02-16 19:13:36 +0000715 if( nRec==0xffffffff || useJournalSize ){
drh968af522003-02-11 14:55:40 +0000716 nRec = (szJ - JOURNAL_HDR_SZ(3))/JOURNAL_PG_SZ(3);
717 }
718 }else{
drhd8d66e82003-02-12 02:10:15 +0000719 nRec = (szJ - JOURNAL_HDR_SZ(2))/JOURNAL_PG_SZ(2);
720 assert( nRec*JOURNAL_PG_SZ(2)+JOURNAL_HDR_SZ(2)==szJ );
drh968af522003-02-11 14:55:40 +0000721 }
722 rc = read32bits(format, &pPager->jfd, &mxPg);
drhd9b02572001-04-15 00:37:09 +0000723 if( rc!=SQLITE_OK ){
drh81a20f22001-10-12 17:30:04 +0000724 goto end_playback;
drhd9b02572001-04-15 00:37:09 +0000725 }
drhd8d66e82003-02-12 02:10:15 +0000726 assert( pPager->origDbSize==0 || pPager->origDbSize==mxPg );
danielk19774adee202004-05-08 08:23:19 +0000727 rc = sqlite3OsTruncate(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)mxPg);
drh81a20f22001-10-12 17:30:04 +0000728 if( rc!=SQLITE_OK ){
729 goto end_playback;
730 }
drhd9b02572001-04-15 00:37:09 +0000731 pPager->dbSize = mxPg;
732
drhfa86c412002-02-02 15:01:15 +0000733 /* Copy original pages out of the journal and back into the database file.
drhed7c8552001-04-11 14:29:21 +0000734 */
drh968af522003-02-11 14:55:40 +0000735 for(i=0; i<nRec; i++){
736 rc = pager_playback_one_page(pPager, &pPager->jfd, format);
737 if( rc!=SQLITE_OK ){
738 if( rc==SQLITE_DONE ){
drh968af522003-02-11 14:55:40 +0000739 rc = SQLITE_OK;
740 }
741 break;
742 }
drhed7c8552001-04-11 14:29:21 +0000743 }
drh81a20f22001-10-12 17:30:04 +0000744
drh4a0681e2003-02-13 01:58:20 +0000745 /* Pages that have been written to the journal but never synced
746 ** where not restored by the loop above. We have to restore those
drh240c5792004-02-08 00:40:52 +0000747 ** pages by reading them back from the original database.
drhdb48ee02003-01-16 13:42:43 +0000748 */
749 if( rc==SQLITE_OK ){
750 PgHdr *pPg;
751 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
drhd0ba1932004-02-10 01:54:28 +0000752 char zBuf[SQLITE_PAGE_SIZE];
drh4a0681e2003-02-13 01:58:20 +0000753 if( !pPg->dirty ) continue;
drhdb48ee02003-01-16 13:42:43 +0000754 if( (int)pPg->pgno <= pPager->origDbSize ){
danielk19774adee202004-05-08 08:23:19 +0000755 sqlite3OsSeek(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)(pPg->pgno-1));
756 rc = sqlite3OsRead(&pPager->fd, zBuf, SQLITE_PAGE_SIZE);
drh9eb9e262004-02-11 02:18:05 +0000757 TRACE2("REFETCH %d\n", pPg->pgno);
758 CODEC(pPager, zBuf, pPg->pgno, 2);
drhdb48ee02003-01-16 13:42:43 +0000759 if( rc ) break;
760 }else{
drhd0ba1932004-02-10 01:54:28 +0000761 memset(zBuf, 0, SQLITE_PAGE_SIZE);
drhdb48ee02003-01-16 13:42:43 +0000762 }
drhd0ba1932004-02-10 01:54:28 +0000763 if( pPg->nRef==0 || memcmp(zBuf, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE) ){
764 memcpy(PGHDR_TO_DATA(pPg), zBuf, SQLITE_PAGE_SIZE);
drh3a840692003-01-29 22:58:26 +0000765 memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra);
766 }
drhdb48ee02003-01-16 13:42:43 +0000767 pPg->needSync = 0;
768 pPg->dirty = 0;
769 }
770 }
drh4a0681e2003-02-13 01:58:20 +0000771
772end_playback:
drhd9b02572001-04-15 00:37:09 +0000773 if( rc!=SQLITE_OK ){
774 pager_unwritelock(pPager);
775 pPager->errMask |= PAGER_ERR_CORRUPT;
776 rc = SQLITE_CORRUPT;
777 }else{
778 rc = pager_unwritelock(pPager);
drhed7c8552001-04-11 14:29:21 +0000779 }
drhd9b02572001-04-15 00:37:09 +0000780 return rc;
drhed7c8552001-04-11 14:29:21 +0000781}
782
783/*
drhac69b052004-05-12 13:30:07 +0000784** Playback the statement journal.
drhfa86c412002-02-02 15:01:15 +0000785**
786** This is similar to playing back the transaction journal but with
787** a few extra twists.
788**
drh663fc632002-02-02 18:49:19 +0000789** (1) The number of pages in the database file at the start of
drhac69b052004-05-12 13:30:07 +0000790** the statement is stored in pPager->stmtSize, not in the
drh663fc632002-02-02 18:49:19 +0000791** journal file itself.
drhfa86c412002-02-02 15:01:15 +0000792**
drhac69b052004-05-12 13:30:07 +0000793** (2) In addition to playing back the statement journal, also
drhfa86c412002-02-02 15:01:15 +0000794** playback all pages of the transaction journal beginning
drhac69b052004-05-12 13:30:07 +0000795** at offset pPager->stmtJSize.
drhfa86c412002-02-02 15:01:15 +0000796*/
drh3aac2dd2004-04-26 14:10:20 +0000797static int pager_stmt_playback(Pager *pPager){
drh968af522003-02-11 14:55:40 +0000798 off_t szJ; /* Size of the full journal */
799 int nRec; /* Number of Records */
drhfa86c412002-02-02 15:01:15 +0000800 int i; /* Loop counter */
801 int rc;
802
803 /* Truncate the database back to its original size.
804 */
drhac69b052004-05-12 13:30:07 +0000805 rc = sqlite3OsTruncate(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)pPager->stmtSize);
806 pPager->dbSize = pPager->stmtSize;
drhfa86c412002-02-02 15:01:15 +0000807
drhac69b052004-05-12 13:30:07 +0000808 /* Figure out how many records are in the statement journal.
drhfa86c412002-02-02 15:01:15 +0000809 */
drhac69b052004-05-12 13:30:07 +0000810 assert( pPager->stmtInUse && pPager->journalOpen );
811 sqlite3OsSeek(&pPager->stfd, 0);
812 nRec = pPager->stmtNRec;
drhfa86c412002-02-02 15:01:15 +0000813
drhac69b052004-05-12 13:30:07 +0000814 /* Copy original pages out of the statement journal and back into the
815 ** database file. Note that the statement journal always uses format
drh968af522003-02-11 14:55:40 +0000816 ** 2 instead of format 3 since it does not need to be concerned with
817 ** power failures corrupting the journal and can thus omit the checksums.
drhfa86c412002-02-02 15:01:15 +0000818 */
819 for(i=nRec-1; i>=0; i--){
drhac69b052004-05-12 13:30:07 +0000820 rc = pager_playback_one_page(pPager, &pPager->stfd, 2);
drh968af522003-02-11 14:55:40 +0000821 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +0000822 if( rc!=SQLITE_OK ) goto end_stmt_playback;
drhfa86c412002-02-02 15:01:15 +0000823 }
824
825 /* Figure out how many pages need to be copied out of the transaction
826 ** journal.
827 */
drhac69b052004-05-12 13:30:07 +0000828 rc = sqlite3OsSeek(&pPager->jfd, pPager->stmtJSize);
drhfa86c412002-02-02 15:01:15 +0000829 if( rc!=SQLITE_OK ){
drh3aac2dd2004-04-26 14:10:20 +0000830 goto end_stmt_playback;
drhfa86c412002-02-02 15:01:15 +0000831 }
danielk19774adee202004-05-08 08:23:19 +0000832 rc = sqlite3OsFileSize(&pPager->jfd, &szJ);
drhfa86c412002-02-02 15:01:15 +0000833 if( rc!=SQLITE_OK ){
drh3aac2dd2004-04-26 14:10:20 +0000834 goto end_stmt_playback;
drhfa86c412002-02-02 15:01:15 +0000835 }
drhac69b052004-05-12 13:30:07 +0000836 nRec = (szJ - pPager->stmtJSize)/JOURNAL_PG_SZ(journal_format);
drhfa86c412002-02-02 15:01:15 +0000837 for(i=nRec-1; i>=0; i--){
drh968af522003-02-11 14:55:40 +0000838 rc = pager_playback_one_page(pPager, &pPager->jfd, journal_format);
839 if( rc!=SQLITE_OK ){
840 assert( rc!=SQLITE_DONE );
drh3aac2dd2004-04-26 14:10:20 +0000841 goto end_stmt_playback;
drh968af522003-02-11 14:55:40 +0000842 }
drhfa86c412002-02-02 15:01:15 +0000843 }
844
drh3aac2dd2004-04-26 14:10:20 +0000845end_stmt_playback:
drhfa86c412002-02-02 15:01:15 +0000846 if( rc!=SQLITE_OK ){
drhfa86c412002-02-02 15:01:15 +0000847 pPager->errMask |= PAGER_ERR_CORRUPT;
848 rc = SQLITE_CORRUPT;
drhfa86c412002-02-02 15:01:15 +0000849 }
850 return rc;
851}
852
853/*
drhf57b14a2001-09-14 18:54:08 +0000854** Change the maximum number of in-memory pages that are allowed.
drhcd61c282002-03-06 22:01:34 +0000855**
856** The maximum number is the absolute value of the mxPage parameter.
857** If mxPage is negative, the noSync flag is also set. noSync bypasses
danielk19774adee202004-05-08 08:23:19 +0000858** calls to sqlite3OsSync(). The pager runs much faster with noSync on,
drhcd61c282002-03-06 22:01:34 +0000859** but if the operating system crashes or there is an abrupt power
860** failure, the database file might be left in an inconsistent and
861** unrepairable state.
drhf57b14a2001-09-14 18:54:08 +0000862*/
drh3aac2dd2004-04-26 14:10:20 +0000863void sqlite3pager_set_cachesize(Pager *pPager, int mxPage){
drh603240c2002-03-05 01:11:12 +0000864 if( mxPage>=0 ){
drha1680452002-04-18 01:56:57 +0000865 pPager->noSync = pPager->tempFile;
drh946966f2004-02-25 02:20:41 +0000866 if( pPager->noSync==0 ) pPager->needSync = 0;
drh603240c2002-03-05 01:11:12 +0000867 }else{
868 pPager->noSync = 1;
869 mxPage = -mxPage;
870 }
drhf57b14a2001-09-14 18:54:08 +0000871 if( mxPage>10 ){
872 pPager->mxPage = mxPage;
873 }
874}
875
876/*
drh973b6e32003-02-12 14:09:42 +0000877** Adjust the robustness of the database to damage due to OS crashes
878** or power failures by changing the number of syncs()s when writing
879** the rollback journal. There are three levels:
880**
danielk19774adee202004-05-08 08:23:19 +0000881** OFF sqlite3OsSync() is never called. This is the default
drh973b6e32003-02-12 14:09:42 +0000882** for temporary and transient files.
883**
884** NORMAL The journal is synced once before writes begin on the
885** database. This is normally adequate protection, but
886** it is theoretically possible, though very unlikely,
887** that an inopertune power failure could leave the journal
888** in a state which would cause damage to the database
889** when it is rolled back.
890**
891** FULL The journal is synced twice before writes begin on the
drh34e79ce2004-02-08 06:05:46 +0000892** database (with some additional information - the nRec field
893** of the journal header - being written in between the two
894** syncs). If we assume that writing a
drh973b6e32003-02-12 14:09:42 +0000895** single disk sector is atomic, then this mode provides
896** assurance that the journal will not be corrupted to the
897** point of causing damage to the database during rollback.
898**
899** Numeric values associated with these states are OFF==1, NORMAL=2,
900** and FULL=3.
901*/
drh3aac2dd2004-04-26 14:10:20 +0000902void sqlite3pager_set_safety_level(Pager *pPager, int level){
drh973b6e32003-02-12 14:09:42 +0000903 pPager->noSync = level==1 || pPager->tempFile;
904 pPager->fullSync = level==3 && !pPager->tempFile;
drh946966f2004-02-25 02:20:41 +0000905 if( pPager->noSync==0 ) pPager->needSync = 0;
drh973b6e32003-02-12 14:09:42 +0000906}
907
908/*
drhfa86c412002-02-02 15:01:15 +0000909** Open a temporary file. Write the name of the file into zName
910** (zName must be at least SQLITE_TEMPNAME_SIZE bytes long.) Write
911** the file descriptor into *fd. Return SQLITE_OK on success or some
912** other error code if we fail.
913**
914** The OS will automatically delete the temporary file when it is
915** closed.
916*/
drh3aac2dd2004-04-26 14:10:20 +0000917static int sqlite3pager_opentemp(char *zFile, OsFile *fd){
drhfa86c412002-02-02 15:01:15 +0000918 int cnt = 8;
919 int rc;
920 do{
921 cnt--;
danielk19774adee202004-05-08 08:23:19 +0000922 sqlite3OsTempFileName(zFile);
923 rc = sqlite3OsOpenExclusive(zFile, fd, 1);
drhfa86c412002-02-02 15:01:15 +0000924 }while( cnt>0 && rc!=SQLITE_OK );
925 return rc;
926}
927
928/*
drhed7c8552001-04-11 14:29:21 +0000929** Create a new page cache and put a pointer to the page cache in *ppPager.
drh5e00f6c2001-09-13 13:46:56 +0000930** The file to be cached need not exist. The file is not locked until
drh3aac2dd2004-04-26 14:10:20 +0000931** the first call to sqlite3pager_get() and is only held open until the
932** last page is released using sqlite3pager_unref().
drh382c0242001-10-06 16:33:02 +0000933**
drh6446c4d2001-12-15 14:22:18 +0000934** If zFilename is NULL then a randomly-named temporary file is created
935** and used as the file to be cached. The file will be deleted
936** automatically when it is closed.
drhed7c8552001-04-11 14:29:21 +0000937*/
drh3aac2dd2004-04-26 14:10:20 +0000938int sqlite3pager_open(
drh7e3b0a02001-04-28 16:52:40 +0000939 Pager **ppPager, /* Return the Pager structure here */
940 const char *zFilename, /* Name of the database file to open */
941 int mxPage, /* Max number of in-memory cache pages */
drhda47d772002-12-02 04:25:19 +0000942 int nExtra, /* Extra bytes append to each in-memory page */
943 int useJournal /* TRUE to use a rollback journal on this file */
drh7e3b0a02001-04-28 16:52:40 +0000944){
drhed7c8552001-04-11 14:29:21 +0000945 Pager *pPager;
drh3e7a6092002-12-07 21:45:14 +0000946 char *zFullPathname;
drhed7c8552001-04-11 14:29:21 +0000947 int nameLen;
drh8cfbf082001-09-19 13:22:39 +0000948 OsFile fd;
drha76c82e2003-07-27 18:59:42 +0000949 int rc, i;
drh5e00f6c2001-09-13 13:46:56 +0000950 int tempFile;
drhac69b052004-05-12 13:30:07 +0000951 int memDb = 0;
drh5e00f6c2001-09-13 13:46:56 +0000952 int readOnly = 0;
drh8cfbf082001-09-19 13:22:39 +0000953 char zTemp[SQLITE_TEMPNAME_SIZE];
drhed7c8552001-04-11 14:29:21 +0000954
drhd9b02572001-04-15 00:37:09 +0000955 *ppPager = 0;
danielk19776f8a5032004-05-10 10:34:51 +0000956 if( sqlite3_malloc_failed ){
drhd9b02572001-04-15 00:37:09 +0000957 return SQLITE_NOMEM;
958 }
drh901afd42003-08-26 11:25:58 +0000959 if( zFilename && zFilename[0] ){
drhac69b052004-05-12 13:30:07 +0000960 if( strcmp(zFilename,":memory:")==0 ){
961 memDb = 1;
962 zFullPathname = sqliteMalloc(4);
963 if( zFullPathname ) strcpy(zFullPathname, "nil");
964 rc = SQLITE_OK;
965 }else{
966 zFullPathname = sqlite3OsFullPathname(zFilename);
967 rc = sqlite3OsOpenReadWrite(zFullPathname, &fd, &readOnly);
968 tempFile = 0;
969 }
drh5e00f6c2001-09-13 13:46:56 +0000970 }else{
drh3aac2dd2004-04-26 14:10:20 +0000971 rc = sqlite3pager_opentemp(zTemp, &fd);
drh5e00f6c2001-09-13 13:46:56 +0000972 zFilename = zTemp;
danielk19774adee202004-05-08 08:23:19 +0000973 zFullPathname = sqlite3OsFullPathname(zFilename);
drh5e00f6c2001-09-13 13:46:56 +0000974 tempFile = 1;
975 }
danielk19776f8a5032004-05-10 10:34:51 +0000976 if( sqlite3_malloc_failed ){
drh3e7a6092002-12-07 21:45:14 +0000977 return SQLITE_NOMEM;
978 }
drh8cfbf082001-09-19 13:22:39 +0000979 if( rc!=SQLITE_OK ){
drh3e7a6092002-12-07 21:45:14 +0000980 sqliteFree(zFullPathname);
drhed7c8552001-04-11 14:29:21 +0000981 return SQLITE_CANTOPEN;
982 }
drh3e7a6092002-12-07 21:45:14 +0000983 nameLen = strlen(zFullPathname);
drha76c82e2003-07-27 18:59:42 +0000984 pPager = sqliteMalloc( sizeof(*pPager) + nameLen*3 + 30 );
drhd9b02572001-04-15 00:37:09 +0000985 if( pPager==0 ){
danielk19774adee202004-05-08 08:23:19 +0000986 sqlite3OsClose(&fd);
drh3e7a6092002-12-07 21:45:14 +0000987 sqliteFree(zFullPathname);
drhd9b02572001-04-15 00:37:09 +0000988 return SQLITE_NOMEM;
989 }
drhdb48ee02003-01-16 13:42:43 +0000990 SET_PAGER(pPager);
drhed7c8552001-04-11 14:29:21 +0000991 pPager->zFilename = (char*)&pPager[1];
drha76c82e2003-07-27 18:59:42 +0000992 pPager->zDirectory = &pPager->zFilename[nameLen+1];
993 pPager->zJournal = &pPager->zDirectory[nameLen+1];
drh3e7a6092002-12-07 21:45:14 +0000994 strcpy(pPager->zFilename, zFullPathname);
drha76c82e2003-07-27 18:59:42 +0000995 strcpy(pPager->zDirectory, zFullPathname);
996 for(i=nameLen; i>0 && pPager->zDirectory[i-1]!='/'; i--){}
997 if( i>0 ) pPager->zDirectory[i-1] = 0;
drh3e7a6092002-12-07 21:45:14 +0000998 strcpy(pPager->zJournal, zFullPathname);
999 sqliteFree(zFullPathname);
drhed7c8552001-04-11 14:29:21 +00001000 strcpy(&pPager->zJournal[nameLen], "-journal");
1001 pPager->fd = fd;
drh8cfbf082001-09-19 13:22:39 +00001002 pPager->journalOpen = 0;
drhac69b052004-05-12 13:30:07 +00001003 pPager->useJournal = useJournal && !memDb;
1004 pPager->stmtOpen = 0;
1005 pPager->stmtInUse = 0;
drhed7c8552001-04-11 14:29:21 +00001006 pPager->nRef = 0;
drhac69b052004-05-12 13:30:07 +00001007 pPager->dbSize = memDb-1;
1008 pPager->pageSize = SQLITE_PAGE_SIZE;
1009 pPager->stmtSize = 0;
1010 pPager->stmtJSize = 0;
drhed7c8552001-04-11 14:29:21 +00001011 pPager->nPage = 0;
drhd79caeb2001-04-15 02:27:24 +00001012 pPager->mxPage = mxPage>5 ? mxPage : 10;
drhed7c8552001-04-11 14:29:21 +00001013 pPager->state = SQLITE_UNLOCK;
drhd9b02572001-04-15 00:37:09 +00001014 pPager->errMask = 0;
drh5e00f6c2001-09-13 13:46:56 +00001015 pPager->tempFile = tempFile;
drhac69b052004-05-12 13:30:07 +00001016 pPager->memDb = memDb;
drh5e00f6c2001-09-13 13:46:56 +00001017 pPager->readOnly = readOnly;
drhf57b14a2001-09-14 18:54:08 +00001018 pPager->needSync = 0;
drhda47d772002-12-02 04:25:19 +00001019 pPager->noSync = pPager->tempFile || !useJournal;
drhed7c8552001-04-11 14:29:21 +00001020 pPager->pFirst = 0;
drh341eae82003-01-21 02:39:36 +00001021 pPager->pFirstSynced = 0;
drhed7c8552001-04-11 14:29:21 +00001022 pPager->pLast = 0;
drh7c717f72001-06-24 20:39:41 +00001023 pPager->nExtra = nExtra;
drhed7c8552001-04-11 14:29:21 +00001024 memset(pPager->aHash, 0, sizeof(pPager->aHash));
1025 *ppPager = pPager;
1026 return SQLITE_OK;
1027}
1028
1029/*
drh72f82862001-05-24 21:06:34 +00001030** Set the destructor for this pager. If not NULL, the destructor is called
drh5e00f6c2001-09-13 13:46:56 +00001031** when the reference count on each page reaches zero. The destructor can
1032** be used to clean up information in the extra segment appended to each page.
drh72f82862001-05-24 21:06:34 +00001033**
drh3aac2dd2004-04-26 14:10:20 +00001034** The destructor is not called as a result sqlite3pager_close().
1035** Destructors are only called by sqlite3pager_unref().
drh72f82862001-05-24 21:06:34 +00001036*/
drh3aac2dd2004-04-26 14:10:20 +00001037void sqlite3pager_set_destructor(Pager *pPager, void (*xDesc)(void*)){
drh72f82862001-05-24 21:06:34 +00001038 pPager->xDestructor = xDesc;
1039}
1040
1041/*
drh5e00f6c2001-09-13 13:46:56 +00001042** Return the total number of pages in the disk file associated with
1043** pPager.
drhed7c8552001-04-11 14:29:21 +00001044*/
drh3aac2dd2004-04-26 14:10:20 +00001045int sqlite3pager_pagecount(Pager *pPager){
drh28be87c2002-11-05 23:03:02 +00001046 off_t n;
drhd9b02572001-04-15 00:37:09 +00001047 assert( pPager!=0 );
drhed7c8552001-04-11 14:29:21 +00001048 if( pPager->dbSize>=0 ){
1049 return pPager->dbSize;
1050 }
danielk19774adee202004-05-08 08:23:19 +00001051 if( sqlite3OsFileSize(&pPager->fd, &n)!=SQLITE_OK ){
drh81a20f22001-10-12 17:30:04 +00001052 pPager->errMask |= PAGER_ERR_DISK;
drh8cfbf082001-09-19 13:22:39 +00001053 return 0;
drhed7c8552001-04-11 14:29:21 +00001054 }
drhd0ba1932004-02-10 01:54:28 +00001055 n /= SQLITE_PAGE_SIZE;
drhd9b02572001-04-15 00:37:09 +00001056 if( pPager->state!=SQLITE_UNLOCK ){
drhed7c8552001-04-11 14:29:21 +00001057 pPager->dbSize = n;
1058 }
1059 return n;
1060}
1061
1062/*
drhf7c57532003-04-25 13:22:51 +00001063** Forward declaration
1064*/
drh34e79ce2004-02-08 06:05:46 +00001065static int syncJournal(Pager*);
drhf7c57532003-04-25 13:22:51 +00001066
drhac69b052004-05-12 13:30:07 +00001067
1068/*
1069** Unlink a page from the free list (the list of all pages where nRef==0)
1070** and from its hash collision chain.
1071*/
1072static void unlinkPage(PgHdr *pPg){
1073 Pager *pPager = pPg->pPager;
1074
1075 /* Keep the pFirstSynced pointer pointing at the first synchronized page */
1076 if( pPg==pPager->pFirstSynced ){
1077 PgHdr *p = pPg->pNextFree;
1078 while( p && p->needSync ){ p = p->pNextFree; }
1079 pPager->pFirstSynced = p;
1080 }
1081
1082 /* Unlink from the freelist */
1083 if( pPg->pPrevFree ){
1084 pPg->pPrevFree->pNextFree = pPg->pNextFree;
1085 }else{
1086 assert( pPager->pFirst==pPg );
1087 pPager->pFirst = pPg->pNextFree;
1088 }
1089 if( pPg->pNextFree ){
1090 pPg->pNextFree->pPrevFree = pPg->pPrevFree;
1091 }else{
1092 assert( pPager->pLast==pPg );
1093 pPager->pLast = pPg->pPrevFree;
1094 }
1095 pPg->pNextFree = pPg->pPrevFree = 0;
1096
1097 /* Unlink from the pgno hash table */
1098 if( pPg->pNextHash ){
1099 pPg->pNextHash->pPrevHash = pPg->pPrevHash;
1100 }
1101 if( pPg->pPrevHash ){
1102 pPg->pPrevHash->pNextHash = pPg->pNextHash;
1103 }else{
1104 int h = pager_hash(pPg->pgno);
1105 assert( pPager->aHash[h]==pPg );
1106 pPager->aHash[h] = pPg->pNextHash;
1107 }
1108 pPg->pNextHash = pPg->pPrevHash = 0;
1109}
1110
1111/*
1112** This routine is used to truncate an in-memory database. Delete
1113** every pages whose pgno is larger than pPager->dbSize and is unreferenced.
1114** Referenced pages larger than pPager->dbSize are zeroed.
1115*/
1116static void memoryTruncate(Pager *pPager){
1117 PgHdr *pPg;
1118 PgHdr **ppPg;
1119 int dbSize = pPager->dbSize;
1120
1121 ppPg = &pPager->pAll;
1122 while( (pPg = *ppPg)!=0 ){
1123 if( pPg->pgno<=dbSize ){
1124 ppPg = &pPg->pNextAll;
1125 }else if( pPg->nRef>0 ){
1126 memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
1127 ppPg = &pPg->pNextAll;
1128 }else{
1129 *ppPg = pPg->pNextAll;
1130 unlinkPage(pPg);
1131 sqliteFree(pPg);
1132 pPager->nPage--;
1133 }
1134 }
1135}
1136
drhf7c57532003-04-25 13:22:51 +00001137/*
1138** Truncate the file to the number of pages specified.
1139*/
drh3aac2dd2004-04-26 14:10:20 +00001140int sqlite3pager_truncate(Pager *pPager, Pgno nPage){
drhf7c57532003-04-25 13:22:51 +00001141 int rc;
drh2e6d11b2003-04-25 15:37:57 +00001142 if( pPager->dbSize<0 ){
drh3aac2dd2004-04-26 14:10:20 +00001143 sqlite3pager_pagecount(pPager);
drh2e6d11b2003-04-25 15:37:57 +00001144 }
1145 if( pPager->errMask!=0 ){
1146 rc = pager_errcode(pPager);
1147 return rc;
1148 }
drh7d02cb72003-06-04 16:24:39 +00001149 if( nPage>=(unsigned)pPager->dbSize ){
drhf7c57532003-04-25 13:22:51 +00001150 return SQLITE_OK;
1151 }
drhac69b052004-05-12 13:30:07 +00001152 if( pPager->memDb ){
1153 pPager->dbSize = nPage;
1154 memoryTruncate(pPager);
1155 return SQLITE_OK;
1156 }
drh34e79ce2004-02-08 06:05:46 +00001157 syncJournal(pPager);
danielk19774adee202004-05-08 08:23:19 +00001158 rc = sqlite3OsTruncate(&pPager->fd, SQLITE_PAGE_SIZE*(off_t)nPage);
drhf7c57532003-04-25 13:22:51 +00001159 if( rc==SQLITE_OK ){
1160 pPager->dbSize = nPage;
1161 }
1162 return rc;
1163}
1164
1165/*
drhed7c8552001-04-11 14:29:21 +00001166** Shutdown the page cache. Free all memory and close all files.
1167**
1168** If a transaction was in progress when this routine is called, that
1169** transaction is rolled back. All outstanding pages are invalidated
1170** and their memory is freed. Any attempt to use a page associated
1171** with this page cache after this function returns will likely
1172** result in a coredump.
1173*/
drh3aac2dd2004-04-26 14:10:20 +00001174int sqlite3pager_close(Pager *pPager){
drhd9b02572001-04-15 00:37:09 +00001175 PgHdr *pPg, *pNext;
drhed7c8552001-04-11 14:29:21 +00001176 switch( pPager->state ){
1177 case SQLITE_WRITELOCK: {
drh3aac2dd2004-04-26 14:10:20 +00001178 sqlite3pager_rollback(pPager);
drhac69b052004-05-12 13:30:07 +00001179 if( !pPager->memDb ){
1180 sqlite3OsUnlock(&pPager->fd);
1181 }
drh8cfbf082001-09-19 13:22:39 +00001182 assert( pPager->journalOpen==0 );
drhed7c8552001-04-11 14:29:21 +00001183 break;
1184 }
1185 case SQLITE_READLOCK: {
drhac69b052004-05-12 13:30:07 +00001186 if( !pPager->memDb ){
1187 sqlite3OsUnlock(&pPager->fd);
1188 }
drhed7c8552001-04-11 14:29:21 +00001189 break;
1190 }
1191 default: {
1192 /* Do nothing */
1193 break;
1194 }
1195 }
drhd9b02572001-04-15 00:37:09 +00001196 for(pPg=pPager->pAll; pPg; pPg=pNext){
1197 pNext = pPg->pNextAll;
1198 sqliteFree(pPg);
drhed7c8552001-04-11 14:29:21 +00001199 }
drhac69b052004-05-12 13:30:07 +00001200 if( !pPager->memDb ){
1201 sqlite3OsClose(&pPager->fd);
1202 }
drh8cfbf082001-09-19 13:22:39 +00001203 assert( pPager->journalOpen==0 );
drh0f892532002-05-30 12:27:03 +00001204 /* Temp files are automatically deleted by the OS
1205 ** if( pPager->tempFile ){
danielk19774adee202004-05-08 08:23:19 +00001206 ** sqlite3OsDelete(pPager->zFilename);
drh0f892532002-05-30 12:27:03 +00001207 ** }
1208 */
drhdb48ee02003-01-16 13:42:43 +00001209 CLR_PAGER(pPager);
drh73509ee2003-04-06 20:44:45 +00001210 if( pPager->zFilename!=(char*)&pPager[1] ){
drha76c82e2003-07-27 18:59:42 +00001211 assert( 0 ); /* Cannot happen */
drh73509ee2003-04-06 20:44:45 +00001212 sqliteFree(pPager->zFilename);
1213 sqliteFree(pPager->zJournal);
drha76c82e2003-07-27 18:59:42 +00001214 sqliteFree(pPager->zDirectory);
drh73509ee2003-04-06 20:44:45 +00001215 }
drhed7c8552001-04-11 14:29:21 +00001216 sqliteFree(pPager);
1217 return SQLITE_OK;
1218}
1219
1220/*
drh5e00f6c2001-09-13 13:46:56 +00001221** Return the page number for the given page data.
drhed7c8552001-04-11 14:29:21 +00001222*/
drh3aac2dd2004-04-26 14:10:20 +00001223Pgno sqlite3pager_pagenumber(void *pData){
drhed7c8552001-04-11 14:29:21 +00001224 PgHdr *p = DATA_TO_PGHDR(pData);
1225 return p->pgno;
1226}
1227
1228/*
drhc8629a12004-05-08 20:07:40 +00001229** The page_ref() function increments the reference count for a page.
1230** If the page is currently on the freelist (the reference count is zero) then
drh7e3b0a02001-04-28 16:52:40 +00001231** remove it from the freelist.
drhc8629a12004-05-08 20:07:40 +00001232**
1233** For non-test systems, page_ref() is a macro that calls _page_ref()
1234** online of the reference count is zero. For test systems, page_ref()
1235** is a real function so that we can set breakpoints and trace it.
drh7e3b0a02001-04-28 16:52:40 +00001236*/
drh836faa42003-01-11 13:30:57 +00001237static void _page_ref(PgHdr *pPg){
drh7e3b0a02001-04-28 16:52:40 +00001238 if( pPg->nRef==0 ){
1239 /* The page is currently on the freelist. Remove it. */
drh341eae82003-01-21 02:39:36 +00001240 if( pPg==pPg->pPager->pFirstSynced ){
1241 PgHdr *p = pPg->pNextFree;
1242 while( p && p->needSync ){ p = p->pNextFree; }
1243 pPg->pPager->pFirstSynced = p;
1244 }
drh7e3b0a02001-04-28 16:52:40 +00001245 if( pPg->pPrevFree ){
1246 pPg->pPrevFree->pNextFree = pPg->pNextFree;
1247 }else{
1248 pPg->pPager->pFirst = pPg->pNextFree;
1249 }
1250 if( pPg->pNextFree ){
1251 pPg->pNextFree->pPrevFree = pPg->pPrevFree;
1252 }else{
1253 pPg->pPager->pLast = pPg->pPrevFree;
1254 }
1255 pPg->pPager->nRef++;
1256 }
1257 pPg->nRef++;
drhdd793422001-06-28 01:54:48 +00001258 REFINFO(pPg);
drhdf0b3b02001-06-23 11:36:20 +00001259}
drhc8629a12004-05-08 20:07:40 +00001260#ifdef SQLITE_TEST
1261 static void page_ref(PgHdr *pPg){
1262 if( pPg->nRef==0 ){
1263 _page_ref(pPg);
1264 }else{
1265 pPg->nRef++;
1266 REFINFO(pPg);
1267 }
1268 }
1269#else
1270# define page_ref(P) ((P)->nRef==0?_page_ref(P):(void)(P)->nRef++)
1271#endif
drhdf0b3b02001-06-23 11:36:20 +00001272
1273/*
1274** Increment the reference count for a page. The input pointer is
1275** a reference to the page data.
1276*/
drh3aac2dd2004-04-26 14:10:20 +00001277int sqlite3pager_ref(void *pData){
drhdf0b3b02001-06-23 11:36:20 +00001278 PgHdr *pPg = DATA_TO_PGHDR(pData);
1279 page_ref(pPg);
drh8c42ca92001-06-22 19:15:00 +00001280 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +00001281}
1282
1283/*
drh34e79ce2004-02-08 06:05:46 +00001284** Sync the journal. In other words, make sure all the pages that have
1285** been written to the journal have actually reached the surface of the
1286** disk. It is not safe to modify the original database file until after
1287** the journal has been synced. If the original database is modified before
1288** the journal is synced and a power failure occurs, the unsynced journal
1289** data would be lost and we would be unable to completely rollback the
1290** database changes. Database corruption would occur.
1291**
1292** This routine also updates the nRec field in the header of the journal.
1293** (See comments on the pager_playback() routine for additional information.)
1294** If the sync mode is FULL, two syncs will occur. First the whole journal
1295** is synced, then the nRec field is updated, then a second sync occurs.
drhb19a2bc2001-09-16 00:13:26 +00001296**
drh34e79ce2004-02-08 06:05:46 +00001297** For temporary databases, we do not care if we are able to rollback
1298** after a power failure, so sync occurs.
drhfa86c412002-02-02 15:01:15 +00001299**
drh34e79ce2004-02-08 06:05:46 +00001300** This routine clears the needSync field of every page current held in
1301** memory.
drh50e5dad2001-09-15 00:57:28 +00001302*/
drh34e79ce2004-02-08 06:05:46 +00001303static int syncJournal(Pager *pPager){
drh50e5dad2001-09-15 00:57:28 +00001304 PgHdr *pPg;
1305 int rc = SQLITE_OK;
drh03eb96a2002-11-10 23:32:56 +00001306
1307 /* Sync the journal before modifying the main database
1308 ** (assuming there is a journal and it needs to be synced.)
1309 */
drh50e5dad2001-09-15 00:57:28 +00001310 if( pPager->needSync ){
drhfa86c412002-02-02 15:01:15 +00001311 if( !pPager->tempFile ){
drhdb48ee02003-01-16 13:42:43 +00001312 assert( pPager->journalOpen );
drh946966f2004-02-25 02:20:41 +00001313 /* assert( !pPager->noSync ); // noSync might be set if synchronous
1314 ** was turned off after the transaction was started. Ticket #615 */
drh968af522003-02-11 14:55:40 +00001315#ifndef NDEBUG
1316 {
drh34e79ce2004-02-08 06:05:46 +00001317 /* Make sure the pPager->nRec counter we are keeping agrees
1318 ** with the nRec computed from the size of the journal file.
1319 */
drh4a0681e2003-02-13 01:58:20 +00001320 off_t hdrSz, pgSz, jSz;
drh968af522003-02-11 14:55:40 +00001321 hdrSz = JOURNAL_HDR_SZ(journal_format);
1322 pgSz = JOURNAL_PG_SZ(journal_format);
danielk19774adee202004-05-08 08:23:19 +00001323 rc = sqlite3OsFileSize(&pPager->jfd, &jSz);
drh968af522003-02-11 14:55:40 +00001324 if( rc!=0 ) return rc;
drh4a0681e2003-02-13 01:58:20 +00001325 assert( pPager->nRec*pgSz+hdrSz==jSz );
drh968af522003-02-11 14:55:40 +00001326 }
1327#endif
drhd8d66e82003-02-12 02:10:15 +00001328 if( journal_format>=3 ){
drh34e79ce2004-02-08 06:05:46 +00001329 /* Write the nRec value into the journal file header */
drhd8d66e82003-02-12 02:10:15 +00001330 off_t szJ;
1331 if( pPager->fullSync ){
1332 TRACE1("SYNC\n");
danielk19774adee202004-05-08 08:23:19 +00001333 rc = sqlite3OsSync(&pPager->jfd);
drhd8d66e82003-02-12 02:10:15 +00001334 if( rc!=0 ) return rc;
1335 }
danielk19774adee202004-05-08 08:23:19 +00001336 sqlite3OsSeek(&pPager->jfd, sizeof(aJournalMagic1));
drh99ee3602003-02-16 19:13:36 +00001337 rc = write32bits(&pPager->jfd, pPager->nRec);
1338 if( rc ) return rc;
drhd8d66e82003-02-12 02:10:15 +00001339 szJ = JOURNAL_HDR_SZ(journal_format) +
1340 pPager->nRec*JOURNAL_PG_SZ(journal_format);
danielk19774adee202004-05-08 08:23:19 +00001341 sqlite3OsSeek(&pPager->jfd, szJ);
drh968af522003-02-11 14:55:40 +00001342 }
drhdb48ee02003-01-16 13:42:43 +00001343 TRACE1("SYNC\n");
danielk19774adee202004-05-08 08:23:19 +00001344 rc = sqlite3OsSync(&pPager->jfd);
drhfa86c412002-02-02 15:01:15 +00001345 if( rc!=0 ) return rc;
drhdb48ee02003-01-16 13:42:43 +00001346 pPager->journalStarted = 1;
drhfa86c412002-02-02 15:01:15 +00001347 }
drh50e5dad2001-09-15 00:57:28 +00001348 pPager->needSync = 0;
drh341eae82003-01-21 02:39:36 +00001349
1350 /* Erase the needSync flag from every page.
1351 */
1352 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1353 pPg->needSync = 0;
1354 }
1355 pPager->pFirstSynced = pPager->pFirst;
drh50e5dad2001-09-15 00:57:28 +00001356 }
drh03eb96a2002-11-10 23:32:56 +00001357
drh341eae82003-01-21 02:39:36 +00001358#ifndef NDEBUG
1359 /* If the Pager.needSync flag is clear then the PgHdr.needSync
1360 ** flag must also be clear for all pages. Verify that this
1361 ** invariant is true.
drh03eb96a2002-11-10 23:32:56 +00001362 */
drh341eae82003-01-21 02:39:36 +00001363 else{
1364 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1365 assert( pPg->needSync==0 );
1366 }
1367 assert( pPager->pFirstSynced==pPager->pFirst );
drh03eb96a2002-11-10 23:32:56 +00001368 }
drh341eae82003-01-21 02:39:36 +00001369#endif
drhdb48ee02003-01-16 13:42:43 +00001370
drh81a20f22001-10-12 17:30:04 +00001371 return rc;
drh50e5dad2001-09-15 00:57:28 +00001372}
1373
1374/*
drh2554f8b2003-01-22 01:26:44 +00001375** Given a list of pages (connected by the PgHdr.pDirty pointer) write
1376** every one of those pages out to the database file and mark them all
1377** as clean.
1378*/
1379static int pager_write_pagelist(PgHdr *pList){
1380 Pager *pPager;
1381 int rc;
1382
1383 if( pList==0 ) return SQLITE_OK;
1384 pPager = pList->pPager;
1385 while( pList ){
1386 assert( pList->dirty );
danielk19774adee202004-05-08 08:23:19 +00001387 sqlite3OsSeek(&pPager->fd, (pList->pgno-1)*(off_t)SQLITE_PAGE_SIZE);
drh9eb9e262004-02-11 02:18:05 +00001388 CODEC(pPager, PGHDR_TO_DATA(pList), pList->pgno, 6);
1389 TRACE2("STORE %d\n", pList->pgno);
danielk19774adee202004-05-08 08:23:19 +00001390 rc = sqlite3OsWrite(&pPager->fd, PGHDR_TO_DATA(pList), SQLITE_PAGE_SIZE);
drh9eb9e262004-02-11 02:18:05 +00001391 CODEC(pPager, PGHDR_TO_DATA(pList), pList->pgno, 0);
drh2554f8b2003-01-22 01:26:44 +00001392 if( rc ) return rc;
1393 pList->dirty = 0;
1394 pList = pList->pDirty;
1395 }
1396 return SQLITE_OK;
1397}
1398
1399/*
1400** Collect every dirty page into a dirty list and
1401** return a pointer to the head of that list. All pages are
1402** collected even if they are still in use.
1403*/
1404static PgHdr *pager_get_all_dirty_pages(Pager *pPager){
1405 PgHdr *p, *pList;
1406 pList = 0;
1407 for(p=pPager->pAll; p; p=p->pNextAll){
1408 if( p->dirty ){
1409 p->pDirty = pList;
1410 pList = p;
1411 }
1412 }
1413 return pList;
1414}
1415
1416/*
drhd9b02572001-04-15 00:37:09 +00001417** Acquire a page.
1418**
drh58a11682001-11-10 13:51:08 +00001419** A read lock on the disk file is obtained when the first page is acquired.
drh5e00f6c2001-09-13 13:46:56 +00001420** This read lock is dropped when the last page is released.
drhd9b02572001-04-15 00:37:09 +00001421**
drh306dc212001-05-21 13:45:10 +00001422** A _get works for any page number greater than 0. If the database
1423** file is smaller than the requested page, then no actual disk
1424** read occurs and the memory image of the page is initialized to
1425** all zeros. The extra data appended to a page is always initialized
1426** to zeros the first time a page is loaded into memory.
1427**
drhd9b02572001-04-15 00:37:09 +00001428** The acquisition might fail for several reasons. In all cases,
1429** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +00001430**
drh3aac2dd2004-04-26 14:10:20 +00001431** See also sqlite3pager_lookup(). Both this routine and _lookup() attempt
drh7e3b0a02001-04-28 16:52:40 +00001432** to find a page in the in-memory cache first. If the page is not already
drh5e00f6c2001-09-13 13:46:56 +00001433** in memory, this routine goes to disk to read it in whereas _lookup()
drh7e3b0a02001-04-28 16:52:40 +00001434** just returns 0. This routine acquires a read-lock the first time it
1435** has to go to disk, and could also playback an old journal if necessary.
1436** Since _lookup() never goes to disk, it never has to deal with locks
1437** or journal files.
drhed7c8552001-04-11 14:29:21 +00001438*/
drh3aac2dd2004-04-26 14:10:20 +00001439int sqlite3pager_get(Pager *pPager, Pgno pgno, void **ppPage){
drhed7c8552001-04-11 14:29:21 +00001440 PgHdr *pPg;
drh8766c342002-11-09 00:33:15 +00001441 int rc;
drhed7c8552001-04-11 14:29:21 +00001442
drhd9b02572001-04-15 00:37:09 +00001443 /* Make sure we have not hit any critical errors.
1444 */
drh836faa42003-01-11 13:30:57 +00001445 assert( pPager!=0 );
1446 assert( pgno!=0 );
drh2e6d11b2003-04-25 15:37:57 +00001447 *ppPage = 0;
drhd9b02572001-04-15 00:37:09 +00001448 if( pPager->errMask & ~(PAGER_ERR_FULL) ){
1449 return pager_errcode(pPager);
1450 }
1451
drhed7c8552001-04-11 14:29:21 +00001452 /* If this is the first page accessed, then get a read lock
1453 ** on the database file.
1454 */
drhac69b052004-05-12 13:30:07 +00001455 if( pPager->nRef==0 && !pPager->memDb ){
danielk19774adee202004-05-08 08:23:19 +00001456 rc = sqlite3OsReadLock(&pPager->fd);
drh8766c342002-11-09 00:33:15 +00001457 if( rc!=SQLITE_OK ){
drh8766c342002-11-09 00:33:15 +00001458 return rc;
drhed7c8552001-04-11 14:29:21 +00001459 }
drhd9b02572001-04-15 00:37:09 +00001460 pPager->state = SQLITE_READLOCK;
drhed7c8552001-04-11 14:29:21 +00001461
1462 /* If a journal file exists, try to play it back.
1463 */
danielk19774adee202004-05-08 08:23:19 +00001464 if( pPager->useJournal && sqlite3OsFileExists(pPager->zJournal) ){
drhe2227f02003-06-14 11:42:57 +00001465 int rc;
drhed7c8552001-04-11 14:29:21 +00001466
drha7fcb052001-12-14 15:09:55 +00001467 /* Get a write lock on the database
1468 */
danielk19774adee202004-05-08 08:23:19 +00001469 rc = sqlite3OsWriteLock(&pPager->fd);
drha7fcb052001-12-14 15:09:55 +00001470 if( rc!=SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00001471 if( sqlite3OsUnlock(&pPager->fd)!=SQLITE_OK ){
drh8766c342002-11-09 00:33:15 +00001472 /* This should never happen! */
1473 rc = SQLITE_INTERNAL;
1474 }
drh8766c342002-11-09 00:33:15 +00001475 return rc;
drha7fcb052001-12-14 15:09:55 +00001476 }
1477 pPager->state = SQLITE_WRITELOCK;
1478
drhe2227f02003-06-14 11:42:57 +00001479 /* Open the journal for reading only. Return SQLITE_BUSY if
1480 ** we are unable to open the journal file.
drhf57b3392001-10-08 13:22:32 +00001481 **
drhe2227f02003-06-14 11:42:57 +00001482 ** The journal file does not need to be locked itself. The
1483 ** journal file is never open unless the main database file holds
1484 ** a write lock, so there is never any chance of two or more
1485 ** processes opening the journal at the same time.
drhed7c8552001-04-11 14:29:21 +00001486 */
danielk19774adee202004-05-08 08:23:19 +00001487 rc = sqlite3OsOpenReadOnly(pPager->zJournal, &pPager->jfd);
drha7fcb052001-12-14 15:09:55 +00001488 if( rc!=SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00001489 rc = sqlite3OsUnlock(&pPager->fd);
drha7fcb052001-12-14 15:09:55 +00001490 assert( rc==SQLITE_OK );
drhed7c8552001-04-11 14:29:21 +00001491 return SQLITE_BUSY;
1492 }
drha7fcb052001-12-14 15:09:55 +00001493 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00001494 pPager->journalStarted = 0;
drhed7c8552001-04-11 14:29:21 +00001495
1496 /* Playback and delete the journal. Drop the database write
1497 ** lock and reacquire the read lock.
1498 */
drh99ee3602003-02-16 19:13:36 +00001499 rc = pager_playback(pPager, 0);
drhd9b02572001-04-15 00:37:09 +00001500 if( rc!=SQLITE_OK ){
1501 return rc;
1502 }
drhed7c8552001-04-11 14:29:21 +00001503 }
1504 pPg = 0;
1505 }else{
1506 /* Search for page in cache */
drhd9b02572001-04-15 00:37:09 +00001507 pPg = pager_lookup(pPager, pgno);
drhac69b052004-05-12 13:30:07 +00001508 if( pPager->memDb && pPager->state==SQLITE_UNLOCK ){
1509 pPager->state = SQLITE_READLOCK;
1510 }
drhed7c8552001-04-11 14:29:21 +00001511 }
1512 if( pPg==0 ){
drhd9b02572001-04-15 00:37:09 +00001513 /* The requested page is not in the page cache. */
drhed7c8552001-04-11 14:29:21 +00001514 int h;
drh7e3b0a02001-04-28 16:52:40 +00001515 pPager->nMiss++;
drhac69b052004-05-12 13:30:07 +00001516 if( pPager->nPage<pPager->mxPage || pPager->pFirst==0 || pPager->memDb ){
drhed7c8552001-04-11 14:29:21 +00001517 /* Create a new page */
drhd0ba1932004-02-10 01:54:28 +00001518 pPg = sqliteMallocRaw( sizeof(*pPg) + SQLITE_PAGE_SIZE
drhac69b052004-05-12 13:30:07 +00001519 + sizeof(u32) + pPager->nExtra
1520 + pPager->memDb*sizeof(PgHistory) );
drhd9b02572001-04-15 00:37:09 +00001521 if( pPg==0 ){
drhd9b02572001-04-15 00:37:09 +00001522 pager_unwritelock(pPager);
1523 pPager->errMask |= PAGER_ERR_MEM;
1524 return SQLITE_NOMEM;
1525 }
drh8c1238a2003-01-02 14:43:55 +00001526 memset(pPg, 0, sizeof(*pPg));
drhac69b052004-05-12 13:30:07 +00001527 if( pPager->memDb ){
1528 memset(PGHDR_TO_HIST(pPg, pPager), 0, sizeof(PgHistory));
1529 }
drhed7c8552001-04-11 14:29:21 +00001530 pPg->pPager = pPager;
drhd9b02572001-04-15 00:37:09 +00001531 pPg->pNextAll = pPager->pAll;
drhd79caeb2001-04-15 02:27:24 +00001532 pPager->pAll = pPg;
drhd9b02572001-04-15 00:37:09 +00001533 pPager->nPage++;
drhed7c8552001-04-11 14:29:21 +00001534 }else{
drhdb48ee02003-01-16 13:42:43 +00001535 /* Find a page to recycle. Try to locate a page that does not
1536 ** require us to do an fsync() on the journal.
1537 */
drh341eae82003-01-21 02:39:36 +00001538 pPg = pPager->pFirstSynced;
drhb19a2bc2001-09-16 00:13:26 +00001539
drhdb48ee02003-01-16 13:42:43 +00001540 /* If we could not find a page that does not require an fsync()
1541 ** on the journal file then fsync the journal file. This is a
1542 ** very slow operation, so we work hard to avoid it. But sometimes
1543 ** it can't be helped.
drhb19a2bc2001-09-16 00:13:26 +00001544 */
drh603240c2002-03-05 01:11:12 +00001545 if( pPg==0 ){
drh34e79ce2004-02-08 06:05:46 +00001546 int rc = syncJournal(pPager);
drh50e5dad2001-09-15 00:57:28 +00001547 if( rc!=0 ){
drh3aac2dd2004-04-26 14:10:20 +00001548 sqlite3pager_rollback(pPager);
drh50e5dad2001-09-15 00:57:28 +00001549 return SQLITE_IOERR;
1550 }
1551 pPg = pPager->pFirst;
1552 }
drhd9b02572001-04-15 00:37:09 +00001553 assert( pPg->nRef==0 );
drhdb48ee02003-01-16 13:42:43 +00001554
1555 /* Write the page to the database file if it is dirty.
1556 */
1557 if( pPg->dirty ){
1558 assert( pPg->needSync==0 );
drh2554f8b2003-01-22 01:26:44 +00001559 pPg->pDirty = 0;
1560 rc = pager_write_pagelist( pPg );
drhdb48ee02003-01-16 13:42:43 +00001561 if( rc!=SQLITE_OK ){
drh3aac2dd2004-04-26 14:10:20 +00001562 sqlite3pager_rollback(pPager);
drhdb48ee02003-01-16 13:42:43 +00001563 return SQLITE_IOERR;
1564 }
drhdb48ee02003-01-16 13:42:43 +00001565 }
drh50e5dad2001-09-15 00:57:28 +00001566 assert( pPg->dirty==0 );
drhd9b02572001-04-15 00:37:09 +00001567
drhdb48ee02003-01-16 13:42:43 +00001568 /* If the page we are recycling is marked as alwaysRollback, then
drh193a6b42002-07-07 16:52:46 +00001569 ** set the global alwaysRollback flag, thus disabling the
1570 ** sqlite_dont_rollback() optimization for the rest of this transaction.
1571 ** It is necessary to do this because the page marked alwaysRollback
1572 ** might be reloaded at a later time but at that point we won't remember
1573 ** that is was marked alwaysRollback. This means that all pages must
1574 ** be marked as alwaysRollback from here on out.
1575 */
1576 if( pPg->alwaysRollback ){
1577 pPager->alwaysRollback = 1;
1578 }
1579
drhd9b02572001-04-15 00:37:09 +00001580 /* Unlink the old page from the free list and the hash table
1581 */
drhac69b052004-05-12 13:30:07 +00001582 unlinkPage(pPg);
drhd9b02572001-04-15 00:37:09 +00001583 pPager->nOvfl++;
drhed7c8552001-04-11 14:29:21 +00001584 }
1585 pPg->pgno = pgno;
drh1ab43002002-01-14 09:28:19 +00001586 if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){
danielk19774adee202004-05-08 08:23:19 +00001587 sqlite3CheckMemory(pPager->aInJournal, pgno/8);
drhdb48ee02003-01-16 13:42:43 +00001588 assert( pPager->journalOpen );
drh6019e162001-07-02 17:51:45 +00001589 pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0;
drhdb48ee02003-01-16 13:42:43 +00001590 pPg->needSync = 0;
drh6019e162001-07-02 17:51:45 +00001591 }else{
1592 pPg->inJournal = 0;
drhdb48ee02003-01-16 13:42:43 +00001593 pPg->needSync = 0;
drh6019e162001-07-02 17:51:45 +00001594 }
drhac69b052004-05-12 13:30:07 +00001595 if( pPager->aInStmt && (int)pgno<=pPager->stmtSize
1596 && (pPager->aInStmt[pgno/8] & (1<<(pgno&7)))!=0 ){
drh3aac2dd2004-04-26 14:10:20 +00001597 page_add_to_stmt_list(pPg);
drhfa86c412002-02-02 15:01:15 +00001598 }else{
drh3aac2dd2004-04-26 14:10:20 +00001599 page_remove_from_stmt_list(pPg);
drhfa86c412002-02-02 15:01:15 +00001600 }
drhed7c8552001-04-11 14:29:21 +00001601 pPg->dirty = 0;
1602 pPg->nRef = 1;
drhdd793422001-06-28 01:54:48 +00001603 REFINFO(pPg);
drhd9b02572001-04-15 00:37:09 +00001604 pPager->nRef++;
1605 h = pager_hash(pgno);
drhed7c8552001-04-11 14:29:21 +00001606 pPg->pNextHash = pPager->aHash[h];
1607 pPager->aHash[h] = pPg;
1608 if( pPg->pNextHash ){
1609 assert( pPg->pNextHash->pPrevHash==0 );
1610 pPg->pNextHash->pPrevHash = pPg;
1611 }
drh2e6d11b2003-04-25 15:37:57 +00001612 if( pPager->nExtra>0 ){
1613 memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra);
1614 }
drh3aac2dd2004-04-26 14:10:20 +00001615 if( pPager->dbSize<0 ) sqlite3pager_pagecount(pPager);
drh2e6d11b2003-04-25 15:37:57 +00001616 if( pPager->errMask!=0 ){
drh3aac2dd2004-04-26 14:10:20 +00001617 sqlite3pager_unref(PGHDR_TO_DATA(pPg));
drh2e6d11b2003-04-25 15:37:57 +00001618 rc = pager_errcode(pPager);
1619 return rc;
1620 }
drh1ab43002002-01-14 09:28:19 +00001621 if( pPager->dbSize<(int)pgno ){
drhd0ba1932004-02-10 01:54:28 +00001622 memset(PGHDR_TO_DATA(pPg), 0, SQLITE_PAGE_SIZE);
drh306dc212001-05-21 13:45:10 +00001623 }else{
drh81a20f22001-10-12 17:30:04 +00001624 int rc;
drhac69b052004-05-12 13:30:07 +00001625 assert( pPager->memDb==0 );
danielk19774adee202004-05-08 08:23:19 +00001626 sqlite3OsSeek(&pPager->fd, (pgno-1)*(off_t)SQLITE_PAGE_SIZE);
1627 rc = sqlite3OsRead(&pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE);
drh9eb9e262004-02-11 02:18:05 +00001628 TRACE2("FETCH %d\n", pPg->pgno);
1629 CODEC(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3);
drh81a20f22001-10-12 17:30:04 +00001630 if( rc!=SQLITE_OK ){
drh28be87c2002-11-05 23:03:02 +00001631 off_t fileSize;
danielk19774adee202004-05-08 08:23:19 +00001632 if( sqlite3OsFileSize(&pPager->fd,&fileSize)!=SQLITE_OK
drhd0ba1932004-02-10 01:54:28 +00001633 || fileSize>=pgno*SQLITE_PAGE_SIZE ){
drh3aac2dd2004-04-26 14:10:20 +00001634 sqlite3pager_unref(PGHDR_TO_DATA(pPg));
drh4e371ee2002-09-05 16:08:27 +00001635 return rc;
1636 }else{
drhd0ba1932004-02-10 01:54:28 +00001637 memset(PGHDR_TO_DATA(pPg), 0, SQLITE_PAGE_SIZE);
drh4e371ee2002-09-05 16:08:27 +00001638 }
drh81a20f22001-10-12 17:30:04 +00001639 }
drh306dc212001-05-21 13:45:10 +00001640 }
drhed7c8552001-04-11 14:29:21 +00001641 }else{
drhd9b02572001-04-15 00:37:09 +00001642 /* The requested page is in the page cache. */
drh7e3b0a02001-04-28 16:52:40 +00001643 pPager->nHit++;
drhdf0b3b02001-06-23 11:36:20 +00001644 page_ref(pPg);
drhed7c8552001-04-11 14:29:21 +00001645 }
1646 *ppPage = PGHDR_TO_DATA(pPg);
1647 return SQLITE_OK;
1648}
1649
1650/*
drh7e3b0a02001-04-28 16:52:40 +00001651** Acquire a page if it is already in the in-memory cache. Do
1652** not read the page from disk. Return a pointer to the page,
1653** or 0 if the page is not in cache.
1654**
drh3aac2dd2004-04-26 14:10:20 +00001655** See also sqlite3pager_get(). The difference between this routine
1656** and sqlite3pager_get() is that _get() will go to the disk and read
drh7e3b0a02001-04-28 16:52:40 +00001657** in the page if the page is not already in cache. This routine
drh5e00f6c2001-09-13 13:46:56 +00001658** returns NULL if the page is not in cache or if a disk I/O error
1659** has ever happened.
drh7e3b0a02001-04-28 16:52:40 +00001660*/
drh3aac2dd2004-04-26 14:10:20 +00001661void *sqlite3pager_lookup(Pager *pPager, Pgno pgno){
drh7e3b0a02001-04-28 16:52:40 +00001662 PgHdr *pPg;
1663
drh836faa42003-01-11 13:30:57 +00001664 assert( pPager!=0 );
1665 assert( pgno!=0 );
drh7e3b0a02001-04-28 16:52:40 +00001666 if( pPager->errMask & ~(PAGER_ERR_FULL) ){
1667 return 0;
1668 }
drh7e3b0a02001-04-28 16:52:40 +00001669 pPg = pager_lookup(pPager, pgno);
1670 if( pPg==0 ) return 0;
drhdf0b3b02001-06-23 11:36:20 +00001671 page_ref(pPg);
drh7e3b0a02001-04-28 16:52:40 +00001672 return PGHDR_TO_DATA(pPg);
1673}
1674
1675/*
drhed7c8552001-04-11 14:29:21 +00001676** Release a page.
1677**
1678** If the number of references to the page drop to zero, then the
1679** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +00001680** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +00001681** removed.
1682*/
drh3aac2dd2004-04-26 14:10:20 +00001683int sqlite3pager_unref(void *pData){
drhed7c8552001-04-11 14:29:21 +00001684 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +00001685
1686 /* Decrement the reference count for this page
1687 */
drhed7c8552001-04-11 14:29:21 +00001688 pPg = DATA_TO_PGHDR(pData);
1689 assert( pPg->nRef>0 );
drhed7c8552001-04-11 14:29:21 +00001690 pPg->nRef--;
drhdd793422001-06-28 01:54:48 +00001691 REFINFO(pPg);
drhd9b02572001-04-15 00:37:09 +00001692
drh72f82862001-05-24 21:06:34 +00001693 /* When the number of references to a page reach 0, call the
1694 ** destructor and add the page to the freelist.
drhd9b02572001-04-15 00:37:09 +00001695 */
drhed7c8552001-04-11 14:29:21 +00001696 if( pPg->nRef==0 ){
drh1eaa2692001-09-18 02:02:23 +00001697 Pager *pPager;
1698 pPager = pPg->pPager;
drhd9b02572001-04-15 00:37:09 +00001699 pPg->pNextFree = 0;
1700 pPg->pPrevFree = pPager->pLast;
drhed7c8552001-04-11 14:29:21 +00001701 pPager->pLast = pPg;
drhd9b02572001-04-15 00:37:09 +00001702 if( pPg->pPrevFree ){
1703 pPg->pPrevFree->pNextFree = pPg;
drhed7c8552001-04-11 14:29:21 +00001704 }else{
1705 pPager->pFirst = pPg;
1706 }
drh341eae82003-01-21 02:39:36 +00001707 if( pPg->needSync==0 && pPager->pFirstSynced==0 ){
1708 pPager->pFirstSynced = pPg;
1709 }
drh72f82862001-05-24 21:06:34 +00001710 if( pPager->xDestructor ){
1711 pPager->xDestructor(pData);
1712 }
drhd9b02572001-04-15 00:37:09 +00001713
1714 /* When all pages reach the freelist, drop the read lock from
1715 ** the database file.
1716 */
1717 pPager->nRef--;
1718 assert( pPager->nRef>=0 );
drhac69b052004-05-12 13:30:07 +00001719 if( pPager->nRef==0 && !pPager->memDb ){
drhd9b02572001-04-15 00:37:09 +00001720 pager_reset(pPager);
1721 }
drhed7c8552001-04-11 14:29:21 +00001722 }
drhd9b02572001-04-15 00:37:09 +00001723 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +00001724}
1725
1726/*
drhda47d772002-12-02 04:25:19 +00001727** Create a journal file for pPager. There should already be a write
1728** lock on the database file when this routine is called.
1729**
1730** Return SQLITE_OK if everything. Return an error code and release the
1731** write lock if anything goes wrong.
1732*/
1733static int pager_open_journal(Pager *pPager){
1734 int rc;
1735 assert( pPager->state==SQLITE_WRITELOCK );
1736 assert( pPager->journalOpen==0 );
1737 assert( pPager->useJournal );
drh3aac2dd2004-04-26 14:10:20 +00001738 sqlite3pager_pagecount(pPager);
drhda47d772002-12-02 04:25:19 +00001739 pPager->aInJournal = sqliteMalloc( pPager->dbSize/8 + 1 );
1740 if( pPager->aInJournal==0 ){
danielk19774adee202004-05-08 08:23:19 +00001741 sqlite3OsReadLock(&pPager->fd);
drhda47d772002-12-02 04:25:19 +00001742 pPager->state = SQLITE_READLOCK;
1743 return SQLITE_NOMEM;
1744 }
danielk19774adee202004-05-08 08:23:19 +00001745 rc = sqlite3OsOpenExclusive(pPager->zJournal, &pPager->jfd,pPager->tempFile);
drhda47d772002-12-02 04:25:19 +00001746 if( rc!=SQLITE_OK ){
1747 sqliteFree(pPager->aInJournal);
1748 pPager->aInJournal = 0;
danielk19774adee202004-05-08 08:23:19 +00001749 sqlite3OsReadLock(&pPager->fd);
drhda47d772002-12-02 04:25:19 +00001750 pPager->state = SQLITE_READLOCK;
1751 return SQLITE_CANTOPEN;
1752 }
danielk19774adee202004-05-08 08:23:19 +00001753 sqlite3OsOpenDirectory(pPager->zDirectory, &pPager->jfd);
drhda47d772002-12-02 04:25:19 +00001754 pPager->journalOpen = 1;
drhdb48ee02003-01-16 13:42:43 +00001755 pPager->journalStarted = 0;
drhda47d772002-12-02 04:25:19 +00001756 pPager->needSync = 0;
1757 pPager->alwaysRollback = 0;
drh968af522003-02-11 14:55:40 +00001758 pPager->nRec = 0;
drh2e6d11b2003-04-25 15:37:57 +00001759 if( pPager->errMask!=0 ){
1760 rc = pager_errcode(pPager);
1761 return rc;
1762 }
drhda47d772002-12-02 04:25:19 +00001763 pPager->origDbSize = pPager->dbSize;
drh968af522003-02-11 14:55:40 +00001764 if( journal_format==JOURNAL_FORMAT_3 ){
danielk19774adee202004-05-08 08:23:19 +00001765 rc = sqlite3OsWrite(&pPager->jfd, aJournalMagic3, sizeof(aJournalMagic3));
drh968af522003-02-11 14:55:40 +00001766 if( rc==SQLITE_OK ){
drh4303fee2003-02-15 23:09:17 +00001767 rc = write32bits(&pPager->jfd, pPager->noSync ? 0xffffffff : 0);
drh968af522003-02-11 14:55:40 +00001768 }
1769 if( rc==SQLITE_OK ){
danielk19774adee202004-05-08 08:23:19 +00001770 sqlite3Randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
drh968af522003-02-11 14:55:40 +00001771 rc = write32bits(&pPager->jfd, pPager->cksumInit);
1772 }
1773 }else if( journal_format==JOURNAL_FORMAT_2 ){
danielk19774adee202004-05-08 08:23:19 +00001774 rc = sqlite3OsWrite(&pPager->jfd, aJournalMagic2, sizeof(aJournalMagic2));
drhda47d772002-12-02 04:25:19 +00001775 }else{
drh968af522003-02-11 14:55:40 +00001776 assert( journal_format==JOURNAL_FORMAT_1 );
danielk19774adee202004-05-08 08:23:19 +00001777 rc = sqlite3OsWrite(&pPager->jfd, aJournalMagic1, sizeof(aJournalMagic1));
drhda47d772002-12-02 04:25:19 +00001778 }
1779 if( rc==SQLITE_OK ){
1780 rc = write32bits(&pPager->jfd, pPager->dbSize);
1781 }
drhac69b052004-05-12 13:30:07 +00001782 if( pPager->stmtAutoopen && rc==SQLITE_OK ){
drh3aac2dd2004-04-26 14:10:20 +00001783 rc = sqlite3pager_stmt_begin(pPager);
drhda47d772002-12-02 04:25:19 +00001784 }
1785 if( rc!=SQLITE_OK ){
1786 rc = pager_unwritelock(pPager);
1787 if( rc==SQLITE_OK ){
1788 rc = SQLITE_FULL;
1789 }
1790 }
1791 return rc;
1792}
1793
1794/*
drh4b845d72002-03-05 12:41:19 +00001795** Acquire a write-lock on the database. The lock is removed when
1796** the any of the following happen:
1797**
drh3aac2dd2004-04-26 14:10:20 +00001798** * sqlite3pager_commit() is called.
1799** * sqlite3pager_rollback() is called.
1800** * sqlite3pager_close() is called.
1801** * sqlite3pager_unref() is called to on every outstanding page.
drh4b845d72002-03-05 12:41:19 +00001802**
1803** The parameter to this routine is a pointer to any open page of the
1804** database file. Nothing changes about the page - it is used merely
1805** to acquire a pointer to the Pager structure and as proof that there
1806** is already a read-lock on the database.
1807**
drhda47d772002-12-02 04:25:19 +00001808** A journal file is opened if this is not a temporary file. For
1809** temporary files, the opening of the journal file is deferred until
1810** there is an actual need to write to the journal.
1811**
drh4b845d72002-03-05 12:41:19 +00001812** If the database is already write-locked, this routine is a no-op.
1813*/
drh3aac2dd2004-04-26 14:10:20 +00001814int sqlite3pager_begin(void *pData){
drh4b845d72002-03-05 12:41:19 +00001815 PgHdr *pPg = DATA_TO_PGHDR(pData);
1816 Pager *pPager = pPg->pPager;
1817 int rc = SQLITE_OK;
1818 assert( pPg->nRef>0 );
1819 assert( pPager->state!=SQLITE_UNLOCK );
1820 if( pPager->state==SQLITE_READLOCK ){
1821 assert( pPager->aInJournal==0 );
drhac69b052004-05-12 13:30:07 +00001822 if( pPager->memDb ){
1823 pPager->state = SQLITE_WRITELOCK;
1824 pPager->origDbSize = pPager->dbSize;
1825 }else{
1826 rc = sqlite3OsWriteLock(&pPager->fd);
1827 if( rc!=SQLITE_OK ){
1828 return rc;
1829 }
1830 pPager->state = SQLITE_WRITELOCK;
1831 pPager->dirtyFile = 0;
1832 TRACE1("TRANSACTION\n");
1833 if( pPager->useJournal && !pPager->tempFile ){
1834 rc = pager_open_journal(pPager);
1835 }
drh4b845d72002-03-05 12:41:19 +00001836 }
1837 }
1838 return rc;
1839}
1840
1841/*
drhed7c8552001-04-11 14:29:21 +00001842** Mark a data page as writeable. The page is written into the journal
1843** if it is not there already. This routine must be called before making
1844** changes to a page.
1845**
1846** The first time this routine is called, the pager creates a new
1847** journal and acquires a write lock on the database. If the write
1848** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +00001849** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +00001850** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +00001851**
1852** If the journal file could not be written because the disk is full,
1853** then this routine returns SQLITE_FULL and does an immediate rollback.
1854** All subsequent write attempts also return SQLITE_FULL until there
drh3aac2dd2004-04-26 14:10:20 +00001855** is a call to sqlite3pager_commit() or sqlite3pager_rollback() to
drhd9b02572001-04-15 00:37:09 +00001856** reset.
drhed7c8552001-04-11 14:29:21 +00001857*/
drh3aac2dd2004-04-26 14:10:20 +00001858int sqlite3pager_write(void *pData){
drh69688d52001-04-14 16:38:23 +00001859 PgHdr *pPg = DATA_TO_PGHDR(pData);
1860 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +00001861 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +00001862
drh6446c4d2001-12-15 14:22:18 +00001863 /* Check for errors
1864 */
drhd9b02572001-04-15 00:37:09 +00001865 if( pPager->errMask ){
1866 return pager_errcode(pPager);
1867 }
drh5e00f6c2001-09-13 13:46:56 +00001868 if( pPager->readOnly ){
1869 return SQLITE_PERM;
1870 }
drh6446c4d2001-12-15 14:22:18 +00001871
1872 /* Mark the page as dirty. If the page has already been written
1873 ** to the journal then we can return right away.
1874 */
drhd9b02572001-04-15 00:37:09 +00001875 pPg->dirty = 1;
drhac69b052004-05-12 13:30:07 +00001876 if( pPg->inJournal && (pPg->inStmt || pPager->stmtInUse==0) ){
drha1680452002-04-18 01:56:57 +00001877 pPager->dirtyFile = 1;
drhfa86c412002-02-02 15:01:15 +00001878 return SQLITE_OK;
1879 }
drh6446c4d2001-12-15 14:22:18 +00001880
1881 /* If we get this far, it means that the page needs to be
drhfa86c412002-02-02 15:01:15 +00001882 ** written to the transaction journal or the ckeckpoint journal
1883 ** or both.
1884 **
1885 ** First check to see that the transaction journal exists and
1886 ** create it if it does not.
drh6446c4d2001-12-15 14:22:18 +00001887 */
drhd9b02572001-04-15 00:37:09 +00001888 assert( pPager->state!=SQLITE_UNLOCK );
drh3aac2dd2004-04-26 14:10:20 +00001889 rc = sqlite3pager_begin(pData);
drhda47d772002-12-02 04:25:19 +00001890 if( rc!=SQLITE_OK ){
1891 return rc;
1892 }
drhd9b02572001-04-15 00:37:09 +00001893 assert( pPager->state==SQLITE_WRITELOCK );
drhda47d772002-12-02 04:25:19 +00001894 if( !pPager->journalOpen && pPager->useJournal ){
1895 rc = pager_open_journal(pPager);
1896 if( rc!=SQLITE_OK ) return rc;
1897 }
1898 assert( pPager->journalOpen || !pPager->useJournal );
1899 pPager->dirtyFile = 1;
drh6446c4d2001-12-15 14:22:18 +00001900
drhfa86c412002-02-02 15:01:15 +00001901 /* The transaction journal now exists and we have a write lock on the
1902 ** main database file. Write the current page to the transaction
1903 ** journal if it is not there already.
drh6446c4d2001-12-15 14:22:18 +00001904 */
drhac69b052004-05-12 13:30:07 +00001905 if( !pPg->inJournal && (pPager->useJournal || pPager->memDb) ){
drhdb48ee02003-01-16 13:42:43 +00001906 if( (int)pPg->pgno <= pPager->origDbSize ){
drh968af522003-02-11 14:55:40 +00001907 int szPg;
1908 u32 saved;
drhac69b052004-05-12 13:30:07 +00001909 if( pPager->memDb ){
1910 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
1911 TRACE2("JOURNAL %d\n", pPg->pgno);
1912 assert( pHist->pOrig==0 );
1913 pHist->pOrig = sqliteMallocRaw( pPager->pageSize );
1914 if( pHist->pOrig ){
1915 memcpy(pHist->pOrig, PGHDR_TO_DATA(pPg), pPager->pageSize);
1916 }
1917 pPg->inJournal = 1;
1918 }else {
1919 if( journal_format>=JOURNAL_FORMAT_3 ){
1920 u32 cksum = pager_cksum(pPager, pPg->pgno, pData);
1921 saved = *(u32*)PGHDR_TO_EXTRA(pPg);
1922 store32bits(cksum, pPg, SQLITE_PAGE_SIZE);
1923 szPg = SQLITE_PAGE_SIZE+8;
1924 }else{
1925 szPg = SQLITE_PAGE_SIZE+4;
1926 }
1927 store32bits(pPg->pgno, pPg, -4);
1928 CODEC(pPager, pData, pPg->pgno, 7);
1929 rc = sqlite3OsWrite(&pPager->jfd, &((char*)pData)[-4], szPg);
1930 TRACE3("JOURNAL %d %d\n", pPg->pgno, pPg->needSync);
1931 CODEC(pPager, pData, pPg->pgno, 0);
1932 if( journal_format>=JOURNAL_FORMAT_3 ){
1933 *(u32*)PGHDR_TO_EXTRA(pPg) = saved;
1934 }
1935 if( rc!=SQLITE_OK ){
1936 sqlite3pager_rollback(pPager);
1937 pPager->errMask |= PAGER_ERR_FULL;
1938 return rc;
1939 }
1940 pPager->nRec++;
1941 assert( pPager->aInJournal!=0 );
1942 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
1943 pPg->needSync = !pPager->noSync;
1944 pPg->inJournal = 1;
1945 if( pPager->stmtInUse ){
1946 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
1947 page_add_to_stmt_list(pPg);
1948 }
drhdb48ee02003-01-16 13:42:43 +00001949 }
drhdb48ee02003-01-16 13:42:43 +00001950 }else{
1951 pPg->needSync = !pPager->journalStarted && !pPager->noSync;
1952 TRACE3("APPEND %d %d\n", pPg->pgno, pPg->needSync);
drhd9b02572001-04-15 00:37:09 +00001953 }
drhdb48ee02003-01-16 13:42:43 +00001954 if( pPg->needSync ){
1955 pPager->needSync = 1;
drhfa86c412002-02-02 15:01:15 +00001956 }
drh69688d52001-04-14 16:38:23 +00001957 }
drh6446c4d2001-12-15 14:22:18 +00001958
drhac69b052004-05-12 13:30:07 +00001959 /* If the statement journal is open and the page is not in it,
1960 ** then write the current page to the statement journal. Note that
1961 ** the statement journal always uses the simplier format 2 that lacks
1962 ** checksums. The header is also omitted from the statement journal.
drh6446c4d2001-12-15 14:22:18 +00001963 */
drhac69b052004-05-12 13:30:07 +00001964 if( pPager->stmtInUse && !pPg->inStmt && (int)pPg->pgno<=pPager->stmtSize ){
drh1e336b42002-02-14 12:50:33 +00001965 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
drhac69b052004-05-12 13:30:07 +00001966 if( pPager->memDb ){
1967 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
1968 assert( pHist->pStmt==0 );
1969 pHist->pStmt = sqliteMallocRaw( pPager->pageSize );
1970 if( pHist->pStmt ){
1971 memcpy(pHist->pStmt, PGHDR_TO_DATA(pPg), pPager->pageSize);
1972 }
1973 TRACE2("STMT-JOURNAL %d\n", pPg->pgno);
1974 }else{
1975 store32bits(pPg->pgno, pPg, -4);
1976 CODEC(pPager, pData, pPg->pgno, 7);
1977 rc = sqlite3OsWrite(&pPager->stfd, ((char*)pData)-4, SQLITE_PAGE_SIZE+4);
1978 TRACE2("STMT-JOURNAL %d\n", pPg->pgno);
1979 CODEC(pPager, pData, pPg->pgno, 0);
1980 if( rc!=SQLITE_OK ){
1981 sqlite3pager_rollback(pPager);
1982 pPager->errMask |= PAGER_ERR_FULL;
1983 return rc;
1984 }
1985 pPager->stmtNRec++;
1986 assert( pPager->aInStmt!=0 );
1987 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drhfa86c412002-02-02 15:01:15 +00001988 }
drh3aac2dd2004-04-26 14:10:20 +00001989 page_add_to_stmt_list(pPg);
drhfa86c412002-02-02 15:01:15 +00001990 }
1991
1992 /* Update the database size and return.
1993 */
drh1ab43002002-01-14 09:28:19 +00001994 if( pPager->dbSize<(int)pPg->pgno ){
drh306dc212001-05-21 13:45:10 +00001995 pPager->dbSize = pPg->pgno;
1996 }
drh69688d52001-04-14 16:38:23 +00001997 return rc;
drhed7c8552001-04-11 14:29:21 +00001998}
1999
2000/*
drhaacc5432002-01-06 17:07:40 +00002001** Return TRUE if the page given in the argument was previously passed
drh3aac2dd2004-04-26 14:10:20 +00002002** to sqlite3pager_write(). In other words, return TRUE if it is ok
drh6019e162001-07-02 17:51:45 +00002003** to change the content of the page.
2004*/
drh3aac2dd2004-04-26 14:10:20 +00002005int sqlite3pager_iswriteable(void *pData){
drh6019e162001-07-02 17:51:45 +00002006 PgHdr *pPg = DATA_TO_PGHDR(pData);
2007 return pPg->dirty;
2008}
2009
2010/*
drh001bbcb2003-03-19 03:14:00 +00002011** Replace the content of a single page with the information in the third
2012** argument.
2013*/
drh3aac2dd2004-04-26 14:10:20 +00002014int sqlite3pager_overwrite(Pager *pPager, Pgno pgno, void *pData){
drh001bbcb2003-03-19 03:14:00 +00002015 void *pPage;
2016 int rc;
2017
drh3aac2dd2004-04-26 14:10:20 +00002018 rc = sqlite3pager_get(pPager, pgno, &pPage);
drh001bbcb2003-03-19 03:14:00 +00002019 if( rc==SQLITE_OK ){
drh3aac2dd2004-04-26 14:10:20 +00002020 rc = sqlite3pager_write(pPage);
drh001bbcb2003-03-19 03:14:00 +00002021 if( rc==SQLITE_OK ){
drhd0ba1932004-02-10 01:54:28 +00002022 memcpy(pPage, pData, SQLITE_PAGE_SIZE);
drh001bbcb2003-03-19 03:14:00 +00002023 }
drh3aac2dd2004-04-26 14:10:20 +00002024 sqlite3pager_unref(pPage);
drh001bbcb2003-03-19 03:14:00 +00002025 }
2026 return rc;
2027}
2028
2029/*
drh30e58752002-03-02 20:41:57 +00002030** A call to this routine tells the pager that it is not necessary to
2031** write the information on page "pgno" back to the disk, even though
2032** that page might be marked as dirty.
2033**
2034** The overlying software layer calls this routine when all of the data
2035** on the given page is unused. The pager marks the page as clean so
2036** that it does not get written to disk.
2037**
2038** Tests show that this optimization, together with the
drh3aac2dd2004-04-26 14:10:20 +00002039** sqlite3pager_dont_rollback() below, more than double the speed
drh30e58752002-03-02 20:41:57 +00002040** of large INSERT operations and quadruple the speed of large DELETEs.
drh8e298f92002-07-06 16:28:47 +00002041**
2042** When this routine is called, set the alwaysRollback flag to true.
drh3aac2dd2004-04-26 14:10:20 +00002043** Subsequent calls to sqlite3pager_dont_rollback() for the same page
drh8e298f92002-07-06 16:28:47 +00002044** will thereafter be ignored. This is necessary to avoid a problem
2045** where a page with data is added to the freelist during one part of
2046** a transaction then removed from the freelist during a later part
2047** of the same transaction and reused for some other purpose. When it
2048** is first added to the freelist, this routine is called. When reused,
2049** the dont_rollback() routine is called. But because the page contains
2050** critical data, we still need to be sure it gets rolled back in spite
2051** of the dont_rollback() call.
drh30e58752002-03-02 20:41:57 +00002052*/
drh3aac2dd2004-04-26 14:10:20 +00002053void sqlite3pager_dont_write(Pager *pPager, Pgno pgno){
drh30e58752002-03-02 20:41:57 +00002054 PgHdr *pPg;
drh8e298f92002-07-06 16:28:47 +00002055
drh30e58752002-03-02 20:41:57 +00002056 pPg = pager_lookup(pPager, pgno);
drh8e298f92002-07-06 16:28:47 +00002057 pPg->alwaysRollback = 1;
drh30e58752002-03-02 20:41:57 +00002058 if( pPg && pPg->dirty ){
drh8124a302002-06-25 14:43:57 +00002059 if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
2060 /* If this pages is the last page in the file and the file has grown
2061 ** during the current transaction, then do NOT mark the page as clean.
2062 ** When the database file grows, we must make sure that the last page
2063 ** gets written at least once so that the disk file will be the correct
2064 ** size. If you do not write this page and the size of the file
2065 ** on the disk ends up being too small, that can lead to database
2066 ** corruption during the next transaction.
2067 */
2068 }else{
drhdb48ee02003-01-16 13:42:43 +00002069 TRACE2("DONT_WRITE %d\n", pgno);
drh8124a302002-06-25 14:43:57 +00002070 pPg->dirty = 0;
2071 }
drh30e58752002-03-02 20:41:57 +00002072 }
2073}
2074
2075/*
2076** A call to this routine tells the pager that if a rollback occurs,
2077** it is not necessary to restore the data on the given page. This
2078** means that the pager does not have to record the given page in the
2079** rollback journal.
2080*/
drh3aac2dd2004-04-26 14:10:20 +00002081void sqlite3pager_dont_rollback(void *pData){
drh30e58752002-03-02 20:41:57 +00002082 PgHdr *pPg = DATA_TO_PGHDR(pData);
2083 Pager *pPager = pPg->pPager;
2084
2085 if( pPager->state!=SQLITE_WRITELOCK || pPager->journalOpen==0 ) return;
drhac69b052004-05-12 13:30:07 +00002086 if( pPg->alwaysRollback || pPager->alwaysRollback || pPager->memDb ) return;
drh30e58752002-03-02 20:41:57 +00002087 if( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ){
2088 assert( pPager->aInJournal!=0 );
2089 pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
2090 pPg->inJournal = 1;
drhac69b052004-05-12 13:30:07 +00002091 if( pPager->stmtInUse ){
2092 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh3aac2dd2004-04-26 14:10:20 +00002093 page_add_to_stmt_list(pPg);
drh30e58752002-03-02 20:41:57 +00002094 }
drhdb48ee02003-01-16 13:42:43 +00002095 TRACE2("DONT_ROLLBACK %d\n", pPg->pgno);
drh30e58752002-03-02 20:41:57 +00002096 }
drhac69b052004-05-12 13:30:07 +00002097 if( pPager->stmtInUse && !pPg->inStmt && (int)pPg->pgno<=pPager->stmtSize ){
drh30e58752002-03-02 20:41:57 +00002098 assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
drhac69b052004-05-12 13:30:07 +00002099 assert( pPager->aInStmt!=0 );
2100 pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
drh3aac2dd2004-04-26 14:10:20 +00002101 page_add_to_stmt_list(pPg);
drh30e58752002-03-02 20:41:57 +00002102 }
2103}
2104
drhac69b052004-05-12 13:30:07 +00002105
2106/*
2107** Clear a PgHistory block
2108*/
2109static void clearHistory(PgHistory *pHist){
2110 sqliteFree(pHist->pOrig);
2111 sqliteFree(pHist->pStmt);
2112 pHist->pOrig = 0;
2113 pHist->pStmt = 0;
2114}
2115
drh30e58752002-03-02 20:41:57 +00002116/*
drhed7c8552001-04-11 14:29:21 +00002117** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +00002118**
2119** If the commit fails for any reason, a rollback attempt is made
2120** and an error code is returned. If the commit worked, SQLITE_OK
2121** is returned.
drhed7c8552001-04-11 14:29:21 +00002122*/
drh3aac2dd2004-04-26 14:10:20 +00002123int sqlite3pager_commit(Pager *pPager){
drha1b351a2001-09-14 16:42:12 +00002124 int rc;
drhed7c8552001-04-11 14:29:21 +00002125 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +00002126
2127 if( pPager->errMask==PAGER_ERR_FULL ){
drh3aac2dd2004-04-26 14:10:20 +00002128 rc = sqlite3pager_rollback(pPager);
drh4e371ee2002-09-05 16:08:27 +00002129 if( rc==SQLITE_OK ){
2130 rc = SQLITE_FULL;
2131 }
drhd9b02572001-04-15 00:37:09 +00002132 return rc;
2133 }
2134 if( pPager->errMask!=0 ){
2135 rc = pager_errcode(pPager);
2136 return rc;
2137 }
2138 if( pPager->state!=SQLITE_WRITELOCK ){
2139 return SQLITE_ERROR;
2140 }
drhdb48ee02003-01-16 13:42:43 +00002141 TRACE1("COMMIT\n");
drhac69b052004-05-12 13:30:07 +00002142 if( pPager->memDb ){
2143 pPg = pager_get_all_dirty_pages(pPager);
2144 while( pPg ){
2145 clearHistory(PGHDR_TO_HIST(pPg, pPager));
2146 pPg->dirty = 0;
2147 pPg->inJournal = 0;
2148 pPg->inStmt = 0;
2149 pPg->pPrevStmt = pPg->pNextStmt = 0;
2150 pPg = pPg->pDirty;
2151 }
2152 pPager->pStmt = 0;
2153 pPager->state = SQLITE_READLOCK;
2154 return SQLITE_OK;
2155 }
drha1680452002-04-18 01:56:57 +00002156 if( pPager->dirtyFile==0 ){
danielk19774adee202004-05-08 08:23:19 +00002157 /* Exit early (without doing the time-consuming sqlite3OsSync() calls)
drha1680452002-04-18 01:56:57 +00002158 ** if there have been no changes to the database file. */
drh341eae82003-01-21 02:39:36 +00002159 assert( pPager->needSync==0 );
drha1680452002-04-18 01:56:57 +00002160 rc = pager_unwritelock(pPager);
2161 pPager->dbSize = -1;
2162 return rc;
2163 }
drhda47d772002-12-02 04:25:19 +00002164 assert( pPager->journalOpen );
drh34e79ce2004-02-08 06:05:46 +00002165 rc = syncJournal(pPager);
drh240c5792004-02-08 00:40:52 +00002166 if( rc!=SQLITE_OK ){
drhd9b02572001-04-15 00:37:09 +00002167 goto commit_abort;
drhed7c8552001-04-11 14:29:21 +00002168 }
drh2554f8b2003-01-22 01:26:44 +00002169 pPg = pager_get_all_dirty_pages(pPager);
2170 if( pPg ){
2171 rc = pager_write_pagelist(pPg);
danielk19774adee202004-05-08 08:23:19 +00002172 if( rc || (!pPager->noSync && sqlite3OsSync(&pPager->fd)!=SQLITE_OK) ){
drh2554f8b2003-01-22 01:26:44 +00002173 goto commit_abort;
2174 }
drh603240c2002-03-05 01:11:12 +00002175 }
drhd9b02572001-04-15 00:37:09 +00002176 rc = pager_unwritelock(pPager);
2177 pPager->dbSize = -1;
2178 return rc;
2179
2180 /* Jump here if anything goes wrong during the commit process.
2181 */
2182commit_abort:
drh3aac2dd2004-04-26 14:10:20 +00002183 rc = sqlite3pager_rollback(pPager);
drhd9b02572001-04-15 00:37:09 +00002184 if( rc==SQLITE_OK ){
2185 rc = SQLITE_FULL;
drhed7c8552001-04-11 14:29:21 +00002186 }
drhed7c8552001-04-11 14:29:21 +00002187 return rc;
2188}
2189
2190/*
2191** Rollback all changes. The database falls back to read-only mode.
2192** All in-memory cache pages revert to their original data contents.
2193** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00002194**
2195** This routine cannot fail unless some other process is not following
2196** the correct locking protocol (SQLITE_PROTOCOL) or unless some other
2197** process is writing trash into the journal file (SQLITE_CORRUPT) or
2198** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
2199** codes are returned for all these occasions. Otherwise,
2200** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00002201*/
drh3aac2dd2004-04-26 14:10:20 +00002202int sqlite3pager_rollback(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00002203 int rc;
drhdb48ee02003-01-16 13:42:43 +00002204 TRACE1("ROLLBACK\n");
drhac69b052004-05-12 13:30:07 +00002205 if( pPager->memDb ){
2206 PgHdr *p;
2207 for(p=pPager->pAll; p; p=p->pNextAll){
2208 PgHistory *pHist;
2209 if( !p->dirty ) continue;
2210 pHist = PGHDR_TO_HIST(p, pPager);
2211 if( pHist->pOrig ){
2212 memcpy(PGHDR_TO_DATA(p), pHist->pOrig, pPager->pageSize);
2213 TRACE2("ROLLBACK-PAGE %d\n", p->pgno);
2214 }else{
2215 TRACE2("PAGE %d is clean\n", p->pgno);
2216 }
2217 clearHistory(pHist);
2218 p->dirty = 0;
2219 p->inJournal = 0;
2220 p->inStmt = 0;
2221 p->pPrevStmt = p->pNextStmt = 0;
2222 }
2223 pPager->pStmt = 0;
2224 pPager->dbSize = pPager->origDbSize;
2225 memoryTruncate(pPager);
2226 pPager->stmtInUse = 0;
2227 pPager->state = SQLITE_READLOCK;
2228 return SQLITE_OK;
2229 }
2230
drhda47d772002-12-02 04:25:19 +00002231 if( !pPager->dirtyFile || !pPager->journalOpen ){
2232 rc = pager_unwritelock(pPager);
2233 pPager->dbSize = -1;
2234 return rc;
2235 }
drhdb48ee02003-01-16 13:42:43 +00002236
drhd9b02572001-04-15 00:37:09 +00002237 if( pPager->errMask!=0 && pPager->errMask!=PAGER_ERR_FULL ){
drh4b845d72002-03-05 12:41:19 +00002238 if( pPager->state>=SQLITE_WRITELOCK ){
drh99ee3602003-02-16 19:13:36 +00002239 pager_playback(pPager, 1);
drh4b845d72002-03-05 12:41:19 +00002240 }
drhd9b02572001-04-15 00:37:09 +00002241 return pager_errcode(pPager);
drhed7c8552001-04-11 14:29:21 +00002242 }
drhd9b02572001-04-15 00:37:09 +00002243 if( pPager->state!=SQLITE_WRITELOCK ){
2244 return SQLITE_OK;
2245 }
drh99ee3602003-02-16 19:13:36 +00002246 rc = pager_playback(pPager, 1);
drhd9b02572001-04-15 00:37:09 +00002247 if( rc!=SQLITE_OK ){
2248 rc = SQLITE_CORRUPT;
2249 pPager->errMask |= PAGER_ERR_CORRUPT;
2250 }
2251 pPager->dbSize = -1;
drhed7c8552001-04-11 14:29:21 +00002252 return rc;
drh98808ba2001-10-18 12:34:46 +00002253}
drhd9b02572001-04-15 00:37:09 +00002254
2255/*
drh5e00f6c2001-09-13 13:46:56 +00002256** Return TRUE if the database file is opened read-only. Return FALSE
2257** if the database is (in theory) writable.
2258*/
drh3aac2dd2004-04-26 14:10:20 +00002259int sqlite3pager_isreadonly(Pager *pPager){
drhbe0072d2001-09-13 14:46:09 +00002260 return pPager->readOnly;
drh5e00f6c2001-09-13 13:46:56 +00002261}
2262
2263/*
drhd9b02572001-04-15 00:37:09 +00002264** This routine is used for testing and analysis only.
2265*/
drh3aac2dd2004-04-26 14:10:20 +00002266int *sqlite3pager_stats(Pager *pPager){
drhd9b02572001-04-15 00:37:09 +00002267 static int a[9];
2268 a[0] = pPager->nRef;
2269 a[1] = pPager->nPage;
2270 a[2] = pPager->mxPage;
2271 a[3] = pPager->dbSize;
2272 a[4] = pPager->state;
2273 a[5] = pPager->errMask;
2274 a[6] = pPager->nHit;
2275 a[7] = pPager->nMiss;
2276 a[8] = pPager->nOvfl;
2277 return a;
2278}
drhdd793422001-06-28 01:54:48 +00002279
drhfa86c412002-02-02 15:01:15 +00002280/*
drhac69b052004-05-12 13:30:07 +00002281** Set the statement rollback point.
drhfa86c412002-02-02 15:01:15 +00002282**
2283** This routine should be called with the transaction journal already
drhac69b052004-05-12 13:30:07 +00002284** open. A new statement journal is created that can be used to rollback
drhaaab5722002-02-19 13:39:21 +00002285** changes of a single SQL command within a larger transaction.
drhfa86c412002-02-02 15:01:15 +00002286*/
drh3aac2dd2004-04-26 14:10:20 +00002287int sqlite3pager_stmt_begin(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00002288 int rc;
2289 char zTemp[SQLITE_TEMPNAME_SIZE];
drhac69b052004-05-12 13:30:07 +00002290 assert( !pPager->stmtInUse );
2291 TRACE1("STMT-BEGIN\n");
2292 if( pPager->memDb ){
2293 pPager->stmtInUse = 1;
2294 pPager->stmtSize = pPager->dbSize;
2295 return SQLITE_OK;
2296 }
drhda47d772002-12-02 04:25:19 +00002297 if( !pPager->journalOpen ){
drhac69b052004-05-12 13:30:07 +00002298 pPager->stmtAutoopen = 1;
drhda47d772002-12-02 04:25:19 +00002299 return SQLITE_OK;
2300 }
drhfa86c412002-02-02 15:01:15 +00002301 assert( pPager->journalOpen );
drhac69b052004-05-12 13:30:07 +00002302 pPager->aInStmt = sqliteMalloc( pPager->dbSize/8 + 1 );
2303 if( pPager->aInStmt==0 ){
danielk19774adee202004-05-08 08:23:19 +00002304 sqlite3OsReadLock(&pPager->fd);
drhfa86c412002-02-02 15:01:15 +00002305 return SQLITE_NOMEM;
2306 }
drh968af522003-02-11 14:55:40 +00002307#ifndef NDEBUG
drhac69b052004-05-12 13:30:07 +00002308 rc = sqlite3OsFileSize(&pPager->jfd, &pPager->stmtJSize);
2309 if( rc ) goto stmt_begin_failed;
2310 assert( pPager->stmtJSize ==
drh968af522003-02-11 14:55:40 +00002311 pPager->nRec*JOURNAL_PG_SZ(journal_format)+JOURNAL_HDR_SZ(journal_format) );
2312#endif
drhac69b052004-05-12 13:30:07 +00002313 pPager->stmtJSize = pPager->nRec*JOURNAL_PG_SZ(journal_format)
drh968af522003-02-11 14:55:40 +00002314 + JOURNAL_HDR_SZ(journal_format);
drhac69b052004-05-12 13:30:07 +00002315 pPager->stmtSize = pPager->dbSize;
2316 if( !pPager->stmtOpen ){
2317 rc = sqlite3pager_opentemp(zTemp, &pPager->stfd);
2318 if( rc ) goto stmt_begin_failed;
2319 pPager->stmtOpen = 1;
2320 pPager->stmtNRec = 0;
drh0f892532002-05-30 12:27:03 +00002321 }
drhac69b052004-05-12 13:30:07 +00002322 pPager->stmtInUse = 1;
drhfa86c412002-02-02 15:01:15 +00002323 return SQLITE_OK;
2324
drhac69b052004-05-12 13:30:07 +00002325stmt_begin_failed:
2326 if( pPager->aInStmt ){
2327 sqliteFree(pPager->aInStmt);
2328 pPager->aInStmt = 0;
drhfa86c412002-02-02 15:01:15 +00002329 }
2330 return rc;
2331}
2332
2333/*
drhac69b052004-05-12 13:30:07 +00002334** Commit a statement.
drhfa86c412002-02-02 15:01:15 +00002335*/
drh3aac2dd2004-04-26 14:10:20 +00002336int sqlite3pager_stmt_commit(Pager *pPager){
drhac69b052004-05-12 13:30:07 +00002337 if( pPager->stmtInUse ){
drh03eb96a2002-11-10 23:32:56 +00002338 PgHdr *pPg, *pNext;
drhac69b052004-05-12 13:30:07 +00002339 TRACE1("STMT-COMMIT\n");
2340 if( !pPager->memDb ){
2341 sqlite3OsSeek(&pPager->stfd, 0);
2342 /* sqlite3OsTruncate(&pPager->stfd, 0); */
2343 sqliteFree( pPager->aInStmt );
2344 pPager->aInStmt = 0;
drh663fc632002-02-02 18:49:19 +00002345 }
drhac69b052004-05-12 13:30:07 +00002346 for(pPg=pPager->pStmt; pPg; pPg=pNext){
2347 pNext = pPg->pNextStmt;
2348 assert( pPg->inStmt );
2349 pPg->inStmt = 0;
2350 pPg->pPrevStmt = pPg->pNextStmt = 0;
2351 if( pPager->memDb ){
2352 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
2353 sqliteFree(pHist->pStmt);
2354 pHist->pStmt = 0;
2355 }
2356 }
2357 pPager->stmtNRec = 0;
2358 pPager->stmtInUse = 0;
2359 pPager->pStmt = 0;
drh663fc632002-02-02 18:49:19 +00002360 }
drhac69b052004-05-12 13:30:07 +00002361 pPager->stmtAutoopen = 0;
drhfa86c412002-02-02 15:01:15 +00002362 return SQLITE_OK;
2363}
2364
2365/*
drhac69b052004-05-12 13:30:07 +00002366** Rollback a statement.
drhfa86c412002-02-02 15:01:15 +00002367*/
drh3aac2dd2004-04-26 14:10:20 +00002368int sqlite3pager_stmt_rollback(Pager *pPager){
drhfa86c412002-02-02 15:01:15 +00002369 int rc;
drhac69b052004-05-12 13:30:07 +00002370 if( pPager->stmtInUse ){
2371 TRACE1("STMT-ROLLBACK\n");
2372 if( pPager->memDb ){
2373 PgHdr *pPg;
2374 for(pPg=pPager->pStmt; pPg; pPg=pPg->pNextStmt){
2375 PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
2376 if( pHist->pStmt ){
2377 memcpy(PGHDR_TO_DATA(pPg), pHist->pStmt, pPager->pageSize);
2378 sqliteFree(pHist->pStmt);
2379 pHist->pStmt = 0;
2380 }
2381 }
2382 pPager->dbSize = pPager->stmtSize;
2383 memoryTruncate(pPager);
2384 rc = SQLITE_OK;
2385 }else{
2386 rc = pager_stmt_playback(pPager);
2387 }
drh3aac2dd2004-04-26 14:10:20 +00002388 sqlite3pager_stmt_commit(pPager);
drh663fc632002-02-02 18:49:19 +00002389 }else{
2390 rc = SQLITE_OK;
2391 }
drhac69b052004-05-12 13:30:07 +00002392 pPager->stmtAutoopen = 0;
drhfa86c412002-02-02 15:01:15 +00002393 return rc;
2394}
2395
drh73509ee2003-04-06 20:44:45 +00002396/*
2397** Return the full pathname of the database file.
2398*/
drh3aac2dd2004-04-26 14:10:20 +00002399const char *sqlite3pager_filename(Pager *pPager){
drh73509ee2003-04-06 20:44:45 +00002400 return pPager->zFilename;
2401}
2402
drhb20ea9d2004-02-09 01:20:36 +00002403/*
2404** Set the codec for this pager
2405*/
drh3aac2dd2004-04-26 14:10:20 +00002406void sqlite3pager_set_codec(
drhb20ea9d2004-02-09 01:20:36 +00002407 Pager *pPager,
drh9eb9e262004-02-11 02:18:05 +00002408 void (*xCodec)(void*,void*,Pgno,int),
drhb20ea9d2004-02-09 01:20:36 +00002409 void *pCodecArg
2410){
2411 pPager->xCodec = xCodec;
2412 pPager->pCodecArg = pCodecArg;
2413}
2414
drh74587e52002-08-13 00:01:16 +00002415#ifdef SQLITE_TEST
drhdd793422001-06-28 01:54:48 +00002416/*
2417** Print a listing of all referenced pages and their ref count.
2418*/
drh3aac2dd2004-04-26 14:10:20 +00002419void sqlite3pager_refdump(Pager *pPager){
drhdd793422001-06-28 01:54:48 +00002420 PgHdr *pPg;
2421 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
2422 if( pPg->nRef<=0 ) continue;
2423 printf("PAGE %3d addr=0x%08x nRef=%d\n",
2424 pPg->pgno, (int)PGHDR_TO_DATA(pPg), pPg->nRef);
2425 }
2426}
2427#endif