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