blob: 4ed28a6767bbc3c58824627e4c6bf65440a46f52 [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"
19#include "xfs_types.h"
David Chinnerda353b02007-08-28 14:00:13 +100020#include "xfs_inum.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070021#include "xfs_log.h"
22#include "xfs_trans.h"
23#include "xfs_sb.h"
David Chinnerda353b02007-08-28 14:00:13 +100024#include "xfs_ag.h"
25#include "xfs_dmapi.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070026#include "xfs_mount.h"
27#include "xfs_export.h"
Christoph Hellwig739bfb22007-08-29 10:58:01 +100028#include "xfs_vnodeops.h"
29#include "xfs_bmap_btree.h"
30#include "xfs_inode.h"
Christoph Hellwig745f6912007-08-30 17:20:39 +100031#include "xfs_vfsops.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070032
David Chinner7989cb82007-02-10 18:34:56 +110033static struct dentry dotdot = { .d_name.name = "..", .d_name.len = 2, };
Nathan Scott9b94c2e2006-03-14 13:32:54 +110034
Linus Torvalds1da177e2005-04-16 15:20:36 -070035/*
Nathan Scott7b718762005-11-02 14:58:39 +110036 * XFS encodes and decodes the fileid portion of NFS filehandles
Linus Torvalds1da177e2005-04-16 15:20:36 -070037 * itself instead of letting the generic NFS code do it. This
38 * allows filesystems with 64 bit inode numbers to be exported.
39 *
40 * Note that a side effect is that xfs_vget() won't be passed a
41 * zero inode/generation pair under normal circumstances. As
42 * however a malicious client could send us such data, the check
43 * remains in that code.
44 */
45
Linus Torvalds1da177e2005-04-16 15:20:36 -070046STATIC struct dentry *
Nathan Scotta50cd262006-03-14 14:06:18 +110047xfs_fs_decode_fh(
Linus Torvalds1da177e2005-04-16 15:20:36 -070048 struct super_block *sb,
49 __u32 *fh,
50 int fh_len,
51 int fileid_type,
52 int (*acceptable)(
53 void *context,
54 struct dentry *de),
55 void *context)
56{
57 xfs_fid2_t ifid;
58 xfs_fid2_t pfid;
59 void *parent = NULL;
60 int is64 = 0;
61 __u32 *p = fh;
62
63#if XFS_BIG_INUMS
64 is64 = (fileid_type & XFS_FILEID_TYPE_64FLAG);
65 fileid_type &= ~XFS_FILEID_TYPE_64FLAG;
66#endif
67
68 /*
69 * Note that we only accept fileids which are long enough
70 * rather than allow the parent generation number to default
71 * to zero. XFS considers zero a valid generation number not
72 * an invalid/wildcard value. There's little point printk'ing
73 * a warning here as we don't have the client information
74 * which would make such a warning useful.
75 */
76 if (fileid_type > 2 ||
77 fh_len < xfs_fileid_length((fileid_type == 2), is64))
78 return NULL;
79
80 p = xfs_fileid_decode_fid2(p, &ifid, is64);
81
82 if (fileid_type == 2) {
83 p = xfs_fileid_decode_fid2(p, &pfid, is64);
84 parent = &pfid;
85 }
Nathan Scott7b718762005-11-02 14:58:39 +110086
Linus Torvalds1da177e2005-04-16 15:20:36 -070087 fh = (__u32 *)&ifid;
David Chinner0c9512d2006-03-14 13:02:13 +110088 return sb->s_export_op->find_exported_dentry(sb, fh, parent, acceptable, context);
Linus Torvalds1da177e2005-04-16 15:20:36 -070089}
90
91
92STATIC int
Nathan Scotta50cd262006-03-14 14:06:18 +110093xfs_fs_encode_fh(
Linus Torvalds1da177e2005-04-16 15:20:36 -070094 struct dentry *dentry,
95 __u32 *fh,
96 int *max_len,
97 int connectable)
98{
99 struct inode *inode = dentry->d_inode;
100 int type = 1;
101 __u32 *p = fh;
102 int len;
103 int is64 = 0;
104#if XFS_BIG_INUMS
Nathan Scottb83bd132006-06-09 16:48:30 +1000105 bhv_vfs_t *vfs = vfs_from_sb(inode->i_sb);
Nathan Scott7b718762005-11-02 14:58:39 +1100106
Nathan Scottc11e2c32005-11-02 15:11:45 +1100107 if (!(vfs->vfs_flag & VFS_32BITINODES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700108 /* filesystem may contain 64bit inode numbers */
109 is64 = XFS_FILEID_TYPE_64FLAG;
110 }
111#endif
112
113 /* Directories don't need their parent encoded, they have ".." */
114 if (S_ISDIR(inode->i_mode))
115 connectable = 0;
116
117 /*
118 * Only encode if there is enough space given. In practice
119 * this means we can't export a filesystem with 64bit inodes
120 * over NFSv2 with the subtree_check export option; the other
121 * seven combinations work. The real answer is "don't use v2".
122 */
123 len = xfs_fileid_length(connectable, is64);
124 if (*max_len < len)
125 return 255;
126 *max_len = len;
127
128 p = xfs_fileid_encode_inode(p, inode, is64);
129 if (connectable) {
130 spin_lock(&dentry->d_lock);
131 p = xfs_fileid_encode_inode(p, dentry->d_parent->d_inode, is64);
132 spin_unlock(&dentry->d_lock);
133 type = 2;
134 }
135 BUG_ON((p - fh) != len);
136 return type | is64;
137}
138
139STATIC struct dentry *
Nathan Scotta50cd262006-03-14 14:06:18 +1100140xfs_fs_get_dentry(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 struct super_block *sb,
142 void *data)
143{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000144 bhv_vnode_t *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 struct inode *inode;
146 struct dentry *result;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 int error;
148
Christoph Hellwig745f6912007-08-30 17:20:39 +1000149 error = xfs_vget(XFS_M(sb), &vp, (fid_t *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 if (error || vp == NULL)
151 return ERR_PTR(-ESTALE) ;
152
Nathan Scottec86dc02006-03-17 17:25:36 +1100153 inode = vn_to_inode(vp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700154 result = d_alloc_anon(inode);
155 if (!result) {
156 iput(inode);
157 return ERR_PTR(-ENOMEM);
158 }
159 return result;
160}
161
162STATIC struct dentry *
Nathan Scotta50cd262006-03-14 14:06:18 +1100163xfs_fs_get_parent(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 struct dentry *child)
165{
166 int error;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000167 bhv_vnode_t *cvp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169
170 cvp = NULL;
Christoph Hellwig739bfb22007-08-29 10:58:01 +1000171 error = xfs_lookup(XFS_I(child->d_inode), &dotdot, &cvp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (unlikely(error))
173 return ERR_PTR(-error);
174
Nathan Scottec86dc02006-03-17 17:25:36 +1100175 parent = d_alloc_anon(vn_to_inode(cvp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700176 if (unlikely(!parent)) {
177 VN_RELE(cvp);
178 return ERR_PTR(-ENOMEM);
179 }
180 return parent;
181}
182
Nathan Scotta50cd262006-03-14 14:06:18 +1100183struct export_operations xfs_export_operations = {
184 .decode_fh = xfs_fs_decode_fh,
185 .encode_fh = xfs_fs_encode_fh,
186 .get_parent = xfs_fs_get_parent,
187 .get_dentry = xfs_fs_get_dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700188};