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