xref: /OK3568_Linux_fs/kernel/fs/fuse/dir.c (revision 4882a59341e53eb6f0b4789bf948001014eff981)
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))) {
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)
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 
582 	/* Userspace expects S_IFREG in create mode */
583 	BUG_ON((mode & S_IFMT) != S_IFREG);
584 
585 	forget = fuse_alloc_forget();
586 	err = -ENOMEM;
587 	if (!forget)
588 		goto out_err;
589 
590 	err = -ENOMEM;
591 	ff = fuse_file_alloc(fm);
592 	if (!ff)
593 		goto out_put_forget_req;
594 
595 	if (!fm->fc->dont_mask)
596 		mode &= ~current_umask();
597 
598 	flags &= ~O_NOCTTY;
599 	memset(&inarg, 0, sizeof(inarg));
600 	memset(&outentry, 0, sizeof(outentry));
601 	inarg.flags = flags;
602 	inarg.mode = mode;
603 	inarg.umask = current_umask();
604 	args.opcode = FUSE_CREATE;
605 	args.nodeid = get_node_id(dir);
606 	args.in_numargs = 2;
607 	args.in_args[0].size = sizeof(inarg);
608 	args.in_args[0].value = &inarg;
609 	args.in_args[1].size = entry->d_name.len + 1;
610 	args.in_args[1].value = entry->d_name.name;
611 	args.out_numargs = 2;
612 	args.out_args[0].size = sizeof(outentry);
613 	args.out_args[0].value = &outentry;
614 	args.out_args[1].size = sizeof(outopen);
615 	args.out_args[1].value = &outopen;
616 	err = fuse_simple_request(fm, &args);
617 	if (err)
618 		goto out_free_ff;
619 
620 	err = -EIO;
621 	if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid) ||
622 	    fuse_invalid_attr(&outentry.attr))
623 		goto out_free_ff;
624 
625 	ff->fh = outopen.fh;
626 	ff->nodeid = outentry.nodeid;
627 	ff->open_flags = outopen.open_flags;
628 	fuse_passthrough_setup(fc, ff, &outopen);
629 	inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
630 			  &outentry.attr, entry_attr_timeout(&outentry), 0);
631 	if (!inode) {
632 		flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
633 		fuse_sync_release(NULL, ff, flags);
634 		fuse_queue_forget(fm->fc, forget, outentry.nodeid, 1);
635 		err = -ENOMEM;
636 		goto out_err;
637 	}
638 	kfree(forget);
639 	d_instantiate(entry, inode);
640 	fuse_change_entry_timeout(entry, &outentry);
641 	fuse_dir_changed(dir);
642 	err = finish_open(file, entry, generic_file_open);
643 	if (err) {
644 		fi = get_fuse_inode(inode);
645 		fuse_sync_release(fi, ff, flags);
646 	} else {
647 		file->private_data = ff;
648 		fuse_finish_open(inode, file);
649 	}
650 	return err;
651 
652 out_free_ff:
653 	fuse_file_free(ff);
654 out_put_forget_req:
655 	kfree(forget);
656 out_err:
657 	return err;
658 }
659 
660 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)661 static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
662 			    struct file *file, unsigned flags,
663 			    umode_t mode)
664 {
665 	int err;
666 	struct fuse_conn *fc = get_fuse_conn(dir);
667 	struct dentry *res = NULL;
668 
669 	if (fuse_is_bad(dir))
670 		return -EIO;
671 
672 	if (d_in_lookup(entry)) {
673 		res = fuse_lookup(dir, entry, 0);
674 		if (IS_ERR(res))
675 			return PTR_ERR(res);
676 
677 		if (res)
678 			entry = res;
679 	}
680 
681 	if (!(flags & O_CREAT) || d_really_is_positive(entry))
682 		goto no_open;
683 
684 	/* Only creates */
685 	file->f_mode |= FMODE_CREATED;
686 
687 	if (fc->no_create)
688 		goto mknod;
689 
690 	err = fuse_create_open(dir, entry, file, flags, mode);
691 	if (err == -ENOSYS) {
692 		fc->no_create = 1;
693 		goto mknod;
694 	}
695 out_dput:
696 	dput(res);
697 	return err;
698 
699 mknod:
700 	err = fuse_mknod(dir, entry, mode, 0);
701 	if (err)
702 		goto out_dput;
703 no_open:
704 	return finish_no_open(file, res);
705 }
706 
707 /*
708  * Code shared between mknod, mkdir, symlink and link
709  */
create_new_entry(struct fuse_mount * fm,struct fuse_args * args,struct inode * dir,struct dentry * entry,umode_t mode)710 static int create_new_entry(struct fuse_mount *fm, struct fuse_args *args,
711 			    struct inode *dir, struct dentry *entry,
712 			    umode_t mode)
713 {
714 	struct fuse_entry_out outarg;
715 	struct inode *inode;
716 	struct dentry *d;
717 	int err;
718 	struct fuse_forget_link *forget;
719 
720 	if (fuse_is_bad(dir))
721 		return -EIO;
722 
723 	forget = fuse_alloc_forget();
724 	if (!forget)
725 		return -ENOMEM;
726 
727 	memset(&outarg, 0, sizeof(outarg));
728 	args->nodeid = get_node_id(dir);
729 	args->out_numargs = 1;
730 	args->out_args[0].size = sizeof(outarg);
731 	args->out_args[0].value = &outarg;
732 	err = fuse_simple_request(fm, args);
733 	if (err)
734 		goto out_put_forget_req;
735 
736 	err = -EIO;
737 	if (invalid_nodeid(outarg.nodeid) || fuse_invalid_attr(&outarg.attr))
738 		goto out_put_forget_req;
739 
740 	if ((outarg.attr.mode ^ mode) & S_IFMT)
741 		goto out_put_forget_req;
742 
743 	inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
744 			  &outarg.attr, entry_attr_timeout(&outarg), 0);
745 	if (!inode) {
746 		fuse_queue_forget(fm->fc, forget, outarg.nodeid, 1);
747 		return -ENOMEM;
748 	}
749 	kfree(forget);
750 
751 	d_drop(entry);
752 	d = d_splice_alias(inode, entry);
753 	if (IS_ERR(d))
754 		return PTR_ERR(d);
755 
756 	if (d) {
757 		fuse_change_entry_timeout(d, &outarg);
758 		dput(d);
759 	} else {
760 		fuse_change_entry_timeout(entry, &outarg);
761 	}
762 	fuse_dir_changed(dir);
763 	return 0;
764 
765  out_put_forget_req:
766 	kfree(forget);
767 	return err;
768 }
769 
fuse_mknod(struct inode * dir,struct dentry * entry,umode_t mode,dev_t rdev)770 static int fuse_mknod(struct inode *dir, struct dentry *entry, umode_t mode,
771 		      dev_t rdev)
772 {
773 	struct fuse_mknod_in inarg;
774 	struct fuse_mount *fm = get_fuse_mount(dir);
775 	FUSE_ARGS(args);
776 
777 	if (!fm->fc->dont_mask)
778 		mode &= ~current_umask();
779 
780 	memset(&inarg, 0, sizeof(inarg));
781 	inarg.mode = mode;
782 	inarg.rdev = new_encode_dev(rdev);
783 	inarg.umask = current_umask();
784 	args.opcode = FUSE_MKNOD;
785 	args.in_numargs = 2;
786 	args.in_args[0].size = sizeof(inarg);
787 	args.in_args[0].value = &inarg;
788 	args.in_args[1].size = entry->d_name.len + 1;
789 	args.in_args[1].value = entry->d_name.name;
790 	return create_new_entry(fm, &args, dir, entry, mode);
791 }
792 
fuse_create(struct inode * dir,struct dentry * entry,umode_t mode,bool excl)793 static int fuse_create(struct inode *dir, struct dentry *entry, umode_t mode,
794 		       bool excl)
795 {
796 	return fuse_mknod(dir, entry, mode, 0);
797 }
798 
fuse_mkdir(struct inode * dir,struct dentry * entry,umode_t mode)799 static int fuse_mkdir(struct inode *dir, struct dentry *entry, umode_t mode)
800 {
801 	struct fuse_mkdir_in inarg;
802 	struct fuse_mount *fm = get_fuse_mount(dir);
803 	FUSE_ARGS(args);
804 
805 	if (!fm->fc->dont_mask)
806 		mode &= ~current_umask();
807 
808 	memset(&inarg, 0, sizeof(inarg));
809 	inarg.mode = mode;
810 	inarg.umask = current_umask();
811 	args.opcode = FUSE_MKDIR;
812 	args.in_numargs = 2;
813 	args.in_args[0].size = sizeof(inarg);
814 	args.in_args[0].value = &inarg;
815 	args.in_args[1].size = entry->d_name.len + 1;
816 	args.in_args[1].value = entry->d_name.name;
817 	return create_new_entry(fm, &args, dir, entry, S_IFDIR);
818 }
819 
fuse_symlink(struct inode * dir,struct dentry * entry,const char * link)820 static int fuse_symlink(struct inode *dir, struct dentry *entry,
821 			const char *link)
822 {
823 	struct fuse_mount *fm = get_fuse_mount(dir);
824 	unsigned len = strlen(link) + 1;
825 	FUSE_ARGS(args);
826 
827 	args.opcode = FUSE_SYMLINK;
828 	args.in_numargs = 2;
829 	args.in_args[0].size = entry->d_name.len + 1;
830 	args.in_args[0].value = entry->d_name.name;
831 	args.in_args[1].size = len;
832 	args.in_args[1].value = link;
833 	return create_new_entry(fm, &args, dir, entry, S_IFLNK);
834 }
835 
fuse_flush_time_update(struct inode * inode)836 void fuse_flush_time_update(struct inode *inode)
837 {
838 	int err = sync_inode_metadata(inode, 1);
839 
840 	mapping_set_error(inode->i_mapping, err);
841 }
842 
fuse_update_ctime(struct inode * inode)843 void fuse_update_ctime(struct inode *inode)
844 {
845 	if (!IS_NOCMTIME(inode)) {
846 		inode->i_ctime = current_time(inode);
847 		mark_inode_dirty_sync(inode);
848 		fuse_flush_time_update(inode);
849 	}
850 }
851 
fuse_unlink(struct inode * dir,struct dentry * entry)852 static int fuse_unlink(struct inode *dir, struct dentry *entry)
853 {
854 	int err;
855 	struct fuse_mount *fm = get_fuse_mount(dir);
856 	FUSE_ARGS(args);
857 
858 	if (fuse_is_bad(dir))
859 		return -EIO;
860 
861 	args.opcode = FUSE_UNLINK;
862 	args.nodeid = get_node_id(dir);
863 	args.in_numargs = 1;
864 	args.in_args[0].size = entry->d_name.len + 1;
865 	args.in_args[0].value = entry->d_name.name;
866 	err = fuse_simple_request(fm, &args);
867 	if (!err) {
868 		struct inode *inode = d_inode(entry);
869 		struct fuse_inode *fi = get_fuse_inode(inode);
870 
871 		spin_lock(&fi->lock);
872 		fi->attr_version = atomic64_inc_return(&fm->fc->attr_version);
873 		/*
874 		 * If i_nlink == 0 then unlink doesn't make sense, yet this can
875 		 * happen if userspace filesystem is careless.  It would be
876 		 * difficult to enforce correct nlink usage so just ignore this
877 		 * condition here
878 		 */
879 		if (inode->i_nlink > 0)
880 			drop_nlink(inode);
881 		spin_unlock(&fi->lock);
882 		fuse_invalidate_attr(inode);
883 		fuse_dir_changed(dir);
884 		fuse_invalidate_entry_cache(entry);
885 		fuse_update_ctime(inode);
886 	} else if (err == -EINTR)
887 		fuse_invalidate_entry(entry);
888 	return err;
889 }
890 
fuse_rmdir(struct inode * dir,struct dentry * entry)891 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
892 {
893 	int err;
894 	struct fuse_mount *fm = get_fuse_mount(dir);
895 	FUSE_ARGS(args);
896 
897 	if (fuse_is_bad(dir))
898 		return -EIO;
899 
900 	args.opcode = FUSE_RMDIR;
901 	args.nodeid = get_node_id(dir);
902 	args.in_numargs = 1;
903 	args.in_args[0].size = entry->d_name.len + 1;
904 	args.in_args[0].value = entry->d_name.name;
905 	err = fuse_simple_request(fm, &args);
906 	if (!err) {
907 		clear_nlink(d_inode(entry));
908 		fuse_dir_changed(dir);
909 		fuse_invalidate_entry_cache(entry);
910 	} else if (err == -EINTR)
911 		fuse_invalidate_entry(entry);
912 	return err;
913 }
914 
fuse_rename_common(struct inode * olddir,struct dentry * oldent,struct inode * newdir,struct dentry * newent,unsigned int flags,int opcode,size_t argsize)915 static int fuse_rename_common(struct inode *olddir, struct dentry *oldent,
916 			      struct inode *newdir, struct dentry *newent,
917 			      unsigned int flags, int opcode, size_t argsize)
918 {
919 	int err;
920 	struct fuse_rename2_in inarg;
921 	struct fuse_mount *fm = get_fuse_mount(olddir);
922 	FUSE_ARGS(args);
923 
924 	memset(&inarg, 0, argsize);
925 	inarg.newdir = get_node_id(newdir);
926 	inarg.flags = flags;
927 	args.opcode = opcode;
928 	args.nodeid = get_node_id(olddir);
929 	args.in_numargs = 3;
930 	args.in_args[0].size = argsize;
931 	args.in_args[0].value = &inarg;
932 	args.in_args[1].size = oldent->d_name.len + 1;
933 	args.in_args[1].value = oldent->d_name.name;
934 	args.in_args[2].size = newent->d_name.len + 1;
935 	args.in_args[2].value = newent->d_name.name;
936 	err = fuse_simple_request(fm, &args);
937 	if (!err) {
938 		/* ctime changes */
939 		fuse_invalidate_attr(d_inode(oldent));
940 		fuse_update_ctime(d_inode(oldent));
941 
942 		if (flags & RENAME_EXCHANGE) {
943 			fuse_invalidate_attr(d_inode(newent));
944 			fuse_update_ctime(d_inode(newent));
945 		}
946 
947 		fuse_dir_changed(olddir);
948 		if (olddir != newdir)
949 			fuse_dir_changed(newdir);
950 
951 		/* newent will end up negative */
952 		if (!(flags & RENAME_EXCHANGE) && d_really_is_positive(newent)) {
953 			fuse_invalidate_attr(d_inode(newent));
954 			fuse_invalidate_entry_cache(newent);
955 			fuse_update_ctime(d_inode(newent));
956 		}
957 	} else if (err == -EINTR) {
958 		/* If request was interrupted, DEITY only knows if the
959 		   rename actually took place.  If the invalidation
960 		   fails (e.g. some process has CWD under the renamed
961 		   directory), then there can be inconsistency between
962 		   the dcache and the real filesystem.  Tough luck. */
963 		fuse_invalidate_entry(oldent);
964 		if (d_really_is_positive(newent))
965 			fuse_invalidate_entry(newent);
966 	}
967 
968 	return err;
969 }
970 
fuse_rename2(struct inode * olddir,struct dentry * oldent,struct inode * newdir,struct dentry * newent,unsigned int flags)971 static int fuse_rename2(struct inode *olddir, struct dentry *oldent,
972 			struct inode *newdir, struct dentry *newent,
973 			unsigned int flags)
974 {
975 	struct fuse_conn *fc = get_fuse_conn(olddir);
976 	int err;
977 
978 	if (fuse_is_bad(olddir))
979 		return -EIO;
980 
981 	if (flags & ~(RENAME_NOREPLACE | RENAME_EXCHANGE | RENAME_WHITEOUT))
982 		return -EINVAL;
983 
984 	if (flags) {
985 		if (fc->no_rename2 || fc->minor < 23)
986 			return -EINVAL;
987 
988 		err = fuse_rename_common(olddir, oldent, newdir, newent, flags,
989 					 FUSE_RENAME2,
990 					 sizeof(struct fuse_rename2_in));
991 		if (err == -ENOSYS) {
992 			fc->no_rename2 = 1;
993 			err = -EINVAL;
994 		}
995 	} else {
996 		err = fuse_rename_common(olddir, oldent, newdir, newent, 0,
997 					 FUSE_RENAME,
998 					 sizeof(struct fuse_rename_in));
999 	}
1000 
1001 	return err;
1002 }
1003 
fuse_link(struct dentry * entry,struct inode * newdir,struct dentry * newent)1004 static int fuse_link(struct dentry *entry, struct inode *newdir,
1005 		     struct dentry *newent)
1006 {
1007 	int err;
1008 	struct fuse_link_in inarg;
1009 	struct inode *inode = d_inode(entry);
1010 	struct fuse_mount *fm = get_fuse_mount(inode);
1011 	FUSE_ARGS(args);
1012 
1013 	memset(&inarg, 0, sizeof(inarg));
1014 	inarg.oldnodeid = get_node_id(inode);
1015 	args.opcode = FUSE_LINK;
1016 	args.in_numargs = 2;
1017 	args.in_args[0].size = sizeof(inarg);
1018 	args.in_args[0].value = &inarg;
1019 	args.in_args[1].size = newent->d_name.len + 1;
1020 	args.in_args[1].value = newent->d_name.name;
1021 	err = create_new_entry(fm, &args, newdir, newent, inode->i_mode);
1022 	/* Contrary to "normal" filesystems it can happen that link
1023 	   makes two "logical" inodes point to the same "physical"
1024 	   inode.  We invalidate the attributes of the old one, so it
1025 	   will reflect changes in the backing inode (link count,
1026 	   etc.)
1027 	*/
1028 	if (!err) {
1029 		struct fuse_inode *fi = get_fuse_inode(inode);
1030 
1031 		spin_lock(&fi->lock);
1032 		fi->attr_version = atomic64_inc_return(&fm->fc->attr_version);
1033 		if (likely(inode->i_nlink < UINT_MAX))
1034 			inc_nlink(inode);
1035 		spin_unlock(&fi->lock);
1036 		fuse_invalidate_attr(inode);
1037 		fuse_update_ctime(inode);
1038 	} else if (err == -EINTR) {
1039 		fuse_invalidate_attr(inode);
1040 	}
1041 	return err;
1042 }
1043 
fuse_fillattr(struct inode * inode,struct fuse_attr * attr,struct kstat * stat)1044 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
1045 			  struct kstat *stat)
1046 {
1047 	unsigned int blkbits;
1048 	struct fuse_conn *fc = get_fuse_conn(inode);
1049 
1050 	/* see the comment in fuse_change_attributes() */
1051 	if (fc->writeback_cache && S_ISREG(inode->i_mode)) {
1052 		attr->size = i_size_read(inode);
1053 		attr->mtime = inode->i_mtime.tv_sec;
1054 		attr->mtimensec = inode->i_mtime.tv_nsec;
1055 		attr->ctime = inode->i_ctime.tv_sec;
1056 		attr->ctimensec = inode->i_ctime.tv_nsec;
1057 	}
1058 
1059 	stat->dev = inode->i_sb->s_dev;
1060 	stat->ino = attr->ino;
1061 	stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
1062 	stat->nlink = attr->nlink;
1063 	stat->uid = make_kuid(fc->user_ns, attr->uid);
1064 	stat->gid = make_kgid(fc->user_ns, attr->gid);
1065 	stat->rdev = inode->i_rdev;
1066 	stat->atime.tv_sec = attr->atime;
1067 	stat->atime.tv_nsec = attr->atimensec;
1068 	stat->mtime.tv_sec = attr->mtime;
1069 	stat->mtime.tv_nsec = attr->mtimensec;
1070 	stat->ctime.tv_sec = attr->ctime;
1071 	stat->ctime.tv_nsec = attr->ctimensec;
1072 	stat->size = attr->size;
1073 	stat->blocks = attr->blocks;
1074 
1075 	if (attr->blksize != 0)
1076 		blkbits = ilog2(attr->blksize);
1077 	else
1078 		blkbits = inode->i_sb->s_blocksize_bits;
1079 
1080 	stat->blksize = 1 << blkbits;
1081 }
1082 
fuse_do_getattr(struct inode * inode,struct kstat * stat,struct file * file)1083 static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
1084 			   struct file *file)
1085 {
1086 	int err;
1087 	struct fuse_getattr_in inarg;
1088 	struct fuse_attr_out outarg;
1089 	struct fuse_mount *fm = get_fuse_mount(inode);
1090 	FUSE_ARGS(args);
1091 	u64 attr_version;
1092 
1093 	attr_version = fuse_get_attr_version(fm->fc);
1094 
1095 	memset(&inarg, 0, sizeof(inarg));
1096 	memset(&outarg, 0, sizeof(outarg));
1097 	/* Directories have separate file-handle space */
1098 	if (file && S_ISREG(inode->i_mode)) {
1099 		struct fuse_file *ff = file->private_data;
1100 
1101 		inarg.getattr_flags |= FUSE_GETATTR_FH;
1102 		inarg.fh = ff->fh;
1103 	}
1104 	args.opcode = FUSE_GETATTR;
1105 	args.nodeid = get_node_id(inode);
1106 	args.in_numargs = 1;
1107 	args.in_args[0].size = sizeof(inarg);
1108 	args.in_args[0].value = &inarg;
1109 	args.out_numargs = 1;
1110 	args.out_args[0].size = sizeof(outarg);
1111 	args.out_args[0].value = &outarg;
1112 	err = fuse_simple_request(fm, &args);
1113 	if (!err) {
1114 		if (fuse_invalid_attr(&outarg.attr) ||
1115 		    inode_wrong_type(inode, outarg.attr.mode)) {
1116 			fuse_make_bad(inode);
1117 			err = -EIO;
1118 		} else {
1119 			fuse_change_attributes(inode, &outarg.attr,
1120 					       attr_timeout(&outarg),
1121 					       attr_version);
1122 			if (stat)
1123 				fuse_fillattr(inode, &outarg.attr, stat);
1124 		}
1125 	}
1126 	return err;
1127 }
1128 
fuse_update_get_attr(struct inode * inode,struct file * file,struct kstat * stat,u32 request_mask,unsigned int flags)1129 static int fuse_update_get_attr(struct inode *inode, struct file *file,
1130 				struct kstat *stat, u32 request_mask,
1131 				unsigned int flags)
1132 {
1133 	struct fuse_inode *fi = get_fuse_inode(inode);
1134 	int err = 0;
1135 	bool sync;
1136 
1137 	if (flags & AT_STATX_FORCE_SYNC)
1138 		sync = true;
1139 	else if (flags & AT_STATX_DONT_SYNC)
1140 		sync = false;
1141 	else if (request_mask & READ_ONCE(fi->inval_mask))
1142 		sync = true;
1143 	else
1144 		sync = time_before64(fi->i_time, get_jiffies_64());
1145 
1146 	if (sync) {
1147 		forget_all_cached_acls(inode);
1148 		err = fuse_do_getattr(inode, stat, file);
1149 	} else if (stat) {
1150 		generic_fillattr(inode, stat);
1151 		stat->mode = fi->orig_i_mode;
1152 		stat->ino = fi->orig_ino;
1153 	}
1154 
1155 	return err;
1156 }
1157 
fuse_update_attributes(struct inode * inode,struct file * file)1158 int fuse_update_attributes(struct inode *inode, struct file *file)
1159 {
1160 	/* Do *not* need to get atime for internal purposes */
1161 	return fuse_update_get_attr(inode, file, NULL,
1162 				    STATX_BASIC_STATS & ~STATX_ATIME, 0);
1163 }
1164 
fuse_reverse_inval_entry(struct fuse_conn * fc,u64 parent_nodeid,u64 child_nodeid,struct qstr * name)1165 int fuse_reverse_inval_entry(struct fuse_conn *fc, u64 parent_nodeid,
1166 			     u64 child_nodeid, struct qstr *name)
1167 {
1168 	int err = -ENOTDIR;
1169 	struct inode *parent;
1170 	struct dentry *dir;
1171 	struct dentry *entry;
1172 
1173 	parent = fuse_ilookup(fc, parent_nodeid, NULL);
1174 	if (!parent)
1175 		return -ENOENT;
1176 
1177 	inode_lock_nested(parent, I_MUTEX_PARENT);
1178 	if (!S_ISDIR(parent->i_mode))
1179 		goto unlock;
1180 
1181 	err = -ENOENT;
1182 	dir = d_find_alias(parent);
1183 	if (!dir)
1184 		goto unlock;
1185 
1186 	name->hash = full_name_hash(dir, name->name, name->len);
1187 	entry = d_lookup(dir, name);
1188 	dput(dir);
1189 	if (!entry)
1190 		goto unlock;
1191 
1192 	fuse_dir_changed(parent);
1193 	fuse_invalidate_entry(entry);
1194 
1195 	if (child_nodeid != 0 && d_really_is_positive(entry)) {
1196 		inode_lock(d_inode(entry));
1197 		if (get_node_id(d_inode(entry)) != child_nodeid) {
1198 			err = -ENOENT;
1199 			goto badentry;
1200 		}
1201 		if (d_mountpoint(entry)) {
1202 			err = -EBUSY;
1203 			goto badentry;
1204 		}
1205 		if (d_is_dir(entry)) {
1206 			shrink_dcache_parent(entry);
1207 			if (!simple_empty(entry)) {
1208 				err = -ENOTEMPTY;
1209 				goto badentry;
1210 			}
1211 			d_inode(entry)->i_flags |= S_DEAD;
1212 		}
1213 		dont_mount(entry);
1214 		clear_nlink(d_inode(entry));
1215 		err = 0;
1216  badentry:
1217 		inode_unlock(d_inode(entry));
1218 		if (!err)
1219 			d_delete(entry);
1220 	} else {
1221 		err = 0;
1222 	}
1223 	dput(entry);
1224 
1225  unlock:
1226 	inode_unlock(parent);
1227 	iput(parent);
1228 	return err;
1229 }
1230 
1231 /*
1232  * Calling into a user-controlled filesystem gives the filesystem
1233  * daemon ptrace-like capabilities over the current process.  This
1234  * means, that the filesystem daemon is able to record the exact
1235  * filesystem operations performed, and can also control the behavior
1236  * of the requester process in otherwise impossible ways.  For example
1237  * it can delay the operation for arbitrary length of time allowing
1238  * DoS against the requester.
1239  *
1240  * For this reason only those processes can call into the filesystem,
1241  * for which the owner of the mount has ptrace privilege.  This
1242  * excludes processes started by other users, suid or sgid processes.
1243  */
fuse_allow_current_process(struct fuse_conn * fc)1244 int fuse_allow_current_process(struct fuse_conn *fc)
1245 {
1246 	const struct cred *cred;
1247 
1248 	if (fc->allow_other)
1249 		return current_in_userns(fc->user_ns);
1250 
1251 	cred = current_cred();
1252 	if (uid_eq(cred->euid, fc->user_id) &&
1253 	    uid_eq(cred->suid, fc->user_id) &&
1254 	    uid_eq(cred->uid,  fc->user_id) &&
1255 	    gid_eq(cred->egid, fc->group_id) &&
1256 	    gid_eq(cred->sgid, fc->group_id) &&
1257 	    gid_eq(cred->gid,  fc->group_id))
1258 		return 1;
1259 
1260 	return 0;
1261 }
1262 
fuse_access(struct inode * inode,int mask)1263 static int fuse_access(struct inode *inode, int mask)
1264 {
1265 	struct fuse_mount *fm = get_fuse_mount(inode);
1266 	FUSE_ARGS(args);
1267 	struct fuse_access_in inarg;
1268 	int err;
1269 
1270 	BUG_ON(mask & MAY_NOT_BLOCK);
1271 
1272 	if (fm->fc->no_access)
1273 		return 0;
1274 
1275 	memset(&inarg, 0, sizeof(inarg));
1276 	inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
1277 	args.opcode = FUSE_ACCESS;
1278 	args.nodeid = get_node_id(inode);
1279 	args.in_numargs = 1;
1280 	args.in_args[0].size = sizeof(inarg);
1281 	args.in_args[0].value = &inarg;
1282 	err = fuse_simple_request(fm, &args);
1283 	if (err == -ENOSYS) {
1284 		fm->fc->no_access = 1;
1285 		err = 0;
1286 	}
1287 	return err;
1288 }
1289 
fuse_perm_getattr(struct inode * inode,int mask)1290 static int fuse_perm_getattr(struct inode *inode, int mask)
1291 {
1292 	if (mask & MAY_NOT_BLOCK)
1293 		return -ECHILD;
1294 
1295 	forget_all_cached_acls(inode);
1296 	return fuse_do_getattr(inode, NULL, NULL);
1297 }
1298 
1299 /*
1300  * Check permission.  The two basic access models of FUSE are:
1301  *
1302  * 1) Local access checking ('default_permissions' mount option) based
1303  * on file mode.  This is the plain old disk filesystem permission
1304  * modell.
1305  *
1306  * 2) "Remote" access checking, where server is responsible for
1307  * checking permission in each inode operation.  An exception to this
1308  * is if ->permission() was invoked from sys_access() in which case an
1309  * access request is sent.  Execute permission is still checked
1310  * locally based on file mode.
1311  */
fuse_permission(struct inode * inode,int mask)1312 static int fuse_permission(struct inode *inode, int mask)
1313 {
1314 	struct fuse_conn *fc = get_fuse_conn(inode);
1315 	bool refreshed = false;
1316 	int err = 0;
1317 
1318 	if (fuse_is_bad(inode))
1319 		return -EIO;
1320 
1321 	if (!fuse_allow_current_process(fc))
1322 		return -EACCES;
1323 
1324 	/*
1325 	 * If attributes are needed, refresh them before proceeding
1326 	 */
1327 	if (fc->default_permissions ||
1328 	    ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
1329 		struct fuse_inode *fi = get_fuse_inode(inode);
1330 		u32 perm_mask = STATX_MODE | STATX_UID | STATX_GID;
1331 
1332 		if (perm_mask & READ_ONCE(fi->inval_mask) ||
1333 		    time_before64(fi->i_time, get_jiffies_64())) {
1334 			refreshed = true;
1335 
1336 			err = fuse_perm_getattr(inode, mask);
1337 			if (err)
1338 				return err;
1339 		}
1340 	}
1341 
1342 	if (fc->default_permissions) {
1343 		err = generic_permission(inode, mask);
1344 
1345 		/* If permission is denied, try to refresh file
1346 		   attributes.  This is also needed, because the root
1347 		   node will at first have no permissions */
1348 		if (err == -EACCES && !refreshed) {
1349 			err = fuse_perm_getattr(inode, mask);
1350 			if (!err)
1351 				err = generic_permission(inode, mask);
1352 		}
1353 
1354 		/* Note: the opposite of the above test does not
1355 		   exist.  So if permissions are revoked this won't be
1356 		   noticed immediately, only after the attribute
1357 		   timeout has expired */
1358 	} else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
1359 		err = fuse_access(inode, mask);
1360 	} else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1361 		if (!(inode->i_mode & S_IXUGO)) {
1362 			if (refreshed)
1363 				return -EACCES;
1364 
1365 			err = fuse_perm_getattr(inode, mask);
1366 			if (!err && !(inode->i_mode & S_IXUGO))
1367 				return -EACCES;
1368 		}
1369 	}
1370 	return err;
1371 }
1372 
fuse_readlink_page(struct inode * inode,struct page * page)1373 static int fuse_readlink_page(struct inode *inode, struct page *page)
1374 {
1375 	struct fuse_mount *fm = get_fuse_mount(inode);
1376 	struct fuse_page_desc desc = { .length = PAGE_SIZE - 1 };
1377 	struct fuse_args_pages ap = {
1378 		.num_pages = 1,
1379 		.pages = &page,
1380 		.descs = &desc,
1381 	};
1382 	char *link;
1383 	ssize_t res;
1384 
1385 	ap.args.opcode = FUSE_READLINK;
1386 	ap.args.nodeid = get_node_id(inode);
1387 	ap.args.out_pages = true;
1388 	ap.args.out_argvar = true;
1389 	ap.args.page_zeroing = true;
1390 	ap.args.out_numargs = 1;
1391 	ap.args.out_args[0].size = desc.length;
1392 	res = fuse_simple_request(fm, &ap.args);
1393 
1394 	fuse_invalidate_atime(inode);
1395 
1396 	if (res < 0)
1397 		return res;
1398 
1399 	if (WARN_ON(res >= PAGE_SIZE))
1400 		return -EIO;
1401 
1402 	link = page_address(page);
1403 	link[res] = '\0';
1404 
1405 	return 0;
1406 }
1407 
fuse_get_link(struct dentry * dentry,struct inode * inode,struct delayed_call * callback)1408 static const char *fuse_get_link(struct dentry *dentry, struct inode *inode,
1409 				 struct delayed_call *callback)
1410 {
1411 	struct fuse_conn *fc = get_fuse_conn(inode);
1412 	struct page *page;
1413 	int err;
1414 
1415 	err = -EIO;
1416 	if (fuse_is_bad(inode))
1417 		goto out_err;
1418 
1419 	if (fc->cache_symlinks)
1420 		return page_get_link(dentry, inode, callback);
1421 
1422 	err = -ECHILD;
1423 	if (!dentry)
1424 		goto out_err;
1425 
1426 	page = alloc_page(GFP_KERNEL);
1427 	err = -ENOMEM;
1428 	if (!page)
1429 		goto out_err;
1430 
1431 	err = fuse_readlink_page(inode, page);
1432 	if (err) {
1433 		__free_page(page);
1434 		goto out_err;
1435 	}
1436 
1437 	set_delayed_call(callback, page_put_link, page);
1438 
1439 	return page_address(page);
1440 
1441 out_err:
1442 	return ERR_PTR(err);
1443 }
1444 
fuse_dir_open(struct inode * inode,struct file * file)1445 static int fuse_dir_open(struct inode *inode, struct file *file)
1446 {
1447 	return fuse_open_common(inode, file, true);
1448 }
1449 
fuse_dir_release(struct inode * inode,struct file * file)1450 static int fuse_dir_release(struct inode *inode, struct file *file)
1451 {
1452 	fuse_release_common(file, true);
1453 
1454 	return 0;
1455 }
1456 
fuse_dir_fsync(struct file * file,loff_t start,loff_t end,int datasync)1457 static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
1458 			  int datasync)
1459 {
1460 	struct inode *inode = file->f_mapping->host;
1461 	struct fuse_conn *fc = get_fuse_conn(inode);
1462 	int err;
1463 
1464 	if (fuse_is_bad(inode))
1465 		return -EIO;
1466 
1467 	if (fc->no_fsyncdir)
1468 		return 0;
1469 
1470 	inode_lock(inode);
1471 	err = fuse_fsync_common(file, start, end, datasync, FUSE_FSYNCDIR);
1472 	if (err == -ENOSYS) {
1473 		fc->no_fsyncdir = 1;
1474 		err = 0;
1475 	}
1476 	inode_unlock(inode);
1477 
1478 	return err;
1479 }
1480 
fuse_dir_ioctl(struct file * file,unsigned int cmd,unsigned long arg)1481 static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
1482 			    unsigned long arg)
1483 {
1484 	struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1485 
1486 	/* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1487 	if (fc->minor < 18)
1488 		return -ENOTTY;
1489 
1490 	return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
1491 }
1492 
fuse_dir_compat_ioctl(struct file * file,unsigned int cmd,unsigned long arg)1493 static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
1494 				   unsigned long arg)
1495 {
1496 	struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1497 
1498 	if (fc->minor < 18)
1499 		return -ENOTTY;
1500 
1501 	return fuse_ioctl_common(file, cmd, arg,
1502 				 FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
1503 }
1504 
update_mtime(unsigned ivalid,bool trust_local_mtime)1505 static bool update_mtime(unsigned ivalid, bool trust_local_mtime)
1506 {
1507 	/* Always update if mtime is explicitly set  */
1508 	if (ivalid & ATTR_MTIME_SET)
1509 		return true;
1510 
1511 	/* Or if kernel i_mtime is the official one */
1512 	if (trust_local_mtime)
1513 		return true;
1514 
1515 	/* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1516 	if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1517 		return false;
1518 
1519 	/* In all other cases update */
1520 	return true;
1521 }
1522 
iattr_to_fattr(struct fuse_conn * fc,struct iattr * iattr,struct fuse_setattr_in * arg,bool trust_local_cmtime)1523 static void iattr_to_fattr(struct fuse_conn *fc, struct iattr *iattr,
1524 			   struct fuse_setattr_in *arg, bool trust_local_cmtime)
1525 {
1526 	unsigned ivalid = iattr->ia_valid;
1527 
1528 	if (ivalid & ATTR_MODE)
1529 		arg->valid |= FATTR_MODE,   arg->mode = iattr->ia_mode;
1530 	if (ivalid & ATTR_UID)
1531 		arg->valid |= FATTR_UID,    arg->uid = from_kuid(fc->user_ns, iattr->ia_uid);
1532 	if (ivalid & ATTR_GID)
1533 		arg->valid |= FATTR_GID,    arg->gid = from_kgid(fc->user_ns, iattr->ia_gid);
1534 	if (ivalid & ATTR_SIZE)
1535 		arg->valid |= FATTR_SIZE,   arg->size = iattr->ia_size;
1536 	if (ivalid & ATTR_ATIME) {
1537 		arg->valid |= FATTR_ATIME;
1538 		arg->atime = iattr->ia_atime.tv_sec;
1539 		arg->atimensec = iattr->ia_atime.tv_nsec;
1540 		if (!(ivalid & ATTR_ATIME_SET))
1541 			arg->valid |= FATTR_ATIME_NOW;
1542 	}
1543 	if ((ivalid & ATTR_MTIME) && update_mtime(ivalid, trust_local_cmtime)) {
1544 		arg->valid |= FATTR_MTIME;
1545 		arg->mtime = iattr->ia_mtime.tv_sec;
1546 		arg->mtimensec = iattr->ia_mtime.tv_nsec;
1547 		if (!(ivalid & ATTR_MTIME_SET) && !trust_local_cmtime)
1548 			arg->valid |= FATTR_MTIME_NOW;
1549 	}
1550 	if ((ivalid & ATTR_CTIME) && trust_local_cmtime) {
1551 		arg->valid |= FATTR_CTIME;
1552 		arg->ctime = iattr->ia_ctime.tv_sec;
1553 		arg->ctimensec = iattr->ia_ctime.tv_nsec;
1554 	}
1555 }
1556 
1557 /*
1558  * Prevent concurrent writepages on inode
1559  *
1560  * This is done by adding a negative bias to the inode write counter
1561  * and waiting for all pending writes to finish.
1562  */
fuse_set_nowrite(struct inode * inode)1563 void fuse_set_nowrite(struct inode *inode)
1564 {
1565 	struct fuse_inode *fi = get_fuse_inode(inode);
1566 
1567 	BUG_ON(!inode_is_locked(inode));
1568 
1569 	spin_lock(&fi->lock);
1570 	BUG_ON(fi->writectr < 0);
1571 	fi->writectr += FUSE_NOWRITE;
1572 	spin_unlock(&fi->lock);
1573 	wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1574 }
1575 
1576 /*
1577  * Allow writepages on inode
1578  *
1579  * Remove the bias from the writecounter and send any queued
1580  * writepages.
1581  */
__fuse_release_nowrite(struct inode * inode)1582 static void __fuse_release_nowrite(struct inode *inode)
1583 {
1584 	struct fuse_inode *fi = get_fuse_inode(inode);
1585 
1586 	BUG_ON(fi->writectr != FUSE_NOWRITE);
1587 	fi->writectr = 0;
1588 	fuse_flush_writepages(inode);
1589 }
1590 
fuse_release_nowrite(struct inode * inode)1591 void fuse_release_nowrite(struct inode *inode)
1592 {
1593 	struct fuse_inode *fi = get_fuse_inode(inode);
1594 
1595 	spin_lock(&fi->lock);
1596 	__fuse_release_nowrite(inode);
1597 	spin_unlock(&fi->lock);
1598 }
1599 
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)1600 static void fuse_setattr_fill(struct fuse_conn *fc, struct fuse_args *args,
1601 			      struct inode *inode,
1602 			      struct fuse_setattr_in *inarg_p,
1603 			      struct fuse_attr_out *outarg_p)
1604 {
1605 	args->opcode = FUSE_SETATTR;
1606 	args->nodeid = get_node_id(inode);
1607 	args->in_numargs = 1;
1608 	args->in_args[0].size = sizeof(*inarg_p);
1609 	args->in_args[0].value = inarg_p;
1610 	args->out_numargs = 1;
1611 	args->out_args[0].size = sizeof(*outarg_p);
1612 	args->out_args[0].value = outarg_p;
1613 }
1614 
1615 /*
1616  * Flush inode->i_mtime to the server
1617  */
fuse_flush_times(struct inode * inode,struct fuse_file * ff)1618 int fuse_flush_times(struct inode *inode, struct fuse_file *ff)
1619 {
1620 	struct fuse_mount *fm = get_fuse_mount(inode);
1621 	FUSE_ARGS(args);
1622 	struct fuse_setattr_in inarg;
1623 	struct fuse_attr_out outarg;
1624 
1625 	memset(&inarg, 0, sizeof(inarg));
1626 	memset(&outarg, 0, sizeof(outarg));
1627 
1628 	inarg.valid = FATTR_MTIME;
1629 	inarg.mtime = inode->i_mtime.tv_sec;
1630 	inarg.mtimensec = inode->i_mtime.tv_nsec;
1631 	if (fm->fc->minor >= 23) {
1632 		inarg.valid |= FATTR_CTIME;
1633 		inarg.ctime = inode->i_ctime.tv_sec;
1634 		inarg.ctimensec = inode->i_ctime.tv_nsec;
1635 	}
1636 	if (ff) {
1637 		inarg.valid |= FATTR_FH;
1638 		inarg.fh = ff->fh;
1639 	}
1640 	fuse_setattr_fill(fm->fc, &args, inode, &inarg, &outarg);
1641 
1642 	return fuse_simple_request(fm, &args);
1643 }
1644 
1645 /*
1646  * Set attributes, and at the same time refresh them.
1647  *
1648  * Truncation is slightly complicated, because the 'truncate' request
1649  * may fail, in which case we don't want to touch the mapping.
1650  * vmtruncate() doesn't allow for this case, so do the rlimit checking
1651  * and the actual truncation by hand.
1652  */
fuse_do_setattr(struct dentry * dentry,struct iattr * attr,struct file * file)1653 int fuse_do_setattr(struct dentry *dentry, struct iattr *attr,
1654 		    struct file *file)
1655 {
1656 	struct inode *inode = d_inode(dentry);
1657 	struct fuse_mount *fm = get_fuse_mount(inode);
1658 	struct fuse_conn *fc = fm->fc;
1659 	struct fuse_inode *fi = get_fuse_inode(inode);
1660 	FUSE_ARGS(args);
1661 	struct fuse_setattr_in inarg;
1662 	struct fuse_attr_out outarg;
1663 	bool is_truncate = false;
1664 	bool is_wb = fc->writeback_cache;
1665 	loff_t oldsize;
1666 	int err;
1667 	bool trust_local_cmtime = is_wb && S_ISREG(inode->i_mode);
1668 	bool fault_blocked = false;
1669 
1670 	if (!fc->default_permissions)
1671 		attr->ia_valid |= ATTR_FORCE;
1672 
1673 	err = setattr_prepare(dentry, attr);
1674 	if (err)
1675 		return err;
1676 
1677 	if (attr->ia_valid & ATTR_SIZE) {
1678 		if (WARN_ON(!S_ISREG(inode->i_mode)))
1679 			return -EIO;
1680 		is_truncate = true;
1681 	}
1682 
1683 	if (FUSE_IS_DAX(inode) && is_truncate) {
1684 		down_write(&fi->i_mmap_sem);
1685 		fault_blocked = true;
1686 		err = fuse_dax_break_layouts(inode, 0, 0);
1687 		if (err) {
1688 			up_write(&fi->i_mmap_sem);
1689 			return err;
1690 		}
1691 	}
1692 
1693 	if (attr->ia_valid & ATTR_OPEN) {
1694 		/* This is coming from open(..., ... | O_TRUNC); */
1695 		WARN_ON(!(attr->ia_valid & ATTR_SIZE));
1696 		WARN_ON(attr->ia_size != 0);
1697 		if (fc->atomic_o_trunc) {
1698 			/*
1699 			 * No need to send request to userspace, since actual
1700 			 * truncation has already been done by OPEN.  But still
1701 			 * need to truncate page cache.
1702 			 */
1703 			i_size_write(inode, 0);
1704 			truncate_pagecache(inode, 0);
1705 			goto out;
1706 		}
1707 		file = NULL;
1708 	}
1709 
1710 	/* Flush dirty data/metadata before non-truncate SETATTR */
1711 	if (is_wb && S_ISREG(inode->i_mode) &&
1712 	    attr->ia_valid &
1713 			(ATTR_MODE | ATTR_UID | ATTR_GID | ATTR_MTIME_SET |
1714 			 ATTR_TIMES_SET)) {
1715 		err = write_inode_now(inode, true);
1716 		if (err)
1717 			return err;
1718 
1719 		fuse_set_nowrite(inode);
1720 		fuse_release_nowrite(inode);
1721 	}
1722 
1723 	if (is_truncate) {
1724 		fuse_set_nowrite(inode);
1725 		set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1726 		if (trust_local_cmtime && attr->ia_size != inode->i_size)
1727 			attr->ia_valid |= ATTR_MTIME | ATTR_CTIME;
1728 	}
1729 
1730 	memset(&inarg, 0, sizeof(inarg));
1731 	memset(&outarg, 0, sizeof(outarg));
1732 	iattr_to_fattr(fc, attr, &inarg, trust_local_cmtime);
1733 	if (file) {
1734 		struct fuse_file *ff = file->private_data;
1735 		inarg.valid |= FATTR_FH;
1736 		inarg.fh = ff->fh;
1737 	}
1738 	if (attr->ia_valid & ATTR_SIZE) {
1739 		/* For mandatory locking in truncate */
1740 		inarg.valid |= FATTR_LOCKOWNER;
1741 		inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1742 	}
1743 	fuse_setattr_fill(fc, &args, inode, &inarg, &outarg);
1744 	err = fuse_simple_request(fm, &args);
1745 	if (err) {
1746 		if (err == -EINTR)
1747 			fuse_invalidate_attr(inode);
1748 		goto error;
1749 	}
1750 
1751 	if (fuse_invalid_attr(&outarg.attr) ||
1752 	    inode_wrong_type(inode, outarg.attr.mode)) {
1753 		fuse_make_bad(inode);
1754 		err = -EIO;
1755 		goto error;
1756 	}
1757 
1758 	spin_lock(&fi->lock);
1759 	/* the kernel maintains i_mtime locally */
1760 	if (trust_local_cmtime) {
1761 		if (attr->ia_valid & ATTR_MTIME)
1762 			inode->i_mtime = attr->ia_mtime;
1763 		if (attr->ia_valid & ATTR_CTIME)
1764 			inode->i_ctime = attr->ia_ctime;
1765 		/* FIXME: clear I_DIRTY_SYNC? */
1766 	}
1767 
1768 	fuse_change_attributes_common(inode, &outarg.attr,
1769 				      attr_timeout(&outarg));
1770 	oldsize = inode->i_size;
1771 	/* see the comment in fuse_change_attributes() */
1772 	if (!is_wb || is_truncate || !S_ISREG(inode->i_mode))
1773 		i_size_write(inode, outarg.attr.size);
1774 
1775 	if (is_truncate) {
1776 		/* NOTE: this may release/reacquire fi->lock */
1777 		__fuse_release_nowrite(inode);
1778 	}
1779 	spin_unlock(&fi->lock);
1780 
1781 	/*
1782 	 * Only call invalidate_inode_pages2() after removing
1783 	 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1784 	 */
1785 	if ((is_truncate || !is_wb) &&
1786 	    S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1787 		truncate_pagecache(inode, outarg.attr.size);
1788 		invalidate_inode_pages2(inode->i_mapping);
1789 	}
1790 
1791 	clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1792 out:
1793 	if (fault_blocked)
1794 		up_write(&fi->i_mmap_sem);
1795 
1796 	return 0;
1797 
1798 error:
1799 	if (is_truncate)
1800 		fuse_release_nowrite(inode);
1801 
1802 	clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1803 
1804 	if (fault_blocked)
1805 		up_write(&fi->i_mmap_sem);
1806 	return err;
1807 }
1808 
fuse_setattr(struct dentry * entry,struct iattr * attr)1809 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1810 {
1811 	struct inode *inode = d_inode(entry);
1812 	struct fuse_conn *fc = get_fuse_conn(inode);
1813 	struct file *file = (attr->ia_valid & ATTR_FILE) ? attr->ia_file : NULL;
1814 	int ret;
1815 
1816 	if (fuse_is_bad(inode))
1817 		return -EIO;
1818 
1819 	if (!fuse_allow_current_process(get_fuse_conn(inode)))
1820 		return -EACCES;
1821 
1822 	if (attr->ia_valid & (ATTR_KILL_SUID | ATTR_KILL_SGID)) {
1823 		attr->ia_valid &= ~(ATTR_KILL_SUID | ATTR_KILL_SGID |
1824 				    ATTR_MODE);
1825 
1826 		/*
1827 		 * The only sane way to reliably kill suid/sgid is to do it in
1828 		 * the userspace filesystem
1829 		 *
1830 		 * This should be done on write(), truncate() and chown().
1831 		 */
1832 		if (!fc->handle_killpriv) {
1833 			/*
1834 			 * ia_mode calculation may have used stale i_mode.
1835 			 * Refresh and recalculate.
1836 			 */
1837 			ret = fuse_do_getattr(inode, NULL, file);
1838 			if (ret)
1839 				return ret;
1840 
1841 			attr->ia_mode = inode->i_mode;
1842 			if (inode->i_mode & S_ISUID) {
1843 				attr->ia_valid |= ATTR_MODE;
1844 				attr->ia_mode &= ~S_ISUID;
1845 			}
1846 			if ((inode->i_mode & (S_ISGID | S_IXGRP)) == (S_ISGID | S_IXGRP)) {
1847 				attr->ia_valid |= ATTR_MODE;
1848 				attr->ia_mode &= ~S_ISGID;
1849 			}
1850 		}
1851 	}
1852 	if (!attr->ia_valid)
1853 		return 0;
1854 
1855 	ret = fuse_do_setattr(entry, attr, file);
1856 	if (!ret) {
1857 		/*
1858 		 * If filesystem supports acls it may have updated acl xattrs in
1859 		 * the filesystem, so forget cached acls for the inode.
1860 		 */
1861 		if (fc->posix_acl)
1862 			forget_all_cached_acls(inode);
1863 
1864 		/* Directory mode changed, may need to revalidate access */
1865 		if (d_is_dir(entry) && (attr->ia_valid & ATTR_MODE))
1866 			fuse_invalidate_entry_cache(entry);
1867 	}
1868 	return ret;
1869 }
1870 
fuse_getattr(const struct path * path,struct kstat * stat,u32 request_mask,unsigned int flags)1871 static int fuse_getattr(const struct path *path, struct kstat *stat,
1872 			u32 request_mask, unsigned int flags)
1873 {
1874 	struct inode *inode = d_inode(path->dentry);
1875 	struct fuse_conn *fc = get_fuse_conn(inode);
1876 
1877 	if (fuse_is_bad(inode))
1878 		return -EIO;
1879 
1880 	if (!fuse_allow_current_process(fc)) {
1881 		if (!request_mask) {
1882 			/*
1883 			 * If user explicitly requested *nothing* then don't
1884 			 * error out, but return st_dev only.
1885 			 */
1886 			stat->result_mask = 0;
1887 			stat->dev = inode->i_sb->s_dev;
1888 			return 0;
1889 		}
1890 		return -EACCES;
1891 	}
1892 
1893 	return fuse_update_get_attr(inode, NULL, stat, request_mask, flags);
1894 }
1895 
1896 static const struct inode_operations fuse_dir_inode_operations = {
1897 	.lookup		= fuse_lookup,
1898 	.mkdir		= fuse_mkdir,
1899 	.symlink	= fuse_symlink,
1900 	.unlink		= fuse_unlink,
1901 	.rmdir		= fuse_rmdir,
1902 	.rename		= fuse_rename2,
1903 	.link		= fuse_link,
1904 	.setattr	= fuse_setattr,
1905 	.create		= fuse_create,
1906 	.atomic_open	= fuse_atomic_open,
1907 	.mknod		= fuse_mknod,
1908 	.permission	= fuse_permission,
1909 	.getattr	= fuse_getattr,
1910 	.listxattr	= fuse_listxattr,
1911 	.get_acl	= fuse_get_acl,
1912 	.set_acl	= fuse_set_acl,
1913 };
1914 
1915 static const struct file_operations fuse_dir_operations = {
1916 	.llseek		= generic_file_llseek,
1917 	.read		= generic_read_dir,
1918 	.iterate_shared	= fuse_readdir,
1919 	.open		= fuse_dir_open,
1920 	.release	= fuse_dir_release,
1921 	.fsync		= fuse_dir_fsync,
1922 	.unlocked_ioctl	= fuse_dir_ioctl,
1923 	.compat_ioctl	= fuse_dir_compat_ioctl,
1924 };
1925 
1926 static const struct inode_operations fuse_common_inode_operations = {
1927 	.setattr	= fuse_setattr,
1928 	.permission	= fuse_permission,
1929 	.getattr	= fuse_getattr,
1930 	.listxattr	= fuse_listxattr,
1931 	.get_acl	= fuse_get_acl,
1932 	.set_acl	= fuse_set_acl,
1933 };
1934 
1935 static const struct inode_operations fuse_symlink_inode_operations = {
1936 	.setattr	= fuse_setattr,
1937 	.get_link	= fuse_get_link,
1938 	.getattr	= fuse_getattr,
1939 	.listxattr	= fuse_listxattr,
1940 };
1941 
fuse_init_common(struct inode * inode)1942 void fuse_init_common(struct inode *inode)
1943 {
1944 	inode->i_op = &fuse_common_inode_operations;
1945 }
1946 
fuse_init_dir(struct inode * inode)1947 void fuse_init_dir(struct inode *inode)
1948 {
1949 	struct fuse_inode *fi = get_fuse_inode(inode);
1950 
1951 	inode->i_op = &fuse_dir_inode_operations;
1952 	inode->i_fop = &fuse_dir_operations;
1953 
1954 	spin_lock_init(&fi->rdc.lock);
1955 	fi->rdc.cached = false;
1956 	fi->rdc.size = 0;
1957 	fi->rdc.pos = 0;
1958 	fi->rdc.version = 0;
1959 }
1960 
fuse_symlink_readpage(struct file * null,struct page * page)1961 static int fuse_symlink_readpage(struct file *null, struct page *page)
1962 {
1963 	int err = fuse_readlink_page(page->mapping->host, page);
1964 
1965 	if (!err)
1966 		SetPageUptodate(page);
1967 
1968 	unlock_page(page);
1969 
1970 	return err;
1971 }
1972 
1973 static const struct address_space_operations fuse_symlink_aops = {
1974 	.readpage	= fuse_symlink_readpage,
1975 };
1976 
fuse_init_symlink(struct inode * inode)1977 void fuse_init_symlink(struct inode *inode)
1978 {
1979 	inode->i_op = &fuse_symlink_inode_operations;
1980 	inode->i_data.a_ops = &fuse_symlink_aops;
1981 	inode_nohighmem(inode);
1982 }
1983