blob: f6e99fa7a683c84509316dbae93f28386f010897 [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"
28
David Chinner7989cb82007-02-10 18:34:56 +110029static struct dentry dotdot = { .d_name.name = "..", .d_name.len = 2, };
Nathan Scott9b94c2e2006-03-14 13:32:54 +110030
Linus Torvalds1da177e2005-04-16 15:20:36 -070031/*
Nathan Scott7b718762005-11-02 14:58:39 +110032 * XFS encodes and decodes the fileid portion of NFS filehandles
Linus Torvalds1da177e2005-04-16 15:20:36 -070033 * itself instead of letting the generic NFS code do it. This
34 * allows filesystems with 64 bit inode numbers to be exported.
35 *
36 * Note that a side effect is that xfs_vget() won't be passed a
37 * zero inode/generation pair under normal circumstances. As
38 * however a malicious client could send us such data, the check
39 * remains in that code.
40 */
41
Linus Torvalds1da177e2005-04-16 15:20:36 -070042STATIC struct dentry *
Nathan Scotta50cd262006-03-14 14:06:18 +110043xfs_fs_decode_fh(
Linus Torvalds1da177e2005-04-16 15:20:36 -070044 struct super_block *sb,
45 __u32 *fh,
46 int fh_len,
47 int fileid_type,
48 int (*acceptable)(
49 void *context,
50 struct dentry *de),
51 void *context)
52{
53 xfs_fid2_t ifid;
54 xfs_fid2_t pfid;
55 void *parent = NULL;
56 int is64 = 0;
57 __u32 *p = fh;
58
59#if XFS_BIG_INUMS
60 is64 = (fileid_type & XFS_FILEID_TYPE_64FLAG);
61 fileid_type &= ~XFS_FILEID_TYPE_64FLAG;
62#endif
63
64 /*
65 * Note that we only accept fileids which are long enough
66 * rather than allow the parent generation number to default
67 * to zero. XFS considers zero a valid generation number not
68 * an invalid/wildcard value. There's little point printk'ing
69 * a warning here as we don't have the client information
70 * which would make such a warning useful.
71 */
72 if (fileid_type > 2 ||
73 fh_len < xfs_fileid_length((fileid_type == 2), is64))
74 return NULL;
75
76 p = xfs_fileid_decode_fid2(p, &ifid, is64);
77
78 if (fileid_type == 2) {
79 p = xfs_fileid_decode_fid2(p, &pfid, is64);
80 parent = &pfid;
81 }
Nathan Scott7b718762005-11-02 14:58:39 +110082
Linus Torvalds1da177e2005-04-16 15:20:36 -070083 fh = (__u32 *)&ifid;
David Chinner0c9512d2006-03-14 13:02:13 +110084 return sb->s_export_op->find_exported_dentry(sb, fh, parent, acceptable, context);
Linus Torvalds1da177e2005-04-16 15:20:36 -070085}
86
87
88STATIC int
Nathan Scotta50cd262006-03-14 14:06:18 +110089xfs_fs_encode_fh(
Linus Torvalds1da177e2005-04-16 15:20:36 -070090 struct dentry *dentry,
91 __u32 *fh,
92 int *max_len,
93 int connectable)
94{
95 struct inode *inode = dentry->d_inode;
96 int type = 1;
97 __u32 *p = fh;
98 int len;
99 int is64 = 0;
100#if XFS_BIG_INUMS
Nathan Scottb83bd132006-06-09 16:48:30 +1000101 bhv_vfs_t *vfs = vfs_from_sb(inode->i_sb);
Nathan Scott7b718762005-11-02 14:58:39 +1100102
Nathan Scottc11e2c32005-11-02 15:11:45 +1100103 if (!(vfs->vfs_flag & VFS_32BITINODES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700104 /* filesystem may contain 64bit inode numbers */
105 is64 = XFS_FILEID_TYPE_64FLAG;
106 }
107#endif
108
109 /* Directories don't need their parent encoded, they have ".." */
110 if (S_ISDIR(inode->i_mode))
111 connectable = 0;
112
113 /*
114 * Only encode if there is enough space given. In practice
115 * this means we can't export a filesystem with 64bit inodes
116 * over NFSv2 with the subtree_check export option; the other
117 * seven combinations work. The real answer is "don't use v2".
118 */
119 len = xfs_fileid_length(connectable, is64);
120 if (*max_len < len)
121 return 255;
122 *max_len = len;
123
124 p = xfs_fileid_encode_inode(p, inode, is64);
125 if (connectable) {
126 spin_lock(&dentry->d_lock);
127 p = xfs_fileid_encode_inode(p, dentry->d_parent->d_inode, is64);
128 spin_unlock(&dentry->d_lock);
129 type = 2;
130 }
131 BUG_ON((p - fh) != len);
132 return type | is64;
133}
134
135STATIC struct dentry *
Nathan Scotta50cd262006-03-14 14:06:18 +1100136xfs_fs_get_dentry(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700137 struct super_block *sb,
138 void *data)
139{
Nathan Scott67fcaa72006-06-09 17:00:52 +1000140 bhv_vnode_t *vp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700141 struct inode *inode;
142 struct dentry *result;
Nathan Scottb83bd132006-06-09 16:48:30 +1000143 bhv_vfs_t *vfsp = vfs_from_sb(sb);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700144 int error;
145
Nathan Scottb83bd132006-06-09 16:48:30 +1000146 error = bhv_vfs_vget(vfsp, &vp, (fid_t *)data);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700147 if (error || vp == NULL)
148 return ERR_PTR(-ESTALE) ;
149
Nathan Scottec86dc02006-03-17 17:25:36 +1100150 inode = vn_to_inode(vp);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700151 result = d_alloc_anon(inode);
152 if (!result) {
153 iput(inode);
154 return ERR_PTR(-ENOMEM);
155 }
156 return result;
157}
158
159STATIC struct dentry *
Nathan Scotta50cd262006-03-14 14:06:18 +1100160xfs_fs_get_parent(
Linus Torvalds1da177e2005-04-16 15:20:36 -0700161 struct dentry *child)
162{
163 int error;
Nathan Scott67fcaa72006-06-09 17:00:52 +1000164 bhv_vnode_t *vp, *cvp;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700165 struct dentry *parent;
Linus Torvalds1da177e2005-04-16 15:20:36 -0700166
167 cvp = NULL;
Nathan Scottec86dc02006-03-17 17:25:36 +1100168 vp = vn_from_inode(child->d_inode);
Nathan Scott67fcaa72006-06-09 17:00:52 +1000169 error = bhv_vop_lookup(vp, &dotdot, &cvp, 0, NULL, NULL);
Linus Torvalds1da177e2005-04-16 15:20:36 -0700170 if (unlikely(error))
171 return ERR_PTR(-error);
172
Nathan Scottec86dc02006-03-17 17:25:36 +1100173 parent = d_alloc_anon(vn_to_inode(cvp));
Linus Torvalds1da177e2005-04-16 15:20:36 -0700174 if (unlikely(!parent)) {
175 VN_RELE(cvp);
176 return ERR_PTR(-ENOMEM);
177 }
178 return parent;
179}
180
Nathan Scotta50cd262006-03-14 14:06:18 +1100181struct export_operations xfs_export_operations = {
182 .decode_fh = xfs_fs_decode_fh,
183 .encode_fh = xfs_fs_encode_fh,
184 .get_parent = xfs_fs_get_parent,
185 .get_dentry = xfs_fs_get_dentry,
Linus Torvalds1da177e2005-04-16 15:20:36 -0700186};