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