blob: a1b2dd828b9d0ae447419b304793d03e9ec144f5 [file] [log] [blame]
Linus Torvalds1da177e2005-04-16 15:20:36 -07001/*
Nathan Scott7b718762005-11-02 14:58:39 +11002 * Copyright (c) 2004-2005 Silicon Graphics, Inc.
3 * All Rights Reserved.
Linus Torvalds1da177e2005-04-16 15:20:36 -07004 *
Nathan Scott7b718762005-11-02 14:58:39 +11005 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License as
Linus Torvalds1da177e2005-04-16 15:20:36 -07007 * published by the Free Software Foundation.
8 *
Nathan Scott7b718762005-11-02 14:58:39 +11009 * This program is distributed in the hope that it would be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
Linus Torvalds1da177e2005-04-16 15:20:36 -070013 *
Nathan Scott7b718762005-11-02 14:58:39 +110014 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write the Free Software Foundation,
16 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
Linus Torvalds1da177e2005-04-16 15:20:36 -070017 */
Linus Torvalds1da177e2005-04-16 15:20:36 -070018#include "xfs.h"
Dave Chinnera4fbe6a2013-10-23 10:51:50 +110019#include "xfs_format.h"
Dave Chinner239880e2013-10-23 10:50:10 +110020#include "xfs_log_format.h"
21#include "xfs_trans_resv.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070022#include "xfs_mount.h"
Dave Chinner57062782013-10-15 09:17:51 +110023#include "xfs_da_format.h"
Dave Chinner9a2cc412014-12-04 09:43:17 +110024#include "xfs_da_btree.h"
Dave Chinner2b9ab5a2013-08-12 20:49:37 +100025#include "xfs_dir2.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_export.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100027#include "xfs_inode.h"
Dave Chinner239880e2013-10-23 10:50:10 +110028#include "xfs_trans.h"
Ben Myers978ebd92010-02-17 14:05:16 -060029#include "xfs_inode_item.h"
Christoph Hellwigef35e922010-06-24 11:51:19 +100030#include "xfs_trace.h"
Dave Chinner33479e02012-10-08 21:56:11 +110031#include "xfs_icache.h"
Dave Chinner239880e2013-10-23 10:50:10 +110032#include "xfs_log.h"
Christoph Hellwig52785112015-02-16 11:49:23 +110033#include "xfs_pnfs.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070034
35/*
Christoph Hellwigc38344f2007-10-21 16:42:11 -070036 * Note that we only accept fileids which are long enough rather than allow
37 * the parent generation number to default to zero. XFS considers zero a
38 * valid generation number not an invalid/wildcard value.
Linus Torvalds1da177e2005-04-16 15:20:36 -070039 */
Christoph Hellwigc38344f2007-10-21 16:42:11 -070040static int xfs_fileid_length(int fileid_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -070041{
Christoph Hellwigc38344f2007-10-21 16:42:11 -070042 switch (fileid_type) {
43 case FILEID_INO32_GEN:
44 return 2;
45 case FILEID_INO32_GEN_PARENT:
46 return 4;
47 case FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG:
48 return 3;
49 case FILEID_INO32_GEN_PARENT | XFS_FILEID_TYPE_64FLAG:
50 return 6;
Linus Torvalds1da177e2005-04-16 15:20:36 -070051 }
Namjae Jeon94e07a752013-02-17 15:48:11 +090052 return FILEID_INVALID;
Linus Torvalds1da177e2005-04-16 15:20:36 -070053}
54
Linus Torvalds1da177e2005-04-16 15:20:36 -070055STATIC int
Nathan Scotta50cd262006-03-14 14:06:18 +110056xfs_fs_encode_fh(
Al Virob0b03822012-04-02 14:34:06 -040057 struct inode *inode,
58 __u32 *fh,
59 int *max_len,
60 struct inode *parent)
Linus Torvalds1da177e2005-04-16 15:20:36 -070061{
Christoph Hellwigc38344f2007-10-21 16:42:11 -070062 struct fid *fid = (struct fid *)fh;
63 struct xfs_fid64 *fid64 = (struct xfs_fid64 *)fh;
Christoph Hellwigc38344f2007-10-21 16:42:11 -070064 int fileid_type;
Linus Torvalds1da177e2005-04-16 15:20:36 -070065 int len;
Linus Torvalds1da177e2005-04-16 15:20:36 -070066
67 /* Directories don't need their parent encoded, they have ".." */
Al Virob0b03822012-04-02 14:34:06 -040068 if (!parent)
Christoph Hellwigc38344f2007-10-21 16:42:11 -070069 fileid_type = FILEID_INO32_GEN;
70 else
71 fileid_type = FILEID_INO32_GEN_PARENT;
72
Samuel Kvasnica576ecb82010-11-19 13:38:49 +000073 /*
74 * If the the filesystem may contain 64bit inode numbers, we need
75 * to use larger file handles that can represent them.
76 *
77 * While we only allocate inodes that do not fit into 32 bits any
78 * large enough filesystem may contain them, thus the slightly
79 * confusing looking conditional below.
80 */
81 if (!(XFS_M(inode->i_sb)->m_flags & XFS_MOUNT_SMALL_INUMS) ||
82 (XFS_M(inode->i_sb)->m_flags & XFS_MOUNT_32BITINODES))
Christoph Hellwigc38344f2007-10-21 16:42:11 -070083 fileid_type |= XFS_FILEID_TYPE_64FLAG;
Linus Torvalds1da177e2005-04-16 15:20:36 -070084
85 /*
86 * Only encode if there is enough space given. In practice
87 * this means we can't export a filesystem with 64bit inodes
88 * over NFSv2 with the subtree_check export option; the other
89 * seven combinations work. The real answer is "don't use v2".
90 */
Christoph Hellwigc38344f2007-10-21 16:42:11 -070091 len = xfs_fileid_length(fileid_type);
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +053092 if (*max_len < len) {
93 *max_len = len;
Namjae Jeon94e07a752013-02-17 15:48:11 +090094 return FILEID_INVALID;
Aneesh Kumar K.V5fe0c232011-01-29 18:43:25 +053095 }
Linus Torvalds1da177e2005-04-16 15:20:36 -070096 *max_len = len;
97
Christoph Hellwigc38344f2007-10-21 16:42:11 -070098 switch (fileid_type) {
99 case FILEID_INO32_GEN_PARENT:
Al Virob0b03822012-04-02 14:34:06 -0400100 fid->i32.parent_ino = XFS_I(parent)->i_ino;
101 fid->i32.parent_gen = parent->i_generation;
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700102 /*FALLTHRU*/
103 case FILEID_INO32_GEN:
Christoph Hellwigc29f7d42011-11-30 08:58:18 +0000104 fid->i32.ino = XFS_I(inode)->i_ino;
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700105 fid->i32.gen = inode->i_generation;
106 break;
107 case FILEID_INO32_GEN_PARENT | XFS_FILEID_TYPE_64FLAG:
Al Virob0b03822012-04-02 14:34:06 -0400108 fid64->parent_ino = XFS_I(parent)->i_ino;
109 fid64->parent_gen = parent->i_generation;
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700110 /*FALLTHRU*/
111 case FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG:
Christoph Hellwigc29f7d42011-11-30 08:58:18 +0000112 fid64->ino = XFS_I(inode)->i_ino;
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700113 fid64->gen = inode->i_generation;
114 break;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700115 }
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700116
117 return fileid_type;
118}
119
120STATIC struct inode *
121xfs_nfs_get_inode(
122 struct super_block *sb,
123 u64 ino,
124 u32 generation)
125 {
Christoph Hellwigf71354b2007-12-18 16:26:55 +1100126 xfs_mount_t *mp = XFS_M(sb);
127 xfs_inode_t *ip;
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700128 int error;
129
Christoph Hellwigf71354b2007-12-18 16:26:55 +1100130 /*
131 * NFS can sometimes send requests for ino 0. Fail them gracefully.
132 */
133 if (ino == 0)
134 return ERR_PTR(-ESTALE);
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700135
Christoph Hellwigc9a98552009-01-01 14:21:16 -0500136 /*
Dave Chinner19207792010-06-24 11:15:47 +1000137 * The XFS_IGET_UNTRUSTED means that an invalid inode number is just
138 * fine and not an indication of a corrupted filesystem as clients can
139 * send invalid file handles and we have to handle it gracefully..
Christoph Hellwigc9a98552009-01-01 14:21:16 -0500140 */
Christoph Hellwigef35e922010-06-24 11:51:19 +1000141 error = xfs_iget(mp, NULL, ino, XFS_IGET_UNTRUSTED, 0, &ip);
Christoph Hellwigc9a98552009-01-01 14:21:16 -0500142 if (error) {
143 /*
144 * EINVAL means the inode cluster doesn't exist anymore.
145 * This implies the filehandle is stale, so we should
146 * translate it here.
147 * We don't use ESTALE directly down the chain to not
148 * confuse applications using bulkstat that expect EINVAL.
149 */
Dave Chinner24513372014-06-25 14:58:08 +1000150 if (error == -EINVAL || error == -ENOENT)
151 error = -ESTALE;
152 return ERR_PTR(error);
Christoph Hellwigc9a98552009-01-01 14:21:16 -0500153 }
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700154
Dave Chinner9e9a2672016-02-09 16:54:58 +1100155 if (VFS_I(ip)->i_generation != generation) {
Christoph Hellwigef35e922010-06-24 11:51:19 +1000156 IRELE(ip);
J. Bruce Fieldsad1a2c82011-07-14 20:50:36 +0000157 return ERR_PTR(-ESTALE);
Christoph Hellwigf71354b2007-12-18 16:26:55 +1100158 }
159
David Chinner01651642008-08-13 15:45:15 +1000160 return VFS_I(ip);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161}
162
163STATIC struct dentry *
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700164xfs_fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
165 int fh_len, int fileid_type)
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166{
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700167 struct xfs_fid64 *fid64 = (struct xfs_fid64 *)fid;
168 struct inode *inode = NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700170 if (fh_len < xfs_fileid_length(fileid_type))
171 return NULL;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700173 switch (fileid_type) {
174 case FILEID_INO32_GEN_PARENT:
175 case FILEID_INO32_GEN:
176 inode = xfs_nfs_get_inode(sb, fid->i32.ino, fid->i32.gen);
177 break;
178 case FILEID_INO32_GEN_PARENT | XFS_FILEID_TYPE_64FLAG:
179 case FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG:
180 inode = xfs_nfs_get_inode(sb, fid64->ino, fid64->gen);
181 break;
182 }
183
Christoph Hellwig44003722008-08-11 15:49:04 +0200184 return d_obtain_alias(inode);
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700185}
186
187STATIC struct dentry *
188xfs_fs_fh_to_parent(struct super_block *sb, struct fid *fid,
189 int fh_len, int fileid_type)
190{
191 struct xfs_fid64 *fid64 = (struct xfs_fid64 *)fid;
192 struct inode *inode = NULL;
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700193
Hugh Dickins35c2a7f2012-10-07 20:32:51 -0700194 if (fh_len < xfs_fileid_length(fileid_type))
195 return NULL;
196
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700197 switch (fileid_type) {
198 case FILEID_INO32_GEN_PARENT:
199 inode = xfs_nfs_get_inode(sb, fid->i32.parent_ino,
200 fid->i32.parent_gen);
201 break;
202 case FILEID_INO32_GEN_PARENT | XFS_FILEID_TYPE_64FLAG:
203 inode = xfs_nfs_get_inode(sb, fid64->parent_ino,
204 fid64->parent_gen);
205 break;
206 }
207
Christoph Hellwig44003722008-08-11 15:49:04 +0200208 return d_obtain_alias(inode);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700209}
210
211STATIC struct dentry *
Nathan Scotta50cd262006-03-14 14:06:18 +1100212xfs_fs_get_parent(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700213 struct dentry *child)
214{
215 int error;
Christoph Hellwigef1f5e72008-03-06 13:46:25 +1100216 struct xfs_inode *cip;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700217
David Howells2b0143b2015-03-17 22:25:59 +0000218 error = xfs_lookup(XFS_I(d_inode(child)), &xfs_name_dotdot, &cip, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700219 if (unlikely(error))
Dave Chinner24513372014-06-25 14:58:08 +1000220 return ERR_PTR(error);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700221
Christoph Hellwig44003722008-08-11 15:49:04 +0200222 return d_obtain_alias(VFS_I(cip));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700223}
224
Ben Myers978ebd92010-02-17 14:05:16 -0600225STATIC int
226xfs_fs_nfs_commit_metadata(
227 struct inode *inode)
228{
229 struct xfs_inode *ip = XFS_I(inode);
230 struct xfs_mount *mp = ip->i_mount;
Christoph Hellwig8292d882011-09-18 20:47:50 +0000231 xfs_lsn_t lsn = 0;
Ben Myers978ebd92010-02-17 14:05:16 -0600232
233 xfs_ilock(ip, XFS_ILOCK_SHARED);
Christoph Hellwig8292d882011-09-18 20:47:50 +0000234 if (xfs_ipincount(ip))
235 lsn = ip->i_itemp->ili_last_lsn;
Ben Myers978ebd92010-02-17 14:05:16 -0600236 xfs_iunlock(ip, XFS_ILOCK_SHARED);
237
Christoph Hellwig8292d882011-09-18 20:47:50 +0000238 if (!lsn)
239 return 0;
Dave Chinner24513372014-06-25 14:58:08 +1000240 return _xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, NULL);
Ben Myers978ebd92010-02-17 14:05:16 -0600241}
242
Christoph Hellwig39655162007-10-21 16:42:17 -0700243const struct export_operations xfs_export_operations = {
Nathan Scotta50cd262006-03-14 14:06:18 +1100244 .encode_fh = xfs_fs_encode_fh,
Christoph Hellwigc38344f2007-10-21 16:42:11 -0700245 .fh_to_dentry = xfs_fs_fh_to_dentry,
246 .fh_to_parent = xfs_fs_fh_to_parent,
Nathan Scotta50cd262006-03-14 14:06:18 +1100247 .get_parent = xfs_fs_get_parent,
Ben Myers978ebd92010-02-17 14:05:16 -0600248 .commit_metadata = xfs_fs_nfs_commit_metadata,
Christoph Hellwig81c39322016-03-04 20:46:16 +0100249#ifdef CONFIG_NFSD_BLOCKLAYOUT
Christoph Hellwig52785112015-02-16 11:49:23 +1100250 .get_uuid = xfs_fs_get_uuid,
251 .map_blocks = xfs_fs_map_blocks,
252 .commit_blocks = xfs_fs_commit_blocks,
253#endif
Linus Torvalds1da177e2005-04-16 15:20:36 -0700254};