index.rst 9.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314
  1. =====================
  2. Linux Filesystems API
  3. =====================
  4. The Linux VFS
  5. =============
  6. The Filesystem types
  7. --------------------
  8. .. kernel-doc:: include/linux/fs.h
  9. :internal:
  10. The Directory Cache
  11. -------------------
  12. .. kernel-doc:: fs/dcache.c
  13. :export:
  14. .. kernel-doc:: include/linux/dcache.h
  15. :internal:
  16. Inode Handling
  17. --------------
  18. .. kernel-doc:: fs/inode.c
  19. :export:
  20. .. kernel-doc:: fs/bad_inode.c
  21. :export:
  22. Registration and Superblocks
  23. ----------------------------
  24. .. kernel-doc:: fs/super.c
  25. :export:
  26. File Locks
  27. ----------
  28. .. kernel-doc:: fs/locks.c
  29. :export:
  30. .. kernel-doc:: fs/locks.c
  31. :internal:
  32. Other Functions
  33. ---------------
  34. .. kernel-doc:: fs/mpage.c
  35. :export:
  36. .. kernel-doc:: fs/namei.c
  37. :export:
  38. .. kernel-doc:: fs/buffer.c
  39. :export:
  40. .. kernel-doc:: block/bio.c
  41. :export:
  42. .. kernel-doc:: fs/seq_file.c
  43. :export:
  44. .. kernel-doc:: fs/filesystems.c
  45. :export:
  46. .. kernel-doc:: fs/fs-writeback.c
  47. :export:
  48. .. kernel-doc:: fs/block_dev.c
  49. :export:
  50. The proc filesystem
  51. ===================
  52. sysctl interface
  53. ----------------
  54. .. kernel-doc:: kernel/sysctl.c
  55. :export:
  56. proc filesystem interface
  57. -------------------------
  58. .. kernel-doc:: fs/proc/base.c
  59. :internal:
  60. Events based on file descriptors
  61. ================================
  62. .. kernel-doc:: fs/eventfd.c
  63. :export:
  64. The Filesystem for Exporting Kernel Objects
  65. ===========================================
  66. .. kernel-doc:: fs/sysfs/file.c
  67. :export:
  68. .. kernel-doc:: fs/sysfs/symlink.c
  69. :export:
  70. The debugfs filesystem
  71. ======================
  72. debugfs interface
  73. -----------------
  74. .. kernel-doc:: fs/debugfs/inode.c
  75. :export:
  76. .. kernel-doc:: fs/debugfs/file.c
  77. :export:
  78. The Linux Journalling API
  79. =========================
  80. Overview
  81. --------
  82. Details
  83. ~~~~~~~
  84. The journalling layer is easy to use. You need to first of all create a
  85. journal_t data structure. There are two calls to do this dependent on
  86. how you decide to allocate the physical media on which the journal
  87. resides. The jbd2_journal_init_inode() call is for journals stored in
  88. filesystem inodes, or the jbd2_journal_init_dev() call can be used
  89. for journal stored on a raw device (in a continuous range of blocks). A
  90. journal_t is a typedef for a struct pointer, so when you are finally
  91. finished make sure you call jbd2_journal_destroy() on it to free up
  92. any used kernel memory.
  93. Once you have got your journal_t object you need to 'mount' or load the
  94. journal file. The journalling layer expects the space for the journal
  95. was already allocated and initialized properly by the userspace tools.
  96. When loading the journal you must call jbd2_journal_load() to process
  97. journal contents. If the client file system detects the journal contents
  98. does not need to be processed (or even need not have valid contents), it
  99. may call jbd2_journal_wipe() to clear the journal contents before
  100. calling jbd2_journal_load().
  101. Note that jbd2_journal_wipe(..,0) calls
  102. jbd2_journal_skip_recovery() for you if it detects any outstanding
  103. transactions in the journal and similarly jbd2_journal_load() will
  104. call jbd2_journal_recover() if necessary. I would advise reading
  105. ext4_load_journal() in fs/ext4/super.c for examples on this stage.
  106. Now you can go ahead and start modifying the underlying filesystem.
  107. Almost.
  108. You still need to actually journal your filesystem changes, this is done
  109. by wrapping them into transactions. Additionally you also need to wrap
  110. the modification of each of the buffers with calls to the journal layer,
  111. so it knows what the modifications you are actually making are. To do
  112. this use jbd2_journal_start() which returns a transaction handle.
  113. jbd2_journal_start() and its counterpart jbd2_journal_stop(), which
  114. indicates the end of a transaction are nestable calls, so you can
  115. reenter a transaction if necessary, but remember you must call
  116. jbd2_journal_stop() the same number of times as jbd2_journal_start()
  117. before the transaction is completed (or more accurately leaves the
  118. update phase). Ext4/VFS makes use of this feature to simplify handling
  119. of inode dirtying, quota support, etc.
  120. Inside each transaction you need to wrap the modifications to the
  121. individual buffers (blocks). Before you start to modify a buffer you
  122. need to call jbd2_journal_get_{create,write,undo}_access() as
  123. appropriate, this allows the journalling layer to copy the unmodified
  124. data if it needs to. After all the buffer may be part of a previously
  125. uncommitted transaction. At this point you are at last ready to modify a
  126. buffer, and once you are have done so you need to call
  127. jbd2_journal_dirty_{meta,}data(). Or if you've asked for access to a
  128. buffer you now know is now longer required to be pushed back on the
  129. device you can call jbd2_journal_forget() in much the same way as you
  130. might have used bforget() in the past.
  131. A jbd2_journal_flush() may be called at any time to commit and
  132. checkpoint all your transactions.
  133. Then at umount time , in your put_super() you can then call
  134. jbd2_journal_destroy() to clean up your in-core journal object.
  135. Unfortunately there a couple of ways the journal layer can cause a
  136. deadlock. The first thing to note is that each task can only have a
  137. single outstanding transaction at any one time, remember nothing commits
  138. until the outermost jbd2_journal_stop(). This means you must complete
  139. the transaction at the end of each file/inode/address etc. operation you
  140. perform, so that the journalling system isn't re-entered on another
  141. journal. Since transactions can't be nested/batched across differing
  142. journals, and another filesystem other than yours (say ext4) may be
  143. modified in a later syscall.
  144. The second case to bear in mind is that jbd2_journal_start() can block
  145. if there isn't enough space in the journal for your transaction (based
  146. on the passed nblocks param) - when it blocks it merely(!) needs to wait
  147. for transactions to complete and be committed from other tasks, so
  148. essentially we are waiting for jbd2_journal_stop(). So to avoid
  149. deadlocks you must treat jbd2_journal_start/stop() as if they were
  150. semaphores and include them in your semaphore ordering rules to prevent
  151. deadlocks. Note that jbd2_journal_extend() has similar blocking
  152. behaviour to jbd2_journal_start() so you can deadlock here just as
  153. easily as on jbd2_journal_start().
  154. Try to reserve the right number of blocks the first time. ;-). This will
  155. be the maximum number of blocks you are going to touch in this
  156. transaction. I advise having a look at at least ext4_jbd.h to see the
  157. basis on which ext4 uses to make these decisions.
  158. Another wriggle to watch out for is your on-disk block allocation
  159. strategy. Why? Because, if you do a delete, you need to ensure you
  160. haven't reused any of the freed blocks until the transaction freeing
  161. these blocks commits. If you reused these blocks and crash happens,
  162. there is no way to restore the contents of the reallocated blocks at the
  163. end of the last fully committed transaction. One simple way of doing
  164. this is to mark blocks as free in internal in-memory block allocation
  165. structures only after the transaction freeing them commits. Ext4 uses
  166. journal commit callback for this purpose.
  167. With journal commit callbacks you can ask the journalling layer to call
  168. a callback function when the transaction is finally committed to disk,
  169. so that you can do some of your own management. You ask the journalling
  170. layer for calling the callback by simply setting
  171. journal->j_commit_callback function pointer and that function is
  172. called after each transaction commit. You can also use
  173. transaction->t_private_list for attaching entries to a transaction
  174. that need processing when the transaction commits.
  175. JBD2 also provides a way to block all transaction updates via
  176. jbd2_journal_{un,}lock_updates(). Ext4 uses this when it wants a
  177. window with a clean and stable fs for a moment. E.g.
  178. ::
  179. jbd2_journal_lock_updates() //stop new stuff happening..
  180. jbd2_journal_flush() // checkpoint everything.
  181. ..do stuff on stable fs
  182. jbd2_journal_unlock_updates() // carry on with filesystem use.
  183. The opportunities for abuse and DOS attacks with this should be obvious,
  184. if you allow unprivileged userspace to trigger codepaths containing
  185. these calls.
  186. Summary
  187. ~~~~~~~
  188. Using the journal is a matter of wrapping the different context changes,
  189. being each mount, each modification (transaction) and each changed
  190. buffer to tell the journalling layer about them.
  191. Data Types
  192. ----------
  193. The journalling layer uses typedefs to 'hide' the concrete definitions
  194. of the structures used. As a client of the JBD2 layer you can just rely
  195. on the using the pointer as a magic cookie of some sort. Obviously the
  196. hiding is not enforced as this is 'C'.
  197. Structures
  198. ~~~~~~~~~~
  199. .. kernel-doc:: include/linux/jbd2.h
  200. :internal:
  201. Functions
  202. ---------
  203. The functions here are split into two groups those that affect a journal
  204. as a whole, and those which are used to manage transactions
  205. Journal Level
  206. ~~~~~~~~~~~~~
  207. .. kernel-doc:: fs/jbd2/journal.c
  208. :export:
  209. .. kernel-doc:: fs/jbd2/recovery.c
  210. :internal:
  211. Transasction Level
  212. ~~~~~~~~~~~~~~~~~~
  213. .. kernel-doc:: fs/jbd2/transaction.c
  214. :export:
  215. See also
  216. --------
  217. `Journaling the Linux ext2fs Filesystem, LinuxExpo 98, Stephen
  218. Tweedie <http://kernel.org/pub/linux/kernel/people/sct/ext3/journal-design.ps.gz>`__
  219. `Ext3 Journalling FileSystem, OLS 2000, Dr. Stephen
  220. Tweedie <http://olstrans.sourceforge.net/release/OLS2000-ext3/OLS2000-ext3.html>`__
  221. splice API
  222. ==========
  223. splice is a method for moving blocks of data around inside the kernel,
  224. without continually transferring them between the kernel and user space.
  225. .. kernel-doc:: fs/splice.c
  226. pipes API
  227. =========
  228. Pipe interfaces are all for in-kernel (builtin image) use. They are not
  229. exported for use by modules.
  230. .. kernel-doc:: include/linux/pipe_fs_i.h
  231. :internal:
  232. .. kernel-doc:: fs/pipe.c