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