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