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
  #include "xfs.h"
a4fbe6ab1   Dave Chinner   xfs: decouple ino...
19
  #include "xfs_format.h"
239880ef6   Dave Chinner   xfs: decouple log...
20
21
  #include "xfs_log_format.h"
  #include "xfs_trans_resv.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
22
  #include "xfs_mount.h"
570627875   Dave Chinner   xfs: unify direct...
23
  #include "xfs_da_format.h"
9a2cc41cd   Dave Chinner   xfs: move type co...
24
  #include "xfs_da_btree.h"
2b9ab5ab9   Dave Chinner   xfs: reshuffle di...
25
  #include "xfs_dir2.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
26
  #include "xfs_export.h"
739bfb2a7   Christoph Hellwig   [XFS] call common...
27
  #include "xfs_inode.h"
239880ef6   Dave Chinner   xfs: decouple log...
28
  #include "xfs_trans.h"
978ebd97d   Ben Myers   xfs_export_operat...
29
  #include "xfs_inode_item.h"
ef35e9255   Christoph Hellwig   xfs: remove xfs_i...
30
  #include "xfs_trace.h"
33479e054   Dave Chinner   xfs: remove xfs_i...
31
  #include "xfs_icache.h"
239880ef6   Dave Chinner   xfs: decouple log...
32
  #include "xfs_log.h"
527851124   Christoph Hellwig   xfs: implement pN...
33
  #include "xfs_pnfs.h"
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
34
35
  
  /*
c38344fe9   Christoph Hellwig   xfs: new export ops
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.
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
39
   */
c38344fe9   Christoph Hellwig   xfs: new export ops
40
  static int xfs_fileid_length(int fileid_type)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
41
  {
c38344fe9   Christoph Hellwig   xfs: new export ops
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;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
51
  	}
94e07a759   Namjae Jeon   fs: encode_fh: re...
52
  	return FILEID_INVALID;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
53
  }
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
54
  STATIC int
a50cd2692   Nathan Scott   [XFS] Switch over...
55
  xfs_fs_encode_fh(
b0b0382bb   Al Viro   ->encode_fh() API...
56
57
58
59
  	struct inode	*inode,
  	__u32		*fh,
  	int		*max_len,
  	struct inode	*parent)
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
60
  {
c38344fe9   Christoph Hellwig   xfs: new export ops
61
62
  	struct fid		*fid = (struct fid *)fh;
  	struct xfs_fid64	*fid64 = (struct xfs_fid64 *)fh;
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 ".." */
b0b0382bb   Al Viro   ->encode_fh() API...
67
  	if (!parent)
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;
94e07a759   Namjae Jeon   fs: encode_fh: re...
92
  		return FILEID_INVALID;
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:
b0b0382bb   Al Viro   ->encode_fh() API...
97
98
  		fid->i32.parent_ino = XFS_I(parent)->i_ino;
  		fid->i32.parent_gen = parent->i_generation;
c38344fe9   Christoph Hellwig   xfs: new export ops
99
100
  		/*FALLTHRU*/
  	case FILEID_INO32_GEN:
c29f7d457   Christoph Hellwig   xfs: fix nfs expo...
101
  		fid->i32.ino = XFS_I(inode)->i_ino;
c38344fe9   Christoph Hellwig   xfs: new export ops
102
103
104
  		fid->i32.gen = inode->i_generation;
  		break;
  	case FILEID_INO32_GEN_PARENT | XFS_FILEID_TYPE_64FLAG:
b0b0382bb   Al Viro   ->encode_fh() API...
105
106
  		fid64->parent_ino = XFS_I(parent)->i_ino;
  		fid64->parent_gen = parent->i_generation;
c38344fe9   Christoph Hellwig   xfs: new export ops
107
108
  		/*FALLTHRU*/
  	case FILEID_INO32_GEN | XFS_FILEID_TYPE_64FLAG:
c29f7d457   Christoph Hellwig   xfs: fix nfs expo...
109
  		fid64->ino = XFS_I(inode)->i_ino;
c38344fe9   Christoph Hellwig   xfs: new export ops
110
111
  		fid64->gen = inode->i_generation;
  		break;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
112
  	}
c38344fe9   Christoph Hellwig   xfs: new export ops
113
114
  
  	return fileid_type;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
115
  }
c38344fe9   Christoph Hellwig   xfs: new export ops
116
117
  STATIC struct inode *
  xfs_nfs_get_inode(
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
118
  	struct super_block	*sb,
c38344fe9   Christoph Hellwig   xfs: new export ops
119
120
121
  	u64			ino,
  	u32			generation)
   {
f71354bc3   Christoph Hellwig   [XFS] Cleanup var...
122
123
   	xfs_mount_t		*mp = XFS_M(sb);
  	xfs_inode_t		*ip;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
124
  	int			error;
f71354bc3   Christoph Hellwig   [XFS] Cleanup var...
125
126
127
128
129
  	/*
  	 * 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
130

c9a98553d   Christoph Hellwig   [XFS] pass XFS_IG...
131
  	/*
1920779e6   Dave Chinner   xfs: rename XFS_I...
132
133
134
  	 * 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...
135
  	 */
ef35e9255   Christoph Hellwig   xfs: remove xfs_i...
136
  	error = xfs_iget(mp, NULL, ino, XFS_IGET_UNTRUSTED, 0, &ip);
c9a98553d   Christoph Hellwig   [XFS] pass XFS_IG...
137
138
139
140
141
142
143
144
  	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.
  		 */
2451337dd   Dave Chinner   xfs: global error...
145
146
147
  		if (error == -EINVAL || error == -ENOENT)
  			error = -ESTALE;
  		return ERR_PTR(error);
c9a98553d   Christoph Hellwig   [XFS] pass XFS_IG...
148
  	}
f71354bc3   Christoph Hellwig   [XFS] Cleanup var...
149

9e9a2674e   Dave Chinner   xfs: move inode g...
150
  	if (VFS_I(ip)->i_generation != generation) {
ef35e9255   Christoph Hellwig   xfs: remove xfs_i...
151
  		IRELE(ip);
ad1a2c878   J. Bruce Fields   xfs: failure mapp...
152
  		return ERR_PTR(-ESTALE);
f71354bc3   Christoph Hellwig   [XFS] Cleanup var...
153
  	}
c38344fe9   Christoph Hellwig   xfs: new export ops
154

016516462   David Chinner   [XFS] Avoid direc...
155
  	return VFS_I(ip);
c38344fe9   Christoph Hellwig   xfs: new export ops
156
157
158
159
160
161
162
163
  }
  
  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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
  
  	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...
178
  	return d_obtain_alias(inode);
c38344fe9   Christoph Hellwig   xfs: new export ops
179
180
181
182
183
184
185
186
  }
  
  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
187

35c2a7f49   Hugh Dickins   tmpfs,ceph,gfs2,i...
188
189
  	if (fh_len < xfs_fileid_length(fileid_type))
  		return NULL;
c38344fe9   Christoph Hellwig   xfs: new export ops
190
191
192
193
194
195
196
197
198
199
  	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...
200
  	return d_obtain_alias(inode);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
201
202
203
  }
  
  STATIC struct dentry *
a50cd2692   Nathan Scott   [XFS] Switch over...
204
  xfs_fs_get_parent(
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
205
206
207
  	struct dentry		*child)
  {
  	int			error;
ef1f5e7ad   Christoph Hellwig   [XFS] cleanup vno...
208
  	struct xfs_inode	*cip;
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
209

2b0143b5c   David Howells   VFS: normal files...
210
  	error = xfs_lookup(XFS_I(d_inode(child)), &xfs_name_dotdot, &cip, NULL);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
211
  	if (unlikely(error))
2451337dd   Dave Chinner   xfs: global error...
212
  		return ERR_PTR(error);
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
213

440037287   Christoph Hellwig   [PATCH] switch al...
214
  	return d_obtain_alias(VFS_I(cip));
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
215
  }
978ebd97d   Ben Myers   xfs_export_operat...
216
217
218
219
220
221
  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...
222
  	xfs_lsn_t		lsn = 0;
978ebd97d   Ben Myers   xfs_export_operat...
223
224
  
  	xfs_ilock(ip, XFS_ILOCK_SHARED);
8292d88c5   Christoph Hellwig   xfs: unlock the i...
225
226
  	if (xfs_ipincount(ip))
  		lsn = ip->i_itemp->ili_last_lsn;
978ebd97d   Ben Myers   xfs_export_operat...
227
  	xfs_iunlock(ip, XFS_ILOCK_SHARED);
8292d88c5   Christoph Hellwig   xfs: unlock the i...
228
229
  	if (!lsn)
  		return 0;
2451337dd   Dave Chinner   xfs: global error...
230
  	return _xfs_log_force_lsn(mp, lsn, XFS_LOG_SYNC, NULL);
978ebd97d   Ben Myers   xfs_export_operat...
231
  }
396551644   Christoph Hellwig   exportfs: make st...
232
  const struct export_operations xfs_export_operations = {
a50cd2692   Nathan Scott   [XFS] Switch over...
233
  	.encode_fh		= xfs_fs_encode_fh,
c38344fe9   Christoph Hellwig   xfs: new export ops
234
235
  	.fh_to_dentry		= xfs_fs_fh_to_dentry,
  	.fh_to_parent		= xfs_fs_fh_to_parent,
a50cd2692   Nathan Scott   [XFS] Switch over...
236
  	.get_parent		= xfs_fs_get_parent,
978ebd97d   Ben Myers   xfs_export_operat...
237
  	.commit_metadata	= xfs_fs_nfs_commit_metadata,
15d66ac20   Benjamin Coddington   xfs: abstract blo...
238
  #ifdef CONFIG_EXPORTFS_BLOCK_OPS
527851124   Christoph Hellwig   xfs: implement pN...
239
240
241
242
  	.get_uuid		= xfs_fs_get_uuid,
  	.map_blocks		= xfs_fs_map_blocks,
  	.commit_blocks		= xfs_fs_commit_blocks,
  #endif
1da177e4c   Linus Torvalds   Linux-2.6.12-rc2
243
  };