xref: /openbmc/linux/fs/xfs/xfs_export.c (revision 750afb08)
1 // SPDX-License-Identifier: GPL-2.0
2 /*
3  * Copyright (c) 2004-2005 Silicon Graphics, Inc.
4  * All Rights Reserved.
5  */
6 #include "xfs.h"
7 #include "xfs_format.h"
8 #include "xfs_log_format.h"
9 #include "xfs_trans_resv.h"
10 #include "xfs_mount.h"
11 #include "xfs_da_format.h"
12 #include "xfs_da_btree.h"
13 #include "xfs_dir2.h"
14 #include "xfs_export.h"
15 #include "xfs_inode.h"
16 #include "xfs_trans.h"
17 #include "xfs_inode_item.h"
18 #include "xfs_trace.h"
19 #include "xfs_icache.h"
20 #include "xfs_log.h"
21 #include "xfs_pnfs.h"
22 
23 /*
24  * Note that we only accept fileids which are long enough rather than allow
25  * the parent generation number to default to zero.  XFS considers zero a
26  * valid generation number not an invalid/wildcard value.
27  */
28 static int xfs_fileid_length(int fileid_type)
29 {
30 	switch (fileid_type) {
31 	case FILEID_INO32_GEN:
32 		return 2;
33 	case FILEID_INO32_GEN_PARENT:
34 		return 4;
35 	case FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG:
36 		return 3;
37 	case FILEID_INO32_GEN_PARENT | XFS_FILEID_TYPE_64FLAG:
38 		return 6;
39 	}
40 	return FILEID_INVALID;
41 }
42 
43 STATIC int
44 xfs_fs_encode_fh(
45 	struct inode	*inode,
46 	__u32		*fh,
47 	int		*max_len,
48 	struct inode	*parent)
49 {
50 	struct fid		*fid = (struct fid *)fh;
51 	struct xfs_fid64	*fid64 = (struct xfs_fid64 *)fh;
52 	int			fileid_type;
53 	int			len;
54 
55 	/* Directories don't need their parent encoded, they have ".." */
56 	if (!parent)
57 		fileid_type = FILEID_INO32_GEN;
58 	else
59 		fileid_type = FILEID_INO32_GEN_PARENT;
60 
61 	/*
62 	 * If the the filesystem may contain 64bit inode numbers, we need
63 	 * to use larger file handles that can represent them.
64 	 *
65 	 * While we only allocate inodes that do not fit into 32 bits any
66 	 * large enough filesystem may contain them, thus the slightly
67 	 * confusing looking conditional below.
68 	 */
69 	if (!(XFS_M(inode->i_sb)->m_flags & XFS_MOUNT_SMALL_INUMS) ||
70 	    (XFS_M(inode->i_sb)->m_flags & XFS_MOUNT_32BITINODES))
71 		fileid_type |= XFS_FILEID_TYPE_64FLAG;
72 
73 	/*
74 	 * Only encode if there is enough space given.  In practice
75 	 * this means we can't export a filesystem with 64bit inodes
76 	 * over NFSv2 with the subtree_check export option; the other
77 	 * seven combinations work.  The real answer is "don't use v2".
78 	 */
79 	len = xfs_fileid_length(fileid_type);
80 	if (*max_len < len) {
81 		*max_len = len;
82 		return FILEID_INVALID;
83 	}
84 	*max_len = len;
85 
86 	switch (fileid_type) {
87 	case FILEID_INO32_GEN_PARENT:
88 		fid->i32.parent_ino = XFS_I(parent)->i_ino;
89 		fid->i32.parent_gen = parent->i_generation;
90 		/*FALLTHRU*/
91 	case FILEID_INO32_GEN:
92 		fid->i32.ino = XFS_I(inode)->i_ino;
93 		fid->i32.gen = inode->i_generation;
94 		break;
95 	case FILEID_INO32_GEN_PARENT | XFS_FILEID_TYPE_64FLAG:
96 		fid64->parent_ino = XFS_I(parent)->i_ino;
97 		fid64->parent_gen = parent->i_generation;
98 		/*FALLTHRU*/
99 	case FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG:
100 		fid64->ino = XFS_I(inode)->i_ino;
101 		fid64->gen = inode->i_generation;
102 		break;
103 	}
104 
105 	return fileid_type;
106 }
107 
108 STATIC struct inode *
109 xfs_nfs_get_inode(
110 	struct super_block	*sb,
111 	u64			ino,
112 	u32			generation)
113 {
114  	xfs_mount_t		*mp = XFS_M(sb);
115 	xfs_inode_t		*ip;
116 	int			error;
117 
118 	/*
119 	 * NFS can sometimes send requests for ino 0.  Fail them gracefully.
120 	 */
121 	if (ino == 0)
122 		return ERR_PTR(-ESTALE);
123 
124 	/*
125 	 * The XFS_IGET_UNTRUSTED means that an invalid inode number is just
126 	 * fine and not an indication of a corrupted filesystem as clients can
127 	 * send invalid file handles and we have to handle it gracefully..
128 	 */
129 	error = xfs_iget(mp, NULL, ino, XFS_IGET_UNTRUSTED, 0, &ip);
130 	if (error) {
131 
132 		/*
133 		 * EINVAL means the inode cluster doesn't exist anymore.
134 		 * EFSCORRUPTED means the metadata pointing to the inode cluster
135 		 * or the inode cluster itself is corrupt.  This implies the
136 		 * filehandle is stale, so we should translate it here.
137 		 * We don't use ESTALE directly down the chain to not
138 		 * confuse applications using bulkstat that expect EINVAL.
139 		 */
140 		switch (error) {
141 		case -EINVAL:
142 		case -ENOENT:
143 		case -EFSCORRUPTED:
144 			error = -ESTALE;
145 			break;
146 		default:
147 			break;
148 		}
149 		return ERR_PTR(error);
150 	}
151 
152 	if (VFS_I(ip)->i_generation != generation) {
153 		xfs_irele(ip);
154 		return ERR_PTR(-ESTALE);
155 	}
156 
157 	return VFS_I(ip);
158 }
159 
160 STATIC struct dentry *
161 xfs_fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
162 		 int fh_len, int fileid_type)
163 {
164 	struct xfs_fid64	*fid64 = (struct xfs_fid64 *)fid;
165 	struct inode		*inode = NULL;
166 
167 	if (fh_len < xfs_fileid_length(fileid_type))
168 		return NULL;
169 
170 	switch (fileid_type) {
171 	case FILEID_INO32_GEN_PARENT:
172 	case FILEID_INO32_GEN:
173 		inode = xfs_nfs_get_inode(sb, fid->i32.ino, fid->i32.gen);
174 		break;
175 	case FILEID_INO32_GEN_PARENT | XFS_FILEID_TYPE_64FLAG:
176 	case FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG:
177 		inode = xfs_nfs_get_inode(sb, fid64->ino, fid64->gen);
178 		break;
179 	}
180 
181 	return d_obtain_alias(inode);
182 }
183 
184 STATIC struct dentry *
185 xfs_fs_fh_to_parent(struct super_block *sb, struct fid *fid,
186 		 int fh_len, int fileid_type)
187 {
188 	struct xfs_fid64	*fid64 = (struct xfs_fid64 *)fid;
189 	struct inode		*inode = NULL;
190 
191 	if (fh_len < xfs_fileid_length(fileid_type))
192 		return NULL;
193 
194 	switch (fileid_type) {
195 	case FILEID_INO32_GEN_PARENT:
196 		inode = xfs_nfs_get_inode(sb, fid->i32.parent_ino,
197 					      fid->i32.parent_gen);
198 		break;
199 	case FILEID_INO32_GEN_PARENT | XFS_FILEID_TYPE_64FLAG:
200 		inode = xfs_nfs_get_inode(sb, fid64->parent_ino,
201 					      fid64->parent_gen);
202 		break;
203 	}
204 
205 	return d_obtain_alias(inode);
206 }
207 
208 STATIC struct dentry *
209 xfs_fs_get_parent(
210 	struct dentry		*child)
211 {
212 	int			error;
213 	struct xfs_inode	*cip;
214 
215 	error = xfs_lookup(XFS_I(d_inode(child)), &xfs_name_dotdot, &cip, NULL);
216 	if (unlikely(error))
217 		return ERR_PTR(error);
218 
219 	return d_obtain_alias(VFS_I(cip));
220 }
221 
222 STATIC int
223 xfs_fs_nfs_commit_metadata(
224 	struct inode		*inode)
225 {
226 	struct xfs_inode	*ip = XFS_I(inode);
227 	struct xfs_mount	*mp = ip->i_mount;
228 	xfs_lsn_t		lsn = 0;
229 
230 	xfs_ilock(ip, XFS_ILOCK_SHARED);
231 	if (xfs_ipincount(ip))
232 		lsn = ip->i_itemp->ili_last_lsn;
233 	xfs_iunlock(ip, XFS_ILOCK_SHARED);
234 
235 	if (!lsn)
236 		return 0;
237 	return xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, NULL);
238 }
239 
240 const struct export_operations xfs_export_operations = {
241 	.encode_fh		= xfs_fs_encode_fh,
242 	.fh_to_dentry		= xfs_fs_fh_to_dentry,
243 	.fh_to_parent		= xfs_fs_fh_to_parent,
244 	.get_parent		= xfs_fs_get_parent,
245 	.commit_metadata	= xfs_fs_nfs_commit_metadata,
246 #ifdef CONFIG_EXPORTFS_BLOCK_OPS
247 	.get_uuid		= xfs_fs_get_uuid,
248 	.map_blocks		= xfs_fs_map_blocks,
249 	.commit_blocks		= xfs_fs_commit_blocks,
250 #endif
251 };
252