]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - fs/9p/fid.c
9p: if v9fs_fid_lookup() gets to asking server, it'd better have hashed dentry
[mirror_ubuntu-bionic-kernel.git] / fs / 9p / fid.c
1 /*
2 * V9FS FID Management
3 *
4 * Copyright (C) 2007 by Latchesar Ionkov <lucho@ionkov.net>
5 * Copyright (C) 2005, 2006 by Eric Van Hensbergen <ericvh@gmail.com>
6 *
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License version 2
9 * as published by the Free Software Foundation.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to:
18 * Free Software Foundation
19 * 51 Franklin Street, Fifth Floor
20 * Boston, MA 02111-1301 USA
21 *
22 */
23
24 #include <linux/module.h>
25 #include <linux/errno.h>
26 #include <linux/fs.h>
27 #include <linux/slab.h>
28 #include <linux/sched.h>
29 #include <linux/idr.h>
30 #include <net/9p/9p.h>
31 #include <net/9p/client.h>
32
33 #include "v9fs.h"
34 #include "v9fs_vfs.h"
35 #include "fid.h"
36
37 /**
38 * v9fs_fid_add - add a fid to a dentry
39 * @dentry: dentry that the fid is being added to
40 * @fid: fid to add
41 *
42 */
43
44 static inline void __add_fid(struct dentry *dentry, struct p9_fid *fid)
45 {
46 hlist_add_head(&fid->dlist, (struct hlist_head *)&dentry->d_fsdata);
47 }
48
49 void v9fs_fid_add(struct dentry *dentry, struct p9_fid *fid)
50 {
51 spin_lock(&dentry->d_lock);
52 __add_fid(dentry, fid);
53 spin_unlock(&dentry->d_lock);
54 }
55
56 /**
57 * v9fs_fid_find - retrieve a fid that belongs to the specified uid
58 * @dentry: dentry to look for fid in
59 * @uid: return fid that belongs to the specified user
60 * @any: if non-zero, return any fid associated with the dentry
61 *
62 */
63
64 static struct p9_fid *v9fs_fid_find(struct dentry *dentry, kuid_t uid, int any)
65 {
66 struct p9_fid *fid, *ret;
67
68 p9_debug(P9_DEBUG_VFS, " dentry: %s (%p) uid %d any %d\n",
69 dentry->d_name.name, dentry, from_kuid(&init_user_ns, uid),
70 any);
71 ret = NULL;
72 /* we'll recheck under lock if there's anything to look in */
73 if (dentry->d_fsdata) {
74 struct hlist_head *h = (struct hlist_head *)&dentry->d_fsdata;
75 struct hlist_node *n;
76 spin_lock(&dentry->d_lock);
77 hlist_for_each_entry(fid, n, h, dlist) {
78 if (any || uid_eq(fid->uid, uid)) {
79 ret = fid;
80 break;
81 }
82 }
83 spin_unlock(&dentry->d_lock);
84 }
85
86 return ret;
87 }
88
89 /*
90 * We need to hold v9ses->rename_sem as long as we hold references
91 * to returned path array. Array element contain pointers to
92 * dentry names.
93 */
94 static int build_path_from_dentry(struct v9fs_session_info *v9ses,
95 struct dentry *dentry, char ***names)
96 {
97 int n = 0, i;
98 char **wnames;
99 struct dentry *ds;
100
101 for (ds = dentry; !IS_ROOT(ds); ds = ds->d_parent)
102 n++;
103
104 wnames = kmalloc(sizeof(char *) * n, GFP_KERNEL);
105 if (!wnames)
106 goto err_out;
107
108 for (ds = dentry, i = (n-1); i >= 0; i--, ds = ds->d_parent)
109 wnames[i] = (char *)ds->d_name.name;
110
111 *names = wnames;
112 return n;
113 err_out:
114 return -ENOMEM;
115 }
116
117 static struct p9_fid *v9fs_fid_lookup_with_uid(struct dentry *dentry,
118 kuid_t uid, int any)
119 {
120 struct dentry *ds;
121 char **wnames, *uname;
122 int i, n, l, clone, access;
123 struct v9fs_session_info *v9ses;
124 struct p9_fid *fid, *old_fid = NULL;
125
126 v9ses = v9fs_dentry2v9ses(dentry);
127 access = v9ses->flags & V9FS_ACCESS_MASK;
128 fid = v9fs_fid_find(dentry, uid, any);
129 if (fid)
130 return fid;
131 /*
132 * we don't have a matching fid. To do a TWALK we need
133 * parent fid. We need to prevent rename when we want to
134 * look at the parent.
135 */
136 down_read(&v9ses->rename_sem);
137 ds = dentry->d_parent;
138 fid = v9fs_fid_find(ds, uid, any);
139 if (fid) {
140 /* Found the parent fid do a lookup with that */
141 fid = p9_client_walk(fid, 1, (char **)&dentry->d_name.name, 1);
142 goto fid_out;
143 }
144 up_read(&v9ses->rename_sem);
145
146 /* start from the root and try to do a lookup */
147 fid = v9fs_fid_find(dentry->d_sb->s_root, uid, any);
148 if (!fid) {
149 /* the user is not attached to the fs yet */
150 if (access == V9FS_ACCESS_SINGLE)
151 return ERR_PTR(-EPERM);
152
153 if (v9fs_proto_dotu(v9ses) || v9fs_proto_dotl(v9ses))
154 uname = NULL;
155 else
156 uname = v9ses->uname;
157
158 fid = p9_client_attach(v9ses->clnt, NULL, uname, uid,
159 v9ses->aname);
160 if (IS_ERR(fid))
161 return fid;
162
163 v9fs_fid_add(dentry->d_sb->s_root, fid);
164 }
165 /* If we are root ourself just return that */
166 if (dentry->d_sb->s_root == dentry)
167 return fid;
168 /*
169 * Do a multipath walk with attached root.
170 * When walking parent we need to make sure we
171 * don't have a parallel rename happening
172 */
173 down_read(&v9ses->rename_sem);
174 n = build_path_from_dentry(v9ses, dentry, &wnames);
175 if (n < 0) {
176 fid = ERR_PTR(n);
177 goto err_out;
178 }
179 clone = 1;
180 i = 0;
181 while (i < n) {
182 l = min(n - i, P9_MAXWELEM);
183 /*
184 * We need to hold rename lock when doing a multipath
185 * walk to ensure none of the patch component change
186 */
187 fid = p9_client_walk(fid, l, &wnames[i], clone);
188 if (IS_ERR(fid)) {
189 if (old_fid) {
190 /*
191 * If we fail, clunk fid which are mapping
192 * to path component and not the last component
193 * of the path.
194 */
195 p9_client_clunk(old_fid);
196 }
197 kfree(wnames);
198 goto err_out;
199 }
200 old_fid = fid;
201 i += l;
202 clone = 0;
203 }
204 kfree(wnames);
205 fid_out:
206 if (!IS_ERR(fid)) {
207 spin_lock(&dentry->d_lock);
208 if (d_unhashed(dentry)) {
209 spin_unlock(&dentry->d_lock);
210 p9_client_clunk(fid);
211 fid = ERR_PTR(-ENOENT);
212 } else {
213 __add_fid(dentry, fid);
214 spin_unlock(&dentry->d_lock);
215 }
216 }
217 err_out:
218 up_read(&v9ses->rename_sem);
219 return fid;
220 }
221
222 /**
223 * v9fs_fid_lookup - lookup for a fid, try to walk if not found
224 * @dentry: dentry to look for fid in
225 *
226 * Look for a fid in the specified dentry for the current user.
227 * If no fid is found, try to create one walking from a fid from the parent
228 * dentry (if it has one), or the root dentry. If the user haven't accessed
229 * the fs yet, attach now and walk from the root.
230 */
231
232 struct p9_fid *v9fs_fid_lookup(struct dentry *dentry)
233 {
234 kuid_t uid;
235 int any, access;
236 struct v9fs_session_info *v9ses;
237
238 v9ses = v9fs_dentry2v9ses(dentry);
239 access = v9ses->flags & V9FS_ACCESS_MASK;
240 switch (access) {
241 case V9FS_ACCESS_SINGLE:
242 case V9FS_ACCESS_USER:
243 case V9FS_ACCESS_CLIENT:
244 uid = current_fsuid();
245 any = 0;
246 break;
247
248 case V9FS_ACCESS_ANY:
249 uid = v9ses->uid;
250 any = 1;
251 break;
252
253 default:
254 uid = INVALID_UID;
255 any = 0;
256 break;
257 }
258 return v9fs_fid_lookup_with_uid(dentry, uid, any);
259 }
260
261 struct p9_fid *v9fs_fid_clone(struct dentry *dentry)
262 {
263 struct p9_fid *fid, *ret;
264
265 fid = v9fs_fid_lookup(dentry);
266 if (IS_ERR(fid))
267 return fid;
268
269 ret = p9_client_walk(fid, 0, NULL, 1);
270 return ret;
271 }
272
273 static struct p9_fid *v9fs_fid_clone_with_uid(struct dentry *dentry, kuid_t uid)
274 {
275 struct p9_fid *fid, *ret;
276
277 fid = v9fs_fid_lookup_with_uid(dentry, uid, 0);
278 if (IS_ERR(fid))
279 return fid;
280
281 ret = p9_client_walk(fid, 0, NULL, 1);
282 return ret;
283 }
284
285 struct p9_fid *v9fs_writeback_fid(struct dentry *dentry)
286 {
287 int err;
288 struct p9_fid *fid;
289
290 fid = v9fs_fid_clone_with_uid(dentry, GLOBAL_ROOT_UID);
291 if (IS_ERR(fid))
292 goto error_out;
293 /*
294 * writeback fid will only be used to write back the
295 * dirty pages. We always request for the open fid in read-write
296 * mode so that a partial page write which result in page
297 * read can work.
298 */
299 err = p9_client_open(fid, O_RDWR);
300 if (err < 0) {
301 p9_client_clunk(fid);
302 fid = ERR_PTR(err);
303 goto error_out;
304 }
305 error_out:
306 return fid;
307 }