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