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