xfs_export.c 4.6 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 19
 */
#include "xfs.h"
#include "xfs_types.h"
20
#include "xfs_inum.h"
L
Linus Torvalds 已提交
21 22 23
#include "xfs_log.h"
#include "xfs_trans.h"
#include "xfs_sb.h"
24 25
#include "xfs_ag.h"
#include "xfs_dmapi.h"
L
Linus Torvalds 已提交
26 27
#include "xfs_mount.h"
#include "xfs_export.h"
28 29 30
#include "xfs_vnodeops.h"
#include "xfs_bmap_btree.h"
#include "xfs_inode.h"
31
#include "xfs_vfsops.h"
L
Linus Torvalds 已提交
32

33
static struct dentry dotdot = { .d_name.name = "..", .d_name.len = 2, };
34

L
Linus Torvalds 已提交
35
/*
36
 * XFS encodes and decodes the fileid portion of NFS filehandles
L
Linus Torvalds 已提交
37 38 39 40 41 42 43 44 45 46
 * itself instead of letting the generic NFS code do it.  This
 * allows filesystems with 64 bit inode numbers to be exported.
 *
 * Note that a side effect is that xfs_vget() won't be passed a
 * zero inode/generation pair under normal circumstances.  As
 * however a malicious client could send us such data, the check
 * remains in that code.
 */

STATIC struct dentry *
47
xfs_fs_decode_fh(
L
Linus Torvalds 已提交
48 49 50 51 52 53 54 55 56
	struct super_block	*sb,
	__u32			*fh,
	int			fh_len,
	int			fileid_type,
	int (*acceptable)(
		void		*context,
		struct dentry	*de),
	void			*context)
{
C
Christoph Hellwig 已提交
57 58
	xfs_fid_t		ifid;
	xfs_fid_t		pfid;
L
Linus Torvalds 已提交
59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85
	void			*parent = NULL;
	int			is64 = 0;
	__u32			*p = fh;

#if XFS_BIG_INUMS
	is64 = (fileid_type & XFS_FILEID_TYPE_64FLAG);
	fileid_type &= ~XFS_FILEID_TYPE_64FLAG;
#endif

	/*
	 * 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.  There's little point printk'ing
	 * a warning here as we don't have the client information
	 * which would make such a warning useful.
	 */
	if (fileid_type > 2 ||
	    fh_len < xfs_fileid_length((fileid_type == 2), is64))
		return NULL;

	p = xfs_fileid_decode_fid2(p, &ifid, is64);

	if (fileid_type == 2) {
		p = xfs_fileid_decode_fid2(p, &pfid, is64);
		parent = &pfid;
	}
86

L
Linus Torvalds 已提交
87
	fh = (__u32 *)&ifid;
88
	return sb->s_export_op->find_exported_dentry(sb, fh, parent, acceptable, context);
L
Linus Torvalds 已提交
89 90 91 92
}


STATIC int
93
xfs_fs_encode_fh(
L
Linus Torvalds 已提交
94 95 96 97 98 99 100 101 102 103 104
	struct dentry		*dentry,
	__u32			*fh,
	int			*max_len,
	int			connectable)
{
	struct inode		*inode = dentry->d_inode;
	int			type = 1;
	__u32			*p = fh;
	int			len;
	int			is64 = 0;
#if XFS_BIG_INUMS
105
	if (!(XFS_M(inode->i_sb)->m_flags & XFS_MOUNT_SMALL_INUMS)) {
L
Linus Torvalds 已提交
106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137
		/* filesystem may contain 64bit inode numbers */
		is64 = XFS_FILEID_TYPE_64FLAG;
	}
#endif

	/* Directories don't need their parent encoded, they have ".." */
	if (S_ISDIR(inode->i_mode))
	    connectable = 0;

	/*
	 * 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".
	 */
	len = xfs_fileid_length(connectable, is64);
	if (*max_len < len)
		return 255;
	*max_len = len;

	p = xfs_fileid_encode_inode(p, inode, is64);
	if (connectable) {
		spin_lock(&dentry->d_lock);
		p = xfs_fileid_encode_inode(p, dentry->d_parent->d_inode, is64);
		spin_unlock(&dentry->d_lock);
		type = 2;
	}
	BUG_ON((p - fh) != len);
	return type | is64;
}

STATIC struct dentry *
138
xfs_fs_get_dentry(
L
Linus Torvalds 已提交
139 140 141
	struct super_block	*sb,
	void			*data)
{
142
	bhv_vnode_t		*vp;
L
Linus Torvalds 已提交
143 144 145 146
	struct inode		*inode;
	struct dentry		*result;
	int			error;

C
Christoph Hellwig 已提交
147
	error = xfs_vget(XFS_M(sb), &vp, data);
L
Linus Torvalds 已提交
148 149 150
	if (error || vp == NULL)
		return ERR_PTR(-ESTALE) ;

151
	inode = vn_to_inode(vp);
L
Linus Torvalds 已提交
152 153 154 155 156 157 158 159 160
	result = d_alloc_anon(inode);
        if (!result) {
		iput(inode);
		return ERR_PTR(-ENOMEM);
	}
	return result;
}

STATIC struct dentry *
161
xfs_fs_get_parent(
L
Linus Torvalds 已提交
162 163 164
	struct dentry		*child)
{
	int			error;
165
	bhv_vnode_t		*cvp;
L
Linus Torvalds 已提交
166 167 168
	struct dentry		*parent;

	cvp = NULL;
169
	error = xfs_lookup(XFS_I(child->d_inode), &dotdot, &cvp);
L
Linus Torvalds 已提交
170 171 172
	if (unlikely(error))
		return ERR_PTR(-error);

173
	parent = d_alloc_anon(vn_to_inode(cvp));
L
Linus Torvalds 已提交
174 175 176 177 178 179 180
	if (unlikely(!parent)) {
		VN_RELE(cvp);
		return ERR_PTR(-ENOMEM);
	}
	return parent;
}

181 182 183 184 185
struct export_operations xfs_export_operations = {
	.decode_fh		= xfs_fs_decode_fh,
	.encode_fh		= xfs_fs_encode_fh,
	.get_parent		= xfs_fs_get_parent,
	.get_dentry		= xfs_fs_get_dentry,
L
Linus Torvalds 已提交
186
};