blob: 2b112d978e9f53676e1ab684dc9ab5d9fed9f269 [file] [log] [blame]
Miklos Szeredie5e55582005-09-09 13:10:28 -07001/*
2 FUSE: Filesystem in Userspace
Miklos Szeredi1729a162008-11-26 12:03:54 +01003 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
Miklos Szeredie5e55582005-09-09 13:10:28 -07004
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7*/
8
9#include "fuse_i.h"
10
11#include <linux/pagemap.h>
12#include <linux/file.h>
Miklos Szeredie5e55582005-09-09 13:10:28 -070013#include <linux/sched.h>
14#include <linux/namei.h>
Miklos Szeredi07e77dc2010-12-07 20:16:56 +010015#include <linux/slab.h>
Miklos Szeredie5e55582005-09-09 13:10:28 -070016
Feng Shuo4582a4a2013-01-15 11:23:28 +080017static bool fuse_use_readdirplus(struct inode *dir, struct file *filp)
18{
19 struct fuse_conn *fc = get_fuse_conn(dir);
20 struct fuse_inode *fi = get_fuse_inode(dir);
21
22 if (!fc->do_readdirplus)
23 return false;
24 if (test_and_clear_bit(FUSE_I_ADVISE_RDPLUS, &fi->state))
25 return true;
26 if (filp->f_pos == 0)
27 return true;
28 return false;
29}
30
31static void fuse_advise_use_readdirplus(struct inode *dir)
32{
33 struct fuse_inode *fi = get_fuse_inode(dir);
34
35 set_bit(FUSE_I_ADVISE_RDPLUS, &fi->state);
36}
37
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070038#if BITS_PER_LONG >= 64
39static inline void fuse_dentry_settime(struct dentry *entry, u64 time)
40{
41 entry->d_time = time;
42}
43
44static inline u64 fuse_dentry_time(struct dentry *entry)
45{
46 return entry->d_time;
47}
48#else
49/*
50 * On 32 bit archs store the high 32 bits of time in d_fsdata
51 */
52static void fuse_dentry_settime(struct dentry *entry, u64 time)
53{
54 entry->d_time = time;
55 entry->d_fsdata = (void *) (unsigned long) (time >> 32);
56}
57
58static u64 fuse_dentry_time(struct dentry *entry)
59{
60 return (u64) entry->d_time +
61 ((u64) (unsigned long) entry->d_fsdata << 32);
62}
63#endif
64
Miklos Szeredi6f9f1182006-01-06 00:19:39 -080065/*
66 * FUSE caches dentries and attributes with separate timeout. The
67 * time in jiffies until the dentry/attributes are valid is stored in
68 * dentry->d_time and fuse_inode->i_time respectively.
69 */
70
71/*
72 * Calculate the time in jiffies until a dentry/attributes are valid
73 */
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070074static u64 time_to_jiffies(unsigned long sec, unsigned long nsec)
Miklos Szeredie5e55582005-09-09 13:10:28 -070075{
Miklos Szeredi685d16d2006-07-30 03:04:08 -070076 if (sec || nsec) {
77 struct timespec ts = {sec, nsec};
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070078 return get_jiffies_64() + timespec_to_jiffies(&ts);
Miklos Szeredi685d16d2006-07-30 03:04:08 -070079 } else
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070080 return 0;
Miklos Szeredie5e55582005-09-09 13:10:28 -070081}
82
Miklos Szeredi6f9f1182006-01-06 00:19:39 -080083/*
84 * Set dentry and possibly attribute timeouts from the lookup/mk*
85 * replies
86 */
Miklos Szeredi1fb69e72007-10-18 03:06:58 -070087static void fuse_change_entry_timeout(struct dentry *entry,
88 struct fuse_entry_out *o)
Miklos Szeredi0aa7c692006-01-06 00:19:34 -080089{
Miklos Szeredi0a0898c2006-07-30 03:04:10 -070090 fuse_dentry_settime(entry,
91 time_to_jiffies(o->entry_valid, o->entry_valid_nsec));
Miklos Szeredi1fb69e72007-10-18 03:06:58 -070092}
93
94static u64 attr_timeout(struct fuse_attr_out *o)
95{
96 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
97}
98
99static u64 entry_attr_timeout(struct fuse_entry_out *o)
100{
101 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800102}
103
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800104/*
105 * Mark the attributes as stale, so that at the next call to
106 * ->getattr() they will be fetched from userspace
107 */
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800108void fuse_invalidate_attr(struct inode *inode)
109{
Miklos Szeredi0a0898c2006-07-30 03:04:10 -0700110 get_fuse_inode(inode)->i_time = 0;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800111}
112
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800113/*
114 * Just mark the entry as stale, so that a next attempt to look it up
115 * will result in a new lookup call to userspace
116 *
117 * This is called when a dentry is about to become negative and the
118 * timeout is unknown (unlink, rmdir, rename and in some cases
119 * lookup)
120 */
Miklos Szeredidbd561d2008-07-25 01:49:00 -0700121void fuse_invalidate_entry_cache(struct dentry *entry)
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800122{
Miklos Szeredi0a0898c2006-07-30 03:04:10 -0700123 fuse_dentry_settime(entry, 0);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800124}
125
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800126/*
127 * Same as fuse_invalidate_entry_cache(), but also try to remove the
128 * dentry from the hash
129 */
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800130static void fuse_invalidate_entry(struct dentry *entry)
131{
132 d_invalidate(entry);
133 fuse_invalidate_entry_cache(entry);
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800134}
135
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700136static void fuse_lookup_init(struct fuse_conn *fc, struct fuse_req *req,
137 u64 nodeid, struct qstr *name,
Miklos Szeredie5e55582005-09-09 13:10:28 -0700138 struct fuse_entry_out *outarg)
139{
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700140 memset(outarg, 0, sizeof(struct fuse_entry_out));
Miklos Szeredie5e55582005-09-09 13:10:28 -0700141 req->in.h.opcode = FUSE_LOOKUP;
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700142 req->in.h.nodeid = nodeid;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700143 req->in.numargs = 1;
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700144 req->in.args[0].size = name->len + 1;
145 req->in.args[0].value = name->name;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700146 req->out.numargs = 1;
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700147 if (fc->minor < 9)
148 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
149 else
150 req->out.args[0].size = sizeof(struct fuse_entry_out);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700151 req->out.args[0].value = outarg;
152}
153
Miklos Szeredi5c5c5e52008-04-30 00:54:43 -0700154u64 fuse_get_attr_version(struct fuse_conn *fc)
Miklos Szeredi7dca9fd2007-11-28 16:21:59 -0800155{
156 u64 curr_version;
157
158 /*
159 * The spin lock isn't actually needed on 64bit archs, but we
160 * don't yet care too much about such optimizations.
161 */
162 spin_lock(&fc->lock);
163 curr_version = fc->attr_version;
164 spin_unlock(&fc->lock);
165
166 return curr_version;
167}
168
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800169/*
170 * Check whether the dentry is still valid
171 *
172 * If the entry validity timeout has expired and the dentry is
173 * positive, try to redo the lookup. If the lookup results in a
174 * different inode, then let the VFS invalidate the dentry and redo
175 * the lookup once more. If the lookup results in the same inode,
176 * then refresh the attributes, timeouts and mark the dentry valid.
177 */
Al Viro0b728e12012-06-10 16:03:43 -0400178static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags)
Miklos Szeredie5e55582005-09-09 13:10:28 -0700179{
Nick Piggin34286d62011-01-07 17:49:57 +1100180 struct inode *inode;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800181
Miklos Szeredie7c0a162011-03-21 13:58:06 +0100182 inode = ACCESS_ONCE(entry->d_inode);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800183 if (inode && is_bad_inode(inode))
Miklos Szeredie5e55582005-09-09 13:10:28 -0700184 return 0;
Miklos Szeredi0a0898c2006-07-30 03:04:10 -0700185 else if (fuse_dentry_time(entry) < get_jiffies_64()) {
Miklos Szeredie5e55582005-09-09 13:10:28 -0700186 int err;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700187 struct fuse_entry_out outarg;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800188 struct fuse_conn *fc;
189 struct fuse_req *req;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100190 struct fuse_forget_link *forget;
Miklos Szeredie956edd2006-10-17 00:10:12 -0700191 struct dentry *parent;
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700192 u64 attr_version;
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800193
Miklos Szeredi50322fe2006-02-28 16:59:03 -0800194 /* For negative dentries, always do a fresh lookup */
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800195 if (!inode)
196 return 0;
197
Al Viro0b728e12012-06-10 16:03:43 -0400198 if (flags & LOOKUP_RCU)
Miklos Szeredie7c0a162011-03-21 13:58:06 +0100199 return -ECHILD;
200
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800201 fc = get_fuse_conn(inode);
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400202 req = fuse_get_req_nopages(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700203 if (IS_ERR(req))
Miklos Szeredie5e55582005-09-09 13:10:28 -0700204 return 0;
205
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100206 forget = fuse_alloc_forget();
207 if (!forget) {
Miklos Szeredi2d510132006-11-25 11:09:20 -0800208 fuse_put_request(fc, req);
209 return 0;
210 }
211
Miklos Szeredi7dca9fd2007-11-28 16:21:59 -0800212 attr_version = fuse_get_attr_version(fc);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700213
Miklos Szeredie956edd2006-10-17 00:10:12 -0700214 parent = dget_parent(entry);
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700215 fuse_lookup_init(fc, req, get_node_id(parent->d_inode),
216 &entry->d_name, &outarg);
Tejun Heob93f8582008-11-26 12:03:55 +0100217 fuse_request_send(fc, req);
Miklos Szeredie956edd2006-10-17 00:10:12 -0700218 dput(parent);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700219 err = req->out.h.error;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800220 fuse_put_request(fc, req);
Miklos Szeredi50322fe2006-02-28 16:59:03 -0800221 /* Zero nodeid is same as -ENOENT */
222 if (!err && !outarg.nodeid)
223 err = -ENOENT;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700224 if (!err) {
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800225 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700226 if (outarg.nodeid != get_node_id(inode)) {
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100227 fuse_queue_forget(fc, forget, outarg.nodeid, 1);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700228 return 0;
229 }
Miklos Szeredi8da5ff22006-10-17 00:10:08 -0700230 spin_lock(&fc->lock);
Miklos Szeredi1729a162008-11-26 12:03:54 +0100231 fi->nlookup++;
Miklos Szeredi8da5ff22006-10-17 00:10:08 -0700232 spin_unlock(&fc->lock);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700233 }
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100234 kfree(forget);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700235 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
Miklos Szeredie5e55582005-09-09 13:10:28 -0700236 return 0;
237
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700238 fuse_change_attributes(inode, &outarg.attr,
239 entry_attr_timeout(&outarg),
240 attr_version);
241 fuse_change_entry_timeout(entry, &outarg);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700242 }
Feng Shuo4582a4a2013-01-15 11:23:28 +0800243 fuse_advise_use_readdirplus(inode);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700244 return 1;
245}
246
Miklos Szeredi8bfc0162006-01-16 22:14:28 -0800247static int invalid_nodeid(u64 nodeid)
Miklos Szeredi2827d0b22005-11-28 13:44:16 -0800248{
249 return !nodeid || nodeid == FUSE_ROOT_ID;
250}
251
Al Viro42695902009-02-20 05:59:13 +0000252const struct dentry_operations fuse_dentry_operations = {
Miklos Szeredie5e55582005-09-09 13:10:28 -0700253 .d_revalidate = fuse_dentry_revalidate,
254};
255
Timo Savolaa5bfffac2007-04-08 16:04:00 -0700256int fuse_valid_type(int m)
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800257{
258 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
259 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
260}
261
Miklos Szeredid2a85162006-10-17 00:10:11 -0700262/*
263 * Add a directory inode to a dentry, ensuring that no other dentry
264 * refers to this inode. Called with fc->inst_mutex.
265 */
Miklos Szeredi0de62562008-07-25 01:48:59 -0700266static struct dentry *fuse_d_add_directory(struct dentry *entry,
267 struct inode *inode)
Miklos Szeredid2a85162006-10-17 00:10:11 -0700268{
269 struct dentry *alias = d_find_alias(inode);
Miklos Szeredi0de62562008-07-25 01:48:59 -0700270 if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
Miklos Szeredid2a85162006-10-17 00:10:11 -0700271 /* This tries to shrink the subtree below alias */
272 fuse_invalidate_entry(alias);
273 dput(alias);
Al Virob3d9b7a2012-06-09 13:51:19 -0400274 if (!hlist_empty(&inode->i_dentry))
Miklos Szeredi0de62562008-07-25 01:48:59 -0700275 return ERR_PTR(-EBUSY);
276 } else {
277 dput(alias);
Miklos Szeredid2a85162006-10-17 00:10:11 -0700278 }
Miklos Szeredi0de62562008-07-25 01:48:59 -0700279 return d_splice_alias(inode, entry);
Miklos Szeredid2a85162006-10-17 00:10:11 -0700280}
281
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700282int fuse_lookup_name(struct super_block *sb, u64 nodeid, struct qstr *name,
283 struct fuse_entry_out *outarg, struct inode **inode)
284{
285 struct fuse_conn *fc = get_fuse_conn_super(sb);
286 struct fuse_req *req;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100287 struct fuse_forget_link *forget;
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700288 u64 attr_version;
289 int err;
290
291 *inode = NULL;
292 err = -ENAMETOOLONG;
293 if (name->len > FUSE_NAME_MAX)
294 goto out;
295
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400296 req = fuse_get_req_nopages(fc);
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700297 err = PTR_ERR(req);
298 if (IS_ERR(req))
299 goto out;
300
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100301 forget = fuse_alloc_forget();
302 err = -ENOMEM;
303 if (!forget) {
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700304 fuse_put_request(fc, req);
305 goto out;
306 }
307
308 attr_version = fuse_get_attr_version(fc);
309
310 fuse_lookup_init(fc, req, nodeid, name, outarg);
Tejun Heob93f8582008-11-26 12:03:55 +0100311 fuse_request_send(fc, req);
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700312 err = req->out.h.error;
313 fuse_put_request(fc, req);
314 /* Zero nodeid is same as -ENOENT, but with valid timeout */
315 if (err || !outarg->nodeid)
316 goto out_put_forget;
317
318 err = -EIO;
319 if (!outarg->nodeid)
320 goto out_put_forget;
321 if (!fuse_valid_type(outarg->attr.mode))
322 goto out_put_forget;
323
324 *inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
325 &outarg->attr, entry_attr_timeout(outarg),
326 attr_version);
327 err = -ENOMEM;
328 if (!*inode) {
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100329 fuse_queue_forget(fc, forget, outarg->nodeid, 1);
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700330 goto out;
331 }
332 err = 0;
333
334 out_put_forget:
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100335 kfree(forget);
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700336 out:
337 return err;
338}
339
Miklos Szeredi0aa7c692006-01-06 00:19:34 -0800340static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
Al Viro00cd8dd2012-06-10 17:13:09 -0400341 unsigned int flags)
Miklos Szeredie5e55582005-09-09 13:10:28 -0700342{
343 int err;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700344 struct fuse_entry_out outarg;
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700345 struct inode *inode;
Miklos Szeredi0de62562008-07-25 01:48:59 -0700346 struct dentry *newent;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700347 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700348 bool outarg_valid = true;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700349
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700350 err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
351 &outarg, &inode);
352 if (err == -ENOENT) {
353 outarg_valid = false;
354 err = 0;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800355 }
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700356 if (err)
357 goto out_err;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800358
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700359 err = -EIO;
360 if (inode && get_node_id(inode) == FUSE_ROOT_ID)
361 goto out_iput;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700362
Miklos Szeredid2a85162006-10-17 00:10:11 -0700363 if (inode && S_ISDIR(inode->i_mode)) {
364 mutex_lock(&fc->inst_mutex);
Miklos Szeredi0de62562008-07-25 01:48:59 -0700365 newent = fuse_d_add_directory(entry, inode);
Miklos Szeredid2a85162006-10-17 00:10:11 -0700366 mutex_unlock(&fc->inst_mutex);
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700367 err = PTR_ERR(newent);
368 if (IS_ERR(newent))
369 goto out_iput;
370 } else {
Miklos Szeredi0de62562008-07-25 01:48:59 -0700371 newent = d_splice_alias(inode, entry);
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700372 }
Miklos Szeredid2a85162006-10-17 00:10:11 -0700373
Miklos Szeredi0de62562008-07-25 01:48:59 -0700374 entry = newent ? newent : entry;
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700375 if (outarg_valid)
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700376 fuse_change_entry_timeout(entry, &outarg);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800377 else
378 fuse_invalidate_entry_cache(entry);
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700379
Feng Shuo4582a4a2013-01-15 11:23:28 +0800380 fuse_advise_use_readdirplus(dir);
Miklos Szeredi0de62562008-07-25 01:48:59 -0700381 return newent;
Miklos Szeredic180eeb2008-07-25 01:49:01 -0700382
383 out_iput:
384 iput(inode);
385 out_err:
386 return ERR_PTR(err);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700387}
388
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800389/*
390 * Atomic create+open operation
391 *
392 * If the filesystem doesn't support this, then fall back to separate
393 * 'mknod' + 'open' requests.
394 */
Al Virod9585272012-06-22 12:39:14 +0400395static int fuse_create_open(struct inode *dir, struct dentry *entry,
Al Viro30d90492012-06-22 12:40:19 +0400396 struct file *file, unsigned flags,
Al Virod9585272012-06-22 12:39:14 +0400397 umode_t mode, int *opened)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800398{
399 int err;
400 struct inode *inode;
401 struct fuse_conn *fc = get_fuse_conn(dir);
402 struct fuse_req *req;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100403 struct fuse_forget_link *forget;
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200404 struct fuse_create_in inarg;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800405 struct fuse_open_out outopen;
406 struct fuse_entry_out outentry;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800407 struct fuse_file *ff;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800408
Miklos Szerediaf109bc2012-08-15 13:01:24 +0200409 /* Userspace expects S_IFREG in create mode */
410 BUG_ON((mode & S_IFMT) != S_IFREG);
411
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100412 forget = fuse_alloc_forget();
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200413 err = -ENOMEM;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100414 if (!forget)
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200415 goto out_err;
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700416
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400417 req = fuse_get_req_nopages(fc);
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700418 err = PTR_ERR(req);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700419 if (IS_ERR(req))
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700420 goto out_put_forget_req;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800421
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700422 err = -ENOMEM;
Tejun Heoacf99432008-11-26 12:03:55 +0100423 ff = fuse_file_alloc(fc);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800424 if (!ff)
425 goto out_put_request;
426
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200427 if (!fc->dont_mask)
428 mode &= ~current_umask();
429
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800430 flags &= ~O_NOCTTY;
431 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700432 memset(&outentry, 0, sizeof(outentry));
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800433 inarg.flags = flags;
434 inarg.mode = mode;
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200435 inarg.umask = current_umask();
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800436 req->in.h.opcode = FUSE_CREATE;
437 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800438 req->in.numargs = 2;
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200439 req->in.args[0].size = fc->minor < 12 ? sizeof(struct fuse_open_in) :
440 sizeof(inarg);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800441 req->in.args[0].value = &inarg;
442 req->in.args[1].size = entry->d_name.len + 1;
443 req->in.args[1].value = entry->d_name.name;
444 req->out.numargs = 2;
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700445 if (fc->minor < 9)
446 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
447 else
448 req->out.args[0].size = sizeof(outentry);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800449 req->out.args[0].value = &outentry;
450 req->out.args[1].size = sizeof(outopen);
451 req->out.args[1].value = &outopen;
Tejun Heob93f8582008-11-26 12:03:55 +0100452 fuse_request_send(fc, req);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800453 err = req->out.h.error;
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200454 if (err)
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800455 goto out_free_ff;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800456
457 err = -EIO;
Miklos Szeredi2827d0b22005-11-28 13:44:16 -0800458 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800459 goto out_free_ff;
460
Miklos Szeredi51eb01e2006-06-25 05:48:50 -0700461 fuse_put_request(fc, req);
Miklos Szeredic7b71432009-04-28 16:56:37 +0200462 ff->fh = outopen.fh;
463 ff->nodeid = outentry.nodeid;
464 ff->open_flags = outopen.open_flags;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800465 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700466 &outentry.attr, entry_attr_timeout(&outentry), 0);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800467 if (!inode) {
468 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200469 fuse_sync_release(ff, flags);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100470 fuse_queue_forget(fc, forget, outentry.nodeid, 1);
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200471 err = -ENOMEM;
472 goto out_err;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800473 }
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100474 kfree(forget);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800475 d_instantiate(entry, inode);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700476 fuse_change_entry_timeout(entry, &outentry);
Miklos Szeredi0952b2a2008-02-06 01:38:38 -0800477 fuse_invalidate_attr(dir);
Al Viro30d90492012-06-22 12:40:19 +0400478 err = finish_open(file, entry, generic_file_open, opened);
479 if (err) {
Miklos Szeredi8b0797a2009-04-28 16:56:39 +0200480 fuse_sync_release(ff, flags);
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200481 } else {
482 file->private_data = fuse_file_get(ff);
483 fuse_finish_open(inode, file);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800484 }
Al Virod9585272012-06-22 12:39:14 +0400485 return err;
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800486
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200487out_free_ff:
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800488 fuse_file_free(ff);
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200489out_put_request:
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800490 fuse_put_request(fc, req);
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200491out_put_forget_req:
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100492 kfree(forget);
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200493out_err:
Al Virod9585272012-06-22 12:39:14 +0400494 return err;
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200495}
496
497static int fuse_mknod(struct inode *, struct dentry *, umode_t, dev_t);
Al Virod9585272012-06-22 12:39:14 +0400498static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
Al Viro30d90492012-06-22 12:40:19 +0400499 struct file *file, unsigned flags,
Al Virod9585272012-06-22 12:39:14 +0400500 umode_t mode, int *opened)
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200501{
502 int err;
503 struct fuse_conn *fc = get_fuse_conn(dir);
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200504 struct dentry *res = NULL;
505
506 if (d_unhashed(entry)) {
Al Viro00cd8dd2012-06-10 17:13:09 -0400507 res = fuse_lookup(dir, entry, 0);
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200508 if (IS_ERR(res))
Al Virod9585272012-06-22 12:39:14 +0400509 return PTR_ERR(res);
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200510
511 if (res)
512 entry = res;
513 }
514
515 if (!(flags & O_CREAT) || entry->d_inode)
516 goto no_open;
517
518 /* Only creates */
Al Viro47237682012-06-10 05:01:45 -0400519 *opened |= FILE_CREATED;
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200520
521 if (fc->no_create)
522 goto mknod;
523
Al Viro30d90492012-06-22 12:40:19 +0400524 err = fuse_create_open(dir, entry, file, flags, mode, opened);
Al Virod9585272012-06-22 12:39:14 +0400525 if (err == -ENOSYS) {
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200526 fc->no_create = 1;
527 goto mknod;
528 }
529out_dput:
530 dput(res);
Al Virod9585272012-06-22 12:39:14 +0400531 return err;
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200532
533mknod:
534 err = fuse_mknod(dir, entry, mode, 0);
Al Virod9585272012-06-22 12:39:14 +0400535 if (err)
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200536 goto out_dput;
Miklos Szeredic8ccbe02012-06-05 15:10:22 +0200537no_open:
Al Viroe45198a2012-06-10 06:48:09 -0400538 return finish_no_open(file, res);
Miklos Szeredifd72faa2005-11-07 00:59:51 -0800539}
540
Miklos Szeredi6f9f1182006-01-06 00:19:39 -0800541/*
542 * Code shared between mknod, mkdir, symlink and link
543 */
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700544static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
545 struct inode *dir, struct dentry *entry,
Al Viro541af6a2011-07-26 03:17:33 -0400546 umode_t mode)
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700547{
548 struct fuse_entry_out outarg;
549 struct inode *inode;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700550 int err;
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100551 struct fuse_forget_link *forget;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800552
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100553 forget = fuse_alloc_forget();
554 if (!forget) {
Miklos Szeredi2d510132006-11-25 11:09:20 -0800555 fuse_put_request(fc, req);
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100556 return -ENOMEM;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800557 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700558
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700559 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700560 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700561 req->out.numargs = 1;
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700562 if (fc->minor < 9)
563 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
564 else
565 req->out.args[0].size = sizeof(outarg);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700566 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +0100567 fuse_request_send(fc, req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700568 err = req->out.h.error;
Miklos Szeredi2d510132006-11-25 11:09:20 -0800569 fuse_put_request(fc, req);
570 if (err)
571 goto out_put_forget_req;
572
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800573 err = -EIO;
574 if (invalid_nodeid(outarg.nodeid))
Miklos Szeredi2d510132006-11-25 11:09:20 -0800575 goto out_put_forget_req;
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800576
577 if ((outarg.attr.mode ^ mode) & S_IFMT)
Miklos Szeredi2d510132006-11-25 11:09:20 -0800578 goto out_put_forget_req;
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800579
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700580 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700581 &outarg.attr, entry_attr_timeout(&outarg), 0);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700582 if (!inode) {
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100583 fuse_queue_forget(fc, forget, outarg.nodeid, 1);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700584 return -ENOMEM;
585 }
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100586 kfree(forget);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700587
Miklos Szeredid2a85162006-10-17 00:10:11 -0700588 if (S_ISDIR(inode->i_mode)) {
589 struct dentry *alias;
590 mutex_lock(&fc->inst_mutex);
591 alias = d_find_alias(inode);
592 if (alias) {
593 /* New directory must have moved since mkdir */
594 mutex_unlock(&fc->inst_mutex);
595 dput(alias);
596 iput(inode);
597 return -EBUSY;
598 }
599 d_instantiate(entry, inode);
600 mutex_unlock(&fc->inst_mutex);
601 } else
602 d_instantiate(entry, inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700603
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700604 fuse_change_entry_timeout(entry, &outarg);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700605 fuse_invalidate_attr(dir);
606 return 0;
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800607
Miklos Szeredi2d510132006-11-25 11:09:20 -0800608 out_put_forget_req:
Miklos Szeredi07e77dc2010-12-07 20:16:56 +0100609 kfree(forget);
Miklos Szeredi39ee0592006-01-06 00:19:43 -0800610 return err;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700611}
612
Al Viro1a67aaf2011-07-26 01:52:52 -0400613static int fuse_mknod(struct inode *dir, struct dentry *entry, umode_t mode,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700614 dev_t rdev)
615{
616 struct fuse_mknod_in inarg;
617 struct fuse_conn *fc = get_fuse_conn(dir);
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400618 struct fuse_req *req = fuse_get_req_nopages(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700619 if (IS_ERR(req))
620 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700621
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200622 if (!fc->dont_mask)
623 mode &= ~current_umask();
624
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700625 memset(&inarg, 0, sizeof(inarg));
626 inarg.mode = mode;
627 inarg.rdev = new_encode_dev(rdev);
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200628 inarg.umask = current_umask();
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700629 req->in.h.opcode = FUSE_MKNOD;
630 req->in.numargs = 2;
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200631 req->in.args[0].size = fc->minor < 12 ? FUSE_COMPAT_MKNOD_IN_SIZE :
632 sizeof(inarg);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700633 req->in.args[0].value = &inarg;
634 req->in.args[1].size = entry->d_name.len + 1;
635 req->in.args[1].value = entry->d_name.name;
636 return create_new_entry(fc, req, dir, entry, mode);
637}
638
Al Viro4acdaf22011-07-26 01:42:34 -0400639static int fuse_create(struct inode *dir, struct dentry *entry, umode_t mode,
Al Viroebfc3b42012-06-10 18:05:36 -0400640 bool excl)
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700641{
642 return fuse_mknod(dir, entry, mode, 0);
643}
644
Al Viro18bb1db2011-07-26 01:41:39 -0400645static int fuse_mkdir(struct inode *dir, struct dentry *entry, umode_t mode)
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700646{
647 struct fuse_mkdir_in inarg;
648 struct fuse_conn *fc = get_fuse_conn(dir);
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400649 struct fuse_req *req = fuse_get_req_nopages(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700650 if (IS_ERR(req))
651 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700652
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200653 if (!fc->dont_mask)
654 mode &= ~current_umask();
655
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700656 memset(&inarg, 0, sizeof(inarg));
657 inarg.mode = mode;
Miklos Szeredie0a43dd2009-06-30 20:12:23 +0200658 inarg.umask = current_umask();
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700659 req->in.h.opcode = FUSE_MKDIR;
660 req->in.numargs = 2;
661 req->in.args[0].size = sizeof(inarg);
662 req->in.args[0].value = &inarg;
663 req->in.args[1].size = entry->d_name.len + 1;
664 req->in.args[1].value = entry->d_name.name;
665 return create_new_entry(fc, req, dir, entry, S_IFDIR);
666}
667
668static int fuse_symlink(struct inode *dir, struct dentry *entry,
669 const char *link)
670{
671 struct fuse_conn *fc = get_fuse_conn(dir);
672 unsigned len = strlen(link) + 1;
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400673 struct fuse_req *req = fuse_get_req_nopages(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700674 if (IS_ERR(req))
675 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700676
677 req->in.h.opcode = FUSE_SYMLINK;
678 req->in.numargs = 2;
679 req->in.args[0].size = entry->d_name.len + 1;
680 req->in.args[0].value = entry->d_name.name;
681 req->in.args[1].size = len;
682 req->in.args[1].value = link;
683 return create_new_entry(fc, req, dir, entry, S_IFLNK);
684}
685
686static int fuse_unlink(struct inode *dir, struct dentry *entry)
687{
688 int err;
689 struct fuse_conn *fc = get_fuse_conn(dir);
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400690 struct fuse_req *req = fuse_get_req_nopages(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700691 if (IS_ERR(req))
692 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700693
694 req->in.h.opcode = FUSE_UNLINK;
695 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700696 req->in.numargs = 1;
697 req->in.args[0].size = entry->d_name.len + 1;
698 req->in.args[0].value = entry->d_name.name;
Tejun Heob93f8582008-11-26 12:03:55 +0100699 fuse_request_send(fc, req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700700 err = req->out.h.error;
701 fuse_put_request(fc, req);
702 if (!err) {
703 struct inode *inode = entry->d_inode;
Miklos Szerediac45d612012-03-05 15:48:11 +0100704 struct fuse_inode *fi = get_fuse_inode(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700705
Miklos Szerediac45d612012-03-05 15:48:11 +0100706 spin_lock(&fc->lock);
707 fi->attr_version = ++fc->attr_version;
Miklos Szeredidfca7ce2013-02-04 15:57:42 +0100708 /*
709 * If i_nlink == 0 then unlink doesn't make sense, yet this can
710 * happen if userspace filesystem is careless. It would be
711 * difficult to enforce correct nlink usage so just ignore this
712 * condition here
713 */
714 if (inode->i_nlink > 0)
715 drop_nlink(inode);
Miklos Szerediac45d612012-03-05 15:48:11 +0100716 spin_unlock(&fc->lock);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700717 fuse_invalidate_attr(inode);
718 fuse_invalidate_attr(dir);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800719 fuse_invalidate_entry_cache(entry);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700720 } else if (err == -EINTR)
721 fuse_invalidate_entry(entry);
722 return err;
723}
724
725static int fuse_rmdir(struct inode *dir, struct dentry *entry)
726{
727 int err;
728 struct fuse_conn *fc = get_fuse_conn(dir);
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400729 struct fuse_req *req = fuse_get_req_nopages(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700730 if (IS_ERR(req))
731 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700732
733 req->in.h.opcode = FUSE_RMDIR;
734 req->in.h.nodeid = get_node_id(dir);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700735 req->in.numargs = 1;
736 req->in.args[0].size = entry->d_name.len + 1;
737 req->in.args[0].value = entry->d_name.name;
Tejun Heob93f8582008-11-26 12:03:55 +0100738 fuse_request_send(fc, req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700739 err = req->out.h.error;
740 fuse_put_request(fc, req);
741 if (!err) {
Dave Hansence71ec32006-09-30 23:29:06 -0700742 clear_nlink(entry->d_inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700743 fuse_invalidate_attr(dir);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800744 fuse_invalidate_entry_cache(entry);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700745 } else if (err == -EINTR)
746 fuse_invalidate_entry(entry);
747 return err;
748}
749
750static int fuse_rename(struct inode *olddir, struct dentry *oldent,
751 struct inode *newdir, struct dentry *newent)
752{
753 int err;
754 struct fuse_rename_in inarg;
755 struct fuse_conn *fc = get_fuse_conn(olddir);
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400756 struct fuse_req *req = fuse_get_req_nopages(fc);
Sage Weile4eaac02011-05-24 13:06:07 -0700757
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700758 if (IS_ERR(req))
759 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700760
761 memset(&inarg, 0, sizeof(inarg));
762 inarg.newdir = get_node_id(newdir);
763 req->in.h.opcode = FUSE_RENAME;
764 req->in.h.nodeid = get_node_id(olddir);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700765 req->in.numargs = 3;
766 req->in.args[0].size = sizeof(inarg);
767 req->in.args[0].value = &inarg;
768 req->in.args[1].size = oldent->d_name.len + 1;
769 req->in.args[1].value = oldent->d_name.name;
770 req->in.args[2].size = newent->d_name.len + 1;
771 req->in.args[2].value = newent->d_name.name;
Tejun Heob93f8582008-11-26 12:03:55 +0100772 fuse_request_send(fc, req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700773 err = req->out.h.error;
774 fuse_put_request(fc, req);
775 if (!err) {
Miklos Szeredi08b63302007-11-28 16:22:03 -0800776 /* ctime changes */
777 fuse_invalidate_attr(oldent->d_inode);
778
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700779 fuse_invalidate_attr(olddir);
780 if (olddir != newdir)
781 fuse_invalidate_attr(newdir);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800782
783 /* newent will end up negative */
Miklos Szeredi5219f342009-11-04 10:24:52 +0100784 if (newent->d_inode) {
785 fuse_invalidate_attr(newent->d_inode);
Miklos Szeredi8cbdf1e2006-01-06 00:19:38 -0800786 fuse_invalidate_entry_cache(newent);
Miklos Szeredi5219f342009-11-04 10:24:52 +0100787 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700788 } else if (err == -EINTR) {
789 /* If request was interrupted, DEITY only knows if the
790 rename actually took place. If the invalidation
791 fails (e.g. some process has CWD under the renamed
792 directory), then there can be inconsistency between
793 the dcache and the real filesystem. Tough luck. */
794 fuse_invalidate_entry(oldent);
795 if (newent->d_inode)
796 fuse_invalidate_entry(newent);
797 }
798
799 return err;
800}
801
802static int fuse_link(struct dentry *entry, struct inode *newdir,
803 struct dentry *newent)
804{
805 int err;
806 struct fuse_link_in inarg;
807 struct inode *inode = entry->d_inode;
808 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400809 struct fuse_req *req = fuse_get_req_nopages(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700810 if (IS_ERR(req))
811 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700812
813 memset(&inarg, 0, sizeof(inarg));
814 inarg.oldnodeid = get_node_id(inode);
815 req->in.h.opcode = FUSE_LINK;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700816 req->in.numargs = 2;
817 req->in.args[0].size = sizeof(inarg);
818 req->in.args[0].value = &inarg;
819 req->in.args[1].size = newent->d_name.len + 1;
820 req->in.args[1].value = newent->d_name.name;
821 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
822 /* Contrary to "normal" filesystems it can happen that link
823 makes two "logical" inodes point to the same "physical"
824 inode. We invalidate the attributes of the old one, so it
825 will reflect changes in the backing inode (link count,
826 etc.)
827 */
Miklos Szerediac45d612012-03-05 15:48:11 +0100828 if (!err) {
829 struct fuse_inode *fi = get_fuse_inode(inode);
830
831 spin_lock(&fc->lock);
832 fi->attr_version = ++fc->attr_version;
833 inc_nlink(inode);
834 spin_unlock(&fc->lock);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700835 fuse_invalidate_attr(inode);
Miklos Szerediac45d612012-03-05 15:48:11 +0100836 } else if (err == -EINTR) {
837 fuse_invalidate_attr(inode);
838 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -0700839 return err;
840}
841
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700842static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
843 struct kstat *stat)
844{
Miklos Szeredi203627b2012-05-10 19:49:38 +0400845 unsigned int blkbits;
846
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700847 stat->dev = inode->i_sb->s_dev;
848 stat->ino = attr->ino;
849 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
850 stat->nlink = attr->nlink;
Eric W. Biederman499dcf22012-02-07 16:26:03 -0800851 stat->uid = make_kuid(&init_user_ns, attr->uid);
852 stat->gid = make_kgid(&init_user_ns, attr->gid);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700853 stat->rdev = inode->i_rdev;
854 stat->atime.tv_sec = attr->atime;
855 stat->atime.tv_nsec = attr->atimensec;
856 stat->mtime.tv_sec = attr->mtime;
857 stat->mtime.tv_nsec = attr->mtimensec;
858 stat->ctime.tv_sec = attr->ctime;
859 stat->ctime.tv_nsec = attr->ctimensec;
860 stat->size = attr->size;
861 stat->blocks = attr->blocks;
Miklos Szeredi203627b2012-05-10 19:49:38 +0400862
863 if (attr->blksize != 0)
864 blkbits = ilog2(attr->blksize);
865 else
866 blkbits = inode->i_sb->s_blocksize_bits;
867
868 stat->blksize = 1 << blkbits;
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700869}
870
Miklos Szeredic79e3222007-10-18 03:06:59 -0700871static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
872 struct file *file)
Miklos Szeredie5e55582005-09-09 13:10:28 -0700873{
874 int err;
Miklos Szeredic79e3222007-10-18 03:06:59 -0700875 struct fuse_getattr_in inarg;
876 struct fuse_attr_out outarg;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700877 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700878 struct fuse_req *req;
879 u64 attr_version;
880
Maxim Patlasovb111c8c2012-10-26 19:48:30 +0400881 req = fuse_get_req_nopages(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -0700882 if (IS_ERR(req))
883 return PTR_ERR(req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700884
Miklos Szeredi7dca9fd2007-11-28 16:21:59 -0800885 attr_version = fuse_get_attr_version(fc);
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700886
Miklos Szeredic79e3222007-10-18 03:06:59 -0700887 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700888 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredic79e3222007-10-18 03:06:59 -0700889 /* Directories have separate file-handle space */
890 if (file && S_ISREG(inode->i_mode)) {
891 struct fuse_file *ff = file->private_data;
892
893 inarg.getattr_flags |= FUSE_GETATTR_FH;
894 inarg.fh = ff->fh;
895 }
Miklos Szeredie5e55582005-09-09 13:10:28 -0700896 req->in.h.opcode = FUSE_GETATTR;
897 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredic79e3222007-10-18 03:06:59 -0700898 req->in.numargs = 1;
899 req->in.args[0].size = sizeof(inarg);
900 req->in.args[0].value = &inarg;
Miklos Szeredie5e55582005-09-09 13:10:28 -0700901 req->out.numargs = 1;
Miklos Szeredi0e9663e2007-10-18 03:07:05 -0700902 if (fc->minor < 9)
903 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
904 else
905 req->out.args[0].size = sizeof(outarg);
Miklos Szeredic79e3222007-10-18 03:06:59 -0700906 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +0100907 fuse_request_send(fc, req);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700908 err = req->out.h.error;
909 fuse_put_request(fc, req);
910 if (!err) {
Miklos Szeredic79e3222007-10-18 03:06:59 -0700911 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
Miklos Szeredie5e55582005-09-09 13:10:28 -0700912 make_bad_inode(inode);
913 err = -EIO;
914 } else {
Miklos Szeredic79e3222007-10-18 03:06:59 -0700915 fuse_change_attributes(inode, &outarg.attr,
916 attr_timeout(&outarg),
Miklos Szeredi1fb69e72007-10-18 03:06:58 -0700917 attr_version);
918 if (stat)
Miklos Szeredic79e3222007-10-18 03:06:59 -0700919 fuse_fillattr(inode, &outarg.attr, stat);
Miklos Szeredie5e55582005-09-09 13:10:28 -0700920 }
921 }
922 return err;
923}
924
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800925int fuse_update_attributes(struct inode *inode, struct kstat *stat,
926 struct file *file, bool *refreshed)
927{
928 struct fuse_inode *fi = get_fuse_inode(inode);
929 int err;
930 bool r;
931
932 if (fi->i_time < get_jiffies_64()) {
933 r = true;
934 err = fuse_do_getattr(inode, stat, file);
935 } else {
936 r = false;
937 err = 0;
938 if (stat) {
939 generic_fillattr(inode, stat);
940 stat->mode = fi->orig_i_mode;
Pavel Shilovsky45c72cd2012-05-10 19:49:38 +0400941 stat->ino = fi->orig_ino;
Miklos Szeredibcb4be82007-11-28 16:21:59 -0800942 }
943 }
944
945 if (refreshed != NULL)
946 *refreshed = r;
947
948 return err;
949}
950
John Muir3b463ae2009-05-31 11:13:57 -0400951int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid,
John Muir451d0f52011-12-06 21:50:06 +0100952 u64 child_nodeid, struct qstr *name)
John Muir3b463ae2009-05-31 11:13:57 -0400953{
954 int err = -ENOTDIR;
955 struct inode *parent;
956 struct dentry *dir;
957 struct dentry *entry;
958
959 parent = ilookup5(sb, parent_nodeid, fuse_inode_eq, &parent_nodeid);
960 if (!parent)
961 return -ENOENT;
962
963 mutex_lock(&parent->i_mutex);
964 if (!S_ISDIR(parent->i_mode))
965 goto unlock;
966
967 err = -ENOENT;
968 dir = d_find_alias(parent);
969 if (!dir)
970 goto unlock;
971
972 entry = d_lookup(dir, name);
973 dput(dir);
974 if (!entry)
975 goto unlock;
976
977 fuse_invalidate_attr(parent);
978 fuse_invalidate_entry(entry);
John Muir451d0f52011-12-06 21:50:06 +0100979
980 if (child_nodeid != 0 && entry->d_inode) {
981 mutex_lock(&entry->d_inode->i_mutex);
982 if (get_node_id(entry->d_inode) != child_nodeid) {
983 err = -ENOENT;
984 goto badentry;
985 }
986 if (d_mountpoint(entry)) {
987 err = -EBUSY;
988 goto badentry;
989 }
990 if (S_ISDIR(entry->d_inode->i_mode)) {
991 shrink_dcache_parent(entry);
992 if (!simple_empty(entry)) {
993 err = -ENOTEMPTY;
994 goto badentry;
995 }
996 entry->d_inode->i_flags |= S_DEAD;
997 }
998 dont_mount(entry);
999 clear_nlink(entry->d_inode);
1000 err = 0;
1001 badentry:
1002 mutex_unlock(&entry->d_inode->i_mutex);
1003 if (!err)
1004 d_delete(entry);
1005 } else {
1006 err = 0;
1007 }
John Muir3b463ae2009-05-31 11:13:57 -04001008 dput(entry);
John Muir3b463ae2009-05-31 11:13:57 -04001009
1010 unlock:
1011 mutex_unlock(&parent->i_mutex);
1012 iput(parent);
1013 return err;
1014}
1015
Miklos Szeredi87729a52005-09-09 13:10:34 -07001016/*
1017 * Calling into a user-controlled filesystem gives the filesystem
Anatol Pomozovc2132c12013-01-14 22:30:00 -08001018 * daemon ptrace-like capabilities over the current process. This
Miklos Szeredi87729a52005-09-09 13:10:34 -07001019 * means, that the filesystem daemon is able to record the exact
1020 * filesystem operations performed, and can also control the behavior
1021 * of the requester process in otherwise impossible ways. For example
1022 * it can delay the operation for arbitrary length of time allowing
1023 * DoS against the requester.
1024 *
1025 * For this reason only those processes can call into the filesystem,
1026 * for which the owner of the mount has ptrace privilege. This
1027 * excludes processes started by other users, suid or sgid processes.
1028 */
Anatol Pomozovc2132c12013-01-14 22:30:00 -08001029int fuse_allow_current_process(struct fuse_conn *fc)
Miklos Szeredi87729a52005-09-09 13:10:34 -07001030{
David Howellsc69e8d92008-11-14 10:39:19 +11001031 const struct cred *cred;
David Howellsc69e8d92008-11-14 10:39:19 +11001032
Miklos Szeredi87729a52005-09-09 13:10:34 -07001033 if (fc->flags & FUSE_ALLOW_OTHER)
1034 return 1;
1035
Anatol Pomozovc2132c12013-01-14 22:30:00 -08001036 cred = current_cred();
Eric W. Biederman499dcf22012-02-07 16:26:03 -08001037 if (uid_eq(cred->euid, fc->user_id) &&
1038 uid_eq(cred->suid, fc->user_id) &&
1039 uid_eq(cred->uid, fc->user_id) &&
1040 gid_eq(cred->egid, fc->group_id) &&
1041 gid_eq(cred->sgid, fc->group_id) &&
1042 gid_eq(cred->gid, fc->group_id))
Anatol Pomozovc2132c12013-01-14 22:30:00 -08001043 return 1;
Miklos Szeredi87729a52005-09-09 13:10:34 -07001044
Anatol Pomozovc2132c12013-01-14 22:30:00 -08001045 return 0;
Miklos Szeredi87729a52005-09-09 13:10:34 -07001046}
1047
Miklos Szeredi31d40d72005-11-07 00:59:50 -08001048static int fuse_access(struct inode *inode, int mask)
1049{
1050 struct fuse_conn *fc = get_fuse_conn(inode);
1051 struct fuse_req *req;
1052 struct fuse_access_in inarg;
1053 int err;
1054
1055 if (fc->no_access)
1056 return 0;
1057
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04001058 req = fuse_get_req_nopages(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001059 if (IS_ERR(req))
1060 return PTR_ERR(req);
Miklos Szeredi31d40d72005-11-07 00:59:50 -08001061
1062 memset(&inarg, 0, sizeof(inarg));
Al Viroe6305c42008-07-15 21:03:57 -04001063 inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
Miklos Szeredi31d40d72005-11-07 00:59:50 -08001064 req->in.h.opcode = FUSE_ACCESS;
1065 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi31d40d72005-11-07 00:59:50 -08001066 req->in.numargs = 1;
1067 req->in.args[0].size = sizeof(inarg);
1068 req->in.args[0].value = &inarg;
Tejun Heob93f8582008-11-26 12:03:55 +01001069 fuse_request_send(fc, req);
Miklos Szeredi31d40d72005-11-07 00:59:50 -08001070 err = req->out.h.error;
1071 fuse_put_request(fc, req);
1072 if (err == -ENOSYS) {
1073 fc->no_access = 1;
1074 err = 0;
1075 }
1076 return err;
1077}
1078
Al Viro10556cb2011-06-20 19:28:19 -04001079static int fuse_perm_getattr(struct inode *inode, int mask)
Miklos Szeredi19690dd2011-03-21 13:58:06 +01001080{
Al Viro10556cb2011-06-20 19:28:19 -04001081 if (mask & MAY_NOT_BLOCK)
Miklos Szeredi19690dd2011-03-21 13:58:06 +01001082 return -ECHILD;
1083
1084 return fuse_do_getattr(inode, NULL, NULL);
1085}
1086
Miklos Szeredi6f9f1182006-01-06 00:19:39 -08001087/*
1088 * Check permission. The two basic access models of FUSE are:
1089 *
1090 * 1) Local access checking ('default_permissions' mount option) based
1091 * on file mode. This is the plain old disk filesystem permission
1092 * modell.
1093 *
1094 * 2) "Remote" access checking, where server is responsible for
1095 * checking permission in each inode operation. An exception to this
1096 * is if ->permission() was invoked from sys_access() in which case an
1097 * access request is sent. Execute permission is still checked
1098 * locally based on file mode.
1099 */
Al Viro10556cb2011-06-20 19:28:19 -04001100static int fuse_permission(struct inode *inode, int mask)
Miklos Szeredie5e55582005-09-09 13:10:28 -07001101{
1102 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi244f6382007-10-16 23:31:02 -07001103 bool refreshed = false;
1104 int err = 0;
Miklos Szeredie5e55582005-09-09 13:10:28 -07001105
Anatol Pomozovc2132c12013-01-14 22:30:00 -08001106 if (!fuse_allow_current_process(fc))
Miklos Szeredie5e55582005-09-09 13:10:28 -07001107 return -EACCES;
Miklos Szeredi244f6382007-10-16 23:31:02 -07001108
1109 /*
Miklos Szeredie8e96152007-10-16 23:31:06 -07001110 * If attributes are needed, refresh them before proceeding
Miklos Szeredi244f6382007-10-16 23:31:02 -07001111 */
Miklos Szeredie8e96152007-10-16 23:31:06 -07001112 if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
1113 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
Miklos Szeredi19690dd2011-03-21 13:58:06 +01001114 struct fuse_inode *fi = get_fuse_inode(inode);
1115
1116 if (fi->i_time < get_jiffies_64()) {
1117 refreshed = true;
1118
Al Viro10556cb2011-06-20 19:28:19 -04001119 err = fuse_perm_getattr(inode, mask);
Miklos Szeredi19690dd2011-03-21 13:58:06 +01001120 if (err)
1121 return err;
1122 }
Miklos Szeredi244f6382007-10-16 23:31:02 -07001123 }
1124
1125 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
Al Viro2830ba72011-06-20 19:16:29 -04001126 err = generic_permission(inode, mask);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001127
1128 /* If permission is denied, try to refresh file
1129 attributes. This is also needed, because the root
1130 node will at first have no permissions */
Miklos Szeredi244f6382007-10-16 23:31:02 -07001131 if (err == -EACCES && !refreshed) {
Al Viro10556cb2011-06-20 19:28:19 -04001132 err = fuse_perm_getattr(inode, mask);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001133 if (!err)
Al Viro2830ba72011-06-20 19:16:29 -04001134 err = generic_permission(inode, mask);
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001135 }
1136
Miklos Szeredi6f9f1182006-01-06 00:19:39 -08001137 /* Note: the opposite of the above test does not
1138 exist. So if permissions are revoked this won't be
1139 noticed immediately, only after the attribute
1140 timeout has expired */
Eric Paris9cfcac82010-07-23 11:43:51 -04001141 } else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
Al Viro10556cb2011-06-20 19:28:19 -04001142 if (mask & MAY_NOT_BLOCK)
Miklos Szeredi19690dd2011-03-21 13:58:06 +01001143 return -ECHILD;
1144
Miklos Szeredie8e96152007-10-16 23:31:06 -07001145 err = fuse_access(inode, mask);
1146 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1147 if (!(inode->i_mode & S_IXUGO)) {
1148 if (refreshed)
1149 return -EACCES;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001150
Al Viro10556cb2011-06-20 19:28:19 -04001151 err = fuse_perm_getattr(inode, mask);
Miklos Szeredie8e96152007-10-16 23:31:06 -07001152 if (!err && !(inode->i_mode & S_IXUGO))
1153 return -EACCES;
1154 }
Miklos Szeredie5e55582005-09-09 13:10:28 -07001155 }
Miklos Szeredi244f6382007-10-16 23:31:02 -07001156 return err;
Miklos Szeredie5e55582005-09-09 13:10:28 -07001157}
1158
1159static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
1160 void *dstbuf, filldir_t filldir)
1161{
1162 while (nbytes >= FUSE_NAME_OFFSET) {
1163 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
1164 size_t reclen = FUSE_DIRENT_SIZE(dirent);
1165 int over;
1166 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1167 return -EIO;
1168 if (reclen > nbytes)
1169 break;
1170
1171 over = filldir(dstbuf, dirent->name, dirent->namelen,
1172 file->f_pos, dirent->ino, dirent->type);
1173 if (over)
1174 break;
1175
1176 buf += reclen;
1177 nbytes -= reclen;
1178 file->f_pos = dirent->off;
1179 }
1180
1181 return 0;
1182}
1183
Anand V. Avati0b05b182012-08-19 08:53:23 -04001184static int fuse_direntplus_link(struct file *file,
1185 struct fuse_direntplus *direntplus,
1186 u64 attr_version)
1187{
1188 int err;
1189 struct fuse_entry_out *o = &direntplus->entry_out;
1190 struct fuse_dirent *dirent = &direntplus->dirent;
1191 struct dentry *parent = file->f_path.dentry;
1192 struct qstr name = QSTR_INIT(dirent->name, dirent->namelen);
1193 struct dentry *dentry;
1194 struct dentry *alias;
1195 struct inode *dir = parent->d_inode;
1196 struct fuse_conn *fc;
1197 struct inode *inode;
1198
1199 if (!o->nodeid) {
1200 /*
1201 * Unlike in the case of fuse_lookup, zero nodeid does not mean
1202 * ENOENT. Instead, it only means the userspace filesystem did
1203 * not want to return attributes/handle for this entry.
1204 *
1205 * So do nothing.
1206 */
1207 return 0;
1208 }
1209
1210 if (name.name[0] == '.') {
1211 /*
1212 * We could potentially refresh the attributes of the directory
1213 * and its parent?
1214 */
1215 if (name.len == 1)
1216 return 0;
1217 if (name.name[1] == '.' && name.len == 2)
1218 return 0;
1219 }
1220 fc = get_fuse_conn(dir);
1221
1222 name.hash = full_name_hash(name.name, name.len);
1223 dentry = d_lookup(parent, &name);
1224 if (dentry && dentry->d_inode) {
1225 inode = dentry->d_inode;
1226 if (get_node_id(inode) == o->nodeid) {
1227 struct fuse_inode *fi;
1228 fi = get_fuse_inode(inode);
1229 spin_lock(&fc->lock);
1230 fi->nlookup++;
1231 spin_unlock(&fc->lock);
1232
1233 /*
1234 * The other branch to 'found' comes via fuse_iget()
1235 * which bumps nlookup inside
1236 */
1237 goto found;
1238 }
1239 err = d_invalidate(dentry);
1240 if (err)
1241 goto out;
1242 dput(dentry);
1243 dentry = NULL;
1244 }
1245
1246 dentry = d_alloc(parent, &name);
1247 err = -ENOMEM;
1248 if (!dentry)
1249 goto out;
1250
1251 inode = fuse_iget(dir->i_sb, o->nodeid, o->generation,
1252 &o->attr, entry_attr_timeout(o), attr_version);
1253 if (!inode)
1254 goto out;
1255
1256 alias = d_materialise_unique(dentry, inode);
1257 err = PTR_ERR(alias);
1258 if (IS_ERR(alias))
1259 goto out;
1260 if (alias) {
1261 dput(dentry);
1262 dentry = alias;
1263 }
1264
1265found:
1266 fuse_change_attributes(inode, &o->attr, entry_attr_timeout(o),
1267 attr_version);
1268
1269 fuse_change_entry_timeout(dentry, o);
1270
1271 err = 0;
1272out:
1273 if (dentry)
1274 dput(dentry);
1275 return err;
1276}
1277
1278static int parse_dirplusfile(char *buf, size_t nbytes, struct file *file,
1279 void *dstbuf, filldir_t filldir, u64 attr_version)
1280{
1281 struct fuse_direntplus *direntplus;
1282 struct fuse_dirent *dirent;
1283 size_t reclen;
1284 int over = 0;
1285 int ret;
1286
1287 while (nbytes >= FUSE_NAME_OFFSET_DIRENTPLUS) {
1288 direntplus = (struct fuse_direntplus *) buf;
1289 dirent = &direntplus->dirent;
1290 reclen = FUSE_DIRENTPLUS_SIZE(direntplus);
1291
1292 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1293 return -EIO;
1294 if (reclen > nbytes)
1295 break;
1296
1297 if (!over) {
1298 /* We fill entries into dstbuf only as much as
1299 it can hold. But we still continue iterating
1300 over remaining entries to link them. If not,
1301 we need to send a FORGET for each of those
1302 which we did not link.
1303 */
1304 over = filldir(dstbuf, dirent->name, dirent->namelen,
1305 file->f_pos, dirent->ino,
1306 dirent->type);
1307 file->f_pos = dirent->off;
1308 }
1309
1310 buf += reclen;
1311 nbytes -= reclen;
1312
1313 ret = fuse_direntplus_link(file, direntplus, attr_version);
1314 if (ret)
1315 fuse_force_forget(file, direntplus->entry_out.nodeid);
1316 }
1317
1318 return 0;
1319}
1320
Miklos Szeredie5e55582005-09-09 13:10:28 -07001321static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
1322{
Feng Shuo4582a4a2013-01-15 11:23:28 +08001323 int plus, err;
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001324 size_t nbytes;
1325 struct page *page;
Josef Sipek7706a9d2006-12-08 02:37:02 -08001326 struct inode *inode = file->f_path.dentry->d_inode;
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001327 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001328 struct fuse_req *req;
Anand V. Avati0b05b182012-08-19 08:53:23 -04001329 u64 attr_version = 0;
Miklos Szeredi248d86e2006-01-06 00:19:39 -08001330
1331 if (is_bad_inode(inode))
1332 return -EIO;
1333
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04001334 req = fuse_get_req(fc, 1);
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001335 if (IS_ERR(req))
1336 return PTR_ERR(req);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001337
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001338 page = alloc_page(GFP_KERNEL);
1339 if (!page) {
1340 fuse_put_request(fc, req);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001341 return -ENOMEM;
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001342 }
Feng Shuo4582a4a2013-01-15 11:23:28 +08001343
1344 plus = fuse_use_readdirplus(inode, file);
Miklos Szeredif4975c62009-04-02 14:25:34 +02001345 req->out.argpages = 1;
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001346 req->num_pages = 1;
1347 req->pages[0] = page;
Maxim Patlasov85f40ae2012-10-26 19:49:33 +04001348 req->page_descs[0].length = PAGE_SIZE;
Feng Shuo4582a4a2013-01-15 11:23:28 +08001349 if (plus) {
Anand V. Avati0b05b182012-08-19 08:53:23 -04001350 attr_version = fuse_get_attr_version(fc);
1351 fuse_read_fill(req, file, file->f_pos, PAGE_SIZE,
1352 FUSE_READDIRPLUS);
1353 } else {
1354 fuse_read_fill(req, file, file->f_pos, PAGE_SIZE,
1355 FUSE_READDIR);
1356 }
Tejun Heob93f8582008-11-26 12:03:55 +01001357 fuse_request_send(fc, req);
Miklos Szeredi361b1eb52006-01-16 22:14:45 -08001358 nbytes = req->out.args[0].size;
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001359 err = req->out.h.error;
1360 fuse_put_request(fc, req);
Anand V. Avati0b05b182012-08-19 08:53:23 -04001361 if (!err) {
Feng Shuo4582a4a2013-01-15 11:23:28 +08001362 if (plus) {
Anand V. Avati0b05b182012-08-19 08:53:23 -04001363 err = parse_dirplusfile(page_address(page), nbytes,
1364 file, dstbuf, filldir,
1365 attr_version);
1366 } else {
1367 err = parse_dirfile(page_address(page), nbytes, file,
1368 dstbuf, filldir);
1369 }
1370 }
Miklos Szeredie5e55582005-09-09 13:10:28 -07001371
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001372 __free_page(page);
Miklos Szeredib36c31b2005-09-09 13:10:38 -07001373 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredi04730fe2005-09-09 13:10:36 -07001374 return err;
Miklos Szeredie5e55582005-09-09 13:10:28 -07001375}
1376
1377static char *read_link(struct dentry *dentry)
1378{
1379 struct inode *inode = dentry->d_inode;
1380 struct fuse_conn *fc = get_fuse_conn(inode);
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04001381 struct fuse_req *req = fuse_get_req_nopages(fc);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001382 char *link;
1383
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001384 if (IS_ERR(req))
David Howellse231c2e2008-02-07 00:15:26 -08001385 return ERR_CAST(req);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001386
1387 link = (char *) __get_free_page(GFP_KERNEL);
1388 if (!link) {
1389 link = ERR_PTR(-ENOMEM);
1390 goto out;
1391 }
1392 req->in.h.opcode = FUSE_READLINK;
1393 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001394 req->out.argvar = 1;
1395 req->out.numargs = 1;
1396 req->out.args[0].size = PAGE_SIZE - 1;
1397 req->out.args[0].value = link;
Tejun Heob93f8582008-11-26 12:03:55 +01001398 fuse_request_send(fc, req);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001399 if (req->out.h.error) {
1400 free_page((unsigned long) link);
1401 link = ERR_PTR(req->out.h.error);
1402 } else
1403 link[req->out.args[0].size] = '\0';
1404 out:
1405 fuse_put_request(fc, req);
Miklos Szeredib36c31b2005-09-09 13:10:38 -07001406 fuse_invalidate_attr(inode); /* atime changed */
Miklos Szeredie5e55582005-09-09 13:10:28 -07001407 return link;
1408}
1409
1410static void free_link(char *link)
1411{
1412 if (!IS_ERR(link))
1413 free_page((unsigned long) link);
1414}
1415
1416static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
1417{
1418 nd_set_link(nd, read_link(dentry));
1419 return NULL;
1420}
1421
1422static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1423{
1424 free_link(nd_get_link(nd));
1425}
1426
1427static int fuse_dir_open(struct inode *inode, struct file *file)
1428{
Miklos Szeredi91fe96b2009-04-28 16:56:37 +02001429 return fuse_open_common(inode, file, true);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001430}
1431
1432static int fuse_dir_release(struct inode *inode, struct file *file)
1433{
Miklos Szeredi8b0797a2009-04-28 16:56:39 +02001434 fuse_release_common(file, FUSE_RELEASEDIR);
1435
1436 return 0;
Miklos Szeredie5e55582005-09-09 13:10:28 -07001437}
1438
Josef Bacik02c24a82011-07-16 20:44:56 -04001439static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
1440 int datasync)
Miklos Szeredi82547982005-09-09 13:10:38 -07001441{
Josef Bacik02c24a82011-07-16 20:44:56 -04001442 return fuse_fsync_common(file, start, end, datasync, 1);
Miklos Szeredi82547982005-09-09 13:10:38 -07001443}
1444
Miklos Szeredib18da0c2011-12-13 11:58:49 +01001445static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
1446 unsigned long arg)
1447{
1448 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1449
1450 /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1451 if (fc->minor < 18)
1452 return -ENOTTY;
1453
1454 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
1455}
1456
1457static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
1458 unsigned long arg)
1459{
1460 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1461
1462 if (fc->minor < 18)
1463 return -ENOTTY;
1464
1465 return fuse_ioctl_common(file, cmd, arg,
1466 FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
1467}
1468
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001469static bool update_mtime(unsigned ivalid)
1470{
1471 /* Always update if mtime is explicitly set */
1472 if (ivalid & ATTR_MTIME_SET)
1473 return true;
1474
1475 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1476 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1477 return false;
1478
1479 /* In all other cases update */
1480 return true;
1481}
1482
Miklos Szeredibefc6492005-11-07 00:59:52 -08001483static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001484{
1485 unsigned ivalid = iattr->ia_valid;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001486
1487 if (ivalid & ATTR_MODE)
Miklos Szeredibefc6492005-11-07 00:59:52 -08001488 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001489 if (ivalid & ATTR_UID)
Eric W. Biederman499dcf22012-02-07 16:26:03 -08001490 arg->valid |= FATTR_UID, arg->uid = from_kuid(&init_user_ns, iattr->ia_uid);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001491 if (ivalid & ATTR_GID)
Eric W. Biederman499dcf22012-02-07 16:26:03 -08001492 arg->valid |= FATTR_GID, arg->gid = from_kgid(&init_user_ns, iattr->ia_gid);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001493 if (ivalid & ATTR_SIZE)
Miklos Szeredibefc6492005-11-07 00:59:52 -08001494 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001495 if (ivalid & ATTR_ATIME) {
1496 arg->valid |= FATTR_ATIME;
Miklos Szeredibefc6492005-11-07 00:59:52 -08001497 arg->atime = iattr->ia_atime.tv_sec;
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001498 arg->atimensec = iattr->ia_atime.tv_nsec;
1499 if (!(ivalid & ATTR_ATIME_SET))
1500 arg->valid |= FATTR_ATIME_NOW;
1501 }
1502 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid)) {
1503 arg->valid |= FATTR_MTIME;
Miklos Szeredibefc6492005-11-07 00:59:52 -08001504 arg->mtime = iattr->ia_mtime.tv_sec;
Miklos Szeredi17637cb2007-10-18 03:07:01 -07001505 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1506 if (!(ivalid & ATTR_MTIME_SET))
1507 arg->valid |= FATTR_MTIME_NOW;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001508 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001509}
1510
Miklos Szeredi6f9f1182006-01-06 00:19:39 -08001511/*
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001512 * Prevent concurrent writepages on inode
1513 *
1514 * This is done by adding a negative bias to the inode write counter
1515 * and waiting for all pending writes to finish.
1516 */
1517void fuse_set_nowrite(struct inode *inode)
1518{
1519 struct fuse_conn *fc = get_fuse_conn(inode);
1520 struct fuse_inode *fi = get_fuse_inode(inode);
1521
1522 BUG_ON(!mutex_is_locked(&inode->i_mutex));
1523
1524 spin_lock(&fc->lock);
1525 BUG_ON(fi->writectr < 0);
1526 fi->writectr += FUSE_NOWRITE;
1527 spin_unlock(&fc->lock);
1528 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1529}
1530
1531/*
1532 * Allow writepages on inode
1533 *
1534 * Remove the bias from the writecounter and send any queued
1535 * writepages.
1536 */
1537static void __fuse_release_nowrite(struct inode *inode)
1538{
1539 struct fuse_inode *fi = get_fuse_inode(inode);
1540
1541 BUG_ON(fi->writectr != FUSE_NOWRITE);
1542 fi->writectr = 0;
1543 fuse_flush_writepages(inode);
1544}
1545
1546void fuse_release_nowrite(struct inode *inode)
1547{
1548 struct fuse_conn *fc = get_fuse_conn(inode);
1549
1550 spin_lock(&fc->lock);
1551 __fuse_release_nowrite(inode);
1552 spin_unlock(&fc->lock);
1553}
1554
1555/*
Miklos Szeredi6f9f1182006-01-06 00:19:39 -08001556 * Set attributes, and at the same time refresh them.
1557 *
1558 * Truncation is slightly complicated, because the 'truncate' request
1559 * may fail, in which case we don't want to touch the mapping.
Miklos Szeredi9ffbb912006-10-17 00:10:06 -07001560 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1561 * and the actual truncation by hand.
Miklos Szeredi6f9f1182006-01-06 00:19:39 -08001562 */
Miklos Szeredi49d49142007-10-18 03:07:00 -07001563static int fuse_do_setattr(struct dentry *entry, struct iattr *attr,
1564 struct file *file)
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001565{
1566 struct inode *inode = entry->d_inode;
1567 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001568 struct fuse_req *req;
1569 struct fuse_setattr_in inarg;
1570 struct fuse_attr_out outarg;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001571 bool is_truncate = false;
1572 loff_t oldsize;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001573 int err;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001574
Anatol Pomozovc2132c12013-01-14 22:30:00 -08001575 if (!fuse_allow_current_process(fc))
Miklos Szeredie57ac682007-10-18 03:06:58 -07001576 return -EACCES;
1577
Christoph Hellwigdb78b872010-06-04 11:30:03 +02001578 if (!(fc->flags & FUSE_DEFAULT_PERMISSIONS))
1579 attr->ia_valid |= ATTR_FORCE;
1580
1581 err = inode_change_ok(inode, attr);
1582 if (err)
1583 return err;
Miklos Szeredi1e9a4ed2005-09-09 13:10:31 -07001584
Miklos Szeredi8d56add2011-02-25 14:44:58 +01001585 if (attr->ia_valid & ATTR_OPEN) {
1586 if (fc->atomic_o_trunc)
1587 return 0;
1588 file = NULL;
1589 }
Miklos Szeredi6ff958e2007-10-18 03:07:02 -07001590
Christoph Hellwig2c27c652010-06-04 11:30:04 +02001591 if (attr->ia_valid & ATTR_SIZE)
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001592 is_truncate = true;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001593
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04001594 req = fuse_get_req_nopages(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001595 if (IS_ERR(req))
1596 return PTR_ERR(req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001597
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001598 if (is_truncate)
1599 fuse_set_nowrite(inode);
1600
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001601 memset(&inarg, 0, sizeof(inarg));
Miklos Szeredi0e9663e2007-10-18 03:07:05 -07001602 memset(&outarg, 0, sizeof(outarg));
Miklos Szeredibefc6492005-11-07 00:59:52 -08001603 iattr_to_fattr(attr, &inarg);
Miklos Szeredi49d49142007-10-18 03:07:00 -07001604 if (file) {
1605 struct fuse_file *ff = file->private_data;
1606 inarg.valid |= FATTR_FH;
1607 inarg.fh = ff->fh;
1608 }
Miklos Szeredif3332112007-10-18 03:07:04 -07001609 if (attr->ia_valid & ATTR_SIZE) {
1610 /* For mandatory locking in truncate */
1611 inarg.valid |= FATTR_LOCKOWNER;
1612 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1613 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001614 req->in.h.opcode = FUSE_SETATTR;
1615 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001616 req->in.numargs = 1;
1617 req->in.args[0].size = sizeof(inarg);
1618 req->in.args[0].value = &inarg;
1619 req->out.numargs = 1;
Miklos Szeredi0e9663e2007-10-18 03:07:05 -07001620 if (fc->minor < 9)
1621 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
1622 else
1623 req->out.args[0].size = sizeof(outarg);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001624 req->out.args[0].value = &outarg;
Tejun Heob93f8582008-11-26 12:03:55 +01001625 fuse_request_send(fc, req);
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001626 err = req->out.h.error;
1627 fuse_put_request(fc, req);
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001628 if (err) {
1629 if (err == -EINTR)
1630 fuse_invalidate_attr(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001631 goto error;
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001632 }
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001633
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001634 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1635 make_bad_inode(inode);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001636 err = -EIO;
1637 goto error;
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001638 }
1639
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001640 spin_lock(&fc->lock);
1641 fuse_change_attributes_common(inode, &outarg.attr,
1642 attr_timeout(&outarg));
1643 oldsize = inode->i_size;
1644 i_size_write(inode, outarg.attr.size);
1645
1646 if (is_truncate) {
1647 /* NOTE: this may release/reacquire fc->lock */
1648 __fuse_release_nowrite(inode);
1649 }
1650 spin_unlock(&fc->lock);
1651
1652 /*
1653 * Only call invalidate_inode_pages2() after removing
1654 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1655 */
1656 if (S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
npiggin@suse.dec08d3b02009-08-21 02:35:06 +10001657 truncate_pagecache(inode, oldsize, outarg.attr.size);
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001658 invalidate_inode_pages2(inode->i_mapping);
1659 }
1660
Miklos Szeredie00d2c22007-10-16 23:31:01 -07001661 return 0;
Miklos Szeredi3be5a522008-04-30 00:54:41 -07001662
1663error:
1664 if (is_truncate)
1665 fuse_release_nowrite(inode);
1666
1667 return err;
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001668}
1669
Miklos Szeredi49d49142007-10-18 03:07:00 -07001670static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1671{
1672 if (attr->ia_valid & ATTR_FILE)
1673 return fuse_do_setattr(entry, attr, attr->ia_file);
1674 else
1675 return fuse_do_setattr(entry, attr, NULL);
1676}
1677
Miklos Szeredie5e55582005-09-09 13:10:28 -07001678static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1679 struct kstat *stat)
1680{
1681 struct inode *inode = entry->d_inode;
Miklos Szeredi244f6382007-10-16 23:31:02 -07001682 struct fuse_conn *fc = get_fuse_conn(inode);
Miklos Szeredi244f6382007-10-16 23:31:02 -07001683
Anatol Pomozovc2132c12013-01-14 22:30:00 -08001684 if (!fuse_allow_current_process(fc))
Miklos Szeredi244f6382007-10-16 23:31:02 -07001685 return -EACCES;
1686
Miklos Szeredibcb4be82007-11-28 16:21:59 -08001687 return fuse_update_attributes(inode, stat, NULL, NULL);
Miklos Szeredie5e55582005-09-09 13:10:28 -07001688}
1689
Miklos Szeredi92a87802005-09-09 13:10:31 -07001690static int fuse_setxattr(struct dentry *entry, const char *name,
1691 const void *value, size_t size, int flags)
1692{
1693 struct inode *inode = entry->d_inode;
1694 struct fuse_conn *fc = get_fuse_conn(inode);
1695 struct fuse_req *req;
1696 struct fuse_setxattr_in inarg;
1697 int err;
1698
Miklos Szeredi92a87802005-09-09 13:10:31 -07001699 if (fc->no_setxattr)
1700 return -EOPNOTSUPP;
1701
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04001702 req = fuse_get_req_nopages(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001703 if (IS_ERR(req))
1704 return PTR_ERR(req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001705
1706 memset(&inarg, 0, sizeof(inarg));
1707 inarg.size = size;
1708 inarg.flags = flags;
1709 req->in.h.opcode = FUSE_SETXATTR;
1710 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001711 req->in.numargs = 3;
1712 req->in.args[0].size = sizeof(inarg);
1713 req->in.args[0].value = &inarg;
1714 req->in.args[1].size = strlen(name) + 1;
1715 req->in.args[1].value = name;
1716 req->in.args[2].size = size;
1717 req->in.args[2].value = value;
Tejun Heob93f8582008-11-26 12:03:55 +01001718 fuse_request_send(fc, req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001719 err = req->out.h.error;
1720 fuse_put_request(fc, req);
1721 if (err == -ENOSYS) {
1722 fc->no_setxattr = 1;
1723 err = -EOPNOTSUPP;
1724 }
1725 return err;
1726}
1727
1728static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1729 void *value, size_t size)
1730{
1731 struct inode *inode = entry->d_inode;
1732 struct fuse_conn *fc = get_fuse_conn(inode);
1733 struct fuse_req *req;
1734 struct fuse_getxattr_in inarg;
1735 struct fuse_getxattr_out outarg;
1736 ssize_t ret;
1737
1738 if (fc->no_getxattr)
1739 return -EOPNOTSUPP;
1740
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04001741 req = fuse_get_req_nopages(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001742 if (IS_ERR(req))
1743 return PTR_ERR(req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001744
1745 memset(&inarg, 0, sizeof(inarg));
1746 inarg.size = size;
1747 req->in.h.opcode = FUSE_GETXATTR;
1748 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001749 req->in.numargs = 2;
1750 req->in.args[0].size = sizeof(inarg);
1751 req->in.args[0].value = &inarg;
1752 req->in.args[1].size = strlen(name) + 1;
1753 req->in.args[1].value = name;
1754 /* This is really two different operations rolled into one */
1755 req->out.numargs = 1;
1756 if (size) {
1757 req->out.argvar = 1;
1758 req->out.args[0].size = size;
1759 req->out.args[0].value = value;
1760 } else {
1761 req->out.args[0].size = sizeof(outarg);
1762 req->out.args[0].value = &outarg;
1763 }
Tejun Heob93f8582008-11-26 12:03:55 +01001764 fuse_request_send(fc, req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001765 ret = req->out.h.error;
1766 if (!ret)
1767 ret = size ? req->out.args[0].size : outarg.size;
1768 else {
1769 if (ret == -ENOSYS) {
1770 fc->no_getxattr = 1;
1771 ret = -EOPNOTSUPP;
1772 }
1773 }
1774 fuse_put_request(fc, req);
1775 return ret;
1776}
1777
1778static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1779{
1780 struct inode *inode = entry->d_inode;
1781 struct fuse_conn *fc = get_fuse_conn(inode);
1782 struct fuse_req *req;
1783 struct fuse_getxattr_in inarg;
1784 struct fuse_getxattr_out outarg;
1785 ssize_t ret;
1786
Anatol Pomozovc2132c12013-01-14 22:30:00 -08001787 if (!fuse_allow_current_process(fc))
Miklos Szeredie57ac682007-10-18 03:06:58 -07001788 return -EACCES;
1789
Miklos Szeredi92a87802005-09-09 13:10:31 -07001790 if (fc->no_listxattr)
1791 return -EOPNOTSUPP;
1792
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04001793 req = fuse_get_req_nopages(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001794 if (IS_ERR(req))
1795 return PTR_ERR(req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001796
1797 memset(&inarg, 0, sizeof(inarg));
1798 inarg.size = size;
1799 req->in.h.opcode = FUSE_LISTXATTR;
1800 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001801 req->in.numargs = 1;
1802 req->in.args[0].size = sizeof(inarg);
1803 req->in.args[0].value = &inarg;
1804 /* This is really two different operations rolled into one */
1805 req->out.numargs = 1;
1806 if (size) {
1807 req->out.argvar = 1;
1808 req->out.args[0].size = size;
1809 req->out.args[0].value = list;
1810 } else {
1811 req->out.args[0].size = sizeof(outarg);
1812 req->out.args[0].value = &outarg;
1813 }
Tejun Heob93f8582008-11-26 12:03:55 +01001814 fuse_request_send(fc, req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001815 ret = req->out.h.error;
1816 if (!ret)
1817 ret = size ? req->out.args[0].size : outarg.size;
1818 else {
1819 if (ret == -ENOSYS) {
1820 fc->no_listxattr = 1;
1821 ret = -EOPNOTSUPP;
1822 }
1823 }
1824 fuse_put_request(fc, req);
1825 return ret;
1826}
1827
1828static int fuse_removexattr(struct dentry *entry, const char *name)
1829{
1830 struct inode *inode = entry->d_inode;
1831 struct fuse_conn *fc = get_fuse_conn(inode);
1832 struct fuse_req *req;
1833 int err;
1834
1835 if (fc->no_removexattr)
1836 return -EOPNOTSUPP;
1837
Maxim Patlasovb111c8c2012-10-26 19:48:30 +04001838 req = fuse_get_req_nopages(fc);
Miklos Szeredice1d5a42006-04-10 22:54:58 -07001839 if (IS_ERR(req))
1840 return PTR_ERR(req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001841
1842 req->in.h.opcode = FUSE_REMOVEXATTR;
1843 req->in.h.nodeid = get_node_id(inode);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001844 req->in.numargs = 1;
1845 req->in.args[0].size = strlen(name) + 1;
1846 req->in.args[0].value = name;
Tejun Heob93f8582008-11-26 12:03:55 +01001847 fuse_request_send(fc, req);
Miklos Szeredi92a87802005-09-09 13:10:31 -07001848 err = req->out.h.error;
1849 fuse_put_request(fc, req);
1850 if (err == -ENOSYS) {
1851 fc->no_removexattr = 1;
1852 err = -EOPNOTSUPP;
1853 }
1854 return err;
1855}
1856
Arjan van de Ven754661f2007-02-12 00:55:38 -08001857static const struct inode_operations fuse_dir_inode_operations = {
Miklos Szeredie5e55582005-09-09 13:10:28 -07001858 .lookup = fuse_lookup,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001859 .mkdir = fuse_mkdir,
1860 .symlink = fuse_symlink,
1861 .unlink = fuse_unlink,
1862 .rmdir = fuse_rmdir,
1863 .rename = fuse_rename,
1864 .link = fuse_link,
1865 .setattr = fuse_setattr,
1866 .create = fuse_create,
Miklos Szeredic8ccbe02012-06-05 15:10:22 +02001867 .atomic_open = fuse_atomic_open,
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001868 .mknod = fuse_mknod,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001869 .permission = fuse_permission,
1870 .getattr = fuse_getattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -07001871 .setxattr = fuse_setxattr,
1872 .getxattr = fuse_getxattr,
1873 .listxattr = fuse_listxattr,
1874 .removexattr = fuse_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001875};
1876
Arjan van de Ven4b6f5d22006-03-28 01:56:42 -08001877static const struct file_operations fuse_dir_operations = {
Miklos Szeredib6aeade2005-09-09 13:10:30 -07001878 .llseek = generic_file_llseek,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001879 .read = generic_read_dir,
1880 .readdir = fuse_readdir,
1881 .open = fuse_dir_open,
1882 .release = fuse_dir_release,
Miklos Szeredi82547982005-09-09 13:10:38 -07001883 .fsync = fuse_dir_fsync,
Miklos Szeredib18da0c2011-12-13 11:58:49 +01001884 .unlocked_ioctl = fuse_dir_ioctl,
1885 .compat_ioctl = fuse_dir_compat_ioctl,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001886};
1887
Arjan van de Ven754661f2007-02-12 00:55:38 -08001888static const struct inode_operations fuse_common_inode_operations = {
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001889 .setattr = fuse_setattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001890 .permission = fuse_permission,
1891 .getattr = fuse_getattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -07001892 .setxattr = fuse_setxattr,
1893 .getxattr = fuse_getxattr,
1894 .listxattr = fuse_listxattr,
1895 .removexattr = fuse_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001896};
1897
Arjan van de Ven754661f2007-02-12 00:55:38 -08001898static const struct inode_operations fuse_symlink_inode_operations = {
Miklos Szeredi9e6268d2005-09-09 13:10:29 -07001899 .setattr = fuse_setattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001900 .follow_link = fuse_follow_link,
1901 .put_link = fuse_put_link,
1902 .readlink = generic_readlink,
1903 .getattr = fuse_getattr,
Miklos Szeredi92a87802005-09-09 13:10:31 -07001904 .setxattr = fuse_setxattr,
1905 .getxattr = fuse_getxattr,
1906 .listxattr = fuse_listxattr,
1907 .removexattr = fuse_removexattr,
Miklos Szeredie5e55582005-09-09 13:10:28 -07001908};
1909
1910void fuse_init_common(struct inode *inode)
1911{
1912 inode->i_op = &fuse_common_inode_operations;
1913}
1914
1915void fuse_init_dir(struct inode *inode)
1916{
1917 inode->i_op = &fuse_dir_inode_operations;
1918 inode->i_fop = &fuse_dir_operations;
1919}
1920
1921void fuse_init_symlink(struct inode *inode)
1922{
1923 inode->i_op = &fuse_symlink_inode_operations;
1924}