blob: e3a5fedac1bad6166b94e92bae3dbcb8ac10565b [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"
20#include "xfs_dmapi.h"
21#include "xfs_log.h"
22#include "xfs_trans.h"
23#include "xfs_sb.h"
Linus Torvalds1da177e2005-04-16 15:20:36 -070024#include "xfs_mount.h"
25#include "xfs_export.h"
26
David Chinner7989cb82007-02-10 18:34:56 +110027static struct dentry dotdot = { .d_name.name = "..", .d_name.len = 2, };
Nathan Scott9b94c2e2006-03-14 13:32:54 +110028
Linus Torvalds1da177e2005-04-16 15:20:36 -070029/*
Nathan Scott7b718762005-11-02 14:58:39 +110030 * XFS encodes and decodes the fileid portion of NFS filehandles
Linus Torvalds1da177e2005-04-16 15:20:36 -070031 * itself instead of letting the generic NFS code do it. This
32 * allows filesystems with 64 bit inode numbers to be exported.
33 *
34 * Note that a side effect is that xfs_vget() won't be passed a
35 * zero inode/generation pair under normal circumstances. As
36 * however a malicious client could send us such data, the check
37 * remains in that code.
38 */
39
Linus Torvalds1da177e2005-04-16 15:20:36 -070040STATIC struct dentry *
Nathan Scotta50cd262006-03-14 14:06:18 +110041xfs_fs_decode_fh(
Linus Torvalds1da177e2005-04-16 15:20:36 -070042 struct super_block *sb,
43 __u32 *fh,
44 int fh_len,
45 int fileid_type,
46 int (*acceptable)(
47 void *context,
48 struct dentry *de),
49 void *context)
50{
51 xfs_fid2_t ifid;
52 xfs_fid2_t pfid;
53 void *parent = NULL;
54 int is64 = 0;
55 __u32 *p = fh;
56
57#if XFS_BIG_INUMS
58 is64 = (fileid_type & XFS_FILEID_TYPE_64FLAG);
59 fileid_type &= ~XFS_FILEID_TYPE_64FLAG;
60#endif
61
62 /*
63 * Note that we only accept fileids which are long enough
64 * rather than allow the parent generation number to default
65 * to zero. XFS considers zero a valid generation number not
66 * an invalid/wildcard value. There's little point printk'ing
67 * a warning here as we don't have the client information
68 * which would make such a warning useful.
69 */
70 if (fileid_type > 2 ||
71 fh_len < xfs_fileid_length((fileid_type == 2), is64))
72 return NULL;
73
74 p = xfs_fileid_decode_fid2(p, &ifid, is64);
75
76 if (fileid_type == 2) {
77 p = xfs_fileid_decode_fid2(p, &pfid, is64);
78 parent = &pfid;
79 }
Nathan Scott7b718762005-11-02 14:58:39 +110080
Linus Torvalds1da177e2005-04-16 15:20:36 -070081 fh = (__u32 *)&ifid;
David Chinner0c9512d2006-03-14 13:02:13 +110082 return sb->s_export_op->find_exported_dentry(sb, fh, parent, acceptable, context);
Linus Torvalds1da177e2005-04-16 15:20:36 -070083}
84
85
86STATIC int
Nathan Scotta50cd262006-03-14 14:06:18 +110087xfs_fs_encode_fh(
Linus Torvalds1da177e2005-04-16 15:20:36 -070088 struct dentry *dentry,
89 __u32 *fh,
90 int *max_len,
91 int connectable)
92{
93 struct inode *inode = dentry->d_inode;
94 int type = 1;
95 __u32 *p = fh;
96 int len;
97 int is64 = 0;
98#if XFS_BIG_INUMS
Nathan Scottb83bd132006-06-09 16:48:30 +100099 bhv_vfs_t *vfs = vfs_from_sb(inode->i_sb);
Nathan Scott7b718762005-11-02 14:58:39 +1100100
Nathan Scottc11e2c32005-11-02 15:11:45 +1100101 if (!(vfs->vfs_flag & VFS_32BITINODES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700102 /* filesystem may contain 64bit inode numbers */
103 is64 = XFS_FILEID_TYPE_64FLAG;
104 }
105#endif
106
107 /* Directories don't need their parent encoded, they have ".." */
108 if (S_ISDIR(inode->i_mode))
109 connectable = 0;
110
111 /*
112 * Only encode if there is enough space given. In practice
113 * this means we can't export a filesystem with 64bit inodes
114 * over NFSv2 with the subtree_check export option; the other
115 * seven combinations work. The real answer is "don't use v2".
116 */
117 len = xfs_fileid_length(connectable, is64);
118 if (*max_len < len)
119 return 255;
120 *max_len = len;
121
122 p = xfs_fileid_encode_inode(p, inode, is64);
123 if (connectable) {
124 spin_lock(&dentry->d_lock);
125 p = xfs_fileid_encode_inode(p, dentry->d_parent->d_inode, is64);
126 spin_unlock(&dentry->d_lock);
127 type = 2;
128 }
129 BUG_ON((p - fh) != len);
130 return type | is64;
131}
132
133STATIC struct dentry *
Nathan Scotta50cd262006-03-14 14:06:18 +1100134xfs_fs_get_dentry(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700135 struct super_block *sb,
136 void *data)
137{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000138 bhv_vnode_t *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700139 struct inode *inode;
140 struct dentry *result;
Nathan Scottb83bd132006-06-09 16:48:30 +1000141 bhv_vfs_t *vfsp = vfs_from_sb(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700142 int error;
143
Nathan Scottb83bd132006-06-09 16:48:30 +1000144 error = bhv_vfs_vget(vfsp, &vp, (fid_t *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700145 if (error || vp == NULL)
146 return ERR_PTR(-ESTALE) ;
147
Nathan Scottec86dc02006-03-17 17:25:36 +1100148 inode = vn_to_inode(vp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700149 result = d_alloc_anon(inode);
150 if (!result) {
151 iput(inode);
152 return ERR_PTR(-ENOMEM);
153 }
154 return result;
155}
156
157STATIC struct dentry *
Nathan Scotta50cd262006-03-14 14:06:18 +1100158xfs_fs_get_parent(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700159 struct dentry *child)
160{
161 int error;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000162 bhv_vnode_t *vp, *cvp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700163 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164
165 cvp = NULL;
Nathan Scottec86dc02006-03-17 17:25:36 +1100166 vp = vn_from_inode(child->d_inode);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000167 error = bhv_vop_lookup(vp, &dotdot, &cvp, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700168 if (unlikely(error))
169 return ERR_PTR(-error);
170
Nathan Scottec86dc02006-03-17 17:25:36 +1100171 parent = d_alloc_anon(vn_to_inode(cvp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700172 if (unlikely(!parent)) {
173 VN_RELE(cvp);
174 return ERR_PTR(-ENOMEM);
175 }
176 return parent;
177}
178
Nathan Scotta50cd262006-03-14 14:06:18 +1100179struct export_operations xfs_export_operations = {
180 .decode_fh = xfs_fs_decode_fh,
181 .encode_fh = xfs_fs_encode_fh,
182 .get_parent = xfs_fs_get_parent,
183 .get_dentry = xfs_fs_get_dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700184};