blob: ebd641f34223a9b28905010aca25b72044ef4842 [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**
drhdd793422001-06-28 01:54:48 +000030** @(#) $Id: pager.c,v 1.12 2001/06/28 01:54:49 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/*
drhdd793422001-06-28 01:54:48 +0000162** Enable reference count tracking here:
163*/
164#if SQLITE_TEST
165int pager_refinfo_enable = 0;
166 static void pager_refinfo(PgHdr *p){
167 static int cnt = 0;
168 if( !pager_refinfo_enable ) return;
169 printf(
170 "REFCNT: %4d addr=0x%08x nRef=%d\n",
171 p->pgno, (int)PGHDR_TO_DATA(p), p->nRef
172 );
173 cnt++; /* Something to set a breakpoint on */
174 }
175# define REFINFO(X) pager_refinfo(X)
176#else
177# define REFINFO(X)
178#endif
179
180/*
drhed7c8552001-04-11 14:29:21 +0000181** Attempt to acquire a read lock (if wrlock==0) or a write lock (if wrlock==1)
182** on the database file. Return 0 on success and non-zero if the lock
183** could not be acquired.
184*/
drhd9b02572001-04-15 00:37:09 +0000185static int pager_lock(int fd, int wrlock){
186 int rc;
drhed7c8552001-04-11 14:29:21 +0000187 struct flock lock;
drhd9b02572001-04-15 00:37:09 +0000188 lock.l_type = wrlock ? F_WRLCK : F_RDLCK;
189 lock.l_whence = SEEK_SET;
190 lock.l_start = lock.l_len = 0L;
191 rc = fcntl(fd, F_SETLK, &lock);
192 return rc!=0;
drhed7c8552001-04-11 14:29:21 +0000193}
194
195/*
196** Unlock the database file.
197*/
drhd9b02572001-04-15 00:37:09 +0000198static int pager_unlock(fd){
199 int rc;
drhed7c8552001-04-11 14:29:21 +0000200 struct flock lock;
201 lock.l_type = F_UNLCK;
drhd9b02572001-04-15 00:37:09 +0000202 lock.l_whence = SEEK_SET;
203 lock.l_start = lock.l_len = 0L;
204 rc = fcntl(fd, F_SETLK, &lock);
205 return rc!=0;
206}
207
208/*
209** Move the cursor for file descriptor fd to the point whereto from
210** the beginning of the file.
211*/
212static int pager_seek(int fd, off_t whereto){
213 lseek(fd, whereto, SEEK_SET);
214 return SQLITE_OK;
215}
216
217/*
218** Truncate the given file so that it contains exactly mxPg pages
219** of data.
220*/
221static int pager_truncate(int fd, Pgno mxPg){
222 int rc;
223 rc = ftruncate(fd, mxPg*SQLITE_PAGE_SIZE);
224 return rc!=0 ? SQLITE_IOERR : SQLITE_OK;
225}
226
227/*
228** Read nBytes of data from fd into pBuf. If the data cannot be
229** read or only a partial read occurs, then the unread parts of
230** pBuf are filled with zeros and this routine returns SQLITE_IOERR.
231** If the read is completely successful, return SQLITE_OK.
232*/
233static int pager_read(int fd, void *pBuf, int nByte){
234 int rc;
235 rc = read(fd, pBuf, nByte);
236 if( rc<0 ){
237 memset(pBuf, 0, nByte);
238 return SQLITE_IOERR;
239 }
240 if( rc<nByte ){
241 memset(&((char*)pBuf)[rc], 0, nByte - rc);
242 rc = SQLITE_IOERR;
243 }else{
244 rc = SQLITE_OK;
245 }
246 return rc;
247}
248
249/*
250** Write nBytes of data into fd. If any problem occurs or if the
251** write is incomplete, SQLITE_IOERR is returned. SQLITE_OK is
252** returned upon complete success.
253*/
254static int pager_write(int fd, const void *pBuf, int nByte){
255 int rc;
256 rc = write(fd, pBuf, nByte);
257 if( rc<nByte ){
258 return SQLITE_FULL;
259 }else{
260 return SQLITE_OK;
261 }
262}
263
264/*
265** Convert the bits in the pPager->errMask into an approprate
266** return code.
267*/
268static int pager_errcode(Pager *pPager){
269 int rc = SQLITE_OK;
270 if( pPager->errMask & PAGER_ERR_LOCK ) rc = SQLITE_PROTOCOL;
271 if( pPager->errMask & PAGER_ERR_FULL ) rc = SQLITE_FULL;
272 if( pPager->errMask & PAGER_ERR_MEM ) rc = SQLITE_NOMEM;
273 if( pPager->errMask & PAGER_ERR_CORRUPT ) rc = SQLITE_CORRUPT;
274 return rc;
drhed7c8552001-04-11 14:29:21 +0000275}
276
277/*
278** Find a page in the hash table given its page number. Return
279** a pointer to the page or NULL if not found.
280*/
drhd9b02572001-04-15 00:37:09 +0000281static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
drhed7c8552001-04-11 14:29:21 +0000282 PgHdr *p = pPager->aHash[pgno % N_PG_HASH];
283 while( p && p->pgno!=pgno ){
284 p = p->pNextHash;
285 }
286 return p;
287}
288
289/*
290** Unlock the database and clear the in-memory cache. This routine
291** sets the state of the pager back to what it was when it was first
292** opened. Any outstanding pages are invalidated and subsequent attempts
293** to access those pages will likely result in a coredump.
294*/
drhd9b02572001-04-15 00:37:09 +0000295static void pager_reset(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +0000296 PgHdr *pPg, *pNext;
drhd9b02572001-04-15 00:37:09 +0000297 for(pPg=pPager->pAll; pPg; pPg=pNext){
298 pNext = pPg->pNextAll;
299 sqliteFree(pPg);
drhed7c8552001-04-11 14:29:21 +0000300 }
301 pPager->pFirst = 0;
drhd9b02572001-04-15 00:37:09 +0000302 pPager->pLast = 0;
303 pPager->pAll = 0;
drhed7c8552001-04-11 14:29:21 +0000304 memset(pPager->aHash, 0, sizeof(pPager->aHash));
305 pPager->nPage = 0;
306 if( pPager->state==SQLITE_WRITELOCK ){
drhd9b02572001-04-15 00:37:09 +0000307 sqlitepager_rollback(pPager);
drhed7c8552001-04-11 14:29:21 +0000308 }
drhd9b02572001-04-15 00:37:09 +0000309 pager_unlock(pPager->fd);
drhed7c8552001-04-11 14:29:21 +0000310 pPager->state = SQLITE_UNLOCK;
drhd9b02572001-04-15 00:37:09 +0000311 pPager->dbSize = -1;
drhed7c8552001-04-11 14:29:21 +0000312 pPager->nRef = 0;
313}
314
315/*
316** When this routine is called, the pager has the journal file open and
317** a write lock on the database. This routine releases the database
318** write lock and acquires a read lock in its place. The journal file
319** is deleted and closed.
320**
321** We have to release the write lock before acquiring the read lock,
322** so there is a race condition where another process can get the lock
323** while we are not holding it. But, no other process should do this
324** because we are also holding a lock on the journal, and no process
325** should get a write lock on the database without first getting a lock
326** on the journal. So this routine should never fail. But it can fail
327** if another process is not playing by the rules. If it does fail,
drhd9b02572001-04-15 00:37:09 +0000328** all in-memory cache pages are invalidated, the PAGER_ERR_LOCK bit
329** is set in pPager->errMask, and this routine returns SQLITE_PROTOCOL.
330** SQLITE_OK is returned on success.
drhed7c8552001-04-11 14:29:21 +0000331*/
drhd9b02572001-04-15 00:37:09 +0000332static int pager_unwritelock(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +0000333 int rc;
drhd9b02572001-04-15 00:37:09 +0000334 PgHdr *pPg;
335 if( pPager->state!=SQLITE_WRITELOCK ) return SQLITE_OK;
336 pager_unlock(pPager->fd);
337 rc = pager_lock(pPager->fd, 0);
drhed7c8552001-04-11 14:29:21 +0000338 unlink(pPager->zJournal);
339 close(pPager->jfd);
340 pPager->jfd = -1;
drhd9b02572001-04-15 00:37:09 +0000341 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
342 pPg->inJournal = 0;
343 pPg->dirty = 0;
344 }
drhed7c8552001-04-11 14:29:21 +0000345 if( rc!=SQLITE_OK ){
346 pPager->state = SQLITE_UNLOCK;
drhed7c8552001-04-11 14:29:21 +0000347 rc = SQLITE_PROTOCOL;
drhd9b02572001-04-15 00:37:09 +0000348 pPager->errMask |= PAGER_ERR_LOCK;
drhed7c8552001-04-11 14:29:21 +0000349 }else{
drhd9b02572001-04-15 00:37:09 +0000350 rc = SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +0000351 pPager->state = SQLITE_READLOCK;
352 }
353 return rc;
354}
355
drhed7c8552001-04-11 14:29:21 +0000356/*
357** Playback the journal and thus restore the database file to
358** the state it was in before we started making changes.
359**
drhd9b02572001-04-15 00:37:09 +0000360** The journal file format is as follows: There is an initial
361** file-type string for sanity checking. Then there is a single
362** Pgno number which is the number of pages in the database before
363** changes were made. The database is truncated to this size.
drh306dc212001-05-21 13:45:10 +0000364** Next come zero or more page records where each page record
365** consists of a Pgno and SQLITE_PAGE_SIZE bytes of data. See
366** the PageRecord structure for details.
drhed7c8552001-04-11 14:29:21 +0000367**
drhd9b02572001-04-15 00:37:09 +0000368** For playback, the pages have to be read from the journal in
369** reverse order and put back into the original database file.
drhed7c8552001-04-11 14:29:21 +0000370**
drhd9b02572001-04-15 00:37:09 +0000371** If the file opened as the journal file is not a well-formed
372** journal file (as determined by looking at the magic number
373** at the beginning) then this routine returns SQLITE_PROTOCOL.
374** If any other errors occur during playback, the database will
375** likely be corrupted, so the PAGER_ERR_CORRUPT bit is set in
376** pPager->errMask and SQLITE_CORRUPT is returned. If it all
377** works, then this routine returns SQLITE_OK.
drhed7c8552001-04-11 14:29:21 +0000378*/
drhd9b02572001-04-15 00:37:09 +0000379static int pager_playback(Pager *pPager){
380 int nRec; /* Number of Records */
381 int i; /* Loop counter */
382 Pgno mxPg = 0; /* Size of the original file in pages */
383 struct stat statbuf; /* Used to size the journal */
384 PgHdr *pPg; /* An existing page in the cache */
385 PageRecord pgRec;
386 unsigned char aMagic[sizeof(aJournalMagic)];
drhed7c8552001-04-11 14:29:21 +0000387 int rc;
388
drhd9b02572001-04-15 00:37:09 +0000389 /* Read the beginning of the journal and truncate the
390 ** database file back to its original size.
drhed7c8552001-04-11 14:29:21 +0000391 */
drhd9b02572001-04-15 00:37:09 +0000392 assert( pPager->jfd>=0 );
393 pager_seek(pPager->jfd, 0);
394 rc = pager_read(pPager->jfd, aMagic, sizeof(aMagic));
395 if( rc!=SQLITE_OK || memcmp(aMagic,aJournalMagic,sizeof(aMagic))!=0 ){
396 return SQLITE_PROTOCOL;
397 }
398 rc = pager_read(pPager->jfd, &mxPg, sizeof(mxPg));
399 if( rc!=SQLITE_OK ){
400 return SQLITE_PROTOCOL;
401 }
402 pager_truncate(pPager->fd, mxPg);
403 pPager->dbSize = mxPg;
404
405 /* Begin reading the journal beginning at the end and moving
406 ** toward the beginning.
407 */
408 if( fstat(pPager->jfd, &statbuf)!=0 ){
drhed7c8552001-04-11 14:29:21 +0000409 return SQLITE_OK;
410 }
drhd9b02572001-04-15 00:37:09 +0000411 nRec = (statbuf.st_size - (sizeof(aMagic)+sizeof(Pgno))) / sizeof(PageRecord);
drhed7c8552001-04-11 14:29:21 +0000412
413 /* Process segments beginning with the last and working backwards
414 ** to the first.
415 */
drhd9b02572001-04-15 00:37:09 +0000416 for(i=nRec-1; i>=0; i--){
drhed7c8552001-04-11 14:29:21 +0000417 /* Seek to the beginning of the segment */
drhd9b02572001-04-15 00:37:09 +0000418 off_t ofst;
419 ofst = i*sizeof(PageRecord) + sizeof(aMagic) + sizeof(Pgno);
420 rc = pager_seek(pPager->jfd, ofst);
421 if( rc!=SQLITE_OK ) break;
422 rc = pager_read(pPager->jfd, &pgRec, sizeof(pgRec));
423 if( rc!=SQLITE_OK ) break;
drhed7c8552001-04-11 14:29:21 +0000424
drhd9b02572001-04-15 00:37:09 +0000425 /* Sanity checking on the page */
426 if( pgRec.pgno>mxPg || pgRec.pgno==0 ){
427 rc = SQLITE_CORRUPT;
428 break;
drhed7c8552001-04-11 14:29:21 +0000429 }
430
drhd9b02572001-04-15 00:37:09 +0000431 /* Playback the page. Update the in-memory copy of the page
432 ** at the same time, if there is one.
drhed7c8552001-04-11 14:29:21 +0000433 */
drhd9b02572001-04-15 00:37:09 +0000434 pPg = pager_lookup(pPager, pgRec.pgno);
435 if( pPg ){
436 memcpy(PGHDR_TO_DATA(pPg), pgRec.aData, SQLITE_PAGE_SIZE);
drhed7c8552001-04-11 14:29:21 +0000437 }
drhd9b02572001-04-15 00:37:09 +0000438 rc = pager_seek(pPager->fd, (pgRec.pgno-1)*SQLITE_PAGE_SIZE);
439 if( rc!=SQLITE_OK ) break;
440 rc = pager_write(pPager->fd, pgRec.aData, SQLITE_PAGE_SIZE);
441 if( rc!=SQLITE_OK ) break;
drhed7c8552001-04-11 14:29:21 +0000442 }
drhd9b02572001-04-15 00:37:09 +0000443 if( rc!=SQLITE_OK ){
444 pager_unwritelock(pPager);
445 pPager->errMask |= PAGER_ERR_CORRUPT;
446 rc = SQLITE_CORRUPT;
447 }else{
448 rc = pager_unwritelock(pPager);
drhed7c8552001-04-11 14:29:21 +0000449 }
drhd9b02572001-04-15 00:37:09 +0000450 return rc;
drhed7c8552001-04-11 14:29:21 +0000451}
452
453/*
454** Create a new page cache and put a pointer to the page cache in *ppPager.
455** The file to be cached need not exist. The file is not opened until
drhd9b02572001-04-15 00:37:09 +0000456** the first call to sqlitepager_get() and is only held open until the
457** last page is released using sqlitepager_unref().
drhed7c8552001-04-11 14:29:21 +0000458*/
drh7e3b0a02001-04-28 16:52:40 +0000459int sqlitepager_open(
460 Pager **ppPager, /* Return the Pager structure here */
461 const char *zFilename, /* Name of the database file to open */
462 int mxPage, /* Max number of in-memory cache pages */
463 int nExtra /* Extra bytes append to each in-memory page */
464){
drhed7c8552001-04-11 14:29:21 +0000465 Pager *pPager;
466 int nameLen;
467 int fd;
468
drhd9b02572001-04-15 00:37:09 +0000469 *ppPager = 0;
470 if( sqlite_malloc_failed ){
471 return SQLITE_NOMEM;
472 }
473 fd = open(zFilename, O_RDWR|O_CREAT, 0644);
drhed7c8552001-04-11 14:29:21 +0000474 if( fd<0 ){
475 return SQLITE_CANTOPEN;
476 }
477 nameLen = strlen(zFilename);
478 pPager = sqliteMalloc( sizeof(*pPager) + nameLen*2 + 30 );
drhd9b02572001-04-15 00:37:09 +0000479 if( pPager==0 ){
480 close(fd);
481 return SQLITE_NOMEM;
482 }
drhed7c8552001-04-11 14:29:21 +0000483 pPager->zFilename = (char*)&pPager[1];
484 pPager->zJournal = &pPager->zFilename[nameLen+1];
485 strcpy(pPager->zFilename, zFilename);
486 strcpy(pPager->zJournal, zFilename);
487 strcpy(&pPager->zJournal[nameLen], "-journal");
488 pPager->fd = fd;
489 pPager->jfd = -1;
490 pPager->nRef = 0;
491 pPager->dbSize = -1;
492 pPager->nPage = 0;
drhd79caeb2001-04-15 02:27:24 +0000493 pPager->mxPage = mxPage>5 ? mxPage : 10;
drhed7c8552001-04-11 14:29:21 +0000494 pPager->state = SQLITE_UNLOCK;
drhd9b02572001-04-15 00:37:09 +0000495 pPager->errMask = 0;
drhed7c8552001-04-11 14:29:21 +0000496 pPager->pFirst = 0;
497 pPager->pLast = 0;
drh7c717f72001-06-24 20:39:41 +0000498 pPager->nExtra = nExtra;
drhed7c8552001-04-11 14:29:21 +0000499 memset(pPager->aHash, 0, sizeof(pPager->aHash));
500 *ppPager = pPager;
501 return SQLITE_OK;
502}
503
504/*
drh72f82862001-05-24 21:06:34 +0000505** Set the destructor for this pager. If not NULL, the destructor is called
506** when the reference count on the page reaches zero.
507**
508** The destructor is not called as a result sqlitepager_close().
509** Destructors are only called by sqlitepager_unref().
510*/
511void sqlitepager_set_destructor(Pager *pPager, void (*xDesc)(void*)){
512 pPager->xDestructor = xDesc;
513}
514
515/*
drhed7c8552001-04-11 14:29:21 +0000516** Return the total number of pages in the file opened by pPager.
517*/
drhd9b02572001-04-15 00:37:09 +0000518int sqlitepager_pagecount(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +0000519 int n;
520 struct stat statbuf;
drhd9b02572001-04-15 00:37:09 +0000521 assert( pPager!=0 );
drhed7c8552001-04-11 14:29:21 +0000522 if( pPager->dbSize>=0 ){
523 return pPager->dbSize;
524 }
525 if( fstat(pPager->fd, &statbuf)!=0 ){
526 n = 0;
527 }else{
528 n = statbuf.st_size/SQLITE_PAGE_SIZE;
529 }
drhd9b02572001-04-15 00:37:09 +0000530 if( pPager->state!=SQLITE_UNLOCK ){
drhed7c8552001-04-11 14:29:21 +0000531 pPager->dbSize = n;
532 }
533 return n;
534}
535
536/*
537** Shutdown the page cache. Free all memory and close all files.
538**
539** If a transaction was in progress when this routine is called, that
540** transaction is rolled back. All outstanding pages are invalidated
541** and their memory is freed. Any attempt to use a page associated
542** with this page cache after this function returns will likely
543** result in a coredump.
544*/
drhd9b02572001-04-15 00:37:09 +0000545int sqlitepager_close(Pager *pPager){
546 PgHdr *pPg, *pNext;
drhed7c8552001-04-11 14:29:21 +0000547 switch( pPager->state ){
548 case SQLITE_WRITELOCK: {
drhd9b02572001-04-15 00:37:09 +0000549 sqlitepager_rollback(pPager);
550 pager_unlock(pPager->fd);
drhed7c8552001-04-11 14:29:21 +0000551 break;
552 }
553 case SQLITE_READLOCK: {
drhd9b02572001-04-15 00:37:09 +0000554 pager_unlock(pPager->fd);
drhed7c8552001-04-11 14:29:21 +0000555 break;
556 }
557 default: {
558 /* Do nothing */
559 break;
560 }
561 }
drhd9b02572001-04-15 00:37:09 +0000562 for(pPg=pPager->pAll; pPg; pPg=pNext){
563 pNext = pPg->pNextAll;
564 sqliteFree(pPg);
drhed7c8552001-04-11 14:29:21 +0000565 }
566 if( pPager->fd>=0 ) close(pPager->fd);
567 assert( pPager->jfd<0 );
568 sqliteFree(pPager);
569 return SQLITE_OK;
570}
571
572/*
573** Return the page number for the given page data
574*/
drhd9b02572001-04-15 00:37:09 +0000575Pgno sqlitepager_pagenumber(void *pData){
drhed7c8552001-04-11 14:29:21 +0000576 PgHdr *p = DATA_TO_PGHDR(pData);
577 return p->pgno;
578}
579
580/*
drh7e3b0a02001-04-28 16:52:40 +0000581** Increment the reference count for a page. If the page is
582** currently on the freelist (the reference count is zero) then
583** remove it from the freelist.
584*/
drhdf0b3b02001-06-23 11:36:20 +0000585static void page_ref(PgHdr *pPg){
drh7e3b0a02001-04-28 16:52:40 +0000586 if( pPg->nRef==0 ){
587 /* The page is currently on the freelist. Remove it. */
588 if( pPg->pPrevFree ){
589 pPg->pPrevFree->pNextFree = pPg->pNextFree;
590 }else{
591 pPg->pPager->pFirst = pPg->pNextFree;
592 }
593 if( pPg->pNextFree ){
594 pPg->pNextFree->pPrevFree = pPg->pPrevFree;
595 }else{
596 pPg->pPager->pLast = pPg->pPrevFree;
597 }
598 pPg->pPager->nRef++;
599 }
600 pPg->nRef++;
drhdd793422001-06-28 01:54:48 +0000601 REFINFO(pPg);
drhdf0b3b02001-06-23 11:36:20 +0000602}
603
604/*
605** Increment the reference count for a page. The input pointer is
606** a reference to the page data.
607*/
608int sqlitepager_ref(void *pData){
609 PgHdr *pPg = DATA_TO_PGHDR(pData);
610 page_ref(pPg);
drh8c42ca92001-06-22 19:15:00 +0000611 return SQLITE_OK;
drh7e3b0a02001-04-28 16:52:40 +0000612}
613
614/*
drhd9b02572001-04-15 00:37:09 +0000615** Acquire a page.
616**
617** A read lock is obtained for the first page acquired. The lock
618** is dropped when the last page is released.
619**
drh306dc212001-05-21 13:45:10 +0000620** A _get works for any page number greater than 0. If the database
621** file is smaller than the requested page, then no actual disk
622** read occurs and the memory image of the page is initialized to
623** all zeros. The extra data appended to a page is always initialized
624** to zeros the first time a page is loaded into memory.
625**
drhd9b02572001-04-15 00:37:09 +0000626** The acquisition might fail for several reasons. In all cases,
627** an appropriate error code is returned and *ppPage is set to NULL.
drh7e3b0a02001-04-28 16:52:40 +0000628**
629** See also sqlitepager_lookup(). Both this routine and _lookup() attempt
630** to find a page in the in-memory cache first. If the page is not already
631** in cache, this routine goes to disk to read it in whereas _lookup()
632** just returns 0. This routine acquires a read-lock the first time it
633** has to go to disk, and could also playback an old journal if necessary.
634** Since _lookup() never goes to disk, it never has to deal with locks
635** or journal files.
drhed7c8552001-04-11 14:29:21 +0000636*/
drhd9b02572001-04-15 00:37:09 +0000637int sqlitepager_get(Pager *pPager, Pgno pgno, void **ppPage){
drhed7c8552001-04-11 14:29:21 +0000638 PgHdr *pPg;
639
drhd9b02572001-04-15 00:37:09 +0000640 /* Make sure we have not hit any critical errors.
641 */
642 if( pPager==0 || pgno==0 ){
643 return SQLITE_ERROR;
644 }
645 if( pPager->errMask & ~(PAGER_ERR_FULL) ){
646 return pager_errcode(pPager);
647 }
648
drhed7c8552001-04-11 14:29:21 +0000649 /* If this is the first page accessed, then get a read lock
650 ** on the database file.
651 */
652 if( pPager->nRef==0 ){
drhd9b02572001-04-15 00:37:09 +0000653 if( pager_lock(pPager->fd, 0)!=0 ){
drhed7c8552001-04-11 14:29:21 +0000654 *ppPage = 0;
655 return SQLITE_BUSY;
656 }
drhd9b02572001-04-15 00:37:09 +0000657 pPager->state = SQLITE_READLOCK;
drhed7c8552001-04-11 14:29:21 +0000658
659 /* If a journal file exists, try to play it back.
660 */
661 if( access(pPager->zJournal,0)==0 ){
662 int rc;
663
664 /* Open the journal for exclusive access. Return SQLITE_BUSY if
665 ** we cannot get exclusive access to the journal file
666 */
667 pPager->jfd = open(pPager->zJournal, O_RDONLY, 0);
drhd9b02572001-04-15 00:37:09 +0000668 if( pPager->jfd<0 || pager_lock(pPager->jfd, 1)!=0 ){
drhed7c8552001-04-11 14:29:21 +0000669 if( pPager->jfd>=0 ){ close(pPager->jfd); pPager->jfd = -1; }
drhd9b02572001-04-15 00:37:09 +0000670 pager_unlock(pPager->fd);
drhed7c8552001-04-11 14:29:21 +0000671 *ppPage = 0;
672 return SQLITE_BUSY;
673 }
674
675 /* Get a write lock on the database */
drhd9b02572001-04-15 00:37:09 +0000676 pager_unlock(pPager->fd);
677 if( pager_lock(pPager->fd, 1)!=0 ){
678 close(pPager->jfd);
679 pPager->jfd = -1;
drhed7c8552001-04-11 14:29:21 +0000680 *ppPage = 0;
681 return SQLITE_PROTOCOL;
682 }
683
684 /* Playback and delete the journal. Drop the database write
685 ** lock and reacquire the read lock.
686 */
drhd9b02572001-04-15 00:37:09 +0000687 rc = pager_playback(pPager);
688 if( rc!=SQLITE_OK ){
689 return rc;
690 }
drhed7c8552001-04-11 14:29:21 +0000691 }
692 pPg = 0;
693 }else{
694 /* Search for page in cache */
drhd9b02572001-04-15 00:37:09 +0000695 pPg = pager_lookup(pPager, pgno);
drhed7c8552001-04-11 14:29:21 +0000696 }
697 if( pPg==0 ){
drhd9b02572001-04-15 00:37:09 +0000698 /* The requested page is not in the page cache. */
drhed7c8552001-04-11 14:29:21 +0000699 int h;
drh7e3b0a02001-04-28 16:52:40 +0000700 pPager->nMiss++;
drhed7c8552001-04-11 14:29:21 +0000701 if( pPager->nPage<pPager->mxPage || pPager->pFirst==0 ){
702 /* Create a new page */
drh7e3b0a02001-04-28 16:52:40 +0000703 pPg = sqliteMalloc( sizeof(*pPg) + SQLITE_PAGE_SIZE + pPager->nExtra );
drhd9b02572001-04-15 00:37:09 +0000704 if( pPg==0 ){
705 *ppPage = 0;
706 pager_unwritelock(pPager);
707 pPager->errMask |= PAGER_ERR_MEM;
708 return SQLITE_NOMEM;
709 }
drhed7c8552001-04-11 14:29:21 +0000710 pPg->pPager = pPager;
drhd9b02572001-04-15 00:37:09 +0000711 pPg->pNextAll = pPager->pAll;
712 if( pPager->pAll ){
713 pPager->pAll->pPrevAll = pPg;
714 }
715 pPg->pPrevAll = 0;
drhd79caeb2001-04-15 02:27:24 +0000716 pPager->pAll = pPg;
drhd9b02572001-04-15 00:37:09 +0000717 pPager->nPage++;
drhed7c8552001-04-11 14:29:21 +0000718 }else{
drhd9b02572001-04-15 00:37:09 +0000719 /* Recycle an older page. First locate the page to be recycled.
720 ** Try to find one that is not dirty and is near the head of
721 ** of the free list */
722 int cnt = 4;
drhed7c8552001-04-11 14:29:21 +0000723 pPg = pPager->pFirst;
drhd9b02572001-04-15 00:37:09 +0000724 while( pPg->dirty && 0<cnt-- ){
725 pPg = pPg->pNextFree;
726 }
727 if( pPg==0 || pPg->dirty ) pPg = pPager->pFirst;
728 assert( pPg->nRef==0 );
729
730 /* If the page to be recycled is dirty, sync the journal and write
731 ** the old page into the database. */
drhed7c8552001-04-11 14:29:21 +0000732 if( pPg->dirty ){
733 int rc;
drhd9b02572001-04-15 00:37:09 +0000734 assert( pPg->inJournal==1 );
735 assert( pPager->state==SQLITE_WRITELOCK );
736 rc = fsync(pPager->jfd);
737 if( rc!=0 ){
738 rc = sqlitepager_rollback(pPager);
drhed7c8552001-04-11 14:29:21 +0000739 *ppPage = 0;
drhd9b02572001-04-15 00:37:09 +0000740 if( rc==SQLITE_OK ) rc = SQLITE_IOERR;
drhed7c8552001-04-11 14:29:21 +0000741 return rc;
742 }
drhd9b02572001-04-15 00:37:09 +0000743 pager_seek(pPager->fd, (pPg->pgno-1)*SQLITE_PAGE_SIZE);
744 rc = pager_write(pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE);
745 if( rc!=SQLITE_OK ){
746 rc = sqlitepager_rollback(pPager);
747 *ppPage = 0;
748 if( rc==SQLITE_OK ) rc = SQLITE_FULL;
749 return rc;
750 }
751 }
752
753 /* Unlink the old page from the free list and the hash table
754 */
755 pPager->pFirst = pPg->pNextFree;
drhed7c8552001-04-11 14:29:21 +0000756 if( pPager->pFirst ){
drhd9b02572001-04-15 00:37:09 +0000757 pPager->pFirst->pPrevFree = 0;
drhed7c8552001-04-11 14:29:21 +0000758 }else{
759 pPager->pLast = 0;
760 }
761 if( pPg->pNextHash ){
762 pPg->pNextHash->pPrevHash = pPg->pPrevHash;
763 }
764 if( pPg->pPrevHash ){
765 pPg->pPrevHash->pNextHash = pPg->pNextHash;
766 }else{
drhd9b02572001-04-15 00:37:09 +0000767 h = pager_hash(pPg->pgno);
drhed7c8552001-04-11 14:29:21 +0000768 assert( pPager->aHash[h]==pPg );
769 pPager->aHash[h] = pPg->pNextHash;
770 }
drhd9b02572001-04-15 00:37:09 +0000771 pPager->nOvfl++;
drhed7c8552001-04-11 14:29:21 +0000772 }
773 pPg->pgno = pgno;
774 pPg->inJournal = 0;
775 pPg->dirty = 0;
776 pPg->nRef = 1;
drhdd793422001-06-28 01:54:48 +0000777 REFINFO(pPg);
drhd9b02572001-04-15 00:37:09 +0000778 pPager->nRef++;
779 h = pager_hash(pgno);
drhed7c8552001-04-11 14:29:21 +0000780 pPg->pNextHash = pPager->aHash[h];
781 pPager->aHash[h] = pPg;
782 if( pPg->pNextHash ){
783 assert( pPg->pNextHash->pPrevHash==0 );
784 pPg->pNextHash->pPrevHash = pPg;
785 }
drh306dc212001-05-21 13:45:10 +0000786 if( pPager->dbSize<0 ) sqlitepager_pagecount(pPager);
787 if( pPager->dbSize<pgno ){
788 memset(PGHDR_TO_DATA(pPg), 0, SQLITE_PAGE_SIZE);
789 }else{
790 pager_seek(pPager->fd, (pgno-1)*SQLITE_PAGE_SIZE);
791 pager_read(pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE);
792 }
drh7e3b0a02001-04-28 16:52:40 +0000793 if( pPager->nExtra>0 ){
794 memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra);
795 }
drhed7c8552001-04-11 14:29:21 +0000796 }else{
drhd9b02572001-04-15 00:37:09 +0000797 /* The requested page is in the page cache. */
drh7e3b0a02001-04-28 16:52:40 +0000798 pPager->nHit++;
drhdf0b3b02001-06-23 11:36:20 +0000799 page_ref(pPg);
drhed7c8552001-04-11 14:29:21 +0000800 }
801 *ppPage = PGHDR_TO_DATA(pPg);
802 return SQLITE_OK;
803}
804
805/*
drh7e3b0a02001-04-28 16:52:40 +0000806** Acquire a page if it is already in the in-memory cache. Do
807** not read the page from disk. Return a pointer to the page,
808** or 0 if the page is not in cache.
809**
810** See also sqlitepager_get(). The difference between this routine
811** and sqlitepager_get() is that _get() will go to the disk and read
812** in the page if the page is not already in cache. This routine
drh306dc212001-05-21 13:45:10 +0000813** returns NULL if the page is not in cache of if a disk I/O has ever
814** happened.
drh7e3b0a02001-04-28 16:52:40 +0000815*/
816void *sqlitepager_lookup(Pager *pPager, Pgno pgno){
817 PgHdr *pPg;
818
819 /* Make sure we have not hit any critical errors.
820 */
821 if( pPager==0 || pgno==0 ){
822 return 0;
823 }
824 if( pPager->errMask & ~(PAGER_ERR_FULL) ){
825 return 0;
826 }
827 if( pPager->nRef==0 ){
828 return 0;
829 }
830 pPg = pager_lookup(pPager, pgno);
831 if( pPg==0 ) return 0;
drhdf0b3b02001-06-23 11:36:20 +0000832 page_ref(pPg);
drh7e3b0a02001-04-28 16:52:40 +0000833 return PGHDR_TO_DATA(pPg);
834}
835
836/*
drhed7c8552001-04-11 14:29:21 +0000837** Release a page.
838**
839** If the number of references to the page drop to zero, then the
840** page is added to the LRU list. When all references to all pages
drhd9b02572001-04-15 00:37:09 +0000841** are released, a rollback occurs and the lock on the database is
drhed7c8552001-04-11 14:29:21 +0000842** removed.
843*/
drhd9b02572001-04-15 00:37:09 +0000844int sqlitepager_unref(void *pData){
drhed7c8552001-04-11 14:29:21 +0000845 Pager *pPager;
846 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +0000847
848 /* Decrement the reference count for this page
849 */
drhed7c8552001-04-11 14:29:21 +0000850 pPg = DATA_TO_PGHDR(pData);
851 assert( pPg->nRef>0 );
852 pPager = pPg->pPager;
853 pPg->nRef--;
drhdd793422001-06-28 01:54:48 +0000854 REFINFO(pPg);
drhd9b02572001-04-15 00:37:09 +0000855
drh72f82862001-05-24 21:06:34 +0000856 /* When the number of references to a page reach 0, call the
857 ** destructor and add the page to the freelist.
drhd9b02572001-04-15 00:37:09 +0000858 */
drhed7c8552001-04-11 14:29:21 +0000859 if( pPg->nRef==0 ){
drhd9b02572001-04-15 00:37:09 +0000860 pPg->pNextFree = 0;
861 pPg->pPrevFree = pPager->pLast;
drhed7c8552001-04-11 14:29:21 +0000862 pPager->pLast = pPg;
drhd9b02572001-04-15 00:37:09 +0000863 if( pPg->pPrevFree ){
864 pPg->pPrevFree->pNextFree = pPg;
drhed7c8552001-04-11 14:29:21 +0000865 }else{
866 pPager->pFirst = pPg;
867 }
drh72f82862001-05-24 21:06:34 +0000868 if( pPager->xDestructor ){
869 pPager->xDestructor(pData);
870 }
drhd9b02572001-04-15 00:37:09 +0000871
872 /* When all pages reach the freelist, drop the read lock from
873 ** the database file.
874 */
875 pPager->nRef--;
876 assert( pPager->nRef>=0 );
877 if( pPager->nRef==0 ){
878 pager_reset(pPager);
879 }
drhed7c8552001-04-11 14:29:21 +0000880 }
drhd9b02572001-04-15 00:37:09 +0000881 return SQLITE_OK;
drhed7c8552001-04-11 14:29:21 +0000882}
883
884/*
885** Mark a data page as writeable. The page is written into the journal
886** if it is not there already. This routine must be called before making
887** changes to a page.
888**
889** The first time this routine is called, the pager creates a new
890** journal and acquires a write lock on the database. If the write
891** lock could not be acquired, this routine returns SQLITE_BUSY. The
drh306dc212001-05-21 13:45:10 +0000892** calling routine must check for that return value and be careful not to
drhed7c8552001-04-11 14:29:21 +0000893** change any page data until this routine returns SQLITE_OK.
drhd9b02572001-04-15 00:37:09 +0000894**
895** If the journal file could not be written because the disk is full,
896** then this routine returns SQLITE_FULL and does an immediate rollback.
897** All subsequent write attempts also return SQLITE_FULL until there
898** is a call to sqlitepager_commit() or sqlitepager_rollback() to
899** reset.
drhed7c8552001-04-11 14:29:21 +0000900*/
drhd9b02572001-04-15 00:37:09 +0000901int sqlitepager_write(void *pData){
drh69688d52001-04-14 16:38:23 +0000902 PgHdr *pPg = DATA_TO_PGHDR(pData);
903 Pager *pPager = pPg->pPager;
drhd79caeb2001-04-15 02:27:24 +0000904 int rc = SQLITE_OK;
drh69688d52001-04-14 16:38:23 +0000905
drhd9b02572001-04-15 00:37:09 +0000906 if( pPager->errMask ){
907 return pager_errcode(pPager);
908 }
909 pPg->dirty = 1;
drh69688d52001-04-14 16:38:23 +0000910 if( pPg->inJournal ){ return SQLITE_OK; }
drhd9b02572001-04-15 00:37:09 +0000911 assert( pPager->state!=SQLITE_UNLOCK );
drhed7c8552001-04-11 14:29:21 +0000912 if( pPager->state==SQLITE_READLOCK ){
913 pPager->jfd = open(pPager->zJournal, O_RDWR|O_CREAT, 0644);
914 if( pPager->jfd<0 ){
915 return SQLITE_CANTOPEN;
916 }
drhd9b02572001-04-15 00:37:09 +0000917 if( pager_lock(pPager->jfd, 1) ){
drhed7c8552001-04-11 14:29:21 +0000918 close(pPager->jfd);
919 pPager->jfd = -1;
920 return SQLITE_BUSY;
921 }
drhd9b02572001-04-15 00:37:09 +0000922 pager_unlock(pPager->fd);
923 if( pager_lock(pPager->fd, 1) ){
drhed7c8552001-04-11 14:29:21 +0000924 close(pPager->jfd);
925 pPager->jfd = -1;
926 pPager->state = SQLITE_UNLOCK;
drhd9b02572001-04-15 00:37:09 +0000927 pPager->errMask |= PAGER_ERR_LOCK;
drhed7c8552001-04-11 14:29:21 +0000928 return SQLITE_PROTOCOL;
929 }
930 pPager->state = SQLITE_WRITELOCK;
drhd9b02572001-04-15 00:37:09 +0000931 sqlitepager_pagecount(pPager);
drh69688d52001-04-14 16:38:23 +0000932 pPager->origDbSize = pPager->dbSize;
drhd9b02572001-04-15 00:37:09 +0000933 rc = pager_write(pPager->jfd, aJournalMagic, sizeof(aJournalMagic));
934 if( rc==SQLITE_OK ){
935 rc = pager_write(pPager->jfd, &pPager->dbSize, sizeof(Pgno));
936 }
937 if( rc!=SQLITE_OK ){
938 rc = pager_unwritelock(pPager);
939 if( rc==SQLITE_OK ) rc = SQLITE_FULL;
940 return rc;
941 }
drhed7c8552001-04-11 14:29:21 +0000942 }
drhd9b02572001-04-15 00:37:09 +0000943 assert( pPager->state==SQLITE_WRITELOCK );
drh69688d52001-04-14 16:38:23 +0000944 assert( pPager->jfd>=0 );
drhd9b02572001-04-15 00:37:09 +0000945 if( pPg->pgno <= pPager->origDbSize ){
946 rc = pager_write(pPager->jfd, &pPg->pgno, sizeof(Pgno));
947 if( rc==SQLITE_OK ){
948 rc = pager_write(pPager->jfd, pData, SQLITE_PAGE_SIZE);
949 }
950 if( rc!=SQLITE_OK ){
951 sqlitepager_rollback(pPager);
952 pPager->errMask |= PAGER_ERR_FULL;
953 return rc;
954 }
drh69688d52001-04-14 16:38:23 +0000955 }
drh69688d52001-04-14 16:38:23 +0000956 pPg->inJournal = 1;
drh306dc212001-05-21 13:45:10 +0000957 if( pPager->dbSize<pPg->pgno ){
958 pPager->dbSize = pPg->pgno;
959 }
drh69688d52001-04-14 16:38:23 +0000960 return rc;
drhed7c8552001-04-11 14:29:21 +0000961}
962
963/*
964** Commit all changes to the database and release the write lock.
drhd9b02572001-04-15 00:37:09 +0000965**
966** If the commit fails for any reason, a rollback attempt is made
967** and an error code is returned. If the commit worked, SQLITE_OK
968** is returned.
drhed7c8552001-04-11 14:29:21 +0000969*/
drhd9b02572001-04-15 00:37:09 +0000970int sqlitepager_commit(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +0000971 int i, rc;
972 PgHdr *pPg;
drhd9b02572001-04-15 00:37:09 +0000973
974 if( pPager->errMask==PAGER_ERR_FULL ){
975 rc = sqlitepager_rollback(pPager);
976 if( rc==SQLITE_OK ) rc = SQLITE_FULL;
977 return rc;
978 }
979 if( pPager->errMask!=0 ){
980 rc = pager_errcode(pPager);
981 return rc;
982 }
983 if( pPager->state!=SQLITE_WRITELOCK ){
984 return SQLITE_ERROR;
985 }
drhed7c8552001-04-11 14:29:21 +0000986 assert( pPager->jfd>=0 );
987 if( fsync(pPager->jfd) ){
drhd9b02572001-04-15 00:37:09 +0000988 goto commit_abort;
drhed7c8552001-04-11 14:29:21 +0000989 }
990 for(i=0; i<N_PG_HASH; i++){
991 for(pPg=pPager->aHash[i]; pPg; pPg=pPg->pNextHash){
992 if( pPg->dirty==0 ) continue;
drhd9b02572001-04-15 00:37:09 +0000993 rc = pager_seek(pPager->fd, (pPg->pgno-1)*SQLITE_PAGE_SIZE);
994 if( rc!=SQLITE_OK ) goto commit_abort;
995 rc = pager_write(pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE);
996 if( rc!=SQLITE_OK ) goto commit_abort;
drhed7c8552001-04-11 14:29:21 +0000997 }
998 }
drhd9b02572001-04-15 00:37:09 +0000999 if( fsync(pPager->fd) ) goto commit_abort;
1000 rc = pager_unwritelock(pPager);
1001 pPager->dbSize = -1;
1002 return rc;
1003
1004 /* Jump here if anything goes wrong during the commit process.
1005 */
1006commit_abort:
1007 rc = sqlitepager_rollback(pPager);
1008 if( rc==SQLITE_OK ){
1009 rc = SQLITE_FULL;
drhed7c8552001-04-11 14:29:21 +00001010 }
drhed7c8552001-04-11 14:29:21 +00001011 return rc;
1012}
1013
1014/*
1015** Rollback all changes. The database falls back to read-only mode.
1016** All in-memory cache pages revert to their original data contents.
1017** The journal is deleted.
drhd9b02572001-04-15 00:37:09 +00001018**
1019** This routine cannot fail unless some other process is not following
1020** the correct locking protocol (SQLITE_PROTOCOL) or unless some other
1021** process is writing trash into the journal file (SQLITE_CORRUPT) or
1022** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error
1023** codes are returned for all these occasions. Otherwise,
1024** SQLITE_OK is returned.
drhed7c8552001-04-11 14:29:21 +00001025*/
drhd9b02572001-04-15 00:37:09 +00001026int sqlitepager_rollback(Pager *pPager){
drhed7c8552001-04-11 14:29:21 +00001027 int rc;
drhd9b02572001-04-15 00:37:09 +00001028 if( pPager->errMask!=0 && pPager->errMask!=PAGER_ERR_FULL ){
1029 return pager_errcode(pPager);
drhed7c8552001-04-11 14:29:21 +00001030 }
drhd9b02572001-04-15 00:37:09 +00001031 if( pPager->state!=SQLITE_WRITELOCK ){
1032 return SQLITE_OK;
1033 }
1034 rc = pager_playback(pPager);
1035 if( rc!=SQLITE_OK ){
1036 rc = SQLITE_CORRUPT;
1037 pPager->errMask |= PAGER_ERR_CORRUPT;
1038 }
1039 pPager->dbSize = -1;
drhed7c8552001-04-11 14:29:21 +00001040 return rc;
1041};
drhd9b02572001-04-15 00:37:09 +00001042
1043/*
1044** This routine is used for testing and analysis only.
1045*/
1046int *sqlitepager_stats(Pager *pPager){
1047 static int a[9];
1048 a[0] = pPager->nRef;
1049 a[1] = pPager->nPage;
1050 a[2] = pPager->mxPage;
1051 a[3] = pPager->dbSize;
1052 a[4] = pPager->state;
1053 a[5] = pPager->errMask;
1054 a[6] = pPager->nHit;
1055 a[7] = pPager->nMiss;
1056 a[8] = pPager->nOvfl;
1057 return a;
1058}
drhdd793422001-06-28 01:54:48 +00001059
1060#if SQLITE_TEST
1061/*
1062** Print a listing of all referenced pages and their ref count.
1063*/
1064void sqlitepager_refdump(Pager *pPager){
1065 PgHdr *pPg;
1066 for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
1067 if( pPg->nRef<=0 ) continue;
1068 printf("PAGE %3d addr=0x%08x nRef=%d\n",
1069 pPg->pgno, (int)PGHDR_TO_DATA(pPg), pPg->nRef);
1070 }
1071}
1072#endif