blob: 80eb249f2fa0ae83a192d6721d2d48e76665951d [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
28/*
Nathan Scott7b718762005-11-02 14:58:39 +110029 * XFS encodes and decodes the fileid portion of NFS filehandles
Linus Torvalds1da177e2005-04-16 15:20:36 -070030 * itself instead of letting the generic NFS code do it. This
31 * allows filesystems with 64 bit inode numbers to be exported.
32 *
33 * Note that a side effect is that xfs_vget() won't be passed a
34 * zero inode/generation pair under normal circumstances. As
35 * however a malicious client could send us such data, the check
36 * remains in that code.
37 */
38
Linus Torvalds1da177e2005-04-16 15:20:36 -070039STATIC struct dentry *
40linvfs_decode_fh(
41 struct super_block *sb,
42 __u32 *fh,
43 int fh_len,
44 int fileid_type,
45 int (*acceptable)(
46 void *context,
47 struct dentry *de),
48 void *context)
49{
50 xfs_fid2_t ifid;
51 xfs_fid2_t pfid;
52 void *parent = NULL;
53 int is64 = 0;
54 __u32 *p = fh;
55
56#if XFS_BIG_INUMS
57 is64 = (fileid_type & XFS_FILEID_TYPE_64FLAG);
58 fileid_type &= ~XFS_FILEID_TYPE_64FLAG;
59#endif
60
61 /*
62 * Note that we only accept fileids which are long enough
63 * rather than allow the parent generation number to default
64 * to zero. XFS considers zero a valid generation number not
65 * an invalid/wildcard value. There's little point printk'ing
66 * a warning here as we don't have the client information
67 * which would make such a warning useful.
68 */
69 if (fileid_type > 2 ||
70 fh_len < xfs_fileid_length((fileid_type == 2), is64))
71 return NULL;
72
73 p = xfs_fileid_decode_fid2(p, &ifid, is64);
74
75 if (fileid_type == 2) {
76 p = xfs_fileid_decode_fid2(p, &pfid, is64);
77 parent = &pfid;
78 }
Nathan Scott7b718762005-11-02 14:58:39 +110079
Linus Torvalds1da177e2005-04-16 15:20:36 -070080 fh = (__u32 *)&ifid;
81 return find_exported_dentry(sb, fh, parent, acceptable, context);
82}
83
84
85STATIC int
86linvfs_encode_fh(
87 struct dentry *dentry,
88 __u32 *fh,
89 int *max_len,
90 int connectable)
91{
92 struct inode *inode = dentry->d_inode;
93 int type = 1;
94 __u32 *p = fh;
95 int len;
96 int is64 = 0;
97#if XFS_BIG_INUMS
98 vfs_t *vfs = LINVFS_GET_VFS(inode->i_sb);
Nathan Scott7b718762005-11-02 14:58:39 +110099
Nathan Scottc11e2c32005-11-02 15:11:45 +1100100 if (!(vfs->vfs_flag & VFS_32BITINODES)) {
Linus Torvalds1da177e2005-04-16 15:20:36 -0700101 /* filesystem may contain 64bit inode numbers */
102 is64 = XFS_FILEID_TYPE_64FLAG;
103 }
104#endif
105
106 /* Directories don't need their parent encoded, they have ".." */
107 if (S_ISDIR(inode->i_mode))
108 connectable = 0;
109
110 /*
111 * Only encode if there is enough space given. In practice
112 * this means we can't export a filesystem with 64bit inodes
113 * over NFSv2 with the subtree_check export option; the other
114 * seven combinations work. The real answer is "don't use v2".
115 */
116 len = xfs_fileid_length(connectable, is64);
117 if (*max_len < len)
118 return 255;
119 *max_len = len;
120
121 p = xfs_fileid_encode_inode(p, inode, is64);
122 if (connectable) {
123 spin_lock(&dentry->d_lock);
124 p = xfs_fileid_encode_inode(p, dentry->d_parent->d_inode, is64);
125 spin_unlock(&dentry->d_lock);
126 type = 2;
127 }
128 BUG_ON((p - fh) != len);
129 return type | is64;
130}
131
132STATIC struct dentry *
133linvfs_get_dentry(
134 struct super_block *sb,
135 void *data)
136{
137 vnode_t *vp;
138 struct inode *inode;
139 struct dentry *result;
140 vfs_t *vfsp = LINVFS_GET_VFS(sb);
141 int error;
142
143 VFS_VGET(vfsp, &vp, (fid_t *)data, error);
144 if (error || vp == NULL)
145 return ERR_PTR(-ESTALE) ;
146
147 inode = LINVFS_GET_IP(vp);
148 result = d_alloc_anon(inode);
149 if (!result) {
150 iput(inode);
151 return ERR_PTR(-ENOMEM);
152 }
153 return result;
154}
155
156STATIC struct dentry *
157linvfs_get_parent(
158 struct dentry *child)
159{
160 int error;
161 vnode_t *vp, *cvp;
162 struct dentry *parent;
163 struct dentry dotdot;
164
165 dotdot.d_name.name = "..";
166 dotdot.d_name.len = 2;
167 dotdot.d_inode = NULL;
168
169 cvp = NULL;
170 vp = LINVFS_GET_VP(child->d_inode);
171 VOP_LOOKUP(vp, &dotdot, &cvp, 0, NULL, NULL, error);
172 if (unlikely(error))
173 return ERR_PTR(-error);
174
175 parent = d_alloc_anon(LINVFS_GET_IP(cvp));
176 if (unlikely(!parent)) {
177 VN_RELE(cvp);
178 return ERR_PTR(-ENOMEM);
179 }
180 return parent;
181}
182
183struct export_operations linvfs_export_ops = {
184 .decode_fh = linvfs_decode_fh,
185 .encode_fh = linvfs_encode_fh,
186 .get_parent = linvfs_get_parent,
187 .get_dentry = linvfs_get_dentry,
188};