Dave Chinner | 0b61f8a | 2018-06-05 19:42:14 -0700 | [diff] [blame] | 1 | // SPDX-License-Identifier: GPL-2.0 |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 2 | /* |
Nathan Scott | 7b71876 | 2005-11-02 14:58:39 +1100 | [diff] [blame] | 3 | * Copyright (c) 2004-2005 Silicon Graphics, Inc. |
| 4 | * All Rights Reserved. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 5 | */ |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 6 | #include "xfs.h" |
Darrick J. Wong | 5467b34 | 2019-06-28 19:25:35 -0700 | [diff] [blame] | 7 | #include "xfs_shared.h" |
Dave Chinner | a4fbe6a | 2013-10-23 10:51:50 +1100 | [diff] [blame] | 8 | #include "xfs_format.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 9 | #include "xfs_log_format.h" |
| 10 | #include "xfs_trans_resv.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 11 | #include "xfs_mount.h" |
Dave Chinner | 2b9ab5a | 2013-08-12 20:49:37 +1000 | [diff] [blame] | 12 | #include "xfs_dir2.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 13 | #include "xfs_export.h" |
Christoph Hellwig | 739bfb2 | 2007-08-29 10:58:01 +1000 | [diff] [blame] | 14 | #include "xfs_inode.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 15 | #include "xfs_trans.h" |
Ben Myers | 978ebd9 | 2010-02-17 14:05:16 -0600 | [diff] [blame] | 16 | #include "xfs_inode_item.h" |
Dave Chinner | 33479e0 | 2012-10-08 21:56:11 +1100 | [diff] [blame] | 17 | #include "xfs_icache.h" |
Dave Chinner | 239880e | 2013-10-23 10:50:10 +1100 | [diff] [blame] | 18 | #include "xfs_log.h" |
Christoph Hellwig | 5278511 | 2015-02-16 11:49:23 +1100 | [diff] [blame] | 19 | #include "xfs_pnfs.h" |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 20 | |
| 21 | /* |
Christoph Hellwig | c38344f | 2007-10-21 16:42:11 -0700 | [diff] [blame] | 22 | * Note that we only accept fileids which are long enough rather than allow |
| 23 | * the parent generation number to default to zero. XFS considers zero a |
| 24 | * valid generation number not an invalid/wildcard value. |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 25 | */ |
Christoph Hellwig | c38344f | 2007-10-21 16:42:11 -0700 | [diff] [blame] | 26 | static int xfs_fileid_length(int fileid_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 27 | { |
Christoph Hellwig | c38344f | 2007-10-21 16:42:11 -0700 | [diff] [blame] | 28 | switch (fileid_type) { |
| 29 | case FILEID_INO32_GEN: |
| 30 | return 2; |
| 31 | case FILEID_INO32_GEN_PARENT: |
| 32 | return 4; |
| 33 | case FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG: |
| 34 | return 3; |
| 35 | case FILEID_INO32_GEN_PARENT | XFS_FILEID_TYPE_64FLAG: |
| 36 | return 6; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 37 | } |
Namjae Jeon | 94e07a75 | 2013-02-17 15:48:11 +0900 | [diff] [blame] | 38 | return FILEID_INVALID; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 39 | } |
| 40 | |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 41 | STATIC int |
Nathan Scott | a50cd26 | 2006-03-14 14:06:18 +1100 | [diff] [blame] | 42 | xfs_fs_encode_fh( |
Al Viro | b0b0382 | 2012-04-02 14:34:06 -0400 | [diff] [blame] | 43 | struct inode *inode, |
| 44 | __u32 *fh, |
| 45 | int *max_len, |
| 46 | struct inode *parent) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 47 | { |
Christoph Hellwig | c38344f | 2007-10-21 16:42:11 -0700 | [diff] [blame] | 48 | struct fid *fid = (struct fid *)fh; |
| 49 | struct xfs_fid64 *fid64 = (struct xfs_fid64 *)fh; |
Christoph Hellwig | c38344f | 2007-10-21 16:42:11 -0700 | [diff] [blame] | 50 | int fileid_type; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 51 | int len; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 52 | |
| 53 | /* Directories don't need their parent encoded, they have ".." */ |
Al Viro | b0b0382 | 2012-04-02 14:34:06 -0400 | [diff] [blame] | 54 | if (!parent) |
Christoph Hellwig | c38344f | 2007-10-21 16:42:11 -0700 | [diff] [blame] | 55 | fileid_type = FILEID_INO32_GEN; |
| 56 | else |
| 57 | fileid_type = FILEID_INO32_GEN_PARENT; |
| 58 | |
Samuel Kvasnica | 576ecb8 | 2010-11-19 13:38:49 +0000 | [diff] [blame] | 59 | /* |
| 60 | * If the the filesystem may contain 64bit inode numbers, we need |
| 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 | */ |
| 67 | if (!(XFS_M(inode->i_sb)->m_flags & XFS_MOUNT_SMALL_INUMS) || |
| 68 | (XFS_M(inode->i_sb)->m_flags & XFS_MOUNT_32BITINODES)) |
Christoph Hellwig | c38344f | 2007-10-21 16:42:11 -0700 | [diff] [blame] | 69 | fileid_type |= XFS_FILEID_TYPE_64FLAG; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 70 | |
| 71 | /* |
| 72 | * Only encode if there is enough space given. In practice |
| 73 | * this means we can't export a filesystem with 64bit inodes |
| 74 | * over NFSv2 with the subtree_check export option; the other |
| 75 | * seven combinations work. The real answer is "don't use v2". |
| 76 | */ |
Christoph Hellwig | c38344f | 2007-10-21 16:42:11 -0700 | [diff] [blame] | 77 | len = xfs_fileid_length(fileid_type); |
Aneesh Kumar K.V | 5fe0c23 | 2011-01-29 18:43:25 +0530 | [diff] [blame] | 78 | if (*max_len < len) { |
| 79 | *max_len = len; |
Namjae Jeon | 94e07a75 | 2013-02-17 15:48:11 +0900 | [diff] [blame] | 80 | return FILEID_INVALID; |
Aneesh Kumar K.V | 5fe0c23 | 2011-01-29 18:43:25 +0530 | [diff] [blame] | 81 | } |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 82 | *max_len = len; |
| 83 | |
Christoph Hellwig | c38344f | 2007-10-21 16:42:11 -0700 | [diff] [blame] | 84 | switch (fileid_type) { |
| 85 | case FILEID_INO32_GEN_PARENT: |
Al Viro | b0b0382 | 2012-04-02 14:34:06 -0400 | [diff] [blame] | 86 | fid->i32.parent_ino = XFS_I(parent)->i_ino; |
| 87 | fid->i32.parent_gen = parent->i_generation; |
Christoph Hellwig | c38344f | 2007-10-21 16:42:11 -0700 | [diff] [blame] | 88 | /*FALLTHRU*/ |
| 89 | case FILEID_INO32_GEN: |
Christoph Hellwig | c29f7d4 | 2011-11-30 08:58:18 +0000 | [diff] [blame] | 90 | fid->i32.ino = XFS_I(inode)->i_ino; |
Christoph Hellwig | c38344f | 2007-10-21 16:42:11 -0700 | [diff] [blame] | 91 | fid->i32.gen = inode->i_generation; |
| 92 | break; |
| 93 | case FILEID_INO32_GEN_PARENT | XFS_FILEID_TYPE_64FLAG: |
Al Viro | b0b0382 | 2012-04-02 14:34:06 -0400 | [diff] [blame] | 94 | fid64->parent_ino = XFS_I(parent)->i_ino; |
| 95 | fid64->parent_gen = parent->i_generation; |
Christoph Hellwig | c38344f | 2007-10-21 16:42:11 -0700 | [diff] [blame] | 96 | /*FALLTHRU*/ |
| 97 | case FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG: |
Christoph Hellwig | c29f7d4 | 2011-11-30 08:58:18 +0000 | [diff] [blame] | 98 | fid64->ino = XFS_I(inode)->i_ino; |
Christoph Hellwig | c38344f | 2007-10-21 16:42:11 -0700 | [diff] [blame] | 99 | fid64->gen = inode->i_generation; |
| 100 | break; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 101 | } |
Christoph Hellwig | c38344f | 2007-10-21 16:42:11 -0700 | [diff] [blame] | 102 | |
| 103 | return fileid_type; |
| 104 | } |
| 105 | |
| 106 | STATIC struct inode * |
| 107 | xfs_nfs_get_inode( |
| 108 | struct super_block *sb, |
| 109 | u64 ino, |
| 110 | u32 generation) |
Joe Perches | 447a564 | 2018-03-21 15:09:32 -0700 | [diff] [blame] | 111 | { |
Christoph Hellwig | f71354b | 2007-12-18 16:26:55 +1100 | [diff] [blame] | 112 | xfs_mount_t *mp = XFS_M(sb); |
| 113 | xfs_inode_t *ip; |
Christoph Hellwig | c38344f | 2007-10-21 16:42:11 -0700 | [diff] [blame] | 114 | int error; |
| 115 | |
Christoph Hellwig | f71354b | 2007-12-18 16:26:55 +1100 | [diff] [blame] | 116 | /* |
| 117 | * NFS can sometimes send requests for ino 0. Fail them gracefully. |
| 118 | */ |
| 119 | if (ino == 0) |
| 120 | return ERR_PTR(-ESTALE); |
Christoph Hellwig | c38344f | 2007-10-21 16:42:11 -0700 | [diff] [blame] | 121 | |
Christoph Hellwig | c9a9855 | 2009-01-01 14:21:16 -0500 | [diff] [blame] | 122 | /* |
Dave Chinner | 1920779 | 2010-06-24 11:15:47 +1000 | [diff] [blame] | 123 | * The XFS_IGET_UNTRUSTED means that an invalid inode number is just |
| 124 | * fine and not an indication of a corrupted filesystem as clients can |
| 125 | * send invalid file handles and we have to handle it gracefully.. |
Christoph Hellwig | c9a9855 | 2009-01-01 14:21:16 -0500 | [diff] [blame] | 126 | */ |
Christoph Hellwig | ef35e92 | 2010-06-24 11:51:19 +1000 | [diff] [blame] | 127 | error = xfs_iget(mp, NULL, ino, XFS_IGET_UNTRUSTED, 0, &ip); |
Christoph Hellwig | c9a9855 | 2009-01-01 14:21:16 -0500 | [diff] [blame] | 128 | if (error) { |
Dave Chinner | 29cad0b | 2018-06-05 10:09:34 -0700 | [diff] [blame] | 129 | |
Christoph Hellwig | c9a9855 | 2009-01-01 14:21:16 -0500 | [diff] [blame] | 130 | /* |
| 131 | * EINVAL means the inode cluster doesn't exist anymore. |
Dave Chinner | 29cad0b | 2018-06-05 10:09:34 -0700 | [diff] [blame] | 132 | * EFSCORRUPTED means the metadata pointing to the inode cluster |
| 133 | * or the inode cluster itself is corrupt. This implies the |
| 134 | * filehandle is stale, so we should translate it here. |
Christoph Hellwig | c9a9855 | 2009-01-01 14:21:16 -0500 | [diff] [blame] | 135 | * We don't use ESTALE directly down the chain to not |
| 136 | * confuse applications using bulkstat that expect EINVAL. |
| 137 | */ |
Dave Chinner | 29cad0b | 2018-06-05 10:09:34 -0700 | [diff] [blame] | 138 | switch (error) { |
| 139 | case -EINVAL: |
| 140 | case -ENOENT: |
| 141 | case -EFSCORRUPTED: |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 142 | error = -ESTALE; |
Dave Chinner | 29cad0b | 2018-06-05 10:09:34 -0700 | [diff] [blame] | 143 | break; |
| 144 | default: |
| 145 | break; |
| 146 | } |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 147 | return ERR_PTR(error); |
Christoph Hellwig | c9a9855 | 2009-01-01 14:21:16 -0500 | [diff] [blame] | 148 | } |
Christoph Hellwig | c38344f | 2007-10-21 16:42:11 -0700 | [diff] [blame] | 149 | |
Dave Chinner | 9e9a267 | 2016-02-09 16:54:58 +1100 | [diff] [blame] | 150 | if (VFS_I(ip)->i_generation != generation) { |
Darrick J. Wong | 44a8736 | 2018-07-25 12:52:32 -0700 | [diff] [blame] | 151 | xfs_irele(ip); |
J. Bruce Fields | ad1a2c8 | 2011-07-14 20:50:36 +0000 | [diff] [blame] | 152 | return ERR_PTR(-ESTALE); |
Christoph Hellwig | f71354b | 2007-12-18 16:26:55 +1100 | [diff] [blame] | 153 | } |
| 154 | |
David Chinner | 0165164 | 2008-08-13 15:45:15 +1000 | [diff] [blame] | 155 | return VFS_I(ip); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | STATIC struct dentry * |
Christoph Hellwig | c38344f | 2007-10-21 16:42:11 -0700 | [diff] [blame] | 159 | xfs_fs_fh_to_dentry(struct super_block *sb, struct fid *fid, |
| 160 | int fh_len, int fileid_type) |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 161 | { |
Christoph Hellwig | c38344f | 2007-10-21 16:42:11 -0700 | [diff] [blame] | 162 | struct xfs_fid64 *fid64 = (struct xfs_fid64 *)fid; |
| 163 | struct inode *inode = NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 164 | |
Christoph Hellwig | c38344f | 2007-10-21 16:42:11 -0700 | [diff] [blame] | 165 | if (fh_len < xfs_fileid_length(fileid_type)) |
| 166 | return NULL; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 167 | |
Christoph Hellwig | c38344f | 2007-10-21 16:42:11 -0700 | [diff] [blame] | 168 | switch (fileid_type) { |
| 169 | case FILEID_INO32_GEN_PARENT: |
| 170 | case FILEID_INO32_GEN: |
| 171 | inode = xfs_nfs_get_inode(sb, fid->i32.ino, fid->i32.gen); |
| 172 | break; |
| 173 | case FILEID_INO32_GEN_PARENT | XFS_FILEID_TYPE_64FLAG: |
| 174 | case FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG: |
| 175 | inode = xfs_nfs_get_inode(sb, fid64->ino, fid64->gen); |
| 176 | break; |
| 177 | } |
| 178 | |
Christoph Hellwig | 4400372 | 2008-08-11 15:49:04 +0200 | [diff] [blame] | 179 | return d_obtain_alias(inode); |
Christoph Hellwig | c38344f | 2007-10-21 16:42:11 -0700 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | STATIC struct dentry * |
| 183 | xfs_fs_fh_to_parent(struct super_block *sb, struct fid *fid, |
| 184 | int fh_len, int fileid_type) |
| 185 | { |
| 186 | struct xfs_fid64 *fid64 = (struct xfs_fid64 *)fid; |
| 187 | struct inode *inode = NULL; |
Christoph Hellwig | c38344f | 2007-10-21 16:42:11 -0700 | [diff] [blame] | 188 | |
Hugh Dickins | 35c2a7f | 2012-10-07 20:32:51 -0700 | [diff] [blame] | 189 | if (fh_len < xfs_fileid_length(fileid_type)) |
| 190 | return NULL; |
| 191 | |
Christoph Hellwig | c38344f | 2007-10-21 16:42:11 -0700 | [diff] [blame] | 192 | switch (fileid_type) { |
| 193 | case FILEID_INO32_GEN_PARENT: |
| 194 | inode = xfs_nfs_get_inode(sb, fid->i32.parent_ino, |
| 195 | fid->i32.parent_gen); |
| 196 | break; |
| 197 | case FILEID_INO32_GEN_PARENT | XFS_FILEID_TYPE_64FLAG: |
| 198 | inode = xfs_nfs_get_inode(sb, fid64->parent_ino, |
| 199 | fid64->parent_gen); |
| 200 | break; |
| 201 | } |
| 202 | |
Christoph Hellwig | 4400372 | 2008-08-11 15:49:04 +0200 | [diff] [blame] | 203 | return d_obtain_alias(inode); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | STATIC struct dentry * |
Nathan Scott | a50cd26 | 2006-03-14 14:06:18 +1100 | [diff] [blame] | 207 | xfs_fs_get_parent( |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 208 | struct dentry *child) |
| 209 | { |
| 210 | int error; |
Christoph Hellwig | ef1f5e7 | 2008-03-06 13:46:25 +1100 | [diff] [blame] | 211 | struct xfs_inode *cip; |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 212 | |
David Howells | 2b0143b | 2015-03-17 22:25:59 +0000 | [diff] [blame] | 213 | error = xfs_lookup(XFS_I(d_inode(child)), &xfs_name_dotdot, &cip, NULL); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 214 | if (unlikely(error)) |
Dave Chinner | 2451337 | 2014-06-25 14:58:08 +1000 | [diff] [blame] | 215 | return ERR_PTR(error); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 216 | |
Christoph Hellwig | 4400372 | 2008-08-11 15:49:04 +0200 | [diff] [blame] | 217 | return d_obtain_alias(VFS_I(cip)); |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 218 | } |
| 219 | |
Ben Myers | 978ebd9 | 2010-02-17 14:05:16 -0600 | [diff] [blame] | 220 | STATIC int |
| 221 | xfs_fs_nfs_commit_metadata( |
| 222 | struct inode *inode) |
| 223 | { |
| 224 | struct xfs_inode *ip = XFS_I(inode); |
| 225 | struct xfs_mount *mp = ip->i_mount; |
Christoph Hellwig | 8292d88 | 2011-09-18 20:47:50 +0000 | [diff] [blame] | 226 | xfs_lsn_t lsn = 0; |
Ben Myers | 978ebd9 | 2010-02-17 14:05:16 -0600 | [diff] [blame] | 227 | |
| 228 | xfs_ilock(ip, XFS_ILOCK_SHARED); |
Christoph Hellwig | 8292d88 | 2011-09-18 20:47:50 +0000 | [diff] [blame] | 229 | if (xfs_ipincount(ip)) |
| 230 | lsn = ip->i_itemp->ili_last_lsn; |
Ben Myers | 978ebd9 | 2010-02-17 14:05:16 -0600 | [diff] [blame] | 231 | xfs_iunlock(ip, XFS_ILOCK_SHARED); |
| 232 | |
Christoph Hellwig | 8292d88 | 2011-09-18 20:47:50 +0000 | [diff] [blame] | 233 | if (!lsn) |
| 234 | return 0; |
Christoph Hellwig | 656de4f | 2018-03-13 23:15:28 -0700 | [diff] [blame] | 235 | return xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, NULL); |
Ben Myers | 978ebd9 | 2010-02-17 14:05:16 -0600 | [diff] [blame] | 236 | } |
| 237 | |
Christoph Hellwig | 3965516 | 2007-10-21 16:42:17 -0700 | [diff] [blame] | 238 | const struct export_operations xfs_export_operations = { |
Nathan Scott | a50cd26 | 2006-03-14 14:06:18 +1100 | [diff] [blame] | 239 | .encode_fh = xfs_fs_encode_fh, |
Christoph Hellwig | c38344f | 2007-10-21 16:42:11 -0700 | [diff] [blame] | 240 | .fh_to_dentry = xfs_fs_fh_to_dentry, |
| 241 | .fh_to_parent = xfs_fs_fh_to_parent, |
Nathan Scott | a50cd26 | 2006-03-14 14:06:18 +1100 | [diff] [blame] | 242 | .get_parent = xfs_fs_get_parent, |
Ben Myers | 978ebd9 | 2010-02-17 14:05:16 -0600 | [diff] [blame] | 243 | .commit_metadata = xfs_fs_nfs_commit_metadata, |
Benjamin Coddington | 15d66ac | 2016-07-08 09:53:20 -0400 | [diff] [blame] | 244 | #ifdef CONFIG_EXPORTFS_BLOCK_OPS |
Christoph Hellwig | 5278511 | 2015-02-16 11:49:23 +1100 | [diff] [blame] | 245 | .get_uuid = xfs_fs_get_uuid, |
| 246 | .map_blocks = xfs_fs_map_blocks, |
| 247 | .commit_blocks = xfs_fs_commit_blocks, |
| 248 | #endif |
Linus Torvalds | 1da177e | 2005-04-16 15:20:36 -0700 | [diff] [blame] | 249 | }; |