Debian Codegraph @

www/cksumvfs.html

raw
1 <!DOCTYPE html>
2 <html><head>
3 <meta name="viewport" content="width=device-width, initial-scale=1.0">
4 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
5 <link href="sqlite.css" rel="stylesheet">
6 <title>The Checksum VFS Shim</title>
7 <!-- path= -->
8 </head>
9 <body>
10 <div class=nosearch>
11 <a href="index.html">
12 <img class="logo" src="images/sqlite370_banner.gif" alt="SQLite" border="0">
13 </a>
14 <div><!-- IE hack to prevent disappearing logo --></div>
15 <div class="tagline desktoponly">
16 Small. Fast. Reliable.<br>Choose any three.
17 </div>
18 <div class="menu mainmenu">
19 <ul>
20 <li><a href="index.html">Home</a>
21 <li class='mobileonly'><a href="javascript:void(0)" onclick='toggle_div("submenu")'>Menu</a>
22 <li class='wideonly'><a href='about.html'>About</a>
23 <li class='desktoponly'><a href="docs.html">Documentation</a>
24 <li class='desktoponly'><a href="download.html">Download</a>
25 <li class='wideonly'><a href='copyright.html'>License</a>
26 <li class='desktoponly'><a href="support.html">Support</a>
27 <li class='desktoponly'><a href="prosupport.html">Purchase</a>
28 <li class='search' id='search_menubutton'>
29 <a href="javascript:void(0)" onclick='toggle_search()'>Search</a>
30 </ul>
31 </div>
32 <div class="menu submenu" id="submenu">
33 <ul>
34 <li><a href='about.html'>About</a>
35 <li><a href='docs.html'>Documentation</a>
36 <li><a href='download.html'>Download</a>
37 <li><a href='support.html'>Support</a>
38 <li><a href='prosupport.html'>Purchase</a>
39 </ul>
40 </div>
41 <div class="searchmenu" id="searchmenu">
42 <form method="GET" action="search">
43 <select name="s" id="searchtype">
44 <option value="d">Search Documentation</option>
45 <option value="c">Search Changelog</option>
46 </select>
47 <input type="text" name="q" id="searchbox" value="">
48 <input type="submit" value="Go">
49 </form>
50 </div>
51 </div>
52 <script>
53 function toggle_div(nm) {
54 var w = document.getElementById(nm);
55 if( w.style.display=="block" ){
56 w.style.display = "none";
57 }else{
58 w.style.display = "block";
59 }
60 }
61 function toggle_search() {
62 var w = document.getElementById("searchmenu");
63 if( w.style.display=="block" ){
64 w.style.display = "none";
65 } else {
66 w.style.display = "block";
67 setTimeout(function(){
68 document.getElementById("searchbox").focus()
69 }, 30);
70 }
71 }
72 function div_off(nm){document.getElementById(nm).style.display="none";}
73 window.onbeforeunload = function(e){div_off("submenu");}
74 /* Disable the Search feature if we are not operating from CGI, since */
75 /* Search is accomplished using CGI and will not work without it. */
76 if( !location.origin || !location.origin.match || !location.origin.match(/http/) ){
77 document.getElementById("search_menubutton").style.display = "none";
78 }
79 /* Used by the Hide/Show button beside syntax diagrams, to toggle the */
80 function hideorshow(btn,obj){
81 var x = document.getElementById(obj);
82 var b = document.getElementById(btn);
83 if( x.style.display!='none' ){
84 x.style.display = 'none';
85 b.innerHTML='show';
86 }else{
87 x.style.display = '';
88 b.innerHTML='hide';
89 }
90 return false;
91 }
92 var antiRobot = 0;
93 function antiRobotGo(){
94 if( antiRobot!=3 ) return;
95 antiRobot = 7;
96 var j = document.getElementById("mtimelink");
97 if(j && j.hasAttribute("data-href")) j.href=j.getAttribute("data-href");
98 }
99 function antiRobotDefense(){
100 document.body.onmousedown=function(){
101 antiRobot |= 2;
102 antiRobotGo();
103 document.body.onmousedown=null;
104 }
105 document.body.onmousemove=function(){
106 antiRobot |= 2;
107 antiRobotGo();
108 document.body.onmousemove=null;
109 }
110 setTimeout(function(){
111 antiRobot |= 1;
112 antiRobotGo();
113 }, 100)
114 antiRobotGo();
115 }
116 antiRobotDefense();
117 </script>
118 <div class=fancy>
119 <div class=nosearch>
120 <div class="fancy_title">
121 The Checksum VFS Shim
122 </div>
123 <div class="fancy_toc">
124 <a onclick="toggle_toc()">
125 <span class="fancy_toc_mark" id="toc_mk">&#x25ba;</span>
126 Table Of Contents
127 </a>
128 <div id="toc_sub"><div class="fancy-toc1"><a href="#overview">1. Overview</a></div>
129 <div class="fancy-toc1"><a href="#compiling">2. Compiling</a></div>
130 <div class="fancy-toc1"><a href="#loading">3. Loading</a></div>
131 <div class="fancy-toc1"><a href="#usage">4. Usage</a></div>
132 <div class="fancy-toc1"><a href="#verification_of_checksums">5. Verification Of Checksums</a></div>
133 <div class="fancy-toc1"><a href="#controlling_checksum_verification">6. Controlling Checksum Verification</a></div>
134 </div>
135 </div>
136 <script>
137 function toggle_toc(){
138 var sub = document.getElementById("toc_sub")
139 var mk = document.getElementById("toc_mk")
140 if( sub.style.display!="block" ){
141 sub.style.display = "block";
142 mk.innerHTML = "&#x25bc;";
143 } else {
144 sub.style.display = "none";
145 mk.innerHTML = "&#x25ba;";
146 }
147 }
148 </script>
149 </div>
150
151
152
153
154
155 <h1 id="overview"><span>1. </span>Overview</h1>
156
157 <p>The checksum VFS extension is a <a href="vfs.html#shim">VFS shim</a> that adds an 8-byte
158 checksum to the end of every page in an SQLite database. The checksum
159 is added as each page is written and verified as each page is read.
160 The checksum is intended to help detect database corruption caused by
161 random bit-flips in the mass storage device.
162
163 </p><p>The checksum VFS extension requires
164 SQLite version 3.32.0 (2020-05-22) or later. It will not
165 work with earlier versions of SQLite.
166
167 </p><h1 id="compiling"><span>2. </span>Compiling</h1>
168
169 <p>The checksum VFS module is a <a href="loadext.html">loadable extension</a>. It is not
170 included in the <a href="amalgamation.html">amalgamation</a>. It must be added to SQLite
171 either at compile-time or at run-time. The source code to
172 the checksum VFS module is in the
173 <a href="https://sqlite.org/src/file/ext/misc/cksumvfs.c">ext/misc/cksumvfs.c</a>
174 source file in the
175 <a href="https://sqlite.org/src">SQLite source tree</a>.
176
177 </p><p>To build the checksum VFS module into a run-time loadable
178 extension, use commands similar to the following:
179
180 </p><ul>
181 <li>(linux) &rarr; <tt>gcc -fPIC -shared cksumvfs.c -o cksumvfs.so</tt>
182 </li><li>(mac) &rarr; <tt>clang -fPIC -dynamiclib cksumvfs.c -o cksumvfs.dylib</tt>
183 </li><li>(windows) &rarr; <tt>cl cksumvfs.c -link -dll -out:cksumvfs.dll</tt>
184 </li></ul>
185
186 <p>You may want to add additional compiler options, of course,
187 according to the needs of your project.
188
189 </p><p>To statically link this extension with your product,
190 compile it like any other C-language module but add the
191 "-DSQLITE_CKSUMVFS_STATIC" option so that this module knows that
192 it is being statically linked rather than dynamically linked.
193
194 </p><h1 id="loading"><span>3. </span>Loading</h1>
195
196 <p>To load this extension as a shared library, you first have to
197 bring up a dummy SQLite database connection to use as the argument
198 to the <a href="c3ref/load_extension.html">sqlite3_load_extension()</a> API call. Then you invoke the
199 <a href="c3ref/load_extension.html">sqlite3_load_extension()</a> API and shutdown the dummy database
200 connection. All subsequent database connections that are opened
201 will include this extension. For example:
202
203 </p><div class="codeblock"><pre>sqlite3 *db;
204 sqlite3_open(":memory:", &db);
205 sqlite3_load_extension(db, "./cksumvfs");
206 sqlite3_close(db);
207 </pre></div>
208
209 <p>If this extension is compiled with -DSQLITE_CKSUMVFS_STATIC and
210 statically linked against the application, initialize it using
211 a single API call as follows:
212
213 </p><div class="codeblock"><pre>sqlite3_cksumvfs_init();
214 </pre></div>
215
216 <p>Cksumvfs is a <a href="vfs.html#shim">VFS shim</a>. When loaded, "cksmvfs" becomes the new
217 default VFS, and it uses the prior default VFS as the next VFS
218 down in the stack. This is normally what you want. However, in
219 complex situations where multiple VFS shims are being loaded,
220 it might be important to ensure that cksumvfs is loaded in the
221 correct order so that it sequences itself into the default VFS
222 Shim stack in the right order.
223
224 </p><h1 id="usage"><span>4. </span>Usage</h1>
225
226 <p>Open database connections using the <a href="c3ref/open.html">sqlite3_open()</a> or
227 <a href="c3ref/open.html">sqlite3_open_v2()</a> interfaces, as normal. Ordinary database files
228 (without a checksum) will operate normally. Databases with
229 checksums will return an SQLITE_IOERR_DATA error if a page is
230 encountered that contains an invalid checksum.
231
232 </p><p>Checksumming only works on databases that have a <a href="fileformat2.html#resbyte">reserve bytes</a>
233 value of exactly 8. The default value for reserve-bytes is 0.
234 Hence, newly created database files will omit the checksum by
235 default. To create a database that includes a checksum, change
236 the reserve-bytes value to 8 by running code similar to this:
237
238 </p><div class="codeblock"><pre>int n = 8;
239 sqlite3_file_control(db, 0, SQLITE_FCNTL_RESERVE_BYTES, &n);
240 </pre></div>
241
242 <p>If you do this immediately after creating a new database file,
243 before anything else has been written into the file, then that
244 might be all that you need to do. Otherwise, the API call
245 above should be followed by:
246
247 </p><div class="codeblock"><pre>sqlite3_exec(db, "VACUUM", 0, 0, 0);
248 </pre></div>
249
250 <p>It never hurts to run the VACUUM, even if you don't need it.
251 If the database is in WAL mode, you should shutdown and
252 reopen all database connections before continuing.
253
254 </p><p>From the CLI, use the ".filectrl reserve_bytes 8" command,
255 followed by "VACUUM;".
256
257 </p><p>Note that SQLite allows the number of reserve-bytes to be
258 increased but not decreased. So if a database file already
259 has a reserve-bytes value greater than 8, there is no way to
260 activate checksumming on that database, other than to dump
261 and restore the database file. Note also that other extensions
262 might also make use of the reserve-bytes. Checksumming will
263 be incompatible with those other extensions.
264
265 </p><h1 id="verification_of_checksums"><span>5. </span>Verification Of Checksums</h1>
266
267 <p>If any checksum is incorrect, the "PRAGMA quick_check" command
268 will find it. To verify that checksums are actually enabled
269 and running, use SQL like the following:
270
271 </p><div class="codeblock"><pre>SELECT count(*), verify_checksum(data)
272 FROM sqlite_dbpage
273 GROUP BY 2;
274 </pre></div>
275
276 <p>There are three possible outputs from the verify_checksum()
277 function: 1, 0, and NULL. 1 is returned if the checksum is
278 correct. 0 is returned if the checksum is incorrect. NULL
279 is returned if the page is unreadable. If checksumming is
280 enabled, the read will fail if the checksum is wrong, so the
281 usual result from verify_checksum() on a bad checksum is NULL.
282
283 </p><p>If everything is OK, the query above should return a single
284 row where the second column is 1. Any other result indicates
285 either that there is a checksum error, or checksum validation
286 is disabled.
287
288 </p><h1 id="controlling_checksum_verification"><span>6. </span>Controlling Checksum Verification</h1>
289
290 <p>The cksumvfs extension implements a new PRAGMA statement that can
291 be used to disable, re-enable, or query the status of checksum
292 verification:
293
294 </p><div class="codeblock"><pre>PRAGMA checksum_verification; -- query status
295 PRAGMA checksum_verification=OFF; -- disable verification
296 PRAGMA checksum_verification=ON; -- re-enable verification
297 </pre></div>
298
299 <p>The "checksum_verification" pragma will return "1" (true) or "0"
300 (false) if checksum verification is enabled or disabled, respectively.
301 "Verification" in this context means the feature that causes
302 SQLITE_IOERR_DATA errors if a checksum mismatch is detected while
303 reading. Checksums are always kept up-to-date as long as the
304 <a href="fileformat2.html#resbyte">reserve bytes</a> value of the database is 8, regardless of the setting
305 of this pragma. Checksum verification can be disabled (for example)
306 to do forensic analysis of a database that has previously reported
307 a checksum error.
308
309 </p><p>The "checksum_verification" pragma will always respond with "0" if
310 the database file does not have a <a href="fileformat2.html#resbyte">reserve bytes</a> value of 8. The
311 pragma will return no rows at all if the cksumvfs extension is
312 not loaded.
313 </p><p align="center"><small><i>This page last modified on <a href="https://sqlite.org/docsrc/honeypot" id="mtimelink" data-href="https://sqlite.org/docsrc/finfo/pages/cksumvfs.in?m=c9b2514ff0">2022-01-08 05:02:57</a> UTC </small></i></p>
314