Documentation Source Text

Check-in [5654d9aebd]
Login

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

Overview
Comment:Initial work on documentation of the VFS.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: 5654d9aebdbc1eda14cbc79cf0edd59f504a1a57
User & Date: drh 2011-05-20 20:41:41.072
Context
2011-05-20
23:21
Further tightening of the validity rules for valid HTTP requests in althttpd.c. (check-in: 73bc374298 user: drh tags: trunk)
20:41
Initial work on documentation of the VFS. (check-in: 5654d9aebd user: drh tags: trunk)
2011-05-19
17:14
Merge the 3.7.6.3 changes into trunk. (check-in: 6800ff0968 user: drh tags: trunk)
Changes
Unified Diff Ignore Whitespace Patch
Added images/vfs1.gif.

cannot compute difference between binary files

Added pages/vfs.in.






















































































































































































































































































































































































































































































































































































































>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
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
<title>The OS Backend (VFS) To SQLite</title>
<tcl>hd_keywords VFS VFSes {OS backend}</tcl>

<h1 align="center">
The SQLite OS Interface or "VFS"
</h1>

<p>
This article describes the SQLite OS portability layer or "VFS" - the
thin module that is at the bottom of the SQLite implementation stack
and which provides portability across operating systems.
</p>

<img src="images/vfs1.gif" align="right" hspace="10">
<h2>1.0 The VFS In Relation To The Rest Of SQLite</h2>

<p>
The internal organization of the SQLite library can be viewed as a
stack of modules such as shown to the right.
The Tokenizer, Parser, and Code Generator components are used to
process SQL statements and convert them into executable programs 
in a virtual machine language or byte code.
Roughly speaking, these top three layers implement
[sqlite3_prepare_v2()].  The byte code generated by the top three
layers is a [prepared statement].
The Virtual Machine module is responsible for running the SQL statement 
byte code. The B-Tree module organizes a database file into multiple 
key/value stores with ordered keys and logarithmic performance. 
The Pager module is responsible for loading pages of the database
file into memory, for implementing and controlling transactions, and 
for creating and maintaining the journal files that prevent database 
corruption following a crash or power failure. 
The OS Interface is a thin abstraction that provides a common set of 
routines for adapting SQLite to run on different operating systems.
Roughly speaking, the bottom four layers implement
[sqlite3_step()].
</p>

<p>
This article is about the bottom layer.
</p>

<p>The OS Interface - also called the "VFS" - is what makes SQLite 
portable across operating systems.  Whenever any of the other modules
in SQLite needs to communicate with the filesystem, or the operating
system, they invoke methods in the VFS to communicate with the 
operating system on their behalf.  Hence, porting SQLite to a new
operating system is simply a matter of writing a new OS interface layer
or "VFS".</p>

<h2>2.0 Multiple VFSes</h2>

<p>
The standard SQLite source tree contains built-in VFSes for os/2, unix,
and windows.  Alternative VFSes can be
added at start-time or run-time using the
[sqlite3_vfs_register()] interface.
</p>

<p>
Multiple VFSes can be registered at the same time.
Each VFS has a unique names.
Separate [database connections] within the same process can be using
different VFSes at the same time.   For that matter, if a single
database connection has multiple database files open using
the [ATTACH] command, then each database might be using a
different VFS.
</p>

<p>
Unix builds come with multiple VFSes built-in.  The default VFS
for unix is called "unix" and is what is used in an overwhelming
majority of applications.  Other VFSes that can be found in unix
may include:
</p>

<ol>
<li><p><b>unix-dotfile</b> - uses dot-file locking rather than
          POSIX advisory locks.
<li><p><b>unix-excl</b> - obtains and holds an exclusive lock on
          database files, preventing other processes from accessing the
          database.  Also keeps the [wal-index] in heap rather than in
          shared memory.
<li><p><b>unix-none</b> - all file locking operations are no-ops.
<li><p><b>unix-namedsem</b> - uses named semaphores for file locking.
       VXWorks only.
</ol>

<p>
The various unix VFSes differ only in the way they handle file locking -
they share most of the implementation in common with one another and
are all located in the same SQLite source file:  "<tt>os_unix.c</tt>".
Note that because they do use different locking strategies, they are
subtle imcompatible with one another.  If two processes are accessing
the same SQLite database using different unix VFSes, they will probably
not see each others locks and may end up interfering with one another,
resulting in database corruption.  The "unix-none" VFS in particular
does no locking at all and will easily result in database corruption if
used to access a database by two or more database connections at a time.
</p>

<h2>2.1 Specifying Which VFS To Use</h2>

<p>
There is always one VFS which is the default VFS.  On unix systems,
the "unix" VFS comes up as the default and on windows it is "win32".
If no other actions are taken, new database connections will make use
of the default VFS.
</p>

<p>
The default VFS can be changed by registering or re-registering the
VFS using the [sqlite3_vfs_register()] interface with a second parameter
of 1.  Hence, of a (unix) process to make the "unix-nolock" VFS the
default in place of "unix", the following code would work:
</p>

<blockquote><pre>
sqlite3_vfs_register(sqlite3_vfs_find("unix-nolock"), 1);
</pre></blockquote>

<p>
An alternate VFS can also be specified as the 4th parameter to the
[sqlite3_open_v2()] function.  For example:
</p>

<blockquote><pre>
int rc = sqlite3_open_v2("demo.db", &db, SQLITE_OPEN_READWRITE, "unix-nolock");
</pre></blockquote>

<p>
Finally, if [URI filenames] have been enabled, then the alternative
VFS can be specified using the "vfs=" parameter on the URI.  This technique
works with [sqlite3_open()], [sqlite3_open16()], [sqlite3_open_v2()], and
when a new database is [ATTACH]-ed to an existing database connection.
For example:
</p>

<blockquote><pre>
ATTACH 'file:demo2.db?vfs=unix-none' AS demo2;
</pre></blockquote>

<p>
The VFS specified by a URI has the highest priority.  After that comes
a VFS specified as the fourth argument to [sqlite3_open_v2()].  The
default VFS is used if no VFS is specified otherwise.
</p>

<h2>2.2 VFS Shims</h2>

<p>
From the point of view of the uppers layers of the SQLite stack, there
is only one VFS in use at a time.  But in practice, a particular VFS might
just be a thin wrapper around another VFS does the real work.
We call a wrapper VFS a "shim".
</p>

<p>
A simple example of a shim is the "vfstrace" VFS.  This is a VFS
(imnplemented in the 
[http://www.sqlite.org/src/finfo?name=src/test_vfstrace.c | test_vfstrace.c]
source file) that writes a message associated with each VFS method call
into a log file, then passes control off to another VFS to do the actual
work.
</p>

<h2>2.3 Other Example VFSes</h2>

<p>
The following are other VFS implementations available in the public
SQLite source tree:
</p>

<ul>
<li><p>
<b>test_demovfs.c</b> -
This file implements a very simple VFS named "demo" that uss POSIX 
functions such as
open(), read(), write(), fsync(), close(), fsync(), sleep(), time(),
and so forth.  This VFS only works on unix systems.  But it is not
intended as a replacement for the standard "unix" VFS used by default
on unix platforms.  The "demo" VFS is deliberately kept very simple
so that it can be used as a learning aid or as template for building
other VFSes or for porting SQLite to new operating systems.

<li><p>
<b>test_quota.c</b> -
This file implements a shim called "quota" that enforces cumulative
file size limits on a collection of database files.  An auxiliary
interface is used to define "quote groups".  A quota group is a
set of files (database files, journals, and temporary files) whose
names all match a [GLOB] pattern.  The sum of the sizes of all files
in each quota group is tracked, and if that sum exceeds a threshold
defined for the quota group, a callback function is invoked.  That
callback can either increase the threshold or cause the operation
that would have exceeded the quota to fail with an 
[SQLITE_FULL] error.  One of the uses of this shim is used to enforce 
resource limits on application databases in Firefox.

<li><p>
<b>test_multiplex.c</b> -
This file implements a shim that allows database files to exceed the
maximum file size of the underlying filesystem.  This shim presents
an interface to the upper six layers of SQLite that makes it look like
very large files are being used, when in reality each such large file
is split up into many smaller files on the underlying system.
This shim has been used, for example, to allow databases to grow
larger than 2 gibibytes on FAT16 filesystems.

<li><p>
<b>test_onefile.c</b> -
This file implements a demonstration VFS named "fs" that shows how SQLite 
can be used on an embedded device that lacks a filesystem.  Content is
written directly to the underlying media.  A VFS derived from this
demonstration code could be used by a gadget with a limited amount of
flash memory to make SQLite behave as the filesystem for the flash memory
on the device.

<li><p>
<b>test_journal.c</b> -
This file implements a shim used during SQLite testing that verifies that
the database and rollback journal are written in the correct order and
are "synced" at appropriate times in order to guarantee that the database
can recover from a power lose are hard reset at any time.  The shim
checks several invariants on the operation of databases and rollback
journals and raises exceptions if any of those invariants are violated.
These invariants, in turn, assure that the database is always recoverable.
Running a large suite of test cases using this shim provides added
assurance that SQLite databases will not be damaged by unexpected
power failures or device resets.

<li><p>
<b>test_vfs.c</b> -
This file implements a shim that can be used to simulate filesystem faults.
This shim is used during testing to verify that SQLite responses sanely
to hardware malfunctions or to other error conditions such as running out
of filesystem space that are difficult to test on a real system.
</ul>

<p>
There are other VFS inplementations both in the core SQLite source code
library and in available extensions.  The list above is not meant to be
exhaustive but merely representative of the kinds of features that can
be realized using the VFS interface.
</p>

<h2>3.0 VFS Implementations</h2>

<p>
A new VFS is implemented by subclassing three objects:
</p>

<ul>
<li>[sqlite3_vfs]
<li>[sqlite3_io_methods]
<li>[sqlite3_file]
</ul>

<p>
An [sqlite3_vfs] object defines the name of the VFS and the core
methods that implement the interface to the operating system, such
as checking for existance of files, deleting files, creating files
and opening and for reading and/or writing, converting filenames
into their canonical form.  The [sqlite3_vfs] object also contains
methods for obtaining randomness from the operating system, for
suspending a process (sleeping) and for finding the current date and
time.
</p>

<p>
The [sqlite3_file] object represents an open file.
The xOpen method of [sqlite3_vfs] constructs an [sqlite3_file]
object when the file is opened.  The [sqlite3_file] keeps track
of the state of the file while it is opened.
</p>

<p>
The [sqlite3_io_methods] object holds the methods used to interact
with an open file.  Each [sqlite3_file] contains a pointer to 
an [sqlite3_io_methods] object that is appropriate for the file it
represents.  The [sqlite3_io_methods] object contains methods to do
things such as read and write from the file, to truncate the file,
to flush any changes to persistent storage, to find the size of the
file, to lock and unlock the file, and to close file and destroy
the [sqlite3_file] object.
</p>

<p>
Writing the code for a new VFS involves constructing a subclass for
the [sqlite3_vfs] object and then registering that VFS object using
a call to [sqlite3_vfs_register()].  The VFS implementation also
provides subclasses for [sqlite3_file] and [sqlite3_io_methods] but
those objects are not registered directly with SQLite.  Instead, the
[sqlite3_file] object is returned from the xOpen method of
[sqlite3_vfs] and the [sqlite3_file] object points to an instance
of the [sqlite3_io_methods] object.
</p>

<h2>To Be Continued...</h2>