1 /* 2 ** 2004 April 6 3 ** 4 ** The author disclaims copyright to this source code. In place of 5 ** a legal notice, here is a blessing: 6 ** 7 ** May you do good and not evil. 8 ** May you find forgiveness for yourself and forgive others. 9 ** May you share freely, never taking more than you give. 10 ** 11 ************************************************************************* 12 ** This file implements an external (disk-based) database using BTrees. 13 ** See the header comment on "btreeInt.h" for additional information. 14 ** Including a description of file format and an overview of operation. 15 */ 16 #include "btreeInt.h" 17 18 /* 19 ** The header string that appears at the beginning of every 20 ** SQLite database. 21 */ 22 static const char zMagicHeader[] = SQLITE_FILE_HEADER; 23 24 /* 25 ** Set this global variable to 1 to enable tracing using the TRACE 26 ** macro. 27 */ 28 #if 0 29 int sqlite3BtreeTrace=1; /* True to enable tracing */ 30 # define TRACE(X) if(sqlite3BtreeTrace){printf X;fflush(stdout);} 31 #else 32 # define TRACE(X) 33 #endif 34 35 /* 36 ** Extract a 2-byte big-endian integer from an array of unsigned bytes. 37 ** But if the value is zero, make it 65536. 38 ** 39 ** This routine is used to extract the "offset to cell content area" value 40 ** from the header of a btree page. If the page size is 65536 and the page 41 ** is empty, the offset should be 65536, but the 2-byte value stores zero. 42 ** This routine makes the necessary adjustment to 65536. 43 */ 44 #define get2byteNotZero(X) (((((int)get2byte(X))-1)&0xffff)+1) 45 46 /* 47 ** Values passed as the 5th argument to allocateBtreePage() 48 */ 49 #define BTALLOC_ANY 0 /* Allocate any page */ 50 #define BTALLOC_EXACT 1 /* Allocate exact page if possible */ 51 #define BTALLOC_LE 2 /* Allocate any page <= the parameter */ 52 53 /* 54 ** Macro IfNotOmitAV(x) returns (x) if SQLITE_OMIT_AUTOVACUUM is not 55 ** defined, or 0 if it is. For example: 56 ** 57 ** bIncrVacuum = IfNotOmitAV(pBtShared->incrVacuum); 58 */ 59 #ifndef SQLITE_OMIT_AUTOVACUUM 60 #define IfNotOmitAV(expr) (expr) 61 #else 62 #define IfNotOmitAV(expr) 0 63 #endif 64 65 #ifndef SQLITE_OMIT_SHARED_CACHE 66 /* 67 ** A list of BtShared objects that are eligible for participation 68 ** in shared cache. This variable has file scope during normal builds, 69 ** but the test harness needs to access it so we make it global for 70 ** test builds. 71 ** 72 ** Access to this variable is protected by SQLITE_MUTEX_STATIC_MASTER. 73 */ 74 #ifdef SQLITE_TEST 75 BtShared *SQLITE_WSD sqlite3SharedCacheList = 0; 76 #else 77 static BtShared *SQLITE_WSD sqlite3SharedCacheList = 0; 78 #endif 79 #endif /* SQLITE_OMIT_SHARED_CACHE */ 80 81 #ifndef SQLITE_OMIT_SHARED_CACHE 82 /* 83 ** Enable or disable the shared pager and schema features. 84 ** 85 ** This routine has no effect on existing database connections. 86 ** The shared cache setting effects only future calls to 87 ** sqlite3_open(), sqlite3_open16(), or sqlite3_open_v2(). 88 */ 89 int sqlite3_enable_shared_cache(int enable){ 90 sqlite3GlobalConfig.sharedCacheEnabled = enable; 91 return SQLITE_OK; 92 } 93 #endif 94 95 96 97 #ifdef SQLITE_OMIT_SHARED_CACHE 98 /* 99 ** The functions querySharedCacheTableLock(), setSharedCacheTableLock(), 100 ** and clearAllSharedCacheTableLocks() 101 ** manipulate entries in the BtShared.pLock linked list used to store 102 ** shared-cache table level locks. If the library is compiled with the 103 ** shared-cache feature disabled, then there is only ever one user 104 ** of each BtShared structure and so this locking is not necessary. 105 ** So define the lock related functions as no-ops. 106 */ 107 #define querySharedCacheTableLock(a,b,c) SQLITE_OK 108 #define setSharedCacheTableLock(a,b,c) SQLITE_OK 109 #define clearAllSharedCacheTableLocks(a) 110 #define downgradeAllSharedCacheTableLocks(a) 111 #define hasSharedCacheTableLock(a,b,c,d) 1 112 #define hasReadConflicts(a, b) 0 113 #endif 114 115 /* 116 ** Implementation of the SQLITE_CORRUPT_PAGE() macro. Takes a single 117 ** (MemPage*) as an argument. The (MemPage*) must not be NULL. 118 ** 119 ** If SQLITE_DEBUG is not defined, then this macro is equivalent to 120 ** SQLITE_CORRUPT_BKPT. Or, if SQLITE_DEBUG is set, then the log message 121 ** normally produced as a side-effect of SQLITE_CORRUPT_BKPT is augmented 122 ** with the page number and filename associated with the (MemPage*). 123 */ 124 #ifdef SQLITE_DEBUG 125 int corruptPageError(int lineno, MemPage *p){ 126 char *zMsg; 127 sqlite3BeginBenignMalloc(); 128 zMsg = sqlite3_mprintf("database corruption page %d of %s", 129 (int)p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0) 130 ); 131 sqlite3EndBenignMalloc(); 132 if( zMsg ){ 133 sqlite3ReportError(SQLITE_CORRUPT, lineno, zMsg); 134 } 135 sqlite3_free(zMsg); 136 return SQLITE_CORRUPT_BKPT; 137 } 138 # define SQLITE_CORRUPT_PAGE(pMemPage) corruptPageError(__LINE__, pMemPage) 139 #else 140 # define SQLITE_CORRUPT_PAGE(pMemPage) SQLITE_CORRUPT_PGNO(pMemPage->pgno) 141 #endif 142 143 #ifndef SQLITE_OMIT_SHARED_CACHE 144 145 #ifdef SQLITE_DEBUG 146 /* 147 **** This function is only used as part of an assert() statement. *** 148 ** 149 ** Check to see if pBtree holds the required locks to read or write to the 150 ** table with root page iRoot. Return 1 if it does and 0 if not. 151 ** 152 ** For example, when writing to a table with root-page iRoot via 153 ** Btree connection pBtree: 154 ** 155 ** assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) ); 156 ** 157 ** When writing to an index that resides in a sharable database, the 158 ** caller should have first obtained a lock specifying the root page of 159 ** the corresponding table. This makes things a bit more complicated, 160 ** as this module treats each table as a separate structure. To determine 161 ** the table corresponding to the index being written, this 162 ** function has to search through the database schema. 163 ** 164 ** Instead of a lock on the table/index rooted at page iRoot, the caller may 165 ** hold a write-lock on the schema table (root page 1). This is also 166 ** acceptable. 167 */ 168 static int hasSharedCacheTableLock( 169 Btree *pBtree, /* Handle that must hold lock */ 170 Pgno iRoot, /* Root page of b-tree */ 171 int isIndex, /* True if iRoot is the root of an index b-tree */ 172 int eLockType /* Required lock type (READ_LOCK or WRITE_LOCK) */ 173 ){ 174 Schema *pSchema = (Schema *)pBtree->pBt->pSchema; 175 Pgno iTab = 0; 176 BtLock *pLock; 177 178 /* If this database is not shareable, or if the client is reading 179 ** and has the read-uncommitted flag set, then no lock is required. 180 ** Return true immediately. 181 */ 182 if( (pBtree->sharable==0) 183 || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommit)) 184 ){ 185 return 1; 186 } 187 188 /* If the client is reading or writing an index and the schema is 189 ** not loaded, then it is too difficult to actually check to see if 190 ** the correct locks are held. So do not bother - just return true. 191 ** This case does not come up very often anyhow. 192 */ 193 if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded)==0) ){ 194 return 1; 195 } 196 197 /* Figure out the root-page that the lock should be held on. For table 198 ** b-trees, this is just the root page of the b-tree being read or 199 ** written. For index b-trees, it is the root page of the associated 200 ** table. */ 201 if( isIndex ){ 202 HashElem *p; 203 for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){ 204 Index *pIdx = (Index *)sqliteHashData(p); 205 if( pIdx->tnum==(int)iRoot ){ 206 if( iTab ){ 207 /* Two or more indexes share the same root page. There must 208 ** be imposter tables. So just return true. The assert is not 209 ** useful in that case. */ 210 return 1; 211 } 212 iTab = pIdx->pTable->tnum; 213 } 214 } 215 }else{ 216 iTab = iRoot; 217 } 218 219 /* Search for the required lock. Either a write-lock on root-page iTab, a 220 ** write-lock on the schema table, or (if the client is reading) a 221 ** read-lock on iTab will suffice. Return 1 if any of these are found. */ 222 for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){ 223 if( pLock->pBtree==pBtree 224 && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1)) 225 && pLock->eLock>=eLockType 226 ){ 227 return 1; 228 } 229 } 230 231 /* Failed to find the required lock. */ 232 return 0; 233 } 234 #endif /* SQLITE_DEBUG */ 235 236 #ifdef SQLITE_DEBUG 237 /* 238 **** This function may be used as part of assert() statements only. **** 239 ** 240 ** Return true if it would be illegal for pBtree to write into the 241 ** table or index rooted at iRoot because other shared connections are 242 ** simultaneously reading that same table or index. 243 ** 244 ** It is illegal for pBtree to write if some other Btree object that 245 ** shares the same BtShared object is currently reading or writing 246 ** the iRoot table. Except, if the other Btree object has the 247 ** read-uncommitted flag set, then it is OK for the other object to 248 ** have a read cursor. 249 ** 250 ** For example, before writing to any part of the table or index 251 ** rooted at page iRoot, one should call: 252 ** 253 ** assert( !hasReadConflicts(pBtree, iRoot) ); 254 */ 255 static int hasReadConflicts(Btree *pBtree, Pgno iRoot){ 256 BtCursor *p; 257 for(p=pBtree->pBt->pCursor; p; p=p->pNext){ 258 if( p->pgnoRoot==iRoot 259 && p->pBtree!=pBtree 260 && 0==(p->pBtree->db->flags & SQLITE_ReadUncommit) 261 ){ 262 return 1; 263 } 264 } 265 return 0; 266 } 267 #endif /* #ifdef SQLITE_DEBUG */ 268 269 /* 270 ** Query to see if Btree handle p may obtain a lock of type eLock 271 ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return 272 ** SQLITE_OK if the lock may be obtained (by calling 273 ** setSharedCacheTableLock()), or SQLITE_LOCKED if not. 274 */ 275 static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){ 276 BtShared *pBt = p->pBt; 277 BtLock *pIter; 278 279 assert( sqlite3BtreeHoldsMutex(p) ); 280 assert( eLock==READ_LOCK || eLock==WRITE_LOCK ); 281 assert( p->db!=0 ); 282 assert( !(p->db->flags&SQLITE_ReadUncommit)||eLock==WRITE_LOCK||iTab==1 ); 283 284 /* If requesting a write-lock, then the Btree must have an open write 285 ** transaction on this file. And, obviously, for this to be so there 286 ** must be an open write transaction on the file itself. 287 */ 288 assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) ); 289 assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE ); 290 291 /* This routine is a no-op if the shared-cache is not enabled */ 292 if( !p->sharable ){ 293 return SQLITE_OK; 294 } 295 296 /* If some other connection is holding an exclusive lock, the 297 ** requested lock may not be obtained. 298 */ 299 if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){ 300 sqlite3ConnectionBlocked(p->db, pBt->pWriter->db); 301 return SQLITE_LOCKED_SHAREDCACHE; 302 } 303 304 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ 305 /* The condition (pIter->eLock!=eLock) in the following if(...) 306 ** statement is a simplification of: 307 ** 308 ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK) 309 ** 310 ** since we know that if eLock==WRITE_LOCK, then no other connection 311 ** may hold a WRITE_LOCK on any table in this file (since there can 312 ** only be a single writer). 313 */ 314 assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK ); 315 assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK); 316 if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){ 317 sqlite3ConnectionBlocked(p->db, pIter->pBtree->db); 318 if( eLock==WRITE_LOCK ){ 319 assert( p==pBt->pWriter ); 320 pBt->btsFlags |= BTS_PENDING; 321 } 322 return SQLITE_LOCKED_SHAREDCACHE; 323 } 324 } 325 return SQLITE_OK; 326 } 327 #endif /* !SQLITE_OMIT_SHARED_CACHE */ 328 329 #ifndef SQLITE_OMIT_SHARED_CACHE 330 /* 331 ** Add a lock on the table with root-page iTable to the shared-btree used 332 ** by Btree handle p. Parameter eLock must be either READ_LOCK or 333 ** WRITE_LOCK. 334 ** 335 ** This function assumes the following: 336 ** 337 ** (a) The specified Btree object p is connected to a sharable 338 ** database (one with the BtShared.sharable flag set), and 339 ** 340 ** (b) No other Btree objects hold a lock that conflicts 341 ** with the requested lock (i.e. querySharedCacheTableLock() has 342 ** already been called and returned SQLITE_OK). 343 ** 344 ** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM 345 ** is returned if a malloc attempt fails. 346 */ 347 static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){ 348 BtShared *pBt = p->pBt; 349 BtLock *pLock = 0; 350 BtLock *pIter; 351 352 assert( sqlite3BtreeHoldsMutex(p) ); 353 assert( eLock==READ_LOCK || eLock==WRITE_LOCK ); 354 assert( p->db!=0 ); 355 356 /* A connection with the read-uncommitted flag set will never try to 357 ** obtain a read-lock using this function. The only read-lock obtained 358 ** by a connection in read-uncommitted mode is on the sqlite_master 359 ** table, and that lock is obtained in BtreeBeginTrans(). */ 360 assert( 0==(p->db->flags&SQLITE_ReadUncommit) || eLock==WRITE_LOCK ); 361 362 /* This function should only be called on a sharable b-tree after it 363 ** has been determined that no other b-tree holds a conflicting lock. */ 364 assert( p->sharable ); 365 assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) ); 366 367 /* First search the list for an existing lock on this table. */ 368 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ 369 if( pIter->iTable==iTable && pIter->pBtree==p ){ 370 pLock = pIter; 371 break; 372 } 373 } 374 375 /* If the above search did not find a BtLock struct associating Btree p 376 ** with table iTable, allocate one and link it into the list. 377 */ 378 if( !pLock ){ 379 pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock)); 380 if( !pLock ){ 381 return SQLITE_NOMEM_BKPT; 382 } 383 pLock->iTable = iTable; 384 pLock->pBtree = p; 385 pLock->pNext = pBt->pLock; 386 pBt->pLock = pLock; 387 } 388 389 /* Set the BtLock.eLock variable to the maximum of the current lock 390 ** and the requested lock. This means if a write-lock was already held 391 ** and a read-lock requested, we don't incorrectly downgrade the lock. 392 */ 393 assert( WRITE_LOCK>READ_LOCK ); 394 if( eLock>pLock->eLock ){ 395 pLock->eLock = eLock; 396 } 397 398 return SQLITE_OK; 399 } 400 #endif /* !SQLITE_OMIT_SHARED_CACHE */ 401 402 #ifndef SQLITE_OMIT_SHARED_CACHE 403 /* 404 ** Release all the table locks (locks obtained via calls to 405 ** the setSharedCacheTableLock() procedure) held by Btree object p. 406 ** 407 ** This function assumes that Btree p has an open read or write 408 ** transaction. If it does not, then the BTS_PENDING flag 409 ** may be incorrectly cleared. 410 */ 411 static void clearAllSharedCacheTableLocks(Btree *p){ 412 BtShared *pBt = p->pBt; 413 BtLock **ppIter = &pBt->pLock; 414 415 assert( sqlite3BtreeHoldsMutex(p) ); 416 assert( p->sharable || 0==*ppIter ); 417 assert( p->inTrans>0 ); 418 419 while( *ppIter ){ 420 BtLock *pLock = *ppIter; 421 assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree ); 422 assert( pLock->pBtree->inTrans>=pLock->eLock ); 423 if( pLock->pBtree==p ){ 424 *ppIter = pLock->pNext; 425 assert( pLock->iTable!=1 || pLock==&p->lock ); 426 if( pLock->iTable!=1 ){ 427 sqlite3_free(pLock); 428 } 429 }else{ 430 ppIter = &pLock->pNext; 431 } 432 } 433 434 assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter ); 435 if( pBt->pWriter==p ){ 436 pBt->pWriter = 0; 437 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING); 438 }else if( pBt->nTransaction==2 ){ 439 /* This function is called when Btree p is concluding its 440 ** transaction. If there currently exists a writer, and p is not 441 ** that writer, then the number of locks held by connections other 442 ** than the writer must be about to drop to zero. In this case 443 ** set the BTS_PENDING flag to 0. 444 ** 445 ** If there is not currently a writer, then BTS_PENDING must 446 ** be zero already. So this next line is harmless in that case. 447 */ 448 pBt->btsFlags &= ~BTS_PENDING; 449 } 450 } 451 452 /* 453 ** This function changes all write-locks held by Btree p into read-locks. 454 */ 455 static void downgradeAllSharedCacheTableLocks(Btree *p){ 456 BtShared *pBt = p->pBt; 457 if( pBt->pWriter==p ){ 458 BtLock *pLock; 459 pBt->pWriter = 0; 460 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING); 461 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){ 462 assert( pLock->eLock==READ_LOCK || pLock->pBtree==p ); 463 pLock->eLock = READ_LOCK; 464 } 465 } 466 } 467 468 #endif /* SQLITE_OMIT_SHARED_CACHE */ 469 470 static void releasePage(MemPage *pPage); /* Forward reference */ 471 static void releasePageOne(MemPage *pPage); /* Forward reference */ 472 static void releasePageNotNull(MemPage *pPage); /* Forward reference */ 473 474 /* 475 ***** This routine is used inside of assert() only **** 476 ** 477 ** Verify that the cursor holds the mutex on its BtShared 478 */ 479 #ifdef SQLITE_DEBUG 480 static int cursorHoldsMutex(BtCursor *p){ 481 return sqlite3_mutex_held(p->pBt->mutex); 482 } 483 484 /* Verify that the cursor and the BtShared agree about what is the current 485 ** database connetion. This is important in shared-cache mode. If the database 486 ** connection pointers get out-of-sync, it is possible for routines like 487 ** btreeInitPage() to reference an stale connection pointer that references a 488 ** a connection that has already closed. This routine is used inside assert() 489 ** statements only and for the purpose of double-checking that the btree code 490 ** does keep the database connection pointers up-to-date. 491 */ 492 static int cursorOwnsBtShared(BtCursor *p){ 493 assert( cursorHoldsMutex(p) ); 494 return (p->pBtree->db==p->pBt->db); 495 } 496 #endif 497 498 /* 499 ** Invalidate the overflow cache of the cursor passed as the first argument. 500 ** on the shared btree structure pBt. 501 */ 502 #define invalidateOverflowCache(pCur) (pCur->curFlags &= ~BTCF_ValidOvfl) 503 504 /* 505 ** Invalidate the overflow page-list cache for all cursors opened 506 ** on the shared btree structure pBt. 507 */ 508 static void invalidateAllOverflowCache(BtShared *pBt){ 509 BtCursor *p; 510 assert( sqlite3_mutex_held(pBt->mutex) ); 511 for(p=pBt->pCursor; p; p=p->pNext){ 512 invalidateOverflowCache(p); 513 } 514 } 515 516 #ifndef SQLITE_OMIT_INCRBLOB 517 /* 518 ** This function is called before modifying the contents of a table 519 ** to invalidate any incrblob cursors that are open on the 520 ** row or one of the rows being modified. 521 ** 522 ** If argument isClearTable is true, then the entire contents of the 523 ** table is about to be deleted. In this case invalidate all incrblob 524 ** cursors open on any row within the table with root-page pgnoRoot. 525 ** 526 ** Otherwise, if argument isClearTable is false, then the row with 527 ** rowid iRow is being replaced or deleted. In this case invalidate 528 ** only those incrblob cursors open on that specific row. 529 */ 530 static void invalidateIncrblobCursors( 531 Btree *pBtree, /* The database file to check */ 532 Pgno pgnoRoot, /* The table that might be changing */ 533 i64 iRow, /* The rowid that might be changing */ 534 int isClearTable /* True if all rows are being deleted */ 535 ){ 536 BtCursor *p; 537 if( pBtree->hasIncrblobCur==0 ) return; 538 assert( sqlite3BtreeHoldsMutex(pBtree) ); 539 pBtree->hasIncrblobCur = 0; 540 for(p=pBtree->pBt->pCursor; p; p=p->pNext){ 541 if( (p->curFlags & BTCF_Incrblob)!=0 ){ 542 pBtree->hasIncrblobCur = 1; 543 if( p->pgnoRoot==pgnoRoot && (isClearTable || p->info.nKey==iRow) ){ 544 p->eState = CURSOR_INVALID; 545 } 546 } 547 } 548 } 549 550 #else 551 /* Stub function when INCRBLOB is omitted */ 552 #define invalidateIncrblobCursors(w,x,y,z) 553 #endif /* SQLITE_OMIT_INCRBLOB */ 554 555 /* 556 ** Set bit pgno of the BtShared.pHasContent bitvec. This is called 557 ** when a page that previously contained data becomes a free-list leaf 558 ** page. 559 ** 560 ** The BtShared.pHasContent bitvec exists to work around an obscure 561 ** bug caused by the interaction of two useful IO optimizations surrounding 562 ** free-list leaf pages: 563 ** 564 ** 1) When all data is deleted from a page and the page becomes 565 ** a free-list leaf page, the page is not written to the database 566 ** (as free-list leaf pages contain no meaningful data). Sometimes 567 ** such a page is not even journalled (as it will not be modified, 568 ** why bother journalling it?). 569 ** 570 ** 2) When a free-list leaf page is reused, its content is not read 571 ** from the database or written to the journal file (why should it 572 ** be, if it is not at all meaningful?). 573 ** 574 ** By themselves, these optimizations work fine and provide a handy 575 ** performance boost to bulk delete or insert operations. However, if 576 ** a page is moved to the free-list and then reused within the same 577 ** transaction, a problem comes up. If the page is not journalled when 578 ** it is moved to the free-list and it is also not journalled when it 579 ** is extracted from the free-list and reused, then the original data 580 ** may be lost. In the event of a rollback, it may not be possible 581 ** to restore the database to its original configuration. 582 ** 583 ** The solution is the BtShared.pHasContent bitvec. Whenever a page is 584 ** moved to become a free-list leaf page, the corresponding bit is 585 ** set in the bitvec. Whenever a leaf page is extracted from the free-list, 586 ** optimization 2 above is omitted if the corresponding bit is already 587 ** set in BtShared.pHasContent. The contents of the bitvec are cleared 588 ** at the end of every transaction. 589 */ 590 static int btreeSetHasContent(BtShared *pBt, Pgno pgno){ 591 int rc = SQLITE_OK; 592 if( !pBt->pHasContent ){ 593 assert( pgno<=pBt->nPage ); 594 pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage); 595 if( !pBt->pHasContent ){ 596 rc = SQLITE_NOMEM_BKPT; 597 } 598 } 599 if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){ 600 rc = sqlite3BitvecSet(pBt->pHasContent, pgno); 601 } 602 return rc; 603 } 604 605 /* 606 ** Query the BtShared.pHasContent vector. 607 ** 608 ** This function is called when a free-list leaf page is removed from the 609 ** free-list for reuse. It returns false if it is safe to retrieve the 610 ** page from the pager layer with the 'no-content' flag set. True otherwise. 611 */ 612 static int btreeGetHasContent(BtShared *pBt, Pgno pgno){ 613 Bitvec *p = pBt->pHasContent; 614 return (p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTest(p, pgno))); 615 } 616 617 /* 618 ** Clear (destroy) the BtShared.pHasContent bitvec. This should be 619 ** invoked at the conclusion of each write-transaction. 620 */ 621 static void btreeClearHasContent(BtShared *pBt){ 622 sqlite3BitvecDestroy(pBt->pHasContent); 623 pBt->pHasContent = 0; 624 } 625 626 /* 627 ** Release all of the apPage[] pages for a cursor. 628 */ 629 static void btreeReleaseAllCursorPages(BtCursor *pCur){ 630 int i; 631 if( pCur->iPage>=0 ){ 632 for(i=0; i<pCur->iPage; i++){ 633 releasePageNotNull(pCur->apPage[i]); 634 } 635 releasePageNotNull(pCur->pPage); 636 pCur->iPage = -1; 637 } 638 } 639 640 /* 641 ** The cursor passed as the only argument must point to a valid entry 642 ** when this function is called (i.e. have eState==CURSOR_VALID). This 643 ** function saves the current cursor key in variables pCur->nKey and 644 ** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error 645 ** code otherwise. 646 ** 647 ** If the cursor is open on an intkey table, then the integer key 648 ** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to 649 ** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is 650 ** set to point to a malloced buffer pCur->nKey bytes in size containing 651 ** the key. 652 */ 653 static int saveCursorKey(BtCursor *pCur){ 654 int rc = SQLITE_OK; 655 assert( CURSOR_VALID==pCur->eState ); 656 assert( 0==pCur->pKey ); 657 assert( cursorHoldsMutex(pCur) ); 658 659 if( pCur->curIntKey ){ 660 /* Only the rowid is required for a table btree */ 661 pCur->nKey = sqlite3BtreeIntegerKey(pCur); 662 }else{ 663 /* For an index btree, save the complete key content */ 664 void *pKey; 665 pCur->nKey = sqlite3BtreePayloadSize(pCur); 666 pKey = sqlite3Malloc( pCur->nKey ); 667 if( pKey ){ 668 rc = sqlite3BtreePayload(pCur, 0, (int)pCur->nKey, pKey); 669 if( rc==SQLITE_OK ){ 670 pCur->pKey = pKey; 671 }else{ 672 sqlite3_free(pKey); 673 } 674 }else{ 675 rc = SQLITE_NOMEM_BKPT; 676 } 677 } 678 assert( !pCur->curIntKey || !pCur->pKey ); 679 return rc; 680 } 681 682 /* 683 ** Save the current cursor position in the variables BtCursor.nKey 684 ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK. 685 ** 686 ** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID) 687 ** prior to calling this routine. 688 */ 689 static int saveCursorPosition(BtCursor *pCur){ 690 int rc; 691 692 assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState ); 693 assert( 0==pCur->pKey ); 694 assert( cursorHoldsMutex(pCur) ); 695 696 if( pCur->eState==CURSOR_SKIPNEXT ){ 697 pCur->eState = CURSOR_VALID; 698 }else{ 699 pCur->skipNext = 0; 700 } 701 702 rc = saveCursorKey(pCur); 703 if( rc==SQLITE_OK ){ 704 btreeReleaseAllCursorPages(pCur); 705 pCur->eState = CURSOR_REQUIRESEEK; 706 } 707 708 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl|BTCF_AtLast); 709 return rc; 710 } 711 712 /* Forward reference */ 713 static int SQLITE_NOINLINE saveCursorsOnList(BtCursor*,Pgno,BtCursor*); 714 715 /* 716 ** Save the positions of all cursors (except pExcept) that are open on 717 ** the table with root-page iRoot. "Saving the cursor position" means that 718 ** the location in the btree is remembered in such a way that it can be 719 ** moved back to the same spot after the btree has been modified. This 720 ** routine is called just before cursor pExcept is used to modify the 721 ** table, for example in BtreeDelete() or BtreeInsert(). 722 ** 723 ** If there are two or more cursors on the same btree, then all such 724 ** cursors should have their BTCF_Multiple flag set. The btreeCursor() 725 ** routine enforces that rule. This routine only needs to be called in 726 ** the uncommon case when pExpect has the BTCF_Multiple flag set. 727 ** 728 ** If pExpect!=NULL and if no other cursors are found on the same root-page, 729 ** then the BTCF_Multiple flag on pExpect is cleared, to avoid another 730 ** pointless call to this routine. 731 ** 732 ** Implementation note: This routine merely checks to see if any cursors 733 ** need to be saved. It calls out to saveCursorsOnList() in the (unusual) 734 ** event that cursors are in need to being saved. 735 */ 736 static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){ 737 BtCursor *p; 738 assert( sqlite3_mutex_held(pBt->mutex) ); 739 assert( pExcept==0 || pExcept->pBt==pBt ); 740 for(p=pBt->pCursor; p; p=p->pNext){ 741 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break; 742 } 743 if( p ) return saveCursorsOnList(p, iRoot, pExcept); 744 if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple; 745 return SQLITE_OK; 746 } 747 748 /* This helper routine to saveAllCursors does the actual work of saving 749 ** the cursors if and when a cursor is found that actually requires saving. 750 ** The common case is that no cursors need to be saved, so this routine is 751 ** broken out from its caller to avoid unnecessary stack pointer movement. 752 */ 753 static int SQLITE_NOINLINE saveCursorsOnList( 754 BtCursor *p, /* The first cursor that needs saving */ 755 Pgno iRoot, /* Only save cursor with this iRoot. Save all if zero */ 756 BtCursor *pExcept /* Do not save this cursor */ 757 ){ 758 do{ 759 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){ 760 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){ 761 int rc = saveCursorPosition(p); 762 if( SQLITE_OK!=rc ){ 763 return rc; 764 } 765 }else{ 766 testcase( p->iPage>=0 ); 767 btreeReleaseAllCursorPages(p); 768 } 769 } 770 p = p->pNext; 771 }while( p ); 772 return SQLITE_OK; 773 } 774 775 /* 776 ** Clear the current cursor position. 777 */ 778 void sqlite3BtreeClearCursor(BtCursor *pCur){ 779 assert( cursorHoldsMutex(pCur) ); 780 sqlite3_free(pCur->pKey); 781 pCur->pKey = 0; 782 pCur->eState = CURSOR_INVALID; 783 } 784 785 /* 786 ** In this version of BtreeMoveto, pKey is a packed index record 787 ** such as is generated by the OP_MakeRecord opcode. Unpack the 788 ** record and then call BtreeMovetoUnpacked() to do the work. 789 */ 790 static int btreeMoveto( 791 BtCursor *pCur, /* Cursor open on the btree to be searched */ 792 const void *pKey, /* Packed key if the btree is an index */ 793 i64 nKey, /* Integer key for tables. Size of pKey for indices */ 794 int bias, /* Bias search to the high end */ 795 int *pRes /* Write search results here */ 796 ){ 797 int rc; /* Status code */ 798 UnpackedRecord *pIdxKey; /* Unpacked index key */ 799 800 if( pKey ){ 801 assert( nKey==(i64)(int)nKey ); 802 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pCur->pKeyInfo); 803 if( pIdxKey==0 ) return SQLITE_NOMEM_BKPT; 804 sqlite3VdbeRecordUnpack(pCur->pKeyInfo, (int)nKey, pKey, pIdxKey); 805 if( pIdxKey->nField==0 ){ 806 rc = SQLITE_CORRUPT_BKPT; 807 goto moveto_done; 808 } 809 }else{ 810 pIdxKey = 0; 811 } 812 rc = sqlite3BtreeMovetoUnpacked(pCur, pIdxKey, nKey, bias, pRes); 813 moveto_done: 814 if( pIdxKey ){ 815 sqlite3DbFree(pCur->pKeyInfo->db, pIdxKey); 816 } 817 return rc; 818 } 819 820 /* 821 ** Restore the cursor to the position it was in (or as close to as possible) 822 ** when saveCursorPosition() was called. Note that this call deletes the 823 ** saved position info stored by saveCursorPosition(), so there can be 824 ** at most one effective restoreCursorPosition() call after each 825 ** saveCursorPosition(). 826 */ 827 static int btreeRestoreCursorPosition(BtCursor *pCur){ 828 int rc; 829 int skipNext; 830 assert( cursorOwnsBtShared(pCur) ); 831 assert( pCur->eState>=CURSOR_REQUIRESEEK ); 832 if( pCur->eState==CURSOR_FAULT ){ 833 return pCur->skipNext; 834 } 835 pCur->eState = CURSOR_INVALID; 836 rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext); 837 if( rc==SQLITE_OK ){ 838 sqlite3_free(pCur->pKey); 839 pCur->pKey = 0; 840 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID ); 841 pCur->skipNext |= skipNext; 842 if( pCur->skipNext && pCur->eState==CURSOR_VALID ){ 843 pCur->eState = CURSOR_SKIPNEXT; 844 } 845 } 846 return rc; 847 } 848 849 #define restoreCursorPosition(p) \ 850 (p->eState>=CURSOR_REQUIRESEEK ? \ 851 btreeRestoreCursorPosition(p) : \ 852 SQLITE_OK) 853 854 /* 855 ** Determine whether or not a cursor has moved from the position where 856 ** it was last placed, or has been invalidated for any other reason. 857 ** Cursors can move when the row they are pointing at is deleted out 858 ** from under them, for example. Cursor might also move if a btree 859 ** is rebalanced. 860 ** 861 ** Calling this routine with a NULL cursor pointer returns false. 862 ** 863 ** Use the separate sqlite3BtreeCursorRestore() routine to restore a cursor 864 ** back to where it ought to be if this routine returns true. 865 */ 866 int sqlite3BtreeCursorHasMoved(BtCursor *pCur){ 867 return pCur->eState!=CURSOR_VALID; 868 } 869 870 /* 871 ** Return a pointer to a fake BtCursor object that will always answer 872 ** false to the sqlite3BtreeCursorHasMoved() routine above. The fake 873 ** cursor returned must not be used with any other Btree interface. 874 */ 875 BtCursor *sqlite3BtreeFakeValidCursor(void){ 876 static u8 fakeCursor = CURSOR_VALID; 877 assert( offsetof(BtCursor, eState)==0 ); 878 return (BtCursor*)&fakeCursor; 879 } 880 881 /* 882 ** This routine restores a cursor back to its original position after it 883 ** has been moved by some outside activity (such as a btree rebalance or 884 ** a row having been deleted out from under the cursor). 885 ** 886 ** On success, the *pDifferentRow parameter is false if the cursor is left 887 ** pointing at exactly the same row. *pDifferntRow is the row the cursor 888 ** was pointing to has been deleted, forcing the cursor to point to some 889 ** nearby row. 890 ** 891 ** This routine should only be called for a cursor that just returned 892 ** TRUE from sqlite3BtreeCursorHasMoved(). 893 */ 894 int sqlite3BtreeCursorRestore(BtCursor *pCur, int *pDifferentRow){ 895 int rc; 896 897 assert( pCur!=0 ); 898 assert( pCur->eState!=CURSOR_VALID ); 899 rc = restoreCursorPosition(pCur); 900 if( rc ){ 901 *pDifferentRow = 1; 902 return rc; 903 } 904 if( pCur->eState!=CURSOR_VALID ){ 905 *pDifferentRow = 1; 906 }else{ 907 assert( pCur->skipNext==0 ); 908 *pDifferentRow = 0; 909 } 910 return SQLITE_OK; 911 } 912 913 #ifdef SQLITE_ENABLE_CURSOR_HINTS 914 /* 915 ** Provide hints to the cursor. The particular hint given (and the type 916 ** and number of the varargs parameters) is determined by the eHintType 917 ** parameter. See the definitions of the BTREE_HINT_* macros for details. 918 */ 919 void sqlite3BtreeCursorHint(BtCursor *pCur, int eHintType, ...){ 920 /* Used only by system that substitute their own storage engine */ 921 } 922 #endif 923 924 /* 925 ** Provide flag hints to the cursor. 926 */ 927 void sqlite3BtreeCursorHintFlags(BtCursor *pCur, unsigned x){ 928 assert( x==BTREE_SEEK_EQ || x==BTREE_BULKLOAD || x==0 ); 929 pCur->hints = x; 930 } 931 932 933 #ifndef SQLITE_OMIT_AUTOVACUUM 934 /* 935 ** Given a page number of a regular database page, return the page 936 ** number for the pointer-map page that contains the entry for the 937 ** input page number. 938 ** 939 ** Return 0 (not a valid page) for pgno==1 since there is 940 ** no pointer map associated with page 1. The integrity_check logic 941 ** requires that ptrmapPageno(*,1)!=1. 942 */ 943 static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){ 944 int nPagesPerMapPage; 945 Pgno iPtrMap, ret; 946 assert( sqlite3_mutex_held(pBt->mutex) ); 947 if( pgno<2 ) return 0; 948 nPagesPerMapPage = (pBt->usableSize/5)+1; 949 iPtrMap = (pgno-2)/nPagesPerMapPage; 950 ret = (iPtrMap*nPagesPerMapPage) + 2; 951 if( ret==PENDING_BYTE_PAGE(pBt) ){ 952 ret++; 953 } 954 return ret; 955 } 956 957 /* 958 ** Write an entry into the pointer map. 959 ** 960 ** This routine updates the pointer map entry for page number 'key' 961 ** so that it maps to type 'eType' and parent page number 'pgno'. 962 ** 963 ** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is 964 ** a no-op. If an error occurs, the appropriate error code is written 965 ** into *pRC. 966 */ 967 static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){ 968 DbPage *pDbPage; /* The pointer map page */ 969 u8 *pPtrmap; /* The pointer map data */ 970 Pgno iPtrmap; /* The pointer map page number */ 971 int offset; /* Offset in pointer map page */ 972 int rc; /* Return code from subfunctions */ 973 974 if( *pRC ) return; 975 976 assert( sqlite3_mutex_held(pBt->mutex) ); 977 /* The master-journal page number must never be used as a pointer map page */ 978 assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) ); 979 980 assert( pBt->autoVacuum ); 981 if( key==0 ){ 982 *pRC = SQLITE_CORRUPT_BKPT; 983 return; 984 } 985 iPtrmap = PTRMAP_PAGENO(pBt, key); 986 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0); 987 if( rc!=SQLITE_OK ){ 988 *pRC = rc; 989 return; 990 } 991 offset = PTRMAP_PTROFFSET(iPtrmap, key); 992 if( offset<0 ){ 993 *pRC = SQLITE_CORRUPT_BKPT; 994 goto ptrmap_exit; 995 } 996 assert( offset <= (int)pBt->usableSize-5 ); 997 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); 998 999 if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){ 1000 TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent)); 1001 *pRC= rc = sqlite3PagerWrite(pDbPage); 1002 if( rc==SQLITE_OK ){ 1003 pPtrmap[offset] = eType; 1004 put4byte(&pPtrmap[offset+1], parent); 1005 } 1006 } 1007 1008 ptrmap_exit: 1009 sqlite3PagerUnref(pDbPage); 1010 } 1011 1012 /* 1013 ** Read an entry from the pointer map. 1014 ** 1015 ** This routine retrieves the pointer map entry for page 'key', writing 1016 ** the type and parent page number to *pEType and *pPgno respectively. 1017 ** An error code is returned if something goes wrong, otherwise SQLITE_OK. 1018 */ 1019 static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){ 1020 DbPage *pDbPage; /* The pointer map page */ 1021 int iPtrmap; /* Pointer map page index */ 1022 u8 *pPtrmap; /* Pointer map page data */ 1023 int offset; /* Offset of entry in pointer map */ 1024 int rc; 1025 1026 assert( sqlite3_mutex_held(pBt->mutex) ); 1027 1028 iPtrmap = PTRMAP_PAGENO(pBt, key); 1029 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0); 1030 if( rc!=0 ){ 1031 return rc; 1032 } 1033 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); 1034 1035 offset = PTRMAP_PTROFFSET(iPtrmap, key); 1036 if( offset<0 ){ 1037 sqlite3PagerUnref(pDbPage); 1038 return SQLITE_CORRUPT_BKPT; 1039 } 1040 assert( offset <= (int)pBt->usableSize-5 ); 1041 assert( pEType!=0 ); 1042 *pEType = pPtrmap[offset]; 1043 if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]); 1044 1045 sqlite3PagerUnref(pDbPage); 1046 if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_PGNO(iPtrmap); 1047 return SQLITE_OK; 1048 } 1049 1050 #else /* if defined SQLITE_OMIT_AUTOVACUUM */ 1051 #define ptrmapPut(w,x,y,z,rc) 1052 #define ptrmapGet(w,x,y,z) SQLITE_OK 1053 #define ptrmapPutOvflPtr(x, y, rc) 1054 #endif 1055 1056 /* 1057 ** Given a btree page and a cell index (0 means the first cell on 1058 ** the page, 1 means the second cell, and so forth) return a pointer 1059 ** to the cell content. 1060 ** 1061 ** findCellPastPtr() does the same except it skips past the initial 1062 ** 4-byte child pointer found on interior pages, if there is one. 1063 ** 1064 ** This routine works only for pages that do not contain overflow cells. 1065 */ 1066 #define findCell(P,I) \ 1067 ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)]))) 1068 #define findCellPastPtr(P,I) \ 1069 ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)]))) 1070 1071 1072 /* 1073 ** This is common tail processing for btreeParseCellPtr() and 1074 ** btreeParseCellPtrIndex() for the case when the cell does not fit entirely 1075 ** on a single B-tree page. Make necessary adjustments to the CellInfo 1076 ** structure. 1077 */ 1078 static SQLITE_NOINLINE void btreeParseCellAdjustSizeForOverflow( 1079 MemPage *pPage, /* Page containing the cell */ 1080 u8 *pCell, /* Pointer to the cell text. */ 1081 CellInfo *pInfo /* Fill in this structure */ 1082 ){ 1083 /* If the payload will not fit completely on the local page, we have 1084 ** to decide how much to store locally and how much to spill onto 1085 ** overflow pages. The strategy is to minimize the amount of unused 1086 ** space on overflow pages while keeping the amount of local storage 1087 ** in between minLocal and maxLocal. 1088 ** 1089 ** Warning: changing the way overflow payload is distributed in any 1090 ** way will result in an incompatible file format. 1091 */ 1092 int minLocal; /* Minimum amount of payload held locally */ 1093 int maxLocal; /* Maximum amount of payload held locally */ 1094 int surplus; /* Overflow payload available for local storage */ 1095 1096 minLocal = pPage->minLocal; 1097 maxLocal = pPage->maxLocal; 1098 surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4); 1099 testcase( surplus==maxLocal ); 1100 testcase( surplus==maxLocal+1 ); 1101 if( surplus <= maxLocal ){ 1102 pInfo->nLocal = (u16)surplus; 1103 }else{ 1104 pInfo->nLocal = (u16)minLocal; 1105 } 1106 pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4; 1107 } 1108 1109 /* 1110 ** The following routines are implementations of the MemPage.xParseCell() 1111 ** method. 1112 ** 1113 ** Parse a cell content block and fill in the CellInfo structure. 1114 ** 1115 ** btreeParseCellPtr() => table btree leaf nodes 1116 ** btreeParseCellNoPayload() => table btree internal nodes 1117 ** btreeParseCellPtrIndex() => index btree nodes 1118 ** 1119 ** There is also a wrapper function btreeParseCell() that works for 1120 ** all MemPage types and that references the cell by index rather than 1121 ** by pointer. 1122 */ 1123 static void btreeParseCellPtrNoPayload( 1124 MemPage *pPage, /* Page containing the cell */ 1125 u8 *pCell, /* Pointer to the cell text. */ 1126 CellInfo *pInfo /* Fill in this structure */ 1127 ){ 1128 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 1129 assert( pPage->leaf==0 ); 1130 assert( pPage->childPtrSize==4 ); 1131 #ifndef SQLITE_DEBUG 1132 UNUSED_PARAMETER(pPage); 1133 #endif 1134 pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey); 1135 pInfo->nPayload = 0; 1136 pInfo->nLocal = 0; 1137 pInfo->pPayload = 0; 1138 return; 1139 } 1140 static void btreeParseCellPtr( 1141 MemPage *pPage, /* Page containing the cell */ 1142 u8 *pCell, /* Pointer to the cell text. */ 1143 CellInfo *pInfo /* Fill in this structure */ 1144 ){ 1145 u8 *pIter; /* For scanning through pCell */ 1146 u32 nPayload; /* Number of bytes of cell payload */ 1147 u64 iKey; /* Extracted Key value */ 1148 1149 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 1150 assert( pPage->leaf==0 || pPage->leaf==1 ); 1151 assert( pPage->intKeyLeaf ); 1152 assert( pPage->childPtrSize==0 ); 1153 pIter = pCell; 1154 1155 /* The next block of code is equivalent to: 1156 ** 1157 ** pIter += getVarint32(pIter, nPayload); 1158 ** 1159 ** The code is inlined to avoid a function call. 1160 */ 1161 nPayload = *pIter; 1162 if( nPayload>=0x80 ){ 1163 u8 *pEnd = &pIter[8]; 1164 nPayload &= 0x7f; 1165 do{ 1166 nPayload = (nPayload<<7) | (*++pIter & 0x7f); 1167 }while( (*pIter)>=0x80 && pIter<pEnd ); 1168 } 1169 pIter++; 1170 1171 /* The next block of code is equivalent to: 1172 ** 1173 ** pIter += getVarint(pIter, (u64*)&pInfo->nKey); 1174 ** 1175 ** The code is inlined to avoid a function call. 1176 */ 1177 iKey = *pIter; 1178 if( iKey>=0x80 ){ 1179 u8 *pEnd = &pIter[7]; 1180 iKey &= 0x7f; 1181 while(1){ 1182 iKey = (iKey<<7) | (*++pIter & 0x7f); 1183 if( (*pIter)<0x80 ) break; 1184 if( pIter>=pEnd ){ 1185 iKey = (iKey<<8) | *++pIter; 1186 break; 1187 } 1188 } 1189 } 1190 pIter++; 1191 1192 pInfo->nKey = *(i64*)&iKey; 1193 pInfo->nPayload = nPayload; 1194 pInfo->pPayload = pIter; 1195 testcase( nPayload==pPage->maxLocal ); 1196 testcase( nPayload==pPage->maxLocal+1 ); 1197 if( nPayload<=pPage->maxLocal ){ 1198 /* This is the (easy) common case where the entire payload fits 1199 ** on the local page. No overflow is required. 1200 */ 1201 pInfo->nSize = nPayload + (u16)(pIter - pCell); 1202 if( pInfo->nSize<4 ) pInfo->nSize = 4; 1203 pInfo->nLocal = (u16)nPayload; 1204 }else{ 1205 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo); 1206 } 1207 } 1208 static void btreeParseCellPtrIndex( 1209 MemPage *pPage, /* Page containing the cell */ 1210 u8 *pCell, /* Pointer to the cell text. */ 1211 CellInfo *pInfo /* Fill in this structure */ 1212 ){ 1213 u8 *pIter; /* For scanning through pCell */ 1214 u32 nPayload; /* Number of bytes of cell payload */ 1215 1216 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 1217 assert( pPage->leaf==0 || pPage->leaf==1 ); 1218 assert( pPage->intKeyLeaf==0 ); 1219 pIter = pCell + pPage->childPtrSize; 1220 nPayload = *pIter; 1221 if( nPayload>=0x80 ){ 1222 u8 *pEnd = &pIter[8]; 1223 nPayload &= 0x7f; 1224 do{ 1225 nPayload = (nPayload<<7) | (*++pIter & 0x7f); 1226 }while( *(pIter)>=0x80 && pIter<pEnd ); 1227 } 1228 pIter++; 1229 pInfo->nKey = nPayload; 1230 pInfo->nPayload = nPayload; 1231 pInfo->pPayload = pIter; 1232 testcase( nPayload==pPage->maxLocal ); 1233 testcase( nPayload==pPage->maxLocal+1 ); 1234 if( nPayload<=pPage->maxLocal ){ 1235 /* This is the (easy) common case where the entire payload fits 1236 ** on the local page. No overflow is required. 1237 */ 1238 pInfo->nSize = nPayload + (u16)(pIter - pCell); 1239 if( pInfo->nSize<4 ) pInfo->nSize = 4; 1240 pInfo->nLocal = (u16)nPayload; 1241 }else{ 1242 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo); 1243 } 1244 } 1245 static void btreeParseCell( 1246 MemPage *pPage, /* Page containing the cell */ 1247 int iCell, /* The cell index. First cell is 0 */ 1248 CellInfo *pInfo /* Fill in this structure */ 1249 ){ 1250 pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo); 1251 } 1252 1253 /* 1254 ** The following routines are implementations of the MemPage.xCellSize 1255 ** method. 1256 ** 1257 ** Compute the total number of bytes that a Cell needs in the cell 1258 ** data area of the btree-page. The return number includes the cell 1259 ** data header and the local payload, but not any overflow page or 1260 ** the space used by the cell pointer. 1261 ** 1262 ** cellSizePtrNoPayload() => table internal nodes 1263 ** cellSizePtr() => all index nodes & table leaf nodes 1264 */ 1265 static u16 cellSizePtr(MemPage *pPage, u8 *pCell){ 1266 u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */ 1267 u8 *pEnd; /* End mark for a varint */ 1268 u32 nSize; /* Size value to return */ 1269 1270 #ifdef SQLITE_DEBUG 1271 /* The value returned by this function should always be the same as 1272 ** the (CellInfo.nSize) value found by doing a full parse of the 1273 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of 1274 ** this function verifies that this invariant is not violated. */ 1275 CellInfo debuginfo; 1276 pPage->xParseCell(pPage, pCell, &debuginfo); 1277 #endif 1278 1279 nSize = *pIter; 1280 if( nSize>=0x80 ){ 1281 pEnd = &pIter[8]; 1282 nSize &= 0x7f; 1283 do{ 1284 nSize = (nSize<<7) | (*++pIter & 0x7f); 1285 }while( *(pIter)>=0x80 && pIter<pEnd ); 1286 } 1287 pIter++; 1288 if( pPage->intKey ){ 1289 /* pIter now points at the 64-bit integer key value, a variable length 1290 ** integer. The following block moves pIter to point at the first byte 1291 ** past the end of the key value. */ 1292 pEnd = &pIter[9]; 1293 while( (*pIter++)&0x80 && pIter<pEnd ); 1294 } 1295 testcase( nSize==pPage->maxLocal ); 1296 testcase( nSize==pPage->maxLocal+1 ); 1297 if( nSize<=pPage->maxLocal ){ 1298 nSize += (u32)(pIter - pCell); 1299 if( nSize<4 ) nSize = 4; 1300 }else{ 1301 int minLocal = pPage->minLocal; 1302 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4); 1303 testcase( nSize==pPage->maxLocal ); 1304 testcase( nSize==pPage->maxLocal+1 ); 1305 if( nSize>pPage->maxLocal ){ 1306 nSize = minLocal; 1307 } 1308 nSize += 4 + (u16)(pIter - pCell); 1309 } 1310 assert( nSize==debuginfo.nSize || CORRUPT_DB ); 1311 return (u16)nSize; 1312 } 1313 static u16 cellSizePtrNoPayload(MemPage *pPage, u8 *pCell){ 1314 u8 *pIter = pCell + 4; /* For looping over bytes of pCell */ 1315 u8 *pEnd; /* End mark for a varint */ 1316 1317 #ifdef SQLITE_DEBUG 1318 /* The value returned by this function should always be the same as 1319 ** the (CellInfo.nSize) value found by doing a full parse of the 1320 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of 1321 ** this function verifies that this invariant is not violated. */ 1322 CellInfo debuginfo; 1323 pPage->xParseCell(pPage, pCell, &debuginfo); 1324 #else 1325 UNUSED_PARAMETER(pPage); 1326 #endif 1327 1328 assert( pPage->childPtrSize==4 ); 1329 pEnd = pIter + 9; 1330 while( (*pIter++)&0x80 && pIter<pEnd ); 1331 assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB ); 1332 return (u16)(pIter - pCell); 1333 } 1334 1335 1336 #ifdef SQLITE_DEBUG 1337 /* This variation on cellSizePtr() is used inside of assert() statements 1338 ** only. */ 1339 static u16 cellSize(MemPage *pPage, int iCell){ 1340 return pPage->xCellSize(pPage, findCell(pPage, iCell)); 1341 } 1342 #endif 1343 1344 #ifndef SQLITE_OMIT_AUTOVACUUM 1345 /* 1346 ** If the cell pCell, part of page pPage contains a pointer 1347 ** to an overflow page, insert an entry into the pointer-map 1348 ** for the overflow page. 1349 */ 1350 static void ptrmapPutOvflPtr(MemPage *pPage, u8 *pCell, int *pRC){ 1351 CellInfo info; 1352 if( *pRC ) return; 1353 assert( pCell!=0 ); 1354 pPage->xParseCell(pPage, pCell, &info); 1355 if( info.nLocal<info.nPayload ){ 1356 Pgno ovfl = get4byte(&pCell[info.nSize-4]); 1357 ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC); 1358 } 1359 } 1360 #endif 1361 1362 1363 /* 1364 ** Defragment the page given. This routine reorganizes cells within the 1365 ** page so that there are no free-blocks on the free-block list. 1366 ** 1367 ** Parameter nMaxFrag is the maximum amount of fragmented space that may be 1368 ** present in the page after this routine returns. 1369 ** 1370 ** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a 1371 ** b-tree page so that there are no freeblocks or fragment bytes, all 1372 ** unused bytes are contained in the unallocated space region, and all 1373 ** cells are packed tightly at the end of the page. 1374 */ 1375 static int defragmentPage(MemPage *pPage, int nMaxFrag){ 1376 int i; /* Loop counter */ 1377 int pc; /* Address of the i-th cell */ 1378 int hdr; /* Offset to the page header */ 1379 int size; /* Size of a cell */ 1380 int usableSize; /* Number of usable bytes on a page */ 1381 int cellOffset; /* Offset to the cell pointer array */ 1382 int cbrk; /* Offset to the cell content area */ 1383 int nCell; /* Number of cells on the page */ 1384 unsigned char *data; /* The page data */ 1385 unsigned char *temp; /* Temp area for cell content */ 1386 unsigned char *src; /* Source of content */ 1387 int iCellFirst; /* First allowable cell index */ 1388 int iCellLast; /* Last possible cell index */ 1389 1390 assert( sqlite3PagerIswriteable(pPage->pDbPage) ); 1391 assert( pPage->pBt!=0 ); 1392 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE ); 1393 assert( pPage->nOverflow==0 ); 1394 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 1395 temp = 0; 1396 src = data = pPage->aData; 1397 hdr = pPage->hdrOffset; 1398 cellOffset = pPage->cellOffset; 1399 nCell = pPage->nCell; 1400 assert( nCell==get2byte(&data[hdr+3]) ); 1401 iCellFirst = cellOffset + 2*nCell; 1402 usableSize = pPage->pBt->usableSize; 1403 1404 /* This block handles pages with two or fewer free blocks and nMaxFrag 1405 ** or fewer fragmented bytes. In this case it is faster to move the 1406 ** two (or one) blocks of cells using memmove() and add the required 1407 ** offsets to each pointer in the cell-pointer array than it is to 1408 ** reconstruct the entire page. */ 1409 if( (int)data[hdr+7]<=nMaxFrag ){ 1410 int iFree = get2byte(&data[hdr+1]); 1411 if( iFree ){ 1412 int iFree2 = get2byte(&data[iFree]); 1413 1414 /* pageFindSlot() has already verified that free blocks are sorted 1415 ** in order of offset within the page, and that no block extends 1416 ** past the end of the page. Provided the two free slots do not 1417 ** overlap, this guarantees that the memmove() calls below will not 1418 ** overwrite the usableSize byte buffer, even if the database page 1419 ** is corrupt. */ 1420 assert( iFree2==0 || iFree2>iFree ); 1421 assert( iFree+get2byte(&data[iFree+2]) <= usableSize ); 1422 assert( iFree2==0 || iFree2+get2byte(&data[iFree2+2]) <= usableSize ); 1423 1424 if( 0==iFree2 || (data[iFree2]==0 && data[iFree2+1]==0) ){ 1425 u8 *pEnd = &data[cellOffset + nCell*2]; 1426 u8 *pAddr; 1427 int sz2 = 0; 1428 int sz = get2byte(&data[iFree+2]); 1429 int top = get2byte(&data[hdr+5]); 1430 if( top>=iFree ){ 1431 return SQLITE_CORRUPT_PAGE(pPage); 1432 } 1433 if( iFree2 ){ 1434 assert( iFree+sz<=iFree2 ); /* Verified by pageFindSlot() */ 1435 sz2 = get2byte(&data[iFree2+2]); 1436 assert( iFree+sz+sz2+iFree2-(iFree+sz) <= usableSize ); 1437 memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz)); 1438 sz += sz2; 1439 } 1440 cbrk = top+sz; 1441 assert( cbrk+(iFree-top) <= usableSize ); 1442 memmove(&data[cbrk], &data[top], iFree-top); 1443 for(pAddr=&data[cellOffset]; pAddr<pEnd; pAddr+=2){ 1444 pc = get2byte(pAddr); 1445 if( pc<iFree ){ put2byte(pAddr, pc+sz); } 1446 else if( pc<iFree2 ){ put2byte(pAddr, pc+sz2); } 1447 } 1448 goto defragment_out; 1449 } 1450 } 1451 } 1452 1453 cbrk = usableSize; 1454 iCellLast = usableSize - 4; 1455 for(i=0; i<nCell; i++){ 1456 u8 *pAddr; /* The i-th cell pointer */ 1457 pAddr = &data[cellOffset + i*2]; 1458 pc = get2byte(pAddr); 1459 testcase( pc==iCellFirst ); 1460 testcase( pc==iCellLast ); 1461 /* These conditions have already been verified in btreeInitPage() 1462 ** if PRAGMA cell_size_check=ON. 1463 */ 1464 if( pc<iCellFirst || pc>iCellLast ){ 1465 return SQLITE_CORRUPT_PAGE(pPage); 1466 } 1467 assert( pc>=iCellFirst && pc<=iCellLast ); 1468 size = pPage->xCellSize(pPage, &src[pc]); 1469 cbrk -= size; 1470 if( cbrk<iCellFirst || pc+size>usableSize ){ 1471 return SQLITE_CORRUPT_PAGE(pPage); 1472 } 1473 assert( cbrk+size<=usableSize && cbrk>=iCellFirst ); 1474 testcase( cbrk+size==usableSize ); 1475 testcase( pc+size==usableSize ); 1476 put2byte(pAddr, cbrk); 1477 if( temp==0 ){ 1478 int x; 1479 if( cbrk==pc ) continue; 1480 temp = sqlite3PagerTempSpace(pPage->pBt->pPager); 1481 x = get2byte(&data[hdr+5]); 1482 memcpy(&temp[x], &data[x], (cbrk+size) - x); 1483 src = temp; 1484 } 1485 memcpy(&data[cbrk], &src[pc], size); 1486 } 1487 data[hdr+7] = 0; 1488 1489 defragment_out: 1490 if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){ 1491 return SQLITE_CORRUPT_PAGE(pPage); 1492 } 1493 assert( cbrk>=iCellFirst ); 1494 put2byte(&data[hdr+5], cbrk); 1495 data[hdr+1] = 0; 1496 data[hdr+2] = 0; 1497 memset(&data[iCellFirst], 0, cbrk-iCellFirst); 1498 assert( sqlite3PagerIswriteable(pPage->pDbPage) ); 1499 return SQLITE_OK; 1500 } 1501 1502 /* 1503 ** Search the free-list on page pPg for space to store a cell nByte bytes in 1504 ** size. If one can be found, return a pointer to the space and remove it 1505 ** from the free-list. 1506 ** 1507 ** If no suitable space can be found on the free-list, return NULL. 1508 ** 1509 ** This function may detect corruption within pPg. If corruption is 1510 ** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned. 1511 ** 1512 ** Slots on the free list that are between 1 and 3 bytes larger than nByte 1513 ** will be ignored if adding the extra space to the fragmentation count 1514 ** causes the fragmentation count to exceed 60. 1515 */ 1516 static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){ 1517 const int hdr = pPg->hdrOffset; 1518 u8 * const aData = pPg->aData; 1519 int iAddr = hdr + 1; 1520 int pc = get2byte(&aData[iAddr]); 1521 int x; 1522 int usableSize = pPg->pBt->usableSize; 1523 int size; /* Size of the free slot */ 1524 1525 assert( pc>0 ); 1526 while( pc<=usableSize-4 ){ 1527 /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each 1528 ** freeblock form a big-endian integer which is the size of the freeblock 1529 ** in bytes, including the 4-byte header. */ 1530 size = get2byte(&aData[pc+2]); 1531 if( (x = size - nByte)>=0 ){ 1532 testcase( x==4 ); 1533 testcase( x==3 ); 1534 if( size+pc > usableSize ){ 1535 *pRc = SQLITE_CORRUPT_PAGE(pPg); 1536 return 0; 1537 }else if( x<4 ){ 1538 /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total 1539 ** number of bytes in fragments may not exceed 60. */ 1540 if( aData[hdr+7]>57 ) return 0; 1541 1542 /* Remove the slot from the free-list. Update the number of 1543 ** fragmented bytes within the page. */ 1544 memcpy(&aData[iAddr], &aData[pc], 2); 1545 aData[hdr+7] += (u8)x; 1546 }else{ 1547 /* The slot remains on the free-list. Reduce its size to account 1548 ** for the portion used by the new allocation. */ 1549 put2byte(&aData[pc+2], x); 1550 } 1551 return &aData[pc + x]; 1552 } 1553 iAddr = pc; 1554 pc = get2byte(&aData[pc]); 1555 if( pc<iAddr+size ) break; 1556 } 1557 if( pc ){ 1558 *pRc = SQLITE_CORRUPT_PAGE(pPg); 1559 } 1560 1561 return 0; 1562 } 1563 1564 /* 1565 ** Allocate nByte bytes of space from within the B-Tree page passed 1566 ** as the first argument. Write into *pIdx the index into pPage->aData[] 1567 ** of the first byte of allocated space. Return either SQLITE_OK or 1568 ** an error code (usually SQLITE_CORRUPT). 1569 ** 1570 ** The caller guarantees that there is sufficient space to make the 1571 ** allocation. This routine might need to defragment in order to bring 1572 ** all the space together, however. This routine will avoid using 1573 ** the first two bytes past the cell pointer area since presumably this 1574 ** allocation is being made in order to insert a new cell, so we will 1575 ** also end up needing a new cell pointer. 1576 */ 1577 static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){ 1578 const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */ 1579 u8 * const data = pPage->aData; /* Local cache of pPage->aData */ 1580 int top; /* First byte of cell content area */ 1581 int rc = SQLITE_OK; /* Integer return code */ 1582 int gap; /* First byte of gap between cell pointers and cell content */ 1583 1584 assert( sqlite3PagerIswriteable(pPage->pDbPage) ); 1585 assert( pPage->pBt ); 1586 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 1587 assert( nByte>=0 ); /* Minimum cell size is 4 */ 1588 assert( pPage->nFree>=nByte ); 1589 assert( pPage->nOverflow==0 ); 1590 assert( nByte < (int)(pPage->pBt->usableSize-8) ); 1591 1592 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf ); 1593 gap = pPage->cellOffset + 2*pPage->nCell; 1594 assert( gap<=65536 ); 1595 /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size 1596 ** and the reserved space is zero (the usual value for reserved space) 1597 ** then the cell content offset of an empty page wants to be 65536. 1598 ** However, that integer is too large to be stored in a 2-byte unsigned 1599 ** integer, so a value of 0 is used in its place. */ 1600 top = get2byte(&data[hdr+5]); 1601 assert( top<=(int)pPage->pBt->usableSize ); /* Prevent by getAndInitPage() */ 1602 if( gap>top ){ 1603 if( top==0 && pPage->pBt->usableSize==65536 ){ 1604 top = 65536; 1605 }else{ 1606 return SQLITE_CORRUPT_PAGE(pPage); 1607 } 1608 } 1609 1610 /* If there is enough space between gap and top for one more cell pointer 1611 ** array entry offset, and if the freelist is not empty, then search the 1612 ** freelist looking for a free slot big enough to satisfy the request. 1613 */ 1614 testcase( gap+2==top ); 1615 testcase( gap+1==top ); 1616 testcase( gap==top ); 1617 if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){ 1618 u8 *pSpace = pageFindSlot(pPage, nByte, &rc); 1619 if( pSpace ){ 1620 assert( pSpace>=data && (pSpace - data)<65536 ); 1621 *pIdx = (int)(pSpace - data); 1622 return SQLITE_OK; 1623 }else if( rc ){ 1624 return rc; 1625 } 1626 } 1627 1628 /* The request could not be fulfilled using a freelist slot. Check 1629 ** to see if defragmentation is necessary. 1630 */ 1631 testcase( gap+2+nByte==top ); 1632 if( gap+2+nByte>top ){ 1633 assert( pPage->nCell>0 || CORRUPT_DB ); 1634 rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte))); 1635 if( rc ) return rc; 1636 top = get2byteNotZero(&data[hdr+5]); 1637 assert( gap+2+nByte<=top ); 1638 } 1639 1640 1641 /* Allocate memory from the gap in between the cell pointer array 1642 ** and the cell content area. The btreeInitPage() call has already 1643 ** validated the freelist. Given that the freelist is valid, there 1644 ** is no way that the allocation can extend off the end of the page. 1645 ** The assert() below verifies the previous sentence. 1646 */ 1647 top -= nByte; 1648 put2byte(&data[hdr+5], top); 1649 assert( top+nByte <= (int)pPage->pBt->usableSize ); 1650 *pIdx = top; 1651 return SQLITE_OK; 1652 } 1653 1654 /* 1655 ** Return a section of the pPage->aData to the freelist. 1656 ** The first byte of the new free block is pPage->aData[iStart] 1657 ** and the size of the block is iSize bytes. 1658 ** 1659 ** Adjacent freeblocks are coalesced. 1660 ** 1661 ** Note that even though the freeblock list was checked by btreeInitPage(), 1662 ** that routine will not detect overlap between cells or freeblocks. Nor 1663 ** does it detect cells or freeblocks that encrouch into the reserved bytes 1664 ** at the end of the page. So do additional corruption checks inside this 1665 ** routine and return SQLITE_CORRUPT if any problems are found. 1666 */ 1667 static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){ 1668 u16 iPtr; /* Address of ptr to next freeblock */ 1669 u16 iFreeBlk; /* Address of the next freeblock */ 1670 u8 hdr; /* Page header size. 0 or 100 */ 1671 u8 nFrag = 0; /* Reduction in fragmentation */ 1672 u16 iOrigSize = iSize; /* Original value of iSize */ 1673 u16 x; /* Offset to cell content area */ 1674 u32 iEnd = iStart + iSize; /* First byte past the iStart buffer */ 1675 unsigned char *data = pPage->aData; /* Page content */ 1676 1677 assert( pPage->pBt!=0 ); 1678 assert( sqlite3PagerIswriteable(pPage->pDbPage) ); 1679 assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize ); 1680 assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize ); 1681 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 1682 assert( iSize>=4 ); /* Minimum cell size is 4 */ 1683 assert( iStart<=pPage->pBt->usableSize-4 ); 1684 1685 /* The list of freeblocks must be in ascending order. Find the 1686 ** spot on the list where iStart should be inserted. 1687 */ 1688 hdr = pPage->hdrOffset; 1689 iPtr = hdr + 1; 1690 if( data[iPtr+1]==0 && data[iPtr]==0 ){ 1691 iFreeBlk = 0; /* Shortcut for the case when the freelist is empty */ 1692 }else{ 1693 while( (iFreeBlk = get2byte(&data[iPtr]))<iStart ){ 1694 if( iFreeBlk<iPtr+4 ){ 1695 if( iFreeBlk==0 ) break; 1696 return SQLITE_CORRUPT_PAGE(pPage); 1697 } 1698 iPtr = iFreeBlk; 1699 } 1700 if( iFreeBlk>pPage->pBt->usableSize-4 ){ 1701 return SQLITE_CORRUPT_PAGE(pPage); 1702 } 1703 assert( iFreeBlk>iPtr || iFreeBlk==0 ); 1704 1705 /* At this point: 1706 ** iFreeBlk: First freeblock after iStart, or zero if none 1707 ** iPtr: The address of a pointer to iFreeBlk 1708 ** 1709 ** Check to see if iFreeBlk should be coalesced onto the end of iStart. 1710 */ 1711 if( iFreeBlk && iEnd+3>=iFreeBlk ){ 1712 nFrag = iFreeBlk - iEnd; 1713 if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_PAGE(pPage); 1714 iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]); 1715 if( iEnd > pPage->pBt->usableSize ){ 1716 return SQLITE_CORRUPT_PAGE(pPage); 1717 } 1718 iSize = iEnd - iStart; 1719 iFreeBlk = get2byte(&data[iFreeBlk]); 1720 } 1721 1722 /* If iPtr is another freeblock (that is, if iPtr is not the freelist 1723 ** pointer in the page header) then check to see if iStart should be 1724 ** coalesced onto the end of iPtr. 1725 */ 1726 if( iPtr>hdr+1 ){ 1727 int iPtrEnd = iPtr + get2byte(&data[iPtr+2]); 1728 if( iPtrEnd+3>=iStart ){ 1729 if( iPtrEnd>iStart ) return SQLITE_CORRUPT_PAGE(pPage); 1730 nFrag += iStart - iPtrEnd; 1731 iSize = iEnd - iPtr; 1732 iStart = iPtr; 1733 } 1734 } 1735 if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_PAGE(pPage); 1736 data[hdr+7] -= nFrag; 1737 } 1738 x = get2byte(&data[hdr+5]); 1739 if( iStart<=x ){ 1740 /* The new freeblock is at the beginning of the cell content area, 1741 ** so just extend the cell content area rather than create another 1742 ** freelist entry */ 1743 if( iStart<x || iPtr!=hdr+1 ) return SQLITE_CORRUPT_PAGE(pPage); 1744 put2byte(&data[hdr+1], iFreeBlk); 1745 put2byte(&data[hdr+5], iEnd); 1746 }else{ 1747 /* Insert the new freeblock into the freelist */ 1748 put2byte(&data[iPtr], iStart); 1749 } 1750 if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){ 1751 /* Overwrite deleted information with zeros when the secure_delete 1752 ** option is enabled */ 1753 memset(&data[iStart], 0, iSize); 1754 } 1755 put2byte(&data[iStart], iFreeBlk); 1756 put2byte(&data[iStart+2], iSize); 1757 pPage->nFree += iOrigSize; 1758 return SQLITE_OK; 1759 } 1760 1761 /* 1762 ** Decode the flags byte (the first byte of the header) for a page 1763 ** and initialize fields of the MemPage structure accordingly. 1764 ** 1765 ** Only the following combinations are supported. Anything different 1766 ** indicates a corrupt database files: 1767 ** 1768 ** PTF_ZERODATA 1769 ** PTF_ZERODATA | PTF_LEAF 1770 ** PTF_LEAFDATA | PTF_INTKEY 1771 ** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF 1772 */ 1773 static int decodeFlags(MemPage *pPage, int flagByte){ 1774 BtShared *pBt; /* A copy of pPage->pBt */ 1775 1776 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) ); 1777 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 1778 pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 ); 1779 flagByte &= ~PTF_LEAF; 1780 pPage->childPtrSize = 4-4*pPage->leaf; 1781 pPage->xCellSize = cellSizePtr; 1782 pBt = pPage->pBt; 1783 if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){ 1784 /* EVIDENCE-OF: R-07291-35328 A value of 5 (0x05) means the page is an 1785 ** interior table b-tree page. */ 1786 assert( (PTF_LEAFDATA|PTF_INTKEY)==5 ); 1787 /* EVIDENCE-OF: R-26900-09176 A value of 13 (0x0d) means the page is a 1788 ** leaf table b-tree page. */ 1789 assert( (PTF_LEAFDATA|PTF_INTKEY|PTF_LEAF)==13 ); 1790 pPage->intKey = 1; 1791 if( pPage->leaf ){ 1792 pPage->intKeyLeaf = 1; 1793 pPage->xParseCell = btreeParseCellPtr; 1794 }else{ 1795 pPage->intKeyLeaf = 0; 1796 pPage->xCellSize = cellSizePtrNoPayload; 1797 pPage->xParseCell = btreeParseCellPtrNoPayload; 1798 } 1799 pPage->maxLocal = pBt->maxLeaf; 1800 pPage->minLocal = pBt->minLeaf; 1801 }else if( flagByte==PTF_ZERODATA ){ 1802 /* EVIDENCE-OF: R-43316-37308 A value of 2 (0x02) means the page is an 1803 ** interior index b-tree page. */ 1804 assert( (PTF_ZERODATA)==2 ); 1805 /* EVIDENCE-OF: R-59615-42828 A value of 10 (0x0a) means the page is a 1806 ** leaf index b-tree page. */ 1807 assert( (PTF_ZERODATA|PTF_LEAF)==10 ); 1808 pPage->intKey = 0; 1809 pPage->intKeyLeaf = 0; 1810 pPage->xParseCell = btreeParseCellPtrIndex; 1811 pPage->maxLocal = pBt->maxLocal; 1812 pPage->minLocal = pBt->minLocal; 1813 }else{ 1814 /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is 1815 ** an error. */ 1816 return SQLITE_CORRUPT_PAGE(pPage); 1817 } 1818 pPage->max1bytePayload = pBt->max1bytePayload; 1819 return SQLITE_OK; 1820 } 1821 1822 /* 1823 ** Initialize the auxiliary information for a disk block. 1824 ** 1825 ** Return SQLITE_OK on success. If we see that the page does 1826 ** not contain a well-formed database page, then return 1827 ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not 1828 ** guarantee that the page is well-formed. It only shows that 1829 ** we failed to detect any corruption. 1830 */ 1831 static int btreeInitPage(MemPage *pPage){ 1832 int pc; /* Address of a freeblock within pPage->aData[] */ 1833 u8 hdr; /* Offset to beginning of page header */ 1834 u8 *data; /* Equal to pPage->aData */ 1835 BtShared *pBt; /* The main btree structure */ 1836 int usableSize; /* Amount of usable space on each page */ 1837 u16 cellOffset; /* Offset from start of page to first cell pointer */ 1838 int nFree; /* Number of unused bytes on the page */ 1839 int top; /* First byte of the cell content area */ 1840 int iCellFirst; /* First allowable cell or freeblock offset */ 1841 int iCellLast; /* Last possible cell or freeblock offset */ 1842 1843 assert( pPage->pBt!=0 ); 1844 assert( pPage->pBt->db!=0 ); 1845 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 1846 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); 1847 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) ); 1848 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) ); 1849 assert( pPage->isInit==0 ); 1850 1851 pBt = pPage->pBt; 1852 hdr = pPage->hdrOffset; 1853 data = pPage->aData; 1854 /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating 1855 ** the b-tree page type. */ 1856 if( decodeFlags(pPage, data[hdr]) ){ 1857 return SQLITE_CORRUPT_PAGE(pPage); 1858 } 1859 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 ); 1860 pPage->maskPage = (u16)(pBt->pageSize - 1); 1861 pPage->nOverflow = 0; 1862 usableSize = pBt->usableSize; 1863 pPage->cellOffset = cellOffset = hdr + 8 + pPage->childPtrSize; 1864 pPage->aDataEnd = &data[usableSize]; 1865 pPage->aCellIdx = &data[cellOffset]; 1866 pPage->aDataOfst = &data[pPage->childPtrSize]; 1867 /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates 1868 ** the start of the cell content area. A zero value for this integer is 1869 ** interpreted as 65536. */ 1870 top = get2byteNotZero(&data[hdr+5]); 1871 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the 1872 ** number of cells on the page. */ 1873 pPage->nCell = get2byte(&data[hdr+3]); 1874 if( pPage->nCell>MX_CELL(pBt) ){ 1875 /* To many cells for a single page. The page must be corrupt */ 1876 return SQLITE_CORRUPT_PAGE(pPage); 1877 } 1878 testcase( pPage->nCell==MX_CELL(pBt) ); 1879 /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only 1880 ** possible for a root page of a table that contains no rows) then the 1881 ** offset to the cell content area will equal the page size minus the 1882 ** bytes of reserved space. */ 1883 assert( pPage->nCell>0 || top==usableSize || CORRUPT_DB ); 1884 1885 /* A malformed database page might cause us to read past the end 1886 ** of page when parsing a cell. 1887 ** 1888 ** The following block of code checks early to see if a cell extends 1889 ** past the end of a page boundary and causes SQLITE_CORRUPT to be 1890 ** returned if it does. 1891 */ 1892 iCellFirst = cellOffset + 2*pPage->nCell; 1893 iCellLast = usableSize - 4; 1894 if( pBt->db->flags & SQLITE_CellSizeCk ){ 1895 int i; /* Index into the cell pointer array */ 1896 int sz; /* Size of a cell */ 1897 1898 if( !pPage->leaf ) iCellLast--; 1899 for(i=0; i<pPage->nCell; i++){ 1900 pc = get2byteAligned(&data[cellOffset+i*2]); 1901 testcase( pc==iCellFirst ); 1902 testcase( pc==iCellLast ); 1903 if( pc<iCellFirst || pc>iCellLast ){ 1904 return SQLITE_CORRUPT_PAGE(pPage); 1905 } 1906 sz = pPage->xCellSize(pPage, &data[pc]); 1907 testcase( pc+sz==usableSize ); 1908 if( pc+sz>usableSize ){ 1909 return SQLITE_CORRUPT_PAGE(pPage); 1910 } 1911 } 1912 if( !pPage->leaf ) iCellLast++; 1913 } 1914 1915 /* Compute the total free space on the page 1916 ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the 1917 ** start of the first freeblock on the page, or is zero if there are no 1918 ** freeblocks. */ 1919 pc = get2byte(&data[hdr+1]); 1920 nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */ 1921 if( pc>0 ){ 1922 u32 next, size; 1923 if( pc<iCellFirst ){ 1924 /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will 1925 ** always be at least one cell before the first freeblock. 1926 */ 1927 return SQLITE_CORRUPT_PAGE(pPage); 1928 } 1929 while( 1 ){ 1930 if( pc>iCellLast ){ 1931 /* Freeblock off the end of the page */ 1932 return SQLITE_CORRUPT_PAGE(pPage); 1933 } 1934 next = get2byte(&data[pc]); 1935 size = get2byte(&data[pc+2]); 1936 nFree = nFree + size; 1937 if( next<=pc+size+3 ) break; 1938 pc = next; 1939 } 1940 if( next>0 ){ 1941 /* Freeblock not in ascending order */ 1942 return SQLITE_CORRUPT_PAGE(pPage); 1943 } 1944 if( pc+size>(unsigned int)usableSize ){ 1945 /* Last freeblock extends past page end */ 1946 return SQLITE_CORRUPT_PAGE(pPage); 1947 } 1948 } 1949 1950 /* At this point, nFree contains the sum of the offset to the start 1951 ** of the cell-content area plus the number of free bytes within 1952 ** the cell-content area. If this is greater than the usable-size 1953 ** of the page, then the page must be corrupted. This check also 1954 ** serves to verify that the offset to the start of the cell-content 1955 ** area, according to the page header, lies within the page. 1956 */ 1957 if( nFree>usableSize ){ 1958 return SQLITE_CORRUPT_PAGE(pPage); 1959 } 1960 pPage->nFree = (u16)(nFree - iCellFirst); 1961 pPage->isInit = 1; 1962 return SQLITE_OK; 1963 } 1964 1965 /* 1966 ** Set up a raw page so that it looks like a database page holding 1967 ** no entries. 1968 */ 1969 static void zeroPage(MemPage *pPage, int flags){ 1970 unsigned char *data = pPage->aData; 1971 BtShared *pBt = pPage->pBt; 1972 u8 hdr = pPage->hdrOffset; 1973 u16 first; 1974 1975 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno ); 1976 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); 1977 assert( sqlite3PagerGetData(pPage->pDbPage) == data ); 1978 assert( sqlite3PagerIswriteable(pPage->pDbPage) ); 1979 assert( sqlite3_mutex_held(pBt->mutex) ); 1980 if( pBt->btsFlags & BTS_FAST_SECURE ){ 1981 memset(&data[hdr], 0, pBt->usableSize - hdr); 1982 } 1983 data[hdr] = (char)flags; 1984 first = hdr + ((flags&PTF_LEAF)==0 ? 12 : 8); 1985 memset(&data[hdr+1], 0, 4); 1986 data[hdr+7] = 0; 1987 put2byte(&data[hdr+5], pBt->usableSize); 1988 pPage->nFree = (u16)(pBt->usableSize - first); 1989 decodeFlags(pPage, flags); 1990 pPage->cellOffset = first; 1991 pPage->aDataEnd = &data[pBt->usableSize]; 1992 pPage->aCellIdx = &data[first]; 1993 pPage->aDataOfst = &data[pPage->childPtrSize]; 1994 pPage->nOverflow = 0; 1995 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 ); 1996 pPage->maskPage = (u16)(pBt->pageSize - 1); 1997 pPage->nCell = 0; 1998 pPage->isInit = 1; 1999 } 2000 2001 2002 /* 2003 ** Convert a DbPage obtained from the pager into a MemPage used by 2004 ** the btree layer. 2005 */ 2006 static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){ 2007 MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage); 2008 if( pgno!=pPage->pgno ){ 2009 pPage->aData = sqlite3PagerGetData(pDbPage); 2010 pPage->pDbPage = pDbPage; 2011 pPage->pBt = pBt; 2012 pPage->pgno = pgno; 2013 pPage->hdrOffset = pgno==1 ? 100 : 0; 2014 } 2015 assert( pPage->aData==sqlite3PagerGetData(pDbPage) ); 2016 return pPage; 2017 } 2018 2019 /* 2020 ** Get a page from the pager. Initialize the MemPage.pBt and 2021 ** MemPage.aData elements if needed. See also: btreeGetUnusedPage(). 2022 ** 2023 ** If the PAGER_GET_NOCONTENT flag is set, it means that we do not care 2024 ** about the content of the page at this time. So do not go to the disk 2025 ** to fetch the content. Just fill in the content with zeros for now. 2026 ** If in the future we call sqlite3PagerWrite() on this page, that 2027 ** means we have started to be concerned about content and the disk 2028 ** read should occur at that point. 2029 */ 2030 static int btreeGetPage( 2031 BtShared *pBt, /* The btree */ 2032 Pgno pgno, /* Number of the page to fetch */ 2033 MemPage **ppPage, /* Return the page in this parameter */ 2034 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */ 2035 ){ 2036 int rc; 2037 DbPage *pDbPage; 2038 2039 assert( flags==0 || flags==PAGER_GET_NOCONTENT || flags==PAGER_GET_READONLY ); 2040 assert( sqlite3_mutex_held(pBt->mutex) ); 2041 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags); 2042 if( rc ) return rc; 2043 *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt); 2044 return SQLITE_OK; 2045 } 2046 2047 /* 2048 ** Retrieve a page from the pager cache. If the requested page is not 2049 ** already in the pager cache return NULL. Initialize the MemPage.pBt and 2050 ** MemPage.aData elements if needed. 2051 */ 2052 static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){ 2053 DbPage *pDbPage; 2054 assert( sqlite3_mutex_held(pBt->mutex) ); 2055 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno); 2056 if( pDbPage ){ 2057 return btreePageFromDbPage(pDbPage, pgno, pBt); 2058 } 2059 return 0; 2060 } 2061 2062 /* 2063 ** Return the size of the database file in pages. If there is any kind of 2064 ** error, return ((unsigned int)-1). 2065 */ 2066 static Pgno btreePagecount(BtShared *pBt){ 2067 return pBt->nPage; 2068 } 2069 u32 sqlite3BtreeLastPage(Btree *p){ 2070 assert( sqlite3BtreeHoldsMutex(p) ); 2071 assert( ((p->pBt->nPage)&0x80000000)==0 ); 2072 return btreePagecount(p->pBt); 2073 } 2074 2075 /* 2076 ** Get a page from the pager and initialize it. 2077 ** 2078 ** If pCur!=0 then the page is being fetched as part of a moveToChild() 2079 ** call. Do additional sanity checking on the page in this case. 2080 ** And if the fetch fails, this routine must decrement pCur->iPage. 2081 ** 2082 ** The page is fetched as read-write unless pCur is not NULL and is 2083 ** a read-only cursor. 2084 ** 2085 ** If an error occurs, then *ppPage is undefined. It 2086 ** may remain unchanged, or it may be set to an invalid value. 2087 */ 2088 static int getAndInitPage( 2089 BtShared *pBt, /* The database file */ 2090 Pgno pgno, /* Number of the page to get */ 2091 MemPage **ppPage, /* Write the page pointer here */ 2092 BtCursor *pCur, /* Cursor to receive the page, or NULL */ 2093 int bReadOnly /* True for a read-only page */ 2094 ){ 2095 int rc; 2096 DbPage *pDbPage; 2097 assert( sqlite3_mutex_held(pBt->mutex) ); 2098 assert( pCur==0 || ppPage==&pCur->pPage ); 2099 assert( pCur==0 || bReadOnly==pCur->curPagerFlags ); 2100 assert( pCur==0 || pCur->iPage>0 ); 2101 2102 if( pgno>btreePagecount(pBt) ){ 2103 rc = SQLITE_CORRUPT_BKPT; 2104 goto getAndInitPage_error; 2105 } 2106 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly); 2107 if( rc ){ 2108 goto getAndInitPage_error; 2109 } 2110 *ppPage = (MemPage*)sqlite3PagerGetExtra(pDbPage); 2111 if( (*ppPage)->isInit==0 ){ 2112 btreePageFromDbPage(pDbPage, pgno, pBt); 2113 rc = btreeInitPage(*ppPage); 2114 if( rc!=SQLITE_OK ){ 2115 releasePage(*ppPage); 2116 goto getAndInitPage_error; 2117 } 2118 } 2119 assert( (*ppPage)->pgno==pgno ); 2120 assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) ); 2121 2122 /* If obtaining a child page for a cursor, we must verify that the page is 2123 ** compatible with the root page. */ 2124 if( pCur && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) ){ 2125 rc = SQLITE_CORRUPT_PGNO(pgno); 2126 releasePage(*ppPage); 2127 goto getAndInitPage_error; 2128 } 2129 return SQLITE_OK; 2130 2131 getAndInitPage_error: 2132 if( pCur ){ 2133 pCur->iPage--; 2134 pCur->pPage = pCur->apPage[pCur->iPage]; 2135 } 2136 testcase( pgno==0 ); 2137 assert( pgno!=0 || rc==SQLITE_CORRUPT ); 2138 return rc; 2139 } 2140 2141 /* 2142 ** Release a MemPage. This should be called once for each prior 2143 ** call to btreeGetPage. 2144 ** 2145 ** Page1 is a special case and must be released using releasePageOne(). 2146 */ 2147 static void releasePageNotNull(MemPage *pPage){ 2148 assert( pPage->aData ); 2149 assert( pPage->pBt ); 2150 assert( pPage->pDbPage!=0 ); 2151 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); 2152 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData ); 2153 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 2154 sqlite3PagerUnrefNotNull(pPage->pDbPage); 2155 } 2156 static void releasePage(MemPage *pPage){ 2157 if( pPage ) releasePageNotNull(pPage); 2158 } 2159 static void releasePageOne(MemPage *pPage){ 2160 assert( pPage!=0 ); 2161 assert( pPage->aData ); 2162 assert( pPage->pBt ); 2163 assert( pPage->pDbPage!=0 ); 2164 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); 2165 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData ); 2166 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 2167 sqlite3PagerUnrefPageOne(pPage->pDbPage); 2168 } 2169 2170 /* 2171 ** Get an unused page. 2172 ** 2173 ** This works just like btreeGetPage() with the addition: 2174 ** 2175 ** * If the page is already in use for some other purpose, immediately 2176 ** release it and return an SQLITE_CURRUPT error. 2177 ** * Make sure the isInit flag is clear 2178 */ 2179 static int btreeGetUnusedPage( 2180 BtShared *pBt, /* The btree */ 2181 Pgno pgno, /* Number of the page to fetch */ 2182 MemPage **ppPage, /* Return the page in this parameter */ 2183 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */ 2184 ){ 2185 int rc = btreeGetPage(pBt, pgno, ppPage, flags); 2186 if( rc==SQLITE_OK ){ 2187 if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){ 2188 releasePage(*ppPage); 2189 *ppPage = 0; 2190 return SQLITE_CORRUPT_BKPT; 2191 } 2192 (*ppPage)->isInit = 0; 2193 }else{ 2194 *ppPage = 0; 2195 } 2196 return rc; 2197 } 2198 2199 2200 /* 2201 ** During a rollback, when the pager reloads information into the cache 2202 ** so that the cache is restored to its original state at the start of 2203 ** the transaction, for each page restored this routine is called. 2204 ** 2205 ** This routine needs to reset the extra data section at the end of the 2206 ** page to agree with the restored data. 2207 */ 2208 static void pageReinit(DbPage *pData){ 2209 MemPage *pPage; 2210 pPage = (MemPage *)sqlite3PagerGetExtra(pData); 2211 assert( sqlite3PagerPageRefcount(pData)>0 ); 2212 if( pPage->isInit ){ 2213 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 2214 pPage->isInit = 0; 2215 if( sqlite3PagerPageRefcount(pData)>1 ){ 2216 /* pPage might not be a btree page; it might be an overflow page 2217 ** or ptrmap page or a free page. In those cases, the following 2218 ** call to btreeInitPage() will likely return SQLITE_CORRUPT. 2219 ** But no harm is done by this. And it is very important that 2220 ** btreeInitPage() be called on every btree page so we make 2221 ** the call for every page that comes in for re-initing. */ 2222 btreeInitPage(pPage); 2223 } 2224 } 2225 } 2226 2227 /* 2228 ** Invoke the busy handler for a btree. 2229 */ 2230 static int btreeInvokeBusyHandler(void *pArg){ 2231 BtShared *pBt = (BtShared*)pArg; 2232 assert( pBt->db ); 2233 assert( sqlite3_mutex_held(pBt->db->mutex) ); 2234 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler, 2235 sqlite3PagerFile(pBt->pPager)); 2236 } 2237 2238 /* 2239 ** Open a database file. 2240 ** 2241 ** zFilename is the name of the database file. If zFilename is NULL 2242 ** then an ephemeral database is created. The ephemeral database might 2243 ** be exclusively in memory, or it might use a disk-based memory cache. 2244 ** Either way, the ephemeral database will be automatically deleted 2245 ** when sqlite3BtreeClose() is called. 2246 ** 2247 ** If zFilename is ":memory:" then an in-memory database is created 2248 ** that is automatically destroyed when it is closed. 2249 ** 2250 ** The "flags" parameter is a bitmask that might contain bits like 2251 ** BTREE_OMIT_JOURNAL and/or BTREE_MEMORY. 2252 ** 2253 ** If the database is already opened in the same database connection 2254 ** and we are in shared cache mode, then the open will fail with an 2255 ** SQLITE_CONSTRAINT error. We cannot allow two or more BtShared 2256 ** objects in the same database connection since doing so will lead 2257 ** to problems with locking. 2258 */ 2259 int sqlite3BtreeOpen( 2260 sqlite3_vfs *pVfs, /* VFS to use for this b-tree */ 2261 const char *zFilename, /* Name of the file containing the BTree database */ 2262 sqlite3 *db, /* Associated database handle */ 2263 Btree **ppBtree, /* Pointer to new Btree object written here */ 2264 int flags, /* Options */ 2265 int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */ 2266 ){ 2267 BtShared *pBt = 0; /* Shared part of btree structure */ 2268 Btree *p; /* Handle to return */ 2269 sqlite3_mutex *mutexOpen = 0; /* Prevents a race condition. Ticket #3537 */ 2270 int rc = SQLITE_OK; /* Result code from this function */ 2271 u8 nReserve; /* Byte of unused space on each page */ 2272 unsigned char zDbHeader[100]; /* Database header content */ 2273 2274 /* True if opening an ephemeral, temporary database */ 2275 const int isTempDb = zFilename==0 || zFilename[0]==0; 2276 2277 /* Set the variable isMemdb to true for an in-memory database, or 2278 ** false for a file-based database. 2279 */ 2280 #ifdef SQLITE_OMIT_MEMORYDB 2281 const int isMemdb = 0; 2282 #else 2283 const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0) 2284 || (isTempDb && sqlite3TempInMemory(db)) 2285 || (vfsFlags & SQLITE_OPEN_MEMORY)!=0; 2286 #endif 2287 2288 assert( db!=0 ); 2289 assert( pVfs!=0 ); 2290 assert( sqlite3_mutex_held(db->mutex) ); 2291 assert( (flags&0xff)==flags ); /* flags fit in 8 bits */ 2292 2293 /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */ 2294 assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 ); 2295 2296 /* A BTREE_SINGLE database is always a temporary and/or ephemeral */ 2297 assert( (flags & BTREE_SINGLE)==0 || isTempDb ); 2298 2299 if( isMemdb ){ 2300 flags |= BTREE_MEMORY; 2301 } 2302 if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){ 2303 vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB; 2304 } 2305 p = sqlite3MallocZero(sizeof(Btree)); 2306 if( !p ){ 2307 return SQLITE_NOMEM_BKPT; 2308 } 2309 p->inTrans = TRANS_NONE; 2310 p->db = db; 2311 #ifndef SQLITE_OMIT_SHARED_CACHE 2312 p->lock.pBtree = p; 2313 p->lock.iTable = 1; 2314 #endif 2315 2316 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) 2317 /* 2318 ** If this Btree is a candidate for shared cache, try to find an 2319 ** existing BtShared object that we can share with 2320 */ 2321 if( isTempDb==0 && (isMemdb==0 || (vfsFlags&SQLITE_OPEN_URI)!=0) ){ 2322 if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){ 2323 int nFilename = sqlite3Strlen30(zFilename)+1; 2324 int nFullPathname = pVfs->mxPathname+1; 2325 char *zFullPathname = sqlite3Malloc(MAX(nFullPathname,nFilename)); 2326 MUTEX_LOGIC( sqlite3_mutex *mutexShared; ) 2327 2328 p->sharable = 1; 2329 if( !zFullPathname ){ 2330 sqlite3_free(p); 2331 return SQLITE_NOMEM_BKPT; 2332 } 2333 if( isMemdb ){ 2334 memcpy(zFullPathname, zFilename, nFilename); 2335 }else{ 2336 rc = sqlite3OsFullPathname(pVfs, zFilename, 2337 nFullPathname, zFullPathname); 2338 if( rc ){ 2339 sqlite3_free(zFullPathname); 2340 sqlite3_free(p); 2341 return rc; 2342 } 2343 } 2344 #if SQLITE_THREADSAFE 2345 mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN); 2346 sqlite3_mutex_enter(mutexOpen); 2347 mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); 2348 sqlite3_mutex_enter(mutexShared); 2349 #endif 2350 for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){ 2351 assert( pBt->nRef>0 ); 2352 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0)) 2353 && sqlite3PagerVfs(pBt->pPager)==pVfs ){ 2354 int iDb; 2355 for(iDb=db->nDb-1; iDb>=0; iDb--){ 2356 Btree *pExisting = db->aDb[iDb].pBt; 2357 if( pExisting && pExisting->pBt==pBt ){ 2358 sqlite3_mutex_leave(mutexShared); 2359 sqlite3_mutex_leave(mutexOpen); 2360 sqlite3_free(zFullPathname); 2361 sqlite3_free(p); 2362 return SQLITE_CONSTRAINT; 2363 } 2364 } 2365 p->pBt = pBt; 2366 pBt->nRef++; 2367 break; 2368 } 2369 } 2370 sqlite3_mutex_leave(mutexShared); 2371 sqlite3_free(zFullPathname); 2372 } 2373 #ifdef SQLITE_DEBUG 2374 else{ 2375 /* In debug mode, we mark all persistent databases as sharable 2376 ** even when they are not. This exercises the locking code and 2377 ** gives more opportunity for asserts(sqlite3_mutex_held()) 2378 ** statements to find locking problems. 2379 */ 2380 p->sharable = 1; 2381 } 2382 #endif 2383 } 2384 #endif 2385 if( pBt==0 ){ 2386 /* 2387 ** The following asserts make sure that structures used by the btree are 2388 ** the right size. This is to guard against size changes that result 2389 ** when compiling on a different architecture. 2390 */ 2391 assert( sizeof(i64)==8 ); 2392 assert( sizeof(u64)==8 ); 2393 assert( sizeof(u32)==4 ); 2394 assert( sizeof(u16)==2 ); 2395 assert( sizeof(Pgno)==4 ); 2396 2397 pBt = sqlite3MallocZero( sizeof(*pBt) ); 2398 if( pBt==0 ){ 2399 rc = SQLITE_NOMEM_BKPT; 2400 goto btree_open_out; 2401 } 2402 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename, 2403 sizeof(MemPage), flags, vfsFlags, pageReinit); 2404 if( rc==SQLITE_OK ){ 2405 sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap); 2406 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader); 2407 } 2408 if( rc!=SQLITE_OK ){ 2409 goto btree_open_out; 2410 } 2411 pBt->openFlags = (u8)flags; 2412 pBt->db = db; 2413 sqlite3PagerSetBusyHandler(pBt->pPager, btreeInvokeBusyHandler, pBt); 2414 p->pBt = pBt; 2415 2416 pBt->pCursor = 0; 2417 pBt->pPage1 = 0; 2418 if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY; 2419 #if defined(SQLITE_SECURE_DELETE) 2420 pBt->btsFlags |= BTS_SECURE_DELETE; 2421 #elif defined(SQLITE_FAST_SECURE_DELETE) 2422 pBt->btsFlags |= BTS_OVERWRITE; 2423 #endif 2424 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is 2425 ** determined by the 2-byte integer located at an offset of 16 bytes from 2426 ** the beginning of the database file. */ 2427 pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16); 2428 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE 2429 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){ 2430 pBt->pageSize = 0; 2431 #ifndef SQLITE_OMIT_AUTOVACUUM 2432 /* If the magic name ":memory:" will create an in-memory database, then 2433 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if 2434 ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if 2435 ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a 2436 ** regular file-name. In this case the auto-vacuum applies as per normal. 2437 */ 2438 if( zFilename && !isMemdb ){ 2439 pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0); 2440 pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0); 2441 } 2442 #endif 2443 nReserve = 0; 2444 }else{ 2445 /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is 2446 ** determined by the one-byte unsigned integer found at an offset of 20 2447 ** into the database file header. */ 2448 nReserve = zDbHeader[20]; 2449 pBt->btsFlags |= BTS_PAGESIZE_FIXED; 2450 #ifndef SQLITE_OMIT_AUTOVACUUM 2451 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0); 2452 pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0); 2453 #endif 2454 } 2455 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve); 2456 if( rc ) goto btree_open_out; 2457 pBt->usableSize = pBt->pageSize - nReserve; 2458 assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */ 2459 2460 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) 2461 /* Add the new BtShared object to the linked list sharable BtShareds. 2462 */ 2463 pBt->nRef = 1; 2464 if( p->sharable ){ 2465 MUTEX_LOGIC( sqlite3_mutex *mutexShared; ) 2466 MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER);) 2467 if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){ 2468 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST); 2469 if( pBt->mutex==0 ){ 2470 rc = SQLITE_NOMEM_BKPT; 2471 goto btree_open_out; 2472 } 2473 } 2474 sqlite3_mutex_enter(mutexShared); 2475 pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList); 2476 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt; 2477 sqlite3_mutex_leave(mutexShared); 2478 } 2479 #endif 2480 } 2481 2482 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) 2483 /* If the new Btree uses a sharable pBtShared, then link the new 2484 ** Btree into the list of all sharable Btrees for the same connection. 2485 ** The list is kept in ascending order by pBt address. 2486 */ 2487 if( p->sharable ){ 2488 int i; 2489 Btree *pSib; 2490 for(i=0; i<db->nDb; i++){ 2491 if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){ 2492 while( pSib->pPrev ){ pSib = pSib->pPrev; } 2493 if( (uptr)p->pBt<(uptr)pSib->pBt ){ 2494 p->pNext = pSib; 2495 p->pPrev = 0; 2496 pSib->pPrev = p; 2497 }else{ 2498 while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){ 2499 pSib = pSib->pNext; 2500 } 2501 p->pNext = pSib->pNext; 2502 p->pPrev = pSib; 2503 if( p->pNext ){ 2504 p->pNext->pPrev = p; 2505 } 2506 pSib->pNext = p; 2507 } 2508 break; 2509 } 2510 } 2511 } 2512 #endif 2513 *ppBtree = p; 2514 2515 btree_open_out: 2516 if( rc!=SQLITE_OK ){ 2517 if( pBt && pBt->pPager ){ 2518 sqlite3PagerClose(pBt->pPager, 0); 2519 } 2520 sqlite3_free(pBt); 2521 sqlite3_free(p); 2522 *ppBtree = 0; 2523 }else{ 2524 sqlite3_file *pFile; 2525 2526 /* If the B-Tree was successfully opened, set the pager-cache size to the 2527 ** default value. Except, when opening on an existing shared pager-cache, 2528 ** do not change the pager-cache size. 2529 */ 2530 if( sqlite3BtreeSchema(p, 0, 0)==0 ){ 2531 sqlite3PagerSetCachesize(p->pBt->pPager, SQLITE_DEFAULT_CACHE_SIZE); 2532 } 2533 2534 pFile = sqlite3PagerFile(pBt->pPager); 2535 if( pFile->pMethods ){ 2536 sqlite3OsFileControlHint(pFile, SQLITE_FCNTL_PDB, (void*)&pBt->db); 2537 } 2538 } 2539 if( mutexOpen ){ 2540 assert( sqlite3_mutex_held(mutexOpen) ); 2541 sqlite3_mutex_leave(mutexOpen); 2542 } 2543 assert( rc!=SQLITE_OK || sqlite3BtreeConnectionCount(*ppBtree)>0 ); 2544 return rc; 2545 } 2546 2547 /* 2548 ** Decrement the BtShared.nRef counter. When it reaches zero, 2549 ** remove the BtShared structure from the sharing list. Return 2550 ** true if the BtShared.nRef counter reaches zero and return 2551 ** false if it is still positive. 2552 */ 2553 static int removeFromSharingList(BtShared *pBt){ 2554 #ifndef SQLITE_OMIT_SHARED_CACHE 2555 MUTEX_LOGIC( sqlite3_mutex *pMaster; ) 2556 BtShared *pList; 2557 int removed = 0; 2558 2559 assert( sqlite3_mutex_notheld(pBt->mutex) ); 2560 MUTEX_LOGIC( pMaster = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MASTER); ) 2561 sqlite3_mutex_enter(pMaster); 2562 pBt->nRef--; 2563 if( pBt->nRef<=0 ){ 2564 if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){ 2565 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext; 2566 }else{ 2567 pList = GLOBAL(BtShared*,sqlite3SharedCacheList); 2568 while( ALWAYS(pList) && pList->pNext!=pBt ){ 2569 pList=pList->pNext; 2570 } 2571 if( ALWAYS(pList) ){ 2572 pList->pNext = pBt->pNext; 2573 } 2574 } 2575 if( SQLITE_THREADSAFE ){ 2576 sqlite3_mutex_free(pBt->mutex); 2577 } 2578 removed = 1; 2579 } 2580 sqlite3_mutex_leave(pMaster); 2581 return removed; 2582 #else 2583 return 1; 2584 #endif 2585 } 2586 2587 /* 2588 ** Make sure pBt->pTmpSpace points to an allocation of 2589 ** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child 2590 ** pointer. 2591 */ 2592 static void allocateTempSpace(BtShared *pBt){ 2593 if( !pBt->pTmpSpace ){ 2594 pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize ); 2595 2596 /* One of the uses of pBt->pTmpSpace is to format cells before 2597 ** inserting them into a leaf page (function fillInCell()). If 2598 ** a cell is less than 4 bytes in size, it is rounded up to 4 bytes 2599 ** by the various routines that manipulate binary cells. Which 2600 ** can mean that fillInCell() only initializes the first 2 or 3 2601 ** bytes of pTmpSpace, but that the first 4 bytes are copied from 2602 ** it into a database page. This is not actually a problem, but it 2603 ** does cause a valgrind error when the 1 or 2 bytes of unitialized 2604 ** data is passed to system call write(). So to avoid this error, 2605 ** zero the first 4 bytes of temp space here. 2606 ** 2607 ** Also: Provide four bytes of initialized space before the 2608 ** beginning of pTmpSpace as an area available to prepend the 2609 ** left-child pointer to the beginning of a cell. 2610 */ 2611 if( pBt->pTmpSpace ){ 2612 memset(pBt->pTmpSpace, 0, 8); 2613 pBt->pTmpSpace += 4; 2614 } 2615 } 2616 } 2617 2618 /* 2619 ** Free the pBt->pTmpSpace allocation 2620 */ 2621 static void freeTempSpace(BtShared *pBt){ 2622 if( pBt->pTmpSpace ){ 2623 pBt->pTmpSpace -= 4; 2624 sqlite3PageFree(pBt->pTmpSpace); 2625 pBt->pTmpSpace = 0; 2626 } 2627 } 2628 2629 /* 2630 ** Close an open database and invalidate all cursors. 2631 */ 2632 int sqlite3BtreeClose(Btree *p){ 2633 BtShared *pBt = p->pBt; 2634 BtCursor *pCur; 2635 2636 /* Close all cursors opened via this handle. */ 2637 assert( sqlite3_mutex_held(p->db->mutex) ); 2638 sqlite3BtreeEnter(p); 2639 pCur = pBt->pCursor; 2640 while( pCur ){ 2641 BtCursor *pTmp = pCur; 2642 pCur = pCur->pNext; 2643 if( pTmp->pBtree==p ){ 2644 sqlite3BtreeCloseCursor(pTmp); 2645 } 2646 } 2647 2648 /* Rollback any active transaction and free the handle structure. 2649 ** The call to sqlite3BtreeRollback() drops any table-locks held by 2650 ** this handle. 2651 */ 2652 sqlite3BtreeRollback(p, SQLITE_OK, 0); 2653 sqlite3BtreeLeave(p); 2654 2655 /* If there are still other outstanding references to the shared-btree 2656 ** structure, return now. The remainder of this procedure cleans 2657 ** up the shared-btree. 2658 */ 2659 assert( p->wantToLock==0 && p->locked==0 ); 2660 if( !p->sharable || removeFromSharingList(pBt) ){ 2661 /* The pBt is no longer on the sharing list, so we can access 2662 ** it without having to hold the mutex. 2663 ** 2664 ** Clean out and delete the BtShared object. 2665 */ 2666 assert( !pBt->pCursor ); 2667 sqlite3PagerClose(pBt->pPager, p->db); 2668 if( pBt->xFreeSchema && pBt->pSchema ){ 2669 pBt->xFreeSchema(pBt->pSchema); 2670 } 2671 sqlite3DbFree(0, pBt->pSchema); 2672 freeTempSpace(pBt); 2673 sqlite3_free(pBt); 2674 } 2675 2676 #ifndef SQLITE_OMIT_SHARED_CACHE 2677 assert( p->wantToLock==0 ); 2678 assert( p->locked==0 ); 2679 if( p->pPrev ) p->pPrev->pNext = p->pNext; 2680 if( p->pNext ) p->pNext->pPrev = p->pPrev; 2681 #endif 2682 2683 sqlite3_free(p); 2684 return SQLITE_OK; 2685 } 2686 2687 /* 2688 ** Change the "soft" limit on the number of pages in the cache. 2689 ** Unused and unmodified pages will be recycled when the number of 2690 ** pages in the cache exceeds this soft limit. But the size of the 2691 ** cache is allowed to grow larger than this limit if it contains 2692 ** dirty pages or pages still in active use. 2693 */ 2694 int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){ 2695 BtShared *pBt = p->pBt; 2696 assert( sqlite3_mutex_held(p->db->mutex) ); 2697 sqlite3BtreeEnter(p); 2698 sqlite3PagerSetCachesize(pBt->pPager, mxPage); 2699 sqlite3BtreeLeave(p); 2700 return SQLITE_OK; 2701 } 2702 2703 /* 2704 ** Change the "spill" limit on the number of pages in the cache. 2705 ** If the number of pages exceeds this limit during a write transaction, 2706 ** the pager might attempt to "spill" pages to the journal early in 2707 ** order to free up memory. 2708 ** 2709 ** The value returned is the current spill size. If zero is passed 2710 ** as an argument, no changes are made to the spill size setting, so 2711 ** using mxPage of 0 is a way to query the current spill size. 2712 */ 2713 int sqlite3BtreeSetSpillSize(Btree *p, int mxPage){ 2714 BtShared *pBt = p->pBt; 2715 int res; 2716 assert( sqlite3_mutex_held(p->db->mutex) ); 2717 sqlite3BtreeEnter(p); 2718 res = sqlite3PagerSetSpillsize(pBt->pPager, mxPage); 2719 sqlite3BtreeLeave(p); 2720 return res; 2721 } 2722 2723 #if SQLITE_MAX_MMAP_SIZE>0 2724 /* 2725 ** Change the limit on the amount of the database file that may be 2726 ** memory mapped. 2727 */ 2728 int sqlite3BtreeSetMmapLimit(Btree *p, sqlite3_int64 szMmap){ 2729 BtShared *pBt = p->pBt; 2730 assert( sqlite3_mutex_held(p->db->mutex) ); 2731 sqlite3BtreeEnter(p); 2732 sqlite3PagerSetMmapLimit(pBt->pPager, szMmap); 2733 sqlite3BtreeLeave(p); 2734 return SQLITE_OK; 2735 } 2736 #endif /* SQLITE_MAX_MMAP_SIZE>0 */ 2737 2738 /* 2739 ** Change the way data is synced to disk in order to increase or decrease 2740 ** how well the database resists damage due to OS crashes and power 2741 ** failures. Level 1 is the same as asynchronous (no syncs() occur and 2742 ** there is a high probability of damage) Level 2 is the default. There 2743 ** is a very low but non-zero probability of damage. Level 3 reduces the 2744 ** probability of damage to near zero but with a write performance reduction. 2745 */ 2746 #ifndef SQLITE_OMIT_PAGER_PRAGMAS 2747 int sqlite3BtreeSetPagerFlags( 2748 Btree *p, /* The btree to set the safety level on */ 2749 unsigned pgFlags /* Various PAGER_* flags */ 2750 ){ 2751 BtShared *pBt = p->pBt; 2752 assert( sqlite3_mutex_held(p->db->mutex) ); 2753 sqlite3BtreeEnter(p); 2754 sqlite3PagerSetFlags(pBt->pPager, pgFlags); 2755 sqlite3BtreeLeave(p); 2756 return SQLITE_OK; 2757 } 2758 #endif 2759 2760 /* 2761 ** Change the default pages size and the number of reserved bytes per page. 2762 ** Or, if the page size has already been fixed, return SQLITE_READONLY 2763 ** without changing anything. 2764 ** 2765 ** The page size must be a power of 2 between 512 and 65536. If the page 2766 ** size supplied does not meet this constraint then the page size is not 2767 ** changed. 2768 ** 2769 ** Page sizes are constrained to be a power of two so that the region 2770 ** of the database file used for locking (beginning at PENDING_BYTE, 2771 ** the first byte past the 1GB boundary, 0x40000000) needs to occur 2772 ** at the beginning of a page. 2773 ** 2774 ** If parameter nReserve is less than zero, then the number of reserved 2775 ** bytes per page is left unchanged. 2776 ** 2777 ** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size 2778 ** and autovacuum mode can no longer be changed. 2779 */ 2780 int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){ 2781 int rc = SQLITE_OK; 2782 BtShared *pBt = p->pBt; 2783 assert( nReserve>=-1 && nReserve<=255 ); 2784 sqlite3BtreeEnter(p); 2785 #if SQLITE_HAS_CODEC 2786 if( nReserve>pBt->optimalReserve ) pBt->optimalReserve = (u8)nReserve; 2787 #endif 2788 if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){ 2789 sqlite3BtreeLeave(p); 2790 return SQLITE_READONLY; 2791 } 2792 if( nReserve<0 ){ 2793 nReserve = pBt->pageSize - pBt->usableSize; 2794 } 2795 assert( nReserve>=0 && nReserve<=255 ); 2796 if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE && 2797 ((pageSize-1)&pageSize)==0 ){ 2798 assert( (pageSize & 7)==0 ); 2799 assert( !pBt->pCursor ); 2800 pBt->pageSize = (u32)pageSize; 2801 freeTempSpace(pBt); 2802 } 2803 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve); 2804 pBt->usableSize = pBt->pageSize - (u16)nReserve; 2805 if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED; 2806 sqlite3BtreeLeave(p); 2807 return rc; 2808 } 2809 2810 /* 2811 ** Return the currently defined page size 2812 */ 2813 int sqlite3BtreeGetPageSize(Btree *p){ 2814 return p->pBt->pageSize; 2815 } 2816 2817 /* 2818 ** This function is similar to sqlite3BtreeGetReserve(), except that it 2819 ** may only be called if it is guaranteed that the b-tree mutex is already 2820 ** held. 2821 ** 2822 ** This is useful in one special case in the backup API code where it is 2823 ** known that the shared b-tree mutex is held, but the mutex on the 2824 ** database handle that owns *p is not. In this case if sqlite3BtreeEnter() 2825 ** were to be called, it might collide with some other operation on the 2826 ** database handle that owns *p, causing undefined behavior. 2827 */ 2828 int sqlite3BtreeGetReserveNoMutex(Btree *p){ 2829 int n; 2830 assert( sqlite3_mutex_held(p->pBt->mutex) ); 2831 n = p->pBt->pageSize - p->pBt->usableSize; 2832 return n; 2833 } 2834 2835 /* 2836 ** Return the number of bytes of space at the end of every page that 2837 ** are intentually left unused. This is the "reserved" space that is 2838 ** sometimes used by extensions. 2839 ** 2840 ** If SQLITE_HAS_MUTEX is defined then the number returned is the 2841 ** greater of the current reserved space and the maximum requested 2842 ** reserve space. 2843 */ 2844 int sqlite3BtreeGetOptimalReserve(Btree *p){ 2845 int n; 2846 sqlite3BtreeEnter(p); 2847 n = sqlite3BtreeGetReserveNoMutex(p); 2848 #ifdef SQLITE_HAS_CODEC 2849 if( n<p->pBt->optimalReserve ) n = p->pBt->optimalReserve; 2850 #endif 2851 sqlite3BtreeLeave(p); 2852 return n; 2853 } 2854 2855 2856 /* 2857 ** Set the maximum page count for a database if mxPage is positive. 2858 ** No changes are made if mxPage is 0 or negative. 2859 ** Regardless of the value of mxPage, return the maximum page count. 2860 */ 2861 int sqlite3BtreeMaxPageCount(Btree *p, int mxPage){ 2862 int n; 2863 sqlite3BtreeEnter(p); 2864 n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage); 2865 sqlite3BtreeLeave(p); 2866 return n; 2867 } 2868 2869 /* 2870 ** Change the values for the BTS_SECURE_DELETE and BTS_OVERWRITE flags: 2871 ** 2872 ** newFlag==0 Both BTS_SECURE_DELETE and BTS_OVERWRITE are cleared 2873 ** newFlag==1 BTS_SECURE_DELETE set and BTS_OVERWRITE is cleared 2874 ** newFlag==2 BTS_SECURE_DELETE cleared and BTS_OVERWRITE is set 2875 ** newFlag==(-1) No changes 2876 ** 2877 ** This routine acts as a query if newFlag is less than zero 2878 ** 2879 ** With BTS_OVERWRITE set, deleted content is overwritten by zeros, but 2880 ** freelist leaf pages are not written back to the database. Thus in-page 2881 ** deleted content is cleared, but freelist deleted content is not. 2882 ** 2883 ** With BTS_SECURE_DELETE, operation is like BTS_OVERWRITE with the addition 2884 ** that freelist leaf pages are written back into the database, increasing 2885 ** the amount of disk I/O. 2886 */ 2887 int sqlite3BtreeSecureDelete(Btree *p, int newFlag){ 2888 int b; 2889 if( p==0 ) return 0; 2890 sqlite3BtreeEnter(p); 2891 assert( BTS_OVERWRITE==BTS_SECURE_DELETE*2 ); 2892 assert( BTS_FAST_SECURE==(BTS_OVERWRITE|BTS_SECURE_DELETE) ); 2893 if( newFlag>=0 ){ 2894 p->pBt->btsFlags &= ~BTS_FAST_SECURE; 2895 p->pBt->btsFlags |= BTS_SECURE_DELETE*newFlag; 2896 } 2897 b = (p->pBt->btsFlags & BTS_FAST_SECURE)/BTS_SECURE_DELETE; 2898 sqlite3BtreeLeave(p); 2899 return b; 2900 } 2901 2902 /* 2903 ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum' 2904 ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it 2905 ** is disabled. The default value for the auto-vacuum property is 2906 ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro. 2907 */ 2908 int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){ 2909 #ifdef SQLITE_OMIT_AUTOVACUUM 2910 return SQLITE_READONLY; 2911 #else 2912 BtShared *pBt = p->pBt; 2913 int rc = SQLITE_OK; 2914 u8 av = (u8)autoVacuum; 2915 2916 sqlite3BtreeEnter(p); 2917 if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){ 2918 rc = SQLITE_READONLY; 2919 }else{ 2920 pBt->autoVacuum = av ?1:0; 2921 pBt->incrVacuum = av==2 ?1:0; 2922 } 2923 sqlite3BtreeLeave(p); 2924 return rc; 2925 #endif 2926 } 2927 2928 /* 2929 ** Return the value of the 'auto-vacuum' property. If auto-vacuum is 2930 ** enabled 1 is returned. Otherwise 0. 2931 */ 2932 int sqlite3BtreeGetAutoVacuum(Btree *p){ 2933 #ifdef SQLITE_OMIT_AUTOVACUUM 2934 return BTREE_AUTOVACUUM_NONE; 2935 #else 2936 int rc; 2937 sqlite3BtreeEnter(p); 2938 rc = ( 2939 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE: 2940 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL: 2941 BTREE_AUTOVACUUM_INCR 2942 ); 2943 sqlite3BtreeLeave(p); 2944 return rc; 2945 #endif 2946 } 2947 2948 /* 2949 ** If the user has not set the safety-level for this database connection 2950 ** using "PRAGMA synchronous", and if the safety-level is not already 2951 ** set to the value passed to this function as the second parameter, 2952 ** set it so. 2953 */ 2954 #if SQLITE_DEFAULT_SYNCHRONOUS!=SQLITE_DEFAULT_WAL_SYNCHRONOUS \ 2955 && !defined(SQLITE_OMIT_WAL) 2956 static void setDefaultSyncFlag(BtShared *pBt, u8 safety_level){ 2957 sqlite3 *db; 2958 Db *pDb; 2959 if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){ 2960 while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; } 2961 if( pDb->bSyncSet==0 2962 && pDb->safety_level!=safety_level 2963 && pDb!=&db->aDb[1] 2964 ){ 2965 pDb->safety_level = safety_level; 2966 sqlite3PagerSetFlags(pBt->pPager, 2967 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK)); 2968 } 2969 } 2970 } 2971 #else 2972 # define setDefaultSyncFlag(pBt,safety_level) 2973 #endif 2974 2975 /* 2976 ** Get a reference to pPage1 of the database file. This will 2977 ** also acquire a readlock on that file. 2978 ** 2979 ** SQLITE_OK is returned on success. If the file is not a 2980 ** well-formed database file, then SQLITE_CORRUPT is returned. 2981 ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM 2982 ** is returned if we run out of memory. 2983 */ 2984 static int lockBtree(BtShared *pBt){ 2985 int rc; /* Result code from subfunctions */ 2986 MemPage *pPage1; /* Page 1 of the database file */ 2987 int nPage; /* Number of pages in the database */ 2988 int nPageFile = 0; /* Number of pages in the database file */ 2989 int nPageHeader; /* Number of pages in the database according to hdr */ 2990 2991 assert( sqlite3_mutex_held(pBt->mutex) ); 2992 assert( pBt->pPage1==0 ); 2993 rc = sqlite3PagerSharedLock(pBt->pPager); 2994 if( rc!=SQLITE_OK ) return rc; 2995 rc = btreeGetPage(pBt, 1, &pPage1, 0); 2996 if( rc!=SQLITE_OK ) return rc; 2997 2998 /* Do some checking to help insure the file we opened really is 2999 ** a valid database file. 3000 */ 3001 nPage = nPageHeader = get4byte(28+(u8*)pPage1->aData); 3002 sqlite3PagerPagecount(pBt->pPager, &nPageFile); 3003 if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){ 3004 nPage = nPageFile; 3005 } 3006 if( nPage>0 ){ 3007 u32 pageSize; 3008 u32 usableSize; 3009 u8 *page1 = pPage1->aData; 3010 rc = SQLITE_NOTADB; 3011 /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins 3012 ** with the following 16 bytes (in hex): 53 51 4c 69 74 65 20 66 6f 72 6d 3013 ** 61 74 20 33 00. */ 3014 if( memcmp(page1, zMagicHeader, 16)!=0 ){ 3015 goto page1_init_failed; 3016 } 3017 3018 #ifdef SQLITE_OMIT_WAL 3019 if( page1[18]>1 ){ 3020 pBt->btsFlags |= BTS_READ_ONLY; 3021 } 3022 if( page1[19]>1 ){ 3023 goto page1_init_failed; 3024 } 3025 #else 3026 if( page1[18]>2 ){ 3027 pBt->btsFlags |= BTS_READ_ONLY; 3028 } 3029 if( page1[19]>2 ){ 3030 goto page1_init_failed; 3031 } 3032 3033 /* If the write version is set to 2, this database should be accessed 3034 ** in WAL mode. If the log is not already open, open it now. Then 3035 ** return SQLITE_OK and return without populating BtShared.pPage1. 3036 ** The caller detects this and calls this function again. This is 3037 ** required as the version of page 1 currently in the page1 buffer 3038 ** may not be the latest version - there may be a newer one in the log 3039 ** file. 3040 */ 3041 if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){ 3042 int isOpen = 0; 3043 rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen); 3044 if( rc!=SQLITE_OK ){ 3045 goto page1_init_failed; 3046 }else{ 3047 setDefaultSyncFlag(pBt, SQLITE_DEFAULT_WAL_SYNCHRONOUS+1); 3048 if( isOpen==0 ){ 3049 releasePageOne(pPage1); 3050 return SQLITE_OK; 3051 } 3052 } 3053 rc = SQLITE_NOTADB; 3054 }else{ 3055 setDefaultSyncFlag(pBt, SQLITE_DEFAULT_SYNCHRONOUS+1); 3056 } 3057 #endif 3058 3059 /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload 3060 ** fractions and the leaf payload fraction values must be 64, 32, and 32. 3061 ** 3062 ** The original design allowed these amounts to vary, but as of 3063 ** version 3.6.0, we require them to be fixed. 3064 */ 3065 if( memcmp(&page1[21], "\100\040\040",3)!=0 ){ 3066 goto page1_init_failed; 3067 } 3068 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is 3069 ** determined by the 2-byte integer located at an offset of 16 bytes from 3070 ** the beginning of the database file. */ 3071 pageSize = (page1[16]<<8) | (page1[17]<<16); 3072 /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two 3073 ** between 512 and 65536 inclusive. */ 3074 if( ((pageSize-1)&pageSize)!=0 3075 || pageSize>SQLITE_MAX_PAGE_SIZE 3076 || pageSize<=256 3077 ){ 3078 goto page1_init_failed; 3079 } 3080 assert( (pageSize & 7)==0 ); 3081 /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte 3082 ** integer at offset 20 is the number of bytes of space at the end of 3083 ** each page to reserve for extensions. 3084 ** 3085 ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is 3086 ** determined by the one-byte unsigned integer found at an offset of 20 3087 ** into the database file header. */ 3088 usableSize = pageSize - page1[20]; 3089 if( (u32)pageSize!=pBt->pageSize ){ 3090 /* After reading the first page of the database assuming a page size 3091 ** of BtShared.pageSize, we have discovered that the page-size is 3092 ** actually pageSize. Unlock the database, leave pBt->pPage1 at 3093 ** zero and return SQLITE_OK. The caller will call this function 3094 ** again with the correct page-size. 3095 */ 3096 releasePageOne(pPage1); 3097 pBt->usableSize = usableSize; 3098 pBt->pageSize = pageSize; 3099 freeTempSpace(pBt); 3100 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, 3101 pageSize-usableSize); 3102 return rc; 3103 } 3104 if( (pBt->db->flags & SQLITE_WriteSchema)==0 && nPage>nPageFile ){ 3105 rc = SQLITE_CORRUPT_BKPT; 3106 goto page1_init_failed; 3107 } 3108 /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to 3109 ** be less than 480. In other words, if the page size is 512, then the 3110 ** reserved space size cannot exceed 32. */ 3111 if( usableSize<480 ){ 3112 goto page1_init_failed; 3113 } 3114 pBt->pageSize = pageSize; 3115 pBt->usableSize = usableSize; 3116 #ifndef SQLITE_OMIT_AUTOVACUUM 3117 pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0); 3118 pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0); 3119 #endif 3120 } 3121 3122 /* maxLocal is the maximum amount of payload to store locally for 3123 ** a cell. Make sure it is small enough so that at least minFanout 3124 ** cells can will fit on one page. We assume a 10-byte page header. 3125 ** Besides the payload, the cell must store: 3126 ** 2-byte pointer to the cell 3127 ** 4-byte child pointer 3128 ** 9-byte nKey value 3129 ** 4-byte nData value 3130 ** 4-byte overflow page pointer 3131 ** So a cell consists of a 2-byte pointer, a header which is as much as 3132 ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow 3133 ** page pointer. 3134 */ 3135 pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23); 3136 pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23); 3137 pBt->maxLeaf = (u16)(pBt->usableSize - 35); 3138 pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23); 3139 if( pBt->maxLocal>127 ){ 3140 pBt->max1bytePayload = 127; 3141 }else{ 3142 pBt->max1bytePayload = (u8)pBt->maxLocal; 3143 } 3144 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) ); 3145 pBt->pPage1 = pPage1; 3146 pBt->nPage = nPage; 3147 return SQLITE_OK; 3148 3149 page1_init_failed: 3150 releasePageOne(pPage1); 3151 pBt->pPage1 = 0; 3152 return rc; 3153 } 3154 3155 #ifndef NDEBUG 3156 /* 3157 ** Return the number of cursors open on pBt. This is for use 3158 ** in assert() expressions, so it is only compiled if NDEBUG is not 3159 ** defined. 3160 ** 3161 ** Only write cursors are counted if wrOnly is true. If wrOnly is 3162 ** false then all cursors are counted. 3163 ** 3164 ** For the purposes of this routine, a cursor is any cursor that 3165 ** is capable of reading or writing to the database. Cursors that 3166 ** have been tripped into the CURSOR_FAULT state are not counted. 3167 */ 3168 static int countValidCursors(BtShared *pBt, int wrOnly){ 3169 BtCursor *pCur; 3170 int r = 0; 3171 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ 3172 if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0) 3173 && pCur->eState!=CURSOR_FAULT ) r++; 3174 } 3175 return r; 3176 } 3177 #endif 3178 3179 /* 3180 ** If there are no outstanding cursors and we are not in the middle 3181 ** of a transaction but there is a read lock on the database, then 3182 ** this routine unrefs the first page of the database file which 3183 ** has the effect of releasing the read lock. 3184 ** 3185 ** If there is a transaction in progress, this routine is a no-op. 3186 */ 3187 static void unlockBtreeIfUnused(BtShared *pBt){ 3188 assert( sqlite3_mutex_held(pBt->mutex) ); 3189 assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE ); 3190 if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){ 3191 MemPage *pPage1 = pBt->pPage1; 3192 assert( pPage1->aData ); 3193 assert( sqlite3PagerRefcount(pBt->pPager)==1 ); 3194 pBt->pPage1 = 0; 3195 releasePageOne(pPage1); 3196 } 3197 } 3198 3199 /* 3200 ** If pBt points to an empty file then convert that empty file 3201 ** into a new empty database by initializing the first page of 3202 ** the database. 3203 */ 3204 static int newDatabase(BtShared *pBt){ 3205 MemPage *pP1; 3206 unsigned char *data; 3207 int rc; 3208 3209 assert( sqlite3_mutex_held(pBt->mutex) ); 3210 if( pBt->nPage>0 ){ 3211 return SQLITE_OK; 3212 } 3213 pP1 = pBt->pPage1; 3214 assert( pP1!=0 ); 3215 data = pP1->aData; 3216 rc = sqlite3PagerWrite(pP1->pDbPage); 3217 if( rc ) return rc; 3218 memcpy(data, zMagicHeader, sizeof(zMagicHeader)); 3219 assert( sizeof(zMagicHeader)==16 ); 3220 data[16] = (u8)((pBt->pageSize>>8)&0xff); 3221 data[17] = (u8)((pBt->pageSize>>16)&0xff); 3222 data[18] = 1; 3223 data[19] = 1; 3224 assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize); 3225 data[20] = (u8)(pBt->pageSize - pBt->usableSize); 3226 data[21] = 64; 3227 data[22] = 32; 3228 data[23] = 32; 3229 memset(&data[24], 0, 100-24); 3230 zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA ); 3231 pBt->btsFlags |= BTS_PAGESIZE_FIXED; 3232 #ifndef SQLITE_OMIT_AUTOVACUUM 3233 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 ); 3234 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 ); 3235 put4byte(&data[36 + 4*4], pBt->autoVacuum); 3236 put4byte(&data[36 + 7*4], pBt->incrVacuum); 3237 #endif 3238 pBt->nPage = 1; 3239 data[31] = 1; 3240 return SQLITE_OK; 3241 } 3242 3243 /* 3244 ** Initialize the first page of the database file (creating a database 3245 ** consisting of a single page and no schema objects). Return SQLITE_OK 3246 ** if successful, or an SQLite error code otherwise. 3247 */ 3248 int sqlite3BtreeNewDb(Btree *p){ 3249 int rc; 3250 sqlite3BtreeEnter(p); 3251 p->pBt->nPage = 0; 3252 rc = newDatabase(p->pBt); 3253 sqlite3BtreeLeave(p); 3254 return rc; 3255 } 3256 3257 /* 3258 ** Attempt to start a new transaction. A write-transaction 3259 ** is started if the second argument is nonzero, otherwise a read- 3260 ** transaction. If the second argument is 2 or more and exclusive 3261 ** transaction is started, meaning that no other process is allowed 3262 ** to access the database. A preexisting transaction may not be 3263 ** upgraded to exclusive by calling this routine a second time - the 3264 ** exclusivity flag only works for a new transaction. 3265 ** 3266 ** A write-transaction must be started before attempting any 3267 ** changes to the database. None of the following routines 3268 ** will work unless a transaction is started first: 3269 ** 3270 ** sqlite3BtreeCreateTable() 3271 ** sqlite3BtreeCreateIndex() 3272 ** sqlite3BtreeClearTable() 3273 ** sqlite3BtreeDropTable() 3274 ** sqlite3BtreeInsert() 3275 ** sqlite3BtreeDelete() 3276 ** sqlite3BtreeUpdateMeta() 3277 ** 3278 ** If an initial attempt to acquire the lock fails because of lock contention 3279 ** and the database was previously unlocked, then invoke the busy handler 3280 ** if there is one. But if there was previously a read-lock, do not 3281 ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is 3282 ** returned when there is already a read-lock in order to avoid a deadlock. 3283 ** 3284 ** Suppose there are two processes A and B. A has a read lock and B has 3285 ** a reserved lock. B tries to promote to exclusive but is blocked because 3286 ** of A's read lock. A tries to promote to reserved but is blocked by B. 3287 ** One or the other of the two processes must give way or there can be 3288 ** no progress. By returning SQLITE_BUSY and not invoking the busy callback 3289 ** when A already has a read lock, we encourage A to give up and let B 3290 ** proceed. 3291 */ 3292 int sqlite3BtreeBeginTrans(Btree *p, int wrflag){ 3293 BtShared *pBt = p->pBt; 3294 int rc = SQLITE_OK; 3295 3296 sqlite3BtreeEnter(p); 3297 btreeIntegrity(p); 3298 3299 /* If the btree is already in a write-transaction, or it 3300 ** is already in a read-transaction and a read-transaction 3301 ** is requested, this is a no-op. 3302 */ 3303 if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){ 3304 goto trans_begun; 3305 } 3306 assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 ); 3307 3308 /* Write transactions are not possible on a read-only database */ 3309 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){ 3310 rc = SQLITE_READONLY; 3311 goto trans_begun; 3312 } 3313 3314 #ifndef SQLITE_OMIT_SHARED_CACHE 3315 { 3316 sqlite3 *pBlock = 0; 3317 /* If another database handle has already opened a write transaction 3318 ** on this shared-btree structure and a second write transaction is 3319 ** requested, return SQLITE_LOCKED. 3320 */ 3321 if( (wrflag && pBt->inTransaction==TRANS_WRITE) 3322 || (pBt->btsFlags & BTS_PENDING)!=0 3323 ){ 3324 pBlock = pBt->pWriter->db; 3325 }else if( wrflag>1 ){ 3326 BtLock *pIter; 3327 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ 3328 if( pIter->pBtree!=p ){ 3329 pBlock = pIter->pBtree->db; 3330 break; 3331 } 3332 } 3333 } 3334 if( pBlock ){ 3335 sqlite3ConnectionBlocked(p->db, pBlock); 3336 rc = SQLITE_LOCKED_SHAREDCACHE; 3337 goto trans_begun; 3338 } 3339 } 3340 #endif 3341 3342 /* Any read-only or read-write transaction implies a read-lock on 3343 ** page 1. So if some other shared-cache client already has a write-lock 3344 ** on page 1, the transaction cannot be opened. */ 3345 rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK); 3346 if( SQLITE_OK!=rc ) goto trans_begun; 3347 3348 pBt->btsFlags &= ~BTS_INITIALLY_EMPTY; 3349 if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY; 3350 do { 3351 /* Call lockBtree() until either pBt->pPage1 is populated or 3352 ** lockBtree() returns something other than SQLITE_OK. lockBtree() 3353 ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after 3354 ** reading page 1 it discovers that the page-size of the database 3355 ** file is not pBt->pageSize. In this case lockBtree() will update 3356 ** pBt->pageSize to the page-size of the file on disk. 3357 */ 3358 while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) ); 3359 3360 if( rc==SQLITE_OK && wrflag ){ 3361 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){ 3362 rc = SQLITE_READONLY; 3363 }else{ 3364 rc = sqlite3PagerBegin(pBt->pPager,wrflag>1,sqlite3TempInMemory(p->db)); 3365 if( rc==SQLITE_OK ){ 3366 rc = newDatabase(pBt); 3367 } 3368 } 3369 } 3370 3371 if( rc!=SQLITE_OK ){ 3372 unlockBtreeIfUnused(pBt); 3373 } 3374 }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE && 3375 btreeInvokeBusyHandler(pBt) ); 3376 sqlite3PagerResetLockTimeout(pBt->pPager); 3377 3378 if( rc==SQLITE_OK ){ 3379 if( p->inTrans==TRANS_NONE ){ 3380 pBt->nTransaction++; 3381 #ifndef SQLITE_OMIT_SHARED_CACHE 3382 if( p->sharable ){ 3383 assert( p->lock.pBtree==p && p->lock.iTable==1 ); 3384 p->lock.eLock = READ_LOCK; 3385 p->lock.pNext = pBt->pLock; 3386 pBt->pLock = &p->lock; 3387 } 3388 #endif 3389 } 3390 p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ); 3391 if( p->inTrans>pBt->inTransaction ){ 3392 pBt->inTransaction = p->inTrans; 3393 } 3394 if( wrflag ){ 3395 MemPage *pPage1 = pBt->pPage1; 3396 #ifndef SQLITE_OMIT_SHARED_CACHE 3397 assert( !pBt->pWriter ); 3398 pBt->pWriter = p; 3399 pBt->btsFlags &= ~BTS_EXCLUSIVE; 3400 if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE; 3401 #endif 3402 3403 /* If the db-size header field is incorrect (as it may be if an old 3404 ** client has been writing the database file), update it now. Doing 3405 ** this sooner rather than later means the database size can safely 3406 ** re-read the database size from page 1 if a savepoint or transaction 3407 ** rollback occurs within the transaction. 3408 */ 3409 if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){ 3410 rc = sqlite3PagerWrite(pPage1->pDbPage); 3411 if( rc==SQLITE_OK ){ 3412 put4byte(&pPage1->aData[28], pBt->nPage); 3413 } 3414 } 3415 } 3416 } 3417 3418 3419 trans_begun: 3420 if( rc==SQLITE_OK && wrflag ){ 3421 /* This call makes sure that the pager has the correct number of 3422 ** open savepoints. If the second parameter is greater than 0 and 3423 ** the sub-journal is not already open, then it will be opened here. 3424 */ 3425 rc = sqlite3PagerOpenSavepoint(pBt->pPager, p->db->nSavepoint); 3426 } 3427 3428 btreeIntegrity(p); 3429 sqlite3BtreeLeave(p); 3430 return rc; 3431 } 3432 3433 #ifndef SQLITE_OMIT_AUTOVACUUM 3434 3435 /* 3436 ** Set the pointer-map entries for all children of page pPage. Also, if 3437 ** pPage contains cells that point to overflow pages, set the pointer 3438 ** map entries for the overflow pages as well. 3439 */ 3440 static int setChildPtrmaps(MemPage *pPage){ 3441 int i; /* Counter variable */ 3442 int nCell; /* Number of cells in page pPage */ 3443 int rc; /* Return code */ 3444 BtShared *pBt = pPage->pBt; 3445 Pgno pgno = pPage->pgno; 3446 3447 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 3448 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage); 3449 if( rc!=SQLITE_OK ) return rc; 3450 nCell = pPage->nCell; 3451 3452 for(i=0; i<nCell; i++){ 3453 u8 *pCell = findCell(pPage, i); 3454 3455 ptrmapPutOvflPtr(pPage, pCell, &rc); 3456 3457 if( !pPage->leaf ){ 3458 Pgno childPgno = get4byte(pCell); 3459 ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc); 3460 } 3461 } 3462 3463 if( !pPage->leaf ){ 3464 Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); 3465 ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc); 3466 } 3467 3468 return rc; 3469 } 3470 3471 /* 3472 ** Somewhere on pPage is a pointer to page iFrom. Modify this pointer so 3473 ** that it points to iTo. Parameter eType describes the type of pointer to 3474 ** be modified, as follows: 3475 ** 3476 ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child 3477 ** page of pPage. 3478 ** 3479 ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow 3480 ** page pointed to by one of the cells on pPage. 3481 ** 3482 ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next 3483 ** overflow page in the list. 3484 */ 3485 static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){ 3486 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 3487 assert( sqlite3PagerIswriteable(pPage->pDbPage) ); 3488 if( eType==PTRMAP_OVERFLOW2 ){ 3489 /* The pointer is always the first 4 bytes of the page in this case. */ 3490 if( get4byte(pPage->aData)!=iFrom ){ 3491 return SQLITE_CORRUPT_PAGE(pPage); 3492 } 3493 put4byte(pPage->aData, iTo); 3494 }else{ 3495 int i; 3496 int nCell; 3497 int rc; 3498 3499 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage); 3500 if( rc ) return rc; 3501 nCell = pPage->nCell; 3502 3503 for(i=0; i<nCell; i++){ 3504 u8 *pCell = findCell(pPage, i); 3505 if( eType==PTRMAP_OVERFLOW1 ){ 3506 CellInfo info; 3507 pPage->xParseCell(pPage, pCell, &info); 3508 if( info.nLocal<info.nPayload ){ 3509 if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){ 3510 return SQLITE_CORRUPT_PAGE(pPage); 3511 } 3512 if( iFrom==get4byte(pCell+info.nSize-4) ){ 3513 put4byte(pCell+info.nSize-4, iTo); 3514 break; 3515 } 3516 } 3517 }else{ 3518 if( get4byte(pCell)==iFrom ){ 3519 put4byte(pCell, iTo); 3520 break; 3521 } 3522 } 3523 } 3524 3525 if( i==nCell ){ 3526 if( eType!=PTRMAP_BTREE || 3527 get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){ 3528 return SQLITE_CORRUPT_PAGE(pPage); 3529 } 3530 put4byte(&pPage->aData[pPage->hdrOffset+8], iTo); 3531 } 3532 } 3533 return SQLITE_OK; 3534 } 3535 3536 3537 /* 3538 ** Move the open database page pDbPage to location iFreePage in the 3539 ** database. The pDbPage reference remains valid. 3540 ** 3541 ** The isCommit flag indicates that there is no need to remember that 3542 ** the journal needs to be sync()ed before database page pDbPage->pgno 3543 ** can be written to. The caller has already promised not to write to that 3544 ** page. 3545 */ 3546 static int relocatePage( 3547 BtShared *pBt, /* Btree */ 3548 MemPage *pDbPage, /* Open page to move */ 3549 u8 eType, /* Pointer map 'type' entry for pDbPage */ 3550 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */ 3551 Pgno iFreePage, /* The location to move pDbPage to */ 3552 int isCommit /* isCommit flag passed to sqlite3PagerMovepage */ 3553 ){ 3554 MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */ 3555 Pgno iDbPage = pDbPage->pgno; 3556 Pager *pPager = pBt->pPager; 3557 int rc; 3558 3559 assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || 3560 eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ); 3561 assert( sqlite3_mutex_held(pBt->mutex) ); 3562 assert( pDbPage->pBt==pBt ); 3563 3564 /* Move page iDbPage from its current location to page number iFreePage */ 3565 TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", 3566 iDbPage, iFreePage, iPtrPage, eType)); 3567 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit); 3568 if( rc!=SQLITE_OK ){ 3569 return rc; 3570 } 3571 pDbPage->pgno = iFreePage; 3572 3573 /* If pDbPage was a btree-page, then it may have child pages and/or cells 3574 ** that point to overflow pages. The pointer map entries for all these 3575 ** pages need to be changed. 3576 ** 3577 ** If pDbPage is an overflow page, then the first 4 bytes may store a 3578 ** pointer to a subsequent overflow page. If this is the case, then 3579 ** the pointer map needs to be updated for the subsequent overflow page. 3580 */ 3581 if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){ 3582 rc = setChildPtrmaps(pDbPage); 3583 if( rc!=SQLITE_OK ){ 3584 return rc; 3585 } 3586 }else{ 3587 Pgno nextOvfl = get4byte(pDbPage->aData); 3588 if( nextOvfl!=0 ){ 3589 ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage, &rc); 3590 if( rc!=SQLITE_OK ){ 3591 return rc; 3592 } 3593 } 3594 } 3595 3596 /* Fix the database pointer on page iPtrPage that pointed at iDbPage so 3597 ** that it points at iFreePage. Also fix the pointer map entry for 3598 ** iPtrPage. 3599 */ 3600 if( eType!=PTRMAP_ROOTPAGE ){ 3601 rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0); 3602 if( rc!=SQLITE_OK ){ 3603 return rc; 3604 } 3605 rc = sqlite3PagerWrite(pPtrPage->pDbPage); 3606 if( rc!=SQLITE_OK ){ 3607 releasePage(pPtrPage); 3608 return rc; 3609 } 3610 rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType); 3611 releasePage(pPtrPage); 3612 if( rc==SQLITE_OK ){ 3613 ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc); 3614 } 3615 } 3616 return rc; 3617 } 3618 3619 /* Forward declaration required by incrVacuumStep(). */ 3620 static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8); 3621 3622 /* 3623 ** Perform a single step of an incremental-vacuum. If successful, return 3624 ** SQLITE_OK. If there is no work to do (and therefore no point in 3625 ** calling this function again), return SQLITE_DONE. Or, if an error 3626 ** occurs, return some other error code. 3627 ** 3628 ** More specifically, this function attempts to re-organize the database so 3629 ** that the last page of the file currently in use is no longer in use. 3630 ** 3631 ** Parameter nFin is the number of pages that this database would contain 3632 ** were this function called until it returns SQLITE_DONE. 3633 ** 3634 ** If the bCommit parameter is non-zero, this function assumes that the 3635 ** caller will keep calling incrVacuumStep() until it returns SQLITE_DONE 3636 ** or an error. bCommit is passed true for an auto-vacuum-on-commit 3637 ** operation, or false for an incremental vacuum. 3638 */ 3639 static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg, int bCommit){ 3640 Pgno nFreeList; /* Number of pages still on the free-list */ 3641 int rc; 3642 3643 assert( sqlite3_mutex_held(pBt->mutex) ); 3644 assert( iLastPg>nFin ); 3645 3646 if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){ 3647 u8 eType; 3648 Pgno iPtrPage; 3649 3650 nFreeList = get4byte(&pBt->pPage1->aData[36]); 3651 if( nFreeList==0 ){ 3652 return SQLITE_DONE; 3653 } 3654 3655 rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage); 3656 if( rc!=SQLITE_OK ){ 3657 return rc; 3658 } 3659 if( eType==PTRMAP_ROOTPAGE ){ 3660 return SQLITE_CORRUPT_BKPT; 3661 } 3662 3663 if( eType==PTRMAP_FREEPAGE ){ 3664 if( bCommit==0 ){ 3665 /* Remove the page from the files free-list. This is not required 3666 ** if bCommit is non-zero. In that case, the free-list will be 3667 ** truncated to zero after this function returns, so it doesn't 3668 ** matter if it still contains some garbage entries. 3669 */ 3670 Pgno iFreePg; 3671 MemPage *pFreePg; 3672 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, BTALLOC_EXACT); 3673 if( rc!=SQLITE_OK ){ 3674 return rc; 3675 } 3676 assert( iFreePg==iLastPg ); 3677 releasePage(pFreePg); 3678 } 3679 } else { 3680 Pgno iFreePg; /* Index of free page to move pLastPg to */ 3681 MemPage *pLastPg; 3682 u8 eMode = BTALLOC_ANY; /* Mode parameter for allocateBtreePage() */ 3683 Pgno iNear = 0; /* nearby parameter for allocateBtreePage() */ 3684 3685 rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0); 3686 if( rc!=SQLITE_OK ){ 3687 return rc; 3688 } 3689 3690 /* If bCommit is zero, this loop runs exactly once and page pLastPg 3691 ** is swapped with the first free page pulled off the free list. 3692 ** 3693 ** On the other hand, if bCommit is greater than zero, then keep 3694 ** looping until a free-page located within the first nFin pages 3695 ** of the file is found. 3696 */ 3697 if( bCommit==0 ){ 3698 eMode = BTALLOC_LE; 3699 iNear = nFin; 3700 } 3701 do { 3702 MemPage *pFreePg; 3703 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iNear, eMode); 3704 if( rc!=SQLITE_OK ){ 3705 releasePage(pLastPg); 3706 return rc; 3707 } 3708 releasePage(pFreePg); 3709 }while( bCommit && iFreePg>nFin ); 3710 assert( iFreePg<iLastPg ); 3711 3712 rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, bCommit); 3713 releasePage(pLastPg); 3714 if( rc!=SQLITE_OK ){ 3715 return rc; 3716 } 3717 } 3718 } 3719 3720 if( bCommit==0 ){ 3721 do { 3722 iLastPg--; 3723 }while( iLastPg==PENDING_BYTE_PAGE(pBt) || PTRMAP_ISPAGE(pBt, iLastPg) ); 3724 pBt->bDoTruncate = 1; 3725 pBt->nPage = iLastPg; 3726 } 3727 return SQLITE_OK; 3728 } 3729 3730 /* 3731 ** The database opened by the first argument is an auto-vacuum database 3732 ** nOrig pages in size containing nFree free pages. Return the expected 3733 ** size of the database in pages following an auto-vacuum operation. 3734 */ 3735 static Pgno finalDbSize(BtShared *pBt, Pgno nOrig, Pgno nFree){ 3736 int nEntry; /* Number of entries on one ptrmap page */ 3737 Pgno nPtrmap; /* Number of PtrMap pages to be freed */ 3738 Pgno nFin; /* Return value */ 3739 3740 nEntry = pBt->usableSize/5; 3741 nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry; 3742 nFin = nOrig - nFree - nPtrmap; 3743 if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<PENDING_BYTE_PAGE(pBt) ){ 3744 nFin--; 3745 } 3746 while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){ 3747 nFin--; 3748 } 3749 3750 return nFin; 3751 } 3752 3753 /* 3754 ** A write-transaction must be opened before calling this function. 3755 ** It performs a single unit of work towards an incremental vacuum. 3756 ** 3757 ** If the incremental vacuum is finished after this function has run, 3758 ** SQLITE_DONE is returned. If it is not finished, but no error occurred, 3759 ** SQLITE_OK is returned. Otherwise an SQLite error code. 3760 */ 3761 int sqlite3BtreeIncrVacuum(Btree *p){ 3762 int rc; 3763 BtShared *pBt = p->pBt; 3764 3765 sqlite3BtreeEnter(p); 3766 assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE ); 3767 if( !pBt->autoVacuum ){ 3768 rc = SQLITE_DONE; 3769 }else{ 3770 Pgno nOrig = btreePagecount(pBt); 3771 Pgno nFree = get4byte(&pBt->pPage1->aData[36]); 3772 Pgno nFin = finalDbSize(pBt, nOrig, nFree); 3773 3774 if( nOrig<nFin ){ 3775 rc = SQLITE_CORRUPT_BKPT; 3776 }else if( nFree>0 ){ 3777 rc = saveAllCursors(pBt, 0, 0); 3778 if( rc==SQLITE_OK ){ 3779 invalidateAllOverflowCache(pBt); 3780 rc = incrVacuumStep(pBt, nFin, nOrig, 0); 3781 } 3782 if( rc==SQLITE_OK ){ 3783 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); 3784 put4byte(&pBt->pPage1->aData[28], pBt->nPage); 3785 } 3786 }else{ 3787 rc = SQLITE_DONE; 3788 } 3789 } 3790 sqlite3BtreeLeave(p); 3791 return rc; 3792 } 3793 3794 /* 3795 ** This routine is called prior to sqlite3PagerCommit when a transaction 3796 ** is committed for an auto-vacuum database. 3797 ** 3798 ** If SQLITE_OK is returned, then *pnTrunc is set to the number of pages 3799 ** the database file should be truncated to during the commit process. 3800 ** i.e. the database has been reorganized so that only the first *pnTrunc 3801 ** pages are in use. 3802 */ 3803 static int autoVacuumCommit(BtShared *pBt){ 3804 int rc = SQLITE_OK; 3805 Pager *pPager = pBt->pPager; 3806 VVA_ONLY( int nRef = sqlite3PagerRefcount(pPager); ) 3807 3808 assert( sqlite3_mutex_held(pBt->mutex) ); 3809 invalidateAllOverflowCache(pBt); 3810 assert(pBt->autoVacuum); 3811 if( !pBt->incrVacuum ){ 3812 Pgno nFin; /* Number of pages in database after autovacuuming */ 3813 Pgno nFree; /* Number of pages on the freelist initially */ 3814 Pgno iFree; /* The next page to be freed */ 3815 Pgno nOrig; /* Database size before freeing */ 3816 3817 nOrig = btreePagecount(pBt); 3818 if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){ 3819 /* It is not possible to create a database for which the final page 3820 ** is either a pointer-map page or the pending-byte page. If one 3821 ** is encountered, this indicates corruption. 3822 */ 3823 return SQLITE_CORRUPT_BKPT; 3824 } 3825 3826 nFree = get4byte(&pBt->pPage1->aData[36]); 3827 nFin = finalDbSize(pBt, nOrig, nFree); 3828 if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT; 3829 if( nFin<nOrig ){ 3830 rc = saveAllCursors(pBt, 0, 0); 3831 } 3832 for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){ 3833 rc = incrVacuumStep(pBt, nFin, iFree, 1); 3834 } 3835 if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){ 3836 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); 3837 put4byte(&pBt->pPage1->aData[32], 0); 3838 put4byte(&pBt->pPage1->aData[36], 0); 3839 put4byte(&pBt->pPage1->aData[28], nFin); 3840 pBt->bDoTruncate = 1; 3841 pBt->nPage = nFin; 3842 } 3843 if( rc!=SQLITE_OK ){ 3844 sqlite3PagerRollback(pPager); 3845 } 3846 } 3847 3848 assert( nRef>=sqlite3PagerRefcount(pPager) ); 3849 return rc; 3850 } 3851 3852 #else /* ifndef SQLITE_OMIT_AUTOVACUUM */ 3853 # define setChildPtrmaps(x) SQLITE_OK 3854 #endif 3855 3856 /* 3857 ** This routine does the first phase of a two-phase commit. This routine 3858 ** causes a rollback journal to be created (if it does not already exist) 3859 ** and populated with enough information so that if a power loss occurs 3860 ** the database can be restored to its original state by playing back 3861 ** the journal. Then the contents of the journal are flushed out to 3862 ** the disk. After the journal is safely on oxide, the changes to the 3863 ** database are written into the database file and flushed to oxide. 3864 ** At the end of this call, the rollback journal still exists on the 3865 ** disk and we are still holding all locks, so the transaction has not 3866 ** committed. See sqlite3BtreeCommitPhaseTwo() for the second phase of the 3867 ** commit process. 3868 ** 3869 ** This call is a no-op if no write-transaction is currently active on pBt. 3870 ** 3871 ** Otherwise, sync the database file for the btree pBt. zMaster points to 3872 ** the name of a master journal file that should be written into the 3873 ** individual journal file, or is NULL, indicating no master journal file 3874 ** (single database transaction). 3875 ** 3876 ** When this is called, the master journal should already have been 3877 ** created, populated with this journal pointer and synced to disk. 3878 ** 3879 ** Once this is routine has returned, the only thing required to commit 3880 ** the write-transaction for this database file is to delete the journal. 3881 */ 3882 int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zMaster){ 3883 int rc = SQLITE_OK; 3884 if( p->inTrans==TRANS_WRITE ){ 3885 BtShared *pBt = p->pBt; 3886 sqlite3BtreeEnter(p); 3887 #ifndef SQLITE_OMIT_AUTOVACUUM 3888 if( pBt->autoVacuum ){ 3889 rc = autoVacuumCommit(pBt); 3890 if( rc!=SQLITE_OK ){ 3891 sqlite3BtreeLeave(p); 3892 return rc; 3893 } 3894 } 3895 if( pBt->bDoTruncate ){ 3896 sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage); 3897 } 3898 #endif 3899 rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zMaster, 0); 3900 sqlite3BtreeLeave(p); 3901 } 3902 return rc; 3903 } 3904 3905 /* 3906 ** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback() 3907 ** at the conclusion of a transaction. 3908 */ 3909 static void btreeEndTransaction(Btree *p){ 3910 BtShared *pBt = p->pBt; 3911 sqlite3 *db = p->db; 3912 assert( sqlite3BtreeHoldsMutex(p) ); 3913 3914 #ifndef SQLITE_OMIT_AUTOVACUUM 3915 pBt->bDoTruncate = 0; 3916 #endif 3917 if( p->inTrans>TRANS_NONE && db->nVdbeRead>1 ){ 3918 /* If there are other active statements that belong to this database 3919 ** handle, downgrade to a read-only transaction. The other statements 3920 ** may still be reading from the database. */ 3921 downgradeAllSharedCacheTableLocks(p); 3922 p->inTrans = TRANS_READ; 3923 }else{ 3924 /* If the handle had any kind of transaction open, decrement the 3925 ** transaction count of the shared btree. If the transaction count 3926 ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused() 3927 ** call below will unlock the pager. */ 3928 if( p->inTrans!=TRANS_NONE ){ 3929 clearAllSharedCacheTableLocks(p); 3930 pBt->nTransaction--; 3931 if( 0==pBt->nTransaction ){ 3932 pBt->inTransaction = TRANS_NONE; 3933 } 3934 } 3935 3936 /* Set the current transaction state to TRANS_NONE and unlock the 3937 ** pager if this call closed the only read or write transaction. */ 3938 p->inTrans = TRANS_NONE; 3939 unlockBtreeIfUnused(pBt); 3940 } 3941 3942 btreeIntegrity(p); 3943 } 3944 3945 /* 3946 ** Commit the transaction currently in progress. 3947 ** 3948 ** This routine implements the second phase of a 2-phase commit. The 3949 ** sqlite3BtreeCommitPhaseOne() routine does the first phase and should 3950 ** be invoked prior to calling this routine. The sqlite3BtreeCommitPhaseOne() 3951 ** routine did all the work of writing information out to disk and flushing the 3952 ** contents so that they are written onto the disk platter. All this 3953 ** routine has to do is delete or truncate or zero the header in the 3954 ** the rollback journal (which causes the transaction to commit) and 3955 ** drop locks. 3956 ** 3957 ** Normally, if an error occurs while the pager layer is attempting to 3958 ** finalize the underlying journal file, this function returns an error and 3959 ** the upper layer will attempt a rollback. However, if the second argument 3960 ** is non-zero then this b-tree transaction is part of a multi-file 3961 ** transaction. In this case, the transaction has already been committed 3962 ** (by deleting a master journal file) and the caller will ignore this 3963 ** functions return code. So, even if an error occurs in the pager layer, 3964 ** reset the b-tree objects internal state to indicate that the write 3965 ** transaction has been closed. This is quite safe, as the pager will have 3966 ** transitioned to the error state. 3967 ** 3968 ** This will release the write lock on the database file. If there 3969 ** are no active cursors, it also releases the read lock. 3970 */ 3971 int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){ 3972 3973 if( p->inTrans==TRANS_NONE ) return SQLITE_OK; 3974 sqlite3BtreeEnter(p); 3975 btreeIntegrity(p); 3976 3977 /* If the handle has a write-transaction open, commit the shared-btrees 3978 ** transaction and set the shared state to TRANS_READ. 3979 */ 3980 if( p->inTrans==TRANS_WRITE ){ 3981 int rc; 3982 BtShared *pBt = p->pBt; 3983 assert( pBt->inTransaction==TRANS_WRITE ); 3984 assert( pBt->nTransaction>0 ); 3985 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager); 3986 if( rc!=SQLITE_OK && bCleanup==0 ){ 3987 sqlite3BtreeLeave(p); 3988 return rc; 3989 } 3990 p->iDataVersion--; /* Compensate for pPager->iDataVersion++; */ 3991 pBt->inTransaction = TRANS_READ; 3992 btreeClearHasContent(pBt); 3993 } 3994 3995 btreeEndTransaction(p); 3996 sqlite3BtreeLeave(p); 3997 return SQLITE_OK; 3998 } 3999 4000 /* 4001 ** Do both phases of a commit. 4002 */ 4003 int sqlite3BtreeCommit(Btree *p){ 4004 int rc; 4005 sqlite3BtreeEnter(p); 4006 rc = sqlite3BtreeCommitPhaseOne(p, 0); 4007 if( rc==SQLITE_OK ){ 4008 rc = sqlite3BtreeCommitPhaseTwo(p, 0); 4009 } 4010 sqlite3BtreeLeave(p); 4011 return rc; 4012 } 4013 4014 /* 4015 ** This routine sets the state to CURSOR_FAULT and the error 4016 ** code to errCode for every cursor on any BtShared that pBtree 4017 ** references. Or if the writeOnly flag is set to 1, then only 4018 ** trip write cursors and leave read cursors unchanged. 4019 ** 4020 ** Every cursor is a candidate to be tripped, including cursors 4021 ** that belong to other database connections that happen to be 4022 ** sharing the cache with pBtree. 4023 ** 4024 ** This routine gets called when a rollback occurs. If the writeOnly 4025 ** flag is true, then only write-cursors need be tripped - read-only 4026 ** cursors save their current positions so that they may continue 4027 ** following the rollback. Or, if writeOnly is false, all cursors are 4028 ** tripped. In general, writeOnly is false if the transaction being 4029 ** rolled back modified the database schema. In this case b-tree root 4030 ** pages may be moved or deleted from the database altogether, making 4031 ** it unsafe for read cursors to continue. 4032 ** 4033 ** If the writeOnly flag is true and an error is encountered while 4034 ** saving the current position of a read-only cursor, all cursors, 4035 ** including all read-cursors are tripped. 4036 ** 4037 ** SQLITE_OK is returned if successful, or if an error occurs while 4038 ** saving a cursor position, an SQLite error code. 4039 */ 4040 int sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode, int writeOnly){ 4041 BtCursor *p; 4042 int rc = SQLITE_OK; 4043 4044 assert( (writeOnly==0 || writeOnly==1) && BTCF_WriteFlag==1 ); 4045 if( pBtree ){ 4046 sqlite3BtreeEnter(pBtree); 4047 for(p=pBtree->pBt->pCursor; p; p=p->pNext){ 4048 if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){ 4049 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){ 4050 rc = saveCursorPosition(p); 4051 if( rc!=SQLITE_OK ){ 4052 (void)sqlite3BtreeTripAllCursors(pBtree, rc, 0); 4053 break; 4054 } 4055 } 4056 }else{ 4057 sqlite3BtreeClearCursor(p); 4058 p->eState = CURSOR_FAULT; 4059 p->skipNext = errCode; 4060 } 4061 btreeReleaseAllCursorPages(p); 4062 } 4063 sqlite3BtreeLeave(pBtree); 4064 } 4065 return rc; 4066 } 4067 4068 /* 4069 ** Rollback the transaction in progress. 4070 ** 4071 ** If tripCode is not SQLITE_OK then cursors will be invalidated (tripped). 4072 ** Only write cursors are tripped if writeOnly is true but all cursors are 4073 ** tripped if writeOnly is false. Any attempt to use 4074 ** a tripped cursor will result in an error. 4075 ** 4076 ** This will release the write lock on the database file. If there 4077 ** are no active cursors, it also releases the read lock. 4078 */ 4079 int sqlite3BtreeRollback(Btree *p, int tripCode, int writeOnly){ 4080 int rc; 4081 BtShared *pBt = p->pBt; 4082 MemPage *pPage1; 4083 4084 assert( writeOnly==1 || writeOnly==0 ); 4085 assert( tripCode==SQLITE_ABORT_ROLLBACK || tripCode==SQLITE_OK ); 4086 sqlite3BtreeEnter(p); 4087 if( tripCode==SQLITE_OK ){ 4088 rc = tripCode = saveAllCursors(pBt, 0, 0); 4089 if( rc ) writeOnly = 0; 4090 }else{ 4091 rc = SQLITE_OK; 4092 } 4093 if( tripCode ){ 4094 int rc2 = sqlite3BtreeTripAllCursors(p, tripCode, writeOnly); 4095 assert( rc==SQLITE_OK || (writeOnly==0 && rc2==SQLITE_OK) ); 4096 if( rc2!=SQLITE_OK ) rc = rc2; 4097 } 4098 btreeIntegrity(p); 4099 4100 if( p->inTrans==TRANS_WRITE ){ 4101 int rc2; 4102 4103 assert( TRANS_WRITE==pBt->inTransaction ); 4104 rc2 = sqlite3PagerRollback(pBt->pPager); 4105 if( rc2!=SQLITE_OK ){ 4106 rc = rc2; 4107 } 4108 4109 /* The rollback may have destroyed the pPage1->aData value. So 4110 ** call btreeGetPage() on page 1 again to make 4111 ** sure pPage1->aData is set correctly. */ 4112 if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){ 4113 int nPage = get4byte(28+(u8*)pPage1->aData); 4114 testcase( nPage==0 ); 4115 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage); 4116 testcase( pBt->nPage!=nPage ); 4117 pBt->nPage = nPage; 4118 releasePageOne(pPage1); 4119 } 4120 assert( countValidCursors(pBt, 1)==0 ); 4121 pBt->inTransaction = TRANS_READ; 4122 btreeClearHasContent(pBt); 4123 } 4124 4125 btreeEndTransaction(p); 4126 sqlite3BtreeLeave(p); 4127 return rc; 4128 } 4129 4130 /* 4131 ** Start a statement subtransaction. The subtransaction can be rolled 4132 ** back independently of the main transaction. You must start a transaction 4133 ** before starting a subtransaction. The subtransaction is ended automatically 4134 ** if the main transaction commits or rolls back. 4135 ** 4136 ** Statement subtransactions are used around individual SQL statements 4137 ** that are contained within a BEGIN...COMMIT block. If a constraint 4138 ** error occurs within the statement, the effect of that one statement 4139 ** can be rolled back without having to rollback the entire transaction. 4140 ** 4141 ** A statement sub-transaction is implemented as an anonymous savepoint. The 4142 ** value passed as the second parameter is the total number of savepoints, 4143 ** including the new anonymous savepoint, open on the B-Tree. i.e. if there 4144 ** are no active savepoints and no other statement-transactions open, 4145 ** iStatement is 1. This anonymous savepoint can be released or rolled back 4146 ** using the sqlite3BtreeSavepoint() function. 4147 */ 4148 int sqlite3BtreeBeginStmt(Btree *p, int iStatement){ 4149 int rc; 4150 BtShared *pBt = p->pBt; 4151 sqlite3BtreeEnter(p); 4152 assert( p->inTrans==TRANS_WRITE ); 4153 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 ); 4154 assert( iStatement>0 ); 4155 assert( iStatement>p->db->nSavepoint ); 4156 assert( pBt->inTransaction==TRANS_WRITE ); 4157 /* At the pager level, a statement transaction is a savepoint with 4158 ** an index greater than all savepoints created explicitly using 4159 ** SQL statements. It is illegal to open, release or rollback any 4160 ** such savepoints while the statement transaction savepoint is active. 4161 */ 4162 rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement); 4163 sqlite3BtreeLeave(p); 4164 return rc; 4165 } 4166 4167 /* 4168 ** The second argument to this function, op, is always SAVEPOINT_ROLLBACK 4169 ** or SAVEPOINT_RELEASE. This function either releases or rolls back the 4170 ** savepoint identified by parameter iSavepoint, depending on the value 4171 ** of op. 4172 ** 4173 ** Normally, iSavepoint is greater than or equal to zero. However, if op is 4174 ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the 4175 ** contents of the entire transaction are rolled back. This is different 4176 ** from a normal transaction rollback, as no locks are released and the 4177 ** transaction remains open. 4178 */ 4179 int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){ 4180 int rc = SQLITE_OK; 4181 if( p && p->inTrans==TRANS_WRITE ){ 4182 BtShared *pBt = p->pBt; 4183 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK ); 4184 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) ); 4185 sqlite3BtreeEnter(p); 4186 if( op==SAVEPOINT_ROLLBACK ){ 4187 rc = saveAllCursors(pBt, 0, 0); 4188 } 4189 if( rc==SQLITE_OK ){ 4190 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint); 4191 } 4192 if( rc==SQLITE_OK ){ 4193 if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){ 4194 pBt->nPage = 0; 4195 } 4196 rc = newDatabase(pBt); 4197 pBt->nPage = get4byte(28 + pBt->pPage1->aData); 4198 4199 /* The database size was written into the offset 28 of the header 4200 ** when the transaction started, so we know that the value at offset 4201 ** 28 is nonzero. */ 4202 assert( pBt->nPage>0 ); 4203 } 4204 sqlite3BtreeLeave(p); 4205 } 4206 return rc; 4207 } 4208 4209 /* 4210 ** Create a new cursor for the BTree whose root is on the page 4211 ** iTable. If a read-only cursor is requested, it is assumed that 4212 ** the caller already has at least a read-only transaction open 4213 ** on the database already. If a write-cursor is requested, then 4214 ** the caller is assumed to have an open write transaction. 4215 ** 4216 ** If the BTREE_WRCSR bit of wrFlag is clear, then the cursor can only 4217 ** be used for reading. If the BTREE_WRCSR bit is set, then the cursor 4218 ** can be used for reading or for writing if other conditions for writing 4219 ** are also met. These are the conditions that must be met in order 4220 ** for writing to be allowed: 4221 ** 4222 ** 1: The cursor must have been opened with wrFlag containing BTREE_WRCSR 4223 ** 4224 ** 2: Other database connections that share the same pager cache 4225 ** but which are not in the READ_UNCOMMITTED state may not have 4226 ** cursors open with wrFlag==0 on the same table. Otherwise 4227 ** the changes made by this write cursor would be visible to 4228 ** the read cursors in the other database connection. 4229 ** 4230 ** 3: The database must be writable (not on read-only media) 4231 ** 4232 ** 4: There must be an active transaction. 4233 ** 4234 ** The BTREE_FORDELETE bit of wrFlag may optionally be set if BTREE_WRCSR 4235 ** is set. If FORDELETE is set, that is a hint to the implementation that 4236 ** this cursor will only be used to seek to and delete entries of an index 4237 ** as part of a larger DELETE statement. The FORDELETE hint is not used by 4238 ** this implementation. But in a hypothetical alternative storage engine 4239 ** in which index entries are automatically deleted when corresponding table 4240 ** rows are deleted, the FORDELETE flag is a hint that all SEEK and DELETE 4241 ** operations on this cursor can be no-ops and all READ operations can 4242 ** return a null row (2-bytes: 0x01 0x00). 4243 ** 4244 ** No checking is done to make sure that page iTable really is the 4245 ** root page of a b-tree. If it is not, then the cursor acquired 4246 ** will not work correctly. 4247 ** 4248 ** It is assumed that the sqlite3BtreeCursorZero() has been called 4249 ** on pCur to initialize the memory space prior to invoking this routine. 4250 */ 4251 static int btreeCursor( 4252 Btree *p, /* The btree */ 4253 int iTable, /* Root page of table to open */ 4254 int wrFlag, /* 1 to write. 0 read-only */ 4255 struct KeyInfo *pKeyInfo, /* First arg to comparison function */ 4256 BtCursor *pCur /* Space for new cursor */ 4257 ){ 4258 BtShared *pBt = p->pBt; /* Shared b-tree handle */ 4259 BtCursor *pX; /* Looping over other all cursors */ 4260 4261 assert( sqlite3BtreeHoldsMutex(p) ); 4262 assert( wrFlag==0 4263 || wrFlag==BTREE_WRCSR 4264 || wrFlag==(BTREE_WRCSR|BTREE_FORDELETE) 4265 ); 4266 4267 /* The following assert statements verify that if this is a sharable 4268 ** b-tree database, the connection is holding the required table locks, 4269 ** and that no other connection has any open cursor that conflicts with 4270 ** this lock. */ 4271 assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, (wrFlag?2:1)) ); 4272 assert( wrFlag==0 || !hasReadConflicts(p, iTable) ); 4273 4274 /* Assert that the caller has opened the required transaction. */ 4275 assert( p->inTrans>TRANS_NONE ); 4276 assert( wrFlag==0 || p->inTrans==TRANS_WRITE ); 4277 assert( pBt->pPage1 && pBt->pPage1->aData ); 4278 assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 ); 4279 4280 if( wrFlag ){ 4281 allocateTempSpace(pBt); 4282 if( pBt->pTmpSpace==0 ) return SQLITE_NOMEM_BKPT; 4283 } 4284 if( iTable==1 && btreePagecount(pBt)==0 ){ 4285 assert( wrFlag==0 ); 4286 iTable = 0; 4287 } 4288 4289 /* Now that no other errors can occur, finish filling in the BtCursor 4290 ** variables and link the cursor into the BtShared list. */ 4291 pCur->pgnoRoot = (Pgno)iTable; 4292 pCur->iPage = -1; 4293 pCur->pKeyInfo = pKeyInfo; 4294 pCur->pBtree = p; 4295 pCur->pBt = pBt; 4296 pCur->curFlags = wrFlag ? BTCF_WriteFlag : 0; 4297 pCur->curPagerFlags = wrFlag ? 0 : PAGER_GET_READONLY; 4298 /* If there are two or more cursors on the same btree, then all such 4299 ** cursors *must* have the BTCF_Multiple flag set. */ 4300 for(pX=pBt->pCursor; pX; pX=pX->pNext){ 4301 if( pX->pgnoRoot==(Pgno)iTable ){ 4302 pX->curFlags |= BTCF_Multiple; 4303 pCur->curFlags |= BTCF_Multiple; 4304 } 4305 } 4306 pCur->pNext = pBt->pCursor; 4307 pBt->pCursor = pCur; 4308 pCur->eState = CURSOR_INVALID; 4309 return SQLITE_OK; 4310 } 4311 int sqlite3BtreeCursor( 4312 Btree *p, /* The btree */ 4313 int iTable, /* Root page of table to open */ 4314 int wrFlag, /* 1 to write. 0 read-only */ 4315 struct KeyInfo *pKeyInfo, /* First arg to xCompare() */ 4316 BtCursor *pCur /* Write new cursor here */ 4317 ){ 4318 int rc; 4319 if( iTable<1 ){ 4320 rc = SQLITE_CORRUPT_BKPT; 4321 }else{ 4322 sqlite3BtreeEnter(p); 4323 rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur); 4324 sqlite3BtreeLeave(p); 4325 } 4326 return rc; 4327 } 4328 4329 /* 4330 ** Return the size of a BtCursor object in bytes. 4331 ** 4332 ** This interfaces is needed so that users of cursors can preallocate 4333 ** sufficient storage to hold a cursor. The BtCursor object is opaque 4334 ** to users so they cannot do the sizeof() themselves - they must call 4335 ** this routine. 4336 */ 4337 int sqlite3BtreeCursorSize(void){ 4338 return ROUND8(sizeof(BtCursor)); 4339 } 4340 4341 /* 4342 ** Initialize memory that will be converted into a BtCursor object. 4343 ** 4344 ** The simple approach here would be to memset() the entire object 4345 ** to zero. But it turns out that the apPage[] and aiIdx[] arrays 4346 ** do not need to be zeroed and they are large, so we can save a lot 4347 ** of run-time by skipping the initialization of those elements. 4348 */ 4349 void sqlite3BtreeCursorZero(BtCursor *p){ 4350 memset(p, 0, offsetof(BtCursor, BTCURSOR_FIRST_UNINIT)); 4351 } 4352 4353 /* 4354 ** Close a cursor. The read lock on the database file is released 4355 ** when the last cursor is closed. 4356 */ 4357 int sqlite3BtreeCloseCursor(BtCursor *pCur){ 4358 Btree *pBtree = pCur->pBtree; 4359 if( pBtree ){ 4360 BtShared *pBt = pCur->pBt; 4361 sqlite3BtreeEnter(pBtree); 4362 assert( pBt->pCursor!=0 ); 4363 if( pBt->pCursor==pCur ){ 4364 pBt->pCursor = pCur->pNext; 4365 }else{ 4366 BtCursor *pPrev = pBt->pCursor; 4367 do{ 4368 if( pPrev->pNext==pCur ){ 4369 pPrev->pNext = pCur->pNext; 4370 break; 4371 } 4372 pPrev = pPrev->pNext; 4373 }while( ALWAYS(pPrev) ); 4374 } 4375 btreeReleaseAllCursorPages(pCur); 4376 unlockBtreeIfUnused(pBt); 4377 sqlite3_free(pCur->aOverflow); 4378 sqlite3_free(pCur->pKey); 4379 sqlite3BtreeLeave(pBtree); 4380 } 4381 return SQLITE_OK; 4382 } 4383 4384 /* 4385 ** Make sure the BtCursor* given in the argument has a valid 4386 ** BtCursor.info structure. If it is not already valid, call 4387 ** btreeParseCell() to fill it in. 4388 ** 4389 ** BtCursor.info is a cache of the information in the current cell. 4390 ** Using this cache reduces the number of calls to btreeParseCell(). 4391 */ 4392 #ifndef NDEBUG 4393 static int cellInfoEqual(CellInfo *a, CellInfo *b){ 4394 if( a->nKey!=b->nKey ) return 0; 4395 if( a->pPayload!=b->pPayload ) return 0; 4396 if( a->nPayload!=b->nPayload ) return 0; 4397 if( a->nLocal!=b->nLocal ) return 0; 4398 if( a->nSize!=b->nSize ) return 0; 4399 return 1; 4400 } 4401 static void assertCellInfo(BtCursor *pCur){ 4402 CellInfo info; 4403 memset(&info, 0, sizeof(info)); 4404 btreeParseCell(pCur->pPage, pCur->ix, &info); 4405 assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) ); 4406 } 4407 #else 4408 #define assertCellInfo(x) 4409 #endif 4410 static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){ 4411 if( pCur->info.nSize==0 ){ 4412 pCur->curFlags |= BTCF_ValidNKey; 4413 btreeParseCell(pCur->pPage,pCur->ix,&pCur->info); 4414 }else{ 4415 assertCellInfo(pCur); 4416 } 4417 } 4418 4419 #ifndef NDEBUG /* The next routine used only within assert() statements */ 4420 /* 4421 ** Return true if the given BtCursor is valid. A valid cursor is one 4422 ** that is currently pointing to a row in a (non-empty) table. 4423 ** This is a verification routine is used only within assert() statements. 4424 */ 4425 int sqlite3BtreeCursorIsValid(BtCursor *pCur){ 4426 return pCur && pCur->eState==CURSOR_VALID; 4427 } 4428 #endif /* NDEBUG */ 4429 int sqlite3BtreeCursorIsValidNN(BtCursor *pCur){ 4430 assert( pCur!=0 ); 4431 return pCur->eState==CURSOR_VALID; 4432 } 4433 4434 /* 4435 ** Return the value of the integer key or "rowid" for a table btree. 4436 ** This routine is only valid for a cursor that is pointing into a 4437 ** ordinary table btree. If the cursor points to an index btree or 4438 ** is invalid, the result of this routine is undefined. 4439 */ 4440 i64 sqlite3BtreeIntegerKey(BtCursor *pCur){ 4441 assert( cursorHoldsMutex(pCur) ); 4442 assert( pCur->eState==CURSOR_VALID ); 4443 assert( pCur->curIntKey ); 4444 getCellInfo(pCur); 4445 return pCur->info.nKey; 4446 } 4447 4448 #ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC 4449 /* 4450 ** Return the offset into the database file for the start of the 4451 ** payload to which the cursor is pointing. 4452 */ 4453 i64 sqlite3BtreeOffset(BtCursor *pCur){ 4454 assert( cursorHoldsMutex(pCur) ); 4455 assert( pCur->eState==CURSOR_VALID ); 4456 getCellInfo(pCur); 4457 return (i64)pCur->pBt->pageSize*((i64)pCur->pPage->pgno - 1) + 4458 (i64)(pCur->info.pPayload - pCur->pPage->aData); 4459 } 4460 #endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */ 4461 4462 /* 4463 ** Return the number of bytes of payload for the entry that pCur is 4464 ** currently pointing to. For table btrees, this will be the amount 4465 ** of data. For index btrees, this will be the size of the key. 4466 ** 4467 ** The caller must guarantee that the cursor is pointing to a non-NULL 4468 ** valid entry. In other words, the calling procedure must guarantee 4469 ** that the cursor has Cursor.eState==CURSOR_VALID. 4470 */ 4471 u32 sqlite3BtreePayloadSize(BtCursor *pCur){ 4472 assert( cursorHoldsMutex(pCur) ); 4473 assert( pCur->eState==CURSOR_VALID ); 4474 getCellInfo(pCur); 4475 return pCur->info.nPayload; 4476 } 4477 4478 /* 4479 ** Given the page number of an overflow page in the database (parameter 4480 ** ovfl), this function finds the page number of the next page in the 4481 ** linked list of overflow pages. If possible, it uses the auto-vacuum 4482 ** pointer-map data instead of reading the content of page ovfl to do so. 4483 ** 4484 ** If an error occurs an SQLite error code is returned. Otherwise: 4485 ** 4486 ** The page number of the next overflow page in the linked list is 4487 ** written to *pPgnoNext. If page ovfl is the last page in its linked 4488 ** list, *pPgnoNext is set to zero. 4489 ** 4490 ** If ppPage is not NULL, and a reference to the MemPage object corresponding 4491 ** to page number pOvfl was obtained, then *ppPage is set to point to that 4492 ** reference. It is the responsibility of the caller to call releasePage() 4493 ** on *ppPage to free the reference. In no reference was obtained (because 4494 ** the pointer-map was used to obtain the value for *pPgnoNext), then 4495 ** *ppPage is set to zero. 4496 */ 4497 static int getOverflowPage( 4498 BtShared *pBt, /* The database file */ 4499 Pgno ovfl, /* Current overflow page number */ 4500 MemPage **ppPage, /* OUT: MemPage handle (may be NULL) */ 4501 Pgno *pPgnoNext /* OUT: Next overflow page number */ 4502 ){ 4503 Pgno next = 0; 4504 MemPage *pPage = 0; 4505 int rc = SQLITE_OK; 4506 4507 assert( sqlite3_mutex_held(pBt->mutex) ); 4508 assert(pPgnoNext); 4509 4510 #ifndef SQLITE_OMIT_AUTOVACUUM 4511 /* Try to find the next page in the overflow list using the 4512 ** autovacuum pointer-map pages. Guess that the next page in 4513 ** the overflow list is page number (ovfl+1). If that guess turns 4514 ** out to be wrong, fall back to loading the data of page 4515 ** number ovfl to determine the next page number. 4516 */ 4517 if( pBt->autoVacuum ){ 4518 Pgno pgno; 4519 Pgno iGuess = ovfl+1; 4520 u8 eType; 4521 4522 while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){ 4523 iGuess++; 4524 } 4525 4526 if( iGuess<=btreePagecount(pBt) ){ 4527 rc = ptrmapGet(pBt, iGuess, &eType, &pgno); 4528 if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){ 4529 next = iGuess; 4530 rc = SQLITE_DONE; 4531 } 4532 } 4533 } 4534 #endif 4535 4536 assert( next==0 || rc==SQLITE_DONE ); 4537 if( rc==SQLITE_OK ){ 4538 rc = btreeGetPage(pBt, ovfl, &pPage, (ppPage==0) ? PAGER_GET_READONLY : 0); 4539 assert( rc==SQLITE_OK || pPage==0 ); 4540 if( rc==SQLITE_OK ){ 4541 next = get4byte(pPage->aData); 4542 } 4543 } 4544 4545 *pPgnoNext = next; 4546 if( ppPage ){ 4547 *ppPage = pPage; 4548 }else{ 4549 releasePage(pPage); 4550 } 4551 return (rc==SQLITE_DONE ? SQLITE_OK : rc); 4552 } 4553 4554 /* 4555 ** Copy data from a buffer to a page, or from a page to a buffer. 4556 ** 4557 ** pPayload is a pointer to data stored on database page pDbPage. 4558 ** If argument eOp is false, then nByte bytes of data are copied 4559 ** from pPayload to the buffer pointed at by pBuf. If eOp is true, 4560 ** then sqlite3PagerWrite() is called on pDbPage and nByte bytes 4561 ** of data are copied from the buffer pBuf to pPayload. 4562 ** 4563 ** SQLITE_OK is returned on success, otherwise an error code. 4564 */ 4565 static int copyPayload( 4566 void *pPayload, /* Pointer to page data */ 4567 void *pBuf, /* Pointer to buffer */ 4568 int nByte, /* Number of bytes to copy */ 4569 int eOp, /* 0 -> copy from page, 1 -> copy to page */ 4570 DbPage *pDbPage /* Page containing pPayload */ 4571 ){ 4572 if( eOp ){ 4573 /* Copy data from buffer to page (a write operation) */ 4574 int rc = sqlite3PagerWrite(pDbPage); 4575 if( rc!=SQLITE_OK ){ 4576 return rc; 4577 } 4578 memcpy(pPayload, pBuf, nByte); 4579 }else{ 4580 /* Copy data from page to buffer (a read operation) */ 4581 memcpy(pBuf, pPayload, nByte); 4582 } 4583 return SQLITE_OK; 4584 } 4585 4586 /* 4587 ** This function is used to read or overwrite payload information 4588 ** for the entry that the pCur cursor is pointing to. The eOp 4589 ** argument is interpreted as follows: 4590 ** 4591 ** 0: The operation is a read. Populate the overflow cache. 4592 ** 1: The operation is a write. Populate the overflow cache. 4593 ** 4594 ** A total of "amt" bytes are read or written beginning at "offset". 4595 ** Data is read to or from the buffer pBuf. 4596 ** 4597 ** The content being read or written might appear on the main page 4598 ** or be scattered out on multiple overflow pages. 4599 ** 4600 ** If the current cursor entry uses one or more overflow pages 4601 ** this function may allocate space for and lazily populate 4602 ** the overflow page-list cache array (BtCursor.aOverflow). 4603 ** Subsequent calls use this cache to make seeking to the supplied offset 4604 ** more efficient. 4605 ** 4606 ** Once an overflow page-list cache has been allocated, it must be 4607 ** invalidated if some other cursor writes to the same table, or if 4608 ** the cursor is moved to a different row. Additionally, in auto-vacuum 4609 ** mode, the following events may invalidate an overflow page-list cache. 4610 ** 4611 ** * An incremental vacuum, 4612 ** * A commit in auto_vacuum="full" mode, 4613 ** * Creating a table (may require moving an overflow page). 4614 */ 4615 static int accessPayload( 4616 BtCursor *pCur, /* Cursor pointing to entry to read from */ 4617 u32 offset, /* Begin reading this far into payload */ 4618 u32 amt, /* Read this many bytes */ 4619 unsigned char *pBuf, /* Write the bytes into this buffer */ 4620 int eOp /* zero to read. non-zero to write. */ 4621 ){ 4622 unsigned char *aPayload; 4623 int rc = SQLITE_OK; 4624 int iIdx = 0; 4625 MemPage *pPage = pCur->pPage; /* Btree page of current entry */ 4626 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */ 4627 #ifdef SQLITE_DIRECT_OVERFLOW_READ 4628 unsigned char * const pBufStart = pBuf; /* Start of original out buffer */ 4629 #endif 4630 4631 assert( pPage ); 4632 assert( eOp==0 || eOp==1 ); 4633 assert( pCur->eState==CURSOR_VALID ); 4634 assert( pCur->ix<pPage->nCell ); 4635 assert( cursorHoldsMutex(pCur) ); 4636 4637 getCellInfo(pCur); 4638 aPayload = pCur->info.pPayload; 4639 assert( offset+amt <= pCur->info.nPayload ); 4640 4641 assert( aPayload > pPage->aData ); 4642 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){ 4643 /* Trying to read or write past the end of the data is an error. The 4644 ** conditional above is really: 4645 ** &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize] 4646 ** but is recast into its current form to avoid integer overflow problems 4647 */ 4648 return SQLITE_CORRUPT_PAGE(pPage); 4649 } 4650 4651 /* Check if data must be read/written to/from the btree page itself. */ 4652 if( offset<pCur->info.nLocal ){ 4653 int a = amt; 4654 if( a+offset>pCur->info.nLocal ){ 4655 a = pCur->info.nLocal - offset; 4656 } 4657 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage); 4658 offset = 0; 4659 pBuf += a; 4660 amt -= a; 4661 }else{ 4662 offset -= pCur->info.nLocal; 4663 } 4664 4665 4666 if( rc==SQLITE_OK && amt>0 ){ 4667 const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */ 4668 Pgno nextPage; 4669 4670 nextPage = get4byte(&aPayload[pCur->info.nLocal]); 4671 4672 /* If the BtCursor.aOverflow[] has not been allocated, allocate it now. 4673 ** 4674 ** The aOverflow[] array is sized at one entry for each overflow page 4675 ** in the overflow chain. The page number of the first overflow page is 4676 ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array 4677 ** means "not yet known" (the cache is lazily populated). 4678 */ 4679 if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){ 4680 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize; 4681 if( pCur->aOverflow==0 4682 || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow) 4683 ){ 4684 Pgno *aNew = (Pgno*)sqlite3Realloc( 4685 pCur->aOverflow, nOvfl*2*sizeof(Pgno) 4686 ); 4687 if( aNew==0 ){ 4688 return SQLITE_NOMEM_BKPT; 4689 }else{ 4690 pCur->aOverflow = aNew; 4691 } 4692 } 4693 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno)); 4694 pCur->curFlags |= BTCF_ValidOvfl; 4695 }else{ 4696 /* If the overflow page-list cache has been allocated and the 4697 ** entry for the first required overflow page is valid, skip 4698 ** directly to it. 4699 */ 4700 if( pCur->aOverflow[offset/ovflSize] ){ 4701 iIdx = (offset/ovflSize); 4702 nextPage = pCur->aOverflow[iIdx]; 4703 offset = (offset%ovflSize); 4704 } 4705 } 4706 4707 assert( rc==SQLITE_OK && amt>0 ); 4708 while( nextPage ){ 4709 /* If required, populate the overflow page-list cache. */ 4710 assert( pCur->aOverflow[iIdx]==0 4711 || pCur->aOverflow[iIdx]==nextPage 4712 || CORRUPT_DB ); 4713 pCur->aOverflow[iIdx] = nextPage; 4714 4715 if( offset>=ovflSize ){ 4716 /* The only reason to read this page is to obtain the page 4717 ** number for the next page in the overflow chain. The page 4718 ** data is not required. So first try to lookup the overflow 4719 ** page-list cache, if any, then fall back to the getOverflowPage() 4720 ** function. 4721 */ 4722 assert( pCur->curFlags & BTCF_ValidOvfl ); 4723 assert( pCur->pBtree->db==pBt->db ); 4724 if( pCur->aOverflow[iIdx+1] ){ 4725 nextPage = pCur->aOverflow[iIdx+1]; 4726 }else{ 4727 rc = getOverflowPage(pBt, nextPage, 0, &nextPage); 4728 } 4729 offset -= ovflSize; 4730 }else{ 4731 /* Need to read this page properly. It contains some of the 4732 ** range of data that is being read (eOp==0) or written (eOp!=0). 4733 */ 4734 #ifdef SQLITE_DIRECT_OVERFLOW_READ 4735 sqlite3_file *fd; /* File from which to do direct overflow read */ 4736 #endif 4737 int a = amt; 4738 if( a + offset > ovflSize ){ 4739 a = ovflSize - offset; 4740 } 4741 4742 #ifdef SQLITE_DIRECT_OVERFLOW_READ 4743 /* If all the following are true: 4744 ** 4745 ** 1) this is a read operation, and 4746 ** 2) data is required from the start of this overflow page, and 4747 ** 3) there is no open write-transaction, and 4748 ** 4) the database is file-backed, and 4749 ** 5) the page is not in the WAL file 4750 ** 6) at least 4 bytes have already been read into the output buffer 4751 ** 4752 ** then data can be read directly from the database file into the 4753 ** output buffer, bypassing the page-cache altogether. This speeds 4754 ** up loading large records that span many overflow pages. 4755 */ 4756 if( eOp==0 /* (1) */ 4757 && offset==0 /* (2) */ 4758 && pBt->inTransaction==TRANS_READ /* (3) */ 4759 && (fd = sqlite3PagerFile(pBt->pPager))->pMethods /* (4) */ 4760 && 0==sqlite3PagerUseWal(pBt->pPager, nextPage) /* (5) */ 4761 && &pBuf[-4]>=pBufStart /* (6) */ 4762 ){ 4763 u8 aSave[4]; 4764 u8 *aWrite = &pBuf[-4]; 4765 assert( aWrite>=pBufStart ); /* due to (6) */ 4766 memcpy(aSave, aWrite, 4); 4767 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1)); 4768 nextPage = get4byte(aWrite); 4769 memcpy(aWrite, aSave, 4); 4770 }else 4771 #endif 4772 4773 { 4774 DbPage *pDbPage; 4775 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage, 4776 (eOp==0 ? PAGER_GET_READONLY : 0) 4777 ); 4778 if( rc==SQLITE_OK ){ 4779 aPayload = sqlite3PagerGetData(pDbPage); 4780 nextPage = get4byte(aPayload); 4781 rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage); 4782 sqlite3PagerUnref(pDbPage); 4783 offset = 0; 4784 } 4785 } 4786 amt -= a; 4787 if( amt==0 ) return rc; 4788 pBuf += a; 4789 } 4790 if( rc ) break; 4791 iIdx++; 4792 } 4793 } 4794 4795 if( rc==SQLITE_OK && amt>0 ){ 4796 /* Overflow chain ends prematurely */ 4797 return SQLITE_CORRUPT_PAGE(pPage); 4798 } 4799 return rc; 4800 } 4801 4802 /* 4803 ** Read part of the payload for the row at which that cursor pCur is currently 4804 ** pointing. "amt" bytes will be transferred into pBuf[]. The transfer 4805 ** begins at "offset". 4806 ** 4807 ** pCur can be pointing to either a table or an index b-tree. 4808 ** If pointing to a table btree, then the content section is read. If 4809 ** pCur is pointing to an index b-tree then the key section is read. 4810 ** 4811 ** For sqlite3BtreePayload(), the caller must ensure that pCur is pointing 4812 ** to a valid row in the table. For sqlite3BtreePayloadChecked(), the 4813 ** cursor might be invalid or might need to be restored before being read. 4814 ** 4815 ** Return SQLITE_OK on success or an error code if anything goes 4816 ** wrong. An error is returned if "offset+amt" is larger than 4817 ** the available payload. 4818 */ 4819 int sqlite3BtreePayload(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ 4820 assert( cursorHoldsMutex(pCur) ); 4821 assert( pCur->eState==CURSOR_VALID ); 4822 assert( pCur->iPage>=0 && pCur->pPage ); 4823 assert( pCur->ix<pCur->pPage->nCell ); 4824 return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0); 4825 } 4826 4827 /* 4828 ** This variant of sqlite3BtreePayload() works even if the cursor has not 4829 ** in the CURSOR_VALID state. It is only used by the sqlite3_blob_read() 4830 ** interface. 4831 */ 4832 #ifndef SQLITE_OMIT_INCRBLOB 4833 static SQLITE_NOINLINE int accessPayloadChecked( 4834 BtCursor *pCur, 4835 u32 offset, 4836 u32 amt, 4837 void *pBuf 4838 ){ 4839 int rc; 4840 if ( pCur->eState==CURSOR_INVALID ){ 4841 return SQLITE_ABORT; 4842 } 4843 assert( cursorOwnsBtShared(pCur) ); 4844 rc = btreeRestoreCursorPosition(pCur); 4845 return rc ? rc : accessPayload(pCur, offset, amt, pBuf, 0); 4846 } 4847 int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ 4848 if( pCur->eState==CURSOR_VALID ){ 4849 assert( cursorOwnsBtShared(pCur) ); 4850 return accessPayload(pCur, offset, amt, pBuf, 0); 4851 }else{ 4852 return accessPayloadChecked(pCur, offset, amt, pBuf); 4853 } 4854 } 4855 #endif /* SQLITE_OMIT_INCRBLOB */ 4856 4857 /* 4858 ** Return a pointer to payload information from the entry that the 4859 ** pCur cursor is pointing to. The pointer is to the beginning of 4860 ** the key if index btrees (pPage->intKey==0) and is the data for 4861 ** table btrees (pPage->intKey==1). The number of bytes of available 4862 ** key/data is written into *pAmt. If *pAmt==0, then the value 4863 ** returned will not be a valid pointer. 4864 ** 4865 ** This routine is an optimization. It is common for the entire key 4866 ** and data to fit on the local page and for there to be no overflow 4867 ** pages. When that is so, this routine can be used to access the 4868 ** key and data without making a copy. If the key and/or data spills 4869 ** onto overflow pages, then accessPayload() must be used to reassemble 4870 ** the key/data and copy it into a preallocated buffer. 4871 ** 4872 ** The pointer returned by this routine looks directly into the cached 4873 ** page of the database. The data might change or move the next time 4874 ** any btree routine is called. 4875 */ 4876 static const void *fetchPayload( 4877 BtCursor *pCur, /* Cursor pointing to entry to read from */ 4878 u32 *pAmt /* Write the number of available bytes here */ 4879 ){ 4880 int amt; 4881 assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage); 4882 assert( pCur->eState==CURSOR_VALID ); 4883 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); 4884 assert( cursorOwnsBtShared(pCur) ); 4885 assert( pCur->ix<pCur->pPage->nCell ); 4886 assert( pCur->info.nSize>0 ); 4887 assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB ); 4888 assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||CORRUPT_DB); 4889 amt = pCur->info.nLocal; 4890 if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){ 4891 /* There is too little space on the page for the expected amount 4892 ** of local content. Database must be corrupt. */ 4893 assert( CORRUPT_DB ); 4894 amt = MAX(0, (int)(pCur->pPage->aDataEnd - pCur->info.pPayload)); 4895 } 4896 *pAmt = (u32)amt; 4897 return (void*)pCur->info.pPayload; 4898 } 4899 4900 4901 /* 4902 ** For the entry that cursor pCur is point to, return as 4903 ** many bytes of the key or data as are available on the local 4904 ** b-tree page. Write the number of available bytes into *pAmt. 4905 ** 4906 ** The pointer returned is ephemeral. The key/data may move 4907 ** or be destroyed on the next call to any Btree routine, 4908 ** including calls from other threads against the same cache. 4909 ** Hence, a mutex on the BtShared should be held prior to calling 4910 ** this routine. 4911 ** 4912 ** These routines is used to get quick access to key and data 4913 ** in the common case where no overflow pages are used. 4914 */ 4915 const void *sqlite3BtreePayloadFetch(BtCursor *pCur, u32 *pAmt){ 4916 return fetchPayload(pCur, pAmt); 4917 } 4918 4919 4920 /* 4921 ** Move the cursor down to a new child page. The newPgno argument is the 4922 ** page number of the child page to move to. 4923 ** 4924 ** This function returns SQLITE_CORRUPT if the page-header flags field of 4925 ** the new child page does not match the flags field of the parent (i.e. 4926 ** if an intkey page appears to be the parent of a non-intkey page, or 4927 ** vice-versa). 4928 */ 4929 static int moveToChild(BtCursor *pCur, u32 newPgno){ 4930 BtShared *pBt = pCur->pBt; 4931 4932 assert( cursorOwnsBtShared(pCur) ); 4933 assert( pCur->eState==CURSOR_VALID ); 4934 assert( pCur->iPage<BTCURSOR_MAX_DEPTH ); 4935 assert( pCur->iPage>=0 ); 4936 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){ 4937 return SQLITE_CORRUPT_BKPT; 4938 } 4939 pCur->info.nSize = 0; 4940 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); 4941 pCur->aiIdx[pCur->iPage] = pCur->ix; 4942 pCur->apPage[pCur->iPage] = pCur->pPage; 4943 pCur->ix = 0; 4944 pCur->iPage++; 4945 return getAndInitPage(pBt, newPgno, &pCur->pPage, pCur, pCur->curPagerFlags); 4946 } 4947 4948 #ifdef SQLITE_DEBUG 4949 /* 4950 ** Page pParent is an internal (non-leaf) tree page. This function 4951 ** asserts that page number iChild is the left-child if the iIdx'th 4952 ** cell in page pParent. Or, if iIdx is equal to the total number of 4953 ** cells in pParent, that page number iChild is the right-child of 4954 ** the page. 4955 */ 4956 static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){ 4957 if( CORRUPT_DB ) return; /* The conditions tested below might not be true 4958 ** in a corrupt database */ 4959 assert( iIdx<=pParent->nCell ); 4960 if( iIdx==pParent->nCell ){ 4961 assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild ); 4962 }else{ 4963 assert( get4byte(findCell(pParent, iIdx))==iChild ); 4964 } 4965 } 4966 #else 4967 # define assertParentIndex(x,y,z) 4968 #endif 4969 4970 /* 4971 ** Move the cursor up to the parent page. 4972 ** 4973 ** pCur->idx is set to the cell index that contains the pointer 4974 ** to the page we are coming from. If we are coming from the 4975 ** right-most child page then pCur->idx is set to one more than 4976 ** the largest cell index. 4977 */ 4978 static void moveToParent(BtCursor *pCur){ 4979 MemPage *pLeaf; 4980 assert( cursorOwnsBtShared(pCur) ); 4981 assert( pCur->eState==CURSOR_VALID ); 4982 assert( pCur->iPage>0 ); 4983 assert( pCur->pPage ); 4984 assertParentIndex( 4985 pCur->apPage[pCur->iPage-1], 4986 pCur->aiIdx[pCur->iPage-1], 4987 pCur->pPage->pgno 4988 ); 4989 testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell ); 4990 pCur->info.nSize = 0; 4991 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); 4992 pCur->ix = pCur->aiIdx[pCur->iPage-1]; 4993 pLeaf = pCur->pPage; 4994 pCur->pPage = pCur->apPage[--pCur->iPage]; 4995 releasePageNotNull(pLeaf); 4996 } 4997 4998 /* 4999 ** Move the cursor to point to the root page of its b-tree structure. 5000 ** 5001 ** If the table has a virtual root page, then the cursor is moved to point 5002 ** to the virtual root page instead of the actual root page. A table has a 5003 ** virtual root page when the actual root page contains no cells and a 5004 ** single child page. This can only happen with the table rooted at page 1. 5005 ** 5006 ** If the b-tree structure is empty, the cursor state is set to 5007 ** CURSOR_INVALID and this routine returns SQLITE_EMPTY. Otherwise, 5008 ** the cursor is set to point to the first cell located on the root 5009 ** (or virtual root) page and the cursor state is set to CURSOR_VALID. 5010 ** 5011 ** If this function returns successfully, it may be assumed that the 5012 ** page-header flags indicate that the [virtual] root-page is the expected 5013 ** kind of b-tree page (i.e. if when opening the cursor the caller did not 5014 ** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D, 5015 ** indicating a table b-tree, or if the caller did specify a KeyInfo 5016 ** structure the flags byte is set to 0x02 or 0x0A, indicating an index 5017 ** b-tree). 5018 */ 5019 static int moveToRoot(BtCursor *pCur){ 5020 MemPage *pRoot; 5021 int rc = SQLITE_OK; 5022 5023 assert( cursorOwnsBtShared(pCur) ); 5024 assert( CURSOR_INVALID < CURSOR_REQUIRESEEK ); 5025 assert( CURSOR_VALID < CURSOR_REQUIRESEEK ); 5026 assert( CURSOR_FAULT > CURSOR_REQUIRESEEK ); 5027 assert( pCur->eState < CURSOR_REQUIRESEEK || pCur->iPage<0 ); 5028 assert( pCur->pgnoRoot>0 || pCur->iPage<0 ); 5029 5030 if( pCur->iPage>=0 ){ 5031 if( pCur->iPage ){ 5032 releasePageNotNull(pCur->pPage); 5033 while( --pCur->iPage ){ 5034 releasePageNotNull(pCur->apPage[pCur->iPage]); 5035 } 5036 pCur->pPage = pCur->apPage[0]; 5037 goto skip_init; 5038 } 5039 }else if( pCur->pgnoRoot==0 ){ 5040 pCur->eState = CURSOR_INVALID; 5041 return SQLITE_EMPTY; 5042 }else{ 5043 assert( pCur->iPage==(-1) ); 5044 if( pCur->eState>=CURSOR_REQUIRESEEK ){ 5045 if( pCur->eState==CURSOR_FAULT ){ 5046 assert( pCur->skipNext!=SQLITE_OK ); 5047 return pCur->skipNext; 5048 } 5049 sqlite3BtreeClearCursor(pCur); 5050 } 5051 rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->pPage, 5052 0, pCur->curPagerFlags); 5053 if( rc!=SQLITE_OK ){ 5054 pCur->eState = CURSOR_INVALID; 5055 return rc; 5056 } 5057 pCur->iPage = 0; 5058 pCur->curIntKey = pCur->pPage->intKey; 5059 } 5060 pRoot = pCur->pPage; 5061 assert( pRoot->pgno==pCur->pgnoRoot ); 5062 5063 /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor 5064 ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is 5065 ** NULL, the caller expects a table b-tree. If this is not the case, 5066 ** return an SQLITE_CORRUPT error. 5067 ** 5068 ** Earlier versions of SQLite assumed that this test could not fail 5069 ** if the root page was already loaded when this function was called (i.e. 5070 ** if pCur->iPage>=0). But this is not so if the database is corrupted 5071 ** in such a way that page pRoot is linked into a second b-tree table 5072 ** (or the freelist). */ 5073 assert( pRoot->intKey==1 || pRoot->intKey==0 ); 5074 if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){ 5075 return SQLITE_CORRUPT_PAGE(pCur->pPage); 5076 } 5077 5078 skip_init: 5079 pCur->ix = 0; 5080 pCur->info.nSize = 0; 5081 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl); 5082 5083 pRoot = pCur->pPage; 5084 if( pRoot->nCell>0 ){ 5085 pCur->eState = CURSOR_VALID; 5086 }else if( !pRoot->leaf ){ 5087 Pgno subpage; 5088 if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT; 5089 subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]); 5090 pCur->eState = CURSOR_VALID; 5091 rc = moveToChild(pCur, subpage); 5092 }else{ 5093 pCur->eState = CURSOR_INVALID; 5094 rc = SQLITE_EMPTY; 5095 } 5096 return rc; 5097 } 5098 5099 /* 5100 ** Move the cursor down to the left-most leaf entry beneath the 5101 ** entry to which it is currently pointing. 5102 ** 5103 ** The left-most leaf is the one with the smallest key - the first 5104 ** in ascending order. 5105 */ 5106 static int moveToLeftmost(BtCursor *pCur){ 5107 Pgno pgno; 5108 int rc = SQLITE_OK; 5109 MemPage *pPage; 5110 5111 assert( cursorOwnsBtShared(pCur) ); 5112 assert( pCur->eState==CURSOR_VALID ); 5113 while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){ 5114 assert( pCur->ix<pPage->nCell ); 5115 pgno = get4byte(findCell(pPage, pCur->ix)); 5116 rc = moveToChild(pCur, pgno); 5117 } 5118 return rc; 5119 } 5120 5121 /* 5122 ** Move the cursor down to the right-most leaf entry beneath the 5123 ** page to which it is currently pointing. Notice the difference 5124 ** between moveToLeftmost() and moveToRightmost(). moveToLeftmost() 5125 ** finds the left-most entry beneath the *entry* whereas moveToRightmost() 5126 ** finds the right-most entry beneath the *page*. 5127 ** 5128 ** The right-most entry is the one with the largest key - the last 5129 ** key in ascending order. 5130 */ 5131 static int moveToRightmost(BtCursor *pCur){ 5132 Pgno pgno; 5133 int rc = SQLITE_OK; 5134 MemPage *pPage = 0; 5135 5136 assert( cursorOwnsBtShared(pCur) ); 5137 assert( pCur->eState==CURSOR_VALID ); 5138 while( !(pPage = pCur->pPage)->leaf ){ 5139 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); 5140 pCur->ix = pPage->nCell; 5141 rc = moveToChild(pCur, pgno); 5142 if( rc ) return rc; 5143 } 5144 pCur->ix = pPage->nCell-1; 5145 assert( pCur->info.nSize==0 ); 5146 assert( (pCur->curFlags & BTCF_ValidNKey)==0 ); 5147 return SQLITE_OK; 5148 } 5149 5150 /* Move the cursor to the first entry in the table. Return SQLITE_OK 5151 ** on success. Set *pRes to 0 if the cursor actually points to something 5152 ** or set *pRes to 1 if the table is empty. 5153 */ 5154 int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){ 5155 int rc; 5156 5157 assert( cursorOwnsBtShared(pCur) ); 5158 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); 5159 rc = moveToRoot(pCur); 5160 if( rc==SQLITE_OK ){ 5161 assert( pCur->pPage->nCell>0 ); 5162 *pRes = 0; 5163 rc = moveToLeftmost(pCur); 5164 }else if( rc==SQLITE_EMPTY ){ 5165 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 ); 5166 *pRes = 1; 5167 rc = SQLITE_OK; 5168 } 5169 return rc; 5170 } 5171 5172 /* Move the cursor to the last entry in the table. Return SQLITE_OK 5173 ** on success. Set *pRes to 0 if the cursor actually points to something 5174 ** or set *pRes to 1 if the table is empty. 5175 */ 5176 int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ 5177 int rc; 5178 5179 assert( cursorOwnsBtShared(pCur) ); 5180 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); 5181 5182 /* If the cursor already points to the last entry, this is a no-op. */ 5183 if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){ 5184 #ifdef SQLITE_DEBUG 5185 /* This block serves to assert() that the cursor really does point 5186 ** to the last entry in the b-tree. */ 5187 int ii; 5188 for(ii=0; ii<pCur->iPage; ii++){ 5189 assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell ); 5190 } 5191 assert( pCur->ix==pCur->pPage->nCell-1 ); 5192 assert( pCur->pPage->leaf ); 5193 #endif 5194 return SQLITE_OK; 5195 } 5196 5197 rc = moveToRoot(pCur); 5198 if( rc==SQLITE_OK ){ 5199 assert( pCur->eState==CURSOR_VALID ); 5200 *pRes = 0; 5201 rc = moveToRightmost(pCur); 5202 if( rc==SQLITE_OK ){ 5203 pCur->curFlags |= BTCF_AtLast; 5204 }else{ 5205 pCur->curFlags &= ~BTCF_AtLast; 5206 } 5207 }else if( rc==SQLITE_EMPTY ){ 5208 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 ); 5209 *pRes = 1; 5210 rc = SQLITE_OK; 5211 } 5212 return rc; 5213 } 5214 5215 /* Move the cursor so that it points to an entry near the key 5216 ** specified by pIdxKey or intKey. Return a success code. 5217 ** 5218 ** For INTKEY tables, the intKey parameter is used. pIdxKey 5219 ** must be NULL. For index tables, pIdxKey is used and intKey 5220 ** is ignored. 5221 ** 5222 ** If an exact match is not found, then the cursor is always 5223 ** left pointing at a leaf page which would hold the entry if it 5224 ** were present. The cursor might point to an entry that comes 5225 ** before or after the key. 5226 ** 5227 ** An integer is written into *pRes which is the result of 5228 ** comparing the key with the entry to which the cursor is 5229 ** pointing. The meaning of the integer written into 5230 ** *pRes is as follows: 5231 ** 5232 ** *pRes<0 The cursor is left pointing at an entry that 5233 ** is smaller than intKey/pIdxKey or if the table is empty 5234 ** and the cursor is therefore left point to nothing. 5235 ** 5236 ** *pRes==0 The cursor is left pointing at an entry that 5237 ** exactly matches intKey/pIdxKey. 5238 ** 5239 ** *pRes>0 The cursor is left pointing at an entry that 5240 ** is larger than intKey/pIdxKey. 5241 ** 5242 ** For index tables, the pIdxKey->eqSeen field is set to 1 if there 5243 ** exists an entry in the table that exactly matches pIdxKey. 5244 */ 5245 int sqlite3BtreeMovetoUnpacked( 5246 BtCursor *pCur, /* The cursor to be moved */ 5247 UnpackedRecord *pIdxKey, /* Unpacked index key */ 5248 i64 intKey, /* The table key */ 5249 int biasRight, /* If true, bias the search to the high end */ 5250 int *pRes /* Write search results here */ 5251 ){ 5252 int rc; 5253 RecordCompare xRecordCompare; 5254 5255 assert( cursorOwnsBtShared(pCur) ); 5256 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); 5257 assert( pRes ); 5258 assert( (pIdxKey==0)==(pCur->pKeyInfo==0) ); 5259 assert( pCur->eState!=CURSOR_VALID || (pIdxKey==0)==(pCur->curIntKey!=0) ); 5260 5261 /* If the cursor is already positioned at the point we are trying 5262 ** to move to, then just return without doing any work */ 5263 if( pIdxKey==0 5264 && pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0 5265 ){ 5266 if( pCur->info.nKey==intKey ){ 5267 *pRes = 0; 5268 return SQLITE_OK; 5269 } 5270 if( pCur->info.nKey<intKey ){ 5271 if( (pCur->curFlags & BTCF_AtLast)!=0 ){ 5272 *pRes = -1; 5273 return SQLITE_OK; 5274 } 5275 /* If the requested key is one more than the previous key, then 5276 ** try to get there using sqlite3BtreeNext() rather than a full 5277 ** binary search. This is an optimization only. The correct answer 5278 ** is still obtained without this case, only a little more slowely */ 5279 if( pCur->info.nKey+1==intKey && !pCur->skipNext ){ 5280 *pRes = 0; 5281 rc = sqlite3BtreeNext(pCur, 0); 5282 if( rc==SQLITE_OK ){ 5283 getCellInfo(pCur); 5284 if( pCur->info.nKey==intKey ){ 5285 return SQLITE_OK; 5286 } 5287 }else if( rc==SQLITE_DONE ){ 5288 rc = SQLITE_OK; 5289 }else{ 5290 return rc; 5291 } 5292 } 5293 } 5294 } 5295 5296 if( pIdxKey ){ 5297 xRecordCompare = sqlite3VdbeFindCompare(pIdxKey); 5298 pIdxKey->errCode = 0; 5299 assert( pIdxKey->default_rc==1 5300 || pIdxKey->default_rc==0 5301 || pIdxKey->default_rc==-1 5302 ); 5303 }else{ 5304 xRecordCompare = 0; /* All keys are integers */ 5305 } 5306 5307 rc = moveToRoot(pCur); 5308 if( rc ){ 5309 if( rc==SQLITE_EMPTY ){ 5310 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 ); 5311 *pRes = -1; 5312 return SQLITE_OK; 5313 } 5314 return rc; 5315 } 5316 assert( pCur->pPage ); 5317 assert( pCur->pPage->isInit ); 5318 assert( pCur->eState==CURSOR_VALID ); 5319 assert( pCur->pPage->nCell > 0 ); 5320 assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey ); 5321 assert( pCur->curIntKey || pIdxKey ); 5322 for(;;){ 5323 int lwr, upr, idx, c; 5324 Pgno chldPg; 5325 MemPage *pPage = pCur->pPage; 5326 u8 *pCell; /* Pointer to current cell in pPage */ 5327 5328 /* pPage->nCell must be greater than zero. If this is the root-page 5329 ** the cursor would have been INVALID above and this for(;;) loop 5330 ** not run. If this is not the root-page, then the moveToChild() routine 5331 ** would have already detected db corruption. Similarly, pPage must 5332 ** be the right kind (index or table) of b-tree page. Otherwise 5333 ** a moveToChild() or moveToRoot() call would have detected corruption. */ 5334 assert( pPage->nCell>0 ); 5335 assert( pPage->intKey==(pIdxKey==0) ); 5336 lwr = 0; 5337 upr = pPage->nCell-1; 5338 assert( biasRight==0 || biasRight==1 ); 5339 idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */ 5340 pCur->ix = (u16)idx; 5341 if( xRecordCompare==0 ){ 5342 for(;;){ 5343 i64 nCellKey; 5344 pCell = findCellPastPtr(pPage, idx); 5345 if( pPage->intKeyLeaf ){ 5346 while( 0x80 <= *(pCell++) ){ 5347 if( pCell>=pPage->aDataEnd ){ 5348 return SQLITE_CORRUPT_PAGE(pPage); 5349 } 5350 } 5351 } 5352 getVarint(pCell, (u64*)&nCellKey); 5353 if( nCellKey<intKey ){ 5354 lwr = idx+1; 5355 if( lwr>upr ){ c = -1; break; } 5356 }else if( nCellKey>intKey ){ 5357 upr = idx-1; 5358 if( lwr>upr ){ c = +1; break; } 5359 }else{ 5360 assert( nCellKey==intKey ); 5361 pCur->ix = (u16)idx; 5362 if( !pPage->leaf ){ 5363 lwr = idx; 5364 goto moveto_next_layer; 5365 }else{ 5366 pCur->curFlags |= BTCF_ValidNKey; 5367 pCur->info.nKey = nCellKey; 5368 pCur->info.nSize = 0; 5369 *pRes = 0; 5370 return SQLITE_OK; 5371 } 5372 } 5373 assert( lwr+upr>=0 ); 5374 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2; */ 5375 } 5376 }else{ 5377 for(;;){ 5378 int nCell; /* Size of the pCell cell in bytes */ 5379 pCell = findCellPastPtr(pPage, idx); 5380 5381 /* The maximum supported page-size is 65536 bytes. This means that 5382 ** the maximum number of record bytes stored on an index B-Tree 5383 ** page is less than 16384 bytes and may be stored as a 2-byte 5384 ** varint. This information is used to attempt to avoid parsing 5385 ** the entire cell by checking for the cases where the record is 5386 ** stored entirely within the b-tree page by inspecting the first 5387 ** 2 bytes of the cell. 5388 */ 5389 nCell = pCell[0]; 5390 if( nCell<=pPage->max1bytePayload ){ 5391 /* This branch runs if the record-size field of the cell is a 5392 ** single byte varint and the record fits entirely on the main 5393 ** b-tree page. */ 5394 testcase( pCell+nCell+1==pPage->aDataEnd ); 5395 c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey); 5396 }else if( !(pCell[1] & 0x80) 5397 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal 5398 ){ 5399 /* The record-size field is a 2 byte varint and the record 5400 ** fits entirely on the main b-tree page. */ 5401 testcase( pCell+nCell+2==pPage->aDataEnd ); 5402 c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey); 5403 }else{ 5404 /* The record flows over onto one or more overflow pages. In 5405 ** this case the whole cell needs to be parsed, a buffer allocated 5406 ** and accessPayload() used to retrieve the record into the 5407 ** buffer before VdbeRecordCompare() can be called. 5408 ** 5409 ** If the record is corrupt, the xRecordCompare routine may read 5410 ** up to two varints past the end of the buffer. An extra 18 5411 ** bytes of padding is allocated at the end of the buffer in 5412 ** case this happens. */ 5413 void *pCellKey; 5414 u8 * const pCellBody = pCell - pPage->childPtrSize; 5415 pPage->xParseCell(pPage, pCellBody, &pCur->info); 5416 nCell = (int)pCur->info.nKey; 5417 testcase( nCell<0 ); /* True if key size is 2^32 or more */ 5418 testcase( nCell==0 ); /* Invalid key size: 0x80 0x80 0x00 */ 5419 testcase( nCell==1 ); /* Invalid key size: 0x80 0x80 0x01 */ 5420 testcase( nCell==2 ); /* Minimum legal index key size */ 5421 if( nCell<2 ){ 5422 rc = SQLITE_CORRUPT_PAGE(pPage); 5423 goto moveto_finish; 5424 } 5425 pCellKey = sqlite3Malloc( nCell+18 ); 5426 if( pCellKey==0 ){ 5427 rc = SQLITE_NOMEM_BKPT; 5428 goto moveto_finish; 5429 } 5430 pCur->ix = (u16)idx; 5431 rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0); 5432 pCur->curFlags &= ~BTCF_ValidOvfl; 5433 if( rc ){ 5434 sqlite3_free(pCellKey); 5435 goto moveto_finish; 5436 } 5437 c = xRecordCompare(nCell, pCellKey, pIdxKey); 5438 sqlite3_free(pCellKey); 5439 } 5440 assert( 5441 (pIdxKey->errCode!=SQLITE_CORRUPT || c==0) 5442 && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed) 5443 ); 5444 if( c<0 ){ 5445 lwr = idx+1; 5446 }else if( c>0 ){ 5447 upr = idx-1; 5448 }else{ 5449 assert( c==0 ); 5450 *pRes = 0; 5451 rc = SQLITE_OK; 5452 pCur->ix = (u16)idx; 5453 if( pIdxKey->errCode ) rc = SQLITE_CORRUPT_BKPT; 5454 goto moveto_finish; 5455 } 5456 if( lwr>upr ) break; 5457 assert( lwr+upr>=0 ); 5458 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2 */ 5459 } 5460 } 5461 assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) ); 5462 assert( pPage->isInit ); 5463 if( pPage->leaf ){ 5464 assert( pCur->ix<pCur->pPage->nCell ); 5465 pCur->ix = (u16)idx; 5466 *pRes = c; 5467 rc = SQLITE_OK; 5468 goto moveto_finish; 5469 } 5470 moveto_next_layer: 5471 if( lwr>=pPage->nCell ){ 5472 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]); 5473 }else{ 5474 chldPg = get4byte(findCell(pPage, lwr)); 5475 } 5476 pCur->ix = (u16)lwr; 5477 rc = moveToChild(pCur, chldPg); 5478 if( rc ) break; 5479 } 5480 moveto_finish: 5481 pCur->info.nSize = 0; 5482 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 ); 5483 return rc; 5484 } 5485 5486 5487 /* 5488 ** Return TRUE if the cursor is not pointing at an entry of the table. 5489 ** 5490 ** TRUE will be returned after a call to sqlite3BtreeNext() moves 5491 ** past the last entry in the table or sqlite3BtreePrev() moves past 5492 ** the first entry. TRUE is also returned if the table is empty. 5493 */ 5494 int sqlite3BtreeEof(BtCursor *pCur){ 5495 /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries 5496 ** have been deleted? This API will need to change to return an error code 5497 ** as well as the boolean result value. 5498 */ 5499 return (CURSOR_VALID!=pCur->eState); 5500 } 5501 5502 /* 5503 ** Return an estimate for the number of rows in the table that pCur is 5504 ** pointing to. Return a negative number if no estimate is currently 5505 ** available. 5506 */ 5507 i64 sqlite3BtreeRowCountEst(BtCursor *pCur){ 5508 i64 n; 5509 u8 i; 5510 5511 assert( cursorOwnsBtShared(pCur) ); 5512 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); 5513 5514 /* Currently this interface is only called by the OP_IfSmaller 5515 ** opcode, and it that case the cursor will always be valid and 5516 ** will always point to a leaf node. */ 5517 if( NEVER(pCur->eState!=CURSOR_VALID) ) return -1; 5518 if( NEVER(pCur->pPage->leaf==0) ) return -1; 5519 5520 n = pCur->pPage->nCell; 5521 for(i=0; i<pCur->iPage; i++){ 5522 n *= pCur->apPage[i]->nCell; 5523 } 5524 return n; 5525 } 5526 5527 /* 5528 ** Advance the cursor to the next entry in the database. 5529 ** Return value: 5530 ** 5531 ** SQLITE_OK success 5532 ** SQLITE_DONE cursor is already pointing at the last element 5533 ** otherwise some kind of error occurred 5534 ** 5535 ** The main entry point is sqlite3BtreeNext(). That routine is optimized 5536 ** for the common case of merely incrementing the cell counter BtCursor.aiIdx 5537 ** to the next cell on the current page. The (slower) btreeNext() helper 5538 ** routine is called when it is necessary to move to a different page or 5539 ** to restore the cursor. 5540 ** 5541 ** If bit 0x01 of the F argument in sqlite3BtreeNext(C,F) is 1, then the 5542 ** cursor corresponds to an SQL index and this routine could have been 5543 ** skipped if the SQL index had been a unique index. The F argument 5544 ** is a hint to the implement. SQLite btree implementation does not use 5545 ** this hint, but COMDB2 does. 5546 */ 5547 static SQLITE_NOINLINE int btreeNext(BtCursor *pCur){ 5548 int rc; 5549 int idx; 5550 MemPage *pPage; 5551 5552 assert( cursorOwnsBtShared(pCur) ); 5553 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID ); 5554 if( pCur->eState!=CURSOR_VALID ){ 5555 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 ); 5556 rc = restoreCursorPosition(pCur); 5557 if( rc!=SQLITE_OK ){ 5558 return rc; 5559 } 5560 if( CURSOR_INVALID==pCur->eState ){ 5561 return SQLITE_DONE; 5562 } 5563 if( pCur->skipNext ){ 5564 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT ); 5565 pCur->eState = CURSOR_VALID; 5566 if( pCur->skipNext>0 ){ 5567 pCur->skipNext = 0; 5568 return SQLITE_OK; 5569 } 5570 pCur->skipNext = 0; 5571 } 5572 } 5573 5574 pPage = pCur->pPage; 5575 idx = ++pCur->ix; 5576 assert( pPage->isInit ); 5577 5578 /* If the database file is corrupt, it is possible for the value of idx 5579 ** to be invalid here. This can only occur if a second cursor modifies 5580 ** the page while cursor pCur is holding a reference to it. Which can 5581 ** only happen if the database is corrupt in such a way as to link the 5582 ** page into more than one b-tree structure. */ 5583 testcase( idx>pPage->nCell ); 5584 5585 if( idx>=pPage->nCell ){ 5586 if( !pPage->leaf ){ 5587 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); 5588 if( rc ) return rc; 5589 return moveToLeftmost(pCur); 5590 } 5591 do{ 5592 if( pCur->iPage==0 ){ 5593 pCur->eState = CURSOR_INVALID; 5594 return SQLITE_DONE; 5595 } 5596 moveToParent(pCur); 5597 pPage = pCur->pPage; 5598 }while( pCur->ix>=pPage->nCell ); 5599 if( pPage->intKey ){ 5600 return sqlite3BtreeNext(pCur, 0); 5601 }else{ 5602 return SQLITE_OK; 5603 } 5604 } 5605 if( pPage->leaf ){ 5606 return SQLITE_OK; 5607 }else{ 5608 return moveToLeftmost(pCur); 5609 } 5610 } 5611 int sqlite3BtreeNext(BtCursor *pCur, int flags){ 5612 MemPage *pPage; 5613 UNUSED_PARAMETER( flags ); /* Used in COMDB2 but not native SQLite */ 5614 assert( cursorOwnsBtShared(pCur) ); 5615 assert( flags==0 || flags==1 ); 5616 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID ); 5617 pCur->info.nSize = 0; 5618 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); 5619 if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur); 5620 pPage = pCur->pPage; 5621 if( (++pCur->ix)>=pPage->nCell ){ 5622 pCur->ix--; 5623 return btreeNext(pCur); 5624 } 5625 if( pPage->leaf ){ 5626 return SQLITE_OK; 5627 }else{ 5628 return moveToLeftmost(pCur); 5629 } 5630 } 5631 5632 /* 5633 ** Step the cursor to the back to the previous entry in the database. 5634 ** Return values: 5635 ** 5636 ** SQLITE_OK success 5637 ** SQLITE_DONE the cursor is already on the first element of the table 5638 ** otherwise some kind of error occurred 5639 ** 5640 ** The main entry point is sqlite3BtreePrevious(). That routine is optimized 5641 ** for the common case of merely decrementing the cell counter BtCursor.aiIdx 5642 ** to the previous cell on the current page. The (slower) btreePrevious() 5643 ** helper routine is called when it is necessary to move to a different page 5644 ** or to restore the cursor. 5645 ** 5646 ** If bit 0x01 of the F argument to sqlite3BtreePrevious(C,F) is 1, then 5647 ** the cursor corresponds to an SQL index and this routine could have been 5648 ** skipped if the SQL index had been a unique index. The F argument is a 5649 ** hint to the implement. The native SQLite btree implementation does not 5650 ** use this hint, but COMDB2 does. 5651 */ 5652 static SQLITE_NOINLINE int btreePrevious(BtCursor *pCur){ 5653 int rc; 5654 MemPage *pPage; 5655 5656 assert( cursorOwnsBtShared(pCur) ); 5657 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID ); 5658 assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 ); 5659 assert( pCur->info.nSize==0 ); 5660 if( pCur->eState!=CURSOR_VALID ){ 5661 rc = restoreCursorPosition(pCur); 5662 if( rc!=SQLITE_OK ){ 5663 return rc; 5664 } 5665 if( CURSOR_INVALID==pCur->eState ){ 5666 return SQLITE_DONE; 5667 } 5668 if( pCur->skipNext ){ 5669 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_SKIPNEXT ); 5670 pCur->eState = CURSOR_VALID; 5671 if( pCur->skipNext<0 ){ 5672 pCur->skipNext = 0; 5673 return SQLITE_OK; 5674 } 5675 pCur->skipNext = 0; 5676 } 5677 } 5678 5679 pPage = pCur->pPage; 5680 assert( pPage->isInit ); 5681 if( !pPage->leaf ){ 5682 int idx = pCur->ix; 5683 rc = moveToChild(pCur, get4byte(findCell(pPage, idx))); 5684 if( rc ) return rc; 5685 rc = moveToRightmost(pCur); 5686 }else{ 5687 while( pCur->ix==0 ){ 5688 if( pCur->iPage==0 ){ 5689 pCur->eState = CURSOR_INVALID; 5690 return SQLITE_DONE; 5691 } 5692 moveToParent(pCur); 5693 } 5694 assert( pCur->info.nSize==0 ); 5695 assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 ); 5696 5697 pCur->ix--; 5698 pPage = pCur->pPage; 5699 if( pPage->intKey && !pPage->leaf ){ 5700 rc = sqlite3BtreePrevious(pCur, 0); 5701 }else{ 5702 rc = SQLITE_OK; 5703 } 5704 } 5705 return rc; 5706 } 5707 int sqlite3BtreePrevious(BtCursor *pCur, int flags){ 5708 assert( cursorOwnsBtShared(pCur) ); 5709 assert( flags==0 || flags==1 ); 5710 assert( pCur->skipNext==0 || pCur->eState!=CURSOR_VALID ); 5711 UNUSED_PARAMETER( flags ); /* Used in COMDB2 but not native SQLite */ 5712 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey); 5713 pCur->info.nSize = 0; 5714 if( pCur->eState!=CURSOR_VALID 5715 || pCur->ix==0 5716 || pCur->pPage->leaf==0 5717 ){ 5718 return btreePrevious(pCur); 5719 } 5720 pCur->ix--; 5721 return SQLITE_OK; 5722 } 5723 5724 /* 5725 ** Allocate a new page from the database file. 5726 ** 5727 ** The new page is marked as dirty. (In other words, sqlite3PagerWrite() 5728 ** has already been called on the new page.) The new page has also 5729 ** been referenced and the calling routine is responsible for calling 5730 ** sqlite3PagerUnref() on the new page when it is done. 5731 ** 5732 ** SQLITE_OK is returned on success. Any other return value indicates 5733 ** an error. *ppPage is set to NULL in the event of an error. 5734 ** 5735 ** If the "nearby" parameter is not 0, then an effort is made to 5736 ** locate a page close to the page number "nearby". This can be used in an 5737 ** attempt to keep related pages close to each other in the database file, 5738 ** which in turn can make database access faster. 5739 ** 5740 ** If the eMode parameter is BTALLOC_EXACT and the nearby page exists 5741 ** anywhere on the free-list, then it is guaranteed to be returned. If 5742 ** eMode is BTALLOC_LT then the page returned will be less than or equal 5743 ** to nearby if any such page exists. If eMode is BTALLOC_ANY then there 5744 ** are no restrictions on which page is returned. 5745 */ 5746 static int allocateBtreePage( 5747 BtShared *pBt, /* The btree */ 5748 MemPage **ppPage, /* Store pointer to the allocated page here */ 5749 Pgno *pPgno, /* Store the page number here */ 5750 Pgno nearby, /* Search for a page near this one */ 5751 u8 eMode /* BTALLOC_EXACT, BTALLOC_LT, or BTALLOC_ANY */ 5752 ){ 5753 MemPage *pPage1; 5754 int rc; 5755 u32 n; /* Number of pages on the freelist */ 5756 u32 k; /* Number of leaves on the trunk of the freelist */ 5757 MemPage *pTrunk = 0; 5758 MemPage *pPrevTrunk = 0; 5759 Pgno mxPage; /* Total size of the database file */ 5760 5761 assert( sqlite3_mutex_held(pBt->mutex) ); 5762 assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) ); 5763 pPage1 = pBt->pPage1; 5764 mxPage = btreePagecount(pBt); 5765 /* EVIDENCE-OF: R-05119-02637 The 4-byte big-endian integer at offset 36 5766 ** stores stores the total number of pages on the freelist. */ 5767 n = get4byte(&pPage1->aData[36]); 5768 testcase( n==mxPage-1 ); 5769 if( n>=mxPage ){ 5770 return SQLITE_CORRUPT_BKPT; 5771 } 5772 if( n>0 ){ 5773 /* There are pages on the freelist. Reuse one of those pages. */ 5774 Pgno iTrunk; 5775 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */ 5776 u32 nSearch = 0; /* Count of the number of search attempts */ 5777 5778 /* If eMode==BTALLOC_EXACT and a query of the pointer-map 5779 ** shows that the page 'nearby' is somewhere on the free-list, then 5780 ** the entire-list will be searched for that page. 5781 */ 5782 #ifndef SQLITE_OMIT_AUTOVACUUM 5783 if( eMode==BTALLOC_EXACT ){ 5784 if( nearby<=mxPage ){ 5785 u8 eType; 5786 assert( nearby>0 ); 5787 assert( pBt->autoVacuum ); 5788 rc = ptrmapGet(pBt, nearby, &eType, 0); 5789 if( rc ) return rc; 5790 if( eType==PTRMAP_FREEPAGE ){ 5791 searchList = 1; 5792 } 5793 } 5794 }else if( eMode==BTALLOC_LE ){ 5795 searchList = 1; 5796 } 5797 #endif 5798 5799 /* Decrement the free-list count by 1. Set iTrunk to the index of the 5800 ** first free-list trunk page. iPrevTrunk is initially 1. 5801 */ 5802 rc = sqlite3PagerWrite(pPage1->pDbPage); 5803 if( rc ) return rc; 5804 put4byte(&pPage1->aData[36], n-1); 5805 5806 /* The code within this loop is run only once if the 'searchList' variable 5807 ** is not true. Otherwise, it runs once for each trunk-page on the 5808 ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT) 5809 ** or until a page less than 'nearby' is located (eMode==BTALLOC_LT) 5810 */ 5811 do { 5812 pPrevTrunk = pTrunk; 5813 if( pPrevTrunk ){ 5814 /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page 5815 ** is the page number of the next freelist trunk page in the list or 5816 ** zero if this is the last freelist trunk page. */ 5817 iTrunk = get4byte(&pPrevTrunk->aData[0]); 5818 }else{ 5819 /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32 5820 ** stores the page number of the first page of the freelist, or zero if 5821 ** the freelist is empty. */ 5822 iTrunk = get4byte(&pPage1->aData[32]); 5823 } 5824 testcase( iTrunk==mxPage ); 5825 if( iTrunk>mxPage || nSearch++ > n ){ 5826 rc = SQLITE_CORRUPT_PGNO(pPrevTrunk ? pPrevTrunk->pgno : 1); 5827 }else{ 5828 rc = btreeGetUnusedPage(pBt, iTrunk, &pTrunk, 0); 5829 } 5830 if( rc ){ 5831 pTrunk = 0; 5832 goto end_allocate_page; 5833 } 5834 assert( pTrunk!=0 ); 5835 assert( pTrunk->aData!=0 ); 5836 /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page 5837 ** is the number of leaf page pointers to follow. */ 5838 k = get4byte(&pTrunk->aData[4]); 5839 if( k==0 && !searchList ){ 5840 /* The trunk has no leaves and the list is not being searched. 5841 ** So extract the trunk page itself and use it as the newly 5842 ** allocated page */ 5843 assert( pPrevTrunk==0 ); 5844 rc = sqlite3PagerWrite(pTrunk->pDbPage); 5845 if( rc ){ 5846 goto end_allocate_page; 5847 } 5848 *pPgno = iTrunk; 5849 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); 5850 *ppPage = pTrunk; 5851 pTrunk = 0; 5852 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); 5853 }else if( k>(u32)(pBt->usableSize/4 - 2) ){ 5854 /* Value of k is out of range. Database corruption */ 5855 rc = SQLITE_CORRUPT_PGNO(iTrunk); 5856 goto end_allocate_page; 5857 #ifndef SQLITE_OMIT_AUTOVACUUM 5858 }else if( searchList 5859 && (nearby==iTrunk || (iTrunk<nearby && eMode==BTALLOC_LE)) 5860 ){ 5861 /* The list is being searched and this trunk page is the page 5862 ** to allocate, regardless of whether it has leaves. 5863 */ 5864 *pPgno = iTrunk; 5865 *ppPage = pTrunk; 5866 searchList = 0; 5867 rc = sqlite3PagerWrite(pTrunk->pDbPage); 5868 if( rc ){ 5869 goto end_allocate_page; 5870 } 5871 if( k==0 ){ 5872 if( !pPrevTrunk ){ 5873 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); 5874 }else{ 5875 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage); 5876 if( rc!=SQLITE_OK ){ 5877 goto end_allocate_page; 5878 } 5879 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4); 5880 } 5881 }else{ 5882 /* The trunk page is required by the caller but it contains 5883 ** pointers to free-list leaves. The first leaf becomes a trunk 5884 ** page in this case. 5885 */ 5886 MemPage *pNewTrunk; 5887 Pgno iNewTrunk = get4byte(&pTrunk->aData[8]); 5888 if( iNewTrunk>mxPage ){ 5889 rc = SQLITE_CORRUPT_PGNO(iTrunk); 5890 goto end_allocate_page; 5891 } 5892 testcase( iNewTrunk==mxPage ); 5893 rc = btreeGetUnusedPage(pBt, iNewTrunk, &pNewTrunk, 0); 5894 if( rc!=SQLITE_OK ){ 5895 goto end_allocate_page; 5896 } 5897 rc = sqlite3PagerWrite(pNewTrunk->pDbPage); 5898 if( rc!=SQLITE_OK ){ 5899 releasePage(pNewTrunk); 5900 goto end_allocate_page; 5901 } 5902 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4); 5903 put4byte(&pNewTrunk->aData[4], k-1); 5904 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4); 5905 releasePage(pNewTrunk); 5906 if( !pPrevTrunk ){ 5907 assert( sqlite3PagerIswriteable(pPage1->pDbPage) ); 5908 put4byte(&pPage1->aData[32], iNewTrunk); 5909 }else{ 5910 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage); 5911 if( rc ){ 5912 goto end_allocate_page; 5913 } 5914 put4byte(&pPrevTrunk->aData[0], iNewTrunk); 5915 } 5916 } 5917 pTrunk = 0; 5918 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); 5919 #endif 5920 }else if( k>0 ){ 5921 /* Extract a leaf from the trunk */ 5922 u32 closest; 5923 Pgno iPage; 5924 unsigned char *aData = pTrunk->aData; 5925 if( nearby>0 ){ 5926 u32 i; 5927 closest = 0; 5928 if( eMode==BTALLOC_LE ){ 5929 for(i=0; i<k; i++){ 5930 iPage = get4byte(&aData[8+i*4]); 5931 if( iPage<=nearby ){ 5932 closest = i; 5933 break; 5934 } 5935 } 5936 }else{ 5937 int dist; 5938 dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby); 5939 for(i=1; i<k; i++){ 5940 int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby); 5941 if( d2<dist ){ 5942 closest = i; 5943 dist = d2; 5944 } 5945 } 5946 } 5947 }else{ 5948 closest = 0; 5949 } 5950 5951 iPage = get4byte(&aData[8+closest*4]); 5952 testcase( iPage==mxPage ); 5953 if( iPage>mxPage ){ 5954 rc = SQLITE_CORRUPT_PGNO(iTrunk); 5955 goto end_allocate_page; 5956 } 5957 testcase( iPage==mxPage ); 5958 if( !searchList 5959 || (iPage==nearby || (iPage<nearby && eMode==BTALLOC_LE)) 5960 ){ 5961 int noContent; 5962 *pPgno = iPage; 5963 TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d" 5964 ": %d more free pages\n", 5965 *pPgno, closest+1, k, pTrunk->pgno, n-1)); 5966 rc = sqlite3PagerWrite(pTrunk->pDbPage); 5967 if( rc ) goto end_allocate_page; 5968 if( closest<k-1 ){ 5969 memcpy(&aData[8+closest*4], &aData[4+k*4], 4); 5970 } 5971 put4byte(&aData[4], k-1); 5972 noContent = !btreeGetHasContent(pBt, *pPgno)? PAGER_GET_NOCONTENT : 0; 5973 rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, noContent); 5974 if( rc==SQLITE_OK ){ 5975 rc = sqlite3PagerWrite((*ppPage)->pDbPage); 5976 if( rc!=SQLITE_OK ){ 5977 releasePage(*ppPage); 5978 *ppPage = 0; 5979 } 5980 } 5981 searchList = 0; 5982 } 5983 } 5984 releasePage(pPrevTrunk); 5985 pPrevTrunk = 0; 5986 }while( searchList ); 5987 }else{ 5988 /* There are no pages on the freelist, so append a new page to the 5989 ** database image. 5990 ** 5991 ** Normally, new pages allocated by this block can be requested from the 5992 ** pager layer with the 'no-content' flag set. This prevents the pager 5993 ** from trying to read the pages content from disk. However, if the 5994 ** current transaction has already run one or more incremental-vacuum 5995 ** steps, then the page we are about to allocate may contain content 5996 ** that is required in the event of a rollback. In this case, do 5997 ** not set the no-content flag. This causes the pager to load and journal 5998 ** the current page content before overwriting it. 5999 ** 6000 ** Note that the pager will not actually attempt to load or journal 6001 ** content for any page that really does lie past the end of the database 6002 ** file on disk. So the effects of disabling the no-content optimization 6003 ** here are confined to those pages that lie between the end of the 6004 ** database image and the end of the database file. 6005 */ 6006 int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0; 6007 6008 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); 6009 if( rc ) return rc; 6010 pBt->nPage++; 6011 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++; 6012 6013 #ifndef SQLITE_OMIT_AUTOVACUUM 6014 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){ 6015 /* If *pPgno refers to a pointer-map page, allocate two new pages 6016 ** at the end of the file instead of one. The first allocated page 6017 ** becomes a new pointer-map page, the second is used by the caller. 6018 */ 6019 MemPage *pPg = 0; 6020 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage)); 6021 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) ); 6022 rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent); 6023 if( rc==SQLITE_OK ){ 6024 rc = sqlite3PagerWrite(pPg->pDbPage); 6025 releasePage(pPg); 6026 } 6027 if( rc ) return rc; 6028 pBt->nPage++; 6029 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; } 6030 } 6031 #endif 6032 put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage); 6033 *pPgno = pBt->nPage; 6034 6035 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); 6036 rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, bNoContent); 6037 if( rc ) return rc; 6038 rc = sqlite3PagerWrite((*ppPage)->pDbPage); 6039 if( rc!=SQLITE_OK ){ 6040 releasePage(*ppPage); 6041 *ppPage = 0; 6042 } 6043 TRACE(("ALLOCATE: %d from end of file\n", *pPgno)); 6044 } 6045 6046 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); 6047 6048 end_allocate_page: 6049 releasePage(pTrunk); 6050 releasePage(pPrevTrunk); 6051 assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 ); 6052 assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 ); 6053 return rc; 6054 } 6055 6056 /* 6057 ** This function is used to add page iPage to the database file free-list. 6058 ** It is assumed that the page is not already a part of the free-list. 6059 ** 6060 ** The value passed as the second argument to this function is optional. 6061 ** If the caller happens to have a pointer to the MemPage object 6062 ** corresponding to page iPage handy, it may pass it as the second value. 6063 ** Otherwise, it may pass NULL. 6064 ** 6065 ** If a pointer to a MemPage object is passed as the second argument, 6066 ** its reference count is not altered by this function. 6067 */ 6068 static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){ 6069 MemPage *pTrunk = 0; /* Free-list trunk page */ 6070 Pgno iTrunk = 0; /* Page number of free-list trunk page */ 6071 MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */ 6072 MemPage *pPage; /* Page being freed. May be NULL. */ 6073 int rc; /* Return Code */ 6074 int nFree; /* Initial number of pages on free-list */ 6075 6076 assert( sqlite3_mutex_held(pBt->mutex) ); 6077 assert( CORRUPT_DB || iPage>1 ); 6078 assert( !pMemPage || pMemPage->pgno==iPage ); 6079 6080 if( iPage<2 ) return SQLITE_CORRUPT_BKPT; 6081 if( pMemPage ){ 6082 pPage = pMemPage; 6083 sqlite3PagerRef(pPage->pDbPage); 6084 }else{ 6085 pPage = btreePageLookup(pBt, iPage); 6086 } 6087 6088 /* Increment the free page count on pPage1 */ 6089 rc = sqlite3PagerWrite(pPage1->pDbPage); 6090 if( rc ) goto freepage_out; 6091 nFree = get4byte(&pPage1->aData[36]); 6092 put4byte(&pPage1->aData[36], nFree+1); 6093 6094 if( pBt->btsFlags & BTS_SECURE_DELETE ){ 6095 /* If the secure_delete option is enabled, then 6096 ** always fully overwrite deleted information with zeros. 6097 */ 6098 if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) ) 6099 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0) 6100 ){ 6101 goto freepage_out; 6102 } 6103 memset(pPage->aData, 0, pPage->pBt->pageSize); 6104 } 6105 6106 /* If the database supports auto-vacuum, write an entry in the pointer-map 6107 ** to indicate that the page is free. 6108 */ 6109 if( ISAUTOVACUUM ){ 6110 ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0, &rc); 6111 if( rc ) goto freepage_out; 6112 } 6113 6114 /* Now manipulate the actual database free-list structure. There are two 6115 ** possibilities. If the free-list is currently empty, or if the first 6116 ** trunk page in the free-list is full, then this page will become a 6117 ** new free-list trunk page. Otherwise, it will become a leaf of the 6118 ** first trunk page in the current free-list. This block tests if it 6119 ** is possible to add the page as a new free-list leaf. 6120 */ 6121 if( nFree!=0 ){ 6122 u32 nLeaf; /* Initial number of leaf cells on trunk page */ 6123 6124 iTrunk = get4byte(&pPage1->aData[32]); 6125 rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0); 6126 if( rc!=SQLITE_OK ){ 6127 goto freepage_out; 6128 } 6129 6130 nLeaf = get4byte(&pTrunk->aData[4]); 6131 assert( pBt->usableSize>32 ); 6132 if( nLeaf > (u32)pBt->usableSize/4 - 2 ){ 6133 rc = SQLITE_CORRUPT_BKPT; 6134 goto freepage_out; 6135 } 6136 if( nLeaf < (u32)pBt->usableSize/4 - 8 ){ 6137 /* In this case there is room on the trunk page to insert the page 6138 ** being freed as a new leaf. 6139 ** 6140 ** Note that the trunk page is not really full until it contains 6141 ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have 6142 ** coded. But due to a coding error in versions of SQLite prior to 6143 ** 3.6.0, databases with freelist trunk pages holding more than 6144 ** usableSize/4 - 8 entries will be reported as corrupt. In order 6145 ** to maintain backwards compatibility with older versions of SQLite, 6146 ** we will continue to restrict the number of entries to usableSize/4 - 8 6147 ** for now. At some point in the future (once everyone has upgraded 6148 ** to 3.6.0 or later) we should consider fixing the conditional above 6149 ** to read "usableSize/4-2" instead of "usableSize/4-8". 6150 ** 6151 ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still 6152 ** avoid using the last six entries in the freelist trunk page array in 6153 ** order that database files created by newer versions of SQLite can be 6154 ** read by older versions of SQLite. 6155 */ 6156 rc = sqlite3PagerWrite(pTrunk->pDbPage); 6157 if( rc==SQLITE_OK ){ 6158 put4byte(&pTrunk->aData[4], nLeaf+1); 6159 put4byte(&pTrunk->aData[8+nLeaf*4], iPage); 6160 if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){ 6161 sqlite3PagerDontWrite(pPage->pDbPage); 6162 } 6163 rc = btreeSetHasContent(pBt, iPage); 6164 } 6165 TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno)); 6166 goto freepage_out; 6167 } 6168 } 6169 6170 /* If control flows to this point, then it was not possible to add the 6171 ** the page being freed as a leaf page of the first trunk in the free-list. 6172 ** Possibly because the free-list is empty, or possibly because the 6173 ** first trunk in the free-list is full. Either way, the page being freed 6174 ** will become the new first trunk page in the free-list. 6175 */ 6176 if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){ 6177 goto freepage_out; 6178 } 6179 rc = sqlite3PagerWrite(pPage->pDbPage); 6180 if( rc!=SQLITE_OK ){ 6181 goto freepage_out; 6182 } 6183 put4byte(pPage->aData, iTrunk); 6184 put4byte(&pPage->aData[4], 0); 6185 put4byte(&pPage1->aData[32], iPage); 6186 TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk)); 6187 6188 freepage_out: 6189 if( pPage ){ 6190 pPage->isInit = 0; 6191 } 6192 releasePage(pPage); 6193 releasePage(pTrunk); 6194 return rc; 6195 } 6196 static void freePage(MemPage *pPage, int *pRC){ 6197 if( (*pRC)==SQLITE_OK ){ 6198 *pRC = freePage2(pPage->pBt, pPage, pPage->pgno); 6199 } 6200 } 6201 6202 /* 6203 ** Free any overflow pages associated with the given Cell. Store 6204 ** size information about the cell in pInfo. 6205 */ 6206 static int clearCell( 6207 MemPage *pPage, /* The page that contains the Cell */ 6208 unsigned char *pCell, /* First byte of the Cell */ 6209 CellInfo *pInfo /* Size information about the cell */ 6210 ){ 6211 BtShared *pBt; 6212 Pgno ovflPgno; 6213 int rc; 6214 int nOvfl; 6215 u32 ovflPageSize; 6216 6217 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 6218 pPage->xParseCell(pPage, pCell, pInfo); 6219 if( pInfo->nLocal==pInfo->nPayload ){ 6220 return SQLITE_OK; /* No overflow pages. Return without doing anything */ 6221 } 6222 if( pCell+pInfo->nSize-1 > pPage->aData+pPage->maskPage ){ 6223 /* Cell extends past end of page */ 6224 return SQLITE_CORRUPT_PAGE(pPage); 6225 } 6226 ovflPgno = get4byte(pCell + pInfo->nSize - 4); 6227 pBt = pPage->pBt; 6228 assert( pBt->usableSize > 4 ); 6229 ovflPageSize = pBt->usableSize - 4; 6230 nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize; 6231 assert( nOvfl>0 || 6232 (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)<ovflPageSize) 6233 ); 6234 while( nOvfl-- ){ 6235 Pgno iNext = 0; 6236 MemPage *pOvfl = 0; 6237 if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){ 6238 /* 0 is not a legal page number and page 1 cannot be an 6239 ** overflow page. Therefore if ovflPgno<2 or past the end of the 6240 ** file the database must be corrupt. */ 6241 return SQLITE_CORRUPT_BKPT; 6242 } 6243 if( nOvfl ){ 6244 rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext); 6245 if( rc ) return rc; 6246 } 6247 6248 if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, ovflPgno))!=0) ) 6249 && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1 6250 ){ 6251 /* There is no reason any cursor should have an outstanding reference 6252 ** to an overflow page belonging to a cell that is being deleted/updated. 6253 ** So if there exists more than one reference to this page, then it 6254 ** must not really be an overflow page and the database must be corrupt. 6255 ** It is helpful to detect this before calling freePage2(), as 6256 ** freePage2() may zero the page contents if secure-delete mode is 6257 ** enabled. If this 'overflow' page happens to be a page that the 6258 ** caller is iterating through or using in some other way, this 6259 ** can be problematic. 6260 */ 6261 rc = SQLITE_CORRUPT_BKPT; 6262 }else{ 6263 rc = freePage2(pBt, pOvfl, ovflPgno); 6264 } 6265 6266 if( pOvfl ){ 6267 sqlite3PagerUnref(pOvfl->pDbPage); 6268 } 6269 if( rc ) return rc; 6270 ovflPgno = iNext; 6271 } 6272 return SQLITE_OK; 6273 } 6274 6275 /* 6276 ** Create the byte sequence used to represent a cell on page pPage 6277 ** and write that byte sequence into pCell[]. Overflow pages are 6278 ** allocated and filled in as necessary. The calling procedure 6279 ** is responsible for making sure sufficient space has been allocated 6280 ** for pCell[]. 6281 ** 6282 ** Note that pCell does not necessary need to point to the pPage->aData 6283 ** area. pCell might point to some temporary storage. The cell will 6284 ** be constructed in this temporary area then copied into pPage->aData 6285 ** later. 6286 */ 6287 static int fillInCell( 6288 MemPage *pPage, /* The page that contains the cell */ 6289 unsigned char *pCell, /* Complete text of the cell */ 6290 const BtreePayload *pX, /* Payload with which to construct the cell */ 6291 int *pnSize /* Write cell size here */ 6292 ){ 6293 int nPayload; 6294 const u8 *pSrc; 6295 int nSrc, n, rc, mn; 6296 int spaceLeft; 6297 MemPage *pToRelease; 6298 unsigned char *pPrior; 6299 unsigned char *pPayload; 6300 BtShared *pBt; 6301 Pgno pgnoOvfl; 6302 int nHeader; 6303 6304 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 6305 6306 /* pPage is not necessarily writeable since pCell might be auxiliary 6307 ** buffer space that is separate from the pPage buffer area */ 6308 assert( pCell<pPage->aData || pCell>=&pPage->aData[pPage->pBt->pageSize] 6309 || sqlite3PagerIswriteable(pPage->pDbPage) ); 6310 6311 /* Fill in the header. */ 6312 nHeader = pPage->childPtrSize; 6313 if( pPage->intKey ){ 6314 nPayload = pX->nData + pX->nZero; 6315 pSrc = pX->pData; 6316 nSrc = pX->nData; 6317 assert( pPage->intKeyLeaf ); /* fillInCell() only called for leaves */ 6318 nHeader += putVarint32(&pCell[nHeader], nPayload); 6319 nHeader += putVarint(&pCell[nHeader], *(u64*)&pX->nKey); 6320 }else{ 6321 assert( pX->nKey<=0x7fffffff && pX->pKey!=0 ); 6322 nSrc = nPayload = (int)pX->nKey; 6323 pSrc = pX->pKey; 6324 nHeader += putVarint32(&pCell[nHeader], nPayload); 6325 } 6326 6327 /* Fill in the payload */ 6328 pPayload = &pCell[nHeader]; 6329 if( nPayload<=pPage->maxLocal ){ 6330 /* This is the common case where everything fits on the btree page 6331 ** and no overflow pages are required. */ 6332 n = nHeader + nPayload; 6333 testcase( n==3 ); 6334 testcase( n==4 ); 6335 if( n<4 ) n = 4; 6336 *pnSize = n; 6337 assert( nSrc<=nPayload ); 6338 testcase( nSrc<nPayload ); 6339 memcpy(pPayload, pSrc, nSrc); 6340 memset(pPayload+nSrc, 0, nPayload-nSrc); 6341 return SQLITE_OK; 6342 } 6343 6344 /* If we reach this point, it means that some of the content will need 6345 ** to spill onto overflow pages. 6346 */ 6347 mn = pPage->minLocal; 6348 n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4); 6349 testcase( n==pPage->maxLocal ); 6350 testcase( n==pPage->maxLocal+1 ); 6351 if( n > pPage->maxLocal ) n = mn; 6352 spaceLeft = n; 6353 *pnSize = n + nHeader + 4; 6354 pPrior = &pCell[nHeader+n]; 6355 pToRelease = 0; 6356 pgnoOvfl = 0; 6357 pBt = pPage->pBt; 6358 6359 /* At this point variables should be set as follows: 6360 ** 6361 ** nPayload Total payload size in bytes 6362 ** pPayload Begin writing payload here 6363 ** spaceLeft Space available at pPayload. If nPayload>spaceLeft, 6364 ** that means content must spill into overflow pages. 6365 ** *pnSize Size of the local cell (not counting overflow pages) 6366 ** pPrior Where to write the pgno of the first overflow page 6367 ** 6368 ** Use a call to btreeParseCellPtr() to verify that the values above 6369 ** were computed correctly. 6370 */ 6371 #ifdef SQLITE_DEBUG 6372 { 6373 CellInfo info; 6374 pPage->xParseCell(pPage, pCell, &info); 6375 assert( nHeader==(int)(info.pPayload - pCell) ); 6376 assert( info.nKey==pX->nKey ); 6377 assert( *pnSize == info.nSize ); 6378 assert( spaceLeft == info.nLocal ); 6379 } 6380 #endif 6381 6382 /* Write the payload into the local Cell and any extra into overflow pages */ 6383 while( 1 ){ 6384 n = nPayload; 6385 if( n>spaceLeft ) n = spaceLeft; 6386 6387 /* If pToRelease is not zero than pPayload points into the data area 6388 ** of pToRelease. Make sure pToRelease is still writeable. */ 6389 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) ); 6390 6391 /* If pPayload is part of the data area of pPage, then make sure pPage 6392 ** is still writeable */ 6393 assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize] 6394 || sqlite3PagerIswriteable(pPage->pDbPage) ); 6395 6396 if( nSrc>=n ){ 6397 memcpy(pPayload, pSrc, n); 6398 }else if( nSrc>0 ){ 6399 n = nSrc; 6400 memcpy(pPayload, pSrc, n); 6401 }else{ 6402 memset(pPayload, 0, n); 6403 } 6404 nPayload -= n; 6405 if( nPayload<=0 ) break; 6406 pPayload += n; 6407 pSrc += n; 6408 nSrc -= n; 6409 spaceLeft -= n; 6410 if( spaceLeft==0 ){ 6411 MemPage *pOvfl = 0; 6412 #ifndef SQLITE_OMIT_AUTOVACUUM 6413 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */ 6414 if( pBt->autoVacuum ){ 6415 do{ 6416 pgnoOvfl++; 6417 } while( 6418 PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt) 6419 ); 6420 } 6421 #endif 6422 rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0); 6423 #ifndef SQLITE_OMIT_AUTOVACUUM 6424 /* If the database supports auto-vacuum, and the second or subsequent 6425 ** overflow page is being allocated, add an entry to the pointer-map 6426 ** for that page now. 6427 ** 6428 ** If this is the first overflow page, then write a partial entry 6429 ** to the pointer-map. If we write nothing to this pointer-map slot, 6430 ** then the optimistic overflow chain processing in clearCell() 6431 ** may misinterpret the uninitialized values and delete the 6432 ** wrong pages from the database. 6433 */ 6434 if( pBt->autoVacuum && rc==SQLITE_OK ){ 6435 u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1); 6436 ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc); 6437 if( rc ){ 6438 releasePage(pOvfl); 6439 } 6440 } 6441 #endif 6442 if( rc ){ 6443 releasePage(pToRelease); 6444 return rc; 6445 } 6446 6447 /* If pToRelease is not zero than pPrior points into the data area 6448 ** of pToRelease. Make sure pToRelease is still writeable. */ 6449 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) ); 6450 6451 /* If pPrior is part of the data area of pPage, then make sure pPage 6452 ** is still writeable */ 6453 assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize] 6454 || sqlite3PagerIswriteable(pPage->pDbPage) ); 6455 6456 put4byte(pPrior, pgnoOvfl); 6457 releasePage(pToRelease); 6458 pToRelease = pOvfl; 6459 pPrior = pOvfl->aData; 6460 put4byte(pPrior, 0); 6461 pPayload = &pOvfl->aData[4]; 6462 spaceLeft = pBt->usableSize - 4; 6463 } 6464 } 6465 releasePage(pToRelease); 6466 return SQLITE_OK; 6467 } 6468 6469 /* 6470 ** Remove the i-th cell from pPage. This routine effects pPage only. 6471 ** The cell content is not freed or deallocated. It is assumed that 6472 ** the cell content has been copied someplace else. This routine just 6473 ** removes the reference to the cell from pPage. 6474 ** 6475 ** "sz" must be the number of bytes in the cell. 6476 */ 6477 static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){ 6478 u32 pc; /* Offset to cell content of cell being deleted */ 6479 u8 *data; /* pPage->aData */ 6480 u8 *ptr; /* Used to move bytes around within data[] */ 6481 int rc; /* The return code */ 6482 int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */ 6483 6484 if( *pRC ) return; 6485 assert( idx>=0 && idx<pPage->nCell ); 6486 assert( CORRUPT_DB || sz==cellSize(pPage, idx) ); 6487 assert( sqlite3PagerIswriteable(pPage->pDbPage) ); 6488 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 6489 data = pPage->aData; 6490 ptr = &pPage->aCellIdx[2*idx]; 6491 pc = get2byte(ptr); 6492 hdr = pPage->hdrOffset; 6493 testcase( pc==get2byte(&data[hdr+5]) ); 6494 testcase( pc+sz==pPage->pBt->usableSize ); 6495 if( pc+sz > pPage->pBt->usableSize ){ 6496 *pRC = SQLITE_CORRUPT_BKPT; 6497 return; 6498 } 6499 rc = freeSpace(pPage, pc, sz); 6500 if( rc ){ 6501 *pRC = rc; 6502 return; 6503 } 6504 pPage->nCell--; 6505 if( pPage->nCell==0 ){ 6506 memset(&data[hdr+1], 0, 4); 6507 data[hdr+7] = 0; 6508 put2byte(&data[hdr+5], pPage->pBt->usableSize); 6509 pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset 6510 - pPage->childPtrSize - 8; 6511 }else{ 6512 memmove(ptr, ptr+2, 2*(pPage->nCell - idx)); 6513 put2byte(&data[hdr+3], pPage->nCell); 6514 pPage->nFree += 2; 6515 } 6516 } 6517 6518 /* 6519 ** Insert a new cell on pPage at cell index "i". pCell points to the 6520 ** content of the cell. 6521 ** 6522 ** If the cell content will fit on the page, then put it there. If it 6523 ** will not fit, then make a copy of the cell content into pTemp if 6524 ** pTemp is not null. Regardless of pTemp, allocate a new entry 6525 ** in pPage->apOvfl[] and make it point to the cell content (either 6526 ** in pTemp or the original pCell) and also record its index. 6527 ** Allocating a new entry in pPage->aCell[] implies that 6528 ** pPage->nOverflow is incremented. 6529 ** 6530 ** *pRC must be SQLITE_OK when this routine is called. 6531 */ 6532 static void insertCell( 6533 MemPage *pPage, /* Page into which we are copying */ 6534 int i, /* New cell becomes the i-th cell of the page */ 6535 u8 *pCell, /* Content of the new cell */ 6536 int sz, /* Bytes of content in pCell */ 6537 u8 *pTemp, /* Temp storage space for pCell, if needed */ 6538 Pgno iChild, /* If non-zero, replace first 4 bytes with this value */ 6539 int *pRC /* Read and write return code from here */ 6540 ){ 6541 int idx = 0; /* Where to write new cell content in data[] */ 6542 int j; /* Loop counter */ 6543 u8 *data; /* The content of the whole page */ 6544 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */ 6545 6546 assert( *pRC==SQLITE_OK ); 6547 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow ); 6548 assert( MX_CELL(pPage->pBt)<=10921 ); 6549 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB ); 6550 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) ); 6551 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) ); 6552 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 6553 /* The cell should normally be sized correctly. However, when moving a 6554 ** malformed cell from a leaf page to an interior page, if the cell size 6555 ** wanted to be less than 4 but got rounded up to 4 on the leaf, then size 6556 ** might be less than 8 (leaf-size + pointer) on the interior node. Hence 6557 ** the term after the || in the following assert(). */ 6558 assert( sz==pPage->xCellSize(pPage, pCell) || (sz==8 && iChild>0) ); 6559 if( pPage->nOverflow || sz+2>pPage->nFree ){ 6560 if( pTemp ){ 6561 memcpy(pTemp, pCell, sz); 6562 pCell = pTemp; 6563 } 6564 if( iChild ){ 6565 put4byte(pCell, iChild); 6566 } 6567 j = pPage->nOverflow++; 6568 /* Comparison against ArraySize-1 since we hold back one extra slot 6569 ** as a contingency. In other words, never need more than 3 overflow 6570 ** slots but 4 are allocated, just to be safe. */ 6571 assert( j < ArraySize(pPage->apOvfl)-1 ); 6572 pPage->apOvfl[j] = pCell; 6573 pPage->aiOvfl[j] = (u16)i; 6574 6575 /* When multiple overflows occur, they are always sequential and in 6576 ** sorted order. This invariants arise because multiple overflows can 6577 ** only occur when inserting divider cells into the parent page during 6578 ** balancing, and the dividers are adjacent and sorted. 6579 */ 6580 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */ 6581 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */ 6582 }else{ 6583 int rc = sqlite3PagerWrite(pPage->pDbPage); 6584 if( rc!=SQLITE_OK ){ 6585 *pRC = rc; 6586 return; 6587 } 6588 assert( sqlite3PagerIswriteable(pPage->pDbPage) ); 6589 data = pPage->aData; 6590 assert( &data[pPage->cellOffset]==pPage->aCellIdx ); 6591 rc = allocateSpace(pPage, sz, &idx); 6592 if( rc ){ *pRC = rc; return; } 6593 /* The allocateSpace() routine guarantees the following properties 6594 ** if it returns successfully */ 6595 assert( idx >= 0 ); 6596 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB ); 6597 assert( idx+sz <= (int)pPage->pBt->usableSize ); 6598 pPage->nFree -= (u16)(2 + sz); 6599 memcpy(&data[idx], pCell, sz); 6600 if( iChild ){ 6601 put4byte(&data[idx], iChild); 6602 } 6603 pIns = pPage->aCellIdx + i*2; 6604 memmove(pIns+2, pIns, 2*(pPage->nCell - i)); 6605 put2byte(pIns, idx); 6606 pPage->nCell++; 6607 /* increment the cell count */ 6608 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++; 6609 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell ); 6610 #ifndef SQLITE_OMIT_AUTOVACUUM 6611 if( pPage->pBt->autoVacuum ){ 6612 /* The cell may contain a pointer to an overflow page. If so, write 6613 ** the entry for the overflow page into the pointer map. 6614 */ 6615 ptrmapPutOvflPtr(pPage, pCell, pRC); 6616 } 6617 #endif 6618 } 6619 } 6620 6621 /* 6622 ** A CellArray object contains a cache of pointers and sizes for a 6623 ** consecutive sequence of cells that might be held on multiple pages. 6624 */ 6625 typedef struct CellArray CellArray; 6626 struct CellArray { 6627 int nCell; /* Number of cells in apCell[] */ 6628 MemPage *pRef; /* Reference page */ 6629 u8 **apCell; /* All cells begin balanced */ 6630 u16 *szCell; /* Local size of all cells in apCell[] */ 6631 }; 6632 6633 /* 6634 ** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been 6635 ** computed. 6636 */ 6637 static void populateCellCache(CellArray *p, int idx, int N){ 6638 assert( idx>=0 && idx+N<=p->nCell ); 6639 while( N>0 ){ 6640 assert( p->apCell[idx]!=0 ); 6641 if( p->szCell[idx]==0 ){ 6642 p->szCell[idx] = p->pRef->xCellSize(p->pRef, p->apCell[idx]); 6643 }else{ 6644 assert( CORRUPT_DB || 6645 p->szCell[idx]==p->pRef->xCellSize(p->pRef, p->apCell[idx]) ); 6646 } 6647 idx++; 6648 N--; 6649 } 6650 } 6651 6652 /* 6653 ** Return the size of the Nth element of the cell array 6654 */ 6655 static SQLITE_NOINLINE u16 computeCellSize(CellArray *p, int N){ 6656 assert( N>=0 && N<p->nCell ); 6657 assert( p->szCell[N]==0 ); 6658 p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]); 6659 return p->szCell[N]; 6660 } 6661 static u16 cachedCellSize(CellArray *p, int N){ 6662 assert( N>=0 && N<p->nCell ); 6663 if( p->szCell[N] ) return p->szCell[N]; 6664 return computeCellSize(p, N); 6665 } 6666 6667 /* 6668 ** Array apCell[] contains pointers to nCell b-tree page cells. The 6669 ** szCell[] array contains the size in bytes of each cell. This function 6670 ** replaces the current contents of page pPg with the contents of the cell 6671 ** array. 6672 ** 6673 ** Some of the cells in apCell[] may currently be stored in pPg. This 6674 ** function works around problems caused by this by making a copy of any 6675 ** such cells before overwriting the page data. 6676 ** 6677 ** The MemPage.nFree field is invalidated by this function. It is the 6678 ** responsibility of the caller to set it correctly. 6679 */ 6680 static int rebuildPage( 6681 MemPage *pPg, /* Edit this page */ 6682 int nCell, /* Final number of cells on page */ 6683 u8 **apCell, /* Array of cells */ 6684 u16 *szCell /* Array of cell sizes */ 6685 ){ 6686 const int hdr = pPg->hdrOffset; /* Offset of header on pPg */ 6687 u8 * const aData = pPg->aData; /* Pointer to data for pPg */ 6688 const int usableSize = pPg->pBt->usableSize; 6689 u8 * const pEnd = &aData[usableSize]; 6690 int i; 6691 u8 *pCellptr = pPg->aCellIdx; 6692 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager); 6693 u8 *pData; 6694 6695 i = get2byte(&aData[hdr+5]); 6696 memcpy(&pTmp[i], &aData[i], usableSize - i); 6697 6698 pData = pEnd; 6699 for(i=0; i<nCell; i++){ 6700 u8 *pCell = apCell[i]; 6701 if( SQLITE_WITHIN(pCell,aData,pEnd) ){ 6702 pCell = &pTmp[pCell - aData]; 6703 } 6704 pData -= szCell[i]; 6705 put2byte(pCellptr, (pData - aData)); 6706 pCellptr += 2; 6707 if( pData < pCellptr ) return SQLITE_CORRUPT_BKPT; 6708 memcpy(pData, pCell, szCell[i]); 6709 assert( szCell[i]==pPg->xCellSize(pPg, pCell) || CORRUPT_DB ); 6710 testcase( szCell[i]!=pPg->xCellSize(pPg,pCell) ); 6711 } 6712 6713 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */ 6714 pPg->nCell = nCell; 6715 pPg->nOverflow = 0; 6716 6717 put2byte(&aData[hdr+1], 0); 6718 put2byte(&aData[hdr+3], pPg->nCell); 6719 put2byte(&aData[hdr+5], pData - aData); 6720 aData[hdr+7] = 0x00; 6721 return SQLITE_OK; 6722 } 6723 6724 /* 6725 ** Array apCell[] contains nCell pointers to b-tree cells. Array szCell 6726 ** contains the size in bytes of each such cell. This function attempts to 6727 ** add the cells stored in the array to page pPg. If it cannot (because 6728 ** the page needs to be defragmented before the cells will fit), non-zero 6729 ** is returned. Otherwise, if the cells are added successfully, zero is 6730 ** returned. 6731 ** 6732 ** Argument pCellptr points to the first entry in the cell-pointer array 6733 ** (part of page pPg) to populate. After cell apCell[0] is written to the 6734 ** page body, a 16-bit offset is written to pCellptr. And so on, for each 6735 ** cell in the array. It is the responsibility of the caller to ensure 6736 ** that it is safe to overwrite this part of the cell-pointer array. 6737 ** 6738 ** When this function is called, *ppData points to the start of the 6739 ** content area on page pPg. If the size of the content area is extended, 6740 ** *ppData is updated to point to the new start of the content area 6741 ** before returning. 6742 ** 6743 ** Finally, argument pBegin points to the byte immediately following the 6744 ** end of the space required by this page for the cell-pointer area (for 6745 ** all cells - not just those inserted by the current call). If the content 6746 ** area must be extended to before this point in order to accomodate all 6747 ** cells in apCell[], then the cells do not fit and non-zero is returned. 6748 */ 6749 static int pageInsertArray( 6750 MemPage *pPg, /* Page to add cells to */ 6751 u8 *pBegin, /* End of cell-pointer array */ 6752 u8 **ppData, /* IN/OUT: Page content -area pointer */ 6753 u8 *pCellptr, /* Pointer to cell-pointer area */ 6754 int iFirst, /* Index of first cell to add */ 6755 int nCell, /* Number of cells to add to pPg */ 6756 CellArray *pCArray /* Array of cells */ 6757 ){ 6758 int i; 6759 u8 *aData = pPg->aData; 6760 u8 *pData = *ppData; 6761 int iEnd = iFirst + nCell; 6762 assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */ 6763 for(i=iFirst; i<iEnd; i++){ 6764 int sz, rc; 6765 u8 *pSlot; 6766 sz = cachedCellSize(pCArray, i); 6767 if( (aData[1]==0 && aData[2]==0) || (pSlot = pageFindSlot(pPg,sz,&rc))==0 ){ 6768 if( (pData - pBegin)<sz ) return 1; 6769 pData -= sz; 6770 pSlot = pData; 6771 } 6772 /* pSlot and pCArray->apCell[i] will never overlap on a well-formed 6773 ** database. But they might for a corrupt database. Hence use memmove() 6774 ** since memcpy() sends SIGABORT with overlapping buffers on OpenBSD */ 6775 assert( (pSlot+sz)<=pCArray->apCell[i] 6776 || pSlot>=(pCArray->apCell[i]+sz) 6777 || CORRUPT_DB ); 6778 memmove(pSlot, pCArray->apCell[i], sz); 6779 put2byte(pCellptr, (pSlot - aData)); 6780 pCellptr += 2; 6781 } 6782 *ppData = pData; 6783 return 0; 6784 } 6785 6786 /* 6787 ** Array apCell[] contains nCell pointers to b-tree cells. Array szCell 6788 ** contains the size in bytes of each such cell. This function adds the 6789 ** space associated with each cell in the array that is currently stored 6790 ** within the body of pPg to the pPg free-list. The cell-pointers and other 6791 ** fields of the page are not updated. 6792 ** 6793 ** This function returns the total number of cells added to the free-list. 6794 */ 6795 static int pageFreeArray( 6796 MemPage *pPg, /* Page to edit */ 6797 int iFirst, /* First cell to delete */ 6798 int nCell, /* Cells to delete */ 6799 CellArray *pCArray /* Array of cells */ 6800 ){ 6801 u8 * const aData = pPg->aData; 6802 u8 * const pEnd = &aData[pPg->pBt->usableSize]; 6803 u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize]; 6804 int nRet = 0; 6805 int i; 6806 int iEnd = iFirst + nCell; 6807 u8 *pFree = 0; 6808 int szFree = 0; 6809 6810 for(i=iFirst; i<iEnd; i++){ 6811 u8 *pCell = pCArray->apCell[i]; 6812 if( SQLITE_WITHIN(pCell, pStart, pEnd) ){ 6813 int sz; 6814 /* No need to use cachedCellSize() here. The sizes of all cells that 6815 ** are to be freed have already been computing while deciding which 6816 ** cells need freeing */ 6817 sz = pCArray->szCell[i]; assert( sz>0 ); 6818 if( pFree!=(pCell + sz) ){ 6819 if( pFree ){ 6820 assert( pFree>aData && (pFree - aData)<65536 ); 6821 freeSpace(pPg, (u16)(pFree - aData), szFree); 6822 } 6823 pFree = pCell; 6824 szFree = sz; 6825 if( pFree+sz>pEnd ) return 0; 6826 }else{ 6827 pFree = pCell; 6828 szFree += sz; 6829 } 6830 nRet++; 6831 } 6832 } 6833 if( pFree ){ 6834 assert( pFree>aData && (pFree - aData)<65536 ); 6835 freeSpace(pPg, (u16)(pFree - aData), szFree); 6836 } 6837 return nRet; 6838 } 6839 6840 /* 6841 ** apCell[] and szCell[] contains pointers to and sizes of all cells in the 6842 ** pages being balanced. The current page, pPg, has pPg->nCell cells starting 6843 ** with apCell[iOld]. After balancing, this page should hold nNew cells 6844 ** starting at apCell[iNew]. 6845 ** 6846 ** This routine makes the necessary adjustments to pPg so that it contains 6847 ** the correct cells after being balanced. 6848 ** 6849 ** The pPg->nFree field is invalid when this function returns. It is the 6850 ** responsibility of the caller to set it correctly. 6851 */ 6852 static int editPage( 6853 MemPage *pPg, /* Edit this page */ 6854 int iOld, /* Index of first cell currently on page */ 6855 int iNew, /* Index of new first cell on page */ 6856 int nNew, /* Final number of cells on page */ 6857 CellArray *pCArray /* Array of cells and sizes */ 6858 ){ 6859 u8 * const aData = pPg->aData; 6860 const int hdr = pPg->hdrOffset; 6861 u8 *pBegin = &pPg->aCellIdx[nNew * 2]; 6862 int nCell = pPg->nCell; /* Cells stored on pPg */ 6863 u8 *pData; 6864 u8 *pCellptr; 6865 int i; 6866 int iOldEnd = iOld + pPg->nCell + pPg->nOverflow; 6867 int iNewEnd = iNew + nNew; 6868 6869 #ifdef SQLITE_DEBUG 6870 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager); 6871 memcpy(pTmp, aData, pPg->pBt->usableSize); 6872 #endif 6873 6874 /* Remove cells from the start and end of the page */ 6875 if( iOld<iNew ){ 6876 int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray); 6877 memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2); 6878 nCell -= nShift; 6879 } 6880 if( iNewEnd < iOldEnd ){ 6881 nCell -= pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray); 6882 } 6883 6884 pData = &aData[get2byteNotZero(&aData[hdr+5])]; 6885 if( pData<pBegin ) goto editpage_fail; 6886 6887 /* Add cells to the start of the page */ 6888 if( iNew<iOld ){ 6889 int nAdd = MIN(nNew,iOld-iNew); 6890 assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB ); 6891 pCellptr = pPg->aCellIdx; 6892 memmove(&pCellptr[nAdd*2], pCellptr, nCell*2); 6893 if( pageInsertArray( 6894 pPg, pBegin, &pData, pCellptr, 6895 iNew, nAdd, pCArray 6896 ) ) goto editpage_fail; 6897 nCell += nAdd; 6898 } 6899 6900 /* Add any overflow cells */ 6901 for(i=0; i<pPg->nOverflow; i++){ 6902 int iCell = (iOld + pPg->aiOvfl[i]) - iNew; 6903 if( iCell>=0 && iCell<nNew ){ 6904 pCellptr = &pPg->aCellIdx[iCell * 2]; 6905 memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2); 6906 nCell++; 6907 if( pageInsertArray( 6908 pPg, pBegin, &pData, pCellptr, 6909 iCell+iNew, 1, pCArray 6910 ) ) goto editpage_fail; 6911 } 6912 } 6913 6914 /* Append cells to the end of the page */ 6915 pCellptr = &pPg->aCellIdx[nCell*2]; 6916 if( pageInsertArray( 6917 pPg, pBegin, &pData, pCellptr, 6918 iNew+nCell, nNew-nCell, pCArray 6919 ) ) goto editpage_fail; 6920 6921 pPg->nCell = nNew; 6922 pPg->nOverflow = 0; 6923 6924 put2byte(&aData[hdr+3], pPg->nCell); 6925 put2byte(&aData[hdr+5], pData - aData); 6926 6927 #ifdef SQLITE_DEBUG 6928 for(i=0; i<nNew && !CORRUPT_DB; i++){ 6929 u8 *pCell = pCArray->apCell[i+iNew]; 6930 int iOff = get2byteAligned(&pPg->aCellIdx[i*2]); 6931 if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize]) ){ 6932 pCell = &pTmp[pCell - aData]; 6933 } 6934 assert( 0==memcmp(pCell, &aData[iOff], 6935 pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) ); 6936 } 6937 #endif 6938 6939 return SQLITE_OK; 6940 editpage_fail: 6941 /* Unable to edit this page. Rebuild it from scratch instead. */ 6942 populateCellCache(pCArray, iNew, nNew); 6943 return rebuildPage(pPg, nNew, &pCArray->apCell[iNew], &pCArray->szCell[iNew]); 6944 } 6945 6946 /* 6947 ** The following parameters determine how many adjacent pages get involved 6948 ** in a balancing operation. NN is the number of neighbors on either side 6949 ** of the page that participate in the balancing operation. NB is the 6950 ** total number of pages that participate, including the target page and 6951 ** NN neighbors on either side. 6952 ** 6953 ** The minimum value of NN is 1 (of course). Increasing NN above 1 6954 ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance 6955 ** in exchange for a larger degradation in INSERT and UPDATE performance. 6956 ** The value of NN appears to give the best results overall. 6957 */ 6958 #define NN 1 /* Number of neighbors on either side of pPage */ 6959 #define NB (NN*2+1) /* Total pages involved in the balance */ 6960 6961 6962 #ifndef SQLITE_OMIT_QUICKBALANCE 6963 /* 6964 ** This version of balance() handles the common special case where 6965 ** a new entry is being inserted on the extreme right-end of the 6966 ** tree, in other words, when the new entry will become the largest 6967 ** entry in the tree. 6968 ** 6969 ** Instead of trying to balance the 3 right-most leaf pages, just add 6970 ** a new page to the right-hand side and put the one new entry in 6971 ** that page. This leaves the right side of the tree somewhat 6972 ** unbalanced. But odds are that we will be inserting new entries 6973 ** at the end soon afterwards so the nearly empty page will quickly 6974 ** fill up. On average. 6975 ** 6976 ** pPage is the leaf page which is the right-most page in the tree. 6977 ** pParent is its parent. pPage must have a single overflow entry 6978 ** which is also the right-most entry on the page. 6979 ** 6980 ** The pSpace buffer is used to store a temporary copy of the divider 6981 ** cell that will be inserted into pParent. Such a cell consists of a 4 6982 ** byte page number followed by a variable length integer. In other 6983 ** words, at most 13 bytes. Hence the pSpace buffer must be at 6984 ** least 13 bytes in size. 6985 */ 6986 static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){ 6987 BtShared *const pBt = pPage->pBt; /* B-Tree Database */ 6988 MemPage *pNew; /* Newly allocated page */ 6989 int rc; /* Return Code */ 6990 Pgno pgnoNew; /* Page number of pNew */ 6991 6992 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 6993 assert( sqlite3PagerIswriteable(pParent->pDbPage) ); 6994 assert( pPage->nOverflow==1 ); 6995 6996 /* This error condition is now caught prior to reaching this function */ 6997 if( NEVER(pPage->nCell==0) ) return SQLITE_CORRUPT_BKPT; 6998 6999 /* Allocate a new page. This page will become the right-sibling of 7000 ** pPage. Make the parent page writable, so that the new divider cell 7001 ** may be inserted. If both these operations are successful, proceed. 7002 */ 7003 rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0); 7004 7005 if( rc==SQLITE_OK ){ 7006 7007 u8 *pOut = &pSpace[4]; 7008 u8 *pCell = pPage->apOvfl[0]; 7009 u16 szCell = pPage->xCellSize(pPage, pCell); 7010 u8 *pStop; 7011 7012 assert( sqlite3PagerIswriteable(pNew->pDbPage) ); 7013 assert( pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) ); 7014 zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF); 7015 rc = rebuildPage(pNew, 1, &pCell, &szCell); 7016 if( NEVER(rc) ) return rc; 7017 pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell; 7018 7019 /* If this is an auto-vacuum database, update the pointer map 7020 ** with entries for the new page, and any pointer from the 7021 ** cell on the page to an overflow page. If either of these 7022 ** operations fails, the return code is set, but the contents 7023 ** of the parent page are still manipulated by thh code below. 7024 ** That is Ok, at this point the parent page is guaranteed to 7025 ** be marked as dirty. Returning an error code will cause a 7026 ** rollback, undoing any changes made to the parent page. 7027 */ 7028 if( ISAUTOVACUUM ){ 7029 ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc); 7030 if( szCell>pNew->minLocal ){ 7031 ptrmapPutOvflPtr(pNew, pCell, &rc); 7032 } 7033 } 7034 7035 /* Create a divider cell to insert into pParent. The divider cell 7036 ** consists of a 4-byte page number (the page number of pPage) and 7037 ** a variable length key value (which must be the same value as the 7038 ** largest key on pPage). 7039 ** 7040 ** To find the largest key value on pPage, first find the right-most 7041 ** cell on pPage. The first two fields of this cell are the 7042 ** record-length (a variable length integer at most 32-bits in size) 7043 ** and the key value (a variable length integer, may have any value). 7044 ** The first of the while(...) loops below skips over the record-length 7045 ** field. The second while(...) loop copies the key value from the 7046 ** cell on pPage into the pSpace buffer. 7047 */ 7048 pCell = findCell(pPage, pPage->nCell-1); 7049 pStop = &pCell[9]; 7050 while( (*(pCell++)&0x80) && pCell<pStop ); 7051 pStop = &pCell[9]; 7052 while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop ); 7053 7054 /* Insert the new divider cell into pParent. */ 7055 if( rc==SQLITE_OK ){ 7056 insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace), 7057 0, pPage->pgno, &rc); 7058 } 7059 7060 /* Set the right-child pointer of pParent to point to the new page. */ 7061 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew); 7062 7063 /* Release the reference to the new page. */ 7064 releasePage(pNew); 7065 } 7066 7067 return rc; 7068 } 7069 #endif /* SQLITE_OMIT_QUICKBALANCE */ 7070 7071 #if 0 7072 /* 7073 ** This function does not contribute anything to the operation of SQLite. 7074 ** it is sometimes activated temporarily while debugging code responsible 7075 ** for setting pointer-map entries. 7076 */ 7077 static int ptrmapCheckPages(MemPage **apPage, int nPage){ 7078 int i, j; 7079 for(i=0; i<nPage; i++){ 7080 Pgno n; 7081 u8 e; 7082 MemPage *pPage = apPage[i]; 7083 BtShared *pBt = pPage->pBt; 7084 assert( pPage->isInit ); 7085 7086 for(j=0; j<pPage->nCell; j++){ 7087 CellInfo info; 7088 u8 *z; 7089 7090 z = findCell(pPage, j); 7091 pPage->xParseCell(pPage, z, &info); 7092 if( info.nLocal<info.nPayload ){ 7093 Pgno ovfl = get4byte(&z[info.nSize-4]); 7094 ptrmapGet(pBt, ovfl, &e, &n); 7095 assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 ); 7096 } 7097 if( !pPage->leaf ){ 7098 Pgno child = get4byte(z); 7099 ptrmapGet(pBt, child, &e, &n); 7100 assert( n==pPage->pgno && e==PTRMAP_BTREE ); 7101 } 7102 } 7103 if( !pPage->leaf ){ 7104 Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]); 7105 ptrmapGet(pBt, child, &e, &n); 7106 assert( n==pPage->pgno && e==PTRMAP_BTREE ); 7107 } 7108 } 7109 return 1; 7110 } 7111 #endif 7112 7113 /* 7114 ** This function is used to copy the contents of the b-tree node stored 7115 ** on page pFrom to page pTo. If page pFrom was not a leaf page, then 7116 ** the pointer-map entries for each child page are updated so that the 7117 ** parent page stored in the pointer map is page pTo. If pFrom contained 7118 ** any cells with overflow page pointers, then the corresponding pointer 7119 ** map entries are also updated so that the parent page is page pTo. 7120 ** 7121 ** If pFrom is currently carrying any overflow cells (entries in the 7122 ** MemPage.apOvfl[] array), they are not copied to pTo. 7123 ** 7124 ** Before returning, page pTo is reinitialized using btreeInitPage(). 7125 ** 7126 ** The performance of this function is not critical. It is only used by 7127 ** the balance_shallower() and balance_deeper() procedures, neither of 7128 ** which are called often under normal circumstances. 7129 */ 7130 static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){ 7131 if( (*pRC)==SQLITE_OK ){ 7132 BtShared * const pBt = pFrom->pBt; 7133 u8 * const aFrom = pFrom->aData; 7134 u8 * const aTo = pTo->aData; 7135 int const iFromHdr = pFrom->hdrOffset; 7136 int const iToHdr = ((pTo->pgno==1) ? 100 : 0); 7137 int rc; 7138 int iData; 7139 7140 7141 assert( pFrom->isInit ); 7142 assert( pFrom->nFree>=iToHdr ); 7143 assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize ); 7144 7145 /* Copy the b-tree node content from page pFrom to page pTo. */ 7146 iData = get2byte(&aFrom[iFromHdr+5]); 7147 memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData); 7148 memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell); 7149 7150 /* Reinitialize page pTo so that the contents of the MemPage structure 7151 ** match the new data. The initialization of pTo can actually fail under 7152 ** fairly obscure circumstances, even though it is a copy of initialized 7153 ** page pFrom. 7154 */ 7155 pTo->isInit = 0; 7156 rc = btreeInitPage(pTo); 7157 if( rc!=SQLITE_OK ){ 7158 *pRC = rc; 7159 return; 7160 } 7161 7162 /* If this is an auto-vacuum database, update the pointer-map entries 7163 ** for any b-tree or overflow pages that pTo now contains the pointers to. 7164 */ 7165 if( ISAUTOVACUUM ){ 7166 *pRC = setChildPtrmaps(pTo); 7167 } 7168 } 7169 } 7170 7171 /* 7172 ** This routine redistributes cells on the iParentIdx'th child of pParent 7173 ** (hereafter "the page") and up to 2 siblings so that all pages have about the 7174 ** same amount of free space. Usually a single sibling on either side of the 7175 ** page are used in the balancing, though both siblings might come from one 7176 ** side if the page is the first or last child of its parent. If the page 7177 ** has fewer than 2 siblings (something which can only happen if the page 7178 ** is a root page or a child of a root page) then all available siblings 7179 ** participate in the balancing. 7180 ** 7181 ** The number of siblings of the page might be increased or decreased by 7182 ** one or two in an effort to keep pages nearly full but not over full. 7183 ** 7184 ** Note that when this routine is called, some of the cells on the page 7185 ** might not actually be stored in MemPage.aData[]. This can happen 7186 ** if the page is overfull. This routine ensures that all cells allocated 7187 ** to the page and its siblings fit into MemPage.aData[] before returning. 7188 ** 7189 ** In the course of balancing the page and its siblings, cells may be 7190 ** inserted into or removed from the parent page (pParent). Doing so 7191 ** may cause the parent page to become overfull or underfull. If this 7192 ** happens, it is the responsibility of the caller to invoke the correct 7193 ** balancing routine to fix this problem (see the balance() routine). 7194 ** 7195 ** If this routine fails for any reason, it might leave the database 7196 ** in a corrupted state. So if this routine fails, the database should 7197 ** be rolled back. 7198 ** 7199 ** The third argument to this function, aOvflSpace, is a pointer to a 7200 ** buffer big enough to hold one page. If while inserting cells into the parent 7201 ** page (pParent) the parent page becomes overfull, this buffer is 7202 ** used to store the parent's overflow cells. Because this function inserts 7203 ** a maximum of four divider cells into the parent page, and the maximum 7204 ** size of a cell stored within an internal node is always less than 1/4 7205 ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large 7206 ** enough for all overflow cells. 7207 ** 7208 ** If aOvflSpace is set to a null pointer, this function returns 7209 ** SQLITE_NOMEM. 7210 */ 7211 static int balance_nonroot( 7212 MemPage *pParent, /* Parent page of siblings being balanced */ 7213 int iParentIdx, /* Index of "the page" in pParent */ 7214 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */ 7215 int isRoot, /* True if pParent is a root-page */ 7216 int bBulk /* True if this call is part of a bulk load */ 7217 ){ 7218 BtShared *pBt; /* The whole database */ 7219 int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */ 7220 int nNew = 0; /* Number of pages in apNew[] */ 7221 int nOld; /* Number of pages in apOld[] */ 7222 int i, j, k; /* Loop counters */ 7223 int nxDiv; /* Next divider slot in pParent->aCell[] */ 7224 int rc = SQLITE_OK; /* The return code */ 7225 u16 leafCorrection; /* 4 if pPage is a leaf. 0 if not */ 7226 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */ 7227 int usableSpace; /* Bytes in pPage beyond the header */ 7228 int pageFlags; /* Value of pPage->aData[0] */ 7229 int iSpace1 = 0; /* First unused byte of aSpace1[] */ 7230 int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */ 7231 int szScratch; /* Size of scratch memory requested */ 7232 MemPage *apOld[NB]; /* pPage and up to two siblings */ 7233 MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ 7234 u8 *pRight; /* Location in parent of right-sibling pointer */ 7235 u8 *apDiv[NB-1]; /* Divider cells in pParent */ 7236 int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */ 7237 int cntOld[NB+2]; /* Old index in b.apCell[] */ 7238 int szNew[NB+2]; /* Combined size of cells placed on i-th page */ 7239 u8 *aSpace1; /* Space for copies of dividers cells */ 7240 Pgno pgno; /* Temp var to store a page number in */ 7241 u8 abDone[NB+2]; /* True after i'th new page is populated */ 7242 Pgno aPgno[NB+2]; /* Page numbers of new pages before shuffling */ 7243 Pgno aPgOrder[NB+2]; /* Copy of aPgno[] used for sorting pages */ 7244 u16 aPgFlags[NB+2]; /* flags field of new pages before shuffling */ 7245 CellArray b; /* Parsed information on cells being balanced */ 7246 7247 memset(abDone, 0, sizeof(abDone)); 7248 b.nCell = 0; 7249 b.apCell = 0; 7250 pBt = pParent->pBt; 7251 assert( sqlite3_mutex_held(pBt->mutex) ); 7252 assert( sqlite3PagerIswriteable(pParent->pDbPage) ); 7253 7254 #if 0 7255 TRACE(("BALANCE: begin page %d child of %d\n", pPage->pgno, pParent->pgno)); 7256 #endif 7257 7258 /* At this point pParent may have at most one overflow cell. And if 7259 ** this overflow cell is present, it must be the cell with 7260 ** index iParentIdx. This scenario comes about when this function 7261 ** is called (indirectly) from sqlite3BtreeDelete(). 7262 */ 7263 assert( pParent->nOverflow==0 || pParent->nOverflow==1 ); 7264 assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx ); 7265 7266 if( !aOvflSpace ){ 7267 return SQLITE_NOMEM_BKPT; 7268 } 7269 7270 /* Find the sibling pages to balance. Also locate the cells in pParent 7271 ** that divide the siblings. An attempt is made to find NN siblings on 7272 ** either side of pPage. More siblings are taken from one side, however, 7273 ** if there are fewer than NN siblings on the other side. If pParent 7274 ** has NB or fewer children then all children of pParent are taken. 7275 ** 7276 ** This loop also drops the divider cells from the parent page. This 7277 ** way, the remainder of the function does not have to deal with any 7278 ** overflow cells in the parent page, since if any existed they will 7279 ** have already been removed. 7280 */ 7281 i = pParent->nOverflow + pParent->nCell; 7282 if( i<2 ){ 7283 nxDiv = 0; 7284 }else{ 7285 assert( bBulk==0 || bBulk==1 ); 7286 if( iParentIdx==0 ){ 7287 nxDiv = 0; 7288 }else if( iParentIdx==i ){ 7289 nxDiv = i-2+bBulk; 7290 }else{ 7291 nxDiv = iParentIdx-1; 7292 } 7293 i = 2-bBulk; 7294 } 7295 nOld = i+1; 7296 if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){ 7297 pRight = &pParent->aData[pParent->hdrOffset+8]; 7298 }else{ 7299 pRight = findCell(pParent, i+nxDiv-pParent->nOverflow); 7300 } 7301 pgno = get4byte(pRight); 7302 while( 1 ){ 7303 rc = getAndInitPage(pBt, pgno, &apOld[i], 0, 0); 7304 if( rc ){ 7305 memset(apOld, 0, (i+1)*sizeof(MemPage*)); 7306 goto balance_cleanup; 7307 } 7308 nMaxCells += 1+apOld[i]->nCell+apOld[i]->nOverflow; 7309 if( (i--)==0 ) break; 7310 7311 if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){ 7312 apDiv[i] = pParent->apOvfl[0]; 7313 pgno = get4byte(apDiv[i]); 7314 szNew[i] = pParent->xCellSize(pParent, apDiv[i]); 7315 pParent->nOverflow = 0; 7316 }else{ 7317 apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow); 7318 pgno = get4byte(apDiv[i]); 7319 szNew[i] = pParent->xCellSize(pParent, apDiv[i]); 7320 7321 /* Drop the cell from the parent page. apDiv[i] still points to 7322 ** the cell within the parent, even though it has been dropped. 7323 ** This is safe because dropping a cell only overwrites the first 7324 ** four bytes of it, and this function does not need the first 7325 ** four bytes of the divider cell. So the pointer is safe to use 7326 ** later on. 7327 ** 7328 ** But not if we are in secure-delete mode. In secure-delete mode, 7329 ** the dropCell() routine will overwrite the entire cell with zeroes. 7330 ** In this case, temporarily copy the cell into the aOvflSpace[] 7331 ** buffer. It will be copied out again as soon as the aSpace[] buffer 7332 ** is allocated. */ 7333 if( pBt->btsFlags & BTS_FAST_SECURE ){ 7334 int iOff; 7335 7336 iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData); 7337 if( (iOff+szNew[i])>(int)pBt->usableSize ){ 7338 rc = SQLITE_CORRUPT_BKPT; 7339 memset(apOld, 0, (i+1)*sizeof(MemPage*)); 7340 goto balance_cleanup; 7341 }else{ 7342 memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]); 7343 apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData]; 7344 } 7345 } 7346 dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc); 7347 } 7348 } 7349 7350 /* Make nMaxCells a multiple of 4 in order to preserve 8-byte 7351 ** alignment */ 7352 nMaxCells = (nMaxCells + 3)&~3; 7353 7354 /* 7355 ** Allocate space for memory structures 7356 */ 7357 szScratch = 7358 nMaxCells*sizeof(u8*) /* b.apCell */ 7359 + nMaxCells*sizeof(u16) /* b.szCell */ 7360 + pBt->pageSize; /* aSpace1 */ 7361 7362 assert( szScratch<=6*(int)pBt->pageSize ); 7363 b.apCell = sqlite3StackAllocRaw(0, szScratch ); 7364 if( b.apCell==0 ){ 7365 rc = SQLITE_NOMEM_BKPT; 7366 goto balance_cleanup; 7367 } 7368 b.szCell = (u16*)&b.apCell[nMaxCells]; 7369 aSpace1 = (u8*)&b.szCell[nMaxCells]; 7370 assert( EIGHT_BYTE_ALIGNMENT(aSpace1) ); 7371 7372 /* 7373 ** Load pointers to all cells on sibling pages and the divider cells 7374 ** into the local b.apCell[] array. Make copies of the divider cells 7375 ** into space obtained from aSpace1[]. The divider cells have already 7376 ** been removed from pParent. 7377 ** 7378 ** If the siblings are on leaf pages, then the child pointers of the 7379 ** divider cells are stripped from the cells before they are copied 7380 ** into aSpace1[]. In this way, all cells in b.apCell[] are without 7381 ** child pointers. If siblings are not leaves, then all cell in 7382 ** b.apCell[] include child pointers. Either way, all cells in b.apCell[] 7383 ** are alike. 7384 ** 7385 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf. 7386 ** leafData: 1 if pPage holds key+data and pParent holds only keys. 7387 */ 7388 b.pRef = apOld[0]; 7389 leafCorrection = b.pRef->leaf*4; 7390 leafData = b.pRef->intKeyLeaf; 7391 for(i=0; i<nOld; i++){ 7392 MemPage *pOld = apOld[i]; 7393 int limit = pOld->nCell; 7394 u8 *aData = pOld->aData; 7395 u16 maskPage = pOld->maskPage; 7396 u8 *piCell = aData + pOld->cellOffset; 7397 u8 *piEnd; 7398 7399 /* Verify that all sibling pages are of the same "type" (table-leaf, 7400 ** table-interior, index-leaf, or index-interior). 7401 */ 7402 if( pOld->aData[0]!=apOld[0]->aData[0] ){ 7403 rc = SQLITE_CORRUPT_BKPT; 7404 goto balance_cleanup; 7405 } 7406 7407 /* Load b.apCell[] with pointers to all cells in pOld. If pOld 7408 ** contains overflow cells, include them in the b.apCell[] array 7409 ** in the correct spot. 7410 ** 7411 ** Note that when there are multiple overflow cells, it is always the 7412 ** case that they are sequential and adjacent. This invariant arises 7413 ** because multiple overflows can only occurs when inserting divider 7414 ** cells into a parent on a prior balance, and divider cells are always 7415 ** adjacent and are inserted in order. There is an assert() tagged 7416 ** with "NOTE 1" in the overflow cell insertion loop to prove this 7417 ** invariant. 7418 ** 7419 ** This must be done in advance. Once the balance starts, the cell 7420 ** offset section of the btree page will be overwritten and we will no 7421 ** long be able to find the cells if a pointer to each cell is not saved 7422 ** first. 7423 */ 7424 memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow)); 7425 if( pOld->nOverflow>0 ){ 7426 limit = pOld->aiOvfl[0]; 7427 for(j=0; j<limit; j++){ 7428 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell)); 7429 piCell += 2; 7430 b.nCell++; 7431 } 7432 for(k=0; k<pOld->nOverflow; k++){ 7433 assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */ 7434 b.apCell[b.nCell] = pOld->apOvfl[k]; 7435 b.nCell++; 7436 } 7437 } 7438 piEnd = aData + pOld->cellOffset + 2*pOld->nCell; 7439 while( piCell<piEnd ){ 7440 assert( b.nCell<nMaxCells ); 7441 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell)); 7442 piCell += 2; 7443 b.nCell++; 7444 } 7445 7446 cntOld[i] = b.nCell; 7447 if( i<nOld-1 && !leafData){ 7448 u16 sz = (u16)szNew[i]; 7449 u8 *pTemp; 7450 assert( b.nCell<nMaxCells ); 7451 b.szCell[b.nCell] = sz; 7452 pTemp = &aSpace1[iSpace1]; 7453 iSpace1 += sz; 7454 assert( sz<=pBt->maxLocal+23 ); 7455 assert( iSpace1 <= (int)pBt->pageSize ); 7456 memcpy(pTemp, apDiv[i], sz); 7457 b.apCell[b.nCell] = pTemp+leafCorrection; 7458 assert( leafCorrection==0 || leafCorrection==4 ); 7459 b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection; 7460 if( !pOld->leaf ){ 7461 assert( leafCorrection==0 ); 7462 assert( pOld->hdrOffset==0 ); 7463 /* The right pointer of the child page pOld becomes the left 7464 ** pointer of the divider cell */ 7465 memcpy(b.apCell[b.nCell], &pOld->aData[8], 4); 7466 }else{ 7467 assert( leafCorrection==4 ); 7468 while( b.szCell[b.nCell]<4 ){ 7469 /* Do not allow any cells smaller than 4 bytes. If a smaller cell 7470 ** does exist, pad it with 0x00 bytes. */ 7471 assert( b.szCell[b.nCell]==3 || CORRUPT_DB ); 7472 assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB ); 7473 aSpace1[iSpace1++] = 0x00; 7474 b.szCell[b.nCell]++; 7475 } 7476 } 7477 b.nCell++; 7478 } 7479 } 7480 7481 /* 7482 ** Figure out the number of pages needed to hold all b.nCell cells. 7483 ** Store this number in "k". Also compute szNew[] which is the total 7484 ** size of all cells on the i-th page and cntNew[] which is the index 7485 ** in b.apCell[] of the cell that divides page i from page i+1. 7486 ** cntNew[k] should equal b.nCell. 7487 ** 7488 ** Values computed by this block: 7489 ** 7490 ** k: The total number of sibling pages 7491 ** szNew[i]: Spaced used on the i-th sibling page. 7492 ** cntNew[i]: Index in b.apCell[] and b.szCell[] for the first cell to 7493 ** the right of the i-th sibling page. 7494 ** usableSpace: Number of bytes of space available on each sibling. 7495 ** 7496 */ 7497 usableSpace = pBt->usableSize - 12 + leafCorrection; 7498 for(i=0; i<nOld; i++){ 7499 MemPage *p = apOld[i]; 7500 szNew[i] = usableSpace - p->nFree; 7501 for(j=0; j<p->nOverflow; j++){ 7502 szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]); 7503 } 7504 cntNew[i] = cntOld[i]; 7505 } 7506 k = nOld; 7507 for(i=0; i<k; i++){ 7508 int sz; 7509 while( szNew[i]>usableSpace ){ 7510 if( i+1>=k ){ 7511 k = i+2; 7512 if( k>NB+2 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; } 7513 szNew[k-1] = 0; 7514 cntNew[k-1] = b.nCell; 7515 } 7516 sz = 2 + cachedCellSize(&b, cntNew[i]-1); 7517 szNew[i] -= sz; 7518 if( !leafData ){ 7519 if( cntNew[i]<b.nCell ){ 7520 sz = 2 + cachedCellSize(&b, cntNew[i]); 7521 }else{ 7522 sz = 0; 7523 } 7524 } 7525 szNew[i+1] += sz; 7526 cntNew[i]--; 7527 } 7528 while( cntNew[i]<b.nCell ){ 7529 sz = 2 + cachedCellSize(&b, cntNew[i]); 7530 if( szNew[i]+sz>usableSpace ) break; 7531 szNew[i] += sz; 7532 cntNew[i]++; 7533 if( !leafData ){ 7534 if( cntNew[i]<b.nCell ){ 7535 sz = 2 + cachedCellSize(&b, cntNew[i]); 7536 }else{ 7537 sz = 0; 7538 } 7539 } 7540 szNew[i+1] -= sz; 7541 } 7542 if( cntNew[i]>=b.nCell ){ 7543 k = i+1; 7544 }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){ 7545 rc = SQLITE_CORRUPT_BKPT; 7546 goto balance_cleanup; 7547 } 7548 } 7549 7550 /* 7551 ** The packing computed by the previous block is biased toward the siblings 7552 ** on the left side (siblings with smaller keys). The left siblings are 7553 ** always nearly full, while the right-most sibling might be nearly empty. 7554 ** The next block of code attempts to adjust the packing of siblings to 7555 ** get a better balance. 7556 ** 7557 ** This adjustment is more than an optimization. The packing above might 7558 ** be so out of balance as to be illegal. For example, the right-most 7559 ** sibling might be completely empty. This adjustment is not optional. 7560 */ 7561 for(i=k-1; i>0; i--){ 7562 int szRight = szNew[i]; /* Size of sibling on the right */ 7563 int szLeft = szNew[i-1]; /* Size of sibling on the left */ 7564 int r; /* Index of right-most cell in left sibling */ 7565 int d; /* Index of first cell to the left of right sibling */ 7566 7567 r = cntNew[i-1] - 1; 7568 d = r + 1 - leafData; 7569 (void)cachedCellSize(&b, d); 7570 do{ 7571 assert( d<nMaxCells ); 7572 assert( r<nMaxCells ); 7573 (void)cachedCellSize(&b, r); 7574 if( szRight!=0 7575 && (bBulk || szRight+b.szCell[d]+2 > szLeft-(b.szCell[r]+(i==k-1?0:2)))){ 7576 break; 7577 } 7578 szRight += b.szCell[d] + 2; 7579 szLeft -= b.szCell[r] + 2; 7580 cntNew[i-1] = r; 7581 r--; 7582 d--; 7583 }while( r>=0 ); 7584 szNew[i] = szRight; 7585 szNew[i-1] = szLeft; 7586 if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){ 7587 rc = SQLITE_CORRUPT_BKPT; 7588 goto balance_cleanup; 7589 } 7590 } 7591 7592 /* Sanity check: For a non-corrupt database file one of the follwing 7593 ** must be true: 7594 ** (1) We found one or more cells (cntNew[0])>0), or 7595 ** (2) pPage is a virtual root page. A virtual root page is when 7596 ** the real root page is page 1 and we are the only child of 7597 ** that page. 7598 */ 7599 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB); 7600 TRACE(("BALANCE: old: %d(nc=%d) %d(nc=%d) %d(nc=%d)\n", 7601 apOld[0]->pgno, apOld[0]->nCell, 7602 nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0, 7603 nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0 7604 )); 7605 7606 /* 7607 ** Allocate k new pages. Reuse old pages where possible. 7608 */ 7609 pageFlags = apOld[0]->aData[0]; 7610 for(i=0; i<k; i++){ 7611 MemPage *pNew; 7612 if( i<nOld ){ 7613 pNew = apNew[i] = apOld[i]; 7614 apOld[i] = 0; 7615 rc = sqlite3PagerWrite(pNew->pDbPage); 7616 nNew++; 7617 if( rc ) goto balance_cleanup; 7618 }else{ 7619 assert( i>0 ); 7620 rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0); 7621 if( rc ) goto balance_cleanup; 7622 zeroPage(pNew, pageFlags); 7623 apNew[i] = pNew; 7624 nNew++; 7625 cntOld[i] = b.nCell; 7626 7627 /* Set the pointer-map entry for the new sibling page. */ 7628 if( ISAUTOVACUUM ){ 7629 ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc); 7630 if( rc!=SQLITE_OK ){ 7631 goto balance_cleanup; 7632 } 7633 } 7634 } 7635 } 7636 7637 /* 7638 ** Reassign page numbers so that the new pages are in ascending order. 7639 ** This helps to keep entries in the disk file in order so that a scan 7640 ** of the table is closer to a linear scan through the file. That in turn 7641 ** helps the operating system to deliver pages from the disk more rapidly. 7642 ** 7643 ** An O(n^2) insertion sort algorithm is used, but since n is never more 7644 ** than (NB+2) (a small constant), that should not be a problem. 7645 ** 7646 ** When NB==3, this one optimization makes the database about 25% faster 7647 ** for large insertions and deletions. 7648 */ 7649 for(i=0; i<nNew; i++){ 7650 aPgOrder[i] = aPgno[i] = apNew[i]->pgno; 7651 aPgFlags[i] = apNew[i]->pDbPage->flags; 7652 for(j=0; j<i; j++){ 7653 if( aPgno[j]==aPgno[i] ){ 7654 /* This branch is taken if the set of sibling pages somehow contains 7655 ** duplicate entries. This can happen if the database is corrupt. 7656 ** It would be simpler to detect this as part of the loop below, but 7657 ** we do the detection here in order to avoid populating the pager 7658 ** cache with two separate objects associated with the same 7659 ** page number. */ 7660 assert( CORRUPT_DB ); 7661 rc = SQLITE_CORRUPT_BKPT; 7662 goto balance_cleanup; 7663 } 7664 } 7665 } 7666 for(i=0; i<nNew; i++){ 7667 int iBest = 0; /* aPgno[] index of page number to use */ 7668 for(j=1; j<nNew; j++){ 7669 if( aPgOrder[j]<aPgOrder[iBest] ) iBest = j; 7670 } 7671 pgno = aPgOrder[iBest]; 7672 aPgOrder[iBest] = 0xffffffff; 7673 if( iBest!=i ){ 7674 if( iBest>i ){ 7675 sqlite3PagerRekey(apNew[iBest]->pDbPage, pBt->nPage+iBest+1, 0); 7676 } 7677 sqlite3PagerRekey(apNew[i]->pDbPage, pgno, aPgFlags[iBest]); 7678 apNew[i]->pgno = pgno; 7679 } 7680 } 7681 7682 TRACE(("BALANCE: new: %d(%d nc=%d) %d(%d nc=%d) %d(%d nc=%d) " 7683 "%d(%d nc=%d) %d(%d nc=%d)\n", 7684 apNew[0]->pgno, szNew[0], cntNew[0], 7685 nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0, 7686 nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0, 7687 nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0, 7688 nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0, 7689 nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0, 7690 nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0, 7691 nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0, 7692 nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0 7693 )); 7694 7695 assert( sqlite3PagerIswriteable(pParent->pDbPage) ); 7696 put4byte(pRight, apNew[nNew-1]->pgno); 7697 7698 /* If the sibling pages are not leaves, ensure that the right-child pointer 7699 ** of the right-most new sibling page is set to the value that was 7700 ** originally in the same field of the right-most old sibling page. */ 7701 if( (pageFlags & PTF_LEAF)==0 && nOld!=nNew ){ 7702 MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1]; 7703 memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4); 7704 } 7705 7706 /* Make any required updates to pointer map entries associated with 7707 ** cells stored on sibling pages following the balance operation. Pointer 7708 ** map entries associated with divider cells are set by the insertCell() 7709 ** routine. The associated pointer map entries are: 7710 ** 7711 ** a) if the cell contains a reference to an overflow chain, the 7712 ** entry associated with the first page in the overflow chain, and 7713 ** 7714 ** b) if the sibling pages are not leaves, the child page associated 7715 ** with the cell. 7716 ** 7717 ** If the sibling pages are not leaves, then the pointer map entry 7718 ** associated with the right-child of each sibling may also need to be 7719 ** updated. This happens below, after the sibling pages have been 7720 ** populated, not here. 7721 */ 7722 if( ISAUTOVACUUM ){ 7723 MemPage *pNew = apNew[0]; 7724 u8 *aOld = pNew->aData; 7725 int cntOldNext = pNew->nCell + pNew->nOverflow; 7726 int usableSize = pBt->usableSize; 7727 int iNew = 0; 7728 int iOld = 0; 7729 7730 for(i=0; i<b.nCell; i++){ 7731 u8 *pCell = b.apCell[i]; 7732 if( i==cntOldNext ){ 7733 MemPage *pOld = (++iOld)<nNew ? apNew[iOld] : apOld[iOld]; 7734 cntOldNext += pOld->nCell + pOld->nOverflow + !leafData; 7735 aOld = pOld->aData; 7736 } 7737 if( i==cntNew[iNew] ){ 7738 pNew = apNew[++iNew]; 7739 if( !leafData ) continue; 7740 } 7741 7742 /* Cell pCell is destined for new sibling page pNew. Originally, it 7743 ** was either part of sibling page iOld (possibly an overflow cell), 7744 ** or else the divider cell to the left of sibling page iOld. So, 7745 ** if sibling page iOld had the same page number as pNew, and if 7746 ** pCell really was a part of sibling page iOld (not a divider or 7747 ** overflow cell), we can skip updating the pointer map entries. */ 7748 if( iOld>=nNew 7749 || pNew->pgno!=aPgno[iOld] 7750 || !SQLITE_WITHIN(pCell,aOld,&aOld[usableSize]) 7751 ){ 7752 if( !leafCorrection ){ 7753 ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc); 7754 } 7755 if( cachedCellSize(&b,i)>pNew->minLocal ){ 7756 ptrmapPutOvflPtr(pNew, pCell, &rc); 7757 } 7758 if( rc ) goto balance_cleanup; 7759 } 7760 } 7761 } 7762 7763 /* Insert new divider cells into pParent. */ 7764 for(i=0; i<nNew-1; i++){ 7765 u8 *pCell; 7766 u8 *pTemp; 7767 int sz; 7768 MemPage *pNew = apNew[i]; 7769 j = cntNew[i]; 7770 7771 assert( j<nMaxCells ); 7772 assert( b.apCell[j]!=0 ); 7773 pCell = b.apCell[j]; 7774 sz = b.szCell[j] + leafCorrection; 7775 pTemp = &aOvflSpace[iOvflSpace]; 7776 if( !pNew->leaf ){ 7777 memcpy(&pNew->aData[8], pCell, 4); 7778 }else if( leafData ){ 7779 /* If the tree is a leaf-data tree, and the siblings are leaves, 7780 ** then there is no divider cell in b.apCell[]. Instead, the divider 7781 ** cell consists of the integer key for the right-most cell of 7782 ** the sibling-page assembled above only. 7783 */ 7784 CellInfo info; 7785 j--; 7786 pNew->xParseCell(pNew, b.apCell[j], &info); 7787 pCell = pTemp; 7788 sz = 4 + putVarint(&pCell[4], info.nKey); 7789 pTemp = 0; 7790 }else{ 7791 pCell -= 4; 7792 /* Obscure case for non-leaf-data trees: If the cell at pCell was 7793 ** previously stored on a leaf node, and its reported size was 4 7794 ** bytes, then it may actually be smaller than this 7795 ** (see btreeParseCellPtr(), 4 bytes is the minimum size of 7796 ** any cell). But it is important to pass the correct size to 7797 ** insertCell(), so reparse the cell now. 7798 ** 7799 ** This can only happen for b-trees used to evaluate "IN (SELECT ...)" 7800 ** and WITHOUT ROWID tables with exactly one column which is the 7801 ** primary key. 7802 */ 7803 if( b.szCell[j]==4 ){ 7804 assert(leafCorrection==4); 7805 sz = pParent->xCellSize(pParent, pCell); 7806 } 7807 } 7808 iOvflSpace += sz; 7809 assert( sz<=pBt->maxLocal+23 ); 7810 assert( iOvflSpace <= (int)pBt->pageSize ); 7811 insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc); 7812 if( rc!=SQLITE_OK ) goto balance_cleanup; 7813 assert( sqlite3PagerIswriteable(pParent->pDbPage) ); 7814 } 7815 7816 /* Now update the actual sibling pages. The order in which they are updated 7817 ** is important, as this code needs to avoid disrupting any page from which 7818 ** cells may still to be read. In practice, this means: 7819 ** 7820 ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1]) 7821 ** then it is not safe to update page apNew[iPg] until after 7822 ** the left-hand sibling apNew[iPg-1] has been updated. 7823 ** 7824 ** (2) If cells are moving right (from apNew[iPg] to apNew[iPg+1]) 7825 ** then it is not safe to update page apNew[iPg] until after 7826 ** the right-hand sibling apNew[iPg+1] has been updated. 7827 ** 7828 ** If neither of the above apply, the page is safe to update. 7829 ** 7830 ** The iPg value in the following loop starts at nNew-1 goes down 7831 ** to 0, then back up to nNew-1 again, thus making two passes over 7832 ** the pages. On the initial downward pass, only condition (1) above 7833 ** needs to be tested because (2) will always be true from the previous 7834 ** step. On the upward pass, both conditions are always true, so the 7835 ** upwards pass simply processes pages that were missed on the downward 7836 ** pass. 7837 */ 7838 for(i=1-nNew; i<nNew; i++){ 7839 int iPg = i<0 ? -i : i; 7840 assert( iPg>=0 && iPg<nNew ); 7841 if( abDone[iPg] ) continue; /* Skip pages already processed */ 7842 if( i>=0 /* On the upwards pass, or... */ 7843 || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */ 7844 ){ 7845 int iNew; 7846 int iOld; 7847 int nNewCell; 7848 7849 /* Verify condition (1): If cells are moving left, update iPg 7850 ** only after iPg-1 has already been updated. */ 7851 assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] ); 7852 7853 /* Verify condition (2): If cells are moving right, update iPg 7854 ** only after iPg+1 has already been updated. */ 7855 assert( cntNew[iPg]>=cntOld[iPg] || abDone[iPg+1] ); 7856 7857 if( iPg==0 ){ 7858 iNew = iOld = 0; 7859 nNewCell = cntNew[0]; 7860 }else{ 7861 iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell; 7862 iNew = cntNew[iPg-1] + !leafData; 7863 nNewCell = cntNew[iPg] - iNew; 7864 } 7865 7866 rc = editPage(apNew[iPg], iOld, iNew, nNewCell, &b); 7867 if( rc ) goto balance_cleanup; 7868 abDone[iPg]++; 7869 apNew[iPg]->nFree = usableSpace-szNew[iPg]; 7870 assert( apNew[iPg]->nOverflow==0 ); 7871 assert( apNew[iPg]->nCell==nNewCell ); 7872 } 7873 } 7874 7875 /* All pages have been processed exactly once */ 7876 assert( memcmp(abDone, "\01\01\01\01\01", nNew)==0 ); 7877 7878 assert( nOld>0 ); 7879 assert( nNew>0 ); 7880 7881 if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){ 7882 /* The root page of the b-tree now contains no cells. The only sibling 7883 ** page is the right-child of the parent. Copy the contents of the 7884 ** child page into the parent, decreasing the overall height of the 7885 ** b-tree structure by one. This is described as the "balance-shallower" 7886 ** sub-algorithm in some documentation. 7887 ** 7888 ** If this is an auto-vacuum database, the call to copyNodeContent() 7889 ** sets all pointer-map entries corresponding to database image pages 7890 ** for which the pointer is stored within the content being copied. 7891 ** 7892 ** It is critical that the child page be defragmented before being 7893 ** copied into the parent, because if the parent is page 1 then it will 7894 ** by smaller than the child due to the database header, and so all the 7895 ** free space needs to be up front. 7896 */ 7897 assert( nNew==1 || CORRUPT_DB ); 7898 rc = defragmentPage(apNew[0], -1); 7899 testcase( rc!=SQLITE_OK ); 7900 assert( apNew[0]->nFree == 7901 (get2byte(&apNew[0]->aData[5])-apNew[0]->cellOffset-apNew[0]->nCell*2) 7902 || rc!=SQLITE_OK 7903 ); 7904 copyNodeContent(apNew[0], pParent, &rc); 7905 freePage(apNew[0], &rc); 7906 }else if( ISAUTOVACUUM && !leafCorrection ){ 7907 /* Fix the pointer map entries associated with the right-child of each 7908 ** sibling page. All other pointer map entries have already been taken 7909 ** care of. */ 7910 for(i=0; i<nNew; i++){ 7911 u32 key = get4byte(&apNew[i]->aData[8]); 7912 ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc); 7913 } 7914 } 7915 7916 assert( pParent->isInit ); 7917 TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n", 7918 nOld, nNew, b.nCell)); 7919 7920 /* Free any old pages that were not reused as new pages. 7921 */ 7922 for(i=nNew; i<nOld; i++){ 7923 freePage(apOld[i], &rc); 7924 } 7925 7926 #if 0 7927 if( ISAUTOVACUUM && rc==SQLITE_OK && apNew[0]->isInit ){ 7928 /* The ptrmapCheckPages() contains assert() statements that verify that 7929 ** all pointer map pages are set correctly. This is helpful while 7930 ** debugging. This is usually disabled because a corrupt database may 7931 ** cause an assert() statement to fail. */ 7932 ptrmapCheckPages(apNew, nNew); 7933 ptrmapCheckPages(&pParent, 1); 7934 } 7935 #endif 7936 7937 /* 7938 ** Cleanup before returning. 7939 */ 7940 balance_cleanup: 7941 sqlite3StackFree(0, b.apCell); 7942 for(i=0; i<nOld; i++){ 7943 releasePage(apOld[i]); 7944 } 7945 for(i=0; i<nNew; i++){ 7946 releasePage(apNew[i]); 7947 } 7948 7949 return rc; 7950 } 7951 7952 7953 /* 7954 ** This function is called when the root page of a b-tree structure is 7955 ** overfull (has one or more overflow pages). 7956 ** 7957 ** A new child page is allocated and the contents of the current root 7958 ** page, including overflow cells, are copied into the child. The root 7959 ** page is then overwritten to make it an empty page with the right-child 7960 ** pointer pointing to the new page. 7961 ** 7962 ** Before returning, all pointer-map entries corresponding to pages 7963 ** that the new child-page now contains pointers to are updated. The 7964 ** entry corresponding to the new right-child pointer of the root 7965 ** page is also updated. 7966 ** 7967 ** If successful, *ppChild is set to contain a reference to the child 7968 ** page and SQLITE_OK is returned. In this case the caller is required 7969 ** to call releasePage() on *ppChild exactly once. If an error occurs, 7970 ** an error code is returned and *ppChild is set to 0. 7971 */ 7972 static int balance_deeper(MemPage *pRoot, MemPage **ppChild){ 7973 int rc; /* Return value from subprocedures */ 7974 MemPage *pChild = 0; /* Pointer to a new child page */ 7975 Pgno pgnoChild = 0; /* Page number of the new child page */ 7976 BtShared *pBt = pRoot->pBt; /* The BTree */ 7977 7978 assert( pRoot->nOverflow>0 ); 7979 assert( sqlite3_mutex_held(pBt->mutex) ); 7980 7981 /* Make pRoot, the root page of the b-tree, writable. Allocate a new 7982 ** page that will become the new right-child of pPage. Copy the contents 7983 ** of the node stored on pRoot into the new child page. 7984 */ 7985 rc = sqlite3PagerWrite(pRoot->pDbPage); 7986 if( rc==SQLITE_OK ){ 7987 rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0); 7988 copyNodeContent(pRoot, pChild, &rc); 7989 if( ISAUTOVACUUM ){ 7990 ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc); 7991 } 7992 } 7993 if( rc ){ 7994 *ppChild = 0; 7995 releasePage(pChild); 7996 return rc; 7997 } 7998 assert( sqlite3PagerIswriteable(pChild->pDbPage) ); 7999 assert( sqlite3PagerIswriteable(pRoot->pDbPage) ); 8000 assert( pChild->nCell==pRoot->nCell ); 8001 8002 TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno)); 8003 8004 /* Copy the overflow cells from pRoot to pChild */ 8005 memcpy(pChild->aiOvfl, pRoot->aiOvfl, 8006 pRoot->nOverflow*sizeof(pRoot->aiOvfl[0])); 8007 memcpy(pChild->apOvfl, pRoot->apOvfl, 8008 pRoot->nOverflow*sizeof(pRoot->apOvfl[0])); 8009 pChild->nOverflow = pRoot->nOverflow; 8010 8011 /* Zero the contents of pRoot. Then install pChild as the right-child. */ 8012 zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF); 8013 put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild); 8014 8015 *ppChild = pChild; 8016 return SQLITE_OK; 8017 } 8018 8019 /* 8020 ** The page that pCur currently points to has just been modified in 8021 ** some way. This function figures out if this modification means the 8022 ** tree needs to be balanced, and if so calls the appropriate balancing 8023 ** routine. Balancing routines are: 8024 ** 8025 ** balance_quick() 8026 ** balance_deeper() 8027 ** balance_nonroot() 8028 */ 8029 static int balance(BtCursor *pCur){ 8030 int rc = SQLITE_OK; 8031 const int nMin = pCur->pBt->usableSize * 2 / 3; 8032 u8 aBalanceQuickSpace[13]; 8033 u8 *pFree = 0; 8034 8035 VVA_ONLY( int balance_quick_called = 0 ); 8036 VVA_ONLY( int balance_deeper_called = 0 ); 8037 8038 do { 8039 int iPage = pCur->iPage; 8040 MemPage *pPage = pCur->pPage; 8041 8042 if( iPage==0 ){ 8043 if( pPage->nOverflow ){ 8044 /* The root page of the b-tree is overfull. In this case call the 8045 ** balance_deeper() function to create a new child for the root-page 8046 ** and copy the current contents of the root-page to it. The 8047 ** next iteration of the do-loop will balance the child page. 8048 */ 8049 assert( balance_deeper_called==0 ); 8050 VVA_ONLY( balance_deeper_called++ ); 8051 rc = balance_deeper(pPage, &pCur->apPage[1]); 8052 if( rc==SQLITE_OK ){ 8053 pCur->iPage = 1; 8054 pCur->ix = 0; 8055 pCur->aiIdx[0] = 0; 8056 pCur->apPage[0] = pPage; 8057 pCur->pPage = pCur->apPage[1]; 8058 assert( pCur->pPage->nOverflow ); 8059 } 8060 }else{ 8061 break; 8062 } 8063 }else if( pPage->nOverflow==0 && pPage->nFree<=nMin ){ 8064 break; 8065 }else{ 8066 MemPage * const pParent = pCur->apPage[iPage-1]; 8067 int const iIdx = pCur->aiIdx[iPage-1]; 8068 8069 rc = sqlite3PagerWrite(pParent->pDbPage); 8070 if( rc==SQLITE_OK ){ 8071 #ifndef SQLITE_OMIT_QUICKBALANCE 8072 if( pPage->intKeyLeaf 8073 && pPage->nOverflow==1 8074 && pPage->aiOvfl[0]==pPage->nCell 8075 && pParent->pgno!=1 8076 && pParent->nCell==iIdx 8077 ){ 8078 /* Call balance_quick() to create a new sibling of pPage on which 8079 ** to store the overflow cell. balance_quick() inserts a new cell 8080 ** into pParent, which may cause pParent overflow. If this 8081 ** happens, the next iteration of the do-loop will balance pParent 8082 ** use either balance_nonroot() or balance_deeper(). Until this 8083 ** happens, the overflow cell is stored in the aBalanceQuickSpace[] 8084 ** buffer. 8085 ** 8086 ** The purpose of the following assert() is to check that only a 8087 ** single call to balance_quick() is made for each call to this 8088 ** function. If this were not verified, a subtle bug involving reuse 8089 ** of the aBalanceQuickSpace[] might sneak in. 8090 */ 8091 assert( balance_quick_called==0 ); 8092 VVA_ONLY( balance_quick_called++ ); 8093 rc = balance_quick(pParent, pPage, aBalanceQuickSpace); 8094 }else 8095 #endif 8096 { 8097 /* In this case, call balance_nonroot() to redistribute cells 8098 ** between pPage and up to 2 of its sibling pages. This involves 8099 ** modifying the contents of pParent, which may cause pParent to 8100 ** become overfull or underfull. The next iteration of the do-loop 8101 ** will balance the parent page to correct this. 8102 ** 8103 ** If the parent page becomes overfull, the overflow cell or cells 8104 ** are stored in the pSpace buffer allocated immediately below. 8105 ** A subsequent iteration of the do-loop will deal with this by 8106 ** calling balance_nonroot() (balance_deeper() may be called first, 8107 ** but it doesn't deal with overflow cells - just moves them to a 8108 ** different page). Once this subsequent call to balance_nonroot() 8109 ** has completed, it is safe to release the pSpace buffer used by 8110 ** the previous call, as the overflow cell data will have been 8111 ** copied either into the body of a database page or into the new 8112 ** pSpace buffer passed to the latter call to balance_nonroot(). 8113 */ 8114 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize); 8115 rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1, 8116 pCur->hints&BTREE_BULKLOAD); 8117 if( pFree ){ 8118 /* If pFree is not NULL, it points to the pSpace buffer used 8119 ** by a previous call to balance_nonroot(). Its contents are 8120 ** now stored either on real database pages or within the 8121 ** new pSpace buffer, so it may be safely freed here. */ 8122 sqlite3PageFree(pFree); 8123 } 8124 8125 /* The pSpace buffer will be freed after the next call to 8126 ** balance_nonroot(), or just before this function returns, whichever 8127 ** comes first. */ 8128 pFree = pSpace; 8129 } 8130 } 8131 8132 pPage->nOverflow = 0; 8133 8134 /* The next iteration of the do-loop balances the parent page. */ 8135 releasePage(pPage); 8136 pCur->iPage--; 8137 assert( pCur->iPage>=0 ); 8138 pCur->pPage = pCur->apPage[pCur->iPage]; 8139 } 8140 }while( rc==SQLITE_OK ); 8141 8142 if( pFree ){ 8143 sqlite3PageFree(pFree); 8144 } 8145 return rc; 8146 } 8147 8148 8149 /* 8150 ** Insert a new record into the BTree. The content of the new record 8151 ** is described by the pX object. The pCur cursor is used only to 8152 ** define what table the record should be inserted into, and is left 8153 ** pointing at a random location. 8154 ** 8155 ** For a table btree (used for rowid tables), only the pX.nKey value of 8156 ** the key is used. The pX.pKey value must be NULL. The pX.nKey is the 8157 ** rowid or INTEGER PRIMARY KEY of the row. The pX.nData,pData,nZero fields 8158 ** hold the content of the row. 8159 ** 8160 ** For an index btree (used for indexes and WITHOUT ROWID tables), the 8161 ** key is an arbitrary byte sequence stored in pX.pKey,nKey. The 8162 ** pX.pData,nData,nZero fields must be zero. 8163 ** 8164 ** If the seekResult parameter is non-zero, then a successful call to 8165 ** MovetoUnpacked() to seek cursor pCur to (pKey,nKey) has already 8166 ** been performed. In other words, if seekResult!=0 then the cursor 8167 ** is currently pointing to a cell that will be adjacent to the cell 8168 ** to be inserted. If seekResult<0 then pCur points to a cell that is 8169 ** smaller then (pKey,nKey). If seekResult>0 then pCur points to a cell 8170 ** that is larger than (pKey,nKey). 8171 ** 8172 ** If seekResult==0, that means pCur is pointing at some unknown location. 8173 ** In that case, this routine must seek the cursor to the correct insertion 8174 ** point for (pKey,nKey) before doing the insertion. For index btrees, 8175 ** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked 8176 ** key values and pX->aMem can be used instead of pX->pKey to avoid having 8177 ** to decode the key. 8178 */ 8179 int sqlite3BtreeInsert( 8180 BtCursor *pCur, /* Insert data into the table of this cursor */ 8181 const BtreePayload *pX, /* Content of the row to be inserted */ 8182 int flags, /* True if this is likely an append */ 8183 int seekResult /* Result of prior MovetoUnpacked() call */ 8184 ){ 8185 int rc; 8186 int loc = seekResult; /* -1: before desired location +1: after */ 8187 int szNew = 0; 8188 int idx; 8189 MemPage *pPage; 8190 Btree *p = pCur->pBtree; 8191 BtShared *pBt = p->pBt; 8192 unsigned char *oldCell; 8193 unsigned char *newCell = 0; 8194 8195 assert( (flags & (BTREE_SAVEPOSITION|BTREE_APPEND))==flags ); 8196 8197 if( pCur->eState==CURSOR_FAULT ){ 8198 assert( pCur->skipNext!=SQLITE_OK ); 8199 return pCur->skipNext; 8200 } 8201 8202 assert( cursorOwnsBtShared(pCur) ); 8203 assert( (pCur->curFlags & BTCF_WriteFlag)!=0 8204 && pBt->inTransaction==TRANS_WRITE 8205 && (pBt->btsFlags & BTS_READ_ONLY)==0 ); 8206 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) ); 8207 8208 /* Assert that the caller has been consistent. If this cursor was opened 8209 ** expecting an index b-tree, then the caller should be inserting blob 8210 ** keys with no associated data. If the cursor was opened expecting an 8211 ** intkey table, the caller should be inserting integer keys with a 8212 ** blob of associated data. */ 8213 assert( (pX->pKey==0)==(pCur->pKeyInfo==0) ); 8214 8215 /* Save the positions of any other cursors open on this table. 8216 ** 8217 ** In some cases, the call to btreeMoveto() below is a no-op. For 8218 ** example, when inserting data into a table with auto-generated integer 8219 ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the 8220 ** integer key to use. It then calls this function to actually insert the 8221 ** data into the intkey B-Tree. In this case btreeMoveto() recognizes 8222 ** that the cursor is already where it needs to be and returns without 8223 ** doing any work. To avoid thwarting these optimizations, it is important 8224 ** not to clear the cursor here. 8225 */ 8226 if( pCur->curFlags & BTCF_Multiple ){ 8227 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur); 8228 if( rc ) return rc; 8229 } 8230 8231 if( pCur->pKeyInfo==0 ){ 8232 assert( pX->pKey==0 ); 8233 /* If this is an insert into a table b-tree, invalidate any incrblob 8234 ** cursors open on the row being replaced */ 8235 invalidateIncrblobCursors(p, pCur->pgnoRoot, pX->nKey, 0); 8236 8237 /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing 8238 ** to a row with the same key as the new entry being inserted. */ 8239 assert( (flags & BTREE_SAVEPOSITION)==0 || 8240 ((pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey) ); 8241 8242 /* If the cursor is currently on the last row and we are appending a 8243 ** new row onto the end, set the "loc" to avoid an unnecessary 8244 ** btreeMoveto() call */ 8245 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){ 8246 loc = 0; 8247 }else if( loc==0 ){ 8248 rc = sqlite3BtreeMovetoUnpacked(pCur, 0, pX->nKey, flags!=0, &loc); 8249 if( rc ) return rc; 8250 } 8251 }else if( loc==0 && (flags & BTREE_SAVEPOSITION)==0 ){ 8252 if( pX->nMem ){ 8253 UnpackedRecord r; 8254 r.pKeyInfo = pCur->pKeyInfo; 8255 r.aMem = pX->aMem; 8256 r.nField = pX->nMem; 8257 r.default_rc = 0; 8258 r.errCode = 0; 8259 r.r1 = 0; 8260 r.r2 = 0; 8261 r.eqSeen = 0; 8262 rc = sqlite3BtreeMovetoUnpacked(pCur, &r, 0, flags!=0, &loc); 8263 }else{ 8264 rc = btreeMoveto(pCur, pX->pKey, pX->nKey, flags!=0, &loc); 8265 } 8266 if( rc ) return rc; 8267 } 8268 assert( pCur->eState==CURSOR_VALID || (pCur->eState==CURSOR_INVALID && loc) ); 8269 8270 pPage = pCur->pPage; 8271 assert( pPage->intKey || pX->nKey>=0 ); 8272 assert( pPage->leaf || !pPage->intKey ); 8273 8274 TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n", 8275 pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno, 8276 loc==0 ? "overwrite" : "new entry")); 8277 assert( pPage->isInit ); 8278 newCell = pBt->pTmpSpace; 8279 assert( newCell!=0 ); 8280 rc = fillInCell(pPage, newCell, pX, &szNew); 8281 if( rc ) goto end_insert; 8282 assert( szNew==pPage->xCellSize(pPage, newCell) ); 8283 assert( szNew <= MX_CELL_SIZE(pBt) ); 8284 idx = pCur->ix; 8285 if( loc==0 ){ 8286 CellInfo info; 8287 assert( idx<pPage->nCell ); 8288 rc = sqlite3PagerWrite(pPage->pDbPage); 8289 if( rc ){ 8290 goto end_insert; 8291 } 8292 oldCell = findCell(pPage, idx); 8293 if( !pPage->leaf ){ 8294 memcpy(newCell, oldCell, 4); 8295 } 8296 rc = clearCell(pPage, oldCell, &info); 8297 if( info.nSize==szNew && info.nLocal==info.nPayload 8298 && (!ISAUTOVACUUM || szNew<pPage->minLocal) 8299 ){ 8300 /* Overwrite the old cell with the new if they are the same size. 8301 ** We could also try to do this if the old cell is smaller, then add 8302 ** the leftover space to the free list. But experiments show that 8303 ** doing that is no faster then skipping this optimization and just 8304 ** calling dropCell() and insertCell(). 8305 ** 8306 ** This optimization cannot be used on an autovacuum database if the 8307 ** new entry uses overflow pages, as the insertCell() call below is 8308 ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry. */ 8309 assert( rc==SQLITE_OK ); /* clearCell never fails when nLocal==nPayload */ 8310 if( oldCell+szNew > pPage->aDataEnd ) return SQLITE_CORRUPT_BKPT; 8311 memcpy(oldCell, newCell, szNew); 8312 return SQLITE_OK; 8313 } 8314 dropCell(pPage, idx, info.nSize, &rc); 8315 if( rc ) goto end_insert; 8316 }else if( loc<0 && pPage->nCell>0 ){ 8317 assert( pPage->leaf ); 8318 idx = ++pCur->ix; 8319 pCur->curFlags &= ~BTCF_ValidNKey; 8320 }else{ 8321 assert( pPage->leaf ); 8322 } 8323 insertCell(pPage, idx, newCell, szNew, 0, 0, &rc); 8324 assert( pPage->nOverflow==0 || rc==SQLITE_OK ); 8325 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 ); 8326 8327 /* If no error has occurred and pPage has an overflow cell, call balance() 8328 ** to redistribute the cells within the tree. Since balance() may move 8329 ** the cursor, zero the BtCursor.info.nSize and BTCF_ValidNKey 8330 ** variables. 8331 ** 8332 ** Previous versions of SQLite called moveToRoot() to move the cursor 8333 ** back to the root page as balance() used to invalidate the contents 8334 ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that, 8335 ** set the cursor state to "invalid". This makes common insert operations 8336 ** slightly faster. 8337 ** 8338 ** There is a subtle but important optimization here too. When inserting 8339 ** multiple records into an intkey b-tree using a single cursor (as can 8340 ** happen while processing an "INSERT INTO ... SELECT" statement), it 8341 ** is advantageous to leave the cursor pointing to the last entry in 8342 ** the b-tree if possible. If the cursor is left pointing to the last 8343 ** entry in the table, and the next row inserted has an integer key 8344 ** larger than the largest existing key, it is possible to insert the 8345 ** row without seeking the cursor. This can be a big performance boost. 8346 */ 8347 pCur->info.nSize = 0; 8348 if( pPage->nOverflow ){ 8349 assert( rc==SQLITE_OK ); 8350 pCur->curFlags &= ~(BTCF_ValidNKey); 8351 rc = balance(pCur); 8352 8353 /* Must make sure nOverflow is reset to zero even if the balance() 8354 ** fails. Internal data structure corruption will result otherwise. 8355 ** Also, set the cursor state to invalid. This stops saveCursorPosition() 8356 ** from trying to save the current position of the cursor. */ 8357 pCur->pPage->nOverflow = 0; 8358 pCur->eState = CURSOR_INVALID; 8359 if( (flags & BTREE_SAVEPOSITION) && rc==SQLITE_OK ){ 8360 btreeReleaseAllCursorPages(pCur); 8361 if( pCur->pKeyInfo ){ 8362 assert( pCur->pKey==0 ); 8363 pCur->pKey = sqlite3Malloc( pX->nKey ); 8364 if( pCur->pKey==0 ){ 8365 rc = SQLITE_NOMEM; 8366 }else{ 8367 memcpy(pCur->pKey, pX->pKey, pX->nKey); 8368 } 8369 } 8370 pCur->eState = CURSOR_REQUIRESEEK; 8371 pCur->nKey = pX->nKey; 8372 } 8373 } 8374 assert( pCur->iPage<0 || pCur->pPage->nOverflow==0 ); 8375 8376 end_insert: 8377 return rc; 8378 } 8379 8380 /* 8381 ** Delete the entry that the cursor is pointing to. 8382 ** 8383 ** If the BTREE_SAVEPOSITION bit of the flags parameter is zero, then 8384 ** the cursor is left pointing at an arbitrary location after the delete. 8385 ** But if that bit is set, then the cursor is left in a state such that 8386 ** the next call to BtreeNext() or BtreePrev() moves it to the same row 8387 ** as it would have been on if the call to BtreeDelete() had been omitted. 8388 ** 8389 ** The BTREE_AUXDELETE bit of flags indicates that is one of several deletes 8390 ** associated with a single table entry and its indexes. Only one of those 8391 ** deletes is considered the "primary" delete. The primary delete occurs 8392 ** on a cursor that is not a BTREE_FORDELETE cursor. All but one delete 8393 ** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag. 8394 ** The BTREE_AUXDELETE bit is a hint that is not used by this implementation, 8395 ** but which might be used by alternative storage engines. 8396 */ 8397 int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){ 8398 Btree *p = pCur->pBtree; 8399 BtShared *pBt = p->pBt; 8400 int rc; /* Return code */ 8401 MemPage *pPage; /* Page to delete cell from */ 8402 unsigned char *pCell; /* Pointer to cell to delete */ 8403 int iCellIdx; /* Index of cell to delete */ 8404 int iCellDepth; /* Depth of node containing pCell */ 8405 CellInfo info; /* Size of the cell being deleted */ 8406 int bSkipnext = 0; /* Leaf cursor in SKIPNEXT state */ 8407 u8 bPreserve = flags & BTREE_SAVEPOSITION; /* Keep cursor valid */ 8408 8409 assert( cursorOwnsBtShared(pCur) ); 8410 assert( pBt->inTransaction==TRANS_WRITE ); 8411 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 ); 8412 assert( pCur->curFlags & BTCF_WriteFlag ); 8413 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) ); 8414 assert( !hasReadConflicts(p, pCur->pgnoRoot) ); 8415 assert( pCur->ix<pCur->pPage->nCell ); 8416 assert( pCur->eState==CURSOR_VALID ); 8417 assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 ); 8418 8419 iCellDepth = pCur->iPage; 8420 iCellIdx = pCur->ix; 8421 pPage = pCur->pPage; 8422 pCell = findCell(pPage, iCellIdx); 8423 8424 /* If the bPreserve flag is set to true, then the cursor position must 8425 ** be preserved following this delete operation. If the current delete 8426 ** will cause a b-tree rebalance, then this is done by saving the cursor 8427 ** key and leaving the cursor in CURSOR_REQUIRESEEK state before 8428 ** returning. 8429 ** 8430 ** Or, if the current delete will not cause a rebalance, then the cursor 8431 ** will be left in CURSOR_SKIPNEXT state pointing to the entry immediately 8432 ** before or after the deleted entry. In this case set bSkipnext to true. */ 8433 if( bPreserve ){ 8434 if( !pPage->leaf 8435 || (pPage->nFree+cellSizePtr(pPage,pCell)+2)>(int)(pBt->usableSize*2/3) 8436 ){ 8437 /* A b-tree rebalance will be required after deleting this entry. 8438 ** Save the cursor key. */ 8439 rc = saveCursorKey(pCur); 8440 if( rc ) return rc; 8441 }else{ 8442 bSkipnext = 1; 8443 } 8444 } 8445 8446 /* If the page containing the entry to delete is not a leaf page, move 8447 ** the cursor to the largest entry in the tree that is smaller than 8448 ** the entry being deleted. This cell will replace the cell being deleted 8449 ** from the internal node. The 'previous' entry is used for this instead 8450 ** of the 'next' entry, as the previous entry is always a part of the 8451 ** sub-tree headed by the child page of the cell being deleted. This makes 8452 ** balancing the tree following the delete operation easier. */ 8453 if( !pPage->leaf ){ 8454 rc = sqlite3BtreePrevious(pCur, 0); 8455 assert( rc!=SQLITE_DONE ); 8456 if( rc ) return rc; 8457 } 8458 8459 /* Save the positions of any other cursors open on this table before 8460 ** making any modifications. */ 8461 if( pCur->curFlags & BTCF_Multiple ){ 8462 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur); 8463 if( rc ) return rc; 8464 } 8465 8466 /* If this is a delete operation to remove a row from a table b-tree, 8467 ** invalidate any incrblob cursors open on the row being deleted. */ 8468 if( pCur->pKeyInfo==0 ){ 8469 invalidateIncrblobCursors(p, pCur->pgnoRoot, pCur->info.nKey, 0); 8470 } 8471 8472 /* Make the page containing the entry to be deleted writable. Then free any 8473 ** overflow pages associated with the entry and finally remove the cell 8474 ** itself from within the page. */ 8475 rc = sqlite3PagerWrite(pPage->pDbPage); 8476 if( rc ) return rc; 8477 rc = clearCell(pPage, pCell, &info); 8478 dropCell(pPage, iCellIdx, info.nSize, &rc); 8479 if( rc ) return rc; 8480 8481 /* If the cell deleted was not located on a leaf page, then the cursor 8482 ** is currently pointing to the largest entry in the sub-tree headed 8483 ** by the child-page of the cell that was just deleted from an internal 8484 ** node. The cell from the leaf node needs to be moved to the internal 8485 ** node to replace the deleted cell. */ 8486 if( !pPage->leaf ){ 8487 MemPage *pLeaf = pCur->pPage; 8488 int nCell; 8489 Pgno n; 8490 unsigned char *pTmp; 8491 8492 if( iCellDepth<pCur->iPage-1 ){ 8493 n = pCur->apPage[iCellDepth+1]->pgno; 8494 }else{ 8495 n = pCur->pPage->pgno; 8496 } 8497 pCell = findCell(pLeaf, pLeaf->nCell-1); 8498 if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT; 8499 nCell = pLeaf->xCellSize(pLeaf, pCell); 8500 assert( MX_CELL_SIZE(pBt) >= nCell ); 8501 pTmp = pBt->pTmpSpace; 8502 assert( pTmp!=0 ); 8503 rc = sqlite3PagerWrite(pLeaf->pDbPage); 8504 if( rc==SQLITE_OK ){ 8505 insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc); 8506 } 8507 dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc); 8508 if( rc ) return rc; 8509 } 8510 8511 /* Balance the tree. If the entry deleted was located on a leaf page, 8512 ** then the cursor still points to that page. In this case the first 8513 ** call to balance() repairs the tree, and the if(...) condition is 8514 ** never true. 8515 ** 8516 ** Otherwise, if the entry deleted was on an internal node page, then 8517 ** pCur is pointing to the leaf page from which a cell was removed to 8518 ** replace the cell deleted from the internal node. This is slightly 8519 ** tricky as the leaf node may be underfull, and the internal node may 8520 ** be either under or overfull. In this case run the balancing algorithm 8521 ** on the leaf node first. If the balance proceeds far enough up the 8522 ** tree that we can be sure that any problem in the internal node has 8523 ** been corrected, so be it. Otherwise, after balancing the leaf node, 8524 ** walk the cursor up the tree to the internal node and balance it as 8525 ** well. */ 8526 rc = balance(pCur); 8527 if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){ 8528 releasePageNotNull(pCur->pPage); 8529 pCur->iPage--; 8530 while( pCur->iPage>iCellDepth ){ 8531 releasePage(pCur->apPage[pCur->iPage--]); 8532 } 8533 pCur->pPage = pCur->apPage[pCur->iPage]; 8534 rc = balance(pCur); 8535 } 8536 8537 if( rc==SQLITE_OK ){ 8538 if( bSkipnext ){ 8539 assert( bPreserve && (pCur->iPage==iCellDepth || CORRUPT_DB) ); 8540 assert( pPage==pCur->pPage || CORRUPT_DB ); 8541 assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell ); 8542 pCur->eState = CURSOR_SKIPNEXT; 8543 if( iCellIdx>=pPage->nCell ){ 8544 pCur->skipNext = -1; 8545 pCur->ix = pPage->nCell-1; 8546 }else{ 8547 pCur->skipNext = 1; 8548 } 8549 }else{ 8550 rc = moveToRoot(pCur); 8551 if( bPreserve ){ 8552 btreeReleaseAllCursorPages(pCur); 8553 pCur->eState = CURSOR_REQUIRESEEK; 8554 } 8555 if( rc==SQLITE_EMPTY ) rc = SQLITE_OK; 8556 } 8557 } 8558 return rc; 8559 } 8560 8561 /* 8562 ** Create a new BTree table. Write into *piTable the page 8563 ** number for the root page of the new table. 8564 ** 8565 ** The type of type is determined by the flags parameter. Only the 8566 ** following values of flags are currently in use. Other values for 8567 ** flags might not work: 8568 ** 8569 ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys 8570 ** BTREE_ZERODATA Used for SQL indices 8571 */ 8572 static int btreeCreateTable(Btree *p, int *piTable, int createTabFlags){ 8573 BtShared *pBt = p->pBt; 8574 MemPage *pRoot; 8575 Pgno pgnoRoot; 8576 int rc; 8577 int ptfFlags; /* Page-type flage for the root page of new table */ 8578 8579 assert( sqlite3BtreeHoldsMutex(p) ); 8580 assert( pBt->inTransaction==TRANS_WRITE ); 8581 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 ); 8582 8583 #ifdef SQLITE_OMIT_AUTOVACUUM 8584 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); 8585 if( rc ){ 8586 return rc; 8587 } 8588 #else 8589 if( pBt->autoVacuum ){ 8590 Pgno pgnoMove; /* Move a page here to make room for the root-page */ 8591 MemPage *pPageMove; /* The page to move to. */ 8592 8593 /* Creating a new table may probably require moving an existing database 8594 ** to make room for the new tables root page. In case this page turns 8595 ** out to be an overflow page, delete all overflow page-map caches 8596 ** held by open cursors. 8597 */ 8598 invalidateAllOverflowCache(pBt); 8599 8600 /* Read the value of meta[3] from the database to determine where the 8601 ** root page of the new table should go. meta[3] is the largest root-page 8602 ** created so far, so the new root-page is (meta[3]+1). 8603 */ 8604 sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &pgnoRoot); 8605 pgnoRoot++; 8606 8607 /* The new root-page may not be allocated on a pointer-map page, or the 8608 ** PENDING_BYTE page. 8609 */ 8610 while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) || 8611 pgnoRoot==PENDING_BYTE_PAGE(pBt) ){ 8612 pgnoRoot++; 8613 } 8614 assert( pgnoRoot>=3 || CORRUPT_DB ); 8615 testcase( pgnoRoot<3 ); 8616 8617 /* Allocate a page. The page that currently resides at pgnoRoot will 8618 ** be moved to the allocated page (unless the allocated page happens 8619 ** to reside at pgnoRoot). 8620 */ 8621 rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, BTALLOC_EXACT); 8622 if( rc!=SQLITE_OK ){ 8623 return rc; 8624 } 8625 8626 if( pgnoMove!=pgnoRoot ){ 8627 /* pgnoRoot is the page that will be used for the root-page of 8628 ** the new table (assuming an error did not occur). But we were 8629 ** allocated pgnoMove. If required (i.e. if it was not allocated 8630 ** by extending the file), the current page at position pgnoMove 8631 ** is already journaled. 8632 */ 8633 u8 eType = 0; 8634 Pgno iPtrPage = 0; 8635 8636 /* Save the positions of any open cursors. This is required in 8637 ** case they are holding a reference to an xFetch reference 8638 ** corresponding to page pgnoRoot. */ 8639 rc = saveAllCursors(pBt, 0, 0); 8640 releasePage(pPageMove); 8641 if( rc!=SQLITE_OK ){ 8642 return rc; 8643 } 8644 8645 /* Move the page currently at pgnoRoot to pgnoMove. */ 8646 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0); 8647 if( rc!=SQLITE_OK ){ 8648 return rc; 8649 } 8650 rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage); 8651 if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){ 8652 rc = SQLITE_CORRUPT_BKPT; 8653 } 8654 if( rc!=SQLITE_OK ){ 8655 releasePage(pRoot); 8656 return rc; 8657 } 8658 assert( eType!=PTRMAP_ROOTPAGE ); 8659 assert( eType!=PTRMAP_FREEPAGE ); 8660 rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0); 8661 releasePage(pRoot); 8662 8663 /* Obtain the page at pgnoRoot */ 8664 if( rc!=SQLITE_OK ){ 8665 return rc; 8666 } 8667 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0); 8668 if( rc!=SQLITE_OK ){ 8669 return rc; 8670 } 8671 rc = sqlite3PagerWrite(pRoot->pDbPage); 8672 if( rc!=SQLITE_OK ){ 8673 releasePage(pRoot); 8674 return rc; 8675 } 8676 }else{ 8677 pRoot = pPageMove; 8678 } 8679 8680 /* Update the pointer-map and meta-data with the new root-page number. */ 8681 ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0, &rc); 8682 if( rc ){ 8683 releasePage(pRoot); 8684 return rc; 8685 } 8686 8687 /* When the new root page was allocated, page 1 was made writable in 8688 ** order either to increase the database filesize, or to decrement the 8689 ** freelist count. Hence, the sqlite3BtreeUpdateMeta() call cannot fail. 8690 */ 8691 assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) ); 8692 rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot); 8693 if( NEVER(rc) ){ 8694 releasePage(pRoot); 8695 return rc; 8696 } 8697 8698 }else{ 8699 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); 8700 if( rc ) return rc; 8701 } 8702 #endif 8703 assert( sqlite3PagerIswriteable(pRoot->pDbPage) ); 8704 if( createTabFlags & BTREE_INTKEY ){ 8705 ptfFlags = PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF; 8706 }else{ 8707 ptfFlags = PTF_ZERODATA | PTF_LEAF; 8708 } 8709 zeroPage(pRoot, ptfFlags); 8710 sqlite3PagerUnref(pRoot->pDbPage); 8711 assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 ); 8712 *piTable = (int)pgnoRoot; 8713 return SQLITE_OK; 8714 } 8715 int sqlite3BtreeCreateTable(Btree *p, int *piTable, int flags){ 8716 int rc; 8717 sqlite3BtreeEnter(p); 8718 rc = btreeCreateTable(p, piTable, flags); 8719 sqlite3BtreeLeave(p); 8720 return rc; 8721 } 8722 8723 /* 8724 ** Erase the given database page and all its children. Return 8725 ** the page to the freelist. 8726 */ 8727 static int clearDatabasePage( 8728 BtShared *pBt, /* The BTree that contains the table */ 8729 Pgno pgno, /* Page number to clear */ 8730 int freePageFlag, /* Deallocate page if true */ 8731 int *pnChange /* Add number of Cells freed to this counter */ 8732 ){ 8733 MemPage *pPage; 8734 int rc; 8735 unsigned char *pCell; 8736 int i; 8737 int hdr; 8738 CellInfo info; 8739 8740 assert( sqlite3_mutex_held(pBt->mutex) ); 8741 if( pgno>btreePagecount(pBt) ){ 8742 return SQLITE_CORRUPT_BKPT; 8743 } 8744 rc = getAndInitPage(pBt, pgno, &pPage, 0, 0); 8745 if( rc ) return rc; 8746 if( pPage->bBusy ){ 8747 rc = SQLITE_CORRUPT_BKPT; 8748 goto cleardatabasepage_out; 8749 } 8750 pPage->bBusy = 1; 8751 hdr = pPage->hdrOffset; 8752 for(i=0; i<pPage->nCell; i++){ 8753 pCell = findCell(pPage, i); 8754 if( !pPage->leaf ){ 8755 rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange); 8756 if( rc ) goto cleardatabasepage_out; 8757 } 8758 rc = clearCell(pPage, pCell, &info); 8759 if( rc ) goto cleardatabasepage_out; 8760 } 8761 if( !pPage->leaf ){ 8762 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange); 8763 if( rc ) goto cleardatabasepage_out; 8764 }else if( pnChange ){ 8765 assert( pPage->intKey || CORRUPT_DB ); 8766 testcase( !pPage->intKey ); 8767 *pnChange += pPage->nCell; 8768 } 8769 if( freePageFlag ){ 8770 freePage(pPage, &rc); 8771 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){ 8772 zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF); 8773 } 8774 8775 cleardatabasepage_out: 8776 pPage->bBusy = 0; 8777 releasePage(pPage); 8778 return rc; 8779 } 8780 8781 /* 8782 ** Delete all information from a single table in the database. iTable is 8783 ** the page number of the root of the table. After this routine returns, 8784 ** the root page is empty, but still exists. 8785 ** 8786 ** This routine will fail with SQLITE_LOCKED if there are any open 8787 ** read cursors on the table. Open write cursors are moved to the 8788 ** root of the table. 8789 ** 8790 ** If pnChange is not NULL, then table iTable must be an intkey table. The 8791 ** integer value pointed to by pnChange is incremented by the number of 8792 ** entries in the table. 8793 */ 8794 int sqlite3BtreeClearTable(Btree *p, int iTable, int *pnChange){ 8795 int rc; 8796 BtShared *pBt = p->pBt; 8797 sqlite3BtreeEnter(p); 8798 assert( p->inTrans==TRANS_WRITE ); 8799 8800 rc = saveAllCursors(pBt, (Pgno)iTable, 0); 8801 8802 if( SQLITE_OK==rc ){ 8803 /* Invalidate all incrblob cursors open on table iTable (assuming iTable 8804 ** is the root of a table b-tree - if it is not, the following call is 8805 ** a no-op). */ 8806 invalidateIncrblobCursors(p, (Pgno)iTable, 0, 1); 8807 rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange); 8808 } 8809 sqlite3BtreeLeave(p); 8810 return rc; 8811 } 8812 8813 /* 8814 ** Delete all information from the single table that pCur is open on. 8815 ** 8816 ** This routine only work for pCur on an ephemeral table. 8817 */ 8818 int sqlite3BtreeClearTableOfCursor(BtCursor *pCur){ 8819 return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0); 8820 } 8821 8822 /* 8823 ** Erase all information in a table and add the root of the table to 8824 ** the freelist. Except, the root of the principle table (the one on 8825 ** page 1) is never added to the freelist. 8826 ** 8827 ** This routine will fail with SQLITE_LOCKED if there are any open 8828 ** cursors on the table. 8829 ** 8830 ** If AUTOVACUUM is enabled and the page at iTable is not the last 8831 ** root page in the database file, then the last root page 8832 ** in the database file is moved into the slot formerly occupied by 8833 ** iTable and that last slot formerly occupied by the last root page 8834 ** is added to the freelist instead of iTable. In this say, all 8835 ** root pages are kept at the beginning of the database file, which 8836 ** is necessary for AUTOVACUUM to work right. *piMoved is set to the 8837 ** page number that used to be the last root page in the file before 8838 ** the move. If no page gets moved, *piMoved is set to 0. 8839 ** The last root page is recorded in meta[3] and the value of 8840 ** meta[3] is updated by this procedure. 8841 */ 8842 static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){ 8843 int rc; 8844 MemPage *pPage = 0; 8845 BtShared *pBt = p->pBt; 8846 8847 assert( sqlite3BtreeHoldsMutex(p) ); 8848 assert( p->inTrans==TRANS_WRITE ); 8849 assert( iTable>=2 ); 8850 8851 rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0); 8852 if( rc ) return rc; 8853 rc = sqlite3BtreeClearTable(p, iTable, 0); 8854 if( rc ){ 8855 releasePage(pPage); 8856 return rc; 8857 } 8858 8859 *piMoved = 0; 8860 8861 #ifdef SQLITE_OMIT_AUTOVACUUM 8862 freePage(pPage, &rc); 8863 releasePage(pPage); 8864 #else 8865 if( pBt->autoVacuum ){ 8866 Pgno maxRootPgno; 8867 sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &maxRootPgno); 8868 8869 if( iTable==maxRootPgno ){ 8870 /* If the table being dropped is the table with the largest root-page 8871 ** number in the database, put the root page on the free list. 8872 */ 8873 freePage(pPage, &rc); 8874 releasePage(pPage); 8875 if( rc!=SQLITE_OK ){ 8876 return rc; 8877 } 8878 }else{ 8879 /* The table being dropped does not have the largest root-page 8880 ** number in the database. So move the page that does into the 8881 ** gap left by the deleted root-page. 8882 */ 8883 MemPage *pMove; 8884 releasePage(pPage); 8885 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0); 8886 if( rc!=SQLITE_OK ){ 8887 return rc; 8888 } 8889 rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0); 8890 releasePage(pMove); 8891 if( rc!=SQLITE_OK ){ 8892 return rc; 8893 } 8894 pMove = 0; 8895 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0); 8896 freePage(pMove, &rc); 8897 releasePage(pMove); 8898 if( rc!=SQLITE_OK ){ 8899 return rc; 8900 } 8901 *piMoved = maxRootPgno; 8902 } 8903 8904 /* Set the new 'max-root-page' value in the database header. This 8905 ** is the old value less one, less one more if that happens to 8906 ** be a root-page number, less one again if that is the 8907 ** PENDING_BYTE_PAGE. 8908 */ 8909 maxRootPgno--; 8910 while( maxRootPgno==PENDING_BYTE_PAGE(pBt) 8911 || PTRMAP_ISPAGE(pBt, maxRootPgno) ){ 8912 maxRootPgno--; 8913 } 8914 assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) ); 8915 8916 rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno); 8917 }else{ 8918 freePage(pPage, &rc); 8919 releasePage(pPage); 8920 } 8921 #endif 8922 return rc; 8923 } 8924 int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){ 8925 int rc; 8926 sqlite3BtreeEnter(p); 8927 rc = btreeDropTable(p, iTable, piMoved); 8928 sqlite3BtreeLeave(p); 8929 return rc; 8930 } 8931 8932 8933 /* 8934 ** This function may only be called if the b-tree connection already 8935 ** has a read or write transaction open on the database. 8936 ** 8937 ** Read the meta-information out of a database file. Meta[0] 8938 ** is the number of free pages currently in the database. Meta[1] 8939 ** through meta[15] are available for use by higher layers. Meta[0] 8940 ** is read-only, the others are read/write. 8941 ** 8942 ** The schema layer numbers meta values differently. At the schema 8943 ** layer (and the SetCookie and ReadCookie opcodes) the number of 8944 ** free pages is not visible. So Cookie[0] is the same as Meta[1]. 8945 ** 8946 ** This routine treats Meta[BTREE_DATA_VERSION] as a special case. Instead 8947 ** of reading the value out of the header, it instead loads the "DataVersion" 8948 ** from the pager. The BTREE_DATA_VERSION value is not actually stored in the 8949 ** database file. It is a number computed by the pager. But its access 8950 ** pattern is the same as header meta values, and so it is convenient to 8951 ** read it from this routine. 8952 */ 8953 void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){ 8954 BtShared *pBt = p->pBt; 8955 8956 sqlite3BtreeEnter(p); 8957 assert( p->inTrans>TRANS_NONE ); 8958 assert( SQLITE_OK==querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK) ); 8959 assert( pBt->pPage1 ); 8960 assert( idx>=0 && idx<=15 ); 8961 8962 if( idx==BTREE_DATA_VERSION ){ 8963 *pMeta = sqlite3PagerDataVersion(pBt->pPager) + p->iDataVersion; 8964 }else{ 8965 *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]); 8966 } 8967 8968 /* If auto-vacuum is disabled in this build and this is an auto-vacuum 8969 ** database, mark the database as read-only. */ 8970 #ifdef SQLITE_OMIT_AUTOVACUUM 8971 if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ){ 8972 pBt->btsFlags |= BTS_READ_ONLY; 8973 } 8974 #endif 8975 8976 sqlite3BtreeLeave(p); 8977 } 8978 8979 /* 8980 ** Write meta-information back into the database. Meta[0] is 8981 ** read-only and may not be written. 8982 */ 8983 int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){ 8984 BtShared *pBt = p->pBt; 8985 unsigned char *pP1; 8986 int rc; 8987 assert( idx>=1 && idx<=15 ); 8988 sqlite3BtreeEnter(p); 8989 assert( p->inTrans==TRANS_WRITE ); 8990 assert( pBt->pPage1!=0 ); 8991 pP1 = pBt->pPage1->aData; 8992 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); 8993 if( rc==SQLITE_OK ){ 8994 put4byte(&pP1[36 + idx*4], iMeta); 8995 #ifndef SQLITE_OMIT_AUTOVACUUM 8996 if( idx==BTREE_INCR_VACUUM ){ 8997 assert( pBt->autoVacuum || iMeta==0 ); 8998 assert( iMeta==0 || iMeta==1 ); 8999 pBt->incrVacuum = (u8)iMeta; 9000 } 9001 #endif 9002 } 9003 sqlite3BtreeLeave(p); 9004 return rc; 9005 } 9006 9007 #ifndef SQLITE_OMIT_BTREECOUNT 9008 /* 9009 ** The first argument, pCur, is a cursor opened on some b-tree. Count the 9010 ** number of entries in the b-tree and write the result to *pnEntry. 9011 ** 9012 ** SQLITE_OK is returned if the operation is successfully executed. 9013 ** Otherwise, if an error is encountered (i.e. an IO error or database 9014 ** corruption) an SQLite error code is returned. 9015 */ 9016 int sqlite3BtreeCount(BtCursor *pCur, i64 *pnEntry){ 9017 i64 nEntry = 0; /* Value to return in *pnEntry */ 9018 int rc; /* Return code */ 9019 9020 rc = moveToRoot(pCur); 9021 if( rc==SQLITE_EMPTY ){ 9022 *pnEntry = 0; 9023 return SQLITE_OK; 9024 } 9025 9026 /* Unless an error occurs, the following loop runs one iteration for each 9027 ** page in the B-Tree structure (not including overflow pages). 9028 */ 9029 while( rc==SQLITE_OK ){ 9030 int iIdx; /* Index of child node in parent */ 9031 MemPage *pPage; /* Current page of the b-tree */ 9032 9033 /* If this is a leaf page or the tree is not an int-key tree, then 9034 ** this page contains countable entries. Increment the entry counter 9035 ** accordingly. 9036 */ 9037 pPage = pCur->pPage; 9038 if( pPage->leaf || !pPage->intKey ){ 9039 nEntry += pPage->nCell; 9040 } 9041 9042 /* pPage is a leaf node. This loop navigates the cursor so that it 9043 ** points to the first interior cell that it points to the parent of 9044 ** the next page in the tree that has not yet been visited. The 9045 ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell 9046 ** of the page, or to the number of cells in the page if the next page 9047 ** to visit is the right-child of its parent. 9048 ** 9049 ** If all pages in the tree have been visited, return SQLITE_OK to the 9050 ** caller. 9051 */ 9052 if( pPage->leaf ){ 9053 do { 9054 if( pCur->iPage==0 ){ 9055 /* All pages of the b-tree have been visited. Return successfully. */ 9056 *pnEntry = nEntry; 9057 return moveToRoot(pCur); 9058 } 9059 moveToParent(pCur); 9060 }while ( pCur->ix>=pCur->pPage->nCell ); 9061 9062 pCur->ix++; 9063 pPage = pCur->pPage; 9064 } 9065 9066 /* Descend to the child node of the cell that the cursor currently 9067 ** points at. This is the right-child if (iIdx==pPage->nCell). 9068 */ 9069 iIdx = pCur->ix; 9070 if( iIdx==pPage->nCell ){ 9071 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); 9072 }else{ 9073 rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx))); 9074 } 9075 } 9076 9077 /* An error has occurred. Return an error code. */ 9078 return rc; 9079 } 9080 #endif 9081 9082 /* 9083 ** Return the pager associated with a BTree. This routine is used for 9084 ** testing and debugging only. 9085 */ 9086 Pager *sqlite3BtreePager(Btree *p){ 9087 return p->pBt->pPager; 9088 } 9089 9090 #ifndef SQLITE_OMIT_INTEGRITY_CHECK 9091 /* 9092 ** Append a message to the error message string. 9093 */ 9094 static void checkAppendMsg( 9095 IntegrityCk *pCheck, 9096 const char *zFormat, 9097 ... 9098 ){ 9099 va_list ap; 9100 if( !pCheck->mxErr ) return; 9101 pCheck->mxErr--; 9102 pCheck->nErr++; 9103 va_start(ap, zFormat); 9104 if( pCheck->errMsg.nChar ){ 9105 sqlite3StrAccumAppend(&pCheck->errMsg, "\n", 1); 9106 } 9107 if( pCheck->zPfx ){ 9108 sqlite3XPrintf(&pCheck->errMsg, pCheck->zPfx, pCheck->v1, pCheck->v2); 9109 } 9110 sqlite3VXPrintf(&pCheck->errMsg, zFormat, ap); 9111 va_end(ap); 9112 if( pCheck->errMsg.accError==STRACCUM_NOMEM ){ 9113 pCheck->mallocFailed = 1; 9114 } 9115 } 9116 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ 9117 9118 #ifndef SQLITE_OMIT_INTEGRITY_CHECK 9119 9120 /* 9121 ** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that 9122 ** corresponds to page iPg is already set. 9123 */ 9124 static int getPageReferenced(IntegrityCk *pCheck, Pgno iPg){ 9125 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 ); 9126 return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07))); 9127 } 9128 9129 /* 9130 ** Set the bit in the IntegrityCk.aPgRef[] array that corresponds to page iPg. 9131 */ 9132 static void setPageReferenced(IntegrityCk *pCheck, Pgno iPg){ 9133 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 ); 9134 pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07)); 9135 } 9136 9137 9138 /* 9139 ** Add 1 to the reference count for page iPage. If this is the second 9140 ** reference to the page, add an error message to pCheck->zErrMsg. 9141 ** Return 1 if there are 2 or more references to the page and 0 if 9142 ** if this is the first reference to the page. 9143 ** 9144 ** Also check that the page number is in bounds. 9145 */ 9146 static int checkRef(IntegrityCk *pCheck, Pgno iPage){ 9147 if( iPage==0 ) return 1; 9148 if( iPage>pCheck->nPage ){ 9149 checkAppendMsg(pCheck, "invalid page number %d", iPage); 9150 return 1; 9151 } 9152 if( getPageReferenced(pCheck, iPage) ){ 9153 checkAppendMsg(pCheck, "2nd reference to page %d", iPage); 9154 return 1; 9155 } 9156 setPageReferenced(pCheck, iPage); 9157 return 0; 9158 } 9159 9160 #ifndef SQLITE_OMIT_AUTOVACUUM 9161 /* 9162 ** Check that the entry in the pointer-map for page iChild maps to 9163 ** page iParent, pointer type ptrType. If not, append an error message 9164 ** to pCheck. 9165 */ 9166 static void checkPtrmap( 9167 IntegrityCk *pCheck, /* Integrity check context */ 9168 Pgno iChild, /* Child page number */ 9169 u8 eType, /* Expected pointer map type */ 9170 Pgno iParent /* Expected pointer map parent page number */ 9171 ){ 9172 int rc; 9173 u8 ePtrmapType; 9174 Pgno iPtrmapParent; 9175 9176 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent); 9177 if( rc!=SQLITE_OK ){ 9178 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->mallocFailed = 1; 9179 checkAppendMsg(pCheck, "Failed to read ptrmap key=%d", iChild); 9180 return; 9181 } 9182 9183 if( ePtrmapType!=eType || iPtrmapParent!=iParent ){ 9184 checkAppendMsg(pCheck, 9185 "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", 9186 iChild, eType, iParent, ePtrmapType, iPtrmapParent); 9187 } 9188 } 9189 #endif 9190 9191 /* 9192 ** Check the integrity of the freelist or of an overflow page list. 9193 ** Verify that the number of pages on the list is N. 9194 */ 9195 static void checkList( 9196 IntegrityCk *pCheck, /* Integrity checking context */ 9197 int isFreeList, /* True for a freelist. False for overflow page list */ 9198 int iPage, /* Page number for first page in the list */ 9199 int N /* Expected number of pages in the list */ 9200 ){ 9201 int i; 9202 int expected = N; 9203 int iFirst = iPage; 9204 while( N-- > 0 && pCheck->mxErr ){ 9205 DbPage *pOvflPage; 9206 unsigned char *pOvflData; 9207 if( iPage<1 ){ 9208 checkAppendMsg(pCheck, 9209 "%d of %d pages missing from overflow list starting at %d", 9210 N+1, expected, iFirst); 9211 break; 9212 } 9213 if( checkRef(pCheck, iPage) ) break; 9214 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){ 9215 checkAppendMsg(pCheck, "failed to get page %d", iPage); 9216 break; 9217 } 9218 pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage); 9219 if( isFreeList ){ 9220 int n = get4byte(&pOvflData[4]); 9221 #ifndef SQLITE_OMIT_AUTOVACUUM 9222 if( pCheck->pBt->autoVacuum ){ 9223 checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0); 9224 } 9225 #endif 9226 if( n>(int)pCheck->pBt->usableSize/4-2 ){ 9227 checkAppendMsg(pCheck, 9228 "freelist leaf count too big on page %d", iPage); 9229 N--; 9230 }else{ 9231 for(i=0; i<n; i++){ 9232 Pgno iFreePage = get4byte(&pOvflData[8+i*4]); 9233 #ifndef SQLITE_OMIT_AUTOVACUUM 9234 if( pCheck->pBt->autoVacuum ){ 9235 checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0); 9236 } 9237 #endif 9238 checkRef(pCheck, iFreePage); 9239 } 9240 N -= n; 9241 } 9242 } 9243 #ifndef SQLITE_OMIT_AUTOVACUUM 9244 else{ 9245 /* If this database supports auto-vacuum and iPage is not the last 9246 ** page in this overflow list, check that the pointer-map entry for 9247 ** the following page matches iPage. 9248 */ 9249 if( pCheck->pBt->autoVacuum && N>0 ){ 9250 i = get4byte(pOvflData); 9251 checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage); 9252 } 9253 } 9254 #endif 9255 iPage = get4byte(pOvflData); 9256 sqlite3PagerUnref(pOvflPage); 9257 9258 if( isFreeList && N<(iPage!=0) ){ 9259 checkAppendMsg(pCheck, "free-page count in header is too small"); 9260 } 9261 } 9262 } 9263 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ 9264 9265 /* 9266 ** An implementation of a min-heap. 9267 ** 9268 ** aHeap[0] is the number of elements on the heap. aHeap[1] is the 9269 ** root element. The daughter nodes of aHeap[N] are aHeap[N*2] 9270 ** and aHeap[N*2+1]. 9271 ** 9272 ** The heap property is this: Every node is less than or equal to both 9273 ** of its daughter nodes. A consequence of the heap property is that the 9274 ** root node aHeap[1] is always the minimum value currently in the heap. 9275 ** 9276 ** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto 9277 ** the heap, preserving the heap property. The btreeHeapPull() routine 9278 ** removes the root element from the heap (the minimum value in the heap) 9279 ** and then moves other nodes around as necessary to preserve the heap 9280 ** property. 9281 ** 9282 ** This heap is used for cell overlap and coverage testing. Each u32 9283 ** entry represents the span of a cell or freeblock on a btree page. 9284 ** The upper 16 bits are the index of the first byte of a range and the 9285 ** lower 16 bits are the index of the last byte of that range. 9286 */ 9287 static void btreeHeapInsert(u32 *aHeap, u32 x){ 9288 u32 j, i = ++aHeap[0]; 9289 aHeap[i] = x; 9290 while( (j = i/2)>0 && aHeap[j]>aHeap[i] ){ 9291 x = aHeap[j]; 9292 aHeap[j] = aHeap[i]; 9293 aHeap[i] = x; 9294 i = j; 9295 } 9296 } 9297 static int btreeHeapPull(u32 *aHeap, u32 *pOut){ 9298 u32 j, i, x; 9299 if( (x = aHeap[0])==0 ) return 0; 9300 *pOut = aHeap[1]; 9301 aHeap[1] = aHeap[x]; 9302 aHeap[x] = 0xffffffff; 9303 aHeap[0]--; 9304 i = 1; 9305 while( (j = i*2)<=aHeap[0] ){ 9306 if( aHeap[j]>aHeap[j+1] ) j++; 9307 if( aHeap[i]<aHeap[j] ) break; 9308 x = aHeap[i]; 9309 aHeap[i] = aHeap[j]; 9310 aHeap[j] = x; 9311 i = j; 9312 } 9313 return 1; 9314 } 9315 9316 #ifndef SQLITE_OMIT_INTEGRITY_CHECK 9317 /* 9318 ** Do various sanity checks on a single page of a tree. Return 9319 ** the tree depth. Root pages return 0. Parents of root pages 9320 ** return 1, and so forth. 9321 ** 9322 ** These checks are done: 9323 ** 9324 ** 1. Make sure that cells and freeblocks do not overlap 9325 ** but combine to completely cover the page. 9326 ** 2. Make sure integer cell keys are in order. 9327 ** 3. Check the integrity of overflow pages. 9328 ** 4. Recursively call checkTreePage on all children. 9329 ** 5. Verify that the depth of all children is the same. 9330 */ 9331 static int checkTreePage( 9332 IntegrityCk *pCheck, /* Context for the sanity check */ 9333 int iPage, /* Page number of the page to check */ 9334 i64 *piMinKey, /* Write minimum integer primary key here */ 9335 i64 maxKey /* Error if integer primary key greater than this */ 9336 ){ 9337 MemPage *pPage = 0; /* The page being analyzed */ 9338 int i; /* Loop counter */ 9339 int rc; /* Result code from subroutine call */ 9340 int depth = -1, d2; /* Depth of a subtree */ 9341 int pgno; /* Page number */ 9342 int nFrag; /* Number of fragmented bytes on the page */ 9343 int hdr; /* Offset to the page header */ 9344 int cellStart; /* Offset to the start of the cell pointer array */ 9345 int nCell; /* Number of cells */ 9346 int doCoverageCheck = 1; /* True if cell coverage checking should be done */ 9347 int keyCanBeEqual = 1; /* True if IPK can be equal to maxKey 9348 ** False if IPK must be strictly less than maxKey */ 9349 u8 *data; /* Page content */ 9350 u8 *pCell; /* Cell content */ 9351 u8 *pCellIdx; /* Next element of the cell pointer array */ 9352 BtShared *pBt; /* The BtShared object that owns pPage */ 9353 u32 pc; /* Address of a cell */ 9354 u32 usableSize; /* Usable size of the page */ 9355 u32 contentOffset; /* Offset to the start of the cell content area */ 9356 u32 *heap = 0; /* Min-heap used for checking cell coverage */ 9357 u32 x, prev = 0; /* Next and previous entry on the min-heap */ 9358 const char *saved_zPfx = pCheck->zPfx; 9359 int saved_v1 = pCheck->v1; 9360 int saved_v2 = pCheck->v2; 9361 u8 savedIsInit = 0; 9362 9363 /* Check that the page exists 9364 */ 9365 pBt = pCheck->pBt; 9366 usableSize = pBt->usableSize; 9367 if( iPage==0 ) return 0; 9368 if( checkRef(pCheck, iPage) ) return 0; 9369 pCheck->zPfx = "Page %d: "; 9370 pCheck->v1 = iPage; 9371 if( (rc = btreeGetPage(pBt, (Pgno)iPage, &pPage, 0))!=0 ){ 9372 checkAppendMsg(pCheck, 9373 "unable to get the page. error code=%d", rc); 9374 goto end_of_check; 9375 } 9376 9377 /* Clear MemPage.isInit to make sure the corruption detection code in 9378 ** btreeInitPage() is executed. */ 9379 savedIsInit = pPage->isInit; 9380 pPage->isInit = 0; 9381 if( (rc = btreeInitPage(pPage))!=0 ){ 9382 assert( rc==SQLITE_CORRUPT ); /* The only possible error from InitPage */ 9383 checkAppendMsg(pCheck, 9384 "btreeInitPage() returns error code %d", rc); 9385 goto end_of_check; 9386 } 9387 data = pPage->aData; 9388 hdr = pPage->hdrOffset; 9389 9390 /* Set up for cell analysis */ 9391 pCheck->zPfx = "On tree page %d cell %d: "; 9392 contentOffset = get2byteNotZero(&data[hdr+5]); 9393 assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */ 9394 9395 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the 9396 ** number of cells on the page. */ 9397 nCell = get2byte(&data[hdr+3]); 9398 assert( pPage->nCell==nCell ); 9399 9400 /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page 9401 ** immediately follows the b-tree page header. */ 9402 cellStart = hdr + 12 - 4*pPage->leaf; 9403 assert( pPage->aCellIdx==&data[cellStart] ); 9404 pCellIdx = &data[cellStart + 2*(nCell-1)]; 9405 9406 if( !pPage->leaf ){ 9407 /* Analyze the right-child page of internal pages */ 9408 pgno = get4byte(&data[hdr+8]); 9409 #ifndef SQLITE_OMIT_AUTOVACUUM 9410 if( pBt->autoVacuum ){ 9411 pCheck->zPfx = "On page %d at right child: "; 9412 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage); 9413 } 9414 #endif 9415 depth = checkTreePage(pCheck, pgno, &maxKey, maxKey); 9416 keyCanBeEqual = 0; 9417 }else{ 9418 /* For leaf pages, the coverage check will occur in the same loop 9419 ** as the other cell checks, so initialize the heap. */ 9420 heap = pCheck->heap; 9421 heap[0] = 0; 9422 } 9423 9424 /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte 9425 ** integer offsets to the cell contents. */ 9426 for(i=nCell-1; i>=0 && pCheck->mxErr; i--){ 9427 CellInfo info; 9428 9429 /* Check cell size */ 9430 pCheck->v2 = i; 9431 assert( pCellIdx==&data[cellStart + i*2] ); 9432 pc = get2byteAligned(pCellIdx); 9433 pCellIdx -= 2; 9434 if( pc<contentOffset || pc>usableSize-4 ){ 9435 checkAppendMsg(pCheck, "Offset %d out of range %d..%d", 9436 pc, contentOffset, usableSize-4); 9437 doCoverageCheck = 0; 9438 continue; 9439 } 9440 pCell = &data[pc]; 9441 pPage->xParseCell(pPage, pCell, &info); 9442 if( pc+info.nSize>usableSize ){ 9443 checkAppendMsg(pCheck, "Extends off end of page"); 9444 doCoverageCheck = 0; 9445 continue; 9446 } 9447 9448 /* Check for integer primary key out of range */ 9449 if( pPage->intKey ){ 9450 if( keyCanBeEqual ? (info.nKey > maxKey) : (info.nKey >= maxKey) ){ 9451 checkAppendMsg(pCheck, "Rowid %lld out of order", info.nKey); 9452 } 9453 maxKey = info.nKey; 9454 keyCanBeEqual = 0; /* Only the first key on the page may ==maxKey */ 9455 } 9456 9457 /* Check the content overflow list */ 9458 if( info.nPayload>info.nLocal ){ 9459 int nPage; /* Number of pages on the overflow chain */ 9460 Pgno pgnoOvfl; /* First page of the overflow chain */ 9461 assert( pc + info.nSize - 4 <= usableSize ); 9462 nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4); 9463 pgnoOvfl = get4byte(&pCell[info.nSize - 4]); 9464 #ifndef SQLITE_OMIT_AUTOVACUUM 9465 if( pBt->autoVacuum ){ 9466 checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage); 9467 } 9468 #endif 9469 checkList(pCheck, 0, pgnoOvfl, nPage); 9470 } 9471 9472 if( !pPage->leaf ){ 9473 /* Check sanity of left child page for internal pages */ 9474 pgno = get4byte(pCell); 9475 #ifndef SQLITE_OMIT_AUTOVACUUM 9476 if( pBt->autoVacuum ){ 9477 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage); 9478 } 9479 #endif 9480 d2 = checkTreePage(pCheck, pgno, &maxKey, maxKey); 9481 keyCanBeEqual = 0; 9482 if( d2!=depth ){ 9483 checkAppendMsg(pCheck, "Child page depth differs"); 9484 depth = d2; 9485 } 9486 }else{ 9487 /* Populate the coverage-checking heap for leaf pages */ 9488 btreeHeapInsert(heap, (pc<<16)|(pc+info.nSize-1)); 9489 } 9490 } 9491 *piMinKey = maxKey; 9492 9493 /* Check for complete coverage of the page 9494 */ 9495 pCheck->zPfx = 0; 9496 if( doCoverageCheck && pCheck->mxErr>0 ){ 9497 /* For leaf pages, the min-heap has already been initialized and the 9498 ** cells have already been inserted. But for internal pages, that has 9499 ** not yet been done, so do it now */ 9500 if( !pPage->leaf ){ 9501 heap = pCheck->heap; 9502 heap[0] = 0; 9503 for(i=nCell-1; i>=0; i--){ 9504 u32 size; 9505 pc = get2byteAligned(&data[cellStart+i*2]); 9506 size = pPage->xCellSize(pPage, &data[pc]); 9507 btreeHeapInsert(heap, (pc<<16)|(pc+size-1)); 9508 } 9509 } 9510 /* Add the freeblocks to the min-heap 9511 ** 9512 ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header 9513 ** is the offset of the first freeblock, or zero if there are no 9514 ** freeblocks on the page. 9515 */ 9516 i = get2byte(&data[hdr+1]); 9517 while( i>0 ){ 9518 int size, j; 9519 assert( (u32)i<=usableSize-4 ); /* Enforced by btreeInitPage() */ 9520 size = get2byte(&data[i+2]); 9521 assert( (u32)(i+size)<=usableSize ); /* Enforced by btreeInitPage() */ 9522 btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1)); 9523 /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a 9524 ** big-endian integer which is the offset in the b-tree page of the next 9525 ** freeblock in the chain, or zero if the freeblock is the last on the 9526 ** chain. */ 9527 j = get2byte(&data[i]); 9528 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of 9529 ** increasing offset. */ 9530 assert( j==0 || j>i+size ); /* Enforced by btreeInitPage() */ 9531 assert( (u32)j<=usableSize-4 ); /* Enforced by btreeInitPage() */ 9532 i = j; 9533 } 9534 /* Analyze the min-heap looking for overlap between cells and/or 9535 ** freeblocks, and counting the number of untracked bytes in nFrag. 9536 ** 9537 ** Each min-heap entry is of the form: (start_address<<16)|end_address. 9538 ** There is an implied first entry the covers the page header, the cell 9539 ** pointer index, and the gap between the cell pointer index and the start 9540 ** of cell content. 9541 ** 9542 ** The loop below pulls entries from the min-heap in order and compares 9543 ** the start_address against the previous end_address. If there is an 9544 ** overlap, that means bytes are used multiple times. If there is a gap, 9545 ** that gap is added to the fragmentation count. 9546 */ 9547 nFrag = 0; 9548 prev = contentOffset - 1; /* Implied first min-heap entry */ 9549 while( btreeHeapPull(heap,&x) ){ 9550 if( (prev&0xffff)>=(x>>16) ){ 9551 checkAppendMsg(pCheck, 9552 "Multiple uses for byte %u of page %d", x>>16, iPage); 9553 break; 9554 }else{ 9555 nFrag += (x>>16) - (prev&0xffff) - 1; 9556 prev = x; 9557 } 9558 } 9559 nFrag += usableSize - (prev&0xffff) - 1; 9560 /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments 9561 ** is stored in the fifth field of the b-tree page header. 9562 ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the 9563 ** number of fragmented free bytes within the cell content area. 9564 */ 9565 if( heap[0]==0 && nFrag!=data[hdr+7] ){ 9566 checkAppendMsg(pCheck, 9567 "Fragmentation of %d bytes reported as %d on page %d", 9568 nFrag, data[hdr+7], iPage); 9569 } 9570 } 9571 9572 end_of_check: 9573 if( !doCoverageCheck ) pPage->isInit = savedIsInit; 9574 releasePage(pPage); 9575 pCheck->zPfx = saved_zPfx; 9576 pCheck->v1 = saved_v1; 9577 pCheck->v2 = saved_v2; 9578 return depth+1; 9579 } 9580 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ 9581 9582 #ifndef SQLITE_OMIT_INTEGRITY_CHECK 9583 /* 9584 ** This routine does a complete check of the given BTree file. aRoot[] is 9585 ** an array of pages numbers were each page number is the root page of 9586 ** a table. nRoot is the number of entries in aRoot. 9587 ** 9588 ** A read-only or read-write transaction must be opened before calling 9589 ** this function. 9590 ** 9591 ** Write the number of error seen in *pnErr. Except for some memory 9592 ** allocation errors, an error message held in memory obtained from 9593 ** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is 9594 ** returned. If a memory allocation error occurs, NULL is returned. 9595 */ 9596 char *sqlite3BtreeIntegrityCheck( 9597 Btree *p, /* The btree to be checked */ 9598 int *aRoot, /* An array of root pages numbers for individual trees */ 9599 int nRoot, /* Number of entries in aRoot[] */ 9600 int mxErr, /* Stop reporting errors after this many */ 9601 int *pnErr /* Write number of errors seen to this variable */ 9602 ){ 9603 Pgno i; 9604 IntegrityCk sCheck; 9605 BtShared *pBt = p->pBt; 9606 int savedDbFlags = pBt->db->flags; 9607 char zErr[100]; 9608 VVA_ONLY( int nRef ); 9609 9610 sqlite3BtreeEnter(p); 9611 assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE ); 9612 VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) ); 9613 assert( nRef>=0 ); 9614 sCheck.pBt = pBt; 9615 sCheck.pPager = pBt->pPager; 9616 sCheck.nPage = btreePagecount(sCheck.pBt); 9617 sCheck.mxErr = mxErr; 9618 sCheck.nErr = 0; 9619 sCheck.mallocFailed = 0; 9620 sCheck.zPfx = 0; 9621 sCheck.v1 = 0; 9622 sCheck.v2 = 0; 9623 sCheck.aPgRef = 0; 9624 sCheck.heap = 0; 9625 sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), SQLITE_MAX_LENGTH); 9626 sCheck.errMsg.printfFlags = SQLITE_PRINTF_INTERNAL; 9627 if( sCheck.nPage==0 ){ 9628 goto integrity_ck_cleanup; 9629 } 9630 9631 sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1); 9632 if( !sCheck.aPgRef ){ 9633 sCheck.mallocFailed = 1; 9634 goto integrity_ck_cleanup; 9635 } 9636 sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize ); 9637 if( sCheck.heap==0 ){ 9638 sCheck.mallocFailed = 1; 9639 goto integrity_ck_cleanup; 9640 } 9641 9642 i = PENDING_BYTE_PAGE(pBt); 9643 if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i); 9644 9645 /* Check the integrity of the freelist 9646 */ 9647 sCheck.zPfx = "Main freelist: "; 9648 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), 9649 get4byte(&pBt->pPage1->aData[36])); 9650 sCheck.zPfx = 0; 9651 9652 /* Check all the tables. 9653 */ 9654 testcase( pBt->db->flags & SQLITE_CellSizeCk ); 9655 pBt->db->flags &= ~SQLITE_CellSizeCk; 9656 for(i=0; (int)i<nRoot && sCheck.mxErr; i++){ 9657 i64 notUsed; 9658 if( aRoot[i]==0 ) continue; 9659 #ifndef SQLITE_OMIT_AUTOVACUUM 9660 if( pBt->autoVacuum && aRoot[i]>1 ){ 9661 checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0); 9662 } 9663 #endif 9664 checkTreePage(&sCheck, aRoot[i], ¬Used, LARGEST_INT64); 9665 } 9666 pBt->db->flags = savedDbFlags; 9667 9668 /* Make sure every page in the file is referenced 9669 */ 9670 for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){ 9671 #ifdef SQLITE_OMIT_AUTOVACUUM 9672 if( getPageReferenced(&sCheck, i)==0 ){ 9673 checkAppendMsg(&sCheck, "Page %d is never used", i); 9674 } 9675 #else 9676 /* If the database supports auto-vacuum, make sure no tables contain 9677 ** references to pointer-map pages. 9678 */ 9679 if( getPageReferenced(&sCheck, i)==0 && 9680 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){ 9681 checkAppendMsg(&sCheck, "Page %d is never used", i); 9682 } 9683 if( getPageReferenced(&sCheck, i)!=0 && 9684 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){ 9685 checkAppendMsg(&sCheck, "Pointer map page %d is referenced", i); 9686 } 9687 #endif 9688 } 9689 9690 /* Clean up and report errors. 9691 */ 9692 integrity_ck_cleanup: 9693 sqlite3PageFree(sCheck.heap); 9694 sqlite3_free(sCheck.aPgRef); 9695 if( sCheck.mallocFailed ){ 9696 sqlite3StrAccumReset(&sCheck.errMsg); 9697 sCheck.nErr++; 9698 } 9699 *pnErr = sCheck.nErr; 9700 if( sCheck.nErr==0 ) sqlite3StrAccumReset(&sCheck.errMsg); 9701 /* Make sure this analysis did not leave any unref() pages. */ 9702 assert( nRef==sqlite3PagerRefcount(pBt->pPager) ); 9703 sqlite3BtreeLeave(p); 9704 return sqlite3StrAccumFinish(&sCheck.errMsg); 9705 } 9706 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ 9707 9708 /* 9709 ** Return the full pathname of the underlying database file. Return 9710 ** an empty string if the database is in-memory or a TEMP database. 9711 ** 9712 ** The pager filename is invariant as long as the pager is 9713 ** open so it is safe to access without the BtShared mutex. 9714 */ 9715 const char *sqlite3BtreeGetFilename(Btree *p){ 9716 assert( p->pBt->pPager!=0 ); 9717 return sqlite3PagerFilename(p->pBt->pPager, 1); 9718 } 9719 9720 /* 9721 ** Return the pathname of the journal file for this database. The return 9722 ** value of this routine is the same regardless of whether the journal file 9723 ** has been created or not. 9724 ** 9725 ** The pager journal filename is invariant as long as the pager is 9726 ** open so it is safe to access without the BtShared mutex. 9727 */ 9728 const char *sqlite3BtreeGetJournalname(Btree *p){ 9729 assert( p->pBt->pPager!=0 ); 9730 return sqlite3PagerJournalname(p->pBt->pPager); 9731 } 9732 9733 /* 9734 ** Return non-zero if a transaction is active. 9735 */ 9736 int sqlite3BtreeIsInTrans(Btree *p){ 9737 assert( p==0 || sqlite3_mutex_held(p->db->mutex) ); 9738 return (p && (p->inTrans==TRANS_WRITE)); 9739 } 9740 9741 #ifndef SQLITE_OMIT_WAL 9742 /* 9743 ** Run a checkpoint on the Btree passed as the first argument. 9744 ** 9745 ** Return SQLITE_LOCKED if this or any other connection has an open 9746 ** transaction on the shared-cache the argument Btree is connected to. 9747 ** 9748 ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART. 9749 */ 9750 int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){ 9751 int rc = SQLITE_OK; 9752 if( p ){ 9753 BtShared *pBt = p->pBt; 9754 sqlite3BtreeEnter(p); 9755 if( pBt->inTransaction!=TRANS_NONE ){ 9756 rc = SQLITE_LOCKED; 9757 }else{ 9758 rc = sqlite3PagerCheckpoint(pBt->pPager, p->db, eMode, pnLog, pnCkpt); 9759 } 9760 sqlite3BtreeLeave(p); 9761 } 9762 return rc; 9763 } 9764 #endif 9765 9766 /* 9767 ** Return non-zero if a read (or write) transaction is active. 9768 */ 9769 int sqlite3BtreeIsInReadTrans(Btree *p){ 9770 assert( p ); 9771 assert( sqlite3_mutex_held(p->db->mutex) ); 9772 return p->inTrans!=TRANS_NONE; 9773 } 9774 9775 int sqlite3BtreeIsInBackup(Btree *p){ 9776 assert( p ); 9777 assert( sqlite3_mutex_held(p->db->mutex) ); 9778 return p->nBackup!=0; 9779 } 9780 9781 /* 9782 ** This function returns a pointer to a blob of memory associated with 9783 ** a single shared-btree. The memory is used by client code for its own 9784 ** purposes (for example, to store a high-level schema associated with 9785 ** the shared-btree). The btree layer manages reference counting issues. 9786 ** 9787 ** The first time this is called on a shared-btree, nBytes bytes of memory 9788 ** are allocated, zeroed, and returned to the caller. For each subsequent 9789 ** call the nBytes parameter is ignored and a pointer to the same blob 9790 ** of memory returned. 9791 ** 9792 ** If the nBytes parameter is 0 and the blob of memory has not yet been 9793 ** allocated, a null pointer is returned. If the blob has already been 9794 ** allocated, it is returned as normal. 9795 ** 9796 ** Just before the shared-btree is closed, the function passed as the 9797 ** xFree argument when the memory allocation was made is invoked on the 9798 ** blob of allocated memory. The xFree function should not call sqlite3_free() 9799 ** on the memory, the btree layer does that. 9800 */ 9801 void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){ 9802 BtShared *pBt = p->pBt; 9803 sqlite3BtreeEnter(p); 9804 if( !pBt->pSchema && nBytes ){ 9805 pBt->pSchema = sqlite3DbMallocZero(0, nBytes); 9806 pBt->xFreeSchema = xFree; 9807 } 9808 sqlite3BtreeLeave(p); 9809 return pBt->pSchema; 9810 } 9811 9812 /* 9813 ** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared 9814 ** btree as the argument handle holds an exclusive lock on the 9815 ** sqlite_master table. Otherwise SQLITE_OK. 9816 */ 9817 int sqlite3BtreeSchemaLocked(Btree *p){ 9818 int rc; 9819 assert( sqlite3_mutex_held(p->db->mutex) ); 9820 sqlite3BtreeEnter(p); 9821 rc = querySharedCacheTableLock(p, MASTER_ROOT, READ_LOCK); 9822 assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE ); 9823 sqlite3BtreeLeave(p); 9824 return rc; 9825 } 9826 9827 9828 #ifndef SQLITE_OMIT_SHARED_CACHE 9829 /* 9830 ** Obtain a lock on the table whose root page is iTab. The 9831 ** lock is a write lock if isWritelock is true or a read lock 9832 ** if it is false. 9833 */ 9834 int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){ 9835 int rc = SQLITE_OK; 9836 assert( p->inTrans!=TRANS_NONE ); 9837 if( p->sharable ){ 9838 u8 lockType = READ_LOCK + isWriteLock; 9839 assert( READ_LOCK+1==WRITE_LOCK ); 9840 assert( isWriteLock==0 || isWriteLock==1 ); 9841 9842 sqlite3BtreeEnter(p); 9843 rc = querySharedCacheTableLock(p, iTab, lockType); 9844 if( rc==SQLITE_OK ){ 9845 rc = setSharedCacheTableLock(p, iTab, lockType); 9846 } 9847 sqlite3BtreeLeave(p); 9848 } 9849 return rc; 9850 } 9851 #endif 9852 9853 #ifndef SQLITE_OMIT_INCRBLOB 9854 /* 9855 ** Argument pCsr must be a cursor opened for writing on an 9856 ** INTKEY table currently pointing at a valid table entry. 9857 ** This function modifies the data stored as part of that entry. 9858 ** 9859 ** Only the data content may only be modified, it is not possible to 9860 ** change the length of the data stored. If this function is called with 9861 ** parameters that attempt to write past the end of the existing data, 9862 ** no modifications are made and SQLITE_CORRUPT is returned. 9863 */ 9864 int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){ 9865 int rc; 9866 assert( cursorOwnsBtShared(pCsr) ); 9867 assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) ); 9868 assert( pCsr->curFlags & BTCF_Incrblob ); 9869 9870 rc = restoreCursorPosition(pCsr); 9871 if( rc!=SQLITE_OK ){ 9872 return rc; 9873 } 9874 assert( pCsr->eState!=CURSOR_REQUIRESEEK ); 9875 if( pCsr->eState!=CURSOR_VALID ){ 9876 return SQLITE_ABORT; 9877 } 9878 9879 /* Save the positions of all other cursors open on this table. This is 9880 ** required in case any of them are holding references to an xFetch 9881 ** version of the b-tree page modified by the accessPayload call below. 9882 ** 9883 ** Note that pCsr must be open on a INTKEY table and saveCursorPosition() 9884 ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence 9885 ** saveAllCursors can only return SQLITE_OK. 9886 */ 9887 VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr); 9888 assert( rc==SQLITE_OK ); 9889 9890 /* Check some assumptions: 9891 ** (a) the cursor is open for writing, 9892 ** (b) there is a read/write transaction open, 9893 ** (c) the connection holds a write-lock on the table (if required), 9894 ** (d) there are no conflicting read-locks, and 9895 ** (e) the cursor points at a valid row of an intKey table. 9896 */ 9897 if( (pCsr->curFlags & BTCF_WriteFlag)==0 ){ 9898 return SQLITE_READONLY; 9899 } 9900 assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0 9901 && pCsr->pBt->inTransaction==TRANS_WRITE ); 9902 assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) ); 9903 assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) ); 9904 assert( pCsr->pPage->intKey ); 9905 9906 return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1); 9907 } 9908 9909 /* 9910 ** Mark this cursor as an incremental blob cursor. 9911 */ 9912 void sqlite3BtreeIncrblobCursor(BtCursor *pCur){ 9913 pCur->curFlags |= BTCF_Incrblob; 9914 pCur->pBtree->hasIncrblobCur = 1; 9915 } 9916 #endif 9917 9918 /* 9919 ** Set both the "read version" (single byte at byte offset 18) and 9920 ** "write version" (single byte at byte offset 19) fields in the database 9921 ** header to iVersion. 9922 */ 9923 int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){ 9924 BtShared *pBt = pBtree->pBt; 9925 int rc; /* Return code */ 9926 9927 assert( iVersion==1 || iVersion==2 ); 9928 9929 /* If setting the version fields to 1, do not automatically open the 9930 ** WAL connection, even if the version fields are currently set to 2. 9931 */ 9932 pBt->btsFlags &= ~BTS_NO_WAL; 9933 if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL; 9934 9935 rc = sqlite3BtreeBeginTrans(pBtree, 0); 9936 if( rc==SQLITE_OK ){ 9937 u8 *aData = pBt->pPage1->aData; 9938 if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){ 9939 rc = sqlite3BtreeBeginTrans(pBtree, 2); 9940 if( rc==SQLITE_OK ){ 9941 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); 9942 if( rc==SQLITE_OK ){ 9943 aData[18] = (u8)iVersion; 9944 aData[19] = (u8)iVersion; 9945 } 9946 } 9947 } 9948 } 9949 9950 pBt->btsFlags &= ~BTS_NO_WAL; 9951 return rc; 9952 } 9953 9954 /* 9955 ** Return true if the cursor has a hint specified. This routine is 9956 ** only used from within assert() statements 9957 */ 9958 int sqlite3BtreeCursorHasHint(BtCursor *pCsr, unsigned int mask){ 9959 return (pCsr->hints & mask)!=0; 9960 } 9961 9962 /* 9963 ** Return true if the given Btree is read-only. 9964 */ 9965 int sqlite3BtreeIsReadonly(Btree *p){ 9966 return (p->pBt->btsFlags & BTS_READ_ONLY)!=0; 9967 } 9968 9969 /* 9970 ** Return the size of the header added to each page by this module. 9971 */ 9972 int sqlite3HeaderSizeBtree(void){ return ROUND8(sizeof(MemPage)); } 9973 9974 #if !defined(SQLITE_OMIT_SHARED_CACHE) 9975 /* 9976 ** Return true if the Btree passed as the only argument is sharable. 9977 */ 9978 int sqlite3BtreeSharable(Btree *p){ 9979 return p->sharable; 9980 } 9981 9982 /* 9983 ** Return the number of connections to the BtShared object accessed by 9984 ** the Btree handle passed as the only argument. For private caches 9985 ** this is always 1. For shared caches it may be 1 or greater. 9986 */ 9987 int sqlite3BtreeConnectionCount(Btree *p){ 9988 testcase( p->sharable ); 9989 return p->pBt->nRef; 9990 } 9991 #endif 9992