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