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