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