filesystems.tmpl 13 KB

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