Linux 3.12.39
[linux/fpc-iii.git] / fs / fuse / dir.c
blob936d40400c56e9ef4176487e161ad0d0cfa87fd8
1 /*
2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU GPL.
6 See the file COPYING.
7 */
9 #include "fuse_i.h"
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>
17 static bool fuse_use_readdirplus(struct inode *dir, struct dir_context *ctx)
19 struct fuse_conn *fc = get_fuse_conn(dir);
20 struct fuse_inode *fi = get_fuse_inode(dir);
22 if (!fc->do_readdirplus)
23 return false;
24 if (!fc->readdirplus_auto)
25 return true;
26 if (test_and_clear_bit(FUSE_I_ADVISE_RDPLUS, &fi->state))
27 return true;
28 if (ctx->pos == 0)
29 return true;
30 return false;
33 static void fuse_advise_use_readdirplus(struct inode *dir)
35 struct fuse_inode *fi = get_fuse_inode(dir);
37 set_bit(FUSE_I_ADVISE_RDPLUS, &fi->state);
40 #if BITS_PER_LONG >= 64
41 static inline void fuse_dentry_settime(struct dentry *entry, u64 time)
43 entry->d_time = time;
46 static inline u64 fuse_dentry_time(struct dentry *entry)
48 return entry->d_time;
50 #else
52 * On 32 bit archs store the high 32 bits of time in d_fsdata
54 static void fuse_dentry_settime(struct dentry *entry, u64 time)
56 entry->d_time = time;
57 entry->d_fsdata = (void *) (unsigned long) (time >> 32);
60 static u64 fuse_dentry_time(struct dentry *entry)
62 return (u64) entry->d_time +
63 ((u64) (unsigned long) entry->d_fsdata << 32);
65 #endif
68 * FUSE caches dentries and attributes with separate timeout. The
69 * time in jiffies until the dentry/attributes are valid is stored in
70 * dentry->d_time and fuse_inode->i_time respectively.
74 * Calculate the time in jiffies until a dentry/attributes are valid
76 static u64 time_to_jiffies(unsigned long sec, unsigned long nsec)
78 if (sec || nsec) {
79 struct timespec ts = {sec, nsec};
80 return get_jiffies_64() + timespec_to_jiffies(&ts);
81 } else
82 return 0;
86 * Set dentry and possibly attribute timeouts from the lookup/mk*
87 * replies
89 static void fuse_change_entry_timeout(struct dentry *entry,
90 struct fuse_entry_out *o)
92 fuse_dentry_settime(entry,
93 time_to_jiffies(o->entry_valid, o->entry_valid_nsec));
96 static u64 attr_timeout(struct fuse_attr_out *o)
98 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
101 static u64 entry_attr_timeout(struct fuse_entry_out *o)
103 return time_to_jiffies(o->attr_valid, o->attr_valid_nsec);
107 * Mark the attributes as stale, so that at the next call to
108 * ->getattr() they will be fetched from userspace
110 void fuse_invalidate_attr(struct inode *inode)
112 get_fuse_inode(inode)->i_time = 0;
116 * Just mark the entry as stale, so that a next attempt to look it up
117 * will result in a new lookup call to userspace
119 * This is called when a dentry is about to become negative and the
120 * timeout is unknown (unlink, rmdir, rename and in some cases
121 * lookup)
123 void fuse_invalidate_entry_cache(struct dentry *entry)
125 fuse_dentry_settime(entry, 0);
129 * Same as fuse_invalidate_entry_cache(), but also try to remove the
130 * dentry from the hash
132 static void fuse_invalidate_entry(struct dentry *entry)
134 d_invalidate(entry);
135 fuse_invalidate_entry_cache(entry);
138 static void fuse_lookup_init(struct fuse_conn *fc, struct fuse_req *req,
139 u64 nodeid, struct qstr *name,
140 struct fuse_entry_out *outarg)
142 memset(outarg, 0, sizeof(struct fuse_entry_out));
143 req->in.h.opcode = FUSE_LOOKUP;
144 req->in.h.nodeid = nodeid;
145 req->in.numargs = 1;
146 req->in.args[0].size = name->len + 1;
147 req->in.args[0].value = name->name;
148 req->out.numargs = 1;
149 if (fc->minor < 9)
150 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
151 else
152 req->out.args[0].size = sizeof(struct fuse_entry_out);
153 req->out.args[0].value = outarg;
156 u64 fuse_get_attr_version(struct fuse_conn *fc)
158 u64 curr_version;
161 * The spin lock isn't actually needed on 64bit archs, but we
162 * don't yet care too much about such optimizations.
164 spin_lock(&fc->lock);
165 curr_version = fc->attr_version;
166 spin_unlock(&fc->lock);
168 return curr_version;
172 * Check whether the dentry is still valid
174 * If the entry validity timeout has expired and the dentry is
175 * positive, try to redo the lookup. If the lookup results in a
176 * different inode, then let the VFS invalidate the dentry and redo
177 * the lookup once more. If the lookup results in the same inode,
178 * then refresh the attributes, timeouts and mark the dentry valid.
180 static int fuse_dentry_revalidate(struct dentry *entry, unsigned int flags)
182 struct inode *inode;
183 struct dentry *parent;
184 struct fuse_conn *fc;
185 struct fuse_inode *fi;
186 int ret;
188 inode = ACCESS_ONCE(entry->d_inode);
189 if (inode && is_bad_inode(inode))
190 goto invalid;
191 else if (time_before64(fuse_dentry_time(entry), get_jiffies_64()) ||
192 (flags & LOOKUP_REVAL)) {
193 int err;
194 struct fuse_entry_out outarg;
195 struct fuse_req *req;
196 struct fuse_forget_link *forget;
197 u64 attr_version;
199 /* For negative dentries, always do a fresh lookup */
200 if (!inode)
201 goto invalid;
203 ret = -ECHILD;
204 if (flags & LOOKUP_RCU)
205 goto out;
207 fc = get_fuse_conn(inode);
208 req = fuse_get_req_nopages(fc);
209 ret = PTR_ERR(req);
210 if (IS_ERR(req))
211 goto out;
213 forget = fuse_alloc_forget();
214 if (!forget) {
215 fuse_put_request(fc, req);
216 ret = -ENOMEM;
217 goto out;
220 attr_version = fuse_get_attr_version(fc);
222 parent = dget_parent(entry);
223 fuse_lookup_init(fc, req, get_node_id(parent->d_inode),
224 &entry->d_name, &outarg);
225 fuse_request_send(fc, req);
226 dput(parent);
227 err = req->out.h.error;
228 fuse_put_request(fc, req);
229 /* Zero nodeid is same as -ENOENT */
230 if (!err && !outarg.nodeid)
231 err = -ENOENT;
232 if (!err) {
233 fi = get_fuse_inode(inode);
234 if (outarg.nodeid != get_node_id(inode)) {
235 fuse_queue_forget(fc, forget, outarg.nodeid, 1);
236 goto invalid;
238 spin_lock(&fc->lock);
239 fi->nlookup++;
240 spin_unlock(&fc->lock);
242 kfree(forget);
243 if (err || (outarg.attr.mode ^ inode->i_mode) & S_IFMT)
244 goto invalid;
246 fuse_change_attributes(inode, &outarg.attr,
247 entry_attr_timeout(&outarg),
248 attr_version);
249 fuse_change_entry_timeout(entry, &outarg);
250 } else if (inode) {
251 fi = get_fuse_inode(inode);
252 if (flags & LOOKUP_RCU) {
253 if (test_bit(FUSE_I_INIT_RDPLUS, &fi->state))
254 return -ECHILD;
255 } else if (test_and_clear_bit(FUSE_I_INIT_RDPLUS, &fi->state)) {
256 parent = dget_parent(entry);
257 fuse_advise_use_readdirplus(parent->d_inode);
258 dput(parent);
261 ret = 1;
262 out:
263 return ret;
265 invalid:
266 ret = 0;
268 if (!(flags & LOOKUP_RCU) && check_submounts_and_drop(entry) != 0)
269 ret = 1;
270 goto out;
273 static int invalid_nodeid(u64 nodeid)
275 return !nodeid || nodeid == FUSE_ROOT_ID;
278 const struct dentry_operations fuse_dentry_operations = {
279 .d_revalidate = fuse_dentry_revalidate,
282 int fuse_valid_type(int m)
284 return S_ISREG(m) || S_ISDIR(m) || S_ISLNK(m) || S_ISCHR(m) ||
285 S_ISBLK(m) || S_ISFIFO(m) || S_ISSOCK(m);
288 int fuse_lookup_name(struct super_block *sb, u64 nodeid, struct qstr *name,
289 struct fuse_entry_out *outarg, struct inode **inode)
291 struct fuse_conn *fc = get_fuse_conn_super(sb);
292 struct fuse_req *req;
293 struct fuse_forget_link *forget;
294 u64 attr_version;
295 int err;
297 *inode = NULL;
298 err = -ENAMETOOLONG;
299 if (name->len > FUSE_NAME_MAX)
300 goto out;
302 req = fuse_get_req_nopages(fc);
303 err = PTR_ERR(req);
304 if (IS_ERR(req))
305 goto out;
307 forget = fuse_alloc_forget();
308 err = -ENOMEM;
309 if (!forget) {
310 fuse_put_request(fc, req);
311 goto out;
314 attr_version = fuse_get_attr_version(fc);
316 fuse_lookup_init(fc, req, nodeid, name, outarg);
317 fuse_request_send(fc, req);
318 err = req->out.h.error;
319 fuse_put_request(fc, req);
320 /* Zero nodeid is same as -ENOENT, but with valid timeout */
321 if (err || !outarg->nodeid)
322 goto out_put_forget;
324 err = -EIO;
325 if (!outarg->nodeid)
326 goto out_put_forget;
327 if (!fuse_valid_type(outarg->attr.mode))
328 goto out_put_forget;
330 *inode = fuse_iget(sb, outarg->nodeid, outarg->generation,
331 &outarg->attr, entry_attr_timeout(outarg),
332 attr_version);
333 err = -ENOMEM;
334 if (!*inode) {
335 fuse_queue_forget(fc, forget, outarg->nodeid, 1);
336 goto out;
338 err = 0;
340 out_put_forget:
341 kfree(forget);
342 out:
343 return err;
346 static struct dentry *fuse_materialise_dentry(struct dentry *dentry,
347 struct inode *inode)
349 struct dentry *newent;
351 if (inode && S_ISDIR(inode->i_mode)) {
352 struct fuse_conn *fc = get_fuse_conn(inode);
354 mutex_lock(&fc->inst_mutex);
355 newent = d_materialise_unique(dentry, inode);
356 mutex_unlock(&fc->inst_mutex);
357 } else {
358 newent = d_materialise_unique(dentry, inode);
361 return newent;
364 static struct dentry *fuse_lookup(struct inode *dir, struct dentry *entry,
365 unsigned int flags)
367 int err;
368 struct fuse_entry_out outarg;
369 struct inode *inode;
370 struct dentry *newent;
371 bool outarg_valid = true;
373 err = fuse_lookup_name(dir->i_sb, get_node_id(dir), &entry->d_name,
374 &outarg, &inode);
375 if (err == -ENOENT) {
376 outarg_valid = false;
377 err = 0;
379 if (err)
380 goto out_err;
382 err = -EIO;
383 if (inode && get_node_id(inode) == FUSE_ROOT_ID)
384 goto out_iput;
386 newent = fuse_materialise_dentry(entry, inode);
387 err = PTR_ERR(newent);
388 if (IS_ERR(newent))
389 goto out_err;
391 entry = newent ? newent : entry;
392 if (outarg_valid)
393 fuse_change_entry_timeout(entry, &outarg);
394 else
395 fuse_invalidate_entry_cache(entry);
397 fuse_advise_use_readdirplus(dir);
398 return newent;
400 out_iput:
401 iput(inode);
402 out_err:
403 return ERR_PTR(err);
407 * Atomic create+open operation
409 * If the filesystem doesn't support this, then fall back to separate
410 * 'mknod' + 'open' requests.
412 static int fuse_create_open(struct inode *dir, struct dentry *entry,
413 struct file *file, unsigned flags,
414 umode_t mode, int *opened)
416 int err;
417 struct inode *inode;
418 struct fuse_conn *fc = get_fuse_conn(dir);
419 struct fuse_req *req;
420 struct fuse_forget_link *forget;
421 struct fuse_create_in inarg;
422 struct fuse_open_out outopen;
423 struct fuse_entry_out outentry;
424 struct fuse_file *ff;
426 /* Userspace expects S_IFREG in create mode */
427 BUG_ON((mode & S_IFMT) != S_IFREG);
429 forget = fuse_alloc_forget();
430 err = -ENOMEM;
431 if (!forget)
432 goto out_err;
434 req = fuse_get_req_nopages(fc);
435 err = PTR_ERR(req);
436 if (IS_ERR(req))
437 goto out_put_forget_req;
439 err = -ENOMEM;
440 ff = fuse_file_alloc(fc);
441 if (!ff)
442 goto out_put_request;
444 if (!fc->dont_mask)
445 mode &= ~current_umask();
447 flags &= ~O_NOCTTY;
448 memset(&inarg, 0, sizeof(inarg));
449 memset(&outentry, 0, sizeof(outentry));
450 inarg.flags = flags;
451 inarg.mode = mode;
452 inarg.umask = current_umask();
453 req->in.h.opcode = FUSE_CREATE;
454 req->in.h.nodeid = get_node_id(dir);
455 req->in.numargs = 2;
456 req->in.args[0].size = fc->minor < 12 ? sizeof(struct fuse_open_in) :
457 sizeof(inarg);
458 req->in.args[0].value = &inarg;
459 req->in.args[1].size = entry->d_name.len + 1;
460 req->in.args[1].value = entry->d_name.name;
461 req->out.numargs = 2;
462 if (fc->minor < 9)
463 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
464 else
465 req->out.args[0].size = sizeof(outentry);
466 req->out.args[0].value = &outentry;
467 req->out.args[1].size = sizeof(outopen);
468 req->out.args[1].value = &outopen;
469 fuse_request_send(fc, req);
470 err = req->out.h.error;
471 if (err)
472 goto out_free_ff;
474 err = -EIO;
475 if (!S_ISREG(outentry.attr.mode) || invalid_nodeid(outentry.nodeid))
476 goto out_free_ff;
478 fuse_put_request(fc, req);
479 ff->fh = outopen.fh;
480 ff->nodeid = outentry.nodeid;
481 ff->open_flags = outopen.open_flags;
482 inode = fuse_iget(dir->i_sb, outentry.nodeid, outentry.generation,
483 &outentry.attr, entry_attr_timeout(&outentry), 0);
484 if (!inode) {
485 flags &= ~(O_CREAT | O_EXCL | O_TRUNC);
486 fuse_sync_release(ff, flags);
487 fuse_queue_forget(fc, forget, outentry.nodeid, 1);
488 err = -ENOMEM;
489 goto out_err;
491 kfree(forget);
492 d_instantiate(entry, inode);
493 fuse_change_entry_timeout(entry, &outentry);
494 fuse_invalidate_attr(dir);
495 err = finish_open(file, entry, generic_file_open, opened);
496 if (err) {
497 fuse_sync_release(ff, flags);
498 } else {
499 file->private_data = fuse_file_get(ff);
500 fuse_finish_open(inode, file);
502 return err;
504 out_free_ff:
505 fuse_file_free(ff);
506 out_put_request:
507 fuse_put_request(fc, req);
508 out_put_forget_req:
509 kfree(forget);
510 out_err:
511 return err;
514 static int fuse_mknod(struct inode *, struct dentry *, umode_t, dev_t);
515 static int fuse_atomic_open(struct inode *dir, struct dentry *entry,
516 struct file *file, unsigned flags,
517 umode_t mode, int *opened)
519 int err;
520 struct fuse_conn *fc = get_fuse_conn(dir);
521 struct dentry *res = NULL;
523 if (d_unhashed(entry)) {
524 res = fuse_lookup(dir, entry, 0);
525 if (IS_ERR(res))
526 return PTR_ERR(res);
528 if (res)
529 entry = res;
532 if (!(flags & O_CREAT) || entry->d_inode)
533 goto no_open;
535 /* Only creates */
536 *opened |= FILE_CREATED;
538 if (fc->no_create)
539 goto mknod;
541 err = fuse_create_open(dir, entry, file, flags, mode, opened);
542 if (err == -ENOSYS) {
543 fc->no_create = 1;
544 goto mknod;
546 out_dput:
547 dput(res);
548 return err;
550 mknod:
551 err = fuse_mknod(dir, entry, mode, 0);
552 if (err)
553 goto out_dput;
554 no_open:
555 return finish_no_open(file, res);
559 * Code shared between mknod, mkdir, symlink and link
561 static int create_new_entry(struct fuse_conn *fc, struct fuse_req *req,
562 struct inode *dir, struct dentry *entry,
563 umode_t mode)
565 struct fuse_entry_out outarg;
566 struct inode *inode;
567 int err;
568 struct fuse_forget_link *forget;
570 forget = fuse_alloc_forget();
571 if (!forget) {
572 fuse_put_request(fc, req);
573 return -ENOMEM;
576 memset(&outarg, 0, sizeof(outarg));
577 req->in.h.nodeid = get_node_id(dir);
578 req->out.numargs = 1;
579 if (fc->minor < 9)
580 req->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
581 else
582 req->out.args[0].size = sizeof(outarg);
583 req->out.args[0].value = &outarg;
584 fuse_request_send(fc, req);
585 err = req->out.h.error;
586 fuse_put_request(fc, req);
587 if (err)
588 goto out_put_forget_req;
590 err = -EIO;
591 if (invalid_nodeid(outarg.nodeid))
592 goto out_put_forget_req;
594 if ((outarg.attr.mode ^ mode) & S_IFMT)
595 goto out_put_forget_req;
597 inode = fuse_iget(dir->i_sb, outarg.nodeid, outarg.generation,
598 &outarg.attr, entry_attr_timeout(&outarg), 0);
599 if (!inode) {
600 fuse_queue_forget(fc, forget, outarg.nodeid, 1);
601 return -ENOMEM;
603 kfree(forget);
605 if (S_ISDIR(inode->i_mode)) {
606 struct dentry *alias;
607 mutex_lock(&fc->inst_mutex);
608 alias = d_find_alias(inode);
609 if (alias) {
610 /* New directory must have moved since mkdir */
611 mutex_unlock(&fc->inst_mutex);
612 dput(alias);
613 iput(inode);
614 return -EBUSY;
616 d_instantiate(entry, inode);
617 mutex_unlock(&fc->inst_mutex);
618 } else
619 d_instantiate(entry, inode);
621 fuse_change_entry_timeout(entry, &outarg);
622 fuse_invalidate_attr(dir);
623 return 0;
625 out_put_forget_req:
626 kfree(forget);
627 return err;
630 static int fuse_mknod(struct inode *dir, struct dentry *entry, umode_t mode,
631 dev_t rdev)
633 struct fuse_mknod_in inarg;
634 struct fuse_conn *fc = get_fuse_conn(dir);
635 struct fuse_req *req = fuse_get_req_nopages(fc);
636 if (IS_ERR(req))
637 return PTR_ERR(req);
639 if (!fc->dont_mask)
640 mode &= ~current_umask();
642 memset(&inarg, 0, sizeof(inarg));
643 inarg.mode = mode;
644 inarg.rdev = new_encode_dev(rdev);
645 inarg.umask = current_umask();
646 req->in.h.opcode = FUSE_MKNOD;
647 req->in.numargs = 2;
648 req->in.args[0].size = fc->minor < 12 ? FUSE_COMPAT_MKNOD_IN_SIZE :
649 sizeof(inarg);
650 req->in.args[0].value = &inarg;
651 req->in.args[1].size = entry->d_name.len + 1;
652 req->in.args[1].value = entry->d_name.name;
653 return create_new_entry(fc, req, dir, entry, mode);
656 static int fuse_create(struct inode *dir, struct dentry *entry, umode_t mode,
657 bool excl)
659 return fuse_mknod(dir, entry, mode, 0);
662 static int fuse_mkdir(struct inode *dir, struct dentry *entry, umode_t mode)
664 struct fuse_mkdir_in inarg;
665 struct fuse_conn *fc = get_fuse_conn(dir);
666 struct fuse_req *req = fuse_get_req_nopages(fc);
667 if (IS_ERR(req))
668 return PTR_ERR(req);
670 if (!fc->dont_mask)
671 mode &= ~current_umask();
673 memset(&inarg, 0, sizeof(inarg));
674 inarg.mode = mode;
675 inarg.umask = current_umask();
676 req->in.h.opcode = FUSE_MKDIR;
677 req->in.numargs = 2;
678 req->in.args[0].size = sizeof(inarg);
679 req->in.args[0].value = &inarg;
680 req->in.args[1].size = entry->d_name.len + 1;
681 req->in.args[1].value = entry->d_name.name;
682 return create_new_entry(fc, req, dir, entry, S_IFDIR);
685 static int fuse_symlink(struct inode *dir, struct dentry *entry,
686 const char *link)
688 struct fuse_conn *fc = get_fuse_conn(dir);
689 unsigned len = strlen(link) + 1;
690 struct fuse_req *req = fuse_get_req_nopages(fc);
691 if (IS_ERR(req))
692 return PTR_ERR(req);
694 req->in.h.opcode = FUSE_SYMLINK;
695 req->in.numargs = 2;
696 req->in.args[0].size = entry->d_name.len + 1;
697 req->in.args[0].value = entry->d_name.name;
698 req->in.args[1].size = len;
699 req->in.args[1].value = link;
700 return create_new_entry(fc, req, dir, entry, S_IFLNK);
703 static int fuse_unlink(struct inode *dir, struct dentry *entry)
705 int err;
706 struct fuse_conn *fc = get_fuse_conn(dir);
707 struct fuse_req *req = fuse_get_req_nopages(fc);
708 if (IS_ERR(req))
709 return PTR_ERR(req);
711 req->in.h.opcode = FUSE_UNLINK;
712 req->in.h.nodeid = get_node_id(dir);
713 req->in.numargs = 1;
714 req->in.args[0].size = entry->d_name.len + 1;
715 req->in.args[0].value = entry->d_name.name;
716 fuse_request_send(fc, req);
717 err = req->out.h.error;
718 fuse_put_request(fc, req);
719 if (!err) {
720 struct inode *inode = entry->d_inode;
721 struct fuse_inode *fi = get_fuse_inode(inode);
723 spin_lock(&fc->lock);
724 fi->attr_version = ++fc->attr_version;
726 * If i_nlink == 0 then unlink doesn't make sense, yet this can
727 * happen if userspace filesystem is careless. It would be
728 * difficult to enforce correct nlink usage so just ignore this
729 * condition here
731 if (inode->i_nlink > 0)
732 drop_nlink(inode);
733 spin_unlock(&fc->lock);
734 fuse_invalidate_attr(inode);
735 fuse_invalidate_attr(dir);
736 fuse_invalidate_entry_cache(entry);
737 } else if (err == -EINTR)
738 fuse_invalidate_entry(entry);
739 return err;
742 static int fuse_rmdir(struct inode *dir, struct dentry *entry)
744 int err;
745 struct fuse_conn *fc = get_fuse_conn(dir);
746 struct fuse_req *req = fuse_get_req_nopages(fc);
747 if (IS_ERR(req))
748 return PTR_ERR(req);
750 req->in.h.opcode = FUSE_RMDIR;
751 req->in.h.nodeid = get_node_id(dir);
752 req->in.numargs = 1;
753 req->in.args[0].size = entry->d_name.len + 1;
754 req->in.args[0].value = entry->d_name.name;
755 fuse_request_send(fc, req);
756 err = req->out.h.error;
757 fuse_put_request(fc, req);
758 if (!err) {
759 clear_nlink(entry->d_inode);
760 fuse_invalidate_attr(dir);
761 fuse_invalidate_entry_cache(entry);
762 } else if (err == -EINTR)
763 fuse_invalidate_entry(entry);
764 return err;
767 static int fuse_rename(struct inode *olddir, struct dentry *oldent,
768 struct inode *newdir, struct dentry *newent)
770 int err;
771 struct fuse_rename_in inarg;
772 struct fuse_conn *fc = get_fuse_conn(olddir);
773 struct fuse_req *req = fuse_get_req_nopages(fc);
775 if (IS_ERR(req))
776 return PTR_ERR(req);
778 memset(&inarg, 0, sizeof(inarg));
779 inarg.newdir = get_node_id(newdir);
780 req->in.h.opcode = FUSE_RENAME;
781 req->in.h.nodeid = get_node_id(olddir);
782 req->in.numargs = 3;
783 req->in.args[0].size = sizeof(inarg);
784 req->in.args[0].value = &inarg;
785 req->in.args[1].size = oldent->d_name.len + 1;
786 req->in.args[1].value = oldent->d_name.name;
787 req->in.args[2].size = newent->d_name.len + 1;
788 req->in.args[2].value = newent->d_name.name;
789 fuse_request_send(fc, req);
790 err = req->out.h.error;
791 fuse_put_request(fc, req);
792 if (!err) {
793 /* ctime changes */
794 fuse_invalidate_attr(oldent->d_inode);
796 fuse_invalidate_attr(olddir);
797 if (olddir != newdir)
798 fuse_invalidate_attr(newdir);
800 /* newent will end up negative */
801 if (newent->d_inode) {
802 fuse_invalidate_attr(newent->d_inode);
803 fuse_invalidate_entry_cache(newent);
805 } else if (err == -EINTR) {
806 /* If request was interrupted, DEITY only knows if the
807 rename actually took place. If the invalidation
808 fails (e.g. some process has CWD under the renamed
809 directory), then there can be inconsistency between
810 the dcache and the real filesystem. Tough luck. */
811 fuse_invalidate_entry(oldent);
812 if (newent->d_inode)
813 fuse_invalidate_entry(newent);
816 return err;
819 static int fuse_link(struct dentry *entry, struct inode *newdir,
820 struct dentry *newent)
822 int err;
823 struct fuse_link_in inarg;
824 struct inode *inode = entry->d_inode;
825 struct fuse_conn *fc = get_fuse_conn(inode);
826 struct fuse_req *req = fuse_get_req_nopages(fc);
827 if (IS_ERR(req))
828 return PTR_ERR(req);
830 memset(&inarg, 0, sizeof(inarg));
831 inarg.oldnodeid = get_node_id(inode);
832 req->in.h.opcode = FUSE_LINK;
833 req->in.numargs = 2;
834 req->in.args[0].size = sizeof(inarg);
835 req->in.args[0].value = &inarg;
836 req->in.args[1].size = newent->d_name.len + 1;
837 req->in.args[1].value = newent->d_name.name;
838 err = create_new_entry(fc, req, newdir, newent, inode->i_mode);
839 /* Contrary to "normal" filesystems it can happen that link
840 makes two "logical" inodes point to the same "physical"
841 inode. We invalidate the attributes of the old one, so it
842 will reflect changes in the backing inode (link count,
843 etc.)
845 if (!err) {
846 struct fuse_inode *fi = get_fuse_inode(inode);
848 spin_lock(&fc->lock);
849 fi->attr_version = ++fc->attr_version;
850 inc_nlink(inode);
851 spin_unlock(&fc->lock);
852 fuse_invalidate_attr(inode);
853 } else if (err == -EINTR) {
854 fuse_invalidate_attr(inode);
856 return err;
859 static void fuse_fillattr(struct inode *inode, struct fuse_attr *attr,
860 struct kstat *stat)
862 unsigned int blkbits;
864 stat->dev = inode->i_sb->s_dev;
865 stat->ino = attr->ino;
866 stat->mode = (inode->i_mode & S_IFMT) | (attr->mode & 07777);
867 stat->nlink = attr->nlink;
868 stat->uid = make_kuid(&init_user_ns, attr->uid);
869 stat->gid = make_kgid(&init_user_ns, attr->gid);
870 stat->rdev = inode->i_rdev;
871 stat->atime.tv_sec = attr->atime;
872 stat->atime.tv_nsec = attr->atimensec;
873 stat->mtime.tv_sec = attr->mtime;
874 stat->mtime.tv_nsec = attr->mtimensec;
875 stat->ctime.tv_sec = attr->ctime;
876 stat->ctime.tv_nsec = attr->ctimensec;
877 stat->size = attr->size;
878 stat->blocks = attr->blocks;
880 if (attr->blksize != 0)
881 blkbits = ilog2(attr->blksize);
882 else
883 blkbits = inode->i_sb->s_blocksize_bits;
885 stat->blksize = 1 << blkbits;
888 static int fuse_do_getattr(struct inode *inode, struct kstat *stat,
889 struct file *file)
891 int err;
892 struct fuse_getattr_in inarg;
893 struct fuse_attr_out outarg;
894 struct fuse_conn *fc = get_fuse_conn(inode);
895 struct fuse_req *req;
896 u64 attr_version;
898 req = fuse_get_req_nopages(fc);
899 if (IS_ERR(req))
900 return PTR_ERR(req);
902 attr_version = fuse_get_attr_version(fc);
904 memset(&inarg, 0, sizeof(inarg));
905 memset(&outarg, 0, sizeof(outarg));
906 /* Directories have separate file-handle space */
907 if (file && S_ISREG(inode->i_mode)) {
908 struct fuse_file *ff = file->private_data;
910 inarg.getattr_flags |= FUSE_GETATTR_FH;
911 inarg.fh = ff->fh;
913 req->in.h.opcode = FUSE_GETATTR;
914 req->in.h.nodeid = get_node_id(inode);
915 req->in.numargs = 1;
916 req->in.args[0].size = sizeof(inarg);
917 req->in.args[0].value = &inarg;
918 req->out.numargs = 1;
919 if (fc->minor < 9)
920 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
921 else
922 req->out.args[0].size = sizeof(outarg);
923 req->out.args[0].value = &outarg;
924 fuse_request_send(fc, req);
925 err = req->out.h.error;
926 fuse_put_request(fc, req);
927 if (!err) {
928 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
929 make_bad_inode(inode);
930 err = -EIO;
931 } else {
932 fuse_change_attributes(inode, &outarg.attr,
933 attr_timeout(&outarg),
934 attr_version);
935 if (stat)
936 fuse_fillattr(inode, &outarg.attr, stat);
939 return err;
942 int fuse_update_attributes(struct inode *inode, struct kstat *stat,
943 struct file *file, bool *refreshed)
945 struct fuse_inode *fi = get_fuse_inode(inode);
946 int err;
947 bool r;
949 if (time_before64(fi->i_time, get_jiffies_64())) {
950 r = true;
951 err = fuse_do_getattr(inode, stat, file);
952 } else {
953 r = false;
954 err = 0;
955 if (stat) {
956 generic_fillattr(inode, stat);
957 stat->mode = fi->orig_i_mode;
958 stat->ino = fi->orig_ino;
962 if (refreshed != NULL)
963 *refreshed = r;
965 return err;
968 int fuse_reverse_inval_entry(struct super_block *sb, u64 parent_nodeid,
969 u64 child_nodeid, struct qstr *name)
971 int err = -ENOTDIR;
972 struct inode *parent;
973 struct dentry *dir;
974 struct dentry *entry;
976 parent = ilookup5(sb, parent_nodeid, fuse_inode_eq, &parent_nodeid);
977 if (!parent)
978 return -ENOENT;
980 mutex_lock(&parent->i_mutex);
981 if (!S_ISDIR(parent->i_mode))
982 goto unlock;
984 err = -ENOENT;
985 dir = d_find_alias(parent);
986 if (!dir)
987 goto unlock;
989 entry = d_lookup(dir, name);
990 dput(dir);
991 if (!entry)
992 goto unlock;
994 fuse_invalidate_attr(parent);
995 fuse_invalidate_entry(entry);
997 if (child_nodeid != 0 && entry->d_inode) {
998 mutex_lock(&entry->d_inode->i_mutex);
999 if (get_node_id(entry->d_inode) != child_nodeid) {
1000 err = -ENOENT;
1001 goto badentry;
1003 if (d_mountpoint(entry)) {
1004 err = -EBUSY;
1005 goto badentry;
1007 if (S_ISDIR(entry->d_inode->i_mode)) {
1008 shrink_dcache_parent(entry);
1009 if (!simple_empty(entry)) {
1010 err = -ENOTEMPTY;
1011 goto badentry;
1013 entry->d_inode->i_flags |= S_DEAD;
1015 dont_mount(entry);
1016 clear_nlink(entry->d_inode);
1017 err = 0;
1018 badentry:
1019 mutex_unlock(&entry->d_inode->i_mutex);
1020 if (!err)
1021 d_delete(entry);
1022 } else {
1023 err = 0;
1025 dput(entry);
1027 unlock:
1028 mutex_unlock(&parent->i_mutex);
1029 iput(parent);
1030 return err;
1034 * Calling into a user-controlled filesystem gives the filesystem
1035 * daemon ptrace-like capabilities over the current process. This
1036 * means, that the filesystem daemon is able to record the exact
1037 * filesystem operations performed, and can also control the behavior
1038 * of the requester process in otherwise impossible ways. For example
1039 * it can delay the operation for arbitrary length of time allowing
1040 * DoS against the requester.
1042 * For this reason only those processes can call into the filesystem,
1043 * for which the owner of the mount has ptrace privilege. This
1044 * excludes processes started by other users, suid or sgid processes.
1046 int fuse_allow_current_process(struct fuse_conn *fc)
1048 const struct cred *cred;
1050 if (fc->flags & FUSE_ALLOW_OTHER)
1051 return 1;
1053 cred = current_cred();
1054 if (uid_eq(cred->euid, fc->user_id) &&
1055 uid_eq(cred->suid, fc->user_id) &&
1056 uid_eq(cred->uid, fc->user_id) &&
1057 gid_eq(cred->egid, fc->group_id) &&
1058 gid_eq(cred->sgid, fc->group_id) &&
1059 gid_eq(cred->gid, fc->group_id))
1060 return 1;
1062 return 0;
1065 static int fuse_access(struct inode *inode, int mask)
1067 struct fuse_conn *fc = get_fuse_conn(inode);
1068 struct fuse_req *req;
1069 struct fuse_access_in inarg;
1070 int err;
1072 BUG_ON(mask & MAY_NOT_BLOCK);
1074 if (fc->no_access)
1075 return 0;
1077 req = fuse_get_req_nopages(fc);
1078 if (IS_ERR(req))
1079 return PTR_ERR(req);
1081 memset(&inarg, 0, sizeof(inarg));
1082 inarg.mask = mask & (MAY_READ | MAY_WRITE | MAY_EXEC);
1083 req->in.h.opcode = FUSE_ACCESS;
1084 req->in.h.nodeid = get_node_id(inode);
1085 req->in.numargs = 1;
1086 req->in.args[0].size = sizeof(inarg);
1087 req->in.args[0].value = &inarg;
1088 fuse_request_send(fc, req);
1089 err = req->out.h.error;
1090 fuse_put_request(fc, req);
1091 if (err == -ENOSYS) {
1092 fc->no_access = 1;
1093 err = 0;
1095 return err;
1098 static int fuse_perm_getattr(struct inode *inode, int mask)
1100 if (mask & MAY_NOT_BLOCK)
1101 return -ECHILD;
1103 return fuse_do_getattr(inode, NULL, NULL);
1107 * Check permission. The two basic access models of FUSE are:
1109 * 1) Local access checking ('default_permissions' mount option) based
1110 * on file mode. This is the plain old disk filesystem permission
1111 * modell.
1113 * 2) "Remote" access checking, where server is responsible for
1114 * checking permission in each inode operation. An exception to this
1115 * is if ->permission() was invoked from sys_access() in which case an
1116 * access request is sent. Execute permission is still checked
1117 * locally based on file mode.
1119 static int fuse_permission(struct inode *inode, int mask)
1121 struct fuse_conn *fc = get_fuse_conn(inode);
1122 bool refreshed = false;
1123 int err = 0;
1125 if (!fuse_allow_current_process(fc))
1126 return -EACCES;
1129 * If attributes are needed, refresh them before proceeding
1131 if ((fc->flags & FUSE_DEFAULT_PERMISSIONS) ||
1132 ((mask & MAY_EXEC) && S_ISREG(inode->i_mode))) {
1133 struct fuse_inode *fi = get_fuse_inode(inode);
1135 if (time_before64(fi->i_time, get_jiffies_64())) {
1136 refreshed = true;
1138 err = fuse_perm_getattr(inode, mask);
1139 if (err)
1140 return err;
1144 if (fc->flags & FUSE_DEFAULT_PERMISSIONS) {
1145 err = generic_permission(inode, mask);
1147 /* If permission is denied, try to refresh file
1148 attributes. This is also needed, because the root
1149 node will at first have no permissions */
1150 if (err == -EACCES && !refreshed) {
1151 err = fuse_perm_getattr(inode, mask);
1152 if (!err)
1153 err = generic_permission(inode, mask);
1156 /* Note: the opposite of the above test does not
1157 exist. So if permissions are revoked this won't be
1158 noticed immediately, only after the attribute
1159 timeout has expired */
1160 } else if (mask & (MAY_ACCESS | MAY_CHDIR)) {
1161 err = fuse_access(inode, mask);
1162 } else if ((mask & MAY_EXEC) && S_ISREG(inode->i_mode)) {
1163 if (!(inode->i_mode & S_IXUGO)) {
1164 if (refreshed)
1165 return -EACCES;
1167 err = fuse_perm_getattr(inode, mask);
1168 if (!err && !(inode->i_mode & S_IXUGO))
1169 return -EACCES;
1172 return err;
1175 static int parse_dirfile(char *buf, size_t nbytes, struct file *file,
1176 struct dir_context *ctx)
1178 while (nbytes >= FUSE_NAME_OFFSET) {
1179 struct fuse_dirent *dirent = (struct fuse_dirent *) buf;
1180 size_t reclen = FUSE_DIRENT_SIZE(dirent);
1181 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1182 return -EIO;
1183 if (reclen > nbytes)
1184 break;
1185 if (memchr(dirent->name, '/', dirent->namelen) != NULL)
1186 return -EIO;
1188 if (!dir_emit(ctx, dirent->name, dirent->namelen,
1189 dirent->ino, dirent->type))
1190 break;
1192 buf += reclen;
1193 nbytes -= reclen;
1194 ctx->pos = dirent->off;
1197 return 0;
1200 static int fuse_direntplus_link(struct file *file,
1201 struct fuse_direntplus *direntplus,
1202 u64 attr_version)
1204 int err;
1205 struct fuse_entry_out *o = &direntplus->entry_out;
1206 struct fuse_dirent *dirent = &direntplus->dirent;
1207 struct dentry *parent = file->f_path.dentry;
1208 struct qstr name = QSTR_INIT(dirent->name, dirent->namelen);
1209 struct dentry *dentry;
1210 struct dentry *alias;
1211 struct inode *dir = parent->d_inode;
1212 struct fuse_conn *fc;
1213 struct inode *inode;
1215 if (!o->nodeid) {
1217 * Unlike in the case of fuse_lookup, zero nodeid does not mean
1218 * ENOENT. Instead, it only means the userspace filesystem did
1219 * not want to return attributes/handle for this entry.
1221 * So do nothing.
1223 return 0;
1226 if (name.name[0] == '.') {
1228 * We could potentially refresh the attributes of the directory
1229 * and its parent?
1231 if (name.len == 1)
1232 return 0;
1233 if (name.name[1] == '.' && name.len == 2)
1234 return 0;
1237 if (invalid_nodeid(o->nodeid))
1238 return -EIO;
1239 if (!fuse_valid_type(o->attr.mode))
1240 return -EIO;
1242 fc = get_fuse_conn(dir);
1244 name.hash = full_name_hash(name.name, name.len);
1245 dentry = d_lookup(parent, &name);
1246 if (dentry) {
1247 inode = dentry->d_inode;
1248 if (!inode) {
1249 d_drop(dentry);
1250 } else if (get_node_id(inode) != o->nodeid ||
1251 ((o->attr.mode ^ inode->i_mode) & S_IFMT)) {
1252 err = d_invalidate(dentry);
1253 if (err)
1254 goto out;
1255 } else if (is_bad_inode(inode)) {
1256 err = -EIO;
1257 goto out;
1258 } else {
1259 struct fuse_inode *fi;
1260 fi = get_fuse_inode(inode);
1261 spin_lock(&fc->lock);
1262 fi->nlookup++;
1263 spin_unlock(&fc->lock);
1265 fuse_change_attributes(inode, &o->attr,
1266 entry_attr_timeout(o),
1267 attr_version);
1270 * The other branch to 'found' comes via fuse_iget()
1271 * which bumps nlookup inside
1273 goto found;
1275 dput(dentry);
1278 dentry = d_alloc(parent, &name);
1279 err = -ENOMEM;
1280 if (!dentry)
1281 goto out;
1283 inode = fuse_iget(dir->i_sb, o->nodeid, o->generation,
1284 &o->attr, entry_attr_timeout(o), attr_version);
1285 if (!inode)
1286 goto out;
1288 alias = fuse_materialise_dentry(dentry, inode);
1289 err = PTR_ERR(alias);
1290 if (IS_ERR(alias))
1291 goto out;
1293 if (alias) {
1294 dput(dentry);
1295 dentry = alias;
1298 found:
1299 if (fc->readdirplus_auto)
1300 set_bit(FUSE_I_INIT_RDPLUS, &get_fuse_inode(inode)->state);
1301 fuse_change_entry_timeout(dentry, o);
1303 err = 0;
1304 out:
1305 dput(dentry);
1306 return err;
1309 static int parse_dirplusfile(char *buf, size_t nbytes, struct file *file,
1310 struct dir_context *ctx, u64 attr_version)
1312 struct fuse_direntplus *direntplus;
1313 struct fuse_dirent *dirent;
1314 size_t reclen;
1315 int over = 0;
1316 int ret;
1318 while (nbytes >= FUSE_NAME_OFFSET_DIRENTPLUS) {
1319 direntplus = (struct fuse_direntplus *) buf;
1320 dirent = &direntplus->dirent;
1321 reclen = FUSE_DIRENTPLUS_SIZE(direntplus);
1323 if (!dirent->namelen || dirent->namelen > FUSE_NAME_MAX)
1324 return -EIO;
1325 if (reclen > nbytes)
1326 break;
1327 if (memchr(dirent->name, '/', dirent->namelen) != NULL)
1328 return -EIO;
1330 if (!over) {
1331 /* We fill entries into dstbuf only as much as
1332 it can hold. But we still continue iterating
1333 over remaining entries to link them. If not,
1334 we need to send a FORGET for each of those
1335 which we did not link.
1337 over = !dir_emit(ctx, dirent->name, dirent->namelen,
1338 dirent->ino, dirent->type);
1339 ctx->pos = dirent->off;
1342 buf += reclen;
1343 nbytes -= reclen;
1345 ret = fuse_direntplus_link(file, direntplus, attr_version);
1346 if (ret)
1347 fuse_force_forget(file, direntplus->entry_out.nodeid);
1350 return 0;
1353 static int fuse_readdir(struct file *file, struct dir_context *ctx)
1355 int plus, err;
1356 size_t nbytes;
1357 struct page *page;
1358 struct inode *inode = file_inode(file);
1359 struct fuse_conn *fc = get_fuse_conn(inode);
1360 struct fuse_req *req;
1361 u64 attr_version = 0;
1363 if (is_bad_inode(inode))
1364 return -EIO;
1366 req = fuse_get_req(fc, 1);
1367 if (IS_ERR(req))
1368 return PTR_ERR(req);
1370 page = alloc_page(GFP_KERNEL);
1371 if (!page) {
1372 fuse_put_request(fc, req);
1373 return -ENOMEM;
1376 plus = fuse_use_readdirplus(inode, ctx);
1377 req->out.argpages = 1;
1378 req->num_pages = 1;
1379 req->pages[0] = page;
1380 req->page_descs[0].length = PAGE_SIZE;
1381 if (plus) {
1382 attr_version = fuse_get_attr_version(fc);
1383 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
1384 FUSE_READDIRPLUS);
1385 } else {
1386 fuse_read_fill(req, file, ctx->pos, PAGE_SIZE,
1387 FUSE_READDIR);
1389 fuse_request_send(fc, req);
1390 nbytes = req->out.args[0].size;
1391 err = req->out.h.error;
1392 fuse_put_request(fc, req);
1393 if (!err) {
1394 if (plus) {
1395 err = parse_dirplusfile(page_address(page), nbytes,
1396 file, ctx,
1397 attr_version);
1398 } else {
1399 err = parse_dirfile(page_address(page), nbytes, file,
1400 ctx);
1404 __free_page(page);
1405 fuse_invalidate_attr(inode); /* atime changed */
1406 return err;
1409 static char *read_link(struct dentry *dentry)
1411 struct inode *inode = dentry->d_inode;
1412 struct fuse_conn *fc = get_fuse_conn(inode);
1413 struct fuse_req *req = fuse_get_req_nopages(fc);
1414 char *link;
1416 if (IS_ERR(req))
1417 return ERR_CAST(req);
1419 link = (char *) __get_free_page(GFP_KERNEL);
1420 if (!link) {
1421 link = ERR_PTR(-ENOMEM);
1422 goto out;
1424 req->in.h.opcode = FUSE_READLINK;
1425 req->in.h.nodeid = get_node_id(inode);
1426 req->out.argvar = 1;
1427 req->out.numargs = 1;
1428 req->out.args[0].size = PAGE_SIZE - 1;
1429 req->out.args[0].value = link;
1430 fuse_request_send(fc, req);
1431 if (req->out.h.error) {
1432 free_page((unsigned long) link);
1433 link = ERR_PTR(req->out.h.error);
1434 } else
1435 link[req->out.args[0].size] = '\0';
1436 out:
1437 fuse_put_request(fc, req);
1438 fuse_invalidate_attr(inode); /* atime changed */
1439 return link;
1442 static void free_link(char *link)
1444 if (!IS_ERR(link))
1445 free_page((unsigned long) link);
1448 static void *fuse_follow_link(struct dentry *dentry, struct nameidata *nd)
1450 nd_set_link(nd, read_link(dentry));
1451 return NULL;
1454 static void fuse_put_link(struct dentry *dentry, struct nameidata *nd, void *c)
1456 free_link(nd_get_link(nd));
1459 static int fuse_dir_open(struct inode *inode, struct file *file)
1461 return fuse_open_common(inode, file, true);
1464 static int fuse_dir_release(struct inode *inode, struct file *file)
1466 fuse_release_common(file, FUSE_RELEASEDIR);
1468 return 0;
1471 static int fuse_dir_fsync(struct file *file, loff_t start, loff_t end,
1472 int datasync)
1474 return fuse_fsync_common(file, start, end, datasync, 1);
1477 static long fuse_dir_ioctl(struct file *file, unsigned int cmd,
1478 unsigned long arg)
1480 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1482 /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1483 if (fc->minor < 18)
1484 return -ENOTTY;
1486 return fuse_ioctl_common(file, cmd, arg, FUSE_IOCTL_DIR);
1489 static long fuse_dir_compat_ioctl(struct file *file, unsigned int cmd,
1490 unsigned long arg)
1492 struct fuse_conn *fc = get_fuse_conn(file->f_mapping->host);
1494 if (fc->minor < 18)
1495 return -ENOTTY;
1497 return fuse_ioctl_common(file, cmd, arg,
1498 FUSE_IOCTL_COMPAT | FUSE_IOCTL_DIR);
1501 static bool update_mtime(unsigned ivalid)
1503 /* Always update if mtime is explicitly set */
1504 if (ivalid & ATTR_MTIME_SET)
1505 return true;
1507 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1508 if ((ivalid & ATTR_SIZE) && (ivalid & (ATTR_OPEN | ATTR_FILE)))
1509 return false;
1511 /* In all other cases update */
1512 return true;
1515 static void iattr_to_fattr(struct iattr *iattr, struct fuse_setattr_in *arg)
1517 unsigned ivalid = iattr->ia_valid;
1519 if (ivalid & ATTR_MODE)
1520 arg->valid |= FATTR_MODE, arg->mode = iattr->ia_mode;
1521 if (ivalid & ATTR_UID)
1522 arg->valid |= FATTR_UID, arg->uid = from_kuid(&init_user_ns, iattr->ia_uid);
1523 if (ivalid & ATTR_GID)
1524 arg->valid |= FATTR_GID, arg->gid = from_kgid(&init_user_ns, iattr->ia_gid);
1525 if (ivalid & ATTR_SIZE)
1526 arg->valid |= FATTR_SIZE, arg->size = iattr->ia_size;
1527 if (ivalid & ATTR_ATIME) {
1528 arg->valid |= FATTR_ATIME;
1529 arg->atime = iattr->ia_atime.tv_sec;
1530 arg->atimensec = iattr->ia_atime.tv_nsec;
1531 if (!(ivalid & ATTR_ATIME_SET))
1532 arg->valid |= FATTR_ATIME_NOW;
1534 if ((ivalid & ATTR_MTIME) && update_mtime(ivalid)) {
1535 arg->valid |= FATTR_MTIME;
1536 arg->mtime = iattr->ia_mtime.tv_sec;
1537 arg->mtimensec = iattr->ia_mtime.tv_nsec;
1538 if (!(ivalid & ATTR_MTIME_SET))
1539 arg->valid |= FATTR_MTIME_NOW;
1544 * Prevent concurrent writepages on inode
1546 * This is done by adding a negative bias to the inode write counter
1547 * and waiting for all pending writes to finish.
1549 void fuse_set_nowrite(struct inode *inode)
1551 struct fuse_conn *fc = get_fuse_conn(inode);
1552 struct fuse_inode *fi = get_fuse_inode(inode);
1554 BUG_ON(!mutex_is_locked(&inode->i_mutex));
1556 spin_lock(&fc->lock);
1557 BUG_ON(fi->writectr < 0);
1558 fi->writectr += FUSE_NOWRITE;
1559 spin_unlock(&fc->lock);
1560 wait_event(fi->page_waitq, fi->writectr == FUSE_NOWRITE);
1564 * Allow writepages on inode
1566 * Remove the bias from the writecounter and send any queued
1567 * writepages.
1569 static void __fuse_release_nowrite(struct inode *inode)
1571 struct fuse_inode *fi = get_fuse_inode(inode);
1573 BUG_ON(fi->writectr != FUSE_NOWRITE);
1574 fi->writectr = 0;
1575 fuse_flush_writepages(inode);
1578 void fuse_release_nowrite(struct inode *inode)
1580 struct fuse_conn *fc = get_fuse_conn(inode);
1582 spin_lock(&fc->lock);
1583 __fuse_release_nowrite(inode);
1584 spin_unlock(&fc->lock);
1588 * Set attributes, and at the same time refresh them.
1590 * Truncation is slightly complicated, because the 'truncate' request
1591 * may fail, in which case we don't want to touch the mapping.
1592 * vmtruncate() doesn't allow for this case, so do the rlimit checking
1593 * and the actual truncation by hand.
1595 int fuse_do_setattr(struct inode *inode, struct iattr *attr,
1596 struct file *file)
1598 struct fuse_conn *fc = get_fuse_conn(inode);
1599 struct fuse_inode *fi = get_fuse_inode(inode);
1600 struct fuse_req *req;
1601 struct fuse_setattr_in inarg;
1602 struct fuse_attr_out outarg;
1603 bool is_truncate = false;
1604 loff_t oldsize;
1605 int err;
1607 if (!(fc->flags & FUSE_DEFAULT_PERMISSIONS))
1608 attr->ia_valid |= ATTR_FORCE;
1610 err = inode_change_ok(inode, attr);
1611 if (err)
1612 return err;
1614 if (attr->ia_valid & ATTR_OPEN) {
1615 if (fc->atomic_o_trunc)
1616 return 0;
1617 file = NULL;
1620 if (attr->ia_valid & ATTR_SIZE)
1621 is_truncate = true;
1623 req = fuse_get_req_nopages(fc);
1624 if (IS_ERR(req))
1625 return PTR_ERR(req);
1627 if (is_truncate) {
1628 fuse_set_nowrite(inode);
1629 set_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1632 memset(&inarg, 0, sizeof(inarg));
1633 memset(&outarg, 0, sizeof(outarg));
1634 iattr_to_fattr(attr, &inarg);
1635 if (file) {
1636 struct fuse_file *ff = file->private_data;
1637 inarg.valid |= FATTR_FH;
1638 inarg.fh = ff->fh;
1640 if (attr->ia_valid & ATTR_SIZE) {
1641 /* For mandatory locking in truncate */
1642 inarg.valid |= FATTR_LOCKOWNER;
1643 inarg.lock_owner = fuse_lock_owner_id(fc, current->files);
1645 req->in.h.opcode = FUSE_SETATTR;
1646 req->in.h.nodeid = get_node_id(inode);
1647 req->in.numargs = 1;
1648 req->in.args[0].size = sizeof(inarg);
1649 req->in.args[0].value = &inarg;
1650 req->out.numargs = 1;
1651 if (fc->minor < 9)
1652 req->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
1653 else
1654 req->out.args[0].size = sizeof(outarg);
1655 req->out.args[0].value = &outarg;
1656 fuse_request_send(fc, req);
1657 err = req->out.h.error;
1658 fuse_put_request(fc, req);
1659 if (err) {
1660 if (err == -EINTR)
1661 fuse_invalidate_attr(inode);
1662 goto error;
1665 if ((inode->i_mode ^ outarg.attr.mode) & S_IFMT) {
1666 make_bad_inode(inode);
1667 err = -EIO;
1668 goto error;
1671 spin_lock(&fc->lock);
1672 fuse_change_attributes_common(inode, &outarg.attr,
1673 attr_timeout(&outarg));
1674 oldsize = inode->i_size;
1675 i_size_write(inode, outarg.attr.size);
1677 if (is_truncate) {
1678 /* NOTE: this may release/reacquire fc->lock */
1679 __fuse_release_nowrite(inode);
1681 spin_unlock(&fc->lock);
1684 * Only call invalidate_inode_pages2() after removing
1685 * FUSE_NOWRITE, otherwise fuse_launder_page() would deadlock.
1687 if (S_ISREG(inode->i_mode) && oldsize != outarg.attr.size) {
1688 truncate_pagecache(inode, outarg.attr.size);
1689 invalidate_inode_pages2(inode->i_mapping);
1692 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1693 return 0;
1695 error:
1696 if (is_truncate)
1697 fuse_release_nowrite(inode);
1699 clear_bit(FUSE_I_SIZE_UNSTABLE, &fi->state);
1700 return err;
1703 static int fuse_setattr(struct dentry *entry, struct iattr *attr)
1705 struct inode *inode = entry->d_inode;
1707 if (!fuse_allow_current_process(get_fuse_conn(inode)))
1708 return -EACCES;
1710 if (attr->ia_valid & ATTR_FILE)
1711 return fuse_do_setattr(inode, attr, attr->ia_file);
1712 else
1713 return fuse_do_setattr(inode, attr, NULL);
1716 static int fuse_getattr(struct vfsmount *mnt, struct dentry *entry,
1717 struct kstat *stat)
1719 struct inode *inode = entry->d_inode;
1720 struct fuse_conn *fc = get_fuse_conn(inode);
1722 if (!fuse_allow_current_process(fc))
1723 return -EACCES;
1725 return fuse_update_attributes(inode, stat, NULL, NULL);
1728 static int fuse_setxattr(struct dentry *entry, const char *name,
1729 const void *value, size_t size, int flags)
1731 struct inode *inode = entry->d_inode;
1732 struct fuse_conn *fc = get_fuse_conn(inode);
1733 struct fuse_req *req;
1734 struct fuse_setxattr_in inarg;
1735 int err;
1737 if (fc->no_setxattr)
1738 return -EOPNOTSUPP;
1740 req = fuse_get_req_nopages(fc);
1741 if (IS_ERR(req))
1742 return PTR_ERR(req);
1744 memset(&inarg, 0, sizeof(inarg));
1745 inarg.size = size;
1746 inarg.flags = flags;
1747 req->in.h.opcode = FUSE_SETXATTR;
1748 req->in.h.nodeid = get_node_id(inode);
1749 req->in.numargs = 3;
1750 req->in.args[0].size = sizeof(inarg);
1751 req->in.args[0].value = &inarg;
1752 req->in.args[1].size = strlen(name) + 1;
1753 req->in.args[1].value = name;
1754 req->in.args[2].size = size;
1755 req->in.args[2].value = value;
1756 fuse_request_send(fc, req);
1757 err = req->out.h.error;
1758 fuse_put_request(fc, req);
1759 if (err == -ENOSYS) {
1760 fc->no_setxattr = 1;
1761 err = -EOPNOTSUPP;
1763 if (!err)
1764 fuse_invalidate_attr(inode);
1765 return err;
1768 static ssize_t fuse_getxattr(struct dentry *entry, const char *name,
1769 void *value, size_t size)
1771 struct inode *inode = entry->d_inode;
1772 struct fuse_conn *fc = get_fuse_conn(inode);
1773 struct fuse_req *req;
1774 struct fuse_getxattr_in inarg;
1775 struct fuse_getxattr_out outarg;
1776 ssize_t ret;
1778 if (fc->no_getxattr)
1779 return -EOPNOTSUPP;
1781 req = fuse_get_req_nopages(fc);
1782 if (IS_ERR(req))
1783 return PTR_ERR(req);
1785 memset(&inarg, 0, sizeof(inarg));
1786 inarg.size = size;
1787 req->in.h.opcode = FUSE_GETXATTR;
1788 req->in.h.nodeid = get_node_id(inode);
1789 req->in.numargs = 2;
1790 req->in.args[0].size = sizeof(inarg);
1791 req->in.args[0].value = &inarg;
1792 req->in.args[1].size = strlen(name) + 1;
1793 req->in.args[1].value = name;
1794 /* This is really two different operations rolled into one */
1795 req->out.numargs = 1;
1796 if (size) {
1797 req->out.argvar = 1;
1798 req->out.args[0].size = size;
1799 req->out.args[0].value = value;
1800 } else {
1801 req->out.args[0].size = sizeof(outarg);
1802 req->out.args[0].value = &outarg;
1804 fuse_request_send(fc, req);
1805 ret = req->out.h.error;
1806 if (!ret)
1807 ret = size ? req->out.args[0].size : outarg.size;
1808 else {
1809 if (ret == -ENOSYS) {
1810 fc->no_getxattr = 1;
1811 ret = -EOPNOTSUPP;
1814 fuse_put_request(fc, req);
1815 return ret;
1818 static ssize_t fuse_listxattr(struct dentry *entry, char *list, size_t size)
1820 struct inode *inode = entry->d_inode;
1821 struct fuse_conn *fc = get_fuse_conn(inode);
1822 struct fuse_req *req;
1823 struct fuse_getxattr_in inarg;
1824 struct fuse_getxattr_out outarg;
1825 ssize_t ret;
1827 if (!fuse_allow_current_process(fc))
1828 return -EACCES;
1830 if (fc->no_listxattr)
1831 return -EOPNOTSUPP;
1833 req = fuse_get_req_nopages(fc);
1834 if (IS_ERR(req))
1835 return PTR_ERR(req);
1837 memset(&inarg, 0, sizeof(inarg));
1838 inarg.size = size;
1839 req->in.h.opcode = FUSE_LISTXATTR;
1840 req->in.h.nodeid = get_node_id(inode);
1841 req->in.numargs = 1;
1842 req->in.args[0].size = sizeof(inarg);
1843 req->in.args[0].value = &inarg;
1844 /* This is really two different operations rolled into one */
1845 req->out.numargs = 1;
1846 if (size) {
1847 req->out.argvar = 1;
1848 req->out.args[0].size = size;
1849 req->out.args[0].value = list;
1850 } else {
1851 req->out.args[0].size = sizeof(outarg);
1852 req->out.args[0].value = &outarg;
1854 fuse_request_send(fc, req);
1855 ret = req->out.h.error;
1856 if (!ret)
1857 ret = size ? req->out.args[0].size : outarg.size;
1858 else {
1859 if (ret == -ENOSYS) {
1860 fc->no_listxattr = 1;
1861 ret = -EOPNOTSUPP;
1864 fuse_put_request(fc, req);
1865 return ret;
1868 static int fuse_removexattr(struct dentry *entry, const char *name)
1870 struct inode *inode = entry->d_inode;
1871 struct fuse_conn *fc = get_fuse_conn(inode);
1872 struct fuse_req *req;
1873 int err;
1875 if (fc->no_removexattr)
1876 return -EOPNOTSUPP;
1878 req = fuse_get_req_nopages(fc);
1879 if (IS_ERR(req))
1880 return PTR_ERR(req);
1882 req->in.h.opcode = FUSE_REMOVEXATTR;
1883 req->in.h.nodeid = get_node_id(inode);
1884 req->in.numargs = 1;
1885 req->in.args[0].size = strlen(name) + 1;
1886 req->in.args[0].value = name;
1887 fuse_request_send(fc, req);
1888 err = req->out.h.error;
1889 fuse_put_request(fc, req);
1890 if (err == -ENOSYS) {
1891 fc->no_removexattr = 1;
1892 err = -EOPNOTSUPP;
1894 if (!err)
1895 fuse_invalidate_attr(inode);
1896 return err;
1899 static const struct inode_operations fuse_dir_inode_operations = {
1900 .lookup = fuse_lookup,
1901 .mkdir = fuse_mkdir,
1902 .symlink = fuse_symlink,
1903 .unlink = fuse_unlink,
1904 .rmdir = fuse_rmdir,
1905 .rename = fuse_rename,
1906 .link = fuse_link,
1907 .setattr = fuse_setattr,
1908 .create = fuse_create,
1909 .atomic_open = fuse_atomic_open,
1910 .mknod = fuse_mknod,
1911 .permission = fuse_permission,
1912 .getattr = fuse_getattr,
1913 .setxattr = fuse_setxattr,
1914 .getxattr = fuse_getxattr,
1915 .listxattr = fuse_listxattr,
1916 .removexattr = fuse_removexattr,
1919 static const struct file_operations fuse_dir_operations = {
1920 .llseek = generic_file_llseek,
1921 .read = generic_read_dir,
1922 .iterate = fuse_readdir,
1923 .open = fuse_dir_open,
1924 .release = fuse_dir_release,
1925 .fsync = fuse_dir_fsync,
1926 .unlocked_ioctl = fuse_dir_ioctl,
1927 .compat_ioctl = fuse_dir_compat_ioctl,
1930 static const struct inode_operations fuse_common_inode_operations = {
1931 .setattr = fuse_setattr,
1932 .permission = fuse_permission,
1933 .getattr = fuse_getattr,
1934 .setxattr = fuse_setxattr,
1935 .getxattr = fuse_getxattr,
1936 .listxattr = fuse_listxattr,
1937 .removexattr = fuse_removexattr,
1940 static const struct inode_operations fuse_symlink_inode_operations = {
1941 .setattr = fuse_setattr,
1942 .follow_link = fuse_follow_link,
1943 .put_link = fuse_put_link,
1944 .readlink = generic_readlink,
1945 .getattr = fuse_getattr,
1946 .setxattr = fuse_setxattr,
1947 .getxattr = fuse_getxattr,
1948 .listxattr = fuse_listxattr,
1949 .removexattr = fuse_removexattr,
1952 void fuse_init_common(struct inode *inode)
1954 inode->i_op = &fuse_common_inode_operations;
1957 void fuse_init_dir(struct inode *inode)
1959 inode->i_op = &fuse_dir_inode_operations;
1960 inode->i_fop = &fuse_dir_operations;
1963 void fuse_init_symlink(struct inode *inode)
1965 inode->i_op = &fuse_symlink_inode_operations;