blob: 3cf4682e2510db5eb3614d15a47e4992365dc673 [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"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +11007#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +11008#include "xfs_log_format.h"
9#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070010#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110011#include "xfs_da_format.h"
Dave Chinner9a2cc412014-12-04 09:43:17 +110012#include "xfs_da_btree.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100013#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070014#include "xfs_export.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100015#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110016#include "xfs_trans.h"
Ben Myers978ebd92010-02-17 14:05:16 -060017#include "xfs_inode_item.h"
Christoph Hellwigef35e922010-06-24 11:51:19 +100018#include "xfs_trace.h"
Dave Chinner33479e02012-10-08 21:56:11 +110019#include "xfs_icache.h"
Dave Chinner239880e2013-10-23 10:50:10 +110020#include "xfs_log.h"
Christoph Hellwig52785112015-02-16 11:49:23 +110021#include "xfs_pnfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022
23/*
Christoph Hellwigc38344f2007-10-21 16:42:11 -070024 * 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.
Linus Torvalds1da177e2005-04-16 15:20:36 -070027 */
Christoph Hellwigc38344f2007-10-21 16:42:11 -070028static int xfs_fileid_length(int fileid_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070029{
Christoph Hellwigc38344f2007-10-21 16:42:11 -070030 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;
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 }
Namjae Jeon94e07a752013-02-17 15:48:11 +090040 return FILEID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070041}
42
Linus Torvalds1da177e2005-04-16 15:20:36 -070043STATIC int
Nathan Scotta50cd262006-03-14 14:06:18 +110044xfs_fs_encode_fh(
Al Virob0b03822012-04-02 14:34:06 -040045 struct inode *inode,
46 __u32 *fh,
47 int *max_len,
48 struct inode *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070049{
Christoph Hellwigc38344f2007-10-21 16:42:11 -070050 struct fid *fid = (struct fid *)fh;
51 struct xfs_fid64 *fid64 = (struct xfs_fid64 *)fh;
Christoph Hellwigc38344f2007-10-21 16:42:11 -070052 int fileid_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070054
55 /* Directories don't need their parent encoded, they have ".." */
Al Virob0b03822012-04-02 14:34:06 -040056 if (!parent)
Christoph Hellwigc38344f2007-10-21 16:42:11 -070057 fileid_type = FILEID_INO32_GEN;
58 else
59 fileid_type = FILEID_INO32_GEN_PARENT;
60
Samuel Kvasnica576ecb82010-11-19 13:38:49 +000061 /*
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))
Christoph Hellwigc38344f2007-10-21 16:42:11 -070071 fileid_type |= XFS_FILEID_TYPE_64FLAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070072
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 */
Christoph Hellwigc38344f2007-10-21 16:42:11 -070079 len = xfs_fileid_length(fileid_type);
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +053080 if (*max_len < len) {
81 *max_len = len;
Namjae Jeon94e07a752013-02-17 15:48:11 +090082 return FILEID_INVALID;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +053083 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070084 *max_len = len;
85
Christoph Hellwigc38344f2007-10-21 16:42:11 -070086 switch (fileid_type) {
87 case FILEID_INO32_GEN_PARENT:
Al Virob0b03822012-04-02 14:34:06 -040088 fid->i32.parent_ino = XFS_I(parent)->i_ino;
89 fid->i32.parent_gen = parent->i_generation;
Christoph Hellwigc38344f2007-10-21 16:42:11 -070090 /*FALLTHRU*/
91 case FILEID_INO32_GEN:
Christoph Hellwigc29f7d42011-11-30 08:58:18 +000092 fid->i32.ino = XFS_I(inode)->i_ino;
Christoph Hellwigc38344f2007-10-21 16:42:11 -070093 fid->i32.gen = inode->i_generation;
94 break;
95 case FILEID_INO32_GEN_PARENT | XFS_FILEID_TYPE_64FLAG:
Al Virob0b03822012-04-02 14:34:06 -040096 fid64->parent_ino = XFS_I(parent)->i_ino;
97 fid64->parent_gen = parent->i_generation;
Christoph Hellwigc38344f2007-10-21 16:42:11 -070098 /*FALLTHRU*/
99 case FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG:
Christoph Hellwigc29f7d42011-11-30 08:58:18 +0000100 fid64->ino = XFS_I(inode)->i_ino;
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700101 fid64->gen = inode->i_generation;
102 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 }
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700104
105 return fileid_type;
106}
107
108STATIC struct inode *
109xfs_nfs_get_inode(
110 struct super_block *sb,
111 u64 ino,
112 u32 generation)
Joe Perches447a5642018-03-21 15:09:32 -0700113{
Christoph Hellwigf71354b2007-12-18 16:26:55 +1100114 xfs_mount_t *mp = XFS_M(sb);
115 xfs_inode_t *ip;
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700116 int error;
117
Christoph Hellwigf71354b2007-12-18 16:26:55 +1100118 /*
119 * NFS can sometimes send requests for ino 0. Fail them gracefully.
120 */
121 if (ino == 0)
122 return ERR_PTR(-ESTALE);
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700123
Christoph Hellwigc9a98552009-01-01 14:21:16 -0500124 /*
Dave Chinner19207792010-06-24 11:15:47 +1000125 * 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..
Christoph Hellwigc9a98552009-01-01 14:21:16 -0500128 */
Christoph Hellwigef35e922010-06-24 11:51:19 +1000129 error = xfs_iget(mp, NULL, ino, XFS_IGET_UNTRUSTED, 0, &ip);
Christoph Hellwigc9a98552009-01-01 14:21:16 -0500130 if (error) {
Dave Chinner29cad0b2018-06-05 10:09:34 -0700131
Christoph Hellwigc9a98552009-01-01 14:21:16 -0500132 /*
133 * EINVAL means the inode cluster doesn't exist anymore.
Dave Chinner29cad0b2018-06-05 10:09:34 -0700134 * 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.
Christoph Hellwigc9a98552009-01-01 14:21:16 -0500137 * We don't use ESTALE directly down the chain to not
138 * confuse applications using bulkstat that expect EINVAL.
139 */
Dave Chinner29cad0b2018-06-05 10:09:34 -0700140 switch (error) {
141 case -EINVAL:
142 case -ENOENT:
143 case -EFSCORRUPTED:
Dave Chinner24513372014-06-25 14:58:08 +1000144 error = -ESTALE;
Dave Chinner29cad0b2018-06-05 10:09:34 -0700145 break;
146 default:
147 break;
148 }
Dave Chinner24513372014-06-25 14:58:08 +1000149 return ERR_PTR(error);
Christoph Hellwigc9a98552009-01-01 14:21:16 -0500150 }
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700151
Dave Chinner9e9a2672016-02-09 16:54:58 +1100152 if (VFS_I(ip)->i_generation != generation) {
Christoph Hellwigef35e922010-06-24 11:51:19 +1000153 IRELE(ip);
J. Bruce Fieldsad1a2c82011-07-14 20:50:36 +0000154 return ERR_PTR(-ESTALE);
Christoph Hellwigf71354b2007-12-18 16:26:55 +1100155 }
156
David Chinner01651642008-08-13 15:45:15 +1000157 return VFS_I(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700158}
159
160STATIC struct dentry *
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700161xfs_fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
162 int fh_len, int fileid_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163{
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700164 struct xfs_fid64 *fid64 = (struct xfs_fid64 *)fid;
165 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700167 if (fh_len < xfs_fileid_length(fileid_type))
168 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700170 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
Christoph Hellwig44003722008-08-11 15:49:04 +0200181 return d_obtain_alias(inode);
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700182}
183
184STATIC struct dentry *
185xfs_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;
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700190
Hugh Dickins35c2a7f2012-10-07 20:32:51 -0700191 if (fh_len < xfs_fileid_length(fileid_type))
192 return NULL;
193
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700194 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
Christoph Hellwig44003722008-08-11 15:49:04 +0200205 return d_obtain_alias(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700206}
207
208STATIC struct dentry *
Nathan Scotta50cd262006-03-14 14:06:18 +1100209xfs_fs_get_parent(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700210 struct dentry *child)
211{
212 int error;
Christoph Hellwigef1f5e72008-03-06 13:46:25 +1100213 struct xfs_inode *cip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700214
David Howells2b0143b2015-03-17 22:25:59 +0000215 error = xfs_lookup(XFS_I(d_inode(child)), &xfs_name_dotdot, &cip, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700216 if (unlikely(error))
Dave Chinner24513372014-06-25 14:58:08 +1000217 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700218
Christoph Hellwig44003722008-08-11 15:49:04 +0200219 return d_obtain_alias(VFS_I(cip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700220}
221
Ben Myers978ebd92010-02-17 14:05:16 -0600222STATIC int
223xfs_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;
Christoph Hellwig8292d882011-09-18 20:47:50 +0000228 xfs_lsn_t lsn = 0;
Ben Myers978ebd92010-02-17 14:05:16 -0600229
230 xfs_ilock(ip, XFS_ILOCK_SHARED);
Christoph Hellwig8292d882011-09-18 20:47:50 +0000231 if (xfs_ipincount(ip))
232 lsn = ip->i_itemp->ili_last_lsn;
Ben Myers978ebd92010-02-17 14:05:16 -0600233 xfs_iunlock(ip, XFS_ILOCK_SHARED);
234
Christoph Hellwig8292d882011-09-18 20:47:50 +0000235 if (!lsn)
236 return 0;
Christoph Hellwig656de4f2018-03-13 23:15:28 -0700237 return xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, NULL);
Ben Myers978ebd92010-02-17 14:05:16 -0600238}
239
Christoph Hellwig39655162007-10-21 16:42:17 -0700240const struct export_operations xfs_export_operations = {
Nathan Scotta50cd262006-03-14 14:06:18 +1100241 .encode_fh = xfs_fs_encode_fh,
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700242 .fh_to_dentry = xfs_fs_fh_to_dentry,
243 .fh_to_parent = xfs_fs_fh_to_parent,
Nathan Scotta50cd262006-03-14 14:06:18 +1100244 .get_parent = xfs_fs_get_parent,
Ben Myers978ebd92010-02-17 14:05:16 -0600245 .commit_metadata = xfs_fs_nfs_commit_metadata,
Benjamin Coddington15d66ac2016-07-08 09:53:20 -0400246#ifdef CONFIG_EXPORTFS_BLOCK_OPS
Christoph Hellwig52785112015-02-16 11:49:23 +1100247 .get_uuid = xfs_fs_get_uuid,
248 .map_blocks = xfs_fs_map_blocks,
249 .commit_blocks = xfs_fs_commit_blocks,
250#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700251};