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.
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
)
24 if (!fc
->readdirplus_auto
)
26 if (test_and_clear_bit(FUSE_I_ADVISE_RDPLUS
, &fi
->state
))
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
)
46 static inline u64
fuse_dentry_time(struct dentry
*entry
)
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
)
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);
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
)
79 struct timespec ts
= {sec
, nsec
};
80 return get_jiffies_64() + timespec_to_jiffies(&ts
);
86 * Set dentry and possibly attribute timeouts from the lookup/mk*
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
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
)
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
;
146 req
->in
.args
[0].size
= name
->len
+ 1;
147 req
->in
.args
[0].value
= name
->name
;
148 req
->out
.numargs
= 1;
150 req
->out
.args
[0].size
= FUSE_COMPAT_ENTRY_OUT_SIZE
;
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
)
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
);
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
)
183 struct dentry
*parent
;
184 struct fuse_conn
*fc
;
185 struct fuse_inode
*fi
;
188 inode
= ACCESS_ONCE(entry
->d_inode
);
189 if (inode
&& is_bad_inode(inode
))
191 else if (time_before64(fuse_dentry_time(entry
), get_jiffies_64()) ||
192 (flags
& LOOKUP_REVAL
)) {
194 struct fuse_entry_out outarg
;
195 struct fuse_req
*req
;
196 struct fuse_forget_link
*forget
;
199 /* For negative dentries, always do a fresh lookup */
204 if (flags
& LOOKUP_RCU
)
207 fc
= get_fuse_conn(inode
);
208 req
= fuse_get_req_nopages(fc
);
213 forget
= fuse_alloc_forget();
215 fuse_put_request(fc
, req
);
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
);
227 err
= req
->out
.h
.error
;
228 fuse_put_request(fc
, req
);
229 /* Zero nodeid is same as -ENOENT */
230 if (!err
&& !outarg
.nodeid
)
233 fi
= get_fuse_inode(inode
);
234 if (outarg
.nodeid
!= get_node_id(inode
)) {
235 fuse_queue_forget(fc
, forget
, outarg
.nodeid
, 1);
238 spin_lock(&fc
->lock
);
240 spin_unlock(&fc
->lock
);
243 if (err
|| (outarg
.attr
.mode
^ inode
->i_mode
) & S_IFMT
)
246 fuse_change_attributes(inode
, &outarg
.attr
,
247 entry_attr_timeout(&outarg
),
249 fuse_change_entry_timeout(entry
, &outarg
);
251 fi
= get_fuse_inode(inode
);
252 if (flags
& LOOKUP_RCU
) {
253 if (test_bit(FUSE_I_INIT_RDPLUS
, &fi
->state
))
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
);
268 if (!(flags
& LOOKUP_RCU
) && check_submounts_and_drop(entry
) != 0)
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
;
299 if (name
->len
> FUSE_NAME_MAX
)
302 req
= fuse_get_req_nopages(fc
);
307 forget
= fuse_alloc_forget();
310 fuse_put_request(fc
, req
);
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
)
327 if (!fuse_valid_type(outarg
->attr
.mode
))
330 *inode
= fuse_iget(sb
, outarg
->nodeid
, outarg
->generation
,
331 &outarg
->attr
, entry_attr_timeout(outarg
),
335 fuse_queue_forget(fc
, forget
, outarg
->nodeid
, 1);
346 static struct dentry
*fuse_materialise_dentry(struct dentry
*dentry
,
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
);
358 newent
= d_materialise_unique(dentry
, inode
);
364 static struct dentry
*fuse_lookup(struct inode
*dir
, struct dentry
*entry
,
368 struct fuse_entry_out outarg
;
370 struct dentry
*newent
;
371 bool outarg_valid
= true;
373 err
= fuse_lookup_name(dir
->i_sb
, get_node_id(dir
), &entry
->d_name
,
375 if (err
== -ENOENT
) {
376 outarg_valid
= false;
383 if (inode
&& get_node_id(inode
) == FUSE_ROOT_ID
)
386 newent
= fuse_materialise_dentry(entry
, inode
);
387 err
= PTR_ERR(newent
);
391 entry
= newent
? newent
: entry
;
393 fuse_change_entry_timeout(entry
, &outarg
);
395 fuse_invalidate_entry_cache(entry
);
397 fuse_advise_use_readdirplus(dir
);
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
)
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();
434 req
= fuse_get_req_nopages(fc
);
437 goto out_put_forget_req
;
440 ff
= fuse_file_alloc(fc
);
442 goto out_put_request
;
445 mode
&= ~current_umask();
448 memset(&inarg
, 0, sizeof(inarg
));
449 memset(&outentry
, 0, sizeof(outentry
));
452 inarg
.umask
= current_umask();
453 req
->in
.h
.opcode
= FUSE_CREATE
;
454 req
->in
.h
.nodeid
= get_node_id(dir
);
456 req
->in
.args
[0].size
= fc
->minor
< 12 ? sizeof(struct fuse_open_in
) :
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;
463 req
->out
.args
[0].size
= FUSE_COMPAT_ENTRY_OUT_SIZE
;
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
;
475 if (!S_ISREG(outentry
.attr
.mode
) || invalid_nodeid(outentry
.nodeid
))
478 fuse_put_request(fc
, req
);
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);
485 flags
&= ~(O_CREAT
| O_EXCL
| O_TRUNC
);
486 fuse_sync_release(ff
, flags
);
487 fuse_queue_forget(fc
, forget
, outentry
.nodeid
, 1);
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
);
497 fuse_sync_release(ff
, flags
);
499 file
->private_data
= fuse_file_get(ff
);
500 fuse_finish_open(inode
, file
);
507 fuse_put_request(fc
, req
);
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
)
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);
532 if (!(flags
& O_CREAT
) || entry
->d_inode
)
536 *opened
|= FILE_CREATED
;
541 err
= fuse_create_open(dir
, entry
, file
, flags
, mode
, opened
);
542 if (err
== -ENOSYS
) {
551 err
= fuse_mknod(dir
, entry
, mode
, 0);
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
,
565 struct fuse_entry_out outarg
;
568 struct fuse_forget_link
*forget
;
570 forget
= fuse_alloc_forget();
572 fuse_put_request(fc
, req
);
576 memset(&outarg
, 0, sizeof(outarg
));
577 req
->in
.h
.nodeid
= get_node_id(dir
);
578 req
->out
.numargs
= 1;
580 req
->out
.args
[0].size
= FUSE_COMPAT_ENTRY_OUT_SIZE
;
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
);
588 goto out_put_forget_req
;
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);
600 fuse_queue_forget(fc
, forget
, outarg
.nodeid
, 1);
605 if (S_ISDIR(inode
->i_mode
)) {
606 struct dentry
*alias
;
607 mutex_lock(&fc
->inst_mutex
);
608 alias
= d_find_alias(inode
);
610 /* New directory must have moved since mkdir */
611 mutex_unlock(&fc
->inst_mutex
);
616 d_instantiate(entry
, inode
);
617 mutex_unlock(&fc
->inst_mutex
);
619 d_instantiate(entry
, inode
);
621 fuse_change_entry_timeout(entry
, &outarg
);
622 fuse_invalidate_attr(dir
);
630 static int fuse_mknod(struct inode
*dir
, struct dentry
*entry
, umode_t mode
,
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
);
640 mode
&= ~current_umask();
642 memset(&inarg
, 0, sizeof(inarg
));
644 inarg
.rdev
= new_encode_dev(rdev
);
645 inarg
.umask
= current_umask();
646 req
->in
.h
.opcode
= FUSE_MKNOD
;
648 req
->in
.args
[0].size
= fc
->minor
< 12 ? FUSE_COMPAT_MKNOD_IN_SIZE
:
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
,
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
);
671 mode
&= ~current_umask();
673 memset(&inarg
, 0, sizeof(inarg
));
675 inarg
.umask
= current_umask();
676 req
->in
.h
.opcode
= FUSE_MKDIR
;
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
,
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
);
694 req
->in
.h
.opcode
= FUSE_SYMLINK
;
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
)
706 struct fuse_conn
*fc
= get_fuse_conn(dir
);
707 struct fuse_req
*req
= fuse_get_req_nopages(fc
);
711 req
->in
.h
.opcode
= FUSE_UNLINK
;
712 req
->in
.h
.nodeid
= get_node_id(dir
);
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
);
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
731 if (inode
->i_nlink
> 0)
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
);
742 static int fuse_rmdir(struct inode
*dir
, struct dentry
*entry
)
745 struct fuse_conn
*fc
= get_fuse_conn(dir
);
746 struct fuse_req
*req
= fuse_get_req_nopages(fc
);
750 req
->in
.h
.opcode
= FUSE_RMDIR
;
751 req
->in
.h
.nodeid
= get_node_id(dir
);
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
);
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
);
767 static int fuse_rename(struct inode
*olddir
, struct dentry
*oldent
,
768 struct inode
*newdir
, struct dentry
*newent
)
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
);
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
);
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
);
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
);
813 fuse_invalidate_entry(newent
);
819 static int fuse_link(struct dentry
*entry
, struct inode
*newdir
,
820 struct dentry
*newent
)
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
);
830 memset(&inarg
, 0, sizeof(inarg
));
831 inarg
.oldnodeid
= get_node_id(inode
);
832 req
->in
.h
.opcode
= FUSE_LINK
;
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,
846 struct fuse_inode
*fi
= get_fuse_inode(inode
);
848 spin_lock(&fc
->lock
);
849 fi
->attr_version
= ++fc
->attr_version
;
851 spin_unlock(&fc
->lock
);
852 fuse_invalidate_attr(inode
);
853 } else if (err
== -EINTR
) {
854 fuse_invalidate_attr(inode
);
859 static void fuse_fillattr(struct inode
*inode
, struct fuse_attr
*attr
,
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
);
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
,
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
;
898 req
= fuse_get_req_nopages(fc
);
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
;
913 req
->in
.h
.opcode
= FUSE_GETATTR
;
914 req
->in
.h
.nodeid
= get_node_id(inode
);
916 req
->in
.args
[0].size
= sizeof(inarg
);
917 req
->in
.args
[0].value
= &inarg
;
918 req
->out
.numargs
= 1;
920 req
->out
.args
[0].size
= FUSE_COMPAT_ATTR_OUT_SIZE
;
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
);
928 if ((inode
->i_mode
^ outarg
.attr
.mode
) & S_IFMT
) {
929 make_bad_inode(inode
);
932 fuse_change_attributes(inode
, &outarg
.attr
,
933 attr_timeout(&outarg
),
936 fuse_fillattr(inode
, &outarg
.attr
, stat
);
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
);
949 if (time_before64(fi
->i_time
, get_jiffies_64())) {
951 err
= fuse_do_getattr(inode
, stat
, file
);
956 generic_fillattr(inode
, stat
);
957 stat
->mode
= fi
->orig_i_mode
;
958 stat
->ino
= fi
->orig_ino
;
962 if (refreshed
!= NULL
)
968 int fuse_reverse_inval_entry(struct super_block
*sb
, u64 parent_nodeid
,
969 u64 child_nodeid
, struct qstr
*name
)
972 struct inode
*parent
;
974 struct dentry
*entry
;
976 parent
= ilookup5(sb
, parent_nodeid
, fuse_inode_eq
, &parent_nodeid
);
980 mutex_lock(&parent
->i_mutex
);
981 if (!S_ISDIR(parent
->i_mode
))
985 dir
= d_find_alias(parent
);
989 entry
= d_lookup(dir
, name
);
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
) {
1003 if (d_mountpoint(entry
)) {
1007 if (S_ISDIR(entry
->d_inode
->i_mode
)) {
1008 shrink_dcache_parent(entry
);
1009 if (!simple_empty(entry
)) {
1013 entry
->d_inode
->i_flags
|= S_DEAD
;
1016 clear_nlink(entry
->d_inode
);
1019 mutex_unlock(&entry
->d_inode
->i_mutex
);
1028 mutex_unlock(&parent
->i_mutex
);
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
)
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
))
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
;
1072 BUG_ON(mask
& MAY_NOT_BLOCK
);
1077 req
= fuse_get_req_nopages(fc
);
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
) {
1098 static int fuse_perm_getattr(struct inode
*inode
, int mask
)
1100 if (mask
& MAY_NOT_BLOCK
)
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
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;
1125 if (!fuse_allow_current_process(fc
))
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())) {
1138 err
= fuse_perm_getattr(inode
, mask
);
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
);
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
)) {
1167 err
= fuse_perm_getattr(inode
, mask
);
1168 if (!err
&& !(inode
->i_mode
& S_IXUGO
))
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
)
1183 if (reclen
> nbytes
)
1185 if (memchr(dirent
->name
, '/', dirent
->namelen
) != NULL
)
1188 if (!dir_emit(ctx
, dirent
->name
, dirent
->namelen
,
1189 dirent
->ino
, dirent
->type
))
1194 ctx
->pos
= dirent
->off
;
1200 static int fuse_direntplus_link(struct file
*file
,
1201 struct fuse_direntplus
*direntplus
,
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
;
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.
1226 if (name
.name
[0] == '.') {
1228 * We could potentially refresh the attributes of the directory
1233 if (name
.name
[1] == '.' && name
.len
== 2)
1237 if (invalid_nodeid(o
->nodeid
))
1239 if (!fuse_valid_type(o
->attr
.mode
))
1242 fc
= get_fuse_conn(dir
);
1244 name
.hash
= full_name_hash(name
.name
, name
.len
);
1245 dentry
= d_lookup(parent
, &name
);
1247 inode
= dentry
->d_inode
;
1250 } else if (get_node_id(inode
) != o
->nodeid
||
1251 ((o
->attr
.mode
^ inode
->i_mode
) & S_IFMT
)) {
1252 err
= d_invalidate(dentry
);
1255 } else if (is_bad_inode(inode
)) {
1259 struct fuse_inode
*fi
;
1260 fi
= get_fuse_inode(inode
);
1261 spin_lock(&fc
->lock
);
1263 spin_unlock(&fc
->lock
);
1265 fuse_change_attributes(inode
, &o
->attr
,
1266 entry_attr_timeout(o
),
1270 * The other branch to 'found' comes via fuse_iget()
1271 * which bumps nlookup inside
1278 dentry
= d_alloc(parent
, &name
);
1283 inode
= fuse_iget(dir
->i_sb
, o
->nodeid
, o
->generation
,
1284 &o
->attr
, entry_attr_timeout(o
), attr_version
);
1288 alias
= fuse_materialise_dentry(dentry
, inode
);
1289 err
= PTR_ERR(alias
);
1299 if (fc
->readdirplus_auto
)
1300 set_bit(FUSE_I_INIT_RDPLUS
, &get_fuse_inode(inode
)->state
);
1301 fuse_change_entry_timeout(dentry
, o
);
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
;
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
)
1325 if (reclen
> nbytes
)
1327 if (memchr(dirent
->name
, '/', dirent
->namelen
) != NULL
)
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
;
1345 ret
= fuse_direntplus_link(file
, direntplus
, attr_version
);
1347 fuse_force_forget(file
, direntplus
->entry_out
.nodeid
);
1353 static int fuse_readdir(struct file
*file
, struct dir_context
*ctx
)
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
))
1366 req
= fuse_get_req(fc
, 1);
1368 return PTR_ERR(req
);
1370 page
= alloc_page(GFP_KERNEL
);
1372 fuse_put_request(fc
, req
);
1376 plus
= fuse_use_readdirplus(inode
, ctx
);
1377 req
->out
.argpages
= 1;
1379 req
->pages
[0] = page
;
1380 req
->page_descs
[0].length
= PAGE_SIZE
;
1382 attr_version
= fuse_get_attr_version(fc
);
1383 fuse_read_fill(req
, file
, ctx
->pos
, PAGE_SIZE
,
1386 fuse_read_fill(req
, file
, ctx
->pos
, PAGE_SIZE
,
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
);
1395 err
= parse_dirplusfile(page_address(page
), nbytes
,
1399 err
= parse_dirfile(page_address(page
), nbytes
, file
,
1405 fuse_invalidate_attr(inode
); /* atime changed */
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
);
1417 return ERR_CAST(req
);
1419 link
= (char *) __get_free_page(GFP_KERNEL
);
1421 link
= ERR_PTR(-ENOMEM
);
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
);
1435 link
[req
->out
.args
[0].size
] = '\0';
1437 fuse_put_request(fc
, req
);
1438 fuse_invalidate_attr(inode
); /* atime changed */
1442 static void free_link(char *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
));
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
);
1471 static int fuse_dir_fsync(struct file
*file
, loff_t start
, loff_t end
,
1474 return fuse_fsync_common(file
, start
, end
, datasync
, 1);
1477 static long fuse_dir_ioctl(struct file
*file
, unsigned int cmd
,
1480 struct fuse_conn
*fc
= get_fuse_conn(file
->f_mapping
->host
);
1482 /* FUSE_IOCTL_DIR only supported for API version >= 7.18 */
1486 return fuse_ioctl_common(file
, cmd
, arg
, FUSE_IOCTL_DIR
);
1489 static long fuse_dir_compat_ioctl(struct file
*file
, unsigned int cmd
,
1492 struct fuse_conn
*fc
= get_fuse_conn(file
->f_mapping
->host
);
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
)
1507 /* If it's an open(O_TRUNC) or an ftruncate(), don't update */
1508 if ((ivalid
& ATTR_SIZE
) && (ivalid
& (ATTR_OPEN
| ATTR_FILE
)))
1511 /* In all other cases update */
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
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
);
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
,
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;
1607 if (!(fc
->flags
& FUSE_DEFAULT_PERMISSIONS
))
1608 attr
->ia_valid
|= ATTR_FORCE
;
1610 err
= inode_change_ok(inode
, attr
);
1614 if (attr
->ia_valid
& ATTR_OPEN
) {
1615 if (fc
->atomic_o_trunc
)
1620 if (attr
->ia_valid
& ATTR_SIZE
)
1623 req
= fuse_get_req_nopages(fc
);
1625 return PTR_ERR(req
);
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
);
1636 struct fuse_file
*ff
= file
->private_data
;
1637 inarg
.valid
|= FATTR_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;
1652 req
->out
.args
[0].size
= FUSE_COMPAT_ATTR_OUT_SIZE
;
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
);
1661 fuse_invalidate_attr(inode
);
1665 if ((inode
->i_mode
^ outarg
.attr
.mode
) & S_IFMT
) {
1666 make_bad_inode(inode
);
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
);
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
);
1697 fuse_release_nowrite(inode
);
1699 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
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
)))
1710 if (attr
->ia_valid
& ATTR_FILE
)
1711 return fuse_do_setattr(inode
, attr
, attr
->ia_file
);
1713 return fuse_do_setattr(inode
, attr
, NULL
);
1716 static int fuse_getattr(struct vfsmount
*mnt
, struct dentry
*entry
,
1719 struct inode
*inode
= entry
->d_inode
;
1720 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1722 if (!fuse_allow_current_process(fc
))
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
;
1737 if (fc
->no_setxattr
)
1740 req
= fuse_get_req_nopages(fc
);
1742 return PTR_ERR(req
);
1744 memset(&inarg
, 0, sizeof(inarg
));
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;
1764 fuse_invalidate_attr(inode
);
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
;
1778 if (fc
->no_getxattr
)
1781 req
= fuse_get_req_nopages(fc
);
1783 return PTR_ERR(req
);
1785 memset(&inarg
, 0, sizeof(inarg
));
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;
1797 req
->out
.argvar
= 1;
1798 req
->out
.args
[0].size
= size
;
1799 req
->out
.args
[0].value
= value
;
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
;
1807 ret
= size
? req
->out
.args
[0].size
: outarg
.size
;
1809 if (ret
== -ENOSYS
) {
1810 fc
->no_getxattr
= 1;
1814 fuse_put_request(fc
, req
);
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
;
1827 if (!fuse_allow_current_process(fc
))
1830 if (fc
->no_listxattr
)
1833 req
= fuse_get_req_nopages(fc
);
1835 return PTR_ERR(req
);
1837 memset(&inarg
, 0, sizeof(inarg
));
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;
1847 req
->out
.argvar
= 1;
1848 req
->out
.args
[0].size
= size
;
1849 req
->out
.args
[0].value
= list
;
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
;
1857 ret
= size
? req
->out
.args
[0].size
: outarg
.size
;
1859 if (ret
== -ENOSYS
) {
1860 fc
->no_listxattr
= 1;
1864 fuse_put_request(fc
, req
);
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
;
1875 if (fc
->no_removexattr
)
1878 req
= fuse_get_req_nopages(fc
);
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;
1895 fuse_invalidate_attr(inode
);
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
,
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
;