filesystems.tmpl 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381
  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. !Eblock/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 jbd2_journal_init_inode() call
  130. is for journals stored in filesystem inodes, or the jbd2_journal_init_dev()
  131. call can be used 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 jbd2_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. The journalling layer expects the space for the journal was already
  139. allocated and initialized properly by the userspace tools. When loading the
  140. journal you must call jbd2_journal_load() to process journal contents. If the
  141. client file system detects the journal contents does not need to be processed
  142. (or even need not have valid contents), it may call jbd2_journal_wipe() to
  143. clear the journal contents before calling jbd2_journal_load().
  144. </para>
  145. <para>
  146. Note that jbd2_journal_wipe(..,0) calls jbd2_journal_skip_recovery() for you if
  147. it detects any outstanding transactions in the journal and similarly
  148. jbd2_journal_load() will call jbd2_journal_recover() if necessary. I would
  149. advise reading ext4_load_journal() in fs/ext4/super.c for examples on this
  150. stage.
  151. </para>
  152. <para>
  153. Now you can go ahead and start modifying the underlying
  154. filesystem. Almost.
  155. </para>
  156. <para>
  157. You still need to actually journal your filesystem changes, this
  158. is done by wrapping them into transactions. Additionally you
  159. also need to wrap the modification of each of the buffers
  160. with calls to the journal layer, so it knows what the modifications
  161. you are actually making are. To do this use jbd2_journal_start() which
  162. returns a transaction handle.
  163. </para>
  164. <para>
  165. jbd2_journal_start()
  166. and its counterpart jbd2_journal_stop(), which indicates the end of a
  167. transaction are nestable calls, so you can reenter a transaction if necessary,
  168. but remember you must call jbd2_journal_stop() the same number of times as
  169. jbd2_journal_start() before the transaction is completed (or more accurately
  170. leaves the update phase). Ext4/VFS makes use of this feature to simplify
  171. handling of inode dirtying, quota support, etc.
  172. </para>
  173. <para>
  174. Inside each transaction you need to wrap the modifications to the
  175. individual buffers (blocks). Before you start to modify a buffer you
  176. need to call jbd2_journal_get_{create,write,undo}_access() as appropriate,
  177. this allows the journalling layer to copy the unmodified data if it
  178. needs to. After all the buffer may be part of a previously uncommitted
  179. transaction.
  180. At this point you are at last ready to modify a buffer, and once
  181. you are have done so you need to call jbd2_journal_dirty_{meta,}data().
  182. Or if you've asked for access to a buffer you now know is now longer
  183. required to be pushed back on the device you can call jbd2_journal_forget()
  184. in much the same way as you might have used bforget() in the past.
  185. </para>
  186. <para>
  187. A jbd2_journal_flush() may be called at any time to commit and checkpoint
  188. all your transactions.
  189. </para>
  190. <para>
  191. Then at umount time , in your put_super() you can then call jbd2_journal_destroy()
  192. to clean up your in-core journal object.
  193. </para>
  194. <para>
  195. Unfortunately there a couple of ways the journal layer can cause a deadlock.
  196. The first thing to note is that each task can only have
  197. a single outstanding transaction at any one time, remember nothing
  198. commits until the outermost jbd2_journal_stop(). This means
  199. you must complete the transaction at the end of each file/inode/address
  200. etc. operation you perform, so that the journalling system isn't re-entered
  201. on another journal. Since transactions can't be nested/batched
  202. across differing journals, and another filesystem other than
  203. yours (say ext4) may be modified in a later syscall.
  204. </para>
  205. <para>
  206. The second case to bear in mind is that jbd2_journal_start() can
  207. block if there isn't enough space in the journal for your transaction
  208. (based on the passed nblocks param) - when it blocks it merely(!) needs to
  209. wait for transactions to complete and be committed from other tasks,
  210. so essentially we are waiting for jbd2_journal_stop(). So to avoid
  211. deadlocks you must treat jbd2_journal_start/stop() as if they
  212. were semaphores and include them in your semaphore ordering rules to prevent
  213. deadlocks. Note that jbd2_journal_extend() has similar blocking behaviour to
  214. jbd2_journal_start() so you can deadlock here just as easily as on
  215. jbd2_journal_start().
  216. </para>
  217. <para>
  218. Try to reserve the right number of blocks the first time. ;-). This will
  219. be the maximum number of blocks you are going to touch in this transaction.
  220. I advise having a look at at least ext4_jbd.h to see the basis on which
  221. ext4 uses to make these decisions.
  222. </para>
  223. <para>
  224. Another wriggle to watch out for is your on-disk block allocation strategy.
  225. Why? Because, if you do a delete, you need to ensure you haven't reused any
  226. of the freed blocks until the transaction freeing these blocks commits. If you
  227. reused these blocks and crash happens, there is no way to restore the contents
  228. of the reallocated blocks at the end of the last fully committed transaction.
  229. One simple way of doing this is to mark blocks as free in internal in-memory
  230. block allocation structures only after the transaction freeing them commits.
  231. Ext4 uses journal commit callback for this purpose.
  232. </para>
  233. <para>
  234. With journal commit callbacks you can ask the journalling layer to call a
  235. callback function when the transaction is finally committed to disk, so that
  236. you can do some of your own management. You ask the journalling layer for
  237. calling the callback by simply setting journal->j_commit_callback function
  238. pointer and that function is called after each transaction commit. You can also
  239. use transaction->t_private_list for attaching entries to a transaction that
  240. need processing when the transaction commits.
  241. </para>
  242. <para>
  243. JBD2 also provides a way to block all transaction updates via
  244. jbd2_journal_{un,}lock_updates(). Ext4 uses this when it wants a window with a
  245. clean and stable fs for a moment. E.g.
  246. </para>
  247. <programlisting>
  248. jbd2_journal_lock_updates() //stop new stuff happening..
  249. jbd2_journal_flush() // checkpoint everything.
  250. ..do stuff on stable fs
  251. jbd2_journal_unlock_updates() // carry on with filesystem use.
  252. </programlisting>
  253. <para>
  254. The opportunities for abuse and DOS attacks with this should be obvious,
  255. if you allow unprivileged userspace to trigger codepaths containing these
  256. calls.
  257. </para>
  258. </sect2>
  259. <sect2 id="jbd_summary">
  260. <title>Summary</title>
  261. <para>
  262. Using the journal is a matter of wrapping the different context changes,
  263. being each mount, each modification (transaction) and each changed buffer
  264. to tell the journalling layer about them.
  265. </para>
  266. </sect2>
  267. </sect1>
  268. <sect1 id="data_types">
  269. <title>Data Types</title>
  270. <para>
  271. The journalling layer uses typedefs to 'hide' the concrete definitions
  272. of the structures used. As a client of the JBD2 layer you can
  273. just rely on the using the pointer as a magic cookie of some sort.
  274. Obviously the hiding is not enforced as this is 'C'.
  275. </para>
  276. <sect2 id="structures"><title>Structures</title>
  277. !Iinclude/linux/jbd2.h
  278. </sect2>
  279. </sect1>
  280. <sect1 id="functions">
  281. <title>Functions</title>
  282. <para>
  283. The functions here are split into two groups those that
  284. affect a journal as a whole, and those which are used to
  285. manage transactions
  286. </para>
  287. <sect2 id="journal_level"><title>Journal Level</title>
  288. !Efs/jbd2/journal.c
  289. !Ifs/jbd2/recovery.c
  290. </sect2>
  291. <sect2 id="transaction_level"><title>Transasction Level</title>
  292. !Efs/jbd2/transaction.c
  293. </sect2>
  294. </sect1>
  295. <sect1 id="see_also">
  296. <title>See also</title>
  297. <para>
  298. <citation>
  299. <ulink url="http://kernel.org/pub/linux/kernel/people/sct/ext3/journal-design.ps.gz">
  300. Journaling the Linux ext2fs Filesystem, LinuxExpo 98, Stephen Tweedie
  301. </ulink>
  302. </citation>
  303. </para>
  304. <para>
  305. <citation>
  306. <ulink url="http://olstrans.sourceforge.net/release/OLS2000-ext3/OLS2000-ext3.html">
  307. Ext3 Journalling FileSystem, OLS 2000, Dr. Stephen Tweedie
  308. </ulink>
  309. </citation>
  310. </para>
  311. </sect1>
  312. </chapter>
  313. <chapter id="splice">
  314. <title>splice API</title>
  315. <para>
  316. splice is a method for moving blocks of data around inside the
  317. kernel, without continually transferring them between the kernel
  318. and user space.
  319. </para>
  320. !Ffs/splice.c
  321. </chapter>
  322. <chapter id="pipes">
  323. <title>pipes API</title>
  324. <para>
  325. Pipe interfaces are all for in-kernel (builtin image) use.
  326. They are not exported for use by modules.
  327. </para>
  328. !Iinclude/linux/pipe_fs_i.h
  329. !Ffs/pipe.c
  330. </chapter>
  331. </book>