blob: 616abaf1c6cd2a62dc7389a86baec8ec55940070 [file] [log] [blame]
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -07001/*
2 * V9FS FID Management
3 *
Latchesar Ionkovba176742007-10-17 14:31:07 -05004 * Copyright (C) 2007 by Latchesar Ionkov <lucho@ionkov.net>
Eric Van Hensbergen46f6dac2006-03-02 02:54:33 -08005 * Copyright (C) 2005, 2006 by Eric Van Hensbergen <ericvh@gmail.com>
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -07006 *
7 * This program is free software; you can redistribute it and/or modify
Eric Van Hensbergen42e8c502006-03-25 03:07:28 -08008 * it under the terms of the GNU General Public License version 2
9 * as published by the Free Software Foundation.
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070010 *
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
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070024#include <linux/module.h>
25#include <linux/errno.h>
26#include <linux/fs.h>
Tejun Heo5a0e3ad2010-03-24 17:04:11 +090027#include <linux/slab.h>
Al Viro914e2632006-10-18 13:55:46 -040028#include <linux/sched.h>
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070029#include <linux/idr.h>
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -050030#include <net/9p/9p.h>
31#include <net/9p/client.h>
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070032
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070033#include "v9fs.h"
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070034#include "v9fs_vfs.h"
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070035#include "fid.h"
36
37/**
Latchesar Ionkovba176742007-10-17 14:31:07 -050038 * v9fs_fid_add - add a fid to a dentry
39 * @dentry: dentry that the fid is being added to
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070040 * @fid: fid to add
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070041 *
42 */
43
Al Viro5e608672013-02-28 01:50:20 -050044static 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
Al Viro2ea03e1d2013-02-28 01:18:14 -050049void v9fs_fid_add(struct dentry *dentry, struct p9_fid *fid)
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070050{
Al Viro634095d2013-02-27 22:37:21 -050051 spin_lock(&dentry->d_lock);
Al Viro5e608672013-02-28 01:50:20 -050052 __add_fid(dentry, fid);
Al Viro634095d2013-02-27 22:37:21 -050053 spin_unlock(&dentry->d_lock);
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -070054}
55
56/**
Latchesar Ionkovba176742007-10-17 14:31:07 -050057 * 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
Eric W. Biedermanb4642552013-01-30 11:48:53 -080064static struct p9_fid *v9fs_fid_find(struct dentry *dentry, kuid_t uid, int any)
Latchesar Ionkovba176742007-10-17 14:31:07 -050065{
Latchesar Ionkovba176742007-10-17 14:31:07 -050066 struct p9_fid *fid, *ret;
67
Joe Perches5d385152011-11-28 10:40:46 -080068 p9_debug(P9_DEBUG_VFS, " dentry: %s (%p) uid %d any %d\n",
Eric W. Biedermanb4642552013-01-30 11:48:53 -080069 dentry->d_name.name, dentry, from_kuid(&init_user_ns, uid),
70 any);
Latchesar Ionkovba176742007-10-17 14:31:07 -050071 ret = NULL;
Al Viroaaeb7ec2013-02-28 01:13:19 -050072 /* 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;
Al Viroc4d30962013-02-27 22:51:08 -050075 struct hlist_node *n;
Al Viro634095d2013-02-27 22:37:21 -050076 spin_lock(&dentry->d_lock);
Al Viroaaeb7ec2013-02-28 01:13:19 -050077 hlist_for_each_entry(fid, n, h, dlist) {
Eric W. Biedermanb4642552013-01-30 11:48:53 -080078 if (any || uid_eq(fid->uid, uid)) {
Latchesar Ionkovba176742007-10-17 14:31:07 -050079 ret = fid;
80 break;
81 }
82 }
Al Viro634095d2013-02-27 22:37:21 -050083 spin_unlock(&dentry->d_lock);
Latchesar Ionkovba176742007-10-17 14:31:07 -050084 }
85
86 return ret;
87}
88
Aneesh Kumar K.Va534c8d2010-06-30 19:18:50 +053089/*
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 */
94static 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;
113err_out:
114 return -ENOMEM;
115}
116
Aneesh Kumar K.V7c9e5922011-02-28 17:04:11 +0530117static struct p9_fid *v9fs_fid_lookup_with_uid(struct dentry *dentry,
Eric W. Biedermanb4642552013-01-30 11:48:53 -0800118 kuid_t uid, int any)
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -0700119{
Aneesh Kumar K.Va534c8d2010-06-30 19:18:50 +0530120 struct dentry *ds;
Latchesar Ionkovba176742007-10-17 14:31:07 -0500121 char **wnames, *uname;
Aneesh Kumar K.V7c9e5922011-02-28 17:04:11 +0530122 int i, n, l, clone, access;
123 struct v9fs_session_info *v9ses;
124 struct p9_fid *fid, *old_fid = NULL;
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -0700125
Aneesh Kumar K.V42869c82011-03-08 16:39:50 +0530126 v9ses = v9fs_dentry2v9ses(dentry);
Latchesar Ionkovba176742007-10-17 14:31:07 -0500127 access = v9ses->flags & V9FS_ACCESS_MASK;
Latchesar Ionkovba176742007-10-17 14:31:07 -0500128 fid = v9fs_fid_find(dentry, uid, any);
129 if (fid)
130 return fid;
Aneesh Kumar K.Va534c8d2010-06-30 19:18:50 +0530131 /*
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);
Latchesar Ionkovba176742007-10-17 14:31:07 -0500137 ds = dentry->d_parent;
138 fid = v9fs_fid_find(ds, uid, any);
Aneesh Kumar K.Va534c8d2010-06-30 19:18:50 +0530139 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);
Latchesar Ionkovba176742007-10-17 14:31:07 -0500145
Aneesh Kumar K.Va534c8d2010-06-30 19:18:50 +0530146 /* 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);
Latchesar Ionkovba176742007-10-17 14:31:07 -0500152
Aneesh Kumar K.Va534c8d2010-06-30 19:18:50 +0530153 if (v9fs_proto_dotu(v9ses) || v9fs_proto_dotl(v9ses))
Latchesar Ionkovba176742007-10-17 14:31:07 -0500154 uname = NULL;
Aneesh Kumar K.Va534c8d2010-06-30 19:18:50 +0530155 else
156 uname = v9ses->uname;
Latchesar Ionkovba176742007-10-17 14:31:07 -0500157
Aneesh Kumar K.Va534c8d2010-06-30 19:18:50 +0530158 fid = p9_client_attach(v9ses->clnt, NULL, uname, uid,
159 v9ses->aname);
160 if (IS_ERR(fid))
161 return fid;
Latchesar Ionkovba176742007-10-17 14:31:07 -0500162
Aneesh Kumar K.Va534c8d2010-06-30 19:18:50 +0530163 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)
Latchesar Ionkovba176742007-10-17 14:31:07 -0500167 return fid;
Aneesh Kumar K.Va534c8d2010-06-30 19:18:50 +0530168 /*
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 }
Latchesar Ionkovba176742007-10-17 14:31:07 -0500179 clone = 1;
180 i = 0;
181 while (i < n) {
182 l = min(n - i, P9_MAXWELEM);
Aneesh Kumar K.Va534c8d2010-06-30 19:18:50 +0530183 /*
184 * We need to hold rename lock when doing a multipath
185 * walk to ensure none of the patch component change
186 */
Latchesar Ionkovba176742007-10-17 14:31:07 -0500187 fid = p9_client_walk(fid, l, &wnames[i], clone);
Eric Van Hensbergenc55703d2008-02-06 19:25:08 -0600188 if (IS_ERR(fid)) {
Aneesh Kumar K.V5b0fa202010-03-19 12:47:26 +0000189 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 }
Latchesar Ionkovba176742007-10-17 14:31:07 -0500197 kfree(wnames);
Aneesh Kumar K.Va534c8d2010-06-30 19:18:50 +0530198 goto err_out;
Latchesar Ionkovba176742007-10-17 14:31:07 -0500199 }
Aneesh Kumar K.V5b0fa202010-03-19 12:47:26 +0000200 old_fid = fid;
Latchesar Ionkovba176742007-10-17 14:31:07 -0500201 i += l;
202 clone = 0;
203 }
Latchesar Ionkovba176742007-10-17 14:31:07 -0500204 kfree(wnames);
Aneesh Kumar K.Va534c8d2010-06-30 19:18:50 +0530205fid_out:
Al Viro5e608672013-02-28 01:50:20 -0500206 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 }
Aneesh Kumar K.Va534c8d2010-06-30 19:18:50 +0530217err_out:
218 up_read(&v9ses->rename_sem);
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500219 return fid;
220}
Eric Van Hensbergen3ed84912005-09-09 13:04:24 -0700221
Aneesh Kumar K.V7c9e5922011-02-28 17:04:11 +0530222/**
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
232struct p9_fid *v9fs_fid_lookup(struct dentry *dentry)
233{
Eric W. Biedermanb4642552013-01-30 11:48:53 -0800234 kuid_t uid;
Aneesh Kumar K.V7c9e5922011-02-28 17:04:11 +0530235 int any, access;
236 struct v9fs_session_info *v9ses;
237
Aneesh Kumar K.V42869c82011-03-08 16:39:50 +0530238 v9ses = v9fs_dentry2v9ses(dentry);
Aneesh Kumar K.V7c9e5922011-02-28 17:04:11 +0530239 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:
Eric W. Biedermanb4642552013-01-30 11:48:53 -0800254 uid = INVALID_UID;
Aneesh Kumar K.V7c9e5922011-02-28 17:04:11 +0530255 any = 0;
256 break;
257 }
258 return v9fs_fid_lookup_with_uid(dentry, uid, any);
259}
260
Latchesar Ionkovbd238fb2007-07-10 17:57:28 -0500261struct p9_fid *v9fs_fid_clone(struct dentry *dentry)
Eric Van Hensbergenda977b22007-01-26 00:57:06 -0800262{
Latchesar Ionkovba176742007-10-17 14:31:07 -0500263 struct p9_fid *fid, *ret;
Eric Van Hensbergenda977b22007-01-26 00:57:06 -0800264
Latchesar Ionkovba176742007-10-17 14:31:07 -0500265 fid = v9fs_fid_lookup(dentry);
266 if (IS_ERR(fid))
267 return fid;
Eric Van Hensbergenda977b22007-01-26 00:57:06 -0800268
Latchesar Ionkovba176742007-10-17 14:31:07 -0500269 ret = p9_client_walk(fid, 0, NULL, 1);
270 return ret;
Eric Van Hensbergenda977b22007-01-26 00:57:06 -0800271}
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530272
Eric W. Biedermanb4642552013-01-30 11:48:53 -0800273static struct p9_fid *v9fs_fid_clone_with_uid(struct dentry *dentry, kuid_t uid)
Aneesh Kumar K.V7c9e5922011-02-28 17:04:11 +0530274{
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}
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530284
285struct p9_fid *v9fs_writeback_fid(struct dentry *dentry)
286{
Aneesh Kumar K.Vdf5d8c82011-03-24 20:38:35 +0530287 int err;
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530288 struct p9_fid *fid;
289
Eric W. Biedermanb4642552013-01-30 11:48:53 -0800290 fid = v9fs_fid_clone_with_uid(dentry, GLOBAL_ROOT_UID);
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530291 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.
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530298 */
Aneesh Kumar K.Vdf5d8c82011-03-24 20:38:35 +0530299 err = p9_client_open(fid, O_RDWR);
Aneesh Kumar K.V3cf387d2011-02-28 17:03:57 +0530300 if (err < 0) {
301 p9_client_clunk(fid);
302 fid = ERR_PTR(err);
303 goto error_out;
304 }
305error_out:
306 return fid;
307}