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