/ Check-in [f5d26380]
Login

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

Overview
Comment:Update the quota shim so that when the same file is opened multiple times, its size only counts against the quota once.
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | experimental
Files: files | file ages | folders
SHA1: f5d263803084107389c5541face8d536b62fffe3
User & Date: drh 2010-09-01 12:50:54
Context
2010-09-01
13:09
Clean up comments in the test_quota.c source file. check-in: c1eec7db user: drh tags: experimental
12:50
Update the quota shim so that when the same file is opened multiple times, its size only counts against the quota once. check-in: f5d26380 user: drh tags: experimental
11:40
Add file test_quota.c, demonstrating how file-system quotas may be implemented as a VFS wrapper. check-in: 383eb87b user: dan tags: experimental
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to src/test_quota.c.

29
30
31
32
33
34
35





36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61

62
63
64
















65
66
67
68
69
70
71
72
73
74

75
76
77
78
79
80
81
...
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
...
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257

258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
















285

286
287

288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311


312


313


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

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

375
376
377

378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398

399
400
401
402
403
404
405
406

407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
...
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
...
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
*/
#include "sqlite3.h"
#include <string.h>
#include <assert.h>

/************************ Object Definitions ******************************/






/*
** This module contains a table of filename patterns that have size
** quotas.  The quota applies to the sum of the sizes of all open
** database files whose names match the GLOB pattern.
**
** Each quota is an instance of the following object.  Quotas must
** be established (using sqlite3_quota_set()) prior to opening any
** of the database connections that access files governed by the
** quota.
**
** Each entry in the quota table is an instance of the following object.
*/
typedef struct quotaGroup quotaGroup;
struct quotaGroup {
  const char *zPattern;          /* Filename pattern to be quotaed */
  sqlite3_int64 iLimit;          /* Upper bound on total file size */
  sqlite3_int64 iSize;           /* Current size of all files */
  void (*xCallback)(             /* Callback invoked when going over quota */
     const char *zFilename,         /* Name of file whose size increases */
     sqlite3_int64 *piLimit,        /* IN/OUT: The current limit */
     sqlite3_int64 iSize,           /* Total size of all files in the group */
     void *pArg                     /* Client data */
  );
  void *pArg;                    /* Third argument to the xCallback() */
  int nRef;                      /* Number of files in the group references. */
  quotaGroup *pNext, **ppPrev;   /* Doubly linked list of all quota objects */

};

/*
















** An instance of the following object represents each file that
** participates in quota tracking.  The sqlite3_file object for the
** underlying VFS is appended to this structure.
*/
typedef struct quotaFile quotaFile;
struct quotaFile {
  sqlite3_file base;              /* Base class - must be first */
  const char *zFilename;          /* Name of this file */
  quotaGroup *pGroup;             /* Upper bound on file size */
  sqlite3_int64 iSize;            /* Current size of this file */

  /* The underlying VFS sqlite3_file is appended to this object */
};

/************************* Global Variables **********************************/
/*
** All global variables used by this file are containing within the following
** gQuota structure.
................................................................................
static void quotaLeave(void){ sqlite3_mutex_leave(gQuota.pMutex); }


/* If the reference count and threshold for a quotaGroup are both
** zero, then destroy the quotaGroup.
*/
static void quotaGroupDeref(quotaGroup *p){
  if( p->nRef==0 && p->iLimit==0 ){
    if( p->pNext ) p->pNext->ppPrev = p->ppPrev;
    if( p->ppPrev ) *p->ppPrev = p->pNext;
    sqlite3_free(p);
  }
}

/*
................................................................................
  }
  return *z==0;
}


/* Find a quotaGroup given the filename.
**
** Return a pointer to the quotaFile object. Return NULL if not found.
*/
static quotaGroup *quotaGroupFind(const char *zFilename){
  quotaGroup *p;
  for(p=gQuota.pGroup; p && strglob(p->zPattern, zFilename)==0; p=p->pNext){}
  return p;
}

/* Translate an sqlite3_file* that is really a quotaFile* into
** an sqlite3_file* for the underlying original VFS.
*/
static sqlite3_file *quotaSubFile(sqlite3_file *pFile){
  quotaFile *p = (quotaFile*)pFile;
  return (sqlite3_file*)&p[1];
}

/************************* VFS Method Wrappers *****************************/
/*
** This is the xOpen method used for the "quota" VFS.
**
** Most of the work is done by the underlying original VFS.  This method
** simply links the new file into the quota group if it is a file that
** needs to be tracked.
*/
static int quotaOpen(
  sqlite3_vfs *pVfs,
  const char *zName,
  sqlite3_file *pFile,
  int flags,
  int *pOutFlags
){
  int rc;

  quotaFile *pQuotaFile;
  quotaGroup *pGroup;
  sqlite3_file *pSubFile;
  sqlite3_vfs *pOrigVfs = gQuota.pOrigVfs;

  /* If the file is not a main database file or a WAL, then use the
  ** normal xOpen method.
  */
  if( (flags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_WAL))==0 ){
    return pOrigVfs->xOpen(pOrigVfs, zName, pFile, flags, pOutFlags);
  }

  /* If the name of the file does not match any quota group, then
  ** use the normal xOpen method.
  */
  quotaEnter();
  pGroup = quotaGroupFind(zName);
  if( pGroup==0 ){
    rc = pOrigVfs->xOpen(pOrigVfs, zName, pFile, flags, pOutFlags);
  }else{
    /* If we get to this point, it means the file needs to be quota tracked.
    */
    pQuotaFile = (quotaFile*)pFile;
    pSubFile = quotaSubFile(pFile);
    rc = pOrigVfs->xOpen(pOrigVfs, zName, pSubFile, flags, pOutFlags);
    if( rc==SQLITE_OK ){
      pQuotaFile->iSize = 0;
















      pQuotaFile->pGroup = pGroup;

      pGroup->nRef++;
      pQuotaFile->zFilename = zName;

      if( pSubFile->pMethods->iVersion==1 ){
        pQuotaFile->base.pMethods = &gQuota.sIoMethodsV1;
      }else{
        pQuotaFile->base.pMethods = &gQuota.sIoMethodsV2;
      }
    }
  }
  quotaLeave();
  return rc;
}

/************************ I/O Method Wrappers *******************************/

/* xClose requests get passed through to the original VFS.  But we
** also have to unlink the quotaFile from the quotaGroup.
*/
static int quotaClose(sqlite3_file *pFile){
  quotaFile *p = (quotaFile*)pFile;
  quotaGroup *pGroup = p->pGroup;
  sqlite3_file *pSubFile = quotaSubFile(pFile);
  int rc;
  rc = pSubFile->pMethods->xClose(pSubFile);
  quotaEnter();
  pGroup->nRef--;


  pGroup->iSize -= p->iSize;


  quotaGroupDeref(pGroup);


  quotaLeave();
  return rc;
}

/* Pass xRead requests directory thru to the original VFS without
** further processing.
*/
static int quotaRead(
  sqlite3_file *pFile,
  void *pBuf,
  int iAmt,
  sqlite3_int64 iOfst
){
  sqlite3_file *pSubFile = quotaSubFile(pFile);
  return pSubFile->pMethods->xRead(pSubFile, pBuf, iAmt, iOfst);
}

/* Check xWrite requests to see if they expand the file.  If they do,
** the perform a quota check before passing them through to the
** original VFS.
*/
static int quotaWrite(
  sqlite3_file *pFile,
  const void *pBuf,
  int iAmt,
  sqlite3_int64 iOfst
){
  quotaFile *p = (quotaFile*)pFile;
  sqlite3_file *pSubFile = quotaSubFile(pFile);
  sqlite3_int64 iEnd = iOfst+iAmt;
  quotaGroup *pGroup;

  sqlite3_int64 szNew;

  if( p->iSize<iEnd ){
    pGroup = p->pGroup;
    quotaEnter();
    szNew = pGroup->iSize - p->iSize + iEnd;
    if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
      if( pGroup->xCallback ){
        pGroup->xCallback(p->zFilename, &pGroup->iLimit, szNew, 
                          pGroup->pArg);
      }
      if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
        quotaLeave();
        return SQLITE_FULL;
      }
    }
    pGroup->iSize = szNew;
    p->iSize = iEnd;
    quotaLeave();
  }
  return pSubFile->pMethods->xWrite(pSubFile, pBuf, iAmt, iOfst);
}

/* Pass xTruncate requests thru to the original VFS.  If the
** success, update the file size.
*/
static int quotaTruncate(sqlite3_file *pFile, sqlite3_int64 size){
  quotaFile *p = (quotaFile*)pFile;
  sqlite3_file *pSubFile = quotaSubFile(pFile);
  int rc = pSubFile->pMethods->xTruncate(pSubFile, size);

  quotaGroup *pGroup = p->pGroup;
  if( rc==SQLITE_OK ){
    quotaEnter();

    pGroup->iSize -= p->iSize;
    p->iSize = size;
    pGroup->iSize += size;
    quotaLeave();
  }
  return rc;
}

/* Pass xSync requests through to the original VFS without change
*/
static int quotaSync(sqlite3_file *pFile, int flags){
  sqlite3_file *pSubFile = quotaSubFile(pFile);
  return pSubFile->pMethods->xSync(pSubFile, flags);
}

/* Pass xFileSize requests through to the original VFS but then
** update the quotaGroup with the new size before returning.
*/
static int quotaFileSize(sqlite3_file *pFile, sqlite3_int64 *pSize){
  quotaFile *p = (quotaFile*)pFile;
  sqlite3_file *pSubFile = quotaSubFile(pFile);

  quotaGroup *pGroup;
  sqlite3_int64 sz;
  int rc;

  rc = pSubFile->pMethods->xFileSize(pSubFile, &sz);
  if( rc==SQLITE_OK ){
    pGroup = p->pGroup;
    quotaEnter();

    pGroup->iSize -= p->iSize;
    p->iSize = sz;
    pGroup->iSize += sz;
    quotaLeave();
    *pSize = sz;
  }
  return rc;
}

/* Pass xLock requests through to the original VFS unchanged.
*/
static int quotaLock(sqlite3_file *pFile, int lock){
  sqlite3_file *pSubFile = quotaSubFile(pFile);
  return pSubFile->pMethods->xLock(pSubFile, lock);
}

/* Pass xUnlock requests through to the original VFS unchanged.
*/
static int quotaUnlock(sqlite3_file *pFile, int lock){
  sqlite3_file *pSubFile = quotaSubFile(pFile);
  return pSubFile->pMethods->xUnlock(pSubFile, lock);
}

/* Pass xCheckReservedLock requests through to the original VFS unchanged.
*/
static int quotaCheckReservedLock(sqlite3_file *pFile, int *pResOut){
  sqlite3_file *pSubFile = quotaSubFile(pFile);
  return pSubFile->pMethods->xCheckReservedLock(pSubFile, pResOut);
}

/* Pass xFileControl requests through to the original VFS unchanged.
*/
static int quotaFileControl(sqlite3_file *pFile, int op, void *pArg){
  sqlite3_file *pSubFile = quotaSubFile(pFile);
  return pSubFile->pMethods->xFileControl(pSubFile, op, pArg);
}

/* Pass xSectorSize requests through to the original VFS unchanged.
*/
static int quotaSectorSize(sqlite3_file *pFile){
  sqlite3_file *pSubFile = quotaSubFile(pFile);
  return pSubFile->pMethods->xSectorSize(pSubFile);
}

/* Pass xDeviceCharacteristics requests through to the original VFS unchanged.
*/
static int quotaDeviceCharacteristics(sqlite3_file *pFile){
  sqlite3_file *pSubFile = quotaSubFile(pFile);
  return pSubFile->pMethods->xDeviceCharacteristics(pSubFile);
}

/* Pass xShmMap requests through to the original VFS unchanged.
*/
static int quotaShmMap(
  sqlite3_file *pFile,            /* Handle open on database file */
  int iRegion,                    /* Region to retrieve */
  int szRegion,                   /* Size of regions */
  int bExtend,                    /* True to extend file if necessary */
  void volatile **pp              /* OUT: Mapped memory */
){
  sqlite3_file *pSubFile = quotaSubFile(pFile);
  return pSubFile->pMethods->xShmMap(pSubFile, iRegion, szRegion, bExtend, pp);
}

/* Pass xShmLock requests through to the original VFS unchanged.
*/
static int quotaShmLock(
  sqlite3_file *pFile,       /* Database file holding the shared memory */
  int ofst,                  /* First lock to acquire or release */
  int n,                     /* Number of locks to acquire or release */
  int flags                  /* What to do with the lock */
){
  sqlite3_file *pSubFile = quotaSubFile(pFile);
  return pSubFile->pMethods->xShmLock(pSubFile, ofst, n, flags);
}

/* Pass xShmBarrier requests through to the original VFS unchanged.
*/
static void quotaShmBarrier(sqlite3_file *pFile){
  sqlite3_file *pSubFile = quotaSubFile(pFile);
  pSubFile->pMethods->xShmBarrier(pSubFile);
}

/* Pass xShmUnmap requests through to the original VFS unchanged.
*/
static int quotaShmUnmap(sqlite3_file *pFile, int deleteFlag){
  sqlite3_file *pSubFile = quotaSubFile(pFile);
  return pSubFile->pMethods->xShmUnmap(pSubFile, deleteFlag);
}

/************************** Public Interfaces *****************************/
/*
** Initialize the quota VFS shim.  Use the VFS named zOrigVfsName
** as the VFS that does the actual work.  Use the default if
** zOrigVfsName==NULL.  
................................................................................
  gQuota.pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
  if( !gQuota.pMutex ){
    return SQLITE_NOMEM;
  }
  gQuota.isInitialized = 1;
  gQuota.pOrigVfs = pOrigVfs;
  gQuota.sThisVfs = *pOrigVfs;
  gQuota.sThisVfs.xOpen = quotaOpen;
  gQuota.sThisVfs.szOsFile += sizeof(quotaFile);
  gQuota.sThisVfs.zName = "quota";
  gQuota.sIoMethodsV1.iVersion = 1;
  gQuota.sIoMethodsV1.xClose = quotaClose;
  gQuota.sIoMethodsV1.xRead = quotaRead;
  gQuota.sIoMethodsV1.xWrite = quotaWrite;
  gQuota.sIoMethodsV1.xTruncate = quotaTruncate;
  gQuota.sIoMethodsV1.xSync = quotaSync;
  gQuota.sIoMethodsV1.xFileSize = quotaFileSize;
  gQuota.sIoMethodsV1.xLock = quotaLock;
  gQuota.sIoMethodsV1.xUnlock = quotaUnlock;
  gQuota.sIoMethodsV1.xCheckReservedLock = quotaCheckReservedLock;
  gQuota.sIoMethodsV1.xFileControl = quotaFileControl;
  gQuota.sIoMethodsV1.xSectorSize = quotaSectorSize;
  gQuota.sIoMethodsV1.xDeviceCharacteristics = quotaDeviceCharacteristics;
  gQuota.sIoMethodsV2 = gQuota.sIoMethodsV1;
  gQuota.sIoMethodsV2.iVersion = 2;
  gQuota.sIoMethodsV2.xShmMap = quotaShmMap;
  gQuota.sIoMethodsV2.xShmLock = quotaShmLock;
  gQuota.sIoMethodsV2.xShmBarrier = quotaShmBarrier;
................................................................................
** THIS ROUTINE IS NOT THREADSAFE.  Call this routine exactly one while
** shutting down in order to free all remaining quota groups.
*/
int sqlite3_quota_shutdown(void){
  quotaGroup *p;
  if( gQuota.isInitialized==0 ) return SQLITE_MISUSE;
  for(p=gQuota.pGroup; p; p=p->pNext){
    if( p->nRef ) return SQLITE_MISUSE;
  }
  while( gQuota.pGroup ){
    quotaGroup *p = gQuota.pGroup;
    gQuota.pGroup = p->pNext;
    sqlite3_free(p);
  }
  gQuota.isInitialized = 0;







>
>
>
>
>












<











<

>



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

<
|

<
<
<
>







 







|







 







|







|


|
|











|


|




>
|

|






|








|



|
|
|

|
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
|
>
|
<
>
|
|

|










|

|
|
|
|

|

|
>
>
|
>
>
|
>
>








|




|
|







|




|
|


>


|
|

|


|








|


|





|
|
|
|
>
|


>
|
|








|
|
|





|
|
|
>




|

<

>
|
|









|
|
|




|
|
|




|
|
|




|
|
|




|
|
|




|
|
|





|





|
|





|




|
|




|
|
|




|
|
|







 







|
|











|







 







|







29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52

53
54
55
56
57
58
59
60
61
62
63

64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88

89
90



91
92
93
94
95
96
97
98
...
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
...
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321

322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449

450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
...
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
...
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
*/
#include "sqlite3.h"
#include <string.h>
#include <assert.h>

/************************ Object Definitions ******************************/

/* Forward declaration of all object types */
typedef struct quotaGroup quotaGroup;
typedef struct quotaOpen quotaOpen;
typedef struct quotaFile quotaFile;

/*
** This module contains a table of filename patterns that have size
** quotas.  The quota applies to the sum of the sizes of all open
** database files whose names match the GLOB pattern.
**
** Each quota is an instance of the following object.  Quotas must
** be established (using sqlite3_quota_set()) prior to opening any
** of the database connections that access files governed by the
** quota.
**
** Each entry in the quota table is an instance of the following object.
*/

struct quotaGroup {
  const char *zPattern;          /* Filename pattern to be quotaed */
  sqlite3_int64 iLimit;          /* Upper bound on total file size */
  sqlite3_int64 iSize;           /* Current size of all files */
  void (*xCallback)(             /* Callback invoked when going over quota */
     const char *zFilename,         /* Name of file whose size increases */
     sqlite3_int64 *piLimit,        /* IN/OUT: The current limit */
     sqlite3_int64 iSize,           /* Total size of all files in the group */
     void *pArg                     /* Client data */
  );
  void *pArg;                    /* Third argument to the xCallback() */

  quotaGroup *pNext, **ppPrev;   /* Doubly linked list of all quota objects */
  quotaFile *pFile;              /* Files within this group */
};

/*
** An instance of this structure represents a single file that is part
** of a quota group.  A single file can be opened multiple times.  In
** order keep multiple openings of the same file from causing the size
** of the file counting against the quota multiple times, each file
** has a unique instance of this object and multiple open connections
** to the same file each point to a single instance of this object.
*/
struct quotaFile {
  char *zFilename;                /* Name of this file */
  quotaGroup *pGroup;             /* Upper bound on file size */
  sqlite3_int64 iSize;            /* Current size of this file */
  int nRef;                       /* Number of times this file is open */
  quotaFile *pNext, **ppPrev;     /* Linked list of files in the same group */
};

/*
** An instance of the following object represents each open connection
** to a file that participates in quota tracking.  The sqlite3_file object
** for the underlying VFS is appended to this structure.
*/

struct quotaOpen {
  sqlite3_file base;              /* Base class - must be first */



  quotaFile *pFile;               /* The underlying file */
  /* The underlying VFS sqlite3_file is appended to this object */
};

/************************* Global Variables **********************************/
/*
** All global variables used by this file are containing within the following
** gQuota structure.
................................................................................
static void quotaLeave(void){ sqlite3_mutex_leave(gQuota.pMutex); }


/* If the reference count and threshold for a quotaGroup are both
** zero, then destroy the quotaGroup.
*/
static void quotaGroupDeref(quotaGroup *p){
  if( p->pFile==0 && p->iLimit==0 ){
    if( p->pNext ) p->pNext->ppPrev = p->ppPrev;
    if( p->ppPrev ) *p->ppPrev = p->pNext;
    sqlite3_free(p);
  }
}

/*
................................................................................
  }
  return *z==0;
}


/* Find a quotaGroup given the filename.
**
** Return a pointer to the quotaOpen object. Return NULL if not found.
*/
static quotaGroup *quotaGroupFind(const char *zFilename){
  quotaGroup *p;
  for(p=gQuota.pGroup; p && strglob(p->zPattern, zFilename)==0; p=p->pNext){}
  return p;
}

/* Translate an sqlite3_file* that is really a quotaOpen* into
** an sqlite3_file* for the underlying original VFS.
*/
static sqlite3_file *quotaSubOpen(sqlite3_file *pOpen){
  quotaOpen *p = (quotaOpen*)pOpen;
  return (sqlite3_file*)&p[1];
}

/************************* VFS Method Wrappers *****************************/
/*
** This is the xOpen method used for the "quota" VFS.
**
** Most of the work is done by the underlying original VFS.  This method
** simply links the new file into the quota group if it is a file that
** needs to be tracked.
*/
static int quotaxOpen(
  sqlite3_vfs *pVfs,
  const char *zName,
  sqlite3_file *pOpen,
  int flags,
  int *pOutFlags
){
  int rc;
  quotaOpen *pQuotaOpen;
  quotaFile *pFile;
  quotaGroup *pGroup;
  sqlite3_file *pSubOpen;
  sqlite3_vfs *pOrigVfs = gQuota.pOrigVfs;

  /* If the file is not a main database file or a WAL, then use the
  ** normal xOpen method.
  */
  if( (flags & (SQLITE_OPEN_MAIN_DB|SQLITE_OPEN_WAL))==0 ){
    return pOrigVfs->xOpen(pOrigVfs, zName, pOpen, flags, pOutFlags);
  }

  /* If the name of the file does not match any quota group, then
  ** use the normal xOpen method.
  */
  quotaEnter();
  pGroup = quotaGroupFind(zName);
  if( pGroup==0 ){
    rc = pOrigVfs->xOpen(pOrigVfs, zName, pOpen, flags, pOutFlags);
  }else{
    /* If we get to this point, it means the file needs to be quota tracked.
    */
    pQuotaOpen = (quotaOpen*)pOpen;
    pSubOpen = quotaSubOpen(pOpen);
    rc = pOrigVfs->xOpen(pOrigVfs, zName, pSubOpen, flags, pOutFlags);
    if( rc==SQLITE_OK ){
      for(pFile=pGroup->pFile; pFile && strcmp(pFile->zFilename, zName);
          pFile=pFile->pNext){}
      if( pFile==0 ){
        int nName = strlen(zName);
        pFile = sqlite3_malloc( sizeof(*pFile) + nName + 1 );
        if( pFile==0 ){
          quotaLeave();
          pSubOpen->pMethods->xClose(pSubOpen);
          return SQLITE_NOMEM;
        }
        memset(pFile, 0, sizeof(*pFile));
        pFile->zFilename = (char*)&pFile[1];
        memcpy(pFile->zFilename, zName, nName+1);
        pFile->pNext = pGroup->pFile;
        if( pGroup->pFile ) pGroup->pFile->ppPrev = &pFile->pNext;
        pFile->ppPrev = &pGroup->pFile;
        pGroup->pFile = pFile;
        pFile->pGroup = pGroup;
      }
      pFile->nRef++;

      pQuotaOpen->pFile = pFile;
      if( pSubOpen->pMethods->iVersion==1 ){
        pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV1;
      }else{
        pQuotaOpen->base.pMethods = &gQuota.sIoMethodsV2;
      }
    }
  }
  quotaLeave();
  return rc;
}

/************************ I/O Method Wrappers *******************************/

/* xClose requests get passed through to the original VFS.  But we
** also have to unlink the quotaOpen from the quotaGroup.
*/
static int quotaClose(sqlite3_file *pOpen){
  quotaOpen *p = (quotaOpen*)pOpen;
  quotaFile *pFile = p->pFile;
  sqlite3_file *pSubOpen = quotaSubOpen(pOpen);
  int rc;
  rc = pSubOpen->pMethods->xClose(pSubOpen);
  quotaEnter();
  pFile->nRef--;
  if( pFile->nRef==0 ){
    quotaGroup *pGroup = pFile->pGroup;
    pGroup->iSize -= pFile->iSize;
    if( pFile->pNext ) pFile->pNext->ppPrev = pFile->ppPrev;
    *pFile->ppPrev = pFile->pNext;
    quotaGroupDeref(pGroup);
    sqlite3_free(pFile);
  }
  quotaLeave();
  return rc;
}

/* Pass xRead requests directory thru to the original VFS without
** further processing.
*/
static int quotaRead(
  sqlite3_file *pOpen,
  void *pBuf,
  int iAmt,
  sqlite3_int64 iOfst
){
  sqlite3_file *pSubOpen = quotaSubOpen(pOpen);
  return pSubOpen->pMethods->xRead(pSubOpen, pBuf, iAmt, iOfst);
}

/* Check xWrite requests to see if they expand the file.  If they do,
** the perform a quota check before passing them through to the
** original VFS.
*/
static int quotaWrite(
  sqlite3_file *pOpen,
  const void *pBuf,
  int iAmt,
  sqlite3_int64 iOfst
){
  quotaOpen *p = (quotaOpen*)pOpen;
  sqlite3_file *pSubOpen = quotaSubOpen(pOpen);
  sqlite3_int64 iEnd = iOfst+iAmt;
  quotaGroup *pGroup;
  quotaFile *pFile = p->pFile;
  sqlite3_int64 szNew;

  if( pFile->iSize<iEnd ){
    pGroup = pFile->pGroup;
    quotaEnter();
    szNew = pGroup->iSize - pFile->iSize + iEnd;
    if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
      if( pGroup->xCallback ){
        pGroup->xCallback(pFile->zFilename, &pGroup->iLimit, szNew, 
                          pGroup->pArg);
      }
      if( szNew>pGroup->iLimit && pGroup->iLimit>0 ){
        quotaLeave();
        return SQLITE_FULL;
      }
    }
    pGroup->iSize = szNew;
    pFile->iSize = iEnd;
    quotaLeave();
  }
  return pSubOpen->pMethods->xWrite(pSubOpen, pBuf, iAmt, iOfst);
}

/* Pass xTruncate requests thru to the original VFS.  If the
** success, update the file size.
*/
static int quotaTruncate(sqlite3_file *pOpen, sqlite3_int64 size){
  quotaOpen *p = (quotaOpen*)pOpen;
  sqlite3_file *pSubOpen = quotaSubOpen(pOpen);
  int rc = pSubOpen->pMethods->xTruncate(pSubOpen, size);
  quotaFile *pFile = p->pFile;
  quotaGroup *pGroup;
  if( rc==SQLITE_OK ){
    quotaEnter();
    pGroup = pFile->pGroup;
    pGroup->iSize -= pFile->iSize;
    pFile->iSize = size;
    pGroup->iSize += size;
    quotaLeave();
  }
  return rc;
}

/* Pass xSync requests through to the original VFS without change
*/
static int quotaSync(sqlite3_file *pOpen, int flags){
  sqlite3_file *pSubOpen = quotaSubOpen(pOpen);
  return pSubOpen->pMethods->xSync(pSubOpen, flags);
}

/* Pass xFileSize requests through to the original VFS but then
** update the quotaGroup with the new size before returning.
*/
static int quotaFileSize(sqlite3_file *pOpen, sqlite3_int64 *pSize){
  quotaOpen *p = (quotaOpen*)pOpen;
  sqlite3_file *pSubOpen = quotaSubOpen(pOpen);
  quotaFile *pFile = p->pFile;
  quotaGroup *pGroup;
  sqlite3_int64 sz;
  int rc;

  rc = pSubOpen->pMethods->xFileSize(pSubOpen, &sz);
  if( rc==SQLITE_OK ){

    quotaEnter();
    pGroup = pFile->pGroup;
    pGroup->iSize -= pFile->iSize;
    pFile->iSize = sz;
    pGroup->iSize += sz;
    quotaLeave();
    *pSize = sz;
  }
  return rc;
}

/* Pass xLock requests through to the original VFS unchanged.
*/
static int quotaLock(sqlite3_file *pOpen, int lock){
  sqlite3_file *pSubOpen = quotaSubOpen(pOpen);
  return pSubOpen->pMethods->xLock(pSubOpen, lock);
}

/* Pass xUnlock requests through to the original VFS unchanged.
*/
static int quotaUnlock(sqlite3_file *pOpen, int lock){
  sqlite3_file *pSubOpen = quotaSubOpen(pOpen);
  return pSubOpen->pMethods->xUnlock(pSubOpen, lock);
}

/* Pass xCheckReservedLock requests through to the original VFS unchanged.
*/
static int quotaCheckReservedLock(sqlite3_file *pOpen, int *pResOut){
  sqlite3_file *pSubOpen = quotaSubOpen(pOpen);
  return pSubOpen->pMethods->xCheckReservedLock(pSubOpen, pResOut);
}

/* Pass xFileControl requests through to the original VFS unchanged.
*/
static int quotaOpenControl(sqlite3_file *pOpen, int op, void *pArg){
  sqlite3_file *pSubOpen = quotaSubOpen(pOpen);
  return pSubOpen->pMethods->xFileControl(pSubOpen, op, pArg);
}

/* Pass xSectorSize requests through to the original VFS unchanged.
*/
static int quotaSectorSize(sqlite3_file *pOpen){
  sqlite3_file *pSubOpen = quotaSubOpen(pOpen);
  return pSubOpen->pMethods->xSectorSize(pSubOpen);
}

/* Pass xDeviceCharacteristics requests through to the original VFS unchanged.
*/
static int quotaDeviceCharacteristics(sqlite3_file *pOpen){
  sqlite3_file *pSubOpen = quotaSubOpen(pOpen);
  return pSubOpen->pMethods->xDeviceCharacteristics(pSubOpen);
}

/* Pass xShmMap requests through to the original VFS unchanged.
*/
static int quotaShmMap(
  sqlite3_file *pOpen,            /* Handle open on database file */
  int iRegion,                    /* Region to retrieve */
  int szRegion,                   /* Size of regions */
  int bExtend,                    /* True to extend file if necessary */
  void volatile **pp              /* OUT: Mapped memory */
){
  sqlite3_file *pSubOpen = quotaSubOpen(pOpen);
  return pSubOpen->pMethods->xShmMap(pSubOpen, iRegion, szRegion, bExtend, pp);
}

/* Pass xShmLock requests through to the original VFS unchanged.
*/
static int quotaShmLock(
  sqlite3_file *pOpen,       /* Database file holding the shared memory */
  int ofst,                  /* First lock to acquire or release */
  int n,                     /* Number of locks to acquire or release */
  int flags                  /* What to do with the lock */
){
  sqlite3_file *pSubOpen = quotaSubOpen(pOpen);
  return pSubOpen->pMethods->xShmLock(pSubOpen, ofst, n, flags);
}

/* Pass xShmBarrier requests through to the original VFS unchanged.
*/
static void quotaShmBarrier(sqlite3_file *pOpen){
  sqlite3_file *pSubOpen = quotaSubOpen(pOpen);
  pSubOpen->pMethods->xShmBarrier(pSubOpen);
}

/* Pass xShmUnmap requests through to the original VFS unchanged.
*/
static int quotaShmUnmap(sqlite3_file *pOpen, int deleteFlag){
  sqlite3_file *pSubOpen = quotaSubOpen(pOpen);
  return pSubOpen->pMethods->xShmUnmap(pSubOpen, deleteFlag);
}

/************************** Public Interfaces *****************************/
/*
** Initialize the quota VFS shim.  Use the VFS named zOrigVfsName
** as the VFS that does the actual work.  Use the default if
** zOrigVfsName==NULL.  
................................................................................
  gQuota.pMutex = sqlite3_mutex_alloc(SQLITE_MUTEX_FAST);
  if( !gQuota.pMutex ){
    return SQLITE_NOMEM;
  }
  gQuota.isInitialized = 1;
  gQuota.pOrigVfs = pOrigVfs;
  gQuota.sThisVfs = *pOrigVfs;
  gQuota.sThisVfs.xOpen = quotaxOpen;
  gQuota.sThisVfs.szOsFile += sizeof(quotaOpen);
  gQuota.sThisVfs.zName = "quota";
  gQuota.sIoMethodsV1.iVersion = 1;
  gQuota.sIoMethodsV1.xClose = quotaClose;
  gQuota.sIoMethodsV1.xRead = quotaRead;
  gQuota.sIoMethodsV1.xWrite = quotaWrite;
  gQuota.sIoMethodsV1.xTruncate = quotaTruncate;
  gQuota.sIoMethodsV1.xSync = quotaSync;
  gQuota.sIoMethodsV1.xFileSize = quotaFileSize;
  gQuota.sIoMethodsV1.xLock = quotaLock;
  gQuota.sIoMethodsV1.xUnlock = quotaUnlock;
  gQuota.sIoMethodsV1.xCheckReservedLock = quotaCheckReservedLock;
  gQuota.sIoMethodsV1.xFileControl = quotaOpenControl;
  gQuota.sIoMethodsV1.xSectorSize = quotaSectorSize;
  gQuota.sIoMethodsV1.xDeviceCharacteristics = quotaDeviceCharacteristics;
  gQuota.sIoMethodsV2 = gQuota.sIoMethodsV1;
  gQuota.sIoMethodsV2.iVersion = 2;
  gQuota.sIoMethodsV2.xShmMap = quotaShmMap;
  gQuota.sIoMethodsV2.xShmLock = quotaShmLock;
  gQuota.sIoMethodsV2.xShmBarrier = quotaShmBarrier;
................................................................................
** THIS ROUTINE IS NOT THREADSAFE.  Call this routine exactly one while
** shutting down in order to free all remaining quota groups.
*/
int sqlite3_quota_shutdown(void){
  quotaGroup *p;
  if( gQuota.isInitialized==0 ) return SQLITE_MISUSE;
  for(p=gQuota.pGroup; p; p=p->pNext){
    if( p->pFile ) return SQLITE_MISUSE;
  }
  while( gQuota.pGroup ){
    quotaGroup *p = gQuota.pGroup;
    gQuota.pGroup = p->pNext;
    sqlite3_free(p);
  }
  gQuota.isInitialized = 0;