drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1 | /* |
| 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 |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 28 | ** access to one or more reader or one writer. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 29 | ** |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame^] | 30 | ** @(#) $Id: pager.c,v 1.12 2001/06/28 01:54:49 drh Exp $ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 31 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 32 | #include "sqliteInt.h" |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 33 | #include "pager.h" |
| 34 | #include <fcntl.h> |
| 35 | #include <sys/stat.h> |
| 36 | #include <unistd.h> |
| 37 | #include <assert.h> |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 38 | #include <string.h> |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 39 | |
| 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 |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 52 | ** file at the same time. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 53 | ** |
| 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 | ** |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 59 | ** The page cache comes up in SQLITE_UNLOCK. The first time a |
| 60 | ** sqlite_page_get() occurs, the state transitions to SQLITE_READLOCK. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 61 | ** After all pages have been released using sqlite_page_unref(), |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 62 | ** the state transitions back to SQLITE_UNLOCK. The first time |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 63 | ** that sqlite_page_write() is called, the state transitions to |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 64 | ** 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. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 69 | */ |
| 70 | #define SQLITE_UNLOCK 0 |
| 71 | #define SQLITE_READLOCK 1 |
| 72 | #define SQLITE_WRITELOCK 2 |
| 73 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 74 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 75 | /* |
| 76 | ** Each in-memory image of a page begins with the following header. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 77 | ** This header is only visible to this pager module. The client |
| 78 | ** code that calls pager sees only the data that follows the header. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 79 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 80 | typedef struct PgHdr PgHdr; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 81 | struct PgHdr { |
| 82 | Pager *pPager; /* The pager to which this page belongs */ |
| 83 | Pgno pgno; /* The page number for this page */ |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 84 | PgHdr *pNextHash, *pPrevHash; /* Hash collision chain for PgHdr.pgno */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 85 | int nRef; /* Number of users of this page */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 86 | PgHdr *pNextFree, *pPrevFree; /* Freelist of pages where nRef==0 */ |
| 87 | PgHdr *pNextAll, *pPrevAll; /* A list of all pages */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 88 | char inJournal; /* TRUE if has been written to journal */ |
| 89 | char dirty; /* TRUE if we need to write back changes */ |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 90 | /* SQLITE_PAGE_SIZE bytes of page data follow this header */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 91 | /* Pager.nExtra bytes of local data follow the page data */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | /* |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 95 | ** Convert a pointer to a PgHdr into a pointer to its data |
| 96 | ** and back again. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 97 | */ |
| 98 | #define PGHDR_TO_DATA(P) ((void*)(&(P)[1])) |
| 99 | #define DATA_TO_PGHDR(D) (&((PgHdr*)(D))[-1]) |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 100 | #define PGHDR_TO_EXTRA(P) ((void*)&((char*)(&(P)[1]))[SQLITE_PAGE_SIZE]) |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 101 | |
| 102 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 103 | ** How big to make the hash table used for locating in-memory pages |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 104 | ** by page number. Knuth says this should be a prime number. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 105 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 106 | #define N_PG_HASH 101 |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 107 | |
| 108 | /* |
| 109 | ** A open page cache is an instance of the following structure. |
| 110 | */ |
| 111 | struct 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 */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 115 | int dbSize; /* Number of pages in the file */ |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 116 | int origDbSize; /* dbSize before the current change */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 117 | int nExtra; /* Add this many bytes to each in-memory page */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 118 | void (*xDestructor)(void*); /* Call this routine when freeing pages */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 119 | int nPage; /* Total number of in-memory pages */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 120 | int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 121 | int mxPage; /* Maximum number of pages to hold in cache */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 122 | 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 */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 125 | PgHdr *pFirst, *pLast; /* List of free pages */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 126 | PgHdr *pAll; /* List of all pages */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 127 | PgHdr *aHash[N_PG_HASH]; /* Hash table to map page number of PgHdr */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 128 | }; |
| 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 | */ |
| 142 | typedef struct PageRecord PageRecord; |
| 143 | struct 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 | */ |
| 152 | static const unsigned char aJournalMagic[] = { |
| 153 | 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd4, |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 154 | }; |
| 155 | |
| 156 | /* |
| 157 | ** Hash a page number |
| 158 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 159 | #define pager_hash(PN) ((PN)%N_PG_HASH) |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 160 | |
| 161 | /* |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame^] | 162 | ** Enable reference count tracking here: |
| 163 | */ |
| 164 | #if SQLITE_TEST |
| 165 | int 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 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 181 | ** 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 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 185 | static int pager_lock(int fd, int wrlock){ |
| 186 | int rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 187 | struct flock lock; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 188 | 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; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 193 | } |
| 194 | |
| 195 | /* |
| 196 | ** Unlock the database file. |
| 197 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 198 | static int pager_unlock(fd){ |
| 199 | int rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 200 | struct flock lock; |
| 201 | lock.l_type = F_UNLCK; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 202 | 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 | */ |
| 212 | static 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 | */ |
| 221 | static 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 | */ |
| 233 | static 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 | */ |
| 254 | static 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 | */ |
| 268 | static 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; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 275 | } |
| 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 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 281 | static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 282 | 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 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 295 | static void pager_reset(Pager *pPager){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 296 | PgHdr *pPg, *pNext; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 297 | for(pPg=pPager->pAll; pPg; pPg=pNext){ |
| 298 | pNext = pPg->pNextAll; |
| 299 | sqliteFree(pPg); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 300 | } |
| 301 | pPager->pFirst = 0; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 302 | pPager->pLast = 0; |
| 303 | pPager->pAll = 0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 304 | memset(pPager->aHash, 0, sizeof(pPager->aHash)); |
| 305 | pPager->nPage = 0; |
| 306 | if( pPager->state==SQLITE_WRITELOCK ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 307 | sqlitepager_rollback(pPager); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 308 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 309 | pager_unlock(pPager->fd); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 310 | pPager->state = SQLITE_UNLOCK; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 311 | pPager->dbSize = -1; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 312 | 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, |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 328 | ** 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. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 331 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 332 | static int pager_unwritelock(Pager *pPager){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 333 | int rc; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 334 | PgHdr *pPg; |
| 335 | if( pPager->state!=SQLITE_WRITELOCK ) return SQLITE_OK; |
| 336 | pager_unlock(pPager->fd); |
| 337 | rc = pager_lock(pPager->fd, 0); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 338 | unlink(pPager->zJournal); |
| 339 | close(pPager->jfd); |
| 340 | pPager->jfd = -1; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 341 | for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ |
| 342 | pPg->inJournal = 0; |
| 343 | pPg->dirty = 0; |
| 344 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 345 | if( rc!=SQLITE_OK ){ |
| 346 | pPager->state = SQLITE_UNLOCK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 347 | rc = SQLITE_PROTOCOL; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 348 | pPager->errMask |= PAGER_ERR_LOCK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 349 | }else{ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 350 | rc = SQLITE_OK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 351 | pPager->state = SQLITE_READLOCK; |
| 352 | } |
| 353 | return rc; |
| 354 | } |
| 355 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 356 | /* |
| 357 | ** Playback the journal and thus restore the database file to |
| 358 | ** the state it was in before we started making changes. |
| 359 | ** |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 360 | ** 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. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 364 | ** 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. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 367 | ** |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 368 | ** For playback, the pages have to be read from the journal in |
| 369 | ** reverse order and put back into the original database file. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 370 | ** |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 371 | ** 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. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 378 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 379 | static 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)]; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 387 | int rc; |
| 388 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 389 | /* Read the beginning of the journal and truncate the |
| 390 | ** database file back to its original size. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 391 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 392 | 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 ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 409 | return SQLITE_OK; |
| 410 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 411 | nRec = (statbuf.st_size - (sizeof(aMagic)+sizeof(Pgno))) / sizeof(PageRecord); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 412 | |
| 413 | /* Process segments beginning with the last and working backwards |
| 414 | ** to the first. |
| 415 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 416 | for(i=nRec-1; i>=0; i--){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 417 | /* Seek to the beginning of the segment */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 418 | 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; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 424 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 425 | /* Sanity checking on the page */ |
| 426 | if( pgRec.pgno>mxPg || pgRec.pgno==0 ){ |
| 427 | rc = SQLITE_CORRUPT; |
| 428 | break; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 429 | } |
| 430 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 431 | /* Playback the page. Update the in-memory copy of the page |
| 432 | ** at the same time, if there is one. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 433 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 434 | pPg = pager_lookup(pPager, pgRec.pgno); |
| 435 | if( pPg ){ |
| 436 | memcpy(PGHDR_TO_DATA(pPg), pgRec.aData, SQLITE_PAGE_SIZE); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 437 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 438 | 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; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 442 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 443 | 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); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 449 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 450 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 451 | } |
| 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 |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 456 | ** the first call to sqlitepager_get() and is only held open until the |
| 457 | ** last page is released using sqlitepager_unref(). |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 458 | */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 459 | int 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 | ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 465 | Pager *pPager; |
| 466 | int nameLen; |
| 467 | int fd; |
| 468 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 469 | *ppPager = 0; |
| 470 | if( sqlite_malloc_failed ){ |
| 471 | return SQLITE_NOMEM; |
| 472 | } |
| 473 | fd = open(zFilename, O_RDWR|O_CREAT, 0644); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 474 | if( fd<0 ){ |
| 475 | return SQLITE_CANTOPEN; |
| 476 | } |
| 477 | nameLen = strlen(zFilename); |
| 478 | pPager = sqliteMalloc( sizeof(*pPager) + nameLen*2 + 30 ); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 479 | if( pPager==0 ){ |
| 480 | close(fd); |
| 481 | return SQLITE_NOMEM; |
| 482 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 483 | 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; |
drh | d79caeb | 2001-04-15 02:27:24 +0000 | [diff] [blame] | 493 | pPager->mxPage = mxPage>5 ? mxPage : 10; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 494 | pPager->state = SQLITE_UNLOCK; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 495 | pPager->errMask = 0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 496 | pPager->pFirst = 0; |
| 497 | pPager->pLast = 0; |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 498 | pPager->nExtra = nExtra; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 499 | memset(pPager->aHash, 0, sizeof(pPager->aHash)); |
| 500 | *ppPager = pPager; |
| 501 | return SQLITE_OK; |
| 502 | } |
| 503 | |
| 504 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 505 | ** 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 | */ |
| 511 | void sqlitepager_set_destructor(Pager *pPager, void (*xDesc)(void*)){ |
| 512 | pPager->xDestructor = xDesc; |
| 513 | } |
| 514 | |
| 515 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 516 | ** Return the total number of pages in the file opened by pPager. |
| 517 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 518 | int sqlitepager_pagecount(Pager *pPager){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 519 | int n; |
| 520 | struct stat statbuf; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 521 | assert( pPager!=0 ); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 522 | 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 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 530 | if( pPager->state!=SQLITE_UNLOCK ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 531 | 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 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 545 | int sqlitepager_close(Pager *pPager){ |
| 546 | PgHdr *pPg, *pNext; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 547 | switch( pPager->state ){ |
| 548 | case SQLITE_WRITELOCK: { |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 549 | sqlitepager_rollback(pPager); |
| 550 | pager_unlock(pPager->fd); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 551 | break; |
| 552 | } |
| 553 | case SQLITE_READLOCK: { |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 554 | pager_unlock(pPager->fd); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 555 | break; |
| 556 | } |
| 557 | default: { |
| 558 | /* Do nothing */ |
| 559 | break; |
| 560 | } |
| 561 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 562 | for(pPg=pPager->pAll; pPg; pPg=pNext){ |
| 563 | pNext = pPg->pNextAll; |
| 564 | sqliteFree(pPg); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 565 | } |
| 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 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 575 | Pgno sqlitepager_pagenumber(void *pData){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 576 | PgHdr *p = DATA_TO_PGHDR(pData); |
| 577 | return p->pgno; |
| 578 | } |
| 579 | |
| 580 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 581 | ** 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 | */ |
drh | df0b3b0 | 2001-06-23 11:36:20 +0000 | [diff] [blame] | 585 | static void page_ref(PgHdr *pPg){ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 586 | 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++; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame^] | 601 | REFINFO(pPg); |
drh | df0b3b0 | 2001-06-23 11:36:20 +0000 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | /* |
| 605 | ** Increment the reference count for a page. The input pointer is |
| 606 | ** a reference to the page data. |
| 607 | */ |
| 608 | int sqlitepager_ref(void *pData){ |
| 609 | PgHdr *pPg = DATA_TO_PGHDR(pData); |
| 610 | page_ref(pPg); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 611 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | /* |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 615 | ** 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 | ** |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 620 | ** 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 | ** |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 626 | ** The acquisition might fail for several reasons. In all cases, |
| 627 | ** an appropriate error code is returned and *ppPage is set to NULL. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 628 | ** |
| 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. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 636 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 637 | int sqlitepager_get(Pager *pPager, Pgno pgno, void **ppPage){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 638 | PgHdr *pPg; |
| 639 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 640 | /* 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 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 649 | /* If this is the first page accessed, then get a read lock |
| 650 | ** on the database file. |
| 651 | */ |
| 652 | if( pPager->nRef==0 ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 653 | if( pager_lock(pPager->fd, 0)!=0 ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 654 | *ppPage = 0; |
| 655 | return SQLITE_BUSY; |
| 656 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 657 | pPager->state = SQLITE_READLOCK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 658 | |
| 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); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 668 | if( pPager->jfd<0 || pager_lock(pPager->jfd, 1)!=0 ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 669 | if( pPager->jfd>=0 ){ close(pPager->jfd); pPager->jfd = -1; } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 670 | pager_unlock(pPager->fd); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 671 | *ppPage = 0; |
| 672 | return SQLITE_BUSY; |
| 673 | } |
| 674 | |
| 675 | /* Get a write lock on the database */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 676 | pager_unlock(pPager->fd); |
| 677 | if( pager_lock(pPager->fd, 1)!=0 ){ |
| 678 | close(pPager->jfd); |
| 679 | pPager->jfd = -1; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 680 | *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 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 687 | rc = pager_playback(pPager); |
| 688 | if( rc!=SQLITE_OK ){ |
| 689 | return rc; |
| 690 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 691 | } |
| 692 | pPg = 0; |
| 693 | }else{ |
| 694 | /* Search for page in cache */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 695 | pPg = pager_lookup(pPager, pgno); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 696 | } |
| 697 | if( pPg==0 ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 698 | /* The requested page is not in the page cache. */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 699 | int h; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 700 | pPager->nMiss++; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 701 | if( pPager->nPage<pPager->mxPage || pPager->pFirst==0 ){ |
| 702 | /* Create a new page */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 703 | pPg = sqliteMalloc( sizeof(*pPg) + SQLITE_PAGE_SIZE + pPager->nExtra ); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 704 | if( pPg==0 ){ |
| 705 | *ppPage = 0; |
| 706 | pager_unwritelock(pPager); |
| 707 | pPager->errMask |= PAGER_ERR_MEM; |
| 708 | return SQLITE_NOMEM; |
| 709 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 710 | pPg->pPager = pPager; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 711 | pPg->pNextAll = pPager->pAll; |
| 712 | if( pPager->pAll ){ |
| 713 | pPager->pAll->pPrevAll = pPg; |
| 714 | } |
| 715 | pPg->pPrevAll = 0; |
drh | d79caeb | 2001-04-15 02:27:24 +0000 | [diff] [blame] | 716 | pPager->pAll = pPg; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 717 | pPager->nPage++; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 718 | }else{ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 719 | /* 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; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 723 | pPg = pPager->pFirst; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 724 | 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. */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 732 | if( pPg->dirty ){ |
| 733 | int rc; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 734 | assert( pPg->inJournal==1 ); |
| 735 | assert( pPager->state==SQLITE_WRITELOCK ); |
| 736 | rc = fsync(pPager->jfd); |
| 737 | if( rc!=0 ){ |
| 738 | rc = sqlitepager_rollback(pPager); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 739 | *ppPage = 0; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 740 | if( rc==SQLITE_OK ) rc = SQLITE_IOERR; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 741 | return rc; |
| 742 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 743 | 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; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 756 | if( pPager->pFirst ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 757 | pPager->pFirst->pPrevFree = 0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 758 | }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{ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 767 | h = pager_hash(pPg->pgno); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 768 | assert( pPager->aHash[h]==pPg ); |
| 769 | pPager->aHash[h] = pPg->pNextHash; |
| 770 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 771 | pPager->nOvfl++; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 772 | } |
| 773 | pPg->pgno = pgno; |
| 774 | pPg->inJournal = 0; |
| 775 | pPg->dirty = 0; |
| 776 | pPg->nRef = 1; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame^] | 777 | REFINFO(pPg); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 778 | pPager->nRef++; |
| 779 | h = pager_hash(pgno); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 780 | 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 | } |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 786 | 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 | } |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 793 | if( pPager->nExtra>0 ){ |
| 794 | memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra); |
| 795 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 796 | }else{ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 797 | /* The requested page is in the page cache. */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 798 | pPager->nHit++; |
drh | df0b3b0 | 2001-06-23 11:36:20 +0000 | [diff] [blame] | 799 | page_ref(pPg); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 800 | } |
| 801 | *ppPage = PGHDR_TO_DATA(pPg); |
| 802 | return SQLITE_OK; |
| 803 | } |
| 804 | |
| 805 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 806 | ** 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 |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 813 | ** returns NULL if the page is not in cache of if a disk I/O has ever |
| 814 | ** happened. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 815 | */ |
| 816 | void *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; |
drh | df0b3b0 | 2001-06-23 11:36:20 +0000 | [diff] [blame] | 832 | page_ref(pPg); |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 833 | return PGHDR_TO_DATA(pPg); |
| 834 | } |
| 835 | |
| 836 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 837 | ** 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 |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 841 | ** are released, a rollback occurs and the lock on the database is |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 842 | ** removed. |
| 843 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 844 | int sqlitepager_unref(void *pData){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 845 | Pager *pPager; |
| 846 | PgHdr *pPg; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 847 | |
| 848 | /* Decrement the reference count for this page |
| 849 | */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 850 | pPg = DATA_TO_PGHDR(pData); |
| 851 | assert( pPg->nRef>0 ); |
| 852 | pPager = pPg->pPager; |
| 853 | pPg->nRef--; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame^] | 854 | REFINFO(pPg); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 855 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 856 | /* When the number of references to a page reach 0, call the |
| 857 | ** destructor and add the page to the freelist. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 858 | */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 859 | if( pPg->nRef==0 ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 860 | pPg->pNextFree = 0; |
| 861 | pPg->pPrevFree = pPager->pLast; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 862 | pPager->pLast = pPg; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 863 | if( pPg->pPrevFree ){ |
| 864 | pPg->pPrevFree->pNextFree = pPg; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 865 | }else{ |
| 866 | pPager->pFirst = pPg; |
| 867 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 868 | if( pPager->xDestructor ){ |
| 869 | pPager->xDestructor(pData); |
| 870 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 871 | |
| 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 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 880 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 881 | return SQLITE_OK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 882 | } |
| 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 |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 892 | ** calling routine must check for that return value and be careful not to |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 893 | ** change any page data until this routine returns SQLITE_OK. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 894 | ** |
| 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. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 900 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 901 | int sqlitepager_write(void *pData){ |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 902 | PgHdr *pPg = DATA_TO_PGHDR(pData); |
| 903 | Pager *pPager = pPg->pPager; |
drh | d79caeb | 2001-04-15 02:27:24 +0000 | [diff] [blame] | 904 | int rc = SQLITE_OK; |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 905 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 906 | if( pPager->errMask ){ |
| 907 | return pager_errcode(pPager); |
| 908 | } |
| 909 | pPg->dirty = 1; |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 910 | if( pPg->inJournal ){ return SQLITE_OK; } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 911 | assert( pPager->state!=SQLITE_UNLOCK ); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 912 | 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 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 917 | if( pager_lock(pPager->jfd, 1) ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 918 | close(pPager->jfd); |
| 919 | pPager->jfd = -1; |
| 920 | return SQLITE_BUSY; |
| 921 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 922 | pager_unlock(pPager->fd); |
| 923 | if( pager_lock(pPager->fd, 1) ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 924 | close(pPager->jfd); |
| 925 | pPager->jfd = -1; |
| 926 | pPager->state = SQLITE_UNLOCK; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 927 | pPager->errMask |= PAGER_ERR_LOCK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 928 | return SQLITE_PROTOCOL; |
| 929 | } |
| 930 | pPager->state = SQLITE_WRITELOCK; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 931 | sqlitepager_pagecount(pPager); |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 932 | pPager->origDbSize = pPager->dbSize; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 933 | 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 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 942 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 943 | assert( pPager->state==SQLITE_WRITELOCK ); |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 944 | assert( pPager->jfd>=0 ); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 945 | 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 | } |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 955 | } |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 956 | pPg->inJournal = 1; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 957 | if( pPager->dbSize<pPg->pgno ){ |
| 958 | pPager->dbSize = pPg->pgno; |
| 959 | } |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 960 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 961 | } |
| 962 | |
| 963 | /* |
| 964 | ** Commit all changes to the database and release the write lock. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 965 | ** |
| 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. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 969 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 970 | int sqlitepager_commit(Pager *pPager){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 971 | int i, rc; |
| 972 | PgHdr *pPg; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 973 | |
| 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 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 986 | assert( pPager->jfd>=0 ); |
| 987 | if( fsync(pPager->jfd) ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 988 | goto commit_abort; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 989 | } |
| 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; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 993 | 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; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 997 | } |
| 998 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 999 | 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 | */ |
| 1006 | commit_abort: |
| 1007 | rc = sqlitepager_rollback(pPager); |
| 1008 | if( rc==SQLITE_OK ){ |
| 1009 | rc = SQLITE_FULL; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1010 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1011 | 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. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1018 | ** |
| 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. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1025 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1026 | int sqlitepager_rollback(Pager *pPager){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1027 | int rc; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1028 | if( pPager->errMask!=0 && pPager->errMask!=PAGER_ERR_FULL ){ |
| 1029 | return pager_errcode(pPager); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1030 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1031 | 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; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1040 | return rc; |
| 1041 | }; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1042 | |
| 1043 | /* |
| 1044 | ** This routine is used for testing and analysis only. |
| 1045 | */ |
| 1046 | int *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 | } |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame^] | 1059 | |
| 1060 | #if SQLITE_TEST |
| 1061 | /* |
| 1062 | ** Print a listing of all referenced pages and their ref count. |
| 1063 | */ |
| 1064 | void 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 |