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