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