blob: f51f28531b8d48d72a4775938c6eb43098af746a [file] [log] [blame]
Randy Dunlapbbb5bbb2006-10-29 22:46:40 -08001<?xml version="1.0" encoding="UTF-8"?>
2<!DOCTYPE book PUBLIC "-//OASIS//DTD DocBook XML V4.1.2//EN"
3 "http://www.oasis-open.org/docbook/xml/4.1.2/docbookx.dtd" []>
4
5<book id="Linux-filesystems-API">
6 <bookinfo>
7 <title>Linux Filesystems API</title>
8
9 <legalnotice>
10 <para>
11 This documentation is free software; you can redistribute
12 it and/or modify it under the terms of the GNU General Public
13 License as published by the Free Software Foundation; either
14 version 2 of the License, or (at your option) any later
15 version.
16 </para>
17
18 <para>
19 This program is distributed in the hope that it will be
20 useful, but WITHOUT ANY WARRANTY; without even the implied
21 warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
22 See the GNU General Public License for more details.
23 </para>
24
25 <para>
26 You should have received a copy of the GNU General Public
27 License along with this program; if not, write to the Free
28 Software Foundation, Inc., 59 Temple Place, Suite 330, Boston,
29 MA 02111-1307 USA
30 </para>
31
32 <para>
33 For more details see the file COPYING in the source
34 distribution of Linux.
35 </para>
36 </legalnotice>
37 </bookinfo>
38
39<toc></toc>
40
41 <chapter id="vfs">
42 <title>The Linux VFS</title>
Rob Landley5c3b4472007-10-16 23:31:31 -070043 <sect1 id="the_filesystem_types"><title>The Filesystem types</title>
Randy Dunlapbbb5bbb2006-10-29 22:46:40 -080044!Iinclude/linux/fs.h
45 </sect1>
Rob Landley5c3b4472007-10-16 23:31:31 -070046 <sect1 id="the_directory_cache"><title>The Directory Cache</title>
Randy Dunlapbbb5bbb2006-10-29 22:46:40 -080047!Efs/dcache.c
48!Iinclude/linux/dcache.h
49 </sect1>
Rob Landley5c3b4472007-10-16 23:31:31 -070050 <sect1 id="inode_handling"><title>Inode Handling</title>
Randy Dunlapbbb5bbb2006-10-29 22:46:40 -080051!Efs/inode.c
52!Efs/bad_inode.c
53 </sect1>
Rob Landley5c3b4472007-10-16 23:31:31 -070054 <sect1 id="registration_and_superblocks"><title>Registration and Superblocks</title>
Randy Dunlapbbb5bbb2006-10-29 22:46:40 -080055!Efs/super.c
56 </sect1>
Rob Landley5c3b4472007-10-16 23:31:31 -070057 <sect1 id="file_locks"><title>File Locks</title>
Randy Dunlapbbb5bbb2006-10-29 22:46:40 -080058!Efs/locks.c
59!Ifs/locks.c
60 </sect1>
Rob Landley5c3b4472007-10-16 23:31:31 -070061 <sect1 id="other_functions"><title>Other Functions</title>
Randy Dunlapbbb5bbb2006-10-29 22:46:40 -080062!Efs/mpage.c
63!Efs/namei.c
64!Efs/buffer.c
65!Efs/bio.c
66!Efs/seq_file.c
67!Efs/filesystems.c
68!Efs/fs-writeback.c
69!Efs/block_dev.c
70 </sect1>
71 </chapter>
72
73 <chapter id="proc">
74 <title>The proc filesystem</title>
75
Rob Landley5c3b4472007-10-16 23:31:31 -070076 <sect1 id="sysctl_interface"><title>sysctl interface</title>
Randy Dunlapbbb5bbb2006-10-29 22:46:40 -080077!Ekernel/sysctl.c
78 </sect1>
79
Rob Landley5c3b4472007-10-16 23:31:31 -070080 <sect1 id="proc_filesystem_interface"><title>proc filesystem interface</title>
Randy Dunlapbbb5bbb2006-10-29 22:46:40 -080081!Ifs/proc/base.c
82 </sect1>
83 </chapter>
84
Randy Dunlap36182182011-02-20 20:08:35 -080085 <chapter id="fs_events">
86 <title>Events based on file descriptors</title>
87!Efs/eventfd.c
88 </chapter>
89
Randy Dunlapbbb5bbb2006-10-29 22:46:40 -080090 <chapter id="sysfs">
91 <title>The Filesystem for Exporting Kernel Objects</title>
92!Efs/sysfs/file.c
93!Efs/sysfs/symlink.c
94!Efs/sysfs/bin.c
95 </chapter>
96
97 <chapter id="debugfs">
98 <title>The debugfs filesystem</title>
99
Rob Landley5c3b4472007-10-16 23:31:31 -0700100 <sect1 id="debugfs_interface"><title>debugfs interface</title>
Randy Dunlapbbb5bbb2006-10-29 22:46:40 -0800101!Efs/debugfs/inode.c
102!Efs/debugfs/file.c
103 </sect1>
104 </chapter>
105
Randy Dunlap733b72c2006-11-02 22:07:02 -0800106 <chapter id="LinuxJDBAPI">
107 <chapterinfo>
108 <title>The Linux Journalling API</title>
109
110 <authorgroup>
111 <author>
112 <firstname>Roger</firstname>
113 <surname>Gammans</surname>
114 <affiliation>
115 <address>
116 <email>rgammans@computer-surgery.co.uk</email>
117 </address>
118 </affiliation>
119 </author>
120 </authorgroup>
121
122 <authorgroup>
123 <author>
124 <firstname>Stephen</firstname>
125 <surname>Tweedie</surname>
126 <affiliation>
127 <address>
128 <email>sct@redhat.com</email>
129 </address>
130 </affiliation>
131 </author>
132 </authorgroup>
133
134 <copyright>
135 <year>2002</year>
136 <holder>Roger Gammans</holder>
137 </copyright>
138 </chapterinfo>
139
140 <title>The Linux Journalling API</title>
141
Rob Landley5c3b4472007-10-16 23:31:31 -0700142 <sect1 id="journaling_overview">
Randy Dunlap733b72c2006-11-02 22:07:02 -0800143 <title>Overview</title>
Rob Landley5c3b4472007-10-16 23:31:31 -0700144 <sect2 id="journaling_details">
Randy Dunlap733b72c2006-11-02 22:07:02 -0800145 <title>Details</title>
146<para>
147The journalling layer is easy to use. You need to
148first of all create a journal_t data structure. There are
149two calls to do this dependent on how you decide to allocate the physical
150media on which the journal resides. The journal_init_inode() call
151is for journals stored in filesystem inodes, or the journal_init_dev()
152call can be use for journal stored on a raw device (in a continuous range
153of blocks). A journal_t is a typedef for a struct pointer, so when
154you are finally finished make sure you call journal_destroy() on it
155to free up any used kernel memory.
156</para>
157
158<para>
159Once you have got your journal_t object you need to 'mount' or load the journal
160file, unless of course you haven't initialised it yet - in which case you
161need to call journal_create().
162</para>
163
164<para>
165Most of the time however your journal file will already have been created, but
166before you load it you must call journal_wipe() to empty the journal file.
167Hang on, you say , what if the filesystem wasn't cleanly umount()'d . Well, it is the
168job of the client file system to detect this and skip the call to journal_wipe().
169</para>
170
171<para>
172In either case the next call should be to journal_load() which prepares the
173journal file for use. Note that journal_wipe(..,0) calls journal_skip_recovery()
174for you if it detects any outstanding transactions in the journal and similarly
175journal_load() will call journal_recover() if necessary.
176I would advise reading fs/ext3/super.c for examples on this stage.
177[RGG: Why is the journal_wipe() call necessary - doesn't this needlessly
178complicate the API. Or isn't a good idea for the journal layer to hide
179dirty mounts from the client fs]
180</para>
181
182<para>
183Now you can go ahead and start modifying the underlying
184filesystem. Almost.
185</para>
186
187<para>
188
189You still need to actually journal your filesystem changes, this
190is done by wrapping them into transactions. Additionally you
191also need to wrap the modification of each of the buffers
192with calls to the journal layer, so it knows what the modifications
193you are actually making are. To do this use journal_start() which
194returns a transaction handle.
195</para>
196
197<para>
198journal_start()
199and its counterpart journal_stop(), which indicates the end of a transaction
200are nestable calls, so you can reenter a transaction if necessary,
201but remember you must call journal_stop() the same number of times as
202journal_start() before the transaction is completed (or more accurately
203leaves the update phase). Ext3/VFS makes use of this feature to simplify
204quota support.
205</para>
206
207<para>
208Inside each transaction you need to wrap the modifications to the
209individual buffers (blocks). Before you start to modify a buffer you
210need to call journal_get_{create,write,undo}_access() as appropriate,
211this allows the journalling layer to copy the unmodified data if it
212needs to. After all the buffer may be part of a previously uncommitted
213transaction.
214At this point you are at last ready to modify a buffer, and once
215you are have done so you need to call journal_dirty_{meta,}data().
216Or if you've asked for access to a buffer you now know is now longer
217required to be pushed back on the device you can call journal_forget()
218in much the same way as you might have used bforget() in the past.
219</para>
220
221<para>
222A journal_flush() may be called at any time to commit and checkpoint
223all your transactions.
224</para>
225
226<para>
227Then at umount time , in your put_super() (2.4) or write_super() (2.5)
228you can then call journal_destroy() to clean up your in-core journal object.
229</para>
230
231<para>
232Unfortunately there a couple of ways the journal layer can cause a deadlock.
233The first thing to note is that each task can only have
234a single outstanding transaction at any one time, remember nothing
235commits until the outermost journal_stop(). This means
236you must complete the transaction at the end of each file/inode/address
237etc. operation you perform, so that the journalling system isn't re-entered
238on another journal. Since transactions can't be nested/batched
239across differing journals, and another filesystem other than
240yours (say ext3) may be modified in a later syscall.
241</para>
242
243<para>
244The second case to bear in mind is that journal_start() can
245block if there isn't enough space in the journal for your transaction
246(based on the passed nblocks param) - when it blocks it merely(!) needs to
247wait for transactions to complete and be committed from other tasks,
248so essentially we are waiting for journal_stop(). So to avoid
249deadlocks you must treat journal_start/stop() as if they
250were semaphores and include them in your semaphore ordering rules to prevent
251deadlocks. Note that journal_extend() has similar blocking behaviour to
252journal_start() so you can deadlock here just as easily as on journal_start().
253</para>
254
255<para>
256Try to reserve the right number of blocks the first time. ;-). This will
257be the maximum number of blocks you are going to touch in this transaction.
258I advise having a look at at least ext3_jbd.h to see the basis on which
259ext3 uses to make these decisions.
260</para>
261
262<para>
263Another wriggle to watch out for is your on-disk block allocation strategy.
264why? Because, if you undo a delete, you need to ensure you haven't reused any
265of the freed blocks in a later transaction. One simple way of doing this
266is make sure any blocks you allocate only have checkpointed transactions
267listed against them. Ext3 does this in ext3_test_allocatable().
268</para>
269
270<para>
271Lock is also providing through journal_{un,}lock_updates(),
272ext3 uses this when it wants a window with a clean and stable fs for a moment.
273eg.
274</para>
275
276<programlisting>
277
278 journal_lock_updates() //stop new stuff happening..
279 journal_flush() // checkpoint everything.
280 ..do stuff on stable fs
281 journal_unlock_updates() // carry on with filesystem use.
282</programlisting>
283
284<para>
285The opportunities for abuse and DOS attacks with this should be obvious,
286if you allow unprivileged userspace to trigger codepaths containing these
287calls.
288</para>
289
290<para>
291A new feature of jbd since 2.5.25 is commit callbacks with the new
292journal_callback_set() function you can now ask the journalling layer
293to call you back when the transaction is finally committed to disk, so that
294you can do some of your own management. The key to this is the journal_callback
295struct, this maintains the internal callback information but you can
296extend it like this:-
297</para>
298<programlisting>
299 struct myfs_callback_s {
300 //Data structure element required by jbd..
301 struct journal_callback for_jbd;
302 // Stuff for myfs allocated together.
303 myfs_inode* i_commited;
304
305 }
306</programlisting>
307
308<para>
309this would be useful if you needed to know when data was committed to a
310particular inode.
311</para>
312
313 </sect2>
314
Rob Landley5c3b4472007-10-16 23:31:31 -0700315 <sect2 id="jbd_summary">
Randy Dunlap733b72c2006-11-02 22:07:02 -0800316 <title>Summary</title>
317<para>
318Using the journal is a matter of wrapping the different context changes,
319being each mount, each modification (transaction) and each changed buffer
320to tell the journalling layer about them.
321</para>
322
323<para>
324Here is a some pseudo code to give you an idea of how it works, as
325an example.
326</para>
327
328<programlisting>
329 journal_t* my_jnrl = journal_create();
330 journal_init_{dev,inode}(jnrl,...)
331 if (clean) journal_wipe();
332 journal_load();
333
334 foreach(transaction) { /*transactions must be
335 completed before
336 a syscall returns to
337 userspace*/
338
339 handle_t * xct=journal_start(my_jnrl);
340 foreach(bh) {
341 journal_get_{create,write,undo}_access(xact,bh);
342 if ( myfs_modify(bh) ) { /* returns true
343 if makes changes */
344 journal_dirty_{meta,}data(xact,bh);
345 } else {
346 journal_forget(bh);
347 }
348 }
349 journal_stop(xct);
350 }
351 journal_destroy(my_jrnl);
352</programlisting>
353 </sect2>
354
355 </sect1>
356
Rob Landley5c3b4472007-10-16 23:31:31 -0700357 <sect1 id="data_types">
Randy Dunlap733b72c2006-11-02 22:07:02 -0800358 <title>Data Types</title>
359 <para>
360 The journalling layer uses typedefs to 'hide' the concrete definitions
361 of the structures used. As a client of the JBD layer you can
362 just rely on the using the pointer as a magic cookie of some sort.
363
364 Obviously the hiding is not enforced as this is 'C'.
365 </para>
Rob Landley5c3b4472007-10-16 23:31:31 -0700366 <sect2 id="structures"><title>Structures</title>
Randy Dunlap733b72c2006-11-02 22:07:02 -0800367!Iinclude/linux/jbd.h
368 </sect2>
369 </sect1>
370
Rob Landley5c3b4472007-10-16 23:31:31 -0700371 <sect1 id="functions">
Randy Dunlap733b72c2006-11-02 22:07:02 -0800372 <title>Functions</title>
373 <para>
374 The functions here are split into two groups those that
375 affect a journal as a whole, and those which are used to
376 manage transactions
377 </para>
Rob Landley5c3b4472007-10-16 23:31:31 -0700378 <sect2 id="journal_level"><title>Journal Level</title>
Randy Dunlap733b72c2006-11-02 22:07:02 -0800379!Efs/jbd/journal.c
380!Ifs/jbd/recovery.c
381 </sect2>
Rob Landley5c3b4472007-10-16 23:31:31 -0700382 <sect2 id="transaction_level"><title>Transasction Level</title>
Randy Dunlap733b72c2006-11-02 22:07:02 -0800383!Efs/jbd/transaction.c
384 </sect2>
385 </sect1>
Rob Landley5c3b4472007-10-16 23:31:31 -0700386 <sect1 id="see_also">
Randy Dunlap733b72c2006-11-02 22:07:02 -0800387 <title>See also</title>
388 <para>
389 <citation>
390 <ulink url="ftp://ftp.uk.linux.org/pub/linux/sct/fs/jfs/journal-design.ps.gz">
391 Journaling the Linux ext2fs Filesystem, LinuxExpo 98, Stephen Tweedie
392 </ulink>
393 </citation>
394 </para>
395 <para>
396 <citation>
397 <ulink url="http://olstrans.sourceforge.net/release/OLS2000-ext3/OLS2000-ext3.html">
398 Ext3 Journalling FileSystem, OLS 2000, Dr. Stephen Tweedie
399 </ulink>
400 </citation>
401 </para>
402 </sect1>
403
404 </chapter>
405
Randy Dunlap073b86d2008-02-13 15:03:23 -0800406 <chapter id="splice">
407 <title>splice API</title>
408 <para>
409 splice is a method for moving blocks of data around inside the
410 kernel, without continually transferring them between the kernel
411 and user space.
412 </para>
413!Ffs/splice.c
414 </chapter>
415
416 <chapter id="pipes">
417 <title>pipes API</title>
418 <para>
419 Pipe interfaces are all for in-kernel (builtin image) use.
420 They are not exported for use by modules.
421 </para>
422!Iinclude/linux/pipe_fs_i.h
423!Ffs/pipe.c
424 </chapter>
425
Randy Dunlapbbb5bbb2006-10-29 22:46:40 -0800426</book>