index.rst 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330
  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 :c:func:`jbd2_journal_init_inode` call is for journals stored in
  88. filesystem inodes, or the :c:func:`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 :c:func:`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 :c:func:`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 :c:func:`jbd2_journal_wipe` to clear the journal contents before
  100. calling :c:func:`jbd2_journal_load`.
  101. Note that jbd2_journal_wipe(..,0) calls
  102. :c:func:`jbd2_journal_skip_recovery` for you if it detects any outstanding
  103. transactions in the journal and similarly :c:func:`jbd2_journal_load` will
  104. call :c:func:`jbd2_journal_recover` if necessary. I would advise reading
  105. :c:func:`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 :c:func:`jbd2_journal_start` which returns a transaction handle.
  113. :c:func:`jbd2_journal_start` and its counterpart :c:func:`jbd2_journal_stop`,
  114. which indicates the end of a transaction are nestable calls, so you can
  115. reenter a transaction if necessary, but remember you must call
  116. :c:func:`jbd2_journal_stop` the same number of times as
  117. :c:func:`jbd2_journal_start` before the transaction is completed (or more
  118. accurately leaves the update phase). Ext4/VFS makes use of this feature to
  119. simplify handling 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 :c:func:`jbd2_journal_get_create_access()` /
  123. :c:func:`jbd2_journal_get_write_access()` /
  124. :c:func:`jbd2_journal_get_undo_access()` as appropriate, this allows the
  125. journalling layer to copy the unmodified
  126. data if it needs to. After all the buffer may be part of a previously
  127. uncommitted transaction. At this point you are at last ready to modify a
  128. buffer, and once you are have done so you need to call
  129. :c:func:`jbd2_journal_dirty_metadata`. Or if you've asked for access to a
  130. buffer you now know is now longer required to be pushed back on the
  131. device you can call :c:func:`jbd2_journal_forget` in much the same way as you
  132. might have used :c:func:`bforget` in the past.
  133. A :c:func:`jbd2_journal_flush` may be called at any time to commit and
  134. checkpoint all your transactions.
  135. Then at umount time , in your :c:func:`put_super` you can then call
  136. :c:func:`jbd2_journal_destroy` to clean up your in-core journal object.
  137. Unfortunately there a couple of ways the journal layer can cause a
  138. deadlock. The first thing to note is that each task can only have a
  139. single outstanding transaction at any one time, remember nothing commits
  140. until the outermost :c:func:`jbd2_journal_stop`. This means you must complete
  141. the transaction at the end of each file/inode/address etc. operation you
  142. perform, so that the journalling system isn't re-entered on another
  143. journal. Since transactions can't be nested/batched across differing
  144. journals, and another filesystem other than yours (say ext4) may be
  145. modified in a later syscall.
  146. The second case to bear in mind is that :c:func:`jbd2_journal_start` can block
  147. if there isn't enough space in the journal for your transaction (based
  148. on the passed nblocks param) - when it blocks it merely(!) needs to wait
  149. for transactions to complete and be committed from other tasks, so
  150. essentially we are waiting for :c:func:`jbd2_journal_stop`. So to avoid
  151. deadlocks you must treat :c:func:`jbd2_journal_start` /
  152. :c:func:`jbd2_journal_stop` as if they were semaphores and include them in
  153. your semaphore ordering rules to prevent
  154. deadlocks. Note that :c:func:`jbd2_journal_extend` has similar blocking
  155. behaviour to :c:func:`jbd2_journal_start` so you can deadlock here just as
  156. easily as on :c:func:`jbd2_journal_start`.
  157. Try to reserve the right number of blocks the first time. ;-). This will
  158. be the maximum number of blocks you are going to touch in this
  159. transaction. I advise having a look at at least ext4_jbd.h to see the
  160. basis on which ext4 uses to make these decisions.
  161. Another wriggle to watch out for is your on-disk block allocation
  162. strategy. Why? Because, if you do a delete, you need to ensure you
  163. haven't reused any of the freed blocks until the transaction freeing
  164. these blocks commits. If you reused these blocks and crash happens,
  165. there is no way to restore the contents of the reallocated blocks at the
  166. end of the last fully committed transaction. One simple way of doing
  167. this is to mark blocks as free in internal in-memory block allocation
  168. structures only after the transaction freeing them commits. Ext4 uses
  169. journal commit callback for this purpose.
  170. With journal commit callbacks you can ask the journalling layer to call
  171. a callback function when the transaction is finally committed to disk,
  172. so that you can do some of your own management. You ask the journalling
  173. layer for calling the callback by simply setting
  174. ``journal->j_commit_callback`` function pointer and that function is
  175. called after each transaction commit. You can also use
  176. ``transaction->t_private_list`` for attaching entries to a transaction
  177. that need processing when the transaction commits.
  178. JBD2 also provides a way to block all transaction updates via
  179. :c:func:`jbd2_journal_lock_updates()` /
  180. :c:func:`jbd2_journal_unlock_updates()`. Ext4 uses this when it wants a
  181. window with a clean and stable fs for a moment. E.g.
  182. ::
  183. jbd2_journal_lock_updates() //stop new stuff happening..
  184. jbd2_journal_flush() // checkpoint everything.
  185. ..do stuff on stable fs
  186. jbd2_journal_unlock_updates() // carry on with filesystem use.
  187. The opportunities for abuse and DOS attacks with this should be obvious,
  188. if you allow unprivileged userspace to trigger codepaths containing
  189. these calls.
  190. Summary
  191. ~~~~~~~
  192. Using the journal is a matter of wrapping the different context changes,
  193. being each mount, each modification (transaction) and each changed
  194. buffer to tell the journalling layer about them.
  195. Data Types
  196. ----------
  197. The journalling layer uses typedefs to 'hide' the concrete definitions
  198. of the structures used. As a client of the JBD2 layer you can just rely
  199. on the using the pointer as a magic cookie of some sort. Obviously the
  200. hiding is not enforced as this is 'C'.
  201. Structures
  202. ~~~~~~~~~~
  203. .. kernel-doc:: include/linux/jbd2.h
  204. :internal:
  205. Functions
  206. ---------
  207. The functions here are split into two groups those that affect a journal
  208. as a whole, and those which are used to manage transactions
  209. Journal Level
  210. ~~~~~~~~~~~~~
  211. .. kernel-doc:: fs/jbd2/journal.c
  212. :export:
  213. .. kernel-doc:: fs/jbd2/recovery.c
  214. :internal:
  215. Transasction Level
  216. ~~~~~~~~~~~~~~~~~~
  217. .. kernel-doc:: fs/jbd2/transaction.c
  218. See also
  219. --------
  220. `Journaling the Linux ext2fs Filesystem, LinuxExpo 98, Stephen
  221. Tweedie <http://kernel.org/pub/linux/kernel/people/sct/ext3/journal-design.ps.gz>`__
  222. `Ext3 Journalling FileSystem, OLS 2000, Dr. Stephen
  223. Tweedie <http://olstrans.sourceforge.net/release/OLS2000-ext3/OLS2000-ext3.html>`__
  224. splice API
  225. ==========
  226. splice is a method for moving blocks of data around inside the kernel,
  227. without continually transferring them between the kernel and user space.
  228. .. kernel-doc:: fs/splice.c
  229. pipes API
  230. =========
  231. Pipe interfaces are all for in-kernel (builtin image) use. They are not
  232. exported for use by modules.
  233. .. kernel-doc:: include/linux/pipe_fs_i.h
  234. :internal:
  235. .. kernel-doc:: fs/pipe.c
  236. Encryption API
  237. ==============
  238. A library which filesystems can hook into to support transparent
  239. encryption of files and directories.
  240. .. toctree::
  241. :maxdepth: 2
  242. fscrypt
  243. fsverity