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