exporting.rst 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247
  1. :orphan:
  2. Making Filesystems Exportable
  3. =============================
  4. Overview
  5. --------
  6. All filesystem operations require a dentry (or two) as a starting
  7. point. Local applications have a reference-counted hold on suitable
  8. dentries via open file descriptors or cwd/root. However remote
  9. applications that access a filesystem via a remote filesystem protocol
  10. such as NFS may not be able to hold such a reference, and so need a
  11. different way to refer to a particular dentry. As the alternative
  12. form of reference needs to be stable across renames, truncates, and
  13. server-reboot (among other things, though these tend to be the most
  14. problematic), there is no simple answer like 'filename'.
  15. The mechanism discussed here allows each filesystem implementation to
  16. specify how to generate an opaque (outside of the filesystem) byte
  17. string for any dentry, and how to find an appropriate dentry for any
  18. given opaque byte string.
  19. This byte string will be called a "filehandle fragment" as it
  20. corresponds to part of an NFS filehandle.
  21. A filesystem which supports the mapping between filehandle fragments
  22. and dentries will be termed "exportable".
  23. Dcache Issues
  24. -------------
  25. The dcache normally contains a proper prefix of any given filesystem
  26. tree. This means that if any filesystem object is in the dcache, then
  27. all of the ancestors of that filesystem object are also in the dcache.
  28. As normal access is by filename this prefix is created naturally and
  29. maintained easily (by each object maintaining a reference count on
  30. its parent).
  31. However when objects are included into the dcache by interpreting a
  32. filehandle fragment, there is no automatic creation of a path prefix
  33. for the object. This leads to two related but distinct features of
  34. the dcache that are not needed for normal filesystem access.
  35. 1. The dcache must sometimes contain objects that are not part of the
  36. proper prefix. i.e that are not connected to the root.
  37. 2. The dcache must be prepared for a newly found (via ->lookup) directory
  38. to already have a (non-connected) dentry, and must be able to move
  39. that dentry into place (based on the parent and name in the
  40. ->lookup). This is particularly needed for directories as
  41. it is a dcache invariant that directories only have one dentry.
  42. To implement these features, the dcache has:
  43. a. A dentry flag DCACHE_DISCONNECTED which is set on
  44. any dentry that might not be part of the proper prefix.
  45. This is set when anonymous dentries are created, and cleared when a
  46. dentry is noticed to be a child of a dentry which is in the proper
  47. prefix. If the refcount on a dentry with this flag set
  48. becomes zero, the dentry is immediately discarded, rather than being
  49. kept in the dcache. If a dentry that is not already in the dcache
  50. is repeatedly accessed by filehandle (as NFSD might do), an new dentry
  51. will be a allocated for each access, and discarded at the end of
  52. the access.
  53. Note that such a dentry can acquire children, name, ancestors, etc.
  54. without losing DCACHE_DISCONNECTED - that flag is only cleared when
  55. subtree is successfully reconnected to root. Until then dentries
  56. in such subtree are retained only as long as there are references;
  57. refcount reaching zero means immediate eviction, same as for unhashed
  58. dentries. That guarantees that we won't need to hunt them down upon
  59. umount.
  60. b. A primitive for creation of secondary roots - d_obtain_root(inode).
  61. Those do _not_ bear DCACHE_DISCONNECTED. They are placed on the
  62. per-superblock list (->s_roots), so they can be located at umount
  63. time for eviction purposes.
  64. c. Helper routines to allocate anonymous dentries, and to help attach
  65. loose directory dentries at lookup time. They are:
  66. d_obtain_alias(inode) will return a dentry for the given inode.
  67. If the inode already has a dentry, one of those is returned.
  68. If it doesn't, a new anonymous (IS_ROOT and
  69. DCACHE_DISCONNECTED) dentry is allocated and attached.
  70. In the case of a directory, care is taken that only one dentry
  71. can ever be attached.
  72. d_splice_alias(inode, dentry) will introduce a new dentry into the tree;
  73. either the passed-in dentry or a preexisting alias for the given inode
  74. (such as an anonymous one created by d_obtain_alias), if appropriate.
  75. It returns NULL when the passed-in dentry is used, following the calling
  76. convention of ->lookup.
  77. Filesystem Issues
  78. -----------------
  79. For a filesystem to be exportable it must:
  80. 1. provide the filehandle fragment routines described below.
  81. 2. make sure that d_splice_alias is used rather than d_add
  82. when ->lookup finds an inode for a given parent and name.
  83. If inode is NULL, d_splice_alias(inode, dentry) is equivalent to::
  84. d_add(dentry, inode), NULL
  85. Similarly, d_splice_alias(ERR_PTR(err), dentry) = ERR_PTR(err)
  86. Typically the ->lookup routine will simply end with a::
  87. return d_splice_alias(inode, dentry);
  88. }
  89. A file system implementation declares that instances of the filesystem
  90. are exportable by setting the s_export_op field in the struct
  91. super_block. This field must point to a "struct export_operations"
  92. struct which has the following members:
  93. encode_fh (mandatory)
  94. Takes a dentry and creates a filehandle fragment which may later be used
  95. to find or create a dentry for the same object.
  96. fh_to_dentry (mandatory)
  97. Given a filehandle fragment, this should find the implied object and
  98. create a dentry for it (possibly with d_obtain_alias).
  99. fh_to_parent (optional but strongly recommended)
  100. Given a filehandle fragment, this should find the parent of the
  101. implied object and create a dentry for it (possibly with
  102. d_obtain_alias). May fail if the filehandle fragment is too small.
  103. get_parent (optional but strongly recommended)
  104. When given a dentry for a directory, this should return a dentry for
  105. the parent. Quite possibly the parent dentry will have been allocated
  106. by d_alloc_anon. The default get_parent function just returns an error
  107. so any filehandle lookup that requires finding a parent will fail.
  108. ->lookup("..") is *not* used as a default as it can leave ".." entries
  109. in the dcache which are too messy to work with.
  110. get_name (optional)
  111. When given a parent dentry and a child dentry, this should find a name
  112. in the directory identified by the parent dentry, which leads to the
  113. object identified by the child dentry. If no get_name function is
  114. supplied, a default implementation is provided which uses vfs_readdir
  115. to find potential names, and matches inode numbers to find the correct
  116. match.
  117. flags
  118. Some filesystems may need to be handled differently than others. The
  119. export_operations struct also includes a flags field that allows the
  120. filesystem to communicate such information to nfsd. See the Export
  121. Operations Flags section below for more explanation.
  122. A filehandle fragment consists of an array of 1 or more 4byte words,
  123. together with a one byte "type".
  124. The decode_fh routine should not depend on the stated size that is
  125. passed to it. This size may be larger than the original filehandle
  126. generated by encode_fh, in which case it will have been padded with
  127. nuls. Rather, the encode_fh routine should choose a "type" which
  128. indicates the decode_fh how much of the filehandle is valid, and how
  129. it should be interpreted.
  130. Export Operations Flags
  131. -----------------------
  132. In addition to the operation vector pointers, struct export_operations also
  133. contains a "flags" field that allows the filesystem to communicate to nfsd
  134. that it may want to do things differently when dealing with it. The
  135. following flags are defined:
  136. EXPORT_OP_NOWCC - disable NFSv3 WCC attributes on this filesystem
  137. RFC 1813 recommends that servers always send weak cache consistency
  138. (WCC) data to the client after each operation. The server should
  139. atomically collect attributes about the inode, do an operation on it,
  140. and then collect the attributes afterward. This allows the client to
  141. skip issuing GETATTRs in some situations but means that the server
  142. is calling vfs_getattr for almost all RPCs. On some filesystems
  143. (particularly those that are clustered or networked) this is expensive
  144. and atomicity is difficult to guarantee. This flag indicates to nfsd
  145. that it should skip providing WCC attributes to the client in NFSv3
  146. replies when doing operations on this filesystem. Consider enabling
  147. this on filesystems that have an expensive ->getattr inode operation,
  148. or when atomicity between pre and post operation attribute collection
  149. is impossible to guarantee.
  150. EXPORT_OP_NOSUBTREECHK - disallow subtree checking on this fs
  151. Many NFS operations deal with filehandles, which the server must then
  152. vet to ensure that they live inside of an exported tree. When the
  153. export consists of an entire filesystem, this is trivial. nfsd can just
  154. ensure that the filehandle live on the filesystem. When only part of a
  155. filesystem is exported however, then nfsd must walk the ancestors of the
  156. inode to ensure that it's within an exported subtree. This is an
  157. expensive operation and not all filesystems can support it properly.
  158. This flag exempts the filesystem from subtree checking and causes
  159. exportfs to get back an error if it tries to enable subtree checking
  160. on it.
  161. EXPORT_OP_CLOSE_BEFORE_UNLINK - always close cached files before unlinking
  162. On some exportable filesystems (such as NFS) unlinking a file that
  163. is still open can cause a fair bit of extra work. For instance,
  164. the NFS client will do a "sillyrename" to ensure that the file
  165. sticks around while it's still open. When reexporting, that open
  166. file is held by nfsd so we usually end up doing a sillyrename, and
  167. then immediately deleting the sillyrenamed file just afterward when
  168. the link count actually goes to zero. Sometimes this delete can race
  169. with other operations (for instance an rmdir of the parent directory).
  170. This flag causes nfsd to close any open files for this inode _before_
  171. calling into the vfs to do an unlink or a rename that would replace
  172. an existing file.
  173. EXPORT_OP_REMOTE_FS - Backing storage for this filesystem is remote
  174. PF_LOCAL_THROTTLE exists for loopback NFSD, where a thread needs to
  175. write to one bdi (the final bdi) in order to free up writes queued
  176. to another bdi (the client bdi). Such threads get a private balance
  177. of dirty pages so that dirty pages for the client bdi do not imact
  178. the daemon writing to the final bdi. For filesystems whose durable
  179. storage is not local (such as exported NFS filesystems), this
  180. constraint has negative consequences. EXPORT_OP_REMOTE_FS enables
  181. an export to disable writeback throttling.
  182. EXPORT_OP_NOATOMIC_ATTR - Filesystem does not update attributes atomically
  183. EXPORT_OP_NOATOMIC_ATTR indicates that the exported filesystem
  184. cannot provide the semantics required by the "atomic" boolean in
  185. NFSv4's change_info4. This boolean indicates to a client whether the
  186. returned before and after change attributes were obtained atomically
  187. with the respect to the requested metadata operation (UNLINK,
  188. OPEN/CREATE, MKDIR, etc).
  189. EXPORT_OP_FLUSH_ON_CLOSE - Filesystem flushes file data on close(2)
  190. On most filesystems, inodes can remain under writeback after the
  191. file is closed. NFSD relies on client activity or local flusher
  192. threads to handle writeback. Certain filesystems, such as NFS, flush
  193. all of an inode's dirty data on last close. Exports that behave this
  194. way should set EXPORT_OP_FLUSH_ON_CLOSE so that NFSD knows to skip
  195. waiting for writeback when closing such files.
  196. EXPORT_OP_ASYNC_LOCK - Indicates a capable filesystem to do async lock
  197. requests from lockd. Only set EXPORT_OP_ASYNC_LOCK if the filesystem has
  198. it's own ->lock() functionality as core posix_lock_file() implementation
  199. has no async lock request handling yet. For more information about how to
  200. indicate an async lock request from a ->lock() file_operations struct, see
  201. fs/locks.c and comment for the function vfs_lock_file().