blob: d32a9edc43a9011a91bba45400991dd5404b31d5 [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"
24#include "xfs_dir.h"
25#include "xfs_mount.h"
26#include "xfs_export.h"
27
Nathan Scott9b94c2e2006-03-14 13:32:54 +110028STATIC struct dentry dotdot = { .d_name.name = "..", .d_name.len = 2, };
29
Linus Torvalds1da177e2005-04-16 15:20:36 -070030/*
Nathan Scott7b718762005-11-02 14:58:39 +110031 * XFS encodes and decodes the fileid portion of NFS filehandles
Linus Torvalds1da177e2005-04-16 15:20:36 -070032 * itself instead of letting the generic NFS code do it. This
33 * allows filesystems with 64 bit inode numbers to be exported.
34 *
35 * Note that a side effect is that xfs_vget() won't be passed a
36 * zero inode/generation pair under normal circumstances. As
37 * however a malicious client could send us such data, the check
38 * remains in that code.
39 */
40
Linus Torvalds1da177e2005-04-16 15:20:36 -070041STATIC struct dentry *
Nathan Scotta50cd262006-03-14 14:06:18 +110042xfs_fs_decode_fh(
Linus Torvalds1da177e2005-04-16 15:20:36 -070043 struct super_block *sb,
44 __u32 *fh,
45 int fh_len,
46 int fileid_type,
47 int (*acceptable)(
48 void *context,
49 struct dentry *de),
50 void *context)
51{
52 xfs_fid2_t ifid;
53 xfs_fid2_t pfid;
54 void *parent = NULL;
55 int is64 = 0;
56 __u32 *p = fh;
57
58#if XFS_BIG_INUMS
59 is64 = (fileid_type & XFS_FILEID_TYPE_64FLAG);
60 fileid_type &= ~XFS_FILEID_TYPE_64FLAG;
61#endif
62
63 /*
64 * Note that we only accept fileids which are long enough
65 * rather than allow the parent generation number to default
66 * to zero. XFS considers zero a valid generation number not
67 * an invalid/wildcard value. There's little point printk'ing
68 * a warning here as we don't have the client information
69 * which would make such a warning useful.
70 */
71 if (fileid_type > 2 ||
72 fh_len < xfs_fileid_length((fileid_type == 2), is64))
73 return NULL;
74
75 p = xfs_fileid_decode_fid2(p, &ifid, is64);
76
77 if (fileid_type == 2) {
78 p = xfs_fileid_decode_fid2(p, &pfid, is64);
79 parent = &pfid;
80 }
Nathan Scott7b718762005-11-02 14:58:39 +110081
Linus Torvalds1da177e2005-04-16 15:20:36 -070082 fh = (__u32 *)&ifid;
David Chinner0c9512d2006-03-14 13:02:13 +110083 return sb->s_export_op->find_exported_dentry(sb, fh, parent, acceptable, context);
Linus Torvalds1da177e2005-04-16 15:20:36 -070084}
85
86
87STATIC int
Nathan Scotta50cd262006-03-14 14:06:18 +110088xfs_fs_encode_fh(
Linus Torvalds1da177e2005-04-16 15:20:36 -070089 struct dentry *dentry,
90 __u32 *fh,
91 int *max_len,
92 int connectable)
93{
94 struct inode *inode = dentry->d_inode;
95 int type = 1;
96 __u32 *p = fh;
97 int len;
98 int is64 = 0;
99#if XFS_BIG_INUMS
Nathan Scottb83bd132006-06-09 16:48:30 +1000100 bhv_vfs_t *vfs = vfs_from_sb(inode->i_sb);
Nathan Scott7b718762005-11-02 14:58:39 +1100101
Nathan Scottc11e2c32005-11-02 15:11:45 +1100102 if (!(vfs->vfs_flag & VFS_32BITINODES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700103 /* filesystem may contain 64bit inode numbers */
104 is64 = XFS_FILEID_TYPE_64FLAG;
105 }
106#endif
107
108 /* Directories don't need their parent encoded, they have ".." */
109 if (S_ISDIR(inode->i_mode))
110 connectable = 0;
111
112 /*
113 * Only encode if there is enough space given. In practice
114 * this means we can't export a filesystem with 64bit inodes
115 * over NFSv2 with the subtree_check export option; the other
116 * seven combinations work. The real answer is "don't use v2".
117 */
118 len = xfs_fileid_length(connectable, is64);
119 if (*max_len < len)
120 return 255;
121 *max_len = len;
122
123 p = xfs_fileid_encode_inode(p, inode, is64);
124 if (connectable) {
125 spin_lock(&dentry->d_lock);
126 p = xfs_fileid_encode_inode(p, dentry->d_parent->d_inode, is64);
127 spin_unlock(&dentry->d_lock);
128 type = 2;
129 }
130 BUG_ON((p - fh) != len);
131 return type | is64;
132}
133
134STATIC struct dentry *
Nathan Scotta50cd262006-03-14 14:06:18 +1100135xfs_fs_get_dentry(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700136 struct super_block *sb,
137 void *data)
138{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000139 bhv_vnode_t *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700140 struct inode *inode;
141 struct dentry *result;
Nathan Scottb83bd132006-06-09 16:48:30 +1000142 bhv_vfs_t *vfsp = vfs_from_sb(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700143 int error;
144
Nathan Scottb83bd132006-06-09 16:48:30 +1000145 error = bhv_vfs_vget(vfsp, &vp, (fid_t *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700146 if (error || vp == NULL)
147 return ERR_PTR(-ESTALE) ;
148
Nathan Scottec86dc02006-03-17 17:25:36 +1100149 inode = vn_to_inode(vp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700150 result = d_alloc_anon(inode);
151 if (!result) {
152 iput(inode);
153 return ERR_PTR(-ENOMEM);
154 }
155 return result;
156}
157
158STATIC struct dentry *
Nathan Scotta50cd262006-03-14 14:06:18 +1100159xfs_fs_get_parent(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700160 struct dentry *child)
161{
162 int error;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000163 bhv_vnode_t *vp, *cvp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700164 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165
166 cvp = NULL;
Nathan Scottec86dc02006-03-17 17:25:36 +1100167 vp = vn_from_inode(child->d_inode);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000168 error = bhv_vop_lookup(vp, &dotdot, &cvp, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700169 if (unlikely(error))
170 return ERR_PTR(-error);
171
Nathan Scottec86dc02006-03-17 17:25:36 +1100172 parent = d_alloc_anon(vn_to_inode(cvp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700173 if (unlikely(!parent)) {
174 VN_RELE(cvp);
175 return ERR_PTR(-ENOMEM);
176 }
177 return parent;
178}
179
Nathan Scotta50cd262006-03-14 14:06:18 +1100180struct export_operations xfs_export_operations = {
181 .decode_fh = xfs_fs_decode_fh,
182 .encode_fh = xfs_fs_encode_fh,
183 .get_parent = xfs_fs_get_parent,
184 .get_dentry = xfs_fs_get_dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700185};