xref: /linux/include/linux/exportfs.h (revision 45a43ac5acc90b8f4835eea92692f620e561a06b)
1 /* SPDX-License-Identifier: GPL-2.0 */
2 #ifndef LINUX_EXPORTFS_H
3 #define LINUX_EXPORTFS_H 1
4 
5 #include <linux/types.h>
6 #include <linux/path.h>
7 
8 struct dentry;
9 struct iattr;
10 struct inode;
11 struct iomap;
12 struct super_block;
13 struct vfsmount;
14 
15 /* limit the handle size to NFSv4 handle size now */
16 #define MAX_HANDLE_SZ 128
17 
18 /*
19  * The fileid_type identifies how the file within the filesystem is encoded.
20  * In theory this is freely set and parsed by the filesystem, but we try to
21  * stick to conventions so we can share some generic code and don't confuse
22  * sniffers like ethereal/wireshark.
23  *
24  * The filesystem must not use the value '0' or '0xff'.
25  */
26 enum fid_type {
27 	/*
28 	 * The root, or export point, of the filesystem.
29 	 * (Never actually passed down to the filesystem.
30 	 */
31 	FILEID_ROOT = 0,
32 
33 	/*
34 	 * 32bit inode number, 32 bit generation number.
35 	 */
36 	FILEID_INO32_GEN = 1,
37 
38 	/*
39 	 * 32bit inode number, 32 bit generation number,
40 	 * 32 bit parent directory inode number.
41 	 */
42 	FILEID_INO32_GEN_PARENT = 2,
43 
44 	/*
45 	 * 64 bit object ID, 64 bit root object ID,
46 	 * 32 bit generation number.
47 	 */
48 	FILEID_BTRFS_WITHOUT_PARENT = 0x4d,
49 
50 	/*
51 	 * 64 bit object ID, 64 bit root object ID,
52 	 * 32 bit generation number,
53 	 * 64 bit parent object ID, 32 bit parent generation.
54 	 */
55 	FILEID_BTRFS_WITH_PARENT = 0x4e,
56 
57 	/*
58 	 * 64 bit object ID, 64 bit root object ID,
59 	 * 32 bit generation number,
60 	 * 64 bit parent object ID, 32 bit parent generation,
61 	 * 64 bit parent root object ID.
62 	 */
63 	FILEID_BTRFS_WITH_PARENT_ROOT = 0x4f,
64 
65 	/*
66 	 * 32 bit block number, 16 bit partition reference,
67 	 * 16 bit unused, 32 bit generation number.
68 	 */
69 	FILEID_UDF_WITHOUT_PARENT = 0x51,
70 
71 	/*
72 	 * 32 bit block number, 16 bit partition reference,
73 	 * 16 bit unused, 32 bit generation number,
74 	 * 32 bit parent block number, 32 bit parent generation number
75 	 */
76 	FILEID_UDF_WITH_PARENT = 0x52,
77 
78 	/*
79 	 * 64 bit checkpoint number, 64 bit inode number,
80 	 * 32 bit generation number.
81 	 */
82 	FILEID_NILFS_WITHOUT_PARENT = 0x61,
83 
84 	/*
85 	 * 64 bit checkpoint number, 64 bit inode number,
86 	 * 32 bit generation number, 32 bit parent generation.
87 	 * 64 bit parent inode number.
88 	 */
89 	FILEID_NILFS_WITH_PARENT = 0x62,
90 
91 	/*
92 	 * 32 bit generation number, 40 bit i_pos.
93 	 */
94 	FILEID_FAT_WITHOUT_PARENT = 0x71,
95 
96 	/*
97 	 * 32 bit generation number, 40 bit i_pos,
98 	 * 32 bit parent generation number, 40 bit parent i_pos
99 	 */
100 	FILEID_FAT_WITH_PARENT = 0x72,
101 
102 	/*
103 	 * 64 bit inode number, 32 bit generation number.
104 	 */
105 	FILEID_INO64_GEN = 0x81,
106 
107 	/*
108 	 * 64 bit inode number, 32 bit generation number,
109 	 * 64 bit parent inode number, 32 bit parent generation.
110 	 */
111 	FILEID_INO64_GEN_PARENT = 0x82,
112 
113 	/*
114 	 * 128 bit child FID (struct lu_fid)
115 	 * 128 bit parent FID (struct lu_fid)
116 	 */
117 	FILEID_LUSTRE = 0x97,
118 
119 	/*
120 	 * 64 bit inode number, 32 bit subvolume, 32 bit generation number:
121 	 */
122 	FILEID_BCACHEFS_WITHOUT_PARENT = 0xb1,
123 	FILEID_BCACHEFS_WITH_PARENT = 0xb2,
124 
125 	/*
126 	 *
127 	 * 64 bit namespace identifier, 32 bit namespace type, 32 bit inode number.
128 	 */
129 	FILEID_NSFS = 0xf1,
130 
131 	/*
132 	 * 64 bit unique kernfs id
133 	 */
134 	FILEID_KERNFS = 0xfe,
135 
136 	/*
137 	 * Filesystems must not use 0xff file ID.
138 	 */
139 	FILEID_INVALID = 0xff,
140 };
141 
142 struct fid {
143 	union {
144 		struct {
145 			u32 ino;
146 			u32 gen;
147 			u32 parent_ino;
148 			u32 parent_gen;
149 		} i32;
150 		struct {
151 			u64 ino;
152 			u32 gen;
153 		} __packed i64;
154 		struct {
155  			u32 block;
156  			u16 partref;
157  			u16 parent_partref;
158  			u32 generation;
159  			u32 parent_block;
160  			u32 parent_generation;
161  		} udf;
162 		DECLARE_FLEX_ARRAY(__u32, raw);
163 	};
164 };
165 
166 enum handle_to_path_flags {
167 	HANDLE_CHECK_PERMS   = (1 << 0),
168 	HANDLE_CHECK_SUBTREE = (1 << 1),
169 };
170 
171 struct handle_to_path_ctx {
172 	struct path root;
173 	enum handle_to_path_flags flags;
174 	unsigned int fh_flags;
175 };
176 
177 #define EXPORT_FH_CONNECTABLE	0x1 /* Encode file handle with parent */
178 #define EXPORT_FH_FID		0x2 /* File handle may be non-decodeable */
179 #define EXPORT_FH_DIR_ONLY	0x4 /* Only decode file handle for a directory */
180 
181 /*
182  * Filesystems use only lower 8 bits of file_handle type for fid_type.
183  * name_to_handle_at() uses upper 16 bits of type as user flags to be
184  * interpreted by open_by_handle_at().
185  */
186 #define FILEID_USER_FLAGS_MASK	0xffff0000
187 #define FILEID_USER_FLAGS(type) ((type) & FILEID_USER_FLAGS_MASK)
188 
189 /* Flags supported in encoded handle_type that is exported to user */
190 #define FILEID_IS_CONNECTABLE	0x10000
191 #define FILEID_IS_DIR		0x20000
192 #define FILEID_VALID_USER_FLAGS	(FILEID_IS_CONNECTABLE | FILEID_IS_DIR)
193 
194 /**
195  * struct export_operations - for nfsd to communicate with file systems
196  * @encode_fh:      encode a file handle fragment from a dentry
197  * @fh_to_dentry:   find the implied object and get a dentry for it
198  * @fh_to_parent:   find the implied object's parent and get a dentry for it
199  * @get_name:       find the name for a given inode in a given directory
200  * @get_parent:     find the parent of a given directory
201  * @commit_metadata: commit metadata changes to stable storage
202  *
203  * Methods for open_by_handle(2) syscall with special kernel file systems:
204  * @permission:     custom permission for opening a file by handle
205  * @open:           custom open routine for opening file by handle
206  *
207  * See Documentation/filesystems/nfs/exporting.rst for details on how to use
208  * this interface correctly and the definition of the flags.
209  *
210  * @encode_fh:
211  *    @encode_fh should store in the file handle fragment @fh (using at most
212  *    @max_len bytes) information that can be used by @decode_fh to recover the
213  *    file referred to by the &struct dentry @de.  If @flag has CONNECTABLE bit
214  *    set, the encode_fh() should store sufficient information so that a good
215  *    attempt can be made to find not only the file but also it's place in the
216  *    filesystem.   This typically means storing a reference to de->d_parent in
217  *    the filehandle fragment.  encode_fh() should return the fileid_type on
218  *    success and on error returns 255 (if the space needed to encode fh is
219  *    greater than @max_len*4 bytes). On error @max_len contains the minimum
220  *    size(in 4 byte unit) needed to encode the file handle.
221  *
222  * @fh_to_dentry:
223  *    @fh_to_dentry is given a &struct super_block (@sb) and a file handle
224  *    fragment (@fh, @fh_len). It should return a &struct dentry which refers
225  *    to the same file that the file handle fragment refers to.  If it cannot,
226  *    it should return a %NULL pointer if the file cannot be found, or an
227  *    %ERR_PTR error code of %ENOMEM if a memory allocation failure occurred.
228  *    Any other error code is treated like %NULL, and will cause an %ESTALE error
229  *    for callers of exportfs_decode_fh().
230  *    Any suitable dentry can be returned including, if necessary, a new dentry
231  *    created with d_alloc_root.  The caller can then find any other extant
232  *    dentries by following the d_alias links.
233  *
234  * @fh_to_parent:
235  *    Same as @fh_to_dentry, except that it returns a pointer to the parent
236  *    dentry if it was encoded into the filehandle fragment by @encode_fh.
237  *
238  * @get_name:
239  *    @get_name should find a name for the given @child in the given @parent
240  *    directory.  The name should be stored in the @name (with the
241  *    understanding that it is already pointing to a %NAME_MAX + 1 sized
242  *    buffer.   get_name() should return %0 on success, a negative error code
243  *    or error.  @get_name will be called without @parent->i_rwsem held.
244  *
245  * @get_parent:
246  *    @get_parent should find the parent directory for the given @child which
247  *    is also a directory.  In the event that it cannot be found, or storage
248  *    space cannot be allocated, a %ERR_PTR should be returned.
249  *
250  * @permission:
251  *    Allow filesystems to specify a custom permission function for the
252  *    open_by_handle_at(2) syscall instead of the default permission check.
253  *    This custom permission function is not respected by nfsd.
254  *
255  * @open:
256  *    Allow filesystems to specify a custom open function for the
257  *    open_by_handle_at(2) syscall instead of the default file_open_root().
258  *    This custom open function is not respected by nfsd.
259  *
260  * @commit_metadata:
261  *    @commit_metadata should commit metadata changes to stable storage.
262  *
263  * @get_uuid:
264  *    Get a filesystem unique signature exposed to clients.
265  *
266  * @map_blocks:
267  *    Map and, if necessary, allocate blocks for a layout.
268  *
269  * @commit_blocks:
270  *    Commit blocks in a layout once the client is done with them.
271  *
272  * @flags:
273  *    Allows the filesystem to communicate to nfsd that it may want to do things
274  *    differently when dealing with it.
275  *
276  * Locking rules:
277  *    get_parent is called with child->d_inode->i_rwsem down
278  *    get_name is not (which is possibly inconsistent)
279  */
280 
281 struct export_operations {
282 	int (*encode_fh)(struct inode *inode, __u32 *fh, int *max_len,
283 			struct inode *parent);
284 	struct dentry * (*fh_to_dentry)(struct super_block *sb, struct fid *fid,
285 			int fh_len, int fh_type);
286 	struct dentry * (*fh_to_parent)(struct super_block *sb, struct fid *fid,
287 			int fh_len, int fh_type);
288 	int (*get_name)(struct dentry *parent, char *name,
289 			struct dentry *child);
290 	struct dentry * (*get_parent)(struct dentry *child);
291 	int (*commit_metadata)(struct inode *inode);
292 
293 	int (*get_uuid)(struct super_block *sb, u8 *buf, u32 *len, u64 *offset);
294 	int (*map_blocks)(struct inode *inode, loff_t offset,
295 			  u64 len, struct iomap *iomap,
296 			  bool write, u32 *device_generation);
297 	int (*commit_blocks)(struct inode *inode, struct iomap *iomaps,
298 			     int nr_iomaps, struct iattr *iattr);
299 	int (*permission)(struct handle_to_path_ctx *ctx, unsigned int oflags);
300 	struct file * (*open)(const struct path *path, unsigned int oflags);
301 #define	EXPORT_OP_NOWCC			(0x1) /* don't collect v3 wcc data */
302 #define	EXPORT_OP_NOSUBTREECHK		(0x2) /* no subtree checking */
303 #define	EXPORT_OP_CLOSE_BEFORE_UNLINK	(0x4) /* close files before unlink */
304 #define EXPORT_OP_REMOTE_FS		(0x8) /* Filesystem is remote */
305 #define EXPORT_OP_NOATOMIC_ATTR		(0x10) /* Filesystem cannot supply
306 						  atomic attribute updates
307 						*/
308 #define EXPORT_OP_FLUSH_ON_CLOSE	(0x20) /* fs flushes file data on close */
309 #define EXPORT_OP_NOLOCKS		(0x40) /* no file locking support */
310 	unsigned long	flags;
311 };
312 
313 /**
314  * exportfs_cannot_lock() - check if export implements file locking
315  * @export_ops:	the nfs export operations to check
316  *
317  * Returns true if the export does not support file locking.
318  */
319 static inline bool
exportfs_cannot_lock(const struct export_operations * export_ops)320 exportfs_cannot_lock(const struct export_operations *export_ops)
321 {
322 	return export_ops->flags & EXPORT_OP_NOLOCKS;
323 }
324 
325 extern int exportfs_encode_inode_fh(struct inode *inode, struct fid *fid,
326 				    int *max_len, struct inode *parent,
327 				    int flags);
328 extern int exportfs_encode_fh(struct dentry *dentry, struct fid *fid,
329 			      int *max_len, int flags);
330 
exportfs_can_encode_fid(const struct export_operations * nop)331 static inline bool exportfs_can_encode_fid(const struct export_operations *nop)
332 {
333 	return !nop || nop->encode_fh;
334 }
335 
exportfs_can_decode_fh(const struct export_operations * nop)336 static inline bool exportfs_can_decode_fh(const struct export_operations *nop)
337 {
338 	return nop && nop->fh_to_dentry;
339 }
340 
exportfs_may_export(const struct export_operations * nop)341 static inline bool exportfs_may_export(const struct export_operations *nop)
342 {
343 	/*
344 	 * Do not allow nfs export for filesystems with custom ->open() or
345 	 * ->permission() ops, which nfsd does not respect (e.g. pidfs, nsfs).
346 	 */
347 	return exportfs_can_decode_fh(nop) && !nop->open && !nop->permission;
348 }
349 
exportfs_can_encode_fh(const struct export_operations * nop,int fh_flags)350 static inline bool exportfs_can_encode_fh(const struct export_operations *nop,
351 					  int fh_flags)
352 {
353 	/*
354 	 * If a non-decodeable file handle was requested, we only need to make
355 	 * sure that filesystem did not opt-out of encoding fid.
356 	 */
357 	if (fh_flags & EXPORT_FH_FID)
358 		return exportfs_can_encode_fid(nop);
359 
360 	/* Normal file handles cannot be created without export ops */
361 	if (!nop)
362 		return false;
363 
364 	/*
365 	 * If a connectable file handle was requested, we need to make sure that
366 	 * filesystem can also decode connected file handles.
367 	 */
368 	if ((fh_flags & EXPORT_FH_CONNECTABLE) && !nop->fh_to_parent)
369 		return false;
370 
371 	/*
372 	 * If a decodeable file handle was requested, we need to make sure that
373 	 * filesystem can also decode file handles.
374 	 */
375 	return exportfs_can_decode_fh(nop);
376 }
377 
exportfs_encode_fid(struct inode * inode,struct fid * fid,int * max_len)378 static inline int exportfs_encode_fid(struct inode *inode, struct fid *fid,
379 				      int *max_len)
380 {
381 	return exportfs_encode_inode_fh(inode, fid, max_len, NULL,
382 					EXPORT_FH_FID);
383 }
384 
385 extern struct dentry *exportfs_decode_fh_raw(struct vfsmount *mnt,
386 					     struct fid *fid, int fh_len,
387 					     int fileid_type,
388 					     unsigned int flags,
389 					     int (*acceptable)(void *, struct dentry *),
390 					     void *context);
391 extern struct dentry *exportfs_decode_fh(struct vfsmount *mnt, struct fid *fid,
392 	int fh_len, int fileid_type, int (*acceptable)(void *, struct dentry *),
393 	void *context);
394 
395 /*
396  * Generic helpers for filesystems.
397  */
398 int generic_encode_ino32_fh(struct inode *inode, __u32 *fh, int *max_len,
399 			    struct inode *parent);
400 struct dentry *generic_fh_to_dentry(struct super_block *sb,
401 	struct fid *fid, int fh_len, int fh_type,
402 	struct inode *(*get_inode) (struct super_block *sb, u64 ino, u32 gen));
403 struct dentry *generic_fh_to_parent(struct super_block *sb,
404 	struct fid *fid, int fh_len, int fh_type,
405 	struct inode *(*get_inode) (struct super_block *sb, u64 ino, u32 gen));
406 
407 #endif /* LINUX_EXPORTFS_H */
408