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