Exporting 7.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. Making Filesystems Exportable
  2. =============================
  3. Most filesystem operations require a dentry (or two) as a starting
  4. point. Local applications have a reference-counted hold on suitable
  5. dentrys via open file descriptors or cwd/root. However remote
  6. applications that access a filesystem via a remote filesystem protocol
  7. such as NFS may not be able to hold such a reference, and so need a
  8. different way to refer to a particular dentry. As the alternative
  9. form of reference needs to be stable across renames, truncates, and
  10. server-reboot (among other things, though these tend to be the most
  11. problematic), there is no simple answer like 'filename'.
  12. The mechanism discussed here allows each filesystem implementation to
  13. specify how to generate an opaque (out side of the filesystem) byte
  14. string for any dentry, and how to find an appropriate dentry for any
  15. given opaque byte string.
  16. This byte string will be called a "filehandle fragment" as it
  17. corresponds to part of an NFS filehandle.
  18. A filesystem which supports the mapping between filehandle fragments
  19. and dentrys will be termed "exportable".
  20. Dcache Issues
  21. -------------
  22. The dcache normally contains a proper prefix of any given filesystem
  23. tree. This means that if any filesystem object is in the dcache, then
  24. all of the ancestors of that filesystem object are also in the dcache.
  25. As normal access is by filename this prefix is created naturally and
  26. maintained easily (by each object maintaining a reference count on
  27. its parent).
  28. However when objects are included into the dcache by interpreting a
  29. filehandle fragment, there is no automatic creation of a path prefix
  30. for the object. This leads to two related but distinct features of
  31. the dcache that are not needed for normal filesystem access.
  32. 1/ The dcache must sometimes contain objects that are not part of the
  33. proper prefix. i.e that are not connected to the root.
  34. 2/ The dcache must be prepared for a newly found (via ->lookup) directory
  35. to already have a (non-connected) dentry, and must be able to move
  36. that dentry into place (based on the parent and name in the
  37. ->lookup). This is particularly needed for directories as
  38. it is a dcache invariant that directories only have one dentry.
  39. To implement these features, the dcache has:
  40. a/ A dentry flag DCACHE_DISCONNECTED which is set on
  41. any dentry that might not be part of the proper prefix.
  42. This is set when anonymous dentries are created, and cleared when a
  43. dentry is noticed to be a child of a dentry which is in the proper
  44. prefix.
  45. b/ A per-superblock list "s_anon" of dentries which are the roots of
  46. subtrees that are not in the proper prefix. These dentries, as
  47. well as the proper prefix, need to be released at unmount time. As
  48. these dentries will not be hashed, they are linked together on the
  49. d_hash list_head.
  50. c/ Helper routines to allocate anonymous dentries, and to help attach
  51. loose directory dentries at lookup time. They are:
  52. d_alloc_anon(inode) will return a dentry for the given inode.
  53. If the inode already has a dentry, one of those is returned.
  54. If it doesn't, a new anonymous (IS_ROOT and
  55. DCACHE_DISCONNECTED) dentry is allocated and attached.
  56. In the case of a directory, care is taken that only one dentry
  57. can ever be attached.
  58. d_splice_alias(inode, dentry) will make sure that there is a
  59. dentry with the same name and parent as the given dentry, and
  60. which refers to the given inode.
  61. If the inode is a directory and already has a dentry, then that
  62. dentry is d_moved over the given dentry.
  63. If the passed dentry gets attached, care is taken that this is
  64. mutually exclusive to a d_alloc_anon operation.
  65. If the passed dentry is used, NULL is returned, else the used
  66. dentry is returned. This corresponds to the calling pattern of
  67. ->lookup.
  68. Filesystem Issues
  69. -----------------
  70. For a filesystem to be exportable it must:
  71. 1/ provide the filehandle fragment routines described below.
  72. 2/ make sure that d_splice_alias is used rather than d_add
  73. when ->lookup finds an inode for a given parent and name.
  74. Typically the ->lookup routine will end:
  75. if (inode)
  76. return d_splice(inode, dentry);
  77. d_add(dentry, inode);
  78. return NULL;
  79. }
  80. A file system implementation declares that instances of the filesystem
  81. are exportable by setting the s_export_op field in the struct
  82. super_block. This field must point to a "struct export_operations"
  83. struct which could potentially be full of NULLs, though normally at
  84. least get_parent will be set.
  85. The primary operations are decode_fh and encode_fh.
  86. decode_fh takes a filehandle fragment and tries to find or create a
  87. dentry for the object referred to by the filehandle.
  88. encode_fh takes a dentry and creates a filehandle fragment which can
  89. later be used to find/create a dentry for the same object.
  90. decode_fh will probably make use of "find_exported_dentry".
  91. This function lives in the "exportfs" module which a filesystem does
  92. not need unless it is being exported. So rather that calling
  93. find_exported_dentry directly, each filesystem should call it through
  94. the find_exported_dentry pointer in it's export_operations table.
  95. This field is set correctly by the exporting agent (e.g. nfsd) when a
  96. filesystem is exported, and before any export operations are called.
  97. find_exported_dentry needs three support functions from the
  98. filesystem:
  99. get_name. When given a parent dentry and a child dentry, this
  100. should find a name in the directory identified by the parent
  101. dentry, which leads to the object identified by the child dentry.
  102. If no get_name function is supplied, a default implementation is
  103. provided which uses vfs_readdir to find potential names, and
  104. matches inode numbers to find the correct match.
  105. get_parent. When given a dentry for a directory, this should return
  106. a dentry for the parent. Quite possibly the parent dentry will
  107. have been allocated by d_alloc_anon.
  108. The default get_parent function just returns an error so any
  109. filehandle lookup that requires finding a parent will fail.
  110. ->lookup("..") is *not* used as a default as it can leave ".."
  111. entries in the dcache which are too messy to work with.
  112. get_dentry. When given an opaque datum, this should find the
  113. implied object and create a dentry for it (possibly with
  114. d_alloc_anon).
  115. The opaque datum is whatever is passed down by the decode_fh
  116. function, and is often simply a fragment of the filehandle
  117. fragment.
  118. decode_fh passes two datums through find_exported_dentry. One that
  119. should be used to identify the target object, and one that can be
  120. used to identify the object's parent, should that be necessary.
  121. The default get_dentry function assumes that the datum contains an
  122. inode number and a generation number, and it attempts to get the
  123. inode using "iget" and check it's validity by matching the
  124. generation number. A filesystem should only depend on the default
  125. if iget can safely be used this way.
  126. If decode_fh and/or encode_fh are left as NULL, then default
  127. implementations are used. These defaults are suitable for ext2 and
  128. extremely similar filesystems (like ext3).
  129. The default encode_fh creates a filehandle fragment from the inode
  130. number and generation number of the target together with the inode
  131. number and generation number of the parent (if the parent is
  132. required).
  133. The default decode_fh extract the target and parent datums from the
  134. filehandle assuming the format used by the default encode_fh and
  135. passed them to find_exported_dentry.
  136. A filehandle fragment consists of an array of 1 or more 4byte words,
  137. together with a one byte "type".
  138. The decode_fh routine should not depend on the stated size that is
  139. passed to it. This size may be larger than the original filehandle
  140. generated by encode_fh, in which case it will have been padded with
  141. nuls. Rather, the encode_fh routine should choose a "type" which
  142. indicates the decode_fh how much of the filehandle is valid, and how
  143. it should be interpreted.