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 | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame^] | 30 | ** @(#) $Id: pager.c,v 1.6 2001/05/21 13:45:10 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. |
| 77 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 78 | typedef struct PgHdr PgHdr; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 79 | struct PgHdr { |
| 80 | Pager *pPager; /* The pager to which this page belongs */ |
| 81 | Pgno pgno; /* The page number for this page */ |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 82 | PgHdr *pNextHash, *pPrevHash; /* Hash collision chain for PgHdr.pgno */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 83 | int nRef; /* Number of users of this page */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 84 | PgHdr *pNextFree, *pPrevFree; /* Freelist of pages where nRef==0 */ |
| 85 | PgHdr *pNextAll, *pPrevAll; /* A list of all pages */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 86 | char inJournal; /* TRUE if has been written to journal */ |
| 87 | char dirty; /* TRUE if we need to write back changes */ |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 88 | /* SQLITE_PAGE_SIZE bytes of page data follow this header */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 89 | /* Pager.nExtra bytes of local data follow the page data */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 90 | }; |
| 91 | |
| 92 | /* |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 93 | ** Convert a pointer to a PgHdr into a pointer to its data |
| 94 | ** and back again. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 95 | */ |
| 96 | #define PGHDR_TO_DATA(P) ((void*)(&(P)[1])) |
| 97 | #define DATA_TO_PGHDR(D) (&((PgHdr*)(D))[-1]) |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 98 | #define PGHDR_TO_EXTRA(P) ((void*)&((char*)(&(P)[1]))[SQLITE_PAGE_SIZE]) |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 99 | |
| 100 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 101 | ** How big to make the hash table used for locating in-memory pages |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame^] | 102 | ** by page number. Knuth says this should be a prime number. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 103 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 104 | #define N_PG_HASH 101 |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 105 | |
| 106 | /* |
| 107 | ** A open page cache is an instance of the following structure. |
| 108 | */ |
| 109 | struct Pager { |
| 110 | char *zFilename; /* Name of the database file */ |
| 111 | char *zJournal; /* Name of the journal file */ |
| 112 | int fd, jfd; /* File descriptors for database and journal */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 113 | int dbSize; /* Number of pages in the file */ |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 114 | int origDbSize; /* dbSize before the current change */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 115 | int nExtra; /* Add this many bytes to each in-memory page */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 116 | int nPage; /* Total number of in-memory pages */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 117 | int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 118 | int mxPage; /* Maximum number of pages to hold in cache */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 119 | int nHit, nMiss, nOvfl; /* Cache hits, missing, and LRU overflows */ |
| 120 | unsigned char state; /* SQLITE_UNLOCK, _READLOCK or _WRITELOCK */ |
| 121 | unsigned char errMask; /* One of several kinds of errors */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 122 | PgHdr *pFirst, *pLast; /* List of free pages */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 123 | PgHdr *pAll; /* List of all pages */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 124 | PgHdr *aHash[N_PG_HASH]; /* Hash table to map page number of PgHdr */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 125 | }; |
| 126 | |
| 127 | /* |
| 128 | ** These are bits that can be set in Pager.errMask. |
| 129 | */ |
| 130 | #define PAGER_ERR_FULL 0x01 /* a write() failed */ |
| 131 | #define PAGER_ERR_MEM 0x02 /* malloc() failed */ |
| 132 | #define PAGER_ERR_LOCK 0x04 /* error in the locking protocol */ |
| 133 | #define PAGER_ERR_CORRUPT 0x08 /* database or journal corruption */ |
| 134 | |
| 135 | /* |
| 136 | ** The journal file contains page records in the following |
| 137 | ** format. |
| 138 | */ |
| 139 | typedef struct PageRecord PageRecord; |
| 140 | struct PageRecord { |
| 141 | Pgno pgno; /* The page number */ |
| 142 | char aData[SQLITE_PAGE_SIZE]; /* Original data for page pgno */ |
| 143 | }; |
| 144 | |
| 145 | /* |
| 146 | ** Journal files begin with the following magic string. This data |
| 147 | ** is completely random. It is used only as a sanity check. |
| 148 | */ |
| 149 | static const unsigned char aJournalMagic[] = { |
| 150 | 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd4, |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 151 | }; |
| 152 | |
| 153 | /* |
| 154 | ** Hash a page number |
| 155 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 156 | #define pager_hash(PN) ((PN)%N_PG_HASH) |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 157 | |
| 158 | /* |
| 159 | ** Attempt to acquire a read lock (if wrlock==0) or a write lock (if wrlock==1) |
| 160 | ** on the database file. Return 0 on success and non-zero if the lock |
| 161 | ** could not be acquired. |
| 162 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 163 | static int pager_lock(int fd, int wrlock){ |
| 164 | int rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 165 | struct flock lock; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 166 | lock.l_type = wrlock ? F_WRLCK : F_RDLCK; |
| 167 | lock.l_whence = SEEK_SET; |
| 168 | lock.l_start = lock.l_len = 0L; |
| 169 | rc = fcntl(fd, F_SETLK, &lock); |
| 170 | return rc!=0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 171 | } |
| 172 | |
| 173 | /* |
| 174 | ** Unlock the database file. |
| 175 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 176 | static int pager_unlock(fd){ |
| 177 | int rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 178 | struct flock lock; |
| 179 | lock.l_type = F_UNLCK; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 180 | lock.l_whence = SEEK_SET; |
| 181 | lock.l_start = lock.l_len = 0L; |
| 182 | rc = fcntl(fd, F_SETLK, &lock); |
| 183 | return rc!=0; |
| 184 | } |
| 185 | |
| 186 | /* |
| 187 | ** Move the cursor for file descriptor fd to the point whereto from |
| 188 | ** the beginning of the file. |
| 189 | */ |
| 190 | static int pager_seek(int fd, off_t whereto){ |
| 191 | lseek(fd, whereto, SEEK_SET); |
| 192 | return SQLITE_OK; |
| 193 | } |
| 194 | |
| 195 | /* |
| 196 | ** Truncate the given file so that it contains exactly mxPg pages |
| 197 | ** of data. |
| 198 | */ |
| 199 | static int pager_truncate(int fd, Pgno mxPg){ |
| 200 | int rc; |
| 201 | rc = ftruncate(fd, mxPg*SQLITE_PAGE_SIZE); |
| 202 | return rc!=0 ? SQLITE_IOERR : SQLITE_OK; |
| 203 | } |
| 204 | |
| 205 | /* |
| 206 | ** Read nBytes of data from fd into pBuf. If the data cannot be |
| 207 | ** read or only a partial read occurs, then the unread parts of |
| 208 | ** pBuf are filled with zeros and this routine returns SQLITE_IOERR. |
| 209 | ** If the read is completely successful, return SQLITE_OK. |
| 210 | */ |
| 211 | static int pager_read(int fd, void *pBuf, int nByte){ |
| 212 | int rc; |
| 213 | rc = read(fd, pBuf, nByte); |
| 214 | if( rc<0 ){ |
| 215 | memset(pBuf, 0, nByte); |
| 216 | return SQLITE_IOERR; |
| 217 | } |
| 218 | if( rc<nByte ){ |
| 219 | memset(&((char*)pBuf)[rc], 0, nByte - rc); |
| 220 | rc = SQLITE_IOERR; |
| 221 | }else{ |
| 222 | rc = SQLITE_OK; |
| 223 | } |
| 224 | return rc; |
| 225 | } |
| 226 | |
| 227 | /* |
| 228 | ** Write nBytes of data into fd. If any problem occurs or if the |
| 229 | ** write is incomplete, SQLITE_IOERR is returned. SQLITE_OK is |
| 230 | ** returned upon complete success. |
| 231 | */ |
| 232 | static int pager_write(int fd, const void *pBuf, int nByte){ |
| 233 | int rc; |
| 234 | rc = write(fd, pBuf, nByte); |
| 235 | if( rc<nByte ){ |
| 236 | return SQLITE_FULL; |
| 237 | }else{ |
| 238 | return SQLITE_OK; |
| 239 | } |
| 240 | } |
| 241 | |
| 242 | /* |
| 243 | ** Convert the bits in the pPager->errMask into an approprate |
| 244 | ** return code. |
| 245 | */ |
| 246 | static int pager_errcode(Pager *pPager){ |
| 247 | int rc = SQLITE_OK; |
| 248 | if( pPager->errMask & PAGER_ERR_LOCK ) rc = SQLITE_PROTOCOL; |
| 249 | if( pPager->errMask & PAGER_ERR_FULL ) rc = SQLITE_FULL; |
| 250 | if( pPager->errMask & PAGER_ERR_MEM ) rc = SQLITE_NOMEM; |
| 251 | if( pPager->errMask & PAGER_ERR_CORRUPT ) rc = SQLITE_CORRUPT; |
| 252 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 253 | } |
| 254 | |
| 255 | /* |
| 256 | ** Find a page in the hash table given its page number. Return |
| 257 | ** a pointer to the page or NULL if not found. |
| 258 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 259 | static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 260 | PgHdr *p = pPager->aHash[pgno % N_PG_HASH]; |
| 261 | while( p && p->pgno!=pgno ){ |
| 262 | p = p->pNextHash; |
| 263 | } |
| 264 | return p; |
| 265 | } |
| 266 | |
| 267 | /* |
| 268 | ** Unlock the database and clear the in-memory cache. This routine |
| 269 | ** sets the state of the pager back to what it was when it was first |
| 270 | ** opened. Any outstanding pages are invalidated and subsequent attempts |
| 271 | ** to access those pages will likely result in a coredump. |
| 272 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 273 | static void pager_reset(Pager *pPager){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 274 | PgHdr *pPg, *pNext; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 275 | for(pPg=pPager->pAll; pPg; pPg=pNext){ |
| 276 | pNext = pPg->pNextAll; |
| 277 | sqliteFree(pPg); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 278 | } |
| 279 | pPager->pFirst = 0; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 280 | pPager->pLast = 0; |
| 281 | pPager->pAll = 0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 282 | memset(pPager->aHash, 0, sizeof(pPager->aHash)); |
| 283 | pPager->nPage = 0; |
| 284 | if( pPager->state==SQLITE_WRITELOCK ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 285 | sqlitepager_rollback(pPager); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 286 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 287 | pager_unlock(pPager->fd); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 288 | pPager->state = SQLITE_UNLOCK; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 289 | pPager->dbSize = -1; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 290 | pPager->nRef = 0; |
| 291 | } |
| 292 | |
| 293 | /* |
| 294 | ** When this routine is called, the pager has the journal file open and |
| 295 | ** a write lock on the database. This routine releases the database |
| 296 | ** write lock and acquires a read lock in its place. The journal file |
| 297 | ** is deleted and closed. |
| 298 | ** |
| 299 | ** We have to release the write lock before acquiring the read lock, |
| 300 | ** so there is a race condition where another process can get the lock |
| 301 | ** while we are not holding it. But, no other process should do this |
| 302 | ** because we are also holding a lock on the journal, and no process |
| 303 | ** should get a write lock on the database without first getting a lock |
| 304 | ** on the journal. So this routine should never fail. But it can fail |
| 305 | ** if another process is not playing by the rules. If it does fail, |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 306 | ** all in-memory cache pages are invalidated, the PAGER_ERR_LOCK bit |
| 307 | ** is set in pPager->errMask, and this routine returns SQLITE_PROTOCOL. |
| 308 | ** SQLITE_OK is returned on success. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 309 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 310 | static int pager_unwritelock(Pager *pPager){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 311 | int rc; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 312 | PgHdr *pPg; |
| 313 | if( pPager->state!=SQLITE_WRITELOCK ) return SQLITE_OK; |
| 314 | pager_unlock(pPager->fd); |
| 315 | rc = pager_lock(pPager->fd, 0); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 316 | unlink(pPager->zJournal); |
| 317 | close(pPager->jfd); |
| 318 | pPager->jfd = -1; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 319 | for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ |
| 320 | pPg->inJournal = 0; |
| 321 | pPg->dirty = 0; |
| 322 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 323 | if( rc!=SQLITE_OK ){ |
| 324 | pPager->state = SQLITE_UNLOCK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 325 | rc = SQLITE_PROTOCOL; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 326 | pPager->errMask |= PAGER_ERR_LOCK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 327 | }else{ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 328 | rc = SQLITE_OK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 329 | pPager->state = SQLITE_READLOCK; |
| 330 | } |
| 331 | return rc; |
| 332 | } |
| 333 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 334 | /* |
| 335 | ** Playback the journal and thus restore the database file to |
| 336 | ** the state it was in before we started making changes. |
| 337 | ** |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 338 | ** The journal file format is as follows: There is an initial |
| 339 | ** file-type string for sanity checking. Then there is a single |
| 340 | ** Pgno number which is the number of pages in the database before |
| 341 | ** changes were made. The database is truncated to this size. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame^] | 342 | ** Next come zero or more page records where each page record |
| 343 | ** consists of a Pgno and SQLITE_PAGE_SIZE bytes of data. See |
| 344 | ** the PageRecord structure for details. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 345 | ** |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 346 | ** For playback, the pages have to be read from the journal in |
| 347 | ** reverse order and put back into the original database file. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 348 | ** |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 349 | ** If the file opened as the journal file is not a well-formed |
| 350 | ** journal file (as determined by looking at the magic number |
| 351 | ** at the beginning) then this routine returns SQLITE_PROTOCOL. |
| 352 | ** If any other errors occur during playback, the database will |
| 353 | ** likely be corrupted, so the PAGER_ERR_CORRUPT bit is set in |
| 354 | ** pPager->errMask and SQLITE_CORRUPT is returned. If it all |
| 355 | ** works, then this routine returns SQLITE_OK. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 356 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 357 | static int pager_playback(Pager *pPager){ |
| 358 | int nRec; /* Number of Records */ |
| 359 | int i; /* Loop counter */ |
| 360 | Pgno mxPg = 0; /* Size of the original file in pages */ |
| 361 | struct stat statbuf; /* Used to size the journal */ |
| 362 | PgHdr *pPg; /* An existing page in the cache */ |
| 363 | PageRecord pgRec; |
| 364 | unsigned char aMagic[sizeof(aJournalMagic)]; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 365 | int rc; |
| 366 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 367 | /* Read the beginning of the journal and truncate the |
| 368 | ** database file back to its original size. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 369 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 370 | assert( pPager->jfd>=0 ); |
| 371 | pager_seek(pPager->jfd, 0); |
| 372 | rc = pager_read(pPager->jfd, aMagic, sizeof(aMagic)); |
| 373 | if( rc!=SQLITE_OK || memcmp(aMagic,aJournalMagic,sizeof(aMagic))!=0 ){ |
| 374 | return SQLITE_PROTOCOL; |
| 375 | } |
| 376 | rc = pager_read(pPager->jfd, &mxPg, sizeof(mxPg)); |
| 377 | if( rc!=SQLITE_OK ){ |
| 378 | return SQLITE_PROTOCOL; |
| 379 | } |
| 380 | pager_truncate(pPager->fd, mxPg); |
| 381 | pPager->dbSize = mxPg; |
| 382 | |
| 383 | /* Begin reading the journal beginning at the end and moving |
| 384 | ** toward the beginning. |
| 385 | */ |
| 386 | if( fstat(pPager->jfd, &statbuf)!=0 ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 387 | return SQLITE_OK; |
| 388 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 389 | nRec = (statbuf.st_size - (sizeof(aMagic)+sizeof(Pgno))) / sizeof(PageRecord); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 390 | |
| 391 | /* Process segments beginning with the last and working backwards |
| 392 | ** to the first. |
| 393 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 394 | for(i=nRec-1; i>=0; i--){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 395 | /* Seek to the beginning of the segment */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 396 | off_t ofst; |
| 397 | ofst = i*sizeof(PageRecord) + sizeof(aMagic) + sizeof(Pgno); |
| 398 | rc = pager_seek(pPager->jfd, ofst); |
| 399 | if( rc!=SQLITE_OK ) break; |
| 400 | rc = pager_read(pPager->jfd, &pgRec, sizeof(pgRec)); |
| 401 | if( rc!=SQLITE_OK ) break; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 402 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 403 | /* Sanity checking on the page */ |
| 404 | if( pgRec.pgno>mxPg || pgRec.pgno==0 ){ |
| 405 | rc = SQLITE_CORRUPT; |
| 406 | break; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 407 | } |
| 408 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 409 | /* Playback the page. Update the in-memory copy of the page |
| 410 | ** at the same time, if there is one. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 411 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 412 | pPg = pager_lookup(pPager, pgRec.pgno); |
| 413 | if( pPg ){ |
| 414 | memcpy(PGHDR_TO_DATA(pPg), pgRec.aData, SQLITE_PAGE_SIZE); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 415 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 416 | rc = pager_seek(pPager->fd, (pgRec.pgno-1)*SQLITE_PAGE_SIZE); |
| 417 | if( rc!=SQLITE_OK ) break; |
| 418 | rc = pager_write(pPager->fd, pgRec.aData, SQLITE_PAGE_SIZE); |
| 419 | if( rc!=SQLITE_OK ) break; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 420 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 421 | if( rc!=SQLITE_OK ){ |
| 422 | pager_unwritelock(pPager); |
| 423 | pPager->errMask |= PAGER_ERR_CORRUPT; |
| 424 | rc = SQLITE_CORRUPT; |
| 425 | }else{ |
| 426 | rc = pager_unwritelock(pPager); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 427 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 428 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 429 | } |
| 430 | |
| 431 | /* |
| 432 | ** Create a new page cache and put a pointer to the page cache in *ppPager. |
| 433 | ** The file to be cached need not exist. The file is not opened until |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 434 | ** the first call to sqlitepager_get() and is only held open until the |
| 435 | ** last page is released using sqlitepager_unref(). |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 436 | */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 437 | int sqlitepager_open( |
| 438 | Pager **ppPager, /* Return the Pager structure here */ |
| 439 | const char *zFilename, /* Name of the database file to open */ |
| 440 | int mxPage, /* Max number of in-memory cache pages */ |
| 441 | int nExtra /* Extra bytes append to each in-memory page */ |
| 442 | ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 443 | Pager *pPager; |
| 444 | int nameLen; |
| 445 | int fd; |
| 446 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 447 | *ppPager = 0; |
| 448 | if( sqlite_malloc_failed ){ |
| 449 | return SQLITE_NOMEM; |
| 450 | } |
| 451 | fd = open(zFilename, O_RDWR|O_CREAT, 0644); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 452 | if( fd<0 ){ |
| 453 | return SQLITE_CANTOPEN; |
| 454 | } |
| 455 | nameLen = strlen(zFilename); |
| 456 | pPager = sqliteMalloc( sizeof(*pPager) + nameLen*2 + 30 ); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 457 | if( pPager==0 ){ |
| 458 | close(fd); |
| 459 | return SQLITE_NOMEM; |
| 460 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 461 | pPager->zFilename = (char*)&pPager[1]; |
| 462 | pPager->zJournal = &pPager->zFilename[nameLen+1]; |
| 463 | strcpy(pPager->zFilename, zFilename); |
| 464 | strcpy(pPager->zJournal, zFilename); |
| 465 | strcpy(&pPager->zJournal[nameLen], "-journal"); |
| 466 | pPager->fd = fd; |
| 467 | pPager->jfd = -1; |
| 468 | pPager->nRef = 0; |
| 469 | pPager->dbSize = -1; |
| 470 | pPager->nPage = 0; |
drh | d79caeb | 2001-04-15 02:27:24 +0000 | [diff] [blame] | 471 | pPager->mxPage = mxPage>5 ? mxPage : 10; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 472 | pPager->state = SQLITE_UNLOCK; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 473 | pPager->errMask = 0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 474 | pPager->pFirst = 0; |
| 475 | pPager->pLast = 0; |
| 476 | memset(pPager->aHash, 0, sizeof(pPager->aHash)); |
| 477 | *ppPager = pPager; |
| 478 | return SQLITE_OK; |
| 479 | } |
| 480 | |
| 481 | /* |
| 482 | ** Return the total number of pages in the file opened by pPager. |
| 483 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 484 | int sqlitepager_pagecount(Pager *pPager){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 485 | int n; |
| 486 | struct stat statbuf; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 487 | assert( pPager!=0 ); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 488 | if( pPager->dbSize>=0 ){ |
| 489 | return pPager->dbSize; |
| 490 | } |
| 491 | if( fstat(pPager->fd, &statbuf)!=0 ){ |
| 492 | n = 0; |
| 493 | }else{ |
| 494 | n = statbuf.st_size/SQLITE_PAGE_SIZE; |
| 495 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 496 | if( pPager->state!=SQLITE_UNLOCK ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 497 | pPager->dbSize = n; |
| 498 | } |
| 499 | return n; |
| 500 | } |
| 501 | |
| 502 | /* |
| 503 | ** Shutdown the page cache. Free all memory and close all files. |
| 504 | ** |
| 505 | ** If a transaction was in progress when this routine is called, that |
| 506 | ** transaction is rolled back. All outstanding pages are invalidated |
| 507 | ** and their memory is freed. Any attempt to use a page associated |
| 508 | ** with this page cache after this function returns will likely |
| 509 | ** result in a coredump. |
| 510 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 511 | int sqlitepager_close(Pager *pPager){ |
| 512 | PgHdr *pPg, *pNext; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 513 | switch( pPager->state ){ |
| 514 | case SQLITE_WRITELOCK: { |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 515 | sqlitepager_rollback(pPager); |
| 516 | pager_unlock(pPager->fd); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 517 | break; |
| 518 | } |
| 519 | case SQLITE_READLOCK: { |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 520 | pager_unlock(pPager->fd); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 521 | break; |
| 522 | } |
| 523 | default: { |
| 524 | /* Do nothing */ |
| 525 | break; |
| 526 | } |
| 527 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 528 | for(pPg=pPager->pAll; pPg; pPg=pNext){ |
| 529 | pNext = pPg->pNextAll; |
| 530 | sqliteFree(pPg); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 531 | } |
| 532 | if( pPager->fd>=0 ) close(pPager->fd); |
| 533 | assert( pPager->jfd<0 ); |
| 534 | sqliteFree(pPager); |
| 535 | return SQLITE_OK; |
| 536 | } |
| 537 | |
| 538 | /* |
| 539 | ** Return the page number for the given page data |
| 540 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 541 | Pgno sqlitepager_pagenumber(void *pData){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 542 | PgHdr *p = DATA_TO_PGHDR(pData); |
| 543 | return p->pgno; |
| 544 | } |
| 545 | |
| 546 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 547 | ** Increment the reference count for a page. If the page is |
| 548 | ** currently on the freelist (the reference count is zero) then |
| 549 | ** remove it from the freelist. |
| 550 | */ |
| 551 | static void sqlitepager_ref(PgHdr *pPg){ |
| 552 | if( pPg->nRef==0 ){ |
| 553 | /* The page is currently on the freelist. Remove it. */ |
| 554 | if( pPg->pPrevFree ){ |
| 555 | pPg->pPrevFree->pNextFree = pPg->pNextFree; |
| 556 | }else{ |
| 557 | pPg->pPager->pFirst = pPg->pNextFree; |
| 558 | } |
| 559 | if( pPg->pNextFree ){ |
| 560 | pPg->pNextFree->pPrevFree = pPg->pPrevFree; |
| 561 | }else{ |
| 562 | pPg->pPager->pLast = pPg->pPrevFree; |
| 563 | } |
| 564 | pPg->pPager->nRef++; |
| 565 | } |
| 566 | pPg->nRef++; |
| 567 | } |
| 568 | |
| 569 | /* |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 570 | ** Acquire a page. |
| 571 | ** |
| 572 | ** A read lock is obtained for the first page acquired. The lock |
| 573 | ** is dropped when the last page is released. |
| 574 | ** |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame^] | 575 | ** A _get works for any page number greater than 0. If the database |
| 576 | ** file is smaller than the requested page, then no actual disk |
| 577 | ** read occurs and the memory image of the page is initialized to |
| 578 | ** all zeros. The extra data appended to a page is always initialized |
| 579 | ** to zeros the first time a page is loaded into memory. |
| 580 | ** |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 581 | ** The acquisition might fail for several reasons. In all cases, |
| 582 | ** an appropriate error code is returned and *ppPage is set to NULL. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 583 | ** |
| 584 | ** See also sqlitepager_lookup(). Both this routine and _lookup() attempt |
| 585 | ** to find a page in the in-memory cache first. If the page is not already |
| 586 | ** in cache, this routine goes to disk to read it in whereas _lookup() |
| 587 | ** just returns 0. This routine acquires a read-lock the first time it |
| 588 | ** has to go to disk, and could also playback an old journal if necessary. |
| 589 | ** Since _lookup() never goes to disk, it never has to deal with locks |
| 590 | ** or journal files. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 591 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 592 | int sqlitepager_get(Pager *pPager, Pgno pgno, void **ppPage){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 593 | PgHdr *pPg; |
| 594 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 595 | /* Make sure we have not hit any critical errors. |
| 596 | */ |
| 597 | if( pPager==0 || pgno==0 ){ |
| 598 | return SQLITE_ERROR; |
| 599 | } |
| 600 | if( pPager->errMask & ~(PAGER_ERR_FULL) ){ |
| 601 | return pager_errcode(pPager); |
| 602 | } |
| 603 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 604 | /* If this is the first page accessed, then get a read lock |
| 605 | ** on the database file. |
| 606 | */ |
| 607 | if( pPager->nRef==0 ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 608 | if( pager_lock(pPager->fd, 0)!=0 ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 609 | *ppPage = 0; |
| 610 | return SQLITE_BUSY; |
| 611 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 612 | pPager->state = SQLITE_READLOCK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 613 | |
| 614 | /* If a journal file exists, try to play it back. |
| 615 | */ |
| 616 | if( access(pPager->zJournal,0)==0 ){ |
| 617 | int rc; |
| 618 | |
| 619 | /* Open the journal for exclusive access. Return SQLITE_BUSY if |
| 620 | ** we cannot get exclusive access to the journal file |
| 621 | */ |
| 622 | pPager->jfd = open(pPager->zJournal, O_RDONLY, 0); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 623 | if( pPager->jfd<0 || pager_lock(pPager->jfd, 1)!=0 ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 624 | if( pPager->jfd>=0 ){ close(pPager->jfd); pPager->jfd = -1; } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 625 | pager_unlock(pPager->fd); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 626 | *ppPage = 0; |
| 627 | return SQLITE_BUSY; |
| 628 | } |
| 629 | |
| 630 | /* Get a write lock on the database */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 631 | pager_unlock(pPager->fd); |
| 632 | if( pager_lock(pPager->fd, 1)!=0 ){ |
| 633 | close(pPager->jfd); |
| 634 | pPager->jfd = -1; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 635 | *ppPage = 0; |
| 636 | return SQLITE_PROTOCOL; |
| 637 | } |
| 638 | |
| 639 | /* Playback and delete the journal. Drop the database write |
| 640 | ** lock and reacquire the read lock. |
| 641 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 642 | rc = pager_playback(pPager); |
| 643 | if( rc!=SQLITE_OK ){ |
| 644 | return rc; |
| 645 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 646 | } |
| 647 | pPg = 0; |
| 648 | }else{ |
| 649 | /* Search for page in cache */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 650 | pPg = pager_lookup(pPager, pgno); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 651 | } |
| 652 | if( pPg==0 ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 653 | /* The requested page is not in the page cache. */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 654 | int h; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 655 | pPager->nMiss++; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 656 | if( pPager->nPage<pPager->mxPage || pPager->pFirst==0 ){ |
| 657 | /* Create a new page */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 658 | pPg = sqliteMalloc( sizeof(*pPg) + SQLITE_PAGE_SIZE + pPager->nExtra ); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 659 | if( pPg==0 ){ |
| 660 | *ppPage = 0; |
| 661 | pager_unwritelock(pPager); |
| 662 | pPager->errMask |= PAGER_ERR_MEM; |
| 663 | return SQLITE_NOMEM; |
| 664 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 665 | pPg->pPager = pPager; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 666 | pPg->pNextAll = pPager->pAll; |
| 667 | if( pPager->pAll ){ |
| 668 | pPager->pAll->pPrevAll = pPg; |
| 669 | } |
| 670 | pPg->pPrevAll = 0; |
drh | d79caeb | 2001-04-15 02:27:24 +0000 | [diff] [blame] | 671 | pPager->pAll = pPg; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 672 | pPager->nPage++; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 673 | }else{ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 674 | /* Recycle an older page. First locate the page to be recycled. |
| 675 | ** Try to find one that is not dirty and is near the head of |
| 676 | ** of the free list */ |
| 677 | int cnt = 4; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 678 | pPg = pPager->pFirst; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 679 | while( pPg->dirty && 0<cnt-- ){ |
| 680 | pPg = pPg->pNextFree; |
| 681 | } |
| 682 | if( pPg==0 || pPg->dirty ) pPg = pPager->pFirst; |
| 683 | assert( pPg->nRef==0 ); |
| 684 | |
| 685 | /* If the page to be recycled is dirty, sync the journal and write |
| 686 | ** the old page into the database. */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 687 | if( pPg->dirty ){ |
| 688 | int rc; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 689 | assert( pPg->inJournal==1 ); |
| 690 | assert( pPager->state==SQLITE_WRITELOCK ); |
| 691 | rc = fsync(pPager->jfd); |
| 692 | if( rc!=0 ){ |
| 693 | rc = sqlitepager_rollback(pPager); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 694 | *ppPage = 0; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 695 | if( rc==SQLITE_OK ) rc = SQLITE_IOERR; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 696 | return rc; |
| 697 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 698 | pager_seek(pPager->fd, (pPg->pgno-1)*SQLITE_PAGE_SIZE); |
| 699 | rc = pager_write(pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE); |
| 700 | if( rc!=SQLITE_OK ){ |
| 701 | rc = sqlitepager_rollback(pPager); |
| 702 | *ppPage = 0; |
| 703 | if( rc==SQLITE_OK ) rc = SQLITE_FULL; |
| 704 | return rc; |
| 705 | } |
| 706 | } |
| 707 | |
| 708 | /* Unlink the old page from the free list and the hash table |
| 709 | */ |
| 710 | pPager->pFirst = pPg->pNextFree; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 711 | if( pPager->pFirst ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 712 | pPager->pFirst->pPrevFree = 0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 713 | }else{ |
| 714 | pPager->pLast = 0; |
| 715 | } |
| 716 | if( pPg->pNextHash ){ |
| 717 | pPg->pNextHash->pPrevHash = pPg->pPrevHash; |
| 718 | } |
| 719 | if( pPg->pPrevHash ){ |
| 720 | pPg->pPrevHash->pNextHash = pPg->pNextHash; |
| 721 | }else{ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 722 | h = pager_hash(pPg->pgno); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 723 | assert( pPager->aHash[h]==pPg ); |
| 724 | pPager->aHash[h] = pPg->pNextHash; |
| 725 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 726 | pPager->nOvfl++; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 727 | } |
| 728 | pPg->pgno = pgno; |
| 729 | pPg->inJournal = 0; |
| 730 | pPg->dirty = 0; |
| 731 | pPg->nRef = 1; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 732 | pPager->nRef++; |
| 733 | h = pager_hash(pgno); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 734 | pPg->pNextHash = pPager->aHash[h]; |
| 735 | pPager->aHash[h] = pPg; |
| 736 | if( pPg->pNextHash ){ |
| 737 | assert( pPg->pNextHash->pPrevHash==0 ); |
| 738 | pPg->pNextHash->pPrevHash = pPg; |
| 739 | } |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame^] | 740 | if( pPager->dbSize<0 ) sqlitepager_pagecount(pPager); |
| 741 | if( pPager->dbSize<pgno ){ |
| 742 | memset(PGHDR_TO_DATA(pPg), 0, SQLITE_PAGE_SIZE); |
| 743 | }else{ |
| 744 | pager_seek(pPager->fd, (pgno-1)*SQLITE_PAGE_SIZE); |
| 745 | pager_read(pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE); |
| 746 | } |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 747 | if( pPager->nExtra>0 ){ |
| 748 | memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra); |
| 749 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 750 | }else{ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 751 | /* The requested page is in the page cache. */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 752 | pPager->nHit++; |
| 753 | sqlitepager_ref(pPg); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 754 | } |
| 755 | *ppPage = PGHDR_TO_DATA(pPg); |
| 756 | return SQLITE_OK; |
| 757 | } |
| 758 | |
| 759 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 760 | ** Acquire a page if it is already in the in-memory cache. Do |
| 761 | ** not read the page from disk. Return a pointer to the page, |
| 762 | ** or 0 if the page is not in cache. |
| 763 | ** |
| 764 | ** See also sqlitepager_get(). The difference between this routine |
| 765 | ** and sqlitepager_get() is that _get() will go to the disk and read |
| 766 | ** in the page if the page is not already in cache. This routine |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame^] | 767 | ** returns NULL if the page is not in cache of if a disk I/O has ever |
| 768 | ** happened. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 769 | */ |
| 770 | void *sqlitepager_lookup(Pager *pPager, Pgno pgno){ |
| 771 | PgHdr *pPg; |
| 772 | |
| 773 | /* Make sure we have not hit any critical errors. |
| 774 | */ |
| 775 | if( pPager==0 || pgno==0 ){ |
| 776 | return 0; |
| 777 | } |
| 778 | if( pPager->errMask & ~(PAGER_ERR_FULL) ){ |
| 779 | return 0; |
| 780 | } |
| 781 | if( pPager->nRef==0 ){ |
| 782 | return 0; |
| 783 | } |
| 784 | pPg = pager_lookup(pPager, pgno); |
| 785 | if( pPg==0 ) return 0; |
| 786 | sqlitepager_ref(pPg); |
| 787 | return PGHDR_TO_DATA(pPg); |
| 788 | } |
| 789 | |
| 790 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 791 | ** Release a page. |
| 792 | ** |
| 793 | ** If the number of references to the page drop to zero, then the |
| 794 | ** page is added to the LRU list. When all references to all pages |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 795 | ** are released, a rollback occurs and the lock on the database is |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 796 | ** removed. |
| 797 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 798 | int sqlitepager_unref(void *pData){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 799 | Pager *pPager; |
| 800 | PgHdr *pPg; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 801 | |
| 802 | /* Decrement the reference count for this page |
| 803 | */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 804 | pPg = DATA_TO_PGHDR(pData); |
| 805 | assert( pPg->nRef>0 ); |
| 806 | pPager = pPg->pPager; |
| 807 | pPg->nRef--; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 808 | |
| 809 | /* When the number of references to a page reach 0, add the |
| 810 | ** page to the freelist. |
| 811 | */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 812 | if( pPg->nRef==0 ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 813 | pPg->pNextFree = 0; |
| 814 | pPg->pPrevFree = pPager->pLast; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 815 | pPager->pLast = pPg; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 816 | if( pPg->pPrevFree ){ |
| 817 | pPg->pPrevFree->pNextFree = pPg; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 818 | }else{ |
| 819 | pPager->pFirst = pPg; |
| 820 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 821 | |
| 822 | /* When all pages reach the freelist, drop the read lock from |
| 823 | ** the database file. |
| 824 | */ |
| 825 | pPager->nRef--; |
| 826 | assert( pPager->nRef>=0 ); |
| 827 | if( pPager->nRef==0 ){ |
| 828 | pager_reset(pPager); |
| 829 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 830 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 831 | return SQLITE_OK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 832 | } |
| 833 | |
| 834 | /* |
| 835 | ** Mark a data page as writeable. The page is written into the journal |
| 836 | ** if it is not there already. This routine must be called before making |
| 837 | ** changes to a page. |
| 838 | ** |
| 839 | ** The first time this routine is called, the pager creates a new |
| 840 | ** journal and acquires a write lock on the database. If the write |
| 841 | ** lock could not be acquired, this routine returns SQLITE_BUSY. The |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame^] | 842 | ** calling routine must check for that return value and be careful not to |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 843 | ** change any page data until this routine returns SQLITE_OK. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 844 | ** |
| 845 | ** If the journal file could not be written because the disk is full, |
| 846 | ** then this routine returns SQLITE_FULL and does an immediate rollback. |
| 847 | ** All subsequent write attempts also return SQLITE_FULL until there |
| 848 | ** is a call to sqlitepager_commit() or sqlitepager_rollback() to |
| 849 | ** reset. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 850 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 851 | int sqlitepager_write(void *pData){ |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 852 | PgHdr *pPg = DATA_TO_PGHDR(pData); |
| 853 | Pager *pPager = pPg->pPager; |
drh | d79caeb | 2001-04-15 02:27:24 +0000 | [diff] [blame] | 854 | int rc = SQLITE_OK; |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 855 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 856 | if( pPager->errMask ){ |
| 857 | return pager_errcode(pPager); |
| 858 | } |
| 859 | pPg->dirty = 1; |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 860 | if( pPg->inJournal ){ return SQLITE_OK; } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 861 | assert( pPager->state!=SQLITE_UNLOCK ); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 862 | if( pPager->state==SQLITE_READLOCK ){ |
| 863 | pPager->jfd = open(pPager->zJournal, O_RDWR|O_CREAT, 0644); |
| 864 | if( pPager->jfd<0 ){ |
| 865 | return SQLITE_CANTOPEN; |
| 866 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 867 | if( pager_lock(pPager->jfd, 1) ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 868 | close(pPager->jfd); |
| 869 | pPager->jfd = -1; |
| 870 | return SQLITE_BUSY; |
| 871 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 872 | pager_unlock(pPager->fd); |
| 873 | if( pager_lock(pPager->fd, 1) ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 874 | close(pPager->jfd); |
| 875 | pPager->jfd = -1; |
| 876 | pPager->state = SQLITE_UNLOCK; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 877 | pPager->errMask |= PAGER_ERR_LOCK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 878 | return SQLITE_PROTOCOL; |
| 879 | } |
| 880 | pPager->state = SQLITE_WRITELOCK; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 881 | sqlitepager_pagecount(pPager); |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 882 | pPager->origDbSize = pPager->dbSize; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 883 | rc = pager_write(pPager->jfd, aJournalMagic, sizeof(aJournalMagic)); |
| 884 | if( rc==SQLITE_OK ){ |
| 885 | rc = pager_write(pPager->jfd, &pPager->dbSize, sizeof(Pgno)); |
| 886 | } |
| 887 | if( rc!=SQLITE_OK ){ |
| 888 | rc = pager_unwritelock(pPager); |
| 889 | if( rc==SQLITE_OK ) rc = SQLITE_FULL; |
| 890 | return rc; |
| 891 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 892 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 893 | assert( pPager->state==SQLITE_WRITELOCK ); |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 894 | assert( pPager->jfd>=0 ); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 895 | if( pPg->pgno <= pPager->origDbSize ){ |
| 896 | rc = pager_write(pPager->jfd, &pPg->pgno, sizeof(Pgno)); |
| 897 | if( rc==SQLITE_OK ){ |
| 898 | rc = pager_write(pPager->jfd, pData, SQLITE_PAGE_SIZE); |
| 899 | } |
| 900 | if( rc!=SQLITE_OK ){ |
| 901 | sqlitepager_rollback(pPager); |
| 902 | pPager->errMask |= PAGER_ERR_FULL; |
| 903 | return rc; |
| 904 | } |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 905 | } |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 906 | pPg->inJournal = 1; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame^] | 907 | if( pPager->dbSize<pPg->pgno ){ |
| 908 | pPager->dbSize = pPg->pgno; |
| 909 | } |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 910 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 911 | } |
| 912 | |
| 913 | /* |
| 914 | ** Commit all changes to the database and release the write lock. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 915 | ** |
| 916 | ** If the commit fails for any reason, a rollback attempt is made |
| 917 | ** and an error code is returned. If the commit worked, SQLITE_OK |
| 918 | ** is returned. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 919 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 920 | int sqlitepager_commit(Pager *pPager){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 921 | int i, rc; |
| 922 | PgHdr *pPg; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 923 | |
| 924 | if( pPager->errMask==PAGER_ERR_FULL ){ |
| 925 | rc = sqlitepager_rollback(pPager); |
| 926 | if( rc==SQLITE_OK ) rc = SQLITE_FULL; |
| 927 | return rc; |
| 928 | } |
| 929 | if( pPager->errMask!=0 ){ |
| 930 | rc = pager_errcode(pPager); |
| 931 | return rc; |
| 932 | } |
| 933 | if( pPager->state!=SQLITE_WRITELOCK ){ |
| 934 | return SQLITE_ERROR; |
| 935 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 936 | assert( pPager->jfd>=0 ); |
| 937 | if( fsync(pPager->jfd) ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 938 | goto commit_abort; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 939 | } |
| 940 | for(i=0; i<N_PG_HASH; i++){ |
| 941 | for(pPg=pPager->aHash[i]; pPg; pPg=pPg->pNextHash){ |
| 942 | if( pPg->dirty==0 ) continue; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 943 | rc = pager_seek(pPager->fd, (pPg->pgno-1)*SQLITE_PAGE_SIZE); |
| 944 | if( rc!=SQLITE_OK ) goto commit_abort; |
| 945 | rc = pager_write(pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE); |
| 946 | if( rc!=SQLITE_OK ) goto commit_abort; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 947 | } |
| 948 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 949 | if( fsync(pPager->fd) ) goto commit_abort; |
| 950 | rc = pager_unwritelock(pPager); |
| 951 | pPager->dbSize = -1; |
| 952 | return rc; |
| 953 | |
| 954 | /* Jump here if anything goes wrong during the commit process. |
| 955 | */ |
| 956 | commit_abort: |
| 957 | rc = sqlitepager_rollback(pPager); |
| 958 | if( rc==SQLITE_OK ){ |
| 959 | rc = SQLITE_FULL; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 960 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 961 | return rc; |
| 962 | } |
| 963 | |
| 964 | /* |
| 965 | ** Rollback all changes. The database falls back to read-only mode. |
| 966 | ** All in-memory cache pages revert to their original data contents. |
| 967 | ** The journal is deleted. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 968 | ** |
| 969 | ** This routine cannot fail unless some other process is not following |
| 970 | ** the correct locking protocol (SQLITE_PROTOCOL) or unless some other |
| 971 | ** process is writing trash into the journal file (SQLITE_CORRUPT) or |
| 972 | ** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error |
| 973 | ** codes are returned for all these occasions. Otherwise, |
| 974 | ** SQLITE_OK is returned. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 975 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 976 | int sqlitepager_rollback(Pager *pPager){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 977 | int rc; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 978 | if( pPager->errMask!=0 && pPager->errMask!=PAGER_ERR_FULL ){ |
| 979 | return pager_errcode(pPager); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 980 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 981 | if( pPager->state!=SQLITE_WRITELOCK ){ |
| 982 | return SQLITE_OK; |
| 983 | } |
| 984 | rc = pager_playback(pPager); |
| 985 | if( rc!=SQLITE_OK ){ |
| 986 | rc = SQLITE_CORRUPT; |
| 987 | pPager->errMask |= PAGER_ERR_CORRUPT; |
| 988 | } |
| 989 | pPager->dbSize = -1; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 990 | return rc; |
| 991 | }; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 992 | |
| 993 | /* |
| 994 | ** This routine is used for testing and analysis only. |
| 995 | */ |
| 996 | int *sqlitepager_stats(Pager *pPager){ |
| 997 | static int a[9]; |
| 998 | a[0] = pPager->nRef; |
| 999 | a[1] = pPager->nPage; |
| 1000 | a[2] = pPager->mxPage; |
| 1001 | a[3] = pPager->dbSize; |
| 1002 | a[4] = pPager->state; |
| 1003 | a[5] = pPager->errMask; |
| 1004 | a[6] = pPager->nHit; |
| 1005 | a[7] = pPager->nMiss; |
| 1006 | a[8] = pPager->nOvfl; |
| 1007 | return a; |
| 1008 | } |