• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2   FUSE: Filesystem in Userspace
3   Copyright (C) 2001-2008  Miklos Szeredi <miklos@szeredi.hu>
4 
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>
13 #include <linux/sched.h>
14 #include <linux/namei.h>
15 #include <linux/slab.h>
16 
fuse_use_readdirplus(struct inode * dir,struct file * filp)17 static 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 (!fc->readdirplus_auto)
25 		return true;
26 	if (test_and_clear_bit(FUSE_I_ADVISE_RDPLUS, &fi->state))
27 		return true;
28 	if (filp->f_pos == 0)
29 		return true;
30 	return false;
31 }
32 
fuse_advise_use_readdirplus(struct inode * dir)33 static void fuse_advise_use_readdirplus(struct inode *dir)
34 {
35 	struct fuse_inode *fi = get_fuse_inode(dir);
36 
37 	set_bit(FUSE_I_ADVISE_RDPLUS, &fi->state);
38 }
39 
40 #if BITS_PER_LONG >= 64
fuse_dentry_settime(struct dentry * entry,u64 time)41 static inline void fuse_dentry_settime(struct dentry *entry, u64 time)
42 {
43 	entry->d_time = time;
44 }
45 
fuse_dentry_time(struct dentry * entry)46 static inline u64 fuse_dentry_time(struct dentry *entry)
47 {
48 	return entry->d_time;
49 }
50 #else
51 /*
52  * On 32 bit archs store the high 32 bits of time in d_fsdata
53  */
fuse_dentry_settime(struct dentry * entry,u64 time)54 static void fuse_dentry_settime(struct dentry *entry, u64 time)
55 {
56 	entry->d_time = time;
57 	entry->d_fsdata = (void *) (unsigned long) (time >> 32);
58 }
59 
fuse_dentry_time(struct dentry * entry)60 static u64 fuse_dentry_time(struct dentry *entry)
61 {
62 	return (u64) entry->d_time +
63 		((u64) (unsigned long) entry->d_fsdata << 32);
64 }
65 #endif
66 
67 /*
68  * FUSE caches dentries and attributes with separate timeout.  The
69  * time in jiffies until the dentry/attributes are valid is stored in
70  * dentry->d_time and fuse_inode->i_time respectively.
71  */
72 
73 /*
74  * Calculate the time in jiffies until a dentry/attributes are valid
75  */
time_to_jiffies(unsigned long sec,unsigned long nsec)76 static u64 time_to_jiffies(unsigned long sec, unsigned long nsec)
77 {
78 	if (sec || nsec) {
79 		struct timespec ts = {sec, nsec};
80 		return get_jiffies_64() + timespec_to_jiffies(&ts);
81 	} else
82 		return 0;
83 }
84 
85 /*
86  * Set dentry and possibly attribute timeouts from the lookup/mk*
87  * replies
88  */
fuse_change_entry_timeout(struct dentry * entry,struct fuse_entry_out * o)89 static void fuse_change_entry_timeout(struct dentry *entry,
90 				      struct fuse_entry_out *o)
91 {
92 	fuse_dentry_settime(entry,
93 		time_to_jiffies(o->entry_valid, o->entry_valid_nsec));
94 }
95 
attr_timeout(struct fuse_attr_out * o)96 static u64 attr_timeout(struct fuse_attr_out *o)
97 {
98 	return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
99 }
100 
entry_attr_timeout(struct fuse_entry_out * o)101 static u64 entry_attr_timeout(struct fuse_entry_out *o)
102 {
103 	return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
104 }
105 
106 /*
107  * Mark the attributes as stale, so that at the next call to
108  * ->getattr() they will be fetched from userspace
109  */
fuse_invalidate_attr(struct inode * inode)110 void fuse_invalidate_attr(struct inode *inode)
111 {
112 	get_fuse_inode(inode)->i_time = 0;
113 }
114 
115 /*
116  * Just mark the entry as stale, so that a next attempt to look it up
117  * will result in a new lookup call to userspace
118  *
119  * This is called when a dentry is about to become negative and the
120  * timeout is unknown (unlink, rmdir, rename and in some cases
121  * lookup)
122  */
fuse_invalidate_entry_cache(struct dentry * entry)123 void fuse_invalidate_entry_cache(struct dentry *entry)
124 {
125 	fuse_dentry_settime(entry, 0);
126 }
127 
128 /*
129  * Same as fuse_invalidate_entry_cache(), but also try to remove the
130  * dentry from the hash
131  */
fuse_invalidate_entry(struct dentry * entry)132 static void fuse_invalidate_entry(struct dentry *entry)
133 {
134 	d_invalidate(entry);
135 	fuse_invalidate_entry_cache(entry);
136 }
137 
fuse_lookup_init(struct fuse_conn * fc,struct fuse_req * req,u64 nodeid,struct qstr * name,struct fuse_entry_out * outarg)138 static void fuse_lookup_init(struct fuse_conn *fc, struct fuse_req *req,
139 			     u64 nodeid, struct qstr *name,
140 			     struct fuse_entry_out *outarg)
141 {
142 	memset(outarg, 0, sizeof(struct fuse_entry_out));
143 	req->in.h.opcode = FUSE_LOOKUP;
144 	req->in.h.nodeid = nodeid;
145 	req->in.numargs = 1;
146 	req->in.args[0].size = name->len + 1;
147 	req->in.args[0].value = name->name;
148 	req->out.numargs = 1;
149 	if (fc->minor < 9)
150 		req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
151 	else
152 		req->out.args[0].size = sizeof(struct fuse_entry_out);
153 	req->out.args[0].value = outarg;
154 }
155 
fuse_get_attr_version(struct fuse_conn * fc)156 u64 fuse_get_attr_version(struct fuse_conn *fc)
157 {
158 	u64 curr_version;
159 
160 	/*
161 	 * The spin lock isn't actually needed on 64bit archs, but we
162 	 * don't yet care too much about such optimizations.
163 	 */
164 	spin_lock(&fc->lock);
165 	curr_version = fc->attr_version;
166 	spin_unlock(&fc->lock);
167 
168 	return curr_version;
169 }
170 
171 /*
172  * Check whether the dentry is still valid
173  *
174  * If the entry validity timeout has expired and the dentry is
175  * positive, try to redo the lookup.  If the lookup results in a
176  * different inode, then let the VFS invalidate the dentry and redo
177  * the lookup once more.  If the lookup results in the same inode,
178  * then refresh the attributes, timeouts and mark the dentry valid.
179  */
fuse_dentry_revalidate(struct dentry * entry,unsigned int flags)180 static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags)
181 {
182 	struct inode *inode;
183 	struct dentry *parent;
184 	struct fuse_conn *fc;
185 
186 	inode = ACCESS_ONCE(entry->d_inode);
187 	if (inode && is_bad_inode(inode))
188 		return 0;
189 	else if (fuse_dentry_time(entry) < get_jiffies_64()) {
190 		int err;
191 		struct fuse_entry_out outarg;
192 		struct fuse_req *req;
193 		struct fuse_forget_link *forget;
194 		u64 attr_version;
195 
196 		/* For negative dentries, always do a fresh lookup */
197 		if (!inode)
198 			return 0;
199 
200 		if (flags & LOOKUP_RCU)
201 			return -ECHILD;
202 
203 		fc = get_fuse_conn(inode);
204 		req = fuse_get_req_nopages(fc);
205 		if (IS_ERR(req))
206 			return 0;
207 
208 		forget = fuse_alloc_forget();
209 		if (!forget) {
210 			fuse_put_request(fc, req);
211 			return 0;
212 		}
213 
214 		attr_version = fuse_get_attr_version(fc);
215 
216 		parent = dget_parent(entry);
217 		fuse_lookup_init(fc, req, get_node_id(parent->d_inode),
218 				 &entry->d_name, &outarg);
219 		fuse_request_send(fc, req);
220 		dput(parent);
221 		err = req->out.h.error;
222 		fuse_put_request(fc, req);
223 		/* Zero nodeid is same as -ENOENT */
224 		if (!err && !outarg.nodeid)
225 			err = -ENOENT;
226 		if (!err) {
227 			struct fuse_inode *fi = get_fuse_inode(inode);
228 			if (outarg.nodeid != get_node_id(inode)) {
229 				fuse_queue_forget(fc, forget, outarg.nodeid, 1);
230 				return 0;
231 			}
232 			spin_lock(&fc->lock);
233 			fi->nlookup++;
234 			spin_unlock(&fc->lock);
235 		}
236 		kfree(forget);
237 		if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
238 			return 0;
239 
240 		fuse_change_attributes(inode, &outarg.attr,
241 				       entry_attr_timeout(&outarg),
242 				       attr_version);
243 		fuse_change_entry_timeout(entry, &outarg);
244 	} else if (inode) {
245 		fc = get_fuse_conn(inode);
246 		if (fc->readdirplus_auto) {
247 			parent = dget_parent(entry);
248 			fuse_advise_use_readdirplus(parent->d_inode);
249 			dput(parent);
250 		}
251 	}
252 	return 1;
253 }
254 
255 /*
256  * Get the canonical path. Since we must translate to a path, this must be done
257  * in the context of the userspace daemon, however, the userspace daemon cannot
258  * look up paths on its own. Instead, we handle the lookup as a special case
259  * inside of the write request.
260  */
fuse_dentry_canonical_path(const struct path * path,struct path * canonical_path)261 static void fuse_dentry_canonical_path(const struct path *path, struct path *canonical_path) {
262 	struct inode *inode = path->dentry->d_inode;
263 	struct fuse_conn *fc = get_fuse_conn(inode);
264 	struct fuse_req *req;
265 	int err;
266 	char *path_name;
267 
268 	req = fuse_get_req(fc, 1);
269 	err = PTR_ERR(req);
270 	if (IS_ERR(req))
271 		goto default_path;
272 
273 	path_name = (char*)__get_free_page(GFP_KERNEL);
274 	if (!path_name) {
275 		fuse_put_request(fc, req);
276 		goto default_path;
277 	}
278 
279 	req->in.h.opcode = FUSE_CANONICAL_PATH;
280 	req->in.h.nodeid = get_node_id(inode);
281 	req->in.numargs = 0;
282 	req->out.numargs = 1;
283 	req->out.args[0].size = PATH_MAX;
284 	req->out.args[0].value = path_name;
285 	req->canonical_path = canonical_path;
286 	req->out.argvar = 1;
287 	fuse_request_send(fc, req);
288 	err = req->out.h.error;
289 	fuse_put_request(fc, req);
290 	free_page((unsigned long)path_name);
291 	if (!err)
292 		return;
293 default_path:
294 	canonical_path->dentry = path->dentry;
295 	canonical_path->mnt = path->mnt;
296 	path_get(canonical_path);
297 }
298 
invalid_nodeid(u64 nodeid)299 static int invalid_nodeid(u64 nodeid)
300 {
301 	return !nodeid || nodeid == FUSE_ROOT_ID;
302 }
303 
304 const struct dentry_operations fuse_dentry_operations = {
305 	.d_revalidate	= fuse_dentry_revalidate,
306 	.d_canonical_path = fuse_dentry_canonical_path,
307 };
308 
fuse_valid_type(int m)309 int fuse_valid_type(int m)
310 {
311 	return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
312 		S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
313 }
314 
315 /*
316  * Add a directory inode to a dentry, ensuring that no other dentry
317  * refers to this inode.  Called with fc->inst_mutex.
318  */
fuse_d_add_directory(struct dentry * entry,struct inode * inode)319 static struct dentry *fuse_d_add_directory(struct dentry *entry,
320 					   struct inode *inode)
321 {
322 	struct dentry *alias = d_find_alias(inode);
323 	if (alias && !(alias->d_flags & DCACHE_DISCONNECTED)) {
324 		/* This tries to shrink the subtree below alias */
325 		fuse_invalidate_entry(alias);
326 		dput(alias);
327 		if (!hlist_empty(&inode->i_dentry))
328 			return ERR_PTR(-EBUSY);
329 	} else {
330 		dput(alias);
331 	}
332 	return d_splice_alias(inode, entry);
333 }
334 
fuse_lookup_name(struct super_block * sb,u64 nodeid,struct qstr * name,struct fuse_entry_out * outarg,struct inode ** inode)335 int fuse_lookup_name(struct super_block *sb, u64 nodeid, struct qstr *name,
336 		     struct fuse_entry_out *outarg, struct inode **inode)
337 {
338 	struct fuse_conn *fc = get_fuse_conn_super(sb);
339 	struct fuse_req *req;
340 	struct fuse_forget_link *forget;
341 	u64 attr_version;
342 	int err;
343 
344 	*inode = NULL;
345 	err = -ENAMETOOLONG;
346 	if (name->len > FUSE_NAME_MAX)
347 		goto out;
348 
349 	req = fuse_get_req_nopages(fc);
350 	err = PTR_ERR(req);
351 	if (IS_ERR(req))
352 		goto out;
353 
354 	forget = fuse_alloc_forget();
355 	err = -ENOMEM;
356 	if (!forget) {
357 		fuse_put_request(fc, req);
358 		goto out;
359 	}
360 
361 	attr_version = fuse_get_attr_version(fc);
362 
363 	fuse_lookup_init(fc, req, nodeid, name, outarg);
364 	fuse_request_send(fc, req);
365 	err = req->out.h.error;
366 	fuse_put_request(fc, req);
367 	/* Zero nodeid is same as -ENOENT, but with valid timeout */
368 	if (err || !outarg->nodeid)
369 		goto out_put_forget;
370 
371 	err = -EIO;
372 	if (!outarg->nodeid)
373 		goto out_put_forget;
374 	if (!fuse_valid_type(outarg->attr.mode))
375 		goto out_put_forget;
376 
377 	*inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
378 			   &outarg->attr, entry_attr_timeout(outarg),
379 			   attr_version);
380 	err = -ENOMEM;
381 	if (!*inode) {
382 		fuse_queue_forget(fc, forget, outarg->nodeid, 1);
383 		goto out;
384 	}
385 	err = 0;
386 
387  out_put_forget:
388 	kfree(forget);
389  out:
390 	return err;
391 }
392 
fuse_lookup(struct inode * dir,struct dentry * entry,unsigned int flags)393 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
394 				  unsigned int flags)
395 {
396 	int err;
397 	struct fuse_entry_out outarg;
398 	struct inode *inode;
399 	struct dentry *newent;
400 	struct fuse_conn *fc = get_fuse_conn(dir);
401 	bool outarg_valid = true;
402 
403 	err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
404 			       &outarg, &inode);
405 	if (err == -ENOENT) {
406 		outarg_valid = false;
407 		err = 0;
408 	}
409 	if (err)
410 		goto out_err;
411 
412 	err = -EIO;
413 	if (inode && get_node_id(inode) == FUSE_ROOT_ID)
414 		goto out_iput;
415 
416 	if (inode && S_ISDIR(inode->i_mode)) {
417 		mutex_lock(&fc->inst_mutex);
418 		newent = fuse_d_add_directory(entry, inode);
419 		mutex_unlock(&fc->inst_mutex);
420 		err = PTR_ERR(newent);
421 		if (IS_ERR(newent))
422 			goto out_iput;
423 	} else {
424 		newent = d_splice_alias(inode, entry);
425 	}
426 
427 	entry = newent ? newent : entry;
428 	if (outarg_valid)
429 		fuse_change_entry_timeout(entry, &outarg);
430 	else
431 		fuse_invalidate_entry_cache(entry);
432 
433 	fuse_advise_use_readdirplus(dir);
434 	return newent;
435 
436  out_iput:
437 	iput(inode);
438  out_err:
439 	return ERR_PTR(err);
440 }
441 
442 /*
443  * Atomic create+open operation
444  *
445  * If the filesystem doesn't support this, then fall back to separate
446  * 'mknod' + 'open' requests.
447  */
fuse_create_open(struct inode * dir,struct dentry * entry,struct file * file,unsigned flags,umode_t mode,int * opened)448 static int fuse_create_open(struct inode *dir, struct dentry *entry,
449 			    struct file *file, unsigned flags,
450 			    umode_t mode, int *opened)
451 {
452 	int err;
453 	struct inode *inode;
454 	struct fuse_conn *fc = get_fuse_conn(dir);
455 	struct fuse_req *req;
456 	struct fuse_forget_link *forget;
457 	struct fuse_create_in inarg;
458 	struct fuse_open_out outopen;
459 	struct fuse_entry_out outentry;
460 	struct fuse_file *ff;
461 
462 	/* Userspace expects S_IFREG in create mode */
463 	BUG_ON((mode & S_IFMT) != S_IFREG);
464 
465 	forget = fuse_alloc_forget();
466 	err = -ENOMEM;
467 	if (!forget)
468 		goto out_err;
469 
470 	req = fuse_get_req_nopages(fc);
471 	err = PTR_ERR(req);
472 	if (IS_ERR(req))
473 		goto out_put_forget_req;
474 
475 	err = -ENOMEM;
476 	ff = fuse_file_alloc(fc);
477 	if (!ff)
478 		goto out_put_request;
479 
480 	if (!fc->dont_mask)
481 		mode &= ~current_umask();
482 
483 	flags &= ~O_NOCTTY;
484 	memset(&inarg, 0, sizeof(inarg));
485 	memset(&outentry, 0, sizeof(outentry));
486 	inarg.flags = flags;
487 	inarg.mode = mode;
488 	inarg.umask = current_umask();
489 	req->in.h.opcode = FUSE_CREATE;
490 	req->in.h.nodeid = get_node_id(dir);
491 	req->in.numargs = 2;
492 	req->in.args[0].size = fc->minor < 12 ? sizeof(struct fuse_open_in) :
493 						sizeof(inarg);
494 	req->in.args[0].value = &inarg;
495 	req->in.args[1].size = entry->d_name.len + 1;
496 	req->in.args[1].value = entry->d_name.name;
497 	req->out.numargs = 2;
498 	if (fc->minor < 9)
499 		req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
500 	else
501 		req->out.args[0].size = sizeof(outentry);
502 	req->out.args[0].value = &outentry;
503 	req->out.args[1].size = sizeof(outopen);
504 	req->out.args[1].value = &outopen;
505 	fuse_request_send(fc, req);
506 	err = req->out.h.error;
507 	if (err)
508 		goto out_free_ff;
509 
510 	err = -EIO;
511 	if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
512 		goto out_free_ff;
513 
514 	fuse_put_request(fc, req);
515 	ff->fh = outopen.fh;
516 	ff->nodeid = outentry.nodeid;
517 	ff->open_flags = outopen.open_flags;
518 	inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
519 			  &outentry.attr, entry_attr_timeout(&outentry), 0);
520 	if (!inode) {
521 		flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
522 		fuse_sync_release(ff, flags);
523 		fuse_queue_forget(fc, forget, outentry.nodeid, 1);
524 		err = -ENOMEM;
525 		goto out_err;
526 	}
527 	kfree(forget);
528 	d_instantiate(entry, inode);
529 	fuse_change_entry_timeout(entry, &outentry);
530 	fuse_invalidate_attr(dir);
531 	err = finish_open(file, entry, generic_file_open, opened);
532 	if (err) {
533 		fuse_sync_release(ff, flags);
534 	} else {
535 		file->private_data = fuse_file_get(ff);
536 		fuse_finish_open(inode, file);
537 	}
538 	return err;
539 
540 out_free_ff:
541 	fuse_file_free(ff);
542 out_put_request:
543 	fuse_put_request(fc, req);
544 out_put_forget_req:
545 	kfree(forget);
546 out_err:
547 	return err;
548 }
549 
550 static int fuse_mknod(struct inode *, struct dentry *, umode_t, dev_t);
fuse_atomic_open(struct inode * dir,struct dentry * entry,struct file * file,unsigned flags,umode_t mode,int * opened)551 static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
552 			    struct file *file, unsigned flags,
553 			    umode_t mode, int *opened)
554 {
555 	int err;
556 	struct fuse_conn *fc = get_fuse_conn(dir);
557 	struct dentry *res = NULL;
558 
559 	if (d_unhashed(entry)) {
560 		res = fuse_lookup(dir, entry, 0);
561 		if (IS_ERR(res))
562 			return PTR_ERR(res);
563 
564 		if (res)
565 			entry = res;
566 	}
567 
568 	if (!(flags & O_CREAT) || entry->d_inode)
569 		goto no_open;
570 
571 	/* Only creates */
572 	*opened |= FILE_CREATED;
573 
574 	if (fc->no_create)
575 		goto mknod;
576 
577 	err = fuse_create_open(dir, entry, file, flags, mode, opened);
578 	if (err == -ENOSYS) {
579 		fc->no_create = 1;
580 		goto mknod;
581 	}
582 out_dput:
583 	dput(res);
584 	return err;
585 
586 mknod:
587 	err = fuse_mknod(dir, entry, mode, 0);
588 	if (err)
589 		goto out_dput;
590 no_open:
591 	return finish_no_open(file, res);
592 }
593 
594 /*
595  * Code shared between mknod, mkdir, symlink and link
596  */
create_new_entry(struct fuse_conn * fc,struct fuse_req * req,struct inode * dir,struct dentry * entry,umode_t mode)597 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
598 			    struct inode *dir, struct dentry *entry,
599 			    umode_t mode)
600 {
601 	struct fuse_entry_out outarg;
602 	struct inode *inode;
603 	int err;
604 	struct fuse_forget_link *forget;
605 
606 	forget = fuse_alloc_forget();
607 	if (!forget) {
608 		fuse_put_request(fc, req);
609 		return -ENOMEM;
610 	}
611 
612 	memset(&outarg, 0, sizeof(outarg));
613 	req->in.h.nodeid = get_node_id(dir);
614 	req->out.numargs = 1;
615 	if (fc->minor < 9)
616 		req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
617 	else
618 		req->out.args[0].size = sizeof(outarg);
619 	req->out.args[0].value = &outarg;
620 	fuse_request_send(fc, req);
621 	err = req->out.h.error;
622 	fuse_put_request(fc, req);
623 	if (err)
624 		goto out_put_forget_req;
625 
626 	err = -EIO;
627 	if (invalid_nodeid(outarg.nodeid))
628 		goto out_put_forget_req;
629 
630 	if ((outarg.attr.mode ^ mode) & S_IFMT)
631 		goto out_put_forget_req;
632 
633 	inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
634 			  &outarg.attr, entry_attr_timeout(&outarg), 0);
635 	if (!inode) {
636 		fuse_queue_forget(fc, forget, outarg.nodeid, 1);
637 		return -ENOMEM;
638 	}
639 	kfree(forget);
640 
641 	if (S_ISDIR(inode->i_mode)) {
642 		struct dentry *alias;
643 		mutex_lock(&fc->inst_mutex);
644 		alias = d_find_alias(inode);
645 		if (alias) {
646 			/* New directory must have moved since mkdir */
647 			mutex_unlock(&fc->inst_mutex);
648 			dput(alias);
649 			iput(inode);
650 			return -EBUSY;
651 		}
652 		d_instantiate(entry, inode);
653 		mutex_unlock(&fc->inst_mutex);
654 	} else
655 		d_instantiate(entry, inode);
656 
657 	fuse_change_entry_timeout(entry, &outarg);
658 	fuse_invalidate_attr(dir);
659 	return 0;
660 
661  out_put_forget_req:
662 	kfree(forget);
663 	return err;
664 }
665 
fuse_mknod(struct inode * dir,struct dentry * entry,umode_t mode,dev_t rdev)666 static int fuse_mknod(struct inode *dir, struct dentry *entry, umode_t mode,
667 		      dev_t rdev)
668 {
669 	struct fuse_mknod_in inarg;
670 	struct fuse_conn *fc = get_fuse_conn(dir);
671 	struct fuse_req *req = fuse_get_req_nopages(fc);
672 	if (IS_ERR(req))
673 		return PTR_ERR(req);
674 
675 	if (!fc->dont_mask)
676 		mode &= ~current_umask();
677 
678 	memset(&inarg, 0, sizeof(inarg));
679 	inarg.mode = mode;
680 	inarg.rdev = new_encode_dev(rdev);
681 	inarg.umask = current_umask();
682 	req->in.h.opcode = FUSE_MKNOD;
683 	req->in.numargs = 2;
684 	req->in.args[0].size = fc->minor < 12 ? FUSE_COMPAT_MKNOD_IN_SIZE :
685 						sizeof(inarg);
686 	req->in.args[0].value = &inarg;
687 	req->in.args[1].size = entry->d_name.len + 1;
688 	req->in.args[1].value = entry->d_name.name;
689 	return create_new_entry(fc, req, dir, entry, mode);
690 }
691 
fuse_create(struct inode * dir,struct dentry * entry,umode_t mode,bool excl)692 static int fuse_create(struct inode *dir, struct dentry *entry, umode_t mode,
693 		       bool excl)
694 {
695 	return fuse_mknod(dir, entry, mode, 0);
696 }
697 
fuse_mkdir(struct inode * dir,struct dentry * entry,umode_t mode)698 static int fuse_mkdir(struct inode *dir, struct dentry *entry, umode_t mode)
699 {
700 	struct fuse_mkdir_in inarg;
701 	struct fuse_conn *fc = get_fuse_conn(dir);
702 	struct fuse_req *req = fuse_get_req_nopages(fc);
703 	if (IS_ERR(req))
704 		return PTR_ERR(req);
705 
706 	if (!fc->dont_mask)
707 		mode &= ~current_umask();
708 
709 	memset(&inarg, 0, sizeof(inarg));
710 	inarg.mode = mode;
711 	inarg.umask = current_umask();
712 	req->in.h.opcode = FUSE_MKDIR;
713 	req->in.numargs = 2;
714 	req->in.args[0].size = sizeof(inarg);
715 	req->in.args[0].value = &inarg;
716 	req->in.args[1].size = entry->d_name.len + 1;
717 	req->in.args[1].value = entry->d_name.name;
718 	return create_new_entry(fc, req, dir, entry, S_IFDIR);
719 }
720 
fuse_symlink(struct inode * dir,struct dentry * entry,const char * link)721 static int fuse_symlink(struct inode *dir, struct dentry *entry,
722 			const char *link)
723 {
724 	struct fuse_conn *fc = get_fuse_conn(dir);
725 	unsigned len = strlen(link) + 1;
726 	struct fuse_req *req = fuse_get_req_nopages(fc);
727 	if (IS_ERR(req))
728 		return PTR_ERR(req);
729 
730 	req->in.h.opcode = FUSE_SYMLINK;
731 	req->in.numargs = 2;
732 	req->in.args[0].size = entry->d_name.len + 1;
733 	req->in.args[0].value = entry->d_name.name;
734 	req->in.args[1].size = len;
735 	req->in.args[1].value = link;
736 	return create_new_entry(fc, req, dir, entry, S_IFLNK);
737 }
738 
fuse_unlink(struct inode * dir,struct dentry * entry)739 static int fuse_unlink(struct inode *dir, struct dentry *entry)
740 {
741 	int err;
742 	struct fuse_conn *fc = get_fuse_conn(dir);
743 	struct fuse_req *req = fuse_get_req_nopages(fc);
744 	if (IS_ERR(req))
745 		return PTR_ERR(req);
746 
747 	req->in.h.opcode = FUSE_UNLINK;
748 	req->in.h.nodeid = get_node_id(dir);
749 	req->in.numargs = 1;
750 	req->in.args[0].size = entry->d_name.len + 1;
751 	req->in.args[0].value = entry->d_name.name;
752 	fuse_request_send(fc, req);
753 	err = req->out.h.error;
754 	fuse_put_request(fc, req);
755 	if (!err) {
756 		struct inode *inode = entry->d_inode;
757 		struct fuse_inode *fi = get_fuse_inode(inode);
758 
759 		spin_lock(&fc->lock);
760 		fi->attr_version = ++fc->attr_version;
761 		/*
762 		 * If i_nlink == 0 then unlink doesn't make sense, yet this can
763 		 * happen if userspace filesystem is careless.  It would be
764 		 * difficult to enforce correct nlink usage so just ignore this
765 		 * condition here
766 		 */
767 		if (inode->i_nlink > 0)
768 			drop_nlink(inode);
769 		spin_unlock(&fc->lock);
770 		fuse_invalidate_attr(inode);
771 		fuse_invalidate_attr(dir);
772 		fuse_invalidate_entry_cache(entry);
773 	} else if (err == -EINTR)
774 		fuse_invalidate_entry(entry);
775 	return err;
776 }
777 
fuse_rmdir(struct inode * dir,struct dentry * entry)778 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
779 {
780 	int err;
781 	struct fuse_conn *fc = get_fuse_conn(dir);
782 	struct fuse_req *req = fuse_get_req_nopages(fc);
783 	if (IS_ERR(req))
784 		return PTR_ERR(req);
785 
786 	req->in.h.opcode = FUSE_RMDIR;
787 	req->in.h.nodeid = get_node_id(dir);
788 	req->in.numargs = 1;
789 	req->in.args[0].size = entry->d_name.len + 1;
790 	req->in.args[0].value = entry->d_name.name;
791 	fuse_request_send(fc, req);
792 	err = req->out.h.error;
793 	fuse_put_request(fc, req);
794 	if (!err) {
795 		clear_nlink(entry->d_inode);
796 		fuse_invalidate_attr(dir);
797 		fuse_invalidate_entry_cache(entry);
798 	} else if (err == -EINTR)
799 		fuse_invalidate_entry(entry);
800 	return err;
801 }
802 
fuse_rename(struct inode * olddir,struct dentry * oldent,struct inode * newdir,struct dentry * newent)803 static int fuse_rename(struct inode *olddir, struct dentry *oldent,
804 		       struct inode *newdir, struct dentry *newent)
805 {
806 	int err;
807 	struct fuse_rename_in inarg;
808 	struct fuse_conn *fc = get_fuse_conn(olddir);
809 	struct fuse_req *req = fuse_get_req_nopages(fc);
810 
811 	if (IS_ERR(req))
812 		return PTR_ERR(req);
813 
814 	memset(&inarg, 0, sizeof(inarg));
815 	inarg.newdir = get_node_id(newdir);
816 	req->in.h.opcode = FUSE_RENAME;
817 	req->in.h.nodeid = get_node_id(olddir);
818 	req->in.numargs = 3;
819 	req->in.args[0].size = sizeof(inarg);
820 	req->in.args[0].value = &inarg;
821 	req->in.args[1].size = oldent->d_name.len + 1;
822 	req->in.args[1].value = oldent->d_name.name;
823 	req->in.args[2].size = newent->d_name.len + 1;
824 	req->in.args[2].value = newent->d_name.name;
825 	fuse_request_send(fc, req);
826 	err = req->out.h.error;
827 	fuse_put_request(fc, req);
828 	if (!err) {
829 		/* ctime changes */
830 		fuse_invalidate_attr(oldent->d_inode);
831 
832 		fuse_invalidate_attr(olddir);
833 		if (olddir != newdir)
834 			fuse_invalidate_attr(newdir);
835 
836 		/* newent will end up negative */
837 		if (newent->d_inode) {
838 			fuse_invalidate_attr(newent->d_inode);
839 			fuse_invalidate_entry_cache(newent);
840 		}
841 	} else if (err == -EINTR) {
842 		/* If request was interrupted, DEITY only knows if the
843 		   rename actually took place.  If the invalidation
844 		   fails (e.g. some process has CWD under the renamed
845 		   directory), then there can be inconsistency between
846 		   the dcache and the real filesystem.  Tough luck. */
847 		fuse_invalidate_entry(oldent);
848 		if (newent->d_inode)
849 			fuse_invalidate_entry(newent);
850 	}
851 
852 	return err;
853 }
854 
fuse_link(struct dentry * entry,struct inode * newdir,struct dentry * newent)855 static int fuse_link(struct dentry *entry, struct inode *newdir,
856 		     struct dentry *newent)
857 {
858 	int err;
859 	struct fuse_link_in inarg;
860 	struct inode *inode = entry->d_inode;
861 	struct fuse_conn *fc = get_fuse_conn(inode);
862 	struct fuse_req *req = fuse_get_req_nopages(fc);
863 	if (IS_ERR(req))
864 		return PTR_ERR(req);
865 
866 	memset(&inarg, 0, sizeof(inarg));
867 	inarg.oldnodeid = get_node_id(inode);
868 	req->in.h.opcode = FUSE_LINK;
869 	req->in.numargs = 2;
870 	req->in.args[0].size = sizeof(inarg);
871 	req->in.args[0].value = &inarg;
872 	req->in.args[1].size = newent->d_name.len + 1;
873 	req->in.args[1].value = newent->d_name.name;
874 	err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
875 	/* Contrary to "normal" filesystems it can happen that link
876 	   makes two "logical" inodes point to the same "physical"
877 	   inode.  We invalidate the attributes of the old one, so it
878 	   will reflect changes in the backing inode (link count,
879 	   etc.)
880 	*/
881 	if (!err) {
882 		struct fuse_inode *fi = get_fuse_inode(inode);
883 
884 		spin_lock(&fc->lock);
885 		fi->attr_version = ++fc->attr_version;
886 		inc_nlink(inode);
887 		spin_unlock(&fc->lock);
888 		fuse_invalidate_attr(inode);
889 	} else if (err == -EINTR) {
890 		fuse_invalidate_attr(inode);
891 	}
892 	return err;
893 }
894 
fuse_fillattr(struct inode * inode,struct fuse_attr * attr,struct kstat * stat)895 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
896 			  struct kstat *stat)
897 {
898 	unsigned int blkbits;
899 
900 	stat->dev = inode->i_sb->s_dev;
901 	stat->ino = attr->ino;
902 	stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
903 	stat->nlink = attr->nlink;
904 	stat->uid = make_kuid(&init_user_ns, attr->uid);
905 	stat->gid = make_kgid(&init_user_ns, attr->gid);
906 	stat->rdev = inode->i_rdev;
907 	stat->atime.tv_sec = attr->atime;
908 	stat->atime.tv_nsec = attr->atimensec;
909 	stat->mtime.tv_sec = attr->mtime;
910 	stat->mtime.tv_nsec = attr->mtimensec;
911 	stat->ctime.tv_sec = attr->ctime;
912 	stat->ctime.tv_nsec = attr->ctimensec;
913 	stat->size = attr->size;
914 	stat->blocks = attr->blocks;
915 
916 	if (attr->blksize != 0)
917 		blkbits = ilog2(attr->blksize);
918 	else
919 		blkbits = inode->i_sb->s_blocksize_bits;
920 
921 	stat->blksize = 1 << blkbits;
922 }
923 
fuse_do_getattr(struct inode * inode,struct kstat * stat,struct file * file)924 static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
925 			   struct file *file)
926 {
927 	int err;
928 	struct fuse_getattr_in inarg;
929 	struct fuse_attr_out outarg;
930 	struct fuse_conn *fc = get_fuse_conn(inode);
931 	struct fuse_req *req;
932 	u64 attr_version;
933 
934 	req = fuse_get_req_nopages(fc);
935 	if (IS_ERR(req))
936 		return PTR_ERR(req);
937 
938 	attr_version = fuse_get_attr_version(fc);
939 
940 	memset(&inarg, 0, sizeof(inarg));
941 	memset(&outarg, 0, sizeof(outarg));
942 	/* Directories have separate file-handle space */
943 	if (file && S_ISREG(inode->i_mode)) {
944 		struct fuse_file *ff = file->private_data;
945 
946 		inarg.getattr_flags |= FUSE_GETATTR_FH;
947 		inarg.fh = ff->fh;
948 	}
949 	req->in.h.opcode = FUSE_GETATTR;
950 	req->in.h.nodeid = get_node_id(inode);
951 	req->in.numargs = 1;
952 	req->in.args[0].size = sizeof(inarg);
953 	req->in.args[0].value = &inarg;
954 	req->out.numargs = 1;
955 	if (fc->minor < 9)
956 		req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
957 	else
958 		req->out.args[0].size = sizeof(outarg);
959 	req->out.args[0].value = &outarg;
960 	fuse_request_send(fc, req);
961 	err = req->out.h.error;
962 	fuse_put_request(fc, req);
963 	if (!err) {
964 		if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
965 			make_bad_inode(inode);
966 			err = -EIO;
967 		} else {
968 			fuse_change_attributes(inode, &outarg.attr,
969 					       attr_timeout(&outarg),
970 					       attr_version);
971 			if (stat)
972 				fuse_fillattr(inode, &outarg.attr, stat);
973 		}
974 	}
975 	return err;
976 }
977 
fuse_update_attributes(struct inode * inode,struct kstat * stat,struct file * file,bool * refreshed)978 int fuse_update_attributes(struct inode *inode, struct kstat *stat,
979 			   struct file *file, bool *refreshed)
980 {
981 	struct fuse_inode *fi = get_fuse_inode(inode);
982 	int err;
983 	bool r;
984 
985 	if (fi->i_time < get_jiffies_64()) {
986 		r = true;
987 		err = fuse_do_getattr(inode, stat, file);
988 	} else {
989 		r = false;
990 		err = 0;
991 		if (stat) {
992 			generic_fillattr(inode, stat);
993 			stat->mode = fi->orig_i_mode;
994 			stat->ino = fi->orig_ino;
995 		}
996 	}
997 
998 	if (refreshed != NULL)
999 		*refreshed = r;
1000 
1001 	return err;
1002 }
1003 
fuse_reverse_inval_entry(struct super_block * sb,u64 parent_nodeid,u64 child_nodeid,struct qstr * name)1004 int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid,
1005 			     u64 child_nodeid, struct qstr *name)
1006 {
1007 	int err = -ENOTDIR;
1008 	struct inode *parent;
1009 	struct dentry *dir;
1010 	struct dentry *entry;
1011 
1012 	parent = ilookup5(sb, parent_nodeid, fuse_inode_eq, &parent_nodeid);
1013 	if (!parent)
1014 		return -ENOENT;
1015 
1016 	mutex_lock(&parent->i_mutex);
1017 	if (!S_ISDIR(parent->i_mode))
1018 		goto unlock;
1019 
1020 	err = -ENOENT;
1021 	dir = d_find_alias(parent);
1022 	if (!dir)
1023 		goto unlock;
1024 
1025 	entry = d_lookup(dir, name);
1026 	dput(dir);
1027 	if (!entry)
1028 		goto unlock;
1029 
1030 	fuse_invalidate_attr(parent);
1031 	fuse_invalidate_entry(entry);
1032 
1033 	if (child_nodeid != 0 && entry->d_inode) {
1034 		mutex_lock(&entry->d_inode->i_mutex);
1035 		if (get_node_id(entry->d_inode) != child_nodeid) {
1036 			err = -ENOENT;
1037 			goto badentry;
1038 		}
1039 		if (d_mountpoint(entry)) {
1040 			err = -EBUSY;
1041 			goto badentry;
1042 		}
1043 		if (S_ISDIR(entry->d_inode->i_mode)) {
1044 			shrink_dcache_parent(entry);
1045 			if (!simple_empty(entry)) {
1046 				err = -ENOTEMPTY;
1047 				goto badentry;
1048 			}
1049 			entry->d_inode->i_flags |= S_DEAD;
1050 		}
1051 		dont_mount(entry);
1052 		clear_nlink(entry->d_inode);
1053 		err = 0;
1054  badentry:
1055 		mutex_unlock(&entry->d_inode->i_mutex);
1056 		if (!err)
1057 			d_delete(entry);
1058 	} else {
1059 		err = 0;
1060 	}
1061 	dput(entry);
1062 
1063  unlock:
1064 	mutex_unlock(&parent->i_mutex);
1065 	iput(parent);
1066 	return err;
1067 }
1068 
1069 /*
1070  * Calling into a user-controlled filesystem gives the filesystem
1071  * daemon ptrace-like capabilities over the current process.  This
1072  * means, that the filesystem daemon is able to record the exact
1073  * filesystem operations performed, and can also control the behavior
1074  * of the requester process in otherwise impossible ways.  For example
1075  * it can delay the operation for arbitrary length of time allowing
1076  * DoS against the requester.
1077  *
1078  * For this reason only those processes can call into the filesystem,
1079  * for which the owner of the mount has ptrace privilege.  This
1080  * excludes processes started by other users, suid or sgid processes.
1081  */
fuse_allow_current_process(struct fuse_conn * fc)1082 int fuse_allow_current_process(struct fuse_conn *fc)
1083 {
1084 	const struct cred *cred;
1085 
1086 	if (fc->flags & FUSE_ALLOW_OTHER)
1087 		return 1;
1088 
1089 	cred = current_cred();
1090 	if (uid_eq(cred->euid, fc->user_id) &&
1091 	    uid_eq(cred->suid, fc->user_id) &&
1092 	    uid_eq(cred->uid,  fc->user_id) &&
1093 	    gid_eq(cred->egid, fc->group_id) &&
1094 	    gid_eq(cred->sgid, fc->group_id) &&
1095 	    gid_eq(cred->gid,  fc->group_id))
1096 		return 1;
1097 
1098 	return 0;
1099 }
1100 
fuse_access(struct inode * inode,int mask)1101 static int fuse_access(struct inode *inode, int mask)
1102 {
1103 	struct fuse_conn *fc = get_fuse_conn(inode);
1104 	struct fuse_req *req;
1105 	struct fuse_access_in inarg;
1106 	int err;
1107 
1108 	if (fc->no_access)
1109 		return 0;
1110 
1111 	req = fuse_get_req_nopages(fc);
1112 	if (IS_ERR(req))
1113 		return PTR_ERR(req);
1114 
1115 	memset(&inarg, 0, sizeof(inarg));
1116 	inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
1117 	req->in.h.opcode = FUSE_ACCESS;
1118 	req->in.h.nodeid = get_node_id(inode);
1119 	req->in.numargs = 1;
1120 	req->in.args[0].size = sizeof(inarg);
1121 	req->in.args[0].value = &inarg;
1122 	fuse_request_send(fc, req);
1123 	err = req->out.h.error;
1124 	fuse_put_request(fc, req);
1125 	if (err == -ENOSYS) {
1126 		fc->no_access = 1;
1127 		err = 0;
1128 	}
1129 	return err;
1130 }
1131 
fuse_perm_getattr(struct inode * inode,int mask)1132 static int fuse_perm_getattr(struct inode *inode, int mask)
1133 {
1134 	if (mask & MAY_NOT_BLOCK)
1135 		return -ECHILD;
1136 
1137 	return fuse_do_getattr(inode, NULL, NULL);
1138 }
1139 
1140 /*
1141  * Check permission.  The two basic access models of FUSE are:
1142  *
1143  * 1) Local access checking ('default_permissions' mount option) based
1144  * on file mode.  This is the plain old disk filesystem permission
1145  * modell.
1146  *
1147  * 2) "Remote" access checking, where server is responsible for
1148  * checking permission in each inode operation.  An exception to this
1149  * is if ->permission() was invoked from sys_access() in which case an
1150  * access request is sent.  Execute permission is still checked
1151  * locally based on file mode.
1152  */
fuse_permission(struct inode * inode,int mask)1153 static int fuse_permission(struct inode *inode, int mask)
1154 {
1155 	struct fuse_conn *fc = get_fuse_conn(inode);
1156 	bool refreshed = false;
1157 	int err = 0;
1158 
1159 	if (!fuse_allow_current_process(fc))
1160 		return -EACCES;
1161 
1162 	/*
1163 	 * If attributes are needed, refresh them before proceeding
1164 	 */
1165 	if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
1166 	    ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
1167 		struct fuse_inode *fi = get_fuse_inode(inode);
1168 
1169 		if (fi->i_time < get_jiffies_64()) {
1170 			refreshed = true;
1171 
1172 			err = fuse_perm_getattr(inode, mask);
1173 			if (err)
1174 				return err;
1175 		}
1176 	}
1177 
1178 	if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1179 		err = generic_permission(inode, mask);
1180 
1181 		/* If permission is denied, try to refresh file
1182 		   attributes.  This is also needed, because the root
1183 		   node will at first have no permissions */
1184 		if (err == -EACCES && !refreshed) {
1185 			err = fuse_perm_getattr(inode, mask);
1186 			if (!err)
1187 				err = generic_permission(inode, mask);
1188 		}
1189 
1190 		/* Note: the opposite of the above test does not
1191 		   exist.  So if permissions are revoked this won't be
1192 		   noticed immediately, only after the attribute
1193 		   timeout has expired */
1194 	} else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
1195 		if (mask & MAY_NOT_BLOCK)
1196 			return -ECHILD;
1197 
1198 		err = fuse_access(inode, mask);
1199 	} else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1200 		if (!(inode->i_mode & S_IXUGO)) {
1201 			if (refreshed)
1202 				return -EACCES;
1203 
1204 			err = fuse_perm_getattr(inode, mask);
1205 			if (!err && !(inode->i_mode & S_IXUGO))
1206 				return -EACCES;
1207 		}
1208 	}
1209 	return err;
1210 }
1211 
parse_dirfile(char * buf,size_t nbytes,struct file * file,void * dstbuf,filldir_t filldir)1212 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
1213 			 void *dstbuf, filldir_t filldir)
1214 {
1215 	while (nbytes >= FUSE_NAME_OFFSET) {
1216 		struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
1217 		size_t reclen = FUSE_DIRENT_SIZE(dirent);
1218 		int over;
1219 		if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1220 			return -EIO;
1221 		if (reclen > nbytes)
1222 			break;
1223 
1224 		over = filldir(dstbuf, dirent->name, dirent->namelen,
1225 			       file->f_pos, dirent->ino, dirent->type);
1226 		if (over)
1227 			break;
1228 
1229 		buf += reclen;
1230 		nbytes -= reclen;
1231 		file->f_pos = dirent->off;
1232 	}
1233 
1234 	return 0;
1235 }
1236 
fuse_direntplus_link(struct file * file,struct fuse_direntplus * direntplus,u64 attr_version)1237 static int fuse_direntplus_link(struct file *file,
1238 				struct fuse_direntplus *direntplus,
1239 				u64 attr_version)
1240 {
1241 	int err;
1242 	struct fuse_entry_out *o = &direntplus->entry_out;
1243 	struct fuse_dirent *dirent = &direntplus->dirent;
1244 	struct dentry *parent = file->f_path.dentry;
1245 	struct qstr name = QSTR_INIT(dirent->name, dirent->namelen);
1246 	struct dentry *dentry;
1247 	struct dentry *alias;
1248 	struct inode *dir = parent->d_inode;
1249 	struct fuse_conn *fc;
1250 	struct inode *inode;
1251 
1252 	if (!o->nodeid) {
1253 		/*
1254 		 * Unlike in the case of fuse_lookup, zero nodeid does not mean
1255 		 * ENOENT. Instead, it only means the userspace filesystem did
1256 		 * not want to return attributes/handle for this entry.
1257 		 *
1258 		 * So do nothing.
1259 		 */
1260 		return 0;
1261 	}
1262 
1263 	if (name.name[0] == '.') {
1264 		/*
1265 		 * We could potentially refresh the attributes of the directory
1266 		 * and its parent?
1267 		 */
1268 		if (name.len == 1)
1269 			return 0;
1270 		if (name.name[1] == '.' && name.len == 2)
1271 			return 0;
1272 	}
1273 	fc = get_fuse_conn(dir);
1274 
1275 	name.hash = full_name_hash(name.name, name.len);
1276 	dentry = d_lookup(parent, &name);
1277 	if (dentry && dentry->d_inode) {
1278 		inode = dentry->d_inode;
1279 		if (get_node_id(inode) == o->nodeid) {
1280 			struct fuse_inode *fi;
1281 			fi = get_fuse_inode(inode);
1282 			spin_lock(&fc->lock);
1283 			fi->nlookup++;
1284 			spin_unlock(&fc->lock);
1285 
1286 			/*
1287 			 * The other branch to 'found' comes via fuse_iget()
1288 			 * which bumps nlookup inside
1289 			 */
1290 			goto found;
1291 		}
1292 		err = d_invalidate(dentry);
1293 		if (err)
1294 			goto out;
1295 		dput(dentry);
1296 		dentry = NULL;
1297 	}
1298 
1299 	dentry = d_alloc(parent, &name);
1300 	err = -ENOMEM;
1301 	if (!dentry)
1302 		goto out;
1303 
1304 	inode = fuse_iget(dir->i_sb, o->nodeid, o->generation,
1305 			  &o->attr, entry_attr_timeout(o), attr_version);
1306 	if (!inode)
1307 		goto out;
1308 
1309 	alias = d_materialise_unique(dentry, inode);
1310 	err = PTR_ERR(alias);
1311 	if (IS_ERR(alias))
1312 		goto out;
1313 	if (alias) {
1314 		dput(dentry);
1315 		dentry = alias;
1316 	}
1317 
1318 found:
1319 	fuse_change_attributes(inode, &o->attr, entry_attr_timeout(o),
1320 			       attr_version);
1321 
1322 	fuse_change_entry_timeout(dentry, o);
1323 
1324 	err = 0;
1325 out:
1326 	if (dentry)
1327 		dput(dentry);
1328 	return err;
1329 }
1330 
parse_dirplusfile(char * buf,size_t nbytes,struct file * file,void * dstbuf,filldir_t filldir,u64 attr_version)1331 static int parse_dirplusfile(char *buf, size_t nbytes, struct file *file,
1332 			     void *dstbuf, filldir_t filldir, u64 attr_version)
1333 {
1334 	struct fuse_direntplus *direntplus;
1335 	struct fuse_dirent *dirent;
1336 	size_t reclen;
1337 	int over = 0;
1338 	int ret;
1339 
1340 	while (nbytes >= FUSE_NAME_OFFSET_DIRENTPLUS) {
1341 		direntplus = (struct fuse_direntplus *) buf;
1342 		dirent = &direntplus->dirent;
1343 		reclen = FUSE_DIRENTPLUS_SIZE(direntplus);
1344 
1345 		if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1346 			return -EIO;
1347 		if (reclen > nbytes)
1348 			break;
1349 
1350 		if (!over) {
1351 			/* We fill entries into dstbuf only as much as
1352 			   it can hold. But we still continue iterating
1353 			   over remaining entries to link them. If not,
1354 			   we need to send a FORGET for each of those
1355 			   which we did not link.
1356 			*/
1357 			over = filldir(dstbuf, dirent->name, dirent->namelen,
1358 				       file->f_pos, dirent->ino,
1359 				       dirent->type);
1360 			file->f_pos = dirent->off;
1361 		}
1362 
1363 		buf += reclen;
1364 		nbytes -= reclen;
1365 
1366 		ret = fuse_direntplus_link(file, direntplus, attr_version);
1367 		if (ret)
1368 			fuse_force_forget(file, direntplus->entry_out.nodeid);
1369 	}
1370 
1371 	return 0;
1372 }
1373 
fuse_readdir(struct file * file,void * dstbuf,filldir_t filldir)1374 static int fuse_readdir(struct file *file, void *dstbuf, filldir_t filldir)
1375 {
1376 	int plus, err;
1377 	size_t nbytes;
1378 	struct page *page;
1379 	struct inode *inode = file_inode(file);
1380 	struct fuse_conn *fc = get_fuse_conn(inode);
1381 	struct fuse_req *req;
1382 	u64 attr_version = 0;
1383 
1384 	if (is_bad_inode(inode))
1385 		return -EIO;
1386 
1387 	req = fuse_get_req(fc, 1);
1388 	if (IS_ERR(req))
1389 		return PTR_ERR(req);
1390 
1391 	page = alloc_page(GFP_KERNEL);
1392 	if (!page) {
1393 		fuse_put_request(fc, req);
1394 		return -ENOMEM;
1395 	}
1396 
1397 	plus = fuse_use_readdirplus(inode, file);
1398 	req->out.argpages = 1;
1399 	req->num_pages = 1;
1400 	req->pages[0] = page;
1401 	req->page_descs[0].length = PAGE_SIZE;
1402 	if (plus) {
1403 		attr_version = fuse_get_attr_version(fc);
1404 		fuse_read_fill(req, file, file->f_pos, PAGE_SIZE,
1405 			       FUSE_READDIRPLUS);
1406 	} else {
1407 		fuse_read_fill(req, file, file->f_pos, PAGE_SIZE,
1408 			       FUSE_READDIR);
1409 	}
1410 	fuse_request_send(fc, req);
1411 	nbytes = req->out.args[0].size;
1412 	err = req->out.h.error;
1413 	fuse_put_request(fc, req);
1414 	if (!err) {
1415 		if (plus) {
1416 			err = parse_dirplusfile(page_address(page), nbytes,
1417 						file, dstbuf, filldir,
1418 						attr_version);
1419 		} else {
1420 			err = parse_dirfile(page_address(page), nbytes, file,
1421 					    dstbuf, filldir);
1422 		}
1423 	}
1424 
1425 	__free_page(page);
1426 	fuse_invalidate_attr(inode); /* atime changed */
1427 	return err;
1428 }
1429 
read_link(struct dentry * dentry)1430 static char *read_link(struct dentry *dentry)
1431 {
1432 	struct inode *inode = dentry->d_inode;
1433 	struct fuse_conn *fc = get_fuse_conn(inode);
1434 	struct fuse_req *req = fuse_get_req_nopages(fc);
1435 	char *link;
1436 
1437 	if (IS_ERR(req))
1438 		return ERR_CAST(req);
1439 
1440 	link = (char *) __get_free_page(GFP_KERNEL);
1441 	if (!link) {
1442 		link = ERR_PTR(-ENOMEM);
1443 		goto out;
1444 	}
1445 	req->in.h.opcode = FUSE_READLINK;
1446 	req->in.h.nodeid = get_node_id(inode);
1447 	req->out.argvar = 1;
1448 	req->out.numargs = 1;
1449 	req->out.args[0].size = PAGE_SIZE - 1;
1450 	req->out.args[0].value = link;
1451 	fuse_request_send(fc, req);
1452 	if (req->out.h.error) {
1453 		free_page((unsigned long) link);
1454 		link = ERR_PTR(req->out.h.error);
1455 	} else
1456 		link[req->out.args[0].size] = '\0';
1457  out:
1458 	fuse_put_request(fc, req);
1459 	fuse_invalidate_attr(inode); /* atime changed */
1460 	return link;
1461 }
1462 
free_link(char * link)1463 static void free_link(char *link)
1464 {
1465 	if (!IS_ERR(link))
1466 		free_page((unsigned long) link);
1467 }
1468 
fuse_follow_link(struct dentry * dentry,struct nameidata * nd)1469 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
1470 {
1471 	nd_set_link(nd, read_link(dentry));
1472 	return NULL;
1473 }
1474 
fuse_put_link(struct dentry * dentry,struct nameidata * nd,void * c)1475 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1476 {
1477 	free_link(nd_get_link(nd));
1478 }
1479 
fuse_dir_open(struct inode * inode,struct file * file)1480 static int fuse_dir_open(struct inode *inode, struct file *file)
1481 {
1482 	return fuse_open_common(inode, file, true);
1483 }
1484 
fuse_dir_release(struct inode * inode,struct file * file)1485 static int fuse_dir_release(struct inode *inode, struct file *file)
1486 {
1487 	fuse_release_common(file, FUSE_RELEASEDIR);
1488 
1489 	return 0;
1490 }
1491 
fuse_dir_fsync(struct file * file,loff_t start,loff_t end,int datasync)1492 static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
1493 			  int datasync)
1494 {
1495 	return fuse_fsync_common(file, start, end, datasync, 1);
1496 }
1497 
fuse_dir_ioctl(struct file * file,unsigned int cmd,unsigned long arg)1498 static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
1499 			    unsigned long arg)
1500 {
1501 	struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1502 
1503 	/* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1504 	if (fc->minor < 18)
1505 		return -ENOTTY;
1506 
1507 	return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
1508 }
1509 
fuse_dir_compat_ioctl(struct file * file,unsigned int cmd,unsigned long arg)1510 static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
1511 				   unsigned long arg)
1512 {
1513 	struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1514 
1515 	if (fc->minor < 18)
1516 		return -ENOTTY;
1517 
1518 	return fuse_ioctl_common(file, cmd, arg,
1519 				 FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
1520 }
1521 
update_mtime(unsigned ivalid)1522 static bool update_mtime(unsigned ivalid)
1523 {
1524 	/* Always update if mtime is explicitly set  */
1525 	if (ivalid & ATTR_MTIME_SET)
1526 		return true;
1527 
1528 	/* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1529 	if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1530 		return false;
1531 
1532 	/* In all other cases update */
1533 	return true;
1534 }
1535 
iattr_to_fattr(struct iattr * iattr,struct fuse_setattr_in * arg)1536 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
1537 {
1538 	unsigned ivalid = iattr->ia_valid;
1539 
1540 	if (ivalid & ATTR_MODE)
1541 		arg->valid |= FATTR_MODE,   arg->mode = iattr->ia_mode;
1542 	if (ivalid & ATTR_UID)
1543 		arg->valid |= FATTR_UID,    arg->uid = from_kuid(&init_user_ns, iattr->ia_uid);
1544 	if (ivalid & ATTR_GID)
1545 		arg->valid |= FATTR_GID,    arg->gid = from_kgid(&init_user_ns, iattr->ia_gid);
1546 	if (ivalid & ATTR_SIZE)
1547 		arg->valid |= FATTR_SIZE,   arg->size = iattr->ia_size;
1548 	if (ivalid & ATTR_ATIME) {
1549 		arg->valid |= FATTR_ATIME;
1550 		arg->atime = iattr->ia_atime.tv_sec;
1551 		arg->atimensec = iattr->ia_atime.tv_nsec;
1552 		if (!(ivalid & ATTR_ATIME_SET))
1553 			arg->valid |= FATTR_ATIME_NOW;
1554 	}
1555 	if ((ivalid & ATTR_MTIME) && update_mtime(ivalid)) {
1556 		arg->valid |= FATTR_MTIME;
1557 		arg->mtime = iattr->ia_mtime.tv_sec;
1558 		arg->mtimensec = iattr->ia_mtime.tv_nsec;
1559 		if (!(ivalid & ATTR_MTIME_SET))
1560 			arg->valid |= FATTR_MTIME_NOW;
1561 	}
1562 }
1563 
1564 /*
1565  * Prevent concurrent writepages on inode
1566  *
1567  * This is done by adding a negative bias to the inode write counter
1568  * and waiting for all pending writes to finish.
1569  */
fuse_set_nowrite(struct inode * inode)1570 void fuse_set_nowrite(struct inode *inode)
1571 {
1572 	struct fuse_conn *fc = get_fuse_conn(inode);
1573 	struct fuse_inode *fi = get_fuse_inode(inode);
1574 
1575 	BUG_ON(!mutex_is_locked(&inode->i_mutex));
1576 
1577 	spin_lock(&fc->lock);
1578 	BUG_ON(fi->writectr < 0);
1579 	fi->writectr += FUSE_NOWRITE;
1580 	spin_unlock(&fc->lock);
1581 	wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1582 }
1583 
1584 /*
1585  * Allow writepages on inode
1586  *
1587  * Remove the bias from the writecounter and send any queued
1588  * writepages.
1589  */
__fuse_release_nowrite(struct inode * inode)1590 static void __fuse_release_nowrite(struct inode *inode)
1591 {
1592 	struct fuse_inode *fi = get_fuse_inode(inode);
1593 
1594 	BUG_ON(fi->writectr != FUSE_NOWRITE);
1595 	fi->writectr = 0;
1596 	fuse_flush_writepages(inode);
1597 }
1598 
fuse_release_nowrite(struct inode * inode)1599 void fuse_release_nowrite(struct inode *inode)
1600 {
1601 	struct fuse_conn *fc = get_fuse_conn(inode);
1602 
1603 	spin_lock(&fc->lock);
1604 	__fuse_release_nowrite(inode);
1605 	spin_unlock(&fc->lock);
1606 }
1607 
1608 /*
1609  * Set attributes, and at the same time refresh them.
1610  *
1611  * Truncation is slightly complicated, because the 'truncate' request
1612  * may fail, in which case we don't want to touch the mapping.
1613  * vmtruncate() doesn't allow for this case, so do the rlimit checking
1614  * and the actual truncation by hand.
1615  */
fuse_do_setattr(struct inode * inode,struct iattr * attr,struct file * file)1616 int fuse_do_setattr(struct inode *inode, struct iattr *attr,
1617 		    struct file *file)
1618 {
1619 	struct fuse_conn *fc = get_fuse_conn(inode);
1620 	struct fuse_req *req;
1621 	struct fuse_setattr_in inarg;
1622 	struct fuse_attr_out outarg;
1623 	bool is_truncate = false;
1624 	loff_t oldsize;
1625 	int err;
1626 
1627 	if (!(fc->flags & FUSE_DEFAULT_PERMISSIONS))
1628 		attr->ia_valid |= ATTR_FORCE;
1629 
1630 	err = inode_change_ok(inode, attr);
1631 	if (err)
1632 		return err;
1633 
1634 	if (attr->ia_valid & ATTR_OPEN) {
1635 		if (fc->atomic_o_trunc)
1636 			return 0;
1637 		file = NULL;
1638 	}
1639 
1640 	if (attr->ia_valid & ATTR_SIZE)
1641 		is_truncate = true;
1642 
1643 	req = fuse_get_req_nopages(fc);
1644 	if (IS_ERR(req))
1645 		return PTR_ERR(req);
1646 
1647 	if (is_truncate)
1648 		fuse_set_nowrite(inode);
1649 
1650 	memset(&inarg, 0, sizeof(inarg));
1651 	memset(&outarg, 0, sizeof(outarg));
1652 	iattr_to_fattr(attr, &inarg);
1653 	if (file) {
1654 		struct fuse_file *ff = file->private_data;
1655 		inarg.valid |= FATTR_FH;
1656 		inarg.fh = ff->fh;
1657 	}
1658 	if (attr->ia_valid & ATTR_SIZE) {
1659 		/* For mandatory locking in truncate */
1660 		inarg.valid |= FATTR_LOCKOWNER;
1661 		inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1662 	}
1663 	req->in.h.opcode = FUSE_SETATTR;
1664 	req->in.h.nodeid = get_node_id(inode);
1665 	req->in.numargs = 1;
1666 	req->in.args[0].size = sizeof(inarg);
1667 	req->in.args[0].value = &inarg;
1668 	req->out.numargs = 1;
1669 	if (fc->minor < 9)
1670 		req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
1671 	else
1672 		req->out.args[0].size = sizeof(outarg);
1673 	req->out.args[0].value = &outarg;
1674 	fuse_request_send(fc, req);
1675 	err = req->out.h.error;
1676 	fuse_put_request(fc, req);
1677 	if (err) {
1678 		if (err == -EINTR)
1679 			fuse_invalidate_attr(inode);
1680 		goto error;
1681 	}
1682 
1683 	if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1684 		make_bad_inode(inode);
1685 		err = -EIO;
1686 		goto error;
1687 	}
1688 
1689 	spin_lock(&fc->lock);
1690 	fuse_change_attributes_common(inode, &outarg.attr,
1691 				      attr_timeout(&outarg));
1692 	oldsize = inode->i_size;
1693 	i_size_write(inode, outarg.attr.size);
1694 
1695 	if (is_truncate) {
1696 		/* NOTE: this may release/reacquire fc->lock */
1697 		__fuse_release_nowrite(inode);
1698 	}
1699 	spin_unlock(&fc->lock);
1700 
1701 	/*
1702 	 * Only call invalidate_inode_pages2() after removing
1703 	 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1704 	 */
1705 	if (S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1706 		truncate_pagecache(inode, oldsize, outarg.attr.size);
1707 		invalidate_inode_pages2(inode->i_mapping);
1708 	}
1709 
1710 	return 0;
1711 
1712 error:
1713 	if (is_truncate)
1714 		fuse_release_nowrite(inode);
1715 
1716 	return err;
1717 }
1718 
fuse_setattr(struct dentry * entry,struct iattr * attr)1719 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1720 {
1721 	struct inode *inode = entry->d_inode;
1722 
1723 	if (!fuse_allow_current_process(get_fuse_conn(inode)))
1724 		return -EACCES;
1725 
1726 	if (attr->ia_valid & ATTR_FILE)
1727 		return fuse_do_setattr(inode, attr, attr->ia_file);
1728 	else
1729 		return fuse_do_setattr(inode, attr, NULL);
1730 }
1731 
fuse_getattr(struct vfsmount * mnt,struct dentry * entry,struct kstat * stat)1732 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1733 			struct kstat *stat)
1734 {
1735 	struct inode *inode = entry->d_inode;
1736 	struct fuse_conn *fc = get_fuse_conn(inode);
1737 
1738 	if (!fuse_allow_current_process(fc))
1739 		return -EACCES;
1740 
1741 	return fuse_update_attributes(inode, stat, NULL, NULL);
1742 }
1743 
fuse_setxattr(struct dentry * entry,const char * name,const void * value,size_t size,int flags)1744 static int fuse_setxattr(struct dentry *entry, const char *name,
1745 			 const void *value, size_t size, int flags)
1746 {
1747 	struct inode *inode = entry->d_inode;
1748 	struct fuse_conn *fc = get_fuse_conn(inode);
1749 	struct fuse_req *req;
1750 	struct fuse_setxattr_in inarg;
1751 	int err;
1752 
1753 	if (fc->no_setxattr)
1754 		return -EOPNOTSUPP;
1755 
1756 	req = fuse_get_req_nopages(fc);
1757 	if (IS_ERR(req))
1758 		return PTR_ERR(req);
1759 
1760 	memset(&inarg, 0, sizeof(inarg));
1761 	inarg.size = size;
1762 	inarg.flags = flags;
1763 	req->in.h.opcode = FUSE_SETXATTR;
1764 	req->in.h.nodeid = get_node_id(inode);
1765 	req->in.numargs = 3;
1766 	req->in.args[0].size = sizeof(inarg);
1767 	req->in.args[0].value = &inarg;
1768 	req->in.args[1].size = strlen(name) + 1;
1769 	req->in.args[1].value = name;
1770 	req->in.args[2].size = size;
1771 	req->in.args[2].value = value;
1772 	fuse_request_send(fc, req);
1773 	err = req->out.h.error;
1774 	fuse_put_request(fc, req);
1775 	if (err == -ENOSYS) {
1776 		fc->no_setxattr = 1;
1777 		err = -EOPNOTSUPP;
1778 	}
1779 	return err;
1780 }
1781 
fuse_getxattr(struct dentry * entry,const char * name,void * value,size_t size)1782 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1783 			     void *value, size_t size)
1784 {
1785 	struct inode *inode = entry->d_inode;
1786 	struct fuse_conn *fc = get_fuse_conn(inode);
1787 	struct fuse_req *req;
1788 	struct fuse_getxattr_in inarg;
1789 	struct fuse_getxattr_out outarg;
1790 	ssize_t ret;
1791 
1792 	if (fc->no_getxattr)
1793 		return -EOPNOTSUPP;
1794 
1795 	req = fuse_get_req_nopages(fc);
1796 	if (IS_ERR(req))
1797 		return PTR_ERR(req);
1798 
1799 	memset(&inarg, 0, sizeof(inarg));
1800 	inarg.size = size;
1801 	req->in.h.opcode = FUSE_GETXATTR;
1802 	req->in.h.nodeid = get_node_id(inode);
1803 	req->in.numargs = 2;
1804 	req->in.args[0].size = sizeof(inarg);
1805 	req->in.args[0].value = &inarg;
1806 	req->in.args[1].size = strlen(name) + 1;
1807 	req->in.args[1].value = name;
1808 	/* This is really two different operations rolled into one */
1809 	req->out.numargs = 1;
1810 	if (size) {
1811 		req->out.argvar = 1;
1812 		req->out.args[0].size = size;
1813 		req->out.args[0].value = value;
1814 	} else {
1815 		req->out.args[0].size = sizeof(outarg);
1816 		req->out.args[0].value = &outarg;
1817 	}
1818 	fuse_request_send(fc, req);
1819 	ret = req->out.h.error;
1820 	if (!ret)
1821 		ret = size ? req->out.args[0].size : outarg.size;
1822 	else {
1823 		if (ret == -ENOSYS) {
1824 			fc->no_getxattr = 1;
1825 			ret = -EOPNOTSUPP;
1826 		}
1827 	}
1828 	fuse_put_request(fc, req);
1829 	return ret;
1830 }
1831 
fuse_listxattr(struct dentry * entry,char * list,size_t size)1832 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1833 {
1834 	struct inode *inode = entry->d_inode;
1835 	struct fuse_conn *fc = get_fuse_conn(inode);
1836 	struct fuse_req *req;
1837 	struct fuse_getxattr_in inarg;
1838 	struct fuse_getxattr_out outarg;
1839 	ssize_t ret;
1840 
1841 	if (!fuse_allow_current_process(fc))
1842 		return -EACCES;
1843 
1844 	if (fc->no_listxattr)
1845 		return -EOPNOTSUPP;
1846 
1847 	req = fuse_get_req_nopages(fc);
1848 	if (IS_ERR(req))
1849 		return PTR_ERR(req);
1850 
1851 	memset(&inarg, 0, sizeof(inarg));
1852 	inarg.size = size;
1853 	req->in.h.opcode = FUSE_LISTXATTR;
1854 	req->in.h.nodeid = get_node_id(inode);
1855 	req->in.numargs = 1;
1856 	req->in.args[0].size = sizeof(inarg);
1857 	req->in.args[0].value = &inarg;
1858 	/* This is really two different operations rolled into one */
1859 	req->out.numargs = 1;
1860 	if (size) {
1861 		req->out.argvar = 1;
1862 		req->out.args[0].size = size;
1863 		req->out.args[0].value = list;
1864 	} else {
1865 		req->out.args[0].size = sizeof(outarg);
1866 		req->out.args[0].value = &outarg;
1867 	}
1868 	fuse_request_send(fc, req);
1869 	ret = req->out.h.error;
1870 	if (!ret)
1871 		ret = size ? req->out.args[0].size : outarg.size;
1872 	else {
1873 		if (ret == -ENOSYS) {
1874 			fc->no_listxattr = 1;
1875 			ret = -EOPNOTSUPP;
1876 		}
1877 	}
1878 	fuse_put_request(fc, req);
1879 	return ret;
1880 }
1881 
fuse_removexattr(struct dentry * entry,const char * name)1882 static int fuse_removexattr(struct dentry *entry, const char *name)
1883 {
1884 	struct inode *inode = entry->d_inode;
1885 	struct fuse_conn *fc = get_fuse_conn(inode);
1886 	struct fuse_req *req;
1887 	int err;
1888 
1889 	if (fc->no_removexattr)
1890 		return -EOPNOTSUPP;
1891 
1892 	req = fuse_get_req_nopages(fc);
1893 	if (IS_ERR(req))
1894 		return PTR_ERR(req);
1895 
1896 	req->in.h.opcode = FUSE_REMOVEXATTR;
1897 	req->in.h.nodeid = get_node_id(inode);
1898 	req->in.numargs = 1;
1899 	req->in.args[0].size = strlen(name) + 1;
1900 	req->in.args[0].value = name;
1901 	fuse_request_send(fc, req);
1902 	err = req->out.h.error;
1903 	fuse_put_request(fc, req);
1904 	if (err == -ENOSYS) {
1905 		fc->no_removexattr = 1;
1906 		err = -EOPNOTSUPP;
1907 	}
1908 	return err;
1909 }
1910 
1911 static const struct inode_operations fuse_dir_inode_operations = {
1912 	.lookup		= fuse_lookup,
1913 	.mkdir		= fuse_mkdir,
1914 	.symlink	= fuse_symlink,
1915 	.unlink		= fuse_unlink,
1916 	.rmdir		= fuse_rmdir,
1917 	.rename		= fuse_rename,
1918 	.link		= fuse_link,
1919 	.setattr	= fuse_setattr,
1920 	.create		= fuse_create,
1921 	.atomic_open	= fuse_atomic_open,
1922 	.mknod		= fuse_mknod,
1923 	.permission	= fuse_permission,
1924 	.getattr	= fuse_getattr,
1925 	.setxattr	= fuse_setxattr,
1926 	.getxattr	= fuse_getxattr,
1927 	.listxattr	= fuse_listxattr,
1928 	.removexattr	= fuse_removexattr,
1929 };
1930 
1931 static const struct file_operations fuse_dir_operations = {
1932 	.llseek		= generic_file_llseek,
1933 	.read		= generic_read_dir,
1934 	.readdir	= fuse_readdir,
1935 	.open		= fuse_dir_open,
1936 	.release	= fuse_dir_release,
1937 	.fsync		= fuse_dir_fsync,
1938 	.unlocked_ioctl	= fuse_dir_ioctl,
1939 	.compat_ioctl	= fuse_dir_compat_ioctl,
1940 };
1941 
1942 static const struct inode_operations fuse_common_inode_operations = {
1943 	.setattr	= fuse_setattr,
1944 	.permission	= fuse_permission,
1945 	.getattr	= fuse_getattr,
1946 	.setxattr	= fuse_setxattr,
1947 	.getxattr	= fuse_getxattr,
1948 	.listxattr	= fuse_listxattr,
1949 	.removexattr	= fuse_removexattr,
1950 };
1951 
1952 static const struct inode_operations fuse_symlink_inode_operations = {
1953 	.setattr	= fuse_setattr,
1954 	.follow_link	= fuse_follow_link,
1955 	.put_link	= fuse_put_link,
1956 	.readlink	= generic_readlink,
1957 	.getattr	= fuse_getattr,
1958 	.setxattr	= fuse_setxattr,
1959 	.getxattr	= fuse_getxattr,
1960 	.listxattr	= fuse_listxattr,
1961 	.removexattr	= fuse_removexattr,
1962 };
1963 
fuse_init_common(struct inode * inode)1964 void fuse_init_common(struct inode *inode)
1965 {
1966 	inode->i_op = &fuse_common_inode_operations;
1967 }
1968 
fuse_init_dir(struct inode * inode)1969 void fuse_init_dir(struct inode *inode)
1970 {
1971 	inode->i_op = &fuse_dir_inode_operations;
1972 	inode->i_fop = &fuse_dir_operations;
1973 }
1974 
fuse_init_symlink(struct inode * inode)1975 void fuse_init_symlink(struct inode *inode)
1976 {
1977 	inode->i_op = &fuse_symlink_inode_operations;
1978 }
1979