Exporting 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162
  1. Making Filesystems Exportable
  2. =============================
  3. Overview
  4. --------
  5. All filesystem operations require a dentry (or two) as a starting
  6. point. Local applications have a reference-counted hold on suitable
  7. dentries via open file descriptors or cwd/root. However remote
  8. applications that access a filesystem via a remote filesystem protocol
  9. such as NFS may not be able to hold such a reference, and so need a
  10. different way to refer to a particular dentry. As the alternative
  11. form of reference needs to be stable across renames, truncates, and
  12. server-reboot (among other things, though these tend to be the most
  13. problematic), there is no simple answer like 'filename'.
  14. The mechanism discussed here allows each filesystem implementation to
  15. specify how to generate an opaque (outside of the filesystem) byte
  16. string for any dentry, and how to find an appropriate dentry for any
  17. given opaque byte string.
  18. This byte string will be called a "filehandle fragment" as it
  19. corresponds to part of an NFS filehandle.
  20. A filesystem which supports the mapping between filehandle fragments
  21. and dentries will be termed "exportable".
  22. Dcache Issues
  23. -------------
  24. The dcache normally contains a proper prefix of any given filesystem
  25. tree. This means that if any filesystem object is in the dcache, then
  26. all of the ancestors of that filesystem object are also in the dcache.
  27. As normal access is by filename this prefix is created naturally and
  28. maintained easily (by each object maintaining a reference count on
  29. its parent).
  30. However when objects are included into the dcache by interpreting a
  31. filehandle fragment, there is no automatic creation of a path prefix
  32. for the object. This leads to two related but distinct features of
  33. the dcache that are not needed for normal filesystem access.
  34. 1/ The dcache must sometimes contain objects that are not part of the
  35. proper prefix. i.e that are not connected to the root.
  36. 2/ The dcache must be prepared for a newly found (via ->lookup) directory
  37. to already have a (non-connected) dentry, and must be able to move
  38. that dentry into place (based on the parent and name in the
  39. ->lookup). This is particularly needed for directories as
  40. it is a dcache invariant that directories only have one dentry.
  41. To implement these features, the dcache has:
  42. a/ A dentry flag DCACHE_DISCONNECTED which is set on
  43. any dentry that might not be part of the proper prefix.
  44. This is set when anonymous dentries are created, and cleared when a
  45. dentry is noticed to be a child of a dentry which is in the proper
  46. prefix.
  47. b/ A per-superblock list "s_anon" of dentries which are the roots of
  48. subtrees that are not in the proper prefix. These dentries, as
  49. well as the proper prefix, need to be released at unmount time. As
  50. these dentries will not be hashed, they are linked together on the
  51. d_hash list_head.
  52. c/ Helper routines to allocate anonymous dentries, and to help attach
  53. loose directory dentries at lookup time. They are:
  54. d_obtain_alias(inode) will return a dentry for the given inode.
  55. If the inode already has a dentry, one of those is returned.
  56. If it doesn't, a new anonymous (IS_ROOT and
  57. DCACHE_DISCONNECTED) dentry is allocated and attached.
  58. In the case of a directory, care is taken that only one dentry
  59. can ever be attached.
  60. d_splice_alias(inode, dentry) or d_materialise_unique(dentry, inode)
  61. will introduce a new dentry into the tree; either the passed-in
  62. dentry or a preexisting alias for the given inode (such as an
  63. anonymous one created by d_obtain_alias), if appropriate. The two
  64. functions differ in their handling of directories with preexisting
  65. aliases:
  66. d_splice_alias will use any existing IS_ROOT dentry, but it will
  67. return -EIO rather than try to move a dentry with a different
  68. parent. This is appropriate for local filesystems, which
  69. should never see such an alias unless the filesystem is
  70. corrupted somehow (for example, if two on-disk directory
  71. entries refer to the same directory.)
  72. d_materialise_unique will attempt to move any dentry. This is
  73. appropriate for distributed filesystems, where finding a
  74. directory other than where we last cached it may be a normal
  75. consequence of concurrent operations on other hosts.
  76. Both functions return NULL when the passed-in dentry is used,
  77. following the calling convention of ->lookup.
  78. Filesystem Issues
  79. -----------------
  80. For a filesystem to be exportable it must:
  81. 1/ provide the filehandle fragment routines described below.
  82. 2/ make sure that d_splice_alias is used rather than d_add
  83. when ->lookup finds an inode for a given parent and name.
  84. If inode is NULL, d_splice_alias(inode, dentry) is equivalent to
  85. d_add(dentry, inode), NULL
  86. Similarly, d_splice_alias(ERR_PTR(err), dentry) = ERR_PTR(err)
  87. Typically the ->lookup routine will simply end with a:
  88. return d_splice_alias(inode, dentry);
  89. }
  90. A file system implementation declares that instances of the filesystem
  91. are exportable by setting the s_export_op field in the struct
  92. super_block. This field must point to a "struct export_operations"
  93. struct which has the following members:
  94. encode_fh (optional)
  95. Takes a dentry and creates a filehandle fragment which can later be used
  96. to find or create a dentry for the same object. The default
  97. implementation creates a filehandle fragment that encodes a 32bit inode
  98. and generation number for the inode encoded, and if necessary the
  99. same information for the parent.
  100. fh_to_dentry (mandatory)
  101. Given a filehandle fragment, this should find the implied object and
  102. create a dentry for it (possibly with d_obtain_alias).
  103. fh_to_parent (optional but strongly recommended)
  104. Given a filehandle fragment, this should find the parent of the
  105. implied object and create a dentry for it (possibly with
  106. d_obtain_alias). May fail if the filehandle fragment is too small.
  107. get_parent (optional but strongly recommended)
  108. When given a dentry for a directory, this should return a dentry for
  109. the parent. Quite possibly the parent dentry will have been allocated
  110. by d_alloc_anon. The default get_parent function just returns an error
  111. so any filehandle lookup that requires finding a parent will fail.
  112. ->lookup("..") is *not* used as a default as it can leave ".." entries
  113. in the dcache which are too messy to work with.
  114. get_name (optional)
  115. When given a parent dentry and a child dentry, this should find a name
  116. in the directory identified by the parent dentry, which leads to the
  117. object identified by the child dentry. If no get_name function is
  118. supplied, a default implementation is provided which uses vfs_readdir
  119. to find potential names, and matches inode numbers to find the correct
  120. match.
  121. A filehandle fragment consists of an array of 1 or more 4byte words,
  122. together with a one byte "type".
  123. The decode_fh routine should not depend on the stated size that is
  124. passed to it. This size may be larger than the original filehandle
  125. generated by encode_fh, in which case it will have been padded with
  126. nuls. Rather, the encode_fh routine should choose a "type" which
  127. indicates the decode_fh how much of the filehandle is valid, and how
  128. it should be interpreted.