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