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