index.rst 11 KB

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