Commit | Line | Data |
---|---|---|
1da177e4 | 1 | /* |
7b718769 NS |
2 | * Copyright (c) 2004-2005 Silicon Graphics, Inc. |
3 | * All Rights Reserved. | |
1da177e4 | 4 | * |
7b718769 NS |
5 | * This program is free software; you can redistribute it and/or |
6 | * modify it under the terms of the GNU General Public License as | |
1da177e4 LT |
7 | * published by the Free Software Foundation. |
8 | * | |
7b718769 NS |
9 | * 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. | |
1da177e4 | 13 | * |
7b718769 NS |
14 | * 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 | |
1da177e4 | 17 | */ |
1da177e4 LT |
18 | #include "xfs.h" |
19 | #include "xfs_types.h" | |
da353b0d | 20 | #include "xfs_inum.h" |
1da177e4 LT |
21 | #include "xfs_log.h" |
22 | #include "xfs_trans.h" | |
23 | #include "xfs_sb.h" | |
da353b0d DC |
24 | #include "xfs_ag.h" |
25 | #include "xfs_dmapi.h" | |
1da177e4 LT |
26 | #include "xfs_mount.h" |
27 | #include "xfs_export.h" | |
739bfb2a CH |
28 | #include "xfs_vnodeops.h" |
29 | #include "xfs_bmap_btree.h" | |
30 | #include "xfs_inode.h" | |
745f6919 | 31 | #include "xfs_vfsops.h" |
1da177e4 | 32 | |
7989cb8e | 33 | static struct dentry dotdot = { .d_name.name = "..", .d_name.len = 2, }; |
9b94c2ed | 34 | |
1da177e4 | 35 | /* |
7b718769 | 36 | * XFS encodes and decodes the fileid portion of NFS filehandles |
1da177e4 LT |
37 | * 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 | ||
1da177e4 | 46 | STATIC struct dentry * |
a50cd269 | 47 | xfs_fs_decode_fh( |
1da177e4 LT |
48 | 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 | } | |
7b718769 | 86 | |
1da177e4 | 87 | fh = (__u32 *)&ifid; |
0c9512d7 | 88 | return sb->s_export_op->find_exported_dentry(sb, fh, parent, acceptable, context); |
1da177e4 LT |
89 | } |
90 | ||
91 | ||
92 | STATIC int | |
a50cd269 | 93 | xfs_fs_encode_fh( |
1da177e4 LT |
94 | 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 | |
bd186aa9 | 105 | if (!(XFS_M(inode->i_sb)->m_flags & XFS_MOUNT_SMALL_INUMS)) { |
1da177e4 LT |
106 | /* filesystem may contain 64bit inode numbers */ |
107 | is64 = XFS_FILEID_TYPE_64FLAG; | |
108 | } | |
109 | #endif | |
110 | ||
111 | /* Directories don't need their parent encoded, they have ".." */ | |
112 | if (S_ISDIR(inode->i_mode)) | |
113 | connectable = 0; | |
114 | ||
115 | /* | |
116 | * Only encode if there is enough space given. In practice | |
117 | * this means we can't export a filesystem with 64bit inodes | |
118 | * over NFSv2 with the subtree_check export option; the other | |
119 | * seven combinations work. The real answer is "don't use v2". | |
120 | */ | |
121 | len = xfs_fileid_length(connectable, is64); | |
122 | if (*max_len < len) | |
123 | return 255; | |
124 | *max_len = len; | |
125 | ||
126 | p = xfs_fileid_encode_inode(p, inode, is64); | |
127 | if (connectable) { | |
128 | spin_lock(&dentry->d_lock); | |
129 | p = xfs_fileid_encode_inode(p, dentry->d_parent->d_inode, is64); | |
130 | spin_unlock(&dentry->d_lock); | |
131 | type = 2; | |
132 | } | |
133 | BUG_ON((p - fh) != len); | |
134 | return type | is64; | |
135 | } | |
136 | ||
137 | STATIC struct dentry * | |
a50cd269 | 138 | xfs_fs_get_dentry( |
1da177e4 LT |
139 | struct super_block *sb, |
140 | void *data) | |
141 | { | |
67fcaa73 | 142 | bhv_vnode_t *vp; |
1da177e4 LT |
143 | struct inode *inode; |
144 | struct dentry *result; | |
1da177e4 LT |
145 | int error; |
146 | ||
745f6919 | 147 | error = xfs_vget(XFS_M(sb), &vp, (fid_t *)data); |
1da177e4 LT |
148 | if (error || vp == NULL) |
149 | return ERR_PTR(-ESTALE) ; | |
150 | ||
ec86dc02 | 151 | inode = vn_to_inode(vp); |
1da177e4 LT |
152 | result = d_alloc_anon(inode); |
153 | if (!result) { | |
154 | iput(inode); | |
155 | return ERR_PTR(-ENOMEM); | |
156 | } | |
157 | return result; | |
158 | } | |
159 | ||
160 | STATIC struct dentry * | |
a50cd269 | 161 | xfs_fs_get_parent( |
1da177e4 LT |
162 | struct dentry *child) |
163 | { | |
164 | int error; | |
739bfb2a | 165 | bhv_vnode_t *cvp; |
1da177e4 | 166 | struct dentry *parent; |
1da177e4 LT |
167 | |
168 | cvp = NULL; | |
739bfb2a | 169 | error = xfs_lookup(XFS_I(child->d_inode), &dotdot, &cvp); |
1da177e4 LT |
170 | if (unlikely(error)) |
171 | return ERR_PTR(-error); | |
172 | ||
ec86dc02 | 173 | parent = d_alloc_anon(vn_to_inode(cvp)); |
1da177e4 LT |
174 | if (unlikely(!parent)) { |
175 | VN_RELE(cvp); | |
176 | return ERR_PTR(-ENOMEM); | |
177 | } | |
178 | return parent; | |
179 | } | |
180 | ||
a50cd269 NS |
181 | struct 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, | |
1da177e4 | 186 | }; |