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