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