blob: a630720a562a6caa1bfe3db8957c7ffd7b56438f [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**
drhdf0b3b02001-06-23 11:36:20 +000030** @(#) $Id: pager.c,v 1.10 2001/06/23 11:36:20 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.
drhbd03cae2001-06-02 02:40:57 +000077** This header is only visible to this pager module. The client
78** code that calls pager sees only the data that follows the header.
drhed7c8552001-04-11 14:29:21 +000079*/
drhd9b02572001-04-15 00:37:09 +000080typedef struct PgHdr PgHdr;
drhed7c8552001-04-11 14:29:21 +000081struct PgHdr {
82 Pager *pPager; /* The pager to which this page belongs */
83 Pgno pgno; /* The page number for this page */
drh69688d52001-04-14 16:38:23 +000084 PgHdr *pNextHash, *pPrevHash; /* Hash collision chain for PgHdr.pgno */
drhed7c8552001-04-11 14:29:21 +000085 int nRef; /* Number of users of this page */
drhd9b02572001-04-15 00:37:09 +000086 PgHdr *pNextFree, *pPrevFree; /* Freelist of pages where nRef==0 */
87 PgHdr *pNextAll, *pPrevAll; /* A list of all pages */
drhed7c8552001-04-11 14:29:21 +000088 char inJournal; /* TRUE if has been written to journal */
89 char dirty; /* TRUE if we need to write back changes */
drh69688d52001-04-14 16:38:23 +000090 /* SQLITE_PAGE_SIZE bytes of page data follow this header */
drh7e3b0a02001-04-28 16:52:40 +000091 /* Pager.nExtra bytes of local data follow the page data */
drhed7c8552001-04-11 14:29:21 +000092};
93
94/*
drh69688d52001-04-14 16:38:23 +000095** Convert a pointer to a PgHdr into a pointer to its data
96** and back again.
drhed7c8552001-04-11 14:29:21 +000097*/
98#define PGHDR_TO_DATA(P) ((void*)(&(P)[1]))
99#define DATA_TO_PGHDR(D) (&((PgHdr*)(D))[-1])
drh7e3b0a02001-04-28 16:52:40 +0000100#define PGHDR_TO_EXTRA(P) ((void*)&((char*)(&(P)[1]))[SQLITE_PAGE_SIZE])
drhed7c8552001-04-11 14:29:21 +0000101
102/*
drhed7c8552001-04-11 14:29:21 +0000103** How big to make the hash table used for locating in-memory pages
drh306dc212001-05-21 13:45:10 +0000104** by page number. Knuth says this should be a prime number.
drhed7c8552001-04-11 14:29:21 +0000105*/
drhd9b02572001-04-15 00:37:09 +0000106#define N_PG_HASH 101
drhed7c8552001-04-11 14:29:21 +0000107
108/*
109** A open page cache is an instance of the following structure.
110*/
111struct Pager {
112 char *zFilename; /* Name of the database file */
113 char *zJournal; /* Name of the journal file */
114 int fd, jfd; /* File descriptors for database and journal */
drhed7c8552001-04-11 14:29:21 +0000115 int dbSize; /* Number of pages in the file */
drh69688d52001-04-14 16:38:23 +0000116 int origDbSize; /* dbSize before the current change */
drh7e3b0a02001-04-28 16:52:40 +0000117 int nExtra; /* Add this many bytes to each in-memory page */
drh72f82862001-05-24 21:06:34 +0000118 void (*xDestructor)(void*); /* Call this routine when freeing pages */
drhed7c8552001-04-11 14:29:21 +0000119 int nPage; /* Total number of in-memory pages */
drhd9b02572001-04-15 00:37:09 +0000120 int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */
drhed7c8552001-04-11 14:29:21 +0000121 int mxPage; /* Maximum number of pages to hold in cache */
drhd9b02572001-04-15 00:37:09 +0000122 int nHit, nMiss, nOvfl; /* Cache hits, missing, and LRU overflows */
123 unsigned char state; /* SQLITE_UNLOCK, _READLOCK or _WRITELOCK */
124 unsigned char errMask; /* One of several kinds of errors */
drhed7c8552001-04-11 14:29:21 +0000125 PgHdr *pFirst, *pLast; /* List of free pages */
drhd9b02572001-04-15 00:37:09 +0000126 PgHdr *pAll; /* List of all pages */
drhed7c8552001-04-11 14:29:21 +0000127 PgHdr *aHash[N_PG_HASH]; /* Hash table to map page number of PgHdr */
drhd9b02572001-04-15 00:37:09 +0000128};
129
130/*
131** These are bits that can be set in Pager.errMask.
132*/
133#define PAGER_ERR_FULL 0x01 /* a write() failed */
134#define PAGER_ERR_MEM 0x02 /* malloc() failed */
135#define PAGER_ERR_LOCK 0x04 /* error in the locking protocol */
136#define PAGER_ERR_CORRUPT 0x08 /* database or journal corruption */
137
138/*
139** The journal file contains page records in the following
140** format.
141*/
142typedef struct PageRecord PageRecord;
143struct PageRecord {
144 Pgno pgno; /* The page number */
145 char aData[SQLITE_PAGE_SIZE]; /* Original data for page pgno */
146};
147
148/*
149** Journal files begin with the following magic string. This data
150** is completely random. It is used only as a sanity check.
151*/
152static const unsigned char aJournalMagic[] = {
153 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd4,
drhed7c8552001-04-11 14:29:21 +0000154};
155
156/*
157** Hash a page number
158*/
drhd9b02572001-04-15 00:37:09 +0000159#define pager_hash(PN) ((PN)%N_PG_HASH)
drhed7c8552001-04-11 14:29:21 +0000160
161/*
162** Attempt to acquire a read lock (if wrlock==0) or a write lock (if wrlock==1)
163** on the database file. Return 0 on success and non-zero if the lock
164** could not be acquired.
165*/
drhd9b02572001-04-15 00:37:09 +0000166static int pager_lock(int fd, int wrlock){
167 int rc;
drhed7c8552001-04-11 14:29:21 +0000168 struct flock lock;
drhd9b02572001-04-15 00:37:09 +0000169 lock.l_type = wrlock ? F_WRLCK : F_RDLCK;
170 lock.l_whence = SEEK_SET;
171 lock.l_start = lock.l_len = 0L;
172 rc = fcntl(fd, F_SETLK, &lock);
173 return rc!=0;
drhed7c8552001-04-11 14:29:21 +0000174}
175
176/*
177** Unlock the database file.
178*/
drhd9b02572001-04-15 00:37:09 +0000179static int pager_unlock(fd){
180 int rc;
drhed7c8552001-04-11 14:29:21 +0000181 struct flock lock;
182 lock.l_type = F_UNLCK;
drhd9b02572001-04-15 00:37:09 +0000183 lock.l_whence = SEEK_SET;
184 lock.l_start = lock.l_len = 0L;
185 rc = fcntl(fd, F_SETLK, &lock);
186 return rc!=0;
187}
188
189/*
190** Move the cursor for file descriptor fd to the point whereto from
191** the beginning of the file.
192*/
193static int pager_seek(int fd, off_t whereto){
194 lseek(fd, whereto, SEEK_SET);
195 return SQLITE_OK;
196}
197
198/*
199** Truncate the given file so that it contains exactly mxPg pages
200** of data.
201*/
202static int pager_truncate(int fd, Pgno mxPg){
203 int rc;
204 rc = ftruncate(fd, mxPg*SQLITE_PAGE_SIZE);
205 return rc!=0 ? SQLITE_IOERR : SQLITE_OK;
206}
207
208/*
209** Read nBytes of data from fd into pBuf. If the data cannot be
210** read or only a partial read occurs, then the unread parts of
211** pBuf are filled with zeros and this routine returns SQLITE_IOERR.
212** If the read is completely successful, return SQLITE_OK.
213*/
214static int pager_read(int fd, void *pBuf, int nByte){
215 int rc;
216 rc = read(fd, pBuf, nByte);
217 if( rc<0 ){
218 memset(pBuf, 0, nByte);
219 return SQLITE_IOERR;
220 }
221 if( rc<nByte ){
222 memset(&((char*)pBuf)[rc], 0, nByte - rc);
223 rc = SQLITE_IOERR;
224 }else{
225 rc = SQLITE_OK;
226 }
227 return rc;
228}
229
230/*
231** Write nBytes of data into fd. If any problem occurs or if the
232** write is incomplete, SQLITE_IOERR is returned. SQLITE_OK is
233** returned upon complete success.
234*/
235static int pager_write(int fd, const void *pBuf, int nByte){
236 int rc;
237 rc = write(fd, pBuf, nByte);
238 if( rc<nByte ){
239 return SQLITE_FULL;
240 }else{
241 return SQLITE_OK;
242 }
243}
244
245/*
246** Convert the bits in the pPager->errMask into an approprate
247** return code.
248*/
249static int pager_errcode(Pager *pPager){
250 int rc = SQLITE_OK;
251 if( pPager->errMask & PAGER_ERR_LOCK ) rc = SQLITE_PROTOCOL;
252 if( pPager->errMask & PAGER_ERR_FULL ) rc = SQLITE_FULL;
253 if( pPager->errMask & PAGER_ERR_MEM ) rc = SQLITE_NOMEM;
254 if( pPager->errMask & PAGER_ERR_CORRUPT ) rc = SQLITE_CORRUPT;
255 return rc;
drhed7c8552001-04-11 14:29:21 +0000256}
257
258/*
259** Find a page in the hash table given its page number. Return
260** a pointer to the page or NULL if not found.
261*/
drhd9b02572001-04-15 00:37:09 +0000262static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drhed7c8552001-04-11 14:29:21 +0000263 PgHdr *p = pPager->aHash[pgno % N_PG_HASH];
264 while( p && p->pgno!=pgno ){
265 p = p->pNextHash;
266 }
267 return p;
268}
269
270/*
271** Unlock the database and clear the in-memory cache. This routine
272** sets the state of the pager back to what it was when it was first
273** opened. Any outstanding pages are invalidated and subsequent attempts
274** to access those pages will likely result in a coredump.
275*/
drhd9b02572001-04-15 00:37:09 +0000276static void pager_reset(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +0000277 PgHdr *pPg, *pNext;
drhd9b02572001-04-15 00:37:09 +0000278 for(pPg=pPager->pAll; pPg; pPg=pNext){
279 pNext = pPg->pNextAll;
280 sqliteFree(pPg);
drhed7c8552001-04-11 14:29:21 +0000281 }
282 pPager->pFirst = 0;
drhd9b02572001-04-15 00:37:09 +0000283 pPager->pLast = 0;
284 pPager->pAll = 0;
drhed7c8552001-04-11 14:29:21 +0000285 memset(pPager->aHash, 0, sizeof(pPager->aHash));
286 pPager->nPage = 0;
287 if( pPager->state==SQLITE_WRITELOCK ){
drhd9b02572001-04-15 00:37:09 +0000288 sqlitepager_rollback(pPager);
drhed7c8552001-04-11 14:29:21 +0000289 }
drhd9b02572001-04-15 00:37:09 +0000290 pager_unlock(pPager->fd);
drhed7c8552001-04-11 14:29:21 +0000291 pPager->state = SQLITE_UNLOCK;
drhd9b02572001-04-15 00:37:09 +0000292 pPager->dbSize = -1;
drhed7c8552001-04-11 14:29:21 +0000293 pPager->nRef = 0;
294}
295
296/*
297** When this routine is called, the pager has the journal file open and
298** a write lock on the database. This routine releases the database
299** write lock and acquires a read lock in its place. The journal file
300** is deleted and closed.
301**
302** We have to release the write lock before acquiring the read lock,
303** so there is a race condition where another process can get the lock
304** while we are not holding it. But, no other process should do this
305** because we are also holding a lock on the journal, and no process
306** should get a write lock on the database without first getting a lock
307** on the journal. So this routine should never fail. But it can fail
308** if another process is not playing by the rules. If it does fail,
drhd9b02572001-04-15 00:37:09 +0000309** all in-memory cache pages are invalidated, the PAGER_ERR_LOCK bit
310** is set in pPager->errMask, and this routine returns SQLITE_PROTOCOL.
311** SQLITE_OK is returned on success.
drhed7c8552001-04-11 14:29:21 +0000312*/
drhd9b02572001-04-15 00:37:09 +0000313static int pager_unwritelock(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +0000314 int rc;
drhd9b02572001-04-15 00:37:09 +0000315 PgHdr *pPg;
316 if( pPager->state!=SQLITE_WRITELOCK ) return SQLITE_OK;
317 pager_unlock(pPager->fd);
318 rc = pager_lock(pPager->fd, 0);
drhed7c8552001-04-11 14:29:21 +0000319 unlink(pPager->zJournal);
320 close(pPager->jfd);
321 pPager->jfd = -1;
drhd9b02572001-04-15 00:37:09 +0000322 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
323 pPg->inJournal = 0;
324 pPg->dirty = 0;
325 }
drhed7c8552001-04-11 14:29:21 +0000326 if( rc!=SQLITE_OK ){
327 pPager->state = SQLITE_UNLOCK;
drhed7c8552001-04-11 14:29:21 +0000328 rc = SQLITE_PROTOCOL;
drhd9b02572001-04-15 00:37:09 +0000329 pPager->errMask |= PAGER_ERR_LOCK;
drhed7c8552001-04-11 14:29:21 +0000330 }else{
drhd9b02572001-04-15 00:37:09 +0000331 rc = SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +0000332 pPager->state = SQLITE_READLOCK;
333 }
334 return rc;
335}
336
drhed7c8552001-04-11 14:29:21 +0000337/*
338** Playback the journal and thus restore the database file to
339** the state it was in before we started making changes.
340**
drhd9b02572001-04-15 00:37:09 +0000341** The journal file format is as follows: There is an initial
342** file-type string for sanity checking. Then there is a single
343** Pgno number which is the number of pages in the database before
344** changes were made. The database is truncated to this size.
drh306dc212001-05-21 13:45:10 +0000345** Next come zero or more page records where each page record
346** consists of a Pgno and SQLITE_PAGE_SIZE bytes of data. See
347** the PageRecord structure for details.
drhed7c8552001-04-11 14:29:21 +0000348**
drhd9b02572001-04-15 00:37:09 +0000349** For playback, the pages have to be read from the journal in
350** reverse order and put back into the original database file.
drhed7c8552001-04-11 14:29:21 +0000351**
drhd9b02572001-04-15 00:37:09 +0000352** If the file opened as the journal file is not a well-formed
353** journal file (as determined by looking at the magic number
354** at the beginning) then this routine returns SQLITE_PROTOCOL.
355** If any other errors occur during playback, the database will
356** likely be corrupted, so the PAGER_ERR_CORRUPT bit is set in
357** pPager->errMask and SQLITE_CORRUPT is returned. If it all
358** works, then this routine returns SQLITE_OK.
drhed7c8552001-04-11 14:29:21 +0000359*/
drhd9b02572001-04-15 00:37:09 +0000360static int pager_playback(Pager *pPager){
361 int nRec; /* Number of Records */
362 int i; /* Loop counter */
363 Pgno mxPg = 0; /* Size of the original file in pages */
364 struct stat statbuf; /* Used to size the journal */
365 PgHdr *pPg; /* An existing page in the cache */
366 PageRecord pgRec;
367 unsigned char aMagic[sizeof(aJournalMagic)];
drhed7c8552001-04-11 14:29:21 +0000368 int rc;
369
drhd9b02572001-04-15 00:37:09 +0000370 /* Read the beginning of the journal and truncate the
371 ** database file back to its original size.
drhed7c8552001-04-11 14:29:21 +0000372 */
drhd9b02572001-04-15 00:37:09 +0000373 assert( pPager->jfd>=0 );
374 pager_seek(pPager->jfd, 0);
375 rc = pager_read(pPager->jfd, aMagic, sizeof(aMagic));
376 if( rc!=SQLITE_OK || memcmp(aMagic,aJournalMagic,sizeof(aMagic))!=0 ){
377 return SQLITE_PROTOCOL;
378 }
379 rc = pager_read(pPager->jfd, &mxPg, sizeof(mxPg));
380 if( rc!=SQLITE_OK ){
381 return SQLITE_PROTOCOL;
382 }
383 pager_truncate(pPager->fd, mxPg);
384 pPager->dbSize = mxPg;
385
386 /* Begin reading the journal beginning at the end and moving
387 ** toward the beginning.
388 */
389 if( fstat(pPager->jfd, &statbuf)!=0 ){
drhed7c8552001-04-11 14:29:21 +0000390 return SQLITE_OK;
391 }
drhd9b02572001-04-15 00:37:09 +0000392 nRec = (statbuf.st_size - (sizeof(aMagic)+sizeof(Pgno))) / sizeof(PageRecord);
drhed7c8552001-04-11 14:29:21 +0000393
394 /* Process segments beginning with the last and working backwards
395 ** to the first.
396 */
drhd9b02572001-04-15 00:37:09 +0000397 for(i=nRec-1; i>=0; i--){
drhed7c8552001-04-11 14:29:21 +0000398 /* Seek to the beginning of the segment */
drhd9b02572001-04-15 00:37:09 +0000399 off_t ofst;
400 ofst = i*sizeof(PageRecord) + sizeof(aMagic) + sizeof(Pgno);
401 rc = pager_seek(pPager->jfd, ofst);
402 if( rc!=SQLITE_OK ) break;
403 rc = pager_read(pPager->jfd, &pgRec, sizeof(pgRec));
404 if( rc!=SQLITE_OK ) break;
drhed7c8552001-04-11 14:29:21 +0000405
drhd9b02572001-04-15 00:37:09 +0000406 /* Sanity checking on the page */
407 if( pgRec.pgno>mxPg || pgRec.pgno==0 ){
408 rc = SQLITE_CORRUPT;
409 break;
drhed7c8552001-04-11 14:29:21 +0000410 }
411
drhd9b02572001-04-15 00:37:09 +0000412 /* Playback the page. Update the in-memory copy of the page
413 ** at the same time, if there is one.
drhed7c8552001-04-11 14:29:21 +0000414 */
drhd9b02572001-04-15 00:37:09 +0000415 pPg = pager_lookup(pPager, pgRec.pgno);
416 if( pPg ){
417 memcpy(PGHDR_TO_DATA(pPg), pgRec.aData, SQLITE_PAGE_SIZE);
drhed7c8552001-04-11 14:29:21 +0000418 }
drhd9b02572001-04-15 00:37:09 +0000419 rc = pager_seek(pPager->fd, (pgRec.pgno-1)*SQLITE_PAGE_SIZE);
420 if( rc!=SQLITE_OK ) break;
421 rc = pager_write(pPager->fd, pgRec.aData, SQLITE_PAGE_SIZE);
422 if( rc!=SQLITE_OK ) break;
drhed7c8552001-04-11 14:29:21 +0000423 }
drhd9b02572001-04-15 00:37:09 +0000424 if( rc!=SQLITE_OK ){
425 pager_unwritelock(pPager);
426 pPager->errMask |= PAGER_ERR_CORRUPT;
427 rc = SQLITE_CORRUPT;
428 }else{
429 rc = pager_unwritelock(pPager);
drhed7c8552001-04-11 14:29:21 +0000430 }
drhd9b02572001-04-15 00:37:09 +0000431 return rc;
drhed7c8552001-04-11 14:29:21 +0000432}
433
434/*
435** Create a new page cache and put a pointer to the page cache in *ppPager.
436** The file to be cached need not exist. The file is not opened until
drhd9b02572001-04-15 00:37:09 +0000437** the first call to sqlitepager_get() and is only held open until the
438** last page is released using sqlitepager_unref().
drhed7c8552001-04-11 14:29:21 +0000439*/
drh7e3b0a02001-04-28 16:52:40 +0000440int sqlitepager_open(
441 Pager **ppPager, /* Return the Pager structure here */
442 const char *zFilename, /* Name of the database file to open */
443 int mxPage, /* Max number of in-memory cache pages */
444 int nExtra /* Extra bytes append to each in-memory page */
445){
drhed7c8552001-04-11 14:29:21 +0000446 Pager *pPager;
447 int nameLen;
448 int fd;
449
drhd9b02572001-04-15 00:37:09 +0000450 *ppPager = 0;
451 if( sqlite_malloc_failed ){
452 return SQLITE_NOMEM;
453 }
454 fd = open(zFilename, O_RDWR|O_CREAT, 0644);
drhed7c8552001-04-11 14:29:21 +0000455 if( fd<0 ){
456 return SQLITE_CANTOPEN;
457 }
458 nameLen = strlen(zFilename);
459 pPager = sqliteMalloc( sizeof(*pPager) + nameLen*2 + 30 );
drhd9b02572001-04-15 00:37:09 +0000460 if( pPager==0 ){
461 close(fd);
462 return SQLITE_NOMEM;
463 }
drhed7c8552001-04-11 14:29:21 +0000464 pPager->zFilename = (char*)&pPager[1];
465 pPager->zJournal = &pPager->zFilename[nameLen+1];
466 strcpy(pPager->zFilename, zFilename);
467 strcpy(pPager->zJournal, zFilename);
468 strcpy(&pPager->zJournal[nameLen], "-journal");
469 pPager->fd = fd;
470 pPager->jfd = -1;
471 pPager->nRef = 0;
472 pPager->dbSize = -1;
473 pPager->nPage = 0;
drhd79caeb2001-04-15 02:27:24 +0000474 pPager->mxPage = mxPage>5 ? mxPage : 10;
drhed7c8552001-04-11 14:29:21 +0000475 pPager->state = SQLITE_UNLOCK;
drhd9b02572001-04-15 00:37:09 +0000476 pPager->errMask = 0;
drhed7c8552001-04-11 14:29:21 +0000477 pPager->pFirst = 0;
478 pPager->pLast = 0;
479 memset(pPager->aHash, 0, sizeof(pPager->aHash));
480 *ppPager = pPager;
481 return SQLITE_OK;
482}
483
484/*
drh72f82862001-05-24 21:06:34 +0000485** Set the destructor for this pager. If not NULL, the destructor is called
486** when the reference count on the page reaches zero.
487**
488** The destructor is not called as a result sqlitepager_close().
489** Destructors are only called by sqlitepager_unref().
490*/
491void sqlitepager_set_destructor(Pager *pPager, void (*xDesc)(void*)){
492 pPager->xDestructor = xDesc;
493}
494
495/*
drhed7c8552001-04-11 14:29:21 +0000496** Return the total number of pages in the file opened by pPager.
497*/
drhd9b02572001-04-15 00:37:09 +0000498int sqlitepager_pagecount(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +0000499 int n;
500 struct stat statbuf;
drhd9b02572001-04-15 00:37:09 +0000501 assert( pPager!=0 );
drhed7c8552001-04-11 14:29:21 +0000502 if( pPager->dbSize>=0 ){
503 return pPager->dbSize;
504 }
505 if( fstat(pPager->fd, &statbuf)!=0 ){
506 n = 0;
507 }else{
508 n = statbuf.st_size/SQLITE_PAGE_SIZE;
509 }
drhd9b02572001-04-15 00:37:09 +0000510 if( pPager->state!=SQLITE_UNLOCK ){
drhed7c8552001-04-11 14:29:21 +0000511 pPager->dbSize = n;
512 }
513 return n;
514}
515
516/*
517** Shutdown the page cache. Free all memory and close all files.
518**
519** If a transaction was in progress when this routine is called, that
520** transaction is rolled back. All outstanding pages are invalidated
521** and their memory is freed. Any attempt to use a page associated
522** with this page cache after this function returns will likely
523** result in a coredump.
524*/
drhd9b02572001-04-15 00:37:09 +0000525int sqlitepager_close(Pager *pPager){
526 PgHdr *pPg, *pNext;
drhed7c8552001-04-11 14:29:21 +0000527 switch( pPager->state ){
528 case SQLITE_WRITELOCK: {
drhd9b02572001-04-15 00:37:09 +0000529 sqlitepager_rollback(pPager);
530 pager_unlock(pPager->fd);
drhed7c8552001-04-11 14:29:21 +0000531 break;
532 }
533 case SQLITE_READLOCK: {
drhd9b02572001-04-15 00:37:09 +0000534 pager_unlock(pPager->fd);
drhed7c8552001-04-11 14:29:21 +0000535 break;
536 }
537 default: {
538 /* Do nothing */
539 break;
540 }
541 }
drhd9b02572001-04-15 00:37:09 +0000542 for(pPg=pPager->pAll; pPg; pPg=pNext){
543 pNext = pPg->pNextAll;
544 sqliteFree(pPg);
drhed7c8552001-04-11 14:29:21 +0000545 }
546 if( pPager->fd>=0 ) close(pPager->fd);
547 assert( pPager->jfd<0 );
548 sqliteFree(pPager);
549 return SQLITE_OK;
550}
551
552/*
553** Return the page number for the given page data
554*/
drhd9b02572001-04-15 00:37:09 +0000555Pgno sqlitepager_pagenumber(void *pData){
drhed7c8552001-04-11 14:29:21 +0000556 PgHdr *p = DATA_TO_PGHDR(pData);
557 return p->pgno;
558}
559
560/*
drh7e3b0a02001-04-28 16:52:40 +0000561** Increment the reference count for a page. If the page is
562** currently on the freelist (the reference count is zero) then
563** remove it from the freelist.
564*/
drhdf0b3b02001-06-23 11:36:20 +0000565static void page_ref(PgHdr *pPg){
drh7e3b0a02001-04-28 16:52:40 +0000566 if( pPg->nRef==0 ){
567 /* The page is currently on the freelist. Remove it. */
568 if( pPg->pPrevFree ){
569 pPg->pPrevFree->pNextFree = pPg->pNextFree;
570 }else{
571 pPg->pPager->pFirst = pPg->pNextFree;
572 }
573 if( pPg->pNextFree ){
574 pPg->pNextFree->pPrevFree = pPg->pPrevFree;
575 }else{
576 pPg->pPager->pLast = pPg->pPrevFree;
577 }
578 pPg->pPager->nRef++;
579 }
580 pPg->nRef++;
drhdf0b3b02001-06-23 11:36:20 +0000581}
582
583/*
584** Increment the reference count for a page. The input pointer is
585** a reference to the page data.
586*/
587int sqlitepager_ref(void *pData){
588 PgHdr *pPg = DATA_TO_PGHDR(pData);
589 page_ref(pPg);
drh8c42ca92001-06-22 19:15:00 +0000590 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +0000591}
592
593/*
drhd9b02572001-04-15 00:37:09 +0000594** Acquire a page.
595**
596** A read lock is obtained for the first page acquired. The lock
597** is dropped when the last page is released.
598**
drh306dc212001-05-21 13:45:10 +0000599** A _get works for any page number greater than 0. If the database
600** file is smaller than the requested page, then no actual disk
601** read occurs and the memory image of the page is initialized to
602** all zeros. The extra data appended to a page is always initialized
603** to zeros the first time a page is loaded into memory.
604**
drhd9b02572001-04-15 00:37:09 +0000605** The acquisition might fail for several reasons. In all cases,
606** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +0000607**
608** See also sqlitepager_lookup(). Both this routine and _lookup() attempt
609** to find a page in the in-memory cache first. If the page is not already
610** in cache, this routine goes to disk to read it in whereas _lookup()
611** just returns 0. This routine acquires a read-lock the first time it
612** has to go to disk, and could also playback an old journal if necessary.
613** Since _lookup() never goes to disk, it never has to deal with locks
614** or journal files.
drhed7c8552001-04-11 14:29:21 +0000615*/
drhd9b02572001-04-15 00:37:09 +0000616int sqlitepager_get(Pager *pPager, Pgno pgno, void **ppPage){
drhed7c8552001-04-11 14:29:21 +0000617 PgHdr *pPg;
618
drhd9b02572001-04-15 00:37:09 +0000619 /* Make sure we have not hit any critical errors.
620 */
621 if( pPager==0 || pgno==0 ){
622 return SQLITE_ERROR;
623 }
624 if( pPager->errMask & ~(PAGER_ERR_FULL) ){
625 return pager_errcode(pPager);
626 }
627
drhed7c8552001-04-11 14:29:21 +0000628 /* If this is the first page accessed, then get a read lock
629 ** on the database file.
630 */
631 if( pPager->nRef==0 ){
drhd9b02572001-04-15 00:37:09 +0000632 if( pager_lock(pPager->fd, 0)!=0 ){
drhed7c8552001-04-11 14:29:21 +0000633 *ppPage = 0;
634 return SQLITE_BUSY;
635 }
drhd9b02572001-04-15 00:37:09 +0000636 pPager->state = SQLITE_READLOCK;
drhed7c8552001-04-11 14:29:21 +0000637
638 /* If a journal file exists, try to play it back.
639 */
640 if( access(pPager->zJournal,0)==0 ){
641 int rc;
642
643 /* Open the journal for exclusive access. Return SQLITE_BUSY if
644 ** we cannot get exclusive access to the journal file
645 */
646 pPager->jfd = open(pPager->zJournal, O_RDONLY, 0);
drhd9b02572001-04-15 00:37:09 +0000647 if( pPager->jfd<0 || pager_lock(pPager->jfd, 1)!=0 ){
drhed7c8552001-04-11 14:29:21 +0000648 if( pPager->jfd>=0 ){ close(pPager->jfd); pPager->jfd = -1; }
drhd9b02572001-04-15 00:37:09 +0000649 pager_unlock(pPager->fd);
drhed7c8552001-04-11 14:29:21 +0000650 *ppPage = 0;
651 return SQLITE_BUSY;
652 }
653
654 /* Get a write lock on the database */
drhd9b02572001-04-15 00:37:09 +0000655 pager_unlock(pPager->fd);
656 if( pager_lock(pPager->fd, 1)!=0 ){
657 close(pPager->jfd);
658 pPager->jfd = -1;
drhed7c8552001-04-11 14:29:21 +0000659 *ppPage = 0;
660 return SQLITE_PROTOCOL;
661 }
662
663 /* Playback and delete the journal. Drop the database write
664 ** lock and reacquire the read lock.
665 */
drhd9b02572001-04-15 00:37:09 +0000666 rc = pager_playback(pPager);
667 if( rc!=SQLITE_OK ){
668 return rc;
669 }
drhed7c8552001-04-11 14:29:21 +0000670 }
671 pPg = 0;
672 }else{
673 /* Search for page in cache */
drhd9b02572001-04-15 00:37:09 +0000674 pPg = pager_lookup(pPager, pgno);
drhed7c8552001-04-11 14:29:21 +0000675 }
676 if( pPg==0 ){
drhd9b02572001-04-15 00:37:09 +0000677 /* The requested page is not in the page cache. */
drhed7c8552001-04-11 14:29:21 +0000678 int h;
drh7e3b0a02001-04-28 16:52:40 +0000679 pPager->nMiss++;
drhed7c8552001-04-11 14:29:21 +0000680 if( pPager->nPage<pPager->mxPage || pPager->pFirst==0 ){
681 /* Create a new page */
drh7e3b0a02001-04-28 16:52:40 +0000682 pPg = sqliteMalloc( sizeof(*pPg) + SQLITE_PAGE_SIZE + pPager->nExtra );
drhd9b02572001-04-15 00:37:09 +0000683 if( pPg==0 ){
684 *ppPage = 0;
685 pager_unwritelock(pPager);
686 pPager->errMask |= PAGER_ERR_MEM;
687 return SQLITE_NOMEM;
688 }
drhed7c8552001-04-11 14:29:21 +0000689 pPg->pPager = pPager;
drhd9b02572001-04-15 00:37:09 +0000690 pPg->pNextAll = pPager->pAll;
691 if( pPager->pAll ){
692 pPager->pAll->pPrevAll = pPg;
693 }
694 pPg->pPrevAll = 0;
drhd79caeb2001-04-15 02:27:24 +0000695 pPager->pAll = pPg;
drhd9b02572001-04-15 00:37:09 +0000696 pPager->nPage++;
drhed7c8552001-04-11 14:29:21 +0000697 }else{
drhd9b02572001-04-15 00:37:09 +0000698 /* Recycle an older page. First locate the page to be recycled.
699 ** Try to find one that is not dirty and is near the head of
700 ** of the free list */
701 int cnt = 4;
drhed7c8552001-04-11 14:29:21 +0000702 pPg = pPager->pFirst;
drhd9b02572001-04-15 00:37:09 +0000703 while( pPg->dirty && 0<cnt-- ){
704 pPg = pPg->pNextFree;
705 }
706 if( pPg==0 || pPg->dirty ) pPg = pPager->pFirst;
707 assert( pPg->nRef==0 );
708
709 /* If the page to be recycled is dirty, sync the journal and write
710 ** the old page into the database. */
drhed7c8552001-04-11 14:29:21 +0000711 if( pPg->dirty ){
712 int rc;
drhd9b02572001-04-15 00:37:09 +0000713 assert( pPg->inJournal==1 );
714 assert( pPager->state==SQLITE_WRITELOCK );
715 rc = fsync(pPager->jfd);
716 if( rc!=0 ){
717 rc = sqlitepager_rollback(pPager);
drhed7c8552001-04-11 14:29:21 +0000718 *ppPage = 0;
drhd9b02572001-04-15 00:37:09 +0000719 if( rc==SQLITE_OK ) rc = SQLITE_IOERR;
drhed7c8552001-04-11 14:29:21 +0000720 return rc;
721 }
drhd9b02572001-04-15 00:37:09 +0000722 pager_seek(pPager->fd, (pPg->pgno-1)*SQLITE_PAGE_SIZE);
723 rc = pager_write(pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE);
724 if( rc!=SQLITE_OK ){
725 rc = sqlitepager_rollback(pPager);
726 *ppPage = 0;
727 if( rc==SQLITE_OK ) rc = SQLITE_FULL;
728 return rc;
729 }
730 }
731
732 /* Unlink the old page from the free list and the hash table
733 */
734 pPager->pFirst = pPg->pNextFree;
drhed7c8552001-04-11 14:29:21 +0000735 if( pPager->pFirst ){
drhd9b02572001-04-15 00:37:09 +0000736 pPager->pFirst->pPrevFree = 0;
drhed7c8552001-04-11 14:29:21 +0000737 }else{
738 pPager->pLast = 0;
739 }
740 if( pPg->pNextHash ){
741 pPg->pNextHash->pPrevHash = pPg->pPrevHash;
742 }
743 if( pPg->pPrevHash ){
744 pPg->pPrevHash->pNextHash = pPg->pNextHash;
745 }else{
drhd9b02572001-04-15 00:37:09 +0000746 h = pager_hash(pPg->pgno);
drhed7c8552001-04-11 14:29:21 +0000747 assert( pPager->aHash[h]==pPg );
748 pPager->aHash[h] = pPg->pNextHash;
749 }
drhd9b02572001-04-15 00:37:09 +0000750 pPager->nOvfl++;
drhed7c8552001-04-11 14:29:21 +0000751 }
752 pPg->pgno = pgno;
753 pPg->inJournal = 0;
754 pPg->dirty = 0;
755 pPg->nRef = 1;
drhd9b02572001-04-15 00:37:09 +0000756 pPager->nRef++;
757 h = pager_hash(pgno);
drhed7c8552001-04-11 14:29:21 +0000758 pPg->pNextHash = pPager->aHash[h];
759 pPager->aHash[h] = pPg;
760 if( pPg->pNextHash ){
761 assert( pPg->pNextHash->pPrevHash==0 );
762 pPg->pNextHash->pPrevHash = pPg;
763 }
drh306dc212001-05-21 13:45:10 +0000764 if( pPager->dbSize<0 ) sqlitepager_pagecount(pPager);
765 if( pPager->dbSize<pgno ){
766 memset(PGHDR_TO_DATA(pPg), 0, SQLITE_PAGE_SIZE);
767 }else{
768 pager_seek(pPager->fd, (pgno-1)*SQLITE_PAGE_SIZE);
769 pager_read(pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE);
770 }
drh7e3b0a02001-04-28 16:52:40 +0000771 if( pPager->nExtra>0 ){
772 memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra);
773 }
drhed7c8552001-04-11 14:29:21 +0000774 }else{
drhd9b02572001-04-15 00:37:09 +0000775 /* The requested page is in the page cache. */
drh7e3b0a02001-04-28 16:52:40 +0000776 pPager->nHit++;
drhdf0b3b02001-06-23 11:36:20 +0000777 page_ref(pPg);
drhed7c8552001-04-11 14:29:21 +0000778 }
779 *ppPage = PGHDR_TO_DATA(pPg);
780 return SQLITE_OK;
781}
782
783/*
drh7e3b0a02001-04-28 16:52:40 +0000784** Acquire a page if it is already in the in-memory cache. Do
785** not read the page from disk. Return a pointer to the page,
786** or 0 if the page is not in cache.
787**
788** See also sqlitepager_get(). The difference between this routine
789** and sqlitepager_get() is that _get() will go to the disk and read
790** in the page if the page is not already in cache. This routine
drh306dc212001-05-21 13:45:10 +0000791** returns NULL if the page is not in cache of if a disk I/O has ever
792** happened.
drh7e3b0a02001-04-28 16:52:40 +0000793*/
794void *sqlitepager_lookup(Pager *pPager, Pgno pgno){
795 PgHdr *pPg;
796
797 /* Make sure we have not hit any critical errors.
798 */
799 if( pPager==0 || pgno==0 ){
800 return 0;
801 }
802 if( pPager->errMask & ~(PAGER_ERR_FULL) ){
803 return 0;
804 }
805 if( pPager->nRef==0 ){
806 return 0;
807 }
808 pPg = pager_lookup(pPager, pgno);
809 if( pPg==0 ) return 0;
drhdf0b3b02001-06-23 11:36:20 +0000810 page_ref(pPg);
drh7e3b0a02001-04-28 16:52:40 +0000811 return PGHDR_TO_DATA(pPg);
812}
813
814/*
drhed7c8552001-04-11 14:29:21 +0000815** Release a page.
816**
817** If the number of references to the page drop to zero, then the
818** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +0000819** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +0000820** removed.
821*/
drhd9b02572001-04-15 00:37:09 +0000822int sqlitepager_unref(void *pData){
drhed7c8552001-04-11 14:29:21 +0000823 Pager *pPager;
824 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +0000825
826 /* Decrement the reference count for this page
827 */
drhed7c8552001-04-11 14:29:21 +0000828 pPg = DATA_TO_PGHDR(pData);
829 assert( pPg->nRef>0 );
830 pPager = pPg->pPager;
831 pPg->nRef--;
drhd9b02572001-04-15 00:37:09 +0000832
drh72f82862001-05-24 21:06:34 +0000833 /* When the number of references to a page reach 0, call the
834 ** destructor and add the page to the freelist.
drhd9b02572001-04-15 00:37:09 +0000835 */
drhed7c8552001-04-11 14:29:21 +0000836 if( pPg->nRef==0 ){
drhd9b02572001-04-15 00:37:09 +0000837 pPg->pNextFree = 0;
838 pPg->pPrevFree = pPager->pLast;
drhed7c8552001-04-11 14:29:21 +0000839 pPager->pLast = pPg;
drhd9b02572001-04-15 00:37:09 +0000840 if( pPg->pPrevFree ){
841 pPg->pPrevFree->pNextFree = pPg;
drhed7c8552001-04-11 14:29:21 +0000842 }else{
843 pPager->pFirst = pPg;
844 }
drh72f82862001-05-24 21:06:34 +0000845 if( pPager->xDestructor ){
846 pPager->xDestructor(pData);
847 }
drhd9b02572001-04-15 00:37:09 +0000848
849 /* When all pages reach the freelist, drop the read lock from
850 ** the database file.
851 */
852 pPager->nRef--;
853 assert( pPager->nRef>=0 );
854 if( pPager->nRef==0 ){
855 pager_reset(pPager);
856 }
drhed7c8552001-04-11 14:29:21 +0000857 }
drhd9b02572001-04-15 00:37:09 +0000858 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +0000859}
860
861/*
862** Mark a data page as writeable. The page is written into the journal
863** if it is not there already. This routine must be called before making
864** changes to a page.
865**
866** The first time this routine is called, the pager creates a new
867** journal and acquires a write lock on the database. If the write
868** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +0000869** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +0000870** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +0000871**
872** If the journal file could not be written because the disk is full,
873** then this routine returns SQLITE_FULL and does an immediate rollback.
874** All subsequent write attempts also return SQLITE_FULL until there
875** is a call to sqlitepager_commit() or sqlitepager_rollback() to
876** reset.
drhed7c8552001-04-11 14:29:21 +0000877*/
drhd9b02572001-04-15 00:37:09 +0000878int sqlitepager_write(void *pData){
drh69688d52001-04-14 16:38:23 +0000879 PgHdr *pPg = DATA_TO_PGHDR(pData);
880 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +0000881 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +0000882
drhd9b02572001-04-15 00:37:09 +0000883 if( pPager->errMask ){
884 return pager_errcode(pPager);
885 }
886 pPg->dirty = 1;
drh69688d52001-04-14 16:38:23 +0000887 if( pPg->inJournal ){ return SQLITE_OK; }
drhd9b02572001-04-15 00:37:09 +0000888 assert( pPager->state!=SQLITE_UNLOCK );
drhed7c8552001-04-11 14:29:21 +0000889 if( pPager->state==SQLITE_READLOCK ){
890 pPager->jfd = open(pPager->zJournal, O_RDWR|O_CREAT, 0644);
891 if( pPager->jfd<0 ){
892 return SQLITE_CANTOPEN;
893 }
drhd9b02572001-04-15 00:37:09 +0000894 if( pager_lock(pPager->jfd, 1) ){
drhed7c8552001-04-11 14:29:21 +0000895 close(pPager->jfd);
896 pPager->jfd = -1;
897 return SQLITE_BUSY;
898 }
drhd9b02572001-04-15 00:37:09 +0000899 pager_unlock(pPager->fd);
900 if( pager_lock(pPager->fd, 1) ){
drhed7c8552001-04-11 14:29:21 +0000901 close(pPager->jfd);
902 pPager->jfd = -1;
903 pPager->state = SQLITE_UNLOCK;
drhd9b02572001-04-15 00:37:09 +0000904 pPager->errMask |= PAGER_ERR_LOCK;
drhed7c8552001-04-11 14:29:21 +0000905 return SQLITE_PROTOCOL;
906 }
907 pPager->state = SQLITE_WRITELOCK;
drhd9b02572001-04-15 00:37:09 +0000908 sqlitepager_pagecount(pPager);
drh69688d52001-04-14 16:38:23 +0000909 pPager->origDbSize = pPager->dbSize;
drhd9b02572001-04-15 00:37:09 +0000910 rc = pager_write(pPager->jfd, aJournalMagic, sizeof(aJournalMagic));
911 if( rc==SQLITE_OK ){
912 rc = pager_write(pPager->jfd, &pPager->dbSize, sizeof(Pgno));
913 }
914 if( rc!=SQLITE_OK ){
915 rc = pager_unwritelock(pPager);
916 if( rc==SQLITE_OK ) rc = SQLITE_FULL;
917 return rc;
918 }
drhed7c8552001-04-11 14:29:21 +0000919 }
drhd9b02572001-04-15 00:37:09 +0000920 assert( pPager->state==SQLITE_WRITELOCK );
drh69688d52001-04-14 16:38:23 +0000921 assert( pPager->jfd>=0 );
drhd9b02572001-04-15 00:37:09 +0000922 if( pPg->pgno <= pPager->origDbSize ){
923 rc = pager_write(pPager->jfd, &pPg->pgno, sizeof(Pgno));
924 if( rc==SQLITE_OK ){
925 rc = pager_write(pPager->jfd, pData, SQLITE_PAGE_SIZE);
926 }
927 if( rc!=SQLITE_OK ){
928 sqlitepager_rollback(pPager);
929 pPager->errMask |= PAGER_ERR_FULL;
930 return rc;
931 }
drh69688d52001-04-14 16:38:23 +0000932 }
drh69688d52001-04-14 16:38:23 +0000933 pPg->inJournal = 1;
drh306dc212001-05-21 13:45:10 +0000934 if( pPager->dbSize<pPg->pgno ){
935 pPager->dbSize = pPg->pgno;
936 }
drh69688d52001-04-14 16:38:23 +0000937 return rc;
drhed7c8552001-04-11 14:29:21 +0000938}
939
940/*
941** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +0000942**
943** If the commit fails for any reason, a rollback attempt is made
944** and an error code is returned. If the commit worked, SQLITE_OK
945** is returned.
drhed7c8552001-04-11 14:29:21 +0000946*/
drhd9b02572001-04-15 00:37:09 +0000947int sqlitepager_commit(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +0000948 int i, rc;
949 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +0000950
951 if( pPager->errMask==PAGER_ERR_FULL ){
952 rc = sqlitepager_rollback(pPager);
953 if( rc==SQLITE_OK ) rc = SQLITE_FULL;
954 return rc;
955 }
956 if( pPager->errMask!=0 ){
957 rc = pager_errcode(pPager);
958 return rc;
959 }
960 if( pPager->state!=SQLITE_WRITELOCK ){
961 return SQLITE_ERROR;
962 }
drhed7c8552001-04-11 14:29:21 +0000963 assert( pPager->jfd>=0 );
964 if( fsync(pPager->jfd) ){
drhd9b02572001-04-15 00:37:09 +0000965 goto commit_abort;
drhed7c8552001-04-11 14:29:21 +0000966 }
967 for(i=0; i<N_PG_HASH; i++){
968 for(pPg=pPager->aHash[i]; pPg; pPg=pPg->pNextHash){
969 if( pPg->dirty==0 ) continue;
drhd9b02572001-04-15 00:37:09 +0000970 rc = pager_seek(pPager->fd, (pPg->pgno-1)*SQLITE_PAGE_SIZE);
971 if( rc!=SQLITE_OK ) goto commit_abort;
972 rc = pager_write(pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE);
973 if( rc!=SQLITE_OK ) goto commit_abort;
drhed7c8552001-04-11 14:29:21 +0000974 }
975 }
drhd9b02572001-04-15 00:37:09 +0000976 if( fsync(pPager->fd) ) goto commit_abort;
977 rc = pager_unwritelock(pPager);
978 pPager->dbSize = -1;
979 return rc;
980
981 /* Jump here if anything goes wrong during the commit process.
982 */
983commit_abort:
984 rc = sqlitepager_rollback(pPager);
985 if( rc==SQLITE_OK ){
986 rc = SQLITE_FULL;
drhed7c8552001-04-11 14:29:21 +0000987 }
drhed7c8552001-04-11 14:29:21 +0000988 return rc;
989}
990
991/*
992** Rollback all changes. The database falls back to read-only mode.
993** All in-memory cache pages revert to their original data contents.
994** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +0000995**
996** This routine cannot fail unless some other process is not following
997** the correct locking protocol (SQLITE_PROTOCOL) or unless some other
998** process is writing trash into the journal file (SQLITE_CORRUPT) or
999** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
1000** codes are returned for all these occasions. Otherwise,
1001** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00001002*/
drhd9b02572001-04-15 00:37:09 +00001003int sqlitepager_rollback(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00001004 int rc;
drhd9b02572001-04-15 00:37:09 +00001005 if( pPager->errMask!=0 && pPager->errMask!=PAGER_ERR_FULL ){
1006 return pager_errcode(pPager);
drhed7c8552001-04-11 14:29:21 +00001007 }
drhd9b02572001-04-15 00:37:09 +00001008 if( pPager->state!=SQLITE_WRITELOCK ){
1009 return SQLITE_OK;
1010 }
1011 rc = pager_playback(pPager);
1012 if( rc!=SQLITE_OK ){
1013 rc = SQLITE_CORRUPT;
1014 pPager->errMask |= PAGER_ERR_CORRUPT;
1015 }
1016 pPager->dbSize = -1;
drhed7c8552001-04-11 14:29:21 +00001017 return rc;
1018};
drhd9b02572001-04-15 00:37:09 +00001019
1020/*
1021** This routine is used for testing and analysis only.
1022*/
1023int *sqlitepager_stats(Pager *pPager){
1024 static int a[9];
1025 a[0] = pPager->nRef;
1026 a[1] = pPager->nPage;
1027 a[2] = pPager->mxPage;
1028 a[3] = pPager->dbSize;
1029 a[4] = pPager->state;
1030 a[5] = pPager->errMask;
1031 a[6] = pPager->nHit;
1032 a[7] = pPager->nMiss;
1033 a[8] = pPager->nOvfl;
1034 return a;
1035}