blob: 0424497aba90d769a954f730751d27c899e1989d [file] [log] [blame]
drhed7c8552001-04-11 14:29:21 +00001/*
2** Copyright (c) 2001 D. Richard Hipp
3**
4** This program is free software; you can redistribute it and/or
5** modify it under the terms of the GNU General Public
6** License as published by the Free Software Foundation; either
7** version 2 of the License, or (at your option) any later version.
8**
9** This program is distributed in the hope that it will be useful,
10** but WITHOUT ANY WARRANTY; without even the implied warranty of
11** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12** General Public License for more details.
13**
14** You should have received a copy of the GNU General Public
15** License along with this library; if not, write to the
16** Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17** Boston, MA 02111-1307, USA.
18**
19** Author contact information:
20** drh@hwaci.com
21** http://www.hwaci.com/drh/
22**
23*************************************************************************
24** This is the implementation of the page cache subsystem.
25**
26** The page cache is used to access a database file. The pager journals
27** all writes in order to support rollback. Locking is used to limit
drh306dc212001-05-21 13:45:10 +000028** access to one or more reader or one writer.
drhed7c8552001-04-11 14:29:21 +000029**
drh306dc212001-05-21 13:45:10 +000030** @(#) $Id: pager.c,v 1.6 2001/05/21 13:45:10 drh Exp $
drhed7c8552001-04-11 14:29:21 +000031*/
drhd9b02572001-04-15 00:37:09 +000032#include "sqliteInt.h"
drhed7c8552001-04-11 14:29:21 +000033#include "pager.h"
34#include <fcntl.h>
35#include <sys/stat.h>
36#include <unistd.h>
37#include <assert.h>
drhd9b02572001-04-15 00:37:09 +000038#include <string.h>
drhed7c8552001-04-11 14:29:21 +000039
40/*
41** The page cache as a whole is always in one of the following
42** states:
43**
44** SQLITE_UNLOCK The page cache is not currently reading or
45** writing the database file. There is no
46** data held in memory. This is the initial
47** state.
48**
49** SQLITE_READLOCK The page cache is reading the database.
50** Writing is not permitted. There can be
51** multiple readers accessing the same database
drh69688d52001-04-14 16:38:23 +000052** file at the same time.
drhed7c8552001-04-11 14:29:21 +000053**
54** SQLITE_WRITELOCK The page cache is writing the database.
55** Access is exclusive. No other processes or
56** threads can be reading or writing while one
57** process is writing.
58**
drh306dc212001-05-21 13:45:10 +000059** The page cache comes up in SQLITE_UNLOCK. The first time a
60** sqlite_page_get() occurs, the state transitions to SQLITE_READLOCK.
drhed7c8552001-04-11 14:29:21 +000061** After all pages have been released using sqlite_page_unref(),
drh306dc212001-05-21 13:45:10 +000062** the state transitions back to SQLITE_UNLOCK. The first time
drhed7c8552001-04-11 14:29:21 +000063** that sqlite_page_write() is called, the state transitions to
drh306dc212001-05-21 13:45:10 +000064** SQLITE_WRITELOCK. (Note that sqlite_page_write() can only be
65** called on an outstanding page which means that the pager must
66** be in SQLITE_READLOCK before it transitions to SQLITE_WRITELOCK.)
67** The sqlite_page_rollback() and sqlite_page_commit() functions
68** transition the state from SQLITE_WRITELOCK back to SQLITE_READLOCK.
drhed7c8552001-04-11 14:29:21 +000069*/
70#define SQLITE_UNLOCK 0
71#define SQLITE_READLOCK 1
72#define SQLITE_WRITELOCK 2
73
drhd9b02572001-04-15 00:37:09 +000074
drhed7c8552001-04-11 14:29:21 +000075/*
76** Each in-memory image of a page begins with the following header.
77*/
drhd9b02572001-04-15 00:37:09 +000078typedef struct PgHdr PgHdr;
drhed7c8552001-04-11 14:29:21 +000079struct PgHdr {
80 Pager *pPager; /* The pager to which this page belongs */
81 Pgno pgno; /* The page number for this page */
drh69688d52001-04-14 16:38:23 +000082 PgHdr *pNextHash, *pPrevHash; /* Hash collision chain for PgHdr.pgno */
drhed7c8552001-04-11 14:29:21 +000083 int nRef; /* Number of users of this page */
drhd9b02572001-04-15 00:37:09 +000084 PgHdr *pNextFree, *pPrevFree; /* Freelist of pages where nRef==0 */
85 PgHdr *pNextAll, *pPrevAll; /* A list of all pages */
drhed7c8552001-04-11 14:29:21 +000086 char inJournal; /* TRUE if has been written to journal */
87 char dirty; /* TRUE if we need to write back changes */
drh69688d52001-04-14 16:38:23 +000088 /* SQLITE_PAGE_SIZE bytes of page data follow this header */
drh7e3b0a02001-04-28 16:52:40 +000089 /* Pager.nExtra bytes of local data follow the page data */
drhed7c8552001-04-11 14:29:21 +000090};
91
92/*
drh69688d52001-04-14 16:38:23 +000093** Convert a pointer to a PgHdr into a pointer to its data
94** and back again.
drhed7c8552001-04-11 14:29:21 +000095*/
96#define PGHDR_TO_DATA(P) ((void*)(&(P)[1]))
97#define DATA_TO_PGHDR(D) (&((PgHdr*)(D))[-1])
drh7e3b0a02001-04-28 16:52:40 +000098#define PGHDR_TO_EXTRA(P) ((void*)&((char*)(&(P)[1]))[SQLITE_PAGE_SIZE])
drhed7c8552001-04-11 14:29:21 +000099
100/*
drhed7c8552001-04-11 14:29:21 +0000101** How big to make the hash table used for locating in-memory pages
drh306dc212001-05-21 13:45:10 +0000102** by page number. Knuth says this should be a prime number.
drhed7c8552001-04-11 14:29:21 +0000103*/
drhd9b02572001-04-15 00:37:09 +0000104#define N_PG_HASH 101
drhed7c8552001-04-11 14:29:21 +0000105
106/*
107** A open page cache is an instance of the following structure.
108*/
109struct Pager {
110 char *zFilename; /* Name of the database file */
111 char *zJournal; /* Name of the journal file */
112 int fd, jfd; /* File descriptors for database and journal */
drhed7c8552001-04-11 14:29:21 +0000113 int dbSize; /* Number of pages in the file */
drh69688d52001-04-14 16:38:23 +0000114 int origDbSize; /* dbSize before the current change */
drh7e3b0a02001-04-28 16:52:40 +0000115 int nExtra; /* Add this many bytes to each in-memory page */
drhed7c8552001-04-11 14:29:21 +0000116 int nPage; /* Total number of in-memory pages */
drhd9b02572001-04-15 00:37:09 +0000117 int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */
drhed7c8552001-04-11 14:29:21 +0000118 int mxPage; /* Maximum number of pages to hold in cache */
drhd9b02572001-04-15 00:37:09 +0000119 int nHit, nMiss, nOvfl; /* Cache hits, missing, and LRU overflows */
120 unsigned char state; /* SQLITE_UNLOCK, _READLOCK or _WRITELOCK */
121 unsigned char errMask; /* One of several kinds of errors */
drhed7c8552001-04-11 14:29:21 +0000122 PgHdr *pFirst, *pLast; /* List of free pages */
drhd9b02572001-04-15 00:37:09 +0000123 PgHdr *pAll; /* List of all pages */
drhed7c8552001-04-11 14:29:21 +0000124 PgHdr *aHash[N_PG_HASH]; /* Hash table to map page number of PgHdr */
drhd9b02572001-04-15 00:37:09 +0000125};
126
127/*
128** These are bits that can be set in Pager.errMask.
129*/
130#define PAGER_ERR_FULL 0x01 /* a write() failed */
131#define PAGER_ERR_MEM 0x02 /* malloc() failed */
132#define PAGER_ERR_LOCK 0x04 /* error in the locking protocol */
133#define PAGER_ERR_CORRUPT 0x08 /* database or journal corruption */
134
135/*
136** The journal file contains page records in the following
137** format.
138*/
139typedef struct PageRecord PageRecord;
140struct PageRecord {
141 Pgno pgno; /* The page number */
142 char aData[SQLITE_PAGE_SIZE]; /* Original data for page pgno */
143};
144
145/*
146** Journal files begin with the following magic string. This data
147** is completely random. It is used only as a sanity check.
148*/
149static const unsigned char aJournalMagic[] = {
150 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd4,
drhed7c8552001-04-11 14:29:21 +0000151};
152
153/*
154** Hash a page number
155*/
drhd9b02572001-04-15 00:37:09 +0000156#define pager_hash(PN) ((PN)%N_PG_HASH)
drhed7c8552001-04-11 14:29:21 +0000157
158/*
159** Attempt to acquire a read lock (if wrlock==0) or a write lock (if wrlock==1)
160** on the database file. Return 0 on success and non-zero if the lock
161** could not be acquired.
162*/
drhd9b02572001-04-15 00:37:09 +0000163static int pager_lock(int fd, int wrlock){
164 int rc;
drhed7c8552001-04-11 14:29:21 +0000165 struct flock lock;
drhd9b02572001-04-15 00:37:09 +0000166 lock.l_type = wrlock ? F_WRLCK : F_RDLCK;
167 lock.l_whence = SEEK_SET;
168 lock.l_start = lock.l_len = 0L;
169 rc = fcntl(fd, F_SETLK, &lock);
170 return rc!=0;
drhed7c8552001-04-11 14:29:21 +0000171}
172
173/*
174** Unlock the database file.
175*/
drhd9b02572001-04-15 00:37:09 +0000176static int pager_unlock(fd){
177 int rc;
drhed7c8552001-04-11 14:29:21 +0000178 struct flock lock;
179 lock.l_type = F_UNLCK;
drhd9b02572001-04-15 00:37:09 +0000180 lock.l_whence = SEEK_SET;
181 lock.l_start = lock.l_len = 0L;
182 rc = fcntl(fd, F_SETLK, &lock);
183 return rc!=0;
184}
185
186/*
187** Move the cursor for file descriptor fd to the point whereto from
188** the beginning of the file.
189*/
190static int pager_seek(int fd, off_t whereto){
191 lseek(fd, whereto, SEEK_SET);
192 return SQLITE_OK;
193}
194
195/*
196** Truncate the given file so that it contains exactly mxPg pages
197** of data.
198*/
199static int pager_truncate(int fd, Pgno mxPg){
200 int rc;
201 rc = ftruncate(fd, mxPg*SQLITE_PAGE_SIZE);
202 return rc!=0 ? SQLITE_IOERR : SQLITE_OK;
203}
204
205/*
206** Read nBytes of data from fd into pBuf. If the data cannot be
207** read or only a partial read occurs, then the unread parts of
208** pBuf are filled with zeros and this routine returns SQLITE_IOERR.
209** If the read is completely successful, return SQLITE_OK.
210*/
211static int pager_read(int fd, void *pBuf, int nByte){
212 int rc;
213 rc = read(fd, pBuf, nByte);
214 if( rc<0 ){
215 memset(pBuf, 0, nByte);
216 return SQLITE_IOERR;
217 }
218 if( rc<nByte ){
219 memset(&((char*)pBuf)[rc], 0, nByte - rc);
220 rc = SQLITE_IOERR;
221 }else{
222 rc = SQLITE_OK;
223 }
224 return rc;
225}
226
227/*
228** Write nBytes of data into fd. If any problem occurs or if the
229** write is incomplete, SQLITE_IOERR is returned. SQLITE_OK is
230** returned upon complete success.
231*/
232static int pager_write(int fd, const void *pBuf, int nByte){
233 int rc;
234 rc = write(fd, pBuf, nByte);
235 if( rc<nByte ){
236 return SQLITE_FULL;
237 }else{
238 return SQLITE_OK;
239 }
240}
241
242/*
243** Convert the bits in the pPager->errMask into an approprate
244** return code.
245*/
246static int pager_errcode(Pager *pPager){
247 int rc = SQLITE_OK;
248 if( pPager->errMask & PAGER_ERR_LOCK ) rc = SQLITE_PROTOCOL;
249 if( pPager->errMask & PAGER_ERR_FULL ) rc = SQLITE_FULL;
250 if( pPager->errMask & PAGER_ERR_MEM ) rc = SQLITE_NOMEM;
251 if( pPager->errMask & PAGER_ERR_CORRUPT ) rc = SQLITE_CORRUPT;
252 return rc;
drhed7c8552001-04-11 14:29:21 +0000253}
254
255/*
256** Find a page in the hash table given its page number. Return
257** a pointer to the page or NULL if not found.
258*/
drhd9b02572001-04-15 00:37:09 +0000259static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drhed7c8552001-04-11 14:29:21 +0000260 PgHdr *p = pPager->aHash[pgno % N_PG_HASH];
261 while( p && p->pgno!=pgno ){
262 p = p->pNextHash;
263 }
264 return p;
265}
266
267/*
268** Unlock the database and clear the in-memory cache. This routine
269** sets the state of the pager back to what it was when it was first
270** opened. Any outstanding pages are invalidated and subsequent attempts
271** to access those pages will likely result in a coredump.
272*/
drhd9b02572001-04-15 00:37:09 +0000273static void pager_reset(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +0000274 PgHdr *pPg, *pNext;
drhd9b02572001-04-15 00:37:09 +0000275 for(pPg=pPager->pAll; pPg; pPg=pNext){
276 pNext = pPg->pNextAll;
277 sqliteFree(pPg);
drhed7c8552001-04-11 14:29:21 +0000278 }
279 pPager->pFirst = 0;
drhd9b02572001-04-15 00:37:09 +0000280 pPager->pLast = 0;
281 pPager->pAll = 0;
drhed7c8552001-04-11 14:29:21 +0000282 memset(pPager->aHash, 0, sizeof(pPager->aHash));
283 pPager->nPage = 0;
284 if( pPager->state==SQLITE_WRITELOCK ){
drhd9b02572001-04-15 00:37:09 +0000285 sqlitepager_rollback(pPager);
drhed7c8552001-04-11 14:29:21 +0000286 }
drhd9b02572001-04-15 00:37:09 +0000287 pager_unlock(pPager->fd);
drhed7c8552001-04-11 14:29:21 +0000288 pPager->state = SQLITE_UNLOCK;
drhd9b02572001-04-15 00:37:09 +0000289 pPager->dbSize = -1;
drhed7c8552001-04-11 14:29:21 +0000290 pPager->nRef = 0;
291}
292
293/*
294** When this routine is called, the pager has the journal file open and
295** a write lock on the database. This routine releases the database
296** write lock and acquires a read lock in its place. The journal file
297** is deleted and closed.
298**
299** We have to release the write lock before acquiring the read lock,
300** so there is a race condition where another process can get the lock
301** while we are not holding it. But, no other process should do this
302** because we are also holding a lock on the journal, and no process
303** should get a write lock on the database without first getting a lock
304** on the journal. So this routine should never fail. But it can fail
305** if another process is not playing by the rules. If it does fail,
drhd9b02572001-04-15 00:37:09 +0000306** all in-memory cache pages are invalidated, the PAGER_ERR_LOCK bit
307** is set in pPager->errMask, and this routine returns SQLITE_PROTOCOL.
308** SQLITE_OK is returned on success.
drhed7c8552001-04-11 14:29:21 +0000309*/
drhd9b02572001-04-15 00:37:09 +0000310static int pager_unwritelock(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +0000311 int rc;
drhd9b02572001-04-15 00:37:09 +0000312 PgHdr *pPg;
313 if( pPager->state!=SQLITE_WRITELOCK ) return SQLITE_OK;
314 pager_unlock(pPager->fd);
315 rc = pager_lock(pPager->fd, 0);
drhed7c8552001-04-11 14:29:21 +0000316 unlink(pPager->zJournal);
317 close(pPager->jfd);
318 pPager->jfd = -1;
drhd9b02572001-04-15 00:37:09 +0000319 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
320 pPg->inJournal = 0;
321 pPg->dirty = 0;
322 }
drhed7c8552001-04-11 14:29:21 +0000323 if( rc!=SQLITE_OK ){
324 pPager->state = SQLITE_UNLOCK;
drhed7c8552001-04-11 14:29:21 +0000325 rc = SQLITE_PROTOCOL;
drhd9b02572001-04-15 00:37:09 +0000326 pPager->errMask |= PAGER_ERR_LOCK;
drhed7c8552001-04-11 14:29:21 +0000327 }else{
drhd9b02572001-04-15 00:37:09 +0000328 rc = SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +0000329 pPager->state = SQLITE_READLOCK;
330 }
331 return rc;
332}
333
drhed7c8552001-04-11 14:29:21 +0000334/*
335** Playback the journal and thus restore the database file to
336** the state it was in before we started making changes.
337**
drhd9b02572001-04-15 00:37:09 +0000338** The journal file format is as follows: There is an initial
339** file-type string for sanity checking. Then there is a single
340** Pgno number which is the number of pages in the database before
341** changes were made. The database is truncated to this size.
drh306dc212001-05-21 13:45:10 +0000342** Next come zero or more page records where each page record
343** consists of a Pgno and SQLITE_PAGE_SIZE bytes of data. See
344** the PageRecord structure for details.
drhed7c8552001-04-11 14:29:21 +0000345**
drhd9b02572001-04-15 00:37:09 +0000346** For playback, the pages have to be read from the journal in
347** reverse order and put back into the original database file.
drhed7c8552001-04-11 14:29:21 +0000348**
drhd9b02572001-04-15 00:37:09 +0000349** If the file opened as the journal file is not a well-formed
350** journal file (as determined by looking at the magic number
351** at the beginning) then this routine returns SQLITE_PROTOCOL.
352** If any other errors occur during playback, the database will
353** likely be corrupted, so the PAGER_ERR_CORRUPT bit is set in
354** pPager->errMask and SQLITE_CORRUPT is returned. If it all
355** works, then this routine returns SQLITE_OK.
drhed7c8552001-04-11 14:29:21 +0000356*/
drhd9b02572001-04-15 00:37:09 +0000357static int pager_playback(Pager *pPager){
358 int nRec; /* Number of Records */
359 int i; /* Loop counter */
360 Pgno mxPg = 0; /* Size of the original file in pages */
361 struct stat statbuf; /* Used to size the journal */
362 PgHdr *pPg; /* An existing page in the cache */
363 PageRecord pgRec;
364 unsigned char aMagic[sizeof(aJournalMagic)];
drhed7c8552001-04-11 14:29:21 +0000365 int rc;
366
drhd9b02572001-04-15 00:37:09 +0000367 /* Read the beginning of the journal and truncate the
368 ** database file back to its original size.
drhed7c8552001-04-11 14:29:21 +0000369 */
drhd9b02572001-04-15 00:37:09 +0000370 assert( pPager->jfd>=0 );
371 pager_seek(pPager->jfd, 0);
372 rc = pager_read(pPager->jfd, aMagic, sizeof(aMagic));
373 if( rc!=SQLITE_OK || memcmp(aMagic,aJournalMagic,sizeof(aMagic))!=0 ){
374 return SQLITE_PROTOCOL;
375 }
376 rc = pager_read(pPager->jfd, &mxPg, sizeof(mxPg));
377 if( rc!=SQLITE_OK ){
378 return SQLITE_PROTOCOL;
379 }
380 pager_truncate(pPager->fd, mxPg);
381 pPager->dbSize = mxPg;
382
383 /* Begin reading the journal beginning at the end and moving
384 ** toward the beginning.
385 */
386 if( fstat(pPager->jfd, &statbuf)!=0 ){
drhed7c8552001-04-11 14:29:21 +0000387 return SQLITE_OK;
388 }
drhd9b02572001-04-15 00:37:09 +0000389 nRec = (statbuf.st_size - (sizeof(aMagic)+sizeof(Pgno))) / sizeof(PageRecord);
drhed7c8552001-04-11 14:29:21 +0000390
391 /* Process segments beginning with the last and working backwards
392 ** to the first.
393 */
drhd9b02572001-04-15 00:37:09 +0000394 for(i=nRec-1; i>=0; i--){
drhed7c8552001-04-11 14:29:21 +0000395 /* Seek to the beginning of the segment */
drhd9b02572001-04-15 00:37:09 +0000396 off_t ofst;
397 ofst = i*sizeof(PageRecord) + sizeof(aMagic) + sizeof(Pgno);
398 rc = pager_seek(pPager->jfd, ofst);
399 if( rc!=SQLITE_OK ) break;
400 rc = pager_read(pPager->jfd, &pgRec, sizeof(pgRec));
401 if( rc!=SQLITE_OK ) break;
drhed7c8552001-04-11 14:29:21 +0000402
drhd9b02572001-04-15 00:37:09 +0000403 /* Sanity checking on the page */
404 if( pgRec.pgno>mxPg || pgRec.pgno==0 ){
405 rc = SQLITE_CORRUPT;
406 break;
drhed7c8552001-04-11 14:29:21 +0000407 }
408
drhd9b02572001-04-15 00:37:09 +0000409 /* Playback the page. Update the in-memory copy of the page
410 ** at the same time, if there is one.
drhed7c8552001-04-11 14:29:21 +0000411 */
drhd9b02572001-04-15 00:37:09 +0000412 pPg = pager_lookup(pPager, pgRec.pgno);
413 if( pPg ){
414 memcpy(PGHDR_TO_DATA(pPg), pgRec.aData, SQLITE_PAGE_SIZE);
drhed7c8552001-04-11 14:29:21 +0000415 }
drhd9b02572001-04-15 00:37:09 +0000416 rc = pager_seek(pPager->fd, (pgRec.pgno-1)*SQLITE_PAGE_SIZE);
417 if( rc!=SQLITE_OK ) break;
418 rc = pager_write(pPager->fd, pgRec.aData, SQLITE_PAGE_SIZE);
419 if( rc!=SQLITE_OK ) break;
drhed7c8552001-04-11 14:29:21 +0000420 }
drhd9b02572001-04-15 00:37:09 +0000421 if( rc!=SQLITE_OK ){
422 pager_unwritelock(pPager);
423 pPager->errMask |= PAGER_ERR_CORRUPT;
424 rc = SQLITE_CORRUPT;
425 }else{
426 rc = pager_unwritelock(pPager);
drhed7c8552001-04-11 14:29:21 +0000427 }
drhd9b02572001-04-15 00:37:09 +0000428 return rc;
drhed7c8552001-04-11 14:29:21 +0000429}
430
431/*
432** Create a new page cache and put a pointer to the page cache in *ppPager.
433** The file to be cached need not exist. The file is not opened until
drhd9b02572001-04-15 00:37:09 +0000434** the first call to sqlitepager_get() and is only held open until the
435** last page is released using sqlitepager_unref().
drhed7c8552001-04-11 14:29:21 +0000436*/
drh7e3b0a02001-04-28 16:52:40 +0000437int sqlitepager_open(
438 Pager **ppPager, /* Return the Pager structure here */
439 const char *zFilename, /* Name of the database file to open */
440 int mxPage, /* Max number of in-memory cache pages */
441 int nExtra /* Extra bytes append to each in-memory page */
442){
drhed7c8552001-04-11 14:29:21 +0000443 Pager *pPager;
444 int nameLen;
445 int fd;
446
drhd9b02572001-04-15 00:37:09 +0000447 *ppPager = 0;
448 if( sqlite_malloc_failed ){
449 return SQLITE_NOMEM;
450 }
451 fd = open(zFilename, O_RDWR|O_CREAT, 0644);
drhed7c8552001-04-11 14:29:21 +0000452 if( fd<0 ){
453 return SQLITE_CANTOPEN;
454 }
455 nameLen = strlen(zFilename);
456 pPager = sqliteMalloc( sizeof(*pPager) + nameLen*2 + 30 );
drhd9b02572001-04-15 00:37:09 +0000457 if( pPager==0 ){
458 close(fd);
459 return SQLITE_NOMEM;
460 }
drhed7c8552001-04-11 14:29:21 +0000461 pPager->zFilename = (char*)&pPager[1];
462 pPager->zJournal = &pPager->zFilename[nameLen+1];
463 strcpy(pPager->zFilename, zFilename);
464 strcpy(pPager->zJournal, zFilename);
465 strcpy(&pPager->zJournal[nameLen], "-journal");
466 pPager->fd = fd;
467 pPager->jfd = -1;
468 pPager->nRef = 0;
469 pPager->dbSize = -1;
470 pPager->nPage = 0;
drhd79caeb2001-04-15 02:27:24 +0000471 pPager->mxPage = mxPage>5 ? mxPage : 10;
drhed7c8552001-04-11 14:29:21 +0000472 pPager->state = SQLITE_UNLOCK;
drhd9b02572001-04-15 00:37:09 +0000473 pPager->errMask = 0;
drhed7c8552001-04-11 14:29:21 +0000474 pPager->pFirst = 0;
475 pPager->pLast = 0;
476 memset(pPager->aHash, 0, sizeof(pPager->aHash));
477 *ppPager = pPager;
478 return SQLITE_OK;
479}
480
481/*
482** Return the total number of pages in the file opened by pPager.
483*/
drhd9b02572001-04-15 00:37:09 +0000484int sqlitepager_pagecount(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +0000485 int n;
486 struct stat statbuf;
drhd9b02572001-04-15 00:37:09 +0000487 assert( pPager!=0 );
drhed7c8552001-04-11 14:29:21 +0000488 if( pPager->dbSize>=0 ){
489 return pPager->dbSize;
490 }
491 if( fstat(pPager->fd, &statbuf)!=0 ){
492 n = 0;
493 }else{
494 n = statbuf.st_size/SQLITE_PAGE_SIZE;
495 }
drhd9b02572001-04-15 00:37:09 +0000496 if( pPager->state!=SQLITE_UNLOCK ){
drhed7c8552001-04-11 14:29:21 +0000497 pPager->dbSize = n;
498 }
499 return n;
500}
501
502/*
503** Shutdown the page cache. Free all memory and close all files.
504**
505** If a transaction was in progress when this routine is called, that
506** transaction is rolled back. All outstanding pages are invalidated
507** and their memory is freed. Any attempt to use a page associated
508** with this page cache after this function returns will likely
509** result in a coredump.
510*/
drhd9b02572001-04-15 00:37:09 +0000511int sqlitepager_close(Pager *pPager){
512 PgHdr *pPg, *pNext;
drhed7c8552001-04-11 14:29:21 +0000513 switch( pPager->state ){
514 case SQLITE_WRITELOCK: {
drhd9b02572001-04-15 00:37:09 +0000515 sqlitepager_rollback(pPager);
516 pager_unlock(pPager->fd);
drhed7c8552001-04-11 14:29:21 +0000517 break;
518 }
519 case SQLITE_READLOCK: {
drhd9b02572001-04-15 00:37:09 +0000520 pager_unlock(pPager->fd);
drhed7c8552001-04-11 14:29:21 +0000521 break;
522 }
523 default: {
524 /* Do nothing */
525 break;
526 }
527 }
drhd9b02572001-04-15 00:37:09 +0000528 for(pPg=pPager->pAll; pPg; pPg=pNext){
529 pNext = pPg->pNextAll;
530 sqliteFree(pPg);
drhed7c8552001-04-11 14:29:21 +0000531 }
532 if( pPager->fd>=0 ) close(pPager->fd);
533 assert( pPager->jfd<0 );
534 sqliteFree(pPager);
535 return SQLITE_OK;
536}
537
538/*
539** Return the page number for the given page data
540*/
drhd9b02572001-04-15 00:37:09 +0000541Pgno sqlitepager_pagenumber(void *pData){
drhed7c8552001-04-11 14:29:21 +0000542 PgHdr *p = DATA_TO_PGHDR(pData);
543 return p->pgno;
544}
545
546/*
drh7e3b0a02001-04-28 16:52:40 +0000547** Increment the reference count for a page. If the page is
548** currently on the freelist (the reference count is zero) then
549** remove it from the freelist.
550*/
551static void sqlitepager_ref(PgHdr *pPg){
552 if( pPg->nRef==0 ){
553 /* The page is currently on the freelist. Remove it. */
554 if( pPg->pPrevFree ){
555 pPg->pPrevFree->pNextFree = pPg->pNextFree;
556 }else{
557 pPg->pPager->pFirst = pPg->pNextFree;
558 }
559 if( pPg->pNextFree ){
560 pPg->pNextFree->pPrevFree = pPg->pPrevFree;
561 }else{
562 pPg->pPager->pLast = pPg->pPrevFree;
563 }
564 pPg->pPager->nRef++;
565 }
566 pPg->nRef++;
567}
568
569/*
drhd9b02572001-04-15 00:37:09 +0000570** Acquire a page.
571**
572** A read lock is obtained for the first page acquired. The lock
573** is dropped when the last page is released.
574**
drh306dc212001-05-21 13:45:10 +0000575** A _get works for any page number greater than 0. If the database
576** file is smaller than the requested page, then no actual disk
577** read occurs and the memory image of the page is initialized to
578** all zeros. The extra data appended to a page is always initialized
579** to zeros the first time a page is loaded into memory.
580**
drhd9b02572001-04-15 00:37:09 +0000581** The acquisition might fail for several reasons. In all cases,
582** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +0000583**
584** See also sqlitepager_lookup(). Both this routine and _lookup() attempt
585** to find a page in the in-memory cache first. If the page is not already
586** in cache, this routine goes to disk to read it in whereas _lookup()
587** just returns 0. This routine acquires a read-lock the first time it
588** has to go to disk, and could also playback an old journal if necessary.
589** Since _lookup() never goes to disk, it never has to deal with locks
590** or journal files.
drhed7c8552001-04-11 14:29:21 +0000591*/
drhd9b02572001-04-15 00:37:09 +0000592int sqlitepager_get(Pager *pPager, Pgno pgno, void **ppPage){
drhed7c8552001-04-11 14:29:21 +0000593 PgHdr *pPg;
594
drhd9b02572001-04-15 00:37:09 +0000595 /* Make sure we have not hit any critical errors.
596 */
597 if( pPager==0 || pgno==0 ){
598 return SQLITE_ERROR;
599 }
600 if( pPager->errMask & ~(PAGER_ERR_FULL) ){
601 return pager_errcode(pPager);
602 }
603
drhed7c8552001-04-11 14:29:21 +0000604 /* If this is the first page accessed, then get a read lock
605 ** on the database file.
606 */
607 if( pPager->nRef==0 ){
drhd9b02572001-04-15 00:37:09 +0000608 if( pager_lock(pPager->fd, 0)!=0 ){
drhed7c8552001-04-11 14:29:21 +0000609 *ppPage = 0;
610 return SQLITE_BUSY;
611 }
drhd9b02572001-04-15 00:37:09 +0000612 pPager->state = SQLITE_READLOCK;
drhed7c8552001-04-11 14:29:21 +0000613
614 /* If a journal file exists, try to play it back.
615 */
616 if( access(pPager->zJournal,0)==0 ){
617 int rc;
618
619 /* Open the journal for exclusive access. Return SQLITE_BUSY if
620 ** we cannot get exclusive access to the journal file
621 */
622 pPager->jfd = open(pPager->zJournal, O_RDONLY, 0);
drhd9b02572001-04-15 00:37:09 +0000623 if( pPager->jfd<0 || pager_lock(pPager->jfd, 1)!=0 ){
drhed7c8552001-04-11 14:29:21 +0000624 if( pPager->jfd>=0 ){ close(pPager->jfd); pPager->jfd = -1; }
drhd9b02572001-04-15 00:37:09 +0000625 pager_unlock(pPager->fd);
drhed7c8552001-04-11 14:29:21 +0000626 *ppPage = 0;
627 return SQLITE_BUSY;
628 }
629
630 /* Get a write lock on the database */
drhd9b02572001-04-15 00:37:09 +0000631 pager_unlock(pPager->fd);
632 if( pager_lock(pPager->fd, 1)!=0 ){
633 close(pPager->jfd);
634 pPager->jfd = -1;
drhed7c8552001-04-11 14:29:21 +0000635 *ppPage = 0;
636 return SQLITE_PROTOCOL;
637 }
638
639 /* Playback and delete the journal. Drop the database write
640 ** lock and reacquire the read lock.
641 */
drhd9b02572001-04-15 00:37:09 +0000642 rc = pager_playback(pPager);
643 if( rc!=SQLITE_OK ){
644 return rc;
645 }
drhed7c8552001-04-11 14:29:21 +0000646 }
647 pPg = 0;
648 }else{
649 /* Search for page in cache */
drhd9b02572001-04-15 00:37:09 +0000650 pPg = pager_lookup(pPager, pgno);
drhed7c8552001-04-11 14:29:21 +0000651 }
652 if( pPg==0 ){
drhd9b02572001-04-15 00:37:09 +0000653 /* The requested page is not in the page cache. */
drhed7c8552001-04-11 14:29:21 +0000654 int h;
drh7e3b0a02001-04-28 16:52:40 +0000655 pPager->nMiss++;
drhed7c8552001-04-11 14:29:21 +0000656 if( pPager->nPage<pPager->mxPage || pPager->pFirst==0 ){
657 /* Create a new page */
drh7e3b0a02001-04-28 16:52:40 +0000658 pPg = sqliteMalloc( sizeof(*pPg) + SQLITE_PAGE_SIZE + pPager->nExtra );
drhd9b02572001-04-15 00:37:09 +0000659 if( pPg==0 ){
660 *ppPage = 0;
661 pager_unwritelock(pPager);
662 pPager->errMask |= PAGER_ERR_MEM;
663 return SQLITE_NOMEM;
664 }
drhed7c8552001-04-11 14:29:21 +0000665 pPg->pPager = pPager;
drhd9b02572001-04-15 00:37:09 +0000666 pPg->pNextAll = pPager->pAll;
667 if( pPager->pAll ){
668 pPager->pAll->pPrevAll = pPg;
669 }
670 pPg->pPrevAll = 0;
drhd79caeb2001-04-15 02:27:24 +0000671 pPager->pAll = pPg;
drhd9b02572001-04-15 00:37:09 +0000672 pPager->nPage++;
drhed7c8552001-04-11 14:29:21 +0000673 }else{
drhd9b02572001-04-15 00:37:09 +0000674 /* Recycle an older page. First locate the page to be recycled.
675 ** Try to find one that is not dirty and is near the head of
676 ** of the free list */
677 int cnt = 4;
drhed7c8552001-04-11 14:29:21 +0000678 pPg = pPager->pFirst;
drhd9b02572001-04-15 00:37:09 +0000679 while( pPg->dirty && 0<cnt-- ){
680 pPg = pPg->pNextFree;
681 }
682 if( pPg==0 || pPg->dirty ) pPg = pPager->pFirst;
683 assert( pPg->nRef==0 );
684
685 /* If the page to be recycled is dirty, sync the journal and write
686 ** the old page into the database. */
drhed7c8552001-04-11 14:29:21 +0000687 if( pPg->dirty ){
688 int rc;
drhd9b02572001-04-15 00:37:09 +0000689 assert( pPg->inJournal==1 );
690 assert( pPager->state==SQLITE_WRITELOCK );
691 rc = fsync(pPager->jfd);
692 if( rc!=0 ){
693 rc = sqlitepager_rollback(pPager);
drhed7c8552001-04-11 14:29:21 +0000694 *ppPage = 0;
drhd9b02572001-04-15 00:37:09 +0000695 if( rc==SQLITE_OK ) rc = SQLITE_IOERR;
drhed7c8552001-04-11 14:29:21 +0000696 return rc;
697 }
drhd9b02572001-04-15 00:37:09 +0000698 pager_seek(pPager->fd, (pPg->pgno-1)*SQLITE_PAGE_SIZE);
699 rc = pager_write(pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE);
700 if( rc!=SQLITE_OK ){
701 rc = sqlitepager_rollback(pPager);
702 *ppPage = 0;
703 if( rc==SQLITE_OK ) rc = SQLITE_FULL;
704 return rc;
705 }
706 }
707
708 /* Unlink the old page from the free list and the hash table
709 */
710 pPager->pFirst = pPg->pNextFree;
drhed7c8552001-04-11 14:29:21 +0000711 if( pPager->pFirst ){
drhd9b02572001-04-15 00:37:09 +0000712 pPager->pFirst->pPrevFree = 0;
drhed7c8552001-04-11 14:29:21 +0000713 }else{
714 pPager->pLast = 0;
715 }
716 if( pPg->pNextHash ){
717 pPg->pNextHash->pPrevHash = pPg->pPrevHash;
718 }
719 if( pPg->pPrevHash ){
720 pPg->pPrevHash->pNextHash = pPg->pNextHash;
721 }else{
drhd9b02572001-04-15 00:37:09 +0000722 h = pager_hash(pPg->pgno);
drhed7c8552001-04-11 14:29:21 +0000723 assert( pPager->aHash[h]==pPg );
724 pPager->aHash[h] = pPg->pNextHash;
725 }
drhd9b02572001-04-15 00:37:09 +0000726 pPager->nOvfl++;
drhed7c8552001-04-11 14:29:21 +0000727 }
728 pPg->pgno = pgno;
729 pPg->inJournal = 0;
730 pPg->dirty = 0;
731 pPg->nRef = 1;
drhd9b02572001-04-15 00:37:09 +0000732 pPager->nRef++;
733 h = pager_hash(pgno);
drhed7c8552001-04-11 14:29:21 +0000734 pPg->pNextHash = pPager->aHash[h];
735 pPager->aHash[h] = pPg;
736 if( pPg->pNextHash ){
737 assert( pPg->pNextHash->pPrevHash==0 );
738 pPg->pNextHash->pPrevHash = pPg;
739 }
drh306dc212001-05-21 13:45:10 +0000740 if( pPager->dbSize<0 ) sqlitepager_pagecount(pPager);
741 if( pPager->dbSize<pgno ){
742 memset(PGHDR_TO_DATA(pPg), 0, SQLITE_PAGE_SIZE);
743 }else{
744 pager_seek(pPager->fd, (pgno-1)*SQLITE_PAGE_SIZE);
745 pager_read(pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE);
746 }
drh7e3b0a02001-04-28 16:52:40 +0000747 if( pPager->nExtra>0 ){
748 memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra);
749 }
drhed7c8552001-04-11 14:29:21 +0000750 }else{
drhd9b02572001-04-15 00:37:09 +0000751 /* The requested page is in the page cache. */
drh7e3b0a02001-04-28 16:52:40 +0000752 pPager->nHit++;
753 sqlitepager_ref(pPg);
drhed7c8552001-04-11 14:29:21 +0000754 }
755 *ppPage = PGHDR_TO_DATA(pPg);
756 return SQLITE_OK;
757}
758
759/*
drh7e3b0a02001-04-28 16:52:40 +0000760** Acquire a page if it is already in the in-memory cache. Do
761** not read the page from disk. Return a pointer to the page,
762** or 0 if the page is not in cache.
763**
764** See also sqlitepager_get(). The difference between this routine
765** and sqlitepager_get() is that _get() will go to the disk and read
766** in the page if the page is not already in cache. This routine
drh306dc212001-05-21 13:45:10 +0000767** returns NULL if the page is not in cache of if a disk I/O has ever
768** happened.
drh7e3b0a02001-04-28 16:52:40 +0000769*/
770void *sqlitepager_lookup(Pager *pPager, Pgno pgno){
771 PgHdr *pPg;
772
773 /* Make sure we have not hit any critical errors.
774 */
775 if( pPager==0 || pgno==0 ){
776 return 0;
777 }
778 if( pPager->errMask & ~(PAGER_ERR_FULL) ){
779 return 0;
780 }
781 if( pPager->nRef==0 ){
782 return 0;
783 }
784 pPg = pager_lookup(pPager, pgno);
785 if( pPg==0 ) return 0;
786 sqlitepager_ref(pPg);
787 return PGHDR_TO_DATA(pPg);
788}
789
790/*
drhed7c8552001-04-11 14:29:21 +0000791** Release a page.
792**
793** If the number of references to the page drop to zero, then the
794** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +0000795** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +0000796** removed.
797*/
drhd9b02572001-04-15 00:37:09 +0000798int sqlitepager_unref(void *pData){
drhed7c8552001-04-11 14:29:21 +0000799 Pager *pPager;
800 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +0000801
802 /* Decrement the reference count for this page
803 */
drhed7c8552001-04-11 14:29:21 +0000804 pPg = DATA_TO_PGHDR(pData);
805 assert( pPg->nRef>0 );
806 pPager = pPg->pPager;
807 pPg->nRef--;
drhd9b02572001-04-15 00:37:09 +0000808
809 /* When the number of references to a page reach 0, add the
810 ** page to the freelist.
811 */
drhed7c8552001-04-11 14:29:21 +0000812 if( pPg->nRef==0 ){
drhd9b02572001-04-15 00:37:09 +0000813 pPg->pNextFree = 0;
814 pPg->pPrevFree = pPager->pLast;
drhed7c8552001-04-11 14:29:21 +0000815 pPager->pLast = pPg;
drhd9b02572001-04-15 00:37:09 +0000816 if( pPg->pPrevFree ){
817 pPg->pPrevFree->pNextFree = pPg;
drhed7c8552001-04-11 14:29:21 +0000818 }else{
819 pPager->pFirst = pPg;
820 }
drhd9b02572001-04-15 00:37:09 +0000821
822 /* When all pages reach the freelist, drop the read lock from
823 ** the database file.
824 */
825 pPager->nRef--;
826 assert( pPager->nRef>=0 );
827 if( pPager->nRef==0 ){
828 pager_reset(pPager);
829 }
drhed7c8552001-04-11 14:29:21 +0000830 }
drhd9b02572001-04-15 00:37:09 +0000831 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +0000832}
833
834/*
835** Mark a data page as writeable. The page is written into the journal
836** if it is not there already. This routine must be called before making
837** changes to a page.
838**
839** The first time this routine is called, the pager creates a new
840** journal and acquires a write lock on the database. If the write
841** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +0000842** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +0000843** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +0000844**
845** If the journal file could not be written because the disk is full,
846** then this routine returns SQLITE_FULL and does an immediate rollback.
847** All subsequent write attempts also return SQLITE_FULL until there
848** is a call to sqlitepager_commit() or sqlitepager_rollback() to
849** reset.
drhed7c8552001-04-11 14:29:21 +0000850*/
drhd9b02572001-04-15 00:37:09 +0000851int sqlitepager_write(void *pData){
drh69688d52001-04-14 16:38:23 +0000852 PgHdr *pPg = DATA_TO_PGHDR(pData);
853 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +0000854 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +0000855
drhd9b02572001-04-15 00:37:09 +0000856 if( pPager->errMask ){
857 return pager_errcode(pPager);
858 }
859 pPg->dirty = 1;
drh69688d52001-04-14 16:38:23 +0000860 if( pPg->inJournal ){ return SQLITE_OK; }
drhd9b02572001-04-15 00:37:09 +0000861 assert( pPager->state!=SQLITE_UNLOCK );
drhed7c8552001-04-11 14:29:21 +0000862 if( pPager->state==SQLITE_READLOCK ){
863 pPager->jfd = open(pPager->zJournal, O_RDWR|O_CREAT, 0644);
864 if( pPager->jfd<0 ){
865 return SQLITE_CANTOPEN;
866 }
drhd9b02572001-04-15 00:37:09 +0000867 if( pager_lock(pPager->jfd, 1) ){
drhed7c8552001-04-11 14:29:21 +0000868 close(pPager->jfd);
869 pPager->jfd = -1;
870 return SQLITE_BUSY;
871 }
drhd9b02572001-04-15 00:37:09 +0000872 pager_unlock(pPager->fd);
873 if( pager_lock(pPager->fd, 1) ){
drhed7c8552001-04-11 14:29:21 +0000874 close(pPager->jfd);
875 pPager->jfd = -1;
876 pPager->state = SQLITE_UNLOCK;
drhd9b02572001-04-15 00:37:09 +0000877 pPager->errMask |= PAGER_ERR_LOCK;
drhed7c8552001-04-11 14:29:21 +0000878 return SQLITE_PROTOCOL;
879 }
880 pPager->state = SQLITE_WRITELOCK;
drhd9b02572001-04-15 00:37:09 +0000881 sqlitepager_pagecount(pPager);
drh69688d52001-04-14 16:38:23 +0000882 pPager->origDbSize = pPager->dbSize;
drhd9b02572001-04-15 00:37:09 +0000883 rc = pager_write(pPager->jfd, aJournalMagic, sizeof(aJournalMagic));
884 if( rc==SQLITE_OK ){
885 rc = pager_write(pPager->jfd, &pPager->dbSize, sizeof(Pgno));
886 }
887 if( rc!=SQLITE_OK ){
888 rc = pager_unwritelock(pPager);
889 if( rc==SQLITE_OK ) rc = SQLITE_FULL;
890 return rc;
891 }
drhed7c8552001-04-11 14:29:21 +0000892 }
drhd9b02572001-04-15 00:37:09 +0000893 assert( pPager->state==SQLITE_WRITELOCK );
drh69688d52001-04-14 16:38:23 +0000894 assert( pPager->jfd>=0 );
drhd9b02572001-04-15 00:37:09 +0000895 if( pPg->pgno <= pPager->origDbSize ){
896 rc = pager_write(pPager->jfd, &pPg->pgno, sizeof(Pgno));
897 if( rc==SQLITE_OK ){
898 rc = pager_write(pPager->jfd, pData, SQLITE_PAGE_SIZE);
899 }
900 if( rc!=SQLITE_OK ){
901 sqlitepager_rollback(pPager);
902 pPager->errMask |= PAGER_ERR_FULL;
903 return rc;
904 }
drh69688d52001-04-14 16:38:23 +0000905 }
drh69688d52001-04-14 16:38:23 +0000906 pPg->inJournal = 1;
drh306dc212001-05-21 13:45:10 +0000907 if( pPager->dbSize<pPg->pgno ){
908 pPager->dbSize = pPg->pgno;
909 }
drh69688d52001-04-14 16:38:23 +0000910 return rc;
drhed7c8552001-04-11 14:29:21 +0000911}
912
913/*
914** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +0000915**
916** If the commit fails for any reason, a rollback attempt is made
917** and an error code is returned. If the commit worked, SQLITE_OK
918** is returned.
drhed7c8552001-04-11 14:29:21 +0000919*/
drhd9b02572001-04-15 00:37:09 +0000920int sqlitepager_commit(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +0000921 int i, rc;
922 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +0000923
924 if( pPager->errMask==PAGER_ERR_FULL ){
925 rc = sqlitepager_rollback(pPager);
926 if( rc==SQLITE_OK ) rc = SQLITE_FULL;
927 return rc;
928 }
929 if( pPager->errMask!=0 ){
930 rc = pager_errcode(pPager);
931 return rc;
932 }
933 if( pPager->state!=SQLITE_WRITELOCK ){
934 return SQLITE_ERROR;
935 }
drhed7c8552001-04-11 14:29:21 +0000936 assert( pPager->jfd>=0 );
937 if( fsync(pPager->jfd) ){
drhd9b02572001-04-15 00:37:09 +0000938 goto commit_abort;
drhed7c8552001-04-11 14:29:21 +0000939 }
940 for(i=0; i<N_PG_HASH; i++){
941 for(pPg=pPager->aHash[i]; pPg; pPg=pPg->pNextHash){
942 if( pPg->dirty==0 ) continue;
drhd9b02572001-04-15 00:37:09 +0000943 rc = pager_seek(pPager->fd, (pPg->pgno-1)*SQLITE_PAGE_SIZE);
944 if( rc!=SQLITE_OK ) goto commit_abort;
945 rc = pager_write(pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE);
946 if( rc!=SQLITE_OK ) goto commit_abort;
drhed7c8552001-04-11 14:29:21 +0000947 }
948 }
drhd9b02572001-04-15 00:37:09 +0000949 if( fsync(pPager->fd) ) goto commit_abort;
950 rc = pager_unwritelock(pPager);
951 pPager->dbSize = -1;
952 return rc;
953
954 /* Jump here if anything goes wrong during the commit process.
955 */
956commit_abort:
957 rc = sqlitepager_rollback(pPager);
958 if( rc==SQLITE_OK ){
959 rc = SQLITE_FULL;
drhed7c8552001-04-11 14:29:21 +0000960 }
drhed7c8552001-04-11 14:29:21 +0000961 return rc;
962}
963
964/*
965** Rollback all changes. The database falls back to read-only mode.
966** All in-memory cache pages revert to their original data contents.
967** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +0000968**
969** This routine cannot fail unless some other process is not following
970** the correct locking protocol (SQLITE_PROTOCOL) or unless some other
971** process is writing trash into the journal file (SQLITE_CORRUPT) or
972** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
973** codes are returned for all these occasions. Otherwise,
974** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +0000975*/
drhd9b02572001-04-15 00:37:09 +0000976int sqlitepager_rollback(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +0000977 int rc;
drhd9b02572001-04-15 00:37:09 +0000978 if( pPager->errMask!=0 && pPager->errMask!=PAGER_ERR_FULL ){
979 return pager_errcode(pPager);
drhed7c8552001-04-11 14:29:21 +0000980 }
drhd9b02572001-04-15 00:37:09 +0000981 if( pPager->state!=SQLITE_WRITELOCK ){
982 return SQLITE_OK;
983 }
984 rc = pager_playback(pPager);
985 if( rc!=SQLITE_OK ){
986 rc = SQLITE_CORRUPT;
987 pPager->errMask |= PAGER_ERR_CORRUPT;
988 }
989 pPager->dbSize = -1;
drhed7c8552001-04-11 14:29:21 +0000990 return rc;
991};
drhd9b02572001-04-15 00:37:09 +0000992
993/*
994** This routine is used for testing and analysis only.
995*/
996int *sqlitepager_stats(Pager *pPager){
997 static int a[9];
998 a[0] = pPager->nRef;
999 a[1] = pPager->nPage;
1000 a[2] = pPager->mxPage;
1001 a[3] = pPager->dbSize;
1002 a[4] = pPager->state;
1003 a[5] = pPager->errMask;
1004 a[6] = pPager->nHit;
1005 a[7] = pPager->nMiss;
1006 a[8] = pPager->nOvfl;
1007 return a;
1008}