extensions/sqlite/sqlite-source/pager.c
author Asher Baker <asherkin@gmail.com>
Thu Apr 26 16:58:18 2012 +0100 (2012-04-26)
changeset 3488 bee90c8ef6c3
parent 1952 b7aa2b626807
permissions -rw-r--r--
Fixed client serials not being unique on Windows (bug 5285, r=psychonic).
ds@1952
     1
/*
ds@1952
     2
** 2001 September 15
ds@1952
     3
**
ds@1952
     4
** The author disclaims copyright to this source code.  In place of
ds@1952
     5
** a legal notice, here is a blessing:
ds@1952
     6
**
ds@1952
     7
**    May you do good and not evil.
ds@1952
     8
**    May you find forgiveness for yourself and forgive others.
ds@1952
     9
**    May you share freely, never taking more than you give.
ds@1952
    10
**
ds@1952
    11
*************************************************************************
ds@1952
    12
** This is the implementation of the page cache subsystem or "pager".
ds@1952
    13
** 
ds@1952
    14
** The pager is used to access a database disk file.  It implements
ds@1952
    15
** atomic commit and rollback through the use of a journal file that
ds@1952
    16
** is separate from the database file.  The pager also implements file
ds@1952
    17
** locking to prevent two processes from writing the same database
ds@1952
    18
** file simultaneously, or one process from reading the database while
ds@1952
    19
** another is writing.
ds@1952
    20
**
ds@1952
    21
** @(#) $Id$
ds@1952
    22
*/
ds@1952
    23
#ifndef SQLITE_OMIT_DISKIO
ds@1952
    24
#include "sqliteInt.h"
ds@1952
    25
#include <assert.h>
ds@1952
    26
#include <string.h>
ds@1952
    27
ds@1952
    28
/*
ds@1952
    29
** Macros for troubleshooting.  Normally turned off
ds@1952
    30
*/
ds@1952
    31
#if 0
ds@1952
    32
#define sqlite3DebugPrintf printf
ds@1952
    33
#define PAGERTRACE1(X)       sqlite3DebugPrintf(X)
ds@1952
    34
#define PAGERTRACE2(X,Y)     sqlite3DebugPrintf(X,Y)
ds@1952
    35
#define PAGERTRACE3(X,Y,Z)   sqlite3DebugPrintf(X,Y,Z)
ds@1952
    36
#define PAGERTRACE4(X,Y,Z,W) sqlite3DebugPrintf(X,Y,Z,W)
ds@1952
    37
#define PAGERTRACE5(X,Y,Z,W,V) sqlite3DebugPrintf(X,Y,Z,W,V)
ds@1952
    38
#else
ds@1952
    39
#define PAGERTRACE1(X)
ds@1952
    40
#define PAGERTRACE2(X,Y)
ds@1952
    41
#define PAGERTRACE3(X,Y,Z)
ds@1952
    42
#define PAGERTRACE4(X,Y,Z,W)
ds@1952
    43
#define PAGERTRACE5(X,Y,Z,W,V)
ds@1952
    44
#endif
ds@1952
    45
ds@1952
    46
/*
ds@1952
    47
** The following two macros are used within the PAGERTRACEX() macros above
ds@1952
    48
** to print out file-descriptors. 
ds@1952
    49
**
ds@1952
    50
** PAGERID() takes a pointer to a Pager struct as it's argument. The
ds@1952
    51
** associated file-descriptor is returned. FILEHANDLEID() takes an sqlite3_file
ds@1952
    52
** struct as it's argument.
ds@1952
    53
*/
ds@1952
    54
#define PAGERID(p) ((int)(p->fd))
ds@1952
    55
#define FILEHANDLEID(fd) ((int)fd)
ds@1952
    56
ds@1952
    57
/*
ds@1952
    58
** The page cache as a whole is always in one of the following
ds@1952
    59
** states:
ds@1952
    60
**
ds@1952
    61
**   PAGER_UNLOCK        The page cache is not currently reading or 
ds@1952
    62
**                       writing the database file.  There is no
ds@1952
    63
**                       data held in memory.  This is the initial
ds@1952
    64
**                       state.
ds@1952
    65
**
ds@1952
    66
**   PAGER_SHARED        The page cache is reading the database.
ds@1952
    67
**                       Writing is not permitted.  There can be
ds@1952
    68
**                       multiple readers accessing the same database
ds@1952
    69
**                       file at the same time.
ds@1952
    70
**
ds@1952
    71
**   PAGER_RESERVED      This process has reserved the database for writing
ds@1952
    72
**                       but has not yet made any changes.  Only one process
ds@1952
    73
**                       at a time can reserve the database.  The original
ds@1952
    74
**                       database file has not been modified so other
ds@1952
    75
**                       processes may still be reading the on-disk
ds@1952
    76
**                       database file.
ds@1952
    77
**
ds@1952
    78
**   PAGER_EXCLUSIVE     The page cache is writing the database.
ds@1952
    79
**                       Access is exclusive.  No other processes or
ds@1952
    80
**                       threads can be reading or writing while one
ds@1952
    81
**                       process is writing.
ds@1952
    82
**
ds@1952
    83
**   PAGER_SYNCED        The pager moves to this state from PAGER_EXCLUSIVE
ds@1952
    84
**                       after all dirty pages have been written to the
ds@1952
    85
**                       database file and the file has been synced to
ds@1952
    86
**                       disk. All that remains to do is to remove or
ds@1952
    87
**                       truncate the journal file and the transaction 
ds@1952
    88
**                       will be committed.
ds@1952
    89
**
ds@1952
    90
** The page cache comes up in PAGER_UNLOCK.  The first time a
ds@1952
    91
** sqlite3PagerGet() occurs, the state transitions to PAGER_SHARED.
ds@1952
    92
** After all pages have been released using sqlite_page_unref(),
ds@1952
    93
** the state transitions back to PAGER_UNLOCK.  The first time
ds@1952
    94
** that sqlite3PagerWrite() is called, the state transitions to
ds@1952
    95
** PAGER_RESERVED.  (Note that sqlite3PagerWrite() can only be
ds@1952
    96
** called on an outstanding page which means that the pager must
ds@1952
    97
** be in PAGER_SHARED before it transitions to PAGER_RESERVED.)
ds@1952
    98
** PAGER_RESERVED means that there is an open rollback journal.
ds@1952
    99
** The transition to PAGER_EXCLUSIVE occurs before any changes
ds@1952
   100
** are made to the database file, though writes to the rollback
ds@1952
   101
** journal occurs with just PAGER_RESERVED.  After an sqlite3PagerRollback()
ds@1952
   102
** or sqlite3PagerCommitPhaseTwo(), the state can go back to PAGER_SHARED,
ds@1952
   103
** or it can stay at PAGER_EXCLUSIVE if we are in exclusive access mode.
ds@1952
   104
*/
ds@1952
   105
#define PAGER_UNLOCK      0
ds@1952
   106
#define PAGER_SHARED      1   /* same as SHARED_LOCK */
ds@1952
   107
#define PAGER_RESERVED    2   /* same as RESERVED_LOCK */
ds@1952
   108
#define PAGER_EXCLUSIVE   4   /* same as EXCLUSIVE_LOCK */
ds@1952
   109
#define PAGER_SYNCED      5
ds@1952
   110
ds@1952
   111
/*
ds@1952
   112
** If the SQLITE_BUSY_RESERVED_LOCK macro is set to true at compile-time,
ds@1952
   113
** then failed attempts to get a reserved lock will invoke the busy callback.
ds@1952
   114
** This is off by default.  To see why, consider the following scenario:
ds@1952
   115
** 
ds@1952
   116
** Suppose thread A already has a shared lock and wants a reserved lock.
ds@1952
   117
** Thread B already has a reserved lock and wants an exclusive lock.  If
ds@1952
   118
** both threads are using their busy callbacks, it might be a long time
ds@1952
   119
** be for one of the threads give up and allows the other to proceed.
ds@1952
   120
** But if the thread trying to get the reserved lock gives up quickly
ds@1952
   121
** (if it never invokes its busy callback) then the contention will be
ds@1952
   122
** resolved quickly.
ds@1952
   123
*/
ds@1952
   124
#ifndef SQLITE_BUSY_RESERVED_LOCK
ds@1952
   125
# define SQLITE_BUSY_RESERVED_LOCK 0
ds@1952
   126
#endif
ds@1952
   127
ds@1952
   128
/*
ds@1952
   129
** This macro rounds values up so that if the value is an address it
ds@1952
   130
** is guaranteed to be an address that is aligned to an 8-byte boundary.
ds@1952
   131
*/
ds@1952
   132
#define FORCE_ALIGNMENT(X)   (((X)+7)&~7)
ds@1952
   133
ds@1952
   134
typedef struct PgHdr PgHdr;
ds@1952
   135
ds@1952
   136
/*
ds@1952
   137
** Each pager stores all currently unreferenced pages in a list sorted
ds@1952
   138
** in least-recently-used (LRU) order (i.e. the first item on the list has 
ds@1952
   139
** not been referenced in a long time, the last item has been recently
ds@1952
   140
** used). An instance of this structure is included as part of each
ds@1952
   141
** pager structure for this purpose (variable Pager.lru).
ds@1952
   142
**
ds@1952
   143
** Additionally, if memory-management is enabled, all unreferenced pages 
ds@1952
   144
** are stored in a global LRU list (global variable sqlite3LruPageList).
ds@1952
   145
**
ds@1952
   146
** In both cases, the PagerLruList.pFirstSynced variable points to
ds@1952
   147
** the first page in the corresponding list that does not require an
ds@1952
   148
** fsync() operation before it's memory can be reclaimed. If no such
ds@1952
   149
** page exists, PagerLruList.pFirstSynced is set to NULL.
ds@1952
   150
*/
ds@1952
   151
typedef struct PagerLruList PagerLruList;
ds@1952
   152
struct PagerLruList {
ds@1952
   153
  PgHdr *pFirst;         /* First page in LRU list */
ds@1952
   154
  PgHdr *pLast;          /* Last page in LRU list (the most recently used) */
ds@1952
   155
  PgHdr *pFirstSynced;   /* First page in list with PgHdr.needSync==0 */
ds@1952
   156
};
ds@1952
   157
ds@1952
   158
/*
ds@1952
   159
** The following structure contains the next and previous pointers used
ds@1952
   160
** to link a PgHdr structure into a PagerLruList linked list. 
ds@1952
   161
*/
ds@1952
   162
typedef struct PagerLruLink PagerLruLink;
ds@1952
   163
struct PagerLruLink {
ds@1952
   164
  PgHdr *pNext;
ds@1952
   165
  PgHdr *pPrev;
ds@1952
   166
};
ds@1952
   167
ds@1952
   168
/*
ds@1952
   169
** Each in-memory image of a page begins with the following header.
ds@1952
   170
** This header is only visible to this pager module.  The client
ds@1952
   171
** code that calls pager sees only the data that follows the header.
ds@1952
   172
**
ds@1952
   173
** Client code should call sqlite3PagerWrite() on a page prior to making
ds@1952
   174
** any modifications to that page.  The first time sqlite3PagerWrite()
ds@1952
   175
** is called, the original page contents are written into the rollback
ds@1952
   176
** journal and PgHdr.inJournal and PgHdr.needSync are set.  Later, once
ds@1952
   177
** the journal page has made it onto the disk surface, PgHdr.needSync
ds@1952
   178
** is cleared.  The modified page cannot be written back into the original
ds@1952
   179
** database file until the journal pages has been synced to disk and the
ds@1952
   180
** PgHdr.needSync has been cleared.
ds@1952
   181
**
ds@1952
   182
** The PgHdr.dirty flag is set when sqlite3PagerWrite() is called and
ds@1952
   183
** is cleared again when the page content is written back to the original
ds@1952
   184
** database file.
ds@1952
   185
**
ds@1952
   186
** Details of important structure elements:
ds@1952
   187
**
ds@1952
   188
** needSync
ds@1952
   189
**
ds@1952
   190
**     If this is true, this means that it is not safe to write the page
ds@1952
   191
**     content to the database because the original content needed
ds@1952
   192
**     for rollback has not by synced to the main rollback journal.
ds@1952
   193
**     The original content may have been written to the rollback journal
ds@1952
   194
**     but it has not yet been synced.  So we cannot write to the database
ds@1952
   195
**     file because power failure might cause the page in the journal file
ds@1952
   196
**     to never reach the disk.  It is as if the write to the journal file
ds@1952
   197
**     does not occur until the journal file is synced.
ds@1952
   198
**     
ds@1952
   199
**     This flag is false if the page content exactly matches what
ds@1952
   200
**     currently exists in the database file.  The needSync flag is also
ds@1952
   201
**     false if the original content has been written to the main rollback
ds@1952
   202
**     journal and synced.  If the page represents a new page that has
ds@1952
   203
**     been added onto the end of the database during the current
ds@1952
   204
**     transaction, the needSync flag is true until the original database
ds@1952
   205
**     size in the journal header has been synced to disk.
ds@1952
   206
**
ds@1952
   207
** inJournal
ds@1952
   208
**
ds@1952
   209
**     This is true if the original page has been written into the main
ds@1952
   210
**     rollback journal.  This is always false for new pages added to
ds@1952
   211
**     the end of the database file during the current transaction.
ds@1952
   212
**     And this flag says nothing about whether or not the journal
ds@1952
   213
**     has been synced to disk.  For pages that are in the original
ds@1952
   214
**     database file, the following expression should always be true:
ds@1952
   215
**
ds@1952
   216
**       inJournal = (pPager->aInJournal[(pgno-1)/8] & (1<<((pgno-1)%8))!=0
ds@1952
   217
**
ds@1952
   218
**     The pPager->aInJournal[] array is only valid for the original
ds@1952
   219
**     pages of the database, not new pages that are added to the end
ds@1952
   220
**     of the database, so obviously the above expression cannot be
ds@1952
   221
**     valid for new pages.  For new pages inJournal is always 0.
ds@1952
   222
**
ds@1952
   223
** dirty
ds@1952
   224
**
ds@1952
   225
**     When true, this means that the content of the page has been
ds@1952
   226
**     modified and needs to be written back to the database file.
ds@1952
   227
**     If false, it means that either the content of the page is
ds@1952
   228
**     unchanged or else the content is unimportant and we do not
ds@1952
   229
**     care whether or not it is preserved.
ds@1952
   230
**
ds@1952
   231
** alwaysRollback
ds@1952
   232
**
ds@1952
   233
**     This means that the sqlite3PagerDontRollback() API should be
ds@1952
   234
**     ignored for this page.  The DontRollback() API attempts to say
ds@1952
   235
**     that the content of the page on disk is unimportant (it is an
ds@1952
   236
**     unused page on the freelist) so that it is unnecessary to 
ds@1952
   237
**     rollback changes to this page because the content of the page
ds@1952
   238
**     can change without changing the meaning of the database.  This
ds@1952
   239
**     flag overrides any DontRollback() attempt.  This flag is set
ds@1952
   240
**     when a page that originally contained valid data is added to
ds@1952
   241
**     the freelist.  Later in the same transaction, this page might
ds@1952
   242
**     be pulled from the freelist and reused for something different
ds@1952
   243
**     and at that point the DontRollback() API will be called because
ds@1952
   244
**     pages taken from the freelist do not need to be protected by
ds@1952
   245
**     the rollback journal.  But this flag says that the page was
ds@1952
   246
**     not originally part of the freelist so that it still needs to
ds@1952
   247
**     be rolled back in spite of any subsequent DontRollback() calls.
ds@1952
   248
**
ds@1952
   249
** needRead 
ds@1952
   250
**
ds@1952
   251
**     This flag means (when true) that the content of the page has
ds@1952
   252
**     not yet been loaded from disk.  The in-memory content is just
ds@1952
   253
**     garbage.  (Actually, we zero the content, but you should not
ds@1952
   254
**     make any assumptions about the content nevertheless.)  If the
ds@1952
   255
**     content is needed in the future, it should be read from the
ds@1952
   256
**     original database file.
ds@1952
   257
*/
ds@1952
   258
struct PgHdr {
ds@1952
   259
  Pager *pPager;                 /* The pager to which this page belongs */
ds@1952
   260
  Pgno pgno;                     /* The page number for this page */
ds@1952
   261
  PgHdr *pNextHash, *pPrevHash;  /* Hash collision chain for PgHdr.pgno */
ds@1952
   262
  PagerLruLink free;             /* Next and previous free pages */
ds@1952
   263
  PgHdr *pNextAll;               /* A list of all pages */
ds@1952
   264
  u8 inJournal;                  /* TRUE if has been written to journal */
ds@1952
   265
  u8 dirty;                      /* TRUE if we need to write back changes */
ds@1952
   266
  u8 needSync;                   /* Sync journal before writing this page */
ds@1952
   267
  u8 alwaysRollback;             /* Disable DontRollback() for this page */
ds@1952
   268
  u8 needRead;                   /* Read content if PagerWrite() is called */
ds@1952
   269
  short int nRef;                /* Number of users of this page */
ds@1952
   270
  PgHdr *pDirty, *pPrevDirty;    /* Dirty pages */
ds@1952
   271
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
ds@1952
   272
  PagerLruLink gfree;            /* Global list of nRef==0 pages */
ds@1952
   273
#endif
ds@1952
   274
#ifdef SQLITE_CHECK_PAGES
ds@1952
   275
  u32 pageHash;
ds@1952
   276
#endif
ds@1952
   277
  void *pData;                   /* Page data */
ds@1952
   278
  /* Pager.nExtra bytes of local data appended to this header */
ds@1952
   279
};
ds@1952
   280
ds@1952
   281
/*
ds@1952
   282
** For an in-memory only database, some extra information is recorded about
ds@1952
   283
** each page so that changes can be rolled back.  (Journal files are not
ds@1952
   284
** used for in-memory databases.)  The following information is added to
ds@1952
   285
** the end of every EXTRA block for in-memory databases.
ds@1952
   286
**
ds@1952
   287
** This information could have been added directly to the PgHdr structure.
ds@1952
   288
** But then it would take up an extra 8 bytes of storage on every PgHdr
ds@1952
   289
** even for disk-based databases.  Splitting it out saves 8 bytes.  This
ds@1952
   290
** is only a savings of 0.8% but those percentages add up.
ds@1952
   291
*/
ds@1952
   292
typedef struct PgHistory PgHistory;
ds@1952
   293
struct PgHistory {
ds@1952
   294
  u8 *pOrig;     /* Original page text.  Restore to this on a full rollback */
ds@1952
   295
  u8 *pStmt;     /* Text as it was at the beginning of the current statement */
ds@1952
   296
  PgHdr *pNextStmt, *pPrevStmt;  /* List of pages in the statement journal */
ds@1952
   297
  u8 inStmt;                     /* TRUE if in the statement subjournal */
ds@1952
   298
};
ds@1952
   299
ds@1952
   300
/*
ds@1952
   301
** A macro used for invoking the codec if there is one
ds@1952
   302
*/
ds@1952
   303
#ifdef SQLITE_HAS_CODEC
ds@1952
   304
# define CODEC1(P,D,N,X) if( P->xCodec!=0 ){ P->xCodec(P->pCodecArg,D,N,X); }
ds@1952
   305
# define CODEC2(P,D,N,X) ((char*)(P->xCodec!=0?P->xCodec(P->pCodecArg,D,N,X):D))
ds@1952
   306
#else
ds@1952
   307
# define CODEC1(P,D,N,X) /* NO-OP */
ds@1952
   308
# define CODEC2(P,D,N,X) ((char*)D)
ds@1952
   309
#endif
ds@1952
   310
ds@1952
   311
/*
ds@1952
   312
** Convert a pointer to a PgHdr into a pointer to its data
ds@1952
   313
** and back again.
ds@1952
   314
*/
ds@1952
   315
#define PGHDR_TO_DATA(P)    ((P)->pData)
ds@1952
   316
#define PGHDR_TO_EXTRA(G,P) ((void*)&((G)[1]))
ds@1952
   317
#define PGHDR_TO_HIST(P,PGR)  \
ds@1952
   318
            ((PgHistory*)&((char*)(&(P)[1]))[(PGR)->nExtra])
ds@1952
   319
ds@1952
   320
/*
ds@1952
   321
** A open page cache is an instance of the following structure.
ds@1952
   322
**
ds@1952
   323
** Pager.errCode may be set to SQLITE_IOERR, SQLITE_CORRUPT, or
ds@1952
   324
** or SQLITE_FULL. Once one of the first three errors occurs, it persists
ds@1952
   325
** and is returned as the result of every major pager API call.  The
ds@1952
   326
** SQLITE_FULL return code is slightly different. It persists only until the
ds@1952
   327
** next successful rollback is performed on the pager cache. Also,
ds@1952
   328
** SQLITE_FULL does not affect the sqlite3PagerGet() and sqlite3PagerLookup()
ds@1952
   329
** APIs, they may still be used successfully.
ds@1952
   330
*/
ds@1952
   331
struct Pager {
ds@1952
   332
  sqlite3_vfs *pVfs;          /* OS functions to use for IO */
ds@1952
   333
  u8 journalOpen;             /* True if journal file descriptors is valid */
ds@1952
   334
  u8 journalStarted;          /* True if header of journal is synced */
ds@1952
   335
  u8 useJournal;              /* Use a rollback journal on this file */
ds@1952
   336
  u8 noReadlock;              /* Do not bother to obtain readlocks */
ds@1952
   337
  u8 stmtOpen;                /* True if the statement subjournal is open */
ds@1952
   338
  u8 stmtInUse;               /* True we are in a statement subtransaction */
ds@1952
   339
  u8 stmtAutoopen;            /* Open stmt journal when main journal is opened*/
ds@1952
   340
  u8 noSync;                  /* Do not sync the journal if true */
ds@1952
   341
  u8 fullSync;                /* Do extra syncs of the journal for robustness */
ds@1952
   342
  u8 sync_flags;              /* One of SYNC_NORMAL or SYNC_FULL */
ds@1952
   343
  u8 state;                   /* PAGER_UNLOCK, _SHARED, _RESERVED, etc. */
ds@1952
   344
  u8 tempFile;                /* zFilename is a temporary file */
ds@1952
   345
  u8 readOnly;                /* True for a read-only database */
ds@1952
   346
  u8 needSync;                /* True if an fsync() is needed on the journal */
ds@1952
   347
  u8 dirtyCache;              /* True if cached pages have changed */
ds@1952
   348
  u8 alwaysRollback;          /* Disable DontRollback() for all pages */
ds@1952
   349
  u8 memDb;                   /* True to inhibit all file I/O */
ds@1952
   350
  u8 setMaster;               /* True if a m-j name has been written to jrnl */
ds@1952
   351
  u8 doNotSync;               /* Boolean. While true, do not spill the cache */
ds@1952
   352
  u8 exclusiveMode;           /* Boolean. True if locking_mode==EXCLUSIVE */
ds@1952
   353
  u8 changeCountDone;         /* Set after incrementing the change-counter */
ds@1952
   354
  u32 vfsFlags;               /* Flags for sqlite3_vfs.xOpen() */
ds@1952
   355
  int errCode;                /* One of several kinds of errors */
ds@1952
   356
  int dbSize;                 /* Number of pages in the file */
ds@1952
   357
  int origDbSize;             /* dbSize before the current change */
ds@1952
   358
  int stmtSize;               /* Size of database (in pages) at stmt_begin() */
ds@1952
   359
  int nRec;                   /* Number of pages written to the journal */
ds@1952
   360
  u32 cksumInit;              /* Quasi-random value added to every checksum */
ds@1952
   361
  int stmtNRec;               /* Number of records in stmt subjournal */
ds@1952
   362
  int nExtra;                 /* Add this many bytes to each in-memory page */
ds@1952
   363
  int pageSize;               /* Number of bytes in a page */
ds@1952
   364
  int nPage;                  /* Total number of in-memory pages */
ds@1952
   365
  int nRef;                   /* Number of in-memory pages with PgHdr.nRef>0 */
ds@1952
   366
  int mxPage;                 /* Maximum number of pages to hold in cache */
ds@1952
   367
  Pgno mxPgno;                /* Maximum allowed size of the database */
ds@1952
   368
  u8 *aInJournal;             /* One bit for each page in the database file */
ds@1952
   369
  u8 *aInStmt;                /* One bit for each page in the database */
ds@1952
   370
  char *zFilename;            /* Name of the database file */
ds@1952
   371
  char *zJournal;             /* Name of the journal file */
ds@1952
   372
  char *zDirectory;           /* Directory hold database and journal files */
ds@1952
   373
  char *zStmtJrnl;            /* Name of the statement journal file */
ds@1952
   374
  sqlite3_file *fd, *jfd;     /* File descriptors for database and journal */
ds@1952
   375
  sqlite3_file *stfd;         /* File descriptor for the statement subjournal*/
ds@1952
   376
  BusyHandler *pBusyHandler;  /* Pointer to sqlite.busyHandler */
ds@1952
   377
  PagerLruList lru;           /* LRU list of free pages */
ds@1952
   378
  PgHdr *pAll;                /* List of all pages */
ds@1952
   379
  PgHdr *pStmt;               /* List of pages in the statement subjournal */
ds@1952
   380
  PgHdr *pDirty;              /* List of all dirty pages */
ds@1952
   381
  i64 journalOff;             /* Current byte offset in the journal file */
ds@1952
   382
  i64 journalHdr;             /* Byte offset to previous journal header */
ds@1952
   383
  i64 stmtHdrOff;             /* First journal header written this statement */
ds@1952
   384
  i64 stmtCksum;              /* cksumInit when statement was started */
ds@1952
   385
  i64 stmtJSize;              /* Size of journal at stmt_begin() */
ds@1952
   386
  int sectorSize;             /* Assumed sector size during rollback */
ds@1952
   387
#ifdef SQLITE_TEST
ds@1952
   388
  int nHit, nMiss;            /* Cache hits and missing */
ds@1952
   389
  int nRead, nWrite;          /* Database pages read/written */
ds@1952
   390
#endif
ds@1952
   391
  void (*xDestructor)(DbPage*,int); /* Call this routine when freeing pages */
ds@1952
   392
  void (*xReiniter)(DbPage*,int);   /* Call this routine when reloading pages */
ds@1952
   393
#ifdef SQLITE_HAS_CODEC
ds@1952
   394
  void *(*xCodec)(void*,void*,Pgno,int); /* Routine for en/decoding data */
ds@1952
   395
  void *pCodecArg;            /* First argument to xCodec() */
ds@1952
   396
#endif
ds@1952
   397
  int nHash;                  /* Size of the pager hash table */
ds@1952
   398
  PgHdr **aHash;              /* Hash table to map page number to PgHdr */
ds@1952
   399
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
ds@1952
   400
  Pager *pNext;               /* Doubly linked list of pagers on which */
ds@1952
   401
  Pager *pPrev;               /* sqlite3_release_memory() will work */
ds@1952
   402
  int iInUseMM;               /* Non-zero if unavailable to MM */
ds@1952
   403
  int iInUseDB;               /* Non-zero if in sqlite3_release_memory() */
ds@1952
   404
#endif
ds@1952
   405
  char *pTmpSpace;            /* Pager.pageSize bytes of space for tmp use */
ds@1952
   406
  char dbFileVers[16];        /* Changes whenever database file changes */
ds@1952
   407
};
ds@1952
   408
ds@1952
   409
/*
ds@1952
   410
** The following global variables hold counters used for
ds@1952
   411
** testing purposes only.  These variables do not exist in
ds@1952
   412
** a non-testing build.  These variables are not thread-safe.
ds@1952
   413
*/
ds@1952
   414
#ifdef SQLITE_TEST
ds@1952
   415
int sqlite3_pager_readdb_count = 0;    /* Number of full pages read from DB */
ds@1952
   416
int sqlite3_pager_writedb_count = 0;   /* Number of full pages written to DB */
ds@1952
   417
int sqlite3_pager_writej_count = 0;    /* Number of pages written to journal */
ds@1952
   418
int sqlite3_pager_pgfree_count = 0;    /* Number of cache pages freed */
ds@1952
   419
# define PAGER_INCR(v)  v++
ds@1952
   420
#else
ds@1952
   421
# define PAGER_INCR(v)
ds@1952
   422
#endif
ds@1952
   423
ds@1952
   424
/*
ds@1952
   425
** The following variable points to the head of a double-linked list
ds@1952
   426
** of all pagers that are eligible for page stealing by the
ds@1952
   427
** sqlite3_release_memory() interface.  Access to this list is
ds@1952
   428
** protected by the SQLITE_MUTEX_STATIC_MEM2 mutex.
ds@1952
   429
*/
ds@1952
   430
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
ds@1952
   431
static Pager *sqlite3PagerList = 0;
ds@1952
   432
static PagerLruList sqlite3LruPageList = {0, 0, 0};
ds@1952
   433
#endif
ds@1952
   434
ds@1952
   435
ds@1952
   436
/*
ds@1952
   437
** Journal files begin with the following magic string.  The data
ds@1952
   438
** was obtained from /dev/random.  It is used only as a sanity check.
ds@1952
   439
**
ds@1952
   440
** Since version 2.8.0, the journal format contains additional sanity
ds@1952
   441
** checking information.  If the power fails while the journal is begin
ds@1952
   442
** written, semi-random garbage data might appear in the journal
ds@1952
   443
** file after power is restored.  If an attempt is then made
ds@1952
   444
** to roll the journal back, the database could be corrupted.  The additional
ds@1952
   445
** sanity checking data is an attempt to discover the garbage in the
ds@1952
   446
** journal and ignore it.
ds@1952
   447
**
ds@1952
   448
** The sanity checking information for the new journal format consists
ds@1952
   449
** of a 32-bit checksum on each page of data.  The checksum covers both
ds@1952
   450
** the page number and the pPager->pageSize bytes of data for the page.
ds@1952
   451
** This cksum is initialized to a 32-bit random value that appears in the
ds@1952
   452
** journal file right after the header.  The random initializer is important,
ds@1952
   453
** because garbage data that appears at the end of a journal is likely
ds@1952
   454
** data that was once in other files that have now been deleted.  If the
ds@1952
   455
** garbage data came from an obsolete journal file, the checksums might
ds@1952
   456
** be correct.  But by initializing the checksum to random value which
ds@1952
   457
** is different for every journal, we minimize that risk.
ds@1952
   458
*/
ds@1952
   459
static const unsigned char aJournalMagic[] = {
ds@1952
   460
  0xd9, 0xd5, 0x05, 0xf9, 0x20, 0xa1, 0x63, 0xd7,
ds@1952
   461
};
ds@1952
   462
ds@1952
   463
/*
ds@1952
   464
** The size of the header and of each page in the journal is determined
ds@1952
   465
** by the following macros.
ds@1952
   466
*/
ds@1952
   467
#define JOURNAL_PG_SZ(pPager)  ((pPager->pageSize) + 8)
ds@1952
   468
ds@1952
   469
/*
ds@1952
   470
** The journal header size for this pager. In the future, this could be
ds@1952
   471
** set to some value read from the disk controller. The important
ds@1952
   472
** characteristic is that it is the same size as a disk sector.
ds@1952
   473
*/
ds@1952
   474
#define JOURNAL_HDR_SZ(pPager) (pPager->sectorSize)
ds@1952
   475
ds@1952
   476
/*
ds@1952
   477
** The macro MEMDB is true if we are dealing with an in-memory database.
ds@1952
   478
** We do this as a macro so that if the SQLITE_OMIT_MEMORYDB macro is set,
ds@1952
   479
** the value of MEMDB will be a constant and the compiler will optimize
ds@1952
   480
** out code that would never execute.
ds@1952
   481
*/
ds@1952
   482
#ifdef SQLITE_OMIT_MEMORYDB
ds@1952
   483
# define MEMDB 0
ds@1952
   484
#else
ds@1952
   485
# define MEMDB pPager->memDb
ds@1952
   486
#endif
ds@1952
   487
ds@1952
   488
/*
ds@1952
   489
** Page number PAGER_MJ_PGNO is never used in an SQLite database (it is
ds@1952
   490
** reserved for working around a windows/posix incompatibility). It is
ds@1952
   491
** used in the journal to signify that the remainder of the journal file 
ds@1952
   492
** is devoted to storing a master journal name - there are no more pages to
ds@1952
   493
** roll back. See comments for function writeMasterJournal() for details.
ds@1952
   494
*/
ds@1952
   495
/* #define PAGER_MJ_PGNO(x) (PENDING_BYTE/((x)->pageSize)) */
ds@1952
   496
#define PAGER_MJ_PGNO(x) ((PENDING_BYTE/((x)->pageSize))+1)
ds@1952
   497
ds@1952
   498
/*
ds@1952
   499
** The maximum legal page number is (2^31 - 1).
ds@1952
   500
*/
ds@1952
   501
#define PAGER_MAX_PGNO 2147483647
ds@1952
   502
ds@1952
   503
/*
ds@1952
   504
** The pagerEnter() and pagerLeave() routines acquire and release
ds@1952
   505
** a mutex on each pager.  The mutex is recursive.
ds@1952
   506
**
ds@1952
   507
** This is a special-purpose mutex.  It only provides mutual exclusion
ds@1952
   508
** between the Btree and the Memory Management sqlite3_release_memory()
ds@1952
   509
** function.  It does not prevent, for example, two Btrees from accessing
ds@1952
   510
** the same pager at the same time.  Other general-purpose mutexes in
ds@1952
   511
** the btree layer handle that chore.
ds@1952
   512
*/
ds@1952
   513
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
ds@1952
   514
  static void pagerEnter(Pager *p){
ds@1952
   515
    p->iInUseDB++;
ds@1952
   516
    if( p->iInUseMM && p->iInUseDB==1 ){
ds@1952
   517
      sqlite3_mutex *mutex;
ds@1952
   518
      mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
ds@1952
   519
      p->iInUseDB = 0;
ds@1952
   520
      sqlite3_mutex_enter(mutex);
ds@1952
   521
      p->iInUseDB = 1;
ds@1952
   522
      sqlite3_mutex_leave(mutex);
ds@1952
   523
    }
ds@1952
   524
    assert( p->iInUseMM==0 );
ds@1952
   525
  }
ds@1952
   526
  static void pagerLeave(Pager *p){
ds@1952
   527
    p->iInUseDB--;
ds@1952
   528
    assert( p->iInUseDB>=0 );
ds@1952
   529
  }
ds@1952
   530
#else
ds@1952
   531
# define pagerEnter(X)
ds@1952
   532
# define pagerLeave(X)
ds@1952
   533
#endif
ds@1952
   534
ds@1952
   535
/*
ds@1952
   536
** Enable reference count tracking (for debugging) here:
ds@1952
   537
*/
ds@1952
   538
#ifdef SQLITE_DEBUG
ds@1952
   539
  int pager3_refinfo_enable = 0;
ds@1952
   540
  static void pager_refinfo(PgHdr *p){
ds@1952
   541
    static int cnt = 0;
ds@1952
   542
    if( !pager3_refinfo_enable ) return;
ds@1952
   543
    sqlite3DebugPrintf(
ds@1952
   544
       "REFCNT: %4d addr=%p nRef=%-3d total=%d\n",
ds@1952
   545
       p->pgno, PGHDR_TO_DATA(p), p->nRef, p->pPager->nRef
ds@1952
   546
    );
ds@1952
   547
    cnt++;   /* Something to set a breakpoint on */
ds@1952
   548
  }
ds@1952
   549
# define REFINFO(X)  pager_refinfo(X)
ds@1952
   550
#else
ds@1952
   551
# define REFINFO(X)
ds@1952
   552
#endif
ds@1952
   553
ds@1952
   554
/*
ds@1952
   555
** Add page pPg to the end of the linked list managed by structure
ds@1952
   556
** pList (pPg becomes the last entry in the list - the most recently 
ds@1952
   557
** used). Argument pLink should point to either pPg->free or pPg->gfree,
ds@1952
   558
** depending on whether pPg is being added to the pager-specific or
ds@1952
   559
** global LRU list.
ds@1952
   560
*/
ds@1952
   561
static void listAdd(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){
ds@1952
   562
  pLink->pNext = 0;
ds@1952
   563
  pLink->pPrev = pList->pLast;
ds@1952
   564
ds@1952
   565
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
ds@1952
   566
  assert(pLink==&pPg->free || pLink==&pPg->gfree);
ds@1952
   567
  assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList);
ds@1952
   568
#endif
ds@1952
   569
ds@1952
   570
  if( pList->pLast ){
ds@1952
   571
    int iOff = (char *)pLink - (char *)pPg;
ds@1952
   572
    PagerLruLink *pLastLink = (PagerLruLink *)(&((u8 *)pList->pLast)[iOff]);
ds@1952
   573
    pLastLink->pNext = pPg;
ds@1952
   574
  }else{
ds@1952
   575
    assert(!pList->pFirst);
ds@1952
   576
    pList->pFirst = pPg;
ds@1952
   577
  }
ds@1952
   578
ds@1952
   579
  pList->pLast = pPg;
ds@1952
   580
  if( !pList->pFirstSynced && pPg->needSync==0 ){
ds@1952
   581
    pList->pFirstSynced = pPg;
ds@1952
   582
  }
ds@1952
   583
}
ds@1952
   584
ds@1952
   585
/*
ds@1952
   586
** Remove pPg from the list managed by the structure pointed to by pList.
ds@1952
   587
**
ds@1952
   588
** Argument pLink should point to either pPg->free or pPg->gfree, depending 
ds@1952
   589
** on whether pPg is being added to the pager-specific or global LRU list.
ds@1952
   590
*/
ds@1952
   591
static void listRemove(PagerLruList *pList, PagerLruLink *pLink, PgHdr *pPg){
ds@1952
   592
  int iOff = (char *)pLink - (char *)pPg;
ds@1952
   593
ds@1952
   594
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
ds@1952
   595
  assert(pLink==&pPg->free || pLink==&pPg->gfree);
ds@1952
   596
  assert(pLink==&pPg->gfree || pList!=&sqlite3LruPageList);
ds@1952
   597
#endif
ds@1952
   598
ds@1952
   599
  if( pPg==pList->pFirst ){
ds@1952
   600
    pList->pFirst = pLink->pNext;
ds@1952
   601
  }
ds@1952
   602
  if( pPg==pList->pLast ){
ds@1952
   603
    pList->pLast = pLink->pPrev;
ds@1952
   604
  }
ds@1952
   605
  if( pLink->pPrev ){
ds@1952
   606
    PagerLruLink *pPrevLink = (PagerLruLink *)(&((u8 *)pLink->pPrev)[iOff]);
ds@1952
   607
    pPrevLink->pNext = pLink->pNext;
ds@1952
   608
  }
ds@1952
   609
  if( pLink->pNext ){
ds@1952
   610
    PagerLruLink *pNextLink = (PagerLruLink *)(&((u8 *)pLink->pNext)[iOff]);
ds@1952
   611
    pNextLink->pPrev = pLink->pPrev;
ds@1952
   612
  }
ds@1952
   613
  if( pPg==pList->pFirstSynced ){
ds@1952
   614
    PgHdr *p = pLink->pNext;
ds@1952
   615
    while( p && p->needSync ){
ds@1952
   616
      PagerLruLink *pL = (PagerLruLink *)(&((u8 *)p)[iOff]);
ds@1952
   617
      p = pL->pNext;
ds@1952
   618
    }
ds@1952
   619
    pList->pFirstSynced = p;
ds@1952
   620
  }
ds@1952
   621
ds@1952
   622
  pLink->pNext = pLink->pPrev = 0;
ds@1952
   623
}
ds@1952
   624
ds@1952
   625
/* 
ds@1952
   626
** Add page pPg to the list of free pages for the pager. If 
ds@1952
   627
** memory-management is enabled, also add the page to the global 
ds@1952
   628
** list of free pages.
ds@1952
   629
*/
ds@1952
   630
static void lruListAdd(PgHdr *pPg){
ds@1952
   631
  listAdd(&pPg->pPager->lru, &pPg->free, pPg);
ds@1952
   632
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
ds@1952
   633
  if( !pPg->pPager->memDb ){
ds@1952
   634
    sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
ds@1952
   635
    listAdd(&sqlite3LruPageList, &pPg->gfree, pPg);
ds@1952
   636
    sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
ds@1952
   637
  }
ds@1952
   638
#endif
ds@1952
   639
}
ds@1952
   640
ds@1952
   641
/* 
ds@1952
   642
** Remove page pPg from the list of free pages for the associated pager.
ds@1952
   643
** If memory-management is enabled, also remove pPg from the global list
ds@1952
   644
** of free pages.
ds@1952
   645
*/
ds@1952
   646
static void lruListRemove(PgHdr *pPg){
ds@1952
   647
  listRemove(&pPg->pPager->lru, &pPg->free, pPg);
ds@1952
   648
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
ds@1952
   649
  if( !pPg->pPager->memDb ){
ds@1952
   650
    sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
ds@1952
   651
    listRemove(&sqlite3LruPageList, &pPg->gfree, pPg);
ds@1952
   652
    sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
ds@1952
   653
  }
ds@1952
   654
#endif
ds@1952
   655
}
ds@1952
   656
ds@1952
   657
/* 
ds@1952
   658
** This function is called just after the needSync flag has been cleared
ds@1952
   659
** from all pages managed by pPager (usually because the journal file
ds@1952
   660
** has just been synced). It updates the pPager->lru.pFirstSynced variable
ds@1952
   661
** and, if memory-management is enabled, the sqlite3LruPageList.pFirstSynced
ds@1952
   662
** variable also.
ds@1952
   663
*/
ds@1952
   664
static void lruListSetFirstSynced(Pager *pPager){
ds@1952
   665
  pPager->lru.pFirstSynced = pPager->lru.pFirst;
ds@1952
   666
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
ds@1952
   667
  if( !pPager->memDb ){
ds@1952
   668
    PgHdr *p;
ds@1952
   669
    sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
ds@1952
   670
    for(p=sqlite3LruPageList.pFirst; p && p->needSync; p=p->gfree.pNext);
ds@1952
   671
    assert(p==pPager->lru.pFirstSynced || p==sqlite3LruPageList.pFirstSynced);
ds@1952
   672
    sqlite3LruPageList.pFirstSynced = p;
ds@1952
   673
    sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
ds@1952
   674
  }
ds@1952
   675
#endif
ds@1952
   676
}
ds@1952
   677
ds@1952
   678
/*
ds@1952
   679
** Return true if page *pPg has already been written to the statement
ds@1952
   680
** journal (or statement snapshot has been created, if *pPg is part
ds@1952
   681
** of an in-memory database).
ds@1952
   682
*/
ds@1952
   683
static int pageInStatement(PgHdr *pPg){
ds@1952
   684
  Pager *pPager = pPg->pPager;
ds@1952
   685
  if( MEMDB ){
ds@1952
   686
    return PGHDR_TO_HIST(pPg, pPager)->inStmt;
ds@1952
   687
  }else{
ds@1952
   688
    Pgno pgno = pPg->pgno;
ds@1952
   689
    u8 *a = pPager->aInStmt;
ds@1952
   690
    return (a && (int)pgno<=pPager->stmtSize && (a[pgno/8] & (1<<(pgno&7))));
ds@1952
   691
  }
ds@1952
   692
}
ds@1952
   693
ds@1952
   694
/*
ds@1952
   695
** Change the size of the pager hash table to N.  N must be a power
ds@1952
   696
** of two.
ds@1952
   697
*/
ds@1952
   698
static void pager_resize_hash_table(Pager *pPager, int N){
ds@1952
   699
  PgHdr **aHash, *pPg;
ds@1952
   700
  assert( N>0 && (N&(N-1))==0 );
ds@1952
   701
  pagerLeave(pPager);
ds@1952
   702
  sqlite3MallocBenignFailure((int)pPager->aHash);
ds@1952
   703
  aHash = sqlite3MallocZero( sizeof(aHash[0])*N );
ds@1952
   704
  pagerEnter(pPager);
ds@1952
   705
  if( aHash==0 ){
ds@1952
   706
    /* Failure to rehash is not an error.  It is only a performance hit. */
ds@1952
   707
    return;
ds@1952
   708
  }
ds@1952
   709
  sqlite3_free(pPager->aHash);
ds@1952
   710
  pPager->nHash = N;
ds@1952
   711
  pPager->aHash = aHash;
ds@1952
   712
  for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
ds@1952
   713
    int h;
ds@1952
   714
    if( pPg->pgno==0 ){
ds@1952
   715
      assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
ds@1952
   716
      continue;
ds@1952
   717
    }
ds@1952
   718
    h = pPg->pgno & (N-1);
ds@1952
   719
    pPg->pNextHash = aHash[h];
ds@1952
   720
    if( aHash[h] ){
ds@1952
   721
      aHash[h]->pPrevHash = pPg;
ds@1952
   722
    }
ds@1952
   723
    aHash[h] = pPg;
ds@1952
   724
    pPg->pPrevHash = 0;
ds@1952
   725
  }
ds@1952
   726
}
ds@1952
   727
ds@1952
   728
/*
ds@1952
   729
** Read a 32-bit integer from the given file descriptor.  Store the integer
ds@1952
   730
** that is read in *pRes.  Return SQLITE_OK if everything worked, or an
ds@1952
   731
** error code is something goes wrong.
ds@1952
   732
**
ds@1952
   733
** All values are stored on disk as big-endian.
ds@1952
   734
*/
ds@1952
   735
static int read32bits(sqlite3_file *fd, i64 offset, u32 *pRes){
ds@1952
   736
  unsigned char ac[4];
ds@1952
   737
  int rc = sqlite3OsRead(fd, ac, sizeof(ac), offset);
ds@1952
   738
  if( rc==SQLITE_OK ){
ds@1952
   739
    *pRes = sqlite3Get4byte(ac);
ds@1952
   740
  }
ds@1952
   741
  return rc;
ds@1952
   742
}
ds@1952
   743
ds@1952
   744
/*
ds@1952
   745
** Write a 32-bit integer into a string buffer in big-endian byte order.
ds@1952
   746
*/
ds@1952
   747
#define put32bits(A,B)  sqlite3Put4byte((u8*)A,B)
ds@1952
   748
ds@1952
   749
/*
ds@1952
   750
** Write a 32-bit integer into the given file descriptor.  Return SQLITE_OK
ds@1952
   751
** on success or an error code is something goes wrong.
ds@1952
   752
*/
ds@1952
   753
static int write32bits(sqlite3_file *fd, i64 offset, u32 val){
ds@1952
   754
  char ac[4];
ds@1952
   755
  put32bits(ac, val);
ds@1952
   756
  return sqlite3OsWrite(fd, ac, 4, offset);
ds@1952
   757
}
ds@1952
   758
ds@1952
   759
/*
ds@1952
   760
** If file pFd is open, call sqlite3OsUnlock() on it.
ds@1952
   761
*/
ds@1952
   762
static int osUnlock(sqlite3_file *pFd, int eLock){
ds@1952
   763
  if( !pFd->pMethods ){
ds@1952
   764
    return SQLITE_OK;
ds@1952
   765
  }
ds@1952
   766
  return sqlite3OsUnlock(pFd, eLock);
ds@1952
   767
}
ds@1952
   768
ds@1952
   769
/*
ds@1952
   770
** This function determines whether or not the atomic-write optimization
ds@1952
   771
** can be used with this pager. The optimization can be used if:
ds@1952
   772
**
ds@1952
   773
**  (a) the value returned by OsDeviceCharacteristics() indicates that
ds@1952
   774
**      a database page may be written atomically, and
ds@1952
   775
**  (b) the value returned by OsSectorSize() is less than or equal
ds@1952
   776
**      to the page size.
ds@1952
   777
**
ds@1952
   778
** If the optimization cannot be used, 0 is returned. If it can be used,
ds@1952
   779
** then the value returned is the size of the journal file when it
ds@1952
   780
** contains rollback data for exactly one page.
ds@1952
   781
*/
ds@1952
   782
#ifdef SQLITE_ENABLE_ATOMIC_WRITE
ds@1952
   783
static int jrnlBufferSize(Pager *pPager){
ds@1952
   784
  int dc;           /* Device characteristics */
ds@1952
   785
  int nSector;      /* Sector size */
ds@1952
   786
  int nPage;        /* Page size */
ds@1952
   787
  sqlite3_file *fd = pPager->fd;
ds@1952
   788
ds@1952
   789
  if( fd->pMethods ){
ds@1952
   790
    dc = sqlite3OsDeviceCharacteristics(fd);
ds@1952
   791
    nSector = sqlite3OsSectorSize(fd);
ds@1952
   792
    nPage = pPager->pageSize;
ds@1952
   793
  }
ds@1952
   794
ds@1952
   795
  assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
ds@1952
   796
  assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
ds@1952
   797
ds@1952
   798
  if( !fd->pMethods || (dc&(SQLITE_IOCAP_ATOMIC|(nPage>>8))&&nSector<=nPage) ){
ds@1952
   799
    return JOURNAL_HDR_SZ(pPager) + JOURNAL_PG_SZ(pPager);
ds@1952
   800
  }
ds@1952
   801
  return 0;
ds@1952
   802
}
ds@1952
   803
#endif
ds@1952
   804
ds@1952
   805
/*
ds@1952
   806
** This function should be called when an error occurs within the pager
ds@1952
   807
** code. The first argument is a pointer to the pager structure, the
ds@1952
   808
** second the error-code about to be returned by a pager API function. 
ds@1952
   809
** The value returned is a copy of the second argument to this function. 
ds@1952
   810
**
ds@1952
   811
** If the second argument is SQLITE_IOERR, SQLITE_CORRUPT, or SQLITE_FULL
ds@1952
   812
** the error becomes persistent. Until the persisten error is cleared,
ds@1952
   813
** subsequent API calls on this Pager will immediately return the same 
ds@1952
   814
** error code.
ds@1952
   815
**
ds@1952
   816
** A persistent error indicates that the contents of the pager-cache 
ds@1952
   817
** cannot be trusted. This state can be cleared by completely discarding 
ds@1952
   818
** the contents of the pager-cache. If a transaction was active when
ds@1952
   819
** the persistent error occured, then the rollback journal may need
ds@1952
   820
** to be replayed.
ds@1952
   821
*/
ds@1952
   822
static void pager_unlock(Pager *pPager);
ds@1952
   823
static int pager_error(Pager *pPager, int rc){
ds@1952
   824
  int rc2 = rc & 0xff;
ds@1952
   825
  assert(
ds@1952
   826
       pPager->errCode==SQLITE_FULL ||
ds@1952
   827
       pPager->errCode==SQLITE_OK ||
ds@1952
   828
       (pPager->errCode & 0xff)==SQLITE_IOERR
ds@1952
   829
  );
ds@1952
   830
  if(
ds@1952
   831
    rc2==SQLITE_FULL ||
ds@1952
   832
    rc2==SQLITE_IOERR ||
ds@1952
   833
    rc2==SQLITE_CORRUPT
ds@1952
   834
  ){
ds@1952
   835
    pPager->errCode = rc;
ds@1952
   836
    if( pPager->state==PAGER_UNLOCK && pPager->nRef==0 ){
ds@1952
   837
      /* If the pager is already unlocked, call pager_unlock() now to
ds@1952
   838
      ** clear the error state and ensure that the pager-cache is 
ds@1952
   839
      ** completely empty.
ds@1952
   840
      */
ds@1952
   841
      pager_unlock(pPager);
ds@1952
   842
    }
ds@1952
   843
  }
ds@1952
   844
  return rc;
ds@1952
   845
}
ds@1952
   846
ds@1952
   847
/*
ds@1952
   848
** If SQLITE_CHECK_PAGES is defined then we do some sanity checking
ds@1952
   849
** on the cache using a hash function.  This is used for testing
ds@1952
   850
** and debugging only.
ds@1952
   851
*/
ds@1952
   852
#ifdef SQLITE_CHECK_PAGES
ds@1952
   853
/*
ds@1952
   854
** Return a 32-bit hash of the page data for pPage.
ds@1952
   855
*/
ds@1952
   856
static u32 pager_datahash(int nByte, unsigned char *pData){
ds@1952
   857
  u32 hash = 0;
ds@1952
   858
  int i;
ds@1952
   859
  for(i=0; i<nByte; i++){
ds@1952
   860
    hash = (hash*1039) + pData[i];
ds@1952
   861
  }
ds@1952
   862
  return hash;
ds@1952
   863
}
ds@1952
   864
static u32 pager_pagehash(PgHdr *pPage){
ds@1952
   865
  return pager_datahash(pPage->pPager->pageSize, 
ds@1952
   866
                        (unsigned char *)PGHDR_TO_DATA(pPage));
ds@1952
   867
}
ds@1952
   868
ds@1952
   869
/*
ds@1952
   870
** The CHECK_PAGE macro takes a PgHdr* as an argument. If SQLITE_CHECK_PAGES
ds@1952
   871
** is defined, and NDEBUG is not defined, an assert() statement checks
ds@1952
   872
** that the page is either dirty or still matches the calculated page-hash.
ds@1952
   873
*/
ds@1952
   874
#define CHECK_PAGE(x) checkPage(x)
ds@1952
   875
static void checkPage(PgHdr *pPg){
ds@1952
   876
  Pager *pPager = pPg->pPager;
ds@1952
   877
  assert( !pPg->pageHash || pPager->errCode || MEMDB || pPg->dirty || 
ds@1952
   878
      pPg->pageHash==pager_pagehash(pPg) );
ds@1952
   879
}
ds@1952
   880
ds@1952
   881
#else
ds@1952
   882
#define pager_datahash(X,Y)  0
ds@1952
   883
#define pager_pagehash(X)  0
ds@1952
   884
#define CHECK_PAGE(x)
ds@1952
   885
#endif
ds@1952
   886
ds@1952
   887
/*
ds@1952
   888
** When this is called the journal file for pager pPager must be open.
ds@1952
   889
** The master journal file name is read from the end of the file and 
ds@1952
   890
** written into memory supplied by the caller. 
ds@1952
   891
**
ds@1952
   892
** zMaster must point to a buffer of at least nMaster bytes allocated by
ds@1952
   893
** the caller. This should be sqlite3_vfs.mxPathname+1 (to ensure there is
ds@1952
   894
** enough space to write the master journal name). If the master journal
ds@1952
   895
** name in the journal is longer than nMaster bytes (including a
ds@1952
   896
** nul-terminator), then this is handled as if no master journal name
ds@1952
   897
** were present in the journal.
ds@1952
   898
**
ds@1952
   899
** If no master journal file name is present zMaster[0] is set to 0 and
ds@1952
   900
** SQLITE_OK returned.
ds@1952
   901
*/
ds@1952
   902
static int readMasterJournal(sqlite3_file *pJrnl, char *zMaster, int nMaster){
ds@1952
   903
  int rc;
ds@1952
   904
  u32 len;
ds@1952
   905
  i64 szJ;
ds@1952
   906
  u32 cksum;
ds@1952
   907
  int i;
ds@1952
   908
  unsigned char aMagic[8]; /* A buffer to hold the magic header */
ds@1952
   909
ds@1952
   910
  zMaster[0] = '\0';
ds@1952
   911
ds@1952
   912
  rc = sqlite3OsFileSize(pJrnl, &szJ);
ds@1952
   913
  if( rc!=SQLITE_OK || szJ<16 ) return rc;
ds@1952
   914
ds@1952
   915
  rc = read32bits(pJrnl, szJ-16, &len);
ds@1952
   916
  if( rc!=SQLITE_OK ) return rc;
ds@1952
   917
ds@1952
   918
  if( len>=nMaster ){
ds@1952
   919
    return SQLITE_OK;
ds@1952
   920
  }
ds@1952
   921
ds@1952
   922
  rc = read32bits(pJrnl, szJ-12, &cksum);
ds@1952
   923
  if( rc!=SQLITE_OK ) return rc;
ds@1952
   924
ds@1952
   925
  rc = sqlite3OsRead(pJrnl, aMagic, 8, szJ-8);
ds@1952
   926
  if( rc!=SQLITE_OK || memcmp(aMagic, aJournalMagic, 8) ) return rc;
ds@1952
   927
ds@1952
   928
  rc = sqlite3OsRead(pJrnl, zMaster, len, szJ-16-len);
ds@1952
   929
  if( rc!=SQLITE_OK ){
ds@1952
   930
    return rc;
ds@1952
   931
  }
ds@1952
   932
  zMaster[len] = '\0';
ds@1952
   933
ds@1952
   934
  /* See if the checksum matches the master journal name */
ds@1952
   935
  for(i=0; i<len; i++){
ds@1952
   936
    cksum -= zMaster[i];
ds@1952
   937
   }
ds@1952
   938
  if( cksum ){
ds@1952
   939
    /* If the checksum doesn't add up, then one or more of the disk sectors
ds@1952
   940
    ** containing the master journal filename is corrupted. This means
ds@1952
   941
    ** definitely roll back, so just return SQLITE_OK and report a (nul)
ds@1952
   942
    ** master-journal filename.
ds@1952
   943
    */
ds@1952
   944
    zMaster[0] = '\0';
ds@1952
   945
  }
ds@1952
   946
   
ds@1952
   947
  return SQLITE_OK;
ds@1952
   948
}
ds@1952
   949
ds@1952
   950
/*
ds@1952
   951
** Seek the journal file descriptor to the next sector boundary where a
ds@1952
   952
** journal header may be read or written. Pager.journalOff is updated with
ds@1952
   953
** the new seek offset.
ds@1952
   954
**
ds@1952
   955
** i.e for a sector size of 512:
ds@1952
   956
**
ds@1952
   957
** Input Offset              Output Offset
ds@1952
   958
** ---------------------------------------
ds@1952
   959
** 0                         0
ds@1952
   960
** 512                       512
ds@1952
   961
** 100                       512
ds@1952
   962
** 2000                      2048
ds@1952
   963
** 
ds@1952
   964
*/
ds@1952
   965
static void seekJournalHdr(Pager *pPager){
ds@1952
   966
  i64 offset = 0;
ds@1952
   967
  i64 c = pPager->journalOff;
ds@1952
   968
  if( c ){
ds@1952
   969
    offset = ((c-1)/JOURNAL_HDR_SZ(pPager) + 1) * JOURNAL_HDR_SZ(pPager);
ds@1952
   970
  }
ds@1952
   971
  assert( offset%JOURNAL_HDR_SZ(pPager)==0 );
ds@1952
   972
  assert( offset>=c );
ds@1952
   973
  assert( (offset-c)<JOURNAL_HDR_SZ(pPager) );
ds@1952
   974
  pPager->journalOff = offset;
ds@1952
   975
}
ds@1952
   976
ds@1952
   977
/*
ds@1952
   978
** The journal file must be open when this routine is called. A journal
ds@1952
   979
** header (JOURNAL_HDR_SZ bytes) is written into the journal file at the
ds@1952
   980
** current location.
ds@1952
   981
**
ds@1952
   982
** The format for the journal header is as follows:
ds@1952
   983
** - 8 bytes: Magic identifying journal format.
ds@1952
   984
** - 4 bytes: Number of records in journal, or -1 no-sync mode is on.
ds@1952
   985
** - 4 bytes: Random number used for page hash.
ds@1952
   986
** - 4 bytes: Initial database page count.
ds@1952
   987
** - 4 bytes: Sector size used by the process that wrote this journal.
ds@1952
   988
** 
ds@1952
   989
** Followed by (JOURNAL_HDR_SZ - 24) bytes of unused space.
ds@1952
   990
*/
ds@1952
   991
static int writeJournalHdr(Pager *pPager){
ds@1952
   992
  char zHeader[sizeof(aJournalMagic)+16];
ds@1952
   993
  int rc;
ds@1952
   994
ds@1952
   995
  if( pPager->stmtHdrOff==0 ){
ds@1952
   996
    pPager->stmtHdrOff = pPager->journalOff;
ds@1952
   997
  }
ds@1952
   998
ds@1952
   999
  seekJournalHdr(pPager);
ds@1952
  1000
  pPager->journalHdr = pPager->journalOff;
ds@1952
  1001
ds@1952
  1002
  memcpy(zHeader, aJournalMagic, sizeof(aJournalMagic));
ds@1952
  1003
ds@1952
  1004
  /* 
ds@1952
  1005
  ** Write the nRec Field - the number of page records that follow this
ds@1952
  1006
  ** journal header. Normally, zero is written to this value at this time.
ds@1952
  1007
  ** After the records are added to the journal (and the journal synced, 
ds@1952
  1008
  ** if in full-sync mode), the zero is overwritten with the true number
ds@1952
  1009
  ** of records (see syncJournal()).
ds@1952
  1010
  **
ds@1952
  1011
  ** A faster alternative is to write 0xFFFFFFFF to the nRec field. When
ds@1952
  1012
  ** reading the journal this value tells SQLite to assume that the
ds@1952
  1013
  ** rest of the journal file contains valid page records. This assumption
ds@1952
  1014
  ** is dangerous, as if a failure occured whilst writing to the journal
ds@1952
  1015
  ** file it may contain some garbage data. There are two scenarios
ds@1952
  1016
  ** where this risk can be ignored:
ds@1952
  1017
  **
ds@1952
  1018
  **   * When the pager is in no-sync mode. Corruption can follow a
ds@1952
  1019
  **     power failure in this case anyway.
ds@1952
  1020
  **
ds@1952
  1021
  **   * When the SQLITE_IOCAP_SAFE_APPEND flag is set. This guarantees
ds@1952
  1022
  **     that garbage data is never appended to the journal file.
ds@1952
  1023
  */
ds@1952
  1024
  assert(pPager->fd->pMethods||pPager->noSync);
ds@1952
  1025
  if( (pPager->noSync) 
ds@1952
  1026
   || (sqlite3OsDeviceCharacteristics(pPager->fd)&SQLITE_IOCAP_SAFE_APPEND) 
ds@1952
  1027
  ){
ds@1952
  1028
    put32bits(&zHeader[sizeof(aJournalMagic)], 0xffffffff);
ds@1952
  1029
  }else{
ds@1952
  1030
    put32bits(&zHeader[sizeof(aJournalMagic)], 0);
ds@1952
  1031
  }
ds@1952
  1032
ds@1952
  1033
  /* The random check-hash initialiser */ 
ds@1952
  1034
  sqlite3Randomness(sizeof(pPager->cksumInit), &pPager->cksumInit);
ds@1952
  1035
  put32bits(&zHeader[sizeof(aJournalMagic)+4], pPager->cksumInit);
ds@1952
  1036
  /* The initial database size */
ds@1952
  1037
  put32bits(&zHeader[sizeof(aJournalMagic)+8], pPager->dbSize);
ds@1952
  1038
  /* The assumed sector size for this process */
ds@1952
  1039
  put32bits(&zHeader[sizeof(aJournalMagic)+12], pPager->sectorSize);
ds@1952
  1040
  IOTRACE(("JHDR %p %lld %d\n", pPager, pPager->journalHdr, sizeof(zHeader)))
ds@1952
  1041
  rc = sqlite3OsWrite(pPager->jfd, zHeader, sizeof(zHeader),pPager->journalOff);
ds@1952
  1042
  pPager->journalOff += JOURNAL_HDR_SZ(pPager);
ds@1952
  1043
ds@1952
  1044
  /* The journal header has been written successfully. Seek the journal
ds@1952
  1045
  ** file descriptor to the end of the journal header sector.
ds@1952
  1046
  */
ds@1952
  1047
  if( rc==SQLITE_OK ){
ds@1952
  1048
    IOTRACE(("JTAIL %p %lld\n", pPager, pPager->journalOff-1))
ds@1952
  1049
    rc = sqlite3OsWrite(pPager->jfd, "\000", 1, pPager->journalOff-1);
ds@1952
  1050
  }
ds@1952
  1051
  return rc;
ds@1952
  1052
}
ds@1952
  1053
ds@1952
  1054
/*
ds@1952
  1055
** The journal file must be open when this is called. A journal header file
ds@1952
  1056
** (JOURNAL_HDR_SZ bytes) is read from the current location in the journal
ds@1952
  1057
** file. See comments above function writeJournalHdr() for a description of
ds@1952
  1058
** the journal header format.
ds@1952
  1059
**
ds@1952
  1060
** If the header is read successfully, *nRec is set to the number of
ds@1952
  1061
** page records following this header and *dbSize is set to the size of the
ds@1952
  1062
** database before the transaction began, in pages. Also, pPager->cksumInit
ds@1952
  1063
** is set to the value read from the journal header. SQLITE_OK is returned
ds@1952
  1064
** in this case.
ds@1952
  1065
**
ds@1952
  1066
** If the journal header file appears to be corrupted, SQLITE_DONE is
ds@1952
  1067
** returned and *nRec and *dbSize are not set.  If JOURNAL_HDR_SZ bytes
ds@1952
  1068
** cannot be read from the journal file an error code is returned.
ds@1952
  1069
*/
ds@1952
  1070
static int readJournalHdr(
ds@1952
  1071
  Pager *pPager, 
ds@1952
  1072
  i64 journalSize,
ds@1952
  1073
  u32 *pNRec, 
ds@1952
  1074
  u32 *pDbSize
ds@1952
  1075
){
ds@1952
  1076
  int rc;
ds@1952
  1077
  unsigned char aMagic[8]; /* A buffer to hold the magic header */
ds@1952
  1078
  i64 jrnlOff;
ds@1952
  1079
ds@1952
  1080
  seekJournalHdr(pPager);
ds@1952
  1081
  if( pPager->journalOff+JOURNAL_HDR_SZ(pPager) > journalSize ){
ds@1952
  1082
    return SQLITE_DONE;
ds@1952
  1083
  }
ds@1952
  1084
  jrnlOff = pPager->journalOff;
ds@1952
  1085
ds@1952
  1086
  rc = sqlite3OsRead(pPager->jfd, aMagic, sizeof(aMagic), jrnlOff);
ds@1952
  1087
  if( rc ) return rc;
ds@1952
  1088
  jrnlOff += sizeof(aMagic);
ds@1952
  1089
ds@1952
  1090
  if( memcmp(aMagic, aJournalMagic, sizeof(aMagic))!=0 ){
ds@1952
  1091
    return SQLITE_DONE;
ds@1952
  1092
  }
ds@1952
  1093
ds@1952
  1094
  rc = read32bits(pPager->jfd, jrnlOff, pNRec);
ds@1952
  1095
  if( rc ) return rc;
ds@1952
  1096
ds@1952
  1097
  rc = read32bits(pPager->jfd, jrnlOff+4, &pPager->cksumInit);
ds@1952
  1098
  if( rc ) return rc;
ds@1952
  1099
ds@1952
  1100
  rc = read32bits(pPager->jfd, jrnlOff+8, pDbSize);
ds@1952
  1101
  if( rc ) return rc;
ds@1952
  1102
ds@1952
  1103
  /* Update the assumed sector-size to match the value used by 
ds@1952
  1104
  ** the process that created this journal. If this journal was
ds@1952
  1105
  ** created by a process other than this one, then this routine
ds@1952
  1106
  ** is being called from within pager_playback(). The local value
ds@1952
  1107
  ** of Pager.sectorSize is restored at the end of that routine.
ds@1952
  1108
  */
ds@1952
  1109
  rc = read32bits(pPager->jfd, jrnlOff+12, (u32 *)&pPager->sectorSize);
ds@1952
  1110
  if( rc ) return rc;
ds@1952
  1111
ds@1952
  1112
  pPager->journalOff += JOURNAL_HDR_SZ(pPager);
ds@1952
  1113
  return SQLITE_OK;
ds@1952
  1114
}
ds@1952
  1115
ds@1952
  1116
ds@1952
  1117
/*
ds@1952
  1118
** Write the supplied master journal name into the journal file for pager
ds@1952
  1119
** pPager at the current location. The master journal name must be the last
ds@1952
  1120
** thing written to a journal file. If the pager is in full-sync mode, the
ds@1952
  1121
** journal file descriptor is advanced to the next sector boundary before
ds@1952
  1122
** anything is written. The format is:
ds@1952
  1123
**
ds@1952
  1124
** + 4 bytes: PAGER_MJ_PGNO.
ds@1952
  1125
** + N bytes: length of master journal name.
ds@1952
  1126
** + 4 bytes: N
ds@1952
  1127
** + 4 bytes: Master journal name checksum.
ds@1952
  1128
** + 8 bytes: aJournalMagic[].
ds@1952
  1129
**
ds@1952
  1130
** The master journal page checksum is the sum of the bytes in the master
ds@1952
  1131
** journal name.
ds@1952
  1132
**
ds@1952
  1133
** If zMaster is a NULL pointer (occurs for a single database transaction), 
ds@1952
  1134
** this call is a no-op.
ds@1952
  1135
*/
ds@1952
  1136
static int writeMasterJournal(Pager *pPager, const char *zMaster){
ds@1952
  1137
  int rc;
ds@1952
  1138
  int len; 
ds@1952
  1139
  int i; 
ds@1952
  1140
  i64 jrnlOff;
ds@1952
  1141
  u32 cksum = 0;
ds@1952
  1142
  char zBuf[sizeof(aJournalMagic)+2*4];
ds@1952
  1143
ds@1952
  1144
  if( !zMaster || pPager->setMaster) return SQLITE_OK;
ds@1952
  1145
  pPager->setMaster = 1;
ds@1952
  1146
ds@1952
  1147
  len = strlen(zMaster);
ds@1952
  1148
  for(i=0; i<len; i++){
ds@1952
  1149
    cksum += zMaster[i];
ds@1952
  1150
  }
ds@1952
  1151
ds@1952
  1152
  /* If in full-sync mode, advance to the next disk sector before writing
ds@1952
  1153
  ** the master journal name. This is in case the previous page written to
ds@1952
  1154
  ** the journal has already been synced.
ds@1952
  1155
  */
ds@1952
  1156
  if( pPager->fullSync ){
ds@1952
  1157
    seekJournalHdr(pPager);
ds@1952
  1158
  }
ds@1952
  1159
  jrnlOff = pPager->journalOff;
ds@1952
  1160
  pPager->journalOff += (len+20);
ds@1952
  1161
ds@1952
  1162
  rc = write32bits(pPager->jfd, jrnlOff, PAGER_MJ_PGNO(pPager));
ds@1952
  1163
  if( rc!=SQLITE_OK ) return rc;
ds@1952
  1164
  jrnlOff += 4;
ds@1952
  1165
ds@1952
  1166
  rc = sqlite3OsWrite(pPager->jfd, zMaster, len, jrnlOff);
ds@1952
  1167
  if( rc!=SQLITE_OK ) return rc;
ds@1952
  1168
  jrnlOff += len;
ds@1952
  1169
ds@1952
  1170
  put32bits(zBuf, len);
ds@1952
  1171
  put32bits(&zBuf[4], cksum);
ds@1952
  1172
  memcpy(&zBuf[8], aJournalMagic, sizeof(aJournalMagic));
ds@1952
  1173
  rc = sqlite3OsWrite(pPager->jfd, zBuf, 8+sizeof(aJournalMagic), jrnlOff);
ds@1952
  1174
  pPager->needSync = !pPager->noSync;
ds@1952
  1175
  return rc;
ds@1952
  1176
}
ds@1952
  1177
ds@1952
  1178
/*
ds@1952
  1179
** Add or remove a page from the list of all pages that are in the
ds@1952
  1180
** statement journal.
ds@1952
  1181
**
ds@1952
  1182
** The Pager keeps a separate list of pages that are currently in
ds@1952
  1183
** the statement journal.  This helps the sqlite3PagerStmtCommit()
ds@1952
  1184
** routine run MUCH faster for the common case where there are many
ds@1952
  1185
** pages in memory but only a few are in the statement journal.
ds@1952
  1186
*/
ds@1952
  1187
static void page_add_to_stmt_list(PgHdr *pPg){
ds@1952
  1188
  Pager *pPager = pPg->pPager;
ds@1952
  1189
  PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
ds@1952
  1190
  assert( MEMDB );
ds@1952
  1191
  if( !pHist->inStmt ){
ds@1952
  1192
    assert( pHist->pPrevStmt==0 && pHist->pNextStmt==0 );
ds@1952
  1193
    if( pPager->pStmt ){
ds@1952
  1194
      PGHDR_TO_HIST(pPager->pStmt, pPager)->pPrevStmt = pPg;
ds@1952
  1195
    }
ds@1952
  1196
    pHist->pNextStmt = pPager->pStmt;
ds@1952
  1197
    pPager->pStmt = pPg;
ds@1952
  1198
    pHist->inStmt = 1;
ds@1952
  1199
  }
ds@1952
  1200
}
ds@1952
  1201
ds@1952
  1202
/*
ds@1952
  1203
** Find a page in the hash table given its page number.  Return
ds@1952
  1204
** a pointer to the page or NULL if not found.
ds@1952
  1205
*/
ds@1952
  1206
static PgHdr *pager_lookup(Pager *pPager, Pgno pgno){
ds@1952
  1207
  PgHdr *p;
ds@1952
  1208
  if( pPager->aHash==0 ) return 0;
ds@1952
  1209
  p = pPager->aHash[pgno & (pPager->nHash-1)];
ds@1952
  1210
  while( p && p->pgno!=pgno ){
ds@1952
  1211
    p = p->pNextHash;
ds@1952
  1212
  }
ds@1952
  1213
  return p;
ds@1952
  1214
}
ds@1952
  1215
ds@1952
  1216
/*
ds@1952
  1217
** Clear the in-memory cache.  This routine
ds@1952
  1218
** sets the state of the pager back to what it was when it was first
ds@1952
  1219
** opened.  Any outstanding pages are invalidated and subsequent attempts
ds@1952
  1220
** to access those pages will likely result in a coredump.
ds@1952
  1221
*/
ds@1952
  1222
static void pager_reset(Pager *pPager){
ds@1952
  1223
  PgHdr *pPg, *pNext;
ds@1952
  1224
  if( pPager->errCode ) return;
ds@1952
  1225
  for(pPg=pPager->pAll; pPg; pPg=pNext){
ds@1952
  1226
    IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
ds@1952
  1227
    PAGER_INCR(sqlite3_pager_pgfree_count);
ds@1952
  1228
    pNext = pPg->pNextAll;
ds@1952
  1229
    lruListRemove(pPg);
ds@1952
  1230
    sqlite3_free(pPg->pData);
ds@1952
  1231
    sqlite3_free(pPg);
ds@1952
  1232
  }
ds@1952
  1233
  assert(pPager->lru.pFirst==0);
ds@1952
  1234
  assert(pPager->lru.pFirstSynced==0);
ds@1952
  1235
  assert(pPager->lru.pLast==0);
ds@1952
  1236
  pPager->pStmt = 0;
ds@1952
  1237
  pPager->pAll = 0;
ds@1952
  1238
  pPager->pDirty = 0;
ds@1952
  1239
  pPager->nHash = 0;
ds@1952
  1240
  sqlite3_free(pPager->aHash);
ds@1952
  1241
  pPager->nPage = 0;
ds@1952
  1242
  pPager->aHash = 0;
ds@1952
  1243
  pPager->nRef = 0;
ds@1952
  1244
}
ds@1952
  1245
ds@1952
  1246
/*
ds@1952
  1247
** Unlock the database file. 
ds@1952
  1248
**
ds@1952
  1249
** If the pager is currently in error state, discard the contents of 
ds@1952
  1250
** the cache and reset the Pager structure internal state. If there is
ds@1952
  1251
** an open journal-file, then the next time a shared-lock is obtained
ds@1952
  1252
** on the pager file (by this or any other process), it will be
ds@1952
  1253
** treated as a hot-journal and rolled back.
ds@1952
  1254
*/
ds@1952
  1255
static void pager_unlock(Pager *pPager){
ds@1952
  1256
  if( !pPager->exclusiveMode ){
ds@1952
  1257
    if( !MEMDB ){
ds@1952
  1258
      if( pPager->fd->pMethods ){
ds@1952
  1259
        osUnlock(pPager->fd, NO_LOCK);
ds@1952
  1260
      }
ds@1952
  1261
      pPager->dbSize = -1;
ds@1952
  1262
      IOTRACE(("UNLOCK %p\n", pPager))
ds@1952
  1263
ds@1952
  1264
      /* If Pager.errCode is set, the contents of the pager cache cannot be
ds@1952
  1265
      ** trusted. Now that the pager file is unlocked, the contents of the
ds@1952
  1266
      ** cache can be discarded and the error code safely cleared.
ds@1952
  1267
      */
ds@1952
  1268
      if( pPager->errCode ){
ds@1952
  1269
        pPager->errCode = SQLITE_OK;
ds@1952
  1270
        pager_reset(pPager);
ds@1952
  1271
        if( pPager->stmtOpen ){
ds@1952
  1272
          sqlite3OsClose(pPager->stfd);
ds@1952
  1273
          sqlite3_free(pPager->aInStmt);
ds@1952
  1274
          pPager->aInStmt = 0;
ds@1952
  1275
        }
ds@1952
  1276
        if( pPager->journalOpen ){
ds@1952
  1277
          sqlite3OsClose(pPager->jfd);
ds@1952
  1278
          pPager->journalOpen = 0;
ds@1952
  1279
          sqlite3_free(pPager->aInJournal);
ds@1952
  1280
          pPager->aInJournal = 0;
ds@1952
  1281
        }
ds@1952
  1282
        pPager->stmtOpen = 0;
ds@1952
  1283
        pPager->stmtInUse = 0;
ds@1952
  1284
        pPager->journalOff = 0;
ds@1952
  1285
        pPager->journalStarted = 0;
ds@1952
  1286
        pPager->stmtAutoopen = 0;
ds@1952
  1287
        pPager->origDbSize = 0;
ds@1952
  1288
      }
ds@1952
  1289
    }
ds@1952
  1290
ds@1952
  1291
    if( !MEMDB || pPager->errCode==SQLITE_OK ){
ds@1952
  1292
      pPager->state = PAGER_UNLOCK;
ds@1952
  1293
      pPager->changeCountDone = 0;
ds@1952
  1294
    }
ds@1952
  1295
  }
ds@1952
  1296
}
ds@1952
  1297
ds@1952
  1298
/*
ds@1952
  1299
** Execute a rollback if a transaction is active and unlock the 
ds@1952
  1300
** database file. If the pager has already entered the error state, 
ds@1952
  1301
** do not attempt the rollback.
ds@1952
  1302
*/
ds@1952
  1303
static void pagerUnlockAndRollback(Pager *p){
ds@1952
  1304
  assert( p->state>=PAGER_RESERVED || p->journalOpen==0 );
ds@1952
  1305
  if( p->errCode==SQLITE_OK && p->state>=PAGER_RESERVED ){
ds@1952
  1306
    sqlite3PagerRollback(p);
ds@1952
  1307
  }
ds@1952
  1308
  pager_unlock(p);
ds@1952
  1309
  assert( p->errCode || !p->journalOpen || (p->exclusiveMode&&!p->journalOff) );
ds@1952
  1310
  assert( p->errCode || !p->stmtOpen || p->exclusiveMode );
ds@1952
  1311
}
ds@1952
  1312
ds@1952
  1313
/*
ds@1952
  1314
** This routine ends a transaction.  A transaction is ended by either
ds@1952
  1315
** a COMMIT or a ROLLBACK.
ds@1952
  1316
**
ds@1952
  1317
** When this routine is called, the pager has the journal file open and
ds@1952
  1318
** a RESERVED or EXCLUSIVE lock on the database.  This routine will release
ds@1952
  1319
** the database lock and acquires a SHARED lock in its place if that is
ds@1952
  1320
** the appropriate thing to do.  Release locks usually is appropriate,
ds@1952
  1321
** unless we are in exclusive access mode or unless this is a 
ds@1952
  1322
** COMMIT AND BEGIN or ROLLBACK AND BEGIN operation.
ds@1952
  1323
**
ds@1952
  1324
** The journal file is either deleted or truncated.
ds@1952
  1325
**
ds@1952
  1326
** TODO: Consider keeping the journal file open for temporary databases.
ds@1952
  1327
** This might give a performance improvement on windows where opening
ds@1952
  1328
** a file is an expensive operation.
ds@1952
  1329
*/
ds@1952
  1330
static int pager_end_transaction(Pager *pPager){
ds@1952
  1331
  PgHdr *pPg;
ds@1952
  1332
  int rc = SQLITE_OK;
ds@1952
  1333
  int rc2 = SQLITE_OK;
ds@1952
  1334
  assert( !MEMDB );
ds@1952
  1335
  if( pPager->state<PAGER_RESERVED ){
ds@1952
  1336
    return SQLITE_OK;
ds@1952
  1337
  }
ds@1952
  1338
  sqlite3PagerStmtCommit(pPager);
ds@1952
  1339
  if( pPager->stmtOpen && !pPager->exclusiveMode ){
ds@1952
  1340
    sqlite3OsClose(pPager->stfd);
ds@1952
  1341
    pPager->stmtOpen = 0;
ds@1952
  1342
  }
ds@1952
  1343
  if( pPager->journalOpen ){
ds@1952
  1344
    if( pPager->exclusiveMode 
ds@1952
  1345
          && (rc = sqlite3OsTruncate(pPager->jfd, 0))==SQLITE_OK ){;
ds@1952
  1346
      pPager->journalOff = 0;
ds@1952
  1347
      pPager->journalStarted = 0;
ds@1952
  1348
    }else{
ds@1952
  1349
      sqlite3OsClose(pPager->jfd);
ds@1952
  1350
      pPager->journalOpen = 0;
ds@1952
  1351
      if( rc==SQLITE_OK ){
ds@1952
  1352
        rc = sqlite3OsDelete(pPager->pVfs, pPager->zJournal, 0);
ds@1952
  1353
      }
ds@1952
  1354
    }
ds@1952
  1355
    sqlite3_free( pPager->aInJournal );
ds@1952
  1356
    pPager->aInJournal = 0;
ds@1952
  1357
    for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
ds@1952
  1358
      pPg->inJournal = 0;
ds@1952
  1359
      pPg->dirty = 0;
ds@1952
  1360
      pPg->needSync = 0;
ds@1952
  1361
      pPg->alwaysRollback = 0;
ds@1952
  1362
#ifdef SQLITE_CHECK_PAGES
ds@1952
  1363
      pPg->pageHash = pager_pagehash(pPg);
ds@1952
  1364
#endif
ds@1952
  1365
    }
ds@1952
  1366
    pPager->pDirty = 0;
ds@1952
  1367
    pPager->dirtyCache = 0;
ds@1952
  1368
    pPager->nRec = 0;
ds@1952
  1369
  }else{
ds@1952
  1370
    assert( pPager->aInJournal==0 );
ds@1952
  1371
    assert( pPager->dirtyCache==0 || pPager->useJournal==0 );
ds@1952
  1372
  }
ds@1952
  1373
ds@1952
  1374
  if( !pPager->exclusiveMode ){
ds@1952
  1375
    rc2 = osUnlock(pPager->fd, SHARED_LOCK);
ds@1952
  1376
    pPager->state = PAGER_SHARED;
ds@1952
  1377
  }else if( pPager->state==PAGER_SYNCED ){
ds@1952
  1378
    pPager->state = PAGER_EXCLUSIVE;
ds@1952
  1379
  }
ds@1952
  1380
  pPager->origDbSize = 0;
ds@1952
  1381
  pPager->setMaster = 0;
ds@1952
  1382
  pPager->needSync = 0;
ds@1952
  1383
  lruListSetFirstSynced(pPager);
ds@1952
  1384
  pPager->dbSize = -1;
ds@1952
  1385
ds@1952
  1386
  return (rc==SQLITE_OK?rc2:rc);
ds@1952
  1387
}
ds@1952
  1388
ds@1952
  1389
/*
ds@1952
  1390
** Compute and return a checksum for the page of data.
ds@1952
  1391
**
ds@1952
  1392
** This is not a real checksum.  It is really just the sum of the 
ds@1952
  1393
** random initial value and the page number.  We experimented with
ds@1952
  1394
** a checksum of the entire data, but that was found to be too slow.
ds@1952
  1395
**
ds@1952
  1396
** Note that the page number is stored at the beginning of data and
ds@1952
  1397
** the checksum is stored at the end.  This is important.  If journal
ds@1952
  1398
** corruption occurs due to a power failure, the most likely scenario
ds@1952
  1399
** is that one end or the other of the record will be changed.  It is
ds@1952
  1400
** much less likely that the two ends of the journal record will be
ds@1952
  1401
** correct and the middle be corrupt.  Thus, this "checksum" scheme,
ds@1952
  1402
** though fast and simple, catches the mostly likely kind of corruption.
ds@1952
  1403
**
ds@1952
  1404
** FIX ME:  Consider adding every 200th (or so) byte of the data to the
ds@1952
  1405
** checksum.  That way if a single page spans 3 or more disk sectors and
ds@1952
  1406
** only the middle sector is corrupt, we will still have a reasonable
ds@1952
  1407
** chance of failing the checksum and thus detecting the problem.
ds@1952
  1408
*/
ds@1952
  1409
static u32 pager_cksum(Pager *pPager, const u8 *aData){
ds@1952
  1410
  u32 cksum = pPager->cksumInit;
ds@1952
  1411
  int i = pPager->pageSize-200;
ds@1952
  1412
  while( i>0 ){
ds@1952
  1413
    cksum += aData[i];
ds@1952
  1414
    i -= 200;
ds@1952
  1415
  }
ds@1952
  1416
  return cksum;
ds@1952
  1417
}
ds@1952
  1418
ds@1952
  1419
/* Forward declaration */
ds@1952
  1420
static void makeClean(PgHdr*);
ds@1952
  1421
ds@1952
  1422
/*
ds@1952
  1423
** Read a single page from the journal file opened on file descriptor
ds@1952
  1424
** jfd.  Playback this one page.
ds@1952
  1425
**
ds@1952
  1426
** If useCksum==0 it means this journal does not use checksums.  Checksums
ds@1952
  1427
** are not used in statement journals because statement journals do not
ds@1952
  1428
** need to survive power failures.
ds@1952
  1429
*/
ds@1952
  1430
static int pager_playback_one_page(
ds@1952
  1431
  Pager *pPager, 
ds@1952
  1432
  sqlite3_file *jfd,
ds@1952
  1433
  i64 offset,
ds@1952
  1434
  int useCksum
ds@1952
  1435
){
ds@1952
  1436
  int rc;
ds@1952
  1437
  PgHdr *pPg;                   /* An existing page in the cache */
ds@1952
  1438
  Pgno pgno;                    /* The page number of a page in journal */
ds@1952
  1439
  u32 cksum;                    /* Checksum used for sanity checking */
ds@1952
  1440
  u8 *aData = (u8 *)pPager->pTmpSpace;   /* Temp storage for a page */
ds@1952
  1441
ds@1952
  1442
  /* useCksum should be true for the main journal and false for
ds@1952
  1443
  ** statement journals.  Verify that this is always the case
ds@1952
  1444
  */
ds@1952
  1445
  assert( jfd == (useCksum ? pPager->jfd : pPager->stfd) );
ds@1952
  1446
  assert( aData );
ds@1952
  1447
ds@1952
  1448
  rc = read32bits(jfd, offset, &pgno);
ds@1952
  1449
  if( rc!=SQLITE_OK ) return rc;
ds@1952
  1450
  rc = sqlite3OsRead(jfd, aData, pPager->pageSize, offset+4);
ds@1952
  1451
  if( rc!=SQLITE_OK ) return rc;
ds@1952
  1452
  pPager->journalOff += pPager->pageSize + 4;
ds@1952
  1453
ds@1952
  1454
  /* Sanity checking on the page.  This is more important that I originally
ds@1952
  1455
  ** thought.  If a power failure occurs while the journal is being written,
ds@1952
  1456
  ** it could cause invalid data to be written into the journal.  We need to
ds@1952
  1457
  ** detect this invalid data (with high probability) and ignore it.
ds@1952
  1458
  */
ds@1952
  1459
  if( pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
ds@1952
  1460
    return SQLITE_DONE;
ds@1952
  1461
  }
ds@1952
  1462
  if( pgno>(unsigned)pPager->dbSize ){
ds@1952
  1463
    return SQLITE_OK;
ds@1952
  1464
  }
ds@1952
  1465
  if( useCksum ){
ds@1952
  1466
    rc = read32bits(jfd, offset+pPager->pageSize+4, &cksum);
ds@1952
  1467
    if( rc ) return rc;
ds@1952
  1468
    pPager->journalOff += 4;
ds@1952
  1469
    if( pager_cksum(pPager, aData)!=cksum ){
ds@1952
  1470
      return SQLITE_DONE;
ds@1952
  1471
    }
ds@1952
  1472
  }
ds@1952
  1473
ds@1952
  1474
  assert( pPager->state==PAGER_RESERVED || pPager->state>=PAGER_EXCLUSIVE );
ds@1952
  1475
ds@1952
  1476
  /* If the pager is in RESERVED state, then there must be a copy of this
ds@1952
  1477
  ** page in the pager cache. In this case just update the pager cache,
ds@1952
  1478
  ** not the database file. The page is left marked dirty in this case.
ds@1952
  1479
  **
ds@1952
  1480
  ** An exception to the above rule: If the database is in no-sync mode
ds@1952
  1481
  ** and a page is moved during an incremental vacuum then the page may
ds@1952
  1482
  ** not be in the pager cache. Later: if a malloc() or IO error occurs
ds@1952
  1483
  ** during a Movepage() call, then the page may not be in the cache
ds@1952
  1484
  ** either. So the condition described in the above paragraph is not
ds@1952
  1485
  ** assert()able.
ds@1952
  1486
  **
ds@1952
  1487
  ** If in EXCLUSIVE state, then we update the pager cache if it exists
ds@1952
  1488
  ** and the main file. The page is then marked not dirty.
ds@1952
  1489
  **
ds@1952
  1490
  ** Ticket #1171:  The statement journal might contain page content that is
ds@1952
  1491
  ** different from the page content at the start of the transaction.
ds@1952
  1492
  ** This occurs when a page is changed prior to the start of a statement
ds@1952
  1493
  ** then changed again within the statement.  When rolling back such a
ds@1952
  1494
  ** statement we must not write to the original database unless we know
ds@1952
  1495
  ** for certain that original page contents are synced into the main rollback
ds@1952
  1496
  ** journal.  Otherwise, a power loss might leave modified data in the
ds@1952
  1497
  ** database file without an entry in the rollback journal that can
ds@1952
  1498
  ** restore the database to its original form.  Two conditions must be
ds@1952
  1499
  ** met before writing to the database files. (1) the database must be
ds@1952
  1500
  ** locked.  (2) we know that the original page content is fully synced
ds@1952
  1501
  ** in the main journal either because the page is not in cache or else
ds@1952
  1502
  ** the page is marked as needSync==0.
ds@1952
  1503
  */
ds@1952
  1504
  pPg = pager_lookup(pPager, pgno);
ds@1952
  1505
  PAGERTRACE4("PLAYBACK %d page %d hash(%08x)\n",
ds@1952
  1506
               PAGERID(pPager), pgno, pager_datahash(pPager->pageSize, aData));
ds@1952
  1507
  if( pPager->state>=PAGER_EXCLUSIVE && (pPg==0 || pPg->needSync==0) ){
ds@1952
  1508
    i64 offset = (pgno-1)*(i64)pPager->pageSize;
ds@1952
  1509
    rc = sqlite3OsWrite(pPager->fd, aData, pPager->pageSize, offset);
ds@1952
  1510
    if( pPg ){
ds@1952
  1511
      makeClean(pPg);
ds@1952
  1512
    }
ds@1952
  1513
  }
ds@1952
  1514
  if( pPg ){
ds@1952
  1515
    /* No page should ever be explicitly rolled back that is in use, except
ds@1952
  1516
    ** for page 1 which is held in use in order to keep the lock on the
ds@1952
  1517
    ** database active. However such a page may be rolled back as a result
ds@1952
  1518
    ** of an internal error resulting in an automatic call to
ds@1952
  1519
    ** sqlite3PagerRollback().
ds@1952
  1520
    */
ds@1952
  1521
    void *pData;
ds@1952
  1522
    /* assert( pPg->nRef==0 || pPg->pgno==1 ); */
ds@1952
  1523
    pData = PGHDR_TO_DATA(pPg);
ds@1952
  1524
    memcpy(pData, aData, pPager->pageSize);
ds@1952
  1525
    if( pPager->xReiniter ){
ds@1952
  1526
      pPager->xReiniter(pPg, pPager->pageSize);
ds@1952
  1527
    }
ds@1952
  1528
#ifdef SQLITE_CHECK_PAGES
ds@1952
  1529
    pPg->pageHash = pager_pagehash(pPg);
ds@1952
  1530
#endif
ds@1952
  1531
    /* If this was page 1, then restore the value of Pager.dbFileVers.
ds@1952
  1532
    ** Do this before any decoding. */
ds@1952
  1533
    if( pgno==1 ){
ds@1952
  1534
      memcpy(&pPager->dbFileVers, &((u8*)pData)[24],sizeof(pPager->dbFileVers));
ds@1952
  1535
    }
ds@1952
  1536
ds@1952
  1537
    /* Decode the page just read from disk */
ds@1952
  1538
    CODEC1(pPager, pData, pPg->pgno, 3);
ds@1952
  1539
  }
ds@1952
  1540
  return rc;
ds@1952
  1541
}
ds@1952
  1542
ds@1952
  1543
/*
ds@1952
  1544
** Parameter zMaster is the name of a master journal file. A single journal
ds@1952
  1545
** file that referred to the master journal file has just been rolled back.
ds@1952
  1546
** This routine checks if it is possible to delete the master journal file,
ds@1952
  1547
** and does so if it is.
ds@1952
  1548
**
ds@1952
  1549
** Argument zMaster may point to Pager.pTmpSpace. So that buffer is not 
ds@1952
  1550
** available for use within this function.
ds@1952
  1551
**
ds@1952
  1552
**
ds@1952
  1553
** The master journal file contains the names of all child journals.
ds@1952
  1554
** To tell if a master journal can be deleted, check to each of the
ds@1952
  1555
** children.  If all children are either missing or do not refer to
ds@1952
  1556
** a different master journal, then this master journal can be deleted.
ds@1952
  1557
*/
ds@1952
  1558
static int pager_delmaster(Pager *pPager, const char *zMaster){
ds@1952
  1559
  sqlite3_vfs *pVfs = pPager->pVfs;
ds@1952
  1560
  int rc;
ds@1952
  1561
  int master_open = 0;
ds@1952
  1562
  sqlite3_file *pMaster;
ds@1952
  1563
  sqlite3_file *pJournal;
ds@1952
  1564
  char *zMasterJournal = 0; /* Contents of master journal file */
ds@1952
  1565
  i64 nMasterJournal;       /* Size of master journal file */
ds@1952
  1566
ds@1952
  1567
  /* Open the master journal file exclusively in case some other process
ds@1952
  1568
  ** is running this routine also. Not that it makes too much difference.
ds@1952
  1569
  */
ds@1952
  1570
  pMaster = (sqlite3_file *)sqlite3_malloc(pVfs->szOsFile * 2);
ds@1952
  1571
  pJournal = (sqlite3_file *)(((u8 *)pMaster) + pVfs->szOsFile);
ds@1952
  1572
  if( !pMaster ){
ds@1952
  1573
    rc = SQLITE_NOMEM;
ds@1952
  1574
  }else{
ds@1952
  1575
    int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MASTER_JOURNAL);
ds@1952
  1576
    rc = sqlite3OsOpen(pVfs, zMaster, pMaster, flags, 0);
ds@1952
  1577
  }
ds@1952
  1578
  if( rc!=SQLITE_OK ) goto delmaster_out;
ds@1952
  1579
  master_open = 1;
ds@1952
  1580
ds@1952
  1581
  rc = sqlite3OsFileSize(pMaster, &nMasterJournal);
ds@1952
  1582
  if( rc!=SQLITE_OK ) goto delmaster_out;
ds@1952
  1583
ds@1952
  1584
  if( nMasterJournal>0 ){
ds@1952
  1585
    char *zJournal;
ds@1952
  1586
    char *zMasterPtr = 0;
ds@1952
  1587
    int nMasterPtr = pPager->pVfs->mxPathname+1;
ds@1952
  1588
ds@1952
  1589
    /* Load the entire master journal file into space obtained from
ds@1952
  1590
    ** sqlite3_malloc() and pointed to by zMasterJournal. 
ds@1952
  1591
    */
ds@1952
  1592
    zMasterJournal = (char *)sqlite3_malloc(nMasterJournal + nMasterPtr);
ds@1952
  1593
    if( !zMasterJournal ){
ds@1952
  1594
      rc = SQLITE_NOMEM;
ds@1952
  1595
      goto delmaster_out;
ds@1952
  1596
    }
ds@1952
  1597
    zMasterPtr = &zMasterJournal[nMasterJournal];
ds@1952
  1598
    rc = sqlite3OsRead(pMaster, zMasterJournal, nMasterJournal, 0);
ds@1952
  1599
    if( rc!=SQLITE_OK ) goto delmaster_out;
ds@1952
  1600
ds@1952
  1601
    zJournal = zMasterJournal;
ds@1952
  1602
    while( (zJournal-zMasterJournal)<nMasterJournal ){
ds@1952
  1603
      if( sqlite3OsAccess(pVfs, zJournal, SQLITE_ACCESS_EXISTS) ){
ds@1952
  1604
        /* One of the journals pointed to by the master journal exists.
ds@1952
  1605
        ** Open it and check if it points at the master journal. If
ds@1952
  1606
        ** so, return without deleting the master journal file.
ds@1952
  1607
        */
ds@1952
  1608
        int c;
ds@1952
  1609
        int flags = (SQLITE_OPEN_READONLY|SQLITE_OPEN_MAIN_JOURNAL);
ds@1952
  1610
        rc = sqlite3OsOpen(pVfs, zJournal, pJournal, flags, 0);
ds@1952
  1611
        if( rc!=SQLITE_OK ){
ds@1952
  1612
          goto delmaster_out;
ds@1952
  1613
        }
ds@1952
  1614
ds@1952
  1615
        rc = readMasterJournal(pJournal, zMasterPtr, nMasterPtr);
ds@1952
  1616
        sqlite3OsClose(pJournal);
ds@1952
  1617
        if( rc!=SQLITE_OK ){
ds@1952
  1618
          goto delmaster_out;
ds@1952
  1619
        }
ds@1952
  1620
ds@1952
  1621
        c = zMasterPtr[0]!=0 && strcmp(zMasterPtr, zMaster)==0;
ds@1952
  1622
        if( c ){
ds@1952
  1623
          /* We have a match. Do not delete the master journal file. */
ds@1952
  1624
          goto delmaster_out;
ds@1952
  1625
        }
ds@1952
  1626
      }
ds@1952
  1627
      zJournal += (strlen(zJournal)+1);
ds@1952
  1628
    }
ds@1952
  1629
  }
ds@1952
  1630
  
ds@1952
  1631
  rc = sqlite3OsDelete(pVfs, zMaster, 0);
ds@1952
  1632
ds@1952
  1633
delmaster_out:
ds@1952
  1634
  if( zMasterJournal ){
ds@1952
  1635
    sqlite3_free(zMasterJournal);
ds@1952
  1636
  }  
ds@1952
  1637
  if( master_open ){
ds@1952
  1638
    sqlite3OsClose(pMaster);
ds@1952
  1639
  }
ds@1952
  1640
  sqlite3_free(pMaster);
ds@1952
  1641
  return rc;
ds@1952
  1642
}
ds@1952
  1643
ds@1952
  1644
ds@1952
  1645
static void pager_truncate_cache(Pager *pPager);
ds@1952
  1646
ds@1952
  1647
/*
ds@1952
  1648
** Truncate the main file of the given pager to the number of pages
ds@1952
  1649
** indicated. Also truncate the cached representation of the file.
ds@1952
  1650
*/
ds@1952
  1651
static int pager_truncate(Pager *pPager, int nPage){
ds@1952
  1652
  int rc = SQLITE_OK;
ds@1952
  1653
  if( pPager->state>=PAGER_EXCLUSIVE && pPager->fd->pMethods ){
ds@1952
  1654
    rc = sqlite3OsTruncate(pPager->fd, pPager->pageSize*(i64)nPage);
ds@1952
  1655
  }
ds@1952
  1656
  if( rc==SQLITE_OK ){
ds@1952
  1657
    pPager->dbSize = nPage;
ds@1952
  1658
    pager_truncate_cache(pPager);
ds@1952
  1659
  }
ds@1952
  1660
  return rc;
ds@1952
  1661
}
ds@1952
  1662
ds@1952
  1663
/*
ds@1952
  1664
** Set the sectorSize for the given pager.
ds@1952
  1665
**
ds@1952
  1666
** The sector size is the larger of the sector size reported
ds@1952
  1667
** by sqlite3OsSectorSize() and the pageSize.
ds@1952
  1668
*/
ds@1952
  1669
static void setSectorSize(Pager *pPager){
ds@1952
  1670
  assert(pPager->fd->pMethods||pPager->tempFile);
ds@1952
  1671
  if( !pPager->tempFile ){
ds@1952
  1672
    /* Sector size doesn't matter for temporary files. Also, the file
ds@1952
  1673
    ** may not have been opened yet, in whcih case the OsSectorSize()
ds@1952
  1674
    ** call will segfault.
ds@1952
  1675
    */
ds@1952
  1676
    pPager->sectorSize = sqlite3OsSectorSize(pPager->fd);
ds@1952
  1677
  }
ds@1952
  1678
  if( pPager->sectorSize<pPager->pageSize ){
ds@1952
  1679
    pPager->sectorSize = pPager->pageSize;
ds@1952
  1680
  }
ds@1952
  1681
}
ds@1952
  1682
ds@1952
  1683
/*
ds@1952
  1684
** Playback the journal and thus restore the database file to
ds@1952
  1685
** the state it was in before we started making changes.  
ds@1952
  1686
**
ds@1952
  1687
** The journal file format is as follows: 
ds@1952
  1688
**
ds@1952
  1689
**  (1)  8 byte prefix.  A copy of aJournalMagic[].
ds@1952
  1690
**  (2)  4 byte big-endian integer which is the number of valid page records
ds@1952
  1691
**       in the journal.  If this value is 0xffffffff, then compute the
ds@1952
  1692
**       number of page records from the journal size.
ds@1952
  1693
**  (3)  4 byte big-endian integer which is the initial value for the 
ds@1952
  1694
**       sanity checksum.
ds@1952
  1695
**  (4)  4 byte integer which is the number of pages to truncate the
ds@1952
  1696
**       database to during a rollback.
ds@1952
  1697
**  (5)  4 byte integer which is the number of bytes in the master journal
ds@1952
  1698
**       name.  The value may be zero (indicate that there is no master
ds@1952
  1699
**       journal.)
ds@1952
  1700
**  (6)  N bytes of the master journal name.  The name will be nul-terminated
ds@1952
  1701
**       and might be shorter than the value read from (5).  If the first byte
ds@1952
  1702
**       of the name is \000 then there is no master journal.  The master
ds@1952
  1703
**       journal name is stored in UTF-8.
ds@1952
  1704
**  (7)  Zero or more pages instances, each as follows:
ds@1952
  1705
**        +  4 byte page number.
ds@1952
  1706
**        +  pPager->pageSize bytes of data.
ds@1952
  1707
**        +  4 byte checksum
ds@1952
  1708
**
ds@1952
  1709
** When we speak of the journal header, we mean the first 6 items above.
ds@1952
  1710
** Each entry in the journal is an instance of the 7th item.
ds@1952
  1711
**
ds@1952
  1712
** Call the value from the second bullet "nRec".  nRec is the number of
ds@1952
  1713
** valid page entries in the journal.  In most cases, you can compute the
ds@1952
  1714
** value of nRec from the size of the journal file.  But if a power
ds@1952
  1715
** failure occurred while the journal was being written, it could be the
ds@1952
  1716
** case that the size of the journal file had already been increased but
ds@1952
  1717
** the extra entries had not yet made it safely to disk.  In such a case,
ds@1952
  1718
** the value of nRec computed from the file size would be too large.  For
ds@1952
  1719
** that reason, we always use the nRec value in the header.
ds@1952
  1720
**
ds@1952
  1721
** If the nRec value is 0xffffffff it means that nRec should be computed
ds@1952
  1722
** from the file size.  This value is used when the user selects the
ds@1952
  1723
** no-sync option for the journal.  A power failure could lead to corruption
ds@1952
  1724
** in this case.  But for things like temporary table (which will be
ds@1952
  1725
** deleted when the power is restored) we don't care.  
ds@1952
  1726
**
ds@1952
  1727
** If the file opened as the journal file is not a well-formed
ds@1952
  1728
** journal file then all pages up to the first corrupted page are rolled
ds@1952
  1729
** back (or no pages if the journal header is corrupted). The journal file
ds@1952
  1730
** is then deleted and SQLITE_OK returned, just as if no corruption had
ds@1952
  1731
** been encountered.
ds@1952
  1732
**
ds@1952
  1733
** If an I/O or malloc() error occurs, the journal-file is not deleted
ds@1952
  1734
** and an error code is returned.
ds@1952
  1735
*/
ds@1952
  1736
static int pager_playback(Pager *pPager, int isHot){
ds@1952
  1737
  sqlite3_vfs *pVfs = pPager->pVfs;
ds@1952
  1738
  i64 szJ;                 /* Size of the journal file in bytes */
ds@1952
  1739
  u32 nRec;                /* Number of Records in the journal */
ds@1952
  1740
  int i;                   /* Loop counter */
ds@1952
  1741
  Pgno mxPg = 0;           /* Size of the original file in pages */
ds@1952
  1742
  int rc;                  /* Result code of a subroutine */
ds@1952
  1743
  char *zMaster = 0;       /* Name of master journal file if any */
ds@1952
  1744
ds@1952
  1745
  /* Figure out how many records are in the journal.  Abort early if
ds@1952
  1746
  ** the journal is empty.
ds@1952
  1747
  */
ds@1952
  1748
  assert( pPager->journalOpen );
ds@1952
  1749
  rc = sqlite3OsFileSize(pPager->jfd, &szJ);
ds@1952
  1750
  if( rc!=SQLITE_OK || szJ==0 ){
ds@1952
  1751
    goto end_playback;
ds@1952
  1752
  }
ds@1952
  1753
ds@1952
  1754
  /* Read the master journal name from the journal, if it is present.
ds@1952
  1755
  ** If a master journal file name is specified, but the file is not
ds@1952
  1756
  ** present on disk, then the journal is not hot and does not need to be
ds@1952
  1757
  ** played back.
ds@1952
  1758
  */
ds@1952
  1759
  zMaster = pPager->pTmpSpace;
ds@1952
  1760
  rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
ds@1952
  1761
  assert( rc!=SQLITE_DONE );
ds@1952
  1762
  if( rc!=SQLITE_OK 
ds@1952
  1763
   || (zMaster[0] && !sqlite3OsAccess(pVfs, zMaster, SQLITE_ACCESS_EXISTS)) 
ds@1952
  1764
  ){
ds@1952
  1765
    zMaster = 0;
ds@1952
  1766
    if( rc==SQLITE_DONE ) rc = SQLITE_OK;
ds@1952
  1767
    goto end_playback;
ds@1952
  1768
  }
ds@1952
  1769
  pPager->journalOff = 0;
ds@1952
  1770
  zMaster = 0;
ds@1952
  1771
ds@1952
  1772
  /* This loop terminates either when the readJournalHdr() call returns
ds@1952
  1773
  ** SQLITE_DONE or an IO error occurs. */
ds@1952
  1774
  while( 1 ){
ds@1952
  1775
ds@1952
  1776
    /* Read the next journal header from the journal file.  If there are
ds@1952
  1777
    ** not enough bytes left in the journal file for a complete header, or
ds@1952
  1778
    ** it is corrupted, then a process must of failed while writing it.
ds@1952
  1779
    ** This indicates nothing more needs to be rolled back.
ds@1952
  1780
    */
ds@1952
  1781
    rc = readJournalHdr(pPager, szJ, &nRec, &mxPg);
ds@1952
  1782
    if( rc!=SQLITE_OK ){ 
ds@1952
  1783
      if( rc==SQLITE_DONE ){
ds@1952
  1784
        rc = SQLITE_OK;
ds@1952
  1785
      }
ds@1952
  1786
      goto end_playback;
ds@1952
  1787
    }
ds@1952
  1788
ds@1952
  1789
    /* If nRec is 0xffffffff, then this journal was created by a process
ds@1952
  1790
    ** working in no-sync mode. This means that the rest of the journal
ds@1952
  1791
    ** file consists of pages, there are no more journal headers. Compute
ds@1952
  1792
    ** the value of nRec based on this assumption.
ds@1952
  1793
    */
ds@1952
  1794
    if( nRec==0xffffffff ){
ds@1952
  1795
      assert( pPager->journalOff==JOURNAL_HDR_SZ(pPager) );
ds@1952
  1796
      nRec = (szJ - JOURNAL_HDR_SZ(pPager))/JOURNAL_PG_SZ(pPager);
ds@1952
  1797
    }
ds@1952
  1798
ds@1952
  1799
    /* If nRec is 0 and this rollback is of a transaction created by this
ds@1952
  1800
    ** process and if this is the final header in the journal, then it means
ds@1952
  1801
    ** that this part of the journal was being filled but has not yet been
ds@1952
  1802
    ** synced to disk.  Compute the number of pages based on the remaining
ds@1952
  1803
    ** size of the file.
ds@1952
  1804
    **
ds@1952
  1805
    ** The third term of the test was added to fix ticket #2565.
ds@1952
  1806
    */
ds@1952
  1807
    if( nRec==0 && !isHot &&
ds@1952
  1808
        pPager->journalHdr+JOURNAL_HDR_SZ(pPager)==pPager->journalOff ){
ds@1952
  1809
      nRec = (szJ - pPager->journalOff) / JOURNAL_PG_SZ(pPager);
ds@1952
  1810
    }
ds@1952
  1811
ds@1952
  1812
    /* If this is the first header read from the journal, truncate the
ds@1952
  1813
    ** database file back to it's original size.
ds@1952
  1814
    */
ds@1952
  1815
    if( pPager->journalOff==JOURNAL_HDR_SZ(pPager) ){
ds@1952
  1816
      rc = pager_truncate(pPager, mxPg);
ds@1952
  1817
      if( rc!=SQLITE_OK ){
ds@1952
  1818
        goto end_playback;
ds@1952
  1819
      }
ds@1952
  1820
    }
ds@1952
  1821
ds@1952
  1822
    /* Copy original pages out of the journal and back into the database file.
ds@1952
  1823
    */
ds@1952
  1824
    for(i=0; i<nRec; i++){
ds@1952
  1825
      rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
ds@1952
  1826
      if( rc!=SQLITE_OK ){
ds@1952
  1827
        if( rc==SQLITE_DONE ){
ds@1952
  1828
          rc = SQLITE_OK;
ds@1952
  1829
          pPager->journalOff = szJ;
ds@1952
  1830
          break;
ds@1952
  1831
        }else{
ds@1952
  1832
          goto end_playback;
ds@1952
  1833
        }
ds@1952
  1834
      }
ds@1952
  1835
    }
ds@1952
  1836
  }
ds@1952
  1837
  /*NOTREACHED*/
ds@1952
  1838
  assert( 0 );
ds@1952
  1839
ds@1952
  1840
end_playback:
ds@1952
  1841
  if( rc==SQLITE_OK ){
ds@1952
  1842
    zMaster = pPager->pTmpSpace;
ds@1952
  1843
    rc = readMasterJournal(pPager->jfd, zMaster, pPager->pVfs->mxPathname+1);
ds@1952
  1844
  }
ds@1952
  1845
  if( rc==SQLITE_OK ){
ds@1952
  1846
    rc = pager_end_transaction(pPager);
ds@1952
  1847
  }
ds@1952
  1848
  if( rc==SQLITE_OK && zMaster[0] ){
ds@1952
  1849
    /* If there was a master journal and this routine will return success,
ds@1952
  1850
    ** see if it is possible to delete the master journal.
ds@1952
  1851
    */
ds@1952
  1852
    rc = pager_delmaster(pPager, zMaster);
ds@1952
  1853
  }
ds@1952
  1854
ds@1952
  1855
  /* The Pager.sectorSize variable may have been updated while rolling
ds@1952
  1856
  ** back a journal created by a process with a different sector size
ds@1952
  1857
  ** value. Reset it to the correct value for this process.
ds@1952
  1858
  */
ds@1952
  1859
  setSectorSize(pPager);
ds@1952
  1860
  return rc;
ds@1952
  1861
}
ds@1952
  1862
ds@1952
  1863
/*
ds@1952
  1864
** Playback the statement journal.
ds@1952
  1865
**
ds@1952
  1866
** This is similar to playing back the transaction journal but with
ds@1952
  1867
** a few extra twists.
ds@1952
  1868
**
ds@1952
  1869
**    (1)  The number of pages in the database file at the start of
ds@1952
  1870
**         the statement is stored in pPager->stmtSize, not in the
ds@1952
  1871
**         journal file itself.
ds@1952
  1872
**
ds@1952
  1873
**    (2)  In addition to playing back the statement journal, also
ds@1952
  1874
**         playback all pages of the transaction journal beginning
ds@1952
  1875
**         at offset pPager->stmtJSize.
ds@1952
  1876
*/
ds@1952
  1877
static int pager_stmt_playback(Pager *pPager){
ds@1952
  1878
  i64 szJ;                 /* Size of the full journal */
ds@1952
  1879
  i64 hdrOff;
ds@1952
  1880
  int nRec;                /* Number of Records */
ds@1952
  1881
  int i;                   /* Loop counter */
ds@1952
  1882
  int rc;
ds@1952
  1883
ds@1952
  1884
  szJ = pPager->journalOff;
ds@1952
  1885
#ifndef NDEBUG 
ds@1952
  1886
  {
ds@1952
  1887
    i64 os_szJ;
ds@1952
  1888
    rc = sqlite3OsFileSize(pPager->jfd, &os_szJ);
ds@1952
  1889
    if( rc!=SQLITE_OK ) return rc;
ds@1952
  1890
    assert( szJ==os_szJ );
ds@1952
  1891
  }
ds@1952
  1892
#endif
ds@1952
  1893
ds@1952
  1894
  /* Set hdrOff to be the offset just after the end of the last journal
ds@1952
  1895
  ** page written before the first journal-header for this statement
ds@1952
  1896
  ** transaction was written, or the end of the file if no journal
ds@1952
  1897
  ** header was written.
ds@1952
  1898
  */
ds@1952
  1899
  hdrOff = pPager->stmtHdrOff;
ds@1952
  1900
  assert( pPager->fullSync || !hdrOff );
ds@1952
  1901
  if( !hdrOff ){
ds@1952
  1902
    hdrOff = szJ;
ds@1952
  1903
  }
ds@1952
  1904
  
ds@1952
  1905
  /* Truncate the database back to its original size.
ds@1952
  1906
  */
ds@1952
  1907
  rc = pager_truncate(pPager, pPager->stmtSize);
ds@1952
  1908
  assert( pPager->state>=PAGER_SHARED );
ds@1952
  1909
ds@1952
  1910
  /* Figure out how many records are in the statement journal.
ds@1952
  1911
  */
ds@1952
  1912
  assert( pPager->stmtInUse && pPager->journalOpen );
ds@1952
  1913
  nRec = pPager->stmtNRec;
ds@1952
  1914
  
ds@1952
  1915
  /* Copy original pages out of the statement journal and back into the
ds@1952
  1916
  ** database file.  Note that the statement journal omits checksums from
ds@1952
  1917
  ** each record since power-failure recovery is not important to statement
ds@1952
  1918
  ** journals.
ds@1952
  1919
  */
ds@1952
  1920
  for(i=0; i<nRec; i++){
ds@1952
  1921
    i64 offset = i*(4+pPager->pageSize);
ds@1952
  1922
    rc = pager_playback_one_page(pPager, pPager->stfd, offset, 0);
ds@1952
  1923
    assert( rc!=SQLITE_DONE );
ds@1952
  1924
    if( rc!=SQLITE_OK ) goto end_stmt_playback;
ds@1952
  1925
  }
ds@1952
  1926
ds@1952
  1927
  /* Now roll some pages back from the transaction journal. Pager.stmtJSize
ds@1952
  1928
  ** was the size of the journal file when this statement was started, so
ds@1952
  1929
  ** everything after that needs to be rolled back, either into the
ds@1952
  1930
  ** database, the memory cache, or both.
ds@1952
  1931
  **
ds@1952
  1932
  ** If it is not zero, then Pager.stmtHdrOff is the offset to the start
ds@1952
  1933
  ** of the first journal header written during this statement transaction.
ds@1952
  1934
  */
ds@1952
  1935
  pPager->journalOff = pPager->stmtJSize;
ds@1952
  1936
  pPager->cksumInit = pPager->stmtCksum;
ds@1952
  1937
  while( pPager->journalOff < hdrOff ){
ds@1952
  1938
    rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
ds@1952
  1939
    assert( rc!=SQLITE_DONE );
ds@1952
  1940
    if( rc!=SQLITE_OK ) goto end_stmt_playback;
ds@1952
  1941
  }
ds@1952
  1942
ds@1952
  1943
  while( pPager->journalOff < szJ ){
ds@1952
  1944
    u32 nJRec;         /* Number of Journal Records */
ds@1952
  1945
    u32 dummy;
ds@1952
  1946
    rc = readJournalHdr(pPager, szJ, &nJRec, &dummy);
ds@1952
  1947
    if( rc!=SQLITE_OK ){
ds@1952
  1948
      assert( rc!=SQLITE_DONE );
ds@1952
  1949
      goto end_stmt_playback;
ds@1952
  1950
    }
ds@1952
  1951
    if( nJRec==0 ){
ds@1952
  1952
      nJRec = (szJ - pPager->journalOff) / (pPager->pageSize+8);
ds@1952
  1953
    }
ds@1952
  1954
    for(i=nJRec-1; i>=0 && pPager->journalOff < szJ; i--){
ds@1952
  1955
      rc = pager_playback_one_page(pPager, pPager->jfd, pPager->journalOff, 1);
ds@1952
  1956
      assert( rc!=SQLITE_DONE );
ds@1952
  1957
      if( rc!=SQLITE_OK ) goto end_stmt_playback;
ds@1952
  1958
    }
ds@1952
  1959
  }
ds@1952
  1960
ds@1952
  1961
  pPager->journalOff = szJ;
ds@1952
  1962
  
ds@1952
  1963
end_stmt_playback:
ds@1952
  1964
  if( rc==SQLITE_OK) {
ds@1952
  1965
    pPager->journalOff = szJ;
ds@1952
  1966
    /* pager_reload_cache(pPager); */
ds@1952
  1967
  }
ds@1952
  1968
  return rc;
ds@1952
  1969
}
ds@1952
  1970
ds@1952
  1971
/*
ds@1952
  1972
** Change the maximum number of in-memory pages that are allowed.
ds@1952
  1973
*/
ds@1952
  1974
void sqlite3PagerSetCachesize(Pager *pPager, int mxPage){
ds@1952
  1975
  if( mxPage>10 ){
ds@1952
  1976
    pPager->mxPage = mxPage;
ds@1952
  1977
  }else{
ds@1952
  1978
    pPager->mxPage = 10;
ds@1952
  1979
  }
ds@1952
  1980
}
ds@1952
  1981
ds@1952
  1982
/*
ds@1952
  1983
** Adjust the robustness of the database to damage due to OS crashes
ds@1952
  1984
** or power failures by changing the number of syncs()s when writing
ds@1952
  1985
** the rollback journal.  There are three levels:
ds@1952
  1986
**
ds@1952
  1987
**    OFF       sqlite3OsSync() is never called.  This is the default
ds@1952
  1988
**              for temporary and transient files.
ds@1952
  1989
**
ds@1952
  1990
**    NORMAL    The journal is synced once before writes begin on the
ds@1952
  1991
**              database.  This is normally adequate protection, but
ds@1952
  1992
**              it is theoretically possible, though very unlikely,
ds@1952
  1993
**              that an inopertune power failure could leave the journal
ds@1952
  1994
**              in a state which would cause damage to the database
ds@1952
  1995
**              when it is rolled back.
ds@1952
  1996
**
ds@1952
  1997
**    FULL      The journal is synced twice before writes begin on the
ds@1952
  1998
**              database (with some additional information - the nRec field
ds@1952
  1999
**              of the journal header - being written in between the two
ds@1952
  2000
**              syncs).  If we assume that writing a
ds@1952
  2001
**              single disk sector is atomic, then this mode provides
ds@1952
  2002
**              assurance that the journal will not be corrupted to the
ds@1952
  2003
**              point of causing damage to the database during rollback.
ds@1952
  2004
**
ds@1952
  2005
** Numeric values associated with these states are OFF==1, NORMAL=2,
ds@1952
  2006
** and FULL=3.
ds@1952
  2007
*/
ds@1952
  2008
#ifndef SQLITE_OMIT_PAGER_PRAGMAS
ds@1952
  2009
void sqlite3PagerSetSafetyLevel(Pager *pPager, int level, int full_fsync){
ds@1952
  2010
  pPager->noSync =  level==1 || pPager->tempFile;
ds@1952
  2011
  pPager->fullSync = level==3 && !pPager->tempFile;
ds@1952
  2012
  pPager->sync_flags = (full_fsync?SQLITE_SYNC_FULL:SQLITE_SYNC_NORMAL);
ds@1952
  2013
  if( pPager->noSync ) pPager->needSync = 0;
ds@1952
  2014
}
ds@1952
  2015
#endif
ds@1952
  2016
ds@1952
  2017
/*
ds@1952
  2018
** The following global variable is incremented whenever the library
ds@1952
  2019
** attempts to open a temporary file.  This information is used for
ds@1952
  2020
** testing and analysis only.  
ds@1952
  2021
*/
ds@1952
  2022
#ifdef SQLITE_TEST
ds@1952
  2023
int sqlite3_opentemp_count = 0;
ds@1952
  2024
#endif
ds@1952
  2025
ds@1952
  2026
/*
ds@1952
  2027
** Open a temporary file. 
ds@1952
  2028
**
ds@1952
  2029
** Write the file descriptor into *fd.  Return SQLITE_OK on success or some
ds@1952
  2030
** other error code if we fail. The OS will automatically delete the temporary
ds@1952
  2031
** file when it is closed.
ds@1952
  2032
*/
ds@1952
  2033
static int sqlite3PagerOpentemp(
ds@1952
  2034
  sqlite3_vfs *pVfs,    /* The virtual file system layer */
ds@1952
  2035
  sqlite3_file *pFile,  /* Write the file descriptor here */
ds@1952
  2036
  char *zFilename,      /* Name of the file.  Might be NULL */
ds@1952
  2037
  int vfsFlags          /* Flags passed through to the VFS */
ds@1952
  2038
){
ds@1952
  2039
  int rc;
ds@1952
  2040
  assert( zFilename!=0 );
ds@1952
  2041
ds@1952
  2042
#ifdef SQLITE_TEST
ds@1952
  2043
  sqlite3_opentemp_count++;  /* Used for testing and analysis only */
ds@1952
  2044
#endif
ds@1952
  2045
ds@1952
  2046
  vfsFlags |=  SQLITE_OPEN_READWRITE | SQLITE_OPEN_CREATE |
ds@1952
  2047
            SQLITE_OPEN_EXCLUSIVE | SQLITE_OPEN_DELETEONCLOSE;
ds@1952
  2048
  rc = sqlite3OsOpen(pVfs, zFilename, pFile, vfsFlags, 0);
ds@1952
  2049
  assert( rc!=SQLITE_OK || pFile->pMethods );
ds@1952
  2050
  return rc;
ds@1952
  2051
}
ds@1952
  2052
ds@1952
  2053
/*
ds@1952
  2054
** Create a new page cache and put a pointer to the page cache in *ppPager.
ds@1952
  2055
** The file to be cached need not exist.  The file is not locked until
ds@1952
  2056
** the first call to sqlite3PagerGet() and is only held open until the
ds@1952
  2057
** last page is released using sqlite3PagerUnref().
ds@1952
  2058
**
ds@1952
  2059
** If zFilename is NULL then a randomly-named temporary file is created
ds@1952
  2060
** and used as the file to be cached.  The file will be deleted
ds@1952
  2061
** automatically when it is closed.
ds@1952
  2062
**
ds@1952
  2063
** If zFilename is ":memory:" then all information is held in cache.
ds@1952
  2064
** It is never written to disk.  This can be used to implement an
ds@1952
  2065
** in-memory database.
ds@1952
  2066
*/
ds@1952
  2067
int sqlite3PagerOpen(
ds@1952
  2068
  sqlite3_vfs *pVfs,       /* The virtual file system to use */
ds@1952
  2069
  Pager **ppPager,         /* Return the Pager structure here */
ds@1952
  2070
  const char *zFilename,   /* Name of the database file to open */
ds@1952
  2071
  int nExtra,              /* Extra bytes append to each in-memory page */
ds@1952
  2072
  int flags,               /* flags controlling this file */
ds@1952
  2073
  int vfsFlags             /* flags passed through to sqlite3_vfs.xOpen() */
ds@1952
  2074
){
ds@1952
  2075
  u8 *pPtr;
ds@1952
  2076
  Pager *pPager = 0;
ds@1952
  2077
  int rc = SQLITE_OK;
ds@1952
  2078
  int i;
ds@1952
  2079
  int tempFile = 0;
ds@1952
  2080
  int memDb = 0;
ds@1952
  2081
  int readOnly = 0;
ds@1952
  2082
  int useJournal = (flags & PAGER_OMIT_JOURNAL)==0;
ds@1952
  2083
  int noReadlock = (flags & PAGER_NO_READLOCK)!=0;
ds@1952
  2084
  int journalFileSize = sqlite3JournalSize(pVfs);
ds@1952
  2085
  int nDefaultPage = SQLITE_DEFAULT_PAGE_SIZE;
ds@1952
  2086
  char *zPathname;
ds@1952
  2087
  int nPathname;
ds@1952
  2088
ds@1952
  2089
  /* The default return is a NULL pointer */
ds@1952
  2090
  *ppPager = 0;
ds@1952
  2091
ds@1952
  2092
  /* Compute the full pathname */
ds@1952
  2093
  nPathname = pVfs->mxPathname+1;
ds@1952
  2094
  zPathname = sqlite3_malloc(nPathname);
ds@1952
  2095
  if( zPathname==0 ){
ds@1952
  2096
    return SQLITE_NOMEM;
ds@1952
  2097
  }
ds@1952
  2098
  if( zFilename && zFilename[0] ){
ds@1952
  2099
#ifndef SQLITE_OMIT_MEMORYDB
ds@1952
  2100
    if( strcmp(zFilename,":memory:")==0 ){
ds@1952
  2101
      memDb = 1;
ds@1952
  2102
      zPathname[0] = 0;
ds@1952
  2103
    }else
ds@1952
  2104
#endif
ds@1952
  2105
    {
ds@1952
  2106
      rc = sqlite3OsFullPathname(pVfs, zFilename, nPathname, zPathname);
ds@1952
  2107
    }
ds@1952
  2108
  }else{
ds@1952
  2109
    rc = sqlite3OsGetTempname(pVfs, nPathname, zPathname);
ds@1952
  2110
  }
ds@1952
  2111
  if( rc!=SQLITE_OK ){
ds@1952
  2112
    sqlite3_free(zPathname);
ds@1952
  2113
    return rc;
ds@1952
  2114
  }
ds@1952
  2115
  nPathname = strlen(zPathname);
ds@1952
  2116
ds@1952
  2117
  /* Allocate memory for the pager structure */
ds@1952
  2118
  pPager = sqlite3MallocZero(
ds@1952
  2119
    sizeof(*pPager) +           /* Pager structure */
ds@1952
  2120
    journalFileSize +           /* The journal file structure */ 
ds@1952
  2121
    pVfs->szOsFile * 2 +        /* The db and stmt journal files */ 
ds@1952
  2122
    4*nPathname + 40            /* zFilename, zDirectory, zJournal, zStmtJrnl */
ds@1952
  2123
  );
ds@1952
  2124
  if( !pPager ){
ds@1952
  2125
    sqlite3_free(zPathname);
ds@1952
  2126
    return SQLITE_NOMEM;
ds@1952
  2127
  }
ds@1952
  2128
  pPtr = (u8 *)&pPager[1];
ds@1952
  2129
  pPager->vfsFlags = vfsFlags;
ds@1952
  2130
  pPager->fd = (sqlite3_file*)&pPtr[pVfs->szOsFile*0];
ds@1952
  2131
  pPager->stfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*1];
ds@1952
  2132
  pPager->jfd = (sqlite3_file*)&pPtr[pVfs->szOsFile*2];
ds@1952
  2133
  pPager->zFilename = (char*)&pPtr[pVfs->szOsFile*2+journalFileSize];
ds@1952
  2134
  pPager->zDirectory = &pPager->zFilename[nPathname+1];
ds@1952
  2135
  pPager->zJournal = &pPager->zDirectory[nPathname+1];
ds@1952
  2136
  pPager->zStmtJrnl = &pPager->zJournal[nPathname+10];
ds@1952
  2137
  pPager->pVfs = pVfs;
ds@1952
  2138
  memcpy(pPager->zFilename, zPathname, nPathname+1);
ds@1952
  2139
  sqlite3_free(zPathname);
ds@1952
  2140
ds@1952
  2141
  /* Open the pager file.
ds@1952
  2142
  */
ds@1952
  2143
  if( zFilename && zFilename[0] && !memDb ){
ds@1952
  2144
    if( nPathname>(pVfs->mxPathname - sizeof("-journal")) ){
ds@1952
  2145
      rc = SQLITE_CANTOPEN;
ds@1952
  2146
    }else{
ds@1952
  2147
      int fout = 0;
ds@1952
  2148
      rc = sqlite3OsOpen(pVfs, pPager->zFilename, pPager->fd,
ds@1952
  2149
                         pPager->vfsFlags, &fout);
ds@1952
  2150
      readOnly = (fout&SQLITE_OPEN_READONLY);
ds@1952
  2151
ds@1952
  2152
      /* If the file was successfully opened for read/write access,
ds@1952
  2153
      ** choose a default page size in case we have to create the
ds@1952
  2154
      ** database file. The default page size is the maximum of:
ds@1952
  2155
      **
ds@1952
  2156
      **    + SQLITE_DEFAULT_PAGE_SIZE,
ds@1952
  2157
      **    + The value returned by sqlite3OsSectorSize()
ds@1952
  2158
      **    + The largest page size that can be written atomically.
ds@1952
  2159
      */
ds@1952
  2160
      if( rc==SQLITE_OK && !readOnly ){
ds@1952
  2161
        int iSectorSize = sqlite3OsSectorSize(pPager->fd);
ds@1952
  2162
        if( nDefaultPage<iSectorSize ){
ds@1952
  2163
          nDefaultPage = iSectorSize;
ds@1952
  2164
        }
ds@1952
  2165
#ifdef SQLITE_ENABLE_ATOMIC_WRITE
ds@1952
  2166
        {
ds@1952
  2167
          int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
ds@1952
  2168
          int ii;
ds@1952
  2169
          assert(SQLITE_IOCAP_ATOMIC512==(512>>8));
ds@1952
  2170
          assert(SQLITE_IOCAP_ATOMIC64K==(65536>>8));
ds@1952
  2171
          assert(SQLITE_MAX_DEFAULT_PAGE_SIZE<=65536);
ds@1952
  2172
          for(ii=nDefaultPage; ii<=SQLITE_MAX_DEFAULT_PAGE_SIZE; ii=ii*2){
ds@1952
  2173
            if( iDc&(SQLITE_IOCAP_ATOMIC|(ii>>8)) ) nDefaultPage = ii;
ds@1952
  2174
          }
ds@1952
  2175
        }
ds@1952
  2176
#endif
ds@1952
  2177
        if( nDefaultPage>SQLITE_MAX_DEFAULT_PAGE_SIZE ){
ds@1952
  2178
          nDefaultPage = SQLITE_MAX_DEFAULT_PAGE_SIZE;
ds@1952
  2179
        }
ds@1952
  2180
      }
ds@1952
  2181
    }
ds@1952
  2182
  }else if( !memDb ){
ds@1952
  2183
    /* If a temporary file is requested, it is not opened immediately.
ds@1952
  2184
    ** In this case we accept the default page size and delay actually
ds@1952
  2185
    ** opening the file until the first call to OsWrite().
ds@1952
  2186
    */ 
ds@1952
  2187
    tempFile = 1;
ds@1952
  2188
    pPager->state = PAGER_EXCLUSIVE;
ds@1952
  2189
  }
ds@1952
  2190
ds@1952
  2191
  if( pPager && rc==SQLITE_OK ){
ds@1952
  2192
    pPager->pTmpSpace = (char *)sqlite3_malloc(nDefaultPage);
ds@1952
  2193
  }
ds@1952
  2194
ds@1952
  2195
  /* If an error occured in either of the blocks above.
ds@1952
  2196
  ** Free the Pager structure and close the file.
ds@1952
  2197
  ** Since the pager is not allocated there is no need to set 
ds@1952
  2198
  ** any Pager.errMask variables.
ds@1952
  2199
  */
ds@1952
  2200
  if( !pPager || !pPager->pTmpSpace ){
ds@1952
  2201
    sqlite3OsClose(pPager->fd);
ds@1952
  2202
    sqlite3_free(pPager);
ds@1952
  2203
    return ((rc==SQLITE_OK)?SQLITE_NOMEM:rc);
ds@1952
  2204
  }
ds@1952
  2205
ds@1952
  2206
  PAGERTRACE3("OPEN %d %s\n", FILEHANDLEID(pPager->fd), pPager->zFilename);
ds@1952
  2207
  IOTRACE(("OPEN %p %s\n", pPager, pPager->zFilename))
ds@1952
  2208
ds@1952
  2209
  /* Fill in Pager.zDirectory[] */
ds@1952
  2210
  memcpy(pPager->zDirectory, pPager->zFilename, nPathname+1);
ds@1952
  2211
  for(i=strlen(pPager->zDirectory); i>0 && pPager->zDirectory[i-1]!='/'; i--){}
ds@1952
  2212
  if( i>0 ) pPager->zDirectory[i-1] = 0;
ds@1952
  2213
ds@1952
  2214
  /* Fill in Pager.zJournal[] and Pager.zStmtJrnl[] */
ds@1952
  2215
  memcpy(pPager->zJournal, pPager->zFilename, nPathname);
ds@1952
  2216
  memcpy(&pPager->zJournal[nPathname], "-journal", 9);
ds@1952
  2217
  memcpy(pPager->zStmtJrnl, pPager->zFilename, nPathname);
ds@1952
  2218
  memcpy(&pPager->zStmtJrnl[nPathname], "-stmtjrnl", 10);
ds@1952
  2219
ds@1952
  2220
  /* pPager->journalOpen = 0; */
ds@1952
  2221
  pPager->useJournal = useJournal && !memDb;
ds@1952
  2222
  pPager->noReadlock = noReadlock && readOnly;
ds@1952
  2223
  /* pPager->stmtOpen = 0; */
ds@1952
  2224
  /* pPager->stmtInUse = 0; */
ds@1952
  2225
  /* pPager->nRef = 0; */
ds@1952
  2226
  pPager->dbSize = memDb-1;
ds@1952
  2227
  pPager->pageSize = nDefaultPage;
ds@1952
  2228
  /* pPager->stmtSize = 0; */
ds@1952
  2229
  /* pPager->stmtJSize = 0; */
ds@1952
  2230
  /* pPager->nPage = 0; */
ds@1952
  2231
  pPager->mxPage = 100;
ds@1952
  2232
  pPager->mxPgno = SQLITE_MAX_PAGE_COUNT;
ds@1952
  2233
  /* pPager->state = PAGER_UNLOCK; */
ds@1952
  2234
  assert( pPager->state == (tempFile ? PAGER_EXCLUSIVE : PAGER_UNLOCK) );
ds@1952
  2235
  /* pPager->errMask = 0; */
ds@1952
  2236
  pPager->tempFile = tempFile;
ds@1952
  2237
  assert( tempFile==PAGER_LOCKINGMODE_NORMAL 
ds@1952
  2238
          || tempFile==PAGER_LOCKINGMODE_EXCLUSIVE );
ds@1952
  2239
  assert( PAGER_LOCKINGMODE_EXCLUSIVE==1 );
ds@1952
  2240
  pPager->exclusiveMode = tempFile; 
ds@1952
  2241
  pPager->memDb = memDb;
ds@1952
  2242
  pPager->readOnly = readOnly;
ds@1952
  2243
  /* pPager->needSync = 0; */
ds@1952
  2244
  pPager->noSync = pPager->tempFile || !useJournal;
ds@1952
  2245
  pPager->fullSync = (pPager->noSync?0:1);
ds@1952
  2246
  pPager->sync_flags = SQLITE_SYNC_NORMAL;
ds@1952
  2247
  /* pPager->pFirst = 0; */
ds@1952
  2248
  /* pPager->pFirstSynced = 0; */
ds@1952
  2249
  /* pPager->pLast = 0; */
ds@1952
  2250
  pPager->nExtra = FORCE_ALIGNMENT(nExtra);
ds@1952
  2251
  assert(pPager->fd->pMethods||memDb||tempFile);
ds@1952
  2252
  if( !memDb ){
ds@1952
  2253
    setSectorSize(pPager);
ds@1952
  2254
  }
ds@1952
  2255
  /* pPager->pBusyHandler = 0; */
ds@1952
  2256
  /* memset(pPager->aHash, 0, sizeof(pPager->aHash)); */
ds@1952
  2257
  *ppPager = pPager;
ds@1952
  2258
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
ds@1952
  2259
  pPager->iInUseMM = 0;
ds@1952
  2260
  pPager->iInUseDB = 0;
ds@1952
  2261
  if( !memDb ){
ds@1952
  2262
    sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
ds@1952
  2263
    sqlite3_mutex_enter(mutex);
ds@1952
  2264
    pPager->pNext = sqlite3PagerList;
ds@1952
  2265
    if( sqlite3PagerList ){
ds@1952
  2266
      assert( sqlite3PagerList->pPrev==0 );
ds@1952
  2267
      sqlite3PagerList->pPrev = pPager;
ds@1952
  2268
    }
ds@1952
  2269
    pPager->pPrev = 0;
ds@1952
  2270
    sqlite3PagerList = pPager;
ds@1952
  2271
    sqlite3_mutex_leave(mutex);
ds@1952
  2272
  }
ds@1952
  2273
#endif
ds@1952
  2274
  return SQLITE_OK;
ds@1952
  2275
}
ds@1952
  2276
ds@1952
  2277
/*
ds@1952
  2278
** Set the busy handler function.
ds@1952
  2279
*/
ds@1952
  2280
void sqlite3PagerSetBusyhandler(Pager *pPager, BusyHandler *pBusyHandler){
ds@1952
  2281
  pPager->pBusyHandler = pBusyHandler;
ds@1952
  2282
}
ds@1952
  2283
ds@1952
  2284
/*
ds@1952
  2285
** Set the destructor for this pager.  If not NULL, the destructor is called
ds@1952
  2286
** when the reference count on each page reaches zero.  The destructor can
ds@1952
  2287
** be used to clean up information in the extra segment appended to each page.
ds@1952
  2288
**
ds@1952
  2289
** The destructor is not called as a result sqlite3PagerClose().  
ds@1952
  2290
** Destructors are only called by sqlite3PagerUnref().
ds@1952
  2291
*/
ds@1952
  2292
void sqlite3PagerSetDestructor(Pager *pPager, void (*xDesc)(DbPage*,int)){
ds@1952
  2293
  pPager->xDestructor = xDesc;
ds@1952
  2294
}
ds@1952
  2295
ds@1952
  2296
/*
ds@1952
  2297
** Set the reinitializer for this pager.  If not NULL, the reinitializer
ds@1952
  2298
** is called when the content of a page in cache is restored to its original
ds@1952
  2299
** value as a result of a rollback.  The callback gives higher-level code
ds@1952
  2300
** an opportunity to restore the EXTRA section to agree with the restored
ds@1952
  2301
** page data.
ds@1952
  2302
*/
ds@1952
  2303
void sqlite3PagerSetReiniter(Pager *pPager, void (*xReinit)(DbPage*,int)){
ds@1952
  2304
  pPager->xReiniter = xReinit;
ds@1952
  2305
}
ds@1952
  2306
ds@1952
  2307
/*
ds@1952
  2308
** Set the page size to *pPageSize. If the suggest new page size is
ds@1952
  2309
** inappropriate, then an alternative page size is set to that
ds@1952
  2310
** value before returning.
ds@1952
  2311
*/
ds@1952
  2312
int sqlite3PagerSetPagesize(Pager *pPager, u16 *pPageSize){
ds@1952
  2313
  int rc = SQLITE_OK;
ds@1952
  2314
  u16 pageSize = *pPageSize;
ds@1952
  2315
  assert( pageSize==0 || (pageSize>=512 && pageSize<=SQLITE_MAX_PAGE_SIZE) );
ds@1952
  2316
  if( pageSize && pageSize!=pPager->pageSize 
ds@1952
  2317
   && !pPager->memDb && pPager->nRef==0 
ds@1952
  2318
  ){
ds@1952
  2319
    char *pNew = (char *)sqlite3_malloc(pageSize);
ds@1952
  2320
    if( !pNew ){
ds@1952
  2321
      rc = SQLITE_NOMEM;
ds@1952
  2322
    }else{
ds@1952
  2323
      pagerEnter(pPager);
ds@1952
  2324
      pager_reset(pPager);
ds@1952
  2325
      pPager->pageSize = pageSize;
ds@1952
  2326
      setSectorSize(pPager);
ds@1952
  2327
      sqlite3_free(pPager->pTmpSpace);
ds@1952
  2328
      pPager->pTmpSpace = pNew;
ds@1952
  2329
      pagerLeave(pPager);
ds@1952
  2330
    }
ds@1952
  2331
  }
ds@1952
  2332
  *pPageSize = pPager->pageSize;
ds@1952
  2333
  return rc;
ds@1952
  2334
}
ds@1952
  2335
ds@1952
  2336
/*
ds@1952
  2337
** Attempt to set the maximum database page count if mxPage is positive. 
ds@1952
  2338
** Make no changes if mxPage is zero or negative.  And never reduce the
ds@1952
  2339
** maximum page count below the current size of the database.
ds@1952
  2340
**
ds@1952
  2341
** Regardless of mxPage, return the current maximum page count.
ds@1952
  2342
*/
ds@1952
  2343
int sqlite3PagerMaxPageCount(Pager *pPager, int mxPage){
ds@1952
  2344
  if( mxPage>0 ){
ds@1952
  2345
    pPager->mxPgno = mxPage;
ds@1952
  2346
  }
ds@1952
  2347
  sqlite3PagerPagecount(pPager);
ds@1952
  2348
  return pPager->mxPgno;
ds@1952
  2349
}
ds@1952
  2350
ds@1952
  2351
/*
ds@1952
  2352
** The following set of routines are used to disable the simulated
ds@1952
  2353
** I/O error mechanism.  These routines are used to avoid simulated
ds@1952
  2354
** errors in places where we do not care about errors.
ds@1952
  2355
**
ds@1952
  2356
** Unless -DSQLITE_TEST=1 is used, these routines are all no-ops
ds@1952
  2357
** and generate no code.
ds@1952
  2358
*/
ds@1952
  2359
#ifdef SQLITE_TEST
ds@1952
  2360
extern int sqlite3_io_error_pending;
ds@1952
  2361
extern int sqlite3_io_error_hit;
ds@1952
  2362
static int saved_cnt;
ds@1952
  2363
void disable_simulated_io_errors(void){
ds@1952
  2364
  saved_cnt = sqlite3_io_error_pending;
ds@1952
  2365
  sqlite3_io_error_pending = -1;
ds@1952
  2366
}
ds@1952
  2367
void enable_simulated_io_errors(void){
ds@1952
  2368
  sqlite3_io_error_pending = saved_cnt;
ds@1952
  2369
}
ds@1952
  2370
#else
ds@1952
  2371
# define disable_simulated_io_errors()
ds@1952
  2372
# define enable_simulated_io_errors()
ds@1952
  2373
#endif
ds@1952
  2374
ds@1952
  2375
/*
ds@1952
  2376
** Read the first N bytes from the beginning of the file into memory
ds@1952
  2377
** that pDest points to. 
ds@1952
  2378
**
ds@1952
  2379
** No error checking is done. The rational for this is that this function 
ds@1952
  2380
** may be called even if the file does not exist or contain a header. In 
ds@1952
  2381
** these cases sqlite3OsRead() will return an error, to which the correct 
ds@1952
  2382
** response is to zero the memory at pDest and continue.  A real IO error 
ds@1952
  2383
** will presumably recur and be picked up later (Todo: Think about this).
ds@1952
  2384
*/
ds@1952
  2385
int sqlite3PagerReadFileheader(Pager *pPager, int N, unsigned char *pDest){
ds@1952
  2386
  int rc = SQLITE_OK;
ds@1952
  2387
  memset(pDest, 0, N);
ds@1952
  2388
  assert(MEMDB||pPager->fd->pMethods||pPager->tempFile);
ds@1952
  2389
  if( pPager->fd->pMethods ){
ds@1952
  2390
    IOTRACE(("DBHDR %p 0 %d\n", pPager, N))
ds@1952
  2391
    rc = sqlite3OsRead(pPager->fd, pDest, N, 0);
ds@1952
  2392
    if( rc==SQLITE_IOERR_SHORT_READ ){
ds@1952
  2393
      rc = SQLITE_OK;
ds@1952
  2394
    }
ds@1952
  2395
  }
ds@1952
  2396
  return rc;
ds@1952
  2397
}
ds@1952
  2398
ds@1952
  2399
/*
ds@1952
  2400
** Return the total number of pages in the disk file associated with
ds@1952
  2401
** pPager. 
ds@1952
  2402
**
ds@1952
  2403
** If the PENDING_BYTE lies on the page directly after the end of the
ds@1952
  2404
** file, then consider this page part of the file too. For example, if
ds@1952
  2405
** PENDING_BYTE is byte 4096 (the first byte of page 5) and the size of the
ds@1952
  2406
** file is 4096 bytes, 5 is returned instead of 4.
ds@1952
  2407
*/
ds@1952
  2408
int sqlite3PagerPagecount(Pager *pPager){
ds@1952
  2409
  i64 n = 0;
ds@1952
  2410
  int rc;
ds@1952
  2411
  assert( pPager!=0 );
ds@1952
  2412
  if( pPager->errCode ){
ds@1952
  2413
    return 0;
ds@1952
  2414
  }
ds@1952
  2415
  if( pPager->dbSize>=0 ){
ds@1952
  2416
    n = pPager->dbSize;
ds@1952
  2417
  } else {
ds@1952
  2418
    assert(pPager->fd->pMethods||pPager->tempFile);
ds@1952
  2419
    if( (pPager->fd->pMethods)
ds@1952
  2420
     && (rc = sqlite3OsFileSize(pPager->fd, &n))!=SQLITE_OK ){
ds@1952
  2421
      pPager->nRef++;
ds@1952
  2422
      pager_error(pPager, rc);
ds@1952
  2423
      pPager->nRef--;
ds@1952
  2424
      return 0;
ds@1952
  2425
    }
ds@1952
  2426
    if( n>0 && n<pPager->pageSize ){
ds@1952
  2427
      n = 1;
ds@1952
  2428
    }else{
ds@1952
  2429
      n /= pPager->pageSize;
ds@1952
  2430
    }
ds@1952
  2431
    if( pPager->state!=PAGER_UNLOCK ){
ds@1952
  2432
      pPager->dbSize = n;
ds@1952
  2433
    }
ds@1952
  2434
  }
ds@1952
  2435
  if( n==(PENDING_BYTE/pPager->pageSize) ){
ds@1952
  2436
    n++;
ds@1952
  2437
  }
ds@1952
  2438
  if( n>pPager->mxPgno ){
ds@1952
  2439
    pPager->mxPgno = n;
ds@1952
  2440
  }
ds@1952
  2441
  return n;
ds@1952
  2442
}
ds@1952
  2443
ds@1952
  2444
ds@1952
  2445
#ifndef SQLITE_OMIT_MEMORYDB
ds@1952
  2446
/*
ds@1952
  2447
** Clear a PgHistory block
ds@1952
  2448
*/
ds@1952
  2449
static void clearHistory(PgHistory *pHist){
ds@1952
  2450
  sqlite3_free(pHist->pOrig);
ds@1952
  2451
  sqlite3_free(pHist->pStmt);
ds@1952
  2452
  pHist->pOrig = 0;
ds@1952
  2453
  pHist->pStmt = 0;
ds@1952
  2454
}
ds@1952
  2455
#else
ds@1952
  2456
#define clearHistory(x)
ds@1952
  2457
#endif
ds@1952
  2458
ds@1952
  2459
/*
ds@1952
  2460
** Forward declaration
ds@1952
  2461
*/
ds@1952
  2462
static int syncJournal(Pager*);
ds@1952
  2463
ds@1952
  2464
/*
ds@1952
  2465
** Unlink pPg from it's hash chain. Also set the page number to 0 to indicate
ds@1952
  2466
** that the page is not part of any hash chain. This is required because the
ds@1952
  2467
** sqlite3PagerMovepage() routine can leave a page in the 
ds@1952
  2468
** pNextFree/pPrevFree list that is not a part of any hash-chain.
ds@1952
  2469
*/
ds@1952
  2470
static void unlinkHashChain(Pager *pPager, PgHdr *pPg){
ds@1952
  2471
  if( pPg->pgno==0 ){
ds@1952
  2472
    assert( pPg->pNextHash==0 && pPg->pPrevHash==0 );
ds@1952
  2473
    return;
ds@1952
  2474
  }
ds@1952
  2475
  if( pPg->pNextHash ){
ds@1952
  2476
    pPg->pNextHash->pPrevHash = pPg->pPrevHash;
ds@1952
  2477
  }
ds@1952
  2478
  if( pPg->pPrevHash ){
ds@1952
  2479
    assert( pPager->aHash[pPg->pgno & (pPager->nHash-1)]!=pPg );
ds@1952
  2480
    pPg->pPrevHash->pNextHash = pPg->pNextHash;
ds@1952
  2481
  }else{
ds@1952
  2482
    int h = pPg->pgno & (pPager->nHash-1);
ds@1952
  2483
    pPager->aHash[h] = pPg->pNextHash;
ds@1952
  2484
  }
ds@1952
  2485
  if( MEMDB ){
ds@1952
  2486
    clearHistory(PGHDR_TO_HIST(pPg, pPager));
ds@1952
  2487
  }
ds@1952
  2488
  pPg->pgno = 0;
ds@1952
  2489
  pPg->pNextHash = pPg->pPrevHash = 0;
ds@1952
  2490
}
ds@1952
  2491
ds@1952
  2492
/*
ds@1952
  2493
** Unlink a page from the free list (the list of all pages where nRef==0)
ds@1952
  2494
** and from its hash collision chain.
ds@1952
  2495
*/
ds@1952
  2496
static void unlinkPage(PgHdr *pPg){
ds@1952
  2497
  Pager *pPager = pPg->pPager;
ds@1952
  2498
ds@1952
  2499
  /* Unlink from free page list */
ds@1952
  2500
  lruListRemove(pPg);
ds@1952
  2501
ds@1952
  2502
  /* Unlink from the pgno hash table */
ds@1952
  2503
  unlinkHashChain(pPager, pPg);
ds@1952
  2504
}
ds@1952
  2505
ds@1952
  2506
/*
ds@1952
  2507
** This routine is used to truncate the cache when a database
ds@1952
  2508
** is truncated.  Drop from the cache all pages whose pgno is
ds@1952
  2509
** larger than pPager->dbSize and is unreferenced.
ds@1952
  2510
**
ds@1952
  2511
** Referenced pages larger than pPager->dbSize are zeroed.
ds@1952
  2512
**
ds@1952
  2513
** Actually, at the point this routine is called, it would be
ds@1952
  2514
** an error to have a referenced page.  But rather than delete
ds@1952
  2515
** that page and guarantee a subsequent segfault, it seems better
ds@1952
  2516
** to zero it and hope that we error out sanely.
ds@1952
  2517
*/
ds@1952
  2518
static void pager_truncate_cache(Pager *pPager){
ds@1952
  2519
  PgHdr *pPg;
ds@1952
  2520
  PgHdr **ppPg;
ds@1952
  2521
  int dbSize = pPager->dbSize;
ds@1952
  2522
ds@1952
  2523
  ppPg = &pPager->pAll;
ds@1952
  2524
  while( (pPg = *ppPg)!=0 ){
ds@1952
  2525
    if( pPg->pgno<=dbSize ){
ds@1952
  2526
      ppPg = &pPg->pNextAll;
ds@1952
  2527
    }else if( pPg->nRef>0 ){
ds@1952
  2528
      memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
ds@1952
  2529
      ppPg = &pPg->pNextAll;
ds@1952
  2530
    }else{
ds@1952
  2531
      *ppPg = pPg->pNextAll;
ds@1952
  2532
      IOTRACE(("PGFREE %p %d\n", pPager, pPg->pgno));
ds@1952
  2533
      PAGER_INCR(sqlite3_pager_pgfree_count);
ds@1952
  2534
      unlinkPage(pPg);
ds@1952
  2535
      makeClean(pPg);
ds@1952
  2536
      sqlite3_free(pPg->pData);
ds@1952
  2537
      sqlite3_free(pPg);
ds@1952
  2538
      pPager->nPage--;
ds@1952
  2539
    }
ds@1952
  2540
  }
ds@1952
  2541
}
ds@1952
  2542
ds@1952
  2543
/*
ds@1952
  2544
** Try to obtain a lock on a file.  Invoke the busy callback if the lock
ds@1952
  2545
** is currently not available.  Repeat until the busy callback returns
ds@1952
  2546
** false or until the lock succeeds.
ds@1952
  2547
**
ds@1952
  2548
** Return SQLITE_OK on success and an error code if we cannot obtain
ds@1952
  2549
** the lock.
ds@1952
  2550
*/
ds@1952
  2551
static int pager_wait_on_lock(Pager *pPager, int locktype){
ds@1952
  2552
  int rc;
ds@1952
  2553
ds@1952
  2554
  /* The OS lock values must be the same as the Pager lock values */
ds@1952
  2555
  assert( PAGER_SHARED==SHARED_LOCK );
ds@1952
  2556
  assert( PAGER_RESERVED==RESERVED_LOCK );
ds@1952
  2557
  assert( PAGER_EXCLUSIVE==EXCLUSIVE_LOCK );
ds@1952
  2558
ds@1952
  2559
  /* If the file is currently unlocked then the size must be unknown */
ds@1952
  2560
  assert( pPager->state>=PAGER_SHARED || pPager->dbSize<0 || MEMDB );
ds@1952
  2561
ds@1952
  2562
  if( pPager->state>=locktype ){
ds@1952
  2563
    rc = SQLITE_OK;
ds@1952
  2564
  }else{
ds@1952
  2565
    do {
ds@1952
  2566
      rc = sqlite3OsLock(pPager->fd, locktype);
ds@1952
  2567
    }while( rc==SQLITE_BUSY && sqlite3InvokeBusyHandler(pPager->pBusyHandler) );
ds@1952
  2568
    if( rc==SQLITE_OK ){
ds@1952
  2569
      pPager->state = locktype;
ds@1952
  2570
      IOTRACE(("LOCK %p %d\n", pPager, locktype))
ds@1952
  2571
    }
ds@1952
  2572
  }
ds@1952
  2573
  return rc;
ds@1952
  2574
}
ds@1952
  2575
ds@1952
  2576
/*
ds@1952
  2577
** Truncate the file to the number of pages specified.
ds@1952
  2578
*/
ds@1952
  2579
int sqlite3PagerTruncate(Pager *pPager, Pgno nPage){
ds@1952
  2580
  int rc;
ds@1952
  2581
  assert( pPager->state>=PAGER_SHARED || MEMDB );
ds@1952
  2582
  sqlite3PagerPagecount(pPager);
ds@1952
  2583
  if( pPager->errCode ){
ds@1952
  2584
    rc = pPager->errCode;
ds@1952
  2585
    return rc;
ds@1952
  2586
  }
ds@1952
  2587
  if( nPage>=(unsigned)pPager->dbSize ){
ds@1952
  2588
    return SQLITE_OK;
ds@1952
  2589
  }
ds@1952
  2590
  if( MEMDB ){
ds@1952
  2591
    pPager->dbSize = nPage;
ds@1952
  2592
    pager_truncate_cache(pPager);
ds@1952
  2593
    return SQLITE_OK;
ds@1952
  2594
  }
ds@1952
  2595
  pagerEnter(pPager);
ds@1952
  2596
  rc = syncJournal(pPager);
ds@1952
  2597
  pagerLeave(pPager);
ds@1952
  2598
  if( rc!=SQLITE_OK ){
ds@1952
  2599
    return rc;
ds@1952
  2600
  }
ds@1952
  2601
ds@1952
  2602
  /* Get an exclusive lock on the database before truncating. */
ds@1952
  2603
  pagerEnter(pPager);
ds@1952
  2604
  rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
ds@1952
  2605
  pagerLeave(pPager);
ds@1952
  2606
  if( rc!=SQLITE_OK ){
ds@1952
  2607
    return rc;
ds@1952
  2608
  }
ds@1952
  2609
ds@1952
  2610
  rc = pager_truncate(pPager, nPage);
ds@1952
  2611
  return rc;
ds@1952
  2612
}
ds@1952
  2613
ds@1952
  2614
/*
ds@1952
  2615
** Shutdown the page cache.  Free all memory and close all files.
ds@1952
  2616
**
ds@1952
  2617
** If a transaction was in progress when this routine is called, that
ds@1952
  2618
** transaction is rolled back.  All outstanding pages are invalidated
ds@1952
  2619
** and their memory is freed.  Any attempt to use a page associated
ds@1952
  2620
** with this page cache after this function returns will likely
ds@1952
  2621
** result in a coredump.
ds@1952
  2622
**
ds@1952
  2623
** This function always succeeds. If a transaction is active an attempt
ds@1952
  2624
** is made to roll it back. If an error occurs during the rollback 
ds@1952
  2625
** a hot journal may be left in the filesystem but no error is returned
ds@1952
  2626
** to the caller.
ds@1952
  2627
*/
ds@1952
  2628
int sqlite3PagerClose(Pager *pPager){
ds@1952
  2629
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
ds@1952
  2630
  if( !MEMDB ){
ds@1952
  2631
    sqlite3_mutex *mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
ds@1952
  2632
    sqlite3_mutex_enter(mutex);
ds@1952
  2633
    if( pPager->pPrev ){
ds@1952
  2634
      pPager->pPrev->pNext = pPager->pNext;
ds@1952
  2635
    }else{
ds@1952
  2636
      sqlite3PagerList = pPager->pNext;
ds@1952
  2637
    }
ds@1952
  2638
    if( pPager->pNext ){
ds@1952
  2639
      pPager->pNext->pPrev = pPager->pPrev;
ds@1952
  2640
    }
ds@1952
  2641
    sqlite3_mutex_leave(mutex);
ds@1952
  2642
  }
ds@1952
  2643
#endif
ds@1952
  2644
ds@1952
  2645
  disable_simulated_io_errors();
ds@1952
  2646
  pPager->errCode = 0;
ds@1952
  2647
  pPager->exclusiveMode = 0;
ds@1952
  2648
  pager_reset(pPager);
ds@1952
  2649
  pagerUnlockAndRollback(pPager);
ds@1952
  2650
  enable_simulated_io_errors();
ds@1952
  2651
  PAGERTRACE2("CLOSE %d\n", PAGERID(pPager));
ds@1952
  2652
  IOTRACE(("CLOSE %p\n", pPager))
ds@1952
  2653
  assert( pPager->errCode || (pPager->journalOpen==0 && pPager->stmtOpen==0) );
ds@1952
  2654
  if( pPager->journalOpen ){
ds@1952
  2655
    sqlite3OsClose(pPager->jfd);
ds@1952
  2656
  }
ds@1952
  2657
  sqlite3_free(pPager->aInJournal);
ds@1952
  2658
  if( pPager->stmtOpen ){
ds@1952
  2659
    sqlite3OsClose(pPager->stfd);
ds@1952
  2660
  }
ds@1952
  2661
  sqlite3OsClose(pPager->fd);
ds@1952
  2662
  /* Temp files are automatically deleted by the OS
ds@1952
  2663
  ** if( pPager->tempFile ){
ds@1952
  2664
  **   sqlite3OsDelete(pPager->zFilename);
ds@1952
  2665
  ** }
ds@1952
  2666
  */
ds@1952
  2667
ds@1952
  2668
  sqlite3_free(pPager->aHash);
ds@1952
  2669
  sqlite3_free(pPager->pTmpSpace);
ds@1952
  2670
  sqlite3_free(pPager);
ds@1952
  2671
  return SQLITE_OK;
ds@1952
  2672
}
ds@1952
  2673
ds@1952
  2674
#if !defined(NDEBUG) || defined(SQLITE_TEST)
ds@1952
  2675
/*
ds@1952
  2676
** Return the page number for the given page data.
ds@1952
  2677
*/
ds@1952
  2678
Pgno sqlite3PagerPagenumber(DbPage *p){
ds@1952
  2679
  return p->pgno;
ds@1952
  2680
}
ds@1952
  2681
#endif
ds@1952
  2682
ds@1952
  2683
/*
ds@1952
  2684
** The page_ref() function increments the reference count for a page.
ds@1952
  2685
** If the page is currently on the freelist (the reference count is zero) then
ds@1952
  2686
** remove it from the freelist.
ds@1952
  2687
**
ds@1952
  2688
** For non-test systems, page_ref() is a macro that calls _page_ref()
ds@1952
  2689
** online of the reference count is zero.  For test systems, page_ref()
ds@1952
  2690
** is a real function so that we can set breakpoints and trace it.
ds@1952
  2691
*/
ds@1952
  2692
static void _page_ref(PgHdr *pPg){
ds@1952
  2693
  if( pPg->nRef==0 ){
ds@1952
  2694
    /* The page is currently on the freelist.  Remove it. */
ds@1952
  2695
    lruListRemove(pPg);
ds@1952
  2696
    pPg->pPager->nRef++;
ds@1952
  2697
  }
ds@1952
  2698
  pPg->nRef++;
ds@1952
  2699
  REFINFO(pPg);
ds@1952
  2700
}
ds@1952
  2701
#ifdef SQLITE_DEBUG
ds@1952
  2702
  static void page_ref(PgHdr *pPg){
ds@1952
  2703
    if( pPg->nRef==0 ){
ds@1952
  2704
      _page_ref(pPg);
ds@1952
  2705
    }else{
ds@1952
  2706
      pPg->nRef++;
ds@1952
  2707
      REFINFO(pPg);
ds@1952
  2708
    }
ds@1952
  2709
  }
ds@1952
  2710
#else
ds@1952
  2711
# define page_ref(P)   ((P)->nRef==0?_page_ref(P):(void)(P)->nRef++)
ds@1952
  2712
#endif
ds@1952
  2713
ds@1952
  2714
/*
ds@1952
  2715
** Increment the reference count for a page.  The input pointer is
ds@1952
  2716
** a reference to the page data.
ds@1952
  2717
*/
ds@1952
  2718
int sqlite3PagerRef(DbPage *pPg){
ds@1952
  2719
  pagerEnter(pPg->pPager);
ds@1952
  2720
  page_ref(pPg);
ds@1952
  2721
  pagerLeave(pPg->pPager);
ds@1952
  2722
  return SQLITE_OK;
ds@1952
  2723
}
ds@1952
  2724
ds@1952
  2725
/*
ds@1952
  2726
** Sync the journal.  In other words, make sure all the pages that have
ds@1952
  2727
** been written to the journal have actually reached the surface of the
ds@1952
  2728
** disk.  It is not safe to modify the original database file until after
ds@1952
  2729
** the journal has been synced.  If the original database is modified before
ds@1952
  2730
** the journal is synced and a power failure occurs, the unsynced journal
ds@1952
  2731
** data would be lost and we would be unable to completely rollback the
ds@1952
  2732
** database changes.  Database corruption would occur.
ds@1952
  2733
** 
ds@1952
  2734
** This routine also updates the nRec field in the header of the journal.
ds@1952
  2735
** (See comments on the pager_playback() routine for additional information.)
ds@1952
  2736
** If the sync mode is FULL, two syncs will occur.  First the whole journal
ds@1952
  2737
** is synced, then the nRec field is updated, then a second sync occurs.
ds@1952
  2738
**
ds@1952
  2739
** For temporary databases, we do not care if we are able to rollback
ds@1952
  2740
** after a power failure, so no sync occurs.
ds@1952
  2741
**
ds@1952
  2742
** If the IOCAP_SEQUENTIAL flag is set for the persistent media on which
ds@1952
  2743
** the database is stored, then OsSync() is never called on the journal
ds@1952
  2744
** file. In this case all that is required is to update the nRec field in
ds@1952
  2745
** the journal header.
ds@1952
  2746
**
ds@1952
  2747
** This routine clears the needSync field of every page current held in
ds@1952
  2748
** memory.
ds@1952
  2749
*/
ds@1952
  2750
static int syncJournal(Pager *pPager){
ds@1952
  2751
  PgHdr *pPg;
ds@1952
  2752
  int rc = SQLITE_OK;
ds@1952
  2753
ds@1952
  2754
ds@1952
  2755
  /* Sync the journal before modifying the main database
ds@1952
  2756
  ** (assuming there is a journal and it needs to be synced.)
ds@1952
  2757
  */
ds@1952
  2758
  if( pPager->needSync ){
ds@1952
  2759
    if( !pPager->tempFile ){
ds@1952
  2760
      int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
ds@1952
  2761
      assert( pPager->journalOpen );
ds@1952
  2762
ds@1952
  2763
      /* assert( !pPager->noSync ); // noSync might be set if synchronous
ds@1952
  2764
      ** was turned off after the transaction was started.  Ticket #615 */
ds@1952
  2765
#ifndef NDEBUG
ds@1952
  2766
      {
ds@1952
  2767
        /* Make sure the pPager->nRec counter we are keeping agrees
ds@1952
  2768
        ** with the nRec computed from the size of the journal file.
ds@1952
  2769
        */
ds@1952
  2770
        i64 jSz;
ds@1952
  2771
        rc = sqlite3OsFileSize(pPager->jfd, &jSz);
ds@1952
  2772
        if( rc!=0 ) return rc;
ds@1952
  2773
        assert( pPager->journalOff==jSz );
ds@1952
  2774
      }
ds@1952
  2775
#endif
ds@1952
  2776
      if( 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
ds@1952
  2777
        /* Write the nRec value into the journal file header. If in
ds@1952
  2778
        ** full-synchronous mode, sync the journal first. This ensures that
ds@1952
  2779
        ** all data has really hit the disk before nRec is updated to mark
ds@1952
  2780
        ** it as a candidate for rollback.
ds@1952
  2781
        **
ds@1952
  2782
        ** This is not required if the persistent media supports the
ds@1952
  2783
        ** SAFE_APPEND property. Because in this case it is not possible 
ds@1952
  2784
        ** for garbage data to be appended to the file, the nRec field
ds@1952
  2785
        ** is populated with 0xFFFFFFFF when the journal header is written
ds@1952
  2786
        ** and never needs to be updated.
ds@1952
  2787
        */
ds@1952
  2788
        i64 jrnlOff;
ds@1952
  2789
        if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
ds@1952
  2790
          PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
ds@1952
  2791
          IOTRACE(("JSYNC %p\n", pPager))
ds@1952
  2792
          rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags);
ds@1952
  2793
          if( rc!=0 ) return rc;
ds@1952
  2794
        }
ds@1952
  2795
ds@1952
  2796
        jrnlOff = pPager->journalHdr + sizeof(aJournalMagic);
ds@1952
  2797
        IOTRACE(("JHDR %p %lld %d\n", pPager, jrnlOff, 4));
ds@1952
  2798
        rc = write32bits(pPager->jfd, jrnlOff, pPager->nRec);
ds@1952
  2799
        if( rc ) return rc;
ds@1952
  2800
      }
ds@1952
  2801
      if( 0==(iDc&SQLITE_IOCAP_SEQUENTIAL) ){
ds@1952
  2802
        PAGERTRACE2("SYNC journal of %d\n", PAGERID(pPager));
ds@1952
  2803
        IOTRACE(("JSYNC %p\n", pPager))
ds@1952
  2804
        rc = sqlite3OsSync(pPager->jfd, pPager->sync_flags| 
ds@1952
  2805
          (pPager->sync_flags==SQLITE_SYNC_FULL?SQLITE_SYNC_DATAONLY:0)
ds@1952
  2806
        );
ds@1952
  2807
        if( rc!=0 ) return rc;
ds@1952
  2808
      }
ds@1952
  2809
      pPager->journalStarted = 1;
ds@1952
  2810
    }
ds@1952
  2811
    pPager->needSync = 0;
ds@1952
  2812
ds@1952
  2813
    /* Erase the needSync flag from every page.
ds@1952
  2814
    */
ds@1952
  2815
    for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
ds@1952
  2816
      pPg->needSync = 0;
ds@1952
  2817
    }
ds@1952
  2818
    lruListSetFirstSynced(pPager);
ds@1952
  2819
  }
ds@1952
  2820
ds@1952
  2821
#ifndef NDEBUG
ds@1952
  2822
  /* If the Pager.needSync flag is clear then the PgHdr.needSync
ds@1952
  2823
  ** flag must also be clear for all pages.  Verify that this
ds@1952
  2824
  ** invariant is true.
ds@1952
  2825
  */
ds@1952
  2826
  else{
ds@1952
  2827
    for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
ds@1952
  2828
      assert( pPg->needSync==0 );
ds@1952
  2829
    }
ds@1952
  2830
    assert( pPager->lru.pFirstSynced==pPager->lru.pFirst );
ds@1952
  2831
  }
ds@1952
  2832
#endif
ds@1952
  2833
ds@1952
  2834
  return rc;
ds@1952
  2835
}
ds@1952
  2836
ds@1952
  2837
/*
ds@1952
  2838
** Merge two lists of pages connected by pDirty and in pgno order.
ds@1952
  2839
** Do not both fixing the pPrevDirty pointers.
ds@1952
  2840
*/
ds@1952
  2841
static PgHdr *merge_pagelist(PgHdr *pA, PgHdr *pB){
ds@1952
  2842
  PgHdr result, *pTail;
ds@1952
  2843
  pTail = &result;
ds@1952
  2844
  while( pA && pB ){
ds@1952
  2845
    if( pA->pgno<pB->pgno ){
ds@1952
  2846
      pTail->pDirty = pA;
ds@1952
  2847
      pTail = pA;
ds@1952
  2848
      pA = pA->pDirty;
ds@1952
  2849
    }else{
ds@1952
  2850
      pTail->pDirty = pB;
ds@1952
  2851
      pTail = pB;
ds@1952
  2852
      pB = pB->pDirty;
ds@1952
  2853
    }
ds@1952
  2854
  }
ds@1952
  2855
  if( pA ){
ds@1952
  2856
    pTail->pDirty = pA;
ds@1952
  2857
  }else if( pB ){
ds@1952
  2858
    pTail->pDirty = pB;
ds@1952
  2859
  }else{
ds@1952
  2860
    pTail->pDirty = 0;
ds@1952
  2861
  }
ds@1952
  2862
  return result.pDirty;
ds@1952
  2863
}
ds@1952
  2864
ds@1952
  2865
/*
ds@1952
  2866
** Sort the list of pages in accending order by pgno.  Pages are
ds@1952
  2867
** connected by pDirty pointers.  The pPrevDirty pointers are
ds@1952
  2868
** corrupted by this sort.
ds@1952
  2869
*/
ds@1952
  2870
#define N_SORT_BUCKET_ALLOC 25
ds@1952
  2871
#define N_SORT_BUCKET       25
ds@1952
  2872
#ifdef SQLITE_TEST
ds@1952
  2873
  int sqlite3_pager_n_sort_bucket = 0;
ds@1952
  2874
  #undef N_SORT_BUCKET
ds@1952
  2875
  #define N_SORT_BUCKET \
ds@1952
  2876
   (sqlite3_pager_n_sort_bucket?sqlite3_pager_n_sort_bucket:N_SORT_BUCKET_ALLOC)
ds@1952
  2877
#endif
ds@1952
  2878
static PgHdr *sort_pagelist(PgHdr *pIn){
ds@1952
  2879
  PgHdr *a[N_SORT_BUCKET_ALLOC], *p;
ds@1952
  2880
  int i;
ds@1952
  2881
  memset(a, 0, sizeof(a));
ds@1952
  2882
  while( pIn ){
ds@1952
  2883
    p = pIn;
ds@1952
  2884
    pIn = p->pDirty;
ds@1952
  2885
    p->pDirty = 0;
ds@1952
  2886
    for(i=0; i<N_SORT_BUCKET-1; i++){
ds@1952
  2887
      if( a[i]==0 ){
ds@1952
  2888
        a[i] = p;
ds@1952
  2889
        break;
ds@1952
  2890
      }else{
ds@1952
  2891
        p = merge_pagelist(a[i], p);
ds@1952
  2892
        a[i] = 0;
ds@1952
  2893
      }
ds@1952
  2894
    }
ds@1952
  2895
    if( i==N_SORT_BUCKET-1 ){
ds@1952
  2896
      /* Coverage: To get here, there need to be 2^(N_SORT_BUCKET) 
ds@1952
  2897
      ** elements in the input list. This is possible, but impractical.
ds@1952
  2898
      ** Testing this line is the point of global variable
ds@1952
  2899
      ** sqlite3_pager_n_sort_bucket.
ds@1952
  2900
      */
ds@1952
  2901
      a[i] = merge_pagelist(a[i], p);
ds@1952
  2902
    }
ds@1952
  2903
  }
ds@1952
  2904
  p = a[0];
ds@1952
  2905
  for(i=1; i<N_SORT_BUCKET; i++){
ds@1952
  2906
    p = merge_pagelist(p, a[i]);
ds@1952
  2907
  }
ds@1952
  2908
  return p;
ds@1952
  2909
}
ds@1952
  2910
ds@1952
  2911
/*
ds@1952
  2912
** Given a list of pages (connected by the PgHdr.pDirty pointer) write
ds@1952
  2913
** every one of those pages out to the database file and mark them all
ds@1952
  2914
** as clean.
ds@1952
  2915
*/
ds@1952
  2916
static int pager_write_pagelist(PgHdr *pList){
ds@1952
  2917
  Pager *pPager;
ds@1952
  2918
  PgHdr *p;
ds@1952
  2919
  int rc;
ds@1952
  2920
ds@1952
  2921
  if( pList==0 ) return SQLITE_OK;
ds@1952
  2922
  pPager = pList->pPager;
ds@1952
  2923
ds@1952
  2924
  /* At this point there may be either a RESERVED or EXCLUSIVE lock on the
ds@1952
  2925
  ** database file. If there is already an EXCLUSIVE lock, the following
ds@1952
  2926
  ** calls to sqlite3OsLock() are no-ops.
ds@1952
  2927
  **
ds@1952
  2928
  ** Moving the lock from RESERVED to EXCLUSIVE actually involves going
ds@1952
  2929
  ** through an intermediate state PENDING.   A PENDING lock prevents new
ds@1952
  2930
  ** readers from attaching to the database but is unsufficient for us to
ds@1952
  2931
  ** write.  The idea of a PENDING lock is to prevent new readers from
ds@1952
  2932
  ** coming in while we wait for existing readers to clear.
ds@1952
  2933
  **
ds@1952
  2934
  ** While the pager is in the RESERVED state, the original database file
ds@1952
  2935
  ** is unchanged and we can rollback without having to playback the
ds@1952
  2936
  ** journal into the original database file.  Once we transition to
ds@1952
  2937
  ** EXCLUSIVE, it means the database file has been changed and any rollback
ds@1952
  2938
  ** will require a journal playback.
ds@1952
  2939
  */
ds@1952
  2940
  rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
ds@1952
  2941
  if( rc!=SQLITE_OK ){
ds@1952
  2942
    return rc;
ds@1952
  2943
  }
ds@1952
  2944
ds@1952
  2945
  pList = sort_pagelist(pList);
ds@1952
  2946
  for(p=pList; p; p=p->pDirty){
ds@1952
  2947
    assert( p->dirty );
ds@1952
  2948
    p->dirty = 0;
ds@1952
  2949
  }
ds@1952
  2950
  while( pList ){
ds@1952
  2951
ds@1952
  2952
    /* If the file has not yet been opened, open it now. */
ds@1952
  2953
    if( !pPager->fd->pMethods ){
ds@1952
  2954
      assert(pPager->tempFile);
ds@1952
  2955
      rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->fd, pPager->zFilename,
ds@1952
  2956
                                pPager->vfsFlags);
ds@1952
  2957
      if( rc ) return rc;
ds@1952
  2958
    }
ds@1952
  2959
ds@1952
  2960
    /* If there are dirty pages in the page cache with page numbers greater
ds@1952
  2961
    ** than Pager.dbSize, this means sqlite3PagerTruncate() was called to
ds@1952
  2962
    ** make the file smaller (presumably by auto-vacuum code). Do not write
ds@1952
  2963
    ** any such pages to the file.
ds@1952
  2964
    */
ds@1952
  2965
    if( pList->pgno<=pPager->dbSize ){
ds@1952
  2966
      i64 offset = (pList->pgno-1)*(i64)pPager->pageSize;
ds@1952
  2967
      char *pData = CODEC2(pPager, PGHDR_TO_DATA(pList), pList->pgno, 6);
ds@1952
  2968
      PAGERTRACE4("STORE %d page %d hash(%08x)\n",
ds@1952
  2969
                   PAGERID(pPager), pList->pgno, pager_pagehash(pList));
ds@1952
  2970
      IOTRACE(("PGOUT %p %d\n", pPager, pList->pgno));
ds@1952
  2971
      rc = sqlite3OsWrite(pPager->fd, pData, pPager->pageSize, offset);
ds@1952
  2972
      PAGER_INCR(sqlite3_pager_writedb_count);
ds@1952
  2973
      PAGER_INCR(pPager->nWrite);
ds@1952
  2974
      if( pList->pgno==1 ){
ds@1952
  2975
        memcpy(&pPager->dbFileVers, &pData[24], sizeof(pPager->dbFileVers));
ds@1952
  2976
      }
ds@1952
  2977
    }
ds@1952
  2978
#ifndef NDEBUG
ds@1952
  2979
    else{
ds@1952
  2980
      PAGERTRACE3("NOSTORE %d page %d\n", PAGERID(pPager), pList->pgno);
ds@1952
  2981
    }
ds@1952
  2982
#endif
ds@1952
  2983
    if( rc ) return rc;
ds@1952
  2984
#ifdef SQLITE_CHECK_PAGES
ds@1952
  2985
    pList->pageHash = pager_pagehash(pList);
ds@1952
  2986
#endif
ds@1952
  2987
    pList = pList->pDirty;
ds@1952
  2988
  }
ds@1952
  2989
  return SQLITE_OK;
ds@1952
  2990
}
ds@1952
  2991
ds@1952
  2992
/*
ds@1952
  2993
** Collect every dirty page into a dirty list and
ds@1952
  2994
** return a pointer to the head of that list.  All pages are
ds@1952
  2995
** collected even if they are still in use.
ds@1952
  2996
*/
ds@1952
  2997
static PgHdr *pager_get_all_dirty_pages(Pager *pPager){
ds@1952
  2998
  return pPager->pDirty;
ds@1952
  2999
}
ds@1952
  3000
ds@1952
  3001
/*
ds@1952
  3002
** Return TRUE if there is a hot journal on the given pager.
ds@1952
  3003
** A hot journal is one that needs to be played back.
ds@1952
  3004
**
ds@1952
  3005
** If the current size of the database file is 0 but a journal file
ds@1952
  3006
** exists, that is probably an old journal left over from a prior
ds@1952
  3007
** database with the same name.  Just delete the journal.
ds@1952
  3008
*/
ds@1952
  3009
static int hasHotJournal(Pager *pPager){
ds@1952
  3010
  sqlite3_vfs *pVfs = pPager->pVfs;
ds@1952
  3011
  if( !pPager->useJournal ) return 0;
ds@1952
  3012
  if( !sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS) ){
ds@1952
  3013
    return 0;
ds@1952
  3014
  }
ds@1952
  3015
  if( sqlite3OsCheckReservedLock(pPager->fd) ){
ds@1952
  3016
    return 0;
ds@1952
  3017
  }
ds@1952
  3018
  if( sqlite3PagerPagecount(pPager)==0 ){
ds@1952
  3019
    sqlite3OsDelete(pVfs, pPager->zJournal, 0);
ds@1952
  3020
    return 0;
ds@1952
  3021
  }else{
ds@1952
  3022
    return 1;
ds@1952
  3023
  }
ds@1952
  3024
}
ds@1952
  3025
ds@1952
  3026
/*
ds@1952
  3027
** Try to find a page in the cache that can be recycled. 
ds@1952
  3028
**
ds@1952
  3029
** This routine may return SQLITE_IOERR, SQLITE_FULL or SQLITE_OK. It 
ds@1952
  3030
** does not set the pPager->errCode variable.
ds@1952
  3031
*/
ds@1952
  3032
static int pager_recycle(Pager *pPager, PgHdr **ppPg){
ds@1952
  3033
  PgHdr *pPg;
ds@1952
  3034
  *ppPg = 0;
ds@1952
  3035
ds@1952
  3036
  /* It is illegal to call this function unless the pager object
ds@1952
  3037
  ** pointed to by pPager has at least one free page (page with nRef==0).
ds@1952
  3038
  */ 
ds@1952
  3039
  assert(!MEMDB);
ds@1952
  3040
  assert(pPager->lru.pFirst);
ds@1952
  3041
ds@1952
  3042
  /* Find a page to recycle.  Try to locate a page that does not
ds@1952
  3043
  ** require us to do an fsync() on the journal.
ds@1952
  3044
  */
ds@1952
  3045
  pPg = pPager->lru.pFirstSynced;
ds@1952
  3046
ds@1952
  3047
  /* If we could not find a page that does not require an fsync()
ds@1952
  3048
  ** on the journal file then fsync the journal file.  This is a
ds@1952
  3049
  ** very slow operation, so we work hard to avoid it.  But sometimes
ds@1952
  3050
  ** it can't be helped.
ds@1952
  3051
  */
ds@1952
  3052
  if( pPg==0 && pPager->lru.pFirst){
ds@1952
  3053
    int iDc = sqlite3OsDeviceCharacteristics(pPager->fd);
ds@1952
  3054
    int rc = syncJournal(pPager);
ds@1952
  3055
    if( rc!=0 ){
ds@1952
  3056
      return rc;
ds@1952
  3057
    }
ds@1952
  3058
    if( pPager->fullSync && 0==(iDc&SQLITE_IOCAP_SAFE_APPEND) ){
ds@1952
  3059
      /* If in full-sync mode, write a new journal header into the
ds@1952
  3060
      ** journal file. This is done to avoid ever modifying a journal
ds@1952
  3061
      ** header that is involved in the rollback of pages that have
ds@1952
  3062
      ** already been written to the database (in case the header is
ds@1952
  3063
      ** trashed when the nRec field is updated).
ds@1952
  3064
      */
ds@1952
  3065
      pPager->nRec = 0;
ds@1952
  3066
      assert( pPager->journalOff > 0 );
ds@1952
  3067
      assert( pPager->doNotSync==0 );
ds@1952
  3068
      rc = writeJournalHdr(pPager);
ds@1952
  3069
      if( rc!=0 ){
ds@1952
  3070
        return rc;
ds@1952
  3071
      }
ds@1952
  3072
    }
ds@1952
  3073
    pPg = pPager->lru.pFirst;
ds@1952
  3074
  }
ds@1952
  3075
ds@1952
  3076
  assert( pPg->nRef==0 );
ds@1952
  3077
ds@1952
  3078
  /* Write the page to the database file if it is dirty.
ds@1952
  3079
  */
ds@1952
  3080
  if( pPg->dirty ){
ds@1952
  3081
    int rc;
ds@1952
  3082
    assert( pPg->needSync==0 );
ds@1952
  3083
    makeClean(pPg);
ds@1952
  3084
    pPg->dirty = 1;
ds@1952
  3085
    pPg->pDirty = 0;
ds@1952
  3086
    rc = pager_write_pagelist( pPg );
ds@1952
  3087
    pPg->dirty = 0;
ds@1952
  3088
    if( rc!=SQLITE_OK ){
ds@1952
  3089
      return rc;
ds@1952
  3090
    }
ds@1952
  3091
  }
ds@1952
  3092
  assert( pPg->dirty==0 );
ds@1952
  3093
ds@1952
  3094
  /* If the page we are recycling is marked as alwaysRollback, then
ds@1952
  3095
  ** set the global alwaysRollback flag, thus disabling the
ds@1952
  3096
  ** sqlite3PagerDontRollback() optimization for the rest of this transaction.
ds@1952
  3097
  ** It is necessary to do this because the page marked alwaysRollback
ds@1952
  3098
  ** might be reloaded at a later time but at that point we won't remember
ds@1952
  3099
  ** that is was marked alwaysRollback.  This means that all pages must
ds@1952
  3100
  ** be marked as alwaysRollback from here on out.
ds@1952
  3101
  */
ds@1952
  3102
  if( pPg->alwaysRollback ){
ds@1952
  3103
    IOTRACE(("ALWAYS_ROLLBACK %p\n", pPager))
ds@1952
  3104
    pPager->alwaysRollback = 1;
ds@1952
  3105
  }
ds@1952
  3106
ds@1952
  3107
  /* Unlink the old page from the free list and the hash table
ds@1952
  3108
  */
ds@1952
  3109
  unlinkPage(pPg);
ds@1952
  3110
  assert( pPg->pgno==0 );
ds@1952
  3111
ds@1952
  3112
  *ppPg = pPg;
ds@1952
  3113
  return SQLITE_OK;
ds@1952
  3114
}
ds@1952
  3115
ds@1952
  3116
#ifdef SQLITE_ENABLE_MEMORY_MANAGEMENT
ds@1952
  3117
/*
ds@1952
  3118
** This function is called to free superfluous dynamically allocated memory
ds@1952
  3119
** held by the pager system. Memory in use by any SQLite pager allocated
ds@1952
  3120
** by the current thread may be sqlite3_free()ed.
ds@1952
  3121
**
ds@1952
  3122
** nReq is the number of bytes of memory required. Once this much has
ds@1952
  3123
** been released, the function returns. The return value is the total number 
ds@1952
  3124
** of bytes of memory released.
ds@1952
  3125
*/
ds@1952
  3126
int sqlite3PagerReleaseMemory(int nReq){
ds@1952
  3127
  int nReleased = 0;          /* Bytes of memory released so far */
ds@1952
  3128
  sqlite3_mutex *mutex;       /* The MEM2 mutex */
ds@1952
  3129
  Pager *pPager;              /* For looping over pagers */
ds@1952
  3130
  int rc = SQLITE_OK;
ds@1952
  3131
ds@1952
  3132
  /* Acquire the memory-management mutex
ds@1952
  3133
  */
ds@1952
  3134
  mutex = sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_MEM2);
ds@1952
  3135
  sqlite3_mutex_enter(mutex);
ds@1952
  3136
ds@1952
  3137
  /* Signal all database connections that memory management wants
ds@1952
  3138
  ** to have access to the pagers.
ds@1952
  3139
  */
ds@1952
  3140
  for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
ds@1952
  3141
     pPager->iInUseMM = 1;
ds@1952
  3142
  }
ds@1952
  3143
ds@1952
  3144
  while( rc==SQLITE_OK && (nReq<0 || nReleased<nReq) ){
ds@1952
  3145
    PgHdr *pPg;
ds@1952
  3146
    PgHdr *pRecycled;
ds@1952
  3147
 
ds@1952
  3148
    /* Try to find a page to recycle that does not require a sync(). If
ds@1952
  3149
    ** this is not possible, find one that does require a sync().
ds@1952
  3150
    */
ds@1952
  3151
    sqlite3_mutex_enter(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
ds@1952
  3152
    pPg = sqlite3LruPageList.pFirstSynced;
ds@1952
  3153
    while( pPg && (pPg->needSync || pPg->pPager->iInUseDB) ){
ds@1952
  3154
      pPg = pPg->gfree.pNext;
ds@1952
  3155
    }
ds@1952
  3156
    if( !pPg ){
ds@1952
  3157
      pPg = sqlite3LruPageList.pFirst;
ds@1952
  3158
      while( pPg && pPg->pPager->iInUseDB ){
ds@1952
  3159
        pPg = pPg->gfree.pNext;
ds@1952
  3160
      }
ds@1952
  3161
    }
ds@1952
  3162
    sqlite3_mutex_leave(sqlite3_mutex_alloc(SQLITE_MUTEX_STATIC_LRU));
ds@1952
  3163
ds@1952
  3164
    /* If pPg==0, then the block above has failed to find a page to
ds@1952
  3165
    ** recycle. In this case return early - no further memory will
ds@1952
  3166
    ** be released.
ds@1952
  3167
    */
ds@1952
  3168
    if( !pPg ) break;
ds@1952
  3169
ds@1952
  3170
    pPager = pPg->pPager;
ds@1952
  3171
    assert(!pPg->needSync || pPg==pPager->lru.pFirst);
ds@1952
  3172
    assert(pPg->needSync || pPg==pPager->lru.pFirstSynced);
ds@1952
  3173
  
ds@1952
  3174
    rc = pager_recycle(pPager, &pRecycled);
ds@1952
  3175
    assert(pRecycled==pPg || rc!=SQLITE_OK);
ds@1952
  3176
    if( rc==SQLITE_OK ){
ds@1952
  3177
      /* We've found a page to free. At this point the page has been 
ds@1952
  3178
      ** removed from the page hash-table, free-list and synced-list 
ds@1952
  3179
      ** (pFirstSynced). It is still in the all pages (pAll) list. 
ds@1952
  3180
      ** Remove it from this list before freeing.
ds@1952
  3181
      **
ds@1952
  3182
      ** Todo: Check the Pager.pStmt list to make sure this is Ok. It 
ds@1952
  3183
      ** probably is though.
ds@1952
  3184
      */
ds@1952
  3185
      PgHdr *pTmp;
ds@1952
  3186
      assert( pPg );
ds@1952
  3187
      if( pPg==pPager->pAll ){
ds@1952
  3188
         pPager->pAll = pPg->pNextAll;
ds@1952
  3189
      }else{
ds@1952
  3190
        for( pTmp=pPager->pAll; pTmp->pNextAll!=pPg; pTmp=pTmp->pNextAll ){}
ds@1952
  3191
        pTmp->pNextAll = pPg->pNextAll;
ds@1952
  3192
      }
ds@1952
  3193
      nReleased += (
ds@1952
  3194
          sizeof(*pPg) + pPager->pageSize
ds@1952
  3195
          + sizeof(u32) + pPager->nExtra
ds@1952
  3196
          + MEMDB*sizeof(PgHistory) 
ds@1952
  3197
      );
ds@1952
  3198
      IOTRACE(("PGFREE %p %d *\n", pPager, pPg->pgno));
ds@1952
  3199
      PAGER_INCR(sqlite3_pager_pgfree_count);
ds@1952
  3200
      sqlite3_free(pPg->pData);
ds@1952
  3201
      sqlite3_free(pPg);
ds@1952
  3202
      pPager->nPage--;
ds@1952
  3203
    }else{
ds@1952
  3204
      /* An error occured whilst writing to the database file or 
ds@1952
  3205
      ** journal in pager_recycle(). The error is not returned to the 
ds@1952
  3206
      ** caller of this function. Instead, set the Pager.errCode variable.
ds@1952
  3207
      ** The error will be returned to the user (or users, in the case 
ds@1952
  3208
      ** of a shared pager cache) of the pager for which the error occured.
ds@1952
  3209
      */
ds@1952
  3210
      assert(
ds@1952
  3211
          (rc&0xff)==SQLITE_IOERR ||
ds@1952
  3212
          rc==SQLITE_FULL ||
ds@1952
  3213
          rc==SQLITE_BUSY
ds@1952
  3214
      );
ds@1952
  3215
      assert( pPager->state>=PAGER_RESERVED );
ds@1952
  3216
      pager_error(pPager, rc);
ds@1952
  3217
    }
ds@1952
  3218
  }
ds@1952
  3219
ds@1952
  3220
  /* Clear the memory management flags and release the mutex
ds@1952
  3221
  */
ds@1952
  3222
  for(pPager=sqlite3PagerList; pPager; pPager=pPager->pNext){
ds@1952
  3223
     pPager->iInUseMM = 0;
ds@1952
  3224
  }
ds@1952
  3225
  sqlite3_mutex_leave(mutex);
ds@1952
  3226
ds@1952
  3227
  /* Return the number of bytes released
ds@1952
  3228
  */
ds@1952
  3229
  return nReleased;
ds@1952
  3230
}
ds@1952
  3231
#endif /* SQLITE_ENABLE_MEMORY_MANAGEMENT */
ds@1952
  3232
ds@1952
  3233
/*
ds@1952
  3234
** Read the content of page pPg out of the database file.
ds@1952
  3235
*/
ds@1952
  3236
static int readDbPage(Pager *pPager, PgHdr *pPg, Pgno pgno){
ds@1952
  3237
  int rc;
ds@1952
  3238
  i64 offset;
ds@1952
  3239
  assert( MEMDB==0 );
ds@1952
  3240
  assert(pPager->fd->pMethods||pPager->tempFile);
ds@1952
  3241
  if( !pPager->fd->pMethods ){
ds@1952
  3242
    return SQLITE_IOERR_SHORT_READ;
ds@1952
  3243
  }
ds@1952
  3244
  offset = (pgno-1)*(i64)pPager->pageSize;
ds@1952
  3245
  rc = sqlite3OsRead(pPager->fd, PGHDR_TO_DATA(pPg), pPager->pageSize, offset);
ds@1952
  3246
  PAGER_INCR(sqlite3_pager_readdb_count);
ds@1952
  3247
  PAGER_INCR(pPager->nRead);
ds@1952
  3248
  IOTRACE(("PGIN %p %d\n", pPager, pgno));
ds@1952
  3249
  if( pgno==1 ){
ds@1952
  3250
    memcpy(&pPager->dbFileVers, &((u8*)PGHDR_TO_DATA(pPg))[24],
ds@1952
  3251
                                              sizeof(pPager->dbFileVers));
ds@1952
  3252
  }
ds@1952
  3253
  CODEC1(pPager, PGHDR_TO_DATA(pPg), pPg->pgno, 3);
ds@1952
  3254
  PAGERTRACE4("FETCH %d page %d hash(%08x)\n",
ds@1952
  3255
               PAGERID(pPager), pPg->pgno, pager_pagehash(pPg));
ds@1952
  3256
  return rc;
ds@1952
  3257
}
ds@1952
  3258
ds@1952
  3259
ds@1952
  3260
/*
ds@1952
  3261
** This function is called to obtain the shared lock required before
ds@1952
  3262
** data may be read from the pager cache. If the shared lock has already
ds@1952
  3263
** been obtained, this function is a no-op.
ds@1952
  3264
**
ds@1952
  3265
** Immediately after obtaining the shared lock (if required), this function
ds@1952
  3266
** checks for a hot-journal file. If one is found, an emergency rollback
ds@1952
  3267
** is performed immediately.
ds@1952
  3268
*/
ds@1952
  3269
static int pagerSharedLock(Pager *pPager){
ds@1952
  3270
  int rc = SQLITE_OK;
ds@1952
  3271
  int isHot = 0;
ds@1952
  3272
ds@1952
  3273
  /* If this database is opened for exclusive access, has no outstanding 
ds@1952
  3274
  ** page references and is in an error-state, now is the chance to clear
ds@1952
  3275
  ** the error. Discard the contents of the pager-cache and treat any
ds@1952
  3276
  ** open journal file as a hot-journal.
ds@1952
  3277
  */
ds@1952
  3278
  if( !MEMDB && pPager->exclusiveMode && pPager->nRef==0 && pPager->errCode ){
ds@1952
  3279
    if( pPager->journalOpen ){
ds@1952
  3280
      isHot = 1;
ds@1952
  3281
    }
ds@1952
  3282
    pager_reset(pPager);
ds@1952
  3283
    pPager->errCode = SQLITE_OK;
ds@1952
  3284
  }
ds@1952
  3285
ds@1952
  3286
  /* If the pager is still in an error state, do not proceed. The error 
ds@1952
  3287
  ** state will be cleared at some point in the future when all page 
ds@1952
  3288
  ** references are dropped and the cache can be discarded.
ds@1952
  3289
  */
ds@1952
  3290
  if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
ds@1952
  3291
    return pPager->errCode;
ds@1952
  3292
  }
ds@1952
  3293
ds@1952
  3294
  if( pPager->state==PAGER_UNLOCK || isHot ){
ds@1952
  3295
    sqlite3_vfs *pVfs = pPager->pVfs;
ds@1952
  3296
    if( !MEMDB ){
ds@1952
  3297
      assert( pPager->nRef==0 );
ds@1952
  3298
      if( !pPager->noReadlock ){
ds@1952
  3299
        rc = pager_wait_on_lock(pPager, SHARED_LOCK);
ds@1952
  3300
        if( rc!=SQLITE_OK ){
ds@1952
  3301
          return pager_error(pPager, rc);
ds@1952
  3302
        }
ds@1952
  3303
        assert( pPager->state>=SHARED_LOCK );
ds@1952
  3304
      }
ds@1952
  3305
  
ds@1952
  3306
      /* If a journal file exists, and there is no RESERVED lock on the
ds@1952
  3307
      ** database file, then it either needs to be played back or deleted.
ds@1952
  3308
      */
ds@1952
  3309
      if( hasHotJournal(pPager) || isHot ){
ds@1952
  3310
        /* Get an EXCLUSIVE lock on the database file. At this point it is
ds@1952
  3311
        ** important that a RESERVED lock is not obtained on the way to the
ds@1952
  3312
        ** EXCLUSIVE lock. If it were, another process might open the
ds@1952
  3313
        ** database file, detect the RESERVED lock, and conclude that the
ds@1952
  3314
        ** database is safe to read while this process is still rolling it 
ds@1952
  3315
        ** back.
ds@1952
  3316
        ** 
ds@1952
  3317
        ** Because the intermediate RESERVED lock is not requested, the
ds@1952
  3318
        ** second process will get to this point in the code and fail to
ds@1952
  3319
        ** obtain it's own EXCLUSIVE lock on the database file.
ds@1952
  3320
        */
ds@1952
  3321
        if( pPager->state<EXCLUSIVE_LOCK ){
ds@1952
  3322
          rc = sqlite3OsLock(pPager->fd, EXCLUSIVE_LOCK);
ds@1952
  3323
          if( rc!=SQLITE_OK ){
ds@1952
  3324
            pager_unlock(pPager);
ds@1952
  3325
            return pager_error(pPager, rc);
ds@1952
  3326
          }
ds@1952
  3327
          pPager->state = PAGER_EXCLUSIVE;
ds@1952
  3328
        }
ds@1952
  3329
 
ds@1952
  3330
        /* Open the journal for reading only.  Return SQLITE_BUSY if
ds@1952
  3331
        ** we are unable to open the journal file. 
ds@1952
  3332
        **
ds@1952
  3333
        ** The journal file does not need to be locked itself.  The
ds@1952
  3334
        ** journal file is never open unless the main database file holds
ds@1952
  3335
        ** a write lock, so there is never any chance of two or more
ds@1952
  3336
        ** processes opening the journal at the same time.
ds@1952
  3337
        **
ds@1952
  3338
        ** Open the journal for read/write access. This is because in 
ds@1952
  3339
        ** exclusive-access mode the file descriptor will be kept open and
ds@1952
  3340
        ** possibly used for a transaction later on. On some systems, the
ds@1952
  3341
        ** OsTruncate() call used in exclusive-access mode also requires
ds@1952
  3342
        ** a read/write file handle.
ds@1952
  3343
        */
ds@1952
  3344
        if( !isHot ){
ds@1952
  3345
          rc = SQLITE_BUSY;
ds@1952
  3346
          if( sqlite3OsAccess(pVfs, pPager->zJournal, SQLITE_ACCESS_EXISTS) ){
ds@1952
  3347
            int fout = 0;
ds@1952
  3348
            int f = SQLITE_OPEN_READWRITE|SQLITE_OPEN_MAIN_JOURNAL;
ds@1952
  3349
            assert( !pPager->tempFile );
ds@1952
  3350
            rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, f, &fout);
ds@1952
  3351
            assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
ds@1952
  3352
            if( fout&SQLITE_OPEN_READONLY ){
ds@1952
  3353
              rc = SQLITE_BUSY;
ds@1952
  3354
              sqlite3OsClose(pPager->jfd);
ds@1952
  3355
            }
ds@1952
  3356
          }
ds@1952
  3357
        }
ds@1952
  3358
        if( rc!=SQLITE_OK ){
ds@1952
  3359
          pager_unlock(pPager);
ds@1952
  3360
          return ((rc==SQLITE_NOMEM||rc==SQLITE_IOERR_NOMEM)?rc:SQLITE_BUSY);
ds@1952
  3361
        }
ds@1952
  3362
        pPager->journalOpen = 1;
ds@1952
  3363
        pPager->journalStarted = 0;
ds@1952
  3364
        pPager->journalOff = 0;
ds@1952
  3365
        pPager->setMaster = 0;
ds@1952
  3366
        pPager->journalHdr = 0;
ds@1952
  3367
 
ds@1952
  3368
        /* Playback and delete the journal.  Drop the database write
ds@1952
  3369
        ** lock and reacquire the read lock.
ds@1952
  3370
        */
ds@1952
  3371
        rc = pager_playback(pPager, 1);
ds@1952
  3372
        if( rc!=SQLITE_OK ){
ds@1952
  3373
          return pager_error(pPager, rc);
ds@1952
  3374
        }
ds@1952
  3375
        assert(pPager->state==PAGER_SHARED || 
ds@1952
  3376
            (pPager->exclusiveMode && pPager->state>PAGER_SHARED)
ds@1952
  3377
        );
ds@1952
  3378
      }
ds@1952
  3379
ds@1952
  3380
      if( pPager->pAll ){
ds@1952
  3381
        /* The shared-lock has just been acquired on the database file
ds@1952
  3382
        ** and there are already pages in the cache (from a previous
ds@1952
  3383
        ** read or write transaction).  Check to see if the database
ds@1952
  3384
        ** has been modified.  If the database has changed, flush the
ds@1952
  3385
        ** cache.
ds@1952
  3386
        **
ds@1952
  3387
        ** Database changes is detected by looking at 15 bytes beginning
ds@1952
  3388
        ** at offset 24 into the file.  The first 4 of these 16 bytes are
ds@1952
  3389
        ** a 32-bit counter that is incremented with each change.  The
ds@1952
  3390
        ** other bytes change randomly with each file change when
ds@1952
  3391
        ** a codec is in use.
ds@1952
  3392
        ** 
ds@1952
  3393
        ** There is a vanishingly small chance that a change will not be 
ds@1952
  3394
        ** detected.  The chance of an undetected change is so small that
ds@1952
  3395
        ** it can be neglected.
ds@1952
  3396
        */
ds@1952
  3397
        char dbFileVers[sizeof(pPager->dbFileVers)];
ds@1952
  3398
        sqlite3PagerPagecount(pPager);
ds@1952
  3399
ds@1952
  3400
        if( pPager->errCode ){
ds@1952
  3401
          return pPager->errCode;
ds@1952
  3402
        }
ds@1952
  3403
ds@1952
  3404
        if( pPager->dbSize>0 ){
ds@1952
  3405
          IOTRACE(("CKVERS %p %d\n", pPager, sizeof(dbFileVers)));
ds@1952
  3406
          rc = sqlite3OsRead(pPager->fd, &dbFileVers, sizeof(dbFileVers), 24);
ds@1952
  3407
          if( rc!=SQLITE_OK ){
ds@1952
  3408
            return rc;
ds@1952
  3409
          }
ds@1952
  3410
        }else{
ds@1952
  3411
          memset(dbFileVers, 0, sizeof(dbFileVers));
ds@1952
  3412
        }
ds@1952
  3413
ds@1952
  3414
        if( memcmp(pPager->dbFileVers, dbFileVers, sizeof(dbFileVers))!=0 ){
ds@1952
  3415
          pager_reset(pPager);
ds@1952
  3416
        }
ds@1952
  3417
      }
ds@1952
  3418
    }
ds@1952
  3419
    assert( pPager->exclusiveMode || pPager->state<=PAGER_SHARED );
ds@1952
  3420
    if( pPager->state==PAGER_UNLOCK ){
ds@1952
  3421
      pPager->state = PAGER_SHARED;
ds@1952
  3422
    }
ds@1952
  3423
  }
ds@1952
  3424
ds@1952
  3425
  return rc;
ds@1952
  3426
}
ds@1952
  3427
ds@1952
  3428
/*
ds@1952
  3429
** Allocate a PgHdr object.   Either create a new one or reuse
ds@1952
  3430
** an existing one that is not otherwise in use.
ds@1952
  3431
**
ds@1952
  3432
** A new PgHdr structure is created if any of the following are
ds@1952
  3433
** true:
ds@1952
  3434
**
ds@1952
  3435
**     (1)  We have not exceeded our maximum allocated cache size
ds@1952
  3436
**          as set by the "PRAGMA cache_size" command.
ds@1952
  3437
**
ds@1952
  3438
**     (2)  There are no unused PgHdr objects available at this time.
ds@1952
  3439
**
ds@1952
  3440
**     (3)  This is an in-memory database.
ds@1952
  3441
**
ds@1952
  3442
**     (4)  There are no PgHdr objects that do not require a journal
ds@1952
  3443
**          file sync and a sync of the journal file is currently
ds@1952
  3444
**          prohibited.
ds@1952
  3445
**
ds@1952
  3446
** Otherwise, reuse an existing PgHdr.  In other words, reuse an
ds@1952
  3447
** existing PgHdr if all of the following are true:
ds@1952
  3448
**
ds@1952
  3449
**     (1)  We have reached or exceeded the maximum cache size
ds@1952
  3450
**          allowed by "PRAGMA cache_size".
ds@1952
  3451
**
ds@1952
  3452
**     (2)  There is a PgHdr available with PgHdr->nRef==0
ds@1952
  3453
**
ds@1952
  3454
**     (3)  We are not in an in-memory database
ds@1952
  3455
**
ds@1952
  3456
**     (4)  Either there is an available PgHdr that does not need
ds@1952
  3457
**          to be synced to disk or else disk syncing is currently
ds@1952
  3458
**          allowed.
ds@1952
  3459
*/
ds@1952
  3460
static int pagerAllocatePage(Pager *pPager, PgHdr **ppPg){
ds@1952
  3461
  int rc = SQLITE_OK;
ds@1952
  3462
  PgHdr *pPg;
ds@1952
  3463
  void *pData;
ds@1952
  3464
ds@1952
  3465
  /* Create a new PgHdr if any of the four conditions defined 
ds@1952
  3466
  ** above are met: */
ds@1952
  3467
  if( pPager->nPage<pPager->mxPage
ds@1952
  3468
   || pPager->lru.pFirst==0 
ds@1952
  3469
   || MEMDB
ds@1952
  3470
   || (pPager->lru.pFirstSynced==0 && pPager->doNotSync)
ds@1952
  3471
  ){
ds@1952
  3472
    if( pPager->nPage>=pPager->nHash ){
ds@1952
  3473
      pager_resize_hash_table(pPager,
ds@1952
  3474
         pPager->nHash<256 ? 256 : pPager->nHash*2);
ds@1952
  3475
      if( pPager->nHash==0 ){
ds@1952
  3476
        rc = SQLITE_NOMEM;
ds@1952
  3477
        goto pager_allocate_out;
ds@1952
  3478
      }
ds@1952
  3479
    }
ds@1952
  3480
    pagerLeave(pPager);
ds@1952
  3481
    pPg = sqlite3_malloc( sizeof(*pPg) + sizeof(u32) + pPager->nExtra
ds@1952
  3482
                            + MEMDB*sizeof(PgHistory) );
ds@1952
  3483
    if( pPg ){
ds@1952
  3484
      pData = sqlite3_malloc( pPager->pageSize );
ds@1952
  3485
      if( pData==0 ){
ds@1952
  3486
        sqlite3_free(pPg);
ds@1952
  3487
        pPg = 0;
ds@1952
  3488
      }
ds@1952
  3489
    }
ds@1952
  3490
    pagerEnter(pPager);
ds@1952
  3491
    if( pPg==0 ){
ds@1952
  3492
      rc = SQLITE_NOMEM;
ds@1952
  3493
      goto pager_allocate_out;
ds@1952
  3494
    }
ds@1952
  3495
    memset(pPg, 0, sizeof(*pPg));
ds@1952
  3496
    if( MEMDB ){
ds@1952
  3497
      memset(PGHDR_TO_HIST(pPg, pPager), 0, sizeof(PgHistory));
ds@1952
  3498
    }
ds@1952
  3499
    pPg->pData = pData;
ds@1952
  3500
    pPg->pPager = pPager;
ds@1952
  3501
    pPg->pNextAll = pPager->pAll;
ds@1952
  3502
    pPager->pAll = pPg;
ds@1952
  3503
    pPager->nPage++;
ds@1952
  3504
  }else{
ds@1952
  3505
    /* Recycle an existing page with a zero ref-count. */
ds@1952
  3506
    rc = pager_recycle(pPager, &pPg);
ds@1952
  3507
    if( rc==SQLITE_BUSY ){
ds@1952
  3508
      rc = SQLITE_IOERR_BLOCKED;
ds@1952
  3509
    }
ds@1952
  3510
    if( rc!=SQLITE_OK ){
ds@1952
  3511
      goto pager_allocate_out;
ds@1952
  3512
    }
ds@1952
  3513
    assert( pPager->state>=SHARED_LOCK );
ds@1952
  3514
    assert(pPg);
ds@1952
  3515
  }
ds@1952
  3516
  *ppPg = pPg;
ds@1952
  3517
ds@1952
  3518
pager_allocate_out:
ds@1952
  3519
  return rc;
ds@1952
  3520
}
ds@1952
  3521
ds@1952
  3522
/*
ds@1952
  3523
** Make sure we have the content for a page.  If the page was
ds@1952
  3524
** previously acquired with noContent==1, then the content was
ds@1952
  3525
** just initialized to zeros instead of being read from disk.
ds@1952
  3526
** But now we need the real data off of disk.  So make sure we
ds@1952
  3527
** have it.  Read it in if we do not have it already.
ds@1952
  3528
*/
ds@1952
  3529
static int pager_get_content(PgHdr *pPg){
ds@1952
  3530
  if( pPg->needRead ){
ds@1952
  3531
    int rc = readDbPage(pPg->pPager, pPg, pPg->pgno);
ds@1952
  3532
    if( rc==SQLITE_OK ){
ds@1952
  3533
      pPg->needRead = 0;
ds@1952
  3534
    }else{
ds@1952
  3535
      return rc;
ds@1952
  3536
    }
ds@1952
  3537
  }
ds@1952
  3538
  return SQLITE_OK;
ds@1952
  3539
}
ds@1952
  3540
ds@1952
  3541
/*
ds@1952
  3542
** Acquire a page.
ds@1952
  3543
**
ds@1952
  3544
** A read lock on the disk file is obtained when the first page is acquired. 
ds@1952
  3545
** This read lock is dropped when the last page is released.
ds@1952
  3546
**
ds@1952
  3547
** This routine works for any page number greater than 0.  If the database
ds@1952
  3548
** file is smaller than the requested page, then no actual disk
ds@1952
  3549
** read occurs and the memory image of the page is initialized to
ds@1952
  3550
** all zeros.  The extra data appended to a page is always initialized
ds@1952
  3551
** to zeros the first time a page is loaded into memory.
ds@1952
  3552
**
ds@1952
  3553
** The acquisition might fail for several reasons.  In all cases,
ds@1952
  3554
** an appropriate error code is returned and *ppPage is set to NULL.
ds@1952
  3555
**
ds@1952
  3556
** See also sqlite3PagerLookup().  Both this routine and Lookup() attempt
ds@1952
  3557
** to find a page in the in-memory cache first.  If the page is not already
ds@1952
  3558
** in memory, this routine goes to disk to read it in whereas Lookup()
ds@1952
  3559
** just returns 0.  This routine acquires a read-lock the first time it
ds@1952
  3560
** has to go to disk, and could also playback an old journal if necessary.
ds@1952
  3561
** Since Lookup() never goes to disk, it never has to deal with locks
ds@1952
  3562
** or journal files.
ds@1952
  3563
**
ds@1952
  3564
** If noContent is false, the page contents are actually read from disk.
ds@1952
  3565
** If noContent is true, it means that we do not care about the contents
ds@1952
  3566
** of the page at this time, so do not do a disk read.  Just fill in the
ds@1952
  3567
** page content with zeros.  But mark the fact that we have not read the
ds@1952
  3568
** content by setting the PgHdr.needRead flag.  Later on, if 
ds@1952
  3569
** sqlite3PagerWrite() is called on this page or if this routine is
ds@1952
  3570
** called again with noContent==0, that means that the content is needed
ds@1952
  3571
** and the disk read should occur at that point.
ds@1952
  3572
*/
ds@1952
  3573
static int pagerAcquire(
ds@1952
  3574
  Pager *pPager,      /* The pager open on the database file */
ds@1952
  3575
  Pgno pgno,          /* Page number to fetch */
ds@1952
  3576
  DbPage **ppPage,    /* Write a pointer to the page here */
ds@1952
  3577
  int noContent       /* Do not bother reading content from disk if true */
ds@1952
  3578
){
ds@1952
  3579
  PgHdr *pPg;
ds@1952
  3580
  int rc;
ds@1952
  3581
ds@1952
  3582
  assert( pPager->state==PAGER_UNLOCK || pPager->nRef>0 || pgno==1 );
ds@1952
  3583
ds@1952
  3584
  /* The maximum page number is 2^31. Return SQLITE_CORRUPT if a page
ds@1952
  3585
  ** number greater than this, or zero, is requested.
ds@1952
  3586
  */
ds@1952
  3587
  if( pgno>PAGER_MAX_PGNO || pgno==0 || pgno==PAGER_MJ_PGNO(pPager) ){
ds@1952
  3588
    return SQLITE_CORRUPT_BKPT;
ds@1952
  3589
  }
ds@1952
  3590
ds@1952
  3591
  /* Make sure we have not hit any critical errors.
ds@1952
  3592
  */ 
ds@1952
  3593
  assert( pPager!=0 );
ds@1952
  3594
  *ppPage = 0;
ds@1952
  3595
ds@1952
  3596
  /* If this is the first page accessed, then get a SHARED lock
ds@1952
  3597
  ** on the database file. pagerSharedLock() is a no-op if 
ds@1952
  3598
  ** a database lock is already held.
ds@1952
  3599
  */
ds@1952
  3600
  rc = pagerSharedLock(pPager);
ds@1952
  3601
  if( rc!=SQLITE_OK ){
ds@1952
  3602
    return rc;
ds@1952
  3603
  }
ds@1952
  3604
  assert( pPager->state!=PAGER_UNLOCK );
ds@1952
  3605
ds@1952
  3606
  pPg = pager_lookup(pPager, pgno);
ds@1952
  3607
  if( pPg==0 ){
ds@1952
  3608
    /* The requested page is not in the page cache. */
ds@1952
  3609
    int nMax;
ds@1952
  3610
    int h;
ds@1952
  3611
    PAGER_INCR(pPager->nMiss);
ds@1952
  3612
    rc = pagerAllocatePage(pPager, &pPg);
ds@1952
  3613
    if( rc!=SQLITE_OK ){
ds@1952
  3614
      return rc;
ds@1952
  3615
    }
ds@1952
  3616
ds@1952
  3617
    pPg->pgno = pgno;
ds@1952
  3618
    assert( !MEMDB || pgno>pPager->stmtSize );
ds@1952
  3619
    if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){
ds@1952
  3620
#if 0
ds@1952
  3621
      sqlite3CheckMemory(pPager->aInJournal, pgno/8);
ds@1952
  3622
#endif
ds@1952
  3623
      assert( pPager->journalOpen );
ds@1952
  3624
      pPg->inJournal = (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0;
ds@1952
  3625
      pPg->needSync = 0;
ds@1952
  3626
    }else{
ds@1952
  3627
      pPg->inJournal = 0;
ds@1952
  3628
      pPg->needSync = 0;
ds@1952
  3629
    }
ds@1952
  3630
ds@1952
  3631
    makeClean(pPg);
ds@1952
  3632
    pPg->nRef = 1;
ds@1952
  3633
    REFINFO(pPg);
ds@1952
  3634
ds@1952
  3635
    pPager->nRef++;
ds@1952
  3636
    if( pPager->nExtra>0 ){
ds@1952
  3637
      memset(PGHDR_TO_EXTRA(pPg, pPager), 0, pPager->nExtra);
ds@1952
  3638
    }
ds@1952
  3639
    nMax = sqlite3PagerPagecount(pPager);
ds@1952
  3640
    if( pPager->errCode ){
ds@1952
  3641
      rc = pPager->errCode;
ds@1952
  3642
      sqlite3PagerUnref(pPg);
ds@1952
  3643
      return rc;
ds@1952
  3644
    }
ds@1952
  3645
ds@1952
  3646
    /* Populate the page with data, either by reading from the database
ds@1952
  3647
    ** file, or by setting the entire page to zero.
ds@1952
  3648
    */
ds@1952
  3649
    if( nMax<(int)pgno || MEMDB || (noContent && !pPager->alwaysRollback) ){
ds@1952
  3650
      if( pgno>pPager->mxPgno ){
ds@1952
  3651
        sqlite3PagerUnref(pPg);
ds@1952
  3652
        return SQLITE_FULL;
ds@1952
  3653
      }
ds@1952
  3654
      memset(PGHDR_TO_DATA(pPg), 0, pPager->pageSize);
ds@1952
  3655
      pPg->needRead = noContent && !pPager->alwaysRollback;
ds@1952
  3656
      IOTRACE(("ZERO %p %d\n", pPager, pgno));
ds@1952
  3657
    }else{
ds@1952
  3658
      rc = readDbPage(pPager, pPg, pgno);
ds@1952
  3659
      if( rc!=SQLITE_OK && rc!=SQLITE_IOERR_SHORT_READ ){
ds@1952
  3660
        pPg->pgno = 0;
ds@1952
  3661
        sqlite3PagerUnref(pPg);
ds@1952
  3662
        return rc;
ds@1952
  3663
      }
ds@1952
  3664
      pPg->needRead = 0;
ds@1952
  3665
    }
ds@1952
  3666
ds@1952
  3667
    /* Link the page into the page hash table */
ds@1952
  3668
    h = pgno & (pPager->nHash-1);
ds@1952
  3669
    assert( pgno!=0 );
ds@1952
  3670
    pPg->pNextHash = pPager->aHash[h];
ds@1952
  3671
    pPager->aHash[h] = pPg;
ds@1952
  3672
    if( pPg->pNextHash ){
ds@1952
  3673
      assert( pPg->pNextHash->pPrevHash==0 );
ds@1952
  3674
      pPg->pNextHash->pPrevHash = pPg;
ds@1952
  3675
    }
ds@1952
  3676
ds@1952
  3677
#ifdef SQLITE_CHECK_PAGES
ds@1952
  3678
    pPg->pageHash = pager_pagehash(pPg);
ds@1952
  3679
#endif
ds@1952
  3680
  }else{
ds@1952
  3681
    /* The requested page is in the page cache. */
ds@1952
  3682
    assert(pPager->nRef>0 || pgno==1);
ds@1952
  3683
    PAGER_INCR(pPager->nHit);
ds@1952
  3684
    if( !noContent ){
ds@1952
  3685
      rc = pager_get_content(pPg);
ds@1952
  3686
      if( rc ){
ds@1952
  3687
        return rc;
ds@1952
  3688
      }
ds@1952
  3689
    }
ds@1952
  3690
    page_ref(pPg);
ds@1952
  3691
  }
ds@1952
  3692
  *ppPage = pPg;
ds@1952
  3693
  return SQLITE_OK;
ds@1952
  3694
}
ds@1952
  3695
int sqlite3PagerAcquire(
ds@1952
  3696
  Pager *pPager,      /* The pager open on the database file */
ds@1952
  3697
  Pgno pgno,          /* Page number to fetch */
ds@1952
  3698
  DbPage **ppPage,    /* Write a pointer to the page here */
ds@1952
  3699
  int noContent       /* Do not bother reading content from disk if true */
ds@1952
  3700
){
ds@1952
  3701
  int rc;
ds@1952
  3702
  pagerEnter(pPager);
ds@1952
  3703
  rc = pagerAcquire(pPager, pgno, ppPage, noContent);
ds@1952
  3704
  pagerLeave(pPager);
ds@1952
  3705
  return rc;
ds@1952
  3706
}
ds@1952
  3707
ds@1952
  3708
ds@1952
  3709
/*
ds@1952
  3710
** Acquire a page if it is already in the in-memory cache.  Do
ds@1952
  3711
** not read the page from disk.  Return a pointer to the page,
ds@1952
  3712
** or 0 if the page is not in cache.
ds@1952
  3713
**
ds@1952
  3714
** See also sqlite3PagerGet().  The difference between this routine
ds@1952
  3715
** and sqlite3PagerGet() is that _get() will go to the disk and read
ds@1952
  3716
** in the page if the page is not already in cache.  This routine
ds@1952
  3717
** returns NULL if the page is not in cache or if a disk I/O error 
ds@1952
  3718
** has ever happened.
ds@1952
  3719
*/
ds@1952
  3720
DbPage *sqlite3PagerLookup(Pager *pPager, Pgno pgno){
ds@1952
  3721
  PgHdr *pPg = 0;
ds@1952
  3722
ds@1952
  3723
  assert( pPager!=0 );
ds@1952
  3724
  assert( pgno!=0 );
ds@1952
  3725
ds@1952
  3726
  pagerEnter(pPager);
ds@1952
  3727
  if( pPager->state==PAGER_UNLOCK ){
ds@1952
  3728
    assert( !pPager->pAll || pPager->exclusiveMode );
ds@1952
  3729
  }else if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
ds@1952
  3730
    /* Do nothing */
ds@1952
  3731
  }else if( (pPg = pager_lookup(pPager, pgno))!=0 ){
ds@1952
  3732
    page_ref(pPg);
ds@1952
  3733
  }
ds@1952
  3734
  pagerLeave(pPager);
ds@1952
  3735
  return pPg;
ds@1952
  3736
}
ds@1952
  3737
ds@1952
  3738
/*
ds@1952
  3739
** Release a page.
ds@1952
  3740
**
ds@1952
  3741
** If the number of references to the page drop to zero, then the
ds@1952
  3742
** page is added to the LRU list.  When all references to all pages
ds@1952
  3743
** are released, a rollback occurs and the lock on the database is
ds@1952
  3744
** removed.
ds@1952
  3745
*/
ds@1952
  3746
int sqlite3PagerUnref(DbPage *pPg){
ds@1952
  3747
  Pager *pPager = pPg->pPager;
ds@1952
  3748
ds@1952
  3749
  /* Decrement the reference count for this page
ds@1952
  3750
  */
ds@1952
  3751
  assert( pPg->nRef>0 );
ds@1952
  3752
  pagerEnter(pPg->pPager);
ds@1952
  3753
  pPg->nRef--;
ds@1952
  3754
  REFINFO(pPg);
ds@1952
  3755
ds@1952
  3756
  CHECK_PAGE(pPg);
ds@1952
  3757
ds@1952
  3758
  /* When the number of references to a page reach 0, call the
ds@1952
  3759
  ** destructor and add the page to the freelist.
ds@1952
  3760
  */
ds@1952
  3761
  if( pPg->nRef==0 ){
ds@1952
  3762
ds@1952
  3763
    lruListAdd(pPg);
ds@1952
  3764
    if( pPager->xDestructor ){
ds@1952
  3765
      pPager->xDestructor(pPg, pPager->pageSize);
ds@1952
  3766
    }
ds@1952
  3767
  
ds@1952
  3768
    /* When all pages reach the freelist, drop the read lock from
ds@1952
  3769
    ** the database file.
ds@1952
  3770
    */
ds@1952
  3771
    pPager->nRef--;
ds@1952
  3772
    assert( pPager->nRef>=0 );
ds@1952
  3773
    if( pPager->nRef==0 && (!pPager->exclusiveMode || pPager->journalOff>0) ){
ds@1952
  3774
      pagerUnlockAndRollback(pPager);
ds@1952
  3775
    }
ds@1952
  3776
  }
ds@1952
  3777
  pagerLeave(pPager);
ds@1952
  3778
  return SQLITE_OK;
ds@1952
  3779
}
ds@1952
  3780
ds@1952
  3781
/*
ds@1952
  3782
** Create a journal file for pPager.  There should already be a RESERVED
ds@1952
  3783
** or EXCLUSIVE lock on the database file when this routine is called.
ds@1952
  3784
**
ds@1952
  3785
** Return SQLITE_OK if everything.  Return an error code and release the
ds@1952
  3786
** write lock if anything goes wrong.
ds@1952
  3787
*/
ds@1952
  3788
static int pager_open_journal(Pager *pPager){
ds@1952
  3789
  sqlite3_vfs *pVfs = pPager->pVfs;
ds@1952
  3790
  int flags = (SQLITE_OPEN_READWRITE|SQLITE_OPEN_EXCLUSIVE|SQLITE_OPEN_CREATE);
ds@1952
  3791
ds@1952
  3792
  int rc;
ds@1952
  3793
  assert( !MEMDB );
ds@1952
  3794
  assert( pPager->state>=PAGER_RESERVED );
ds@1952
  3795
  assert( pPager->journalOpen==0 );
ds@1952
  3796
  assert( pPager->useJournal );
ds@1952
  3797
  assert( pPager->aInJournal==0 );
ds@1952
  3798
  sqlite3PagerPagecount(pPager);
ds@1952
  3799
  pagerLeave(pPager);
ds@1952
  3800
  pPager->aInJournal = sqlite3MallocZero( pPager->dbSize/8 + 1 );
ds@1952
  3801
  pagerEnter(pPager);
ds@1952
  3802
  if( pPager->aInJournal==0 ){
ds@1952
  3803
    rc = SQLITE_NOMEM;
ds@1952
  3804
    goto failed_to_open_journal;
ds@1952
  3805
  }
ds@1952
  3806
ds@1952
  3807
  if( pPager->tempFile ){
ds@1952
  3808
    flags |= (SQLITE_OPEN_DELETEONCLOSE|SQLITE_OPEN_TEMP_JOURNAL);
ds@1952
  3809
  }else{
ds@1952
  3810
    flags |= (SQLITE_OPEN_MAIN_JOURNAL);
ds@1952
  3811
  }
ds@1952
  3812
#ifdef SQLITE_ENABLE_ATOMIC_WRITE
ds@1952
  3813
  rc = sqlite3JournalOpen(
ds@1952
  3814
      pVfs, pPager->zJournal, pPager->jfd, flags, jrnlBufferSize(pPager)
ds@1952
  3815
  );
ds@1952
  3816
#else
ds@1952
  3817
  rc = sqlite3OsOpen(pVfs, pPager->zJournal, pPager->jfd, flags, 0);
ds@1952
  3818
#endif
ds@1952
  3819
  assert( rc!=SQLITE_OK || pPager->jfd->pMethods );
ds@1952
  3820
  pPager->journalOff = 0;
ds@1952
  3821
  pPager->setMaster = 0;
ds@1952
  3822
  pPager->journalHdr = 0;
ds@1952
  3823
  if( rc!=SQLITE_OK ){
ds@1952
  3824
    if( rc==SQLITE_NOMEM ){
ds@1952
  3825
      sqlite3OsDelete(pVfs, pPager->zJournal, 0);
ds@1952
  3826
    }
ds@1952
  3827
    goto failed_to_open_journal;
ds@1952
  3828
  }
ds@1952
  3829
  pPager->journalOpen = 1;
ds@1952
  3830
  pPager->journalStarted = 0;
ds@1952
  3831
  pPager->needSync = 0;
ds@1952
  3832
  pPager->alwaysRollback = 0;
ds@1952
  3833
  pPager->nRec = 0;
ds@1952
  3834
  if( pPager->errCode ){
ds@1952
  3835
    rc = pPager->errCode;
ds@1952
  3836
    goto failed_to_open_journal;
ds@1952
  3837
  }
ds@1952
  3838
  pPager->origDbSize = pPager->dbSize;
ds@1952
  3839
ds@1952
  3840
  rc = writeJournalHdr(pPager);
ds@1952
  3841
ds@1952
  3842
  if( pPager->stmtAutoopen && rc==SQLITE_OK ){
ds@1952
  3843
    rc = sqlite3PagerStmtBegin(pPager);
ds@1952
  3844
  }
ds@1952
  3845
  if( rc!=SQLITE_OK && rc!=SQLITE_NOMEM && rc!=SQLITE_IOERR_NOMEM ){
ds@1952
  3846
    rc = pager_end_transaction(pPager);
ds@1952
  3847
    if( rc==SQLITE_OK ){
ds@1952
  3848
      rc = SQLITE_FULL;
ds@1952
  3849
    }
ds@1952
  3850
  }
ds@1952
  3851
  return rc;
ds@1952
  3852
ds@1952
  3853
failed_to_open_journal:
ds@1952
  3854
  sqlite3_free(pPager->aInJournal);
ds@1952
  3855
  pPager->aInJournal = 0;
ds@1952
  3856
  return rc;
ds@1952
  3857
}
ds@1952
  3858
ds@1952
  3859
/*
ds@1952
  3860
** Acquire a write-lock on the database.  The lock is removed when
ds@1952
  3861
** the any of the following happen:
ds@1952
  3862
**
ds@1952
  3863
**   *  sqlite3PagerCommitPhaseTwo() is called.
ds@1952
  3864
**   *  sqlite3PagerRollback() is called.
ds@1952
  3865
**   *  sqlite3PagerClose() is called.
ds@1952
  3866
**   *  sqlite3PagerUnref() is called to on every outstanding page.
ds@1952
  3867
**
ds@1952
  3868
** The first parameter to this routine is a pointer to any open page of the
ds@1952
  3869
** database file.  Nothing changes about the page - it is used merely to
ds@1952
  3870
** acquire a pointer to the Pager structure and as proof that there is
ds@1952
  3871
** already a read-lock on the database.
ds@1952
  3872
**
ds@1952
  3873
** The second parameter indicates how much space in bytes to reserve for a
ds@1952
  3874
** master journal file-name at the start of the journal when it is created.
ds@1952
  3875
**
ds@1952
  3876
** A journal file is opened if this is not a temporary file.  For temporary
ds@1952
  3877
** files, the opening of the journal file is deferred until there is an
ds@1952
  3878
** actual need to write to the journal.
ds@1952
  3879
**
ds@1952
  3880
** If the database is already reserved for writing, this routine is a no-op.
ds@1952
  3881
**
ds@1952
  3882
** If exFlag is true, go ahead and get an EXCLUSIVE lock on the file
ds@1952
  3883
** immediately instead of waiting until we try to flush the cache.  The
ds@1952
  3884
** exFlag is ignored if a transaction is already active.
ds@1952
  3885
*/
ds@1952
  3886
int sqlite3PagerBegin(DbPage *pPg, int exFlag){
ds@1952
  3887
  Pager *pPager = pPg->pPager;
ds@1952
  3888
  int rc = SQLITE_OK;
ds@1952
  3889
  pagerEnter(pPager);
ds@1952
  3890
  assert( pPg->nRef>0 );
ds@1952
  3891
  assert( pPager->state!=PAGER_UNLOCK );
ds@1952
  3892
  if( pPager->state==PAGER_SHARED ){
ds@1952
  3893
    assert( pPager->aInJournal==0 );
ds@1952
  3894
    if( MEMDB ){
ds@1952
  3895
      pPager->state = PAGER_EXCLUSIVE;
ds@1952
  3896
      pPager->origDbSize = pPager->dbSize;
ds@1952
  3897
    }else{
ds@1952
  3898
      rc = sqlite3OsLock(pPager->fd, RESERVED_LOCK);
ds@1952
  3899
      if( rc==SQLITE_OK ){
ds@1952
  3900
        pPager->state = PAGER_RESERVED;
ds@1952
  3901
        if( exFlag ){
ds@1952
  3902
          rc = pager_wait_on_lock(pPager, EXCLUSIVE_LOCK);
ds@1952
  3903
        }
ds@1952
  3904
      }
ds@1952
  3905
      if( rc!=SQLITE_OK ){
ds@1952
  3906
        pagerLeave(pPager);
ds@1952
  3907
        return rc;
ds@1952
  3908
      }
ds@1952
  3909
      pPager->dirtyCache = 0;
ds@1952
  3910
      PAGERTRACE2("TRANSACTION %d\n", PAGERID(pPager));
ds@1952
  3911
      if( pPager->useJournal && !pPager->tempFile ){
ds@1952
  3912
        rc = pager_open_journal(pPager);
ds@1952
  3913
      }
ds@1952
  3914
    }
ds@1952
  3915
  }else if( pPager->journalOpen && pPager->journalOff==0 ){
ds@1952
  3916
    /* This happens when the pager was in exclusive-access mode last
ds@1952
  3917
    ** time a (read or write) transaction was successfully concluded
ds@1952
  3918
    ** by this connection. Instead of deleting the journal file it was 
ds@1952
  3919
    ** kept open and truncated to 0 bytes.
ds@1952
  3920
    */
ds@1952
  3921
    assert( pPager->nRec==0 );
ds@1952
  3922
    assert( pPager->origDbSize==0 );
ds@1952
  3923
    assert( pPager->aInJournal==0 );
ds@1952
  3924
    sqlite3PagerPagecount(pPager);
ds@1952
  3925
    pagerLeave(pPager);
ds@1952
  3926
    pPager->aInJournal = sqlite3MallocZero( pPager->dbSize/8 + 1 );
ds@1952
  3927
    pagerEnter(pPager);
ds@1952
  3928
    if( !pPager->aInJournal ){
ds@1952
  3929
      rc = SQLITE_NOMEM;
ds@1952
  3930
    }else{
ds@1952
  3931
      pPager->origDbSize = pPager->dbSize;
ds@1952
  3932
      rc = writeJournalHdr(pPager);
ds@1952
  3933
    }
ds@1952
  3934
  }
ds@1952
  3935
  assert( !pPager->journalOpen || pPager->journalOff>0 || rc!=SQLITE_OK );
ds@1952
  3936
  pagerLeave(pPager);
ds@1952
  3937
  return rc;
ds@1952
  3938
}
ds@1952
  3939
ds@1952
  3940
/*
ds@1952
  3941
** Make a page dirty.  Set its dirty flag and add it to the dirty
ds@1952
  3942
** page list.
ds@1952
  3943
*/
ds@1952
  3944
static void makeDirty(PgHdr *pPg){
ds@1952
  3945
  if( pPg->dirty==0 ){
ds@1952
  3946
    Pager *pPager = pPg->pPager;
ds@1952
  3947
    pPg->dirty = 1;
ds@1952
  3948
    pPg->pDirty = pPager->pDirty;
ds@1952
  3949
    if( pPager->pDirty ){
ds@1952
  3950
      pPager->pDirty->pPrevDirty = pPg;
ds@1952
  3951
    }
ds@1952
  3952
    pPg->pPrevDirty = 0;
ds@1952
  3953
    pPager->pDirty = pPg;
ds@1952
  3954
  }
ds@1952
  3955
}
ds@1952
  3956
ds@1952
  3957
/*
ds@1952
  3958
** Make a page clean.  Clear its dirty bit and remove it from the
ds@1952
  3959
** dirty page list.
ds@1952
  3960
*/
ds@1952
  3961
static void makeClean(PgHdr *pPg){
ds@1952
  3962
  if( pPg->dirty ){
ds@1952
  3963
    pPg->dirty = 0;
ds@1952
  3964
    if( pPg->pDirty ){
ds@1952
  3965
      assert( pPg->pDirty->pPrevDirty==pPg );
ds@1952
  3966
      pPg->pDirty->pPrevDirty = pPg->pPrevDirty;
ds@1952
  3967
    }
ds@1952
  3968
    if( pPg->pPrevDirty ){
ds@1952
  3969
      assert( pPg->pPrevDirty->pDirty==pPg );
ds@1952
  3970
      pPg->pPrevDirty->pDirty = pPg->pDirty;
ds@1952
  3971
    }else{
ds@1952
  3972
      assert( pPg->pPager->pDirty==pPg );
ds@1952
  3973
      pPg->pPager->pDirty = pPg->pDirty;
ds@1952
  3974
    }
ds@1952
  3975
  }
ds@1952
  3976
}
ds@1952
  3977
ds@1952
  3978
ds@1952
  3979
/*
ds@1952
  3980
** Mark a data page as writeable.  The page is written into the journal 
ds@1952
  3981
** if it is not there already.  This routine must be called before making
ds@1952
  3982
** changes to a page.
ds@1952
  3983
**
ds@1952
  3984
** The first time this routine is called, the pager creates a new
ds@1952
  3985
** journal and acquires a RESERVED lock on the database.  If the RESERVED
ds@1952
  3986
** lock could not be acquired, this routine returns SQLITE_BUSY.  The
ds@1952
  3987
** calling routine must check for that return value and be careful not to
ds@1952
  3988
** change any page data until this routine returns SQLITE_OK.
ds@1952
  3989
**
ds@1952
  3990
** If the journal file could not be written because the disk is full,
ds@1952
  3991
** then this routine returns SQLITE_FULL and does an immediate rollback.
ds@1952
  3992
** All subsequent write attempts also return SQLITE_FULL until there
ds@1952
  3993
** is a call to sqlite3PagerCommit() or sqlite3PagerRollback() to
ds@1952
  3994
** reset.
ds@1952
  3995
*/
ds@1952
  3996
static int pager_write(PgHdr *pPg){
ds@1952
  3997
  void *pData = PGHDR_TO_DATA(pPg);
ds@1952
  3998
  Pager *pPager = pPg->pPager;
ds@1952
  3999
  int rc = SQLITE_OK;
ds@1952
  4000
ds@1952
  4001
  /* Check for errors
ds@1952
  4002
  */
ds@1952
  4003
  if( pPager->errCode ){ 
ds@1952
  4004
    return pPager->errCode;
ds@1952
  4005
  }
ds@1952
  4006
  if( pPager->readOnly ){
ds@1952
  4007
    return SQLITE_PERM;
ds@1952
  4008
  }
ds@1952
  4009
ds@1952
  4010
  assert( !pPager->setMaster );
ds@1952
  4011
ds@1952
  4012
  CHECK_PAGE(pPg);
ds@1952
  4013
ds@1952
  4014
  /* If this page was previously acquired with noContent==1, that means
ds@1952
  4015
  ** we didn't really read in the content of the page.  This can happen
ds@1952
  4016
  ** (for example) when the page is being moved to the freelist.  But
ds@1952
  4017
  ** now we are (perhaps) moving the page off of the freelist for
ds@1952
  4018
  ** reuse and we need to know its original content so that content
ds@1952
  4019
  ** can be stored in the rollback journal.  So do the read at this
ds@1952
  4020
  ** time.
ds@1952
  4021
  */
ds@1952
  4022
  rc = pager_get_content(pPg);
ds@1952
  4023
  if( rc ){
ds@1952
  4024
    return rc;
ds@1952
  4025
  }
ds@1952
  4026
ds@1952
  4027
  /* Mark the page as dirty.  If the page has already been written
ds@1952
  4028
  ** to the journal then we can return right away.
ds@1952
  4029
  */
ds@1952
  4030
  makeDirty(pPg);
ds@1952
  4031
  if( pPg->inJournal && (pageInStatement(pPg) || pPager->stmtInUse==0) ){
ds@1952
  4032
    pPager->dirtyCache = 1;
ds@1952
  4033
  }else{
ds@1952
  4034
ds@1952
  4035
    /* If we get this far, it means that the page needs to be
ds@1952
  4036
    ** written to the transaction journal or the ckeckpoint journal
ds@1952
  4037
    ** or both.
ds@1952
  4038
    **
ds@1952
  4039
    ** First check to see that the transaction journal exists and
ds@1952
  4040
    ** create it if it does not.
ds@1952
  4041
    */
ds@1952
  4042
    assert( pPager->state!=PAGER_UNLOCK );
ds@1952
  4043
    rc = sqlite3PagerBegin(pPg, 0);
ds@1952
  4044
    if( rc!=SQLITE_OK ){
ds@1952
  4045
      return rc;
ds@1952
  4046
    }
ds@1952
  4047
    assert( pPager->state>=PAGER_RESERVED );
ds@1952
  4048
    if( !pPager->journalOpen && pPager->useJournal ){
ds@1952
  4049
      rc = pager_open_journal(pPager);
ds@1952
  4050
      if( rc!=SQLITE_OK ) return rc;
ds@1952
  4051
    }
ds@1952
  4052
    assert( pPager->journalOpen || !pPager->useJournal );
ds@1952
  4053
    pPager->dirtyCache = 1;
ds@1952
  4054
  
ds@1952
  4055
    /* The transaction journal now exists and we have a RESERVED or an
ds@1952
  4056
    ** EXCLUSIVE lock on the main database file.  Write the current page to
ds@1952
  4057
    ** the transaction journal if it is not there already.
ds@1952
  4058
    */
ds@1952
  4059
    if( !pPg->inJournal && (pPager->useJournal || MEMDB) ){
ds@1952
  4060
      if( (int)pPg->pgno <= pPager->origDbSize ){
ds@1952
  4061
        if( MEMDB ){
ds@1952
  4062
          PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
ds@1952
  4063
          PAGERTRACE3("JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
ds@1952
  4064
          assert( pHist->pOrig==0 );
ds@1952
  4065
          pHist->pOrig = sqlite3_malloc( pPager->pageSize );
ds@1952
  4066
          if( pHist->pOrig ){
ds@1952
  4067
            memcpy(pHist->pOrig, PGHDR_TO_DATA(pPg), pPager->pageSize);
ds@1952
  4068
          }
ds@1952
  4069
        }else{
ds@1952
  4070
          u32 cksum;
ds@1952
  4071
          char *pData2;
ds@1952
  4072
ds@1952
  4073
          /* We should never write to the journal file the page that
ds@1952
  4074
          ** contains the database locks.  The following assert verifies
ds@1952
  4075
          ** that we do not. */
ds@1952
  4076
          assert( pPg->pgno!=PAGER_MJ_PGNO(pPager) );
ds@1952
  4077
          pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
ds@1952
  4078
          cksum = pager_cksum(pPager, (u8*)pData2);
ds@1952
  4079
          rc = write32bits(pPager->jfd, pPager->journalOff, pPg->pgno);
ds@1952
  4080
          if( rc==SQLITE_OK ){
ds@1952
  4081
            rc = sqlite3OsWrite(pPager->jfd, pData2, pPager->pageSize,
ds@1952
  4082
                                pPager->journalOff + 4);
ds@1952
  4083
            pPager->journalOff += pPager->pageSize+4;
ds@1952
  4084
          }
ds@1952
  4085
          if( rc==SQLITE_OK ){
ds@1952
  4086
            rc = write32bits(pPager->jfd, pPager->journalOff, cksum);
ds@1952
  4087
            pPager->journalOff += 4;
ds@1952
  4088
          }
ds@1952
  4089
          IOTRACE(("JOUT %p %d %lld %d\n", pPager, pPg->pgno, 
ds@1952
  4090
                   pPager->journalOff, pPager->pageSize));
ds@1952
  4091
          PAGER_INCR(sqlite3_pager_writej_count);
ds@1952
  4092
          PAGERTRACE5("JOURNAL %d page %d needSync=%d hash(%08x)\n",
ds@1952
  4093
               PAGERID(pPager), pPg->pgno, pPg->needSync, pager_pagehash(pPg));
ds@1952
  4094
ds@1952
  4095
          /* An error has occured writing to the journal file. The 
ds@1952
  4096
          ** transaction will be rolled back by the layer above.
ds@1952
  4097
          */
ds@1952
  4098
          if( rc!=SQLITE_OK ){
ds@1952
  4099
            return rc;
ds@1952
  4100
          }
ds@1952
  4101
ds@1952
  4102
          pPager->nRec++;
ds@1952
  4103
          assert( pPager->aInJournal!=0 );
ds@1952
  4104
          pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
ds@1952
  4105
          pPg->needSync = !pPager->noSync;
ds@1952
  4106
          if( pPager->stmtInUse ){
ds@1952
  4107
            pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
ds@1952
  4108
          }
ds@1952
  4109
        }
ds@1952
  4110
      }else{
ds@1952
  4111
        pPg->needSync = !pPager->journalStarted && !pPager->noSync;
ds@1952
  4112
        PAGERTRACE4("APPEND %d page %d needSync=%d\n",
ds@1952
  4113
                PAGERID(pPager), pPg->pgno, pPg->needSync);
ds@1952
  4114
      }
ds@1952
  4115
      if( pPg->needSync ){
ds@1952
  4116
        pPager->needSync = 1;
ds@1952
  4117
      }
ds@1952
  4118
      pPg->inJournal = 1;
ds@1952
  4119
    }
ds@1952
  4120
  
ds@1952
  4121
    /* If the statement journal is open and the page is not in it,
ds@1952
  4122
    ** then write the current page to the statement journal.  Note that
ds@1952
  4123
    ** the statement journal format differs from the standard journal format
ds@1952
  4124
    ** in that it omits the checksums and the header.
ds@1952
  4125
    */
ds@1952
  4126
    if( pPager->stmtInUse 
ds@1952
  4127
     && !pageInStatement(pPg) 
ds@1952
  4128
     && (int)pPg->pgno<=pPager->stmtSize 
ds@1952
  4129
    ){
ds@1952
  4130
      assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
ds@1952
  4131
      if( MEMDB ){
ds@1952
  4132
        PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
ds@1952
  4133
        assert( pHist->pStmt==0 );
ds@1952
  4134
        pHist->pStmt = sqlite3_malloc( pPager->pageSize );
ds@1952
  4135
        if( pHist->pStmt ){
ds@1952
  4136
          memcpy(pHist->pStmt, PGHDR_TO_DATA(pPg), pPager->pageSize);
ds@1952
  4137
        }
ds@1952
  4138
        PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
ds@1952
  4139
        page_add_to_stmt_list(pPg);
ds@1952
  4140
      }else{
ds@1952
  4141
        i64 offset = pPager->stmtNRec*(4+pPager->pageSize);
ds@1952
  4142
        char *pData2 = CODEC2(pPager, pData, pPg->pgno, 7);
ds@1952
  4143
        rc = write32bits(pPager->stfd, offset, pPg->pgno);
ds@1952
  4144
        if( rc==SQLITE_OK ){
ds@1952
  4145
          rc = sqlite3OsWrite(pPager->stfd, pData2, pPager->pageSize, offset+4);
ds@1952
  4146
        }
ds@1952
  4147
        PAGERTRACE3("STMT-JOURNAL %d page %d\n", PAGERID(pPager), pPg->pgno);
ds@1952
  4148
        if( rc!=SQLITE_OK ){
ds@1952
  4149
          return rc;
ds@1952
  4150
        }
ds@1952
  4151
        pPager->stmtNRec++;
ds@1952
  4152
        assert( pPager->aInStmt!=0 );
ds@1952
  4153
        pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
ds@1952
  4154
      }
ds@1952
  4155
    }
ds@1952
  4156
  }
ds@1952
  4157
ds@1952
  4158
  /* Update the database size and return.
ds@1952
  4159
  */
ds@1952
  4160
  assert( pPager->state>=PAGER_SHARED );
ds@1952
  4161
  if( pPager->dbSize<(int)pPg->pgno ){
ds@1952
  4162
    pPager->dbSize = pPg->pgno;
ds@1952
  4163
    if( !MEMDB && pPager->dbSize==PENDING_BYTE/pPager->pageSize ){
ds@1952
  4164
      pPager->dbSize++;
ds@1952
  4165
    }
ds@1952
  4166
  }
ds@1952
  4167
  return rc;
ds@1952
  4168
}
ds@1952
  4169
ds@1952
  4170
/*
ds@1952
  4171
** This function is used to mark a data-page as writable. It uses 
ds@1952
  4172
** pager_write() to open a journal file (if it is not already open)
ds@1952
  4173
** and write the page *pData to the journal.
ds@1952
  4174
**
ds@1952
  4175
** The difference between this function and pager_write() is that this
ds@1952
  4176
** function also deals with the special case where 2 or more pages
ds@1952
  4177
** fit on a single disk sector. In this case all co-resident pages
ds@1952
  4178
** must have been written to the journal file before returning.
ds@1952
  4179
*/
ds@1952
  4180
int sqlite3PagerWrite(DbPage *pDbPage){
ds@1952
  4181
  int rc = SQLITE_OK;
ds@1952
  4182
ds@1952
  4183
  PgHdr *pPg = pDbPage;
ds@1952
  4184
  Pager *pPager = pPg->pPager;
ds@1952
  4185
  Pgno nPagePerSector = (pPager->sectorSize/pPager->pageSize);
ds@1952
  4186
ds@1952
  4187
  pagerEnter(pPager);
ds@1952
  4188
  if( !MEMDB && nPagePerSector>1 ){
ds@1952
  4189
    Pgno nPageCount;          /* Total number of pages in database file */
ds@1952
  4190
    Pgno pg1;                 /* First page of the sector pPg is located on. */
ds@1952
  4191
    int nPage;                /* Number of pages starting at pg1 to journal */
ds@1952
  4192
    int ii;
ds@1952
  4193
    int needSync = 0;
ds@1952
  4194
ds@1952
  4195
    /* Set the doNotSync flag to 1. This is because we cannot allow a journal
ds@1952
  4196
    ** header to be written between the pages journaled by this function.
ds@1952
  4197
    */
ds@1952
  4198
    assert( pPager->doNotSync==0 );
ds@1952
  4199
    pPager->doNotSync = 1;
ds@1952
  4200
ds@1952
  4201
    /* This trick assumes that both the page-size and sector-size are
ds@1952
  4202
    ** an integer power of 2. It sets variable pg1 to the identifier
ds@1952
  4203
    ** of the first page of the sector pPg is located on.
ds@1952
  4204
    */
ds@1952
  4205
    pg1 = ((pPg->pgno-1) & ~(nPagePerSector-1)) + 1;
ds@1952
  4206
ds@1952
  4207
    nPageCount = sqlite3PagerPagecount(pPager);
ds@1952
  4208
    if( pPg->pgno>nPageCount ){
ds@1952
  4209
      nPage = (pPg->pgno - pg1)+1;
ds@1952
  4210
    }else if( (pg1+nPagePerSector-1)>nPageCount ){
ds@1952
  4211
      nPage = nPageCount+1-pg1;
ds@1952
  4212
    }else{
ds@1952
  4213
      nPage = nPagePerSector;
ds@1952
  4214
    }
ds@1952
  4215
    assert(nPage>0);
ds@1952
  4216
    assert(pg1<=pPg->pgno);
ds@1952
  4217
    assert((pg1+nPage)>pPg->pgno);
ds@1952
  4218
ds@1952
  4219
    for(ii=0; ii<nPage && rc==SQLITE_OK; ii++){
ds@1952
  4220
      Pgno pg = pg1+ii;
ds@1952
  4221
      PgHdr *pPage;
ds@1952
  4222
      if( !pPager->aInJournal || pg==pPg->pgno || 
ds@1952
  4223
          pg>pPager->origDbSize || !(pPager->aInJournal[pg/8]&(1<<(pg&7)))
ds@1952
  4224
      ) {
ds@1952
  4225
        if( pg!=PAGER_MJ_PGNO(pPager) ){
ds@1952
  4226
          rc = sqlite3PagerGet(pPager, pg, &pPage);
ds@1952
  4227
          if( rc==SQLITE_OK ){
ds@1952
  4228
            rc = pager_write(pPage);
ds@1952
  4229
            if( pPage->needSync ){
ds@1952
  4230
              needSync = 1;
ds@1952
  4231
            }
ds@1952
  4232
            sqlite3PagerUnref(pPage);
ds@1952
  4233
          }
ds@1952
  4234
        }
ds@1952
  4235
      }else if( (pPage = pager_lookup(pPager, pg)) ){
ds@1952
  4236
        if( pPage->needSync ){
ds@1952
  4237
          needSync = 1;
ds@1952
  4238
        }
ds@1952
  4239
      }
ds@1952
  4240
    }
ds@1952
  4241
ds@1952
  4242
    /* If the PgHdr.needSync flag is set for any of the nPage pages 
ds@1952
  4243
    ** starting at pg1, then it needs to be set for all of them. Because
ds@1952
  4244
    ** writing to any of these nPage pages may damage the others, the
ds@1952
  4245
    ** journal file must contain sync()ed copies of all of them
ds@1952
  4246
    ** before any of them can be written out to the database file.
ds@1952
  4247
    */
ds@1952
  4248
    if( needSync ){
ds@1952
  4249
      for(ii=0; ii<nPage && needSync; ii++){
ds@1952
  4250
        PgHdr *pPage = pager_lookup(pPager, pg1+ii);
ds@1952
  4251
        if( pPage ) pPage->needSync = 1;
ds@1952
  4252
      }
ds@1952
  4253
      assert(pPager->needSync);
ds@1952
  4254
    }
ds@1952
  4255
ds@1952
  4256
    assert( pPager->doNotSync==1 );
ds@1952
  4257
    pPager->doNotSync = 0;
ds@1952
  4258
  }else{
ds@1952
  4259
    rc = pager_write(pDbPage);
ds@1952
  4260
  }
ds@1952
  4261
  pagerLeave(pPager);
ds@1952
  4262
  return rc;
ds@1952
  4263
}
ds@1952
  4264
ds@1952
  4265
/*
ds@1952
  4266
** Return TRUE if the page given in the argument was previously passed
ds@1952
  4267
** to sqlite3PagerWrite().  In other words, return TRUE if it is ok
ds@1952
  4268
** to change the content of the page.
ds@1952
  4269
*/
ds@1952
  4270
#ifndef NDEBUG
ds@1952
  4271
int sqlite3PagerIswriteable(DbPage *pPg){
ds@1952
  4272
  return pPg->dirty;
ds@1952
  4273
}
ds@1952
  4274
#endif
ds@1952
  4275
ds@1952
  4276
#ifndef SQLITE_OMIT_VACUUM
ds@1952
  4277
/*
ds@1952
  4278
** Replace the content of a single page with the information in the third
ds@1952
  4279
** argument.
ds@1952
  4280
*/
ds@1952
  4281
int sqlite3PagerOverwrite(Pager *pPager, Pgno pgno, void *pData){
ds@1952
  4282
  PgHdr *pPg;
ds@1952
  4283
  int rc;
ds@1952
  4284
ds@1952
  4285
  pagerEnter(pPager);
ds@1952
  4286
  rc = sqlite3PagerGet(pPager, pgno, &pPg);
ds@1952
  4287
  if( rc==SQLITE_OK ){
ds@1952
  4288
    rc = sqlite3PagerWrite(pPg);
ds@1952
  4289
    if( rc==SQLITE_OK ){
ds@1952
  4290
      memcpy(sqlite3PagerGetData(pPg), pData, pPager->pageSize);
ds@1952
  4291
    }
ds@1952
  4292
    sqlite3PagerUnref(pPg);
ds@1952
  4293
  }
ds@1952
  4294
  pagerLeave(pPager);
ds@1952
  4295
  return rc;
ds@1952
  4296
}
ds@1952
  4297
#endif
ds@1952
  4298
ds@1952
  4299
/*
ds@1952
  4300
** A call to this routine tells the pager that it is not necessary to
ds@1952
  4301
** write the information on page pPg back to the disk, even though
ds@1952
  4302
** that page might be marked as dirty.
ds@1952
  4303
**
ds@1952
  4304
** The overlying software layer calls this routine when all of the data
ds@1952
  4305
** on the given page is unused.  The pager marks the page as clean so
ds@1952
  4306
** that it does not get written to disk.
ds@1952
  4307
**
ds@1952
  4308
** Tests show that this optimization, together with the
ds@1952
  4309
** sqlite3PagerDontRollback() below, more than double the speed
ds@1952
  4310
** of large INSERT operations and quadruple the speed of large DELETEs.
ds@1952
  4311
**
ds@1952
  4312
** When this routine is called, set the alwaysRollback flag to true.
ds@1952
  4313
** Subsequent calls to sqlite3PagerDontRollback() for the same page
ds@1952
  4314
** will thereafter be ignored.  This is necessary to avoid a problem
ds@1952
  4315
** where a page with data is added to the freelist during one part of
ds@1952
  4316
** a transaction then removed from the freelist during a later part
ds@1952
  4317
** of the same transaction and reused for some other purpose.  When it
ds@1952
  4318
** is first added to the freelist, this routine is called.  When reused,
ds@1952
  4319
** the sqlite3PagerDontRollback() routine is called.  But because the
ds@1952
  4320
** page contains critical data, we still need to be sure it gets
ds@1952
  4321
** rolled back in spite of the sqlite3PagerDontRollback() call.
ds@1952
  4322
*/
ds@1952
  4323
void sqlite3PagerDontWrite(DbPage *pDbPage){
ds@1952
  4324
  PgHdr *pPg = pDbPage;
ds@1952
  4325
  Pager *pPager = pPg->pPager;
ds@1952
  4326
ds@1952
  4327
  if( MEMDB ) return;
ds@1952
  4328
  pagerEnter(pPager);
ds@1952
  4329
  pPg->alwaysRollback = 1;
ds@1952
  4330
  if( pPg->dirty && !pPager->stmtInUse ){
ds@1952
  4331
    assert( pPager->state>=PAGER_SHARED );
ds@1952
  4332
    if( pPager->dbSize==(int)pPg->pgno && pPager->origDbSize<pPager->dbSize ){
ds@1952
  4333
      /* If this pages is the last page in the file and the file has grown
ds@1952
  4334
      ** during the current transaction, then do NOT mark the page as clean.
ds@1952
  4335
      ** When the database file grows, we must make sure that the last page
ds@1952
  4336
      ** gets written at least once so that the disk file will be the correct
ds@1952
  4337
      ** size. If you do not write this page and the size of the file
ds@1952
  4338
      ** on the disk ends up being too small, that can lead to database
ds@1952
  4339
      ** corruption during the next transaction.
ds@1952
  4340
      */
ds@1952
  4341
    }else{
ds@1952
  4342
      PAGERTRACE3("DONT_WRITE page %d of %d\n", pPg->pgno, PAGERID(pPager));
ds@1952
  4343
      IOTRACE(("CLEAN %p %d\n", pPager, pPg->pgno))
ds@1952
  4344
      makeClean(pPg);
ds@1952
  4345
#ifdef SQLITE_CHECK_PAGES
ds@1952
  4346
      pPg->pageHash = pager_pagehash(pPg);
ds@1952
  4347
#endif
ds@1952
  4348
    }
ds@1952
  4349
  }
ds@1952
  4350
  pagerLeave(pPager);
ds@1952
  4351
}
ds@1952
  4352
ds@1952
  4353
/*
ds@1952
  4354
** A call to this routine tells the pager that if a rollback occurs,
ds@1952
  4355
** it is not necessary to restore the data on the given page.  This
ds@1952
  4356
** means that the pager does not have to record the given page in the
ds@1952
  4357
** rollback journal.
ds@1952
  4358
**
ds@1952
  4359
** If we have not yet actually read the content of this page (if
ds@1952
  4360
** the PgHdr.needRead flag is set) then this routine acts as a promise
ds@1952
  4361
** that we will never need to read the page content in the future.
ds@1952
  4362
** so the needRead flag can be cleared at this point.
ds@1952
  4363
*/
ds@1952
  4364
void sqlite3PagerDontRollback(DbPage *pPg){
ds@1952
  4365
  Pager *pPager = pPg->pPager;
ds@1952
  4366
ds@1952
  4367
  pagerEnter(pPager);
ds@1952
  4368
  assert( pPager->state>=PAGER_RESERVED );
ds@1952
  4369
  if( pPager->journalOpen==0 ) return;
ds@1952
  4370
  if( pPg->alwaysRollback || pPager->alwaysRollback || MEMDB ) return;
ds@1952
  4371
  if( !pPg->inJournal && (int)pPg->pgno <= pPager->origDbSize ){
ds@1952
  4372
    assert( pPager->aInJournal!=0 );
ds@1952
  4373
    pPager->aInJournal[pPg->pgno/8] |= 1<<(pPg->pgno&7);
ds@1952
  4374
    pPg->inJournal = 1;
ds@1952
  4375
    pPg->needRead = 0;
ds@1952
  4376
    if( pPager->stmtInUse ){
ds@1952
  4377
      pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
ds@1952
  4378
    }
ds@1952
  4379
    PAGERTRACE3("DONT_ROLLBACK page %d of %d\n", pPg->pgno, PAGERID(pPager));
ds@1952
  4380
    IOTRACE(("GARBAGE %p %d\n", pPager, pPg->pgno))
ds@1952
  4381
  }
ds@1952
  4382
  if( pPager->stmtInUse 
ds@1952
  4383
   && !pageInStatement(pPg) 
ds@1952
  4384
   && (int)pPg->pgno<=pPager->stmtSize 
ds@1952
  4385
  ){
ds@1952
  4386
    assert( pPg->inJournal || (int)pPg->pgno>pPager->origDbSize );
ds@1952
  4387
    assert( pPager->aInStmt!=0 );
ds@1952
  4388
    pPager->aInStmt[pPg->pgno/8] |= 1<<(pPg->pgno&7);
ds@1952
  4389
  }
ds@1952
  4390
  pagerLeave(pPager);
ds@1952
  4391
}
ds@1952
  4392
ds@1952
  4393
ds@1952
  4394
/*
ds@1952
  4395
** This routine is called to increment the database file change-counter,
ds@1952
  4396
** stored at byte 24 of the pager file.
ds@1952
  4397
*/
ds@1952
  4398
static int pager_incr_changecounter(Pager *pPager, int isDirect){
ds@1952
  4399
  PgHdr *pPgHdr;
ds@1952
  4400
  u32 change_counter;
ds@1952
  4401
  int rc = SQLITE_OK;
ds@1952
  4402
ds@1952
  4403
  if( !pPager->changeCountDone ){
ds@1952
  4404
    /* Open page 1 of the file for writing. */
ds@1952
  4405
    rc = sqlite3PagerGet(pPager, 1, &pPgHdr);
ds@1952
  4406
    if( rc!=SQLITE_OK ) return rc;
ds@1952
  4407
ds@1952
  4408
    if( !isDirect ){
ds@1952
  4409
      rc = sqlite3PagerWrite(pPgHdr);
ds@1952
  4410
      if( rc!=SQLITE_OK ){
ds@1952
  4411
        sqlite3PagerUnref(pPgHdr);
ds@1952
  4412
        return rc;
ds@1952
  4413
      }
ds@1952
  4414
    }
ds@1952
  4415
ds@1952
  4416
    /* Increment the value just read and write it back to byte 24. */
ds@1952
  4417
    change_counter = sqlite3Get4byte((u8*)pPager->dbFileVers);
ds@1952
  4418
    change_counter++;
ds@1952
  4419
    put32bits(((char*)PGHDR_TO_DATA(pPgHdr))+24, change_counter);
ds@1952
  4420
ds@1952
  4421
    if( isDirect && pPager->fd->pMethods ){
ds@1952
  4422
      const void *zBuf = PGHDR_TO_DATA(pPgHdr);
ds@1952
  4423
      rc = sqlite3OsWrite(pPager->fd, zBuf, pPager->pageSize, 0);
ds@1952
  4424
    }
ds@1952
  4425
ds@1952
  4426
    /* Release the page reference. */
ds@1952
  4427
    sqlite3PagerUnref(pPgHdr);
ds@1952
  4428
    pPager->changeCountDone = 1;
ds@1952
  4429
  }
ds@1952
  4430
  return rc;
ds@1952
  4431
}
ds@1952
  4432
ds@1952
  4433
/*
ds@1952
  4434
** Sync the database file for the pager pPager. zMaster points to the name
ds@1952
  4435
** of a master journal file that should be written into the individual
ds@1952
  4436
** journal file. zMaster may be NULL, which is interpreted as no master
ds@1952
  4437
** journal (a single database transaction).
ds@1952
  4438
**
ds@1952
  4439
** This routine ensures that the journal is synced, all dirty pages written
ds@1952
  4440
** to the database file and the database file synced. The only thing that
ds@1952
  4441
** remains to commit the transaction is to delete the journal file (or
ds@1952
  4442
** master journal file if specified).
ds@1952
  4443
**
ds@1952
  4444
** Note that if zMaster==NULL, this does not overwrite a previous value
ds@1952
  4445
** passed to an sqlite3PagerCommitPhaseOne() call.
ds@1952
  4446
**
ds@1952
  4447
** If parameter nTrunc is non-zero, then the pager file is truncated to
ds@1952
  4448
** nTrunc pages (this is used by auto-vacuum databases).
ds@1952
  4449
*/
ds@1952
  4450
int sqlite3PagerCommitPhaseOne(Pager *pPager, const char *zMaster, Pgno nTrunc){
ds@1952
  4451
  int rc = SQLITE_OK;
ds@1952
  4452
ds@1952
  4453
  PAGERTRACE4("DATABASE SYNC: File=%s zMaster=%s nTrunc=%d\n", 
ds@1952
  4454
      pPager->zFilename, zMaster, nTrunc);
ds@1952
  4455
  pagerEnter(pPager);
ds@1952
  4456
ds@1952
  4457
  /* If this is an in-memory db, or no pages have been written to, or this
ds@1952
  4458
  ** function has already been called, it is a no-op.
ds@1952
  4459
  */
ds@1952
  4460
  if( pPager->state!=PAGER_SYNCED && !MEMDB && pPager->dirtyCache ){
ds@1952
  4461
    PgHdr *pPg;
ds@1952
  4462
ds@1952
  4463
#ifdef SQLITE_ENABLE_ATOMIC_WRITE
ds@1952
  4464
    /* The atomic-write optimization can be used if all of the
ds@1952
  4465
    ** following are true:
ds@1952
  4466
    **
ds@1952
  4467
    **    + The file-system supports the atomic-write property for
ds@1952
  4468
    **      blocks of size page-size, and
ds@1952
  4469
    **    + This commit is not part of a multi-file transaction, and
ds@1952
  4470
    **    + Exactly one page has been modified and store in the journal file.
ds@1952
  4471
    **
ds@1952
  4472
    ** If the optimization can be used, then the journal file will never
ds@1952
  4473
    ** be created for this transaction.
ds@1952
  4474
    */
ds@1952
  4475
    int useAtomicWrite = (
ds@1952
  4476
        !zMaster && 
ds@1952
  4477
        pPager->journalOff==jrnlBufferSize(pPager) && 
ds@1952
  4478
        nTrunc==0 && 
ds@1952
  4479
        (0==pPager->pDirty || 0==pPager->pDirty->pDirty)
ds@1952
  4480
    );
ds@1952
  4481
    if( useAtomicWrite ){
ds@1952
  4482
      /* Update the nRec field in the journal file. */
ds@1952
  4483
      int offset = pPager->journalHdr + sizeof(aJournalMagic);
ds@1952
  4484
      assert(pPager->nRec==1);
ds@1952
  4485
      rc = write32bits(pPager->jfd, offset, pPager->nRec);
ds@1952
  4486
ds@1952
  4487
      /* Update the db file change counter. The following call will modify
ds@1952
  4488
      ** the in-memory representation of page 1 to include the updated
ds@1952
  4489
      ** change counter and then write page 1 directly to the database
ds@1952
  4490
      ** file. Because of the atomic-write property of the host file-system, 
ds@1952
  4491
      ** this is safe.
ds@1952
  4492
      */
ds@1952
  4493
      if( rc==SQLITE_OK ){
ds@1952
  4494
        rc = pager_incr_changecounter(pPager, 1);
ds@1952
  4495
      }
ds@1952
  4496
    }else{
ds@1952
  4497
      rc = sqlite3JournalCreate(pPager->jfd);
ds@1952
  4498
    }
ds@1952
  4499
ds@1952
  4500
    if( !useAtomicWrite && rc==SQLITE_OK )
ds@1952
  4501
#endif
ds@1952
  4502
ds@1952
  4503
    /* If a master journal file name has already been written to the
ds@1952
  4504
    ** journal file, then no sync is required. This happens when it is
ds@1952
  4505
    ** written, then the process fails to upgrade from a RESERVED to an
ds@1952
  4506
    ** EXCLUSIVE lock. The next time the process tries to commit the
ds@1952
  4507
    ** transaction the m-j name will have already been written.
ds@1952
  4508
    */
ds@1952
  4509
    if( !pPager->setMaster ){
ds@1952
  4510
      assert( pPager->journalOpen );
ds@1952
  4511
      rc = pager_incr_changecounter(pPager, 0);
ds@1952
  4512
      if( rc!=SQLITE_OK ) goto sync_exit;
ds@1952
  4513
#ifndef SQLITE_OMIT_AUTOVACUUM
ds@1952
  4514
      if( nTrunc!=0 ){
ds@1952
  4515
        /* If this transaction has made the database smaller, then all pages
ds@1952
  4516
        ** being discarded by the truncation must be written to the journal
ds@1952
  4517
        ** file.
ds@1952
  4518
        */
ds@1952
  4519
        Pgno i;
ds@1952
  4520
        int iSkip = PAGER_MJ_PGNO(pPager);
ds@1952
  4521
        for( i=nTrunc+1; i<=pPager->origDbSize; i++ ){
ds@1952
  4522
          if( !(pPager->aInJournal[i/8] & (1<<(i&7))) && i!=iSkip ){
ds@1952
  4523
            rc = sqlite3PagerGet(pPager, i, &pPg);
ds@1952
  4524
            if( rc!=SQLITE_OK ) goto sync_exit;
ds@1952
  4525
            rc = sqlite3PagerWrite(pPg);
ds@1952
  4526
            sqlite3PagerUnref(pPg);
ds@1952
  4527
            if( rc!=SQLITE_OK ) goto sync_exit;
ds@1952
  4528
          }
ds@1952
  4529
        } 
ds@1952
  4530
      }
ds@1952
  4531
#endif
ds@1952
  4532
      rc = writeMasterJournal(pPager, zMaster);
ds@1952
  4533
      if( rc!=SQLITE_OK ) goto sync_exit;
ds@1952
  4534
      rc = syncJournal(pPager);
ds@1952
  4535
    }
ds@1952
  4536
    if( rc!=SQLITE_OK ) goto sync_exit;
ds@1952
  4537
ds@1952
  4538
#ifndef SQLITE_OMIT_AUTOVACUUM
ds@1952
  4539
    if( nTrunc!=0 ){
ds@1952
  4540
      rc = sqlite3PagerTruncate(pPager, nTrunc);
ds@1952
  4541
      if( rc!=SQLITE_OK ) goto sync_exit;
ds@1952
  4542
    }
ds@1952
  4543
#endif
ds@1952
  4544
ds@1952
  4545
    /* Write all dirty pages to the database file */
ds@1952
  4546
    pPg = pager_get_all_dirty_pages(pPager);
ds@1952
  4547
    rc = pager_write_pagelist(pPg);
ds@1952
  4548
    if( rc!=SQLITE_OK ){
ds@1952
  4549
      while( pPg && !pPg->dirty ){ pPg = pPg->pDirty; }
ds@1952
  4550
      pPager->pDirty = pPg;
ds@1952
  4551
      goto sync_exit;
ds@1952
  4552
    }
ds@1952
  4553
    pPager->pDirty = 0;
ds@1952
  4554
ds@1952
  4555
    /* Sync the database file. */
ds@1952
  4556
    if( !pPager->noSync ){
ds@1952
  4557
      rc = sqlite3OsSync(pPager->fd, pPager->sync_flags);
ds@1952
  4558
    }
ds@1952
  4559
    IOTRACE(("DBSYNC %p\n", pPager))
ds@1952
  4560
ds@1952
  4561
    pPager->state = PAGER_SYNCED;
ds@1952
  4562
  }else if( MEMDB && nTrunc!=0 ){
ds@1952
  4563
    rc = sqlite3PagerTruncate(pPager, nTrunc);
ds@1952
  4564
  }
ds@1952
  4565
ds@1952
  4566
sync_exit:
ds@1952
  4567
  if( rc==SQLITE_IOERR_BLOCKED ){
ds@1952
  4568
    /* pager_incr_changecounter() may attempt to obtain an exclusive
ds@1952
  4569
     * lock to spill the cache and return IOERR_BLOCKED. But since 
ds@1952
  4570
     * there is no chance the cache is inconsistent, it's
ds@1952
  4571
     * better to return SQLITE_BUSY.
ds@1952
  4572
     */
ds@1952
  4573
    rc = SQLITE_BUSY;
ds@1952
  4574
  }
ds@1952
  4575
  pagerLeave(pPager);
ds@1952
  4576
  return rc;
ds@1952
  4577
}
ds@1952
  4578
ds@1952
  4579
ds@1952
  4580
/*
ds@1952
  4581
** Commit all changes to the database and release the write lock.
ds@1952
  4582
**
ds@1952
  4583
** If the commit fails for any reason, a rollback attempt is made
ds@1952
  4584
** and an error code is returned.  If the commit worked, SQLITE_OK
ds@1952
  4585
** is returned.
ds@1952
  4586
*/
ds@1952
  4587
int sqlite3PagerCommitPhaseTwo(Pager *pPager){
ds@1952
  4588
  int rc;
ds@1952
  4589
  PgHdr *pPg;
ds@1952
  4590
ds@1952
  4591
  if( pPager->errCode ){
ds@1952
  4592
    return pPager->errCode;
ds@1952
  4593
  }
ds@1952
  4594
  if( pPager->state<PAGER_RESERVED ){
ds@1952
  4595
    return SQLITE_ERROR;
ds@1952
  4596
  }
ds@1952
  4597
  pagerEnter(pPager);
ds@1952
  4598
  PAGERTRACE2("COMMIT %d\n", PAGERID(pPager));
ds@1952
  4599
  if( MEMDB ){
ds@1952
  4600
    pPg = pager_get_all_dirty_pages(pPager);
ds@1952
  4601
    while( pPg ){
ds@1952
  4602
      PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
ds@1952
  4603
      clearHistory(pHist);
ds@1952
  4604
      pPg->dirty = 0;
ds@1952
  4605
      pPg->inJournal = 0;
ds@1952
  4606
      pHist->inStmt = 0;
ds@1952
  4607
      pPg->needSync = 0;
ds@1952
  4608
      pHist->pPrevStmt = pHist->pNextStmt = 0;
ds@1952
  4609
      pPg = pPg->pDirty;
ds@1952
  4610
    }
ds@1952
  4611
    pPager->pDirty = 0;
ds@1952
  4612
#ifndef NDEBUG
ds@1952
  4613
    for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
ds@1952
  4614
      PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
ds@1952
  4615
      assert( !pPg->alwaysRollback );
ds@1952
  4616
      assert( !pHist->pOrig );
ds@1952
  4617
      assert( !pHist->pStmt );
ds@1952
  4618
    }
ds@1952
  4619
#endif
ds@1952
  4620
    pPager->pStmt = 0;
ds@1952
  4621
    pPager->state = PAGER_SHARED;
ds@1952
  4622
    return SQLITE_OK;
ds@1952
  4623
  }
ds@1952
  4624
  assert( pPager->journalOpen || !pPager->dirtyCache );
ds@1952
  4625
  assert( pPager->state==PAGER_SYNCED || !pPager->dirtyCache );
ds@1952
  4626
  rc = pager_end_transaction(pPager);
ds@1952
  4627
  rc = pager_error(pPager, rc);
ds@1952
  4628
  pagerLeave(pPager);
ds@1952
  4629
  return rc;
ds@1952
  4630
}
ds@1952
  4631
ds@1952
  4632
/*
ds@1952
  4633
** Rollback all changes.  The database falls back to PAGER_SHARED mode.
ds@1952
  4634
** All in-memory cache pages revert to their original data contents.
ds@1952
  4635
** The journal is deleted.
ds@1952
  4636
**
ds@1952
  4637
** This routine cannot fail unless some other process is not following
ds@1952
  4638
** the correct locking protocol or unless some other
ds@1952
  4639
** process is writing trash into the journal file (SQLITE_CORRUPT) or
ds@1952
  4640
** unless a prior malloc() failed (SQLITE_NOMEM).  Appropriate error
ds@1952
  4641
** codes are returned for all these occasions.  Otherwise,
ds@1952
  4642
** SQLITE_OK is returned.
ds@1952
  4643
*/
ds@1952
  4644
int sqlite3PagerRollback(Pager *pPager){
ds@1952
  4645
  int rc;
ds@1952
  4646
  PAGERTRACE2("ROLLBACK %d\n", PAGERID(pPager));
ds@1952
  4647
  if( MEMDB ){
ds@1952
  4648
    PgHdr *p;
ds@1952
  4649
    for(p=pPager->pAll; p; p=p->pNextAll){
ds@1952
  4650
      PgHistory *pHist;
ds@1952
  4651
      assert( !p->alwaysRollback );
ds@1952
  4652
      if( !p->dirty ){
ds@1952
  4653
        assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pOrig );
ds@1952
  4654
        assert( !((PgHistory *)PGHDR_TO_HIST(p, pPager))->pStmt );
ds@1952
  4655
        continue;
ds@1952
  4656
      }
ds@1952
  4657
ds@1952
  4658
      pHist = PGHDR_TO_HIST(p, pPager);
ds@1952
  4659
      if( pHist->pOrig ){
ds@1952
  4660
        memcpy(PGHDR_TO_DATA(p), pHist->pOrig, pPager->pageSize);
ds@1952
  4661
        PAGERTRACE3("ROLLBACK-PAGE %d of %d\n", p->pgno, PAGERID(pPager));
ds@1952
  4662
      }else{
ds@1952
  4663
        PAGERTRACE3("PAGE %d is clean on %d\n", p->pgno, PAGERID(pPager));
ds@1952
  4664
      }
ds@1952
  4665
      clearHistory(pHist);
ds@1952
  4666
      p->dirty = 0;
ds@1952
  4667
      p->inJournal = 0;
ds@1952
  4668
      pHist->inStmt = 0;
ds@1952
  4669
      pHist->pPrevStmt = pHist->pNextStmt = 0;
ds@1952
  4670
      if( pPager->xReiniter ){
ds@1952
  4671
        pPager->xReiniter(p, pPager->pageSize);
ds@1952
  4672
      }
ds@1952
  4673
    }
ds@1952
  4674
    pPager->pDirty = 0;
ds@1952
  4675
    pPager->pStmt = 0;
ds@1952
  4676
    pPager->dbSize = pPager->origDbSize;
ds@1952
  4677
    pager_truncate_cache(pPager);
ds@1952
  4678
    pPager->stmtInUse = 0;
ds@1952
  4679
    pPager->state = PAGER_SHARED;
ds@1952
  4680
    return SQLITE_OK;
ds@1952
  4681
  }
ds@1952
  4682
ds@1952
  4683
  pagerEnter(pPager);
ds@1952
  4684
  if( !pPager->dirtyCache || !pPager->journalOpen ){
ds@1952
  4685
    rc = pager_end_transaction(pPager);
ds@1952
  4686
    pagerLeave(pPager);
ds@1952
  4687
    return rc;
ds@1952
  4688
  }
ds@1952
  4689
ds@1952
  4690
  if( pPager->errCode && pPager->errCode!=SQLITE_FULL ){
ds@1952
  4691
    if( pPager->state>=PAGER_EXCLUSIVE ){
ds@1952
  4692
      pager_playback(pPager, 0);
ds@1952
  4693
    }
ds@1952
  4694
    pagerLeave(pPager);
ds@1952
  4695
    return pPager->errCode;
ds@1952
  4696
  }
ds@1952
  4697
  if( pPager->state==PAGER_RESERVED ){
ds@1952
  4698
    int rc2;
ds@1952
  4699
    rc = pager_playback(pPager, 0);
ds@1952
  4700
    rc2 = pager_end_transaction(pPager);
ds@1952
  4701
    if( rc==SQLITE_OK ){
ds@1952
  4702
      rc = rc2;
ds@1952
  4703
    }
ds@1952
  4704
  }else{
ds@1952
  4705
    rc = pager_playback(pPager, 0);
ds@1952
  4706
  }
ds@1952
  4707
  /* pager_reset(pPager); */
ds@1952
  4708
  pPager->dbSize = -1;
ds@1952
  4709
ds@1952
  4710
  /* If an error occurs during a ROLLBACK, we can no longer trust the pager
ds@1952
  4711
  ** cache. So call pager_error() on the way out to make any error 
ds@1952
  4712
  ** persistent.
ds@1952
  4713
  */
ds@1952
  4714
  rc = pager_error(pPager, rc);
ds@1952
  4715
  pagerLeave(pPager);
ds@1952
  4716
  return rc;
ds@1952
  4717
}
ds@1952
  4718
ds@1952
  4719
/*
ds@1952
  4720
** Return TRUE if the database file is opened read-only.  Return FALSE
ds@1952
  4721
** if the database is (in theory) writable.
ds@1952
  4722
*/
ds@1952
  4723
int sqlite3PagerIsreadonly(Pager *pPager){
ds@1952
  4724
  return pPager->readOnly;
ds@1952
  4725
}
ds@1952
  4726
ds@1952
  4727
/*
ds@1952
  4728
** Return the number of references to the pager.
ds@1952
  4729
*/
ds@1952
  4730
int sqlite3PagerRefcount(Pager *pPager){
ds@1952
  4731
  return pPager->nRef;
ds@1952
  4732
}
ds@1952
  4733
ds@1952
  4734
#ifdef SQLITE_TEST
ds@1952
  4735
/*
ds@1952
  4736
** This routine is used for testing and analysis only.
ds@1952
  4737
*/
ds@1952
  4738
int *sqlite3PagerStats(Pager *pPager){
ds@1952
  4739
  static int a[11];
ds@1952
  4740
  a[0] = pPager->nRef;
ds@1952
  4741
  a[1] = pPager->nPage;
ds@1952
  4742
  a[2] = pPager->mxPage;
ds@1952
  4743
  a[3] = pPager->dbSize;
ds@1952
  4744
  a[4] = pPager->state;
ds@1952
  4745
  a[5] = pPager->errCode;
ds@1952
  4746
  a[6] = pPager->nHit;
ds@1952
  4747
  a[7] = pPager->nMiss;
ds@1952
  4748
  a[8] = 0;  /* Used to be pPager->nOvfl */
ds@1952
  4749
  a[9] = pPager->nRead;
ds@1952
  4750
  a[10] = pPager->nWrite;
ds@1952
  4751
  return a;
ds@1952
  4752
}
ds@1952
  4753
#endif
ds@1952
  4754
ds@1952
  4755
/*
ds@1952
  4756
** Set the statement rollback point.
ds@1952
  4757
**
ds@1952
  4758
** This routine should be called with the transaction journal already
ds@1952
  4759
** open.  A new statement journal is created that can be used to rollback
ds@1952
  4760
** changes of a single SQL command within a larger transaction.
ds@1952
  4761
*/
ds@1952
  4762
static int pagerStmtBegin(Pager *pPager){
ds@1952
  4763
  int rc;
ds@1952
  4764
  assert( !pPager->stmtInUse );
ds@1952
  4765
  assert( pPager->state>=PAGER_SHARED );
ds@1952
  4766
  assert( pPager->dbSize>=0 );
ds@1952
  4767
  PAGERTRACE2("STMT-BEGIN %d\n", PAGERID(pPager));
ds@1952
  4768
  if( MEMDB ){
ds@1952
  4769
    pPager->stmtInUse = 1;
ds@1952
  4770
    pPager->stmtSize = pPager->dbSize;
ds@1952
  4771
    return SQLITE_OK;
ds@1952
  4772
  }
ds@1952
  4773
  if( !pPager->journalOpen ){
ds@1952
  4774
    pPager->stmtAutoopen = 1;
ds@1952
  4775
    return SQLITE_OK;
ds@1952
  4776
  }
ds@1952
  4777
  assert( pPager->journalOpen );
ds@1952
  4778
  pagerLeave(pPager);
ds@1952
  4779
  assert( pPager->aInStmt==0 );
ds@1952
  4780
  pPager->aInStmt = sqlite3MallocZero( pPager->dbSize/8 + 1 );
ds@1952
  4781
  pagerEnter(pPager);
ds@1952
  4782
  if( pPager->aInStmt==0 ){
ds@1952
  4783
    /* sqlite3OsLock(pPager->fd, SHARED_LOCK); */
ds@1952
  4784
    return SQLITE_NOMEM;
ds@1952
  4785
  }
ds@1952
  4786
#ifndef NDEBUG
ds@1952
  4787
  rc = sqlite3OsFileSize(pPager->jfd, &pPager->stmtJSize);
ds@1952
  4788
  if( rc ) goto stmt_begin_failed;
ds@1952
  4789
  assert( pPager->stmtJSize == pPager->journalOff );
ds@1952
  4790
#endif
ds@1952
  4791
  pPager->stmtJSize = pPager->journalOff;
ds@1952
  4792
  pPager->stmtSize = pPager->dbSize;
ds@1952
  4793
  pPager->stmtHdrOff = 0;
ds@1952
  4794
  pPager->stmtCksum = pPager->cksumInit;
ds@1952
  4795
  if( !pPager->stmtOpen ){
ds@1952
  4796
    rc = sqlite3PagerOpentemp(pPager->pVfs, pPager->stfd, pPager->zStmtJrnl,
ds@1952
  4797
                              SQLITE_OPEN_SUBJOURNAL);
ds@1952
  4798
    if( rc ){
ds@1952
  4799
      goto stmt_begin_failed;
ds@1952
  4800
    }
ds@1952
  4801
    pPager->stmtOpen = 1;
ds@1952
  4802
    pPager->stmtNRec = 0;
ds@1952
  4803
  }
ds@1952
  4804
  pPager->stmtInUse = 1;
ds@1952
  4805
  return SQLITE_OK;
ds@1952
  4806
 
ds@1952
  4807
stmt_begin_failed:
ds@1952
  4808
  if( pPager->aInStmt ){
ds@1952
  4809
    sqlite3_free(pPager->aInStmt);
ds@1952
  4810
    pPager->aInStmt = 0;
ds@1952
  4811
  }
ds@1952
  4812
  return rc;
ds@1952
  4813
}
ds@1952
  4814
int sqlite3PagerStmtBegin(Pager *pPager){
ds@1952
  4815
  int rc;
ds@1952
  4816
  pagerEnter(pPager);
ds@1952
  4817
  rc = pagerStmtBegin(pPager);
ds@1952
  4818
  pagerLeave(pPager);
ds@1952
  4819
  return rc;
ds@1952
  4820
}
ds@1952
  4821
ds@1952
  4822
/*
ds@1952
  4823
** Commit a statement.
ds@1952
  4824
*/
ds@1952
  4825
int sqlite3PagerStmtCommit(Pager *pPager){
ds@1952
  4826
  pagerEnter(pPager);
ds@1952
  4827
  if( pPager->stmtInUse ){
ds@1952
  4828
    PgHdr *pPg, *pNext;
ds@1952
  4829
    PAGERTRACE2("STMT-COMMIT %d\n", PAGERID(pPager));
ds@1952
  4830
    if( !MEMDB ){
ds@1952
  4831
      /* sqlite3OsTruncate(pPager->stfd, 0); */
ds@1952
  4832
      sqlite3_free( pPager->aInStmt );
ds@1952
  4833
      pPager->aInStmt = 0;
ds@1952
  4834
    }else{
ds@1952
  4835
      for(pPg=pPager->pStmt; pPg; pPg=pNext){
ds@1952
  4836
        PgHistory *pHist = PGHDR_TO_HIST(pPg, pPager);
ds@1952
  4837
        pNext = pHist->pNextStmt;
ds@1952
  4838
        assert( pHist->inStmt );
ds@1952
  4839
        pHist->inStmt = 0;
ds@1952
  4840
        pHist->pPrevStmt = pHist->pNextStmt = 0;
ds@1952
  4841
        sqlite3_free(pHist->pStmt);
ds@1952
  4842
        pHist->pStmt = 0;
ds@1952
  4843
      }
ds@1952
  4844
    }
ds@1952
  4845
    pPager->stmtNRec = 0;
ds@1952
  4846
    pPager->stmtInUse = 0;
ds@1952
  4847
    pPager->pStmt = 0;
ds@1952
  4848
  }
ds@1952
  4849
  pPager->stmtAutoopen = 0;
ds@1952
  4850
  pagerLeave(pPager);
ds@1952
  4851
  return SQLITE_OK;
ds@1952
  4852
}
ds@1952
  4853
ds@1952
  4854
/*
ds@1952
  4855
** Rollback a statement.
ds@1952
  4856
*/
ds@1952
  4857
int sqlite3PagerStmtRollback(Pager *pPager){
ds@1952
  4858
  int rc;
ds@1952
  4859
  pagerEnter(pPager);
ds@1952
  4860
  if( pPager->stmtInUse ){
ds@1952
  4861
    PAGERTRACE2("STMT-ROLLBACK %d\n", PAGERID(pPager));
ds@1952
  4862
    if( MEMDB ){
ds@1952
  4863
      PgHdr *pPg;
ds@1952
  4864
      PgHistory *pHist;
ds@1952
  4865
      for(pPg=pPager->pStmt; pPg; pPg=pHist->pNextStmt){
ds@1952
  4866
        pHist = PGHDR_TO_HIST(pPg, pPager);
ds@1952
  4867
        if( pHist->pStmt ){
ds@1952
  4868
          memcpy(PGHDR_TO_DATA(pPg), pHist->pStmt, pPager->pageSize);
ds@1952
  4869
          sqlite3_free(pHist->pStmt);
ds@1952
  4870
          pHist->pStmt = 0;
ds@1952
  4871
        }
ds@1952
  4872
      }
ds@1952
  4873
      pPager->dbSize = pPager->stmtSize;
ds@1952
  4874
      pager_truncate_cache(pPager);
ds@1952
  4875
      rc = SQLITE_OK;
ds@1952
  4876
    }else{
ds@1952
  4877
      rc = pager_stmt_playback(pPager);
ds@1952
  4878
    }
ds@1952
  4879
    sqlite3PagerStmtCommit(pPager);
ds@1952
  4880
  }else{
ds@1952
  4881
    rc = SQLITE_OK;
ds@1952
  4882
  }
ds@1952
  4883
  pPager->stmtAutoopen = 0;
ds@1952
  4884
  pagerLeave(pPager);
ds@1952
  4885
  return rc;
ds@1952
  4886
}
ds@1952
  4887
ds@1952
  4888
/*
ds@1952
  4889
** Return the full pathname of the database file.
ds@1952
  4890
*/
ds@1952
  4891
const char *sqlite3PagerFilename(Pager *pPager){
ds@1952
  4892
  return pPager->zFilename;
ds@1952
  4893
}
ds@1952
  4894
ds@1952
  4895
/*
ds@1952
  4896
** Return the VFS structure for the pager.
ds@1952
  4897
*/
ds@1952
  4898
const sqlite3_vfs *sqlite3PagerVfs(Pager *pPager){
ds@1952
  4899
  return pPager->pVfs;
ds@1952
  4900
}
ds@1952
  4901
ds@1952
  4902
/*
ds@1952
  4903
** Return the file handle for the database file associated
ds@1952
  4904
** with the pager.  This might return NULL if the file has
ds@1952
  4905
** not yet been opened.
ds@1952
  4906
*/
ds@1952
  4907
sqlite3_file *sqlite3PagerFile(Pager *pPager){
ds@1952
  4908
  return pPager->fd;
ds@1952
  4909
}
ds@1952
  4910
ds@1952
  4911
/*
ds@1952
  4912
** Return the directory of the database file.
ds@1952
  4913
*/
ds@1952
  4914
const char *sqlite3PagerDirname(Pager *pPager){
ds@1952
  4915
  return pPager->zDirectory;
ds@1952
  4916
}
ds@1952
  4917
ds@1952
  4918
/*
ds@1952
  4919
** Return the full pathname of the journal file.
ds@1952
  4920
*/
ds@1952
  4921
const char *sqlite3PagerJournalname(Pager *pPager){
ds@1952
  4922
  return pPager->zJournal;
ds@1952
  4923
}
ds@1952
  4924
ds@1952
  4925
/*
ds@1952
  4926
** Return true if fsync() calls are disabled for this pager.  Return FALSE
ds@1952
  4927
** if fsync()s are executed normally.
ds@1952
  4928
*/
ds@1952
  4929
int sqlite3PagerNosync(Pager *pPager){
ds@1952
  4930
  return pPager->noSync;
ds@1952
  4931
}
ds@1952
  4932
ds@1952
  4933
#ifdef SQLITE_HAS_CODEC
ds@1952
  4934
/*
ds@1952
  4935
** Set the codec for this pager
ds@1952
  4936
*/
ds@1952
  4937
void sqlite3PagerSetCodec(
ds@1952
  4938
  Pager *pPager,
ds@1952
  4939
  void *(*xCodec)(void*,void*,Pgno,int),
ds@1952
  4940
  void *pCodecArg
ds@1952
  4941
){
ds@1952
  4942
  pPager->xCodec = xCodec;
ds@1952
  4943
  pPager->pCodecArg = pCodecArg;
ds@1952
  4944
}
ds@1952
  4945
#endif
ds@1952
  4946
ds@1952
  4947
#ifndef SQLITE_OMIT_AUTOVACUUM
ds@1952
  4948
/*
ds@1952
  4949
** Move the page pPg to location pgno in the file. 
ds@1952
  4950
**
ds@1952
  4951
** There must be no references to the page previously located at
ds@1952
  4952
** pgno (which we call pPgOld) though that page is allowed to be
ds@1952
  4953
** in cache.  If the page previous located at pgno is not already
ds@1952
  4954
** in the rollback journal, it is not put there by by this routine.
ds@1952
  4955
**
ds@1952
  4956
** References to the page pPg remain valid. Updating any
ds@1952
  4957
** meta-data associated with pPg (i.e. data stored in the nExtra bytes
ds@1952
  4958
** allocated along with the page) is the responsibility of the caller.
ds@1952
  4959
**
ds@1952
  4960
** A transaction must be active when this routine is called. It used to be
ds@1952
  4961
** required that a statement transaction was not active, but this restriction
ds@1952
  4962
** has been removed (CREATE INDEX needs to move a page when a statement
ds@1952
  4963
** transaction is active).
ds@1952
  4964
*/
ds@1952
  4965
int sqlite3PagerMovepage(Pager *pPager, DbPage *pPg, Pgno pgno){
ds@1952
  4966
  PgHdr *pPgOld;  /* The page being overwritten. */
ds@1952
  4967
  int h;
ds@1952
  4968
  Pgno needSyncPgno = 0;
ds@1952
  4969
ds@1952
  4970
  pagerEnter(pPager);
ds@1952
  4971
  assert( pPg->nRef>0 );
ds@1952
  4972
ds@1952
  4973
  PAGERTRACE5("MOVE %d page %d (needSync=%d) moves to %d\n", 
ds@1952
  4974
      PAGERID(pPager), pPg->pgno, pPg->needSync, pgno);
ds@1952
  4975
  IOTRACE(("MOVE %p %d %d\n", pPager, pPg->pgno, pgno))
ds@1952
  4976
ds@1952
  4977
  pager_get_content(pPg);
ds@1952
  4978
  if( pPg->needSync ){
ds@1952
  4979
    needSyncPgno = pPg->pgno;
ds@1952
  4980
    assert( pPg->inJournal || (int)pgno>pPager->origDbSize );
ds@1952
  4981
    assert( pPg->dirty );
ds@1952
  4982
    assert( pPager->needSync );
ds@1952
  4983
  }
ds@1952
  4984
ds@1952
  4985
  /* Unlink pPg from it's hash-chain */
ds@1952
  4986
  unlinkHashChain(pPager, pPg);
ds@1952
  4987
ds@1952
  4988
  /* If the cache contains a page with page-number pgno, remove it
ds@1952
  4989
  ** from it's hash chain. Also, if the PgHdr.needSync was set for 
ds@1952
  4990
  ** page pgno before the 'move' operation, it needs to be retained 
ds@1952
  4991
  ** for the page moved there.
ds@1952
  4992
  */
ds@1952
  4993
  pPg->needSync = 0;
ds@1952
  4994
  pPgOld = pager_lookup(pPager, pgno);
ds@1952
  4995
  if( pPgOld ){
ds@1952
  4996
    assert( pPgOld->nRef==0 );
ds@1952
  4997
    unlinkHashChain(pPager, pPgOld);
ds@1952
  4998
    makeClean(pPgOld);
ds@1952
  4999
    pPg->needSync = pPgOld->needSync;
ds@1952
  5000
  }else{
ds@1952
  5001
    pPg->needSync = 0;
ds@1952
  5002
  }
ds@1952
  5003
  if( pPager->aInJournal && (int)pgno<=pPager->origDbSize ){
ds@1952
  5004
    pPg->inJournal =  (pPager->aInJournal[pgno/8] & (1<<(pgno&7)))!=0;
ds@1952
  5005
  }else{
ds@1952
  5006
    pPg->inJournal = 0;
ds@1952
  5007
    assert( pPg->needSync==0 || (int)pgno>pPager->origDbSize );
ds@1952
  5008
  }
ds@1952
  5009
ds@1952
  5010
  /* Change the page number for pPg and insert it into the new hash-chain. */
ds@1952
  5011
  assert( pgno!=0 );
ds@1952
  5012
  pPg->pgno = pgno;
ds@1952
  5013
  h = pgno & (pPager->nHash-1);
ds@1952
  5014
  if( pPager->aHash[h] ){
ds@1952
  5015
    assert( pPager->aHash[h]->pPrevHash==0 );
ds@1952
  5016
    pPager->aHash[h]->pPrevHash = pPg;
ds@1952
  5017
  }
ds@1952
  5018
  pPg->pNextHash = pPager->aHash[h];
ds@1952
  5019
  pPager->aHash[h] = pPg;
ds@1952
  5020
  pPg->pPrevHash = 0;
ds@1952
  5021
ds@1952
  5022
  makeDirty(pPg);
ds@1952
  5023
  pPager->dirtyCache = 1;
ds@1952
  5024
ds@1952
  5025
  if( needSyncPgno ){
ds@1952
  5026
    /* If needSyncPgno is non-zero, then the journal file needs to be 
ds@1952
  5027
    ** sync()ed before any data is written to database file page needSyncPgno.
ds@1952
  5028
    ** Currently, no such page exists in the page-cache and the 
ds@1952
  5029
    ** Pager.aInJournal bit has been set. This needs to be remedied by loading
ds@1952
  5030
    ** the page into the pager-cache and setting the PgHdr.needSync flag.
ds@1952
  5031
    **
ds@1952
  5032
    ** The sqlite3PagerGet() call may cause the journal to sync. So make
ds@1952
  5033
    ** sure the Pager.needSync flag is set too.
ds@1952
  5034
    */
ds@1952
  5035
    int rc;
ds@1952
  5036
    PgHdr *pPgHdr;
ds@1952
  5037
    assert( pPager->needSync );
ds@1952
  5038
    rc = sqlite3PagerGet(pPager, needSyncPgno, &pPgHdr);
ds@1952
  5039
    if( rc!=SQLITE_OK ) return rc;
ds@1952
  5040
    pPager->needSync = 1;
ds@1952
  5041
    pPgHdr->needSync = 1;
ds@1952
  5042
    pPgHdr->inJournal = 1;
ds@1952
  5043
    makeDirty(pPgHdr);
ds@1952
  5044
    sqlite3PagerUnref(pPgHdr);
ds@1952
  5045
  }
ds@1952
  5046
ds@1952
  5047
  pagerLeave(pPager);
ds@1952
  5048
  return SQLITE_OK;
ds@1952
  5049
}
ds@1952
  5050
#endif
ds@1952
  5051
ds@1952
  5052
/*
ds@1952
  5053
** Return a pointer to the data for the specified page.
ds@1952
  5054
*/
ds@1952
  5055
void *sqlite3PagerGetData(DbPage *pPg){
ds@1952
  5056
  return PGHDR_TO_DATA(pPg);
ds@1952
  5057
}
ds@1952
  5058
ds@1952
  5059
/*
ds@1952
  5060
** Return a pointer to the Pager.nExtra bytes of "extra" space 
ds@1952
  5061
** allocated along with the specified page.
ds@1952
  5062
*/
ds@1952
  5063
void *sqlite3PagerGetExtra(DbPage *pPg){
ds@1952
  5064
  Pager *pPager = pPg->pPager;
ds@1952
  5065
  return (pPager?PGHDR_TO_EXTRA(pPg, pPager):0);
ds@1952
  5066
}
ds@1952
  5067
ds@1952
  5068
/*
ds@1952
  5069
** Get/set the locking-mode for this pager. Parameter eMode must be one
ds@1952
  5070
** of PAGER_LOCKINGMODE_QUERY, PAGER_LOCKINGMODE_NORMAL or 
ds@1952
  5071
** PAGER_LOCKINGMODE_EXCLUSIVE. If the parameter is not _QUERY, then
ds@1952
  5072
** the locking-mode is set to the value specified.
ds@1952
  5073
**
ds@1952
  5074
** The returned value is either PAGER_LOCKINGMODE_NORMAL or
ds@1952
  5075
** PAGER_LOCKINGMODE_EXCLUSIVE, indicating the current (possibly updated)
ds@1952
  5076
** locking-mode.
ds@1952
  5077
*/
ds@1952
  5078
int sqlite3PagerLockingMode(Pager *pPager, int eMode){
ds@1952
  5079
  assert( eMode==PAGER_LOCKINGMODE_QUERY
ds@1952
  5080
            || eMode==PAGER_LOCKINGMODE_NORMAL
ds@1952
  5081
            || eMode==PAGER_LOCKINGMODE_EXCLUSIVE );
ds@1952
  5082
  assert( PAGER_LOCKINGMODE_QUERY<0 );
ds@1952
  5083
  assert( PAGER_LOCKINGMODE_NORMAL>=0 && PAGER_LOCKINGMODE_EXCLUSIVE>=0 );
ds@1952
  5084
  if( eMode>=0 && !pPager->tempFile ){
ds@1952
  5085
    pPager->exclusiveMode = eMode;
ds@1952
  5086
  }
ds@1952
  5087
  return (int)pPager->exclusiveMode;
ds@1952
  5088
}
ds@1952
  5089
ds@1952
  5090
#ifdef SQLITE_DEBUG
ds@1952
  5091
/*
ds@1952
  5092
** Print a listing of all referenced pages and their ref count.
ds@1952
  5093
*/
ds@1952
  5094
void sqlite3PagerRefdump(Pager *pPager){
ds@1952
  5095
  PgHdr *pPg;
ds@1952
  5096
  for(pPg=pPager->pAll; pPg; pPg=pPg->pNextAll){
ds@1952
  5097
    if( pPg->nRef<=0 ) continue;
ds@1952
  5098
    sqlite3DebugPrintf("PAGE %3d addr=%p nRef=%d\n", 
ds@1952
  5099
       pPg->pgno, PGHDR_TO_DATA(pPg), pPg->nRef);
ds@1952
  5100
  }
ds@1952
  5101
}
ds@1952
  5102
#endif
ds@1952
  5103
ds@1952
  5104
#endif /* SQLITE_OMIT_DISKIO */