xfs_export.c 6.5 KB
Newer Older
L
Linus Torvalds 已提交
1
/*
2 3
 * Copyright (c) 2004-2005 Silicon Graphics, Inc.
 * All Rights Reserved.
L
Linus Torvalds 已提交
4
 *
5 6
 * This program is free software; you can redistribute it and/or
 * modify it under the terms of the GNU General Public License as
L
Linus Torvalds 已提交
7 8
 * published by the Free Software Foundation.
 *
9 10 11 12
 * This program is distributed in the hope that it would be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
L
Linus Torvalds 已提交
13
 *
14 15 16
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write the Free Software Foundation,
 * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
L
Linus Torvalds 已提交
17 18
 */
#include "xfs.h"
19 20
#include "xfs_log_format.h"
#include "xfs_trans_resv.h"
L
Linus Torvalds 已提交
21
#include "xfs_sb.h"
22
#include "xfs_ag.h"
L
Linus Torvalds 已提交
23
#include "xfs_mount.h"
24
#include "xfs_da_format.h"
25
#include "xfs_dir2.h"
L
Linus Torvalds 已提交
26
#include "xfs_export.h"
27 28
#include "xfs_bmap_btree.h"
#include "xfs_inode.h"
29
#include "xfs_trans.h"
30
#include "xfs_inode_item.h"
C
Christoph Hellwig 已提交
31
#include "xfs_trace.h"
D
Dave Chinner 已提交
32
#include "xfs_icache.h"
33
#include "xfs_log.h"
L
Linus Torvalds 已提交
34 35

/*
C
Christoph Hellwig 已提交
36 37 38
 * Note that we only accept fileids which are long enough rather than allow
 * the parent generation number to default to zero.  XFS considers zero a
 * valid generation number not an invalid/wildcard value.
L
Linus Torvalds 已提交
39
 */
C
Christoph Hellwig 已提交
40
static int xfs_fileid_length(int fileid_type)
L
Linus Torvalds 已提交
41
{
C
Christoph Hellwig 已提交
42 43 44 45 46 47 48 49 50
	switch (fileid_type) {
	case FILEID_INO32_GEN:
		return 2;
	case FILEID_INO32_GEN_PARENT:
		return 4;
	case FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG:
		return 3;
	case FILEID_INO32_GEN_PARENT | XFS_FILEID_TYPE_64FLAG:
		return 6;
L
Linus Torvalds 已提交
51
	}
52
	return FILEID_INVALID;
L
Linus Torvalds 已提交
53 54 55
}

STATIC int
56
xfs_fs_encode_fh(
A
Al Viro 已提交
57 58 59 60
	struct inode	*inode,
	__u32		*fh,
	int		*max_len,
	struct inode	*parent)
L
Linus Torvalds 已提交
61
{
C
Christoph Hellwig 已提交
62 63 64
	struct fid		*fid = (struct fid *)fh;
	struct xfs_fid64	*fid64 = (struct xfs_fid64 *)fh;
	int			fileid_type;
L
Linus Torvalds 已提交
65 66 67
	int			len;

	/* Directories don't need their parent encoded, they have ".." */
A
Al Viro 已提交
68
	if (!parent)
C
Christoph Hellwig 已提交
69 70 71 72
		fileid_type = FILEID_INO32_GEN;
	else
		fileid_type = FILEID_INO32_GEN_PARENT;

73 74 75 76 77 78 79 80 81 82
	/*
	 * If the the filesystem may contain 64bit inode numbers, we need
	 * to use larger file handles that can represent them.
	 *
	 * While we only allocate inodes that do not fit into 32 bits any
	 * large enough filesystem may contain them, thus the slightly
	 * confusing looking conditional below.
	 */
	if (!(XFS_M(inode->i_sb)->m_flags & XFS_MOUNT_SMALL_INUMS) ||
	    (XFS_M(inode->i_sb)->m_flags & XFS_MOUNT_32BITINODES))
C
Christoph Hellwig 已提交
83
		fileid_type |= XFS_FILEID_TYPE_64FLAG;
L
Linus Torvalds 已提交
84 85 86 87 88 89 90

	/*
	 * Only encode if there is enough space given.  In practice
	 * this means we can't export a filesystem with 64bit inodes
	 * over NFSv2 with the subtree_check export option; the other
	 * seven combinations work.  The real answer is "don't use v2".
	 */
C
Christoph Hellwig 已提交
91
	len = xfs_fileid_length(fileid_type);
92 93
	if (*max_len < len) {
		*max_len = len;
94
		return FILEID_INVALID;
95
	}
L
Linus Torvalds 已提交
96 97
	*max_len = len;

C
Christoph Hellwig 已提交
98 99
	switch (fileid_type) {
	case FILEID_INO32_GEN_PARENT:
A
Al Viro 已提交
100 101
		fid->i32.parent_ino = XFS_I(parent)->i_ino;
		fid->i32.parent_gen = parent->i_generation;
C
Christoph Hellwig 已提交
102 103
		/*FALLTHRU*/
	case FILEID_INO32_GEN:
104
		fid->i32.ino = XFS_I(inode)->i_ino;
C
Christoph Hellwig 已提交
105 106 107
		fid->i32.gen = inode->i_generation;
		break;
	case FILEID_INO32_GEN_PARENT | XFS_FILEID_TYPE_64FLAG:
A
Al Viro 已提交
108 109
		fid64->parent_ino = XFS_I(parent)->i_ino;
		fid64->parent_gen = parent->i_generation;
C
Christoph Hellwig 已提交
110 111
		/*FALLTHRU*/
	case FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG:
112
		fid64->ino = XFS_I(inode)->i_ino;
C
Christoph Hellwig 已提交
113 114
		fid64->gen = inode->i_generation;
		break;
L
Linus Torvalds 已提交
115
	}
C
Christoph Hellwig 已提交
116 117

	return fileid_type;
L
Linus Torvalds 已提交
118 119
}

C
Christoph Hellwig 已提交
120 121
STATIC struct inode *
xfs_nfs_get_inode(
L
Linus Torvalds 已提交
122
	struct super_block	*sb,
C
Christoph Hellwig 已提交
123 124 125
	u64			ino,
	u32			generation)
 {
126 127
 	xfs_mount_t		*mp = XFS_M(sb);
	xfs_inode_t		*ip;
L
Linus Torvalds 已提交
128 129
	int			error;

130 131 132 133 134
	/*
	 * NFS can sometimes send requests for ino 0.  Fail them gracefully.
	 */
	if (ino == 0)
		return ERR_PTR(-ESTALE);
L
Linus Torvalds 已提交
135

136
	/*
137 138 139
	 * The XFS_IGET_UNTRUSTED means that an invalid inode number is just
	 * fine and not an indication of a corrupted filesystem as clients can
	 * send invalid file handles and we have to handle it gracefully..
140
	 */
C
Christoph Hellwig 已提交
141
	error = xfs_iget(mp, NULL, ino, XFS_IGET_UNTRUSTED, 0, &ip);
142 143 144 145 146 147 148 149
	if (error) {
		/*
		 * EINVAL means the inode cluster doesn't exist anymore.
		 * This implies the filehandle is stale, so we should
		 * translate it here.
		 * We don't use ESTALE directly down the chain to not
		 * confuse applications using bulkstat that expect EINVAL.
		 */
150
		if (error == EINVAL || error == ENOENT)
151
			error = ESTALE;
C
Christoph Hellwig 已提交
152
		return ERR_PTR(-error);
153
	}
154

155
	if (ip->i_d.di_gen != generation) {
C
Christoph Hellwig 已提交
156
		IRELE(ip);
157
		return ERR_PTR(-ESTALE);
158
	}
C
Christoph Hellwig 已提交
159

160
	return VFS_I(ip);
C
Christoph Hellwig 已提交
161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183
}

STATIC struct dentry *
xfs_fs_fh_to_dentry(struct super_block *sb, struct fid *fid,
		 int fh_len, int fileid_type)
{
	struct xfs_fid64	*fid64 = (struct xfs_fid64 *)fid;
	struct inode		*inode = NULL;

	if (fh_len < xfs_fileid_length(fileid_type))
		return NULL;

	switch (fileid_type) {
	case FILEID_INO32_GEN_PARENT:
	case FILEID_INO32_GEN:
		inode = xfs_nfs_get_inode(sb, fid->i32.ino, fid->i32.gen);
		break;
	case FILEID_INO32_GEN_PARENT | XFS_FILEID_TYPE_64FLAG:
	case FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG:
		inode = xfs_nfs_get_inode(sb, fid64->ino, fid64->gen);
		break;
	}

184
	return d_obtain_alias(inode);
C
Christoph Hellwig 已提交
185 186 187 188 189 190 191 192 193
}

STATIC struct dentry *
xfs_fs_fh_to_parent(struct super_block *sb, struct fid *fid,
		 int fh_len, int fileid_type)
{
	struct xfs_fid64	*fid64 = (struct xfs_fid64 *)fid;
	struct inode		*inode = NULL;

194 195 196
	if (fh_len < xfs_fileid_length(fileid_type))
		return NULL;

C
Christoph Hellwig 已提交
197 198 199 200 201 202 203 204 205 206 207
	switch (fileid_type) {
	case FILEID_INO32_GEN_PARENT:
		inode = xfs_nfs_get_inode(sb, fid->i32.parent_ino,
					      fid->i32.parent_gen);
		break;
	case FILEID_INO32_GEN_PARENT | XFS_FILEID_TYPE_64FLAG:
		inode = xfs_nfs_get_inode(sb, fid64->parent_ino,
					      fid64->parent_gen);
		break;
	}

208
	return d_obtain_alias(inode);
L
Linus Torvalds 已提交
209 210 211
}

STATIC struct dentry *
212
xfs_fs_get_parent(
L
Linus Torvalds 已提交
213 214 215
	struct dentry		*child)
{
	int			error;
216
	struct xfs_inode	*cip;
L
Linus Torvalds 已提交
217

218
	error = xfs_lookup(XFS_I(child->d_inode), &xfs_name_dotdot, &cip, NULL);
L
Linus Torvalds 已提交
219 220 221
	if (unlikely(error))
		return ERR_PTR(-error);

222
	return d_obtain_alias(VFS_I(cip));
L
Linus Torvalds 已提交
223 224
}

225 226 227 228 229 230
STATIC int
xfs_fs_nfs_commit_metadata(
	struct inode		*inode)
{
	struct xfs_inode	*ip = XFS_I(inode);
	struct xfs_mount	*mp = ip->i_mount;
231
	xfs_lsn_t		lsn = 0;
232 233

	xfs_ilock(ip, XFS_ILOCK_SHARED);
234 235
	if (xfs_ipincount(ip))
		lsn = ip->i_itemp->ili_last_lsn;
236 237
	xfs_iunlock(ip, XFS_ILOCK_SHARED);

238 239 240
	if (!lsn)
		return 0;
	return _xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, NULL);
241 242
}

243
const struct export_operations xfs_export_operations = {
244
	.encode_fh		= xfs_fs_encode_fh,
C
Christoph Hellwig 已提交
245 246
	.fh_to_dentry		= xfs_fs_fh_to_dentry,
	.fh_to_parent		= xfs_fs_fh_to_parent,
247
	.get_parent		= xfs_fs_get_parent,
248
	.commit_metadata	= xfs_fs_nfs_commit_metadata,
L
Linus Torvalds 已提交
249
};