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