SQLite4
Check-in [4cb2e9c5af42b7e714779da70e1ccc2e80c84d9e]
Not logged in

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
SHA1 Hash:4cb2e9c5af42b7e714779da70e1ccc2e80c84d9e
Date: 2014-02-03 15:54:19
User: dan
Comment:Zero shared-memory before attempting recovery operations.
Tags And Properties
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/bt_lock.c

357
358
359
360
361
362
363



364
365
366
367
368
369
370
  if( rc==SQLITE4_OK ){
    rc = btLockLockop(p, BT_LOCK_DMS1, BT_LOCK_EXCL, 1);
    if( rc==SQLITE4_OK ){
      rc = btLockLockop(p, BT_LOCK_DMS2_RW, BT_LOCK_EXCL, 0);
      if( rc==SQLITE4_OK ){
        rc = btLockLockop(p, BT_LOCK_DMS2_RO, BT_LOCK_EXCL, 0);
      }



      if( rc==SQLITE4_OK ){
        rc = xRecover(p);
      }
      btLockLockop(p, BT_LOCK_DMS2_RO, BT_LOCK_UNLOCK, 0);
      btLockLockop(p, BT_LOCK_DMS2_RW, BT_LOCK_UNLOCK, 0);
      if( rc==SQLITE4_OK || rc==SQLITE4_BUSY ){
        rc = btLockLockop(p, BT_LOCK_DMS2_RW, BT_LOCK_SHARED, 0);







>
>
>







357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
  if( rc==SQLITE4_OK ){
    rc = btLockLockop(p, BT_LOCK_DMS1, BT_LOCK_EXCL, 1);
    if( rc==SQLITE4_OK ){
      rc = btLockLockop(p, BT_LOCK_DMS2_RW, BT_LOCK_EXCL, 0);
      if( rc==SQLITE4_OK ){
        rc = btLockLockop(p, BT_LOCK_DMS2_RO, BT_LOCK_EXCL, 0);
      }
      if( rc==SQLITE4_OK ){
        rc = pVfs->xShmMap(p->pFd, 0, 0, 0);
      }
      if( rc==SQLITE4_OK ){
        rc = xRecover(p);
      }
      btLockLockop(p, BT_LOCK_DMS2_RO, BT_LOCK_UNLOCK, 0);
      btLockLockop(p, BT_LOCK_DMS2_RW, BT_LOCK_UNLOCK, 0);
      if( rc==SQLITE4_OK || rc==SQLITE4_BUSY ){
        rc = btLockLockop(p, BT_LOCK_DMS2_RW, BT_LOCK_SHARED, 0);

Changes to src/bt_log.c

252
253
254
255
256
257
258
259
260
261
262

263
264
265
266
267
268
269
...
277
278
279
280
281
282
283










284
285
286
287
288
289
290
...
778
779
780
781
782
783
784


785
786
787
788
789
790
791
...
940
941
942
943
944
945
946



947
948
949
950
951
952
953
    btLogChecksum(nativeCksum, a, 8, aIn, aOut);
    btLogChecksum(nativeCksum, &a[4], nByte-4, aOut, aOut);
  }else{
    btLogChecksum(nativeCksum, a, nByte, aIn, aOut);
  }
}

#define BT_ALLOC_DEBUG 0
#define BT_PAGE_DEBUG  0
#define BT_VAL_DEBUG   0
#define BT_HDR_DEBUG   0


static void btDebugTopology(BtLock *pLock, char *zStr, int iSide, u32 *aLog){
#if BT_PAGE_DEBUG
  fprintf(stderr, "%d:%s: (side=%d) %d..%d  %d..%d  %d..%d\n", 
      pLock->iDebugId, zStr, iSide,
      (int)aLog[0], (int)aLog[1], (int)aLog[2], 
      (int)aLog[3], (int)aLog[4], (int)aLog[5]
................................................................................
  static int nCall = 0;
  fprintf(stderr, "%d:%d: %s db-header "
      "(pgsz=%d nPg=%d iRoot=%d iCookie=%d iFreePg=%d iFreeBlk=%d)\n",
      pLock->iDebugId, nCall++,
      zStr, (int)pHdr->pgsz, (int)pHdr->nPg, (int)pHdr->iRoot, 
      (int)pHdr->iCookie, (int)pHdr->iFreePg, (int)pHdr->iFreeBlk
  );










  fflush(stderr);
#endif
}

#ifndef NDEBUG
void sqlite4BtDebugReadlock(BtLock *pLock, u32 iFirst, u32 iLast){
#if BT_PAGE_DEBUG
................................................................................
  BtLog *pLog,                    /* Log module handle */
  void *pCtx,                     /* Pointer to FrameRecoverCtx */
  u32 iFrame,                     /* Frame number */
  BtFrameHdr *pHdr                /* Frame header */
){
  FrameRecoverCtx *pFRC = (FrameRecoverCtx*)pCtx;



  if( btLogIsEmpty(pLog) ){
    /* This is the first frame recovered. It is therefore both the first
    ** and last frame of log region (c).  */
    pLog->snapshot.aLog[4] = iFrame;
    pLog->snapshot.aLog[5] = iFrame;
  }else{
    u32 iExpect = pLog->snapshot.aLog[5]+1;
................................................................................
  i64 nByte = 0;                  /* Size of log file on disk */
  int rc;                         /* Return code */
  BtWalHdr *pHdr = 0;
  int iSlot = 0;
  FrameRecoverCtx ctx = {0, 0};
  BtWalHdr hdr1;
  BtWalHdr hdr2;




  /* Read a log file header from the start of the file. */
  rc = pVfs->xSize(pLog->pFd, &nByte);
  if( rc==SQLITE4_OK && nByte>0 ){
    rc = btLogReadHeader(pLog, 0, &hdr1);
    if( rc==SQLITE4_OK ){
      rc = btLogReadHeader(pLog, hdr1.nSector, &hdr2);







|
|
|
|
>







 







>
>
>
>
>
>
>
>
>
>







 







>
>







 







>
>
>







252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
...
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
...
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
...
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
    btLogChecksum(nativeCksum, a, 8, aIn, aOut);
    btLogChecksum(nativeCksum, &a[4], nByte-4, aOut, aOut);
  }else{
    btLogChecksum(nativeCksum, a, nByte, aIn, aOut);
  }
}

#define BT_ALLOC_DEBUG   0
#define BT_PAGE_DEBUG    0
#define BT_VAL_DEBUG     0
#define BT_HDR_DEBUG     0
#define BT_RECOVER_DEBUG 0

static void btDebugTopology(BtLock *pLock, char *zStr, int iSide, u32 *aLog){
#if BT_PAGE_DEBUG
  fprintf(stderr, "%d:%s: (side=%d) %d..%d  %d..%d  %d..%d\n", 
      pLock->iDebugId, zStr, iSide,
      (int)aLog[0], (int)aLog[1], (int)aLog[2], 
      (int)aLog[3], (int)aLog[4], (int)aLog[5]
................................................................................
  static int nCall = 0;
  fprintf(stderr, "%d:%d: %s db-header "
      "(pgsz=%d nPg=%d iRoot=%d iCookie=%d iFreePg=%d iFreeBlk=%d)\n",
      pLock->iDebugId, nCall++,
      zStr, (int)pHdr->pgsz, (int)pHdr->nPg, (int)pHdr->iRoot, 
      (int)pHdr->iCookie, (int)pHdr->iFreePg, (int)pHdr->iFreeBlk
  );
  fflush(stderr);
#endif
}

static void btDebugRecoverFrame(BtLock *pLock, u32 iFrame, u32 pgno){
#if BT_RECOVER_DEBUG
  static int nCall = 0;
  fprintf(stderr, "%d:%d: recovered page %d from frame %d\n",
      pLock->iDebugId, nCall++, (int)pgno, (int)iFrame
  );
  fflush(stderr);
#endif
}

#ifndef NDEBUG
void sqlite4BtDebugReadlock(BtLock *pLock, u32 iFirst, u32 iLast){
#if BT_PAGE_DEBUG
................................................................................
  BtLog *pLog,                    /* Log module handle */
  void *pCtx,                     /* Pointer to FrameRecoverCtx */
  u32 iFrame,                     /* Frame number */
  BtFrameHdr *pHdr                /* Frame header */
){
  FrameRecoverCtx *pFRC = (FrameRecoverCtx*)pCtx;

  btDebugRecoverFrame(pLog->pLock, iFrame, pHdr->pgno);

  if( btLogIsEmpty(pLog) ){
    /* This is the first frame recovered. It is therefore both the first
    ** and last frame of log region (c).  */
    pLog->snapshot.aLog[4] = iFrame;
    pLog->snapshot.aLog[5] = iFrame;
  }else{
    u32 iExpect = pLog->snapshot.aLog[5]+1;
................................................................................
  i64 nByte = 0;                  /* Size of log file on disk */
  int rc;                         /* Return code */
  BtWalHdr *pHdr = 0;
  int iSlot = 0;
  FrameRecoverCtx ctx = {0, 0};
  BtWalHdr hdr1;
  BtWalHdr hdr2;

static int nCall = 0;
nCall++;

  /* Read a log file header from the start of the file. */
  rc = pVfs->xSize(pLog->pFd, &nByte);
  if( rc==SQLITE4_OK && nByte>0 ){
    rc = btLogReadHeader(pLog, 0, &hdr1);
    if( rc==SQLITE4_OK ){
      rc = btLogReadHeader(pLog, hdr1.nSector, &hdr2);

Changes to src/bt_unix.c

318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344

















345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372

373
374
375
376
377
378
379

  return rc;
}

int btPosixOsShmMap(bt_file *pFile, int iChunk, int sz, void **ppShm){
  BtPosixFile *p = (BtPosixFile *)pFile;

  *ppShm = 0;
  assert( sz==BT_SHM_CHUNK_SIZE );
  if( iChunk>=p->nShm ){
    int i;
    void **apNew;
    int nNew = iChunk+1;
    off_t nReq = nNew * BT_SHM_CHUNK_SIZE;
    struct stat sStat;

    /* If the shared-memory file has not been opened, open it now. */
    if( p->shmfd<=0 ){
      char *zShm = btPosixShmFile(p);
      if( !zShm ) return btErrorBkpt(SQLITE4_NOMEM);
      p->shmfd = open(zShm, O_RDWR|O_CREAT, 0644);
      sqlite4_free(p->pSqlEnv, zShm);
      if( p->shmfd<0 ){ 
        return btErrorBkpt(SQLITE4_IOERR);
      }
    }


















    /* If the shared-memory file is not large enough to contain the 
    ** requested chunk, cause it to grow.  */
    if( fstat(p->shmfd, &sStat) ){
      return btErrorBkpt(SQLITE4_IOERR);
    }
    if( sStat.st_size<nReq ){
      if( ftruncate(p->shmfd, nReq) ){
        return btErrorBkpt(SQLITE4_IOERR);
      }
    }

    apNew = (void**)sqlite4_realloc(p->pSqlEnv, p->apShm, sizeof(void*) * nNew);
    if( !apNew ) return btErrorBkpt(SQLITE4_NOMEM);
    for(i=p->nShm; i<nNew; i++){
      apNew[i] = 0;
    }
    p->apShm = apNew;
    p->nShm = nNew;
  }

  if( p->apShm[iChunk]==0 ){
    p->apShm[iChunk] = mmap(0, BT_SHM_CHUNK_SIZE, 
        PROT_READ|PROT_WRITE, MAP_SHARED, p->shmfd, iChunk*BT_SHM_CHUNK_SIZE
    );
    if( p->apShm[iChunk]==0 ) return btErrorBkpt(SQLITE4_IOERR);
  }

  *ppShm = p->apShm[iChunk];

  return SQLITE4_OK;
}

void btPosixOsShmBarrier(bt_file *pFile){
}

int btPosixOsShmUnmap(bt_file *pFile, int bDelete){







<
<
<
<
<
<
<
<
<
|
|
|
|
|
|
|
|
|
|

>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
|
|
|
|
|
|
|
|
|

|
|
|
|
|
|
|
|

|
|
|
|
|
|

|
>







318
319
320
321
322
323
324









325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388

  return rc;
}

int btPosixOsShmMap(bt_file *pFile, int iChunk, int sz, void **ppShm){
  BtPosixFile *p = (BtPosixFile *)pFile;










  /* If the shared-memory file has not been opened, open it now. */
  if( p->shmfd<=0 ){
    char *zShm = btPosixShmFile(p);
    if( !zShm ) return btErrorBkpt(SQLITE4_NOMEM);
    p->shmfd = open(zShm, O_RDWR|O_CREAT, 0644);
    sqlite4_free(p->pSqlEnv, zShm);
    if( p->shmfd<0 ){ 
      return btErrorBkpt(SQLITE4_IOERR);
    }
  }

  if( ppShm==0 ){
    assert( p->nShm==0 );
    if( p->nShm ) return btErrorBkpt(SQLITE4_MISUSE);
    if( ftruncate(p->shmfd, 0) ){
      return btErrorBkpt(SQLITE4_IOERR);
    }
  }else{

    *ppShm = 0;
    assert( sz==BT_SHM_CHUNK_SIZE );
    if( iChunk>=p->nShm ){
      int i;
      void **apNew;
      int nNew = iChunk+1;
      off_t nReq = nNew * BT_SHM_CHUNK_SIZE;
      struct stat sStat;

      /* If the shared-memory file is not large enough to contain the 
       ** requested chunk, cause it to grow.  */
      if( fstat(p->shmfd, &sStat) ){
        return btErrorBkpt(SQLITE4_IOERR);
      }
      if( sStat.st_size<nReq ){
        if( ftruncate(p->shmfd, nReq) ){
          return btErrorBkpt(SQLITE4_IOERR);
        }
      }

      apNew = (void**)sqlite4_realloc(p->pSqlEnv, p->apShm, sizeof(void*)*nNew);
      if( !apNew ) return btErrorBkpt(SQLITE4_NOMEM);
      for(i=p->nShm; i<nNew; i++){
        apNew[i] = 0;
      }
      p->apShm = apNew;
      p->nShm = nNew;
    }

    if( p->apShm[iChunk]==0 ){
      p->apShm[iChunk] = mmap(0, BT_SHM_CHUNK_SIZE, 
          PROT_READ|PROT_WRITE, MAP_SHARED, p->shmfd, iChunk*BT_SHM_CHUNK_SIZE
      );
      if( p->apShm[iChunk]==0 ) return btErrorBkpt(SQLITE4_IOERR);
    }

    *ppShm = p->apShm[iChunk];
  }
  return SQLITE4_OK;
}

void btPosixOsShmBarrier(bt_file *pFile){
}

int btPosixOsShmUnmap(bt_file *pFile, int bDelete){