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