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