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