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