xref: /openbmc/linux/include/linux/exportfs.h (revision 304e9c83)
1b2441318SGreg Kroah-Hartman /* SPDX-License-Identifier: GPL-2.0 */
2a5694255SChristoph Hellwig #ifndef LINUX_EXPORTFS_H
3a5694255SChristoph Hellwig #define LINUX_EXPORTFS_H 1
4a5694255SChristoph Hellwig 
5a5694255SChristoph Hellwig #include <linux/types.h>
6a5694255SChristoph Hellwig 
7a5694255SChristoph Hellwig struct dentry;
87fbc1067SChristoph Hellwig struct iattr;
92596110aSChristoph Hellwig struct inode;
10199a31c6SChristoph Hellwig struct iomap;
11a5694255SChristoph Hellwig struct super_block;
12d37065cdSChristoph Hellwig struct vfsmount;
13a5694255SChristoph Hellwig 
14990d6c2dSAneesh Kumar K.V /* limit the handle size to NFSv4 handle size now */
15990d6c2dSAneesh Kumar K.V #define MAX_HANDLE_SZ 128
16990d6c2dSAneesh Kumar K.V 
176e91ea2bSChristoph Hellwig /*
186e91ea2bSChristoph Hellwig  * The fileid_type identifies how the file within the filesystem is encoded.
196e91ea2bSChristoph Hellwig  * In theory this is freely set and parsed by the filesystem, but we try to
206e91ea2bSChristoph Hellwig  * stick to conventions so we can share some generic code and don't confuse
216e91ea2bSChristoph Hellwig  * sniffers like ethereal/wireshark.
226e91ea2bSChristoph Hellwig  *
236e91ea2bSChristoph Hellwig  * The filesystem must not use the value '0' or '0xff'.
246e91ea2bSChristoph Hellwig  */
256e91ea2bSChristoph Hellwig enum fid_type {
266e91ea2bSChristoph Hellwig 	/*
276e91ea2bSChristoph Hellwig 	 * The root, or export point, of the filesystem.
286e91ea2bSChristoph Hellwig 	 * (Never actually passed down to the filesystem.
296e91ea2bSChristoph Hellwig 	 */
306e91ea2bSChristoph Hellwig 	FILEID_ROOT = 0,
316e91ea2bSChristoph Hellwig 
326e91ea2bSChristoph Hellwig 	/*
336e91ea2bSChristoph Hellwig 	 * 32bit inode number, 32 bit generation number.
346e91ea2bSChristoph Hellwig 	 */
356e91ea2bSChristoph Hellwig 	FILEID_INO32_GEN = 1,
366e91ea2bSChristoph Hellwig 
376e91ea2bSChristoph Hellwig 	/*
386e91ea2bSChristoph Hellwig 	 * 32bit inode number, 32 bit generation number,
396e91ea2bSChristoph Hellwig 	 * 32 bit parent directory inode number.
406e91ea2bSChristoph Hellwig 	 */
416e91ea2bSChristoph Hellwig 	FILEID_INO32_GEN_PARENT = 2,
42221e583aSRasmus Rohde 
43221e583aSRasmus Rohde 	/*
44e4464facSDavid Woodhouse 	 * 64 bit object ID, 64 bit root object ID,
45e4464facSDavid Woodhouse 	 * 32 bit generation number.
46e4464facSDavid Woodhouse 	 */
47e4464facSDavid Woodhouse 	FILEID_BTRFS_WITHOUT_PARENT = 0x4d,
48e4464facSDavid Woodhouse 
49e4464facSDavid Woodhouse 	/*
50e4464facSDavid Woodhouse 	 * 64 bit object ID, 64 bit root object ID,
51e4464facSDavid Woodhouse 	 * 32 bit generation number,
52e4464facSDavid Woodhouse 	 * 64 bit parent object ID, 32 bit parent generation.
53e4464facSDavid Woodhouse 	 */
54e4464facSDavid Woodhouse 	FILEID_BTRFS_WITH_PARENT = 0x4e,
55e4464facSDavid Woodhouse 
56e4464facSDavid Woodhouse 	/*
57e4464facSDavid Woodhouse 	 * 64 bit object ID, 64 bit root object ID,
58e4464facSDavid Woodhouse 	 * 32 bit generation number,
59e4464facSDavid Woodhouse 	 * 64 bit parent object ID, 32 bit parent generation,
60e4464facSDavid Woodhouse 	 * 64 bit parent root object ID.
61e4464facSDavid Woodhouse 	 */
62e4464facSDavid Woodhouse 	FILEID_BTRFS_WITH_PARENT_ROOT = 0x4f,
63e4464facSDavid Woodhouse 
64e4464facSDavid Woodhouse 	/*
65221e583aSRasmus Rohde 	 * 32 bit block number, 16 bit partition reference,
66221e583aSRasmus Rohde 	 * 16 bit unused, 32 bit generation number.
67221e583aSRasmus Rohde 	 */
68221e583aSRasmus Rohde 	FILEID_UDF_WITHOUT_PARENT = 0x51,
69221e583aSRasmus Rohde 
70221e583aSRasmus Rohde 	/*
71221e583aSRasmus Rohde 	 * 32 bit block number, 16 bit partition reference,
72221e583aSRasmus Rohde 	 * 16 bit unused, 32 bit generation number,
73221e583aSRasmus Rohde 	 * 32 bit parent block number, 32 bit parent generation number
74221e583aSRasmus Rohde 	 */
75221e583aSRasmus Rohde 	FILEID_UDF_WITH_PARENT = 0x52,
768e656fd5SRyusuke Konishi 
778e656fd5SRyusuke Konishi 	/*
788e656fd5SRyusuke Konishi 	 * 64 bit checkpoint number, 64 bit inode number,
798e656fd5SRyusuke Konishi 	 * 32 bit generation number.
808e656fd5SRyusuke Konishi 	 */
818e656fd5SRyusuke Konishi 	FILEID_NILFS_WITHOUT_PARENT = 0x61,
828e656fd5SRyusuke Konishi 
838e656fd5SRyusuke Konishi 	/*
848e656fd5SRyusuke Konishi 	 * 64 bit checkpoint number, 64 bit inode number,
858e656fd5SRyusuke Konishi 	 * 32 bit generation number, 32 bit parent generation.
868e656fd5SRyusuke Konishi 	 * 64 bit parent inode number.
878e656fd5SRyusuke Konishi 	 */
888e656fd5SRyusuke Konishi 	FILEID_NILFS_WITH_PARENT = 0x62,
89216b6cbdSNamjae Jeon 
90216b6cbdSNamjae Jeon 	/*
91ea3983acSNamjae Jeon 	 * 32 bit generation number, 40 bit i_pos.
92ea3983acSNamjae Jeon 	 */
93ea3983acSNamjae Jeon 	FILEID_FAT_WITHOUT_PARENT = 0x71,
94ea3983acSNamjae Jeon 
95ea3983acSNamjae Jeon 	/*
96ea3983acSNamjae Jeon 	 * 32 bit generation number, 40 bit i_pos,
97ea3983acSNamjae Jeon 	 * 32 bit parent generation number, 40 bit parent i_pos
98ea3983acSNamjae Jeon 	 */
99ea3983acSNamjae Jeon 	FILEID_FAT_WITH_PARENT = 0x72,
100ea3983acSNamjae Jeon 
101ea3983acSNamjae Jeon 	/*
102cb776592SDmitry Eremin 	 * 128 bit child FID (struct lu_fid)
103cb776592SDmitry Eremin 	 * 128 bit parent FID (struct lu_fid)
104cb776592SDmitry Eremin 	 */
105cb776592SDmitry Eremin 	FILEID_LUSTRE = 0x97,
106cb776592SDmitry Eremin 
107cb776592SDmitry Eremin 	/*
10833c5ac91STejun Heo 	 * 64 bit unique kernfs id
10933c5ac91STejun Heo 	 */
11033c5ac91STejun Heo 	FILEID_KERNFS = 0xfe,
11133c5ac91STejun Heo 
11233c5ac91STejun Heo 	/*
113216b6cbdSNamjae Jeon 	 * Filesystems must not use 0xff file ID.
114216b6cbdSNamjae Jeon 	 */
115216b6cbdSNamjae Jeon 	FILEID_INVALID = 0xff,
1166e91ea2bSChristoph Hellwig };
1176e91ea2bSChristoph Hellwig 
1186e91ea2bSChristoph Hellwig struct fid {
1196e91ea2bSChristoph Hellwig 	union {
1206e91ea2bSChristoph Hellwig 		struct {
1216e91ea2bSChristoph Hellwig 			u32 ino;
1226e91ea2bSChristoph Hellwig 			u32 gen;
1236e91ea2bSChristoph Hellwig 			u32 parent_ino;
1246e91ea2bSChristoph Hellwig 			u32 parent_gen;
1256e91ea2bSChristoph Hellwig 		} i32;
126221e583aSRasmus Rohde  		struct {
127221e583aSRasmus Rohde  			u32 block;
128221e583aSRasmus Rohde  			u16 partref;
129221e583aSRasmus Rohde  			u16 parent_partref;
130221e583aSRasmus Rohde  			u32 generation;
131221e583aSRasmus Rohde  			u32 parent_block;
132221e583aSRasmus Rohde  			u32 parent_generation;
133221e583aSRasmus Rohde  		} udf;
13437e2b570SGustavo A. R. Silva 		DECLARE_FLEX_ARRAY(__u32, raw);
1356e91ea2bSChristoph Hellwig 	};
1366e91ea2bSChristoph Hellwig };
137a5694255SChristoph Hellwig 
138b5287827SAmir Goldstein #define EXPORT_FH_CONNECTABLE	0x1 /* Encode file handle with parent */
139*304e9c83SAmir Goldstein #define EXPORT_FH_FID		0x2 /* File handle may be non-decodeable */
140b5287827SAmir Goldstein 
141a5694255SChristoph Hellwig /**
142a5694255SChristoph Hellwig  * struct export_operations - for nfsd to communicate with file systems
143a5694255SChristoph Hellwig  * @encode_fh:      encode a file handle fragment from a dentry
1449b89ca7aSMarc Dionne  * @fh_to_dentry:   find the implied object and get a dentry for it
1459b89ca7aSMarc Dionne  * @fh_to_parent:   find the implied object's parent and get a dentry for it
146a5694255SChristoph Hellwig  * @get_name:       find the name for a given inode in a given directory
147a5694255SChristoph Hellwig  * @get_parent:     find the parent of a given directory
148f501912aSBen Myers  * @commit_metadata: commit metadata changes to stable storage
149a5694255SChristoph Hellwig  *
150ec23eb54SMauro Carvalho Chehab  * See Documentation/filesystems/nfs/exporting.rst for details on how to use
151e38f9817SChristoph Hellwig  * this interface correctly.
152a5694255SChristoph Hellwig  *
153a5694255SChristoph Hellwig  * encode_fh:
154a5694255SChristoph Hellwig  *    @encode_fh should store in the file handle fragment @fh (using at most
155a5694255SChristoph Hellwig  *    @max_len bytes) information that can be used by @decode_fh to recover the
156b5287827SAmir Goldstein  *    file referred to by the &struct dentry @de.  If @flag has CONNECTABLE bit
157a5694255SChristoph Hellwig  *    set, the encode_fh() should store sufficient information so that a good
158a5694255SChristoph Hellwig  *    attempt can be made to find not only the file but also it's place in the
159a5694255SChristoph Hellwig  *    filesystem.   This typically means storing a reference to de->d_parent in
1605fe0c237SAneesh Kumar K.V  *    the filehandle fragment.  encode_fh() should return the fileid_type on
1615fe0c237SAneesh Kumar K.V  *    success and on error returns 255 (if the space needed to encode fh is
1625fe0c237SAneesh Kumar K.V  *    greater than @max_len*4 bytes). On error @max_len contains the minimum
1635fe0c237SAneesh Kumar K.V  *    size(in 4 byte unit) needed to encode the file handle.
164a5694255SChristoph Hellwig  *
1652596110aSChristoph Hellwig  * fh_to_dentry:
1662596110aSChristoph Hellwig  *    @fh_to_dentry is given a &struct super_block (@sb) and a file handle
1672596110aSChristoph Hellwig  *    fragment (@fh, @fh_len). It should return a &struct dentry which refers
1682596110aSChristoph Hellwig  *    to the same file that the file handle fragment refers to.  If it cannot,
16909bb8bffSNeilBrown  *    it should return a %NULL pointer if the file cannot be found, or an
17009bb8bffSNeilBrown  *    %ERR_PTR error code of %ENOMEM if a memory allocation failure occurred.
17109bb8bffSNeilBrown  *    Any other error code is treated like %NULL, and will cause an %ESTALE error
17209bb8bffSNeilBrown  *    for callers of exportfs_decode_fh().
17309bb8bffSNeilBrown  *    Any suitable dentry can be returned including, if necessary, a new dentry
17409bb8bffSNeilBrown  *    created with d_alloc_root.  The caller can then find any other extant
17509bb8bffSNeilBrown  *    dentries by following the d_alias links.
1762596110aSChristoph Hellwig  *
1772596110aSChristoph Hellwig  * fh_to_parent:
1782596110aSChristoph Hellwig  *    Same as @fh_to_dentry, except that it returns a pointer to the parent
1792596110aSChristoph Hellwig  *    dentry if it was encoded into the filehandle fragment by @encode_fh.
1802596110aSChristoph Hellwig  *
181a5694255SChristoph Hellwig  * get_name:
182a5694255SChristoph Hellwig  *    @get_name should find a name for the given @child in the given @parent
183a5694255SChristoph Hellwig  *    directory.  The name should be stored in the @name (with the
184cd1a406fSRandy Dunlap  *    understanding that it is already pointing to a %NAME_MAX+1 sized
185a5694255SChristoph Hellwig  *    buffer.   get_name() should return %0 on success, a negative error code
186a5694255SChristoph Hellwig  *    or error.  @get_name will be called without @parent->i_mutex held.
187a5694255SChristoph Hellwig  *
188a5694255SChristoph Hellwig  * get_parent:
189a5694255SChristoph Hellwig  *    @get_parent should find the parent directory for the given @child which
190a5694255SChristoph Hellwig  *    is also a directory.  In the event that it cannot be found, or storage
191a5694255SChristoph Hellwig  *    space cannot be allocated, a %ERR_PTR should be returned.
192a5694255SChristoph Hellwig  *
193f501912aSBen Myers  * commit_metadata:
194f501912aSBen Myers  *    @commit_metadata should commit metadata changes to stable storage.
195f501912aSBen Myers  *
196a5694255SChristoph Hellwig  * Locking rules:
197a5694255SChristoph Hellwig  *    get_parent is called with child->d_inode->i_mutex down
198a5694255SChristoph Hellwig  *    get_name is not (which is possibly inconsistent)
199a5694255SChristoph Hellwig  */
200a5694255SChristoph Hellwig 
201a5694255SChristoph Hellwig struct export_operations {
202b0b0382bSAl Viro 	int (*encode_fh)(struct inode *inode, __u32 *fh, int *max_len,
203b0b0382bSAl Viro 			struct inode *parent);
2042596110aSChristoph Hellwig 	struct dentry * (*fh_to_dentry)(struct super_block *sb, struct fid *fid,
2052596110aSChristoph Hellwig 			int fh_len, int fh_type);
2062596110aSChristoph Hellwig 	struct dentry * (*fh_to_parent)(struct super_block *sb, struct fid *fid,
2072596110aSChristoph Hellwig 			int fh_len, int fh_type);
208a5694255SChristoph Hellwig 	int (*get_name)(struct dentry *parent, char *name,
209a5694255SChristoph Hellwig 			struct dentry *child);
210a5694255SChristoph Hellwig 	struct dentry * (*get_parent)(struct dentry *child);
211f501912aSBen Myers 	int (*commit_metadata)(struct inode *inode);
2127fbc1067SChristoph Hellwig 
2137fbc1067SChristoph Hellwig 	int (*get_uuid)(struct super_block *sb, u8 *buf, u32 *len, u64 *offset);
2147fbc1067SChristoph Hellwig 	int (*map_blocks)(struct inode *inode, loff_t offset,
2157fbc1067SChristoph Hellwig 			  u64 len, struct iomap *iomap,
2167fbc1067SChristoph Hellwig 			  bool write, u32 *device_generation);
2177fbc1067SChristoph Hellwig 	int (*commit_blocks)(struct inode *inode, struct iomap *iomaps,
2187fbc1067SChristoph Hellwig 			     int nr_iomaps, struct iattr *iattr);
2197f84b488SJeff Layton #define	EXPORT_OP_NOWCC			(0x1) /* don't collect v3 wcc data */
2207f84b488SJeff Layton #define	EXPORT_OP_NOSUBTREECHK		(0x2) /* no subtree checking */
2217f84b488SJeff Layton #define	EXPORT_OP_CLOSE_BEFORE_UNLINK	(0x4) /* close files before unlink */
22201cbf385STrond Myklebust #define EXPORT_OP_REMOTE_FS		(0x8) /* Filesystem is remote */
223716a8bc7STrond Myklebust #define EXPORT_OP_NOATOMIC_ATTR		(0x10) /* Filesystem cannot supply
224716a8bc7STrond Myklebust 						  atomic attribute updates
225716a8bc7STrond Myklebust 						*/
226dcb779fcSJeff Layton #define EXPORT_OP_FLUSH_ON_CLOSE	(0x20) /* fs flushes file data on close */
227daab110eSJeff Layton 	unsigned long	flags;
228a5694255SChristoph Hellwig };
229a5694255SChristoph Hellwig 
230711c7bf9SCyrill Gorcunov extern int exportfs_encode_inode_fh(struct inode *inode, struct fid *fid,
231*304e9c83SAmir Goldstein 				    int *max_len, struct inode *parent,
232*304e9c83SAmir Goldstein 				    int flags);
2336e91ea2bSChristoph Hellwig extern int exportfs_encode_fh(struct dentry *dentry, struct fid *fid,
234b5287827SAmir Goldstein 			      int *max_len, int flags);
235*304e9c83SAmir Goldstein 
exportfs_encode_fid(struct inode * inode,struct fid * fid,int * max_len)236*304e9c83SAmir Goldstein static inline int exportfs_encode_fid(struct inode *inode, struct fid *fid,
237*304e9c83SAmir Goldstein 				      int *max_len)
238*304e9c83SAmir Goldstein {
239*304e9c83SAmir Goldstein 	return exportfs_encode_inode_fh(inode, fid, max_len, NULL,
240*304e9c83SAmir Goldstein 					EXPORT_FH_FID);
241*304e9c83SAmir Goldstein }
242*304e9c83SAmir Goldstein 
243d045465fSTrond Myklebust extern struct dentry *exportfs_decode_fh_raw(struct vfsmount *mnt,
244d045465fSTrond Myklebust 					     struct fid *fid, int fh_len,
245d045465fSTrond Myklebust 					     int fileid_type,
246d045465fSTrond Myklebust 					     int (*acceptable)(void *, struct dentry *),
247d045465fSTrond Myklebust 					     void *context);
2486e91ea2bSChristoph Hellwig extern struct dentry *exportfs_decode_fh(struct vfsmount *mnt, struct fid *fid,
249d37065cdSChristoph Hellwig 	int fh_len, int fileid_type, int (*acceptable)(void *, struct dentry *),
250d37065cdSChristoph Hellwig 	void *context);
251d37065cdSChristoph Hellwig 
2522596110aSChristoph Hellwig /*
2532596110aSChristoph Hellwig  * Generic helpers for filesystems.
2542596110aSChristoph Hellwig  */
2552596110aSChristoph Hellwig extern struct dentry *generic_fh_to_dentry(struct super_block *sb,
2562596110aSChristoph Hellwig 	struct fid *fid, int fh_len, int fh_type,
2572596110aSChristoph Hellwig 	struct inode *(*get_inode) (struct super_block *sb, u64 ino, u32 gen));
2582596110aSChristoph Hellwig extern struct dentry *generic_fh_to_parent(struct super_block *sb,
2592596110aSChristoph Hellwig 	struct fid *fid, int fh_len, int fh_type,
2602596110aSChristoph Hellwig 	struct inode *(*get_inode) (struct super_block *sb, u64 ino, u32 gen));
2612596110aSChristoph Hellwig 
262a5694255SChristoph Hellwig #endif /* LINUX_EXPORTFS_H */
263