filesystems.tmpl 13 KB

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