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