drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1 | /* |
| 2 | ** Copyright (c) 2001 D. Richard Hipp |
| 3 | ** |
| 4 | ** This program is free software; you can redistribute it and/or |
| 5 | ** modify it under the terms of the GNU General Public |
| 6 | ** License as published by the Free Software Foundation; either |
| 7 | ** version 2 of the License, or (at your option) any later version. |
| 8 | ** |
| 9 | ** This program is distributed in the hope that it will be useful, |
| 10 | ** but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
| 12 | ** General Public License for more details. |
| 13 | ** |
| 14 | ** You should have received a copy of the GNU General Public |
| 15 | ** License along with this library; if not, write to the |
| 16 | ** Free Software Foundation, Inc., 59 Temple Place - Suite 330, |
| 17 | ** Boston, MA 02111-1307, USA. |
| 18 | ** |
| 19 | ** Author contact information: |
| 20 | ** drh@hwaci.com |
| 21 | ** http://www.hwaci.com/drh/ |
| 22 | ** |
| 23 | ************************************************************************* |
| 24 | ** This is the implementation of the page cache subsystem. |
| 25 | ** |
| 26 | ** The page cache is used to access a database file. The pager journals |
| 27 | ** all writes in order to support rollback. Locking is used to limit |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 28 | ** access to one or more reader or to one writer. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 29 | ** |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame^] | 30 | ** @(#) $Id: pager.c,v 1.19 2001/09/15 00:57:29 drh Exp $ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 31 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 32 | #include "sqliteInt.h" |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 33 | #include "pager.h" |
| 34 | #include <fcntl.h> |
| 35 | #include <sys/stat.h> |
| 36 | #include <unistd.h> |
| 37 | #include <assert.h> |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 38 | #include <string.h> |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 39 | |
| 40 | /* |
| 41 | ** The page cache as a whole is always in one of the following |
| 42 | ** states: |
| 43 | ** |
| 44 | ** SQLITE_UNLOCK The page cache is not currently reading or |
| 45 | ** writing the database file. There is no |
| 46 | ** data held in memory. This is the initial |
| 47 | ** state. |
| 48 | ** |
| 49 | ** SQLITE_READLOCK The page cache is reading the database. |
| 50 | ** Writing is not permitted. There can be |
| 51 | ** multiple readers accessing the same database |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 52 | ** file at the same time. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 53 | ** |
| 54 | ** SQLITE_WRITELOCK The page cache is writing the database. |
| 55 | ** Access is exclusive. No other processes or |
| 56 | ** threads can be reading or writing while one |
| 57 | ** process is writing. |
| 58 | ** |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 59 | ** The page cache comes up in SQLITE_UNLOCK. The first time a |
| 60 | ** sqlite_page_get() occurs, the state transitions to SQLITE_READLOCK. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 61 | ** After all pages have been released using sqlite_page_unref(), |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 62 | ** the state transitions back to SQLITE_UNLOCK. The first time |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 63 | ** that sqlite_page_write() is called, the state transitions to |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 64 | ** SQLITE_WRITELOCK. (Note that sqlite_page_write() can only be |
| 65 | ** called on an outstanding page which means that the pager must |
| 66 | ** be in SQLITE_READLOCK before it transitions to SQLITE_WRITELOCK.) |
| 67 | ** The sqlite_page_rollback() and sqlite_page_commit() functions |
| 68 | ** transition the state from SQLITE_WRITELOCK back to SQLITE_READLOCK. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 69 | */ |
| 70 | #define SQLITE_UNLOCK 0 |
| 71 | #define SQLITE_READLOCK 1 |
| 72 | #define SQLITE_WRITELOCK 2 |
| 73 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 74 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 75 | /* |
| 76 | ** Each in-memory image of a page begins with the following header. |
drh | bd03cae | 2001-06-02 02:40:57 +0000 | [diff] [blame] | 77 | ** This header is only visible to this pager module. The client |
| 78 | ** code that calls pager sees only the data that follows the header. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 79 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 80 | typedef struct PgHdr PgHdr; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 81 | struct PgHdr { |
| 82 | Pager *pPager; /* The pager to which this page belongs */ |
| 83 | Pgno pgno; /* The page number for this page */ |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 84 | PgHdr *pNextHash, *pPrevHash; /* Hash collision chain for PgHdr.pgno */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 85 | int nRef; /* Number of users of this page */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 86 | PgHdr *pNextFree, *pPrevFree; /* Freelist of pages where nRef==0 */ |
| 87 | PgHdr *pNextAll, *pPrevAll; /* A list of all pages */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 88 | char inJournal; /* TRUE if has been written to journal */ |
| 89 | char dirty; /* TRUE if we need to write back changes */ |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 90 | /* SQLITE_PAGE_SIZE bytes of page data follow this header */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 91 | /* Pager.nExtra bytes of local data follow the page data */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 92 | }; |
| 93 | |
| 94 | /* |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 95 | ** Convert a pointer to a PgHdr into a pointer to its data |
| 96 | ** and back again. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 97 | */ |
| 98 | #define PGHDR_TO_DATA(P) ((void*)(&(P)[1])) |
| 99 | #define DATA_TO_PGHDR(D) (&((PgHdr*)(D))[-1]) |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 100 | #define PGHDR_TO_EXTRA(P) ((void*)&((char*)(&(P)[1]))[SQLITE_PAGE_SIZE]) |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 101 | |
| 102 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 103 | ** How big to make the hash table used for locating in-memory pages |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 104 | ** by page number. Knuth says this should be a prime number. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 105 | */ |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 106 | #define N_PG_HASH 907 |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 107 | |
| 108 | /* |
| 109 | ** A open page cache is an instance of the following structure. |
| 110 | */ |
| 111 | struct Pager { |
| 112 | char *zFilename; /* Name of the database file */ |
| 113 | char *zJournal; /* Name of the journal file */ |
| 114 | int fd, jfd; /* File descriptors for database and journal */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 115 | int dbSize; /* Number of pages in the file */ |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 116 | int origDbSize; /* dbSize before the current change */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 117 | int nExtra; /* Add this many bytes to each in-memory page */ |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 118 | void (*xDestructor)(void*); /* Call this routine when freeing pages */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 119 | int nPage; /* Total number of in-memory pages */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 120 | int nRef; /* Number of in-memory pages with PgHdr.nRef>0 */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 121 | int mxPage; /* Maximum number of pages to hold in cache */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 122 | int nHit, nMiss, nOvfl; /* Cache hits, missing, and LRU overflows */ |
| 123 | unsigned char state; /* SQLITE_UNLOCK, _READLOCK or _WRITELOCK */ |
| 124 | unsigned char errMask; /* One of several kinds of errors */ |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 125 | unsigned char tempFile; /* zFilename is a temporary file */ |
| 126 | unsigned char readOnly; /* True for a read-only database */ |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 127 | unsigned char needSync; /* True if an fsync() is needed on the journal */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 128 | unsigned char *aInJournal; /* One bit for each page in the database file */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 129 | PgHdr *pFirst, *pLast; /* List of free pages */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 130 | PgHdr *pAll; /* List of all pages */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 131 | PgHdr *aHash[N_PG_HASH]; /* Hash table to map page number of PgHdr */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | /* |
| 135 | ** These are bits that can be set in Pager.errMask. |
| 136 | */ |
| 137 | #define PAGER_ERR_FULL 0x01 /* a write() failed */ |
| 138 | #define PAGER_ERR_MEM 0x02 /* malloc() failed */ |
| 139 | #define PAGER_ERR_LOCK 0x04 /* error in the locking protocol */ |
| 140 | #define PAGER_ERR_CORRUPT 0x08 /* database or journal corruption */ |
| 141 | |
| 142 | /* |
| 143 | ** The journal file contains page records in the following |
| 144 | ** format. |
| 145 | */ |
| 146 | typedef struct PageRecord PageRecord; |
| 147 | struct PageRecord { |
| 148 | Pgno pgno; /* The page number */ |
| 149 | char aData[SQLITE_PAGE_SIZE]; /* Original data for page pgno */ |
| 150 | }; |
| 151 | |
| 152 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 153 | ** Journal files begin with the following magic string. The data |
| 154 | ** was obtained from /dev/random. It is used only as a sanity check. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 155 | */ |
| 156 | static const unsigned char aJournalMagic[] = { |
| 157 | 0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd4, |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 158 | }; |
| 159 | |
| 160 | /* |
| 161 | ** Hash a page number |
| 162 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 163 | #define pager_hash(PN) ((PN)%N_PG_HASH) |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 164 | |
| 165 | /* |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 166 | ** Enable reference count tracking here: |
| 167 | */ |
| 168 | #if SQLITE_TEST |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 169 | int pager_refinfo_enable = 0; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 170 | static void pager_refinfo(PgHdr *p){ |
| 171 | static int cnt = 0; |
| 172 | if( !pager_refinfo_enable ) return; |
| 173 | printf( |
| 174 | "REFCNT: %4d addr=0x%08x nRef=%d\n", |
| 175 | p->pgno, (int)PGHDR_TO_DATA(p), p->nRef |
| 176 | ); |
| 177 | cnt++; /* Something to set a breakpoint on */ |
| 178 | } |
| 179 | # define REFINFO(X) pager_refinfo(X) |
| 180 | #else |
| 181 | # define REFINFO(X) |
| 182 | #endif |
| 183 | |
| 184 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 185 | ** Attempt to acquire a read lock (if wrlock==0) or a write lock (if wrlock==1) |
| 186 | ** on the database file. Return 0 on success and non-zero if the lock |
| 187 | ** could not be acquired. |
| 188 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 189 | static int pager_lock(int fd, int wrlock){ |
| 190 | int rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 191 | struct flock lock; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 192 | lock.l_type = wrlock ? F_WRLCK : F_RDLCK; |
| 193 | lock.l_whence = SEEK_SET; |
| 194 | lock.l_start = lock.l_len = 0L; |
| 195 | rc = fcntl(fd, F_SETLK, &lock); |
| 196 | return rc!=0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | /* |
| 200 | ** Unlock the database file. |
| 201 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 202 | static int pager_unlock(fd){ |
| 203 | int rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 204 | struct flock lock; |
| 205 | lock.l_type = F_UNLCK; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 206 | lock.l_whence = SEEK_SET; |
| 207 | lock.l_start = lock.l_len = 0L; |
| 208 | rc = fcntl(fd, F_SETLK, &lock); |
| 209 | return rc!=0; |
| 210 | } |
| 211 | |
| 212 | /* |
| 213 | ** Move the cursor for file descriptor fd to the point whereto from |
| 214 | ** the beginning of the file. |
| 215 | */ |
| 216 | static int pager_seek(int fd, off_t whereto){ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 217 | /*printf("SEEK to page %d\n", whereto/SQLITE_PAGE_SIZE + 1);*/ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 218 | lseek(fd, whereto, SEEK_SET); |
| 219 | return SQLITE_OK; |
| 220 | } |
| 221 | |
| 222 | /* |
| 223 | ** Truncate the given file so that it contains exactly mxPg pages |
| 224 | ** of data. |
| 225 | */ |
| 226 | static int pager_truncate(int fd, Pgno mxPg){ |
| 227 | int rc; |
| 228 | rc = ftruncate(fd, mxPg*SQLITE_PAGE_SIZE); |
| 229 | return rc!=0 ? SQLITE_IOERR : SQLITE_OK; |
| 230 | } |
| 231 | |
| 232 | /* |
| 233 | ** Read nBytes of data from fd into pBuf. If the data cannot be |
| 234 | ** read or only a partial read occurs, then the unread parts of |
| 235 | ** pBuf are filled with zeros and this routine returns SQLITE_IOERR. |
| 236 | ** If the read is completely successful, return SQLITE_OK. |
| 237 | */ |
| 238 | static int pager_read(int fd, void *pBuf, int nByte){ |
| 239 | int rc; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 240 | /* printf("READ\n");*/ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 241 | rc = read(fd, pBuf, nByte); |
| 242 | if( rc<0 ){ |
| 243 | memset(pBuf, 0, nByte); |
| 244 | return SQLITE_IOERR; |
| 245 | } |
| 246 | if( rc<nByte ){ |
| 247 | memset(&((char*)pBuf)[rc], 0, nByte - rc); |
| 248 | rc = SQLITE_IOERR; |
| 249 | }else{ |
| 250 | rc = SQLITE_OK; |
| 251 | } |
| 252 | return rc; |
| 253 | } |
| 254 | |
| 255 | /* |
| 256 | ** Write nBytes of data into fd. If any problem occurs or if the |
| 257 | ** write is incomplete, SQLITE_IOERR is returned. SQLITE_OK is |
| 258 | ** returned upon complete success. |
| 259 | */ |
| 260 | static int pager_write(int fd, const void *pBuf, int nByte){ |
| 261 | int rc; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 262 | /*printf("WRITE\n");*/ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 263 | rc = write(fd, pBuf, nByte); |
| 264 | if( rc<nByte ){ |
| 265 | return SQLITE_FULL; |
| 266 | }else{ |
| 267 | return SQLITE_OK; |
| 268 | } |
| 269 | } |
| 270 | |
| 271 | /* |
| 272 | ** Convert the bits in the pPager->errMask into an approprate |
| 273 | ** return code. |
| 274 | */ |
| 275 | static int pager_errcode(Pager *pPager){ |
| 276 | int rc = SQLITE_OK; |
| 277 | if( pPager->errMask & PAGER_ERR_LOCK ) rc = SQLITE_PROTOCOL; |
| 278 | if( pPager->errMask & PAGER_ERR_FULL ) rc = SQLITE_FULL; |
| 279 | if( pPager->errMask & PAGER_ERR_MEM ) rc = SQLITE_NOMEM; |
| 280 | if( pPager->errMask & PAGER_ERR_CORRUPT ) rc = SQLITE_CORRUPT; |
| 281 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 282 | } |
| 283 | |
| 284 | /* |
| 285 | ** Find a page in the hash table given its page number. Return |
| 286 | ** a pointer to the page or NULL if not found. |
| 287 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 288 | static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 289 | PgHdr *p = pPager->aHash[pgno % N_PG_HASH]; |
| 290 | while( p && p->pgno!=pgno ){ |
| 291 | p = p->pNextHash; |
| 292 | } |
| 293 | return p; |
| 294 | } |
| 295 | |
| 296 | /* |
| 297 | ** Unlock the database and clear the in-memory cache. This routine |
| 298 | ** sets the state of the pager back to what it was when it was first |
| 299 | ** opened. Any outstanding pages are invalidated and subsequent attempts |
| 300 | ** to access those pages will likely result in a coredump. |
| 301 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 302 | static void pager_reset(Pager *pPager){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 303 | PgHdr *pPg, *pNext; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 304 | for(pPg=pPager->pAll; pPg; pPg=pNext){ |
| 305 | pNext = pPg->pNextAll; |
| 306 | sqliteFree(pPg); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 307 | } |
| 308 | pPager->pFirst = 0; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 309 | pPager->pLast = 0; |
| 310 | pPager->pAll = 0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 311 | memset(pPager->aHash, 0, sizeof(pPager->aHash)); |
| 312 | pPager->nPage = 0; |
| 313 | if( pPager->state==SQLITE_WRITELOCK ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 314 | sqlitepager_rollback(pPager); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 315 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 316 | pager_unlock(pPager->fd); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 317 | pPager->state = SQLITE_UNLOCK; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 318 | pPager->dbSize = -1; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 319 | pPager->nRef = 0; |
| 320 | } |
| 321 | |
| 322 | /* |
| 323 | ** When this routine is called, the pager has the journal file open and |
| 324 | ** a write lock on the database. This routine releases the database |
| 325 | ** write lock and acquires a read lock in its place. The journal file |
| 326 | ** is deleted and closed. |
| 327 | ** |
| 328 | ** We have to release the write lock before acquiring the read lock, |
| 329 | ** so there is a race condition where another process can get the lock |
| 330 | ** while we are not holding it. But, no other process should do this |
| 331 | ** because we are also holding a lock on the journal, and no process |
| 332 | ** should get a write lock on the database without first getting a lock |
| 333 | ** on the journal. So this routine should never fail. But it can fail |
| 334 | ** if another process is not playing by the rules. If it does fail, |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 335 | ** all in-memory cache pages are invalidated, the PAGER_ERR_LOCK bit |
| 336 | ** is set in pPager->errMask, and this routine returns SQLITE_PROTOCOL. |
| 337 | ** SQLITE_OK is returned on success. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 338 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 339 | static int pager_unwritelock(Pager *pPager){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 340 | int rc; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 341 | PgHdr *pPg; |
| 342 | if( pPager->state!=SQLITE_WRITELOCK ) return SQLITE_OK; |
| 343 | pager_unlock(pPager->fd); |
| 344 | rc = pager_lock(pPager->fd, 0); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 345 | unlink(pPager->zJournal); |
| 346 | close(pPager->jfd); |
| 347 | pPager->jfd = -1; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 348 | sqliteFree( pPager->aInJournal ); |
| 349 | pPager->aInJournal = 0; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 350 | for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ |
| 351 | pPg->inJournal = 0; |
| 352 | pPg->dirty = 0; |
| 353 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 354 | if( rc!=SQLITE_OK ){ |
| 355 | pPager->state = SQLITE_UNLOCK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 356 | rc = SQLITE_PROTOCOL; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 357 | pPager->errMask |= PAGER_ERR_LOCK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 358 | }else{ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 359 | rc = SQLITE_OK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 360 | pPager->state = SQLITE_READLOCK; |
| 361 | } |
| 362 | return rc; |
| 363 | } |
| 364 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 365 | /* |
| 366 | ** Playback the journal and thus restore the database file to |
| 367 | ** the state it was in before we started making changes. |
| 368 | ** |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 369 | ** The journal file format is as follows: There is an initial |
| 370 | ** file-type string for sanity checking. Then there is a single |
| 371 | ** Pgno number which is the number of pages in the database before |
| 372 | ** changes were made. The database is truncated to this size. |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 373 | ** Next come zero or more page records where each page record |
| 374 | ** consists of a Pgno and SQLITE_PAGE_SIZE bytes of data. See |
| 375 | ** the PageRecord structure for details. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 376 | ** |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 377 | ** For playback, the pages have to be read from the journal in |
| 378 | ** reverse order and put back into the original database file. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 379 | ** |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 380 | ** If the file opened as the journal file is not a well-formed |
| 381 | ** journal file (as determined by looking at the magic number |
| 382 | ** at the beginning) then this routine returns SQLITE_PROTOCOL. |
| 383 | ** If any other errors occur during playback, the database will |
| 384 | ** likely be corrupted, so the PAGER_ERR_CORRUPT bit is set in |
| 385 | ** pPager->errMask and SQLITE_CORRUPT is returned. If it all |
| 386 | ** works, then this routine returns SQLITE_OK. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 387 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 388 | static int pager_playback(Pager *pPager){ |
| 389 | int nRec; /* Number of Records */ |
| 390 | int i; /* Loop counter */ |
| 391 | Pgno mxPg = 0; /* Size of the original file in pages */ |
| 392 | struct stat statbuf; /* Used to size the journal */ |
| 393 | PgHdr *pPg; /* An existing page in the cache */ |
| 394 | PageRecord pgRec; |
| 395 | unsigned char aMagic[sizeof(aJournalMagic)]; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 396 | int rc; |
| 397 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 398 | /* Read the beginning of the journal and truncate the |
| 399 | ** database file back to its original size. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 400 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 401 | assert( pPager->jfd>=0 ); |
| 402 | pager_seek(pPager->jfd, 0); |
| 403 | rc = pager_read(pPager->jfd, aMagic, sizeof(aMagic)); |
| 404 | if( rc!=SQLITE_OK || memcmp(aMagic,aJournalMagic,sizeof(aMagic))!=0 ){ |
| 405 | return SQLITE_PROTOCOL; |
| 406 | } |
| 407 | rc = pager_read(pPager->jfd, &mxPg, sizeof(mxPg)); |
| 408 | if( rc!=SQLITE_OK ){ |
| 409 | return SQLITE_PROTOCOL; |
| 410 | } |
| 411 | pager_truncate(pPager->fd, mxPg); |
| 412 | pPager->dbSize = mxPg; |
| 413 | |
| 414 | /* Begin reading the journal beginning at the end and moving |
| 415 | ** toward the beginning. |
| 416 | */ |
| 417 | if( fstat(pPager->jfd, &statbuf)!=0 ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 418 | return SQLITE_OK; |
| 419 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 420 | nRec = (statbuf.st_size - (sizeof(aMagic)+sizeof(Pgno))) / sizeof(PageRecord); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 421 | |
| 422 | /* Process segments beginning with the last and working backwards |
| 423 | ** to the first. |
| 424 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 425 | for(i=nRec-1; i>=0; i--){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 426 | /* Seek to the beginning of the segment */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 427 | off_t ofst; |
| 428 | ofst = i*sizeof(PageRecord) + sizeof(aMagic) + sizeof(Pgno); |
| 429 | rc = pager_seek(pPager->jfd, ofst); |
| 430 | if( rc!=SQLITE_OK ) break; |
| 431 | rc = pager_read(pPager->jfd, &pgRec, sizeof(pgRec)); |
| 432 | if( rc!=SQLITE_OK ) break; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 433 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 434 | /* Sanity checking on the page */ |
| 435 | if( pgRec.pgno>mxPg || pgRec.pgno==0 ){ |
| 436 | rc = SQLITE_CORRUPT; |
| 437 | break; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 438 | } |
| 439 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 440 | /* Playback the page. Update the in-memory copy of the page |
| 441 | ** at the same time, if there is one. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 442 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 443 | pPg = pager_lookup(pPager, pgRec.pgno); |
| 444 | if( pPg ){ |
| 445 | memcpy(PGHDR_TO_DATA(pPg), pgRec.aData, SQLITE_PAGE_SIZE); |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 446 | memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 447 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 448 | rc = pager_seek(pPager->fd, (pgRec.pgno-1)*SQLITE_PAGE_SIZE); |
| 449 | if( rc!=SQLITE_OK ) break; |
| 450 | rc = pager_write(pPager->fd, pgRec.aData, SQLITE_PAGE_SIZE); |
| 451 | if( rc!=SQLITE_OK ) break; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 452 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 453 | if( rc!=SQLITE_OK ){ |
| 454 | pager_unwritelock(pPager); |
| 455 | pPager->errMask |= PAGER_ERR_CORRUPT; |
| 456 | rc = SQLITE_CORRUPT; |
| 457 | }else{ |
| 458 | rc = pager_unwritelock(pPager); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 459 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 460 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 461 | } |
| 462 | |
| 463 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 464 | ** Locate a directory where we can potentially create a temporary |
| 465 | ** file. |
| 466 | */ |
| 467 | static const char *findTempDir(void){ |
| 468 | static const char *azDirs[] = { |
| 469 | ".", |
| 470 | "/var/tmp", |
| 471 | "/usr/tmp", |
| 472 | "/tmp", |
| 473 | "/temp", |
| 474 | "./temp", |
| 475 | }; |
| 476 | int i; |
| 477 | struct stat buf; |
| 478 | for(i=0; i<sizeof(azDirs)/sizeof(azDirs[0]); i++){ |
drh | 3fc190c | 2001-09-14 03:24:23 +0000 | [diff] [blame] | 479 | if( stat(azDirs[i], &buf) ) continue; |
| 480 | if( !S_ISDIR(buf.st_mode) ) continue; |
| 481 | if( access(azDirs[i], 07) ) continue; |
| 482 | return azDirs[i]; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 483 | } |
| 484 | return 0; |
| 485 | } |
| 486 | |
| 487 | /* |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 488 | ** Change the maximum number of in-memory pages that are allowed. |
| 489 | */ |
| 490 | void sqlitepager_set_cachesize(Pager *pPager, int mxPage){ |
| 491 | if( mxPage>10 ){ |
| 492 | pPager->mxPage = mxPage; |
| 493 | } |
| 494 | } |
| 495 | |
| 496 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 497 | ** Create a new page cache and put a pointer to the page cache in *ppPager. |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 498 | ** The file to be cached need not exist. The file is not locked until |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 499 | ** the first call to sqlitepager_get() and is only held open until the |
| 500 | ** last page is released using sqlitepager_unref(). |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 501 | */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 502 | int sqlitepager_open( |
| 503 | Pager **ppPager, /* Return the Pager structure here */ |
| 504 | const char *zFilename, /* Name of the database file to open */ |
| 505 | int mxPage, /* Max number of in-memory cache pages */ |
| 506 | int nExtra /* Extra bytes append to each in-memory page */ |
| 507 | ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 508 | Pager *pPager; |
| 509 | int nameLen; |
| 510 | int fd; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 511 | int tempFile; |
| 512 | int readOnly = 0; |
| 513 | char zTemp[300]; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 514 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 515 | *ppPager = 0; |
| 516 | if( sqlite_malloc_failed ){ |
| 517 | return SQLITE_NOMEM; |
| 518 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 519 | if( zFilename ){ |
| 520 | fd = open(zFilename, O_RDWR|O_CREAT, 0644); |
| 521 | if( fd<0 ){ |
| 522 | fd = open(zFilename, O_RDONLY, 0); |
| 523 | readOnly = 1; |
| 524 | } |
| 525 | tempFile = 0; |
| 526 | }else{ |
| 527 | int cnt = 8; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 528 | const char *zDir = findTempDir(); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 529 | if( zDir==0 ) return SQLITE_CANTOPEN; |
| 530 | do{ |
| 531 | cnt--; |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 532 | sprintf(zTemp,"%s/_sqlite_%u", zDir, (unsigned)sqliteRandomInteger()); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 533 | fd = open(zTemp, O_RDWR|O_CREAT|O_EXCL, 0600); |
| 534 | }while( cnt>0 && fd<0 ); |
| 535 | zFilename = zTemp; |
| 536 | tempFile = 1; |
| 537 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 538 | if( fd<0 ){ |
| 539 | return SQLITE_CANTOPEN; |
| 540 | } |
| 541 | nameLen = strlen(zFilename); |
| 542 | pPager = sqliteMalloc( sizeof(*pPager) + nameLen*2 + 30 ); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 543 | if( pPager==0 ){ |
| 544 | close(fd); |
| 545 | return SQLITE_NOMEM; |
| 546 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 547 | pPager->zFilename = (char*)&pPager[1]; |
| 548 | pPager->zJournal = &pPager->zFilename[nameLen+1]; |
| 549 | strcpy(pPager->zFilename, zFilename); |
| 550 | strcpy(pPager->zJournal, zFilename); |
| 551 | strcpy(&pPager->zJournal[nameLen], "-journal"); |
| 552 | pPager->fd = fd; |
| 553 | pPager->jfd = -1; |
| 554 | pPager->nRef = 0; |
| 555 | pPager->dbSize = -1; |
| 556 | pPager->nPage = 0; |
drh | d79caeb | 2001-04-15 02:27:24 +0000 | [diff] [blame] | 557 | pPager->mxPage = mxPage>5 ? mxPage : 10; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 558 | pPager->state = SQLITE_UNLOCK; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 559 | pPager->errMask = 0; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 560 | pPager->tempFile = tempFile; |
| 561 | pPager->readOnly = readOnly; |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 562 | pPager->needSync = 0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 563 | pPager->pFirst = 0; |
| 564 | pPager->pLast = 0; |
drh | 7c717f7 | 2001-06-24 20:39:41 +0000 | [diff] [blame] | 565 | pPager->nExtra = nExtra; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 566 | memset(pPager->aHash, 0, sizeof(pPager->aHash)); |
| 567 | *ppPager = pPager; |
| 568 | return SQLITE_OK; |
| 569 | } |
| 570 | |
| 571 | /* |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 572 | ** Set the destructor for this pager. If not NULL, the destructor is called |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 573 | ** when the reference count on each page reaches zero. The destructor can |
| 574 | ** be used to clean up information in the extra segment appended to each page. |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 575 | ** |
| 576 | ** The destructor is not called as a result sqlitepager_close(). |
| 577 | ** Destructors are only called by sqlitepager_unref(). |
| 578 | */ |
| 579 | void sqlitepager_set_destructor(Pager *pPager, void (*xDesc)(void*)){ |
| 580 | pPager->xDestructor = xDesc; |
| 581 | } |
| 582 | |
| 583 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 584 | ** Return the total number of pages in the disk file associated with |
| 585 | ** pPager. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 586 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 587 | int sqlitepager_pagecount(Pager *pPager){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 588 | int n; |
| 589 | struct stat statbuf; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 590 | assert( pPager!=0 ); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 591 | if( pPager->dbSize>=0 ){ |
| 592 | return pPager->dbSize; |
| 593 | } |
| 594 | if( fstat(pPager->fd, &statbuf)!=0 ){ |
| 595 | n = 0; |
| 596 | }else{ |
| 597 | n = statbuf.st_size/SQLITE_PAGE_SIZE; |
| 598 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 599 | if( pPager->state!=SQLITE_UNLOCK ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 600 | pPager->dbSize = n; |
| 601 | } |
| 602 | return n; |
| 603 | } |
| 604 | |
| 605 | /* |
| 606 | ** Shutdown the page cache. Free all memory and close all files. |
| 607 | ** |
| 608 | ** If a transaction was in progress when this routine is called, that |
| 609 | ** transaction is rolled back. All outstanding pages are invalidated |
| 610 | ** and their memory is freed. Any attempt to use a page associated |
| 611 | ** with this page cache after this function returns will likely |
| 612 | ** result in a coredump. |
| 613 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 614 | int sqlitepager_close(Pager *pPager){ |
| 615 | PgHdr *pPg, *pNext; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 616 | switch( pPager->state ){ |
| 617 | case SQLITE_WRITELOCK: { |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 618 | sqlitepager_rollback(pPager); |
| 619 | pager_unlock(pPager->fd); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 620 | break; |
| 621 | } |
| 622 | case SQLITE_READLOCK: { |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 623 | pager_unlock(pPager->fd); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 624 | break; |
| 625 | } |
| 626 | default: { |
| 627 | /* Do nothing */ |
| 628 | break; |
| 629 | } |
| 630 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 631 | for(pPg=pPager->pAll; pPg; pPg=pNext){ |
| 632 | pNext = pPg->pNextAll; |
| 633 | sqliteFree(pPg); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 634 | } |
| 635 | if( pPager->fd>=0 ) close(pPager->fd); |
| 636 | assert( pPager->jfd<0 ); |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 637 | if( pPager->tempFile ){ |
| 638 | unlink(pPager->zFilename); |
| 639 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 640 | sqliteFree(pPager); |
| 641 | return SQLITE_OK; |
| 642 | } |
| 643 | |
| 644 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 645 | ** Return the page number for the given page data. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 646 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 647 | Pgno sqlitepager_pagenumber(void *pData){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 648 | PgHdr *p = DATA_TO_PGHDR(pData); |
| 649 | return p->pgno; |
| 650 | } |
| 651 | |
| 652 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 653 | ** Increment the reference count for a page. If the page is |
| 654 | ** currently on the freelist (the reference count is zero) then |
| 655 | ** remove it from the freelist. |
| 656 | */ |
drh | df0b3b0 | 2001-06-23 11:36:20 +0000 | [diff] [blame] | 657 | static void page_ref(PgHdr *pPg){ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 658 | if( pPg->nRef==0 ){ |
| 659 | /* The page is currently on the freelist. Remove it. */ |
| 660 | if( pPg->pPrevFree ){ |
| 661 | pPg->pPrevFree->pNextFree = pPg->pNextFree; |
| 662 | }else{ |
| 663 | pPg->pPager->pFirst = pPg->pNextFree; |
| 664 | } |
| 665 | if( pPg->pNextFree ){ |
| 666 | pPg->pNextFree->pPrevFree = pPg->pPrevFree; |
| 667 | }else{ |
| 668 | pPg->pPager->pLast = pPg->pPrevFree; |
| 669 | } |
| 670 | pPg->pPager->nRef++; |
| 671 | } |
| 672 | pPg->nRef++; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 673 | REFINFO(pPg); |
drh | df0b3b0 | 2001-06-23 11:36:20 +0000 | [diff] [blame] | 674 | } |
| 675 | |
| 676 | /* |
| 677 | ** Increment the reference count for a page. The input pointer is |
| 678 | ** a reference to the page data. |
| 679 | */ |
| 680 | int sqlitepager_ref(void *pData){ |
| 681 | PgHdr *pPg = DATA_TO_PGHDR(pData); |
| 682 | page_ref(pPg); |
drh | 8c42ca9 | 2001-06-22 19:15:00 +0000 | [diff] [blame] | 683 | return SQLITE_OK; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 684 | } |
| 685 | |
| 686 | /* |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame^] | 687 | ** Sync the journal and write all free dirty pages to the database file. |
| 688 | */ |
| 689 | static int syncAllPages(Pager *pPager){ |
| 690 | PgHdr *pPg; |
| 691 | int rc = SQLITE_OK; |
| 692 | if( pPager->needSync ){ |
| 693 | rc = fsync(pPager->jfd); |
| 694 | if( rc!=0 ) return rc; |
| 695 | pPager->needSync = 0; |
| 696 | } |
| 697 | for(pPg=pPager->pFirst; pPg; pPg=pPg->pNextFree){ |
| 698 | if( pPg->dirty ){ |
| 699 | pager_seek(pPager->fd, (pPg->pgno-1)*SQLITE_PAGE_SIZE); |
| 700 | rc = pager_write(pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE); |
| 701 | if( rc!=SQLITE_OK ) break; |
| 702 | pPg->dirty = 0; |
| 703 | } |
| 704 | } |
| 705 | return SQLITE_OK; |
| 706 | } |
| 707 | |
| 708 | /* |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 709 | ** Acquire a page. |
| 710 | ** |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 711 | ** A read lock on the disk file is obtained when the first page acquired. |
| 712 | ** This read lock is dropped when the last page is released. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 713 | ** |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 714 | ** A _get works for any page number greater than 0. If the database |
| 715 | ** file is smaller than the requested page, then no actual disk |
| 716 | ** read occurs and the memory image of the page is initialized to |
| 717 | ** all zeros. The extra data appended to a page is always initialized |
| 718 | ** to zeros the first time a page is loaded into memory. |
| 719 | ** |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 720 | ** The acquisition might fail for several reasons. In all cases, |
| 721 | ** an appropriate error code is returned and *ppPage is set to NULL. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 722 | ** |
| 723 | ** See also sqlitepager_lookup(). Both this routine and _lookup() attempt |
| 724 | ** to find a page in the in-memory cache first. If the page is not already |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 725 | ** in memory, this routine goes to disk to read it in whereas _lookup() |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 726 | ** just returns 0. This routine acquires a read-lock the first time it |
| 727 | ** has to go to disk, and could also playback an old journal if necessary. |
| 728 | ** Since _lookup() never goes to disk, it never has to deal with locks |
| 729 | ** or journal files. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 730 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 731 | int sqlitepager_get(Pager *pPager, Pgno pgno, void **ppPage){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 732 | PgHdr *pPg; |
| 733 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 734 | /* Make sure we have not hit any critical errors. |
| 735 | */ |
| 736 | if( pPager==0 || pgno==0 ){ |
| 737 | return SQLITE_ERROR; |
| 738 | } |
| 739 | if( pPager->errMask & ~(PAGER_ERR_FULL) ){ |
| 740 | return pager_errcode(pPager); |
| 741 | } |
| 742 | |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 743 | /* If this is the first page accessed, then get a read lock |
| 744 | ** on the database file. |
| 745 | */ |
| 746 | if( pPager->nRef==0 ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 747 | if( pager_lock(pPager->fd, 0)!=0 ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 748 | *ppPage = 0; |
| 749 | return SQLITE_BUSY; |
| 750 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 751 | pPager->state = SQLITE_READLOCK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 752 | |
| 753 | /* If a journal file exists, try to play it back. |
| 754 | */ |
| 755 | if( access(pPager->zJournal,0)==0 ){ |
| 756 | int rc; |
| 757 | |
| 758 | /* Open the journal for exclusive access. Return SQLITE_BUSY if |
| 759 | ** we cannot get exclusive access to the journal file |
| 760 | */ |
| 761 | pPager->jfd = open(pPager->zJournal, O_RDONLY, 0); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 762 | if( pPager->jfd<0 || pager_lock(pPager->jfd, 1)!=0 ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 763 | if( pPager->jfd>=0 ){ close(pPager->jfd); pPager->jfd = -1; } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 764 | pager_unlock(pPager->fd); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 765 | *ppPage = 0; |
| 766 | return SQLITE_BUSY; |
| 767 | } |
| 768 | |
| 769 | /* Get a write lock on the database */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 770 | pager_unlock(pPager->fd); |
| 771 | if( pager_lock(pPager->fd, 1)!=0 ){ |
| 772 | close(pPager->jfd); |
| 773 | pPager->jfd = -1; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 774 | *ppPage = 0; |
| 775 | return SQLITE_PROTOCOL; |
| 776 | } |
| 777 | |
| 778 | /* Playback and delete the journal. Drop the database write |
| 779 | ** lock and reacquire the read lock. |
| 780 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 781 | rc = pager_playback(pPager); |
| 782 | if( rc!=SQLITE_OK ){ |
| 783 | return rc; |
| 784 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 785 | } |
| 786 | pPg = 0; |
| 787 | }else{ |
| 788 | /* Search for page in cache */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 789 | pPg = pager_lookup(pPager, pgno); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 790 | } |
| 791 | if( pPg==0 ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 792 | /* The requested page is not in the page cache. */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 793 | int h; |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 794 | pPager->nMiss++; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 795 | if( pPager->nPage<pPager->mxPage || pPager->pFirst==0 ){ |
| 796 | /* Create a new page */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 797 | pPg = sqliteMalloc( sizeof(*pPg) + SQLITE_PAGE_SIZE + pPager->nExtra ); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 798 | if( pPg==0 ){ |
| 799 | *ppPage = 0; |
| 800 | pager_unwritelock(pPager); |
| 801 | pPager->errMask |= PAGER_ERR_MEM; |
| 802 | return SQLITE_NOMEM; |
| 803 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 804 | pPg->pPager = pPager; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 805 | pPg->pNextAll = pPager->pAll; |
| 806 | if( pPager->pAll ){ |
| 807 | pPager->pAll->pPrevAll = pPg; |
| 808 | } |
| 809 | pPg->pPrevAll = 0; |
drh | d79caeb | 2001-04-15 02:27:24 +0000 | [diff] [blame] | 810 | pPager->pAll = pPg; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 811 | pPager->nPage++; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 812 | }else{ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 813 | /* Recycle an older page. First locate the page to be recycled. |
| 814 | ** Try to find one that is not dirty and is near the head of |
| 815 | ** of the free list */ |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame^] | 816 | int cnt = pPager->mxPage/2; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 817 | pPg = pPager->pFirst; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 818 | while( pPg->dirty && 0<cnt-- && pPg->pNextFree ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 819 | pPg = pPg->pNextFree; |
| 820 | } |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame^] | 821 | if( pPg==0 || pPg->dirty ){ |
| 822 | int rc = syncAllPages(pPager); |
| 823 | if( rc!=0 ){ |
| 824 | sqlitepager_rollback(pPager); |
| 825 | *ppPage = 0; |
| 826 | return SQLITE_IOERR; |
| 827 | } |
| 828 | pPg = pPager->pFirst; |
| 829 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 830 | assert( pPg->nRef==0 ); |
| 831 | |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame^] | 832 | |
| 833 | #if 0 |
| 834 | /**** Since putting in the call to syncAllPages() above, this code |
| 835 | ** is no longer used. I've kept it here for historical reference |
| 836 | ** only. |
| 837 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 838 | /* If the page to be recycled is dirty, sync the journal and write |
| 839 | ** the old page into the database. */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 840 | if( pPg->dirty ){ |
| 841 | int rc; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 842 | assert( pPg->inJournal==1 ); |
| 843 | assert( pPager->state==SQLITE_WRITELOCK ); |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 844 | if( pPager->needSync ){ |
| 845 | rc = fsync(pPager->jfd); |
| 846 | if( rc!=0 ){ |
| 847 | rc = sqlitepager_rollback(pPager); |
| 848 | *ppPage = 0; |
| 849 | if( rc==SQLITE_OK ) rc = SQLITE_IOERR; |
| 850 | return rc; |
| 851 | } |
| 852 | pPager->needSync = 0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 853 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 854 | pager_seek(pPager->fd, (pPg->pgno-1)*SQLITE_PAGE_SIZE); |
| 855 | rc = pager_write(pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE); |
| 856 | if( rc!=SQLITE_OK ){ |
| 857 | rc = sqlitepager_rollback(pPager); |
| 858 | *ppPage = 0; |
| 859 | if( rc==SQLITE_OK ) rc = SQLITE_FULL; |
| 860 | return rc; |
| 861 | } |
| 862 | } |
drh | 50e5dad | 2001-09-15 00:57:28 +0000 | [diff] [blame^] | 863 | #endif |
| 864 | assert( pPg->dirty==0 ); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 865 | |
| 866 | /* Unlink the old page from the free list and the hash table |
| 867 | */ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 868 | if( pPg->pPrevFree ){ |
| 869 | pPg->pPrevFree->pNextFree = pPg->pNextFree; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 870 | }else{ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 871 | assert( pPager->pFirst==pPg ); |
| 872 | pPager->pFirst = pPg->pNextFree; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 873 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 874 | if( pPg->pNextFree ){ |
| 875 | pPg->pNextFree->pPrevFree = pPg->pPrevFree; |
| 876 | }else{ |
| 877 | assert( pPager->pLast==pPg ); |
| 878 | pPager->pLast = pPg->pPrevFree; |
| 879 | } |
| 880 | pPg->pNextFree = pPg->pPrevFree = 0; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 881 | if( pPg->pNextHash ){ |
| 882 | pPg->pNextHash->pPrevHash = pPg->pPrevHash; |
| 883 | } |
| 884 | if( pPg->pPrevHash ){ |
| 885 | pPg->pPrevHash->pNextHash = pPg->pNextHash; |
| 886 | }else{ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 887 | h = pager_hash(pPg->pgno); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 888 | assert( pPager->aHash[h]==pPg ); |
| 889 | pPager->aHash[h] = pPg->pNextHash; |
| 890 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 891 | pPg->pNextHash = pPg->pPrevHash = 0; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 892 | pPager->nOvfl++; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 893 | } |
| 894 | pPg->pgno = pgno; |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 895 | if( pPager->aInJournal && pgno<=pPager->origDbSize ){ |
| 896 | pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0; |
| 897 | }else{ |
| 898 | pPg->inJournal = 0; |
| 899 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 900 | pPg->dirty = 0; |
| 901 | pPg->nRef = 1; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 902 | REFINFO(pPg); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 903 | pPager->nRef++; |
| 904 | h = pager_hash(pgno); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 905 | pPg->pNextHash = pPager->aHash[h]; |
| 906 | pPager->aHash[h] = pPg; |
| 907 | if( pPg->pNextHash ){ |
| 908 | assert( pPg->pNextHash->pPrevHash==0 ); |
| 909 | pPg->pNextHash->pPrevHash = pPg; |
| 910 | } |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 911 | if( pPager->dbSize<0 ) sqlitepager_pagecount(pPager); |
| 912 | if( pPager->dbSize<pgno ){ |
| 913 | memset(PGHDR_TO_DATA(pPg), 0, SQLITE_PAGE_SIZE); |
| 914 | }else{ |
| 915 | pager_seek(pPager->fd, (pgno-1)*SQLITE_PAGE_SIZE); |
| 916 | pager_read(pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE); |
| 917 | } |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 918 | if( pPager->nExtra>0 ){ |
| 919 | memset(PGHDR_TO_EXTRA(pPg), 0, pPager->nExtra); |
| 920 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 921 | }else{ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 922 | /* The requested page is in the page cache. */ |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 923 | pPager->nHit++; |
drh | df0b3b0 | 2001-06-23 11:36:20 +0000 | [diff] [blame] | 924 | page_ref(pPg); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 925 | } |
| 926 | *ppPage = PGHDR_TO_DATA(pPg); |
| 927 | return SQLITE_OK; |
| 928 | } |
| 929 | |
| 930 | /* |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 931 | ** Acquire a page if it is already in the in-memory cache. Do |
| 932 | ** not read the page from disk. Return a pointer to the page, |
| 933 | ** or 0 if the page is not in cache. |
| 934 | ** |
| 935 | ** See also sqlitepager_get(). The difference between this routine |
| 936 | ** and sqlitepager_get() is that _get() will go to the disk and read |
| 937 | ** in the page if the page is not already in cache. This routine |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 938 | ** returns NULL if the page is not in cache or if a disk I/O error |
| 939 | ** has ever happened. |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 940 | */ |
| 941 | void *sqlitepager_lookup(Pager *pPager, Pgno pgno){ |
| 942 | PgHdr *pPg; |
| 943 | |
| 944 | /* Make sure we have not hit any critical errors. |
| 945 | */ |
| 946 | if( pPager==0 || pgno==0 ){ |
| 947 | return 0; |
| 948 | } |
| 949 | if( pPager->errMask & ~(PAGER_ERR_FULL) ){ |
| 950 | return 0; |
| 951 | } |
| 952 | if( pPager->nRef==0 ){ |
| 953 | return 0; |
| 954 | } |
| 955 | pPg = pager_lookup(pPager, pgno); |
| 956 | if( pPg==0 ) return 0; |
drh | df0b3b0 | 2001-06-23 11:36:20 +0000 | [diff] [blame] | 957 | page_ref(pPg); |
drh | 7e3b0a0 | 2001-04-28 16:52:40 +0000 | [diff] [blame] | 958 | return PGHDR_TO_DATA(pPg); |
| 959 | } |
| 960 | |
| 961 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 962 | ** Release a page. |
| 963 | ** |
| 964 | ** If the number of references to the page drop to zero, then the |
| 965 | ** page is added to the LRU list. When all references to all pages |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 966 | ** are released, a rollback occurs and the lock on the database is |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 967 | ** removed. |
| 968 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 969 | int sqlitepager_unref(void *pData){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 970 | Pager *pPager; |
| 971 | PgHdr *pPg; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 972 | |
| 973 | /* Decrement the reference count for this page |
| 974 | */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 975 | pPg = DATA_TO_PGHDR(pData); |
| 976 | assert( pPg->nRef>0 ); |
| 977 | pPager = pPg->pPager; |
| 978 | pPg->nRef--; |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 979 | REFINFO(pPg); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 980 | |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 981 | /* When the number of references to a page reach 0, call the |
| 982 | ** destructor and add the page to the freelist. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 983 | */ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 984 | if( pPg->nRef==0 ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 985 | pPg->pNextFree = 0; |
| 986 | pPg->pPrevFree = pPager->pLast; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 987 | pPager->pLast = pPg; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 988 | if( pPg->pPrevFree ){ |
| 989 | pPg->pPrevFree->pNextFree = pPg; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 990 | }else{ |
| 991 | pPager->pFirst = pPg; |
| 992 | } |
drh | 72f8286 | 2001-05-24 21:06:34 +0000 | [diff] [blame] | 993 | if( pPager->xDestructor ){ |
| 994 | pPager->xDestructor(pData); |
| 995 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 996 | |
| 997 | /* When all pages reach the freelist, drop the read lock from |
| 998 | ** the database file. |
| 999 | */ |
| 1000 | pPager->nRef--; |
| 1001 | assert( pPager->nRef>=0 ); |
| 1002 | if( pPager->nRef==0 ){ |
| 1003 | pager_reset(pPager); |
| 1004 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1005 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1006 | return SQLITE_OK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1007 | } |
| 1008 | |
| 1009 | /* |
| 1010 | ** Mark a data page as writeable. The page is written into the journal |
| 1011 | ** if it is not there already. This routine must be called before making |
| 1012 | ** changes to a page. |
| 1013 | ** |
| 1014 | ** The first time this routine is called, the pager creates a new |
| 1015 | ** journal and acquires a write lock on the database. If the write |
| 1016 | ** lock could not be acquired, this routine returns SQLITE_BUSY. The |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1017 | ** calling routine must check for that return value and be careful not to |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1018 | ** change any page data until this routine returns SQLITE_OK. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1019 | ** |
| 1020 | ** If the journal file could not be written because the disk is full, |
| 1021 | ** then this routine returns SQLITE_FULL and does an immediate rollback. |
| 1022 | ** All subsequent write attempts also return SQLITE_FULL until there |
| 1023 | ** is a call to sqlitepager_commit() or sqlitepager_rollback() to |
| 1024 | ** reset. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1025 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1026 | int sqlitepager_write(void *pData){ |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 1027 | PgHdr *pPg = DATA_TO_PGHDR(pData); |
| 1028 | Pager *pPager = pPg->pPager; |
drh | d79caeb | 2001-04-15 02:27:24 +0000 | [diff] [blame] | 1029 | int rc = SQLITE_OK; |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 1030 | |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1031 | if( pPager->errMask ){ |
| 1032 | return pager_errcode(pPager); |
| 1033 | } |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1034 | if( pPager->readOnly ){ |
| 1035 | return SQLITE_PERM; |
| 1036 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1037 | pPg->dirty = 1; |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 1038 | if( pPg->inJournal ){ return SQLITE_OK; } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1039 | assert( pPager->state!=SQLITE_UNLOCK ); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1040 | if( pPager->state==SQLITE_READLOCK ){ |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1041 | assert( pPager->aInJournal==0 ); |
| 1042 | pPager->aInJournal = sqliteMalloc( pPager->dbSize/8 + 1 ); |
| 1043 | if( pPager->aInJournal==0 ){ |
| 1044 | return SQLITE_NOMEM; |
| 1045 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1046 | pPager->jfd = open(pPager->zJournal, O_RDWR|O_CREAT, 0644); |
| 1047 | if( pPager->jfd<0 ){ |
| 1048 | return SQLITE_CANTOPEN; |
| 1049 | } |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1050 | pPager->needSync = 0; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1051 | if( pager_lock(pPager->jfd, 1) ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1052 | close(pPager->jfd); |
| 1053 | pPager->jfd = -1; |
| 1054 | return SQLITE_BUSY; |
| 1055 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1056 | pager_unlock(pPager->fd); |
| 1057 | if( pager_lock(pPager->fd, 1) ){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1058 | close(pPager->jfd); |
| 1059 | pPager->jfd = -1; |
| 1060 | pPager->state = SQLITE_UNLOCK; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1061 | pPager->errMask |= PAGER_ERR_LOCK; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1062 | return SQLITE_PROTOCOL; |
| 1063 | } |
| 1064 | pPager->state = SQLITE_WRITELOCK; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1065 | sqlitepager_pagecount(pPager); |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 1066 | pPager->origDbSize = pPager->dbSize; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1067 | rc = pager_write(pPager->jfd, aJournalMagic, sizeof(aJournalMagic)); |
| 1068 | if( rc==SQLITE_OK ){ |
| 1069 | rc = pager_write(pPager->jfd, &pPager->dbSize, sizeof(Pgno)); |
| 1070 | } |
| 1071 | if( rc!=SQLITE_OK ){ |
| 1072 | rc = pager_unwritelock(pPager); |
| 1073 | if( rc==SQLITE_OK ) rc = SQLITE_FULL; |
| 1074 | return rc; |
| 1075 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1076 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1077 | assert( pPager->state==SQLITE_WRITELOCK ); |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 1078 | assert( pPager->jfd>=0 ); |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1079 | if( pPg->pgno <= pPager->origDbSize ){ |
| 1080 | rc = pager_write(pPager->jfd, &pPg->pgno, sizeof(Pgno)); |
| 1081 | if( rc==SQLITE_OK ){ |
| 1082 | rc = pager_write(pPager->jfd, pData, SQLITE_PAGE_SIZE); |
| 1083 | } |
| 1084 | if( rc!=SQLITE_OK ){ |
| 1085 | sqlitepager_rollback(pPager); |
| 1086 | pPager->errMask |= PAGER_ERR_FULL; |
| 1087 | return rc; |
| 1088 | } |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1089 | assert( pPager->aInJournal!=0 ); |
| 1090 | pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7); |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1091 | pPager->needSync = 1; |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 1092 | } |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 1093 | pPg->inJournal = 1; |
drh | 306dc21 | 2001-05-21 13:45:10 +0000 | [diff] [blame] | 1094 | if( pPager->dbSize<pPg->pgno ){ |
| 1095 | pPager->dbSize = pPg->pgno; |
| 1096 | } |
drh | 69688d5 | 2001-04-14 16:38:23 +0000 | [diff] [blame] | 1097 | return rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1098 | } |
| 1099 | |
| 1100 | /* |
drh | 6019e16 | 2001-07-02 17:51:45 +0000 | [diff] [blame] | 1101 | ** Return TRUE if the page given in the argument was previous passed |
| 1102 | ** to sqlitepager_write(). In other words, return TRUE if it is ok |
| 1103 | ** to change the content of the page. |
| 1104 | */ |
| 1105 | int sqlitepager_iswriteable(void *pData){ |
| 1106 | PgHdr *pPg = DATA_TO_PGHDR(pData); |
| 1107 | return pPg->dirty; |
| 1108 | } |
| 1109 | |
| 1110 | /* |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1111 | ** Commit all changes to the database and release the write lock. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1112 | ** |
| 1113 | ** If the commit fails for any reason, a rollback attempt is made |
| 1114 | ** and an error code is returned. If the commit worked, SQLITE_OK |
| 1115 | ** is returned. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1116 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1117 | int sqlitepager_commit(Pager *pPager){ |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 1118 | int rc; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1119 | PgHdr *pPg; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1120 | |
| 1121 | if( pPager->errMask==PAGER_ERR_FULL ){ |
| 1122 | rc = sqlitepager_rollback(pPager); |
| 1123 | if( rc==SQLITE_OK ) rc = SQLITE_FULL; |
| 1124 | return rc; |
| 1125 | } |
| 1126 | if( pPager->errMask!=0 ){ |
| 1127 | rc = pager_errcode(pPager); |
| 1128 | return rc; |
| 1129 | } |
| 1130 | if( pPager->state!=SQLITE_WRITELOCK ){ |
| 1131 | return SQLITE_ERROR; |
| 1132 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1133 | assert( pPager->jfd>=0 ); |
drh | f57b14a | 2001-09-14 18:54:08 +0000 | [diff] [blame] | 1134 | if( pPager->needSync && fsync(pPager->jfd) ){ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1135 | goto commit_abort; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1136 | } |
drh | a1b351a | 2001-09-14 16:42:12 +0000 | [diff] [blame] | 1137 | for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ |
| 1138 | if( pPg->dirty==0 ) continue; |
| 1139 | rc = pager_seek(pPager->fd, (pPg->pgno-1)*SQLITE_PAGE_SIZE); |
| 1140 | if( rc!=SQLITE_OK ) goto commit_abort; |
| 1141 | rc = pager_write(pPager->fd, PGHDR_TO_DATA(pPg), SQLITE_PAGE_SIZE); |
| 1142 | if( rc!=SQLITE_OK ) goto commit_abort; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1143 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1144 | if( fsync(pPager->fd) ) goto commit_abort; |
| 1145 | rc = pager_unwritelock(pPager); |
| 1146 | pPager->dbSize = -1; |
| 1147 | return rc; |
| 1148 | |
| 1149 | /* Jump here if anything goes wrong during the commit process. |
| 1150 | */ |
| 1151 | commit_abort: |
| 1152 | rc = sqlitepager_rollback(pPager); |
| 1153 | if( rc==SQLITE_OK ){ |
| 1154 | rc = SQLITE_FULL; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1155 | } |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1156 | return rc; |
| 1157 | } |
| 1158 | |
| 1159 | /* |
| 1160 | ** Rollback all changes. The database falls back to read-only mode. |
| 1161 | ** All in-memory cache pages revert to their original data contents. |
| 1162 | ** The journal is deleted. |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1163 | ** |
| 1164 | ** This routine cannot fail unless some other process is not following |
| 1165 | ** the correct locking protocol (SQLITE_PROTOCOL) or unless some other |
| 1166 | ** process is writing trash into the journal file (SQLITE_CORRUPT) or |
| 1167 | ** unless a prior malloc() failed (SQLITE_NOMEM). Appropriate error |
| 1168 | ** codes are returned for all these occasions. Otherwise, |
| 1169 | ** SQLITE_OK is returned. |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1170 | */ |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1171 | int sqlitepager_rollback(Pager *pPager){ |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1172 | int rc; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1173 | if( pPager->errMask!=0 && pPager->errMask!=PAGER_ERR_FULL ){ |
| 1174 | return pager_errcode(pPager); |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1175 | } |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1176 | if( pPager->state!=SQLITE_WRITELOCK ){ |
| 1177 | return SQLITE_OK; |
| 1178 | } |
| 1179 | rc = pager_playback(pPager); |
| 1180 | if( rc!=SQLITE_OK ){ |
| 1181 | rc = SQLITE_CORRUPT; |
| 1182 | pPager->errMask |= PAGER_ERR_CORRUPT; |
| 1183 | } |
| 1184 | pPager->dbSize = -1; |
drh | ed7c855 | 2001-04-11 14:29:21 +0000 | [diff] [blame] | 1185 | return rc; |
| 1186 | }; |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1187 | |
| 1188 | /* |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1189 | ** Return TRUE if the database file is opened read-only. Return FALSE |
| 1190 | ** if the database is (in theory) writable. |
| 1191 | */ |
| 1192 | int sqlitepager_isreadonly(Pager *pPager){ |
drh | be0072d | 2001-09-13 14:46:09 +0000 | [diff] [blame] | 1193 | return pPager->readOnly; |
drh | 5e00f6c | 2001-09-13 13:46:56 +0000 | [diff] [blame] | 1194 | } |
| 1195 | |
| 1196 | /* |
drh | d9b0257 | 2001-04-15 00:37:09 +0000 | [diff] [blame] | 1197 | ** This routine is used for testing and analysis only. |
| 1198 | */ |
| 1199 | int *sqlitepager_stats(Pager *pPager){ |
| 1200 | static int a[9]; |
| 1201 | a[0] = pPager->nRef; |
| 1202 | a[1] = pPager->nPage; |
| 1203 | a[2] = pPager->mxPage; |
| 1204 | a[3] = pPager->dbSize; |
| 1205 | a[4] = pPager->state; |
| 1206 | a[5] = pPager->errMask; |
| 1207 | a[6] = pPager->nHit; |
| 1208 | a[7] = pPager->nMiss; |
| 1209 | a[8] = pPager->nOvfl; |
| 1210 | return a; |
| 1211 | } |
drh | dd79342 | 2001-06-28 01:54:48 +0000 | [diff] [blame] | 1212 | |
| 1213 | #if SQLITE_TEST |
| 1214 | /* |
| 1215 | ** Print a listing of all referenced pages and their ref count. |
| 1216 | */ |
| 1217 | void sqlitepager_refdump(Pager *pPager){ |
| 1218 | PgHdr *pPg; |
| 1219 | for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){ |
| 1220 | if( pPg->nRef<=0 ) continue; |
| 1221 | printf("PAGE %3d addr=0x%08x nRef=%d\n", |
| 1222 | pPg->pgno, (int)PGHDR_TO_DATA(pPg), pPg->nRef); |
| 1223 | } |
| 1224 | } |
| 1225 | #endif |