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