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