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