filesystems.tmpl 13 KB

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