blob: 1064c2342876807837bf400a87ac9d296bfec9fa [file] [log] [blame]
Dave Chinner0b61f8a2018-06-05 19:42:14 -07001// SPDX-License-Identifier: GPL-2.0
Linus Torvalds1da177e2005-04-16 15:20:36 -07002/*
Nathan Scott7b718762005-11-02 14:58:39 +11003 * Copyright (c) 2004-2005 Silicon Graphics, Inc.
4 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07005 */
Linus Torvalds1da177e2005-04-16 15:20:36 -07006#include "xfs.h"
Darrick J. Wong5467b342019-06-28 19:25:35 -07007#include "xfs_shared.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +11008#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +11009#include "xfs_log_format.h"
10#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070011#include "xfs_mount.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100012#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070013#include "xfs_export.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100014#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110015#include "xfs_trans.h"
Ben Myers978ebd92010-02-17 14:05:16 -060016#include "xfs_inode_item.h"
Dave Chinner33479e02012-10-08 21:56:11 +110017#include "xfs_icache.h"
Christoph Hellwig52785112015-02-16 11:49:23 +110018#include "xfs_pnfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070019
20/*
Christoph Hellwigc38344f2007-10-21 16:42:11 -070021 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070024 */
Christoph Hellwigc38344f2007-10-21 16:42:11 -070025static int xfs_fileid_length(int fileid_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070026{
Christoph Hellwigc38344f2007-10-21 16:42:11 -070027 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070036 }
Namjae Jeon94e07a752013-02-17 15:48:11 +090037 return FILEID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070038}
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040STATIC int
Nathan Scotta50cd262006-03-14 14:06:18 +110041xfs_fs_encode_fh(
Al Virob0b03822012-04-02 14:34:06 -040042 struct inode *inode,
43 __u32 *fh,
44 int *max_len,
45 struct inode *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070046{
Dave Chinner2e973b22021-08-18 18:46:52 -070047 struct xfs_mount *mp = XFS_M(inode->i_sb);
Christoph Hellwigc38344f2007-10-21 16:42:11 -070048 struct fid *fid = (struct fid *)fh;
49 struct xfs_fid64 *fid64 = (struct xfs_fid64 *)fh;
Christoph Hellwigc38344f2007-10-21 16:42:11 -070050 int fileid_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070052
53 /* Directories don't need their parent encoded, they have ".." */
Al Virob0b03822012-04-02 14:34:06 -040054 if (!parent)
Christoph Hellwigc38344f2007-10-21 16:42:11 -070055 fileid_type = FILEID_INO32_GEN;
56 else
57 fileid_type = FILEID_INO32_GEN_PARENT;
58
Samuel Kvasnica576ecb82010-11-19 13:38:49 +000059 /*
Randy Dunlapb63da6c2020-08-05 08:49:58 -070060 * If the filesystem may contain 64bit inode numbers, we need
Samuel Kvasnica576ecb82010-11-19 13:38:49 +000061 * 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 */
Dave Chinner2e973b22021-08-18 18:46:52 -070067 if (!xfs_has_small_inums(mp) || xfs_is_inode32(mp))
Christoph Hellwigc38344f2007-10-21 16:42:11 -070068 fileid_type |= XFS_FILEID_TYPE_64FLAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070069
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 */
Christoph Hellwigc38344f2007-10-21 16:42:11 -070076 len = xfs_fileid_length(fileid_type);
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +053077 if (*max_len < len) {
78 *max_len = len;
Namjae Jeon94e07a752013-02-17 15:48:11 +090079 return FILEID_INVALID;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +053080 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 *max_len = len;
82
Christoph Hellwigc38344f2007-10-21 16:42:11 -070083 switch (fileid_type) {
84 case FILEID_INO32_GEN_PARENT:
Al Virob0b03822012-04-02 14:34:06 -040085 fid->i32.parent_ino = XFS_I(parent)->i_ino;
86 fid->i32.parent_gen = parent->i_generation;
Gustavo A. R. Silva53004ee2021-04-20 17:54:36 -050087 fallthrough;
Christoph Hellwigc38344f2007-10-21 16:42:11 -070088 case FILEID_INO32_GEN:
Christoph Hellwigc29f7d42011-11-30 08:58:18 +000089 fid->i32.ino = XFS_I(inode)->i_ino;
Christoph Hellwigc38344f2007-10-21 16:42:11 -070090 fid->i32.gen = inode->i_generation;
91 break;
92 case FILEID_INO32_GEN_PARENT | XFS_FILEID_TYPE_64FLAG:
Al Virob0b03822012-04-02 14:34:06 -040093 fid64->parent_ino = XFS_I(parent)->i_ino;
94 fid64->parent_gen = parent->i_generation;
Gustavo A. R. Silva53004ee2021-04-20 17:54:36 -050095 fallthrough;
Christoph Hellwigc38344f2007-10-21 16:42:11 -070096 case FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG:
Christoph Hellwigc29f7d42011-11-30 08:58:18 +000097 fid64->ino = XFS_I(inode)->i_ino;
Christoph Hellwigc38344f2007-10-21 16:42:11 -070098 fid64->gen = inode->i_generation;
99 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700100 }
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700101
102 return fileid_type;
103}
104
105STATIC struct inode *
106xfs_nfs_get_inode(
107 struct super_block *sb,
108 u64 ino,
109 u32 generation)
Joe Perches447a5642018-03-21 15:09:32 -0700110{
Christoph Hellwigf71354b2007-12-18 16:26:55 +1100111 xfs_mount_t *mp = XFS_M(sb);
112 xfs_inode_t *ip;
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700113 int error;
114
Christoph Hellwigf71354b2007-12-18 16:26:55 +1100115 /*
116 * NFS can sometimes send requests for ino 0. Fail them gracefully.
117 */
118 if (ino == 0)
119 return ERR_PTR(-ESTALE);
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700120
Christoph Hellwigc9a98552009-01-01 14:21:16 -0500121 /*
Dave Chinner19207792010-06-24 11:15:47 +1000122 * 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..
Christoph Hellwigc9a98552009-01-01 14:21:16 -0500125 */
Christoph Hellwigef35e922010-06-24 11:51:19 +1000126 error = xfs_iget(mp, NULL, ino, XFS_IGET_UNTRUSTED, 0, &ip);
Christoph Hellwigc9a98552009-01-01 14:21:16 -0500127 if (error) {
Dave Chinner29cad0b2018-06-05 10:09:34 -0700128
Christoph Hellwigc9a98552009-01-01 14:21:16 -0500129 /*
130 * EINVAL means the inode cluster doesn't exist anymore.
Dave Chinner29cad0b2018-06-05 10:09:34 -0700131 * 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.
Christoph Hellwigc9a98552009-01-01 14:21:16 -0500134 * We don't use ESTALE directly down the chain to not
135 * confuse applications using bulkstat that expect EINVAL.
136 */
Dave Chinner29cad0b2018-06-05 10:09:34 -0700137 switch (error) {
138 case -EINVAL:
139 case -ENOENT:
140 case -EFSCORRUPTED:
Dave Chinner24513372014-06-25 14:58:08 +1000141 error = -ESTALE;
Dave Chinner29cad0b2018-06-05 10:09:34 -0700142 break;
143 default:
144 break;
145 }
Dave Chinner24513372014-06-25 14:58:08 +1000146 return ERR_PTR(error);
Christoph Hellwigc9a98552009-01-01 14:21:16 -0500147 }
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700148
Dave Chinner9e9a2672016-02-09 16:54:58 +1100149 if (VFS_I(ip)->i_generation != generation) {
Darrick J. Wong44a87362018-07-25 12:52:32 -0700150 xfs_irele(ip);
J. Bruce Fieldsad1a2c82011-07-14 20:50:36 +0000151 return ERR_PTR(-ESTALE);
Christoph Hellwigf71354b2007-12-18 16:26:55 +1100152 }
153
David Chinner01651642008-08-13 15:45:15 +1000154 return VFS_I(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700155}
156
157STATIC struct dentry *
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700158xfs_fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
159 int fh_len, int fileid_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160{
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700161 struct xfs_fid64 *fid64 = (struct xfs_fid64 *)fid;
162 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700164 if (fh_len < xfs_fileid_length(fileid_type))
165 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700167 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
Christoph Hellwig44003722008-08-11 15:49:04 +0200178 return d_obtain_alias(inode);
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700179}
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;
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700187
Hugh Dickins35c2a7f2012-10-07 20:32:51 -0700188 if (fh_len < xfs_fileid_length(fileid_type))
189 return NULL;
190
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700191 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
Christoph Hellwig44003722008-08-11 15:49:04 +0200202 return d_obtain_alias(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700203}
204
205STATIC struct dentry *
Nathan Scotta50cd262006-03-14 14:06:18 +1100206xfs_fs_get_parent(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700207 struct dentry *child)
208{
209 int error;
Christoph Hellwigef1f5e72008-03-06 13:46:25 +1100210 struct xfs_inode *cip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700211
David Howells2b0143b2015-03-17 22:25:59 +0000212 error = xfs_lookup(XFS_I(d_inode(child)), &xfs_name_dotdot, &cip, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 if (unlikely(error))
Dave Chinner24513372014-06-25 14:58:08 +1000214 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700215
Christoph Hellwig44003722008-08-11 15:49:04 +0200216 return d_obtain_alias(VFS_I(cip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217}
218
Ben Myers978ebd92010-02-17 14:05:16 -0600219STATIC int
220xfs_fs_nfs_commit_metadata(
221 struct inode *inode)
222{
Christoph Hellwig54fbdd12020-04-03 11:45:37 -0700223 return xfs_log_force_inode(XFS_I(inode));
Ben Myers978ebd92010-02-17 14:05:16 -0600224}
225
Christoph Hellwig39655162007-10-21 16:42:17 -0700226const struct export_operations xfs_export_operations = {
Nathan Scotta50cd262006-03-14 14:06:18 +1100227 .encode_fh = xfs_fs_encode_fh,
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700228 .fh_to_dentry = xfs_fs_fh_to_dentry,
229 .fh_to_parent = xfs_fs_fh_to_parent,
Nathan Scotta50cd262006-03-14 14:06:18 +1100230 .get_parent = xfs_fs_get_parent,
Ben Myers978ebd92010-02-17 14:05:16 -0600231 .commit_metadata = xfs_fs_nfs_commit_metadata,
Benjamin Coddington15d66ac2016-07-08 09:53:20 -0400232#ifdef CONFIG_EXPORTFS_BLOCK_OPS
Christoph Hellwig52785112015-02-16 11:49:23 +1100233 .get_uuid = xfs_fs_get_uuid,
234 .map_blocks = xfs_fs_map_blocks,
235 .commit_blocks = xfs_fs_commit_blocks,
236#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700237};