~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/fuse/dir.c

Version: ~ [ linux-5.12-rc7 ] ~ [ linux-5.11.13 ] ~ [ linux-5.10.29 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.111 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.186 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.230 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.266 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.266 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp