Blame view

fs/xfs/xfs_export.c 6.63 KB
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
1
  /*
7b7187698   Nathan Scott   [XFS] Update lice...
2
3
   * Copyright (c) 2004-2005 Silicon Graphics, Inc.
   * All Rights Reserved.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
4
   *
7b7187698   Nathan Scott   [XFS] Update lice...
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
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
7
8
   * published by the Free Software Foundation.
   *
7b7187698   Nathan Scott   [XFS] Update lice...
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.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
13
   *
7b7187698   Nathan Scott   [XFS] Update lice...
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
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
17
   */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
18
19
  #include "xfs.h"
  #include "xfs_types.h"
da353b0d6   David Chinner   [XFS] Radix tree ...
20
  #include "xfs_inum.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
21
22
23
  #include "xfs_log.h"
  #include "xfs_trans.h"
  #include "xfs_sb.h"
da353b0d6   David Chinner   [XFS] Radix tree ...
24
  #include "xfs_ag.h"
556b8b166   Barry Naujok   [XFS] remove bhv_...
25
  #include "xfs_dir2.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26
27
  #include "xfs_mount.h"
  #include "xfs_export.h"
739bfb2a7   Christoph Hellwig   [XFS] call common...
28
29
30
  #include "xfs_vnodeops.h"
  #include "xfs_bmap_btree.h"
  #include "xfs_inode.h"
978ebd97d   Ben Myers   xfs_export_operat...
31
  #include "xfs_inode_item.h"
ef35e9255   Christoph Hellwig   xfs: remove xfs_i...
32
  #include "xfs_trace.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
33
34
  
  /*
c38344fe9   Christoph Hellwig   xfs: new export ops
35
36
37
   * 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.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
38
   */
c38344fe9   Christoph Hellwig   xfs: new export ops
39
  static int xfs_fileid_length(int fileid_type)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
40
  {
c38344fe9   Christoph Hellwig   xfs: new export ops
41
42
43
44
45
46
47
48
49
  	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;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
50
  	}
c38344fe9   Christoph Hellwig   xfs: new export ops
51
  	return 255; /* invalid */
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
52
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
53
  STATIC int
a50cd2692   Nathan Scott   [XFS] Switch over...
54
  xfs_fs_encode_fh(
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
55
56
57
58
59
  	struct dentry		*dentry,
  	__u32			*fh,
  	int			*max_len,
  	int			connectable)
  {
c38344fe9   Christoph Hellwig   xfs: new export ops
60
61
  	struct fid		*fid = (struct fid *)fh;
  	struct xfs_fid64	*fid64 = (struct xfs_fid64 *)fh;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
62
  	struct inode		*inode = dentry->d_inode;
c38344fe9   Christoph Hellwig   xfs: new export ops
63
  	int			fileid_type;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
64
  	int			len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
65
66
  
  	/* Directories don't need their parent encoded, they have ".." */
24bd861d1   Christoph Hellwig   [XFS] don't encod...
67
  	if (S_ISDIR(inode->i_mode) || !connectable)
c38344fe9   Christoph Hellwig   xfs: new export ops
68
69
70
  		fileid_type = FILEID_INO32_GEN;
  	else
  		fileid_type = FILEID_INO32_GEN_PARENT;
576ecb8e2   Samuel Kvasnica   xfs: fix exportin...
71
72
73
74
75
76
77
78
79
80
  	/*
  	 * 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))
c38344fe9   Christoph Hellwig   xfs: new export ops
81
  		fileid_type |= XFS_FILEID_TYPE_64FLAG;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
82
83
84
85
86
87
88
  
  	/*
  	 * 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".
  	 */
c38344fe9   Christoph Hellwig   xfs: new export ops
89
  	len = xfs_fileid_length(fileid_type);
5fe0c2378   Aneesh Kumar K.V   exportfs: Return ...
90
91
  	if (*max_len < len) {
  		*max_len = len;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
92
  		return 255;
5fe0c2378   Aneesh Kumar K.V   exportfs: Return ...
93
  	}
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
94
  	*max_len = len;
c38344fe9   Christoph Hellwig   xfs: new export ops
95
96
  	switch (fileid_type) {
  	case FILEID_INO32_GEN_PARENT:
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
97
  		spin_lock(&dentry->d_lock);
c29f7d457   Christoph Hellwig   xfs: fix nfs expo...
98
  		fid->i32.parent_ino = XFS_I(dentry->d_parent->d_inode)->i_ino;
c38344fe9   Christoph Hellwig   xfs: new export ops
99
  		fid->i32.parent_gen = dentry->d_parent->d_inode->i_generation;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
100
  		spin_unlock(&dentry->d_lock);
c38344fe9   Christoph Hellwig   xfs: new export ops
101
102
  		/*FALLTHRU*/
  	case FILEID_INO32_GEN:
c29f7d457   Christoph Hellwig   xfs: fix nfs expo...
103
  		fid->i32.ino = XFS_I(inode)->i_ino;
c38344fe9   Christoph Hellwig   xfs: new export ops
104
105
106
107
  		fid->i32.gen = inode->i_generation;
  		break;
  	case FILEID_INO32_GEN_PARENT | XFS_FILEID_TYPE_64FLAG:
  		spin_lock(&dentry->d_lock);
c29f7d457   Christoph Hellwig   xfs: fix nfs expo...
108
  		fid64->parent_ino = XFS_I(dentry->d_parent->d_inode)->i_ino;
c38344fe9   Christoph Hellwig   xfs: new export ops
109
110
111
112
  		fid64->parent_gen = dentry->d_parent->d_inode->i_generation;
  		spin_unlock(&dentry->d_lock);
  		/*FALLTHRU*/
  	case FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG:
c29f7d457   Christoph Hellwig   xfs: fix nfs expo...
113
  		fid64->ino = XFS_I(inode)->i_ino;
c38344fe9   Christoph Hellwig   xfs: new export ops
114
115
  		fid64->gen = inode->i_generation;
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
116
  	}
c38344fe9   Christoph Hellwig   xfs: new export ops
117
118
  
  	return fileid_type;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
119
  }
c38344fe9   Christoph Hellwig   xfs: new export ops
120
121
  STATIC struct inode *
  xfs_nfs_get_inode(
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
122
  	struct super_block	*sb,
c38344fe9   Christoph Hellwig   xfs: new export ops
123
124
125
  	u64			ino,
  	u32			generation)
   {
f71354bc3   Christoph Hellwig   [XFS] Cleanup var...
126
127
   	xfs_mount_t		*mp = XFS_M(sb);
  	xfs_inode_t		*ip;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
128
  	int			error;
f71354bc3   Christoph Hellwig   [XFS] Cleanup var...
129
130
131
132
133
  	/*
  	 * NFS can sometimes send requests for ino 0.  Fail them gracefully.
  	 */
  	if (ino == 0)
  		return ERR_PTR(-ESTALE);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
134

c9a98553d   Christoph Hellwig   [XFS] pass XFS_IG...
135
  	/*
1920779e6   Dave Chinner   xfs: rename XFS_I...
136
137
138
  	 * 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..
c9a98553d   Christoph Hellwig   [XFS] pass XFS_IG...
139
  	 */
ef35e9255   Christoph Hellwig   xfs: remove xfs_i...
140
  	error = xfs_iget(mp, NULL, ino, XFS_IGET_UNTRUSTED, 0, &ip);
c9a98553d   Christoph Hellwig   [XFS] pass XFS_IG...
141
142
143
144
145
146
147
148
  	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.
  		 */
ad1a2c878   J. Bruce Fields   xfs: failure mapp...
149
  		if (error == EINVAL || error == ENOENT)
c9a98553d   Christoph Hellwig   [XFS] pass XFS_IG...
150
  			error = ESTALE;
c38344fe9   Christoph Hellwig   xfs: new export ops
151
  		return ERR_PTR(-error);
c9a98553d   Christoph Hellwig   [XFS] pass XFS_IG...
152
  	}
f71354bc3   Christoph Hellwig   [XFS] Cleanup var...
153

6a7f422d4   Christoph Hellwig   [XFS] kill di_mod...
154
  	if (ip->i_d.di_gen != generation) {
ef35e9255   Christoph Hellwig   xfs: remove xfs_i...
155
  		IRELE(ip);
ad1a2c878   J. Bruce Fields   xfs: failure mapp...
156
  		return ERR_PTR(-ESTALE);
f71354bc3   Christoph Hellwig   [XFS] Cleanup var...
157
  	}
c38344fe9   Christoph Hellwig   xfs: new export ops
158

016516462   David Chinner   [XFS] Avoid direc...
159
  	return VFS_I(ip);
c38344fe9   Christoph Hellwig   xfs: new export ops
160
161
162
163
164
165
166
167
  }
  
  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;
c38344fe9   Christoph Hellwig   xfs: new export ops
168
169
170
171
172
173
174
175
176
177
178
179
180
181
  
  	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;
  	}
440037287   Christoph Hellwig   [PATCH] switch al...
182
  	return d_obtain_alias(inode);
c38344fe9   Christoph Hellwig   xfs: new export ops
183
184
185
186
187
188
189
190
  }
  
  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;
c38344fe9   Christoph Hellwig   xfs: new export ops
191
192
193
194
195
196
197
198
199
200
201
  
  	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;
  	}
440037287   Christoph Hellwig   [PATCH] switch al...
202
  	return d_obtain_alias(inode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
203
204
205
  }
  
  STATIC struct dentry *
a50cd2692   Nathan Scott   [XFS] Switch over...
206
  xfs_fs_get_parent(
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
207
208
209
  	struct dentry		*child)
  {
  	int			error;
ef1f5e7ad   Christoph Hellwig   [XFS] cleanup vno...
210
  	struct xfs_inode	*cip;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
211

384f3ced0   Barry Naujok   [XFS] Return case...
212
  	error = xfs_lookup(XFS_I(child->d_inode), &xfs_name_dotdot, &cip, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
213
214
  	if (unlikely(error))
  		return ERR_PTR(-error);
440037287   Christoph Hellwig   [PATCH] switch al...
215
  	return d_obtain_alias(VFS_I(cip));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
216
  }
978ebd97d   Ben Myers   xfs_export_operat...
217
218
219
220
221
222
  STATIC int
  xfs_fs_nfs_commit_metadata(
  	struct inode		*inode)
  {
  	struct xfs_inode	*ip = XFS_I(inode);
  	struct xfs_mount	*mp = ip->i_mount;
8292d88c5   Christoph Hellwig   xfs: unlock the i...
223
  	xfs_lsn_t		lsn = 0;
978ebd97d   Ben Myers   xfs_export_operat...
224
225
  
  	xfs_ilock(ip, XFS_ILOCK_SHARED);
8292d88c5   Christoph Hellwig   xfs: unlock the i...
226
227
  	if (xfs_ipincount(ip))
  		lsn = ip->i_itemp->ili_last_lsn;
978ebd97d   Ben Myers   xfs_export_operat...
228
  	xfs_iunlock(ip, XFS_ILOCK_SHARED);
8292d88c5   Christoph Hellwig   xfs: unlock the i...
229
230
231
  	if (!lsn)
  		return 0;
  	return _xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, NULL);
978ebd97d   Ben Myers   xfs_export_operat...
232
  }
396551644   Christoph Hellwig   exportfs: make st...
233
  const struct export_operations xfs_export_operations = {
a50cd2692   Nathan Scott   [XFS] Switch over...
234
  	.encode_fh		= xfs_fs_encode_fh,
c38344fe9   Christoph Hellwig   xfs: new export ops
235
236
  	.fh_to_dentry		= xfs_fs_fh_to_dentry,
  	.fh_to_parent		= xfs_fs_fh_to_parent,
a50cd2692   Nathan Scott   [XFS] Switch over...
237
  	.get_parent		= xfs_fs_get_parent,
978ebd97d   Ben Myers   xfs_export_operat...
238
  	.commit_metadata	= xfs_fs_nfs_commit_metadata,
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
239
  };