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_MAIN. 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 #ifdef SQLITE_DEBUG 116 /* 117 ** Return and reset the seek counter for a Btree object. 118 */ 119 sqlite3_uint64 sqlite3BtreeSeekCount(Btree *pBt){ 120 u64 n = pBt->nSeek; 121 pBt->nSeek = 0; 122 return n; 123 } 124 #endif 125 126 /* 127 ** Implementation of the SQLITE_CORRUPT_PAGE() macro. Takes a single 128 ** (MemPage*) as an argument. The (MemPage*) must not be NULL. 129 ** 130 ** If SQLITE_DEBUG is not defined, then this macro is equivalent to 131 ** SQLITE_CORRUPT_BKPT. Or, if SQLITE_DEBUG is set, then the log message 132 ** normally produced as a side-effect of SQLITE_CORRUPT_BKPT is augmented 133 ** with the page number and filename associated with the (MemPage*). 134 */ 135 #ifdef SQLITE_DEBUG 136 int corruptPageError(int lineno, MemPage *p){ 137 char *zMsg; 138 sqlite3BeginBenignMalloc(); 139 zMsg = sqlite3_mprintf("database corruption page %d of %s", 140 (int)p->pgno, sqlite3PagerFilename(p->pBt->pPager, 0) 141 ); 142 sqlite3EndBenignMalloc(); 143 if( zMsg ){ 144 sqlite3ReportError(SQLITE_CORRUPT, lineno, zMsg); 145 } 146 sqlite3_free(zMsg); 147 return SQLITE_CORRUPT_BKPT; 148 } 149 # define SQLITE_CORRUPT_PAGE(pMemPage) corruptPageError(__LINE__, pMemPage) 150 #else 151 # define SQLITE_CORRUPT_PAGE(pMemPage) SQLITE_CORRUPT_PGNO(pMemPage->pgno) 152 #endif 153 154 #ifndef SQLITE_OMIT_SHARED_CACHE 155 156 #ifdef SQLITE_DEBUG 157 /* 158 **** This function is only used as part of an assert() statement. *** 159 ** 160 ** Check to see if pBtree holds the required locks to read or write to the 161 ** table with root page iRoot. Return 1 if it does and 0 if not. 162 ** 163 ** For example, when writing to a table with root-page iRoot via 164 ** Btree connection pBtree: 165 ** 166 ** assert( hasSharedCacheTableLock(pBtree, iRoot, 0, WRITE_LOCK) ); 167 ** 168 ** When writing to an index that resides in a sharable database, the 169 ** caller should have first obtained a lock specifying the root page of 170 ** the corresponding table. This makes things a bit more complicated, 171 ** as this module treats each table as a separate structure. To determine 172 ** the table corresponding to the index being written, this 173 ** function has to search through the database schema. 174 ** 175 ** Instead of a lock on the table/index rooted at page iRoot, the caller may 176 ** hold a write-lock on the schema table (root page 1). This is also 177 ** acceptable. 178 */ 179 static int hasSharedCacheTableLock( 180 Btree *pBtree, /* Handle that must hold lock */ 181 Pgno iRoot, /* Root page of b-tree */ 182 int isIndex, /* True if iRoot is the root of an index b-tree */ 183 int eLockType /* Required lock type (READ_LOCK or WRITE_LOCK) */ 184 ){ 185 Schema *pSchema = (Schema *)pBtree->pBt->pSchema; 186 Pgno iTab = 0; 187 BtLock *pLock; 188 189 /* If this database is not shareable, or if the client is reading 190 ** and has the read-uncommitted flag set, then no lock is required. 191 ** Return true immediately. 192 */ 193 if( (pBtree->sharable==0) 194 || (eLockType==READ_LOCK && (pBtree->db->flags & SQLITE_ReadUncommit)) 195 ){ 196 return 1; 197 } 198 199 /* If the client is reading or writing an index and the schema is 200 ** not loaded, then it is too difficult to actually check to see if 201 ** the correct locks are held. So do not bother - just return true. 202 ** This case does not come up very often anyhow. 203 */ 204 if( isIndex && (!pSchema || (pSchema->schemaFlags&DB_SchemaLoaded)==0) ){ 205 return 1; 206 } 207 208 /* Figure out the root-page that the lock should be held on. For table 209 ** b-trees, this is just the root page of the b-tree being read or 210 ** written. For index b-trees, it is the root page of the associated 211 ** table. */ 212 if( isIndex ){ 213 HashElem *p; 214 int bSeen = 0; 215 for(p=sqliteHashFirst(&pSchema->idxHash); p; p=sqliteHashNext(p)){ 216 Index *pIdx = (Index *)sqliteHashData(p); 217 if( pIdx->tnum==(int)iRoot ){ 218 if( bSeen ){ 219 /* Two or more indexes share the same root page. There must 220 ** be imposter tables. So just return true. The assert is not 221 ** useful in that case. */ 222 return 1; 223 } 224 iTab = pIdx->pTable->tnum; 225 bSeen = 1; 226 } 227 } 228 }else{ 229 iTab = iRoot; 230 } 231 232 /* Search for the required lock. Either a write-lock on root-page iTab, a 233 ** write-lock on the schema table, or (if the client is reading) a 234 ** read-lock on iTab will suffice. Return 1 if any of these are found. */ 235 for(pLock=pBtree->pBt->pLock; pLock; pLock=pLock->pNext){ 236 if( pLock->pBtree==pBtree 237 && (pLock->iTable==iTab || (pLock->eLock==WRITE_LOCK && pLock->iTable==1)) 238 && pLock->eLock>=eLockType 239 ){ 240 return 1; 241 } 242 } 243 244 /* Failed to find the required lock. */ 245 return 0; 246 } 247 #endif /* SQLITE_DEBUG */ 248 249 #ifdef SQLITE_DEBUG 250 /* 251 **** This function may be used as part of assert() statements only. **** 252 ** 253 ** Return true if it would be illegal for pBtree to write into the 254 ** table or index rooted at iRoot because other shared connections are 255 ** simultaneously reading that same table or index. 256 ** 257 ** It is illegal for pBtree to write if some other Btree object that 258 ** shares the same BtShared object is currently reading or writing 259 ** the iRoot table. Except, if the other Btree object has the 260 ** read-uncommitted flag set, then it is OK for the other object to 261 ** have a read cursor. 262 ** 263 ** For example, before writing to any part of the table or index 264 ** rooted at page iRoot, one should call: 265 ** 266 ** assert( !hasReadConflicts(pBtree, iRoot) ); 267 */ 268 static int hasReadConflicts(Btree *pBtree, Pgno iRoot){ 269 BtCursor *p; 270 for(p=pBtree->pBt->pCursor; p; p=p->pNext){ 271 if( p->pgnoRoot==iRoot 272 && p->pBtree!=pBtree 273 && 0==(p->pBtree->db->flags & SQLITE_ReadUncommit) 274 ){ 275 return 1; 276 } 277 } 278 return 0; 279 } 280 #endif /* #ifdef SQLITE_DEBUG */ 281 282 /* 283 ** Query to see if Btree handle p may obtain a lock of type eLock 284 ** (READ_LOCK or WRITE_LOCK) on the table with root-page iTab. Return 285 ** SQLITE_OK if the lock may be obtained (by calling 286 ** setSharedCacheTableLock()), or SQLITE_LOCKED if not. 287 */ 288 static int querySharedCacheTableLock(Btree *p, Pgno iTab, u8 eLock){ 289 BtShared *pBt = p->pBt; 290 BtLock *pIter; 291 292 assert( sqlite3BtreeHoldsMutex(p) ); 293 assert( eLock==READ_LOCK || eLock==WRITE_LOCK ); 294 assert( p->db!=0 ); 295 assert( !(p->db->flags&SQLITE_ReadUncommit)||eLock==WRITE_LOCK||iTab==1 ); 296 297 /* If requesting a write-lock, then the Btree must have an open write 298 ** transaction on this file. And, obviously, for this to be so there 299 ** must be an open write transaction on the file itself. 300 */ 301 assert( eLock==READ_LOCK || (p==pBt->pWriter && p->inTrans==TRANS_WRITE) ); 302 assert( eLock==READ_LOCK || pBt->inTransaction==TRANS_WRITE ); 303 304 /* This routine is a no-op if the shared-cache is not enabled */ 305 if( !p->sharable ){ 306 return SQLITE_OK; 307 } 308 309 /* If some other connection is holding an exclusive lock, the 310 ** requested lock may not be obtained. 311 */ 312 if( pBt->pWriter!=p && (pBt->btsFlags & BTS_EXCLUSIVE)!=0 ){ 313 sqlite3ConnectionBlocked(p->db, pBt->pWriter->db); 314 return SQLITE_LOCKED_SHAREDCACHE; 315 } 316 317 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ 318 /* The condition (pIter->eLock!=eLock) in the following if(...) 319 ** statement is a simplification of: 320 ** 321 ** (eLock==WRITE_LOCK || pIter->eLock==WRITE_LOCK) 322 ** 323 ** since we know that if eLock==WRITE_LOCK, then no other connection 324 ** may hold a WRITE_LOCK on any table in this file (since there can 325 ** only be a single writer). 326 */ 327 assert( pIter->eLock==READ_LOCK || pIter->eLock==WRITE_LOCK ); 328 assert( eLock==READ_LOCK || pIter->pBtree==p || pIter->eLock==READ_LOCK); 329 if( pIter->pBtree!=p && pIter->iTable==iTab && pIter->eLock!=eLock ){ 330 sqlite3ConnectionBlocked(p->db, pIter->pBtree->db); 331 if( eLock==WRITE_LOCK ){ 332 assert( p==pBt->pWriter ); 333 pBt->btsFlags |= BTS_PENDING; 334 } 335 return SQLITE_LOCKED_SHAREDCACHE; 336 } 337 } 338 return SQLITE_OK; 339 } 340 #endif /* !SQLITE_OMIT_SHARED_CACHE */ 341 342 #ifndef SQLITE_OMIT_SHARED_CACHE 343 /* 344 ** Add a lock on the table with root-page iTable to the shared-btree used 345 ** by Btree handle p. Parameter eLock must be either READ_LOCK or 346 ** WRITE_LOCK. 347 ** 348 ** This function assumes the following: 349 ** 350 ** (a) The specified Btree object p is connected to a sharable 351 ** database (one with the BtShared.sharable flag set), and 352 ** 353 ** (b) No other Btree objects hold a lock that conflicts 354 ** with the requested lock (i.e. querySharedCacheTableLock() has 355 ** already been called and returned SQLITE_OK). 356 ** 357 ** SQLITE_OK is returned if the lock is added successfully. SQLITE_NOMEM 358 ** is returned if a malloc attempt fails. 359 */ 360 static int setSharedCacheTableLock(Btree *p, Pgno iTable, u8 eLock){ 361 BtShared *pBt = p->pBt; 362 BtLock *pLock = 0; 363 BtLock *pIter; 364 365 assert( sqlite3BtreeHoldsMutex(p) ); 366 assert( eLock==READ_LOCK || eLock==WRITE_LOCK ); 367 assert( p->db!=0 ); 368 369 /* A connection with the read-uncommitted flag set will never try to 370 ** obtain a read-lock using this function. The only read-lock obtained 371 ** by a connection in read-uncommitted mode is on the sqlite_schema 372 ** table, and that lock is obtained in BtreeBeginTrans(). */ 373 assert( 0==(p->db->flags&SQLITE_ReadUncommit) || eLock==WRITE_LOCK ); 374 375 /* This function should only be called on a sharable b-tree after it 376 ** has been determined that no other b-tree holds a conflicting lock. */ 377 assert( p->sharable ); 378 assert( SQLITE_OK==querySharedCacheTableLock(p, iTable, eLock) ); 379 380 /* First search the list for an existing lock on this table. */ 381 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ 382 if( pIter->iTable==iTable && pIter->pBtree==p ){ 383 pLock = pIter; 384 break; 385 } 386 } 387 388 /* If the above search did not find a BtLock struct associating Btree p 389 ** with table iTable, allocate one and link it into the list. 390 */ 391 if( !pLock ){ 392 pLock = (BtLock *)sqlite3MallocZero(sizeof(BtLock)); 393 if( !pLock ){ 394 return SQLITE_NOMEM_BKPT; 395 } 396 pLock->iTable = iTable; 397 pLock->pBtree = p; 398 pLock->pNext = pBt->pLock; 399 pBt->pLock = pLock; 400 } 401 402 /* Set the BtLock.eLock variable to the maximum of the current lock 403 ** and the requested lock. This means if a write-lock was already held 404 ** and a read-lock requested, we don't incorrectly downgrade the lock. 405 */ 406 assert( WRITE_LOCK>READ_LOCK ); 407 if( eLock>pLock->eLock ){ 408 pLock->eLock = eLock; 409 } 410 411 return SQLITE_OK; 412 } 413 #endif /* !SQLITE_OMIT_SHARED_CACHE */ 414 415 #ifndef SQLITE_OMIT_SHARED_CACHE 416 /* 417 ** Release all the table locks (locks obtained via calls to 418 ** the setSharedCacheTableLock() procedure) held by Btree object p. 419 ** 420 ** This function assumes that Btree p has an open read or write 421 ** transaction. If it does not, then the BTS_PENDING flag 422 ** may be incorrectly cleared. 423 */ 424 static void clearAllSharedCacheTableLocks(Btree *p){ 425 BtShared *pBt = p->pBt; 426 BtLock **ppIter = &pBt->pLock; 427 428 assert( sqlite3BtreeHoldsMutex(p) ); 429 assert( p->sharable || 0==*ppIter ); 430 assert( p->inTrans>0 ); 431 432 while( *ppIter ){ 433 BtLock *pLock = *ppIter; 434 assert( (pBt->btsFlags & BTS_EXCLUSIVE)==0 || pBt->pWriter==pLock->pBtree ); 435 assert( pLock->pBtree->inTrans>=pLock->eLock ); 436 if( pLock->pBtree==p ){ 437 *ppIter = pLock->pNext; 438 assert( pLock->iTable!=1 || pLock==&p->lock ); 439 if( pLock->iTable!=1 ){ 440 sqlite3_free(pLock); 441 } 442 }else{ 443 ppIter = &pLock->pNext; 444 } 445 } 446 447 assert( (pBt->btsFlags & BTS_PENDING)==0 || pBt->pWriter ); 448 if( pBt->pWriter==p ){ 449 pBt->pWriter = 0; 450 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING); 451 }else if( pBt->nTransaction==2 ){ 452 /* This function is called when Btree p is concluding its 453 ** transaction. If there currently exists a writer, and p is not 454 ** that writer, then the number of locks held by connections other 455 ** than the writer must be about to drop to zero. In this case 456 ** set the BTS_PENDING flag to 0. 457 ** 458 ** If there is not currently a writer, then BTS_PENDING must 459 ** be zero already. So this next line is harmless in that case. 460 */ 461 pBt->btsFlags &= ~BTS_PENDING; 462 } 463 } 464 465 /* 466 ** This function changes all write-locks held by Btree p into read-locks. 467 */ 468 static void downgradeAllSharedCacheTableLocks(Btree *p){ 469 BtShared *pBt = p->pBt; 470 if( pBt->pWriter==p ){ 471 BtLock *pLock; 472 pBt->pWriter = 0; 473 pBt->btsFlags &= ~(BTS_EXCLUSIVE|BTS_PENDING); 474 for(pLock=pBt->pLock; pLock; pLock=pLock->pNext){ 475 assert( pLock->eLock==READ_LOCK || pLock->pBtree==p ); 476 pLock->eLock = READ_LOCK; 477 } 478 } 479 } 480 481 #endif /* SQLITE_OMIT_SHARED_CACHE */ 482 483 static void releasePage(MemPage *pPage); /* Forward reference */ 484 static void releasePageOne(MemPage *pPage); /* Forward reference */ 485 static void releasePageNotNull(MemPage *pPage); /* Forward reference */ 486 487 /* 488 ***** This routine is used inside of assert() only **** 489 ** 490 ** Verify that the cursor holds the mutex on its BtShared 491 */ 492 #ifdef SQLITE_DEBUG 493 static int cursorHoldsMutex(BtCursor *p){ 494 return sqlite3_mutex_held(p->pBt->mutex); 495 } 496 497 /* Verify that the cursor and the BtShared agree about what is the current 498 ** database connetion. This is important in shared-cache mode. If the database 499 ** connection pointers get out-of-sync, it is possible for routines like 500 ** btreeInitPage() to reference an stale connection pointer that references a 501 ** a connection that has already closed. This routine is used inside assert() 502 ** statements only and for the purpose of double-checking that the btree code 503 ** does keep the database connection pointers up-to-date. 504 */ 505 static int cursorOwnsBtShared(BtCursor *p){ 506 assert( cursorHoldsMutex(p) ); 507 return (p->pBtree->db==p->pBt->db); 508 } 509 #endif 510 511 /* 512 ** Invalidate the overflow cache of the cursor passed as the first argument. 513 ** on the shared btree structure pBt. 514 */ 515 #define invalidateOverflowCache(pCur) (pCur->curFlags &= ~BTCF_ValidOvfl) 516 517 /* 518 ** Invalidate the overflow page-list cache for all cursors opened 519 ** on the shared btree structure pBt. 520 */ 521 static void invalidateAllOverflowCache(BtShared *pBt){ 522 BtCursor *p; 523 assert( sqlite3_mutex_held(pBt->mutex) ); 524 for(p=pBt->pCursor; p; p=p->pNext){ 525 invalidateOverflowCache(p); 526 } 527 } 528 529 #ifndef SQLITE_OMIT_INCRBLOB 530 /* 531 ** This function is called before modifying the contents of a table 532 ** to invalidate any incrblob cursors that are open on the 533 ** row or one of the rows being modified. 534 ** 535 ** If argument isClearTable is true, then the entire contents of the 536 ** table is about to be deleted. In this case invalidate all incrblob 537 ** cursors open on any row within the table with root-page pgnoRoot. 538 ** 539 ** Otherwise, if argument isClearTable is false, then the row with 540 ** rowid iRow is being replaced or deleted. In this case invalidate 541 ** only those incrblob cursors open on that specific row. 542 */ 543 static void invalidateIncrblobCursors( 544 Btree *pBtree, /* The database file to check */ 545 Pgno pgnoRoot, /* The table that might be changing */ 546 i64 iRow, /* The rowid that might be changing */ 547 int isClearTable /* True if all rows are being deleted */ 548 ){ 549 BtCursor *p; 550 assert( pBtree->hasIncrblobCur ); 551 assert( sqlite3BtreeHoldsMutex(pBtree) ); 552 pBtree->hasIncrblobCur = 0; 553 for(p=pBtree->pBt->pCursor; p; p=p->pNext){ 554 if( (p->curFlags & BTCF_Incrblob)!=0 ){ 555 pBtree->hasIncrblobCur = 1; 556 if( p->pgnoRoot==pgnoRoot && (isClearTable || p->info.nKey==iRow) ){ 557 p->eState = CURSOR_INVALID; 558 } 559 } 560 } 561 } 562 563 #else 564 /* Stub function when INCRBLOB is omitted */ 565 #define invalidateIncrblobCursors(w,x,y,z) 566 #endif /* SQLITE_OMIT_INCRBLOB */ 567 568 /* 569 ** Set bit pgno of the BtShared.pHasContent bitvec. This is called 570 ** when a page that previously contained data becomes a free-list leaf 571 ** page. 572 ** 573 ** The BtShared.pHasContent bitvec exists to work around an obscure 574 ** bug caused by the interaction of two useful IO optimizations surrounding 575 ** free-list leaf pages: 576 ** 577 ** 1) When all data is deleted from a page and the page becomes 578 ** a free-list leaf page, the page is not written to the database 579 ** (as free-list leaf pages contain no meaningful data). Sometimes 580 ** such a page is not even journalled (as it will not be modified, 581 ** why bother journalling it?). 582 ** 583 ** 2) When a free-list leaf page is reused, its content is not read 584 ** from the database or written to the journal file (why should it 585 ** be, if it is not at all meaningful?). 586 ** 587 ** By themselves, these optimizations work fine and provide a handy 588 ** performance boost to bulk delete or insert operations. However, if 589 ** a page is moved to the free-list and then reused within the same 590 ** transaction, a problem comes up. If the page is not journalled when 591 ** it is moved to the free-list and it is also not journalled when it 592 ** is extracted from the free-list and reused, then the original data 593 ** may be lost. In the event of a rollback, it may not be possible 594 ** to restore the database to its original configuration. 595 ** 596 ** The solution is the BtShared.pHasContent bitvec. Whenever a page is 597 ** moved to become a free-list leaf page, the corresponding bit is 598 ** set in the bitvec. Whenever a leaf page is extracted from the free-list, 599 ** optimization 2 above is omitted if the corresponding bit is already 600 ** set in BtShared.pHasContent. The contents of the bitvec are cleared 601 ** at the end of every transaction. 602 */ 603 static int btreeSetHasContent(BtShared *pBt, Pgno pgno){ 604 int rc = SQLITE_OK; 605 if( !pBt->pHasContent ){ 606 assert( pgno<=pBt->nPage ); 607 pBt->pHasContent = sqlite3BitvecCreate(pBt->nPage); 608 if( !pBt->pHasContent ){ 609 rc = SQLITE_NOMEM_BKPT; 610 } 611 } 612 if( rc==SQLITE_OK && pgno<=sqlite3BitvecSize(pBt->pHasContent) ){ 613 rc = sqlite3BitvecSet(pBt->pHasContent, pgno); 614 } 615 return rc; 616 } 617 618 /* 619 ** Query the BtShared.pHasContent vector. 620 ** 621 ** This function is called when a free-list leaf page is removed from the 622 ** free-list for reuse. It returns false if it is safe to retrieve the 623 ** page from the pager layer with the 'no-content' flag set. True otherwise. 624 */ 625 static int btreeGetHasContent(BtShared *pBt, Pgno pgno){ 626 Bitvec *p = pBt->pHasContent; 627 return p && (pgno>sqlite3BitvecSize(p) || sqlite3BitvecTestNotNull(p, pgno)); 628 } 629 630 /* 631 ** Clear (destroy) the BtShared.pHasContent bitvec. This should be 632 ** invoked at the conclusion of each write-transaction. 633 */ 634 static void btreeClearHasContent(BtShared *pBt){ 635 sqlite3BitvecDestroy(pBt->pHasContent); 636 pBt->pHasContent = 0; 637 } 638 639 /* 640 ** Release all of the apPage[] pages for a cursor. 641 */ 642 static void btreeReleaseAllCursorPages(BtCursor *pCur){ 643 int i; 644 if( pCur->iPage>=0 ){ 645 for(i=0; i<pCur->iPage; i++){ 646 releasePageNotNull(pCur->apPage[i]); 647 } 648 releasePageNotNull(pCur->pPage); 649 pCur->iPage = -1; 650 } 651 } 652 653 /* 654 ** The cursor passed as the only argument must point to a valid entry 655 ** when this function is called (i.e. have eState==CURSOR_VALID). This 656 ** function saves the current cursor key in variables pCur->nKey and 657 ** pCur->pKey. SQLITE_OK is returned if successful or an SQLite error 658 ** code otherwise. 659 ** 660 ** If the cursor is open on an intkey table, then the integer key 661 ** (the rowid) is stored in pCur->nKey and pCur->pKey is left set to 662 ** NULL. If the cursor is open on a non-intkey table, then pCur->pKey is 663 ** set to point to a malloced buffer pCur->nKey bytes in size containing 664 ** the key. 665 */ 666 static int saveCursorKey(BtCursor *pCur){ 667 int rc = SQLITE_OK; 668 assert( CURSOR_VALID==pCur->eState ); 669 assert( 0==pCur->pKey ); 670 assert( cursorHoldsMutex(pCur) ); 671 672 if( pCur->curIntKey ){ 673 /* Only the rowid is required for a table btree */ 674 pCur->nKey = sqlite3BtreeIntegerKey(pCur); 675 }else{ 676 /* For an index btree, save the complete key content. It is possible 677 ** that the current key is corrupt. In that case, it is possible that 678 ** the sqlite3VdbeRecordUnpack() function may overread the buffer by 679 ** up to the size of 1 varint plus 1 8-byte value when the cursor 680 ** position is restored. Hence the 17 bytes of padding allocated 681 ** below. */ 682 void *pKey; 683 pCur->nKey = sqlite3BtreePayloadSize(pCur); 684 pKey = sqlite3Malloc( pCur->nKey + 9 + 8 ); 685 if( pKey ){ 686 rc = sqlite3BtreePayload(pCur, 0, (int)pCur->nKey, pKey); 687 if( rc==SQLITE_OK ){ 688 memset(((u8*)pKey)+pCur->nKey, 0, 9+8); 689 pCur->pKey = pKey; 690 }else{ 691 sqlite3_free(pKey); 692 } 693 }else{ 694 rc = SQLITE_NOMEM_BKPT; 695 } 696 } 697 assert( !pCur->curIntKey || !pCur->pKey ); 698 return rc; 699 } 700 701 /* 702 ** Save the current cursor position in the variables BtCursor.nKey 703 ** and BtCursor.pKey. The cursor's state is set to CURSOR_REQUIRESEEK. 704 ** 705 ** The caller must ensure that the cursor is valid (has eState==CURSOR_VALID) 706 ** prior to calling this routine. 707 */ 708 static int saveCursorPosition(BtCursor *pCur){ 709 int rc; 710 711 assert( CURSOR_VALID==pCur->eState || CURSOR_SKIPNEXT==pCur->eState ); 712 assert( 0==pCur->pKey ); 713 assert( cursorHoldsMutex(pCur) ); 714 715 if( pCur->curFlags & BTCF_Pinned ){ 716 return SQLITE_CONSTRAINT_PINNED; 717 } 718 if( pCur->eState==CURSOR_SKIPNEXT ){ 719 pCur->eState = CURSOR_VALID; 720 }else{ 721 pCur->skipNext = 0; 722 } 723 724 rc = saveCursorKey(pCur); 725 if( rc==SQLITE_OK ){ 726 btreeReleaseAllCursorPages(pCur); 727 pCur->eState = CURSOR_REQUIRESEEK; 728 } 729 730 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl|BTCF_AtLast); 731 return rc; 732 } 733 734 /* Forward reference */ 735 static int SQLITE_NOINLINE saveCursorsOnList(BtCursor*,Pgno,BtCursor*); 736 737 /* 738 ** Save the positions of all cursors (except pExcept) that are open on 739 ** the table with root-page iRoot. "Saving the cursor position" means that 740 ** the location in the btree is remembered in such a way that it can be 741 ** moved back to the same spot after the btree has been modified. This 742 ** routine is called just before cursor pExcept is used to modify the 743 ** table, for example in BtreeDelete() or BtreeInsert(). 744 ** 745 ** If there are two or more cursors on the same btree, then all such 746 ** cursors should have their BTCF_Multiple flag set. The btreeCursor() 747 ** routine enforces that rule. This routine only needs to be called in 748 ** the uncommon case when pExpect has the BTCF_Multiple flag set. 749 ** 750 ** If pExpect!=NULL and if no other cursors are found on the same root-page, 751 ** then the BTCF_Multiple flag on pExpect is cleared, to avoid another 752 ** pointless call to this routine. 753 ** 754 ** Implementation note: This routine merely checks to see if any cursors 755 ** need to be saved. It calls out to saveCursorsOnList() in the (unusual) 756 ** event that cursors are in need to being saved. 757 */ 758 static int saveAllCursors(BtShared *pBt, Pgno iRoot, BtCursor *pExcept){ 759 BtCursor *p; 760 assert( sqlite3_mutex_held(pBt->mutex) ); 761 assert( pExcept==0 || pExcept->pBt==pBt ); 762 for(p=pBt->pCursor; p; p=p->pNext){ 763 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ) break; 764 } 765 if( p ) return saveCursorsOnList(p, iRoot, pExcept); 766 if( pExcept ) pExcept->curFlags &= ~BTCF_Multiple; 767 return SQLITE_OK; 768 } 769 770 /* This helper routine to saveAllCursors does the actual work of saving 771 ** the cursors if and when a cursor is found that actually requires saving. 772 ** The common case is that no cursors need to be saved, so this routine is 773 ** broken out from its caller to avoid unnecessary stack pointer movement. 774 */ 775 static int SQLITE_NOINLINE saveCursorsOnList( 776 BtCursor *p, /* The first cursor that needs saving */ 777 Pgno iRoot, /* Only save cursor with this iRoot. Save all if zero */ 778 BtCursor *pExcept /* Do not save this cursor */ 779 ){ 780 do{ 781 if( p!=pExcept && (0==iRoot || p->pgnoRoot==iRoot) ){ 782 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){ 783 int rc = saveCursorPosition(p); 784 if( SQLITE_OK!=rc ){ 785 return rc; 786 } 787 }else{ 788 testcase( p->iPage>=0 ); 789 btreeReleaseAllCursorPages(p); 790 } 791 } 792 p = p->pNext; 793 }while( p ); 794 return SQLITE_OK; 795 } 796 797 /* 798 ** Clear the current cursor position. 799 */ 800 void sqlite3BtreeClearCursor(BtCursor *pCur){ 801 assert( cursorHoldsMutex(pCur) ); 802 sqlite3_free(pCur->pKey); 803 pCur->pKey = 0; 804 pCur->eState = CURSOR_INVALID; 805 } 806 807 /* 808 ** In this version of BtreeMoveto, pKey is a packed index record 809 ** such as is generated by the OP_MakeRecord opcode. Unpack the 810 ** record and then call BtreeMovetoUnpacked() to do the work. 811 */ 812 static int btreeMoveto( 813 BtCursor *pCur, /* Cursor open on the btree to be searched */ 814 const void *pKey, /* Packed key if the btree is an index */ 815 i64 nKey, /* Integer key for tables. Size of pKey for indices */ 816 int bias, /* Bias search to the high end */ 817 int *pRes /* Write search results here */ 818 ){ 819 int rc; /* Status code */ 820 UnpackedRecord *pIdxKey; /* Unpacked index key */ 821 822 if( pKey ){ 823 KeyInfo *pKeyInfo = pCur->pKeyInfo; 824 assert( nKey==(i64)(int)nKey ); 825 pIdxKey = sqlite3VdbeAllocUnpackedRecord(pKeyInfo); 826 if( pIdxKey==0 ) return SQLITE_NOMEM_BKPT; 827 sqlite3VdbeRecordUnpack(pKeyInfo, (int)nKey, pKey, pIdxKey); 828 if( pIdxKey->nField==0 || pIdxKey->nField>pKeyInfo->nAllField ){ 829 rc = SQLITE_CORRUPT_BKPT; 830 }else{ 831 rc = sqlite3BtreeIndexMoveto(pCur, pIdxKey, pRes); 832 } 833 sqlite3DbFree(pCur->pKeyInfo->db, pIdxKey); 834 }else{ 835 pIdxKey = 0; 836 rc = sqlite3BtreeTableMoveto(pCur, nKey, bias, pRes); 837 } 838 return rc; 839 } 840 841 /* 842 ** Restore the cursor to the position it was in (or as close to as possible) 843 ** when saveCursorPosition() was called. Note that this call deletes the 844 ** saved position info stored by saveCursorPosition(), so there can be 845 ** at most one effective restoreCursorPosition() call after each 846 ** saveCursorPosition(). 847 */ 848 static int btreeRestoreCursorPosition(BtCursor *pCur){ 849 int rc; 850 int skipNext = 0; 851 assert( cursorOwnsBtShared(pCur) ); 852 assert( pCur->eState>=CURSOR_REQUIRESEEK ); 853 if( pCur->eState==CURSOR_FAULT ){ 854 return pCur->skipNext; 855 } 856 pCur->eState = CURSOR_INVALID; 857 if( sqlite3FaultSim(410) ){ 858 rc = SQLITE_IOERR; 859 }else{ 860 rc = btreeMoveto(pCur, pCur->pKey, pCur->nKey, 0, &skipNext); 861 } 862 if( rc==SQLITE_OK ){ 863 sqlite3_free(pCur->pKey); 864 pCur->pKey = 0; 865 assert( pCur->eState==CURSOR_VALID || pCur->eState==CURSOR_INVALID ); 866 if( skipNext ) pCur->skipNext = skipNext; 867 if( pCur->skipNext && pCur->eState==CURSOR_VALID ){ 868 pCur->eState = CURSOR_SKIPNEXT; 869 } 870 } 871 return rc; 872 } 873 874 #define restoreCursorPosition(p) \ 875 (p->eState>=CURSOR_REQUIRESEEK ? \ 876 btreeRestoreCursorPosition(p) : \ 877 SQLITE_OK) 878 879 /* 880 ** Determine whether or not a cursor has moved from the position where 881 ** it was last placed, or has been invalidated for any other reason. 882 ** Cursors can move when the row they are pointing at is deleted out 883 ** from under them, for example. Cursor might also move if a btree 884 ** is rebalanced. 885 ** 886 ** Calling this routine with a NULL cursor pointer returns false. 887 ** 888 ** Use the separate sqlite3BtreeCursorRestore() routine to restore a cursor 889 ** back to where it ought to be if this routine returns true. 890 */ 891 int sqlite3BtreeCursorHasMoved(BtCursor *pCur){ 892 assert( EIGHT_BYTE_ALIGNMENT(pCur) 893 || pCur==sqlite3BtreeFakeValidCursor() ); 894 assert( offsetof(BtCursor, eState)==0 ); 895 assert( sizeof(pCur->eState)==1 ); 896 return CURSOR_VALID != *(u8*)pCur; 897 } 898 899 /* 900 ** Return a pointer to a fake BtCursor object that will always answer 901 ** false to the sqlite3BtreeCursorHasMoved() routine above. The fake 902 ** cursor returned must not be used with any other Btree interface. 903 */ 904 BtCursor *sqlite3BtreeFakeValidCursor(void){ 905 static u8 fakeCursor = CURSOR_VALID; 906 assert( offsetof(BtCursor, eState)==0 ); 907 return (BtCursor*)&fakeCursor; 908 } 909 910 /* 911 ** This routine restores a cursor back to its original position after it 912 ** has been moved by some outside activity (such as a btree rebalance or 913 ** a row having been deleted out from under the cursor). 914 ** 915 ** On success, the *pDifferentRow parameter is false if the cursor is left 916 ** pointing at exactly the same row. *pDifferntRow is the row the cursor 917 ** was pointing to has been deleted, forcing the cursor to point to some 918 ** nearby row. 919 ** 920 ** This routine should only be called for a cursor that just returned 921 ** TRUE from sqlite3BtreeCursorHasMoved(). 922 */ 923 int sqlite3BtreeCursorRestore(BtCursor *pCur, int *pDifferentRow){ 924 int rc; 925 926 assert( pCur!=0 ); 927 assert( pCur->eState!=CURSOR_VALID ); 928 rc = restoreCursorPosition(pCur); 929 if( rc ){ 930 *pDifferentRow = 1; 931 return rc; 932 } 933 if( pCur->eState!=CURSOR_VALID ){ 934 *pDifferentRow = 1; 935 }else{ 936 *pDifferentRow = 0; 937 } 938 return SQLITE_OK; 939 } 940 941 #ifdef SQLITE_ENABLE_CURSOR_HINTS 942 /* 943 ** Provide hints to the cursor. The particular hint given (and the type 944 ** and number of the varargs parameters) is determined by the eHintType 945 ** parameter. See the definitions of the BTREE_HINT_* macros for details. 946 */ 947 void sqlite3BtreeCursorHint(BtCursor *pCur, int eHintType, ...){ 948 /* Used only by system that substitute their own storage engine */ 949 } 950 #endif 951 952 /* 953 ** Provide flag hints to the cursor. 954 */ 955 void sqlite3BtreeCursorHintFlags(BtCursor *pCur, unsigned x){ 956 assert( x==BTREE_SEEK_EQ || x==BTREE_BULKLOAD || x==0 ); 957 pCur->hints = x; 958 } 959 960 961 #ifndef SQLITE_OMIT_AUTOVACUUM 962 /* 963 ** Given a page number of a regular database page, return the page 964 ** number for the pointer-map page that contains the entry for the 965 ** input page number. 966 ** 967 ** Return 0 (not a valid page) for pgno==1 since there is 968 ** no pointer map associated with page 1. The integrity_check logic 969 ** requires that ptrmapPageno(*,1)!=1. 970 */ 971 static Pgno ptrmapPageno(BtShared *pBt, Pgno pgno){ 972 int nPagesPerMapPage; 973 Pgno iPtrMap, ret; 974 assert( sqlite3_mutex_held(pBt->mutex) ); 975 if( pgno<2 ) return 0; 976 nPagesPerMapPage = (pBt->usableSize/5)+1; 977 iPtrMap = (pgno-2)/nPagesPerMapPage; 978 ret = (iPtrMap*nPagesPerMapPage) + 2; 979 if( ret==PENDING_BYTE_PAGE(pBt) ){ 980 ret++; 981 } 982 return ret; 983 } 984 985 /* 986 ** Write an entry into the pointer map. 987 ** 988 ** This routine updates the pointer map entry for page number 'key' 989 ** so that it maps to type 'eType' and parent page number 'pgno'. 990 ** 991 ** If *pRC is initially non-zero (non-SQLITE_OK) then this routine is 992 ** a no-op. If an error occurs, the appropriate error code is written 993 ** into *pRC. 994 */ 995 static void ptrmapPut(BtShared *pBt, Pgno key, u8 eType, Pgno parent, int *pRC){ 996 DbPage *pDbPage; /* The pointer map page */ 997 u8 *pPtrmap; /* The pointer map data */ 998 Pgno iPtrmap; /* The pointer map page number */ 999 int offset; /* Offset in pointer map page */ 1000 int rc; /* Return code from subfunctions */ 1001 1002 if( *pRC ) return; 1003 1004 assert( sqlite3_mutex_held(pBt->mutex) ); 1005 /* The super-journal page number must never be used as a pointer map page */ 1006 assert( 0==PTRMAP_ISPAGE(pBt, PENDING_BYTE_PAGE(pBt)) ); 1007 1008 assert( pBt->autoVacuum ); 1009 if( key==0 ){ 1010 *pRC = SQLITE_CORRUPT_BKPT; 1011 return; 1012 } 1013 iPtrmap = PTRMAP_PAGENO(pBt, key); 1014 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0); 1015 if( rc!=SQLITE_OK ){ 1016 *pRC = rc; 1017 return; 1018 } 1019 if( ((char*)sqlite3PagerGetExtra(pDbPage))[0]!=0 ){ 1020 /* The first byte of the extra data is the MemPage.isInit byte. 1021 ** If that byte is set, it means this page is also being used 1022 ** as a btree page. */ 1023 *pRC = SQLITE_CORRUPT_BKPT; 1024 goto ptrmap_exit; 1025 } 1026 offset = PTRMAP_PTROFFSET(iPtrmap, key); 1027 if( offset<0 ){ 1028 *pRC = SQLITE_CORRUPT_BKPT; 1029 goto ptrmap_exit; 1030 } 1031 assert( offset <= (int)pBt->usableSize-5 ); 1032 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); 1033 1034 if( eType!=pPtrmap[offset] || get4byte(&pPtrmap[offset+1])!=parent ){ 1035 TRACE(("PTRMAP_UPDATE: %d->(%d,%d)\n", key, eType, parent)); 1036 *pRC= rc = sqlite3PagerWrite(pDbPage); 1037 if( rc==SQLITE_OK ){ 1038 pPtrmap[offset] = eType; 1039 put4byte(&pPtrmap[offset+1], parent); 1040 } 1041 } 1042 1043 ptrmap_exit: 1044 sqlite3PagerUnref(pDbPage); 1045 } 1046 1047 /* 1048 ** Read an entry from the pointer map. 1049 ** 1050 ** This routine retrieves the pointer map entry for page 'key', writing 1051 ** the type and parent page number to *pEType and *pPgno respectively. 1052 ** An error code is returned if something goes wrong, otherwise SQLITE_OK. 1053 */ 1054 static int ptrmapGet(BtShared *pBt, Pgno key, u8 *pEType, Pgno *pPgno){ 1055 DbPage *pDbPage; /* The pointer map page */ 1056 int iPtrmap; /* Pointer map page index */ 1057 u8 *pPtrmap; /* Pointer map page data */ 1058 int offset; /* Offset of entry in pointer map */ 1059 int rc; 1060 1061 assert( sqlite3_mutex_held(pBt->mutex) ); 1062 1063 iPtrmap = PTRMAP_PAGENO(pBt, key); 1064 rc = sqlite3PagerGet(pBt->pPager, iPtrmap, &pDbPage, 0); 1065 if( rc!=0 ){ 1066 return rc; 1067 } 1068 pPtrmap = (u8 *)sqlite3PagerGetData(pDbPage); 1069 1070 offset = PTRMAP_PTROFFSET(iPtrmap, key); 1071 if( offset<0 ){ 1072 sqlite3PagerUnref(pDbPage); 1073 return SQLITE_CORRUPT_BKPT; 1074 } 1075 assert( offset <= (int)pBt->usableSize-5 ); 1076 assert( pEType!=0 ); 1077 *pEType = pPtrmap[offset]; 1078 if( pPgno ) *pPgno = get4byte(&pPtrmap[offset+1]); 1079 1080 sqlite3PagerUnref(pDbPage); 1081 if( *pEType<1 || *pEType>5 ) return SQLITE_CORRUPT_PGNO(iPtrmap); 1082 return SQLITE_OK; 1083 } 1084 1085 #else /* if defined SQLITE_OMIT_AUTOVACUUM */ 1086 #define ptrmapPut(w,x,y,z,rc) 1087 #define ptrmapGet(w,x,y,z) SQLITE_OK 1088 #define ptrmapPutOvflPtr(x, y, z, rc) 1089 #endif 1090 1091 /* 1092 ** Given a btree page and a cell index (0 means the first cell on 1093 ** the page, 1 means the second cell, and so forth) return a pointer 1094 ** to the cell content. 1095 ** 1096 ** findCellPastPtr() does the same except it skips past the initial 1097 ** 4-byte child pointer found on interior pages, if there is one. 1098 ** 1099 ** This routine works only for pages that do not contain overflow cells. 1100 */ 1101 #define findCell(P,I) \ 1102 ((P)->aData + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)]))) 1103 #define findCellPastPtr(P,I) \ 1104 ((P)->aDataOfst + ((P)->maskPage & get2byteAligned(&(P)->aCellIdx[2*(I)]))) 1105 1106 1107 /* 1108 ** This is common tail processing for btreeParseCellPtr() and 1109 ** btreeParseCellPtrIndex() for the case when the cell does not fit entirely 1110 ** on a single B-tree page. Make necessary adjustments to the CellInfo 1111 ** structure. 1112 */ 1113 static SQLITE_NOINLINE void btreeParseCellAdjustSizeForOverflow( 1114 MemPage *pPage, /* Page containing the cell */ 1115 u8 *pCell, /* Pointer to the cell text. */ 1116 CellInfo *pInfo /* Fill in this structure */ 1117 ){ 1118 /* If the payload will not fit completely on the local page, we have 1119 ** to decide how much to store locally and how much to spill onto 1120 ** overflow pages. The strategy is to minimize the amount of unused 1121 ** space on overflow pages while keeping the amount of local storage 1122 ** in between minLocal and maxLocal. 1123 ** 1124 ** Warning: changing the way overflow payload is distributed in any 1125 ** way will result in an incompatible file format. 1126 */ 1127 int minLocal; /* Minimum amount of payload held locally */ 1128 int maxLocal; /* Maximum amount of payload held locally */ 1129 int surplus; /* Overflow payload available for local storage */ 1130 1131 minLocal = pPage->minLocal; 1132 maxLocal = pPage->maxLocal; 1133 surplus = minLocal + (pInfo->nPayload - minLocal)%(pPage->pBt->usableSize-4); 1134 testcase( surplus==maxLocal ); 1135 testcase( surplus==maxLocal+1 ); 1136 if( surplus <= maxLocal ){ 1137 pInfo->nLocal = (u16)surplus; 1138 }else{ 1139 pInfo->nLocal = (u16)minLocal; 1140 } 1141 pInfo->nSize = (u16)(&pInfo->pPayload[pInfo->nLocal] - pCell) + 4; 1142 } 1143 1144 /* 1145 ** Given a record with nPayload bytes of payload stored within btree 1146 ** page pPage, return the number of bytes of payload stored locally. 1147 */ 1148 static int btreePayloadToLocal(MemPage *pPage, i64 nPayload){ 1149 int maxLocal; /* Maximum amount of payload held locally */ 1150 maxLocal = pPage->maxLocal; 1151 if( nPayload<=maxLocal ){ 1152 return nPayload; 1153 }else{ 1154 int minLocal; /* Minimum amount of payload held locally */ 1155 int surplus; /* Overflow payload available for local storage */ 1156 minLocal = pPage->minLocal; 1157 surplus = minLocal + (nPayload - minLocal)%(pPage->pBt->usableSize-4); 1158 return ( surplus <= maxLocal ) ? surplus : minLocal; 1159 } 1160 } 1161 1162 /* 1163 ** The following routines are implementations of the MemPage.xParseCell() 1164 ** method. 1165 ** 1166 ** Parse a cell content block and fill in the CellInfo structure. 1167 ** 1168 ** btreeParseCellPtr() => table btree leaf nodes 1169 ** btreeParseCellNoPayload() => table btree internal nodes 1170 ** btreeParseCellPtrIndex() => index btree nodes 1171 ** 1172 ** There is also a wrapper function btreeParseCell() that works for 1173 ** all MemPage types and that references the cell by index rather than 1174 ** by pointer. 1175 */ 1176 static void btreeParseCellPtrNoPayload( 1177 MemPage *pPage, /* Page containing the cell */ 1178 u8 *pCell, /* Pointer to the cell text. */ 1179 CellInfo *pInfo /* Fill in this structure */ 1180 ){ 1181 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 1182 assert( pPage->leaf==0 ); 1183 assert( pPage->childPtrSize==4 ); 1184 #ifndef SQLITE_DEBUG 1185 UNUSED_PARAMETER(pPage); 1186 #endif 1187 pInfo->nSize = 4 + getVarint(&pCell[4], (u64*)&pInfo->nKey); 1188 pInfo->nPayload = 0; 1189 pInfo->nLocal = 0; 1190 pInfo->pPayload = 0; 1191 return; 1192 } 1193 static void btreeParseCellPtr( 1194 MemPage *pPage, /* Page containing the cell */ 1195 u8 *pCell, /* Pointer to the cell text. */ 1196 CellInfo *pInfo /* Fill in this structure */ 1197 ){ 1198 u8 *pIter; /* For scanning through pCell */ 1199 u32 nPayload; /* Number of bytes of cell payload */ 1200 u64 iKey; /* Extracted Key value */ 1201 1202 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 1203 assert( pPage->leaf==0 || pPage->leaf==1 ); 1204 assert( pPage->intKeyLeaf ); 1205 assert( pPage->childPtrSize==0 ); 1206 pIter = pCell; 1207 1208 /* The next block of code is equivalent to: 1209 ** 1210 ** pIter += getVarint32(pIter, nPayload); 1211 ** 1212 ** The code is inlined to avoid a function call. 1213 */ 1214 nPayload = *pIter; 1215 if( nPayload>=0x80 ){ 1216 u8 *pEnd = &pIter[8]; 1217 nPayload &= 0x7f; 1218 do{ 1219 nPayload = (nPayload<<7) | (*++pIter & 0x7f); 1220 }while( (*pIter)>=0x80 && pIter<pEnd ); 1221 } 1222 pIter++; 1223 1224 /* The next block of code is equivalent to: 1225 ** 1226 ** pIter += getVarint(pIter, (u64*)&pInfo->nKey); 1227 ** 1228 ** The code is inlined to avoid a function call. 1229 */ 1230 iKey = *pIter; 1231 if( iKey>=0x80 ){ 1232 u8 *pEnd = &pIter[7]; 1233 iKey &= 0x7f; 1234 while(1){ 1235 iKey = (iKey<<7) | (*++pIter & 0x7f); 1236 if( (*pIter)<0x80 ) break; 1237 if( pIter>=pEnd ){ 1238 iKey = (iKey<<8) | *++pIter; 1239 break; 1240 } 1241 } 1242 } 1243 pIter++; 1244 1245 pInfo->nKey = *(i64*)&iKey; 1246 pInfo->nPayload = nPayload; 1247 pInfo->pPayload = pIter; 1248 testcase( nPayload==pPage->maxLocal ); 1249 testcase( nPayload==(u32)pPage->maxLocal+1 ); 1250 if( nPayload<=pPage->maxLocal ){ 1251 /* This is the (easy) common case where the entire payload fits 1252 ** on the local page. No overflow is required. 1253 */ 1254 pInfo->nSize = nPayload + (u16)(pIter - pCell); 1255 if( pInfo->nSize<4 ) pInfo->nSize = 4; 1256 pInfo->nLocal = (u16)nPayload; 1257 }else{ 1258 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo); 1259 } 1260 } 1261 static void btreeParseCellPtrIndex( 1262 MemPage *pPage, /* Page containing the cell */ 1263 u8 *pCell, /* Pointer to the cell text. */ 1264 CellInfo *pInfo /* Fill in this structure */ 1265 ){ 1266 u8 *pIter; /* For scanning through pCell */ 1267 u32 nPayload; /* Number of bytes of cell payload */ 1268 1269 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 1270 assert( pPage->leaf==0 || pPage->leaf==1 ); 1271 assert( pPage->intKeyLeaf==0 ); 1272 pIter = pCell + pPage->childPtrSize; 1273 nPayload = *pIter; 1274 if( nPayload>=0x80 ){ 1275 u8 *pEnd = &pIter[8]; 1276 nPayload &= 0x7f; 1277 do{ 1278 nPayload = (nPayload<<7) | (*++pIter & 0x7f); 1279 }while( *(pIter)>=0x80 && pIter<pEnd ); 1280 } 1281 pIter++; 1282 pInfo->nKey = nPayload; 1283 pInfo->nPayload = nPayload; 1284 pInfo->pPayload = pIter; 1285 testcase( nPayload==pPage->maxLocal ); 1286 testcase( nPayload==(u32)pPage->maxLocal+1 ); 1287 if( nPayload<=pPage->maxLocal ){ 1288 /* This is the (easy) common case where the entire payload fits 1289 ** on the local page. No overflow is required. 1290 */ 1291 pInfo->nSize = nPayload + (u16)(pIter - pCell); 1292 if( pInfo->nSize<4 ) pInfo->nSize = 4; 1293 pInfo->nLocal = (u16)nPayload; 1294 }else{ 1295 btreeParseCellAdjustSizeForOverflow(pPage, pCell, pInfo); 1296 } 1297 } 1298 static void btreeParseCell( 1299 MemPage *pPage, /* Page containing the cell */ 1300 int iCell, /* The cell index. First cell is 0 */ 1301 CellInfo *pInfo /* Fill in this structure */ 1302 ){ 1303 pPage->xParseCell(pPage, findCell(pPage, iCell), pInfo); 1304 } 1305 1306 /* 1307 ** The following routines are implementations of the MemPage.xCellSize 1308 ** method. 1309 ** 1310 ** Compute the total number of bytes that a Cell needs in the cell 1311 ** data area of the btree-page. The return number includes the cell 1312 ** data header and the local payload, but not any overflow page or 1313 ** the space used by the cell pointer. 1314 ** 1315 ** cellSizePtrNoPayload() => table internal nodes 1316 ** cellSizePtr() => all index nodes & table leaf nodes 1317 */ 1318 static u16 cellSizePtr(MemPage *pPage, u8 *pCell){ 1319 u8 *pIter = pCell + pPage->childPtrSize; /* For looping over bytes of pCell */ 1320 u8 *pEnd; /* End mark for a varint */ 1321 u32 nSize; /* Size value to return */ 1322 1323 #ifdef SQLITE_DEBUG 1324 /* The value returned by this function should always be the same as 1325 ** the (CellInfo.nSize) value found by doing a full parse of the 1326 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of 1327 ** this function verifies that this invariant is not violated. */ 1328 CellInfo debuginfo; 1329 pPage->xParseCell(pPage, pCell, &debuginfo); 1330 #endif 1331 1332 nSize = *pIter; 1333 if( nSize>=0x80 ){ 1334 pEnd = &pIter[8]; 1335 nSize &= 0x7f; 1336 do{ 1337 nSize = (nSize<<7) | (*++pIter & 0x7f); 1338 }while( *(pIter)>=0x80 && pIter<pEnd ); 1339 } 1340 pIter++; 1341 if( pPage->intKey ){ 1342 /* pIter now points at the 64-bit integer key value, a variable length 1343 ** integer. The following block moves pIter to point at the first byte 1344 ** past the end of the key value. */ 1345 pEnd = &pIter[9]; 1346 while( (*pIter++)&0x80 && pIter<pEnd ); 1347 } 1348 testcase( nSize==pPage->maxLocal ); 1349 testcase( nSize==(u32)pPage->maxLocal+1 ); 1350 if( nSize<=pPage->maxLocal ){ 1351 nSize += (u32)(pIter - pCell); 1352 if( nSize<4 ) nSize = 4; 1353 }else{ 1354 int minLocal = pPage->minLocal; 1355 nSize = minLocal + (nSize - minLocal) % (pPage->pBt->usableSize - 4); 1356 testcase( nSize==pPage->maxLocal ); 1357 testcase( nSize==(u32)pPage->maxLocal+1 ); 1358 if( nSize>pPage->maxLocal ){ 1359 nSize = minLocal; 1360 } 1361 nSize += 4 + (u16)(pIter - pCell); 1362 } 1363 assert( nSize==debuginfo.nSize || CORRUPT_DB ); 1364 return (u16)nSize; 1365 } 1366 static u16 cellSizePtrNoPayload(MemPage *pPage, u8 *pCell){ 1367 u8 *pIter = pCell + 4; /* For looping over bytes of pCell */ 1368 u8 *pEnd; /* End mark for a varint */ 1369 1370 #ifdef SQLITE_DEBUG 1371 /* The value returned by this function should always be the same as 1372 ** the (CellInfo.nSize) value found by doing a full parse of the 1373 ** cell. If SQLITE_DEBUG is defined, an assert() at the bottom of 1374 ** this function verifies that this invariant is not violated. */ 1375 CellInfo debuginfo; 1376 pPage->xParseCell(pPage, pCell, &debuginfo); 1377 #else 1378 UNUSED_PARAMETER(pPage); 1379 #endif 1380 1381 assert( pPage->childPtrSize==4 ); 1382 pEnd = pIter + 9; 1383 while( (*pIter++)&0x80 && pIter<pEnd ); 1384 assert( debuginfo.nSize==(u16)(pIter - pCell) || CORRUPT_DB ); 1385 return (u16)(pIter - pCell); 1386 } 1387 1388 1389 #ifdef SQLITE_DEBUG 1390 /* This variation on cellSizePtr() is used inside of assert() statements 1391 ** only. */ 1392 static u16 cellSize(MemPage *pPage, int iCell){ 1393 return pPage->xCellSize(pPage, findCell(pPage, iCell)); 1394 } 1395 #endif 1396 1397 #ifndef SQLITE_OMIT_AUTOVACUUM 1398 /* 1399 ** The cell pCell is currently part of page pSrc but will ultimately be part 1400 ** of pPage. (pSrc and pPager are often the same.) If pCell contains a 1401 ** pointer to an overflow page, insert an entry into the pointer-map for 1402 ** the overflow page that will be valid after pCell has been moved to pPage. 1403 */ 1404 static void ptrmapPutOvflPtr(MemPage *pPage, MemPage *pSrc, u8 *pCell,int *pRC){ 1405 CellInfo info; 1406 if( *pRC ) return; 1407 assert( pCell!=0 ); 1408 pPage->xParseCell(pPage, pCell, &info); 1409 if( info.nLocal<info.nPayload ){ 1410 Pgno ovfl; 1411 if( SQLITE_WITHIN(pSrc->aDataEnd, pCell, pCell+info.nLocal) ){ 1412 testcase( pSrc!=pPage ); 1413 *pRC = SQLITE_CORRUPT_BKPT; 1414 return; 1415 } 1416 ovfl = get4byte(&pCell[info.nSize-4]); 1417 ptrmapPut(pPage->pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, pRC); 1418 } 1419 } 1420 #endif 1421 1422 1423 /* 1424 ** Defragment the page given. This routine reorganizes cells within the 1425 ** page so that there are no free-blocks on the free-block list. 1426 ** 1427 ** Parameter nMaxFrag is the maximum amount of fragmented space that may be 1428 ** present in the page after this routine returns. 1429 ** 1430 ** EVIDENCE-OF: R-44582-60138 SQLite may from time to time reorganize a 1431 ** b-tree page so that there are no freeblocks or fragment bytes, all 1432 ** unused bytes are contained in the unallocated space region, and all 1433 ** cells are packed tightly at the end of the page. 1434 */ 1435 static int defragmentPage(MemPage *pPage, int nMaxFrag){ 1436 int i; /* Loop counter */ 1437 int pc; /* Address of the i-th cell */ 1438 int hdr; /* Offset to the page header */ 1439 int size; /* Size of a cell */ 1440 int usableSize; /* Number of usable bytes on a page */ 1441 int cellOffset; /* Offset to the cell pointer array */ 1442 int cbrk; /* Offset to the cell content area */ 1443 int nCell; /* Number of cells on the page */ 1444 unsigned char *data; /* The page data */ 1445 unsigned char *temp; /* Temp area for cell content */ 1446 unsigned char *src; /* Source of content */ 1447 int iCellFirst; /* First allowable cell index */ 1448 int iCellLast; /* Last possible cell index */ 1449 int iCellStart; /* First cell offset in input */ 1450 1451 assert( sqlite3PagerIswriteable(pPage->pDbPage) ); 1452 assert( pPage->pBt!=0 ); 1453 assert( pPage->pBt->usableSize <= SQLITE_MAX_PAGE_SIZE ); 1454 assert( pPage->nOverflow==0 ); 1455 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 1456 temp = 0; 1457 src = data = pPage->aData; 1458 hdr = pPage->hdrOffset; 1459 cellOffset = pPage->cellOffset; 1460 nCell = pPage->nCell; 1461 assert( nCell==get2byte(&data[hdr+3]) || CORRUPT_DB ); 1462 iCellFirst = cellOffset + 2*nCell; 1463 usableSize = pPage->pBt->usableSize; 1464 1465 /* This block handles pages with two or fewer free blocks and nMaxFrag 1466 ** or fewer fragmented bytes. In this case it is faster to move the 1467 ** two (or one) blocks of cells using memmove() and add the required 1468 ** offsets to each pointer in the cell-pointer array than it is to 1469 ** reconstruct the entire page. */ 1470 if( (int)data[hdr+7]<=nMaxFrag ){ 1471 int iFree = get2byte(&data[hdr+1]); 1472 if( iFree>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage); 1473 if( iFree ){ 1474 int iFree2 = get2byte(&data[iFree]); 1475 if( iFree2>usableSize-4 ) return SQLITE_CORRUPT_PAGE(pPage); 1476 if( 0==iFree2 || (data[iFree2]==0 && data[iFree2+1]==0) ){ 1477 u8 *pEnd = &data[cellOffset + nCell*2]; 1478 u8 *pAddr; 1479 int sz2 = 0; 1480 int sz = get2byte(&data[iFree+2]); 1481 int top = get2byte(&data[hdr+5]); 1482 if( top>=iFree ){ 1483 return SQLITE_CORRUPT_PAGE(pPage); 1484 } 1485 if( iFree2 ){ 1486 if( iFree+sz>iFree2 ) return SQLITE_CORRUPT_PAGE(pPage); 1487 sz2 = get2byte(&data[iFree2+2]); 1488 if( iFree2+sz2 > usableSize ) return SQLITE_CORRUPT_PAGE(pPage); 1489 memmove(&data[iFree+sz+sz2], &data[iFree+sz], iFree2-(iFree+sz)); 1490 sz += sz2; 1491 }else if( NEVER(iFree+sz>usableSize) ){ 1492 return SQLITE_CORRUPT_PAGE(pPage); 1493 } 1494 1495 cbrk = top+sz; 1496 assert( cbrk+(iFree-top) <= usableSize ); 1497 memmove(&data[cbrk], &data[top], iFree-top); 1498 for(pAddr=&data[cellOffset]; pAddr<pEnd; pAddr+=2){ 1499 pc = get2byte(pAddr); 1500 if( pc<iFree ){ put2byte(pAddr, pc+sz); } 1501 else if( pc<iFree2 ){ put2byte(pAddr, pc+sz2); } 1502 } 1503 goto defragment_out; 1504 } 1505 } 1506 } 1507 1508 cbrk = usableSize; 1509 iCellLast = usableSize - 4; 1510 iCellStart = get2byte(&data[hdr+5]); 1511 for(i=0; i<nCell; i++){ 1512 u8 *pAddr; /* The i-th cell pointer */ 1513 pAddr = &data[cellOffset + i*2]; 1514 pc = get2byte(pAddr); 1515 testcase( pc==iCellFirst ); 1516 testcase( pc==iCellLast ); 1517 /* These conditions have already been verified in btreeInitPage() 1518 ** if PRAGMA cell_size_check=ON. 1519 */ 1520 if( pc<iCellStart || pc>iCellLast ){ 1521 return SQLITE_CORRUPT_PAGE(pPage); 1522 } 1523 assert( pc>=iCellStart && pc<=iCellLast ); 1524 size = pPage->xCellSize(pPage, &src[pc]); 1525 cbrk -= size; 1526 if( cbrk<iCellStart || pc+size>usableSize ){ 1527 return SQLITE_CORRUPT_PAGE(pPage); 1528 } 1529 assert( cbrk+size<=usableSize && cbrk>=iCellStart ); 1530 testcase( cbrk+size==usableSize ); 1531 testcase( pc+size==usableSize ); 1532 put2byte(pAddr, cbrk); 1533 if( temp==0 ){ 1534 if( cbrk==pc ) continue; 1535 temp = sqlite3PagerTempSpace(pPage->pBt->pPager); 1536 memcpy(&temp[iCellStart], &data[iCellStart], usableSize - iCellStart); 1537 src = temp; 1538 } 1539 memcpy(&data[cbrk], &src[pc], size); 1540 } 1541 data[hdr+7] = 0; 1542 1543 defragment_out: 1544 assert( pPage->nFree>=0 ); 1545 if( data[hdr+7]+cbrk-iCellFirst!=pPage->nFree ){ 1546 return SQLITE_CORRUPT_PAGE(pPage); 1547 } 1548 assert( cbrk>=iCellFirst ); 1549 put2byte(&data[hdr+5], cbrk); 1550 data[hdr+1] = 0; 1551 data[hdr+2] = 0; 1552 memset(&data[iCellFirst], 0, cbrk-iCellFirst); 1553 assert( sqlite3PagerIswriteable(pPage->pDbPage) ); 1554 return SQLITE_OK; 1555 } 1556 1557 /* 1558 ** Search the free-list on page pPg for space to store a cell nByte bytes in 1559 ** size. If one can be found, return a pointer to the space and remove it 1560 ** from the free-list. 1561 ** 1562 ** If no suitable space can be found on the free-list, return NULL. 1563 ** 1564 ** This function may detect corruption within pPg. If corruption is 1565 ** detected then *pRc is set to SQLITE_CORRUPT and NULL is returned. 1566 ** 1567 ** Slots on the free list that are between 1 and 3 bytes larger than nByte 1568 ** will be ignored if adding the extra space to the fragmentation count 1569 ** causes the fragmentation count to exceed 60. 1570 */ 1571 static u8 *pageFindSlot(MemPage *pPg, int nByte, int *pRc){ 1572 const int hdr = pPg->hdrOffset; /* Offset to page header */ 1573 u8 * const aData = pPg->aData; /* Page data */ 1574 int iAddr = hdr + 1; /* Address of ptr to pc */ 1575 int pc = get2byte(&aData[iAddr]); /* Address of a free slot */ 1576 int x; /* Excess size of the slot */ 1577 int maxPC = pPg->pBt->usableSize - nByte; /* Max address for a usable slot */ 1578 int size; /* Size of the free slot */ 1579 1580 assert( pc>0 ); 1581 while( pc<=maxPC ){ 1582 /* EVIDENCE-OF: R-22710-53328 The third and fourth bytes of each 1583 ** freeblock form a big-endian integer which is the size of the freeblock 1584 ** in bytes, including the 4-byte header. */ 1585 size = get2byte(&aData[pc+2]); 1586 if( (x = size - nByte)>=0 ){ 1587 testcase( x==4 ); 1588 testcase( x==3 ); 1589 if( x<4 ){ 1590 /* EVIDENCE-OF: R-11498-58022 In a well-formed b-tree page, the total 1591 ** number of bytes in fragments may not exceed 60. */ 1592 if( aData[hdr+7]>57 ) return 0; 1593 1594 /* Remove the slot from the free-list. Update the number of 1595 ** fragmented bytes within the page. */ 1596 memcpy(&aData[iAddr], &aData[pc], 2); 1597 aData[hdr+7] += (u8)x; 1598 }else if( x+pc > maxPC ){ 1599 /* This slot extends off the end of the usable part of the page */ 1600 *pRc = SQLITE_CORRUPT_PAGE(pPg); 1601 return 0; 1602 }else{ 1603 /* The slot remains on the free-list. Reduce its size to account 1604 ** for the portion used by the new allocation. */ 1605 put2byte(&aData[pc+2], x); 1606 } 1607 return &aData[pc + x]; 1608 } 1609 iAddr = pc; 1610 pc = get2byte(&aData[pc]); 1611 if( pc<=iAddr+size ){ 1612 if( pc ){ 1613 /* The next slot in the chain is not past the end of the current slot */ 1614 *pRc = SQLITE_CORRUPT_PAGE(pPg); 1615 } 1616 return 0; 1617 } 1618 } 1619 if( pc>maxPC+nByte-4 ){ 1620 /* The free slot chain extends off the end of the page */ 1621 *pRc = SQLITE_CORRUPT_PAGE(pPg); 1622 } 1623 return 0; 1624 } 1625 1626 /* 1627 ** Allocate nByte bytes of space from within the B-Tree page passed 1628 ** as the first argument. Write into *pIdx the index into pPage->aData[] 1629 ** of the first byte of allocated space. Return either SQLITE_OK or 1630 ** an error code (usually SQLITE_CORRUPT). 1631 ** 1632 ** The caller guarantees that there is sufficient space to make the 1633 ** allocation. This routine might need to defragment in order to bring 1634 ** all the space together, however. This routine will avoid using 1635 ** the first two bytes past the cell pointer area since presumably this 1636 ** allocation is being made in order to insert a new cell, so we will 1637 ** also end up needing a new cell pointer. 1638 */ 1639 static int allocateSpace(MemPage *pPage, int nByte, int *pIdx){ 1640 const int hdr = pPage->hdrOffset; /* Local cache of pPage->hdrOffset */ 1641 u8 * const data = pPage->aData; /* Local cache of pPage->aData */ 1642 int top; /* First byte of cell content area */ 1643 int rc = SQLITE_OK; /* Integer return code */ 1644 int gap; /* First byte of gap between cell pointers and cell content */ 1645 1646 assert( sqlite3PagerIswriteable(pPage->pDbPage) ); 1647 assert( pPage->pBt ); 1648 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 1649 assert( nByte>=0 ); /* Minimum cell size is 4 */ 1650 assert( pPage->nFree>=nByte ); 1651 assert( pPage->nOverflow==0 ); 1652 assert( nByte < (int)(pPage->pBt->usableSize-8) ); 1653 1654 assert( pPage->cellOffset == hdr + 12 - 4*pPage->leaf ); 1655 gap = pPage->cellOffset + 2*pPage->nCell; 1656 assert( gap<=65536 ); 1657 /* EVIDENCE-OF: R-29356-02391 If the database uses a 65536-byte page size 1658 ** and the reserved space is zero (the usual value for reserved space) 1659 ** then the cell content offset of an empty page wants to be 65536. 1660 ** However, that integer is too large to be stored in a 2-byte unsigned 1661 ** integer, so a value of 0 is used in its place. */ 1662 top = get2byte(&data[hdr+5]); 1663 assert( top<=(int)pPage->pBt->usableSize ); /* by btreeComputeFreeSpace() */ 1664 if( gap>top ){ 1665 if( top==0 && pPage->pBt->usableSize==65536 ){ 1666 top = 65536; 1667 }else{ 1668 return SQLITE_CORRUPT_PAGE(pPage); 1669 } 1670 } 1671 1672 /* If there is enough space between gap and top for one more cell pointer, 1673 ** and if the freelist is not empty, then search the 1674 ** freelist looking for a slot big enough to satisfy the request. 1675 */ 1676 testcase( gap+2==top ); 1677 testcase( gap+1==top ); 1678 testcase( gap==top ); 1679 if( (data[hdr+2] || data[hdr+1]) && gap+2<=top ){ 1680 u8 *pSpace = pageFindSlot(pPage, nByte, &rc); 1681 if( pSpace ){ 1682 int g2; 1683 assert( pSpace+nByte<=data+pPage->pBt->usableSize ); 1684 *pIdx = g2 = (int)(pSpace-data); 1685 if( g2<=gap ){ 1686 return SQLITE_CORRUPT_PAGE(pPage); 1687 }else{ 1688 return SQLITE_OK; 1689 } 1690 }else if( rc ){ 1691 return rc; 1692 } 1693 } 1694 1695 /* The request could not be fulfilled using a freelist slot. Check 1696 ** to see if defragmentation is necessary. 1697 */ 1698 testcase( gap+2+nByte==top ); 1699 if( gap+2+nByte>top ){ 1700 assert( pPage->nCell>0 || CORRUPT_DB ); 1701 assert( pPage->nFree>=0 ); 1702 rc = defragmentPage(pPage, MIN(4, pPage->nFree - (2+nByte))); 1703 if( rc ) return rc; 1704 top = get2byteNotZero(&data[hdr+5]); 1705 assert( gap+2+nByte<=top ); 1706 } 1707 1708 1709 /* Allocate memory from the gap in between the cell pointer array 1710 ** and the cell content area. The btreeComputeFreeSpace() call has already 1711 ** validated the freelist. Given that the freelist is valid, there 1712 ** is no way that the allocation can extend off the end of the page. 1713 ** The assert() below verifies the previous sentence. 1714 */ 1715 top -= nByte; 1716 put2byte(&data[hdr+5], top); 1717 assert( top+nByte <= (int)pPage->pBt->usableSize ); 1718 *pIdx = top; 1719 return SQLITE_OK; 1720 } 1721 1722 /* 1723 ** Return a section of the pPage->aData to the freelist. 1724 ** The first byte of the new free block is pPage->aData[iStart] 1725 ** and the size of the block is iSize bytes. 1726 ** 1727 ** Adjacent freeblocks are coalesced. 1728 ** 1729 ** Even though the freeblock list was checked by btreeComputeFreeSpace(), 1730 ** that routine will not detect overlap between cells or freeblocks. Nor 1731 ** does it detect cells or freeblocks that encrouch into the reserved bytes 1732 ** at the end of the page. So do additional corruption checks inside this 1733 ** routine and return SQLITE_CORRUPT if any problems are found. 1734 */ 1735 static int freeSpace(MemPage *pPage, u16 iStart, u16 iSize){ 1736 u16 iPtr; /* Address of ptr to next freeblock */ 1737 u16 iFreeBlk; /* Address of the next freeblock */ 1738 u8 hdr; /* Page header size. 0 or 100 */ 1739 u8 nFrag = 0; /* Reduction in fragmentation */ 1740 u16 iOrigSize = iSize; /* Original value of iSize */ 1741 u16 x; /* Offset to cell content area */ 1742 u32 iEnd = iStart + iSize; /* First byte past the iStart buffer */ 1743 unsigned char *data = pPage->aData; /* Page content */ 1744 1745 assert( pPage->pBt!=0 ); 1746 assert( sqlite3PagerIswriteable(pPage->pDbPage) ); 1747 assert( CORRUPT_DB || iStart>=pPage->hdrOffset+6+pPage->childPtrSize ); 1748 assert( CORRUPT_DB || iEnd <= pPage->pBt->usableSize ); 1749 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 1750 assert( iSize>=4 ); /* Minimum cell size is 4 */ 1751 assert( iStart<=pPage->pBt->usableSize-4 ); 1752 1753 /* The list of freeblocks must be in ascending order. Find the 1754 ** spot on the list where iStart should be inserted. 1755 */ 1756 hdr = pPage->hdrOffset; 1757 iPtr = hdr + 1; 1758 if( data[iPtr+1]==0 && data[iPtr]==0 ){ 1759 iFreeBlk = 0; /* Shortcut for the case when the freelist is empty */ 1760 }else{ 1761 while( (iFreeBlk = get2byte(&data[iPtr]))<iStart ){ 1762 if( iFreeBlk<iPtr+4 ){ 1763 if( iFreeBlk==0 ) break; /* TH3: corrupt082.100 */ 1764 return SQLITE_CORRUPT_PAGE(pPage); 1765 } 1766 iPtr = iFreeBlk; 1767 } 1768 if( iFreeBlk>pPage->pBt->usableSize-4 ){ /* TH3: corrupt081.100 */ 1769 return SQLITE_CORRUPT_PAGE(pPage); 1770 } 1771 assert( iFreeBlk>iPtr || iFreeBlk==0 ); 1772 1773 /* At this point: 1774 ** iFreeBlk: First freeblock after iStart, or zero if none 1775 ** iPtr: The address of a pointer to iFreeBlk 1776 ** 1777 ** Check to see if iFreeBlk should be coalesced onto the end of iStart. 1778 */ 1779 if( iFreeBlk && iEnd+3>=iFreeBlk ){ 1780 nFrag = iFreeBlk - iEnd; 1781 if( iEnd>iFreeBlk ) return SQLITE_CORRUPT_PAGE(pPage); 1782 iEnd = iFreeBlk + get2byte(&data[iFreeBlk+2]); 1783 if( iEnd > pPage->pBt->usableSize ){ 1784 return SQLITE_CORRUPT_PAGE(pPage); 1785 } 1786 iSize = iEnd - iStart; 1787 iFreeBlk = get2byte(&data[iFreeBlk]); 1788 } 1789 1790 /* If iPtr is another freeblock (that is, if iPtr is not the freelist 1791 ** pointer in the page header) then check to see if iStart should be 1792 ** coalesced onto the end of iPtr. 1793 */ 1794 if( iPtr>hdr+1 ){ 1795 int iPtrEnd = iPtr + get2byte(&data[iPtr+2]); 1796 if( iPtrEnd+3>=iStart ){ 1797 if( iPtrEnd>iStart ) return SQLITE_CORRUPT_PAGE(pPage); 1798 nFrag += iStart - iPtrEnd; 1799 iSize = iEnd - iPtr; 1800 iStart = iPtr; 1801 } 1802 } 1803 if( nFrag>data[hdr+7] ) return SQLITE_CORRUPT_PAGE(pPage); 1804 data[hdr+7] -= nFrag; 1805 } 1806 x = get2byte(&data[hdr+5]); 1807 if( iStart<=x ){ 1808 /* The new freeblock is at the beginning of the cell content area, 1809 ** so just extend the cell content area rather than create another 1810 ** freelist entry */ 1811 if( iStart<x ) return SQLITE_CORRUPT_PAGE(pPage); 1812 if( iPtr!=hdr+1 ) return SQLITE_CORRUPT_PAGE(pPage); 1813 put2byte(&data[hdr+1], iFreeBlk); 1814 put2byte(&data[hdr+5], iEnd); 1815 }else{ 1816 /* Insert the new freeblock into the freelist */ 1817 put2byte(&data[iPtr], iStart); 1818 } 1819 if( pPage->pBt->btsFlags & BTS_FAST_SECURE ){ 1820 /* Overwrite deleted information with zeros when the secure_delete 1821 ** option is enabled */ 1822 memset(&data[iStart], 0, iSize); 1823 } 1824 put2byte(&data[iStart], iFreeBlk); 1825 put2byte(&data[iStart+2], iSize); 1826 pPage->nFree += iOrigSize; 1827 return SQLITE_OK; 1828 } 1829 1830 /* 1831 ** Decode the flags byte (the first byte of the header) for a page 1832 ** and initialize fields of the MemPage structure accordingly. 1833 ** 1834 ** Only the following combinations are supported. Anything different 1835 ** indicates a corrupt database files: 1836 ** 1837 ** PTF_ZERODATA 1838 ** PTF_ZERODATA | PTF_LEAF 1839 ** PTF_LEAFDATA | PTF_INTKEY 1840 ** PTF_LEAFDATA | PTF_INTKEY | PTF_LEAF 1841 */ 1842 static int decodeFlags(MemPage *pPage, int flagByte){ 1843 BtShared *pBt; /* A copy of pPage->pBt */ 1844 1845 assert( pPage->hdrOffset==(pPage->pgno==1 ? 100 : 0) ); 1846 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 1847 pPage->leaf = (u8)(flagByte>>3); assert( PTF_LEAF == 1<<3 ); 1848 flagByte &= ~PTF_LEAF; 1849 pPage->childPtrSize = 4-4*pPage->leaf; 1850 pPage->xCellSize = cellSizePtr; 1851 pBt = pPage->pBt; 1852 if( flagByte==(PTF_LEAFDATA | PTF_INTKEY) ){ 1853 /* EVIDENCE-OF: R-07291-35328 A value of 5 (0x05) means the page is an 1854 ** interior table b-tree page. */ 1855 assert( (PTF_LEAFDATA|PTF_INTKEY)==5 ); 1856 /* EVIDENCE-OF: R-26900-09176 A value of 13 (0x0d) means the page is a 1857 ** leaf table b-tree page. */ 1858 assert( (PTF_LEAFDATA|PTF_INTKEY|PTF_LEAF)==13 ); 1859 pPage->intKey = 1; 1860 if( pPage->leaf ){ 1861 pPage->intKeyLeaf = 1; 1862 pPage->xParseCell = btreeParseCellPtr; 1863 }else{ 1864 pPage->intKeyLeaf = 0; 1865 pPage->xCellSize = cellSizePtrNoPayload; 1866 pPage->xParseCell = btreeParseCellPtrNoPayload; 1867 } 1868 pPage->maxLocal = pBt->maxLeaf; 1869 pPage->minLocal = pBt->minLeaf; 1870 }else if( flagByte==PTF_ZERODATA ){ 1871 /* EVIDENCE-OF: R-43316-37308 A value of 2 (0x02) means the page is an 1872 ** interior index b-tree page. */ 1873 assert( (PTF_ZERODATA)==2 ); 1874 /* EVIDENCE-OF: R-59615-42828 A value of 10 (0x0a) means the page is a 1875 ** leaf index b-tree page. */ 1876 assert( (PTF_ZERODATA|PTF_LEAF)==10 ); 1877 pPage->intKey = 0; 1878 pPage->intKeyLeaf = 0; 1879 pPage->xParseCell = btreeParseCellPtrIndex; 1880 pPage->maxLocal = pBt->maxLocal; 1881 pPage->minLocal = pBt->minLocal; 1882 }else{ 1883 /* EVIDENCE-OF: R-47608-56469 Any other value for the b-tree page type is 1884 ** an error. */ 1885 return SQLITE_CORRUPT_PAGE(pPage); 1886 } 1887 pPage->max1bytePayload = pBt->max1bytePayload; 1888 return SQLITE_OK; 1889 } 1890 1891 /* 1892 ** Compute the amount of freespace on the page. In other words, fill 1893 ** in the pPage->nFree field. 1894 */ 1895 static int btreeComputeFreeSpace(MemPage *pPage){ 1896 int pc; /* Address of a freeblock within pPage->aData[] */ 1897 u8 hdr; /* Offset to beginning of page header */ 1898 u8 *data; /* Equal to pPage->aData */ 1899 int usableSize; /* Amount of usable space on each page */ 1900 int nFree; /* Number of unused bytes on the page */ 1901 int top; /* First byte of the cell content area */ 1902 int iCellFirst; /* First allowable cell or freeblock offset */ 1903 int iCellLast; /* Last possible cell or freeblock offset */ 1904 1905 assert( pPage->pBt!=0 ); 1906 assert( pPage->pBt->db!=0 ); 1907 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 1908 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); 1909 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) ); 1910 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) ); 1911 assert( pPage->isInit==1 ); 1912 assert( pPage->nFree<0 ); 1913 1914 usableSize = pPage->pBt->usableSize; 1915 hdr = pPage->hdrOffset; 1916 data = pPage->aData; 1917 /* EVIDENCE-OF: R-58015-48175 The two-byte integer at offset 5 designates 1918 ** the start of the cell content area. A zero value for this integer is 1919 ** interpreted as 65536. */ 1920 top = get2byteNotZero(&data[hdr+5]); 1921 iCellFirst = hdr + 8 + pPage->childPtrSize + 2*pPage->nCell; 1922 iCellLast = usableSize - 4; 1923 1924 /* Compute the total free space on the page 1925 ** EVIDENCE-OF: R-23588-34450 The two-byte integer at offset 1 gives the 1926 ** start of the first freeblock on the page, or is zero if there are no 1927 ** freeblocks. */ 1928 pc = get2byte(&data[hdr+1]); 1929 nFree = data[hdr+7] + top; /* Init nFree to non-freeblock free space */ 1930 if( pc>0 ){ 1931 u32 next, size; 1932 if( pc<top ){ 1933 /* EVIDENCE-OF: R-55530-52930 In a well-formed b-tree page, there will 1934 ** always be at least one cell before the first freeblock. 1935 */ 1936 return SQLITE_CORRUPT_PAGE(pPage); 1937 } 1938 while( 1 ){ 1939 if( pc>iCellLast ){ 1940 /* Freeblock off the end of the page */ 1941 return SQLITE_CORRUPT_PAGE(pPage); 1942 } 1943 next = get2byte(&data[pc]); 1944 size = get2byte(&data[pc+2]); 1945 nFree = nFree + size; 1946 if( next<=pc+size+3 ) break; 1947 pc = next; 1948 } 1949 if( next>0 ){ 1950 /* Freeblock not in ascending order */ 1951 return SQLITE_CORRUPT_PAGE(pPage); 1952 } 1953 if( pc+size>(unsigned int)usableSize ){ 1954 /* Last freeblock extends past page end */ 1955 return SQLITE_CORRUPT_PAGE(pPage); 1956 } 1957 } 1958 1959 /* At this point, nFree contains the sum of the offset to the start 1960 ** of the cell-content area plus the number of free bytes within 1961 ** the cell-content area. If this is greater than the usable-size 1962 ** of the page, then the page must be corrupted. This check also 1963 ** serves to verify that the offset to the start of the cell-content 1964 ** area, according to the page header, lies within the page. 1965 */ 1966 if( nFree>usableSize || nFree<iCellFirst ){ 1967 return SQLITE_CORRUPT_PAGE(pPage); 1968 } 1969 pPage->nFree = (u16)(nFree - iCellFirst); 1970 return SQLITE_OK; 1971 } 1972 1973 /* 1974 ** Do additional sanity check after btreeInitPage() if 1975 ** PRAGMA cell_size_check=ON 1976 */ 1977 static SQLITE_NOINLINE int btreeCellSizeCheck(MemPage *pPage){ 1978 int iCellFirst; /* First allowable cell or freeblock offset */ 1979 int iCellLast; /* Last possible cell or freeblock offset */ 1980 int i; /* Index into the cell pointer array */ 1981 int sz; /* Size of a cell */ 1982 int pc; /* Address of a freeblock within pPage->aData[] */ 1983 u8 *data; /* Equal to pPage->aData */ 1984 int usableSize; /* Maximum usable space on the page */ 1985 int cellOffset; /* Start of cell content area */ 1986 1987 iCellFirst = pPage->cellOffset + 2*pPage->nCell; 1988 usableSize = pPage->pBt->usableSize; 1989 iCellLast = usableSize - 4; 1990 data = pPage->aData; 1991 cellOffset = pPage->cellOffset; 1992 if( !pPage->leaf ) iCellLast--; 1993 for(i=0; i<pPage->nCell; i++){ 1994 pc = get2byteAligned(&data[cellOffset+i*2]); 1995 testcase( pc==iCellFirst ); 1996 testcase( pc==iCellLast ); 1997 if( pc<iCellFirst || pc>iCellLast ){ 1998 return SQLITE_CORRUPT_PAGE(pPage); 1999 } 2000 sz = pPage->xCellSize(pPage, &data[pc]); 2001 testcase( pc+sz==usableSize ); 2002 if( pc+sz>usableSize ){ 2003 return SQLITE_CORRUPT_PAGE(pPage); 2004 } 2005 } 2006 return SQLITE_OK; 2007 } 2008 2009 /* 2010 ** Initialize the auxiliary information for a disk block. 2011 ** 2012 ** Return SQLITE_OK on success. If we see that the page does 2013 ** not contain a well-formed database page, then return 2014 ** SQLITE_CORRUPT. Note that a return of SQLITE_OK does not 2015 ** guarantee that the page is well-formed. It only shows that 2016 ** we failed to detect any corruption. 2017 */ 2018 static int btreeInitPage(MemPage *pPage){ 2019 u8 *data; /* Equal to pPage->aData */ 2020 BtShared *pBt; /* The main btree structure */ 2021 2022 assert( pPage->pBt!=0 ); 2023 assert( pPage->pBt->db!=0 ); 2024 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 2025 assert( pPage->pgno==sqlite3PagerPagenumber(pPage->pDbPage) ); 2026 assert( pPage == sqlite3PagerGetExtra(pPage->pDbPage) ); 2027 assert( pPage->aData == sqlite3PagerGetData(pPage->pDbPage) ); 2028 assert( pPage->isInit==0 ); 2029 2030 pBt = pPage->pBt; 2031 data = pPage->aData + pPage->hdrOffset; 2032 /* EVIDENCE-OF: R-28594-02890 The one-byte flag at offset 0 indicating 2033 ** the b-tree page type. */ 2034 if( decodeFlags(pPage, data[0]) ){ 2035 return SQLITE_CORRUPT_PAGE(pPage); 2036 } 2037 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 ); 2038 pPage->maskPage = (u16)(pBt->pageSize - 1); 2039 pPage->nOverflow = 0; 2040 pPage->cellOffset = pPage->hdrOffset + 8 + pPage->childPtrSize; 2041 pPage->aCellIdx = data + pPage->childPtrSize + 8; 2042 pPage->aDataEnd = pPage->aData + pBt->usableSize; 2043 pPage->aDataOfst = pPage->aData + pPage->childPtrSize; 2044 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the 2045 ** number of cells on the page. */ 2046 pPage->nCell = get2byte(&data[3]); 2047 if( pPage->nCell>MX_CELL(pBt) ){ 2048 /* To many cells for a single page. The page must be corrupt */ 2049 return SQLITE_CORRUPT_PAGE(pPage); 2050 } 2051 testcase( pPage->nCell==MX_CELL(pBt) ); 2052 /* EVIDENCE-OF: R-24089-57979 If a page contains no cells (which is only 2053 ** possible for a root page of a table that contains no rows) then the 2054 ** offset to the cell content area will equal the page size minus the 2055 ** bytes of reserved space. */ 2056 assert( pPage->nCell>0 2057 || get2byteNotZero(&data[5])==(int)pBt->usableSize 2058 || CORRUPT_DB ); 2059 pPage->nFree = -1; /* Indicate that this value is yet uncomputed */ 2060 pPage->isInit = 1; 2061 if( pBt->db->flags & SQLITE_CellSizeCk ){ 2062 return btreeCellSizeCheck(pPage); 2063 } 2064 return SQLITE_OK; 2065 } 2066 2067 /* 2068 ** Set up a raw page so that it looks like a database page holding 2069 ** no entries. 2070 */ 2071 static void zeroPage(MemPage *pPage, int flags){ 2072 unsigned char *data = pPage->aData; 2073 BtShared *pBt = pPage->pBt; 2074 u8 hdr = pPage->hdrOffset; 2075 u16 first; 2076 2077 assert( sqlite3PagerPagenumber(pPage->pDbPage)==pPage->pgno ); 2078 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); 2079 assert( sqlite3PagerGetData(pPage->pDbPage) == data ); 2080 assert( sqlite3PagerIswriteable(pPage->pDbPage) ); 2081 assert( sqlite3_mutex_held(pBt->mutex) ); 2082 if( pBt->btsFlags & BTS_FAST_SECURE ){ 2083 memset(&data[hdr], 0, pBt->usableSize - hdr); 2084 } 2085 data[hdr] = (char)flags; 2086 first = hdr + ((flags&PTF_LEAF)==0 ? 12 : 8); 2087 memset(&data[hdr+1], 0, 4); 2088 data[hdr+7] = 0; 2089 put2byte(&data[hdr+5], pBt->usableSize); 2090 pPage->nFree = (u16)(pBt->usableSize - first); 2091 decodeFlags(pPage, flags); 2092 pPage->cellOffset = first; 2093 pPage->aDataEnd = &data[pBt->usableSize]; 2094 pPage->aCellIdx = &data[first]; 2095 pPage->aDataOfst = &data[pPage->childPtrSize]; 2096 pPage->nOverflow = 0; 2097 assert( pBt->pageSize>=512 && pBt->pageSize<=65536 ); 2098 pPage->maskPage = (u16)(pBt->pageSize - 1); 2099 pPage->nCell = 0; 2100 pPage->isInit = 1; 2101 } 2102 2103 2104 /* 2105 ** Convert a DbPage obtained from the pager into a MemPage used by 2106 ** the btree layer. 2107 */ 2108 static MemPage *btreePageFromDbPage(DbPage *pDbPage, Pgno pgno, BtShared *pBt){ 2109 MemPage *pPage = (MemPage*)sqlite3PagerGetExtra(pDbPage); 2110 if( pgno!=pPage->pgno ){ 2111 pPage->aData = sqlite3PagerGetData(pDbPage); 2112 pPage->pDbPage = pDbPage; 2113 pPage->pBt = pBt; 2114 pPage->pgno = pgno; 2115 pPage->hdrOffset = pgno==1 ? 100 : 0; 2116 } 2117 assert( pPage->aData==sqlite3PagerGetData(pDbPage) ); 2118 return pPage; 2119 } 2120 2121 /* 2122 ** Get a page from the pager. Initialize the MemPage.pBt and 2123 ** MemPage.aData elements if needed. See also: btreeGetUnusedPage(). 2124 ** 2125 ** If the PAGER_GET_NOCONTENT flag is set, it means that we do not care 2126 ** about the content of the page at this time. So do not go to the disk 2127 ** to fetch the content. Just fill in the content with zeros for now. 2128 ** If in the future we call sqlite3PagerWrite() on this page, that 2129 ** means we have started to be concerned about content and the disk 2130 ** read should occur at that point. 2131 */ 2132 static int btreeGetPage( 2133 BtShared *pBt, /* The btree */ 2134 Pgno pgno, /* Number of the page to fetch */ 2135 MemPage **ppPage, /* Return the page in this parameter */ 2136 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */ 2137 ){ 2138 int rc; 2139 DbPage *pDbPage; 2140 2141 assert( flags==0 || flags==PAGER_GET_NOCONTENT || flags==PAGER_GET_READONLY ); 2142 assert( sqlite3_mutex_held(pBt->mutex) ); 2143 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, flags); 2144 if( rc ) return rc; 2145 *ppPage = btreePageFromDbPage(pDbPage, pgno, pBt); 2146 return SQLITE_OK; 2147 } 2148 2149 /* 2150 ** Retrieve a page from the pager cache. If the requested page is not 2151 ** already in the pager cache return NULL. Initialize the MemPage.pBt and 2152 ** MemPage.aData elements if needed. 2153 */ 2154 static MemPage *btreePageLookup(BtShared *pBt, Pgno pgno){ 2155 DbPage *pDbPage; 2156 assert( sqlite3_mutex_held(pBt->mutex) ); 2157 pDbPage = sqlite3PagerLookup(pBt->pPager, pgno); 2158 if( pDbPage ){ 2159 return btreePageFromDbPage(pDbPage, pgno, pBt); 2160 } 2161 return 0; 2162 } 2163 2164 /* 2165 ** Return the size of the database file in pages. If there is any kind of 2166 ** error, return ((unsigned int)-1). 2167 */ 2168 static Pgno btreePagecount(BtShared *pBt){ 2169 return pBt->nPage; 2170 } 2171 Pgno sqlite3BtreeLastPage(Btree *p){ 2172 assert( sqlite3BtreeHoldsMutex(p) ); 2173 return btreePagecount(p->pBt); 2174 } 2175 2176 /* 2177 ** Get a page from the pager and initialize it. 2178 ** 2179 ** If pCur!=0 then the page is being fetched as part of a moveToChild() 2180 ** call. Do additional sanity checking on the page in this case. 2181 ** And if the fetch fails, this routine must decrement pCur->iPage. 2182 ** 2183 ** The page is fetched as read-write unless pCur is not NULL and is 2184 ** a read-only cursor. 2185 ** 2186 ** If an error occurs, then *ppPage is undefined. It 2187 ** may remain unchanged, or it may be set to an invalid value. 2188 */ 2189 static int getAndInitPage( 2190 BtShared *pBt, /* The database file */ 2191 Pgno pgno, /* Number of the page to get */ 2192 MemPage **ppPage, /* Write the page pointer here */ 2193 BtCursor *pCur, /* Cursor to receive the page, or NULL */ 2194 int bReadOnly /* True for a read-only page */ 2195 ){ 2196 int rc; 2197 DbPage *pDbPage; 2198 assert( sqlite3_mutex_held(pBt->mutex) ); 2199 assert( pCur==0 || ppPage==&pCur->pPage ); 2200 assert( pCur==0 || bReadOnly==pCur->curPagerFlags ); 2201 assert( pCur==0 || pCur->iPage>0 ); 2202 2203 if( pgno>btreePagecount(pBt) ){ 2204 rc = SQLITE_CORRUPT_BKPT; 2205 goto getAndInitPage_error1; 2206 } 2207 rc = sqlite3PagerGet(pBt->pPager, pgno, (DbPage**)&pDbPage, bReadOnly); 2208 if( rc ){ 2209 goto getAndInitPage_error1; 2210 } 2211 *ppPage = (MemPage*)sqlite3PagerGetExtra(pDbPage); 2212 if( (*ppPage)->isInit==0 ){ 2213 btreePageFromDbPage(pDbPage, pgno, pBt); 2214 rc = btreeInitPage(*ppPage); 2215 if( rc!=SQLITE_OK ){ 2216 goto getAndInitPage_error2; 2217 } 2218 } 2219 assert( (*ppPage)->pgno==pgno ); 2220 assert( (*ppPage)->aData==sqlite3PagerGetData(pDbPage) ); 2221 2222 /* If obtaining a child page for a cursor, we must verify that the page is 2223 ** compatible with the root page. */ 2224 if( pCur && ((*ppPage)->nCell<1 || (*ppPage)->intKey!=pCur->curIntKey) ){ 2225 rc = SQLITE_CORRUPT_PGNO(pgno); 2226 goto getAndInitPage_error2; 2227 } 2228 return SQLITE_OK; 2229 2230 getAndInitPage_error2: 2231 releasePage(*ppPage); 2232 getAndInitPage_error1: 2233 if( pCur ){ 2234 pCur->iPage--; 2235 pCur->pPage = pCur->apPage[pCur->iPage]; 2236 } 2237 testcase( pgno==0 ); 2238 assert( pgno!=0 || rc==SQLITE_CORRUPT ); 2239 return rc; 2240 } 2241 2242 /* 2243 ** Release a MemPage. This should be called once for each prior 2244 ** call to btreeGetPage. 2245 ** 2246 ** Page1 is a special case and must be released using releasePageOne(). 2247 */ 2248 static void releasePageNotNull(MemPage *pPage){ 2249 assert( pPage->aData ); 2250 assert( pPage->pBt ); 2251 assert( pPage->pDbPage!=0 ); 2252 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); 2253 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData ); 2254 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 2255 sqlite3PagerUnrefNotNull(pPage->pDbPage); 2256 } 2257 static void releasePage(MemPage *pPage){ 2258 if( pPage ) releasePageNotNull(pPage); 2259 } 2260 static void releasePageOne(MemPage *pPage){ 2261 assert( pPage!=0 ); 2262 assert( pPage->aData ); 2263 assert( pPage->pBt ); 2264 assert( pPage->pDbPage!=0 ); 2265 assert( sqlite3PagerGetExtra(pPage->pDbPage) == (void*)pPage ); 2266 assert( sqlite3PagerGetData(pPage->pDbPage)==pPage->aData ); 2267 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 2268 sqlite3PagerUnrefPageOne(pPage->pDbPage); 2269 } 2270 2271 /* 2272 ** Get an unused page. 2273 ** 2274 ** This works just like btreeGetPage() with the addition: 2275 ** 2276 ** * If the page is already in use for some other purpose, immediately 2277 ** release it and return an SQLITE_CURRUPT error. 2278 ** * Make sure the isInit flag is clear 2279 */ 2280 static int btreeGetUnusedPage( 2281 BtShared *pBt, /* The btree */ 2282 Pgno pgno, /* Number of the page to fetch */ 2283 MemPage **ppPage, /* Return the page in this parameter */ 2284 int flags /* PAGER_GET_NOCONTENT or PAGER_GET_READONLY */ 2285 ){ 2286 int rc = btreeGetPage(pBt, pgno, ppPage, flags); 2287 if( rc==SQLITE_OK ){ 2288 if( sqlite3PagerPageRefcount((*ppPage)->pDbPage)>1 ){ 2289 releasePage(*ppPage); 2290 *ppPage = 0; 2291 return SQLITE_CORRUPT_BKPT; 2292 } 2293 (*ppPage)->isInit = 0; 2294 }else{ 2295 *ppPage = 0; 2296 } 2297 return rc; 2298 } 2299 2300 2301 /* 2302 ** During a rollback, when the pager reloads information into the cache 2303 ** so that the cache is restored to its original state at the start of 2304 ** the transaction, for each page restored this routine is called. 2305 ** 2306 ** This routine needs to reset the extra data section at the end of the 2307 ** page to agree with the restored data. 2308 */ 2309 static void pageReinit(DbPage *pData){ 2310 MemPage *pPage; 2311 pPage = (MemPage *)sqlite3PagerGetExtra(pData); 2312 assert( sqlite3PagerPageRefcount(pData)>0 ); 2313 if( pPage->isInit ){ 2314 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 2315 pPage->isInit = 0; 2316 if( sqlite3PagerPageRefcount(pData)>1 ){ 2317 /* pPage might not be a btree page; it might be an overflow page 2318 ** or ptrmap page or a free page. In those cases, the following 2319 ** call to btreeInitPage() will likely return SQLITE_CORRUPT. 2320 ** But no harm is done by this. And it is very important that 2321 ** btreeInitPage() be called on every btree page so we make 2322 ** the call for every page that comes in for re-initing. */ 2323 btreeInitPage(pPage); 2324 } 2325 } 2326 } 2327 2328 /* 2329 ** Invoke the busy handler for a btree. 2330 */ 2331 static int btreeInvokeBusyHandler(void *pArg){ 2332 BtShared *pBt = (BtShared*)pArg; 2333 assert( pBt->db ); 2334 assert( sqlite3_mutex_held(pBt->db->mutex) ); 2335 return sqlite3InvokeBusyHandler(&pBt->db->busyHandler); 2336 } 2337 2338 /* 2339 ** Open a database file. 2340 ** 2341 ** zFilename is the name of the database file. If zFilename is NULL 2342 ** then an ephemeral database is created. The ephemeral database might 2343 ** be exclusively in memory, or it might use a disk-based memory cache. 2344 ** Either way, the ephemeral database will be automatically deleted 2345 ** when sqlite3BtreeClose() is called. 2346 ** 2347 ** If zFilename is ":memory:" then an in-memory database is created 2348 ** that is automatically destroyed when it is closed. 2349 ** 2350 ** The "flags" parameter is a bitmask that might contain bits like 2351 ** BTREE_OMIT_JOURNAL and/or BTREE_MEMORY. 2352 ** 2353 ** If the database is already opened in the same database connection 2354 ** and we are in shared cache mode, then the open will fail with an 2355 ** SQLITE_CONSTRAINT error. We cannot allow two or more BtShared 2356 ** objects in the same database connection since doing so will lead 2357 ** to problems with locking. 2358 */ 2359 int sqlite3BtreeOpen( 2360 sqlite3_vfs *pVfs, /* VFS to use for this b-tree */ 2361 const char *zFilename, /* Name of the file containing the BTree database */ 2362 sqlite3 *db, /* Associated database handle */ 2363 Btree **ppBtree, /* Pointer to new Btree object written here */ 2364 int flags, /* Options */ 2365 int vfsFlags /* Flags passed through to sqlite3_vfs.xOpen() */ 2366 ){ 2367 BtShared *pBt = 0; /* Shared part of btree structure */ 2368 Btree *p; /* Handle to return */ 2369 sqlite3_mutex *mutexOpen = 0; /* Prevents a race condition. Ticket #3537 */ 2370 int rc = SQLITE_OK; /* Result code from this function */ 2371 u8 nReserve; /* Byte of unused space on each page */ 2372 unsigned char zDbHeader[100]; /* Database header content */ 2373 2374 /* True if opening an ephemeral, temporary database */ 2375 const int isTempDb = zFilename==0 || zFilename[0]==0; 2376 2377 /* Set the variable isMemdb to true for an in-memory database, or 2378 ** false for a file-based database. 2379 */ 2380 #ifdef SQLITE_OMIT_MEMORYDB 2381 const int isMemdb = 0; 2382 #else 2383 const int isMemdb = (zFilename && strcmp(zFilename, ":memory:")==0) 2384 || (isTempDb && sqlite3TempInMemory(db)) 2385 || (vfsFlags & SQLITE_OPEN_MEMORY)!=0; 2386 #endif 2387 2388 assert( db!=0 ); 2389 assert( pVfs!=0 ); 2390 assert( sqlite3_mutex_held(db->mutex) ); 2391 assert( (flags&0xff)==flags ); /* flags fit in 8 bits */ 2392 2393 /* Only a BTREE_SINGLE database can be BTREE_UNORDERED */ 2394 assert( (flags & BTREE_UNORDERED)==0 || (flags & BTREE_SINGLE)!=0 ); 2395 2396 /* A BTREE_SINGLE database is always a temporary and/or ephemeral */ 2397 assert( (flags & BTREE_SINGLE)==0 || isTempDb ); 2398 2399 if( isMemdb ){ 2400 flags |= BTREE_MEMORY; 2401 } 2402 if( (vfsFlags & SQLITE_OPEN_MAIN_DB)!=0 && (isMemdb || isTempDb) ){ 2403 vfsFlags = (vfsFlags & ~SQLITE_OPEN_MAIN_DB) | SQLITE_OPEN_TEMP_DB; 2404 } 2405 p = sqlite3MallocZero(sizeof(Btree)); 2406 if( !p ){ 2407 return SQLITE_NOMEM_BKPT; 2408 } 2409 p->inTrans = TRANS_NONE; 2410 p->db = db; 2411 #ifndef SQLITE_OMIT_SHARED_CACHE 2412 p->lock.pBtree = p; 2413 p->lock.iTable = 1; 2414 #endif 2415 2416 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) 2417 /* 2418 ** If this Btree is a candidate for shared cache, try to find an 2419 ** existing BtShared object that we can share with 2420 */ 2421 if( isTempDb==0 && (isMemdb==0 || (vfsFlags&SQLITE_OPEN_URI)!=0) ){ 2422 if( vfsFlags & SQLITE_OPEN_SHAREDCACHE ){ 2423 int nFilename = sqlite3Strlen30(zFilename)+1; 2424 int nFullPathname = pVfs->mxPathname+1; 2425 char *zFullPathname = sqlite3Malloc(MAX(nFullPathname,nFilename)); 2426 MUTEX_LOGIC( sqlite3_mutex *mutexShared; ) 2427 2428 p->sharable = 1; 2429 if( !zFullPathname ){ 2430 sqlite3_free(p); 2431 return SQLITE_NOMEM_BKPT; 2432 } 2433 if( isMemdb ){ 2434 memcpy(zFullPathname, zFilename, nFilename); 2435 }else{ 2436 rc = sqlite3OsFullPathname(pVfs, zFilename, 2437 nFullPathname, zFullPathname); 2438 if( rc ){ 2439 if( rc==SQLITE_OK_SYMLINK ){ 2440 rc = SQLITE_OK; 2441 }else{ 2442 sqlite3_free(zFullPathname); 2443 sqlite3_free(p); 2444 return rc; 2445 } 2446 } 2447 } 2448 #if SQLITE_THREADSAFE 2449 mutexOpen = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_OPEN); 2450 sqlite3_mutex_enter(mutexOpen); 2451 mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); 2452 sqlite3_mutex_enter(mutexShared); 2453 #endif 2454 for(pBt=GLOBAL(BtShared*,sqlite3SharedCacheList); pBt; pBt=pBt->pNext){ 2455 assert( pBt->nRef>0 ); 2456 if( 0==strcmp(zFullPathname, sqlite3PagerFilename(pBt->pPager, 0)) 2457 && sqlite3PagerVfs(pBt->pPager)==pVfs ){ 2458 int iDb; 2459 for(iDb=db->nDb-1; iDb>=0; iDb--){ 2460 Btree *pExisting = db->aDb[iDb].pBt; 2461 if( pExisting && pExisting->pBt==pBt ){ 2462 sqlite3_mutex_leave(mutexShared); 2463 sqlite3_mutex_leave(mutexOpen); 2464 sqlite3_free(zFullPathname); 2465 sqlite3_free(p); 2466 return SQLITE_CONSTRAINT; 2467 } 2468 } 2469 p->pBt = pBt; 2470 pBt->nRef++; 2471 break; 2472 } 2473 } 2474 sqlite3_mutex_leave(mutexShared); 2475 sqlite3_free(zFullPathname); 2476 } 2477 #ifdef SQLITE_DEBUG 2478 else{ 2479 /* In debug mode, we mark all persistent databases as sharable 2480 ** even when they are not. This exercises the locking code and 2481 ** gives more opportunity for asserts(sqlite3_mutex_held()) 2482 ** statements to find locking problems. 2483 */ 2484 p->sharable = 1; 2485 } 2486 #endif 2487 } 2488 #endif 2489 if( pBt==0 ){ 2490 /* 2491 ** The following asserts make sure that structures used by the btree are 2492 ** the right size. This is to guard against size changes that result 2493 ** when compiling on a different architecture. 2494 */ 2495 assert( sizeof(i64)==8 ); 2496 assert( sizeof(u64)==8 ); 2497 assert( sizeof(u32)==4 ); 2498 assert( sizeof(u16)==2 ); 2499 assert( sizeof(Pgno)==4 ); 2500 2501 pBt = sqlite3MallocZero( sizeof(*pBt) ); 2502 if( pBt==0 ){ 2503 rc = SQLITE_NOMEM_BKPT; 2504 goto btree_open_out; 2505 } 2506 rc = sqlite3PagerOpen(pVfs, &pBt->pPager, zFilename, 2507 sizeof(MemPage), flags, vfsFlags, pageReinit); 2508 if( rc==SQLITE_OK ){ 2509 sqlite3PagerSetMmapLimit(pBt->pPager, db->szMmap); 2510 rc = sqlite3PagerReadFileheader(pBt->pPager,sizeof(zDbHeader),zDbHeader); 2511 } 2512 if( rc!=SQLITE_OK ){ 2513 goto btree_open_out; 2514 } 2515 pBt->openFlags = (u8)flags; 2516 pBt->db = db; 2517 sqlite3PagerSetBusyHandler(pBt->pPager, btreeInvokeBusyHandler, pBt); 2518 p->pBt = pBt; 2519 2520 pBt->pCursor = 0; 2521 pBt->pPage1 = 0; 2522 if( sqlite3PagerIsreadonly(pBt->pPager) ) pBt->btsFlags |= BTS_READ_ONLY; 2523 #if defined(SQLITE_SECURE_DELETE) 2524 pBt->btsFlags |= BTS_SECURE_DELETE; 2525 #elif defined(SQLITE_FAST_SECURE_DELETE) 2526 pBt->btsFlags |= BTS_OVERWRITE; 2527 #endif 2528 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is 2529 ** determined by the 2-byte integer located at an offset of 16 bytes from 2530 ** the beginning of the database file. */ 2531 pBt->pageSize = (zDbHeader[16]<<8) | (zDbHeader[17]<<16); 2532 if( pBt->pageSize<512 || pBt->pageSize>SQLITE_MAX_PAGE_SIZE 2533 || ((pBt->pageSize-1)&pBt->pageSize)!=0 ){ 2534 pBt->pageSize = 0; 2535 #ifndef SQLITE_OMIT_AUTOVACUUM 2536 /* If the magic name ":memory:" will create an in-memory database, then 2537 ** leave the autoVacuum mode at 0 (do not auto-vacuum), even if 2538 ** SQLITE_DEFAULT_AUTOVACUUM is true. On the other hand, if 2539 ** SQLITE_OMIT_MEMORYDB has been defined, then ":memory:" is just a 2540 ** regular file-name. In this case the auto-vacuum applies as per normal. 2541 */ 2542 if( zFilename && !isMemdb ){ 2543 pBt->autoVacuum = (SQLITE_DEFAULT_AUTOVACUUM ? 1 : 0); 2544 pBt->incrVacuum = (SQLITE_DEFAULT_AUTOVACUUM==2 ? 1 : 0); 2545 } 2546 #endif 2547 nReserve = 0; 2548 }else{ 2549 /* EVIDENCE-OF: R-37497-42412 The size of the reserved region is 2550 ** determined by the one-byte unsigned integer found at an offset of 20 2551 ** into the database file header. */ 2552 nReserve = zDbHeader[20]; 2553 pBt->btsFlags |= BTS_PAGESIZE_FIXED; 2554 #ifndef SQLITE_OMIT_AUTOVACUUM 2555 pBt->autoVacuum = (get4byte(&zDbHeader[36 + 4*4])?1:0); 2556 pBt->incrVacuum = (get4byte(&zDbHeader[36 + 7*4])?1:0); 2557 #endif 2558 } 2559 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve); 2560 if( rc ) goto btree_open_out; 2561 pBt->usableSize = pBt->pageSize - nReserve; 2562 assert( (pBt->pageSize & 7)==0 ); /* 8-byte alignment of pageSize */ 2563 2564 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) 2565 /* Add the new BtShared object to the linked list sharable BtShareds. 2566 */ 2567 pBt->nRef = 1; 2568 if( p->sharable ){ 2569 MUTEX_LOGIC( sqlite3_mutex *mutexShared; ) 2570 MUTEX_LOGIC( mutexShared = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN);) 2571 if( SQLITE_THREADSAFE && sqlite3GlobalConfig.bCoreMutex ){ 2572 pBt->mutex = sqlite3MutexAlloc(SQLITE_MUTEX_FAST); 2573 if( pBt->mutex==0 ){ 2574 rc = SQLITE_NOMEM_BKPT; 2575 goto btree_open_out; 2576 } 2577 } 2578 sqlite3_mutex_enter(mutexShared); 2579 pBt->pNext = GLOBAL(BtShared*,sqlite3SharedCacheList); 2580 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt; 2581 sqlite3_mutex_leave(mutexShared); 2582 } 2583 #endif 2584 } 2585 2586 #if !defined(SQLITE_OMIT_SHARED_CACHE) && !defined(SQLITE_OMIT_DISKIO) 2587 /* If the new Btree uses a sharable pBtShared, then link the new 2588 ** Btree into the list of all sharable Btrees for the same connection. 2589 ** The list is kept in ascending order by pBt address. 2590 */ 2591 if( p->sharable ){ 2592 int i; 2593 Btree *pSib; 2594 for(i=0; i<db->nDb; i++){ 2595 if( (pSib = db->aDb[i].pBt)!=0 && pSib->sharable ){ 2596 while( pSib->pPrev ){ pSib = pSib->pPrev; } 2597 if( (uptr)p->pBt<(uptr)pSib->pBt ){ 2598 p->pNext = pSib; 2599 p->pPrev = 0; 2600 pSib->pPrev = p; 2601 }else{ 2602 while( pSib->pNext && (uptr)pSib->pNext->pBt<(uptr)p->pBt ){ 2603 pSib = pSib->pNext; 2604 } 2605 p->pNext = pSib->pNext; 2606 p->pPrev = pSib; 2607 if( p->pNext ){ 2608 p->pNext->pPrev = p; 2609 } 2610 pSib->pNext = p; 2611 } 2612 break; 2613 } 2614 } 2615 } 2616 #endif 2617 *ppBtree = p; 2618 2619 btree_open_out: 2620 if( rc!=SQLITE_OK ){ 2621 if( pBt && pBt->pPager ){ 2622 sqlite3PagerClose(pBt->pPager, 0); 2623 } 2624 sqlite3_free(pBt); 2625 sqlite3_free(p); 2626 *ppBtree = 0; 2627 }else{ 2628 sqlite3_file *pFile; 2629 2630 /* If the B-Tree was successfully opened, set the pager-cache size to the 2631 ** default value. Except, when opening on an existing shared pager-cache, 2632 ** do not change the pager-cache size. 2633 */ 2634 if( sqlite3BtreeSchema(p, 0, 0)==0 ){ 2635 sqlite3BtreeSetCacheSize(p, SQLITE_DEFAULT_CACHE_SIZE); 2636 } 2637 2638 pFile = sqlite3PagerFile(pBt->pPager); 2639 if( pFile->pMethods ){ 2640 sqlite3OsFileControlHint(pFile, SQLITE_FCNTL_PDB, (void*)&pBt->db); 2641 } 2642 } 2643 if( mutexOpen ){ 2644 assert( sqlite3_mutex_held(mutexOpen) ); 2645 sqlite3_mutex_leave(mutexOpen); 2646 } 2647 assert( rc!=SQLITE_OK || sqlite3BtreeConnectionCount(*ppBtree)>0 ); 2648 return rc; 2649 } 2650 2651 /* 2652 ** Decrement the BtShared.nRef counter. When it reaches zero, 2653 ** remove the BtShared structure from the sharing list. Return 2654 ** true if the BtShared.nRef counter reaches zero and return 2655 ** false if it is still positive. 2656 */ 2657 static int removeFromSharingList(BtShared *pBt){ 2658 #ifndef SQLITE_OMIT_SHARED_CACHE 2659 MUTEX_LOGIC( sqlite3_mutex *pMainMtx; ) 2660 BtShared *pList; 2661 int removed = 0; 2662 2663 assert( sqlite3_mutex_notheld(pBt->mutex) ); 2664 MUTEX_LOGIC( pMainMtx = sqlite3MutexAlloc(SQLITE_MUTEX_STATIC_MAIN); ) 2665 sqlite3_mutex_enter(pMainMtx); 2666 pBt->nRef--; 2667 if( pBt->nRef<=0 ){ 2668 if( GLOBAL(BtShared*,sqlite3SharedCacheList)==pBt ){ 2669 GLOBAL(BtShared*,sqlite3SharedCacheList) = pBt->pNext; 2670 }else{ 2671 pList = GLOBAL(BtShared*,sqlite3SharedCacheList); 2672 while( ALWAYS(pList) && pList->pNext!=pBt ){ 2673 pList=pList->pNext; 2674 } 2675 if( ALWAYS(pList) ){ 2676 pList->pNext = pBt->pNext; 2677 } 2678 } 2679 if( SQLITE_THREADSAFE ){ 2680 sqlite3_mutex_free(pBt->mutex); 2681 } 2682 removed = 1; 2683 } 2684 sqlite3_mutex_leave(pMainMtx); 2685 return removed; 2686 #else 2687 return 1; 2688 #endif 2689 } 2690 2691 /* 2692 ** Make sure pBt->pTmpSpace points to an allocation of 2693 ** MX_CELL_SIZE(pBt) bytes with a 4-byte prefix for a left-child 2694 ** pointer. 2695 */ 2696 static SQLITE_NOINLINE int allocateTempSpace(BtShared *pBt){ 2697 assert( pBt!=0 ); 2698 assert( pBt->pTmpSpace==0 ); 2699 /* This routine is called only by btreeCursor() when allocating the 2700 ** first write cursor for the BtShared object */ 2701 assert( pBt->pCursor!=0 && (pBt->pCursor->curFlags & BTCF_WriteFlag)!=0 ); 2702 pBt->pTmpSpace = sqlite3PageMalloc( pBt->pageSize ); 2703 if( pBt->pTmpSpace==0 ){ 2704 BtCursor *pCur = pBt->pCursor; 2705 pBt->pCursor = pCur->pNext; /* Unlink the cursor */ 2706 memset(pCur, 0, sizeof(*pCur)); 2707 return SQLITE_NOMEM_BKPT; 2708 } 2709 2710 /* One of the uses of pBt->pTmpSpace is to format cells before 2711 ** inserting them into a leaf page (function fillInCell()). If 2712 ** a cell is less than 4 bytes in size, it is rounded up to 4 bytes 2713 ** by the various routines that manipulate binary cells. Which 2714 ** can mean that fillInCell() only initializes the first 2 or 3 2715 ** bytes of pTmpSpace, but that the first 4 bytes are copied from 2716 ** it into a database page. This is not actually a problem, but it 2717 ** does cause a valgrind error when the 1 or 2 bytes of unitialized 2718 ** data is passed to system call write(). So to avoid this error, 2719 ** zero the first 4 bytes of temp space here. 2720 ** 2721 ** Also: Provide four bytes of initialized space before the 2722 ** beginning of pTmpSpace as an area available to prepend the 2723 ** left-child pointer to the beginning of a cell. 2724 */ 2725 memset(pBt->pTmpSpace, 0, 8); 2726 pBt->pTmpSpace += 4; 2727 return SQLITE_OK; 2728 } 2729 2730 /* 2731 ** Free the pBt->pTmpSpace allocation 2732 */ 2733 static void freeTempSpace(BtShared *pBt){ 2734 if( pBt->pTmpSpace ){ 2735 pBt->pTmpSpace -= 4; 2736 sqlite3PageFree(pBt->pTmpSpace); 2737 pBt->pTmpSpace = 0; 2738 } 2739 } 2740 2741 /* 2742 ** Close an open database and invalidate all cursors. 2743 */ 2744 int sqlite3BtreeClose(Btree *p){ 2745 BtShared *pBt = p->pBt; 2746 2747 /* Close all cursors opened via this handle. */ 2748 assert( sqlite3_mutex_held(p->db->mutex) ); 2749 sqlite3BtreeEnter(p); 2750 2751 /* Verify that no other cursors have this Btree open */ 2752 #ifdef SQLITE_DEBUG 2753 { 2754 BtCursor *pCur = pBt->pCursor; 2755 while( pCur ){ 2756 BtCursor *pTmp = pCur; 2757 pCur = pCur->pNext; 2758 assert( pTmp->pBtree!=p ); 2759 2760 } 2761 } 2762 #endif 2763 2764 /* Rollback any active transaction and free the handle structure. 2765 ** The call to sqlite3BtreeRollback() drops any table-locks held by 2766 ** this handle. 2767 */ 2768 sqlite3BtreeRollback(p, SQLITE_OK, 0); 2769 sqlite3BtreeLeave(p); 2770 2771 /* If there are still other outstanding references to the shared-btree 2772 ** structure, return now. The remainder of this procedure cleans 2773 ** up the shared-btree. 2774 */ 2775 assert( p->wantToLock==0 && p->locked==0 ); 2776 if( !p->sharable || removeFromSharingList(pBt) ){ 2777 /* The pBt is no longer on the sharing list, so we can access 2778 ** it without having to hold the mutex. 2779 ** 2780 ** Clean out and delete the BtShared object. 2781 */ 2782 assert( !pBt->pCursor ); 2783 sqlite3PagerClose(pBt->pPager, p->db); 2784 if( pBt->xFreeSchema && pBt->pSchema ){ 2785 pBt->xFreeSchema(pBt->pSchema); 2786 } 2787 sqlite3DbFree(0, pBt->pSchema); 2788 freeTempSpace(pBt); 2789 sqlite3_free(pBt); 2790 } 2791 2792 #ifndef SQLITE_OMIT_SHARED_CACHE 2793 assert( p->wantToLock==0 ); 2794 assert( p->locked==0 ); 2795 if( p->pPrev ) p->pPrev->pNext = p->pNext; 2796 if( p->pNext ) p->pNext->pPrev = p->pPrev; 2797 #endif 2798 2799 sqlite3_free(p); 2800 return SQLITE_OK; 2801 } 2802 2803 /* 2804 ** Change the "soft" limit on the number of pages in the cache. 2805 ** Unused and unmodified pages will be recycled when the number of 2806 ** pages in the cache exceeds this soft limit. But the size of the 2807 ** cache is allowed to grow larger than this limit if it contains 2808 ** dirty pages or pages still in active use. 2809 */ 2810 int sqlite3BtreeSetCacheSize(Btree *p, int mxPage){ 2811 BtShared *pBt = p->pBt; 2812 assert( sqlite3_mutex_held(p->db->mutex) ); 2813 sqlite3BtreeEnter(p); 2814 sqlite3PagerSetCachesize(pBt->pPager, mxPage); 2815 sqlite3BtreeLeave(p); 2816 return SQLITE_OK; 2817 } 2818 2819 /* 2820 ** Change the "spill" limit on the number of pages in the cache. 2821 ** If the number of pages exceeds this limit during a write transaction, 2822 ** the pager might attempt to "spill" pages to the journal early in 2823 ** order to free up memory. 2824 ** 2825 ** The value returned is the current spill size. If zero is passed 2826 ** as an argument, no changes are made to the spill size setting, so 2827 ** using mxPage of 0 is a way to query the current spill size. 2828 */ 2829 int sqlite3BtreeSetSpillSize(Btree *p, int mxPage){ 2830 BtShared *pBt = p->pBt; 2831 int res; 2832 assert( sqlite3_mutex_held(p->db->mutex) ); 2833 sqlite3BtreeEnter(p); 2834 res = sqlite3PagerSetSpillsize(pBt->pPager, mxPage); 2835 sqlite3BtreeLeave(p); 2836 return res; 2837 } 2838 2839 #if SQLITE_MAX_MMAP_SIZE>0 2840 /* 2841 ** Change the limit on the amount of the database file that may be 2842 ** memory mapped. 2843 */ 2844 int sqlite3BtreeSetMmapLimit(Btree *p, sqlite3_int64 szMmap){ 2845 BtShared *pBt = p->pBt; 2846 assert( sqlite3_mutex_held(p->db->mutex) ); 2847 sqlite3BtreeEnter(p); 2848 sqlite3PagerSetMmapLimit(pBt->pPager, szMmap); 2849 sqlite3BtreeLeave(p); 2850 return SQLITE_OK; 2851 } 2852 #endif /* SQLITE_MAX_MMAP_SIZE>0 */ 2853 2854 /* 2855 ** Change the way data is synced to disk in order to increase or decrease 2856 ** how well the database resists damage due to OS crashes and power 2857 ** failures. Level 1 is the same as asynchronous (no syncs() occur and 2858 ** there is a high probability of damage) Level 2 is the default. There 2859 ** is a very low but non-zero probability of damage. Level 3 reduces the 2860 ** probability of damage to near zero but with a write performance reduction. 2861 */ 2862 #ifndef SQLITE_OMIT_PAGER_PRAGMAS 2863 int sqlite3BtreeSetPagerFlags( 2864 Btree *p, /* The btree to set the safety level on */ 2865 unsigned pgFlags /* Various PAGER_* flags */ 2866 ){ 2867 BtShared *pBt = p->pBt; 2868 assert( sqlite3_mutex_held(p->db->mutex) ); 2869 sqlite3BtreeEnter(p); 2870 sqlite3PagerSetFlags(pBt->pPager, pgFlags); 2871 sqlite3BtreeLeave(p); 2872 return SQLITE_OK; 2873 } 2874 #endif 2875 2876 /* 2877 ** Change the default pages size and the number of reserved bytes per page. 2878 ** Or, if the page size has already been fixed, return SQLITE_READONLY 2879 ** without changing anything. 2880 ** 2881 ** The page size must be a power of 2 between 512 and 65536. If the page 2882 ** size supplied does not meet this constraint then the page size is not 2883 ** changed. 2884 ** 2885 ** Page sizes are constrained to be a power of two so that the region 2886 ** of the database file used for locking (beginning at PENDING_BYTE, 2887 ** the first byte past the 1GB boundary, 0x40000000) needs to occur 2888 ** at the beginning of a page. 2889 ** 2890 ** If parameter nReserve is less than zero, then the number of reserved 2891 ** bytes per page is left unchanged. 2892 ** 2893 ** If the iFix!=0 then the BTS_PAGESIZE_FIXED flag is set so that the page size 2894 ** and autovacuum mode can no longer be changed. 2895 */ 2896 int sqlite3BtreeSetPageSize(Btree *p, int pageSize, int nReserve, int iFix){ 2897 int rc = SQLITE_OK; 2898 int x; 2899 BtShared *pBt = p->pBt; 2900 assert( nReserve>=0 && nReserve<=255 ); 2901 sqlite3BtreeEnter(p); 2902 pBt->nReserveWanted = nReserve; 2903 x = pBt->pageSize - pBt->usableSize; 2904 if( nReserve<x ) nReserve = x; 2905 if( pBt->btsFlags & BTS_PAGESIZE_FIXED ){ 2906 sqlite3BtreeLeave(p); 2907 return SQLITE_READONLY; 2908 } 2909 assert( nReserve>=0 && nReserve<=255 ); 2910 if( pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE && 2911 ((pageSize-1)&pageSize)==0 ){ 2912 assert( (pageSize & 7)==0 ); 2913 assert( !pBt->pCursor ); 2914 if( nReserve>32 && pageSize==512 ) pageSize = 1024; 2915 pBt->pageSize = (u32)pageSize; 2916 freeTempSpace(pBt); 2917 } 2918 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, nReserve); 2919 pBt->usableSize = pBt->pageSize - (u16)nReserve; 2920 if( iFix ) pBt->btsFlags |= BTS_PAGESIZE_FIXED; 2921 sqlite3BtreeLeave(p); 2922 return rc; 2923 } 2924 2925 /* 2926 ** Return the currently defined page size 2927 */ 2928 int sqlite3BtreeGetPageSize(Btree *p){ 2929 return p->pBt->pageSize; 2930 } 2931 2932 /* 2933 ** This function is similar to sqlite3BtreeGetReserve(), except that it 2934 ** may only be called if it is guaranteed that the b-tree mutex is already 2935 ** held. 2936 ** 2937 ** This is useful in one special case in the backup API code where it is 2938 ** known that the shared b-tree mutex is held, but the mutex on the 2939 ** database handle that owns *p is not. In this case if sqlite3BtreeEnter() 2940 ** were to be called, it might collide with some other operation on the 2941 ** database handle that owns *p, causing undefined behavior. 2942 */ 2943 int sqlite3BtreeGetReserveNoMutex(Btree *p){ 2944 int n; 2945 assert( sqlite3_mutex_held(p->pBt->mutex) ); 2946 n = p->pBt->pageSize - p->pBt->usableSize; 2947 return n; 2948 } 2949 2950 /* 2951 ** Return the number of bytes of space at the end of every page that 2952 ** are intentually left unused. This is the "reserved" space that is 2953 ** sometimes used by extensions. 2954 ** 2955 ** The value returned is the larger of the current reserve size and 2956 ** the latest reserve size requested by SQLITE_FILECTRL_RESERVE_BYTES. 2957 ** The amount of reserve can only grow - never shrink. 2958 */ 2959 int sqlite3BtreeGetRequestedReserve(Btree *p){ 2960 int n1, n2; 2961 sqlite3BtreeEnter(p); 2962 n1 = (int)p->pBt->nReserveWanted; 2963 n2 = sqlite3BtreeGetReserveNoMutex(p); 2964 sqlite3BtreeLeave(p); 2965 return n1>n2 ? n1 : n2; 2966 } 2967 2968 2969 /* 2970 ** Set the maximum page count for a database if mxPage is positive. 2971 ** No changes are made if mxPage is 0 or negative. 2972 ** Regardless of the value of mxPage, return the maximum page count. 2973 */ 2974 Pgno sqlite3BtreeMaxPageCount(Btree *p, Pgno mxPage){ 2975 Pgno n; 2976 sqlite3BtreeEnter(p); 2977 n = sqlite3PagerMaxPageCount(p->pBt->pPager, mxPage); 2978 sqlite3BtreeLeave(p); 2979 return n; 2980 } 2981 2982 /* 2983 ** Change the values for the BTS_SECURE_DELETE and BTS_OVERWRITE flags: 2984 ** 2985 ** newFlag==0 Both BTS_SECURE_DELETE and BTS_OVERWRITE are cleared 2986 ** newFlag==1 BTS_SECURE_DELETE set and BTS_OVERWRITE is cleared 2987 ** newFlag==2 BTS_SECURE_DELETE cleared and BTS_OVERWRITE is set 2988 ** newFlag==(-1) No changes 2989 ** 2990 ** This routine acts as a query if newFlag is less than zero 2991 ** 2992 ** With BTS_OVERWRITE set, deleted content is overwritten by zeros, but 2993 ** freelist leaf pages are not written back to the database. Thus in-page 2994 ** deleted content is cleared, but freelist deleted content is not. 2995 ** 2996 ** With BTS_SECURE_DELETE, operation is like BTS_OVERWRITE with the addition 2997 ** that freelist leaf pages are written back into the database, increasing 2998 ** the amount of disk I/O. 2999 */ 3000 int sqlite3BtreeSecureDelete(Btree *p, int newFlag){ 3001 int b; 3002 if( p==0 ) return 0; 3003 sqlite3BtreeEnter(p); 3004 assert( BTS_OVERWRITE==BTS_SECURE_DELETE*2 ); 3005 assert( BTS_FAST_SECURE==(BTS_OVERWRITE|BTS_SECURE_DELETE) ); 3006 if( newFlag>=0 ){ 3007 p->pBt->btsFlags &= ~BTS_FAST_SECURE; 3008 p->pBt->btsFlags |= BTS_SECURE_DELETE*newFlag; 3009 } 3010 b = (p->pBt->btsFlags & BTS_FAST_SECURE)/BTS_SECURE_DELETE; 3011 sqlite3BtreeLeave(p); 3012 return b; 3013 } 3014 3015 /* 3016 ** Change the 'auto-vacuum' property of the database. If the 'autoVacuum' 3017 ** parameter is non-zero, then auto-vacuum mode is enabled. If zero, it 3018 ** is disabled. The default value for the auto-vacuum property is 3019 ** determined by the SQLITE_DEFAULT_AUTOVACUUM macro. 3020 */ 3021 int sqlite3BtreeSetAutoVacuum(Btree *p, int autoVacuum){ 3022 #ifdef SQLITE_OMIT_AUTOVACUUM 3023 return SQLITE_READONLY; 3024 #else 3025 BtShared *pBt = p->pBt; 3026 int rc = SQLITE_OK; 3027 u8 av = (u8)autoVacuum; 3028 3029 sqlite3BtreeEnter(p); 3030 if( (pBt->btsFlags & BTS_PAGESIZE_FIXED)!=0 && (av ?1:0)!=pBt->autoVacuum ){ 3031 rc = SQLITE_READONLY; 3032 }else{ 3033 pBt->autoVacuum = av ?1:0; 3034 pBt->incrVacuum = av==2 ?1:0; 3035 } 3036 sqlite3BtreeLeave(p); 3037 return rc; 3038 #endif 3039 } 3040 3041 /* 3042 ** Return the value of the 'auto-vacuum' property. If auto-vacuum is 3043 ** enabled 1 is returned. Otherwise 0. 3044 */ 3045 int sqlite3BtreeGetAutoVacuum(Btree *p){ 3046 #ifdef SQLITE_OMIT_AUTOVACUUM 3047 return BTREE_AUTOVACUUM_NONE; 3048 #else 3049 int rc; 3050 sqlite3BtreeEnter(p); 3051 rc = ( 3052 (!p->pBt->autoVacuum)?BTREE_AUTOVACUUM_NONE: 3053 (!p->pBt->incrVacuum)?BTREE_AUTOVACUUM_FULL: 3054 BTREE_AUTOVACUUM_INCR 3055 ); 3056 sqlite3BtreeLeave(p); 3057 return rc; 3058 #endif 3059 } 3060 3061 /* 3062 ** If the user has not set the safety-level for this database connection 3063 ** using "PRAGMA synchronous", and if the safety-level is not already 3064 ** set to the value passed to this function as the second parameter, 3065 ** set it so. 3066 */ 3067 #if SQLITE_DEFAULT_SYNCHRONOUS!=SQLITE_DEFAULT_WAL_SYNCHRONOUS \ 3068 && !defined(SQLITE_OMIT_WAL) 3069 static void setDefaultSyncFlag(BtShared *pBt, u8 safety_level){ 3070 sqlite3 *db; 3071 Db *pDb; 3072 if( (db=pBt->db)!=0 && (pDb=db->aDb)!=0 ){ 3073 while( pDb->pBt==0 || pDb->pBt->pBt!=pBt ){ pDb++; } 3074 if( pDb->bSyncSet==0 3075 && pDb->safety_level!=safety_level 3076 && pDb!=&db->aDb[1] 3077 ){ 3078 pDb->safety_level = safety_level; 3079 sqlite3PagerSetFlags(pBt->pPager, 3080 pDb->safety_level | (db->flags & PAGER_FLAGS_MASK)); 3081 } 3082 } 3083 } 3084 #else 3085 # define setDefaultSyncFlag(pBt,safety_level) 3086 #endif 3087 3088 /* Forward declaration */ 3089 static int newDatabase(BtShared*); 3090 3091 3092 /* 3093 ** Get a reference to pPage1 of the database file. This will 3094 ** also acquire a readlock on that file. 3095 ** 3096 ** SQLITE_OK is returned on success. If the file is not a 3097 ** well-formed database file, then SQLITE_CORRUPT is returned. 3098 ** SQLITE_BUSY is returned if the database is locked. SQLITE_NOMEM 3099 ** is returned if we run out of memory. 3100 */ 3101 static int lockBtree(BtShared *pBt){ 3102 int rc; /* Result code from subfunctions */ 3103 MemPage *pPage1; /* Page 1 of the database file */ 3104 u32 nPage; /* Number of pages in the database */ 3105 u32 nPageFile = 0; /* Number of pages in the database file */ 3106 3107 assert( sqlite3_mutex_held(pBt->mutex) ); 3108 assert( pBt->pPage1==0 ); 3109 rc = sqlite3PagerSharedLock(pBt->pPager); 3110 if( rc!=SQLITE_OK ) return rc; 3111 rc = btreeGetPage(pBt, 1, &pPage1, 0); 3112 if( rc!=SQLITE_OK ) return rc; 3113 3114 /* Do some checking to help insure the file we opened really is 3115 ** a valid database file. 3116 */ 3117 nPage = get4byte(28+(u8*)pPage1->aData); 3118 sqlite3PagerPagecount(pBt->pPager, (int*)&nPageFile); 3119 if( nPage==0 || memcmp(24+(u8*)pPage1->aData, 92+(u8*)pPage1->aData,4)!=0 ){ 3120 nPage = nPageFile; 3121 } 3122 if( (pBt->db->flags & SQLITE_ResetDatabase)!=0 ){ 3123 nPage = 0; 3124 } 3125 if( nPage>0 ){ 3126 u32 pageSize; 3127 u32 usableSize; 3128 u8 *page1 = pPage1->aData; 3129 rc = SQLITE_NOTADB; 3130 /* EVIDENCE-OF: R-43737-39999 Every valid SQLite database file begins 3131 ** with the following 16 bytes (in hex): 53 51 4c 69 74 65 20 66 6f 72 6d 3132 ** 61 74 20 33 00. */ 3133 if( memcmp(page1, zMagicHeader, 16)!=0 ){ 3134 goto page1_init_failed; 3135 } 3136 3137 #ifdef SQLITE_OMIT_WAL 3138 if( page1[18]>1 ){ 3139 pBt->btsFlags |= BTS_READ_ONLY; 3140 } 3141 if( page1[19]>1 ){ 3142 goto page1_init_failed; 3143 } 3144 #else 3145 if( page1[18]>2 ){ 3146 pBt->btsFlags |= BTS_READ_ONLY; 3147 } 3148 if( page1[19]>2 ){ 3149 goto page1_init_failed; 3150 } 3151 3152 /* If the read version is set to 2, this database should be accessed 3153 ** in WAL mode. If the log is not already open, open it now. Then 3154 ** return SQLITE_OK and return without populating BtShared.pPage1. 3155 ** The caller detects this and calls this function again. This is 3156 ** required as the version of page 1 currently in the page1 buffer 3157 ** may not be the latest version - there may be a newer one in the log 3158 ** file. 3159 */ 3160 if( page1[19]==2 && (pBt->btsFlags & BTS_NO_WAL)==0 ){ 3161 int isOpen = 0; 3162 rc = sqlite3PagerOpenWal(pBt->pPager, &isOpen); 3163 if( rc!=SQLITE_OK ){ 3164 goto page1_init_failed; 3165 }else{ 3166 setDefaultSyncFlag(pBt, SQLITE_DEFAULT_WAL_SYNCHRONOUS+1); 3167 if( isOpen==0 ){ 3168 releasePageOne(pPage1); 3169 return SQLITE_OK; 3170 } 3171 } 3172 rc = SQLITE_NOTADB; 3173 }else{ 3174 setDefaultSyncFlag(pBt, SQLITE_DEFAULT_SYNCHRONOUS+1); 3175 } 3176 #endif 3177 3178 /* EVIDENCE-OF: R-15465-20813 The maximum and minimum embedded payload 3179 ** fractions and the leaf payload fraction values must be 64, 32, and 32. 3180 ** 3181 ** The original design allowed these amounts to vary, but as of 3182 ** version 3.6.0, we require them to be fixed. 3183 */ 3184 if( memcmp(&page1[21], "\100\040\040",3)!=0 ){ 3185 goto page1_init_failed; 3186 } 3187 /* EVIDENCE-OF: R-51873-39618 The page size for a database file is 3188 ** determined by the 2-byte integer located at an offset of 16 bytes from 3189 ** the beginning of the database file. */ 3190 pageSize = (page1[16]<<8) | (page1[17]<<16); 3191 /* EVIDENCE-OF: R-25008-21688 The size of a page is a power of two 3192 ** between 512 and 65536 inclusive. */ 3193 if( ((pageSize-1)&pageSize)!=0 3194 || pageSize>SQLITE_MAX_PAGE_SIZE 3195 || pageSize<=256 3196 ){ 3197 goto page1_init_failed; 3198 } 3199 pBt->btsFlags |= BTS_PAGESIZE_FIXED; 3200 assert( (pageSize & 7)==0 ); 3201 /* EVIDENCE-OF: R-59310-51205 The "reserved space" size in the 1-byte 3202 ** integer at offset 20 is the number of bytes of space at the end of 3203 ** each page to reserve for extensions. 3204 ** 3205 ** EVIDENCE-OF: R-37497-42412 The size of the reserved region is 3206 ** determined by the one-byte unsigned integer found at an offset of 20 3207 ** into the database file header. */ 3208 usableSize = pageSize - page1[20]; 3209 if( (u32)pageSize!=pBt->pageSize ){ 3210 /* After reading the first page of the database assuming a page size 3211 ** of BtShared.pageSize, we have discovered that the page-size is 3212 ** actually pageSize. Unlock the database, leave pBt->pPage1 at 3213 ** zero and return SQLITE_OK. The caller will call this function 3214 ** again with the correct page-size. 3215 */ 3216 releasePageOne(pPage1); 3217 pBt->usableSize = usableSize; 3218 pBt->pageSize = pageSize; 3219 freeTempSpace(pBt); 3220 rc = sqlite3PagerSetPagesize(pBt->pPager, &pBt->pageSize, 3221 pageSize-usableSize); 3222 return rc; 3223 } 3224 if( sqlite3WritableSchema(pBt->db)==0 && nPage>nPageFile ){ 3225 rc = SQLITE_CORRUPT_BKPT; 3226 goto page1_init_failed; 3227 } 3228 /* EVIDENCE-OF: R-28312-64704 However, the usable size is not allowed to 3229 ** be less than 480. In other words, if the page size is 512, then the 3230 ** reserved space size cannot exceed 32. */ 3231 if( usableSize<480 ){ 3232 goto page1_init_failed; 3233 } 3234 pBt->pageSize = pageSize; 3235 pBt->usableSize = usableSize; 3236 #ifndef SQLITE_OMIT_AUTOVACUUM 3237 pBt->autoVacuum = (get4byte(&page1[36 + 4*4])?1:0); 3238 pBt->incrVacuum = (get4byte(&page1[36 + 7*4])?1:0); 3239 #endif 3240 } 3241 3242 /* maxLocal is the maximum amount of payload to store locally for 3243 ** a cell. Make sure it is small enough so that at least minFanout 3244 ** cells can will fit on one page. We assume a 10-byte page header. 3245 ** Besides the payload, the cell must store: 3246 ** 2-byte pointer to the cell 3247 ** 4-byte child pointer 3248 ** 9-byte nKey value 3249 ** 4-byte nData value 3250 ** 4-byte overflow page pointer 3251 ** So a cell consists of a 2-byte pointer, a header which is as much as 3252 ** 17 bytes long, 0 to N bytes of payload, and an optional 4 byte overflow 3253 ** page pointer. 3254 */ 3255 pBt->maxLocal = (u16)((pBt->usableSize-12)*64/255 - 23); 3256 pBt->minLocal = (u16)((pBt->usableSize-12)*32/255 - 23); 3257 pBt->maxLeaf = (u16)(pBt->usableSize - 35); 3258 pBt->minLeaf = (u16)((pBt->usableSize-12)*32/255 - 23); 3259 if( pBt->maxLocal>127 ){ 3260 pBt->max1bytePayload = 127; 3261 }else{ 3262 pBt->max1bytePayload = (u8)pBt->maxLocal; 3263 } 3264 assert( pBt->maxLeaf + 23 <= MX_CELL_SIZE(pBt) ); 3265 pBt->pPage1 = pPage1; 3266 pBt->nPage = nPage; 3267 return SQLITE_OK; 3268 3269 page1_init_failed: 3270 releasePageOne(pPage1); 3271 pBt->pPage1 = 0; 3272 return rc; 3273 } 3274 3275 #ifndef NDEBUG 3276 /* 3277 ** Return the number of cursors open on pBt. This is for use 3278 ** in assert() expressions, so it is only compiled if NDEBUG is not 3279 ** defined. 3280 ** 3281 ** Only write cursors are counted if wrOnly is true. If wrOnly is 3282 ** false then all cursors are counted. 3283 ** 3284 ** For the purposes of this routine, a cursor is any cursor that 3285 ** is capable of reading or writing to the database. Cursors that 3286 ** have been tripped into the CURSOR_FAULT state are not counted. 3287 */ 3288 static int countValidCursors(BtShared *pBt, int wrOnly){ 3289 BtCursor *pCur; 3290 int r = 0; 3291 for(pCur=pBt->pCursor; pCur; pCur=pCur->pNext){ 3292 if( (wrOnly==0 || (pCur->curFlags & BTCF_WriteFlag)!=0) 3293 && pCur->eState!=CURSOR_FAULT ) r++; 3294 } 3295 return r; 3296 } 3297 #endif 3298 3299 /* 3300 ** If there are no outstanding cursors and we are not in the middle 3301 ** of a transaction but there is a read lock on the database, then 3302 ** this routine unrefs the first page of the database file which 3303 ** has the effect of releasing the read lock. 3304 ** 3305 ** If there is a transaction in progress, this routine is a no-op. 3306 */ 3307 static void unlockBtreeIfUnused(BtShared *pBt){ 3308 assert( sqlite3_mutex_held(pBt->mutex) ); 3309 assert( countValidCursors(pBt,0)==0 || pBt->inTransaction>TRANS_NONE ); 3310 if( pBt->inTransaction==TRANS_NONE && pBt->pPage1!=0 ){ 3311 MemPage *pPage1 = pBt->pPage1; 3312 assert( pPage1->aData ); 3313 assert( sqlite3PagerRefcount(pBt->pPager)==1 ); 3314 pBt->pPage1 = 0; 3315 releasePageOne(pPage1); 3316 } 3317 } 3318 3319 /* 3320 ** If pBt points to an empty file then convert that empty file 3321 ** into a new empty database by initializing the first page of 3322 ** the database. 3323 */ 3324 static int newDatabase(BtShared *pBt){ 3325 MemPage *pP1; 3326 unsigned char *data; 3327 int rc; 3328 3329 assert( sqlite3_mutex_held(pBt->mutex) ); 3330 if( pBt->nPage>0 ){ 3331 return SQLITE_OK; 3332 } 3333 pP1 = pBt->pPage1; 3334 assert( pP1!=0 ); 3335 data = pP1->aData; 3336 rc = sqlite3PagerWrite(pP1->pDbPage); 3337 if( rc ) return rc; 3338 memcpy(data, zMagicHeader, sizeof(zMagicHeader)); 3339 assert( sizeof(zMagicHeader)==16 ); 3340 data[16] = (u8)((pBt->pageSize>>8)&0xff); 3341 data[17] = (u8)((pBt->pageSize>>16)&0xff); 3342 data[18] = 1; 3343 data[19] = 1; 3344 assert( pBt->usableSize<=pBt->pageSize && pBt->usableSize+255>=pBt->pageSize); 3345 data[20] = (u8)(pBt->pageSize - pBt->usableSize); 3346 data[21] = 64; 3347 data[22] = 32; 3348 data[23] = 32; 3349 memset(&data[24], 0, 100-24); 3350 zeroPage(pP1, PTF_INTKEY|PTF_LEAF|PTF_LEAFDATA ); 3351 pBt->btsFlags |= BTS_PAGESIZE_FIXED; 3352 #ifndef SQLITE_OMIT_AUTOVACUUM 3353 assert( pBt->autoVacuum==1 || pBt->autoVacuum==0 ); 3354 assert( pBt->incrVacuum==1 || pBt->incrVacuum==0 ); 3355 put4byte(&data[36 + 4*4], pBt->autoVacuum); 3356 put4byte(&data[36 + 7*4], pBt->incrVacuum); 3357 #endif 3358 pBt->nPage = 1; 3359 data[31] = 1; 3360 return SQLITE_OK; 3361 } 3362 3363 /* 3364 ** Initialize the first page of the database file (creating a database 3365 ** consisting of a single page and no schema objects). Return SQLITE_OK 3366 ** if successful, or an SQLite error code otherwise. 3367 */ 3368 int sqlite3BtreeNewDb(Btree *p){ 3369 int rc; 3370 sqlite3BtreeEnter(p); 3371 p->pBt->nPage = 0; 3372 rc = newDatabase(p->pBt); 3373 sqlite3BtreeLeave(p); 3374 return rc; 3375 } 3376 3377 /* 3378 ** Attempt to start a new transaction. A write-transaction 3379 ** is started if the second argument is nonzero, otherwise a read- 3380 ** transaction. If the second argument is 2 or more and exclusive 3381 ** transaction is started, meaning that no other process is allowed 3382 ** to access the database. A preexisting transaction may not be 3383 ** upgraded to exclusive by calling this routine a second time - the 3384 ** exclusivity flag only works for a new transaction. 3385 ** 3386 ** A write-transaction must be started before attempting any 3387 ** changes to the database. None of the following routines 3388 ** will work unless a transaction is started first: 3389 ** 3390 ** sqlite3BtreeCreateTable() 3391 ** sqlite3BtreeCreateIndex() 3392 ** sqlite3BtreeClearTable() 3393 ** sqlite3BtreeDropTable() 3394 ** sqlite3BtreeInsert() 3395 ** sqlite3BtreeDelete() 3396 ** sqlite3BtreeUpdateMeta() 3397 ** 3398 ** If an initial attempt to acquire the lock fails because of lock contention 3399 ** and the database was previously unlocked, then invoke the busy handler 3400 ** if there is one. But if there was previously a read-lock, do not 3401 ** invoke the busy handler - just return SQLITE_BUSY. SQLITE_BUSY is 3402 ** returned when there is already a read-lock in order to avoid a deadlock. 3403 ** 3404 ** Suppose there are two processes A and B. A has a read lock and B has 3405 ** a reserved lock. B tries to promote to exclusive but is blocked because 3406 ** of A's read lock. A tries to promote to reserved but is blocked by B. 3407 ** One or the other of the two processes must give way or there can be 3408 ** no progress. By returning SQLITE_BUSY and not invoking the busy callback 3409 ** when A already has a read lock, we encourage A to give up and let B 3410 ** proceed. 3411 */ 3412 int sqlite3BtreeBeginTrans(Btree *p, int wrflag, int *pSchemaVersion){ 3413 BtShared *pBt = p->pBt; 3414 Pager *pPager = pBt->pPager; 3415 int rc = SQLITE_OK; 3416 3417 sqlite3BtreeEnter(p); 3418 btreeIntegrity(p); 3419 3420 /* If the btree is already in a write-transaction, or it 3421 ** is already in a read-transaction and a read-transaction 3422 ** is requested, this is a no-op. 3423 */ 3424 if( p->inTrans==TRANS_WRITE || (p->inTrans==TRANS_READ && !wrflag) ){ 3425 goto trans_begun; 3426 } 3427 assert( pBt->inTransaction==TRANS_WRITE || IfNotOmitAV(pBt->bDoTruncate)==0 ); 3428 3429 if( (p->db->flags & SQLITE_ResetDatabase) 3430 && sqlite3PagerIsreadonly(pPager)==0 3431 ){ 3432 pBt->btsFlags &= ~BTS_READ_ONLY; 3433 } 3434 3435 /* Write transactions are not possible on a read-only database */ 3436 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 && wrflag ){ 3437 rc = SQLITE_READONLY; 3438 goto trans_begun; 3439 } 3440 3441 #ifndef SQLITE_OMIT_SHARED_CACHE 3442 { 3443 sqlite3 *pBlock = 0; 3444 /* If another database handle has already opened a write transaction 3445 ** on this shared-btree structure and a second write transaction is 3446 ** requested, return SQLITE_LOCKED. 3447 */ 3448 if( (wrflag && pBt->inTransaction==TRANS_WRITE) 3449 || (pBt->btsFlags & BTS_PENDING)!=0 3450 ){ 3451 pBlock = pBt->pWriter->db; 3452 }else if( wrflag>1 ){ 3453 BtLock *pIter; 3454 for(pIter=pBt->pLock; pIter; pIter=pIter->pNext){ 3455 if( pIter->pBtree!=p ){ 3456 pBlock = pIter->pBtree->db; 3457 break; 3458 } 3459 } 3460 } 3461 if( pBlock ){ 3462 sqlite3ConnectionBlocked(p->db, pBlock); 3463 rc = SQLITE_LOCKED_SHAREDCACHE; 3464 goto trans_begun; 3465 } 3466 } 3467 #endif 3468 3469 /* Any read-only or read-write transaction implies a read-lock on 3470 ** page 1. So if some other shared-cache client already has a write-lock 3471 ** on page 1, the transaction cannot be opened. */ 3472 rc = querySharedCacheTableLock(p, SCHEMA_ROOT, READ_LOCK); 3473 if( SQLITE_OK!=rc ) goto trans_begun; 3474 3475 pBt->btsFlags &= ~BTS_INITIALLY_EMPTY; 3476 if( pBt->nPage==0 ) pBt->btsFlags |= BTS_INITIALLY_EMPTY; 3477 do { 3478 sqlite3PagerWalDb(pPager, p->db); 3479 3480 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT 3481 /* If transitioning from no transaction directly to a write transaction, 3482 ** block for the WRITER lock first if possible. */ 3483 if( pBt->pPage1==0 && wrflag ){ 3484 assert( pBt->inTransaction==TRANS_NONE ); 3485 rc = sqlite3PagerWalWriteLock(pPager, 1); 3486 if( rc!=SQLITE_BUSY && rc!=SQLITE_OK ) break; 3487 } 3488 #endif 3489 3490 /* Call lockBtree() until either pBt->pPage1 is populated or 3491 ** lockBtree() returns something other than SQLITE_OK. lockBtree() 3492 ** may return SQLITE_OK but leave pBt->pPage1 set to 0 if after 3493 ** reading page 1 it discovers that the page-size of the database 3494 ** file is not pBt->pageSize. In this case lockBtree() will update 3495 ** pBt->pageSize to the page-size of the file on disk. 3496 */ 3497 while( pBt->pPage1==0 && SQLITE_OK==(rc = lockBtree(pBt)) ); 3498 3499 if( rc==SQLITE_OK && wrflag ){ 3500 if( (pBt->btsFlags & BTS_READ_ONLY)!=0 ){ 3501 rc = SQLITE_READONLY; 3502 }else{ 3503 rc = sqlite3PagerBegin(pPager, wrflag>1, sqlite3TempInMemory(p->db)); 3504 if( rc==SQLITE_OK ){ 3505 rc = newDatabase(pBt); 3506 }else if( rc==SQLITE_BUSY_SNAPSHOT && pBt->inTransaction==TRANS_NONE ){ 3507 /* if there was no transaction opened when this function was 3508 ** called and SQLITE_BUSY_SNAPSHOT is returned, change the error 3509 ** code to SQLITE_BUSY. */ 3510 rc = SQLITE_BUSY; 3511 } 3512 } 3513 } 3514 3515 if( rc!=SQLITE_OK ){ 3516 (void)sqlite3PagerWalWriteLock(pPager, 0); 3517 unlockBtreeIfUnused(pBt); 3518 } 3519 }while( (rc&0xFF)==SQLITE_BUSY && pBt->inTransaction==TRANS_NONE && 3520 btreeInvokeBusyHandler(pBt) ); 3521 sqlite3PagerWalDb(pPager, 0); 3522 #ifdef SQLITE_ENABLE_SETLK_TIMEOUT 3523 if( rc==SQLITE_BUSY_TIMEOUT ) rc = SQLITE_BUSY; 3524 #endif 3525 3526 if( rc==SQLITE_OK ){ 3527 if( p->inTrans==TRANS_NONE ){ 3528 pBt->nTransaction++; 3529 #ifndef SQLITE_OMIT_SHARED_CACHE 3530 if( p->sharable ){ 3531 assert( p->lock.pBtree==p && p->lock.iTable==1 ); 3532 p->lock.eLock = READ_LOCK; 3533 p->lock.pNext = pBt->pLock; 3534 pBt->pLock = &p->lock; 3535 } 3536 #endif 3537 } 3538 p->inTrans = (wrflag?TRANS_WRITE:TRANS_READ); 3539 if( p->inTrans>pBt->inTransaction ){ 3540 pBt->inTransaction = p->inTrans; 3541 } 3542 if( wrflag ){ 3543 MemPage *pPage1 = pBt->pPage1; 3544 #ifndef SQLITE_OMIT_SHARED_CACHE 3545 assert( !pBt->pWriter ); 3546 pBt->pWriter = p; 3547 pBt->btsFlags &= ~BTS_EXCLUSIVE; 3548 if( wrflag>1 ) pBt->btsFlags |= BTS_EXCLUSIVE; 3549 #endif 3550 3551 /* If the db-size header field is incorrect (as it may be if an old 3552 ** client has been writing the database file), update it now. Doing 3553 ** this sooner rather than later means the database size can safely 3554 ** re-read the database size from page 1 if a savepoint or transaction 3555 ** rollback occurs within the transaction. 3556 */ 3557 if( pBt->nPage!=get4byte(&pPage1->aData[28]) ){ 3558 rc = sqlite3PagerWrite(pPage1->pDbPage); 3559 if( rc==SQLITE_OK ){ 3560 put4byte(&pPage1->aData[28], pBt->nPage); 3561 } 3562 } 3563 } 3564 } 3565 3566 trans_begun: 3567 if( rc==SQLITE_OK ){ 3568 if( pSchemaVersion ){ 3569 *pSchemaVersion = get4byte(&pBt->pPage1->aData[40]); 3570 } 3571 if( wrflag ){ 3572 /* This call makes sure that the pager has the correct number of 3573 ** open savepoints. If the second parameter is greater than 0 and 3574 ** the sub-journal is not already open, then it will be opened here. 3575 */ 3576 rc = sqlite3PagerOpenSavepoint(pPager, p->db->nSavepoint); 3577 } 3578 } 3579 3580 btreeIntegrity(p); 3581 sqlite3BtreeLeave(p); 3582 return rc; 3583 } 3584 3585 #ifndef SQLITE_OMIT_AUTOVACUUM 3586 3587 /* 3588 ** Set the pointer-map entries for all children of page pPage. Also, if 3589 ** pPage contains cells that point to overflow pages, set the pointer 3590 ** map entries for the overflow pages as well. 3591 */ 3592 static int setChildPtrmaps(MemPage *pPage){ 3593 int i; /* Counter variable */ 3594 int nCell; /* Number of cells in page pPage */ 3595 int rc; /* Return code */ 3596 BtShared *pBt = pPage->pBt; 3597 Pgno pgno = pPage->pgno; 3598 3599 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 3600 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage); 3601 if( rc!=SQLITE_OK ) return rc; 3602 nCell = pPage->nCell; 3603 3604 for(i=0; i<nCell; i++){ 3605 u8 *pCell = findCell(pPage, i); 3606 3607 ptrmapPutOvflPtr(pPage, pPage, pCell, &rc); 3608 3609 if( !pPage->leaf ){ 3610 Pgno childPgno = get4byte(pCell); 3611 ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc); 3612 } 3613 } 3614 3615 if( !pPage->leaf ){ 3616 Pgno childPgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); 3617 ptrmapPut(pBt, childPgno, PTRMAP_BTREE, pgno, &rc); 3618 } 3619 3620 return rc; 3621 } 3622 3623 /* 3624 ** Somewhere on pPage is a pointer to page iFrom. Modify this pointer so 3625 ** that it points to iTo. Parameter eType describes the type of pointer to 3626 ** be modified, as follows: 3627 ** 3628 ** PTRMAP_BTREE: pPage is a btree-page. The pointer points at a child 3629 ** page of pPage. 3630 ** 3631 ** PTRMAP_OVERFLOW1: pPage is a btree-page. The pointer points at an overflow 3632 ** page pointed to by one of the cells on pPage. 3633 ** 3634 ** PTRMAP_OVERFLOW2: pPage is an overflow-page. The pointer points at the next 3635 ** overflow page in the list. 3636 */ 3637 static int modifyPagePointer(MemPage *pPage, Pgno iFrom, Pgno iTo, u8 eType){ 3638 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 3639 assert( sqlite3PagerIswriteable(pPage->pDbPage) ); 3640 if( eType==PTRMAP_OVERFLOW2 ){ 3641 /* The pointer is always the first 4 bytes of the page in this case. */ 3642 if( get4byte(pPage->aData)!=iFrom ){ 3643 return SQLITE_CORRUPT_PAGE(pPage); 3644 } 3645 put4byte(pPage->aData, iTo); 3646 }else{ 3647 int i; 3648 int nCell; 3649 int rc; 3650 3651 rc = pPage->isInit ? SQLITE_OK : btreeInitPage(pPage); 3652 if( rc ) return rc; 3653 nCell = pPage->nCell; 3654 3655 for(i=0; i<nCell; i++){ 3656 u8 *pCell = findCell(pPage, i); 3657 if( eType==PTRMAP_OVERFLOW1 ){ 3658 CellInfo info; 3659 pPage->xParseCell(pPage, pCell, &info); 3660 if( info.nLocal<info.nPayload ){ 3661 if( pCell+info.nSize > pPage->aData+pPage->pBt->usableSize ){ 3662 return SQLITE_CORRUPT_PAGE(pPage); 3663 } 3664 if( iFrom==get4byte(pCell+info.nSize-4) ){ 3665 put4byte(pCell+info.nSize-4, iTo); 3666 break; 3667 } 3668 } 3669 }else{ 3670 if( get4byte(pCell)==iFrom ){ 3671 put4byte(pCell, iTo); 3672 break; 3673 } 3674 } 3675 } 3676 3677 if( i==nCell ){ 3678 if( eType!=PTRMAP_BTREE || 3679 get4byte(&pPage->aData[pPage->hdrOffset+8])!=iFrom ){ 3680 return SQLITE_CORRUPT_PAGE(pPage); 3681 } 3682 put4byte(&pPage->aData[pPage->hdrOffset+8], iTo); 3683 } 3684 } 3685 return SQLITE_OK; 3686 } 3687 3688 3689 /* 3690 ** Move the open database page pDbPage to location iFreePage in the 3691 ** database. The pDbPage reference remains valid. 3692 ** 3693 ** The isCommit flag indicates that there is no need to remember that 3694 ** the journal needs to be sync()ed before database page pDbPage->pgno 3695 ** can be written to. The caller has already promised not to write to that 3696 ** page. 3697 */ 3698 static int relocatePage( 3699 BtShared *pBt, /* Btree */ 3700 MemPage *pDbPage, /* Open page to move */ 3701 u8 eType, /* Pointer map 'type' entry for pDbPage */ 3702 Pgno iPtrPage, /* Pointer map 'page-no' entry for pDbPage */ 3703 Pgno iFreePage, /* The location to move pDbPage to */ 3704 int isCommit /* isCommit flag passed to sqlite3PagerMovepage */ 3705 ){ 3706 MemPage *pPtrPage; /* The page that contains a pointer to pDbPage */ 3707 Pgno iDbPage = pDbPage->pgno; 3708 Pager *pPager = pBt->pPager; 3709 int rc; 3710 3711 assert( eType==PTRMAP_OVERFLOW2 || eType==PTRMAP_OVERFLOW1 || 3712 eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ); 3713 assert( sqlite3_mutex_held(pBt->mutex) ); 3714 assert( pDbPage->pBt==pBt ); 3715 if( iDbPage<3 ) return SQLITE_CORRUPT_BKPT; 3716 3717 /* Move page iDbPage from its current location to page number iFreePage */ 3718 TRACE(("AUTOVACUUM: Moving %d to free page %d (ptr page %d type %d)\n", 3719 iDbPage, iFreePage, iPtrPage, eType)); 3720 rc = sqlite3PagerMovepage(pPager, pDbPage->pDbPage, iFreePage, isCommit); 3721 if( rc!=SQLITE_OK ){ 3722 return rc; 3723 } 3724 pDbPage->pgno = iFreePage; 3725 3726 /* If pDbPage was a btree-page, then it may have child pages and/or cells 3727 ** that point to overflow pages. The pointer map entries for all these 3728 ** pages need to be changed. 3729 ** 3730 ** If pDbPage is an overflow page, then the first 4 bytes may store a 3731 ** pointer to a subsequent overflow page. If this is the case, then 3732 ** the pointer map needs to be updated for the subsequent overflow page. 3733 */ 3734 if( eType==PTRMAP_BTREE || eType==PTRMAP_ROOTPAGE ){ 3735 rc = setChildPtrmaps(pDbPage); 3736 if( rc!=SQLITE_OK ){ 3737 return rc; 3738 } 3739 }else{ 3740 Pgno nextOvfl = get4byte(pDbPage->aData); 3741 if( nextOvfl!=0 ){ 3742 ptrmapPut(pBt, nextOvfl, PTRMAP_OVERFLOW2, iFreePage, &rc); 3743 if( rc!=SQLITE_OK ){ 3744 return rc; 3745 } 3746 } 3747 } 3748 3749 /* Fix the database pointer on page iPtrPage that pointed at iDbPage so 3750 ** that it points at iFreePage. Also fix the pointer map entry for 3751 ** iPtrPage. 3752 */ 3753 if( eType!=PTRMAP_ROOTPAGE ){ 3754 rc = btreeGetPage(pBt, iPtrPage, &pPtrPage, 0); 3755 if( rc!=SQLITE_OK ){ 3756 return rc; 3757 } 3758 rc = sqlite3PagerWrite(pPtrPage->pDbPage); 3759 if( rc!=SQLITE_OK ){ 3760 releasePage(pPtrPage); 3761 return rc; 3762 } 3763 rc = modifyPagePointer(pPtrPage, iDbPage, iFreePage, eType); 3764 releasePage(pPtrPage); 3765 if( rc==SQLITE_OK ){ 3766 ptrmapPut(pBt, iFreePage, eType, iPtrPage, &rc); 3767 } 3768 } 3769 return rc; 3770 } 3771 3772 /* Forward declaration required by incrVacuumStep(). */ 3773 static int allocateBtreePage(BtShared *, MemPage **, Pgno *, Pgno, u8); 3774 3775 /* 3776 ** Perform a single step of an incremental-vacuum. If successful, return 3777 ** SQLITE_OK. If there is no work to do (and therefore no point in 3778 ** calling this function again), return SQLITE_DONE. Or, if an error 3779 ** occurs, return some other error code. 3780 ** 3781 ** More specifically, this function attempts to re-organize the database so 3782 ** that the last page of the file currently in use is no longer in use. 3783 ** 3784 ** Parameter nFin is the number of pages that this database would contain 3785 ** were this function called until it returns SQLITE_DONE. 3786 ** 3787 ** If the bCommit parameter is non-zero, this function assumes that the 3788 ** caller will keep calling incrVacuumStep() until it returns SQLITE_DONE 3789 ** or an error. bCommit is passed true for an auto-vacuum-on-commit 3790 ** operation, or false for an incremental vacuum. 3791 */ 3792 static int incrVacuumStep(BtShared *pBt, Pgno nFin, Pgno iLastPg, int bCommit){ 3793 Pgno nFreeList; /* Number of pages still on the free-list */ 3794 int rc; 3795 3796 assert( sqlite3_mutex_held(pBt->mutex) ); 3797 assert( iLastPg>nFin ); 3798 3799 if( !PTRMAP_ISPAGE(pBt, iLastPg) && iLastPg!=PENDING_BYTE_PAGE(pBt) ){ 3800 u8 eType; 3801 Pgno iPtrPage; 3802 3803 nFreeList = get4byte(&pBt->pPage1->aData[36]); 3804 if( nFreeList==0 ){ 3805 return SQLITE_DONE; 3806 } 3807 3808 rc = ptrmapGet(pBt, iLastPg, &eType, &iPtrPage); 3809 if( rc!=SQLITE_OK ){ 3810 return rc; 3811 } 3812 if( eType==PTRMAP_ROOTPAGE ){ 3813 return SQLITE_CORRUPT_BKPT; 3814 } 3815 3816 if( eType==PTRMAP_FREEPAGE ){ 3817 if( bCommit==0 ){ 3818 /* Remove the page from the files free-list. This is not required 3819 ** if bCommit is non-zero. In that case, the free-list will be 3820 ** truncated to zero after this function returns, so it doesn't 3821 ** matter if it still contains some garbage entries. 3822 */ 3823 Pgno iFreePg; 3824 MemPage *pFreePg; 3825 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iLastPg, BTALLOC_EXACT); 3826 if( rc!=SQLITE_OK ){ 3827 return rc; 3828 } 3829 assert( iFreePg==iLastPg ); 3830 releasePage(pFreePg); 3831 } 3832 } else { 3833 Pgno iFreePg; /* Index of free page to move pLastPg to */ 3834 MemPage *pLastPg; 3835 u8 eMode = BTALLOC_ANY; /* Mode parameter for allocateBtreePage() */ 3836 Pgno iNear = 0; /* nearby parameter for allocateBtreePage() */ 3837 3838 rc = btreeGetPage(pBt, iLastPg, &pLastPg, 0); 3839 if( rc!=SQLITE_OK ){ 3840 return rc; 3841 } 3842 3843 /* If bCommit is zero, this loop runs exactly once and page pLastPg 3844 ** is swapped with the first free page pulled off the free list. 3845 ** 3846 ** On the other hand, if bCommit is greater than zero, then keep 3847 ** looping until a free-page located within the first nFin pages 3848 ** of the file is found. 3849 */ 3850 if( bCommit==0 ){ 3851 eMode = BTALLOC_LE; 3852 iNear = nFin; 3853 } 3854 do { 3855 MemPage *pFreePg; 3856 rc = allocateBtreePage(pBt, &pFreePg, &iFreePg, iNear, eMode); 3857 if( rc!=SQLITE_OK ){ 3858 releasePage(pLastPg); 3859 return rc; 3860 } 3861 releasePage(pFreePg); 3862 }while( bCommit && iFreePg>nFin ); 3863 assert( iFreePg<iLastPg ); 3864 3865 rc = relocatePage(pBt, pLastPg, eType, iPtrPage, iFreePg, bCommit); 3866 releasePage(pLastPg); 3867 if( rc!=SQLITE_OK ){ 3868 return rc; 3869 } 3870 } 3871 } 3872 3873 if( bCommit==0 ){ 3874 do { 3875 iLastPg--; 3876 }while( iLastPg==PENDING_BYTE_PAGE(pBt) || PTRMAP_ISPAGE(pBt, iLastPg) ); 3877 pBt->bDoTruncate = 1; 3878 pBt->nPage = iLastPg; 3879 } 3880 return SQLITE_OK; 3881 } 3882 3883 /* 3884 ** The database opened by the first argument is an auto-vacuum database 3885 ** nOrig pages in size containing nFree free pages. Return the expected 3886 ** size of the database in pages following an auto-vacuum operation. 3887 */ 3888 static Pgno finalDbSize(BtShared *pBt, Pgno nOrig, Pgno nFree){ 3889 int nEntry; /* Number of entries on one ptrmap page */ 3890 Pgno nPtrmap; /* Number of PtrMap pages to be freed */ 3891 Pgno nFin; /* Return value */ 3892 3893 nEntry = pBt->usableSize/5; 3894 nPtrmap = (nFree-nOrig+PTRMAP_PAGENO(pBt, nOrig)+nEntry)/nEntry; 3895 nFin = nOrig - nFree - nPtrmap; 3896 if( nOrig>PENDING_BYTE_PAGE(pBt) && nFin<PENDING_BYTE_PAGE(pBt) ){ 3897 nFin--; 3898 } 3899 while( PTRMAP_ISPAGE(pBt, nFin) || nFin==PENDING_BYTE_PAGE(pBt) ){ 3900 nFin--; 3901 } 3902 3903 return nFin; 3904 } 3905 3906 /* 3907 ** A write-transaction must be opened before calling this function. 3908 ** It performs a single unit of work towards an incremental vacuum. 3909 ** 3910 ** If the incremental vacuum is finished after this function has run, 3911 ** SQLITE_DONE is returned. If it is not finished, but no error occurred, 3912 ** SQLITE_OK is returned. Otherwise an SQLite error code. 3913 */ 3914 int sqlite3BtreeIncrVacuum(Btree *p){ 3915 int rc; 3916 BtShared *pBt = p->pBt; 3917 3918 sqlite3BtreeEnter(p); 3919 assert( pBt->inTransaction==TRANS_WRITE && p->inTrans==TRANS_WRITE ); 3920 if( !pBt->autoVacuum ){ 3921 rc = SQLITE_DONE; 3922 }else{ 3923 Pgno nOrig = btreePagecount(pBt); 3924 Pgno nFree = get4byte(&pBt->pPage1->aData[36]); 3925 Pgno nFin = finalDbSize(pBt, nOrig, nFree); 3926 3927 if( nOrig<nFin || nFree>=nOrig ){ 3928 rc = SQLITE_CORRUPT_BKPT; 3929 }else if( nFree>0 ){ 3930 rc = saveAllCursors(pBt, 0, 0); 3931 if( rc==SQLITE_OK ){ 3932 invalidateAllOverflowCache(pBt); 3933 rc = incrVacuumStep(pBt, nFin, nOrig, 0); 3934 } 3935 if( rc==SQLITE_OK ){ 3936 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); 3937 put4byte(&pBt->pPage1->aData[28], pBt->nPage); 3938 } 3939 }else{ 3940 rc = SQLITE_DONE; 3941 } 3942 } 3943 sqlite3BtreeLeave(p); 3944 return rc; 3945 } 3946 3947 /* 3948 ** This routine is called prior to sqlite3PagerCommit when a transaction 3949 ** is committed for an auto-vacuum database. 3950 */ 3951 static int autoVacuumCommit(Btree *p){ 3952 int rc = SQLITE_OK; 3953 Pager *pPager; 3954 BtShared *pBt; 3955 sqlite3 *db; 3956 VVA_ONLY( int nRef ); 3957 3958 assert( p!=0 ); 3959 pBt = p->pBt; 3960 pPager = pBt->pPager; 3961 VVA_ONLY( nRef = sqlite3PagerRefcount(pPager); ) 3962 3963 assert( sqlite3_mutex_held(pBt->mutex) ); 3964 invalidateAllOverflowCache(pBt); 3965 assert(pBt->autoVacuum); 3966 if( !pBt->incrVacuum ){ 3967 Pgno nFin; /* Number of pages in database after autovacuuming */ 3968 Pgno nFree; /* Number of pages on the freelist initially */ 3969 Pgno nVac; /* Number of pages to vacuum */ 3970 Pgno iFree; /* The next page to be freed */ 3971 Pgno nOrig; /* Database size before freeing */ 3972 3973 nOrig = btreePagecount(pBt); 3974 if( PTRMAP_ISPAGE(pBt, nOrig) || nOrig==PENDING_BYTE_PAGE(pBt) ){ 3975 /* It is not possible to create a database for which the final page 3976 ** is either a pointer-map page or the pending-byte page. If one 3977 ** is encountered, this indicates corruption. 3978 */ 3979 return SQLITE_CORRUPT_BKPT; 3980 } 3981 3982 nFree = get4byte(&pBt->pPage1->aData[36]); 3983 db = p->db; 3984 if( db->xAutovacPages ){ 3985 int iDb; 3986 for(iDb=0; ALWAYS(iDb<db->nDb); iDb++){ 3987 if( db->aDb[iDb].pBt==p ) break; 3988 } 3989 nVac = db->xAutovacPages( 3990 db->pAutovacPagesArg, 3991 db->aDb[iDb].zDbSName, 3992 nOrig, 3993 nFree, 3994 pBt->pageSize 3995 ); 3996 if( nVac>nFree ){ 3997 nVac = nFree; 3998 } 3999 if( nVac==0 ){ 4000 return SQLITE_OK; 4001 } 4002 }else{ 4003 nVac = nFree; 4004 } 4005 nFin = finalDbSize(pBt, nOrig, nVac); 4006 if( nFin>nOrig ) return SQLITE_CORRUPT_BKPT; 4007 if( nFin<nOrig ){ 4008 rc = saveAllCursors(pBt, 0, 0); 4009 } 4010 for(iFree=nOrig; iFree>nFin && rc==SQLITE_OK; iFree--){ 4011 rc = incrVacuumStep(pBt, nFin, iFree, nVac==nFree); 4012 } 4013 if( (rc==SQLITE_DONE || rc==SQLITE_OK) && nFree>0 ){ 4014 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); 4015 if( nVac==nFree ){ 4016 put4byte(&pBt->pPage1->aData[32], 0); 4017 put4byte(&pBt->pPage1->aData[36], 0); 4018 } 4019 put4byte(&pBt->pPage1->aData[28], nFin); 4020 pBt->bDoTruncate = 1; 4021 pBt->nPage = nFin; 4022 } 4023 if( rc!=SQLITE_OK ){ 4024 sqlite3PagerRollback(pPager); 4025 } 4026 } 4027 4028 assert( nRef>=sqlite3PagerRefcount(pPager) ); 4029 return rc; 4030 } 4031 4032 #else /* ifndef SQLITE_OMIT_AUTOVACUUM */ 4033 # define setChildPtrmaps(x) SQLITE_OK 4034 #endif 4035 4036 /* 4037 ** This routine does the first phase of a two-phase commit. This routine 4038 ** causes a rollback journal to be created (if it does not already exist) 4039 ** and populated with enough information so that if a power loss occurs 4040 ** the database can be restored to its original state by playing back 4041 ** the journal. Then the contents of the journal are flushed out to 4042 ** the disk. After the journal is safely on oxide, the changes to the 4043 ** database are written into the database file and flushed to oxide. 4044 ** At the end of this call, the rollback journal still exists on the 4045 ** disk and we are still holding all locks, so the transaction has not 4046 ** committed. See sqlite3BtreeCommitPhaseTwo() for the second phase of the 4047 ** commit process. 4048 ** 4049 ** This call is a no-op if no write-transaction is currently active on pBt. 4050 ** 4051 ** Otherwise, sync the database file for the btree pBt. zSuperJrnl points to 4052 ** the name of a super-journal file that should be written into the 4053 ** individual journal file, or is NULL, indicating no super-journal file 4054 ** (single database transaction). 4055 ** 4056 ** When this is called, the super-journal should already have been 4057 ** created, populated with this journal pointer and synced to disk. 4058 ** 4059 ** Once this is routine has returned, the only thing required to commit 4060 ** the write-transaction for this database file is to delete the journal. 4061 */ 4062 int sqlite3BtreeCommitPhaseOne(Btree *p, const char *zSuperJrnl){ 4063 int rc = SQLITE_OK; 4064 if( p->inTrans==TRANS_WRITE ){ 4065 BtShared *pBt = p->pBt; 4066 sqlite3BtreeEnter(p); 4067 #ifndef SQLITE_OMIT_AUTOVACUUM 4068 if( pBt->autoVacuum ){ 4069 rc = autoVacuumCommit(p); 4070 if( rc!=SQLITE_OK ){ 4071 sqlite3BtreeLeave(p); 4072 return rc; 4073 } 4074 } 4075 if( pBt->bDoTruncate ){ 4076 sqlite3PagerTruncateImage(pBt->pPager, pBt->nPage); 4077 } 4078 #endif 4079 rc = sqlite3PagerCommitPhaseOne(pBt->pPager, zSuperJrnl, 0); 4080 sqlite3BtreeLeave(p); 4081 } 4082 return rc; 4083 } 4084 4085 /* 4086 ** This function is called from both BtreeCommitPhaseTwo() and BtreeRollback() 4087 ** at the conclusion of a transaction. 4088 */ 4089 static void btreeEndTransaction(Btree *p){ 4090 BtShared *pBt = p->pBt; 4091 sqlite3 *db = p->db; 4092 assert( sqlite3BtreeHoldsMutex(p) ); 4093 4094 #ifndef SQLITE_OMIT_AUTOVACUUM 4095 pBt->bDoTruncate = 0; 4096 #endif 4097 if( p->inTrans>TRANS_NONE && db->nVdbeRead>1 ){ 4098 /* If there are other active statements that belong to this database 4099 ** handle, downgrade to a read-only transaction. The other statements 4100 ** may still be reading from the database. */ 4101 downgradeAllSharedCacheTableLocks(p); 4102 p->inTrans = TRANS_READ; 4103 }else{ 4104 /* If the handle had any kind of transaction open, decrement the 4105 ** transaction count of the shared btree. If the transaction count 4106 ** reaches 0, set the shared state to TRANS_NONE. The unlockBtreeIfUnused() 4107 ** call below will unlock the pager. */ 4108 if( p->inTrans!=TRANS_NONE ){ 4109 clearAllSharedCacheTableLocks(p); 4110 pBt->nTransaction--; 4111 if( 0==pBt->nTransaction ){ 4112 pBt->inTransaction = TRANS_NONE; 4113 } 4114 } 4115 4116 /* Set the current transaction state to TRANS_NONE and unlock the 4117 ** pager if this call closed the only read or write transaction. */ 4118 p->inTrans = TRANS_NONE; 4119 unlockBtreeIfUnused(pBt); 4120 } 4121 4122 btreeIntegrity(p); 4123 } 4124 4125 /* 4126 ** Commit the transaction currently in progress. 4127 ** 4128 ** This routine implements the second phase of a 2-phase commit. The 4129 ** sqlite3BtreeCommitPhaseOne() routine does the first phase and should 4130 ** be invoked prior to calling this routine. The sqlite3BtreeCommitPhaseOne() 4131 ** routine did all the work of writing information out to disk and flushing the 4132 ** contents so that they are written onto the disk platter. All this 4133 ** routine has to do is delete or truncate or zero the header in the 4134 ** the rollback journal (which causes the transaction to commit) and 4135 ** drop locks. 4136 ** 4137 ** Normally, if an error occurs while the pager layer is attempting to 4138 ** finalize the underlying journal file, this function returns an error and 4139 ** the upper layer will attempt a rollback. However, if the second argument 4140 ** is non-zero then this b-tree transaction is part of a multi-file 4141 ** transaction. In this case, the transaction has already been committed 4142 ** (by deleting a super-journal file) and the caller will ignore this 4143 ** functions return code. So, even if an error occurs in the pager layer, 4144 ** reset the b-tree objects internal state to indicate that the write 4145 ** transaction has been closed. This is quite safe, as the pager will have 4146 ** transitioned to the error state. 4147 ** 4148 ** This will release the write lock on the database file. If there 4149 ** are no active cursors, it also releases the read lock. 4150 */ 4151 int sqlite3BtreeCommitPhaseTwo(Btree *p, int bCleanup){ 4152 4153 if( p->inTrans==TRANS_NONE ) return SQLITE_OK; 4154 sqlite3BtreeEnter(p); 4155 btreeIntegrity(p); 4156 4157 /* If the handle has a write-transaction open, commit the shared-btrees 4158 ** transaction and set the shared state to TRANS_READ. 4159 */ 4160 if( p->inTrans==TRANS_WRITE ){ 4161 int rc; 4162 BtShared *pBt = p->pBt; 4163 assert( pBt->inTransaction==TRANS_WRITE ); 4164 assert( pBt->nTransaction>0 ); 4165 rc = sqlite3PagerCommitPhaseTwo(pBt->pPager); 4166 if( rc!=SQLITE_OK && bCleanup==0 ){ 4167 sqlite3BtreeLeave(p); 4168 return rc; 4169 } 4170 p->iBDataVersion--; /* Compensate for pPager->iDataVersion++; */ 4171 pBt->inTransaction = TRANS_READ; 4172 btreeClearHasContent(pBt); 4173 } 4174 4175 btreeEndTransaction(p); 4176 sqlite3BtreeLeave(p); 4177 return SQLITE_OK; 4178 } 4179 4180 /* 4181 ** Do both phases of a commit. 4182 */ 4183 int sqlite3BtreeCommit(Btree *p){ 4184 int rc; 4185 sqlite3BtreeEnter(p); 4186 rc = sqlite3BtreeCommitPhaseOne(p, 0); 4187 if( rc==SQLITE_OK ){ 4188 rc = sqlite3BtreeCommitPhaseTwo(p, 0); 4189 } 4190 sqlite3BtreeLeave(p); 4191 return rc; 4192 } 4193 4194 /* 4195 ** This routine sets the state to CURSOR_FAULT and the error 4196 ** code to errCode for every cursor on any BtShared that pBtree 4197 ** references. Or if the writeOnly flag is set to 1, then only 4198 ** trip write cursors and leave read cursors unchanged. 4199 ** 4200 ** Every cursor is a candidate to be tripped, including cursors 4201 ** that belong to other database connections that happen to be 4202 ** sharing the cache with pBtree. 4203 ** 4204 ** This routine gets called when a rollback occurs. If the writeOnly 4205 ** flag is true, then only write-cursors need be tripped - read-only 4206 ** cursors save their current positions so that they may continue 4207 ** following the rollback. Or, if writeOnly is false, all cursors are 4208 ** tripped. In general, writeOnly is false if the transaction being 4209 ** rolled back modified the database schema. In this case b-tree root 4210 ** pages may be moved or deleted from the database altogether, making 4211 ** it unsafe for read cursors to continue. 4212 ** 4213 ** If the writeOnly flag is true and an error is encountered while 4214 ** saving the current position of a read-only cursor, all cursors, 4215 ** including all read-cursors are tripped. 4216 ** 4217 ** SQLITE_OK is returned if successful, or if an error occurs while 4218 ** saving a cursor position, an SQLite error code. 4219 */ 4220 int sqlite3BtreeTripAllCursors(Btree *pBtree, int errCode, int writeOnly){ 4221 BtCursor *p; 4222 int rc = SQLITE_OK; 4223 4224 assert( (writeOnly==0 || writeOnly==1) && BTCF_WriteFlag==1 ); 4225 if( pBtree ){ 4226 sqlite3BtreeEnter(pBtree); 4227 for(p=pBtree->pBt->pCursor; p; p=p->pNext){ 4228 if( writeOnly && (p->curFlags & BTCF_WriteFlag)==0 ){ 4229 if( p->eState==CURSOR_VALID || p->eState==CURSOR_SKIPNEXT ){ 4230 rc = saveCursorPosition(p); 4231 if( rc!=SQLITE_OK ){ 4232 (void)sqlite3BtreeTripAllCursors(pBtree, rc, 0); 4233 break; 4234 } 4235 } 4236 }else{ 4237 sqlite3BtreeClearCursor(p); 4238 p->eState = CURSOR_FAULT; 4239 p->skipNext = errCode; 4240 } 4241 btreeReleaseAllCursorPages(p); 4242 } 4243 sqlite3BtreeLeave(pBtree); 4244 } 4245 return rc; 4246 } 4247 4248 /* 4249 ** Set the pBt->nPage field correctly, according to the current 4250 ** state of the database. Assume pBt->pPage1 is valid. 4251 */ 4252 static void btreeSetNPage(BtShared *pBt, MemPage *pPage1){ 4253 int nPage = get4byte(&pPage1->aData[28]); 4254 testcase( nPage==0 ); 4255 if( nPage==0 ) sqlite3PagerPagecount(pBt->pPager, &nPage); 4256 testcase( pBt->nPage!=(u32)nPage ); 4257 pBt->nPage = nPage; 4258 } 4259 4260 /* 4261 ** Rollback the transaction in progress. 4262 ** 4263 ** If tripCode is not SQLITE_OK then cursors will be invalidated (tripped). 4264 ** Only write cursors are tripped if writeOnly is true but all cursors are 4265 ** tripped if writeOnly is false. Any attempt to use 4266 ** a tripped cursor will result in an error. 4267 ** 4268 ** This will release the write lock on the database file. If there 4269 ** are no active cursors, it also releases the read lock. 4270 */ 4271 int sqlite3BtreeRollback(Btree *p, int tripCode, int writeOnly){ 4272 int rc; 4273 BtShared *pBt = p->pBt; 4274 MemPage *pPage1; 4275 4276 assert( writeOnly==1 || writeOnly==0 ); 4277 assert( tripCode==SQLITE_ABORT_ROLLBACK || tripCode==SQLITE_OK ); 4278 sqlite3BtreeEnter(p); 4279 if( tripCode==SQLITE_OK ){ 4280 rc = tripCode = saveAllCursors(pBt, 0, 0); 4281 if( rc ) writeOnly = 0; 4282 }else{ 4283 rc = SQLITE_OK; 4284 } 4285 if( tripCode ){ 4286 int rc2 = sqlite3BtreeTripAllCursors(p, tripCode, writeOnly); 4287 assert( rc==SQLITE_OK || (writeOnly==0 && rc2==SQLITE_OK) ); 4288 if( rc2!=SQLITE_OK ) rc = rc2; 4289 } 4290 btreeIntegrity(p); 4291 4292 if( p->inTrans==TRANS_WRITE ){ 4293 int rc2; 4294 4295 assert( TRANS_WRITE==pBt->inTransaction ); 4296 rc2 = sqlite3PagerRollback(pBt->pPager); 4297 if( rc2!=SQLITE_OK ){ 4298 rc = rc2; 4299 } 4300 4301 /* The rollback may have destroyed the pPage1->aData value. So 4302 ** call btreeGetPage() on page 1 again to make 4303 ** sure pPage1->aData is set correctly. */ 4304 if( btreeGetPage(pBt, 1, &pPage1, 0)==SQLITE_OK ){ 4305 btreeSetNPage(pBt, pPage1); 4306 releasePageOne(pPage1); 4307 } 4308 assert( countValidCursors(pBt, 1)==0 ); 4309 pBt->inTransaction = TRANS_READ; 4310 btreeClearHasContent(pBt); 4311 } 4312 4313 btreeEndTransaction(p); 4314 sqlite3BtreeLeave(p); 4315 return rc; 4316 } 4317 4318 /* 4319 ** Start a statement subtransaction. The subtransaction can be rolled 4320 ** back independently of the main transaction. You must start a transaction 4321 ** before starting a subtransaction. The subtransaction is ended automatically 4322 ** if the main transaction commits or rolls back. 4323 ** 4324 ** Statement subtransactions are used around individual SQL statements 4325 ** that are contained within a BEGIN...COMMIT block. If a constraint 4326 ** error occurs within the statement, the effect of that one statement 4327 ** can be rolled back without having to rollback the entire transaction. 4328 ** 4329 ** A statement sub-transaction is implemented as an anonymous savepoint. The 4330 ** value passed as the second parameter is the total number of savepoints, 4331 ** including the new anonymous savepoint, open on the B-Tree. i.e. if there 4332 ** are no active savepoints and no other statement-transactions open, 4333 ** iStatement is 1. This anonymous savepoint can be released or rolled back 4334 ** using the sqlite3BtreeSavepoint() function. 4335 */ 4336 int sqlite3BtreeBeginStmt(Btree *p, int iStatement){ 4337 int rc; 4338 BtShared *pBt = p->pBt; 4339 sqlite3BtreeEnter(p); 4340 assert( p->inTrans==TRANS_WRITE ); 4341 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 ); 4342 assert( iStatement>0 ); 4343 assert( iStatement>p->db->nSavepoint ); 4344 assert( pBt->inTransaction==TRANS_WRITE ); 4345 /* At the pager level, a statement transaction is a savepoint with 4346 ** an index greater than all savepoints created explicitly using 4347 ** SQL statements. It is illegal to open, release or rollback any 4348 ** such savepoints while the statement transaction savepoint is active. 4349 */ 4350 rc = sqlite3PagerOpenSavepoint(pBt->pPager, iStatement); 4351 sqlite3BtreeLeave(p); 4352 return rc; 4353 } 4354 4355 /* 4356 ** The second argument to this function, op, is always SAVEPOINT_ROLLBACK 4357 ** or SAVEPOINT_RELEASE. This function either releases or rolls back the 4358 ** savepoint identified by parameter iSavepoint, depending on the value 4359 ** of op. 4360 ** 4361 ** Normally, iSavepoint is greater than or equal to zero. However, if op is 4362 ** SAVEPOINT_ROLLBACK, then iSavepoint may also be -1. In this case the 4363 ** contents of the entire transaction are rolled back. This is different 4364 ** from a normal transaction rollback, as no locks are released and the 4365 ** transaction remains open. 4366 */ 4367 int sqlite3BtreeSavepoint(Btree *p, int op, int iSavepoint){ 4368 int rc = SQLITE_OK; 4369 if( p && p->inTrans==TRANS_WRITE ){ 4370 BtShared *pBt = p->pBt; 4371 assert( op==SAVEPOINT_RELEASE || op==SAVEPOINT_ROLLBACK ); 4372 assert( iSavepoint>=0 || (iSavepoint==-1 && op==SAVEPOINT_ROLLBACK) ); 4373 sqlite3BtreeEnter(p); 4374 if( op==SAVEPOINT_ROLLBACK ){ 4375 rc = saveAllCursors(pBt, 0, 0); 4376 } 4377 if( rc==SQLITE_OK ){ 4378 rc = sqlite3PagerSavepoint(pBt->pPager, op, iSavepoint); 4379 } 4380 if( rc==SQLITE_OK ){ 4381 if( iSavepoint<0 && (pBt->btsFlags & BTS_INITIALLY_EMPTY)!=0 ){ 4382 pBt->nPage = 0; 4383 } 4384 rc = newDatabase(pBt); 4385 btreeSetNPage(pBt, pBt->pPage1); 4386 4387 /* pBt->nPage might be zero if the database was corrupt when 4388 ** the transaction was started. Otherwise, it must be at least 1. */ 4389 assert( CORRUPT_DB || pBt->nPage>0 ); 4390 } 4391 sqlite3BtreeLeave(p); 4392 } 4393 return rc; 4394 } 4395 4396 /* 4397 ** Create a new cursor for the BTree whose root is on the page 4398 ** iTable. If a read-only cursor is requested, it is assumed that 4399 ** the caller already has at least a read-only transaction open 4400 ** on the database already. If a write-cursor is requested, then 4401 ** the caller is assumed to have an open write transaction. 4402 ** 4403 ** If the BTREE_WRCSR bit of wrFlag is clear, then the cursor can only 4404 ** be used for reading. If the BTREE_WRCSR bit is set, then the cursor 4405 ** can be used for reading or for writing if other conditions for writing 4406 ** are also met. These are the conditions that must be met in order 4407 ** for writing to be allowed: 4408 ** 4409 ** 1: The cursor must have been opened with wrFlag containing BTREE_WRCSR 4410 ** 4411 ** 2: Other database connections that share the same pager cache 4412 ** but which are not in the READ_UNCOMMITTED state may not have 4413 ** cursors open with wrFlag==0 on the same table. Otherwise 4414 ** the changes made by this write cursor would be visible to 4415 ** the read cursors in the other database connection. 4416 ** 4417 ** 3: The database must be writable (not on read-only media) 4418 ** 4419 ** 4: There must be an active transaction. 4420 ** 4421 ** The BTREE_FORDELETE bit of wrFlag may optionally be set if BTREE_WRCSR 4422 ** is set. If FORDELETE is set, that is a hint to the implementation that 4423 ** this cursor will only be used to seek to and delete entries of an index 4424 ** as part of a larger DELETE statement. The FORDELETE hint is not used by 4425 ** this implementation. But in a hypothetical alternative storage engine 4426 ** in which index entries are automatically deleted when corresponding table 4427 ** rows are deleted, the FORDELETE flag is a hint that all SEEK and DELETE 4428 ** operations on this cursor can be no-ops and all READ operations can 4429 ** return a null row (2-bytes: 0x01 0x00). 4430 ** 4431 ** No checking is done to make sure that page iTable really is the 4432 ** root page of a b-tree. If it is not, then the cursor acquired 4433 ** will not work correctly. 4434 ** 4435 ** It is assumed that the sqlite3BtreeCursorZero() has been called 4436 ** on pCur to initialize the memory space prior to invoking this routine. 4437 */ 4438 static int btreeCursor( 4439 Btree *p, /* The btree */ 4440 Pgno iTable, /* Root page of table to open */ 4441 int wrFlag, /* 1 to write. 0 read-only */ 4442 struct KeyInfo *pKeyInfo, /* First arg to comparison function */ 4443 BtCursor *pCur /* Space for new cursor */ 4444 ){ 4445 BtShared *pBt = p->pBt; /* Shared b-tree handle */ 4446 BtCursor *pX; /* Looping over other all cursors */ 4447 4448 assert( sqlite3BtreeHoldsMutex(p) ); 4449 assert( wrFlag==0 4450 || wrFlag==BTREE_WRCSR 4451 || wrFlag==(BTREE_WRCSR|BTREE_FORDELETE) 4452 ); 4453 4454 /* The following assert statements verify that if this is a sharable 4455 ** b-tree database, the connection is holding the required table locks, 4456 ** and that no other connection has any open cursor that conflicts with 4457 ** this lock. The iTable<1 term disables the check for corrupt schemas. */ 4458 assert( hasSharedCacheTableLock(p, iTable, pKeyInfo!=0, (wrFlag?2:1)) 4459 || iTable<1 ); 4460 assert( wrFlag==0 || !hasReadConflicts(p, iTable) ); 4461 4462 /* Assert that the caller has opened the required transaction. */ 4463 assert( p->inTrans>TRANS_NONE ); 4464 assert( wrFlag==0 || p->inTrans==TRANS_WRITE ); 4465 assert( pBt->pPage1 && pBt->pPage1->aData ); 4466 assert( wrFlag==0 || (pBt->btsFlags & BTS_READ_ONLY)==0 ); 4467 4468 if( iTable<=1 ){ 4469 if( iTable<1 ){ 4470 return SQLITE_CORRUPT_BKPT; 4471 }else if( btreePagecount(pBt)==0 ){ 4472 assert( wrFlag==0 ); 4473 iTable = 0; 4474 } 4475 } 4476 4477 /* Now that no other errors can occur, finish filling in the BtCursor 4478 ** variables and link the cursor into the BtShared list. */ 4479 pCur->pgnoRoot = iTable; 4480 pCur->iPage = -1; 4481 pCur->pKeyInfo = pKeyInfo; 4482 pCur->pBtree = p; 4483 pCur->pBt = pBt; 4484 pCur->curFlags = 0; 4485 /* If there are two or more cursors on the same btree, then all such 4486 ** cursors *must* have the BTCF_Multiple flag set. */ 4487 for(pX=pBt->pCursor; pX; pX=pX->pNext){ 4488 if( pX->pgnoRoot==iTable ){ 4489 pX->curFlags |= BTCF_Multiple; 4490 pCur->curFlags = BTCF_Multiple; 4491 } 4492 } 4493 pCur->eState = CURSOR_INVALID; 4494 pCur->pNext = pBt->pCursor; 4495 pBt->pCursor = pCur; 4496 if( wrFlag ){ 4497 pCur->curFlags |= BTCF_WriteFlag; 4498 pCur->curPagerFlags = 0; 4499 if( pBt->pTmpSpace==0 ) return allocateTempSpace(pBt); 4500 }else{ 4501 pCur->curPagerFlags = PAGER_GET_READONLY; 4502 } 4503 return SQLITE_OK; 4504 } 4505 static int btreeCursorWithLock( 4506 Btree *p, /* The btree */ 4507 Pgno iTable, /* Root page of table to open */ 4508 int wrFlag, /* 1 to write. 0 read-only */ 4509 struct KeyInfo *pKeyInfo, /* First arg to comparison function */ 4510 BtCursor *pCur /* Space for new cursor */ 4511 ){ 4512 int rc; 4513 sqlite3BtreeEnter(p); 4514 rc = btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur); 4515 sqlite3BtreeLeave(p); 4516 return rc; 4517 } 4518 int sqlite3BtreeCursor( 4519 Btree *p, /* The btree */ 4520 Pgno iTable, /* Root page of table to open */ 4521 int wrFlag, /* 1 to write. 0 read-only */ 4522 struct KeyInfo *pKeyInfo, /* First arg to xCompare() */ 4523 BtCursor *pCur /* Write new cursor here */ 4524 ){ 4525 if( p->sharable ){ 4526 return btreeCursorWithLock(p, iTable, wrFlag, pKeyInfo, pCur); 4527 }else{ 4528 return btreeCursor(p, iTable, wrFlag, pKeyInfo, pCur); 4529 } 4530 } 4531 4532 /* 4533 ** Return the size of a BtCursor object in bytes. 4534 ** 4535 ** This interfaces is needed so that users of cursors can preallocate 4536 ** sufficient storage to hold a cursor. The BtCursor object is opaque 4537 ** to users so they cannot do the sizeof() themselves - they must call 4538 ** this routine. 4539 */ 4540 int sqlite3BtreeCursorSize(void){ 4541 return ROUND8(sizeof(BtCursor)); 4542 } 4543 4544 /* 4545 ** Initialize memory that will be converted into a BtCursor object. 4546 ** 4547 ** The simple approach here would be to memset() the entire object 4548 ** to zero. But it turns out that the apPage[] and aiIdx[] arrays 4549 ** do not need to be zeroed and they are large, so we can save a lot 4550 ** of run-time by skipping the initialization of those elements. 4551 */ 4552 void sqlite3BtreeCursorZero(BtCursor *p){ 4553 memset(p, 0, offsetof(BtCursor, BTCURSOR_FIRST_UNINIT)); 4554 } 4555 4556 /* 4557 ** Close a cursor. The read lock on the database file is released 4558 ** when the last cursor is closed. 4559 */ 4560 int sqlite3BtreeCloseCursor(BtCursor *pCur){ 4561 Btree *pBtree = pCur->pBtree; 4562 if( pBtree ){ 4563 BtShared *pBt = pCur->pBt; 4564 sqlite3BtreeEnter(pBtree); 4565 assert( pBt->pCursor!=0 ); 4566 if( pBt->pCursor==pCur ){ 4567 pBt->pCursor = pCur->pNext; 4568 }else{ 4569 BtCursor *pPrev = pBt->pCursor; 4570 do{ 4571 if( pPrev->pNext==pCur ){ 4572 pPrev->pNext = pCur->pNext; 4573 break; 4574 } 4575 pPrev = pPrev->pNext; 4576 }while( ALWAYS(pPrev) ); 4577 } 4578 btreeReleaseAllCursorPages(pCur); 4579 unlockBtreeIfUnused(pBt); 4580 sqlite3_free(pCur->aOverflow); 4581 sqlite3_free(pCur->pKey); 4582 if( (pBt->openFlags & BTREE_SINGLE) && pBt->pCursor==0 ){ 4583 /* Since the BtShared is not sharable, there is no need to 4584 ** worry about the missing sqlite3BtreeLeave() call here. */ 4585 assert( pBtree->sharable==0 ); 4586 sqlite3BtreeClose(pBtree); 4587 }else{ 4588 sqlite3BtreeLeave(pBtree); 4589 } 4590 pCur->pBtree = 0; 4591 } 4592 return SQLITE_OK; 4593 } 4594 4595 /* 4596 ** Make sure the BtCursor* given in the argument has a valid 4597 ** BtCursor.info structure. If it is not already valid, call 4598 ** btreeParseCell() to fill it in. 4599 ** 4600 ** BtCursor.info is a cache of the information in the current cell. 4601 ** Using this cache reduces the number of calls to btreeParseCell(). 4602 */ 4603 #ifndef NDEBUG 4604 static int cellInfoEqual(CellInfo *a, CellInfo *b){ 4605 if( a->nKey!=b->nKey ) return 0; 4606 if( a->pPayload!=b->pPayload ) return 0; 4607 if( a->nPayload!=b->nPayload ) return 0; 4608 if( a->nLocal!=b->nLocal ) return 0; 4609 if( a->nSize!=b->nSize ) return 0; 4610 return 1; 4611 } 4612 static void assertCellInfo(BtCursor *pCur){ 4613 CellInfo info; 4614 memset(&info, 0, sizeof(info)); 4615 btreeParseCell(pCur->pPage, pCur->ix, &info); 4616 assert( CORRUPT_DB || cellInfoEqual(&info, &pCur->info) ); 4617 } 4618 #else 4619 #define assertCellInfo(x) 4620 #endif 4621 static SQLITE_NOINLINE void getCellInfo(BtCursor *pCur){ 4622 if( pCur->info.nSize==0 ){ 4623 pCur->curFlags |= BTCF_ValidNKey; 4624 btreeParseCell(pCur->pPage,pCur->ix,&pCur->info); 4625 }else{ 4626 assertCellInfo(pCur); 4627 } 4628 } 4629 4630 #ifndef NDEBUG /* The next routine used only within assert() statements */ 4631 /* 4632 ** Return true if the given BtCursor is valid. A valid cursor is one 4633 ** that is currently pointing to a row in a (non-empty) table. 4634 ** This is a verification routine is used only within assert() statements. 4635 */ 4636 int sqlite3BtreeCursorIsValid(BtCursor *pCur){ 4637 return pCur && pCur->eState==CURSOR_VALID; 4638 } 4639 #endif /* NDEBUG */ 4640 int sqlite3BtreeCursorIsValidNN(BtCursor *pCur){ 4641 assert( pCur!=0 ); 4642 return pCur->eState==CURSOR_VALID; 4643 } 4644 4645 /* 4646 ** Return the value of the integer key or "rowid" for a table btree. 4647 ** This routine is only valid for a cursor that is pointing into a 4648 ** ordinary table btree. If the cursor points to an index btree or 4649 ** is invalid, the result of this routine is undefined. 4650 */ 4651 i64 sqlite3BtreeIntegerKey(BtCursor *pCur){ 4652 assert( cursorHoldsMutex(pCur) ); 4653 assert( pCur->eState==CURSOR_VALID ); 4654 assert( pCur->curIntKey ); 4655 getCellInfo(pCur); 4656 return pCur->info.nKey; 4657 } 4658 4659 /* 4660 ** Pin or unpin a cursor. 4661 */ 4662 void sqlite3BtreeCursorPin(BtCursor *pCur){ 4663 assert( (pCur->curFlags & BTCF_Pinned)==0 ); 4664 pCur->curFlags |= BTCF_Pinned; 4665 } 4666 void sqlite3BtreeCursorUnpin(BtCursor *pCur){ 4667 assert( (pCur->curFlags & BTCF_Pinned)!=0 ); 4668 pCur->curFlags &= ~BTCF_Pinned; 4669 } 4670 4671 #ifdef SQLITE_ENABLE_OFFSET_SQL_FUNC 4672 /* 4673 ** Return the offset into the database file for the start of the 4674 ** payload to which the cursor is pointing. 4675 */ 4676 i64 sqlite3BtreeOffset(BtCursor *pCur){ 4677 assert( cursorHoldsMutex(pCur) ); 4678 assert( pCur->eState==CURSOR_VALID ); 4679 getCellInfo(pCur); 4680 return (i64)pCur->pBt->pageSize*((i64)pCur->pPage->pgno - 1) + 4681 (i64)(pCur->info.pPayload - pCur->pPage->aData); 4682 } 4683 #endif /* SQLITE_ENABLE_OFFSET_SQL_FUNC */ 4684 4685 /* 4686 ** Return the number of bytes of payload for the entry that pCur is 4687 ** currently pointing to. For table btrees, this will be the amount 4688 ** of data. For index btrees, this will be the size of the key. 4689 ** 4690 ** The caller must guarantee that the cursor is pointing to a non-NULL 4691 ** valid entry. In other words, the calling procedure must guarantee 4692 ** that the cursor has Cursor.eState==CURSOR_VALID. 4693 */ 4694 u32 sqlite3BtreePayloadSize(BtCursor *pCur){ 4695 assert( cursorHoldsMutex(pCur) ); 4696 assert( pCur->eState==CURSOR_VALID ); 4697 getCellInfo(pCur); 4698 return pCur->info.nPayload; 4699 } 4700 4701 /* 4702 ** Return an upper bound on the size of any record for the table 4703 ** that the cursor is pointing into. 4704 ** 4705 ** This is an optimization. Everything will still work if this 4706 ** routine always returns 2147483647 (which is the largest record 4707 ** that SQLite can handle) or more. But returning a smaller value might 4708 ** prevent large memory allocations when trying to interpret a 4709 ** corrupt datrabase. 4710 ** 4711 ** The current implementation merely returns the size of the underlying 4712 ** database file. 4713 */ 4714 sqlite3_int64 sqlite3BtreeMaxRecordSize(BtCursor *pCur){ 4715 assert( cursorHoldsMutex(pCur) ); 4716 assert( pCur->eState==CURSOR_VALID ); 4717 return pCur->pBt->pageSize * (sqlite3_int64)pCur->pBt->nPage; 4718 } 4719 4720 /* 4721 ** Given the page number of an overflow page in the database (parameter 4722 ** ovfl), this function finds the page number of the next page in the 4723 ** linked list of overflow pages. If possible, it uses the auto-vacuum 4724 ** pointer-map data instead of reading the content of page ovfl to do so. 4725 ** 4726 ** If an error occurs an SQLite error code is returned. Otherwise: 4727 ** 4728 ** The page number of the next overflow page in the linked list is 4729 ** written to *pPgnoNext. If page ovfl is the last page in its linked 4730 ** list, *pPgnoNext is set to zero. 4731 ** 4732 ** If ppPage is not NULL, and a reference to the MemPage object corresponding 4733 ** to page number pOvfl was obtained, then *ppPage is set to point to that 4734 ** reference. It is the responsibility of the caller to call releasePage() 4735 ** on *ppPage to free the reference. In no reference was obtained (because 4736 ** the pointer-map was used to obtain the value for *pPgnoNext), then 4737 ** *ppPage is set to zero. 4738 */ 4739 static int getOverflowPage( 4740 BtShared *pBt, /* The database file */ 4741 Pgno ovfl, /* Current overflow page number */ 4742 MemPage **ppPage, /* OUT: MemPage handle (may be NULL) */ 4743 Pgno *pPgnoNext /* OUT: Next overflow page number */ 4744 ){ 4745 Pgno next = 0; 4746 MemPage *pPage = 0; 4747 int rc = SQLITE_OK; 4748 4749 assert( sqlite3_mutex_held(pBt->mutex) ); 4750 assert(pPgnoNext); 4751 4752 #ifndef SQLITE_OMIT_AUTOVACUUM 4753 /* Try to find the next page in the overflow list using the 4754 ** autovacuum pointer-map pages. Guess that the next page in 4755 ** the overflow list is page number (ovfl+1). If that guess turns 4756 ** out to be wrong, fall back to loading the data of page 4757 ** number ovfl to determine the next page number. 4758 */ 4759 if( pBt->autoVacuum ){ 4760 Pgno pgno; 4761 Pgno iGuess = ovfl+1; 4762 u8 eType; 4763 4764 while( PTRMAP_ISPAGE(pBt, iGuess) || iGuess==PENDING_BYTE_PAGE(pBt) ){ 4765 iGuess++; 4766 } 4767 4768 if( iGuess<=btreePagecount(pBt) ){ 4769 rc = ptrmapGet(pBt, iGuess, &eType, &pgno); 4770 if( rc==SQLITE_OK && eType==PTRMAP_OVERFLOW2 && pgno==ovfl ){ 4771 next = iGuess; 4772 rc = SQLITE_DONE; 4773 } 4774 } 4775 } 4776 #endif 4777 4778 assert( next==0 || rc==SQLITE_DONE ); 4779 if( rc==SQLITE_OK ){ 4780 rc = btreeGetPage(pBt, ovfl, &pPage, (ppPage==0) ? PAGER_GET_READONLY : 0); 4781 assert( rc==SQLITE_OK || pPage==0 ); 4782 if( rc==SQLITE_OK ){ 4783 next = get4byte(pPage->aData); 4784 } 4785 } 4786 4787 *pPgnoNext = next; 4788 if( ppPage ){ 4789 *ppPage = pPage; 4790 }else{ 4791 releasePage(pPage); 4792 } 4793 return (rc==SQLITE_DONE ? SQLITE_OK : rc); 4794 } 4795 4796 /* 4797 ** Copy data from a buffer to a page, or from a page to a buffer. 4798 ** 4799 ** pPayload is a pointer to data stored on database page pDbPage. 4800 ** If argument eOp is false, then nByte bytes of data are copied 4801 ** from pPayload to the buffer pointed at by pBuf. If eOp is true, 4802 ** then sqlite3PagerWrite() is called on pDbPage and nByte bytes 4803 ** of data are copied from the buffer pBuf to pPayload. 4804 ** 4805 ** SQLITE_OK is returned on success, otherwise an error code. 4806 */ 4807 static int copyPayload( 4808 void *pPayload, /* Pointer to page data */ 4809 void *pBuf, /* Pointer to buffer */ 4810 int nByte, /* Number of bytes to copy */ 4811 int eOp, /* 0 -> copy from page, 1 -> copy to page */ 4812 DbPage *pDbPage /* Page containing pPayload */ 4813 ){ 4814 if( eOp ){ 4815 /* Copy data from buffer to page (a write operation) */ 4816 int rc = sqlite3PagerWrite(pDbPage); 4817 if( rc!=SQLITE_OK ){ 4818 return rc; 4819 } 4820 memcpy(pPayload, pBuf, nByte); 4821 }else{ 4822 /* Copy data from page to buffer (a read operation) */ 4823 memcpy(pBuf, pPayload, nByte); 4824 } 4825 return SQLITE_OK; 4826 } 4827 4828 /* 4829 ** This function is used to read or overwrite payload information 4830 ** for the entry that the pCur cursor is pointing to. The eOp 4831 ** argument is interpreted as follows: 4832 ** 4833 ** 0: The operation is a read. Populate the overflow cache. 4834 ** 1: The operation is a write. Populate the overflow cache. 4835 ** 4836 ** A total of "amt" bytes are read or written beginning at "offset". 4837 ** Data is read to or from the buffer pBuf. 4838 ** 4839 ** The content being read or written might appear on the main page 4840 ** or be scattered out on multiple overflow pages. 4841 ** 4842 ** If the current cursor entry uses one or more overflow pages 4843 ** this function may allocate space for and lazily populate 4844 ** the overflow page-list cache array (BtCursor.aOverflow). 4845 ** Subsequent calls use this cache to make seeking to the supplied offset 4846 ** more efficient. 4847 ** 4848 ** Once an overflow page-list cache has been allocated, it must be 4849 ** invalidated if some other cursor writes to the same table, or if 4850 ** the cursor is moved to a different row. Additionally, in auto-vacuum 4851 ** mode, the following events may invalidate an overflow page-list cache. 4852 ** 4853 ** * An incremental vacuum, 4854 ** * A commit in auto_vacuum="full" mode, 4855 ** * Creating a table (may require moving an overflow page). 4856 */ 4857 static int accessPayload( 4858 BtCursor *pCur, /* Cursor pointing to entry to read from */ 4859 u32 offset, /* Begin reading this far into payload */ 4860 u32 amt, /* Read this many bytes */ 4861 unsigned char *pBuf, /* Write the bytes into this buffer */ 4862 int eOp /* zero to read. non-zero to write. */ 4863 ){ 4864 unsigned char *aPayload; 4865 int rc = SQLITE_OK; 4866 int iIdx = 0; 4867 MemPage *pPage = pCur->pPage; /* Btree page of current entry */ 4868 BtShared *pBt = pCur->pBt; /* Btree this cursor belongs to */ 4869 #ifdef SQLITE_DIRECT_OVERFLOW_READ 4870 unsigned char * const pBufStart = pBuf; /* Start of original out buffer */ 4871 #endif 4872 4873 assert( pPage ); 4874 assert( eOp==0 || eOp==1 ); 4875 assert( pCur->eState==CURSOR_VALID ); 4876 if( pCur->ix>=pPage->nCell ){ 4877 return SQLITE_CORRUPT_PAGE(pPage); 4878 } 4879 assert( cursorHoldsMutex(pCur) ); 4880 4881 getCellInfo(pCur); 4882 aPayload = pCur->info.pPayload; 4883 assert( offset+amt <= pCur->info.nPayload ); 4884 4885 assert( aPayload > pPage->aData ); 4886 if( (uptr)(aPayload - pPage->aData) > (pBt->usableSize - pCur->info.nLocal) ){ 4887 /* Trying to read or write past the end of the data is an error. The 4888 ** conditional above is really: 4889 ** &aPayload[pCur->info.nLocal] > &pPage->aData[pBt->usableSize] 4890 ** but is recast into its current form to avoid integer overflow problems 4891 */ 4892 return SQLITE_CORRUPT_PAGE(pPage); 4893 } 4894 4895 /* Check if data must be read/written to/from the btree page itself. */ 4896 if( offset<pCur->info.nLocal ){ 4897 int a = amt; 4898 if( a+offset>pCur->info.nLocal ){ 4899 a = pCur->info.nLocal - offset; 4900 } 4901 rc = copyPayload(&aPayload[offset], pBuf, a, eOp, pPage->pDbPage); 4902 offset = 0; 4903 pBuf += a; 4904 amt -= a; 4905 }else{ 4906 offset -= pCur->info.nLocal; 4907 } 4908 4909 4910 if( rc==SQLITE_OK && amt>0 ){ 4911 const u32 ovflSize = pBt->usableSize - 4; /* Bytes content per ovfl page */ 4912 Pgno nextPage; 4913 4914 nextPage = get4byte(&aPayload[pCur->info.nLocal]); 4915 4916 /* If the BtCursor.aOverflow[] has not been allocated, allocate it now. 4917 ** 4918 ** The aOverflow[] array is sized at one entry for each overflow page 4919 ** in the overflow chain. The page number of the first overflow page is 4920 ** stored in aOverflow[0], etc. A value of 0 in the aOverflow[] array 4921 ** means "not yet known" (the cache is lazily populated). 4922 */ 4923 if( (pCur->curFlags & BTCF_ValidOvfl)==0 ){ 4924 int nOvfl = (pCur->info.nPayload-pCur->info.nLocal+ovflSize-1)/ovflSize; 4925 if( pCur->aOverflow==0 4926 || nOvfl*(int)sizeof(Pgno) > sqlite3MallocSize(pCur->aOverflow) 4927 ){ 4928 Pgno *aNew = (Pgno*)sqlite3Realloc( 4929 pCur->aOverflow, nOvfl*2*sizeof(Pgno) 4930 ); 4931 if( aNew==0 ){ 4932 return SQLITE_NOMEM_BKPT; 4933 }else{ 4934 pCur->aOverflow = aNew; 4935 } 4936 } 4937 memset(pCur->aOverflow, 0, nOvfl*sizeof(Pgno)); 4938 pCur->curFlags |= BTCF_ValidOvfl; 4939 }else{ 4940 /* If the overflow page-list cache has been allocated and the 4941 ** entry for the first required overflow page is valid, skip 4942 ** directly to it. 4943 */ 4944 if( pCur->aOverflow[offset/ovflSize] ){ 4945 iIdx = (offset/ovflSize); 4946 nextPage = pCur->aOverflow[iIdx]; 4947 offset = (offset%ovflSize); 4948 } 4949 } 4950 4951 assert( rc==SQLITE_OK && amt>0 ); 4952 while( nextPage ){ 4953 /* If required, populate the overflow page-list cache. */ 4954 if( nextPage > pBt->nPage ) return SQLITE_CORRUPT_BKPT; 4955 assert( pCur->aOverflow[iIdx]==0 4956 || pCur->aOverflow[iIdx]==nextPage 4957 || CORRUPT_DB ); 4958 pCur->aOverflow[iIdx] = nextPage; 4959 4960 if( offset>=ovflSize ){ 4961 /* The only reason to read this page is to obtain the page 4962 ** number for the next page in the overflow chain. The page 4963 ** data is not required. So first try to lookup the overflow 4964 ** page-list cache, if any, then fall back to the getOverflowPage() 4965 ** function. 4966 */ 4967 assert( pCur->curFlags & BTCF_ValidOvfl ); 4968 assert( pCur->pBtree->db==pBt->db ); 4969 if( pCur->aOverflow[iIdx+1] ){ 4970 nextPage = pCur->aOverflow[iIdx+1]; 4971 }else{ 4972 rc = getOverflowPage(pBt, nextPage, 0, &nextPage); 4973 } 4974 offset -= ovflSize; 4975 }else{ 4976 /* Need to read this page properly. It contains some of the 4977 ** range of data that is being read (eOp==0) or written (eOp!=0). 4978 */ 4979 int a = amt; 4980 if( a + offset > ovflSize ){ 4981 a = ovflSize - offset; 4982 } 4983 4984 #ifdef SQLITE_DIRECT_OVERFLOW_READ 4985 /* If all the following are true: 4986 ** 4987 ** 1) this is a read operation, and 4988 ** 2) data is required from the start of this overflow page, and 4989 ** 3) there are no dirty pages in the page-cache 4990 ** 4) the database is file-backed, and 4991 ** 5) the page is not in the WAL file 4992 ** 6) at least 4 bytes have already been read into the output buffer 4993 ** 4994 ** then data can be read directly from the database file into the 4995 ** output buffer, bypassing the page-cache altogether. This speeds 4996 ** up loading large records that span many overflow pages. 4997 */ 4998 if( eOp==0 /* (1) */ 4999 && offset==0 /* (2) */ 5000 && sqlite3PagerDirectReadOk(pBt->pPager, nextPage) /* (3,4,5) */ 5001 && &pBuf[-4]>=pBufStart /* (6) */ 5002 ){ 5003 sqlite3_file *fd = sqlite3PagerFile(pBt->pPager); 5004 u8 aSave[4]; 5005 u8 *aWrite = &pBuf[-4]; 5006 assert( aWrite>=pBufStart ); /* due to (6) */ 5007 memcpy(aSave, aWrite, 4); 5008 rc = sqlite3OsRead(fd, aWrite, a+4, (i64)pBt->pageSize*(nextPage-1)); 5009 if( rc && nextPage>pBt->nPage ) rc = SQLITE_CORRUPT_BKPT; 5010 nextPage = get4byte(aWrite); 5011 memcpy(aWrite, aSave, 4); 5012 }else 5013 #endif 5014 5015 { 5016 DbPage *pDbPage; 5017 rc = sqlite3PagerGet(pBt->pPager, nextPage, &pDbPage, 5018 (eOp==0 ? PAGER_GET_READONLY : 0) 5019 ); 5020 if( rc==SQLITE_OK ){ 5021 aPayload = sqlite3PagerGetData(pDbPage); 5022 nextPage = get4byte(aPayload); 5023 rc = copyPayload(&aPayload[offset+4], pBuf, a, eOp, pDbPage); 5024 sqlite3PagerUnref(pDbPage); 5025 offset = 0; 5026 } 5027 } 5028 amt -= a; 5029 if( amt==0 ) return rc; 5030 pBuf += a; 5031 } 5032 if( rc ) break; 5033 iIdx++; 5034 } 5035 } 5036 5037 if( rc==SQLITE_OK && amt>0 ){ 5038 /* Overflow chain ends prematurely */ 5039 return SQLITE_CORRUPT_PAGE(pPage); 5040 } 5041 return rc; 5042 } 5043 5044 /* 5045 ** Read part of the payload for the row at which that cursor pCur is currently 5046 ** pointing. "amt" bytes will be transferred into pBuf[]. The transfer 5047 ** begins at "offset". 5048 ** 5049 ** pCur can be pointing to either a table or an index b-tree. 5050 ** If pointing to a table btree, then the content section is read. If 5051 ** pCur is pointing to an index b-tree then the key section is read. 5052 ** 5053 ** For sqlite3BtreePayload(), the caller must ensure that pCur is pointing 5054 ** to a valid row in the table. For sqlite3BtreePayloadChecked(), the 5055 ** cursor might be invalid or might need to be restored before being read. 5056 ** 5057 ** Return SQLITE_OK on success or an error code if anything goes 5058 ** wrong. An error is returned if "offset+amt" is larger than 5059 ** the available payload. 5060 */ 5061 int sqlite3BtreePayload(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ 5062 assert( cursorHoldsMutex(pCur) ); 5063 assert( pCur->eState==CURSOR_VALID ); 5064 assert( pCur->iPage>=0 && pCur->pPage ); 5065 return accessPayload(pCur, offset, amt, (unsigned char*)pBuf, 0); 5066 } 5067 5068 /* 5069 ** This variant of sqlite3BtreePayload() works even if the cursor has not 5070 ** in the CURSOR_VALID state. It is only used by the sqlite3_blob_read() 5071 ** interface. 5072 */ 5073 #ifndef SQLITE_OMIT_INCRBLOB 5074 static SQLITE_NOINLINE int accessPayloadChecked( 5075 BtCursor *pCur, 5076 u32 offset, 5077 u32 amt, 5078 void *pBuf 5079 ){ 5080 int rc; 5081 if ( pCur->eState==CURSOR_INVALID ){ 5082 return SQLITE_ABORT; 5083 } 5084 assert( cursorOwnsBtShared(pCur) ); 5085 rc = btreeRestoreCursorPosition(pCur); 5086 return rc ? rc : accessPayload(pCur, offset, amt, pBuf, 0); 5087 } 5088 int sqlite3BtreePayloadChecked(BtCursor *pCur, u32 offset, u32 amt, void *pBuf){ 5089 if( pCur->eState==CURSOR_VALID ){ 5090 assert( cursorOwnsBtShared(pCur) ); 5091 return accessPayload(pCur, offset, amt, pBuf, 0); 5092 }else{ 5093 return accessPayloadChecked(pCur, offset, amt, pBuf); 5094 } 5095 } 5096 #endif /* SQLITE_OMIT_INCRBLOB */ 5097 5098 /* 5099 ** Return a pointer to payload information from the entry that the 5100 ** pCur cursor is pointing to. The pointer is to the beginning of 5101 ** the key if index btrees (pPage->intKey==0) and is the data for 5102 ** table btrees (pPage->intKey==1). The number of bytes of available 5103 ** key/data is written into *pAmt. If *pAmt==0, then the value 5104 ** returned will not be a valid pointer. 5105 ** 5106 ** This routine is an optimization. It is common for the entire key 5107 ** and data to fit on the local page and for there to be no overflow 5108 ** pages. When that is so, this routine can be used to access the 5109 ** key and data without making a copy. If the key and/or data spills 5110 ** onto overflow pages, then accessPayload() must be used to reassemble 5111 ** the key/data and copy it into a preallocated buffer. 5112 ** 5113 ** The pointer returned by this routine looks directly into the cached 5114 ** page of the database. The data might change or move the next time 5115 ** any btree routine is called. 5116 */ 5117 static const void *fetchPayload( 5118 BtCursor *pCur, /* Cursor pointing to entry to read from */ 5119 u32 *pAmt /* Write the number of available bytes here */ 5120 ){ 5121 int amt; 5122 assert( pCur!=0 && pCur->iPage>=0 && pCur->pPage); 5123 assert( pCur->eState==CURSOR_VALID ); 5124 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); 5125 assert( cursorOwnsBtShared(pCur) ); 5126 assert( pCur->ix<pCur->pPage->nCell || CORRUPT_DB ); 5127 assert( pCur->info.nSize>0 ); 5128 assert( pCur->info.pPayload>pCur->pPage->aData || CORRUPT_DB ); 5129 assert( pCur->info.pPayload<pCur->pPage->aDataEnd ||CORRUPT_DB); 5130 amt = pCur->info.nLocal; 5131 if( amt>(int)(pCur->pPage->aDataEnd - pCur->info.pPayload) ){ 5132 /* There is too little space on the page for the expected amount 5133 ** of local content. Database must be corrupt. */ 5134 assert( CORRUPT_DB ); 5135 amt = MAX(0, (int)(pCur->pPage->aDataEnd - pCur->info.pPayload)); 5136 } 5137 *pAmt = (u32)amt; 5138 return (void*)pCur->info.pPayload; 5139 } 5140 5141 5142 /* 5143 ** For the entry that cursor pCur is point to, return as 5144 ** many bytes of the key or data as are available on the local 5145 ** b-tree page. Write the number of available bytes into *pAmt. 5146 ** 5147 ** The pointer returned is ephemeral. The key/data may move 5148 ** or be destroyed on the next call to any Btree routine, 5149 ** including calls from other threads against the same cache. 5150 ** Hence, a mutex on the BtShared should be held prior to calling 5151 ** this routine. 5152 ** 5153 ** These routines is used to get quick access to key and data 5154 ** in the common case where no overflow pages are used. 5155 */ 5156 const void *sqlite3BtreePayloadFetch(BtCursor *pCur, u32 *pAmt){ 5157 return fetchPayload(pCur, pAmt); 5158 } 5159 5160 5161 /* 5162 ** Move the cursor down to a new child page. The newPgno argument is the 5163 ** page number of the child page to move to. 5164 ** 5165 ** This function returns SQLITE_CORRUPT if the page-header flags field of 5166 ** the new child page does not match the flags field of the parent (i.e. 5167 ** if an intkey page appears to be the parent of a non-intkey page, or 5168 ** vice-versa). 5169 */ 5170 static int moveToChild(BtCursor *pCur, u32 newPgno){ 5171 BtShared *pBt = pCur->pBt; 5172 5173 assert( cursorOwnsBtShared(pCur) ); 5174 assert( pCur->eState==CURSOR_VALID ); 5175 assert( pCur->iPage<BTCURSOR_MAX_DEPTH ); 5176 assert( pCur->iPage>=0 ); 5177 if( pCur->iPage>=(BTCURSOR_MAX_DEPTH-1) ){ 5178 return SQLITE_CORRUPT_BKPT; 5179 } 5180 pCur->info.nSize = 0; 5181 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); 5182 pCur->aiIdx[pCur->iPage] = pCur->ix; 5183 pCur->apPage[pCur->iPage] = pCur->pPage; 5184 pCur->ix = 0; 5185 pCur->iPage++; 5186 return getAndInitPage(pBt, newPgno, &pCur->pPage, pCur, pCur->curPagerFlags); 5187 } 5188 5189 #ifdef SQLITE_DEBUG 5190 /* 5191 ** Page pParent is an internal (non-leaf) tree page. This function 5192 ** asserts that page number iChild is the left-child if the iIdx'th 5193 ** cell in page pParent. Or, if iIdx is equal to the total number of 5194 ** cells in pParent, that page number iChild is the right-child of 5195 ** the page. 5196 */ 5197 static void assertParentIndex(MemPage *pParent, int iIdx, Pgno iChild){ 5198 if( CORRUPT_DB ) return; /* The conditions tested below might not be true 5199 ** in a corrupt database */ 5200 assert( iIdx<=pParent->nCell ); 5201 if( iIdx==pParent->nCell ){ 5202 assert( get4byte(&pParent->aData[pParent->hdrOffset+8])==iChild ); 5203 }else{ 5204 assert( get4byte(findCell(pParent, iIdx))==iChild ); 5205 } 5206 } 5207 #else 5208 # define assertParentIndex(x,y,z) 5209 #endif 5210 5211 /* 5212 ** Move the cursor up to the parent page. 5213 ** 5214 ** pCur->idx is set to the cell index that contains the pointer 5215 ** to the page we are coming from. If we are coming from the 5216 ** right-most child page then pCur->idx is set to one more than 5217 ** the largest cell index. 5218 */ 5219 static void moveToParent(BtCursor *pCur){ 5220 MemPage *pLeaf; 5221 assert( cursorOwnsBtShared(pCur) ); 5222 assert( pCur->eState==CURSOR_VALID ); 5223 assert( pCur->iPage>0 ); 5224 assert( pCur->pPage ); 5225 assertParentIndex( 5226 pCur->apPage[pCur->iPage-1], 5227 pCur->aiIdx[pCur->iPage-1], 5228 pCur->pPage->pgno 5229 ); 5230 testcase( pCur->aiIdx[pCur->iPage-1] > pCur->apPage[pCur->iPage-1]->nCell ); 5231 pCur->info.nSize = 0; 5232 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); 5233 pCur->ix = pCur->aiIdx[pCur->iPage-1]; 5234 pLeaf = pCur->pPage; 5235 pCur->pPage = pCur->apPage[--pCur->iPage]; 5236 releasePageNotNull(pLeaf); 5237 } 5238 5239 /* 5240 ** Move the cursor to point to the root page of its b-tree structure. 5241 ** 5242 ** If the table has a virtual root page, then the cursor is moved to point 5243 ** to the virtual root page instead of the actual root page. A table has a 5244 ** virtual root page when the actual root page contains no cells and a 5245 ** single child page. This can only happen with the table rooted at page 1. 5246 ** 5247 ** If the b-tree structure is empty, the cursor state is set to 5248 ** CURSOR_INVALID and this routine returns SQLITE_EMPTY. Otherwise, 5249 ** the cursor is set to point to the first cell located on the root 5250 ** (or virtual root) page and the cursor state is set to CURSOR_VALID. 5251 ** 5252 ** If this function returns successfully, it may be assumed that the 5253 ** page-header flags indicate that the [virtual] root-page is the expected 5254 ** kind of b-tree page (i.e. if when opening the cursor the caller did not 5255 ** specify a KeyInfo structure the flags byte is set to 0x05 or 0x0D, 5256 ** indicating a table b-tree, or if the caller did specify a KeyInfo 5257 ** structure the flags byte is set to 0x02 or 0x0A, indicating an index 5258 ** b-tree). 5259 */ 5260 static int moveToRoot(BtCursor *pCur){ 5261 MemPage *pRoot; 5262 int rc = SQLITE_OK; 5263 5264 assert( cursorOwnsBtShared(pCur) ); 5265 assert( CURSOR_INVALID < CURSOR_REQUIRESEEK ); 5266 assert( CURSOR_VALID < CURSOR_REQUIRESEEK ); 5267 assert( CURSOR_FAULT > CURSOR_REQUIRESEEK ); 5268 assert( pCur->eState < CURSOR_REQUIRESEEK || pCur->iPage<0 ); 5269 assert( pCur->pgnoRoot>0 || pCur->iPage<0 ); 5270 5271 if( pCur->iPage>=0 ){ 5272 if( pCur->iPage ){ 5273 releasePageNotNull(pCur->pPage); 5274 while( --pCur->iPage ){ 5275 releasePageNotNull(pCur->apPage[pCur->iPage]); 5276 } 5277 pRoot = pCur->pPage = pCur->apPage[0]; 5278 goto skip_init; 5279 } 5280 }else if( pCur->pgnoRoot==0 ){ 5281 pCur->eState = CURSOR_INVALID; 5282 return SQLITE_EMPTY; 5283 }else{ 5284 assert( pCur->iPage==(-1) ); 5285 if( pCur->eState>=CURSOR_REQUIRESEEK ){ 5286 if( pCur->eState==CURSOR_FAULT ){ 5287 assert( pCur->skipNext!=SQLITE_OK ); 5288 return pCur->skipNext; 5289 } 5290 sqlite3BtreeClearCursor(pCur); 5291 } 5292 rc = getAndInitPage(pCur->pBtree->pBt, pCur->pgnoRoot, &pCur->pPage, 5293 0, pCur->curPagerFlags); 5294 if( rc!=SQLITE_OK ){ 5295 pCur->eState = CURSOR_INVALID; 5296 return rc; 5297 } 5298 pCur->iPage = 0; 5299 pCur->curIntKey = pCur->pPage->intKey; 5300 } 5301 pRoot = pCur->pPage; 5302 assert( pRoot->pgno==pCur->pgnoRoot ); 5303 5304 /* If pCur->pKeyInfo is not NULL, then the caller that opened this cursor 5305 ** expected to open it on an index b-tree. Otherwise, if pKeyInfo is 5306 ** NULL, the caller expects a table b-tree. If this is not the case, 5307 ** return an SQLITE_CORRUPT error. 5308 ** 5309 ** Earlier versions of SQLite assumed that this test could not fail 5310 ** if the root page was already loaded when this function was called (i.e. 5311 ** if pCur->iPage>=0). But this is not so if the database is corrupted 5312 ** in such a way that page pRoot is linked into a second b-tree table 5313 ** (or the freelist). */ 5314 assert( pRoot->intKey==1 || pRoot->intKey==0 ); 5315 if( pRoot->isInit==0 || (pCur->pKeyInfo==0)!=pRoot->intKey ){ 5316 return SQLITE_CORRUPT_PAGE(pCur->pPage); 5317 } 5318 5319 skip_init: 5320 pCur->ix = 0; 5321 pCur->info.nSize = 0; 5322 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidNKey|BTCF_ValidOvfl); 5323 5324 if( pRoot->nCell>0 ){ 5325 pCur->eState = CURSOR_VALID; 5326 }else if( !pRoot->leaf ){ 5327 Pgno subpage; 5328 if( pRoot->pgno!=1 ) return SQLITE_CORRUPT_BKPT; 5329 subpage = get4byte(&pRoot->aData[pRoot->hdrOffset+8]); 5330 pCur->eState = CURSOR_VALID; 5331 rc = moveToChild(pCur, subpage); 5332 }else{ 5333 pCur->eState = CURSOR_INVALID; 5334 rc = SQLITE_EMPTY; 5335 } 5336 return rc; 5337 } 5338 5339 /* 5340 ** Move the cursor down to the left-most leaf entry beneath the 5341 ** entry to which it is currently pointing. 5342 ** 5343 ** The left-most leaf is the one with the smallest key - the first 5344 ** in ascending order. 5345 */ 5346 static int moveToLeftmost(BtCursor *pCur){ 5347 Pgno pgno; 5348 int rc = SQLITE_OK; 5349 MemPage *pPage; 5350 5351 assert( cursorOwnsBtShared(pCur) ); 5352 assert( pCur->eState==CURSOR_VALID ); 5353 while( rc==SQLITE_OK && !(pPage = pCur->pPage)->leaf ){ 5354 assert( pCur->ix<pPage->nCell ); 5355 pgno = get4byte(findCell(pPage, pCur->ix)); 5356 rc = moveToChild(pCur, pgno); 5357 } 5358 return rc; 5359 } 5360 5361 /* 5362 ** Move the cursor down to the right-most leaf entry beneath the 5363 ** page to which it is currently pointing. Notice the difference 5364 ** between moveToLeftmost() and moveToRightmost(). moveToLeftmost() 5365 ** finds the left-most entry beneath the *entry* whereas moveToRightmost() 5366 ** finds the right-most entry beneath the *page*. 5367 ** 5368 ** The right-most entry is the one with the largest key - the last 5369 ** key in ascending order. 5370 */ 5371 static int moveToRightmost(BtCursor *pCur){ 5372 Pgno pgno; 5373 int rc = SQLITE_OK; 5374 MemPage *pPage = 0; 5375 5376 assert( cursorOwnsBtShared(pCur) ); 5377 assert( pCur->eState==CURSOR_VALID ); 5378 while( !(pPage = pCur->pPage)->leaf ){ 5379 pgno = get4byte(&pPage->aData[pPage->hdrOffset+8]); 5380 pCur->ix = pPage->nCell; 5381 rc = moveToChild(pCur, pgno); 5382 if( rc ) return rc; 5383 } 5384 pCur->ix = pPage->nCell-1; 5385 assert( pCur->info.nSize==0 ); 5386 assert( (pCur->curFlags & BTCF_ValidNKey)==0 ); 5387 return SQLITE_OK; 5388 } 5389 5390 /* Move the cursor to the first entry in the table. Return SQLITE_OK 5391 ** on success. Set *pRes to 0 if the cursor actually points to something 5392 ** or set *pRes to 1 if the table is empty. 5393 */ 5394 int sqlite3BtreeFirst(BtCursor *pCur, int *pRes){ 5395 int rc; 5396 5397 assert( cursorOwnsBtShared(pCur) ); 5398 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); 5399 rc = moveToRoot(pCur); 5400 if( rc==SQLITE_OK ){ 5401 assert( pCur->pPage->nCell>0 ); 5402 *pRes = 0; 5403 rc = moveToLeftmost(pCur); 5404 }else if( rc==SQLITE_EMPTY ){ 5405 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 ); 5406 *pRes = 1; 5407 rc = SQLITE_OK; 5408 } 5409 return rc; 5410 } 5411 5412 /* Move the cursor to the last entry in the table. Return SQLITE_OK 5413 ** on success. Set *pRes to 0 if the cursor actually points to something 5414 ** or set *pRes to 1 if the table is empty. 5415 */ 5416 int sqlite3BtreeLast(BtCursor *pCur, int *pRes){ 5417 int rc; 5418 5419 assert( cursorOwnsBtShared(pCur) ); 5420 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); 5421 5422 /* If the cursor already points to the last entry, this is a no-op. */ 5423 if( CURSOR_VALID==pCur->eState && (pCur->curFlags & BTCF_AtLast)!=0 ){ 5424 #ifdef SQLITE_DEBUG 5425 /* This block serves to assert() that the cursor really does point 5426 ** to the last entry in the b-tree. */ 5427 int ii; 5428 for(ii=0; ii<pCur->iPage; ii++){ 5429 assert( pCur->aiIdx[ii]==pCur->apPage[ii]->nCell ); 5430 } 5431 assert( pCur->ix==pCur->pPage->nCell-1 || CORRUPT_DB ); 5432 testcase( pCur->ix!=pCur->pPage->nCell-1 ); 5433 /* ^-- dbsqlfuzz b92b72e4de80b5140c30ab71372ca719b8feb618 */ 5434 assert( pCur->pPage->leaf ); 5435 #endif 5436 *pRes = 0; 5437 return SQLITE_OK; 5438 } 5439 5440 rc = moveToRoot(pCur); 5441 if( rc==SQLITE_OK ){ 5442 assert( pCur->eState==CURSOR_VALID ); 5443 *pRes = 0; 5444 rc = moveToRightmost(pCur); 5445 if( rc==SQLITE_OK ){ 5446 pCur->curFlags |= BTCF_AtLast; 5447 }else{ 5448 pCur->curFlags &= ~BTCF_AtLast; 5449 } 5450 }else if( rc==SQLITE_EMPTY ){ 5451 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 ); 5452 *pRes = 1; 5453 rc = SQLITE_OK; 5454 } 5455 return rc; 5456 } 5457 5458 /* Move the cursor so that it points to an entry in a table (a.k.a INTKEY) 5459 ** table near the key intKey. Return a success code. 5460 ** 5461 ** If an exact match is not found, then the cursor is always 5462 ** left pointing at a leaf page which would hold the entry if it 5463 ** were present. The cursor might point to an entry that comes 5464 ** before or after the key. 5465 ** 5466 ** An integer is written into *pRes which is the result of 5467 ** comparing the key with the entry to which the cursor is 5468 ** pointing. The meaning of the integer written into 5469 ** *pRes is as follows: 5470 ** 5471 ** *pRes<0 The cursor is left pointing at an entry that 5472 ** is smaller than intKey or if the table is empty 5473 ** and the cursor is therefore left point to nothing. 5474 ** 5475 ** *pRes==0 The cursor is left pointing at an entry that 5476 ** exactly matches intKey. 5477 ** 5478 ** *pRes>0 The cursor is left pointing at an entry that 5479 ** is larger than intKey. 5480 */ 5481 int sqlite3BtreeTableMoveto( 5482 BtCursor *pCur, /* The cursor to be moved */ 5483 i64 intKey, /* The table key */ 5484 int biasRight, /* If true, bias the search to the high end */ 5485 int *pRes /* Write search results here */ 5486 ){ 5487 int rc; 5488 5489 assert( cursorOwnsBtShared(pCur) ); 5490 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); 5491 assert( pRes ); 5492 assert( pCur->pKeyInfo==0 ); 5493 assert( pCur->eState!=CURSOR_VALID || pCur->curIntKey!=0 ); 5494 5495 /* If the cursor is already positioned at the point we are trying 5496 ** to move to, then just return without doing any work */ 5497 if( pCur->eState==CURSOR_VALID && (pCur->curFlags & BTCF_ValidNKey)!=0 ){ 5498 if( pCur->info.nKey==intKey ){ 5499 *pRes = 0; 5500 return SQLITE_OK; 5501 } 5502 if( pCur->info.nKey<intKey ){ 5503 if( (pCur->curFlags & BTCF_AtLast)!=0 ){ 5504 *pRes = -1; 5505 return SQLITE_OK; 5506 } 5507 /* If the requested key is one more than the previous key, then 5508 ** try to get there using sqlite3BtreeNext() rather than a full 5509 ** binary search. This is an optimization only. The correct answer 5510 ** is still obtained without this case, only a little more slowely */ 5511 if( pCur->info.nKey+1==intKey ){ 5512 *pRes = 0; 5513 rc = sqlite3BtreeNext(pCur, 0); 5514 if( rc==SQLITE_OK ){ 5515 getCellInfo(pCur); 5516 if( pCur->info.nKey==intKey ){ 5517 return SQLITE_OK; 5518 } 5519 }else if( rc!=SQLITE_DONE ){ 5520 return rc; 5521 } 5522 } 5523 } 5524 } 5525 5526 #ifdef SQLITE_DEBUG 5527 pCur->pBtree->nSeek++; /* Performance measurement during testing */ 5528 #endif 5529 5530 rc = moveToRoot(pCur); 5531 if( rc ){ 5532 if( rc==SQLITE_EMPTY ){ 5533 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 ); 5534 *pRes = -1; 5535 return SQLITE_OK; 5536 } 5537 return rc; 5538 } 5539 assert( pCur->pPage ); 5540 assert( pCur->pPage->isInit ); 5541 assert( pCur->eState==CURSOR_VALID ); 5542 assert( pCur->pPage->nCell > 0 ); 5543 assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey ); 5544 assert( pCur->curIntKey ); 5545 5546 for(;;){ 5547 int lwr, upr, idx, c; 5548 Pgno chldPg; 5549 MemPage *pPage = pCur->pPage; 5550 u8 *pCell; /* Pointer to current cell in pPage */ 5551 5552 /* pPage->nCell must be greater than zero. If this is the root-page 5553 ** the cursor would have been INVALID above and this for(;;) loop 5554 ** not run. If this is not the root-page, then the moveToChild() routine 5555 ** would have already detected db corruption. Similarly, pPage must 5556 ** be the right kind (index or table) of b-tree page. Otherwise 5557 ** a moveToChild() or moveToRoot() call would have detected corruption. */ 5558 assert( pPage->nCell>0 ); 5559 assert( pPage->intKey ); 5560 lwr = 0; 5561 upr = pPage->nCell-1; 5562 assert( biasRight==0 || biasRight==1 ); 5563 idx = upr>>(1-biasRight); /* idx = biasRight ? upr : (lwr+upr)/2; */ 5564 pCur->ix = (u16)idx; 5565 for(;;){ 5566 i64 nCellKey; 5567 pCell = findCellPastPtr(pPage, idx); 5568 if( pPage->intKeyLeaf ){ 5569 while( 0x80 <= *(pCell++) ){ 5570 if( pCell>=pPage->aDataEnd ){ 5571 return SQLITE_CORRUPT_PAGE(pPage); 5572 } 5573 } 5574 } 5575 getVarint(pCell, (u64*)&nCellKey); 5576 if( nCellKey<intKey ){ 5577 lwr = idx+1; 5578 if( lwr>upr ){ c = -1; break; } 5579 }else if( nCellKey>intKey ){ 5580 upr = idx-1; 5581 if( lwr>upr ){ c = +1; break; } 5582 }else{ 5583 assert( nCellKey==intKey ); 5584 pCur->ix = (u16)idx; 5585 if( !pPage->leaf ){ 5586 lwr = idx; 5587 goto moveto_table_next_layer; 5588 }else{ 5589 pCur->curFlags |= BTCF_ValidNKey; 5590 pCur->info.nKey = nCellKey; 5591 pCur->info.nSize = 0; 5592 *pRes = 0; 5593 return SQLITE_OK; 5594 } 5595 } 5596 assert( lwr+upr>=0 ); 5597 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2; */ 5598 } 5599 assert( lwr==upr+1 || !pPage->leaf ); 5600 assert( pPage->isInit ); 5601 if( pPage->leaf ){ 5602 assert( pCur->ix<pCur->pPage->nCell ); 5603 pCur->ix = (u16)idx; 5604 *pRes = c; 5605 rc = SQLITE_OK; 5606 goto moveto_table_finish; 5607 } 5608 moveto_table_next_layer: 5609 if( lwr>=pPage->nCell ){ 5610 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]); 5611 }else{ 5612 chldPg = get4byte(findCell(pPage, lwr)); 5613 } 5614 pCur->ix = (u16)lwr; 5615 rc = moveToChild(pCur, chldPg); 5616 if( rc ) break; 5617 } 5618 moveto_table_finish: 5619 pCur->info.nSize = 0; 5620 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 ); 5621 return rc; 5622 } 5623 5624 /* Move the cursor so that it points to an entry in an index table 5625 ** near the key pIdxKey. Return a success code. 5626 ** 5627 ** If an exact match is not found, then the cursor is always 5628 ** left pointing at a leaf page which would hold the entry if it 5629 ** were present. The cursor might point to an entry that comes 5630 ** before or after the key. 5631 ** 5632 ** An integer is written into *pRes which is the result of 5633 ** comparing the key with the entry to which the cursor is 5634 ** pointing. The meaning of the integer written into 5635 ** *pRes is as follows: 5636 ** 5637 ** *pRes<0 The cursor is left pointing at an entry that 5638 ** is smaller than pIdxKey or if the table is empty 5639 ** and the cursor is therefore left point to nothing. 5640 ** 5641 ** *pRes==0 The cursor is left pointing at an entry that 5642 ** exactly matches pIdxKey. 5643 ** 5644 ** *pRes>0 The cursor is left pointing at an entry that 5645 ** is larger than pIdxKey. 5646 ** 5647 ** The pIdxKey->eqSeen field is set to 1 if there 5648 ** exists an entry in the table that exactly matches pIdxKey. 5649 */ 5650 int sqlite3BtreeIndexMoveto( 5651 BtCursor *pCur, /* The cursor to be moved */ 5652 UnpackedRecord *pIdxKey, /* Unpacked index key */ 5653 int *pRes /* Write search results here */ 5654 ){ 5655 int rc; 5656 RecordCompare xRecordCompare; 5657 5658 assert( cursorOwnsBtShared(pCur) ); 5659 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); 5660 assert( pRes ); 5661 assert( pCur->pKeyInfo!=0 ); 5662 5663 #ifdef SQLITE_DEBUG 5664 pCur->pBtree->nSeek++; /* Performance measurement during testing */ 5665 #endif 5666 5667 xRecordCompare = sqlite3VdbeFindCompare(pIdxKey); 5668 pIdxKey->errCode = 0; 5669 assert( pIdxKey->default_rc==1 5670 || pIdxKey->default_rc==0 5671 || pIdxKey->default_rc==-1 5672 ); 5673 5674 rc = moveToRoot(pCur); 5675 if( rc ){ 5676 if( rc==SQLITE_EMPTY ){ 5677 assert( pCur->pgnoRoot==0 || pCur->pPage->nCell==0 ); 5678 *pRes = -1; 5679 return SQLITE_OK; 5680 } 5681 return rc; 5682 } 5683 assert( pCur->pPage ); 5684 assert( pCur->pPage->isInit ); 5685 assert( pCur->eState==CURSOR_VALID ); 5686 assert( pCur->pPage->nCell > 0 ); 5687 assert( pCur->iPage==0 || pCur->apPage[0]->intKey==pCur->curIntKey ); 5688 assert( pCur->curIntKey || pIdxKey ); 5689 for(;;){ 5690 int lwr, upr, idx, c; 5691 Pgno chldPg; 5692 MemPage *pPage = pCur->pPage; 5693 u8 *pCell; /* Pointer to current cell in pPage */ 5694 5695 /* pPage->nCell must be greater than zero. If this is the root-page 5696 ** the cursor would have been INVALID above and this for(;;) loop 5697 ** not run. If this is not the root-page, then the moveToChild() routine 5698 ** would have already detected db corruption. Similarly, pPage must 5699 ** be the right kind (index or table) of b-tree page. Otherwise 5700 ** a moveToChild() or moveToRoot() call would have detected corruption. */ 5701 assert( pPage->nCell>0 ); 5702 assert( pPage->intKey==(pIdxKey==0) ); 5703 lwr = 0; 5704 upr = pPage->nCell-1; 5705 idx = upr>>1; /* idx = (lwr+upr)/2; */ 5706 pCur->ix = (u16)idx; 5707 for(;;){ 5708 int nCell; /* Size of the pCell cell in bytes */ 5709 pCell = findCellPastPtr(pPage, idx); 5710 5711 /* The maximum supported page-size is 65536 bytes. This means that 5712 ** the maximum number of record bytes stored on an index B-Tree 5713 ** page is less than 16384 bytes and may be stored as a 2-byte 5714 ** varint. This information is used to attempt to avoid parsing 5715 ** the entire cell by checking for the cases where the record is 5716 ** stored entirely within the b-tree page by inspecting the first 5717 ** 2 bytes of the cell. 5718 */ 5719 nCell = pCell[0]; 5720 if( nCell<=pPage->max1bytePayload ){ 5721 /* This branch runs if the record-size field of the cell is a 5722 ** single byte varint and the record fits entirely on the main 5723 ** b-tree page. */ 5724 testcase( pCell+nCell+1==pPage->aDataEnd ); 5725 c = xRecordCompare(nCell, (void*)&pCell[1], pIdxKey); 5726 }else if( !(pCell[1] & 0x80) 5727 && (nCell = ((nCell&0x7f)<<7) + pCell[1])<=pPage->maxLocal 5728 ){ 5729 /* The record-size field is a 2 byte varint and the record 5730 ** fits entirely on the main b-tree page. */ 5731 testcase( pCell+nCell+2==pPage->aDataEnd ); 5732 c = xRecordCompare(nCell, (void*)&pCell[2], pIdxKey); 5733 }else{ 5734 /* The record flows over onto one or more overflow pages. In 5735 ** this case the whole cell needs to be parsed, a buffer allocated 5736 ** and accessPayload() used to retrieve the record into the 5737 ** buffer before VdbeRecordCompare() can be called. 5738 ** 5739 ** If the record is corrupt, the xRecordCompare routine may read 5740 ** up to two varints past the end of the buffer. An extra 18 5741 ** bytes of padding is allocated at the end of the buffer in 5742 ** case this happens. */ 5743 void *pCellKey; 5744 u8 * const pCellBody = pCell - pPage->childPtrSize; 5745 const int nOverrun = 18; /* Size of the overrun padding */ 5746 pPage->xParseCell(pPage, pCellBody, &pCur->info); 5747 nCell = (int)pCur->info.nKey; 5748 testcase( nCell<0 ); /* True if key size is 2^32 or more */ 5749 testcase( nCell==0 ); /* Invalid key size: 0x80 0x80 0x00 */ 5750 testcase( nCell==1 ); /* Invalid key size: 0x80 0x80 0x01 */ 5751 testcase( nCell==2 ); /* Minimum legal index key size */ 5752 if( nCell<2 || nCell/pCur->pBt->usableSize>pCur->pBt->nPage ){ 5753 rc = SQLITE_CORRUPT_PAGE(pPage); 5754 goto moveto_index_finish; 5755 } 5756 pCellKey = sqlite3Malloc( nCell+nOverrun ); 5757 if( pCellKey==0 ){ 5758 rc = SQLITE_NOMEM_BKPT; 5759 goto moveto_index_finish; 5760 } 5761 pCur->ix = (u16)idx; 5762 rc = accessPayload(pCur, 0, nCell, (unsigned char*)pCellKey, 0); 5763 memset(((u8*)pCellKey)+nCell,0,nOverrun); /* Fix uninit warnings */ 5764 pCur->curFlags &= ~BTCF_ValidOvfl; 5765 if( rc ){ 5766 sqlite3_free(pCellKey); 5767 goto moveto_index_finish; 5768 } 5769 c = sqlite3VdbeRecordCompare(nCell, pCellKey, pIdxKey); 5770 sqlite3_free(pCellKey); 5771 } 5772 assert( 5773 (pIdxKey->errCode!=SQLITE_CORRUPT || c==0) 5774 && (pIdxKey->errCode!=SQLITE_NOMEM || pCur->pBtree->db->mallocFailed) 5775 ); 5776 if( c<0 ){ 5777 lwr = idx+1; 5778 }else if( c>0 ){ 5779 upr = idx-1; 5780 }else{ 5781 assert( c==0 ); 5782 *pRes = 0; 5783 rc = SQLITE_OK; 5784 pCur->ix = (u16)idx; 5785 if( pIdxKey->errCode ) rc = SQLITE_CORRUPT_BKPT; 5786 goto moveto_index_finish; 5787 } 5788 if( lwr>upr ) break; 5789 assert( lwr+upr>=0 ); 5790 idx = (lwr+upr)>>1; /* idx = (lwr+upr)/2 */ 5791 } 5792 assert( lwr==upr+1 || (pPage->intKey && !pPage->leaf) ); 5793 assert( pPage->isInit ); 5794 if( pPage->leaf ){ 5795 assert( pCur->ix<pCur->pPage->nCell ); 5796 pCur->ix = (u16)idx; 5797 *pRes = c; 5798 rc = SQLITE_OK; 5799 goto moveto_index_finish; 5800 } 5801 if( lwr>=pPage->nCell ){ 5802 chldPg = get4byte(&pPage->aData[pPage->hdrOffset+8]); 5803 }else{ 5804 chldPg = get4byte(findCell(pPage, lwr)); 5805 } 5806 pCur->ix = (u16)lwr; 5807 rc = moveToChild(pCur, chldPg); 5808 if( rc ) break; 5809 } 5810 moveto_index_finish: 5811 pCur->info.nSize = 0; 5812 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 ); 5813 return rc; 5814 } 5815 5816 5817 /* 5818 ** Return TRUE if the cursor is not pointing at an entry of the table. 5819 ** 5820 ** TRUE will be returned after a call to sqlite3BtreeNext() moves 5821 ** past the last entry in the table or sqlite3BtreePrev() moves past 5822 ** the first entry. TRUE is also returned if the table is empty. 5823 */ 5824 int sqlite3BtreeEof(BtCursor *pCur){ 5825 /* TODO: What if the cursor is in CURSOR_REQUIRESEEK but all table entries 5826 ** have been deleted? This API will need to change to return an error code 5827 ** as well as the boolean result value. 5828 */ 5829 return (CURSOR_VALID!=pCur->eState); 5830 } 5831 5832 /* 5833 ** Return an estimate for the number of rows in the table that pCur is 5834 ** pointing to. Return a negative number if no estimate is currently 5835 ** available. 5836 */ 5837 i64 sqlite3BtreeRowCountEst(BtCursor *pCur){ 5838 i64 n; 5839 u8 i; 5840 5841 assert( cursorOwnsBtShared(pCur) ); 5842 assert( sqlite3_mutex_held(pCur->pBtree->db->mutex) ); 5843 5844 /* Currently this interface is only called by the OP_IfSmaller 5845 ** opcode, and it that case the cursor will always be valid and 5846 ** will always point to a leaf node. */ 5847 if( NEVER(pCur->eState!=CURSOR_VALID) ) return -1; 5848 if( NEVER(pCur->pPage->leaf==0) ) return -1; 5849 5850 n = pCur->pPage->nCell; 5851 for(i=0; i<pCur->iPage; i++){ 5852 n *= pCur->apPage[i]->nCell; 5853 } 5854 return n; 5855 } 5856 5857 /* 5858 ** Advance the cursor to the next entry in the database. 5859 ** Return value: 5860 ** 5861 ** SQLITE_OK success 5862 ** SQLITE_DONE cursor is already pointing at the last element 5863 ** otherwise some kind of error occurred 5864 ** 5865 ** The main entry point is sqlite3BtreeNext(). That routine is optimized 5866 ** for the common case of merely incrementing the cell counter BtCursor.aiIdx 5867 ** to the next cell on the current page. The (slower) btreeNext() helper 5868 ** routine is called when it is necessary to move to a different page or 5869 ** to restore the cursor. 5870 ** 5871 ** If bit 0x01 of the F argument in sqlite3BtreeNext(C,F) is 1, then the 5872 ** cursor corresponds to an SQL index and this routine could have been 5873 ** skipped if the SQL index had been a unique index. The F argument 5874 ** is a hint to the implement. SQLite btree implementation does not use 5875 ** this hint, but COMDB2 does. 5876 */ 5877 static SQLITE_NOINLINE int btreeNext(BtCursor *pCur){ 5878 int rc; 5879 int idx; 5880 MemPage *pPage; 5881 5882 assert( cursorOwnsBtShared(pCur) ); 5883 if( pCur->eState!=CURSOR_VALID ){ 5884 assert( (pCur->curFlags & BTCF_ValidOvfl)==0 ); 5885 rc = restoreCursorPosition(pCur); 5886 if( rc!=SQLITE_OK ){ 5887 return rc; 5888 } 5889 if( CURSOR_INVALID==pCur->eState ){ 5890 return SQLITE_DONE; 5891 } 5892 if( pCur->eState==CURSOR_SKIPNEXT ){ 5893 pCur->eState = CURSOR_VALID; 5894 if( pCur->skipNext>0 ) return SQLITE_OK; 5895 } 5896 } 5897 5898 pPage = pCur->pPage; 5899 idx = ++pCur->ix; 5900 if( !pPage->isInit || sqlite3FaultSim(412) ){ 5901 /* The only known way for this to happen is for there to be a 5902 ** recursive SQL function that does a DELETE operation as part of a 5903 ** SELECT which deletes content out from under an active cursor 5904 ** in a corrupt database file where the table being DELETE-ed from 5905 ** has pages in common with the table being queried. See TH3 5906 ** module cov1/btree78.test testcase 220 (2018-06-08) for an 5907 ** example. */ 5908 return SQLITE_CORRUPT_BKPT; 5909 } 5910 5911 if( idx>=pPage->nCell ){ 5912 if( !pPage->leaf ){ 5913 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); 5914 if( rc ) return rc; 5915 return moveToLeftmost(pCur); 5916 } 5917 do{ 5918 if( pCur->iPage==0 ){ 5919 pCur->eState = CURSOR_INVALID; 5920 return SQLITE_DONE; 5921 } 5922 moveToParent(pCur); 5923 pPage = pCur->pPage; 5924 }while( pCur->ix>=pPage->nCell ); 5925 if( pPage->intKey ){ 5926 return sqlite3BtreeNext(pCur, 0); 5927 }else{ 5928 return SQLITE_OK; 5929 } 5930 } 5931 if( pPage->leaf ){ 5932 return SQLITE_OK; 5933 }else{ 5934 return moveToLeftmost(pCur); 5935 } 5936 } 5937 int sqlite3BtreeNext(BtCursor *pCur, int flags){ 5938 MemPage *pPage; 5939 UNUSED_PARAMETER( flags ); /* Used in COMDB2 but not native SQLite */ 5940 assert( cursorOwnsBtShared(pCur) ); 5941 assert( flags==0 || flags==1 ); 5942 pCur->info.nSize = 0; 5943 pCur->curFlags &= ~(BTCF_ValidNKey|BTCF_ValidOvfl); 5944 if( pCur->eState!=CURSOR_VALID ) return btreeNext(pCur); 5945 pPage = pCur->pPage; 5946 if( (++pCur->ix)>=pPage->nCell ){ 5947 pCur->ix--; 5948 return btreeNext(pCur); 5949 } 5950 if( pPage->leaf ){ 5951 return SQLITE_OK; 5952 }else{ 5953 return moveToLeftmost(pCur); 5954 } 5955 } 5956 5957 /* 5958 ** Step the cursor to the back to the previous entry in the database. 5959 ** Return values: 5960 ** 5961 ** SQLITE_OK success 5962 ** SQLITE_DONE the cursor is already on the first element of the table 5963 ** otherwise some kind of error occurred 5964 ** 5965 ** The main entry point is sqlite3BtreePrevious(). That routine is optimized 5966 ** for the common case of merely decrementing the cell counter BtCursor.aiIdx 5967 ** to the previous cell on the current page. The (slower) btreePrevious() 5968 ** helper routine is called when it is necessary to move to a different page 5969 ** or to restore the cursor. 5970 ** 5971 ** If bit 0x01 of the F argument to sqlite3BtreePrevious(C,F) is 1, then 5972 ** the cursor corresponds to an SQL index and this routine could have been 5973 ** skipped if the SQL index had been a unique index. The F argument is a 5974 ** hint to the implement. The native SQLite btree implementation does not 5975 ** use this hint, but COMDB2 does. 5976 */ 5977 static SQLITE_NOINLINE int btreePrevious(BtCursor *pCur){ 5978 int rc; 5979 MemPage *pPage; 5980 5981 assert( cursorOwnsBtShared(pCur) ); 5982 assert( (pCur->curFlags & (BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey))==0 ); 5983 assert( pCur->info.nSize==0 ); 5984 if( pCur->eState!=CURSOR_VALID ){ 5985 rc = restoreCursorPosition(pCur); 5986 if( rc!=SQLITE_OK ){ 5987 return rc; 5988 } 5989 if( CURSOR_INVALID==pCur->eState ){ 5990 return SQLITE_DONE; 5991 } 5992 if( CURSOR_SKIPNEXT==pCur->eState ){ 5993 pCur->eState = CURSOR_VALID; 5994 if( pCur->skipNext<0 ) return SQLITE_OK; 5995 } 5996 } 5997 5998 pPage = pCur->pPage; 5999 assert( pPage->isInit ); 6000 if( !pPage->leaf ){ 6001 int idx = pCur->ix; 6002 rc = moveToChild(pCur, get4byte(findCell(pPage, idx))); 6003 if( rc ) return rc; 6004 rc = moveToRightmost(pCur); 6005 }else{ 6006 while( pCur->ix==0 ){ 6007 if( pCur->iPage==0 ){ 6008 pCur->eState = CURSOR_INVALID; 6009 return SQLITE_DONE; 6010 } 6011 moveToParent(pCur); 6012 } 6013 assert( pCur->info.nSize==0 ); 6014 assert( (pCur->curFlags & (BTCF_ValidOvfl))==0 ); 6015 6016 pCur->ix--; 6017 pPage = pCur->pPage; 6018 if( pPage->intKey && !pPage->leaf ){ 6019 rc = sqlite3BtreePrevious(pCur, 0); 6020 }else{ 6021 rc = SQLITE_OK; 6022 } 6023 } 6024 return rc; 6025 } 6026 int sqlite3BtreePrevious(BtCursor *pCur, int flags){ 6027 assert( cursorOwnsBtShared(pCur) ); 6028 assert( flags==0 || flags==1 ); 6029 UNUSED_PARAMETER( flags ); /* Used in COMDB2 but not native SQLite */ 6030 pCur->curFlags &= ~(BTCF_AtLast|BTCF_ValidOvfl|BTCF_ValidNKey); 6031 pCur->info.nSize = 0; 6032 if( pCur->eState!=CURSOR_VALID 6033 || pCur->ix==0 6034 || pCur->pPage->leaf==0 6035 ){ 6036 return btreePrevious(pCur); 6037 } 6038 pCur->ix--; 6039 return SQLITE_OK; 6040 } 6041 6042 /* 6043 ** Allocate a new page from the database file. 6044 ** 6045 ** The new page is marked as dirty. (In other words, sqlite3PagerWrite() 6046 ** has already been called on the new page.) The new page has also 6047 ** been referenced and the calling routine is responsible for calling 6048 ** sqlite3PagerUnref() on the new page when it is done. 6049 ** 6050 ** SQLITE_OK is returned on success. Any other return value indicates 6051 ** an error. *ppPage is set to NULL in the event of an error. 6052 ** 6053 ** If the "nearby" parameter is not 0, then an effort is made to 6054 ** locate a page close to the page number "nearby". This can be used in an 6055 ** attempt to keep related pages close to each other in the database file, 6056 ** which in turn can make database access faster. 6057 ** 6058 ** If the eMode parameter is BTALLOC_EXACT and the nearby page exists 6059 ** anywhere on the free-list, then it is guaranteed to be returned. If 6060 ** eMode is BTALLOC_LT then the page returned will be less than or equal 6061 ** to nearby if any such page exists. If eMode is BTALLOC_ANY then there 6062 ** are no restrictions on which page is returned. 6063 */ 6064 static int allocateBtreePage( 6065 BtShared *pBt, /* The btree */ 6066 MemPage **ppPage, /* Store pointer to the allocated page here */ 6067 Pgno *pPgno, /* Store the page number here */ 6068 Pgno nearby, /* Search for a page near this one */ 6069 u8 eMode /* BTALLOC_EXACT, BTALLOC_LT, or BTALLOC_ANY */ 6070 ){ 6071 MemPage *pPage1; 6072 int rc; 6073 u32 n; /* Number of pages on the freelist */ 6074 u32 k; /* Number of leaves on the trunk of the freelist */ 6075 MemPage *pTrunk = 0; 6076 MemPage *pPrevTrunk = 0; 6077 Pgno mxPage; /* Total size of the database file */ 6078 6079 assert( sqlite3_mutex_held(pBt->mutex) ); 6080 assert( eMode==BTALLOC_ANY || (nearby>0 && IfNotOmitAV(pBt->autoVacuum)) ); 6081 pPage1 = pBt->pPage1; 6082 mxPage = btreePagecount(pBt); 6083 /* EVIDENCE-OF: R-05119-02637 The 4-byte big-endian integer at offset 36 6084 ** stores stores the total number of pages on the freelist. */ 6085 n = get4byte(&pPage1->aData[36]); 6086 testcase( n==mxPage-1 ); 6087 if( n>=mxPage ){ 6088 return SQLITE_CORRUPT_BKPT; 6089 } 6090 if( n>0 ){ 6091 /* There are pages on the freelist. Reuse one of those pages. */ 6092 Pgno iTrunk; 6093 u8 searchList = 0; /* If the free-list must be searched for 'nearby' */ 6094 u32 nSearch = 0; /* Count of the number of search attempts */ 6095 6096 /* If eMode==BTALLOC_EXACT and a query of the pointer-map 6097 ** shows that the page 'nearby' is somewhere on the free-list, then 6098 ** the entire-list will be searched for that page. 6099 */ 6100 #ifndef SQLITE_OMIT_AUTOVACUUM 6101 if( eMode==BTALLOC_EXACT ){ 6102 if( nearby<=mxPage ){ 6103 u8 eType; 6104 assert( nearby>0 ); 6105 assert( pBt->autoVacuum ); 6106 rc = ptrmapGet(pBt, nearby, &eType, 0); 6107 if( rc ) return rc; 6108 if( eType==PTRMAP_FREEPAGE ){ 6109 searchList = 1; 6110 } 6111 } 6112 }else if( eMode==BTALLOC_LE ){ 6113 searchList = 1; 6114 } 6115 #endif 6116 6117 /* Decrement the free-list count by 1. Set iTrunk to the index of the 6118 ** first free-list trunk page. iPrevTrunk is initially 1. 6119 */ 6120 rc = sqlite3PagerWrite(pPage1->pDbPage); 6121 if( rc ) return rc; 6122 put4byte(&pPage1->aData[36], n-1); 6123 6124 /* The code within this loop is run only once if the 'searchList' variable 6125 ** is not true. Otherwise, it runs once for each trunk-page on the 6126 ** free-list until the page 'nearby' is located (eMode==BTALLOC_EXACT) 6127 ** or until a page less than 'nearby' is located (eMode==BTALLOC_LT) 6128 */ 6129 do { 6130 pPrevTrunk = pTrunk; 6131 if( pPrevTrunk ){ 6132 /* EVIDENCE-OF: R-01506-11053 The first integer on a freelist trunk page 6133 ** is the page number of the next freelist trunk page in the list or 6134 ** zero if this is the last freelist trunk page. */ 6135 iTrunk = get4byte(&pPrevTrunk->aData[0]); 6136 }else{ 6137 /* EVIDENCE-OF: R-59841-13798 The 4-byte big-endian integer at offset 32 6138 ** stores the page number of the first page of the freelist, or zero if 6139 ** the freelist is empty. */ 6140 iTrunk = get4byte(&pPage1->aData[32]); 6141 } 6142 testcase( iTrunk==mxPage ); 6143 if( iTrunk>mxPage || nSearch++ > n ){ 6144 rc = SQLITE_CORRUPT_PGNO(pPrevTrunk ? pPrevTrunk->pgno : 1); 6145 }else{ 6146 rc = btreeGetUnusedPage(pBt, iTrunk, &pTrunk, 0); 6147 } 6148 if( rc ){ 6149 pTrunk = 0; 6150 goto end_allocate_page; 6151 } 6152 assert( pTrunk!=0 ); 6153 assert( pTrunk->aData!=0 ); 6154 /* EVIDENCE-OF: R-13523-04394 The second integer on a freelist trunk page 6155 ** is the number of leaf page pointers to follow. */ 6156 k = get4byte(&pTrunk->aData[4]); 6157 if( k==0 && !searchList ){ 6158 /* The trunk has no leaves and the list is not being searched. 6159 ** So extract the trunk page itself and use it as the newly 6160 ** allocated page */ 6161 assert( pPrevTrunk==0 ); 6162 rc = sqlite3PagerWrite(pTrunk->pDbPage); 6163 if( rc ){ 6164 goto end_allocate_page; 6165 } 6166 *pPgno = iTrunk; 6167 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); 6168 *ppPage = pTrunk; 6169 pTrunk = 0; 6170 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); 6171 }else if( k>(u32)(pBt->usableSize/4 - 2) ){ 6172 /* Value of k is out of range. Database corruption */ 6173 rc = SQLITE_CORRUPT_PGNO(iTrunk); 6174 goto end_allocate_page; 6175 #ifndef SQLITE_OMIT_AUTOVACUUM 6176 }else if( searchList 6177 && (nearby==iTrunk || (iTrunk<nearby && eMode==BTALLOC_LE)) 6178 ){ 6179 /* The list is being searched and this trunk page is the page 6180 ** to allocate, regardless of whether it has leaves. 6181 */ 6182 *pPgno = iTrunk; 6183 *ppPage = pTrunk; 6184 searchList = 0; 6185 rc = sqlite3PagerWrite(pTrunk->pDbPage); 6186 if( rc ){ 6187 goto end_allocate_page; 6188 } 6189 if( k==0 ){ 6190 if( !pPrevTrunk ){ 6191 memcpy(&pPage1->aData[32], &pTrunk->aData[0], 4); 6192 }else{ 6193 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage); 6194 if( rc!=SQLITE_OK ){ 6195 goto end_allocate_page; 6196 } 6197 memcpy(&pPrevTrunk->aData[0], &pTrunk->aData[0], 4); 6198 } 6199 }else{ 6200 /* The trunk page is required by the caller but it contains 6201 ** pointers to free-list leaves. The first leaf becomes a trunk 6202 ** page in this case. 6203 */ 6204 MemPage *pNewTrunk; 6205 Pgno iNewTrunk = get4byte(&pTrunk->aData[8]); 6206 if( iNewTrunk>mxPage ){ 6207 rc = SQLITE_CORRUPT_PGNO(iTrunk); 6208 goto end_allocate_page; 6209 } 6210 testcase( iNewTrunk==mxPage ); 6211 rc = btreeGetUnusedPage(pBt, iNewTrunk, &pNewTrunk, 0); 6212 if( rc!=SQLITE_OK ){ 6213 goto end_allocate_page; 6214 } 6215 rc = sqlite3PagerWrite(pNewTrunk->pDbPage); 6216 if( rc!=SQLITE_OK ){ 6217 releasePage(pNewTrunk); 6218 goto end_allocate_page; 6219 } 6220 memcpy(&pNewTrunk->aData[0], &pTrunk->aData[0], 4); 6221 put4byte(&pNewTrunk->aData[4], k-1); 6222 memcpy(&pNewTrunk->aData[8], &pTrunk->aData[12], (k-1)*4); 6223 releasePage(pNewTrunk); 6224 if( !pPrevTrunk ){ 6225 assert( sqlite3PagerIswriteable(pPage1->pDbPage) ); 6226 put4byte(&pPage1->aData[32], iNewTrunk); 6227 }else{ 6228 rc = sqlite3PagerWrite(pPrevTrunk->pDbPage); 6229 if( rc ){ 6230 goto end_allocate_page; 6231 } 6232 put4byte(&pPrevTrunk->aData[0], iNewTrunk); 6233 } 6234 } 6235 pTrunk = 0; 6236 TRACE(("ALLOCATE: %d trunk - %d free pages left\n", *pPgno, n-1)); 6237 #endif 6238 }else if( k>0 ){ 6239 /* Extract a leaf from the trunk */ 6240 u32 closest; 6241 Pgno iPage; 6242 unsigned char *aData = pTrunk->aData; 6243 if( nearby>0 ){ 6244 u32 i; 6245 closest = 0; 6246 if( eMode==BTALLOC_LE ){ 6247 for(i=0; i<k; i++){ 6248 iPage = get4byte(&aData[8+i*4]); 6249 if( iPage<=nearby ){ 6250 closest = i; 6251 break; 6252 } 6253 } 6254 }else{ 6255 int dist; 6256 dist = sqlite3AbsInt32(get4byte(&aData[8]) - nearby); 6257 for(i=1; i<k; i++){ 6258 int d2 = sqlite3AbsInt32(get4byte(&aData[8+i*4]) - nearby); 6259 if( d2<dist ){ 6260 closest = i; 6261 dist = d2; 6262 } 6263 } 6264 } 6265 }else{ 6266 closest = 0; 6267 } 6268 6269 iPage = get4byte(&aData[8+closest*4]); 6270 testcase( iPage==mxPage ); 6271 if( iPage>mxPage || iPage<2 ){ 6272 rc = SQLITE_CORRUPT_PGNO(iTrunk); 6273 goto end_allocate_page; 6274 } 6275 testcase( iPage==mxPage ); 6276 if( !searchList 6277 || (iPage==nearby || (iPage<nearby && eMode==BTALLOC_LE)) 6278 ){ 6279 int noContent; 6280 *pPgno = iPage; 6281 TRACE(("ALLOCATE: %d was leaf %d of %d on trunk %d" 6282 ": %d more free pages\n", 6283 *pPgno, closest+1, k, pTrunk->pgno, n-1)); 6284 rc = sqlite3PagerWrite(pTrunk->pDbPage); 6285 if( rc ) goto end_allocate_page; 6286 if( closest<k-1 ){ 6287 memcpy(&aData[8+closest*4], &aData[4+k*4], 4); 6288 } 6289 put4byte(&aData[4], k-1); 6290 noContent = !btreeGetHasContent(pBt, *pPgno)? PAGER_GET_NOCONTENT : 0; 6291 rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, noContent); 6292 if( rc==SQLITE_OK ){ 6293 rc = sqlite3PagerWrite((*ppPage)->pDbPage); 6294 if( rc!=SQLITE_OK ){ 6295 releasePage(*ppPage); 6296 *ppPage = 0; 6297 } 6298 } 6299 searchList = 0; 6300 } 6301 } 6302 releasePage(pPrevTrunk); 6303 pPrevTrunk = 0; 6304 }while( searchList ); 6305 }else{ 6306 /* There are no pages on the freelist, so append a new page to the 6307 ** database image. 6308 ** 6309 ** Normally, new pages allocated by this block can be requested from the 6310 ** pager layer with the 'no-content' flag set. This prevents the pager 6311 ** from trying to read the pages content from disk. However, if the 6312 ** current transaction has already run one or more incremental-vacuum 6313 ** steps, then the page we are about to allocate may contain content 6314 ** that is required in the event of a rollback. In this case, do 6315 ** not set the no-content flag. This causes the pager to load and journal 6316 ** the current page content before overwriting it. 6317 ** 6318 ** Note that the pager will not actually attempt to load or journal 6319 ** content for any page that really does lie past the end of the database 6320 ** file on disk. So the effects of disabling the no-content optimization 6321 ** here are confined to those pages that lie between the end of the 6322 ** database image and the end of the database file. 6323 */ 6324 int bNoContent = (0==IfNotOmitAV(pBt->bDoTruncate))? PAGER_GET_NOCONTENT:0; 6325 6326 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); 6327 if( rc ) return rc; 6328 pBt->nPage++; 6329 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ) pBt->nPage++; 6330 6331 #ifndef SQLITE_OMIT_AUTOVACUUM 6332 if( pBt->autoVacuum && PTRMAP_ISPAGE(pBt, pBt->nPage) ){ 6333 /* If *pPgno refers to a pointer-map page, allocate two new pages 6334 ** at the end of the file instead of one. The first allocated page 6335 ** becomes a new pointer-map page, the second is used by the caller. 6336 */ 6337 MemPage *pPg = 0; 6338 TRACE(("ALLOCATE: %d from end of file (pointer-map page)\n", pBt->nPage)); 6339 assert( pBt->nPage!=PENDING_BYTE_PAGE(pBt) ); 6340 rc = btreeGetUnusedPage(pBt, pBt->nPage, &pPg, bNoContent); 6341 if( rc==SQLITE_OK ){ 6342 rc = sqlite3PagerWrite(pPg->pDbPage); 6343 releasePage(pPg); 6344 } 6345 if( rc ) return rc; 6346 pBt->nPage++; 6347 if( pBt->nPage==PENDING_BYTE_PAGE(pBt) ){ pBt->nPage++; } 6348 } 6349 #endif 6350 put4byte(28 + (u8*)pBt->pPage1->aData, pBt->nPage); 6351 *pPgno = pBt->nPage; 6352 6353 assert( *pPgno!=PENDING_BYTE_PAGE(pBt) ); 6354 rc = btreeGetUnusedPage(pBt, *pPgno, ppPage, bNoContent); 6355 if( rc ) return rc; 6356 rc = sqlite3PagerWrite((*ppPage)->pDbPage); 6357 if( rc!=SQLITE_OK ){ 6358 releasePage(*ppPage); 6359 *ppPage = 0; 6360 } 6361 TRACE(("ALLOCATE: %d from end of file\n", *pPgno)); 6362 } 6363 6364 assert( CORRUPT_DB || *pPgno!=PENDING_BYTE_PAGE(pBt) ); 6365 6366 end_allocate_page: 6367 releasePage(pTrunk); 6368 releasePage(pPrevTrunk); 6369 assert( rc!=SQLITE_OK || sqlite3PagerPageRefcount((*ppPage)->pDbPage)<=1 ); 6370 assert( rc!=SQLITE_OK || (*ppPage)->isInit==0 ); 6371 return rc; 6372 } 6373 6374 /* 6375 ** This function is used to add page iPage to the database file free-list. 6376 ** It is assumed that the page is not already a part of the free-list. 6377 ** 6378 ** The value passed as the second argument to this function is optional. 6379 ** If the caller happens to have a pointer to the MemPage object 6380 ** corresponding to page iPage handy, it may pass it as the second value. 6381 ** Otherwise, it may pass NULL. 6382 ** 6383 ** If a pointer to a MemPage object is passed as the second argument, 6384 ** its reference count is not altered by this function. 6385 */ 6386 static int freePage2(BtShared *pBt, MemPage *pMemPage, Pgno iPage){ 6387 MemPage *pTrunk = 0; /* Free-list trunk page */ 6388 Pgno iTrunk = 0; /* Page number of free-list trunk page */ 6389 MemPage *pPage1 = pBt->pPage1; /* Local reference to page 1 */ 6390 MemPage *pPage; /* Page being freed. May be NULL. */ 6391 int rc; /* Return Code */ 6392 u32 nFree; /* Initial number of pages on free-list */ 6393 6394 assert( sqlite3_mutex_held(pBt->mutex) ); 6395 assert( CORRUPT_DB || iPage>1 ); 6396 assert( !pMemPage || pMemPage->pgno==iPage ); 6397 6398 if( NEVER(iPage<2) || iPage>pBt->nPage ){ 6399 return SQLITE_CORRUPT_BKPT; 6400 } 6401 if( pMemPage ){ 6402 pPage = pMemPage; 6403 sqlite3PagerRef(pPage->pDbPage); 6404 }else{ 6405 pPage = btreePageLookup(pBt, iPage); 6406 } 6407 6408 /* Increment the free page count on pPage1 */ 6409 rc = sqlite3PagerWrite(pPage1->pDbPage); 6410 if( rc ) goto freepage_out; 6411 nFree = get4byte(&pPage1->aData[36]); 6412 put4byte(&pPage1->aData[36], nFree+1); 6413 6414 if( pBt->btsFlags & BTS_SECURE_DELETE ){ 6415 /* If the secure_delete option is enabled, then 6416 ** always fully overwrite deleted information with zeros. 6417 */ 6418 if( (!pPage && ((rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0) ) 6419 || ((rc = sqlite3PagerWrite(pPage->pDbPage))!=0) 6420 ){ 6421 goto freepage_out; 6422 } 6423 memset(pPage->aData, 0, pPage->pBt->pageSize); 6424 } 6425 6426 /* If the database supports auto-vacuum, write an entry in the pointer-map 6427 ** to indicate that the page is free. 6428 */ 6429 if( ISAUTOVACUUM ){ 6430 ptrmapPut(pBt, iPage, PTRMAP_FREEPAGE, 0, &rc); 6431 if( rc ) goto freepage_out; 6432 } 6433 6434 /* Now manipulate the actual database free-list structure. There are two 6435 ** possibilities. If the free-list is currently empty, or if the first 6436 ** trunk page in the free-list is full, then this page will become a 6437 ** new free-list trunk page. Otherwise, it will become a leaf of the 6438 ** first trunk page in the current free-list. This block tests if it 6439 ** is possible to add the page as a new free-list leaf. 6440 */ 6441 if( nFree!=0 ){ 6442 u32 nLeaf; /* Initial number of leaf cells on trunk page */ 6443 6444 iTrunk = get4byte(&pPage1->aData[32]); 6445 if( iTrunk>btreePagecount(pBt) ){ 6446 rc = SQLITE_CORRUPT_BKPT; 6447 goto freepage_out; 6448 } 6449 rc = btreeGetPage(pBt, iTrunk, &pTrunk, 0); 6450 if( rc!=SQLITE_OK ){ 6451 goto freepage_out; 6452 } 6453 6454 nLeaf = get4byte(&pTrunk->aData[4]); 6455 assert( pBt->usableSize>32 ); 6456 if( nLeaf > (u32)pBt->usableSize/4 - 2 ){ 6457 rc = SQLITE_CORRUPT_BKPT; 6458 goto freepage_out; 6459 } 6460 if( nLeaf < (u32)pBt->usableSize/4 - 8 ){ 6461 /* In this case there is room on the trunk page to insert the page 6462 ** being freed as a new leaf. 6463 ** 6464 ** Note that the trunk page is not really full until it contains 6465 ** usableSize/4 - 2 entries, not usableSize/4 - 8 entries as we have 6466 ** coded. But due to a coding error in versions of SQLite prior to 6467 ** 3.6.0, databases with freelist trunk pages holding more than 6468 ** usableSize/4 - 8 entries will be reported as corrupt. In order 6469 ** to maintain backwards compatibility with older versions of SQLite, 6470 ** we will continue to restrict the number of entries to usableSize/4 - 8 6471 ** for now. At some point in the future (once everyone has upgraded 6472 ** to 3.6.0 or later) we should consider fixing the conditional above 6473 ** to read "usableSize/4-2" instead of "usableSize/4-8". 6474 ** 6475 ** EVIDENCE-OF: R-19920-11576 However, newer versions of SQLite still 6476 ** avoid using the last six entries in the freelist trunk page array in 6477 ** order that database files created by newer versions of SQLite can be 6478 ** read by older versions of SQLite. 6479 */ 6480 rc = sqlite3PagerWrite(pTrunk->pDbPage); 6481 if( rc==SQLITE_OK ){ 6482 put4byte(&pTrunk->aData[4], nLeaf+1); 6483 put4byte(&pTrunk->aData[8+nLeaf*4], iPage); 6484 if( pPage && (pBt->btsFlags & BTS_SECURE_DELETE)==0 ){ 6485 sqlite3PagerDontWrite(pPage->pDbPage); 6486 } 6487 rc = btreeSetHasContent(pBt, iPage); 6488 } 6489 TRACE(("FREE-PAGE: %d leaf on trunk page %d\n",pPage->pgno,pTrunk->pgno)); 6490 goto freepage_out; 6491 } 6492 } 6493 6494 /* If control flows to this point, then it was not possible to add the 6495 ** the page being freed as a leaf page of the first trunk in the free-list. 6496 ** Possibly because the free-list is empty, or possibly because the 6497 ** first trunk in the free-list is full. Either way, the page being freed 6498 ** will become the new first trunk page in the free-list. 6499 */ 6500 if( pPage==0 && SQLITE_OK!=(rc = btreeGetPage(pBt, iPage, &pPage, 0)) ){ 6501 goto freepage_out; 6502 } 6503 rc = sqlite3PagerWrite(pPage->pDbPage); 6504 if( rc!=SQLITE_OK ){ 6505 goto freepage_out; 6506 } 6507 put4byte(pPage->aData, iTrunk); 6508 put4byte(&pPage->aData[4], 0); 6509 put4byte(&pPage1->aData[32], iPage); 6510 TRACE(("FREE-PAGE: %d new trunk page replacing %d\n", pPage->pgno, iTrunk)); 6511 6512 freepage_out: 6513 if( pPage ){ 6514 pPage->isInit = 0; 6515 } 6516 releasePage(pPage); 6517 releasePage(pTrunk); 6518 return rc; 6519 } 6520 static void freePage(MemPage *pPage, int *pRC){ 6521 if( (*pRC)==SQLITE_OK ){ 6522 *pRC = freePage2(pPage->pBt, pPage, pPage->pgno); 6523 } 6524 } 6525 6526 /* 6527 ** Free the overflow pages associated with the given Cell. 6528 */ 6529 static SQLITE_NOINLINE int clearCellOverflow( 6530 MemPage *pPage, /* The page that contains the Cell */ 6531 unsigned char *pCell, /* First byte of the Cell */ 6532 CellInfo *pInfo /* Size information about the cell */ 6533 ){ 6534 BtShared *pBt; 6535 Pgno ovflPgno; 6536 int rc; 6537 int nOvfl; 6538 u32 ovflPageSize; 6539 6540 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 6541 assert( pInfo->nLocal!=pInfo->nPayload ); 6542 testcase( pCell + pInfo->nSize == pPage->aDataEnd ); 6543 testcase( pCell + (pInfo->nSize-1) == pPage->aDataEnd ); 6544 if( pCell + pInfo->nSize > pPage->aDataEnd ){ 6545 /* Cell extends past end of page */ 6546 return SQLITE_CORRUPT_PAGE(pPage); 6547 } 6548 ovflPgno = get4byte(pCell + pInfo->nSize - 4); 6549 pBt = pPage->pBt; 6550 assert( pBt->usableSize > 4 ); 6551 ovflPageSize = pBt->usableSize - 4; 6552 nOvfl = (pInfo->nPayload - pInfo->nLocal + ovflPageSize - 1)/ovflPageSize; 6553 assert( nOvfl>0 || 6554 (CORRUPT_DB && (pInfo->nPayload + ovflPageSize)<ovflPageSize) 6555 ); 6556 while( nOvfl-- ){ 6557 Pgno iNext = 0; 6558 MemPage *pOvfl = 0; 6559 if( ovflPgno<2 || ovflPgno>btreePagecount(pBt) ){ 6560 /* 0 is not a legal page number and page 1 cannot be an 6561 ** overflow page. Therefore if ovflPgno<2 or past the end of the 6562 ** file the database must be corrupt. */ 6563 return SQLITE_CORRUPT_BKPT; 6564 } 6565 if( nOvfl ){ 6566 rc = getOverflowPage(pBt, ovflPgno, &pOvfl, &iNext); 6567 if( rc ) return rc; 6568 } 6569 6570 if( ( pOvfl || ((pOvfl = btreePageLookup(pBt, ovflPgno))!=0) ) 6571 && sqlite3PagerPageRefcount(pOvfl->pDbPage)!=1 6572 ){ 6573 /* There is no reason any cursor should have an outstanding reference 6574 ** to an overflow page belonging to a cell that is being deleted/updated. 6575 ** So if there exists more than one reference to this page, then it 6576 ** must not really be an overflow page and the database must be corrupt. 6577 ** It is helpful to detect this before calling freePage2(), as 6578 ** freePage2() may zero the page contents if secure-delete mode is 6579 ** enabled. If this 'overflow' page happens to be a page that the 6580 ** caller is iterating through or using in some other way, this 6581 ** can be problematic. 6582 */ 6583 rc = SQLITE_CORRUPT_BKPT; 6584 }else{ 6585 rc = freePage2(pBt, pOvfl, ovflPgno); 6586 } 6587 6588 if( pOvfl ){ 6589 sqlite3PagerUnref(pOvfl->pDbPage); 6590 } 6591 if( rc ) return rc; 6592 ovflPgno = iNext; 6593 } 6594 return SQLITE_OK; 6595 } 6596 6597 /* Call xParseCell to compute the size of a cell. If the cell contains 6598 ** overflow, then invoke cellClearOverflow to clear out that overflow. 6599 ** STore the result code (SQLITE_OK or some error code) in rc. 6600 ** 6601 ** Implemented as macro to force inlining for performance. 6602 */ 6603 #define BTREE_CLEAR_CELL(rc, pPage, pCell, sInfo) \ 6604 pPage->xParseCell(pPage, pCell, &sInfo); \ 6605 if( sInfo.nLocal!=sInfo.nPayload ){ \ 6606 rc = clearCellOverflow(pPage, pCell, &sInfo); \ 6607 }else{ \ 6608 rc = SQLITE_OK; \ 6609 } 6610 6611 6612 /* 6613 ** Create the byte sequence used to represent a cell on page pPage 6614 ** and write that byte sequence into pCell[]. Overflow pages are 6615 ** allocated and filled in as necessary. The calling procedure 6616 ** is responsible for making sure sufficient space has been allocated 6617 ** for pCell[]. 6618 ** 6619 ** Note that pCell does not necessary need to point to the pPage->aData 6620 ** area. pCell might point to some temporary storage. The cell will 6621 ** be constructed in this temporary area then copied into pPage->aData 6622 ** later. 6623 */ 6624 static int fillInCell( 6625 MemPage *pPage, /* The page that contains the cell */ 6626 unsigned char *pCell, /* Complete text of the cell */ 6627 const BtreePayload *pX, /* Payload with which to construct the cell */ 6628 int *pnSize /* Write cell size here */ 6629 ){ 6630 int nPayload; 6631 const u8 *pSrc; 6632 int nSrc, n, rc, mn; 6633 int spaceLeft; 6634 MemPage *pToRelease; 6635 unsigned char *pPrior; 6636 unsigned char *pPayload; 6637 BtShared *pBt; 6638 Pgno pgnoOvfl; 6639 int nHeader; 6640 6641 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 6642 6643 /* pPage is not necessarily writeable since pCell might be auxiliary 6644 ** buffer space that is separate from the pPage buffer area */ 6645 assert( pCell<pPage->aData || pCell>=&pPage->aData[pPage->pBt->pageSize] 6646 || sqlite3PagerIswriteable(pPage->pDbPage) ); 6647 6648 /* Fill in the header. */ 6649 nHeader = pPage->childPtrSize; 6650 if( pPage->intKey ){ 6651 nPayload = pX->nData + pX->nZero; 6652 pSrc = pX->pData; 6653 nSrc = pX->nData; 6654 assert( pPage->intKeyLeaf ); /* fillInCell() only called for leaves */ 6655 nHeader += putVarint32(&pCell[nHeader], nPayload); 6656 nHeader += putVarint(&pCell[nHeader], *(u64*)&pX->nKey); 6657 }else{ 6658 assert( pX->nKey<=0x7fffffff && pX->pKey!=0 ); 6659 nSrc = nPayload = (int)pX->nKey; 6660 pSrc = pX->pKey; 6661 nHeader += putVarint32(&pCell[nHeader], nPayload); 6662 } 6663 6664 /* Fill in the payload */ 6665 pPayload = &pCell[nHeader]; 6666 if( nPayload<=pPage->maxLocal ){ 6667 /* This is the common case where everything fits on the btree page 6668 ** and no overflow pages are required. */ 6669 n = nHeader + nPayload; 6670 testcase( n==3 ); 6671 testcase( n==4 ); 6672 if( n<4 ) n = 4; 6673 *pnSize = n; 6674 assert( nSrc<=nPayload ); 6675 testcase( nSrc<nPayload ); 6676 memcpy(pPayload, pSrc, nSrc); 6677 memset(pPayload+nSrc, 0, nPayload-nSrc); 6678 return SQLITE_OK; 6679 } 6680 6681 /* If we reach this point, it means that some of the content will need 6682 ** to spill onto overflow pages. 6683 */ 6684 mn = pPage->minLocal; 6685 n = mn + (nPayload - mn) % (pPage->pBt->usableSize - 4); 6686 testcase( n==pPage->maxLocal ); 6687 testcase( n==pPage->maxLocal+1 ); 6688 if( n > pPage->maxLocal ) n = mn; 6689 spaceLeft = n; 6690 *pnSize = n + nHeader + 4; 6691 pPrior = &pCell[nHeader+n]; 6692 pToRelease = 0; 6693 pgnoOvfl = 0; 6694 pBt = pPage->pBt; 6695 6696 /* At this point variables should be set as follows: 6697 ** 6698 ** nPayload Total payload size in bytes 6699 ** pPayload Begin writing payload here 6700 ** spaceLeft Space available at pPayload. If nPayload>spaceLeft, 6701 ** that means content must spill into overflow pages. 6702 ** *pnSize Size of the local cell (not counting overflow pages) 6703 ** pPrior Where to write the pgno of the first overflow page 6704 ** 6705 ** Use a call to btreeParseCellPtr() to verify that the values above 6706 ** were computed correctly. 6707 */ 6708 #ifdef SQLITE_DEBUG 6709 { 6710 CellInfo info; 6711 pPage->xParseCell(pPage, pCell, &info); 6712 assert( nHeader==(int)(info.pPayload - pCell) ); 6713 assert( info.nKey==pX->nKey ); 6714 assert( *pnSize == info.nSize ); 6715 assert( spaceLeft == info.nLocal ); 6716 } 6717 #endif 6718 6719 /* Write the payload into the local Cell and any extra into overflow pages */ 6720 while( 1 ){ 6721 n = nPayload; 6722 if( n>spaceLeft ) n = spaceLeft; 6723 6724 /* If pToRelease is not zero than pPayload points into the data area 6725 ** of pToRelease. Make sure pToRelease is still writeable. */ 6726 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) ); 6727 6728 /* If pPayload is part of the data area of pPage, then make sure pPage 6729 ** is still writeable */ 6730 assert( pPayload<pPage->aData || pPayload>=&pPage->aData[pBt->pageSize] 6731 || sqlite3PagerIswriteable(pPage->pDbPage) ); 6732 6733 if( nSrc>=n ){ 6734 memcpy(pPayload, pSrc, n); 6735 }else if( nSrc>0 ){ 6736 n = nSrc; 6737 memcpy(pPayload, pSrc, n); 6738 }else{ 6739 memset(pPayload, 0, n); 6740 } 6741 nPayload -= n; 6742 if( nPayload<=0 ) break; 6743 pPayload += n; 6744 pSrc += n; 6745 nSrc -= n; 6746 spaceLeft -= n; 6747 if( spaceLeft==0 ){ 6748 MemPage *pOvfl = 0; 6749 #ifndef SQLITE_OMIT_AUTOVACUUM 6750 Pgno pgnoPtrmap = pgnoOvfl; /* Overflow page pointer-map entry page */ 6751 if( pBt->autoVacuum ){ 6752 do{ 6753 pgnoOvfl++; 6754 } while( 6755 PTRMAP_ISPAGE(pBt, pgnoOvfl) || pgnoOvfl==PENDING_BYTE_PAGE(pBt) 6756 ); 6757 } 6758 #endif 6759 rc = allocateBtreePage(pBt, &pOvfl, &pgnoOvfl, pgnoOvfl, 0); 6760 #ifndef SQLITE_OMIT_AUTOVACUUM 6761 /* If the database supports auto-vacuum, and the second or subsequent 6762 ** overflow page is being allocated, add an entry to the pointer-map 6763 ** for that page now. 6764 ** 6765 ** If this is the first overflow page, then write a partial entry 6766 ** to the pointer-map. If we write nothing to this pointer-map slot, 6767 ** then the optimistic overflow chain processing in clearCell() 6768 ** may misinterpret the uninitialized values and delete the 6769 ** wrong pages from the database. 6770 */ 6771 if( pBt->autoVacuum && rc==SQLITE_OK ){ 6772 u8 eType = (pgnoPtrmap?PTRMAP_OVERFLOW2:PTRMAP_OVERFLOW1); 6773 ptrmapPut(pBt, pgnoOvfl, eType, pgnoPtrmap, &rc); 6774 if( rc ){ 6775 releasePage(pOvfl); 6776 } 6777 } 6778 #endif 6779 if( rc ){ 6780 releasePage(pToRelease); 6781 return rc; 6782 } 6783 6784 /* If pToRelease is not zero than pPrior points into the data area 6785 ** of pToRelease. Make sure pToRelease is still writeable. */ 6786 assert( pToRelease==0 || sqlite3PagerIswriteable(pToRelease->pDbPage) ); 6787 6788 /* If pPrior is part of the data area of pPage, then make sure pPage 6789 ** is still writeable */ 6790 assert( pPrior<pPage->aData || pPrior>=&pPage->aData[pBt->pageSize] 6791 || sqlite3PagerIswriteable(pPage->pDbPage) ); 6792 6793 put4byte(pPrior, pgnoOvfl); 6794 releasePage(pToRelease); 6795 pToRelease = pOvfl; 6796 pPrior = pOvfl->aData; 6797 put4byte(pPrior, 0); 6798 pPayload = &pOvfl->aData[4]; 6799 spaceLeft = pBt->usableSize - 4; 6800 } 6801 } 6802 releasePage(pToRelease); 6803 return SQLITE_OK; 6804 } 6805 6806 /* 6807 ** Remove the i-th cell from pPage. This routine effects pPage only. 6808 ** The cell content is not freed or deallocated. It is assumed that 6809 ** the cell content has been copied someplace else. This routine just 6810 ** removes the reference to the cell from pPage. 6811 ** 6812 ** "sz" must be the number of bytes in the cell. 6813 */ 6814 static void dropCell(MemPage *pPage, int idx, int sz, int *pRC){ 6815 u32 pc; /* Offset to cell content of cell being deleted */ 6816 u8 *data; /* pPage->aData */ 6817 u8 *ptr; /* Used to move bytes around within data[] */ 6818 int rc; /* The return code */ 6819 int hdr; /* Beginning of the header. 0 most pages. 100 page 1 */ 6820 6821 if( *pRC ) return; 6822 assert( idx>=0 && idx<pPage->nCell ); 6823 assert( CORRUPT_DB || sz==cellSize(pPage, idx) ); 6824 assert( sqlite3PagerIswriteable(pPage->pDbPage) ); 6825 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 6826 assert( pPage->nFree>=0 ); 6827 data = pPage->aData; 6828 ptr = &pPage->aCellIdx[2*idx]; 6829 pc = get2byte(ptr); 6830 hdr = pPage->hdrOffset; 6831 testcase( pc==(u32)get2byte(&data[hdr+5]) ); 6832 testcase( pc+sz==pPage->pBt->usableSize ); 6833 if( pc+sz > pPage->pBt->usableSize ){ 6834 *pRC = SQLITE_CORRUPT_BKPT; 6835 return; 6836 } 6837 rc = freeSpace(pPage, pc, sz); 6838 if( rc ){ 6839 *pRC = rc; 6840 return; 6841 } 6842 pPage->nCell--; 6843 if( pPage->nCell==0 ){ 6844 memset(&data[hdr+1], 0, 4); 6845 data[hdr+7] = 0; 6846 put2byte(&data[hdr+5], pPage->pBt->usableSize); 6847 pPage->nFree = pPage->pBt->usableSize - pPage->hdrOffset 6848 - pPage->childPtrSize - 8; 6849 }else{ 6850 memmove(ptr, ptr+2, 2*(pPage->nCell - idx)); 6851 put2byte(&data[hdr+3], pPage->nCell); 6852 pPage->nFree += 2; 6853 } 6854 } 6855 6856 /* 6857 ** Insert a new cell on pPage at cell index "i". pCell points to the 6858 ** content of the cell. 6859 ** 6860 ** If the cell content will fit on the page, then put it there. If it 6861 ** will not fit, then make a copy of the cell content into pTemp if 6862 ** pTemp is not null. Regardless of pTemp, allocate a new entry 6863 ** in pPage->apOvfl[] and make it point to the cell content (either 6864 ** in pTemp or the original pCell) and also record its index. 6865 ** Allocating a new entry in pPage->aCell[] implies that 6866 ** pPage->nOverflow is incremented. 6867 ** 6868 ** *pRC must be SQLITE_OK when this routine is called. 6869 */ 6870 static void insertCell( 6871 MemPage *pPage, /* Page into which we are copying */ 6872 int i, /* New cell becomes the i-th cell of the page */ 6873 u8 *pCell, /* Content of the new cell */ 6874 int sz, /* Bytes of content in pCell */ 6875 u8 *pTemp, /* Temp storage space for pCell, if needed */ 6876 Pgno iChild, /* If non-zero, replace first 4 bytes with this value */ 6877 int *pRC /* Read and write return code from here */ 6878 ){ 6879 int idx = 0; /* Where to write new cell content in data[] */ 6880 int j; /* Loop counter */ 6881 u8 *data; /* The content of the whole page */ 6882 u8 *pIns; /* The point in pPage->aCellIdx[] where no cell inserted */ 6883 6884 assert( *pRC==SQLITE_OK ); 6885 assert( i>=0 && i<=pPage->nCell+pPage->nOverflow ); 6886 assert( MX_CELL(pPage->pBt)<=10921 ); 6887 assert( pPage->nCell<=MX_CELL(pPage->pBt) || CORRUPT_DB ); 6888 assert( pPage->nOverflow<=ArraySize(pPage->apOvfl) ); 6889 assert( ArraySize(pPage->apOvfl)==ArraySize(pPage->aiOvfl) ); 6890 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 6891 assert( sz==pPage->xCellSize(pPage, pCell) || CORRUPT_DB ); 6892 assert( pPage->nFree>=0 ); 6893 if( pPage->nOverflow || sz+2>pPage->nFree ){ 6894 if( pTemp ){ 6895 memcpy(pTemp, pCell, sz); 6896 pCell = pTemp; 6897 } 6898 if( iChild ){ 6899 put4byte(pCell, iChild); 6900 } 6901 j = pPage->nOverflow++; 6902 /* Comparison against ArraySize-1 since we hold back one extra slot 6903 ** as a contingency. In other words, never need more than 3 overflow 6904 ** slots but 4 are allocated, just to be safe. */ 6905 assert( j < ArraySize(pPage->apOvfl)-1 ); 6906 pPage->apOvfl[j] = pCell; 6907 pPage->aiOvfl[j] = (u16)i; 6908 6909 /* When multiple overflows occur, they are always sequential and in 6910 ** sorted order. This invariants arise because multiple overflows can 6911 ** only occur when inserting divider cells into the parent page during 6912 ** balancing, and the dividers are adjacent and sorted. 6913 */ 6914 assert( j==0 || pPage->aiOvfl[j-1]<(u16)i ); /* Overflows in sorted order */ 6915 assert( j==0 || i==pPage->aiOvfl[j-1]+1 ); /* Overflows are sequential */ 6916 }else{ 6917 int rc = sqlite3PagerWrite(pPage->pDbPage); 6918 if( rc!=SQLITE_OK ){ 6919 *pRC = rc; 6920 return; 6921 } 6922 assert( sqlite3PagerIswriteable(pPage->pDbPage) ); 6923 data = pPage->aData; 6924 assert( &data[pPage->cellOffset]==pPage->aCellIdx ); 6925 rc = allocateSpace(pPage, sz, &idx); 6926 if( rc ){ *pRC = rc; return; } 6927 /* The allocateSpace() routine guarantees the following properties 6928 ** if it returns successfully */ 6929 assert( idx >= 0 ); 6930 assert( idx >= pPage->cellOffset+2*pPage->nCell+2 || CORRUPT_DB ); 6931 assert( idx+sz <= (int)pPage->pBt->usableSize ); 6932 pPage->nFree -= (u16)(2 + sz); 6933 if( iChild ){ 6934 /* In a corrupt database where an entry in the cell index section of 6935 ** a btree page has a value of 3 or less, the pCell value might point 6936 ** as many as 4 bytes in front of the start of the aData buffer for 6937 ** the source page. Make sure this does not cause problems by not 6938 ** reading the first 4 bytes */ 6939 memcpy(&data[idx+4], pCell+4, sz-4); 6940 put4byte(&data[idx], iChild); 6941 }else{ 6942 memcpy(&data[idx], pCell, sz); 6943 } 6944 pIns = pPage->aCellIdx + i*2; 6945 memmove(pIns+2, pIns, 2*(pPage->nCell - i)); 6946 put2byte(pIns, idx); 6947 pPage->nCell++; 6948 /* increment the cell count */ 6949 if( (++data[pPage->hdrOffset+4])==0 ) data[pPage->hdrOffset+3]++; 6950 assert( get2byte(&data[pPage->hdrOffset+3])==pPage->nCell || CORRUPT_DB ); 6951 #ifndef SQLITE_OMIT_AUTOVACUUM 6952 if( pPage->pBt->autoVacuum ){ 6953 /* The cell may contain a pointer to an overflow page. If so, write 6954 ** the entry for the overflow page into the pointer map. 6955 */ 6956 ptrmapPutOvflPtr(pPage, pPage, pCell, pRC); 6957 } 6958 #endif 6959 } 6960 } 6961 6962 /* 6963 ** The following parameters determine how many adjacent pages get involved 6964 ** in a balancing operation. NN is the number of neighbors on either side 6965 ** of the page that participate in the balancing operation. NB is the 6966 ** total number of pages that participate, including the target page and 6967 ** NN neighbors on either side. 6968 ** 6969 ** The minimum value of NN is 1 (of course). Increasing NN above 1 6970 ** (to 2 or 3) gives a modest improvement in SELECT and DELETE performance 6971 ** in exchange for a larger degradation in INSERT and UPDATE performance. 6972 ** The value of NN appears to give the best results overall. 6973 ** 6974 ** (Later:) The description above makes it seem as if these values are 6975 ** tunable - as if you could change them and recompile and it would all work. 6976 ** But that is unlikely. NB has been 3 since the inception of SQLite and 6977 ** we have never tested any other value. 6978 */ 6979 #define NN 1 /* Number of neighbors on either side of pPage */ 6980 #define NB 3 /* (NN*2+1): Total pages involved in the balance */ 6981 6982 /* 6983 ** A CellArray object contains a cache of pointers and sizes for a 6984 ** consecutive sequence of cells that might be held on multiple pages. 6985 ** 6986 ** The cells in this array are the divider cell or cells from the pParent 6987 ** page plus up to three child pages. There are a total of nCell cells. 6988 ** 6989 ** pRef is a pointer to one of the pages that contributes cells. This is 6990 ** used to access information such as MemPage.intKey and MemPage.pBt->pageSize 6991 ** which should be common to all pages that contribute cells to this array. 6992 ** 6993 ** apCell[] and szCell[] hold, respectively, pointers to the start of each 6994 ** cell and the size of each cell. Some of the apCell[] pointers might refer 6995 ** to overflow cells. In other words, some apCel[] pointers might not point 6996 ** to content area of the pages. 6997 ** 6998 ** A szCell[] of zero means the size of that cell has not yet been computed. 6999 ** 7000 ** The cells come from as many as four different pages: 7001 ** 7002 ** ----------- 7003 ** | Parent | 7004 ** ----------- 7005 ** / | \ 7006 ** / | \ 7007 ** --------- --------- --------- 7008 ** |Child-1| |Child-2| |Child-3| 7009 ** --------- --------- --------- 7010 ** 7011 ** The order of cells is in the array is for an index btree is: 7012 ** 7013 ** 1. All cells from Child-1 in order 7014 ** 2. The first divider cell from Parent 7015 ** 3. All cells from Child-2 in order 7016 ** 4. The second divider cell from Parent 7017 ** 5. All cells from Child-3 in order 7018 ** 7019 ** For a table-btree (with rowids) the items 2 and 4 are empty because 7020 ** content exists only in leaves and there are no divider cells. 7021 ** 7022 ** For an index btree, the apEnd[] array holds pointer to the end of page 7023 ** for Child-1, the Parent, Child-2, the Parent (again), and Child-3, 7024 ** respectively. The ixNx[] array holds the number of cells contained in 7025 ** each of these 5 stages, and all stages to the left. Hence: 7026 ** 7027 ** ixNx[0] = Number of cells in Child-1. 7028 ** ixNx[1] = Number of cells in Child-1 plus 1 for first divider. 7029 ** ixNx[2] = Number of cells in Child-1 and Child-2 + 1 for 1st divider. 7030 ** ixNx[3] = Number of cells in Child-1 and Child-2 + both divider cells 7031 ** ixNx[4] = Total number of cells. 7032 ** 7033 ** For a table-btree, the concept is similar, except only apEnd[0]..apEnd[2] 7034 ** are used and they point to the leaf pages only, and the ixNx value are: 7035 ** 7036 ** ixNx[0] = Number of cells in Child-1. 7037 ** ixNx[1] = Number of cells in Child-1 and Child-2. 7038 ** ixNx[2] = Total number of cells. 7039 ** 7040 ** Sometimes when deleting, a child page can have zero cells. In those 7041 ** cases, ixNx[] entries with higher indexes, and the corresponding apEnd[] 7042 ** entries, shift down. The end result is that each ixNx[] entry should 7043 ** be larger than the previous 7044 */ 7045 typedef struct CellArray CellArray; 7046 struct CellArray { 7047 int nCell; /* Number of cells in apCell[] */ 7048 MemPage *pRef; /* Reference page */ 7049 u8 **apCell; /* All cells begin balanced */ 7050 u16 *szCell; /* Local size of all cells in apCell[] */ 7051 u8 *apEnd[NB*2]; /* MemPage.aDataEnd values */ 7052 int ixNx[NB*2]; /* Index of at which we move to the next apEnd[] */ 7053 }; 7054 7055 /* 7056 ** Make sure the cell sizes at idx, idx+1, ..., idx+N-1 have been 7057 ** computed. 7058 */ 7059 static void populateCellCache(CellArray *p, int idx, int N){ 7060 assert( idx>=0 && idx+N<=p->nCell ); 7061 while( N>0 ){ 7062 assert( p->apCell[idx]!=0 ); 7063 if( p->szCell[idx]==0 ){ 7064 p->szCell[idx] = p->pRef->xCellSize(p->pRef, p->apCell[idx]); 7065 }else{ 7066 assert( CORRUPT_DB || 7067 p->szCell[idx]==p->pRef->xCellSize(p->pRef, p->apCell[idx]) ); 7068 } 7069 idx++; 7070 N--; 7071 } 7072 } 7073 7074 /* 7075 ** Return the size of the Nth element of the cell array 7076 */ 7077 static SQLITE_NOINLINE u16 computeCellSize(CellArray *p, int N){ 7078 assert( N>=0 && N<p->nCell ); 7079 assert( p->szCell[N]==0 ); 7080 p->szCell[N] = p->pRef->xCellSize(p->pRef, p->apCell[N]); 7081 return p->szCell[N]; 7082 } 7083 static u16 cachedCellSize(CellArray *p, int N){ 7084 assert( N>=0 && N<p->nCell ); 7085 if( p->szCell[N] ) return p->szCell[N]; 7086 return computeCellSize(p, N); 7087 } 7088 7089 /* 7090 ** Array apCell[] contains pointers to nCell b-tree page cells. The 7091 ** szCell[] array contains the size in bytes of each cell. This function 7092 ** replaces the current contents of page pPg with the contents of the cell 7093 ** array. 7094 ** 7095 ** Some of the cells in apCell[] may currently be stored in pPg. This 7096 ** function works around problems caused by this by making a copy of any 7097 ** such cells before overwriting the page data. 7098 ** 7099 ** The MemPage.nFree field is invalidated by this function. It is the 7100 ** responsibility of the caller to set it correctly. 7101 */ 7102 static int rebuildPage( 7103 CellArray *pCArray, /* Content to be added to page pPg */ 7104 int iFirst, /* First cell in pCArray to use */ 7105 int nCell, /* Final number of cells on page */ 7106 MemPage *pPg /* The page to be reconstructed */ 7107 ){ 7108 const int hdr = pPg->hdrOffset; /* Offset of header on pPg */ 7109 u8 * const aData = pPg->aData; /* Pointer to data for pPg */ 7110 const int usableSize = pPg->pBt->usableSize; 7111 u8 * const pEnd = &aData[usableSize]; 7112 int i = iFirst; /* Which cell to copy from pCArray*/ 7113 u32 j; /* Start of cell content area */ 7114 int iEnd = i+nCell; /* Loop terminator */ 7115 u8 *pCellptr = pPg->aCellIdx; 7116 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager); 7117 u8 *pData; 7118 int k; /* Current slot in pCArray->apEnd[] */ 7119 u8 *pSrcEnd; /* Current pCArray->apEnd[k] value */ 7120 7121 assert( i<iEnd ); 7122 j = get2byte(&aData[hdr+5]); 7123 if( NEVER(j>(u32)usableSize) ){ j = 0; } 7124 memcpy(&pTmp[j], &aData[j], usableSize - j); 7125 7126 for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2); k++){} 7127 pSrcEnd = pCArray->apEnd[k]; 7128 7129 pData = pEnd; 7130 while( 1/*exit by break*/ ){ 7131 u8 *pCell = pCArray->apCell[i]; 7132 u16 sz = pCArray->szCell[i]; 7133 assert( sz>0 ); 7134 if( SQLITE_WITHIN(pCell,aData+j,pEnd) ){ 7135 if( ((uptr)(pCell+sz))>(uptr)pEnd ) return SQLITE_CORRUPT_BKPT; 7136 pCell = &pTmp[pCell - aData]; 7137 }else if( (uptr)(pCell+sz)>(uptr)pSrcEnd 7138 && (uptr)(pCell)<(uptr)pSrcEnd 7139 ){ 7140 return SQLITE_CORRUPT_BKPT; 7141 } 7142 7143 pData -= sz; 7144 put2byte(pCellptr, (pData - aData)); 7145 pCellptr += 2; 7146 if( pData < pCellptr ) return SQLITE_CORRUPT_BKPT; 7147 memmove(pData, pCell, sz); 7148 assert( sz==pPg->xCellSize(pPg, pCell) || CORRUPT_DB ); 7149 i++; 7150 if( i>=iEnd ) break; 7151 if( pCArray->ixNx[k]<=i ){ 7152 k++; 7153 pSrcEnd = pCArray->apEnd[k]; 7154 } 7155 } 7156 7157 /* The pPg->nFree field is now set incorrectly. The caller will fix it. */ 7158 pPg->nCell = nCell; 7159 pPg->nOverflow = 0; 7160 7161 put2byte(&aData[hdr+1], 0); 7162 put2byte(&aData[hdr+3], pPg->nCell); 7163 put2byte(&aData[hdr+5], pData - aData); 7164 aData[hdr+7] = 0x00; 7165 return SQLITE_OK; 7166 } 7167 7168 /* 7169 ** The pCArray objects contains pointers to b-tree cells and the cell sizes. 7170 ** This function attempts to add the cells stored in the array to page pPg. 7171 ** If it cannot (because the page needs to be defragmented before the cells 7172 ** will fit), non-zero is returned. Otherwise, if the cells are added 7173 ** successfully, zero is returned. 7174 ** 7175 ** Argument pCellptr points to the first entry in the cell-pointer array 7176 ** (part of page pPg) to populate. After cell apCell[0] is written to the 7177 ** page body, a 16-bit offset is written to pCellptr. And so on, for each 7178 ** cell in the array. It is the responsibility of the caller to ensure 7179 ** that it is safe to overwrite this part of the cell-pointer array. 7180 ** 7181 ** When this function is called, *ppData points to the start of the 7182 ** content area on page pPg. If the size of the content area is extended, 7183 ** *ppData is updated to point to the new start of the content area 7184 ** before returning. 7185 ** 7186 ** Finally, argument pBegin points to the byte immediately following the 7187 ** end of the space required by this page for the cell-pointer area (for 7188 ** all cells - not just those inserted by the current call). If the content 7189 ** area must be extended to before this point in order to accomodate all 7190 ** cells in apCell[], then the cells do not fit and non-zero is returned. 7191 */ 7192 static int pageInsertArray( 7193 MemPage *pPg, /* Page to add cells to */ 7194 u8 *pBegin, /* End of cell-pointer array */ 7195 u8 **ppData, /* IN/OUT: Page content-area pointer */ 7196 u8 *pCellptr, /* Pointer to cell-pointer area */ 7197 int iFirst, /* Index of first cell to add */ 7198 int nCell, /* Number of cells to add to pPg */ 7199 CellArray *pCArray /* Array of cells */ 7200 ){ 7201 int i = iFirst; /* Loop counter - cell index to insert */ 7202 u8 *aData = pPg->aData; /* Complete page */ 7203 u8 *pData = *ppData; /* Content area. A subset of aData[] */ 7204 int iEnd = iFirst + nCell; /* End of loop. One past last cell to ins */ 7205 int k; /* Current slot in pCArray->apEnd[] */ 7206 u8 *pEnd; /* Maximum extent of cell data */ 7207 assert( CORRUPT_DB || pPg->hdrOffset==0 ); /* Never called on page 1 */ 7208 if( iEnd<=iFirst ) return 0; 7209 for(k=0; pCArray->ixNx[k]<=i && ALWAYS(k<NB*2); k++){} 7210 pEnd = pCArray->apEnd[k]; 7211 while( 1 /*Exit by break*/ ){ 7212 int sz, rc; 7213 u8 *pSlot; 7214 assert( pCArray->szCell[i]!=0 ); 7215 sz = pCArray->szCell[i]; 7216 if( (aData[1]==0 && aData[2]==0) || (pSlot = pageFindSlot(pPg,sz,&rc))==0 ){ 7217 if( (pData - pBegin)<sz ) return 1; 7218 pData -= sz; 7219 pSlot = pData; 7220 } 7221 /* pSlot and pCArray->apCell[i] will never overlap on a well-formed 7222 ** database. But they might for a corrupt database. Hence use memmove() 7223 ** since memcpy() sends SIGABORT with overlapping buffers on OpenBSD */ 7224 assert( (pSlot+sz)<=pCArray->apCell[i] 7225 || pSlot>=(pCArray->apCell[i]+sz) 7226 || CORRUPT_DB ); 7227 if( (uptr)(pCArray->apCell[i]+sz)>(uptr)pEnd 7228 && (uptr)(pCArray->apCell[i])<(uptr)pEnd 7229 ){ 7230 assert( CORRUPT_DB ); 7231 (void)SQLITE_CORRUPT_BKPT; 7232 return 1; 7233 } 7234 memmove(pSlot, pCArray->apCell[i], sz); 7235 put2byte(pCellptr, (pSlot - aData)); 7236 pCellptr += 2; 7237 i++; 7238 if( i>=iEnd ) break; 7239 if( pCArray->ixNx[k]<=i ){ 7240 k++; 7241 pEnd = pCArray->apEnd[k]; 7242 } 7243 } 7244 *ppData = pData; 7245 return 0; 7246 } 7247 7248 /* 7249 ** The pCArray object contains pointers to b-tree cells and their sizes. 7250 ** 7251 ** This function adds the space associated with each cell in the array 7252 ** that is currently stored within the body of pPg to the pPg free-list. 7253 ** The cell-pointers and other fields of the page are not updated. 7254 ** 7255 ** This function returns the total number of cells added to the free-list. 7256 */ 7257 static int pageFreeArray( 7258 MemPage *pPg, /* Page to edit */ 7259 int iFirst, /* First cell to delete */ 7260 int nCell, /* Cells to delete */ 7261 CellArray *pCArray /* Array of cells */ 7262 ){ 7263 u8 * const aData = pPg->aData; 7264 u8 * const pEnd = &aData[pPg->pBt->usableSize]; 7265 u8 * const pStart = &aData[pPg->hdrOffset + 8 + pPg->childPtrSize]; 7266 int nRet = 0; 7267 int i; 7268 int iEnd = iFirst + nCell; 7269 u8 *pFree = 0; 7270 int szFree = 0; 7271 7272 for(i=iFirst; i<iEnd; i++){ 7273 u8 *pCell = pCArray->apCell[i]; 7274 if( SQLITE_WITHIN(pCell, pStart, pEnd) ){ 7275 int sz; 7276 /* No need to use cachedCellSize() here. The sizes of all cells that 7277 ** are to be freed have already been computing while deciding which 7278 ** cells need freeing */ 7279 sz = pCArray->szCell[i]; assert( sz>0 ); 7280 if( pFree!=(pCell + sz) ){ 7281 if( pFree ){ 7282 assert( pFree>aData && (pFree - aData)<65536 ); 7283 freeSpace(pPg, (u16)(pFree - aData), szFree); 7284 } 7285 pFree = pCell; 7286 szFree = sz; 7287 if( pFree+sz>pEnd ){ 7288 return 0; 7289 } 7290 }else{ 7291 pFree = pCell; 7292 szFree += sz; 7293 } 7294 nRet++; 7295 } 7296 } 7297 if( pFree ){ 7298 assert( pFree>aData && (pFree - aData)<65536 ); 7299 freeSpace(pPg, (u16)(pFree - aData), szFree); 7300 } 7301 return nRet; 7302 } 7303 7304 /* 7305 ** pCArray contains pointers to and sizes of all cells in the page being 7306 ** balanced. The current page, pPg, has pPg->nCell cells starting with 7307 ** pCArray->apCell[iOld]. After balancing, this page should hold nNew cells 7308 ** starting at apCell[iNew]. 7309 ** 7310 ** This routine makes the necessary adjustments to pPg so that it contains 7311 ** the correct cells after being balanced. 7312 ** 7313 ** The pPg->nFree field is invalid when this function returns. It is the 7314 ** responsibility of the caller to set it correctly. 7315 */ 7316 static int editPage( 7317 MemPage *pPg, /* Edit this page */ 7318 int iOld, /* Index of first cell currently on page */ 7319 int iNew, /* Index of new first cell on page */ 7320 int nNew, /* Final number of cells on page */ 7321 CellArray *pCArray /* Array of cells and sizes */ 7322 ){ 7323 u8 * const aData = pPg->aData; 7324 const int hdr = pPg->hdrOffset; 7325 u8 *pBegin = &pPg->aCellIdx[nNew * 2]; 7326 int nCell = pPg->nCell; /* Cells stored on pPg */ 7327 u8 *pData; 7328 u8 *pCellptr; 7329 int i; 7330 int iOldEnd = iOld + pPg->nCell + pPg->nOverflow; 7331 int iNewEnd = iNew + nNew; 7332 7333 #ifdef SQLITE_DEBUG 7334 u8 *pTmp = sqlite3PagerTempSpace(pPg->pBt->pPager); 7335 memcpy(pTmp, aData, pPg->pBt->usableSize); 7336 #endif 7337 7338 /* Remove cells from the start and end of the page */ 7339 assert( nCell>=0 ); 7340 if( iOld<iNew ){ 7341 int nShift = pageFreeArray(pPg, iOld, iNew-iOld, pCArray); 7342 if( NEVER(nShift>nCell) ) return SQLITE_CORRUPT_BKPT; 7343 memmove(pPg->aCellIdx, &pPg->aCellIdx[nShift*2], nCell*2); 7344 nCell -= nShift; 7345 } 7346 if( iNewEnd < iOldEnd ){ 7347 int nTail = pageFreeArray(pPg, iNewEnd, iOldEnd - iNewEnd, pCArray); 7348 assert( nCell>=nTail ); 7349 nCell -= nTail; 7350 } 7351 7352 pData = &aData[get2byteNotZero(&aData[hdr+5])]; 7353 if( pData<pBegin ) goto editpage_fail; 7354 if( NEVER(pData>pPg->aDataEnd) ) goto editpage_fail; 7355 7356 /* Add cells to the start of the page */ 7357 if( iNew<iOld ){ 7358 int nAdd = MIN(nNew,iOld-iNew); 7359 assert( (iOld-iNew)<nNew || nCell==0 || CORRUPT_DB ); 7360 assert( nAdd>=0 ); 7361 pCellptr = pPg->aCellIdx; 7362 memmove(&pCellptr[nAdd*2], pCellptr, nCell*2); 7363 if( pageInsertArray( 7364 pPg, pBegin, &pData, pCellptr, 7365 iNew, nAdd, pCArray 7366 ) ) goto editpage_fail; 7367 nCell += nAdd; 7368 } 7369 7370 /* Add any overflow cells */ 7371 for(i=0; i<pPg->nOverflow; i++){ 7372 int iCell = (iOld + pPg->aiOvfl[i]) - iNew; 7373 if( iCell>=0 && iCell<nNew ){ 7374 pCellptr = &pPg->aCellIdx[iCell * 2]; 7375 if( nCell>iCell ){ 7376 memmove(&pCellptr[2], pCellptr, (nCell - iCell) * 2); 7377 } 7378 nCell++; 7379 cachedCellSize(pCArray, iCell+iNew); 7380 if( pageInsertArray( 7381 pPg, pBegin, &pData, pCellptr, 7382 iCell+iNew, 1, pCArray 7383 ) ) goto editpage_fail; 7384 } 7385 } 7386 7387 /* Append cells to the end of the page */ 7388 assert( nCell>=0 ); 7389 pCellptr = &pPg->aCellIdx[nCell*2]; 7390 if( pageInsertArray( 7391 pPg, pBegin, &pData, pCellptr, 7392 iNew+nCell, nNew-nCell, pCArray 7393 ) ) goto editpage_fail; 7394 7395 pPg->nCell = nNew; 7396 pPg->nOverflow = 0; 7397 7398 put2byte(&aData[hdr+3], pPg->nCell); 7399 put2byte(&aData[hdr+5], pData - aData); 7400 7401 #ifdef SQLITE_DEBUG 7402 for(i=0; i<nNew && !CORRUPT_DB; i++){ 7403 u8 *pCell = pCArray->apCell[i+iNew]; 7404 int iOff = get2byteAligned(&pPg->aCellIdx[i*2]); 7405 if( SQLITE_WITHIN(pCell, aData, &aData[pPg->pBt->usableSize]) ){ 7406 pCell = &pTmp[pCell - aData]; 7407 } 7408 assert( 0==memcmp(pCell, &aData[iOff], 7409 pCArray->pRef->xCellSize(pCArray->pRef, pCArray->apCell[i+iNew])) ); 7410 } 7411 #endif 7412 7413 return SQLITE_OK; 7414 editpage_fail: 7415 /* Unable to edit this page. Rebuild it from scratch instead. */ 7416 populateCellCache(pCArray, iNew, nNew); 7417 return rebuildPage(pCArray, iNew, nNew, pPg); 7418 } 7419 7420 7421 #ifndef SQLITE_OMIT_QUICKBALANCE 7422 /* 7423 ** This version of balance() handles the common special case where 7424 ** a new entry is being inserted on the extreme right-end of the 7425 ** tree, in other words, when the new entry will become the largest 7426 ** entry in the tree. 7427 ** 7428 ** Instead of trying to balance the 3 right-most leaf pages, just add 7429 ** a new page to the right-hand side and put the one new entry in 7430 ** that page. This leaves the right side of the tree somewhat 7431 ** unbalanced. But odds are that we will be inserting new entries 7432 ** at the end soon afterwards so the nearly empty page will quickly 7433 ** fill up. On average. 7434 ** 7435 ** pPage is the leaf page which is the right-most page in the tree. 7436 ** pParent is its parent. pPage must have a single overflow entry 7437 ** which is also the right-most entry on the page. 7438 ** 7439 ** The pSpace buffer is used to store a temporary copy of the divider 7440 ** cell that will be inserted into pParent. Such a cell consists of a 4 7441 ** byte page number followed by a variable length integer. In other 7442 ** words, at most 13 bytes. Hence the pSpace buffer must be at 7443 ** least 13 bytes in size. 7444 */ 7445 static int balance_quick(MemPage *pParent, MemPage *pPage, u8 *pSpace){ 7446 BtShared *const pBt = pPage->pBt; /* B-Tree Database */ 7447 MemPage *pNew; /* Newly allocated page */ 7448 int rc; /* Return Code */ 7449 Pgno pgnoNew; /* Page number of pNew */ 7450 7451 assert( sqlite3_mutex_held(pPage->pBt->mutex) ); 7452 assert( sqlite3PagerIswriteable(pParent->pDbPage) ); 7453 assert( pPage->nOverflow==1 ); 7454 7455 if( pPage->nCell==0 ) return SQLITE_CORRUPT_BKPT; /* dbfuzz001.test */ 7456 assert( pPage->nFree>=0 ); 7457 assert( pParent->nFree>=0 ); 7458 7459 /* Allocate a new page. This page will become the right-sibling of 7460 ** pPage. Make the parent page writable, so that the new divider cell 7461 ** may be inserted. If both these operations are successful, proceed. 7462 */ 7463 rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0); 7464 7465 if( rc==SQLITE_OK ){ 7466 7467 u8 *pOut = &pSpace[4]; 7468 u8 *pCell = pPage->apOvfl[0]; 7469 u16 szCell = pPage->xCellSize(pPage, pCell); 7470 u8 *pStop; 7471 CellArray b; 7472 7473 assert( sqlite3PagerIswriteable(pNew->pDbPage) ); 7474 assert( CORRUPT_DB || pPage->aData[0]==(PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF) ); 7475 zeroPage(pNew, PTF_INTKEY|PTF_LEAFDATA|PTF_LEAF); 7476 b.nCell = 1; 7477 b.pRef = pPage; 7478 b.apCell = &pCell; 7479 b.szCell = &szCell; 7480 b.apEnd[0] = pPage->aDataEnd; 7481 b.ixNx[0] = 2; 7482 rc = rebuildPage(&b, 0, 1, pNew); 7483 if( NEVER(rc) ){ 7484 releasePage(pNew); 7485 return rc; 7486 } 7487 pNew->nFree = pBt->usableSize - pNew->cellOffset - 2 - szCell; 7488 7489 /* If this is an auto-vacuum database, update the pointer map 7490 ** with entries for the new page, and any pointer from the 7491 ** cell on the page to an overflow page. If either of these 7492 ** operations fails, the return code is set, but the contents 7493 ** of the parent page are still manipulated by thh code below. 7494 ** That is Ok, at this point the parent page is guaranteed to 7495 ** be marked as dirty. Returning an error code will cause a 7496 ** rollback, undoing any changes made to the parent page. 7497 */ 7498 if( ISAUTOVACUUM ){ 7499 ptrmapPut(pBt, pgnoNew, PTRMAP_BTREE, pParent->pgno, &rc); 7500 if( szCell>pNew->minLocal ){ 7501 ptrmapPutOvflPtr(pNew, pNew, pCell, &rc); 7502 } 7503 } 7504 7505 /* Create a divider cell to insert into pParent. The divider cell 7506 ** consists of a 4-byte page number (the page number of pPage) and 7507 ** a variable length key value (which must be the same value as the 7508 ** largest key on pPage). 7509 ** 7510 ** To find the largest key value on pPage, first find the right-most 7511 ** cell on pPage. The first two fields of this cell are the 7512 ** record-length (a variable length integer at most 32-bits in size) 7513 ** and the key value (a variable length integer, may have any value). 7514 ** The first of the while(...) loops below skips over the record-length 7515 ** field. The second while(...) loop copies the key value from the 7516 ** cell on pPage into the pSpace buffer. 7517 */ 7518 pCell = findCell(pPage, pPage->nCell-1); 7519 pStop = &pCell[9]; 7520 while( (*(pCell++)&0x80) && pCell<pStop ); 7521 pStop = &pCell[9]; 7522 while( ((*(pOut++) = *(pCell++))&0x80) && pCell<pStop ); 7523 7524 /* Insert the new divider cell into pParent. */ 7525 if( rc==SQLITE_OK ){ 7526 insertCell(pParent, pParent->nCell, pSpace, (int)(pOut-pSpace), 7527 0, pPage->pgno, &rc); 7528 } 7529 7530 /* Set the right-child pointer of pParent to point to the new page. */ 7531 put4byte(&pParent->aData[pParent->hdrOffset+8], pgnoNew); 7532 7533 /* Release the reference to the new page. */ 7534 releasePage(pNew); 7535 } 7536 7537 return rc; 7538 } 7539 #endif /* SQLITE_OMIT_QUICKBALANCE */ 7540 7541 #if 0 7542 /* 7543 ** This function does not contribute anything to the operation of SQLite. 7544 ** it is sometimes activated temporarily while debugging code responsible 7545 ** for setting pointer-map entries. 7546 */ 7547 static int ptrmapCheckPages(MemPage **apPage, int nPage){ 7548 int i, j; 7549 for(i=0; i<nPage; i++){ 7550 Pgno n; 7551 u8 e; 7552 MemPage *pPage = apPage[i]; 7553 BtShared *pBt = pPage->pBt; 7554 assert( pPage->isInit ); 7555 7556 for(j=0; j<pPage->nCell; j++){ 7557 CellInfo info; 7558 u8 *z; 7559 7560 z = findCell(pPage, j); 7561 pPage->xParseCell(pPage, z, &info); 7562 if( info.nLocal<info.nPayload ){ 7563 Pgno ovfl = get4byte(&z[info.nSize-4]); 7564 ptrmapGet(pBt, ovfl, &e, &n); 7565 assert( n==pPage->pgno && e==PTRMAP_OVERFLOW1 ); 7566 } 7567 if( !pPage->leaf ){ 7568 Pgno child = get4byte(z); 7569 ptrmapGet(pBt, child, &e, &n); 7570 assert( n==pPage->pgno && e==PTRMAP_BTREE ); 7571 } 7572 } 7573 if( !pPage->leaf ){ 7574 Pgno child = get4byte(&pPage->aData[pPage->hdrOffset+8]); 7575 ptrmapGet(pBt, child, &e, &n); 7576 assert( n==pPage->pgno && e==PTRMAP_BTREE ); 7577 } 7578 } 7579 return 1; 7580 } 7581 #endif 7582 7583 /* 7584 ** This function is used to copy the contents of the b-tree node stored 7585 ** on page pFrom to page pTo. If page pFrom was not a leaf page, then 7586 ** the pointer-map entries for each child page are updated so that the 7587 ** parent page stored in the pointer map is page pTo. If pFrom contained 7588 ** any cells with overflow page pointers, then the corresponding pointer 7589 ** map entries are also updated so that the parent page is page pTo. 7590 ** 7591 ** If pFrom is currently carrying any overflow cells (entries in the 7592 ** MemPage.apOvfl[] array), they are not copied to pTo. 7593 ** 7594 ** Before returning, page pTo is reinitialized using btreeInitPage(). 7595 ** 7596 ** The performance of this function is not critical. It is only used by 7597 ** the balance_shallower() and balance_deeper() procedures, neither of 7598 ** which are called often under normal circumstances. 7599 */ 7600 static void copyNodeContent(MemPage *pFrom, MemPage *pTo, int *pRC){ 7601 if( (*pRC)==SQLITE_OK ){ 7602 BtShared * const pBt = pFrom->pBt; 7603 u8 * const aFrom = pFrom->aData; 7604 u8 * const aTo = pTo->aData; 7605 int const iFromHdr = pFrom->hdrOffset; 7606 int const iToHdr = ((pTo->pgno==1) ? 100 : 0); 7607 int rc; 7608 int iData; 7609 7610 7611 assert( pFrom->isInit ); 7612 assert( pFrom->nFree>=iToHdr ); 7613 assert( get2byte(&aFrom[iFromHdr+5]) <= (int)pBt->usableSize ); 7614 7615 /* Copy the b-tree node content from page pFrom to page pTo. */ 7616 iData = get2byte(&aFrom[iFromHdr+5]); 7617 memcpy(&aTo[iData], &aFrom[iData], pBt->usableSize-iData); 7618 memcpy(&aTo[iToHdr], &aFrom[iFromHdr], pFrom->cellOffset + 2*pFrom->nCell); 7619 7620 /* Reinitialize page pTo so that the contents of the MemPage structure 7621 ** match the new data. The initialization of pTo can actually fail under 7622 ** fairly obscure circumstances, even though it is a copy of initialized 7623 ** page pFrom. 7624 */ 7625 pTo->isInit = 0; 7626 rc = btreeInitPage(pTo); 7627 if( rc==SQLITE_OK ) rc = btreeComputeFreeSpace(pTo); 7628 if( rc!=SQLITE_OK ){ 7629 *pRC = rc; 7630 return; 7631 } 7632 7633 /* If this is an auto-vacuum database, update the pointer-map entries 7634 ** for any b-tree or overflow pages that pTo now contains the pointers to. 7635 */ 7636 if( ISAUTOVACUUM ){ 7637 *pRC = setChildPtrmaps(pTo); 7638 } 7639 } 7640 } 7641 7642 /* 7643 ** This routine redistributes cells on the iParentIdx'th child of pParent 7644 ** (hereafter "the page") and up to 2 siblings so that all pages have about the 7645 ** same amount of free space. Usually a single sibling on either side of the 7646 ** page are used in the balancing, though both siblings might come from one 7647 ** side if the page is the first or last child of its parent. If the page 7648 ** has fewer than 2 siblings (something which can only happen if the page 7649 ** is a root page or a child of a root page) then all available siblings 7650 ** participate in the balancing. 7651 ** 7652 ** The number of siblings of the page might be increased or decreased by 7653 ** one or two in an effort to keep pages nearly full but not over full. 7654 ** 7655 ** Note that when this routine is called, some of the cells on the page 7656 ** might not actually be stored in MemPage.aData[]. This can happen 7657 ** if the page is overfull. This routine ensures that all cells allocated 7658 ** to the page and its siblings fit into MemPage.aData[] before returning. 7659 ** 7660 ** In the course of balancing the page and its siblings, cells may be 7661 ** inserted into or removed from the parent page (pParent). Doing so 7662 ** may cause the parent page to become overfull or underfull. If this 7663 ** happens, it is the responsibility of the caller to invoke the correct 7664 ** balancing routine to fix this problem (see the balance() routine). 7665 ** 7666 ** If this routine fails for any reason, it might leave the database 7667 ** in a corrupted state. So if this routine fails, the database should 7668 ** be rolled back. 7669 ** 7670 ** The third argument to this function, aOvflSpace, is a pointer to a 7671 ** buffer big enough to hold one page. If while inserting cells into the parent 7672 ** page (pParent) the parent page becomes overfull, this buffer is 7673 ** used to store the parent's overflow cells. Because this function inserts 7674 ** a maximum of four divider cells into the parent page, and the maximum 7675 ** size of a cell stored within an internal node is always less than 1/4 7676 ** of the page-size, the aOvflSpace[] buffer is guaranteed to be large 7677 ** enough for all overflow cells. 7678 ** 7679 ** If aOvflSpace is set to a null pointer, this function returns 7680 ** SQLITE_NOMEM. 7681 */ 7682 static int balance_nonroot( 7683 MemPage *pParent, /* Parent page of siblings being balanced */ 7684 int iParentIdx, /* Index of "the page" in pParent */ 7685 u8 *aOvflSpace, /* page-size bytes of space for parent ovfl */ 7686 int isRoot, /* True if pParent is a root-page */ 7687 int bBulk /* True if this call is part of a bulk load */ 7688 ){ 7689 BtShared *pBt; /* The whole database */ 7690 int nMaxCells = 0; /* Allocated size of apCell, szCell, aFrom. */ 7691 int nNew = 0; /* Number of pages in apNew[] */ 7692 int nOld; /* Number of pages in apOld[] */ 7693 int i, j, k; /* Loop counters */ 7694 int nxDiv; /* Next divider slot in pParent->aCell[] */ 7695 int rc = SQLITE_OK; /* The return code */ 7696 u16 leafCorrection; /* 4 if pPage is a leaf. 0 if not */ 7697 int leafData; /* True if pPage is a leaf of a LEAFDATA tree */ 7698 int usableSpace; /* Bytes in pPage beyond the header */ 7699 int pageFlags; /* Value of pPage->aData[0] */ 7700 int iSpace1 = 0; /* First unused byte of aSpace1[] */ 7701 int iOvflSpace = 0; /* First unused byte of aOvflSpace[] */ 7702 int szScratch; /* Size of scratch memory requested */ 7703 MemPage *apOld[NB]; /* pPage and up to two siblings */ 7704 MemPage *apNew[NB+2]; /* pPage and up to NB siblings after balancing */ 7705 u8 *pRight; /* Location in parent of right-sibling pointer */ 7706 u8 *apDiv[NB-1]; /* Divider cells in pParent */ 7707 int cntNew[NB+2]; /* Index in b.paCell[] of cell after i-th page */ 7708 int cntOld[NB+2]; /* Old index in b.apCell[] */ 7709 int szNew[NB+2]; /* Combined size of cells placed on i-th page */ 7710 u8 *aSpace1; /* Space for copies of dividers cells */ 7711 Pgno pgno; /* Temp var to store a page number in */ 7712 u8 abDone[NB+2]; /* True after i'th new page is populated */ 7713 Pgno aPgno[NB+2]; /* Page numbers of new pages before shuffling */ 7714 Pgno aPgOrder[NB+2]; /* Copy of aPgno[] used for sorting pages */ 7715 u16 aPgFlags[NB+2]; /* flags field of new pages before shuffling */ 7716 CellArray b; /* Parsed information on cells being balanced */ 7717 7718 memset(abDone, 0, sizeof(abDone)); 7719 memset(&b, 0, sizeof(b)); 7720 pBt = pParent->pBt; 7721 assert( sqlite3_mutex_held(pBt->mutex) ); 7722 assert( sqlite3PagerIswriteable(pParent->pDbPage) ); 7723 7724 /* At this point pParent may have at most one overflow cell. And if 7725 ** this overflow cell is present, it must be the cell with 7726 ** index iParentIdx. This scenario comes about when this function 7727 ** is called (indirectly) from sqlite3BtreeDelete(). 7728 */ 7729 assert( pParent->nOverflow==0 || pParent->nOverflow==1 ); 7730 assert( pParent->nOverflow==0 || pParent->aiOvfl[0]==iParentIdx ); 7731 7732 if( !aOvflSpace ){ 7733 return SQLITE_NOMEM_BKPT; 7734 } 7735 assert( pParent->nFree>=0 ); 7736 7737 /* Find the sibling pages to balance. Also locate the cells in pParent 7738 ** that divide the siblings. An attempt is made to find NN siblings on 7739 ** either side of pPage. More siblings are taken from one side, however, 7740 ** if there are fewer than NN siblings on the other side. If pParent 7741 ** has NB or fewer children then all children of pParent are taken. 7742 ** 7743 ** This loop also drops the divider cells from the parent page. This 7744 ** way, the remainder of the function does not have to deal with any 7745 ** overflow cells in the parent page, since if any existed they will 7746 ** have already been removed. 7747 */ 7748 i = pParent->nOverflow + pParent->nCell; 7749 if( i<2 ){ 7750 nxDiv = 0; 7751 }else{ 7752 assert( bBulk==0 || bBulk==1 ); 7753 if( iParentIdx==0 ){ 7754 nxDiv = 0; 7755 }else if( iParentIdx==i ){ 7756 nxDiv = i-2+bBulk; 7757 }else{ 7758 nxDiv = iParentIdx-1; 7759 } 7760 i = 2-bBulk; 7761 } 7762 nOld = i+1; 7763 if( (i+nxDiv-pParent->nOverflow)==pParent->nCell ){ 7764 pRight = &pParent->aData[pParent->hdrOffset+8]; 7765 }else{ 7766 pRight = findCell(pParent, i+nxDiv-pParent->nOverflow); 7767 } 7768 pgno = get4byte(pRight); 7769 while( 1 ){ 7770 if( rc==SQLITE_OK ){ 7771 rc = getAndInitPage(pBt, pgno, &apOld[i], 0, 0); 7772 } 7773 if( rc ){ 7774 memset(apOld, 0, (i+1)*sizeof(MemPage*)); 7775 goto balance_cleanup; 7776 } 7777 if( apOld[i]->nFree<0 ){ 7778 rc = btreeComputeFreeSpace(apOld[i]); 7779 if( rc ){ 7780 memset(apOld, 0, (i)*sizeof(MemPage*)); 7781 goto balance_cleanup; 7782 } 7783 } 7784 nMaxCells += apOld[i]->nCell + ArraySize(pParent->apOvfl); 7785 if( (i--)==0 ) break; 7786 7787 if( pParent->nOverflow && i+nxDiv==pParent->aiOvfl[0] ){ 7788 apDiv[i] = pParent->apOvfl[0]; 7789 pgno = get4byte(apDiv[i]); 7790 szNew[i] = pParent->xCellSize(pParent, apDiv[i]); 7791 pParent->nOverflow = 0; 7792 }else{ 7793 apDiv[i] = findCell(pParent, i+nxDiv-pParent->nOverflow); 7794 pgno = get4byte(apDiv[i]); 7795 szNew[i] = pParent->xCellSize(pParent, apDiv[i]); 7796 7797 /* Drop the cell from the parent page. apDiv[i] still points to 7798 ** the cell within the parent, even though it has been dropped. 7799 ** This is safe because dropping a cell only overwrites the first 7800 ** four bytes of it, and this function does not need the first 7801 ** four bytes of the divider cell. So the pointer is safe to use 7802 ** later on. 7803 ** 7804 ** But not if we are in secure-delete mode. In secure-delete mode, 7805 ** the dropCell() routine will overwrite the entire cell with zeroes. 7806 ** In this case, temporarily copy the cell into the aOvflSpace[] 7807 ** buffer. It will be copied out again as soon as the aSpace[] buffer 7808 ** is allocated. */ 7809 if( pBt->btsFlags & BTS_FAST_SECURE ){ 7810 int iOff; 7811 7812 /* If the following if() condition is not true, the db is corrupted. 7813 ** The call to dropCell() below will detect this. */ 7814 iOff = SQLITE_PTR_TO_INT(apDiv[i]) - SQLITE_PTR_TO_INT(pParent->aData); 7815 if( (iOff+szNew[i])<=(int)pBt->usableSize ){ 7816 memcpy(&aOvflSpace[iOff], apDiv[i], szNew[i]); 7817 apDiv[i] = &aOvflSpace[apDiv[i]-pParent->aData]; 7818 } 7819 } 7820 dropCell(pParent, i+nxDiv-pParent->nOverflow, szNew[i], &rc); 7821 } 7822 } 7823 7824 /* Make nMaxCells a multiple of 4 in order to preserve 8-byte 7825 ** alignment */ 7826 nMaxCells = (nMaxCells + 3)&~3; 7827 7828 /* 7829 ** Allocate space for memory structures 7830 */ 7831 szScratch = 7832 nMaxCells*sizeof(u8*) /* b.apCell */ 7833 + nMaxCells*sizeof(u16) /* b.szCell */ 7834 + pBt->pageSize; /* aSpace1 */ 7835 7836 assert( szScratch<=7*(int)pBt->pageSize ); 7837 b.apCell = sqlite3StackAllocRaw(0, szScratch ); 7838 if( b.apCell==0 ){ 7839 rc = SQLITE_NOMEM_BKPT; 7840 goto balance_cleanup; 7841 } 7842 b.szCell = (u16*)&b.apCell[nMaxCells]; 7843 aSpace1 = (u8*)&b.szCell[nMaxCells]; 7844 assert( EIGHT_BYTE_ALIGNMENT(aSpace1) ); 7845 7846 /* 7847 ** Load pointers to all cells on sibling pages and the divider cells 7848 ** into the local b.apCell[] array. Make copies of the divider cells 7849 ** into space obtained from aSpace1[]. The divider cells have already 7850 ** been removed from pParent. 7851 ** 7852 ** If the siblings are on leaf pages, then the child pointers of the 7853 ** divider cells are stripped from the cells before they are copied 7854 ** into aSpace1[]. In this way, all cells in b.apCell[] are without 7855 ** child pointers. If siblings are not leaves, then all cell in 7856 ** b.apCell[] include child pointers. Either way, all cells in b.apCell[] 7857 ** are alike. 7858 ** 7859 ** leafCorrection: 4 if pPage is a leaf. 0 if pPage is not a leaf. 7860 ** leafData: 1 if pPage holds key+data and pParent holds only keys. 7861 */ 7862 b.pRef = apOld[0]; 7863 leafCorrection = b.pRef->leaf*4; 7864 leafData = b.pRef->intKeyLeaf; 7865 for(i=0; i<nOld; i++){ 7866 MemPage *pOld = apOld[i]; 7867 int limit = pOld->nCell; 7868 u8 *aData = pOld->aData; 7869 u16 maskPage = pOld->maskPage; 7870 u8 *piCell = aData + pOld->cellOffset; 7871 u8 *piEnd; 7872 VVA_ONLY( int nCellAtStart = b.nCell; ) 7873 7874 /* Verify that all sibling pages are of the same "type" (table-leaf, 7875 ** table-interior, index-leaf, or index-interior). 7876 */ 7877 if( pOld->aData[0]!=apOld[0]->aData[0] ){ 7878 rc = SQLITE_CORRUPT_BKPT; 7879 goto balance_cleanup; 7880 } 7881 7882 /* Load b.apCell[] with pointers to all cells in pOld. If pOld 7883 ** contains overflow cells, include them in the b.apCell[] array 7884 ** in the correct spot. 7885 ** 7886 ** Note that when there are multiple overflow cells, it is always the 7887 ** case that they are sequential and adjacent. This invariant arises 7888 ** because multiple overflows can only occurs when inserting divider 7889 ** cells into a parent on a prior balance, and divider cells are always 7890 ** adjacent and are inserted in order. There is an assert() tagged 7891 ** with "NOTE 1" in the overflow cell insertion loop to prove this 7892 ** invariant. 7893 ** 7894 ** This must be done in advance. Once the balance starts, the cell 7895 ** offset section of the btree page will be overwritten and we will no 7896 ** long be able to find the cells if a pointer to each cell is not saved 7897 ** first. 7898 */ 7899 memset(&b.szCell[b.nCell], 0, sizeof(b.szCell[0])*(limit+pOld->nOverflow)); 7900 if( pOld->nOverflow>0 ){ 7901 if( NEVER(limit<pOld->aiOvfl[0]) ){ 7902 rc = SQLITE_CORRUPT_BKPT; 7903 goto balance_cleanup; 7904 } 7905 limit = pOld->aiOvfl[0]; 7906 for(j=0; j<limit; j++){ 7907 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell)); 7908 piCell += 2; 7909 b.nCell++; 7910 } 7911 for(k=0; k<pOld->nOverflow; k++){ 7912 assert( k==0 || pOld->aiOvfl[k-1]+1==pOld->aiOvfl[k] );/* NOTE 1 */ 7913 b.apCell[b.nCell] = pOld->apOvfl[k]; 7914 b.nCell++; 7915 } 7916 } 7917 piEnd = aData + pOld->cellOffset + 2*pOld->nCell; 7918 while( piCell<piEnd ){ 7919 assert( b.nCell<nMaxCells ); 7920 b.apCell[b.nCell] = aData + (maskPage & get2byteAligned(piCell)); 7921 piCell += 2; 7922 b.nCell++; 7923 } 7924 assert( (b.nCell-nCellAtStart)==(pOld->nCell+pOld->nOverflow) ); 7925 7926 cntOld[i] = b.nCell; 7927 if( i<nOld-1 && !leafData){ 7928 u16 sz = (u16)szNew[i]; 7929 u8 *pTemp; 7930 assert( b.nCell<nMaxCells ); 7931 b.szCell[b.nCell] = sz; 7932 pTemp = &aSpace1[iSpace1]; 7933 iSpace1 += sz; 7934 assert( sz<=pBt->maxLocal+23 ); 7935 assert( iSpace1 <= (int)pBt->pageSize ); 7936 memcpy(pTemp, apDiv[i], sz); 7937 b.apCell[b.nCell] = pTemp+leafCorrection; 7938 assert( leafCorrection==0 || leafCorrection==4 ); 7939 b.szCell[b.nCell] = b.szCell[b.nCell] - leafCorrection; 7940 if( !pOld->leaf ){ 7941 assert( leafCorrection==0 ); 7942 assert( pOld->hdrOffset==0 || CORRUPT_DB ); 7943 /* The right pointer of the child page pOld becomes the left 7944 ** pointer of the divider cell */ 7945 memcpy(b.apCell[b.nCell], &pOld->aData[8], 4); 7946 }else{ 7947 assert( leafCorrection==4 ); 7948 while( b.szCell[b.nCell]<4 ){ 7949 /* Do not allow any cells smaller than 4 bytes. If a smaller cell 7950 ** does exist, pad it with 0x00 bytes. */ 7951 assert( b.szCell[b.nCell]==3 || CORRUPT_DB ); 7952 assert( b.apCell[b.nCell]==&aSpace1[iSpace1-3] || CORRUPT_DB ); 7953 aSpace1[iSpace1++] = 0x00; 7954 b.szCell[b.nCell]++; 7955 } 7956 } 7957 b.nCell++; 7958 } 7959 } 7960 7961 /* 7962 ** Figure out the number of pages needed to hold all b.nCell cells. 7963 ** Store this number in "k". Also compute szNew[] which is the total 7964 ** size of all cells on the i-th page and cntNew[] which is the index 7965 ** in b.apCell[] of the cell that divides page i from page i+1. 7966 ** cntNew[k] should equal b.nCell. 7967 ** 7968 ** Values computed by this block: 7969 ** 7970 ** k: The total number of sibling pages 7971 ** szNew[i]: Spaced used on the i-th sibling page. 7972 ** cntNew[i]: Index in b.apCell[] and b.szCell[] for the first cell to 7973 ** the right of the i-th sibling page. 7974 ** usableSpace: Number of bytes of space available on each sibling. 7975 ** 7976 */ 7977 usableSpace = pBt->usableSize - 12 + leafCorrection; 7978 for(i=k=0; i<nOld; i++, k++){ 7979 MemPage *p = apOld[i]; 7980 b.apEnd[k] = p->aDataEnd; 7981 b.ixNx[k] = cntOld[i]; 7982 if( k && b.ixNx[k]==b.ixNx[k-1] ){ 7983 k--; /* Omit b.ixNx[] entry for child pages with no cells */ 7984 } 7985 if( !leafData ){ 7986 k++; 7987 b.apEnd[k] = pParent->aDataEnd; 7988 b.ixNx[k] = cntOld[i]+1; 7989 } 7990 assert( p->nFree>=0 ); 7991 szNew[i] = usableSpace - p->nFree; 7992 for(j=0; j<p->nOverflow; j++){ 7993 szNew[i] += 2 + p->xCellSize(p, p->apOvfl[j]); 7994 } 7995 cntNew[i] = cntOld[i]; 7996 } 7997 k = nOld; 7998 for(i=0; i<k; i++){ 7999 int sz; 8000 while( szNew[i]>usableSpace ){ 8001 if( i+1>=k ){ 8002 k = i+2; 8003 if( k>NB+2 ){ rc = SQLITE_CORRUPT_BKPT; goto balance_cleanup; } 8004 szNew[k-1] = 0; 8005 cntNew[k-1] = b.nCell; 8006 } 8007 sz = 2 + cachedCellSize(&b, cntNew[i]-1); 8008 szNew[i] -= sz; 8009 if( !leafData ){ 8010 if( cntNew[i]<b.nCell ){ 8011 sz = 2 + cachedCellSize(&b, cntNew[i]); 8012 }else{ 8013 sz = 0; 8014 } 8015 } 8016 szNew[i+1] += sz; 8017 cntNew[i]--; 8018 } 8019 while( cntNew[i]<b.nCell ){ 8020 sz = 2 + cachedCellSize(&b, cntNew[i]); 8021 if( szNew[i]+sz>usableSpace ) break; 8022 szNew[i] += sz; 8023 cntNew[i]++; 8024 if( !leafData ){ 8025 if( cntNew[i]<b.nCell ){ 8026 sz = 2 + cachedCellSize(&b, cntNew[i]); 8027 }else{ 8028 sz = 0; 8029 } 8030 } 8031 szNew[i+1] -= sz; 8032 } 8033 if( cntNew[i]>=b.nCell ){ 8034 k = i+1; 8035 }else if( cntNew[i] <= (i>0 ? cntNew[i-1] : 0) ){ 8036 rc = SQLITE_CORRUPT_BKPT; 8037 goto balance_cleanup; 8038 } 8039 } 8040 8041 /* 8042 ** The packing computed by the previous block is biased toward the siblings 8043 ** on the left side (siblings with smaller keys). The left siblings are 8044 ** always nearly full, while the right-most sibling might be nearly empty. 8045 ** The next block of code attempts to adjust the packing of siblings to 8046 ** get a better balance. 8047 ** 8048 ** This adjustment is more than an optimization. The packing above might 8049 ** be so out of balance as to be illegal. For example, the right-most 8050 ** sibling might be completely empty. This adjustment is not optional. 8051 */ 8052 for(i=k-1; i>0; i--){ 8053 int szRight = szNew[i]; /* Size of sibling on the right */ 8054 int szLeft = szNew[i-1]; /* Size of sibling on the left */ 8055 int r; /* Index of right-most cell in left sibling */ 8056 int d; /* Index of first cell to the left of right sibling */ 8057 8058 r = cntNew[i-1] - 1; 8059 d = r + 1 - leafData; 8060 (void)cachedCellSize(&b, d); 8061 do{ 8062 assert( d<nMaxCells ); 8063 assert( r<nMaxCells ); 8064 (void)cachedCellSize(&b, r); 8065 if( szRight!=0 8066 && (bBulk || szRight+b.szCell[d]+2 > szLeft-(b.szCell[r]+(i==k-1?0:2)))){ 8067 break; 8068 } 8069 szRight += b.szCell[d] + 2; 8070 szLeft -= b.szCell[r] + 2; 8071 cntNew[i-1] = r; 8072 r--; 8073 d--; 8074 }while( r>=0 ); 8075 szNew[i] = szRight; 8076 szNew[i-1] = szLeft; 8077 if( cntNew[i-1] <= (i>1 ? cntNew[i-2] : 0) ){ 8078 rc = SQLITE_CORRUPT_BKPT; 8079 goto balance_cleanup; 8080 } 8081 } 8082 8083 /* Sanity check: For a non-corrupt database file one of the follwing 8084 ** must be true: 8085 ** (1) We found one or more cells (cntNew[0])>0), or 8086 ** (2) pPage is a virtual root page. A virtual root page is when 8087 ** the real root page is page 1 and we are the only child of 8088 ** that page. 8089 */ 8090 assert( cntNew[0]>0 || (pParent->pgno==1 && pParent->nCell==0) || CORRUPT_DB); 8091 TRACE(("BALANCE: old: %d(nc=%d) %d(nc=%d) %d(nc=%d)\n", 8092 apOld[0]->pgno, apOld[0]->nCell, 8093 nOld>=2 ? apOld[1]->pgno : 0, nOld>=2 ? apOld[1]->nCell : 0, 8094 nOld>=3 ? apOld[2]->pgno : 0, nOld>=3 ? apOld[2]->nCell : 0 8095 )); 8096 8097 /* 8098 ** Allocate k new pages. Reuse old pages where possible. 8099 */ 8100 pageFlags = apOld[0]->aData[0]; 8101 for(i=0; i<k; i++){ 8102 MemPage *pNew; 8103 if( i<nOld ){ 8104 pNew = apNew[i] = apOld[i]; 8105 apOld[i] = 0; 8106 rc = sqlite3PagerWrite(pNew->pDbPage); 8107 nNew++; 8108 if( sqlite3PagerPageRefcount(pNew->pDbPage)!=1+(i==(iParentIdx-nxDiv)) 8109 && rc==SQLITE_OK 8110 ){ 8111 rc = SQLITE_CORRUPT_BKPT; 8112 } 8113 if( rc ) goto balance_cleanup; 8114 }else{ 8115 assert( i>0 ); 8116 rc = allocateBtreePage(pBt, &pNew, &pgno, (bBulk ? 1 : pgno), 0); 8117 if( rc ) goto balance_cleanup; 8118 zeroPage(pNew, pageFlags); 8119 apNew[i] = pNew; 8120 nNew++; 8121 cntOld[i] = b.nCell; 8122 8123 /* Set the pointer-map entry for the new sibling page. */ 8124 if( ISAUTOVACUUM ){ 8125 ptrmapPut(pBt, pNew->pgno, PTRMAP_BTREE, pParent->pgno, &rc); 8126 if( rc!=SQLITE_OK ){ 8127 goto balance_cleanup; 8128 } 8129 } 8130 } 8131 } 8132 8133 /* 8134 ** Reassign page numbers so that the new pages are in ascending order. 8135 ** This helps to keep entries in the disk file in order so that a scan 8136 ** of the table is closer to a linear scan through the file. That in turn 8137 ** helps the operating system to deliver pages from the disk more rapidly. 8138 ** 8139 ** An O(n^2) insertion sort algorithm is used, but since n is never more 8140 ** than (NB+2) (a small constant), that should not be a problem. 8141 ** 8142 ** When NB==3, this one optimization makes the database about 25% faster 8143 ** for large insertions and deletions. 8144 */ 8145 for(i=0; i<nNew; i++){ 8146 aPgOrder[i] = aPgno[i] = apNew[i]->pgno; 8147 aPgFlags[i] = apNew[i]->pDbPage->flags; 8148 for(j=0; j<i; j++){ 8149 if( NEVER(aPgno[j]==aPgno[i]) ){ 8150 /* This branch is taken if the set of sibling pages somehow contains 8151 ** duplicate entries. This can happen if the database is corrupt. 8152 ** It would be simpler to detect this as part of the loop below, but 8153 ** we do the detection here in order to avoid populating the pager 8154 ** cache with two separate objects associated with the same 8155 ** page number. */ 8156 assert( CORRUPT_DB ); 8157 rc = SQLITE_CORRUPT_BKPT; 8158 goto balance_cleanup; 8159 } 8160 } 8161 } 8162 for(i=0; i<nNew; i++){ 8163 int iBest = 0; /* aPgno[] index of page number to use */ 8164 for(j=1; j<nNew; j++){ 8165 if( aPgOrder[j]<aPgOrder[iBest] ) iBest = j; 8166 } 8167 pgno = aPgOrder[iBest]; 8168 aPgOrder[iBest] = 0xffffffff; 8169 if( iBest!=i ){ 8170 if( iBest>i ){ 8171 sqlite3PagerRekey(apNew[iBest]->pDbPage, pBt->nPage+iBest+1, 0); 8172 } 8173 sqlite3PagerRekey(apNew[i]->pDbPage, pgno, aPgFlags[iBest]); 8174 apNew[i]->pgno = pgno; 8175 } 8176 } 8177 8178 TRACE(("BALANCE: new: %d(%d nc=%d) %d(%d nc=%d) %d(%d nc=%d) " 8179 "%d(%d nc=%d) %d(%d nc=%d)\n", 8180 apNew[0]->pgno, szNew[0], cntNew[0], 8181 nNew>=2 ? apNew[1]->pgno : 0, nNew>=2 ? szNew[1] : 0, 8182 nNew>=2 ? cntNew[1] - cntNew[0] - !leafData : 0, 8183 nNew>=3 ? apNew[2]->pgno : 0, nNew>=3 ? szNew[2] : 0, 8184 nNew>=3 ? cntNew[2] - cntNew[1] - !leafData : 0, 8185 nNew>=4 ? apNew[3]->pgno : 0, nNew>=4 ? szNew[3] : 0, 8186 nNew>=4 ? cntNew[3] - cntNew[2] - !leafData : 0, 8187 nNew>=5 ? apNew[4]->pgno : 0, nNew>=5 ? szNew[4] : 0, 8188 nNew>=5 ? cntNew[4] - cntNew[3] - !leafData : 0 8189 )); 8190 8191 assert( sqlite3PagerIswriteable(pParent->pDbPage) ); 8192 assert( nNew>=1 && nNew<=ArraySize(apNew) ); 8193 assert( apNew[nNew-1]!=0 ); 8194 put4byte(pRight, apNew[nNew-1]->pgno); 8195 8196 /* If the sibling pages are not leaves, ensure that the right-child pointer 8197 ** of the right-most new sibling page is set to the value that was 8198 ** originally in the same field of the right-most old sibling page. */ 8199 if( (pageFlags & PTF_LEAF)==0 && nOld!=nNew ){ 8200 MemPage *pOld = (nNew>nOld ? apNew : apOld)[nOld-1]; 8201 memcpy(&apNew[nNew-1]->aData[8], &pOld->aData[8], 4); 8202 } 8203 8204 /* Make any required updates to pointer map entries associated with 8205 ** cells stored on sibling pages following the balance operation. Pointer 8206 ** map entries associated with divider cells are set by the insertCell() 8207 ** routine. The associated pointer map entries are: 8208 ** 8209 ** a) if the cell contains a reference to an overflow chain, the 8210 ** entry associated with the first page in the overflow chain, and 8211 ** 8212 ** b) if the sibling pages are not leaves, the child page associated 8213 ** with the cell. 8214 ** 8215 ** If the sibling pages are not leaves, then the pointer map entry 8216 ** associated with the right-child of each sibling may also need to be 8217 ** updated. This happens below, after the sibling pages have been 8218 ** populated, not here. 8219 */ 8220 if( ISAUTOVACUUM ){ 8221 MemPage *pOld; 8222 MemPage *pNew = pOld = apNew[0]; 8223 int cntOldNext = pNew->nCell + pNew->nOverflow; 8224 int iNew = 0; 8225 int iOld = 0; 8226 8227 for(i=0; i<b.nCell; i++){ 8228 u8 *pCell = b.apCell[i]; 8229 while( i==cntOldNext ){ 8230 iOld++; 8231 assert( iOld<nNew || iOld<nOld ); 8232 assert( iOld>=0 && iOld<NB ); 8233 pOld = iOld<nNew ? apNew[iOld] : apOld[iOld]; 8234 cntOldNext += pOld->nCell + pOld->nOverflow + !leafData; 8235 } 8236 if( i==cntNew[iNew] ){ 8237 pNew = apNew[++iNew]; 8238 if( !leafData ) continue; 8239 } 8240 8241 /* Cell pCell is destined for new sibling page pNew. Originally, it 8242 ** was either part of sibling page iOld (possibly an overflow cell), 8243 ** or else the divider cell to the left of sibling page iOld. So, 8244 ** if sibling page iOld had the same page number as pNew, and if 8245 ** pCell really was a part of sibling page iOld (not a divider or 8246 ** overflow cell), we can skip updating the pointer map entries. */ 8247 if( iOld>=nNew 8248 || pNew->pgno!=aPgno[iOld] 8249 || !SQLITE_WITHIN(pCell,pOld->aData,pOld->aDataEnd) 8250 ){ 8251 if( !leafCorrection ){ 8252 ptrmapPut(pBt, get4byte(pCell), PTRMAP_BTREE, pNew->pgno, &rc); 8253 } 8254 if( cachedCellSize(&b,i)>pNew->minLocal ){ 8255 ptrmapPutOvflPtr(pNew, pOld, pCell, &rc); 8256 } 8257 if( rc ) goto balance_cleanup; 8258 } 8259 } 8260 } 8261 8262 /* Insert new divider cells into pParent. */ 8263 for(i=0; i<nNew-1; i++){ 8264 u8 *pCell; 8265 u8 *pTemp; 8266 int sz; 8267 u8 *pSrcEnd; 8268 MemPage *pNew = apNew[i]; 8269 j = cntNew[i]; 8270 8271 assert( j<nMaxCells ); 8272 assert( b.apCell[j]!=0 ); 8273 pCell = b.apCell[j]; 8274 sz = b.szCell[j] + leafCorrection; 8275 pTemp = &aOvflSpace[iOvflSpace]; 8276 if( !pNew->leaf ){ 8277 memcpy(&pNew->aData[8], pCell, 4); 8278 }else if( leafData ){ 8279 /* If the tree is a leaf-data tree, and the siblings are leaves, 8280 ** then there is no divider cell in b.apCell[]. Instead, the divider 8281 ** cell consists of the integer key for the right-most cell of 8282 ** the sibling-page assembled above only. 8283 */ 8284 CellInfo info; 8285 j--; 8286 pNew->xParseCell(pNew, b.apCell[j], &info); 8287 pCell = pTemp; 8288 sz = 4 + putVarint(&pCell[4], info.nKey); 8289 pTemp = 0; 8290 }else{ 8291 pCell -= 4; 8292 /* Obscure case for non-leaf-data trees: If the cell at pCell was 8293 ** previously stored on a leaf node, and its reported size was 4 8294 ** bytes, then it may actually be smaller than this 8295 ** (see btreeParseCellPtr(), 4 bytes is the minimum size of 8296 ** any cell). But it is important to pass the correct size to 8297 ** insertCell(), so reparse the cell now. 8298 ** 8299 ** This can only happen for b-trees used to evaluate "IN (SELECT ...)" 8300 ** and WITHOUT ROWID tables with exactly one column which is the 8301 ** primary key. 8302 */ 8303 if( b.szCell[j]==4 ){ 8304 assert(leafCorrection==4); 8305 sz = pParent->xCellSize(pParent, pCell); 8306 } 8307 } 8308 iOvflSpace += sz; 8309 assert( sz<=pBt->maxLocal+23 ); 8310 assert( iOvflSpace <= (int)pBt->pageSize ); 8311 for(k=0; b.ixNx[k]<=i && ALWAYS(k<NB*2); k++){} 8312 pSrcEnd = b.apEnd[k]; 8313 if( SQLITE_WITHIN(pSrcEnd, pCell, pCell+sz) ){ 8314 rc = SQLITE_CORRUPT_BKPT; 8315 goto balance_cleanup; 8316 } 8317 insertCell(pParent, nxDiv+i, pCell, sz, pTemp, pNew->pgno, &rc); 8318 if( rc!=SQLITE_OK ) goto balance_cleanup; 8319 assert( sqlite3PagerIswriteable(pParent->pDbPage) ); 8320 } 8321 8322 /* Now update the actual sibling pages. The order in which they are updated 8323 ** is important, as this code needs to avoid disrupting any page from which 8324 ** cells may still to be read. In practice, this means: 8325 ** 8326 ** (1) If cells are moving left (from apNew[iPg] to apNew[iPg-1]) 8327 ** then it is not safe to update page apNew[iPg] until after 8328 ** the left-hand sibling apNew[iPg-1] has been updated. 8329 ** 8330 ** (2) If cells are moving right (from apNew[iPg] to apNew[iPg+1]) 8331 ** then it is not safe to update page apNew[iPg] until after 8332 ** the right-hand sibling apNew[iPg+1] has been updated. 8333 ** 8334 ** If neither of the above apply, the page is safe to update. 8335 ** 8336 ** The iPg value in the following loop starts at nNew-1 goes down 8337 ** to 0, then back up to nNew-1 again, thus making two passes over 8338 ** the pages. On the initial downward pass, only condition (1) above 8339 ** needs to be tested because (2) will always be true from the previous 8340 ** step. On the upward pass, both conditions are always true, so the 8341 ** upwards pass simply processes pages that were missed on the downward 8342 ** pass. 8343 */ 8344 for(i=1-nNew; i<nNew; i++){ 8345 int iPg = i<0 ? -i : i; 8346 assert( iPg>=0 && iPg<nNew ); 8347 if( abDone[iPg] ) continue; /* Skip pages already processed */ 8348 if( i>=0 /* On the upwards pass, or... */ 8349 || cntOld[iPg-1]>=cntNew[iPg-1] /* Condition (1) is true */ 8350 ){ 8351 int iNew; 8352 int iOld; 8353 int nNewCell; 8354 8355 /* Verify condition (1): If cells are moving left, update iPg 8356 ** only after iPg-1 has already been updated. */ 8357 assert( iPg==0 || cntOld[iPg-1]>=cntNew[iPg-1] || abDone[iPg-1] ); 8358 8359 /* Verify condition (2): If cells are moving right, update iPg 8360 ** only after iPg+1 has already been updated. */ 8361 assert( cntNew[iPg]>=cntOld[iPg] || abDone[iPg+1] ); 8362 8363 if( iPg==0 ){ 8364 iNew = iOld = 0; 8365 nNewCell = cntNew[0]; 8366 }else{ 8367 iOld = iPg<nOld ? (cntOld[iPg-1] + !leafData) : b.nCell; 8368 iNew = cntNew[iPg-1] + !leafData; 8369 nNewCell = cntNew[iPg] - iNew; 8370 } 8371 8372 rc = editPage(apNew[iPg], iOld, iNew, nNewCell, &b); 8373 if( rc ) goto balance_cleanup; 8374 abDone[iPg]++; 8375 apNew[iPg]->nFree = usableSpace-szNew[iPg]; 8376 assert( apNew[iPg]->nOverflow==0 ); 8377 assert( apNew[iPg]->nCell==nNewCell ); 8378 } 8379 } 8380 8381 /* All pages have been processed exactly once */ 8382 assert( memcmp(abDone, "\01\01\01\01\01", nNew)==0 ); 8383 8384 assert( nOld>0 ); 8385 assert( nNew>0 ); 8386 8387 if( isRoot && pParent->nCell==0 && pParent->hdrOffset<=apNew[0]->nFree ){ 8388 /* The root page of the b-tree now contains no cells. The only sibling 8389 ** page is the right-child of the parent. Copy the contents of the 8390 ** child page into the parent, decreasing the overall height of the 8391 ** b-tree structure by one. This is described as the "balance-shallower" 8392 ** sub-algorithm in some documentation. 8393 ** 8394 ** If this is an auto-vacuum database, the call to copyNodeContent() 8395 ** sets all pointer-map entries corresponding to database image pages 8396 ** for which the pointer is stored within the content being copied. 8397 ** 8398 ** It is critical that the child page be defragmented before being 8399 ** copied into the parent, because if the parent is page 1 then it will 8400 ** by smaller than the child due to the database header, and so all the 8401 ** free space needs to be up front. 8402 */ 8403 assert( nNew==1 || CORRUPT_DB ); 8404 rc = defragmentPage(apNew[0], -1); 8405 testcase( rc!=SQLITE_OK ); 8406 assert( apNew[0]->nFree == 8407 (get2byteNotZero(&apNew[0]->aData[5]) - apNew[0]->cellOffset 8408 - apNew[0]->nCell*2) 8409 || rc!=SQLITE_OK 8410 ); 8411 copyNodeContent(apNew[0], pParent, &rc); 8412 freePage(apNew[0], &rc); 8413 }else if( ISAUTOVACUUM && !leafCorrection ){ 8414 /* Fix the pointer map entries associated with the right-child of each 8415 ** sibling page. All other pointer map entries have already been taken 8416 ** care of. */ 8417 for(i=0; i<nNew; i++){ 8418 u32 key = get4byte(&apNew[i]->aData[8]); 8419 ptrmapPut(pBt, key, PTRMAP_BTREE, apNew[i]->pgno, &rc); 8420 } 8421 } 8422 8423 assert( pParent->isInit ); 8424 TRACE(("BALANCE: finished: old=%d new=%d cells=%d\n", 8425 nOld, nNew, b.nCell)); 8426 8427 /* Free any old pages that were not reused as new pages. 8428 */ 8429 for(i=nNew; i<nOld; i++){ 8430 freePage(apOld[i], &rc); 8431 } 8432 8433 #if 0 8434 if( ISAUTOVACUUM && rc==SQLITE_OK && apNew[0]->isInit ){ 8435 /* The ptrmapCheckPages() contains assert() statements that verify that 8436 ** all pointer map pages are set correctly. This is helpful while 8437 ** debugging. This is usually disabled because a corrupt database may 8438 ** cause an assert() statement to fail. */ 8439 ptrmapCheckPages(apNew, nNew); 8440 ptrmapCheckPages(&pParent, 1); 8441 } 8442 #endif 8443 8444 /* 8445 ** Cleanup before returning. 8446 */ 8447 balance_cleanup: 8448 sqlite3StackFree(0, b.apCell); 8449 for(i=0; i<nOld; i++){ 8450 releasePage(apOld[i]); 8451 } 8452 for(i=0; i<nNew; i++){ 8453 releasePage(apNew[i]); 8454 } 8455 8456 return rc; 8457 } 8458 8459 8460 /* 8461 ** This function is called when the root page of a b-tree structure is 8462 ** overfull (has one or more overflow pages). 8463 ** 8464 ** A new child page is allocated and the contents of the current root 8465 ** page, including overflow cells, are copied into the child. The root 8466 ** page is then overwritten to make it an empty page with the right-child 8467 ** pointer pointing to the new page. 8468 ** 8469 ** Before returning, all pointer-map entries corresponding to pages 8470 ** that the new child-page now contains pointers to are updated. The 8471 ** entry corresponding to the new right-child pointer of the root 8472 ** page is also updated. 8473 ** 8474 ** If successful, *ppChild is set to contain a reference to the child 8475 ** page and SQLITE_OK is returned. In this case the caller is required 8476 ** to call releasePage() on *ppChild exactly once. If an error occurs, 8477 ** an error code is returned and *ppChild is set to 0. 8478 */ 8479 static int balance_deeper(MemPage *pRoot, MemPage **ppChild){ 8480 int rc; /* Return value from subprocedures */ 8481 MemPage *pChild = 0; /* Pointer to a new child page */ 8482 Pgno pgnoChild = 0; /* Page number of the new child page */ 8483 BtShared *pBt = pRoot->pBt; /* The BTree */ 8484 8485 assert( pRoot->nOverflow>0 ); 8486 assert( sqlite3_mutex_held(pBt->mutex) ); 8487 8488 /* Make pRoot, the root page of the b-tree, writable. Allocate a new 8489 ** page that will become the new right-child of pPage. Copy the contents 8490 ** of the node stored on pRoot into the new child page. 8491 */ 8492 rc = sqlite3PagerWrite(pRoot->pDbPage); 8493 if( rc==SQLITE_OK ){ 8494 rc = allocateBtreePage(pBt,&pChild,&pgnoChild,pRoot->pgno,0); 8495 copyNodeContent(pRoot, pChild, &rc); 8496 if( ISAUTOVACUUM ){ 8497 ptrmapPut(pBt, pgnoChild, PTRMAP_BTREE, pRoot->pgno, &rc); 8498 } 8499 } 8500 if( rc ){ 8501 *ppChild = 0; 8502 releasePage(pChild); 8503 return rc; 8504 } 8505 assert( sqlite3PagerIswriteable(pChild->pDbPage) ); 8506 assert( sqlite3PagerIswriteable(pRoot->pDbPage) ); 8507 assert( pChild->nCell==pRoot->nCell || CORRUPT_DB ); 8508 8509 TRACE(("BALANCE: copy root %d into %d\n", pRoot->pgno, pChild->pgno)); 8510 8511 /* Copy the overflow cells from pRoot to pChild */ 8512 memcpy(pChild->aiOvfl, pRoot->aiOvfl, 8513 pRoot->nOverflow*sizeof(pRoot->aiOvfl[0])); 8514 memcpy(pChild->apOvfl, pRoot->apOvfl, 8515 pRoot->nOverflow*sizeof(pRoot->apOvfl[0])); 8516 pChild->nOverflow = pRoot->nOverflow; 8517 8518 /* Zero the contents of pRoot. Then install pChild as the right-child. */ 8519 zeroPage(pRoot, pChild->aData[0] & ~PTF_LEAF); 8520 put4byte(&pRoot->aData[pRoot->hdrOffset+8], pgnoChild); 8521 8522 *ppChild = pChild; 8523 return SQLITE_OK; 8524 } 8525 8526 /* 8527 ** Return SQLITE_CORRUPT if any cursor other than pCur is currently valid 8528 ** on the same B-tree as pCur. 8529 ** 8530 ** This can occur if a database is corrupt with two or more SQL tables 8531 ** pointing to the same b-tree. If an insert occurs on one SQL table 8532 ** and causes a BEFORE TRIGGER to do a secondary insert on the other SQL 8533 ** table linked to the same b-tree. If the secondary insert causes a 8534 ** rebalance, that can change content out from under the cursor on the 8535 ** first SQL table, violating invariants on the first insert. 8536 */ 8537 static int anotherValidCursor(BtCursor *pCur){ 8538 BtCursor *pOther; 8539 for(pOther=pCur->pBt->pCursor; pOther; pOther=pOther->pNext){ 8540 if( pOther!=pCur 8541 && pOther->eState==CURSOR_VALID 8542 && pOther->pPage==pCur->pPage 8543 ){ 8544 return SQLITE_CORRUPT_BKPT; 8545 } 8546 } 8547 return SQLITE_OK; 8548 } 8549 8550 /* 8551 ** The page that pCur currently points to has just been modified in 8552 ** some way. This function figures out if this modification means the 8553 ** tree needs to be balanced, and if so calls the appropriate balancing 8554 ** routine. Balancing routines are: 8555 ** 8556 ** balance_quick() 8557 ** balance_deeper() 8558 ** balance_nonroot() 8559 */ 8560 static int balance(BtCursor *pCur){ 8561 int rc = SQLITE_OK; 8562 const int nMin = pCur->pBt->usableSize * 2 / 3; 8563 u8 aBalanceQuickSpace[13]; 8564 u8 *pFree = 0; 8565 8566 VVA_ONLY( int balance_quick_called = 0 ); 8567 VVA_ONLY( int balance_deeper_called = 0 ); 8568 8569 do { 8570 int iPage; 8571 MemPage *pPage = pCur->pPage; 8572 8573 if( NEVER(pPage->nFree<0) && btreeComputeFreeSpace(pPage) ) break; 8574 if( pPage->nOverflow==0 && pPage->nFree<=nMin ){ 8575 break; 8576 }else if( (iPage = pCur->iPage)==0 ){ 8577 if( pPage->nOverflow && (rc = anotherValidCursor(pCur))==SQLITE_OK ){ 8578 /* The root page of the b-tree is overfull. In this case call the 8579 ** balance_deeper() function to create a new child for the root-page 8580 ** and copy the current contents of the root-page to it. The 8581 ** next iteration of the do-loop will balance the child page. 8582 */ 8583 assert( balance_deeper_called==0 ); 8584 VVA_ONLY( balance_deeper_called++ ); 8585 rc = balance_deeper(pPage, &pCur->apPage[1]); 8586 if( rc==SQLITE_OK ){ 8587 pCur->iPage = 1; 8588 pCur->ix = 0; 8589 pCur->aiIdx[0] = 0; 8590 pCur->apPage[0] = pPage; 8591 pCur->pPage = pCur->apPage[1]; 8592 assert( pCur->pPage->nOverflow ); 8593 } 8594 }else{ 8595 break; 8596 } 8597 }else{ 8598 MemPage * const pParent = pCur->apPage[iPage-1]; 8599 int const iIdx = pCur->aiIdx[iPage-1]; 8600 8601 rc = sqlite3PagerWrite(pParent->pDbPage); 8602 if( rc==SQLITE_OK && pParent->nFree<0 ){ 8603 rc = btreeComputeFreeSpace(pParent); 8604 } 8605 if( rc==SQLITE_OK ){ 8606 #ifndef SQLITE_OMIT_QUICKBALANCE 8607 if( pPage->intKeyLeaf 8608 && pPage->nOverflow==1 8609 && pPage->aiOvfl[0]==pPage->nCell 8610 && pParent->pgno!=1 8611 && pParent->nCell==iIdx 8612 ){ 8613 /* Call balance_quick() to create a new sibling of pPage on which 8614 ** to store the overflow cell. balance_quick() inserts a new cell 8615 ** into pParent, which may cause pParent overflow. If this 8616 ** happens, the next iteration of the do-loop will balance pParent 8617 ** use either balance_nonroot() or balance_deeper(). Until this 8618 ** happens, the overflow cell is stored in the aBalanceQuickSpace[] 8619 ** buffer. 8620 ** 8621 ** The purpose of the following assert() is to check that only a 8622 ** single call to balance_quick() is made for each call to this 8623 ** function. If this were not verified, a subtle bug involving reuse 8624 ** of the aBalanceQuickSpace[] might sneak in. 8625 */ 8626 assert( balance_quick_called==0 ); 8627 VVA_ONLY( balance_quick_called++ ); 8628 rc = balance_quick(pParent, pPage, aBalanceQuickSpace); 8629 }else 8630 #endif 8631 { 8632 /* In this case, call balance_nonroot() to redistribute cells 8633 ** between pPage and up to 2 of its sibling pages. This involves 8634 ** modifying the contents of pParent, which may cause pParent to 8635 ** become overfull or underfull. The next iteration of the do-loop 8636 ** will balance the parent page to correct this. 8637 ** 8638 ** If the parent page becomes overfull, the overflow cell or cells 8639 ** are stored in the pSpace buffer allocated immediately below. 8640 ** A subsequent iteration of the do-loop will deal with this by 8641 ** calling balance_nonroot() (balance_deeper() may be called first, 8642 ** but it doesn't deal with overflow cells - just moves them to a 8643 ** different page). Once this subsequent call to balance_nonroot() 8644 ** has completed, it is safe to release the pSpace buffer used by 8645 ** the previous call, as the overflow cell data will have been 8646 ** copied either into the body of a database page or into the new 8647 ** pSpace buffer passed to the latter call to balance_nonroot(). 8648 */ 8649 u8 *pSpace = sqlite3PageMalloc(pCur->pBt->pageSize); 8650 rc = balance_nonroot(pParent, iIdx, pSpace, iPage==1, 8651 pCur->hints&BTREE_BULKLOAD); 8652 if( pFree ){ 8653 /* If pFree is not NULL, it points to the pSpace buffer used 8654 ** by a previous call to balance_nonroot(). Its contents are 8655 ** now stored either on real database pages or within the 8656 ** new pSpace buffer, so it may be safely freed here. */ 8657 sqlite3PageFree(pFree); 8658 } 8659 8660 /* The pSpace buffer will be freed after the next call to 8661 ** balance_nonroot(), or just before this function returns, whichever 8662 ** comes first. */ 8663 pFree = pSpace; 8664 } 8665 } 8666 8667 pPage->nOverflow = 0; 8668 8669 /* The next iteration of the do-loop balances the parent page. */ 8670 releasePage(pPage); 8671 pCur->iPage--; 8672 assert( pCur->iPage>=0 ); 8673 pCur->pPage = pCur->apPage[pCur->iPage]; 8674 } 8675 }while( rc==SQLITE_OK ); 8676 8677 if( pFree ){ 8678 sqlite3PageFree(pFree); 8679 } 8680 return rc; 8681 } 8682 8683 /* Overwrite content from pX into pDest. Only do the write if the 8684 ** content is different from what is already there. 8685 */ 8686 static int btreeOverwriteContent( 8687 MemPage *pPage, /* MemPage on which writing will occur */ 8688 u8 *pDest, /* Pointer to the place to start writing */ 8689 const BtreePayload *pX, /* Source of data to write */ 8690 int iOffset, /* Offset of first byte to write */ 8691 int iAmt /* Number of bytes to be written */ 8692 ){ 8693 int nData = pX->nData - iOffset; 8694 if( nData<=0 ){ 8695 /* Overwritting with zeros */ 8696 int i; 8697 for(i=0; i<iAmt && pDest[i]==0; i++){} 8698 if( i<iAmt ){ 8699 int rc = sqlite3PagerWrite(pPage->pDbPage); 8700 if( rc ) return rc; 8701 memset(pDest + i, 0, iAmt - i); 8702 } 8703 }else{ 8704 if( nData<iAmt ){ 8705 /* Mixed read data and zeros at the end. Make a recursive call 8706 ** to write the zeros then fall through to write the real data */ 8707 int rc = btreeOverwriteContent(pPage, pDest+nData, pX, iOffset+nData, 8708 iAmt-nData); 8709 if( rc ) return rc; 8710 iAmt = nData; 8711 } 8712 if( memcmp(pDest, ((u8*)pX->pData) + iOffset, iAmt)!=0 ){ 8713 int rc = sqlite3PagerWrite(pPage->pDbPage); 8714 if( rc ) return rc; 8715 /* In a corrupt database, it is possible for the source and destination 8716 ** buffers to overlap. This is harmless since the database is already 8717 ** corrupt but it does cause valgrind and ASAN warnings. So use 8718 ** memmove(). */ 8719 memmove(pDest, ((u8*)pX->pData) + iOffset, iAmt); 8720 } 8721 } 8722 return SQLITE_OK; 8723 } 8724 8725 /* 8726 ** Overwrite the cell that cursor pCur is pointing to with fresh content 8727 ** contained in pX. 8728 */ 8729 static int btreeOverwriteCell(BtCursor *pCur, const BtreePayload *pX){ 8730 int iOffset; /* Next byte of pX->pData to write */ 8731 int nTotal = pX->nData + pX->nZero; /* Total bytes of to write */ 8732 int rc; /* Return code */ 8733 MemPage *pPage = pCur->pPage; /* Page being written */ 8734 BtShared *pBt; /* Btree */ 8735 Pgno ovflPgno; /* Next overflow page to write */ 8736 u32 ovflPageSize; /* Size to write on overflow page */ 8737 8738 if( pCur->info.pPayload + pCur->info.nLocal > pPage->aDataEnd 8739 || pCur->info.pPayload < pPage->aData + pPage->cellOffset 8740 ){ 8741 return SQLITE_CORRUPT_BKPT; 8742 } 8743 /* Overwrite the local portion first */ 8744 rc = btreeOverwriteContent(pPage, pCur->info.pPayload, pX, 8745 0, pCur->info.nLocal); 8746 if( rc ) return rc; 8747 if( pCur->info.nLocal==nTotal ) return SQLITE_OK; 8748 8749 /* Now overwrite the overflow pages */ 8750 iOffset = pCur->info.nLocal; 8751 assert( nTotal>=0 ); 8752 assert( iOffset>=0 ); 8753 ovflPgno = get4byte(pCur->info.pPayload + iOffset); 8754 pBt = pPage->pBt; 8755 ovflPageSize = pBt->usableSize - 4; 8756 do{ 8757 rc = btreeGetPage(pBt, ovflPgno, &pPage, 0); 8758 if( rc ) return rc; 8759 if( sqlite3PagerPageRefcount(pPage->pDbPage)!=1 || pPage->isInit ){ 8760 rc = SQLITE_CORRUPT_BKPT; 8761 }else{ 8762 if( iOffset+ovflPageSize<(u32)nTotal ){ 8763 ovflPgno = get4byte(pPage->aData); 8764 }else{ 8765 ovflPageSize = nTotal - iOffset; 8766 } 8767 rc = btreeOverwriteContent(pPage, pPage->aData+4, pX, 8768 iOffset, ovflPageSize); 8769 } 8770 sqlite3PagerUnref(pPage->pDbPage); 8771 if( rc ) return rc; 8772 iOffset += ovflPageSize; 8773 }while( iOffset<nTotal ); 8774 return SQLITE_OK; 8775 } 8776 8777 8778 /* 8779 ** Insert a new record into the BTree. The content of the new record 8780 ** is described by the pX object. The pCur cursor is used only to 8781 ** define what table the record should be inserted into, and is left 8782 ** pointing at a random location. 8783 ** 8784 ** For a table btree (used for rowid tables), only the pX.nKey value of 8785 ** the key is used. The pX.pKey value must be NULL. The pX.nKey is the 8786 ** rowid or INTEGER PRIMARY KEY of the row. The pX.nData,pData,nZero fields 8787 ** hold the content of the row. 8788 ** 8789 ** For an index btree (used for indexes and WITHOUT ROWID tables), the 8790 ** key is an arbitrary byte sequence stored in pX.pKey,nKey. The 8791 ** pX.pData,nData,nZero fields must be zero. 8792 ** 8793 ** If the seekResult parameter is non-zero, then a successful call to 8794 ** MovetoUnpacked() to seek cursor pCur to (pKey,nKey) has already 8795 ** been performed. In other words, if seekResult!=0 then the cursor 8796 ** is currently pointing to a cell that will be adjacent to the cell 8797 ** to be inserted. If seekResult<0 then pCur points to a cell that is 8798 ** smaller then (pKey,nKey). If seekResult>0 then pCur points to a cell 8799 ** that is larger than (pKey,nKey). 8800 ** 8801 ** If seekResult==0, that means pCur is pointing at some unknown location. 8802 ** In that case, this routine must seek the cursor to the correct insertion 8803 ** point for (pKey,nKey) before doing the insertion. For index btrees, 8804 ** if pX->nMem is non-zero, then pX->aMem contains pointers to the unpacked 8805 ** key values and pX->aMem can be used instead of pX->pKey to avoid having 8806 ** to decode the key. 8807 */ 8808 int sqlite3BtreeInsert( 8809 BtCursor *pCur, /* Insert data into the table of this cursor */ 8810 const BtreePayload *pX, /* Content of the row to be inserted */ 8811 int flags, /* True if this is likely an append */ 8812 int seekResult /* Result of prior MovetoUnpacked() call */ 8813 ){ 8814 int rc; 8815 int loc = seekResult; /* -1: before desired location +1: after */ 8816 int szNew = 0; 8817 int idx; 8818 MemPage *pPage; 8819 Btree *p = pCur->pBtree; 8820 BtShared *pBt = p->pBt; 8821 unsigned char *oldCell; 8822 unsigned char *newCell = 0; 8823 8824 assert( (flags & (BTREE_SAVEPOSITION|BTREE_APPEND|BTREE_PREFORMAT))==flags ); 8825 assert( (flags & BTREE_PREFORMAT)==0 || seekResult || pCur->pKeyInfo==0 ); 8826 8827 if( pCur->eState==CURSOR_FAULT ){ 8828 assert( pCur->skipNext!=SQLITE_OK ); 8829 return pCur->skipNext; 8830 } 8831 8832 assert( cursorOwnsBtShared(pCur) ); 8833 assert( (pCur->curFlags & BTCF_WriteFlag)!=0 8834 && pBt->inTransaction==TRANS_WRITE 8835 && (pBt->btsFlags & BTS_READ_ONLY)==0 ); 8836 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) ); 8837 8838 /* Assert that the caller has been consistent. If this cursor was opened 8839 ** expecting an index b-tree, then the caller should be inserting blob 8840 ** keys with no associated data. If the cursor was opened expecting an 8841 ** intkey table, the caller should be inserting integer keys with a 8842 ** blob of associated data. */ 8843 assert( (flags & BTREE_PREFORMAT) || (pX->pKey==0)==(pCur->pKeyInfo==0) ); 8844 8845 /* Save the positions of any other cursors open on this table. 8846 ** 8847 ** In some cases, the call to btreeMoveto() below is a no-op. For 8848 ** example, when inserting data into a table with auto-generated integer 8849 ** keys, the VDBE layer invokes sqlite3BtreeLast() to figure out the 8850 ** integer key to use. It then calls this function to actually insert the 8851 ** data into the intkey B-Tree. In this case btreeMoveto() recognizes 8852 ** that the cursor is already where it needs to be and returns without 8853 ** doing any work. To avoid thwarting these optimizations, it is important 8854 ** not to clear the cursor here. 8855 */ 8856 if( pCur->curFlags & BTCF_Multiple ){ 8857 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur); 8858 if( rc ) return rc; 8859 if( loc && pCur->iPage<0 ){ 8860 /* This can only happen if the schema is corrupt such that there is more 8861 ** than one table or index with the same root page as used by the cursor. 8862 ** Which can only happen if the SQLITE_NoSchemaError flag was set when 8863 ** the schema was loaded. This cannot be asserted though, as a user might 8864 ** set the flag, load the schema, and then unset the flag. */ 8865 return SQLITE_CORRUPT_BKPT; 8866 } 8867 } 8868 8869 if( pCur->pKeyInfo==0 ){ 8870 assert( pX->pKey==0 ); 8871 /* If this is an insert into a table b-tree, invalidate any incrblob 8872 ** cursors open on the row being replaced */ 8873 if( p->hasIncrblobCur ){ 8874 invalidateIncrblobCursors(p, pCur->pgnoRoot, pX->nKey, 0); 8875 } 8876 8877 /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing 8878 ** to a row with the same key as the new entry being inserted. 8879 */ 8880 #ifdef SQLITE_DEBUG 8881 if( flags & BTREE_SAVEPOSITION ){ 8882 assert( pCur->curFlags & BTCF_ValidNKey ); 8883 assert( pX->nKey==pCur->info.nKey ); 8884 assert( loc==0 ); 8885 } 8886 #endif 8887 8888 /* On the other hand, BTREE_SAVEPOSITION==0 does not imply 8889 ** that the cursor is not pointing to a row to be overwritten. 8890 ** So do a complete check. 8891 */ 8892 if( (pCur->curFlags&BTCF_ValidNKey)!=0 && pX->nKey==pCur->info.nKey ){ 8893 /* The cursor is pointing to the entry that is to be 8894 ** overwritten */ 8895 assert( pX->nData>=0 && pX->nZero>=0 ); 8896 if( pCur->info.nSize!=0 8897 && pCur->info.nPayload==(u32)pX->nData+pX->nZero 8898 ){ 8899 /* New entry is the same size as the old. Do an overwrite */ 8900 return btreeOverwriteCell(pCur, pX); 8901 } 8902 assert( loc==0 ); 8903 }else if( loc==0 ){ 8904 /* The cursor is *not* pointing to the cell to be overwritten, nor 8905 ** to an adjacent cell. Move the cursor so that it is pointing either 8906 ** to the cell to be overwritten or an adjacent cell. 8907 */ 8908 rc = sqlite3BtreeTableMoveto(pCur, pX->nKey, 8909 (flags & BTREE_APPEND)!=0, &loc); 8910 if( rc ) return rc; 8911 } 8912 }else{ 8913 /* This is an index or a WITHOUT ROWID table */ 8914 8915 /* If BTREE_SAVEPOSITION is set, the cursor must already be pointing 8916 ** to a row with the same key as the new entry being inserted. 8917 */ 8918 assert( (flags & BTREE_SAVEPOSITION)==0 || loc==0 ); 8919 8920 /* If the cursor is not already pointing either to the cell to be 8921 ** overwritten, or if a new cell is being inserted, if the cursor is 8922 ** not pointing to an immediately adjacent cell, then move the cursor 8923 ** so that it does. 8924 */ 8925 if( loc==0 && (flags & BTREE_SAVEPOSITION)==0 ){ 8926 if( pX->nMem ){ 8927 UnpackedRecord r; 8928 r.pKeyInfo = pCur->pKeyInfo; 8929 r.aMem = pX->aMem; 8930 r.nField = pX->nMem; 8931 r.default_rc = 0; 8932 r.eqSeen = 0; 8933 rc = sqlite3BtreeIndexMoveto(pCur, &r, &loc); 8934 }else{ 8935 rc = btreeMoveto(pCur, pX->pKey, pX->nKey, 8936 (flags & BTREE_APPEND)!=0, &loc); 8937 } 8938 if( rc ) return rc; 8939 } 8940 8941 /* If the cursor is currently pointing to an entry to be overwritten 8942 ** and the new content is the same as as the old, then use the 8943 ** overwrite optimization. 8944 */ 8945 if( loc==0 ){ 8946 getCellInfo(pCur); 8947 if( pCur->info.nKey==pX->nKey ){ 8948 BtreePayload x2; 8949 x2.pData = pX->pKey; 8950 x2.nData = pX->nKey; 8951 x2.nZero = 0; 8952 return btreeOverwriteCell(pCur, &x2); 8953 } 8954 } 8955 } 8956 assert( pCur->eState==CURSOR_VALID 8957 || (pCur->eState==CURSOR_INVALID && loc) 8958 || CORRUPT_DB ); 8959 8960 pPage = pCur->pPage; 8961 assert( pPage->intKey || pX->nKey>=0 || (flags & BTREE_PREFORMAT) ); 8962 assert( pPage->leaf || !pPage->intKey ); 8963 if( pPage->nFree<0 ){ 8964 if( NEVER(pCur->eState>CURSOR_INVALID) ){ 8965 rc = SQLITE_CORRUPT_BKPT; 8966 }else{ 8967 rc = btreeComputeFreeSpace(pPage); 8968 } 8969 if( rc ) return rc; 8970 } 8971 8972 TRACE(("INSERT: table=%d nkey=%lld ndata=%d page=%d %s\n", 8973 pCur->pgnoRoot, pX->nKey, pX->nData, pPage->pgno, 8974 loc==0 ? "overwrite" : "new entry")); 8975 assert( pPage->isInit ); 8976 newCell = pBt->pTmpSpace; 8977 assert( newCell!=0 ); 8978 if( flags & BTREE_PREFORMAT ){ 8979 rc = SQLITE_OK; 8980 szNew = pBt->nPreformatSize; 8981 if( szNew<4 ) szNew = 4; 8982 if( ISAUTOVACUUM && szNew>pPage->maxLocal ){ 8983 CellInfo info; 8984 pPage->xParseCell(pPage, newCell, &info); 8985 if( info.nPayload!=info.nLocal ){ 8986 Pgno ovfl = get4byte(&newCell[szNew-4]); 8987 ptrmapPut(pBt, ovfl, PTRMAP_OVERFLOW1, pPage->pgno, &rc); 8988 } 8989 } 8990 }else{ 8991 rc = fillInCell(pPage, newCell, pX, &szNew); 8992 } 8993 if( rc ) goto end_insert; 8994 assert( szNew==pPage->xCellSize(pPage, newCell) ); 8995 assert( szNew <= MX_CELL_SIZE(pBt) ); 8996 idx = pCur->ix; 8997 if( loc==0 ){ 8998 CellInfo info; 8999 assert( idx>=0 ); 9000 if( idx>=pPage->nCell ){ 9001 return SQLITE_CORRUPT_BKPT; 9002 } 9003 rc = sqlite3PagerWrite(pPage->pDbPage); 9004 if( rc ){ 9005 goto end_insert; 9006 } 9007 oldCell = findCell(pPage, idx); 9008 if( !pPage->leaf ){ 9009 memcpy(newCell, oldCell, 4); 9010 } 9011 BTREE_CLEAR_CELL(rc, pPage, oldCell, info); 9012 testcase( pCur->curFlags & BTCF_ValidOvfl ); 9013 invalidateOverflowCache(pCur); 9014 if( info.nSize==szNew && info.nLocal==info.nPayload 9015 && (!ISAUTOVACUUM || szNew<pPage->minLocal) 9016 ){ 9017 /* Overwrite the old cell with the new if they are the same size. 9018 ** We could also try to do this if the old cell is smaller, then add 9019 ** the leftover space to the free list. But experiments show that 9020 ** doing that is no faster then skipping this optimization and just 9021 ** calling dropCell() and insertCell(). 9022 ** 9023 ** This optimization cannot be used on an autovacuum database if the 9024 ** new entry uses overflow pages, as the insertCell() call below is 9025 ** necessary to add the PTRMAP_OVERFLOW1 pointer-map entry. */ 9026 assert( rc==SQLITE_OK ); /* clearCell never fails when nLocal==nPayload */ 9027 if( oldCell < pPage->aData+pPage->hdrOffset+10 ){ 9028 return SQLITE_CORRUPT_BKPT; 9029 } 9030 if( oldCell+szNew > pPage->aDataEnd ){ 9031 return SQLITE_CORRUPT_BKPT; 9032 } 9033 memcpy(oldCell, newCell, szNew); 9034 return SQLITE_OK; 9035 } 9036 dropCell(pPage, idx, info.nSize, &rc); 9037 if( rc ) goto end_insert; 9038 }else if( loc<0 && pPage->nCell>0 ){ 9039 assert( pPage->leaf ); 9040 idx = ++pCur->ix; 9041 pCur->curFlags &= ~BTCF_ValidNKey; 9042 }else{ 9043 assert( pPage->leaf ); 9044 } 9045 insertCell(pPage, idx, newCell, szNew, 0, 0, &rc); 9046 assert( pPage->nOverflow==0 || rc==SQLITE_OK ); 9047 assert( rc!=SQLITE_OK || pPage->nCell>0 || pPage->nOverflow>0 ); 9048 9049 /* If no error has occurred and pPage has an overflow cell, call balance() 9050 ** to redistribute the cells within the tree. Since balance() may move 9051 ** the cursor, zero the BtCursor.info.nSize and BTCF_ValidNKey 9052 ** variables. 9053 ** 9054 ** Previous versions of SQLite called moveToRoot() to move the cursor 9055 ** back to the root page as balance() used to invalidate the contents 9056 ** of BtCursor.apPage[] and BtCursor.aiIdx[]. Instead of doing that, 9057 ** set the cursor state to "invalid". This makes common insert operations 9058 ** slightly faster. 9059 ** 9060 ** There is a subtle but important optimization here too. When inserting 9061 ** multiple records into an intkey b-tree using a single cursor (as can 9062 ** happen while processing an "INSERT INTO ... SELECT" statement), it 9063 ** is advantageous to leave the cursor pointing to the last entry in 9064 ** the b-tree if possible. If the cursor is left pointing to the last 9065 ** entry in the table, and the next row inserted has an integer key 9066 ** larger than the largest existing key, it is possible to insert the 9067 ** row without seeking the cursor. This can be a big performance boost. 9068 */ 9069 pCur->info.nSize = 0; 9070 if( pPage->nOverflow ){ 9071 assert( rc==SQLITE_OK ); 9072 pCur->curFlags &= ~(BTCF_ValidNKey); 9073 rc = balance(pCur); 9074 9075 /* Must make sure nOverflow is reset to zero even if the balance() 9076 ** fails. Internal data structure corruption will result otherwise. 9077 ** Also, set the cursor state to invalid. This stops saveCursorPosition() 9078 ** from trying to save the current position of the cursor. */ 9079 pCur->pPage->nOverflow = 0; 9080 pCur->eState = CURSOR_INVALID; 9081 if( (flags & BTREE_SAVEPOSITION) && rc==SQLITE_OK ){ 9082 btreeReleaseAllCursorPages(pCur); 9083 if( pCur->pKeyInfo ){ 9084 assert( pCur->pKey==0 ); 9085 pCur->pKey = sqlite3Malloc( pX->nKey ); 9086 if( pCur->pKey==0 ){ 9087 rc = SQLITE_NOMEM; 9088 }else{ 9089 memcpy(pCur->pKey, pX->pKey, pX->nKey); 9090 } 9091 } 9092 pCur->eState = CURSOR_REQUIRESEEK; 9093 pCur->nKey = pX->nKey; 9094 } 9095 } 9096 assert( pCur->iPage<0 || pCur->pPage->nOverflow==0 ); 9097 9098 end_insert: 9099 return rc; 9100 } 9101 9102 /* 9103 ** This function is used as part of copying the current row from cursor 9104 ** pSrc into cursor pDest. If the cursors are open on intkey tables, then 9105 ** parameter iKey is used as the rowid value when the record is copied 9106 ** into pDest. Otherwise, the record is copied verbatim. 9107 ** 9108 ** This function does not actually write the new value to cursor pDest. 9109 ** Instead, it creates and populates any required overflow pages and 9110 ** writes the data for the new cell into the BtShared.pTmpSpace buffer 9111 ** for the destination database. The size of the cell, in bytes, is left 9112 ** in BtShared.nPreformatSize. The caller completes the insertion by 9113 ** calling sqlite3BtreeInsert() with the BTREE_PREFORMAT flag specified. 9114 ** 9115 ** SQLITE_OK is returned if successful, or an SQLite error code otherwise. 9116 */ 9117 int sqlite3BtreeTransferRow(BtCursor *pDest, BtCursor *pSrc, i64 iKey){ 9118 int rc = SQLITE_OK; 9119 BtShared *pBt = pDest->pBt; 9120 u8 *aOut = pBt->pTmpSpace; /* Pointer to next output buffer */ 9121 const u8 *aIn; /* Pointer to next input buffer */ 9122 u32 nIn; /* Size of input buffer aIn[] */ 9123 u32 nRem; /* Bytes of data still to copy */ 9124 9125 getCellInfo(pSrc); 9126 aOut += putVarint32(aOut, pSrc->info.nPayload); 9127 if( pDest->pKeyInfo==0 ) aOut += putVarint(aOut, iKey); 9128 nIn = pSrc->info.nLocal; 9129 aIn = pSrc->info.pPayload; 9130 if( aIn+nIn>pSrc->pPage->aDataEnd ){ 9131 return SQLITE_CORRUPT_BKPT; 9132 } 9133 nRem = pSrc->info.nPayload; 9134 if( nIn==nRem && nIn<pDest->pPage->maxLocal ){ 9135 memcpy(aOut, aIn, nIn); 9136 pBt->nPreformatSize = nIn + (aOut - pBt->pTmpSpace); 9137 }else{ 9138 Pager *pSrcPager = pSrc->pBt->pPager; 9139 u8 *pPgnoOut = 0; 9140 Pgno ovflIn = 0; 9141 DbPage *pPageIn = 0; 9142 MemPage *pPageOut = 0; 9143 u32 nOut; /* Size of output buffer aOut[] */ 9144 9145 nOut = btreePayloadToLocal(pDest->pPage, pSrc->info.nPayload); 9146 pBt->nPreformatSize = nOut + (aOut - pBt->pTmpSpace); 9147 if( nOut<pSrc->info.nPayload ){ 9148 pPgnoOut = &aOut[nOut]; 9149 pBt->nPreformatSize += 4; 9150 } 9151 9152 if( nRem>nIn ){ 9153 if( aIn+nIn+4>pSrc->pPage->aDataEnd ){ 9154 return SQLITE_CORRUPT_BKPT; 9155 } 9156 ovflIn = get4byte(&pSrc->info.pPayload[nIn]); 9157 } 9158 9159 do { 9160 nRem -= nOut; 9161 do{ 9162 assert( nOut>0 ); 9163 if( nIn>0 ){ 9164 int nCopy = MIN(nOut, nIn); 9165 memcpy(aOut, aIn, nCopy); 9166 nOut -= nCopy; 9167 nIn -= nCopy; 9168 aOut += nCopy; 9169 aIn += nCopy; 9170 } 9171 if( nOut>0 ){ 9172 sqlite3PagerUnref(pPageIn); 9173 pPageIn = 0; 9174 rc = sqlite3PagerGet(pSrcPager, ovflIn, &pPageIn, PAGER_GET_READONLY); 9175 if( rc==SQLITE_OK ){ 9176 aIn = (const u8*)sqlite3PagerGetData(pPageIn); 9177 ovflIn = get4byte(aIn); 9178 aIn += 4; 9179 nIn = pSrc->pBt->usableSize - 4; 9180 } 9181 } 9182 }while( rc==SQLITE_OK && nOut>0 ); 9183 9184 if( rc==SQLITE_OK && nRem>0 && ALWAYS(pPgnoOut) ){ 9185 Pgno pgnoNew; 9186 MemPage *pNew = 0; 9187 rc = allocateBtreePage(pBt, &pNew, &pgnoNew, 0, 0); 9188 put4byte(pPgnoOut, pgnoNew); 9189 if( ISAUTOVACUUM && pPageOut ){ 9190 ptrmapPut(pBt, pgnoNew, PTRMAP_OVERFLOW2, pPageOut->pgno, &rc); 9191 } 9192 releasePage(pPageOut); 9193 pPageOut = pNew; 9194 if( pPageOut ){ 9195 pPgnoOut = pPageOut->aData; 9196 put4byte(pPgnoOut, 0); 9197 aOut = &pPgnoOut[4]; 9198 nOut = MIN(pBt->usableSize - 4, nRem); 9199 } 9200 } 9201 }while( nRem>0 && rc==SQLITE_OK ); 9202 9203 releasePage(pPageOut); 9204 sqlite3PagerUnref(pPageIn); 9205 } 9206 9207 return rc; 9208 } 9209 9210 /* 9211 ** Delete the entry that the cursor is pointing to. 9212 ** 9213 ** If the BTREE_SAVEPOSITION bit of the flags parameter is zero, then 9214 ** the cursor is left pointing at an arbitrary location after the delete. 9215 ** But if that bit is set, then the cursor is left in a state such that 9216 ** the next call to BtreeNext() or BtreePrev() moves it to the same row 9217 ** as it would have been on if the call to BtreeDelete() had been omitted. 9218 ** 9219 ** The BTREE_AUXDELETE bit of flags indicates that is one of several deletes 9220 ** associated with a single table entry and its indexes. Only one of those 9221 ** deletes is considered the "primary" delete. The primary delete occurs 9222 ** on a cursor that is not a BTREE_FORDELETE cursor. All but one delete 9223 ** operation on non-FORDELETE cursors is tagged with the AUXDELETE flag. 9224 ** The BTREE_AUXDELETE bit is a hint that is not used by this implementation, 9225 ** but which might be used by alternative storage engines. 9226 */ 9227 int sqlite3BtreeDelete(BtCursor *pCur, u8 flags){ 9228 Btree *p = pCur->pBtree; 9229 BtShared *pBt = p->pBt; 9230 int rc; /* Return code */ 9231 MemPage *pPage; /* Page to delete cell from */ 9232 unsigned char *pCell; /* Pointer to cell to delete */ 9233 int iCellIdx; /* Index of cell to delete */ 9234 int iCellDepth; /* Depth of node containing pCell */ 9235 CellInfo info; /* Size of the cell being deleted */ 9236 int bSkipnext = 0; /* Leaf cursor in SKIPNEXT state */ 9237 u8 bPreserve = flags & BTREE_SAVEPOSITION; /* Keep cursor valid */ 9238 9239 assert( cursorOwnsBtShared(pCur) ); 9240 assert( pBt->inTransaction==TRANS_WRITE ); 9241 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 ); 9242 assert( pCur->curFlags & BTCF_WriteFlag ); 9243 assert( hasSharedCacheTableLock(p, pCur->pgnoRoot, pCur->pKeyInfo!=0, 2) ); 9244 assert( !hasReadConflicts(p, pCur->pgnoRoot) ); 9245 assert( (flags & ~(BTREE_SAVEPOSITION | BTREE_AUXDELETE))==0 ); 9246 if( pCur->eState==CURSOR_REQUIRESEEK ){ 9247 rc = btreeRestoreCursorPosition(pCur); 9248 assert( rc!=SQLITE_OK || CORRUPT_DB || pCur->eState==CURSOR_VALID ); 9249 if( rc || pCur->eState!=CURSOR_VALID ) return rc; 9250 } 9251 assert( CORRUPT_DB || pCur->eState==CURSOR_VALID ); 9252 9253 iCellDepth = pCur->iPage; 9254 iCellIdx = pCur->ix; 9255 pPage = pCur->pPage; 9256 pCell = findCell(pPage, iCellIdx); 9257 if( pPage->nFree<0 && btreeComputeFreeSpace(pPage) ) return SQLITE_CORRUPT; 9258 9259 /* If the bPreserve flag is set to true, then the cursor position must 9260 ** be preserved following this delete operation. If the current delete 9261 ** will cause a b-tree rebalance, then this is done by saving the cursor 9262 ** key and leaving the cursor in CURSOR_REQUIRESEEK state before 9263 ** returning. 9264 ** 9265 ** Or, if the current delete will not cause a rebalance, then the cursor 9266 ** will be left in CURSOR_SKIPNEXT state pointing to the entry immediately 9267 ** before or after the deleted entry. In this case set bSkipnext to true. */ 9268 if( bPreserve ){ 9269 if( !pPage->leaf 9270 || (pPage->nFree+cellSizePtr(pPage,pCell)+2)>(int)(pBt->usableSize*2/3) 9271 || pPage->nCell==1 /* See dbfuzz001.test for a test case */ 9272 ){ 9273 /* A b-tree rebalance will be required after deleting this entry. 9274 ** Save the cursor key. */ 9275 rc = saveCursorKey(pCur); 9276 if( rc ) return rc; 9277 }else{ 9278 bSkipnext = 1; 9279 } 9280 } 9281 9282 /* If the page containing the entry to delete is not a leaf page, move 9283 ** the cursor to the largest entry in the tree that is smaller than 9284 ** the entry being deleted. This cell will replace the cell being deleted 9285 ** from the internal node. The 'previous' entry is used for this instead 9286 ** of the 'next' entry, as the previous entry is always a part of the 9287 ** sub-tree headed by the child page of the cell being deleted. This makes 9288 ** balancing the tree following the delete operation easier. */ 9289 if( !pPage->leaf ){ 9290 rc = sqlite3BtreePrevious(pCur, 0); 9291 assert( rc!=SQLITE_DONE ); 9292 if( rc ) return rc; 9293 } 9294 9295 /* Save the positions of any other cursors open on this table before 9296 ** making any modifications. */ 9297 if( pCur->curFlags & BTCF_Multiple ){ 9298 rc = saveAllCursors(pBt, pCur->pgnoRoot, pCur); 9299 if( rc ) return rc; 9300 } 9301 9302 /* If this is a delete operation to remove a row from a table b-tree, 9303 ** invalidate any incrblob cursors open on the row being deleted. */ 9304 if( pCur->pKeyInfo==0 && p->hasIncrblobCur ){ 9305 invalidateIncrblobCursors(p, pCur->pgnoRoot, pCur->info.nKey, 0); 9306 } 9307 9308 /* Make the page containing the entry to be deleted writable. Then free any 9309 ** overflow pages associated with the entry and finally remove the cell 9310 ** itself from within the page. */ 9311 rc = sqlite3PagerWrite(pPage->pDbPage); 9312 if( rc ) return rc; 9313 BTREE_CLEAR_CELL(rc, pPage, pCell, info); 9314 dropCell(pPage, iCellIdx, info.nSize, &rc); 9315 if( rc ) return rc; 9316 9317 /* If the cell deleted was not located on a leaf page, then the cursor 9318 ** is currently pointing to the largest entry in the sub-tree headed 9319 ** by the child-page of the cell that was just deleted from an internal 9320 ** node. The cell from the leaf node needs to be moved to the internal 9321 ** node to replace the deleted cell. */ 9322 if( !pPage->leaf ){ 9323 MemPage *pLeaf = pCur->pPage; 9324 int nCell; 9325 Pgno n; 9326 unsigned char *pTmp; 9327 9328 if( pLeaf->nFree<0 ){ 9329 rc = btreeComputeFreeSpace(pLeaf); 9330 if( rc ) return rc; 9331 } 9332 if( iCellDepth<pCur->iPage-1 ){ 9333 n = pCur->apPage[iCellDepth+1]->pgno; 9334 }else{ 9335 n = pCur->pPage->pgno; 9336 } 9337 pCell = findCell(pLeaf, pLeaf->nCell-1); 9338 if( pCell<&pLeaf->aData[4] ) return SQLITE_CORRUPT_BKPT; 9339 nCell = pLeaf->xCellSize(pLeaf, pCell); 9340 assert( MX_CELL_SIZE(pBt) >= nCell ); 9341 pTmp = pBt->pTmpSpace; 9342 assert( pTmp!=0 ); 9343 rc = sqlite3PagerWrite(pLeaf->pDbPage); 9344 if( rc==SQLITE_OK ){ 9345 insertCell(pPage, iCellIdx, pCell-4, nCell+4, pTmp, n, &rc); 9346 } 9347 dropCell(pLeaf, pLeaf->nCell-1, nCell, &rc); 9348 if( rc ) return rc; 9349 } 9350 9351 /* Balance the tree. If the entry deleted was located on a leaf page, 9352 ** then the cursor still points to that page. In this case the first 9353 ** call to balance() repairs the tree, and the if(...) condition is 9354 ** never true. 9355 ** 9356 ** Otherwise, if the entry deleted was on an internal node page, then 9357 ** pCur is pointing to the leaf page from which a cell was removed to 9358 ** replace the cell deleted from the internal node. This is slightly 9359 ** tricky as the leaf node may be underfull, and the internal node may 9360 ** be either under or overfull. In this case run the balancing algorithm 9361 ** on the leaf node first. If the balance proceeds far enough up the 9362 ** tree that we can be sure that any problem in the internal node has 9363 ** been corrected, so be it. Otherwise, after balancing the leaf node, 9364 ** walk the cursor up the tree to the internal node and balance it as 9365 ** well. */ 9366 rc = balance(pCur); 9367 if( rc==SQLITE_OK && pCur->iPage>iCellDepth ){ 9368 releasePageNotNull(pCur->pPage); 9369 pCur->iPage--; 9370 while( pCur->iPage>iCellDepth ){ 9371 releasePage(pCur->apPage[pCur->iPage--]); 9372 } 9373 pCur->pPage = pCur->apPage[pCur->iPage]; 9374 rc = balance(pCur); 9375 } 9376 9377 if( rc==SQLITE_OK ){ 9378 if( bSkipnext ){ 9379 assert( bPreserve && (pCur->iPage==iCellDepth || CORRUPT_DB) ); 9380 assert( pPage==pCur->pPage || CORRUPT_DB ); 9381 assert( (pPage->nCell>0 || CORRUPT_DB) && iCellIdx<=pPage->nCell ); 9382 pCur->eState = CURSOR_SKIPNEXT; 9383 if( iCellIdx>=pPage->nCell ){ 9384 pCur->skipNext = -1; 9385 pCur->ix = pPage->nCell-1; 9386 }else{ 9387 pCur->skipNext = 1; 9388 } 9389 }else{ 9390 rc = moveToRoot(pCur); 9391 if( bPreserve ){ 9392 btreeReleaseAllCursorPages(pCur); 9393 pCur->eState = CURSOR_REQUIRESEEK; 9394 } 9395 if( rc==SQLITE_EMPTY ) rc = SQLITE_OK; 9396 } 9397 } 9398 return rc; 9399 } 9400 9401 /* 9402 ** Create a new BTree table. Write into *piTable the page 9403 ** number for the root page of the new table. 9404 ** 9405 ** The type of type is determined by the flags parameter. Only the 9406 ** following values of flags are currently in use. Other values for 9407 ** flags might not work: 9408 ** 9409 ** BTREE_INTKEY|BTREE_LEAFDATA Used for SQL tables with rowid keys 9410 ** BTREE_ZERODATA Used for SQL indices 9411 */ 9412 static int btreeCreateTable(Btree *p, Pgno *piTable, int createTabFlags){ 9413 BtShared *pBt = p->pBt; 9414 MemPage *pRoot; 9415 Pgno pgnoRoot; 9416 int rc; 9417 int ptfFlags; /* Page-type flage for the root page of new table */ 9418 9419 assert( sqlite3BtreeHoldsMutex(p) ); 9420 assert( pBt->inTransaction==TRANS_WRITE ); 9421 assert( (pBt->btsFlags & BTS_READ_ONLY)==0 ); 9422 9423 #ifdef SQLITE_OMIT_AUTOVACUUM 9424 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); 9425 if( rc ){ 9426 return rc; 9427 } 9428 #else 9429 if( pBt->autoVacuum ){ 9430 Pgno pgnoMove; /* Move a page here to make room for the root-page */ 9431 MemPage *pPageMove; /* The page to move to. */ 9432 9433 /* Creating a new table may probably require moving an existing database 9434 ** to make room for the new tables root page. In case this page turns 9435 ** out to be an overflow page, delete all overflow page-map caches 9436 ** held by open cursors. 9437 */ 9438 invalidateAllOverflowCache(pBt); 9439 9440 /* Read the value of meta[3] from the database to determine where the 9441 ** root page of the new table should go. meta[3] is the largest root-page 9442 ** created so far, so the new root-page is (meta[3]+1). 9443 */ 9444 sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &pgnoRoot); 9445 if( pgnoRoot>btreePagecount(pBt) ){ 9446 return SQLITE_CORRUPT_BKPT; 9447 } 9448 pgnoRoot++; 9449 9450 /* The new root-page may not be allocated on a pointer-map page, or the 9451 ** PENDING_BYTE page. 9452 */ 9453 while( pgnoRoot==PTRMAP_PAGENO(pBt, pgnoRoot) || 9454 pgnoRoot==PENDING_BYTE_PAGE(pBt) ){ 9455 pgnoRoot++; 9456 } 9457 assert( pgnoRoot>=3 ); 9458 9459 /* Allocate a page. The page that currently resides at pgnoRoot will 9460 ** be moved to the allocated page (unless the allocated page happens 9461 ** to reside at pgnoRoot). 9462 */ 9463 rc = allocateBtreePage(pBt, &pPageMove, &pgnoMove, pgnoRoot, BTALLOC_EXACT); 9464 if( rc!=SQLITE_OK ){ 9465 return rc; 9466 } 9467 9468 if( pgnoMove!=pgnoRoot ){ 9469 /* pgnoRoot is the page that will be used for the root-page of 9470 ** the new table (assuming an error did not occur). But we were 9471 ** allocated pgnoMove. If required (i.e. if it was not allocated 9472 ** by extending the file), the current page at position pgnoMove 9473 ** is already journaled. 9474 */ 9475 u8 eType = 0; 9476 Pgno iPtrPage = 0; 9477 9478 /* Save the positions of any open cursors. This is required in 9479 ** case they are holding a reference to an xFetch reference 9480 ** corresponding to page pgnoRoot. */ 9481 rc = saveAllCursors(pBt, 0, 0); 9482 releasePage(pPageMove); 9483 if( rc!=SQLITE_OK ){ 9484 return rc; 9485 } 9486 9487 /* Move the page currently at pgnoRoot to pgnoMove. */ 9488 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0); 9489 if( rc!=SQLITE_OK ){ 9490 return rc; 9491 } 9492 rc = ptrmapGet(pBt, pgnoRoot, &eType, &iPtrPage); 9493 if( eType==PTRMAP_ROOTPAGE || eType==PTRMAP_FREEPAGE ){ 9494 rc = SQLITE_CORRUPT_BKPT; 9495 } 9496 if( rc!=SQLITE_OK ){ 9497 releasePage(pRoot); 9498 return rc; 9499 } 9500 assert( eType!=PTRMAP_ROOTPAGE ); 9501 assert( eType!=PTRMAP_FREEPAGE ); 9502 rc = relocatePage(pBt, pRoot, eType, iPtrPage, pgnoMove, 0); 9503 releasePage(pRoot); 9504 9505 /* Obtain the page at pgnoRoot */ 9506 if( rc!=SQLITE_OK ){ 9507 return rc; 9508 } 9509 rc = btreeGetPage(pBt, pgnoRoot, &pRoot, 0); 9510 if( rc!=SQLITE_OK ){ 9511 return rc; 9512 } 9513 rc = sqlite3PagerWrite(pRoot->pDbPage); 9514 if( rc!=SQLITE_OK ){ 9515 releasePage(pRoot); 9516 return rc; 9517 } 9518 }else{ 9519 pRoot = pPageMove; 9520 } 9521 9522 /* Update the pointer-map and meta-data with the new root-page number. */ 9523 ptrmapPut(pBt, pgnoRoot, PTRMAP_ROOTPAGE, 0, &rc); 9524 if( rc ){ 9525 releasePage(pRoot); 9526 return rc; 9527 } 9528 9529 /* When the new root page was allocated, page 1 was made writable in 9530 ** order either to increase the database filesize, or to decrement the 9531 ** freelist count. Hence, the sqlite3BtreeUpdateMeta() call cannot fail. 9532 */ 9533 assert( sqlite3PagerIswriteable(pBt->pPage1->pDbPage) ); 9534 rc = sqlite3BtreeUpdateMeta(p, 4, pgnoRoot); 9535 if( NEVER(rc) ){ 9536 releasePage(pRoot); 9537 return rc; 9538 } 9539 9540 }else{ 9541 rc = allocateBtreePage(pBt, &pRoot, &pgnoRoot, 1, 0); 9542 if( rc ) return rc; 9543 } 9544 #endif 9545 assert( sqlite3PagerIswriteable(pRoot->pDbPage) ); 9546 if( createTabFlags & BTREE_INTKEY ){ 9547 ptfFlags = PTF_INTKEY | PTF_LEAFDATA | PTF_LEAF; 9548 }else{ 9549 ptfFlags = PTF_ZERODATA | PTF_LEAF; 9550 } 9551 zeroPage(pRoot, ptfFlags); 9552 sqlite3PagerUnref(pRoot->pDbPage); 9553 assert( (pBt->openFlags & BTREE_SINGLE)==0 || pgnoRoot==2 ); 9554 *piTable = pgnoRoot; 9555 return SQLITE_OK; 9556 } 9557 int sqlite3BtreeCreateTable(Btree *p, Pgno *piTable, int flags){ 9558 int rc; 9559 sqlite3BtreeEnter(p); 9560 rc = btreeCreateTable(p, piTable, flags); 9561 sqlite3BtreeLeave(p); 9562 return rc; 9563 } 9564 9565 /* 9566 ** Erase the given database page and all its children. Return 9567 ** the page to the freelist. 9568 */ 9569 static int clearDatabasePage( 9570 BtShared *pBt, /* The BTree that contains the table */ 9571 Pgno pgno, /* Page number to clear */ 9572 int freePageFlag, /* Deallocate page if true */ 9573 i64 *pnChange /* Add number of Cells freed to this counter */ 9574 ){ 9575 MemPage *pPage; 9576 int rc; 9577 unsigned char *pCell; 9578 int i; 9579 int hdr; 9580 CellInfo info; 9581 9582 assert( sqlite3_mutex_held(pBt->mutex) ); 9583 if( pgno>btreePagecount(pBt) ){ 9584 return SQLITE_CORRUPT_BKPT; 9585 } 9586 rc = getAndInitPage(pBt, pgno, &pPage, 0, 0); 9587 if( rc ) return rc; 9588 if( (pBt->openFlags & BTREE_SINGLE)==0 9589 && sqlite3PagerPageRefcount(pPage->pDbPage)!=1 9590 ){ 9591 rc = SQLITE_CORRUPT_BKPT; 9592 goto cleardatabasepage_out; 9593 } 9594 hdr = pPage->hdrOffset; 9595 for(i=0; i<pPage->nCell; i++){ 9596 pCell = findCell(pPage, i); 9597 if( !pPage->leaf ){ 9598 rc = clearDatabasePage(pBt, get4byte(pCell), 1, pnChange); 9599 if( rc ) goto cleardatabasepage_out; 9600 } 9601 BTREE_CLEAR_CELL(rc, pPage, pCell, info); 9602 if( rc ) goto cleardatabasepage_out; 9603 } 9604 if( !pPage->leaf ){ 9605 rc = clearDatabasePage(pBt, get4byte(&pPage->aData[hdr+8]), 1, pnChange); 9606 if( rc ) goto cleardatabasepage_out; 9607 if( pPage->intKey ) pnChange = 0; 9608 } 9609 if( pnChange ){ 9610 testcase( !pPage->intKey ); 9611 *pnChange += pPage->nCell; 9612 } 9613 if( freePageFlag ){ 9614 freePage(pPage, &rc); 9615 }else if( (rc = sqlite3PagerWrite(pPage->pDbPage))==0 ){ 9616 zeroPage(pPage, pPage->aData[hdr] | PTF_LEAF); 9617 } 9618 9619 cleardatabasepage_out: 9620 releasePage(pPage); 9621 return rc; 9622 } 9623 9624 /* 9625 ** Delete all information from a single table in the database. iTable is 9626 ** the page number of the root of the table. After this routine returns, 9627 ** the root page is empty, but still exists. 9628 ** 9629 ** This routine will fail with SQLITE_LOCKED if there are any open 9630 ** read cursors on the table. Open write cursors are moved to the 9631 ** root of the table. 9632 ** 9633 ** If pnChange is not NULL, then the integer value pointed to by pnChange 9634 ** is incremented by the number of entries in the table. 9635 */ 9636 int sqlite3BtreeClearTable(Btree *p, int iTable, i64 *pnChange){ 9637 int rc; 9638 BtShared *pBt = p->pBt; 9639 sqlite3BtreeEnter(p); 9640 assert( p->inTrans==TRANS_WRITE ); 9641 9642 rc = saveAllCursors(pBt, (Pgno)iTable, 0); 9643 9644 if( SQLITE_OK==rc ){ 9645 /* Invalidate all incrblob cursors open on table iTable (assuming iTable 9646 ** is the root of a table b-tree - if it is not, the following call is 9647 ** a no-op). */ 9648 if( p->hasIncrblobCur ){ 9649 invalidateIncrblobCursors(p, (Pgno)iTable, 0, 1); 9650 } 9651 rc = clearDatabasePage(pBt, (Pgno)iTable, 0, pnChange); 9652 } 9653 sqlite3BtreeLeave(p); 9654 return rc; 9655 } 9656 9657 /* 9658 ** Delete all information from the single table that pCur is open on. 9659 ** 9660 ** This routine only work for pCur on an ephemeral table. 9661 */ 9662 int sqlite3BtreeClearTableOfCursor(BtCursor *pCur){ 9663 return sqlite3BtreeClearTable(pCur->pBtree, pCur->pgnoRoot, 0); 9664 } 9665 9666 /* 9667 ** Erase all information in a table and add the root of the table to 9668 ** the freelist. Except, the root of the principle table (the one on 9669 ** page 1) is never added to the freelist. 9670 ** 9671 ** This routine will fail with SQLITE_LOCKED if there are any open 9672 ** cursors on the table. 9673 ** 9674 ** If AUTOVACUUM is enabled and the page at iTable is not the last 9675 ** root page in the database file, then the last root page 9676 ** in the database file is moved into the slot formerly occupied by 9677 ** iTable and that last slot formerly occupied by the last root page 9678 ** is added to the freelist instead of iTable. In this say, all 9679 ** root pages are kept at the beginning of the database file, which 9680 ** is necessary for AUTOVACUUM to work right. *piMoved is set to the 9681 ** page number that used to be the last root page in the file before 9682 ** the move. If no page gets moved, *piMoved is set to 0. 9683 ** The last root page is recorded in meta[3] and the value of 9684 ** meta[3] is updated by this procedure. 9685 */ 9686 static int btreeDropTable(Btree *p, Pgno iTable, int *piMoved){ 9687 int rc; 9688 MemPage *pPage = 0; 9689 BtShared *pBt = p->pBt; 9690 9691 assert( sqlite3BtreeHoldsMutex(p) ); 9692 assert( p->inTrans==TRANS_WRITE ); 9693 assert( iTable>=2 ); 9694 if( iTable>btreePagecount(pBt) ){ 9695 return SQLITE_CORRUPT_BKPT; 9696 } 9697 9698 rc = sqlite3BtreeClearTable(p, iTable, 0); 9699 if( rc ) return rc; 9700 rc = btreeGetPage(pBt, (Pgno)iTable, &pPage, 0); 9701 if( NEVER(rc) ){ 9702 releasePage(pPage); 9703 return rc; 9704 } 9705 9706 *piMoved = 0; 9707 9708 #ifdef SQLITE_OMIT_AUTOVACUUM 9709 freePage(pPage, &rc); 9710 releasePage(pPage); 9711 #else 9712 if( pBt->autoVacuum ){ 9713 Pgno maxRootPgno; 9714 sqlite3BtreeGetMeta(p, BTREE_LARGEST_ROOT_PAGE, &maxRootPgno); 9715 9716 if( iTable==maxRootPgno ){ 9717 /* If the table being dropped is the table with the largest root-page 9718 ** number in the database, put the root page on the free list. 9719 */ 9720 freePage(pPage, &rc); 9721 releasePage(pPage); 9722 if( rc!=SQLITE_OK ){ 9723 return rc; 9724 } 9725 }else{ 9726 /* The table being dropped does not have the largest root-page 9727 ** number in the database. So move the page that does into the 9728 ** gap left by the deleted root-page. 9729 */ 9730 MemPage *pMove; 9731 releasePage(pPage); 9732 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0); 9733 if( rc!=SQLITE_OK ){ 9734 return rc; 9735 } 9736 rc = relocatePage(pBt, pMove, PTRMAP_ROOTPAGE, 0, iTable, 0); 9737 releasePage(pMove); 9738 if( rc!=SQLITE_OK ){ 9739 return rc; 9740 } 9741 pMove = 0; 9742 rc = btreeGetPage(pBt, maxRootPgno, &pMove, 0); 9743 freePage(pMove, &rc); 9744 releasePage(pMove); 9745 if( rc!=SQLITE_OK ){ 9746 return rc; 9747 } 9748 *piMoved = maxRootPgno; 9749 } 9750 9751 /* Set the new 'max-root-page' value in the database header. This 9752 ** is the old value less one, less one more if that happens to 9753 ** be a root-page number, less one again if that is the 9754 ** PENDING_BYTE_PAGE. 9755 */ 9756 maxRootPgno--; 9757 while( maxRootPgno==PENDING_BYTE_PAGE(pBt) 9758 || PTRMAP_ISPAGE(pBt, maxRootPgno) ){ 9759 maxRootPgno--; 9760 } 9761 assert( maxRootPgno!=PENDING_BYTE_PAGE(pBt) ); 9762 9763 rc = sqlite3BtreeUpdateMeta(p, 4, maxRootPgno); 9764 }else{ 9765 freePage(pPage, &rc); 9766 releasePage(pPage); 9767 } 9768 #endif 9769 return rc; 9770 } 9771 int sqlite3BtreeDropTable(Btree *p, int iTable, int *piMoved){ 9772 int rc; 9773 sqlite3BtreeEnter(p); 9774 rc = btreeDropTable(p, iTable, piMoved); 9775 sqlite3BtreeLeave(p); 9776 return rc; 9777 } 9778 9779 9780 /* 9781 ** This function may only be called if the b-tree connection already 9782 ** has a read or write transaction open on the database. 9783 ** 9784 ** Read the meta-information out of a database file. Meta[0] 9785 ** is the number of free pages currently in the database. Meta[1] 9786 ** through meta[15] are available for use by higher layers. Meta[0] 9787 ** is read-only, the others are read/write. 9788 ** 9789 ** The schema layer numbers meta values differently. At the schema 9790 ** layer (and the SetCookie and ReadCookie opcodes) the number of 9791 ** free pages is not visible. So Cookie[0] is the same as Meta[1]. 9792 ** 9793 ** This routine treats Meta[BTREE_DATA_VERSION] as a special case. Instead 9794 ** of reading the value out of the header, it instead loads the "DataVersion" 9795 ** from the pager. The BTREE_DATA_VERSION value is not actually stored in the 9796 ** database file. It is a number computed by the pager. But its access 9797 ** pattern is the same as header meta values, and so it is convenient to 9798 ** read it from this routine. 9799 */ 9800 void sqlite3BtreeGetMeta(Btree *p, int idx, u32 *pMeta){ 9801 BtShared *pBt = p->pBt; 9802 9803 sqlite3BtreeEnter(p); 9804 assert( p->inTrans>TRANS_NONE ); 9805 assert( SQLITE_OK==querySharedCacheTableLock(p, SCHEMA_ROOT, READ_LOCK) ); 9806 assert( pBt->pPage1 ); 9807 assert( idx>=0 && idx<=15 ); 9808 9809 if( idx==BTREE_DATA_VERSION ){ 9810 *pMeta = sqlite3PagerDataVersion(pBt->pPager) + p->iBDataVersion; 9811 }else{ 9812 *pMeta = get4byte(&pBt->pPage1->aData[36 + idx*4]); 9813 } 9814 9815 /* If auto-vacuum is disabled in this build and this is an auto-vacuum 9816 ** database, mark the database as read-only. */ 9817 #ifdef SQLITE_OMIT_AUTOVACUUM 9818 if( idx==BTREE_LARGEST_ROOT_PAGE && *pMeta>0 ){ 9819 pBt->btsFlags |= BTS_READ_ONLY; 9820 } 9821 #endif 9822 9823 sqlite3BtreeLeave(p); 9824 } 9825 9826 /* 9827 ** Write meta-information back into the database. Meta[0] is 9828 ** read-only and may not be written. 9829 */ 9830 int sqlite3BtreeUpdateMeta(Btree *p, int idx, u32 iMeta){ 9831 BtShared *pBt = p->pBt; 9832 unsigned char *pP1; 9833 int rc; 9834 assert( idx>=1 && idx<=15 ); 9835 sqlite3BtreeEnter(p); 9836 assert( p->inTrans==TRANS_WRITE ); 9837 assert( pBt->pPage1!=0 ); 9838 pP1 = pBt->pPage1->aData; 9839 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); 9840 if( rc==SQLITE_OK ){ 9841 put4byte(&pP1[36 + idx*4], iMeta); 9842 #ifndef SQLITE_OMIT_AUTOVACUUM 9843 if( idx==BTREE_INCR_VACUUM ){ 9844 assert( pBt->autoVacuum || iMeta==0 ); 9845 assert( iMeta==0 || iMeta==1 ); 9846 pBt->incrVacuum = (u8)iMeta; 9847 } 9848 #endif 9849 } 9850 sqlite3BtreeLeave(p); 9851 return rc; 9852 } 9853 9854 /* 9855 ** The first argument, pCur, is a cursor opened on some b-tree. Count the 9856 ** number of entries in the b-tree and write the result to *pnEntry. 9857 ** 9858 ** SQLITE_OK is returned if the operation is successfully executed. 9859 ** Otherwise, if an error is encountered (i.e. an IO error or database 9860 ** corruption) an SQLite error code is returned. 9861 */ 9862 int sqlite3BtreeCount(sqlite3 *db, BtCursor *pCur, i64 *pnEntry){ 9863 i64 nEntry = 0; /* Value to return in *pnEntry */ 9864 int rc; /* Return code */ 9865 9866 rc = moveToRoot(pCur); 9867 if( rc==SQLITE_EMPTY ){ 9868 *pnEntry = 0; 9869 return SQLITE_OK; 9870 } 9871 9872 /* Unless an error occurs, the following loop runs one iteration for each 9873 ** page in the B-Tree structure (not including overflow pages). 9874 */ 9875 while( rc==SQLITE_OK && !AtomicLoad(&db->u1.isInterrupted) ){ 9876 int iIdx; /* Index of child node in parent */ 9877 MemPage *pPage; /* Current page of the b-tree */ 9878 9879 /* If this is a leaf page or the tree is not an int-key tree, then 9880 ** this page contains countable entries. Increment the entry counter 9881 ** accordingly. 9882 */ 9883 pPage = pCur->pPage; 9884 if( pPage->leaf || !pPage->intKey ){ 9885 nEntry += pPage->nCell; 9886 } 9887 9888 /* pPage is a leaf node. This loop navigates the cursor so that it 9889 ** points to the first interior cell that it points to the parent of 9890 ** the next page in the tree that has not yet been visited. The 9891 ** pCur->aiIdx[pCur->iPage] value is set to the index of the parent cell 9892 ** of the page, or to the number of cells in the page if the next page 9893 ** to visit is the right-child of its parent. 9894 ** 9895 ** If all pages in the tree have been visited, return SQLITE_OK to the 9896 ** caller. 9897 */ 9898 if( pPage->leaf ){ 9899 do { 9900 if( pCur->iPage==0 ){ 9901 /* All pages of the b-tree have been visited. Return successfully. */ 9902 *pnEntry = nEntry; 9903 return moveToRoot(pCur); 9904 } 9905 moveToParent(pCur); 9906 }while ( pCur->ix>=pCur->pPage->nCell ); 9907 9908 pCur->ix++; 9909 pPage = pCur->pPage; 9910 } 9911 9912 /* Descend to the child node of the cell that the cursor currently 9913 ** points at. This is the right-child if (iIdx==pPage->nCell). 9914 */ 9915 iIdx = pCur->ix; 9916 if( iIdx==pPage->nCell ){ 9917 rc = moveToChild(pCur, get4byte(&pPage->aData[pPage->hdrOffset+8])); 9918 }else{ 9919 rc = moveToChild(pCur, get4byte(findCell(pPage, iIdx))); 9920 } 9921 } 9922 9923 /* An error has occurred. Return an error code. */ 9924 return rc; 9925 } 9926 9927 /* 9928 ** Return the pager associated with a BTree. This routine is used for 9929 ** testing and debugging only. 9930 */ 9931 Pager *sqlite3BtreePager(Btree *p){ 9932 return p->pBt->pPager; 9933 } 9934 9935 #ifndef SQLITE_OMIT_INTEGRITY_CHECK 9936 /* 9937 ** Append a message to the error message string. 9938 */ 9939 static void checkAppendMsg( 9940 IntegrityCk *pCheck, 9941 const char *zFormat, 9942 ... 9943 ){ 9944 va_list ap; 9945 if( !pCheck->mxErr ) return; 9946 pCheck->mxErr--; 9947 pCheck->nErr++; 9948 va_start(ap, zFormat); 9949 if( pCheck->errMsg.nChar ){ 9950 sqlite3_str_append(&pCheck->errMsg, "\n", 1); 9951 } 9952 if( pCheck->zPfx ){ 9953 sqlite3_str_appendf(&pCheck->errMsg, pCheck->zPfx, pCheck->v1, pCheck->v2); 9954 } 9955 sqlite3_str_vappendf(&pCheck->errMsg, zFormat, ap); 9956 va_end(ap); 9957 if( pCheck->errMsg.accError==SQLITE_NOMEM ){ 9958 pCheck->bOomFault = 1; 9959 } 9960 } 9961 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ 9962 9963 #ifndef SQLITE_OMIT_INTEGRITY_CHECK 9964 9965 /* 9966 ** Return non-zero if the bit in the IntegrityCk.aPgRef[] array that 9967 ** corresponds to page iPg is already set. 9968 */ 9969 static int getPageReferenced(IntegrityCk *pCheck, Pgno iPg){ 9970 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 ); 9971 return (pCheck->aPgRef[iPg/8] & (1 << (iPg & 0x07))); 9972 } 9973 9974 /* 9975 ** Set the bit in the IntegrityCk.aPgRef[] array that corresponds to page iPg. 9976 */ 9977 static void setPageReferenced(IntegrityCk *pCheck, Pgno iPg){ 9978 assert( iPg<=pCheck->nPage && sizeof(pCheck->aPgRef[0])==1 ); 9979 pCheck->aPgRef[iPg/8] |= (1 << (iPg & 0x07)); 9980 } 9981 9982 9983 /* 9984 ** Add 1 to the reference count for page iPage. If this is the second 9985 ** reference to the page, add an error message to pCheck->zErrMsg. 9986 ** Return 1 if there are 2 or more references to the page and 0 if 9987 ** if this is the first reference to the page. 9988 ** 9989 ** Also check that the page number is in bounds. 9990 */ 9991 static int checkRef(IntegrityCk *pCheck, Pgno iPage){ 9992 if( iPage>pCheck->nPage || iPage==0 ){ 9993 checkAppendMsg(pCheck, "invalid page number %d", iPage); 9994 return 1; 9995 } 9996 if( getPageReferenced(pCheck, iPage) ){ 9997 checkAppendMsg(pCheck, "2nd reference to page %d", iPage); 9998 return 1; 9999 } 10000 if( AtomicLoad(&pCheck->db->u1.isInterrupted) ) return 1; 10001 setPageReferenced(pCheck, iPage); 10002 return 0; 10003 } 10004 10005 #ifndef SQLITE_OMIT_AUTOVACUUM 10006 /* 10007 ** Check that the entry in the pointer-map for page iChild maps to 10008 ** page iParent, pointer type ptrType. If not, append an error message 10009 ** to pCheck. 10010 */ 10011 static void checkPtrmap( 10012 IntegrityCk *pCheck, /* Integrity check context */ 10013 Pgno iChild, /* Child page number */ 10014 u8 eType, /* Expected pointer map type */ 10015 Pgno iParent /* Expected pointer map parent page number */ 10016 ){ 10017 int rc; 10018 u8 ePtrmapType; 10019 Pgno iPtrmapParent; 10020 10021 rc = ptrmapGet(pCheck->pBt, iChild, &ePtrmapType, &iPtrmapParent); 10022 if( rc!=SQLITE_OK ){ 10023 if( rc==SQLITE_NOMEM || rc==SQLITE_IOERR_NOMEM ) pCheck->bOomFault = 1; 10024 checkAppendMsg(pCheck, "Failed to read ptrmap key=%d", iChild); 10025 return; 10026 } 10027 10028 if( ePtrmapType!=eType || iPtrmapParent!=iParent ){ 10029 checkAppendMsg(pCheck, 10030 "Bad ptr map entry key=%d expected=(%d,%d) got=(%d,%d)", 10031 iChild, eType, iParent, ePtrmapType, iPtrmapParent); 10032 } 10033 } 10034 #endif 10035 10036 /* 10037 ** Check the integrity of the freelist or of an overflow page list. 10038 ** Verify that the number of pages on the list is N. 10039 */ 10040 static void checkList( 10041 IntegrityCk *pCheck, /* Integrity checking context */ 10042 int isFreeList, /* True for a freelist. False for overflow page list */ 10043 Pgno iPage, /* Page number for first page in the list */ 10044 u32 N /* Expected number of pages in the list */ 10045 ){ 10046 int i; 10047 u32 expected = N; 10048 int nErrAtStart = pCheck->nErr; 10049 while( iPage!=0 && pCheck->mxErr ){ 10050 DbPage *pOvflPage; 10051 unsigned char *pOvflData; 10052 if( checkRef(pCheck, iPage) ) break; 10053 N--; 10054 if( sqlite3PagerGet(pCheck->pPager, (Pgno)iPage, &pOvflPage, 0) ){ 10055 checkAppendMsg(pCheck, "failed to get page %d", iPage); 10056 break; 10057 } 10058 pOvflData = (unsigned char *)sqlite3PagerGetData(pOvflPage); 10059 if( isFreeList ){ 10060 u32 n = (u32)get4byte(&pOvflData[4]); 10061 #ifndef SQLITE_OMIT_AUTOVACUUM 10062 if( pCheck->pBt->autoVacuum ){ 10063 checkPtrmap(pCheck, iPage, PTRMAP_FREEPAGE, 0); 10064 } 10065 #endif 10066 if( n>pCheck->pBt->usableSize/4-2 ){ 10067 checkAppendMsg(pCheck, 10068 "freelist leaf count too big on page %d", iPage); 10069 N--; 10070 }else{ 10071 for(i=0; i<(int)n; i++){ 10072 Pgno iFreePage = get4byte(&pOvflData[8+i*4]); 10073 #ifndef SQLITE_OMIT_AUTOVACUUM 10074 if( pCheck->pBt->autoVacuum ){ 10075 checkPtrmap(pCheck, iFreePage, PTRMAP_FREEPAGE, 0); 10076 } 10077 #endif 10078 checkRef(pCheck, iFreePage); 10079 } 10080 N -= n; 10081 } 10082 } 10083 #ifndef SQLITE_OMIT_AUTOVACUUM 10084 else{ 10085 /* If this database supports auto-vacuum and iPage is not the last 10086 ** page in this overflow list, check that the pointer-map entry for 10087 ** the following page matches iPage. 10088 */ 10089 if( pCheck->pBt->autoVacuum && N>0 ){ 10090 i = get4byte(pOvflData); 10091 checkPtrmap(pCheck, i, PTRMAP_OVERFLOW2, iPage); 10092 } 10093 } 10094 #endif 10095 iPage = get4byte(pOvflData); 10096 sqlite3PagerUnref(pOvflPage); 10097 } 10098 if( N && nErrAtStart==pCheck->nErr ){ 10099 checkAppendMsg(pCheck, 10100 "%s is %d but should be %d", 10101 isFreeList ? "size" : "overflow list length", 10102 expected-N, expected); 10103 } 10104 } 10105 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ 10106 10107 /* 10108 ** An implementation of a min-heap. 10109 ** 10110 ** aHeap[0] is the number of elements on the heap. aHeap[1] is the 10111 ** root element. The daughter nodes of aHeap[N] are aHeap[N*2] 10112 ** and aHeap[N*2+1]. 10113 ** 10114 ** The heap property is this: Every node is less than or equal to both 10115 ** of its daughter nodes. A consequence of the heap property is that the 10116 ** root node aHeap[1] is always the minimum value currently in the heap. 10117 ** 10118 ** The btreeHeapInsert() routine inserts an unsigned 32-bit number onto 10119 ** the heap, preserving the heap property. The btreeHeapPull() routine 10120 ** removes the root element from the heap (the minimum value in the heap) 10121 ** and then moves other nodes around as necessary to preserve the heap 10122 ** property. 10123 ** 10124 ** This heap is used for cell overlap and coverage testing. Each u32 10125 ** entry represents the span of a cell or freeblock on a btree page. 10126 ** The upper 16 bits are the index of the first byte of a range and the 10127 ** lower 16 bits are the index of the last byte of that range. 10128 */ 10129 static void btreeHeapInsert(u32 *aHeap, u32 x){ 10130 u32 j, i = ++aHeap[0]; 10131 aHeap[i] = x; 10132 while( (j = i/2)>0 && aHeap[j]>aHeap[i] ){ 10133 x = aHeap[j]; 10134 aHeap[j] = aHeap[i]; 10135 aHeap[i] = x; 10136 i = j; 10137 } 10138 } 10139 static int btreeHeapPull(u32 *aHeap, u32 *pOut){ 10140 u32 j, i, x; 10141 if( (x = aHeap[0])==0 ) return 0; 10142 *pOut = aHeap[1]; 10143 aHeap[1] = aHeap[x]; 10144 aHeap[x] = 0xffffffff; 10145 aHeap[0]--; 10146 i = 1; 10147 while( (j = i*2)<=aHeap[0] ){ 10148 if( aHeap[j]>aHeap[j+1] ) j++; 10149 if( aHeap[i]<aHeap[j] ) break; 10150 x = aHeap[i]; 10151 aHeap[i] = aHeap[j]; 10152 aHeap[j] = x; 10153 i = j; 10154 } 10155 return 1; 10156 } 10157 10158 #ifndef SQLITE_OMIT_INTEGRITY_CHECK 10159 /* 10160 ** Do various sanity checks on a single page of a tree. Return 10161 ** the tree depth. Root pages return 0. Parents of root pages 10162 ** return 1, and so forth. 10163 ** 10164 ** These checks are done: 10165 ** 10166 ** 1. Make sure that cells and freeblocks do not overlap 10167 ** but combine to completely cover the page. 10168 ** 2. Make sure integer cell keys are in order. 10169 ** 3. Check the integrity of overflow pages. 10170 ** 4. Recursively call checkTreePage on all children. 10171 ** 5. Verify that the depth of all children is the same. 10172 */ 10173 static int checkTreePage( 10174 IntegrityCk *pCheck, /* Context for the sanity check */ 10175 Pgno iPage, /* Page number of the page to check */ 10176 i64 *piMinKey, /* Write minimum integer primary key here */ 10177 i64 maxKey /* Error if integer primary key greater than this */ 10178 ){ 10179 MemPage *pPage = 0; /* The page being analyzed */ 10180 int i; /* Loop counter */ 10181 int rc; /* Result code from subroutine call */ 10182 int depth = -1, d2; /* Depth of a subtree */ 10183 int pgno; /* Page number */ 10184 int nFrag; /* Number of fragmented bytes on the page */ 10185 int hdr; /* Offset to the page header */ 10186 int cellStart; /* Offset to the start of the cell pointer array */ 10187 int nCell; /* Number of cells */ 10188 int doCoverageCheck = 1; /* True if cell coverage checking should be done */ 10189 int keyCanBeEqual = 1; /* True if IPK can be equal to maxKey 10190 ** False if IPK must be strictly less than maxKey */ 10191 u8 *data; /* Page content */ 10192 u8 *pCell; /* Cell content */ 10193 u8 *pCellIdx; /* Next element of the cell pointer array */ 10194 BtShared *pBt; /* The BtShared object that owns pPage */ 10195 u32 pc; /* Address of a cell */ 10196 u32 usableSize; /* Usable size of the page */ 10197 u32 contentOffset; /* Offset to the start of the cell content area */ 10198 u32 *heap = 0; /* Min-heap used for checking cell coverage */ 10199 u32 x, prev = 0; /* Next and previous entry on the min-heap */ 10200 const char *saved_zPfx = pCheck->zPfx; 10201 int saved_v1 = pCheck->v1; 10202 int saved_v2 = pCheck->v2; 10203 u8 savedIsInit = 0; 10204 10205 /* Check that the page exists 10206 */ 10207 pBt = pCheck->pBt; 10208 usableSize = pBt->usableSize; 10209 if( iPage==0 ) return 0; 10210 if( checkRef(pCheck, iPage) ) return 0; 10211 pCheck->zPfx = "Page %u: "; 10212 pCheck->v1 = iPage; 10213 if( (rc = btreeGetPage(pBt, iPage, &pPage, 0))!=0 ){ 10214 checkAppendMsg(pCheck, 10215 "unable to get the page. error code=%d", rc); 10216 goto end_of_check; 10217 } 10218 10219 /* Clear MemPage.isInit to make sure the corruption detection code in 10220 ** btreeInitPage() is executed. */ 10221 savedIsInit = pPage->isInit; 10222 pPage->isInit = 0; 10223 if( (rc = btreeInitPage(pPage))!=0 ){ 10224 assert( rc==SQLITE_CORRUPT ); /* The only possible error from InitPage */ 10225 checkAppendMsg(pCheck, 10226 "btreeInitPage() returns error code %d", rc); 10227 goto end_of_check; 10228 } 10229 if( (rc = btreeComputeFreeSpace(pPage))!=0 ){ 10230 assert( rc==SQLITE_CORRUPT ); 10231 checkAppendMsg(pCheck, "free space corruption", rc); 10232 goto end_of_check; 10233 } 10234 data = pPage->aData; 10235 hdr = pPage->hdrOffset; 10236 10237 /* Set up for cell analysis */ 10238 pCheck->zPfx = "On tree page %u cell %d: "; 10239 contentOffset = get2byteNotZero(&data[hdr+5]); 10240 assert( contentOffset<=usableSize ); /* Enforced by btreeInitPage() */ 10241 10242 /* EVIDENCE-OF: R-37002-32774 The two-byte integer at offset 3 gives the 10243 ** number of cells on the page. */ 10244 nCell = get2byte(&data[hdr+3]); 10245 assert( pPage->nCell==nCell ); 10246 10247 /* EVIDENCE-OF: R-23882-45353 The cell pointer array of a b-tree page 10248 ** immediately follows the b-tree page header. */ 10249 cellStart = hdr + 12 - 4*pPage->leaf; 10250 assert( pPage->aCellIdx==&data[cellStart] ); 10251 pCellIdx = &data[cellStart + 2*(nCell-1)]; 10252 10253 if( !pPage->leaf ){ 10254 /* Analyze the right-child page of internal pages */ 10255 pgno = get4byte(&data[hdr+8]); 10256 #ifndef SQLITE_OMIT_AUTOVACUUM 10257 if( pBt->autoVacuum ){ 10258 pCheck->zPfx = "On page %u at right child: "; 10259 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage); 10260 } 10261 #endif 10262 depth = checkTreePage(pCheck, pgno, &maxKey, maxKey); 10263 keyCanBeEqual = 0; 10264 }else{ 10265 /* For leaf pages, the coverage check will occur in the same loop 10266 ** as the other cell checks, so initialize the heap. */ 10267 heap = pCheck->heap; 10268 heap[0] = 0; 10269 } 10270 10271 /* EVIDENCE-OF: R-02776-14802 The cell pointer array consists of K 2-byte 10272 ** integer offsets to the cell contents. */ 10273 for(i=nCell-1; i>=0 && pCheck->mxErr; i--){ 10274 CellInfo info; 10275 10276 /* Check cell size */ 10277 pCheck->v2 = i; 10278 assert( pCellIdx==&data[cellStart + i*2] ); 10279 pc = get2byteAligned(pCellIdx); 10280 pCellIdx -= 2; 10281 if( pc<contentOffset || pc>usableSize-4 ){ 10282 checkAppendMsg(pCheck, "Offset %d out of range %d..%d", 10283 pc, contentOffset, usableSize-4); 10284 doCoverageCheck = 0; 10285 continue; 10286 } 10287 pCell = &data[pc]; 10288 pPage->xParseCell(pPage, pCell, &info); 10289 if( pc+info.nSize>usableSize ){ 10290 checkAppendMsg(pCheck, "Extends off end of page"); 10291 doCoverageCheck = 0; 10292 continue; 10293 } 10294 10295 /* Check for integer primary key out of range */ 10296 if( pPage->intKey ){ 10297 if( keyCanBeEqual ? (info.nKey > maxKey) : (info.nKey >= maxKey) ){ 10298 checkAppendMsg(pCheck, "Rowid %lld out of order", info.nKey); 10299 } 10300 maxKey = info.nKey; 10301 keyCanBeEqual = 0; /* Only the first key on the page may ==maxKey */ 10302 } 10303 10304 /* Check the content overflow list */ 10305 if( info.nPayload>info.nLocal ){ 10306 u32 nPage; /* Number of pages on the overflow chain */ 10307 Pgno pgnoOvfl; /* First page of the overflow chain */ 10308 assert( pc + info.nSize - 4 <= usableSize ); 10309 nPage = (info.nPayload - info.nLocal + usableSize - 5)/(usableSize - 4); 10310 pgnoOvfl = get4byte(&pCell[info.nSize - 4]); 10311 #ifndef SQLITE_OMIT_AUTOVACUUM 10312 if( pBt->autoVacuum ){ 10313 checkPtrmap(pCheck, pgnoOvfl, PTRMAP_OVERFLOW1, iPage); 10314 } 10315 #endif 10316 checkList(pCheck, 0, pgnoOvfl, nPage); 10317 } 10318 10319 if( !pPage->leaf ){ 10320 /* Check sanity of left child page for internal pages */ 10321 pgno = get4byte(pCell); 10322 #ifndef SQLITE_OMIT_AUTOVACUUM 10323 if( pBt->autoVacuum ){ 10324 checkPtrmap(pCheck, pgno, PTRMAP_BTREE, iPage); 10325 } 10326 #endif 10327 d2 = checkTreePage(pCheck, pgno, &maxKey, maxKey); 10328 keyCanBeEqual = 0; 10329 if( d2!=depth ){ 10330 checkAppendMsg(pCheck, "Child page depth differs"); 10331 depth = d2; 10332 } 10333 }else{ 10334 /* Populate the coverage-checking heap for leaf pages */ 10335 btreeHeapInsert(heap, (pc<<16)|(pc+info.nSize-1)); 10336 } 10337 } 10338 *piMinKey = maxKey; 10339 10340 /* Check for complete coverage of the page 10341 */ 10342 pCheck->zPfx = 0; 10343 if( doCoverageCheck && pCheck->mxErr>0 ){ 10344 /* For leaf pages, the min-heap has already been initialized and the 10345 ** cells have already been inserted. But for internal pages, that has 10346 ** not yet been done, so do it now */ 10347 if( !pPage->leaf ){ 10348 heap = pCheck->heap; 10349 heap[0] = 0; 10350 for(i=nCell-1; i>=0; i--){ 10351 u32 size; 10352 pc = get2byteAligned(&data[cellStart+i*2]); 10353 size = pPage->xCellSize(pPage, &data[pc]); 10354 btreeHeapInsert(heap, (pc<<16)|(pc+size-1)); 10355 } 10356 } 10357 /* Add the freeblocks to the min-heap 10358 ** 10359 ** EVIDENCE-OF: R-20690-50594 The second field of the b-tree page header 10360 ** is the offset of the first freeblock, or zero if there are no 10361 ** freeblocks on the page. 10362 */ 10363 i = get2byte(&data[hdr+1]); 10364 while( i>0 ){ 10365 int size, j; 10366 assert( (u32)i<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */ 10367 size = get2byte(&data[i+2]); 10368 assert( (u32)(i+size)<=usableSize ); /* due to btreeComputeFreeSpace() */ 10369 btreeHeapInsert(heap, (((u32)i)<<16)|(i+size-1)); 10370 /* EVIDENCE-OF: R-58208-19414 The first 2 bytes of a freeblock are a 10371 ** big-endian integer which is the offset in the b-tree page of the next 10372 ** freeblock in the chain, or zero if the freeblock is the last on the 10373 ** chain. */ 10374 j = get2byte(&data[i]); 10375 /* EVIDENCE-OF: R-06866-39125 Freeblocks are always connected in order of 10376 ** increasing offset. */ 10377 assert( j==0 || j>i+size ); /* Enforced by btreeComputeFreeSpace() */ 10378 assert( (u32)j<=usableSize-4 ); /* Enforced by btreeComputeFreeSpace() */ 10379 i = j; 10380 } 10381 /* Analyze the min-heap looking for overlap between cells and/or 10382 ** freeblocks, and counting the number of untracked bytes in nFrag. 10383 ** 10384 ** Each min-heap entry is of the form: (start_address<<16)|end_address. 10385 ** There is an implied first entry the covers the page header, the cell 10386 ** pointer index, and the gap between the cell pointer index and the start 10387 ** of cell content. 10388 ** 10389 ** The loop below pulls entries from the min-heap in order and compares 10390 ** the start_address against the previous end_address. If there is an 10391 ** overlap, that means bytes are used multiple times. If there is a gap, 10392 ** that gap is added to the fragmentation count. 10393 */ 10394 nFrag = 0; 10395 prev = contentOffset - 1; /* Implied first min-heap entry */ 10396 while( btreeHeapPull(heap,&x) ){ 10397 if( (prev&0xffff)>=(x>>16) ){ 10398 checkAppendMsg(pCheck, 10399 "Multiple uses for byte %u of page %u", x>>16, iPage); 10400 break; 10401 }else{ 10402 nFrag += (x>>16) - (prev&0xffff) - 1; 10403 prev = x; 10404 } 10405 } 10406 nFrag += usableSize - (prev&0xffff) - 1; 10407 /* EVIDENCE-OF: R-43263-13491 The total number of bytes in all fragments 10408 ** is stored in the fifth field of the b-tree page header. 10409 ** EVIDENCE-OF: R-07161-27322 The one-byte integer at offset 7 gives the 10410 ** number of fragmented free bytes within the cell content area. 10411 */ 10412 if( heap[0]==0 && nFrag!=data[hdr+7] ){ 10413 checkAppendMsg(pCheck, 10414 "Fragmentation of %d bytes reported as %d on page %u", 10415 nFrag, data[hdr+7], iPage); 10416 } 10417 } 10418 10419 end_of_check: 10420 if( !doCoverageCheck ) pPage->isInit = savedIsInit; 10421 releasePage(pPage); 10422 pCheck->zPfx = saved_zPfx; 10423 pCheck->v1 = saved_v1; 10424 pCheck->v2 = saved_v2; 10425 return depth+1; 10426 } 10427 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ 10428 10429 #ifndef SQLITE_OMIT_INTEGRITY_CHECK 10430 /* 10431 ** This routine does a complete check of the given BTree file. aRoot[] is 10432 ** an array of pages numbers were each page number is the root page of 10433 ** a table. nRoot is the number of entries in aRoot. 10434 ** 10435 ** A read-only or read-write transaction must be opened before calling 10436 ** this function. 10437 ** 10438 ** Write the number of error seen in *pnErr. Except for some memory 10439 ** allocation errors, an error message held in memory obtained from 10440 ** malloc is returned if *pnErr is non-zero. If *pnErr==0 then NULL is 10441 ** returned. If a memory allocation error occurs, NULL is returned. 10442 ** 10443 ** If the first entry in aRoot[] is 0, that indicates that the list of 10444 ** root pages is incomplete. This is a "partial integrity-check". This 10445 ** happens when performing an integrity check on a single table. The 10446 ** zero is skipped, of course. But in addition, the freelist checks 10447 ** and the checks to make sure every page is referenced are also skipped, 10448 ** since obviously it is not possible to know which pages are covered by 10449 ** the unverified btrees. Except, if aRoot[1] is 1, then the freelist 10450 ** checks are still performed. 10451 */ 10452 char *sqlite3BtreeIntegrityCheck( 10453 sqlite3 *db, /* Database connection that is running the check */ 10454 Btree *p, /* The btree to be checked */ 10455 Pgno *aRoot, /* An array of root pages numbers for individual trees */ 10456 int nRoot, /* Number of entries in aRoot[] */ 10457 int mxErr, /* Stop reporting errors after this many */ 10458 int *pnErr /* Write number of errors seen to this variable */ 10459 ){ 10460 Pgno i; 10461 IntegrityCk sCheck; 10462 BtShared *pBt = p->pBt; 10463 u64 savedDbFlags = pBt->db->flags; 10464 char zErr[100]; 10465 int bPartial = 0; /* True if not checking all btrees */ 10466 int bCkFreelist = 1; /* True to scan the freelist */ 10467 VVA_ONLY( int nRef ); 10468 assert( nRoot>0 ); 10469 10470 /* aRoot[0]==0 means this is a partial check */ 10471 if( aRoot[0]==0 ){ 10472 assert( nRoot>1 ); 10473 bPartial = 1; 10474 if( aRoot[1]!=1 ) bCkFreelist = 0; 10475 } 10476 10477 sqlite3BtreeEnter(p); 10478 assert( p->inTrans>TRANS_NONE && pBt->inTransaction>TRANS_NONE ); 10479 VVA_ONLY( nRef = sqlite3PagerRefcount(pBt->pPager) ); 10480 assert( nRef>=0 ); 10481 sCheck.db = db; 10482 sCheck.pBt = pBt; 10483 sCheck.pPager = pBt->pPager; 10484 sCheck.nPage = btreePagecount(sCheck.pBt); 10485 sCheck.mxErr = mxErr; 10486 sCheck.nErr = 0; 10487 sCheck.bOomFault = 0; 10488 sCheck.zPfx = 0; 10489 sCheck.v1 = 0; 10490 sCheck.v2 = 0; 10491 sCheck.aPgRef = 0; 10492 sCheck.heap = 0; 10493 sqlite3StrAccumInit(&sCheck.errMsg, 0, zErr, sizeof(zErr), SQLITE_MAX_LENGTH); 10494 sCheck.errMsg.printfFlags = SQLITE_PRINTF_INTERNAL; 10495 if( sCheck.nPage==0 ){ 10496 goto integrity_ck_cleanup; 10497 } 10498 10499 sCheck.aPgRef = sqlite3MallocZero((sCheck.nPage / 8)+ 1); 10500 if( !sCheck.aPgRef ){ 10501 sCheck.bOomFault = 1; 10502 goto integrity_ck_cleanup; 10503 } 10504 sCheck.heap = (u32*)sqlite3PageMalloc( pBt->pageSize ); 10505 if( sCheck.heap==0 ){ 10506 sCheck.bOomFault = 1; 10507 goto integrity_ck_cleanup; 10508 } 10509 10510 i = PENDING_BYTE_PAGE(pBt); 10511 if( i<=sCheck.nPage ) setPageReferenced(&sCheck, i); 10512 10513 /* Check the integrity of the freelist 10514 */ 10515 if( bCkFreelist ){ 10516 sCheck.zPfx = "Main freelist: "; 10517 checkList(&sCheck, 1, get4byte(&pBt->pPage1->aData[32]), 10518 get4byte(&pBt->pPage1->aData[36])); 10519 sCheck.zPfx = 0; 10520 } 10521 10522 /* Check all the tables. 10523 */ 10524 #ifndef SQLITE_OMIT_AUTOVACUUM 10525 if( !bPartial ){ 10526 if( pBt->autoVacuum ){ 10527 Pgno mx = 0; 10528 Pgno mxInHdr; 10529 for(i=0; (int)i<nRoot; i++) if( mx<aRoot[i] ) mx = aRoot[i]; 10530 mxInHdr = get4byte(&pBt->pPage1->aData[52]); 10531 if( mx!=mxInHdr ){ 10532 checkAppendMsg(&sCheck, 10533 "max rootpage (%d) disagrees with header (%d)", 10534 mx, mxInHdr 10535 ); 10536 } 10537 }else if( get4byte(&pBt->pPage1->aData[64])!=0 ){ 10538 checkAppendMsg(&sCheck, 10539 "incremental_vacuum enabled with a max rootpage of zero" 10540 ); 10541 } 10542 } 10543 #endif 10544 testcase( pBt->db->flags & SQLITE_CellSizeCk ); 10545 pBt->db->flags &= ~(u64)SQLITE_CellSizeCk; 10546 for(i=0; (int)i<nRoot && sCheck.mxErr; i++){ 10547 i64 notUsed; 10548 if( aRoot[i]==0 ) continue; 10549 #ifndef SQLITE_OMIT_AUTOVACUUM 10550 if( pBt->autoVacuum && aRoot[i]>1 && !bPartial ){ 10551 checkPtrmap(&sCheck, aRoot[i], PTRMAP_ROOTPAGE, 0); 10552 } 10553 #endif 10554 checkTreePage(&sCheck, aRoot[i], ¬Used, LARGEST_INT64); 10555 } 10556 pBt->db->flags = savedDbFlags; 10557 10558 /* Make sure every page in the file is referenced 10559 */ 10560 if( !bPartial ){ 10561 for(i=1; i<=sCheck.nPage && sCheck.mxErr; i++){ 10562 #ifdef SQLITE_OMIT_AUTOVACUUM 10563 if( getPageReferenced(&sCheck, i)==0 ){ 10564 checkAppendMsg(&sCheck, "Page %d is never used", i); 10565 } 10566 #else 10567 /* If the database supports auto-vacuum, make sure no tables contain 10568 ** references to pointer-map pages. 10569 */ 10570 if( getPageReferenced(&sCheck, i)==0 && 10571 (PTRMAP_PAGENO(pBt, i)!=i || !pBt->autoVacuum) ){ 10572 checkAppendMsg(&sCheck, "Page %d is never used", i); 10573 } 10574 if( getPageReferenced(&sCheck, i)!=0 && 10575 (PTRMAP_PAGENO(pBt, i)==i && pBt->autoVacuum) ){ 10576 checkAppendMsg(&sCheck, "Pointer map page %d is referenced", i); 10577 } 10578 #endif 10579 } 10580 } 10581 10582 /* Clean up and report errors. 10583 */ 10584 integrity_ck_cleanup: 10585 sqlite3PageFree(sCheck.heap); 10586 sqlite3_free(sCheck.aPgRef); 10587 if( sCheck.bOomFault ){ 10588 sqlite3_str_reset(&sCheck.errMsg); 10589 sCheck.nErr++; 10590 } 10591 *pnErr = sCheck.nErr; 10592 if( sCheck.nErr==0 ) sqlite3_str_reset(&sCheck.errMsg); 10593 /* Make sure this analysis did not leave any unref() pages. */ 10594 assert( nRef==sqlite3PagerRefcount(pBt->pPager) ); 10595 sqlite3BtreeLeave(p); 10596 return sqlite3StrAccumFinish(&sCheck.errMsg); 10597 } 10598 #endif /* SQLITE_OMIT_INTEGRITY_CHECK */ 10599 10600 /* 10601 ** Return the full pathname of the underlying database file. Return 10602 ** an empty string if the database is in-memory or a TEMP database. 10603 ** 10604 ** The pager filename is invariant as long as the pager is 10605 ** open so it is safe to access without the BtShared mutex. 10606 */ 10607 const char *sqlite3BtreeGetFilename(Btree *p){ 10608 assert( p->pBt->pPager!=0 ); 10609 return sqlite3PagerFilename(p->pBt->pPager, 1); 10610 } 10611 10612 /* 10613 ** Return the pathname of the journal file for this database. The return 10614 ** value of this routine is the same regardless of whether the journal file 10615 ** has been created or not. 10616 ** 10617 ** The pager journal filename is invariant as long as the pager is 10618 ** open so it is safe to access without the BtShared mutex. 10619 */ 10620 const char *sqlite3BtreeGetJournalname(Btree *p){ 10621 assert( p->pBt->pPager!=0 ); 10622 return sqlite3PagerJournalname(p->pBt->pPager); 10623 } 10624 10625 /* 10626 ** Return one of SQLITE_TXN_NONE, SQLITE_TXN_READ, or SQLITE_TXN_WRITE 10627 ** to describe the current transaction state of Btree p. 10628 */ 10629 int sqlite3BtreeTxnState(Btree *p){ 10630 assert( p==0 || sqlite3_mutex_held(p->db->mutex) ); 10631 return p ? p->inTrans : 0; 10632 } 10633 10634 #ifndef SQLITE_OMIT_WAL 10635 /* 10636 ** Run a checkpoint on the Btree passed as the first argument. 10637 ** 10638 ** Return SQLITE_LOCKED if this or any other connection has an open 10639 ** transaction on the shared-cache the argument Btree is connected to. 10640 ** 10641 ** Parameter eMode is one of SQLITE_CHECKPOINT_PASSIVE, FULL or RESTART. 10642 */ 10643 int sqlite3BtreeCheckpoint(Btree *p, int eMode, int *pnLog, int *pnCkpt){ 10644 int rc = SQLITE_OK; 10645 if( p ){ 10646 BtShared *pBt = p->pBt; 10647 sqlite3BtreeEnter(p); 10648 if( pBt->inTransaction!=TRANS_NONE ){ 10649 rc = SQLITE_LOCKED; 10650 }else{ 10651 rc = sqlite3PagerCheckpoint(pBt->pPager, p->db, eMode, pnLog, pnCkpt); 10652 } 10653 sqlite3BtreeLeave(p); 10654 } 10655 return rc; 10656 } 10657 #endif 10658 10659 /* 10660 ** Return true if there is currently a backup running on Btree p. 10661 */ 10662 int sqlite3BtreeIsInBackup(Btree *p){ 10663 assert( p ); 10664 assert( sqlite3_mutex_held(p->db->mutex) ); 10665 return p->nBackup!=0; 10666 } 10667 10668 /* 10669 ** This function returns a pointer to a blob of memory associated with 10670 ** a single shared-btree. The memory is used by client code for its own 10671 ** purposes (for example, to store a high-level schema associated with 10672 ** the shared-btree). The btree layer manages reference counting issues. 10673 ** 10674 ** The first time this is called on a shared-btree, nBytes bytes of memory 10675 ** are allocated, zeroed, and returned to the caller. For each subsequent 10676 ** call the nBytes parameter is ignored and a pointer to the same blob 10677 ** of memory returned. 10678 ** 10679 ** If the nBytes parameter is 0 and the blob of memory has not yet been 10680 ** allocated, a null pointer is returned. If the blob has already been 10681 ** allocated, it is returned as normal. 10682 ** 10683 ** Just before the shared-btree is closed, the function passed as the 10684 ** xFree argument when the memory allocation was made is invoked on the 10685 ** blob of allocated memory. The xFree function should not call sqlite3_free() 10686 ** on the memory, the btree layer does that. 10687 */ 10688 void *sqlite3BtreeSchema(Btree *p, int nBytes, void(*xFree)(void *)){ 10689 BtShared *pBt = p->pBt; 10690 sqlite3BtreeEnter(p); 10691 if( !pBt->pSchema && nBytes ){ 10692 pBt->pSchema = sqlite3DbMallocZero(0, nBytes); 10693 pBt->xFreeSchema = xFree; 10694 } 10695 sqlite3BtreeLeave(p); 10696 return pBt->pSchema; 10697 } 10698 10699 /* 10700 ** Return SQLITE_LOCKED_SHAREDCACHE if another user of the same shared 10701 ** btree as the argument handle holds an exclusive lock on the 10702 ** sqlite_schema table. Otherwise SQLITE_OK. 10703 */ 10704 int sqlite3BtreeSchemaLocked(Btree *p){ 10705 int rc; 10706 assert( sqlite3_mutex_held(p->db->mutex) ); 10707 sqlite3BtreeEnter(p); 10708 rc = querySharedCacheTableLock(p, SCHEMA_ROOT, READ_LOCK); 10709 assert( rc==SQLITE_OK || rc==SQLITE_LOCKED_SHAREDCACHE ); 10710 sqlite3BtreeLeave(p); 10711 return rc; 10712 } 10713 10714 10715 #ifndef SQLITE_OMIT_SHARED_CACHE 10716 /* 10717 ** Obtain a lock on the table whose root page is iTab. The 10718 ** lock is a write lock if isWritelock is true or a read lock 10719 ** if it is false. 10720 */ 10721 int sqlite3BtreeLockTable(Btree *p, int iTab, u8 isWriteLock){ 10722 int rc = SQLITE_OK; 10723 assert( p->inTrans!=TRANS_NONE ); 10724 if( p->sharable ){ 10725 u8 lockType = READ_LOCK + isWriteLock; 10726 assert( READ_LOCK+1==WRITE_LOCK ); 10727 assert( isWriteLock==0 || isWriteLock==1 ); 10728 10729 sqlite3BtreeEnter(p); 10730 rc = querySharedCacheTableLock(p, iTab, lockType); 10731 if( rc==SQLITE_OK ){ 10732 rc = setSharedCacheTableLock(p, iTab, lockType); 10733 } 10734 sqlite3BtreeLeave(p); 10735 } 10736 return rc; 10737 } 10738 #endif 10739 10740 #ifndef SQLITE_OMIT_INCRBLOB 10741 /* 10742 ** Argument pCsr must be a cursor opened for writing on an 10743 ** INTKEY table currently pointing at a valid table entry. 10744 ** This function modifies the data stored as part of that entry. 10745 ** 10746 ** Only the data content may only be modified, it is not possible to 10747 ** change the length of the data stored. If this function is called with 10748 ** parameters that attempt to write past the end of the existing data, 10749 ** no modifications are made and SQLITE_CORRUPT is returned. 10750 */ 10751 int sqlite3BtreePutData(BtCursor *pCsr, u32 offset, u32 amt, void *z){ 10752 int rc; 10753 assert( cursorOwnsBtShared(pCsr) ); 10754 assert( sqlite3_mutex_held(pCsr->pBtree->db->mutex) ); 10755 assert( pCsr->curFlags & BTCF_Incrblob ); 10756 10757 rc = restoreCursorPosition(pCsr); 10758 if( rc!=SQLITE_OK ){ 10759 return rc; 10760 } 10761 assert( pCsr->eState!=CURSOR_REQUIRESEEK ); 10762 if( pCsr->eState!=CURSOR_VALID ){ 10763 return SQLITE_ABORT; 10764 } 10765 10766 /* Save the positions of all other cursors open on this table. This is 10767 ** required in case any of them are holding references to an xFetch 10768 ** version of the b-tree page modified by the accessPayload call below. 10769 ** 10770 ** Note that pCsr must be open on a INTKEY table and saveCursorPosition() 10771 ** and hence saveAllCursors() cannot fail on a BTREE_INTKEY table, hence 10772 ** saveAllCursors can only return SQLITE_OK. 10773 */ 10774 VVA_ONLY(rc =) saveAllCursors(pCsr->pBt, pCsr->pgnoRoot, pCsr); 10775 assert( rc==SQLITE_OK ); 10776 10777 /* Check some assumptions: 10778 ** (a) the cursor is open for writing, 10779 ** (b) there is a read/write transaction open, 10780 ** (c) the connection holds a write-lock on the table (if required), 10781 ** (d) there are no conflicting read-locks, and 10782 ** (e) the cursor points at a valid row of an intKey table. 10783 */ 10784 if( (pCsr->curFlags & BTCF_WriteFlag)==0 ){ 10785 return SQLITE_READONLY; 10786 } 10787 assert( (pCsr->pBt->btsFlags & BTS_READ_ONLY)==0 10788 && pCsr->pBt->inTransaction==TRANS_WRITE ); 10789 assert( hasSharedCacheTableLock(pCsr->pBtree, pCsr->pgnoRoot, 0, 2) ); 10790 assert( !hasReadConflicts(pCsr->pBtree, pCsr->pgnoRoot) ); 10791 assert( pCsr->pPage->intKey ); 10792 10793 return accessPayload(pCsr, offset, amt, (unsigned char *)z, 1); 10794 } 10795 10796 /* 10797 ** Mark this cursor as an incremental blob cursor. 10798 */ 10799 void sqlite3BtreeIncrblobCursor(BtCursor *pCur){ 10800 pCur->curFlags |= BTCF_Incrblob; 10801 pCur->pBtree->hasIncrblobCur = 1; 10802 } 10803 #endif 10804 10805 /* 10806 ** Set both the "read version" (single byte at byte offset 18) and 10807 ** "write version" (single byte at byte offset 19) fields in the database 10808 ** header to iVersion. 10809 */ 10810 int sqlite3BtreeSetVersion(Btree *pBtree, int iVersion){ 10811 BtShared *pBt = pBtree->pBt; 10812 int rc; /* Return code */ 10813 10814 assert( iVersion==1 || iVersion==2 ); 10815 10816 /* If setting the version fields to 1, do not automatically open the 10817 ** WAL connection, even if the version fields are currently set to 2. 10818 */ 10819 pBt->btsFlags &= ~BTS_NO_WAL; 10820 if( iVersion==1 ) pBt->btsFlags |= BTS_NO_WAL; 10821 10822 rc = sqlite3BtreeBeginTrans(pBtree, 0, 0); 10823 if( rc==SQLITE_OK ){ 10824 u8 *aData = pBt->pPage1->aData; 10825 if( aData[18]!=(u8)iVersion || aData[19]!=(u8)iVersion ){ 10826 rc = sqlite3BtreeBeginTrans(pBtree, 2, 0); 10827 if( rc==SQLITE_OK ){ 10828 rc = sqlite3PagerWrite(pBt->pPage1->pDbPage); 10829 if( rc==SQLITE_OK ){ 10830 aData[18] = (u8)iVersion; 10831 aData[19] = (u8)iVersion; 10832 } 10833 } 10834 } 10835 } 10836 10837 pBt->btsFlags &= ~BTS_NO_WAL; 10838 return rc; 10839 } 10840 10841 /* 10842 ** Return true if the cursor has a hint specified. This routine is 10843 ** only used from within assert() statements 10844 */ 10845 int sqlite3BtreeCursorHasHint(BtCursor *pCsr, unsigned int mask){ 10846 return (pCsr->hints & mask)!=0; 10847 } 10848 10849 /* 10850 ** Return true if the given Btree is read-only. 10851 */ 10852 int sqlite3BtreeIsReadonly(Btree *p){ 10853 return (p->pBt->btsFlags & BTS_READ_ONLY)!=0; 10854 } 10855 10856 /* 10857 ** Return the size of the header added to each page by this module. 10858 */ 10859 int sqlite3HeaderSizeBtree(void){ return ROUND8(sizeof(MemPage)); } 10860 10861 #if !defined(SQLITE_OMIT_SHARED_CACHE) 10862 /* 10863 ** Return true if the Btree passed as the only argument is sharable. 10864 */ 10865 int sqlite3BtreeSharable(Btree *p){ 10866 return p->sharable; 10867 } 10868 10869 /* 10870 ** Return the number of connections to the BtShared object accessed by 10871 ** the Btree handle passed as the only argument. For private caches 10872 ** this is always 1. For shared caches it may be 1 or greater. 10873 */ 10874 int sqlite3BtreeConnectionCount(Btree *p){ 10875 testcase( p->sharable ); 10876 return p->pBt->nRef; 10877 } 10878 #endif 10879