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.
12 #include <linux/pagemap.h>
13 #include <linux/slab.h>
14 #include <linux/kernel.h>
15 #include <linux/sched.h>
16 #include <linux/module.h>
17 #include <linux/compat.h>
18 #include <linux/swap.h>
19 #include <linux/aio.h>
20 #include <linux/falloc.h>
21 #include <asm/div64.h>
24 struct mutex fuse_iolog_lock
;
26 static struct fuse_proc_info fuse_iolog
[FUSE_IOLOG_MAX
];
27 static struct timespec fuse_iolog_time
;
28 static struct task_struct
*fuse_iolog_thread
=NULL
;
31 struct timespec
*start
,
33 struct timespec
*diff
)
35 if ((end
->tv_nsec
-start
->tv_nsec
)<0) {
36 diff
->tv_sec
= end
->tv_sec
-start
->tv_sec
-1;
37 diff
->tv_nsec
= 1000000000+end
->tv_nsec
-start
->tv_nsec
;
39 diff
->tv_sec
= end
->tv_sec
-start
->tv_sec
;
40 diff
->tv_nsec
= end
->tv_nsec
-start
->tv_nsec
;
45 struct fuse_ioiog_type_map
51 #define FUSE_IOLOG_TYPE_MAX 46
53 static const char *fuse_iolog_type
[FUSE_IOLOG_TYPE_MAX
]={
95 "notify_reply", /*41*/
96 "batch_forget", /*42*/
102 static const char *fuse_iolog_type2str(int type
)
104 if (type
>=0 && type
<FUSE_IOLOG_TYPE_MAX
)
106 else if (type
==CUSE_INIT
)
107 type
=FUSE_IOLOG_TYPE_MAX
-1;
111 return fuse_iolog_type
[type
];
114 int fuse_iolog_print(void)
117 char buf
[FUSE_IOLOG_BUFLEN
], *ptr
;
119 len
=FUSE_IOLOG_BUFLEN
-1;
122 for (i
=0;i
<FUSE_IOLOG_MAX
&& fuse_iolog
[i
].valid
;i
++) {
124 if (fuse_iolog
[i
].read
.count
|| fuse_iolog
[i
].write
.count
) {
125 n
=snprintf(ptr
, len
, "{%d:R(%d,%d,%d),W(%d,%d,%d)}",
127 fuse_iolog
[i
].read
.bytes
,
128 fuse_iolog
[i
].read
.count
,
129 fuse_iolog
[i
].read
.us
,
130 fuse_iolog
[i
].write
.bytes
,
131 fuse_iolog
[i
].write
.count
,
132 fuse_iolog
[i
].write
.us
);
141 if (fuse_iolog
[i
].misc_type
) {
142 n
=snprintf(ptr
, len
, "{%d:%s(%d,%d,%d)}",
144 fuse_iolog_type2str(fuse_iolog
[i
].misc_type
),
145 fuse_iolog
[i
].misc
.bytes
,
146 fuse_iolog
[i
].misc
.count
,
147 fuse_iolog
[i
].misc
.us
);
157 xlog_printk(ANDROID_LOG_DEBUG
, "BLOCK_TAG", "FUSEIO %s\n", buf
);
159 return ptr
- &buf
[0];
162 xlog_printk(ANDROID_LOG_DEBUG
, "BLOCK_TAG",
163 "FUSEIO log buffer overflow \n");
168 void fuse_iolog_proc_clear(void)
170 memset(&fuse_iolog
[0], 0, sizeof(struct fuse_proc_info
)*FUSE_IOLOG_MAX
);
171 get_monotonic_boottime(&fuse_iolog_time
);
174 inline __u32
fuse_iolog_timeus(struct timespec
*t
)
182 if (t
->tv_sec
> 3600)
183 return 0xD693A400; /* 3600000000 */
185 _t
= t
->tv_sec
* 1000000 + us
;
193 __u32
fuse_iolog_timeus_diff(struct timespec
*start
, struct timespec
*end
)
195 struct timespec diff
;
196 fuse_time_diff(start
, end
, &diff
);
197 return fuse_iolog_timeus(&diff
);
201 inline int fuse_iolog_proc_update(struct fuse_proc_info
*info
,
202 __u32 io_bytes
, int type
, struct timespec
*diff
)
204 struct fuse_rw_info
*rwi
;
207 _t
= fuse_iolog_timeus(diff
);
211 else if (type
== FUSE_WRITE
)
214 if (info
->misc_type
==0)
215 info
->misc_type
=type
;
216 else if (info
->misc_type
!=type
) /* misc type mismatch => continue */
221 rwi
->bytes
+= io_bytes
;
228 static int fuse_iolog_watch(void *arg
)
230 unsigned int timeout
;
232 struct timespec curr
, diff
;
235 if (kthread_should_stop()) break;
237 get_monotonic_boottime(&curr
);
239 mutex_lock(&fuse_iolog_lock
);
240 fuse_time_diff(&fuse_iolog_time
, &curr
, &diff
);
242 n
=fuse_iolog_print();
245 fuse_iolog_proc_clear();
247 mutex_unlock(&fuse_iolog_lock
);
250 set_current_state(TASK_INTERRUPTIBLE
);
251 timeout
= schedule_timeout(FUSE_IOLOG_LATENCY
*HZ
);
258 void fuse_iolog_init(void)
262 mutex_init(&fuse_iolog_lock
);
263 mutex_lock(&fuse_iolog_lock
);
264 fuse_iolog_proc_clear();
265 mutex_unlock(&fuse_iolog_lock
);
267 fuse_iolog_thread
=kthread_create(fuse_iolog_watch
, NULL
, "fuse_log");
268 if (IS_ERR(fuse_iolog_thread
)) {
269 ret
= PTR_ERR(fuse_iolog_thread
);
270 xlog_printk(ANDROID_LOG_DEBUG
, "BLOCK_TAG",
271 "Fail to create fuse_log thread %d\n", ret
);
272 fuse_iolog_thread
= NULL
;
275 wake_up_process(fuse_iolog_thread
);
280 void fuse_iolog_exit(void)
282 kthread_stop(fuse_iolog_thread
);
284 void fuse_iolog_add(__u32 io_bytes
, int type
,
285 struct timespec
*start
,
286 struct timespec
*end
)
288 struct fuse_proc_info
*info
;
289 struct timespec diff
;
292 pid
= task_pid_nr(current
);
293 fuse_time_diff(start
, end
, &diff
);
295 mutex_lock(&fuse_iolog_lock
);
297 for (i
=0;i
<FUSE_IOLOG_MAX
;i
++) {
301 if (info
->pid
== pid
) {
302 if (fuse_iolog_proc_update(info
, io_bytes
, type
, &diff
)) {
303 continue; // ops mismatch
315 fuse_iolog_proc_update(info
, io_bytes
, type
, &diff
);
317 get_monotonic_boottime(&fuse_iolog_time
);
322 if (i
==FUSE_IOLOG_MAX
) {
324 fuse_iolog_proc_clear();
328 fuse_iolog_proc_update(info
, io_bytes
, type
, &diff
);
331 mutex_unlock(&fuse_iolog_lock
);
336 static const struct file_operations fuse_direct_io_file_operations
;
338 static int fuse_send_open(struct fuse_conn
*fc
, u64 nodeid
, struct file
*file
,
339 int opcode
, struct fuse_open_out
*outargp
)
341 struct fuse_open_in inarg
;
342 struct fuse_req
*req
;
345 req
= fuse_get_req_nopages(fc
);
349 memset(&inarg
, 0, sizeof(inarg
));
350 inarg
.flags
= file
->f_flags
& ~(O_CREAT
| O_EXCL
| O_NOCTTY
);
351 if (!fc
->atomic_o_trunc
)
352 inarg
.flags
&= ~O_TRUNC
;
353 req
->in
.h
.opcode
= opcode
;
354 req
->in
.h
.nodeid
= nodeid
;
356 req
->in
.args
[0].size
= sizeof(inarg
);
357 req
->in
.args
[0].value
= &inarg
;
358 req
->out
.numargs
= 1;
359 req
->out
.args
[0].size
= sizeof(*outargp
);
360 req
->out
.args
[0].value
= outargp
;
361 fuse_request_send(fc
, req
);
362 err
= req
->out
.h
.error
;
363 fuse_put_request(fc
, req
);
368 struct fuse_file
*fuse_file_alloc(struct fuse_conn
*fc
)
370 struct fuse_file
*ff
;
372 ff
= kzalloc(sizeof(struct fuse_file
), GFP_KERNEL
);
377 ff
->reserved_req
= fuse_request_alloc(0);
378 if (unlikely(!ff
->reserved_req
)) {
383 INIT_LIST_HEAD(&ff
->write_entry
);
384 atomic_set(&ff
->count
, 0);
385 RB_CLEAR_NODE(&ff
->polled_node
);
386 init_waitqueue_head(&ff
->poll_wait
);
388 spin_lock(&fc
->lock
);
389 ff
->kh
= ++fc
->khctr
;
390 spin_unlock(&fc
->lock
);
395 void fuse_file_free(struct fuse_file
*ff
)
397 fuse_request_free(ff
->reserved_req
);
401 struct fuse_file
*fuse_file_get(struct fuse_file
*ff
)
403 atomic_inc(&ff
->count
);
407 static void fuse_release_async(struct work_struct
*work
)
409 struct fuse_req
*req
;
410 struct fuse_conn
*fc
;
413 req
= container_of(work
, struct fuse_req
, misc
.release
.work
);
414 path
= req
->misc
.release
.path
;
415 fc
= get_fuse_conn(path
.dentry
->d_inode
);
417 fuse_put_request(fc
, req
);
421 static void fuse_release_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
423 if (fc
->destroy_req
) {
425 * If this is a fuseblk mount, then it's possible that
426 * releasing the path will result in releasing the
427 * super block and sending the DESTROY request. If
428 * the server is single threaded, this would hang.
429 * For this reason do the path_put() in a separate
432 atomic_inc(&req
->count
);
433 INIT_WORK(&req
->misc
.release
.work
, fuse_release_async
);
434 schedule_work(&req
->misc
.release
.work
);
436 path_put(&req
->misc
.release
.path
);
440 static void fuse_file_put(struct fuse_file
*ff
, bool sync
)
442 if (atomic_dec_and_test(&ff
->count
)) {
443 struct fuse_req
*req
= ff
->reserved_req
;
448 fuse_request_send(ff
->fc
, req
);
449 path_put(&req
->misc
.release
.path
);
450 fuse_put_request(ff
->fc
, req
);
452 req
->end
= fuse_release_end
;
454 fuse_request_send_background(ff
->fc
, req
);
460 int fuse_do_open(struct fuse_conn
*fc
, u64 nodeid
, struct file
*file
,
463 struct fuse_open_out outarg
;
464 struct fuse_file
*ff
;
466 int opcode
= isdir
? FUSE_OPENDIR
: FUSE_OPEN
;
468 ff
= fuse_file_alloc(fc
);
472 err
= fuse_send_open(fc
, nodeid
, file
, opcode
, &outarg
);
479 outarg
.open_flags
&= ~FOPEN_DIRECT_IO
;
483 ff
->open_flags
= outarg
.open_flags
;
484 file
->private_data
= fuse_file_get(ff
);
488 EXPORT_SYMBOL_GPL(fuse_do_open
);
490 void fuse_finish_open(struct inode
*inode
, struct file
*file
)
492 struct fuse_file
*ff
= file
->private_data
;
493 struct fuse_conn
*fc
= get_fuse_conn(inode
);
495 if (ff
->open_flags
& FOPEN_DIRECT_IO
)
496 file
->f_op
= &fuse_direct_io_file_operations
;
497 if (!(ff
->open_flags
& FOPEN_KEEP_CACHE
))
498 invalidate_inode_pages2(inode
->i_mapping
);
499 if (ff
->open_flags
& FOPEN_NONSEEKABLE
)
500 nonseekable_open(inode
, file
);
501 if (fc
->atomic_o_trunc
&& (file
->f_flags
& O_TRUNC
)) {
502 struct fuse_inode
*fi
= get_fuse_inode(inode
);
504 spin_lock(&fc
->lock
);
505 fi
->attr_version
= ++fc
->attr_version
;
506 i_size_write(inode
, 0);
507 spin_unlock(&fc
->lock
);
508 fuse_invalidate_attr(inode
);
512 int fuse_open_common(struct inode
*inode
, struct file
*file
, bool isdir
)
514 struct fuse_conn
*fc
= get_fuse_conn(inode
);
517 err
= generic_file_open(inode
, file
);
521 err
= fuse_do_open(fc
, get_node_id(inode
), file
, isdir
);
525 fuse_finish_open(inode
, file
);
530 static void fuse_prepare_release(struct fuse_file
*ff
, int flags
, int opcode
)
532 struct fuse_conn
*fc
= ff
->fc
;
533 struct fuse_req
*req
= ff
->reserved_req
;
534 struct fuse_release_in
*inarg
= &req
->misc
.release
.in
;
536 spin_lock(&fc
->lock
);
537 list_del(&ff
->write_entry
);
538 if (!RB_EMPTY_NODE(&ff
->polled_node
))
539 rb_erase(&ff
->polled_node
, &fc
->polled_files
);
540 spin_unlock(&fc
->lock
);
542 wake_up_interruptible_all(&ff
->poll_wait
);
545 inarg
->flags
= flags
;
546 req
->in
.h
.opcode
= opcode
;
547 req
->in
.h
.nodeid
= ff
->nodeid
;
549 req
->in
.args
[0].size
= sizeof(struct fuse_release_in
);
550 req
->in
.args
[0].value
= inarg
;
553 void fuse_release_common(struct file
*file
, int opcode
)
555 struct fuse_file
*ff
;
556 struct fuse_req
*req
;
558 ff
= file
->private_data
;
562 req
= ff
->reserved_req
;
563 fuse_prepare_release(ff
, file
->f_flags
, opcode
);
566 struct fuse_release_in
*inarg
= &req
->misc
.release
.in
;
567 inarg
->release_flags
|= FUSE_RELEASE_FLOCK_UNLOCK
;
568 inarg
->lock_owner
= fuse_lock_owner_id(ff
->fc
,
571 /* Hold vfsmount and dentry until release is finished */
572 path_get(&file
->f_path
);
573 req
->misc
.release
.path
= file
->f_path
;
576 * Normally this will send the RELEASE request, however if
577 * some asynchronous READ or WRITE requests are outstanding,
578 * the sending will be delayed.
580 * Make the release synchronous if this is a fuseblk mount,
581 * synchronous RELEASE is allowed (and desirable) in this case
582 * because the server can be trusted not to screw up.
584 fuse_file_put(ff
, ff
->fc
->destroy_req
!= NULL
);
587 static int fuse_open(struct inode
*inode
, struct file
*file
)
589 return fuse_open_common(inode
, file
, false);
592 static int fuse_release(struct inode
*inode
, struct file
*file
)
594 fuse_release_common(file
, FUSE_RELEASE
);
596 /* return value is ignored by VFS */
600 void fuse_sync_release(struct fuse_file
*ff
, int flags
)
602 WARN_ON(atomic_read(&ff
->count
) > 1);
603 fuse_prepare_release(ff
, flags
, FUSE_RELEASE
);
604 ff
->reserved_req
->force
= 1;
605 ff
->reserved_req
->background
= 0;
606 fuse_request_send(ff
->fc
, ff
->reserved_req
);
607 fuse_put_request(ff
->fc
, ff
->reserved_req
);
610 EXPORT_SYMBOL_GPL(fuse_sync_release
);
613 * Scramble the ID space with XTEA, so that the value of the files_struct
614 * pointer is not exposed to userspace.
616 u64
fuse_lock_owner_id(struct fuse_conn
*fc
, fl_owner_t id
)
618 u32
*k
= fc
->scramble_key
;
619 u64 v
= (unsigned long) id
;
625 for (i
= 0; i
< 32; i
++) {
626 v0
+= ((v1
<< 4 ^ v1
>> 5) + v1
) ^ (sum
+ k
[sum
& 3]);
628 v1
+= ((v0
<< 4 ^ v0
>> 5) + v0
) ^ (sum
+ k
[sum
>>11 & 3]);
631 return (u64
) v0
+ ((u64
) v1
<< 32);
635 * Check if page is under writeback
637 * This is currently done by walking the list of writepage requests
638 * for the inode, which can be pretty inefficient.
640 static bool fuse_page_is_writeback(struct inode
*inode
, pgoff_t index
)
642 struct fuse_conn
*fc
= get_fuse_conn(inode
);
643 struct fuse_inode
*fi
= get_fuse_inode(inode
);
644 struct fuse_req
*req
;
647 spin_lock(&fc
->lock
);
648 list_for_each_entry(req
, &fi
->writepages
, writepages_entry
) {
651 BUG_ON(req
->inode
!= inode
);
652 curr_index
= req
->misc
.write
.in
.offset
>> PAGE_CACHE_SHIFT
;
653 if (curr_index
== index
) {
658 spin_unlock(&fc
->lock
);
664 * Wait for page writeback to be completed.
666 * Since fuse doesn't rely on the VM writeback tracking, this has to
667 * use some other means.
669 static int fuse_wait_on_page_writeback(struct inode
*inode
, pgoff_t index
)
671 struct fuse_inode
*fi
= get_fuse_inode(inode
);
673 wait_event(fi
->page_waitq
, !fuse_page_is_writeback(inode
, index
));
677 static int fuse_flush(struct file
*file
, fl_owner_t id
)
679 struct inode
*inode
= file_inode(file
);
680 struct fuse_conn
*fc
= get_fuse_conn(inode
);
681 struct fuse_file
*ff
= file
->private_data
;
682 struct fuse_req
*req
;
683 struct fuse_flush_in inarg
;
686 if (is_bad_inode(inode
))
692 req
= fuse_get_req_nofail_nopages(fc
, file
);
693 memset(&inarg
, 0, sizeof(inarg
));
695 inarg
.lock_owner
= fuse_lock_owner_id(fc
, id
);
696 req
->in
.h
.opcode
= FUSE_FLUSH
;
697 req
->in
.h
.nodeid
= get_node_id(inode
);
699 req
->in
.args
[0].size
= sizeof(inarg
);
700 req
->in
.args
[0].value
= &inarg
;
702 fuse_request_send(fc
, req
);
703 err
= req
->out
.h
.error
;
704 fuse_put_request(fc
, req
);
705 if (err
== -ENOSYS
) {
713 * Wait for all pending writepages on the inode to finish.
715 * This is currently done by blocking further writes with FUSE_NOWRITE
716 * and waiting for all sent writes to complete.
718 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
719 * could conflict with truncation.
721 static void fuse_sync_writes(struct inode
*inode
)
723 fuse_set_nowrite(inode
);
724 fuse_release_nowrite(inode
);
727 int fuse_fsync_common(struct file
*file
, loff_t start
, loff_t end
,
728 int datasync
, int isdir
)
730 struct inode
*inode
= file
->f_mapping
->host
;
731 struct fuse_conn
*fc
= get_fuse_conn(inode
);
732 struct fuse_file
*ff
= file
->private_data
;
733 struct fuse_req
*req
;
734 struct fuse_fsync_in inarg
;
737 if (is_bad_inode(inode
))
740 err
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
744 if ((!isdir
&& fc
->no_fsync
) || (isdir
&& fc
->no_fsyncdir
))
747 mutex_lock(&inode
->i_mutex
);
750 * Start writeback against all dirty pages of the inode, then
751 * wait for all outstanding writes, before sending the FSYNC
754 err
= write_inode_now(inode
, 0);
758 fuse_sync_writes(inode
);
760 req
= fuse_get_req_nopages(fc
);
766 memset(&inarg
, 0, sizeof(inarg
));
768 inarg
.fsync_flags
= datasync
? 1 : 0;
769 req
->in
.h
.opcode
= isdir
? FUSE_FSYNCDIR
: FUSE_FSYNC
;
770 req
->in
.h
.nodeid
= get_node_id(inode
);
772 req
->in
.args
[0].size
= sizeof(inarg
);
773 req
->in
.args
[0].value
= &inarg
;
774 fuse_request_send(fc
, req
);
775 err
= req
->out
.h
.error
;
776 fuse_put_request(fc
, req
);
777 if (err
== -ENOSYS
) {
785 mutex_unlock(&inode
->i_mutex
);
789 static int fuse_fsync(struct file
*file
, loff_t start
, loff_t end
,
792 return fuse_fsync_common(file
, start
, end
, datasync
, 0);
795 void fuse_read_fill(struct fuse_req
*req
, struct file
*file
, loff_t pos
,
796 size_t count
, int opcode
)
798 struct fuse_read_in
*inarg
= &req
->misc
.read
.in
;
799 struct fuse_file
*ff
= file
->private_data
;
804 inarg
->flags
= file
->f_flags
;
805 req
->in
.h
.opcode
= opcode
;
806 req
->in
.h
.nodeid
= ff
->nodeid
;
808 req
->in
.args
[0].size
= sizeof(struct fuse_read_in
);
809 req
->in
.args
[0].value
= inarg
;
811 req
->out
.numargs
= 1;
812 req
->out
.args
[0].size
= count
;
815 static void fuse_release_user_pages(struct fuse_req
*req
, int write
)
819 for (i
= 0; i
< req
->num_pages
; i
++) {
820 struct page
*page
= req
->pages
[i
];
822 set_page_dirty_lock(page
);
828 * In case of short read, the caller sets 'pos' to the position of
829 * actual end of fuse request in IO request. Otherwise, if bytes_requested
830 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
833 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
834 * both submitted asynchronously. The first of them was ACKed by userspace as
835 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
836 * second request was ACKed as short, e.g. only 1K was read, resulting in
839 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
840 * will be equal to the length of the longest contiguous fragment of
841 * transferred data starting from the beginning of IO request.
843 static void fuse_aio_complete(struct fuse_io_priv
*io
, int err
, ssize_t pos
)
847 spin_lock(&io
->lock
);
849 io
->err
= io
->err
? : err
;
850 else if (pos
>= 0 && (io
->bytes
< 0 || pos
< io
->bytes
))
854 spin_unlock(&io
->lock
);
861 else if (io
->bytes
>= 0 && io
->write
)
864 res
= io
->bytes
< 0 ? io
->size
: io
->bytes
;
866 if (!is_sync_kiocb(io
->iocb
)) {
867 struct path
*path
= &io
->iocb
->ki_filp
->f_path
;
868 struct inode
*inode
= path
->dentry
->d_inode
;
869 struct fuse_conn
*fc
= get_fuse_conn(inode
);
870 struct fuse_inode
*fi
= get_fuse_inode(inode
);
872 spin_lock(&fc
->lock
);
873 fi
->attr_version
= ++fc
->attr_version
;
874 spin_unlock(&fc
->lock
);
878 aio_complete(io
->iocb
, res
, 0);
883 static void fuse_aio_complete_req(struct fuse_conn
*fc
, struct fuse_req
*req
)
885 struct fuse_io_priv
*io
= req
->io
;
888 fuse_release_user_pages(req
, !io
->write
);
891 if (req
->misc
.write
.in
.size
!= req
->misc
.write
.out
.size
)
892 pos
= req
->misc
.write
.in
.offset
- io
->offset
+
893 req
->misc
.write
.out
.size
;
895 if (req
->misc
.read
.in
.size
!= req
->out
.args
[0].size
)
896 pos
= req
->misc
.read
.in
.offset
- io
->offset
+
897 req
->out
.args
[0].size
;
900 fuse_aio_complete(io
, req
->out
.h
.error
, pos
);
903 static size_t fuse_async_req_send(struct fuse_conn
*fc
, struct fuse_req
*req
,
904 size_t num_bytes
, struct fuse_io_priv
*io
)
906 spin_lock(&io
->lock
);
907 io
->size
+= num_bytes
;
909 spin_unlock(&io
->lock
);
912 req
->end
= fuse_aio_complete_req
;
914 __fuse_get_request(req
);
915 fuse_request_send_background(fc
, req
);
920 static size_t fuse_send_read(struct fuse_req
*req
, struct fuse_io_priv
*io
,
921 loff_t pos
, size_t count
, fl_owner_t owner
)
923 struct file
*file
= io
->file
;
924 struct fuse_file
*ff
= file
->private_data
;
925 struct fuse_conn
*fc
= ff
->fc
;
927 fuse_read_fill(req
, file
, pos
, count
, FUSE_READ
);
929 struct fuse_read_in
*inarg
= &req
->misc
.read
.in
;
931 inarg
->read_flags
|= FUSE_READ_LOCKOWNER
;
932 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
936 return fuse_async_req_send(fc
, req
, count
, io
);
938 fuse_request_send(fc
, req
);
939 return req
->out
.args
[0].size
;
942 static void fuse_read_update_size(struct inode
*inode
, loff_t size
,
945 struct fuse_conn
*fc
= get_fuse_conn(inode
);
946 struct fuse_inode
*fi
= get_fuse_inode(inode
);
948 spin_lock(&fc
->lock
);
949 if (attr_ver
== fi
->attr_version
&& size
< inode
->i_size
&&
950 !test_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
)) {
951 fi
->attr_version
= ++fc
->attr_version
;
952 i_size_write(inode
, size
);
954 spin_unlock(&fc
->lock
);
957 static int fuse_readpage(struct file
*file
, struct page
*page
)
959 struct fuse_io_priv io
= { .async
= 0, .file
= file
};
960 struct inode
*inode
= page
->mapping
->host
;
961 struct fuse_conn
*fc
= get_fuse_conn(inode
);
962 struct fuse_req
*req
;
964 loff_t pos
= page_offset(page
);
965 size_t count
= PAGE_CACHE_SIZE
;
970 if (is_bad_inode(inode
))
974 * Page writeback can extend beyond the lifetime of the
975 * page-cache page, so make sure we read a properly synced
978 fuse_wait_on_page_writeback(inode
, page
->index
);
980 req
= fuse_get_req(fc
, 1);
985 attr_ver
= fuse_get_attr_version(fc
);
987 req
->out
.page_zeroing
= 1;
988 req
->out
.argpages
= 1;
990 req
->pages
[0] = page
;
991 req
->page_descs
[0].length
= count
;
992 num_read
= fuse_send_read(req
, &io
, pos
, count
, NULL
);
993 err
= req
->out
.h
.error
;
994 fuse_put_request(fc
, req
);
998 * Short read means EOF. If file size is larger, truncate it
1000 if (num_read
< count
)
1001 fuse_read_update_size(inode
, pos
+ num_read
, attr_ver
);
1003 SetPageUptodate(page
);
1006 fuse_invalidate_attr(inode
); /* atime changed */
1012 static void fuse_readpages_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
1015 size_t count
= req
->misc
.read
.in
.size
;
1016 size_t num_read
= req
->out
.args
[0].size
;
1017 struct address_space
*mapping
= NULL
;
1019 for (i
= 0; mapping
== NULL
&& i
< req
->num_pages
; i
++)
1020 mapping
= req
->pages
[i
]->mapping
;
1023 struct inode
*inode
= mapping
->host
;
1026 * Short read means EOF. If file size is larger, truncate it
1028 if (!req
->out
.h
.error
&& num_read
< count
) {
1031 pos
= page_offset(req
->pages
[0]) + num_read
;
1032 fuse_read_update_size(inode
, pos
,
1033 req
->misc
.read
.attr_ver
);
1035 fuse_invalidate_attr(inode
); /* atime changed */
1038 for (i
= 0; i
< req
->num_pages
; i
++) {
1039 struct page
*page
= req
->pages
[i
];
1040 if (!req
->out
.h
.error
)
1041 SetPageUptodate(page
);
1045 page_cache_release(page
);
1048 fuse_file_put(req
->ff
, false);
1051 static void fuse_send_readpages(struct fuse_req
*req
, struct file
*file
)
1053 struct fuse_file
*ff
= file
->private_data
;
1054 struct fuse_conn
*fc
= ff
->fc
;
1055 loff_t pos
= page_offset(req
->pages
[0]);
1056 size_t count
= req
->num_pages
<< PAGE_CACHE_SHIFT
;
1058 req
->out
.argpages
= 1;
1059 req
->out
.page_zeroing
= 1;
1060 req
->out
.page_replace
= 1;
1061 fuse_read_fill(req
, file
, pos
, count
, FUSE_READ
);
1062 req
->misc
.read
.attr_ver
= fuse_get_attr_version(fc
);
1063 if (fc
->async_read
) {
1064 req
->ff
= fuse_file_get(ff
);
1065 req
->end
= fuse_readpages_end
;
1066 fuse_request_send_background_ex(fc
, req
, count
);
1068 fuse_request_send_ex(fc
, req
, count
);
1069 fuse_readpages_end(fc
, req
);
1070 fuse_put_request(fc
, req
);
1074 struct fuse_fill_data
{
1075 struct fuse_req
*req
;
1077 struct inode
*inode
;
1081 static int fuse_readpages_fill(void *_data
, struct page
*page
)
1083 struct fuse_fill_data
*data
= _data
;
1084 struct fuse_req
*req
= data
->req
;
1085 struct inode
*inode
= data
->inode
;
1086 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1088 fuse_wait_on_page_writeback(inode
, page
->index
);
1090 if (req
->num_pages
&&
1091 (req
->num_pages
== FUSE_MAX_PAGES_PER_REQ
||
1092 (req
->num_pages
+ 1) * PAGE_CACHE_SIZE
> fc
->max_read
||
1093 req
->pages
[req
->num_pages
- 1]->index
+ 1 != page
->index
)) {
1094 int nr_alloc
= min_t(unsigned, data
->nr_pages
,
1095 FUSE_MAX_PAGES_PER_REQ
);
1096 fuse_send_readpages(req
, data
->file
);
1098 req
= fuse_get_req_for_background(fc
, nr_alloc
);
1100 req
= fuse_get_req(fc
, nr_alloc
);
1105 return PTR_ERR(req
);
1109 if (WARN_ON(req
->num_pages
>= req
->max_pages
)) {
1110 fuse_put_request(fc
, req
);
1114 page_cache_get(page
);
1115 req
->pages
[req
->num_pages
] = page
;
1116 req
->page_descs
[req
->num_pages
].length
= PAGE_SIZE
;
1122 static int fuse_readpages(struct file
*file
, struct address_space
*mapping
,
1123 struct list_head
*pages
, unsigned nr_pages
)
1125 struct inode
*inode
= mapping
->host
;
1126 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1127 struct fuse_fill_data data
;
1129 int nr_alloc
= min_t(unsigned, nr_pages
, FUSE_MAX_PAGES_PER_REQ
);
1132 if (is_bad_inode(inode
))
1138 data
.req
= fuse_get_req_for_background(fc
, nr_alloc
);
1140 data
.req
= fuse_get_req(fc
, nr_alloc
);
1141 data
.nr_pages
= nr_pages
;
1142 err
= PTR_ERR(data
.req
);
1143 if (IS_ERR(data
.req
))
1146 err
= read_cache_pages(mapping
, pages
, fuse_readpages_fill
, &data
);
1148 if (data
.req
->num_pages
)
1149 fuse_send_readpages(data
.req
, file
);
1151 fuse_put_request(fc
, data
.req
);
1157 static ssize_t
fuse_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
1158 unsigned long nr_segs
, loff_t pos
)
1160 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
1161 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1164 * In auto invalidate mode, always update attributes on read.
1165 * Otherwise, only update if we attempt to read past EOF (to ensure
1166 * i_size is up to date).
1168 if (fc
->auto_inval_data
||
1169 (pos
+ iov_length(iov
, nr_segs
) > i_size_read(inode
))) {
1171 err
= fuse_update_attributes(inode
, NULL
, iocb
->ki_filp
, NULL
);
1176 return generic_file_aio_read(iocb
, iov
, nr_segs
, pos
);
1179 static void fuse_write_fill(struct fuse_req
*req
, struct fuse_file
*ff
,
1180 loff_t pos
, size_t count
)
1182 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
1183 struct fuse_write_out
*outarg
= &req
->misc
.write
.out
;
1186 inarg
->offset
= pos
;
1187 inarg
->size
= count
;
1188 req
->in
.h
.opcode
= FUSE_WRITE
;
1189 req
->in
.h
.nodeid
= ff
->nodeid
;
1190 req
->in
.numargs
= 2;
1191 if (ff
->fc
->minor
< 9)
1192 req
->in
.args
[0].size
= FUSE_COMPAT_WRITE_IN_SIZE
;
1194 req
->in
.args
[0].size
= sizeof(struct fuse_write_in
);
1195 req
->in
.args
[0].value
= inarg
;
1196 req
->in
.args
[1].size
= count
;
1197 req
->out
.numargs
= 1;
1198 req
->out
.args
[0].size
= sizeof(struct fuse_write_out
);
1199 req
->out
.args
[0].value
= outarg
;
1202 static size_t fuse_send_write(struct fuse_req
*req
, struct fuse_io_priv
*io
,
1203 loff_t pos
, size_t count
, fl_owner_t owner
)
1205 struct file
*file
= io
->file
;
1206 struct fuse_file
*ff
= file
->private_data
;
1207 struct fuse_conn
*fc
= ff
->fc
;
1208 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
1210 fuse_write_fill(req
, ff
, pos
, count
);
1211 inarg
->flags
= file
->f_flags
;
1212 if (owner
!= NULL
) {
1213 inarg
->write_flags
|= FUSE_WRITE_LOCKOWNER
;
1214 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
1218 return fuse_async_req_send(fc
, req
, count
, io
);
1220 fuse_request_send_ex(fc
, req
, count
);
1221 return req
->misc
.write
.out
.size
;
1224 void fuse_write_update_size(struct inode
*inode
, loff_t pos
)
1226 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1227 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1229 spin_lock(&fc
->lock
);
1230 fi
->attr_version
= ++fc
->attr_version
;
1231 if (pos
> inode
->i_size
)
1232 i_size_write(inode
, pos
);
1233 spin_unlock(&fc
->lock
);
1236 static size_t fuse_send_write_pages(struct fuse_req
*req
, struct file
*file
,
1237 struct inode
*inode
, loff_t pos
,
1243 struct fuse_io_priv io
= { .async
= 0, .file
= file
};
1245 for (i
= 0; i
< req
->num_pages
; i
++)
1246 fuse_wait_on_page_writeback(inode
, req
->pages
[i
]->index
);
1248 res
= fuse_send_write(req
, &io
, pos
, count
, NULL
);
1250 offset
= req
->page_descs
[0].offset
;
1252 for (i
= 0; i
< req
->num_pages
; i
++) {
1253 struct page
*page
= req
->pages
[i
];
1255 if (!req
->out
.h
.error
&& !offset
&& count
>= PAGE_CACHE_SIZE
)
1256 SetPageUptodate(page
);
1258 if (count
> PAGE_CACHE_SIZE
- offset
)
1259 count
-= PAGE_CACHE_SIZE
- offset
;
1265 page_cache_release(page
);
1271 static ssize_t
fuse_fill_write_pages(struct fuse_req
*req
,
1272 struct address_space
*mapping
,
1273 struct iov_iter
*ii
, loff_t pos
)
1275 struct fuse_conn
*fc
= get_fuse_conn(mapping
->host
);
1276 unsigned offset
= pos
& (PAGE_CACHE_SIZE
- 1);
1280 req
->in
.argpages
= 1;
1281 req
->page_descs
[0].offset
= offset
;
1286 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
1287 size_t bytes
= min_t(size_t, PAGE_CACHE_SIZE
- offset
,
1288 iov_iter_count(ii
));
1290 bytes
= min_t(size_t, bytes
, fc
->max_write
- count
);
1294 if (iov_iter_fault_in_readable(ii
, bytes
))
1298 page
= grab_cache_page_write_begin(mapping
, index
, 0);
1302 if (mapping_writably_mapped(mapping
))
1303 flush_dcache_page(page
);
1305 pagefault_disable();
1306 tmp
= iov_iter_copy_from_user_atomic(page
, ii
, offset
, bytes
);
1308 flush_dcache_page(page
);
1310 mark_page_accessed(page
);
1312 iov_iter_advance(ii
, tmp
);
1315 page_cache_release(page
);
1316 bytes
= min(bytes
, iov_iter_single_seg_count(ii
));
1321 req
->pages
[req
->num_pages
] = page
;
1322 req
->page_descs
[req
->num_pages
].length
= tmp
;
1328 if (offset
== PAGE_CACHE_SIZE
)
1331 if (!fc
->big_writes
)
1333 } while (iov_iter_count(ii
) && count
< fc
->max_write
&&
1334 req
->num_pages
< req
->max_pages
&& offset
== 0);
1336 return count
> 0 ? count
: err
;
1339 static inline unsigned fuse_wr_pages(loff_t pos
, size_t len
)
1341 return min_t(unsigned,
1342 ((pos
+ len
- 1) >> PAGE_CACHE_SHIFT
) -
1343 (pos
>> PAGE_CACHE_SHIFT
) + 1,
1344 FUSE_MAX_PAGES_PER_REQ
);
1347 static ssize_t
fuse_perform_write(struct file
*file
,
1348 struct address_space
*mapping
,
1349 struct iov_iter
*ii
, loff_t pos
)
1351 struct inode
*inode
= mapping
->host
;
1352 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1353 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1357 if (is_bad_inode(inode
))
1360 if (inode
->i_size
< pos
+ iov_iter_count(ii
))
1361 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1364 struct fuse_req
*req
;
1366 unsigned nr_pages
= fuse_wr_pages(pos
, iov_iter_count(ii
));
1368 req
= fuse_get_req(fc
, nr_pages
);
1374 count
= fuse_fill_write_pages(req
, mapping
, ii
, pos
);
1380 num_written
= fuse_send_write_pages(req
, file
, inode
,
1382 err
= req
->out
.h
.error
;
1387 /* break out of the loop on short write */
1388 if (num_written
!= count
)
1392 fuse_put_request(fc
, req
);
1393 } while (!err
&& iov_iter_count(ii
));
1396 fuse_write_update_size(inode
, pos
);
1398 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1399 fuse_invalidate_attr(inode
);
1401 return res
> 0 ? res
: err
;
1404 static ssize_t
fuse_file_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
1405 unsigned long nr_segs
, loff_t pos
)
1407 struct file
*file
= iocb
->ki_filp
;
1408 struct address_space
*mapping
= file
->f_mapping
;
1411 ssize_t written
= 0;
1412 ssize_t written_buffered
= 0;
1413 struct inode
*inode
= mapping
->host
;
1418 WARN_ON(iocb
->ki_pos
!= pos
);
1421 err
= generic_segment_checks(iov
, &nr_segs
, &ocount
, VERIFY_READ
);
1426 mutex_lock(&inode
->i_mutex
);
1428 /* We can write back this queue in page reclaim */
1429 current
->backing_dev_info
= mapping
->backing_dev_info
;
1431 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
1438 err
= file_remove_suid(file
);
1442 err
= file_update_time(file
);
1446 if (file
->f_flags
& O_DIRECT
) {
1447 written
= generic_file_direct_write(iocb
, iov
, &nr_segs
,
1450 if (written
< 0 || written
== count
)
1456 iov_iter_init(&i
, iov
, nr_segs
, count
, written
);
1457 written_buffered
= fuse_perform_write(file
, mapping
, &i
, pos
);
1458 if (written_buffered
< 0) {
1459 err
= written_buffered
;
1462 endbyte
= pos
+ written_buffered
- 1;
1464 err
= filemap_write_and_wait_range(file
->f_mapping
, pos
,
1469 invalidate_mapping_pages(file
->f_mapping
,
1470 pos
>> PAGE_CACHE_SHIFT
,
1471 endbyte
>> PAGE_CACHE_SHIFT
);
1473 written
+= written_buffered
;
1474 iocb
->ki_pos
= pos
+ written_buffered
;
1476 iov_iter_init(&i
, iov
, nr_segs
, count
, 0);
1477 written
= fuse_perform_write(file
, mapping
, &i
, pos
);
1479 iocb
->ki_pos
= pos
+ written
;
1482 current
->backing_dev_info
= NULL
;
1483 mutex_unlock(&inode
->i_mutex
);
1485 return written
? written
: err
;
1488 static inline void fuse_page_descs_length_init(struct fuse_req
*req
,
1489 unsigned index
, unsigned nr_pages
)
1493 for (i
= index
; i
< index
+ nr_pages
; i
++)
1494 req
->page_descs
[i
].length
= PAGE_SIZE
-
1495 req
->page_descs
[i
].offset
;
1498 static inline unsigned long fuse_get_user_addr(const struct iov_iter
*ii
)
1500 return (unsigned long)ii
->iov
->iov_base
+ ii
->iov_offset
;
1503 static inline size_t fuse_get_frag_size(const struct iov_iter
*ii
,
1506 return min(iov_iter_single_seg_count(ii
), max_size
);
1509 static int fuse_get_user_pages(struct fuse_req
*req
, struct iov_iter
*ii
,
1510 size_t *nbytesp
, int write
)
1512 size_t nbytes
= 0; /* # bytes already packed in req */
1514 /* Special case for kernel I/O: can copy directly into the buffer */
1515 if (segment_eq(get_fs(), KERNEL_DS
)) {
1516 unsigned long user_addr
= fuse_get_user_addr(ii
);
1517 size_t frag_size
= fuse_get_frag_size(ii
, *nbytesp
);
1520 req
->in
.args
[1].value
= (void *) user_addr
;
1522 req
->out
.args
[0].value
= (void *) user_addr
;
1524 iov_iter_advance(ii
, frag_size
);
1525 *nbytesp
= frag_size
;
1529 while (nbytes
< *nbytesp
&& req
->num_pages
< req
->max_pages
) {
1531 unsigned long user_addr
= fuse_get_user_addr(ii
);
1532 unsigned offset
= user_addr
& ~PAGE_MASK
;
1533 size_t frag_size
= fuse_get_frag_size(ii
, *nbytesp
- nbytes
);
1536 unsigned n
= req
->max_pages
- req
->num_pages
;
1537 frag_size
= min_t(size_t, frag_size
, n
<< PAGE_SHIFT
);
1539 npages
= (frag_size
+ offset
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1540 npages
= clamp(npages
, 1U, n
);
1542 ret
= get_user_pages_fast(user_addr
, npages
, !write
,
1543 &req
->pages
[req
->num_pages
]);
1548 frag_size
= min_t(size_t, frag_size
,
1549 (npages
<< PAGE_SHIFT
) - offset
);
1550 iov_iter_advance(ii
, frag_size
);
1552 req
->page_descs
[req
->num_pages
].offset
= offset
;
1553 fuse_page_descs_length_init(req
, req
->num_pages
, npages
);
1555 req
->num_pages
+= npages
;
1556 req
->page_descs
[req
->num_pages
- 1].length
-=
1557 (npages
<< PAGE_SHIFT
) - offset
- frag_size
;
1559 nbytes
+= frag_size
;
1563 req
->in
.argpages
= 1;
1565 req
->out
.argpages
= 1;
1572 static inline int fuse_iter_npages(const struct iov_iter
*ii_p
)
1574 struct iov_iter ii
= *ii_p
;
1577 while (iov_iter_count(&ii
) && npages
< FUSE_MAX_PAGES_PER_REQ
) {
1578 unsigned long user_addr
= fuse_get_user_addr(&ii
);
1579 unsigned offset
= user_addr
& ~PAGE_MASK
;
1580 size_t frag_size
= iov_iter_single_seg_count(&ii
);
1582 npages
+= (frag_size
+ offset
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1583 iov_iter_advance(&ii
, frag_size
);
1586 return min(npages
, FUSE_MAX_PAGES_PER_REQ
);
1589 ssize_t
fuse_direct_io(struct fuse_io_priv
*io
, const struct iovec
*iov
,
1590 unsigned long nr_segs
, size_t count
, loff_t
*ppos
,
1593 struct file
*file
= io
->file
;
1594 struct fuse_file
*ff
= file
->private_data
;
1595 struct fuse_conn
*fc
= ff
->fc
;
1596 size_t nmax
= write
? fc
->max_write
: fc
->max_read
;
1599 struct fuse_req
*req
;
1602 iov_iter_init(&ii
, iov
, nr_segs
, count
, 0);
1605 req
= fuse_get_req_for_background(fc
, fuse_iter_npages(&ii
));
1607 req
= fuse_get_req(fc
, fuse_iter_npages(&ii
));
1609 return PTR_ERR(req
);
1613 fl_owner_t owner
= current
->files
;
1614 size_t nbytes
= min(count
, nmax
);
1615 int err
= fuse_get_user_pages(req
, &ii
, &nbytes
, write
);
1622 nres
= fuse_send_write(req
, io
, pos
, nbytes
, owner
);
1624 nres
= fuse_send_read(req
, io
, pos
, nbytes
, owner
);
1627 fuse_release_user_pages(req
, !write
);
1628 if (req
->out
.h
.error
) {
1630 res
= req
->out
.h
.error
;
1632 } else if (nres
> nbytes
) {
1642 fuse_put_request(fc
, req
);
1644 req
= fuse_get_req_for_background(fc
,
1645 fuse_iter_npages(&ii
));
1647 req
= fuse_get_req(fc
, fuse_iter_npages(&ii
));
1653 fuse_put_request(fc
, req
);
1659 EXPORT_SYMBOL_GPL(fuse_direct_io
);
1661 static ssize_t
__fuse_direct_read(struct fuse_io_priv
*io
,
1662 const struct iovec
*iov
,
1663 unsigned long nr_segs
, loff_t
*ppos
,
1667 struct file
*file
= io
->file
;
1668 struct inode
*inode
= file_inode(file
);
1670 if (is_bad_inode(inode
))
1673 res
= fuse_direct_io(io
, iov
, nr_segs
, count
, ppos
, 0);
1675 fuse_invalidate_attr(inode
);
1680 static ssize_t
fuse_direct_read(struct file
*file
, char __user
*buf
,
1681 size_t count
, loff_t
*ppos
)
1683 struct fuse_io_priv io
= { .async
= 0, .file
= file
};
1684 struct iovec iov
= { .iov_base
= buf
, .iov_len
= count
};
1685 return __fuse_direct_read(&io
, &iov
, 1, ppos
, count
);
1688 static ssize_t
__fuse_direct_write(struct fuse_io_priv
*io
,
1689 const struct iovec
*iov
,
1690 unsigned long nr_segs
, loff_t
*ppos
)
1692 struct file
*file
= io
->file
;
1693 struct inode
*inode
= file_inode(file
);
1694 size_t count
= iov_length(iov
, nr_segs
);
1697 res
= generic_write_checks(file
, ppos
, &count
, 0);
1699 res
= fuse_direct_io(io
, iov
, nr_segs
, count
, ppos
, 1);
1701 fuse_invalidate_attr(inode
);
1706 static ssize_t
fuse_direct_write(struct file
*file
, const char __user
*buf
,
1707 size_t count
, loff_t
*ppos
)
1709 struct iovec iov
= { .iov_base
= (void __user
*)buf
, .iov_len
= count
};
1710 struct inode
*inode
= file_inode(file
);
1712 struct fuse_io_priv io
= { .async
= 0, .file
= file
};
1714 if (is_bad_inode(inode
))
1717 /* Don't allow parallel writes to the same file */
1718 mutex_lock(&inode
->i_mutex
);
1719 res
= __fuse_direct_write(&io
, &iov
, 1, ppos
);
1721 fuse_write_update_size(inode
, *ppos
);
1722 mutex_unlock(&inode
->i_mutex
);
1727 static void fuse_writepage_free(struct fuse_conn
*fc
, struct fuse_req
*req
)
1729 __free_page(req
->pages
[0]);
1730 fuse_file_put(req
->ff
, false);
1733 static void fuse_writepage_finish(struct fuse_conn
*fc
, struct fuse_req
*req
)
1735 struct inode
*inode
= req
->inode
;
1736 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1737 struct backing_dev_info
*bdi
= inode
->i_mapping
->backing_dev_info
;
1739 list_del(&req
->writepages_entry
);
1740 dec_bdi_stat(bdi
, BDI_WRITEBACK
);
1741 dec_zone_page_state(req
->pages
[0], NR_WRITEBACK_TEMP
);
1742 bdi_writeout_inc(bdi
);
1743 wake_up(&fi
->page_waitq
);
1746 /* Called under fc->lock, may release and reacquire it */
1747 static void fuse_send_writepage(struct fuse_conn
*fc
, struct fuse_req
*req
)
1748 __releases(fc
->lock
)
1749 __acquires(fc
->lock
)
1751 struct fuse_inode
*fi
= get_fuse_inode(req
->inode
);
1752 loff_t size
= i_size_read(req
->inode
);
1753 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
1758 if (inarg
->offset
+ PAGE_CACHE_SIZE
<= size
) {
1759 inarg
->size
= PAGE_CACHE_SIZE
;
1760 } else if (inarg
->offset
< size
) {
1761 inarg
->size
= size
& (PAGE_CACHE_SIZE
- 1);
1763 /* Got truncated off completely */
1767 req
->in
.args
[1].size
= inarg
->size
;
1769 fuse_request_send_background_locked(fc
, req
);
1773 fuse_writepage_finish(fc
, req
);
1774 spin_unlock(&fc
->lock
);
1775 fuse_writepage_free(fc
, req
);
1776 fuse_put_request(fc
, req
);
1777 spin_lock(&fc
->lock
);
1781 * If fi->writectr is positive (no truncate or fsync going on) send
1782 * all queued writepage requests.
1784 * Called with fc->lock
1786 void fuse_flush_writepages(struct inode
*inode
)
1787 __releases(fc
->lock
)
1788 __acquires(fc
->lock
)
1790 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1791 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1792 struct fuse_req
*req
;
1794 while (fi
->writectr
>= 0 && !list_empty(&fi
->queued_writes
)) {
1795 req
= list_entry(fi
->queued_writes
.next
, struct fuse_req
, list
);
1796 list_del_init(&req
->list
);
1797 fuse_send_writepage(fc
, req
);
1801 static void fuse_writepage_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
1803 struct inode
*inode
= req
->inode
;
1804 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1806 mapping_set_error(inode
->i_mapping
, req
->out
.h
.error
);
1807 spin_lock(&fc
->lock
);
1809 fuse_writepage_finish(fc
, req
);
1810 spin_unlock(&fc
->lock
);
1811 fuse_writepage_free(fc
, req
);
1814 static int fuse_writepage_locked(struct page
*page
)
1816 struct address_space
*mapping
= page
->mapping
;
1817 struct inode
*inode
= mapping
->host
;
1818 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1819 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1820 struct fuse_req
*req
;
1821 struct fuse_file
*ff
;
1822 struct page
*tmp_page
;
1824 set_page_writeback(page
);
1826 req
= fuse_request_alloc_nofs(1);
1830 req
->background
= 1; /* writeback always goes to bg_queue */
1831 tmp_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
1835 spin_lock(&fc
->lock
);
1836 BUG_ON(list_empty(&fi
->write_files
));
1837 ff
= list_entry(fi
->write_files
.next
, struct fuse_file
, write_entry
);
1838 req
->ff
= fuse_file_get(ff
);
1839 spin_unlock(&fc
->lock
);
1841 fuse_write_fill(req
, ff
, page_offset(page
), 0);
1843 copy_highpage(tmp_page
, page
);
1844 req
->misc
.write
.in
.write_flags
|= FUSE_WRITE_CACHE
;
1845 req
->in
.argpages
= 1;
1847 req
->pages
[0] = tmp_page
;
1848 req
->page_descs
[0].offset
= 0;
1849 req
->page_descs
[0].length
= PAGE_SIZE
;
1850 req
->end
= fuse_writepage_end
;
1853 inc_bdi_stat(mapping
->backing_dev_info
, BDI_WRITEBACK
);
1854 inc_zone_page_state(tmp_page
, NR_WRITEBACK_TEMP
);
1856 spin_lock(&fc
->lock
);
1857 list_add(&req
->writepages_entry
, &fi
->writepages
);
1858 list_add_tail(&req
->list
, &fi
->queued_writes
);
1859 fuse_flush_writepages(inode
);
1860 spin_unlock(&fc
->lock
);
1862 end_page_writeback(page
);
1867 fuse_request_free(req
);
1869 end_page_writeback(page
);
1873 static int fuse_writepage(struct page
*page
, struct writeback_control
*wbc
)
1877 err
= fuse_writepage_locked(page
);
1883 static int fuse_launder_page(struct page
*page
)
1886 if (clear_page_dirty_for_io(page
)) {
1887 struct inode
*inode
= page
->mapping
->host
;
1888 err
= fuse_writepage_locked(page
);
1890 fuse_wait_on_page_writeback(inode
, page
->index
);
1896 * Write back dirty pages now, because there may not be any suitable
1899 static void fuse_vma_close(struct vm_area_struct
*vma
)
1901 filemap_write_and_wait(vma
->vm_file
->f_mapping
);
1905 * Wait for writeback against this page to complete before allowing it
1906 * to be marked dirty again, and hence written back again, possibly
1907 * before the previous writepage completed.
1909 * Block here, instead of in ->writepage(), so that the userspace fs
1910 * can only block processes actually operating on the filesystem.
1912 * Otherwise unprivileged userspace fs would be able to block
1917 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
1919 static int fuse_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1921 struct page
*page
= vmf
->page
;
1923 * Don't use page->mapping as it may become NULL from a
1924 * concurrent truncate.
1926 struct inode
*inode
= vma
->vm_file
->f_mapping
->host
;
1928 fuse_wait_on_page_writeback(inode
, page
->index
);
1932 static const struct vm_operations_struct fuse_file_vm_ops
= {
1933 .close
= fuse_vma_close
,
1934 .fault
= filemap_fault
,
1935 .page_mkwrite
= fuse_page_mkwrite
,
1936 .remap_pages
= generic_file_remap_pages
,
1939 static int fuse_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1941 if ((vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
)) {
1942 struct inode
*inode
= file_inode(file
);
1943 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1944 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1945 struct fuse_file
*ff
= file
->private_data
;
1947 * file may be written through mmap, so chain it onto the
1948 * inodes's write_file list
1950 spin_lock(&fc
->lock
);
1951 if (list_empty(&ff
->write_entry
))
1952 list_add(&ff
->write_entry
, &fi
->write_files
);
1953 spin_unlock(&fc
->lock
);
1955 file_accessed(file
);
1956 vma
->vm_ops
= &fuse_file_vm_ops
;
1960 static int fuse_direct_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1962 /* Can't provide the coherency needed for MAP_SHARED */
1963 if (vma
->vm_flags
& VM_MAYSHARE
)
1966 invalidate_inode_pages2(file
->f_mapping
);
1968 return generic_file_mmap(file
, vma
);
1971 static int convert_fuse_file_lock(const struct fuse_file_lock
*ffl
,
1972 struct file_lock
*fl
)
1974 switch (ffl
->type
) {
1980 if (ffl
->start
> OFFSET_MAX
|| ffl
->end
> OFFSET_MAX
||
1981 ffl
->end
< ffl
->start
)
1984 fl
->fl_start
= ffl
->start
;
1985 fl
->fl_end
= ffl
->end
;
1986 fl
->fl_pid
= ffl
->pid
;
1992 fl
->fl_type
= ffl
->type
;
1996 static void fuse_lk_fill(struct fuse_req
*req
, struct file
*file
,
1997 const struct file_lock
*fl
, int opcode
, pid_t pid
,
2000 struct inode
*inode
= file_inode(file
);
2001 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2002 struct fuse_file
*ff
= file
->private_data
;
2003 struct fuse_lk_in
*arg
= &req
->misc
.lk_in
;
2006 arg
->owner
= fuse_lock_owner_id(fc
, fl
->fl_owner
);
2007 arg
->lk
.start
= fl
->fl_start
;
2008 arg
->lk
.end
= fl
->fl_end
;
2009 arg
->lk
.type
= fl
->fl_type
;
2012 arg
->lk_flags
|= FUSE_LK_FLOCK
;
2013 req
->in
.h
.opcode
= opcode
;
2014 req
->in
.h
.nodeid
= get_node_id(inode
);
2015 req
->in
.numargs
= 1;
2016 req
->in
.args
[0].size
= sizeof(*arg
);
2017 req
->in
.args
[0].value
= arg
;
2020 static int fuse_getlk(struct file
*file
, struct file_lock
*fl
)
2022 struct inode
*inode
= file_inode(file
);
2023 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2024 struct fuse_req
*req
;
2025 struct fuse_lk_out outarg
;
2028 req
= fuse_get_req_nopages(fc
);
2030 return PTR_ERR(req
);
2032 fuse_lk_fill(req
, file
, fl
, FUSE_GETLK
, 0, 0);
2033 req
->out
.numargs
= 1;
2034 req
->out
.args
[0].size
= sizeof(outarg
);
2035 req
->out
.args
[0].value
= &outarg
;
2036 fuse_request_send(fc
, req
);
2037 err
= req
->out
.h
.error
;
2038 fuse_put_request(fc
, req
);
2040 err
= convert_fuse_file_lock(&outarg
.lk
, fl
);
2045 static int fuse_setlk(struct file
*file
, struct file_lock
*fl
, int flock
)
2047 struct inode
*inode
= file_inode(file
);
2048 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2049 struct fuse_req
*req
;
2050 int opcode
= (fl
->fl_flags
& FL_SLEEP
) ? FUSE_SETLKW
: FUSE_SETLK
;
2051 pid_t pid
= fl
->fl_type
!= F_UNLCK
? current
->tgid
: 0;
2054 if (fl
->fl_lmops
&& fl
->fl_lmops
->lm_grant
) {
2055 /* NLM needs asynchronous locks, which we don't support yet */
2059 /* Unlock on close is handled by the flush method */
2060 if (fl
->fl_flags
& FL_CLOSE
)
2063 req
= fuse_get_req_nopages(fc
);
2065 return PTR_ERR(req
);
2067 fuse_lk_fill(req
, file
, fl
, opcode
, pid
, flock
);
2068 fuse_request_send(fc
, req
);
2069 err
= req
->out
.h
.error
;
2070 /* locking is restartable */
2073 fuse_put_request(fc
, req
);
2077 static int fuse_file_lock(struct file
*file
, int cmd
, struct file_lock
*fl
)
2079 struct inode
*inode
= file_inode(file
);
2080 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2083 if (cmd
== F_CANCELLK
) {
2085 } else if (cmd
== F_GETLK
) {
2087 posix_test_lock(file
, fl
);
2090 err
= fuse_getlk(file
, fl
);
2093 err
= posix_lock_file(file
, fl
, NULL
);
2095 err
= fuse_setlk(file
, fl
, 0);
2100 static int fuse_file_flock(struct file
*file
, int cmd
, struct file_lock
*fl
)
2102 struct inode
*inode
= file_inode(file
);
2103 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2107 err
= flock_lock_file_wait(file
, fl
);
2109 struct fuse_file
*ff
= file
->private_data
;
2111 /* emulate flock with POSIX locks */
2112 fl
->fl_owner
= (fl_owner_t
) file
;
2114 err
= fuse_setlk(file
, fl
, 1);
2120 static sector_t
fuse_bmap(struct address_space
*mapping
, sector_t block
)
2122 struct inode
*inode
= mapping
->host
;
2123 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2124 struct fuse_req
*req
;
2125 struct fuse_bmap_in inarg
;
2126 struct fuse_bmap_out outarg
;
2129 if (!inode
->i_sb
->s_bdev
|| fc
->no_bmap
)
2132 req
= fuse_get_req_nopages(fc
);
2136 memset(&inarg
, 0, sizeof(inarg
));
2137 inarg
.block
= block
;
2138 inarg
.blocksize
= inode
->i_sb
->s_blocksize
;
2139 req
->in
.h
.opcode
= FUSE_BMAP
;
2140 req
->in
.h
.nodeid
= get_node_id(inode
);
2141 req
->in
.numargs
= 1;
2142 req
->in
.args
[0].size
= sizeof(inarg
);
2143 req
->in
.args
[0].value
= &inarg
;
2144 req
->out
.numargs
= 1;
2145 req
->out
.args
[0].size
= sizeof(outarg
);
2146 req
->out
.args
[0].value
= &outarg
;
2147 fuse_request_send(fc
, req
);
2148 err
= req
->out
.h
.error
;
2149 fuse_put_request(fc
, req
);
2153 return err
? 0 : outarg
.block
;
2156 static loff_t
fuse_file_llseek(struct file
*file
, loff_t offset
, int whence
)
2159 struct inode
*inode
= file_inode(file
);
2161 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2162 if (whence
== SEEK_CUR
|| whence
== SEEK_SET
)
2163 return generic_file_llseek(file
, offset
, whence
);
2165 mutex_lock(&inode
->i_mutex
);
2166 retval
= fuse_update_attributes(inode
, NULL
, file
, NULL
);
2168 retval
= generic_file_llseek(file
, offset
, whence
);
2169 mutex_unlock(&inode
->i_mutex
);
2174 static int fuse_ioctl_copy_user(struct page
**pages
, struct iovec
*iov
,
2175 unsigned int nr_segs
, size_t bytes
, bool to_user
)
2183 iov_iter_init(&ii
, iov
, nr_segs
, bytes
, 0);
2185 while (iov_iter_count(&ii
)) {
2186 struct page
*page
= pages
[page_idx
++];
2187 size_t todo
= min_t(size_t, PAGE_SIZE
, iov_iter_count(&ii
));
2193 char __user
*uaddr
= ii
.iov
->iov_base
+ ii
.iov_offset
;
2194 size_t iov_len
= ii
.iov
->iov_len
- ii
.iov_offset
;
2195 size_t copy
= min(todo
, iov_len
);
2199 left
= copy_from_user(kaddr
, uaddr
, copy
);
2201 left
= copy_to_user(uaddr
, kaddr
, copy
);
2206 iov_iter_advance(&ii
, copy
);
2218 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2219 * ABI was defined to be 'struct iovec' which is different on 32bit
2220 * and 64bit. Fortunately we can determine which structure the server
2221 * used from the size of the reply.
2223 static int fuse_copy_ioctl_iovec_old(struct iovec
*dst
, void *src
,
2224 size_t transferred
, unsigned count
,
2227 #ifdef CONFIG_COMPAT
2228 if (count
* sizeof(struct compat_iovec
) == transferred
) {
2229 struct compat_iovec
*ciov
= src
;
2233 * With this interface a 32bit server cannot support
2234 * non-compat (i.e. ones coming from 64bit apps) ioctl
2240 for (i
= 0; i
< count
; i
++) {
2241 dst
[i
].iov_base
= compat_ptr(ciov
[i
].iov_base
);
2242 dst
[i
].iov_len
= ciov
[i
].iov_len
;
2248 if (count
* sizeof(struct iovec
) != transferred
)
2251 memcpy(dst
, src
, transferred
);
2255 /* Make sure iov_length() won't overflow */
2256 static int fuse_verify_ioctl_iov(struct iovec
*iov
, size_t count
)
2259 u32 max
= FUSE_MAX_PAGES_PER_REQ
<< PAGE_SHIFT
;
2261 for (n
= 0; n
< count
; n
++, iov
++) {
2262 if (iov
->iov_len
> (size_t) max
)
2264 max
-= iov
->iov_len
;
2269 static int fuse_copy_ioctl_iovec(struct fuse_conn
*fc
, struct iovec
*dst
,
2270 void *src
, size_t transferred
, unsigned count
,
2274 struct fuse_ioctl_iovec
*fiov
= src
;
2276 if (fc
->minor
< 16) {
2277 return fuse_copy_ioctl_iovec_old(dst
, src
, transferred
,
2281 if (count
* sizeof(struct fuse_ioctl_iovec
) != transferred
)
2284 for (i
= 0; i
< count
; i
++) {
2285 /* Did the server supply an inappropriate value? */
2286 if (fiov
[i
].base
!= (unsigned long) fiov
[i
].base
||
2287 fiov
[i
].len
!= (unsigned long) fiov
[i
].len
)
2290 dst
[i
].iov_base
= (void __user
*) (unsigned long) fiov
[i
].base
;
2291 dst
[i
].iov_len
= (size_t) fiov
[i
].len
;
2293 #ifdef CONFIG_COMPAT
2295 (ptr_to_compat(dst
[i
].iov_base
) != fiov
[i
].base
||
2296 (compat_size_t
) dst
[i
].iov_len
!= fiov
[i
].len
))
2306 * For ioctls, there is no generic way to determine how much memory
2307 * needs to be read and/or written. Furthermore, ioctls are allowed
2308 * to dereference the passed pointer, so the parameter requires deep
2309 * copying but FUSE has no idea whatsoever about what to copy in or
2312 * This is solved by allowing FUSE server to retry ioctl with
2313 * necessary in/out iovecs. Let's assume the ioctl implementation
2314 * needs to read in the following structure.
2321 * On the first callout to FUSE server, inarg->in_size and
2322 * inarg->out_size will be NULL; then, the server completes the ioctl
2323 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2324 * the actual iov array to
2326 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2328 * which tells FUSE to copy in the requested area and retry the ioctl.
2329 * On the second round, the server has access to the structure and
2330 * from that it can tell what to look for next, so on the invocation,
2331 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2333 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2334 * { .iov_base = a.buf, .iov_len = a.buflen } }
2336 * FUSE will copy both struct a and the pointed buffer from the
2337 * process doing the ioctl and retry ioctl with both struct a and the
2340 * This time, FUSE server has everything it needs and completes ioctl
2341 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2343 * Copying data out works the same way.
2345 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2346 * automatically initializes in and out iovs by decoding @cmd with
2347 * _IOC_* macros and the server is not allowed to request RETRY. This
2348 * limits ioctl data transfers to well-formed ioctls and is the forced
2349 * behavior for all FUSE servers.
2351 long fuse_do_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
,
2354 struct fuse_file
*ff
= file
->private_data
;
2355 struct fuse_conn
*fc
= ff
->fc
;
2356 struct fuse_ioctl_in inarg
= {
2362 struct fuse_ioctl_out outarg
;
2363 struct fuse_req
*req
= NULL
;
2364 struct page
**pages
= NULL
;
2365 struct iovec
*iov_page
= NULL
;
2366 struct iovec
*in_iov
= NULL
, *out_iov
= NULL
;
2367 unsigned int in_iovs
= 0, out_iovs
= 0, num_pages
= 0, max_pages
;
2368 size_t in_size
, out_size
, transferred
;
2371 #if BITS_PER_LONG == 32
2372 inarg
.flags
|= FUSE_IOCTL_32BIT
;
2374 if (flags
& FUSE_IOCTL_COMPAT
)
2375 inarg
.flags
|= FUSE_IOCTL_32BIT
;
2378 /* assume all the iovs returned by client always fits in a page */
2379 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec
) * FUSE_IOCTL_MAX_IOV
> PAGE_SIZE
);
2382 pages
= kcalloc(FUSE_MAX_PAGES_PER_REQ
, sizeof(pages
[0]), GFP_KERNEL
);
2383 iov_page
= (struct iovec
*) __get_free_page(GFP_KERNEL
);
2384 if (!pages
|| !iov_page
)
2388 * If restricted, initialize IO parameters as encoded in @cmd.
2389 * RETRY from server is not allowed.
2391 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
)) {
2392 struct iovec
*iov
= iov_page
;
2394 iov
->iov_base
= (void __user
*)arg
;
2395 iov
->iov_len
= _IOC_SIZE(cmd
);
2397 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
2402 if (_IOC_DIR(cmd
) & _IOC_READ
) {
2409 inarg
.in_size
= in_size
= iov_length(in_iov
, in_iovs
);
2410 inarg
.out_size
= out_size
= iov_length(out_iov
, out_iovs
);
2413 * Out data can be used either for actual out data or iovs,
2414 * make sure there always is at least one page.
2416 out_size
= max_t(size_t, out_size
, PAGE_SIZE
);
2417 max_pages
= DIV_ROUND_UP(max(in_size
, out_size
), PAGE_SIZE
);
2419 /* make sure there are enough buffer pages and init request with them */
2421 if (max_pages
> FUSE_MAX_PAGES_PER_REQ
)
2423 while (num_pages
< max_pages
) {
2424 pages
[num_pages
] = alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
2425 if (!pages
[num_pages
])
2430 req
= fuse_get_req(fc
, num_pages
);
2436 memcpy(req
->pages
, pages
, sizeof(req
->pages
[0]) * num_pages
);
2437 req
->num_pages
= num_pages
;
2438 fuse_page_descs_length_init(req
, 0, req
->num_pages
);
2440 /* okay, let's send it to the client */
2441 req
->in
.h
.opcode
= FUSE_IOCTL
;
2442 req
->in
.h
.nodeid
= ff
->nodeid
;
2443 req
->in
.numargs
= 1;
2444 req
->in
.args
[0].size
= sizeof(inarg
);
2445 req
->in
.args
[0].value
= &inarg
;
2448 req
->in
.args
[1].size
= in_size
;
2449 req
->in
.argpages
= 1;
2451 err
= fuse_ioctl_copy_user(pages
, in_iov
, in_iovs
, in_size
,
2457 req
->out
.numargs
= 2;
2458 req
->out
.args
[0].size
= sizeof(outarg
);
2459 req
->out
.args
[0].value
= &outarg
;
2460 req
->out
.args
[1].size
= out_size
;
2461 req
->out
.argpages
= 1;
2462 req
->out
.argvar
= 1;
2464 fuse_request_send(fc
, req
);
2465 err
= req
->out
.h
.error
;
2466 transferred
= req
->out
.args
[1].size
;
2467 fuse_put_request(fc
, req
);
2472 /* did it ask for retry? */
2473 if (outarg
.flags
& FUSE_IOCTL_RETRY
) {
2476 /* no retry if in restricted mode */
2478 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
))
2481 in_iovs
= outarg
.in_iovs
;
2482 out_iovs
= outarg
.out_iovs
;
2485 * Make sure things are in boundary, separate checks
2486 * are to protect against overflow.
2489 if (in_iovs
> FUSE_IOCTL_MAX_IOV
||
2490 out_iovs
> FUSE_IOCTL_MAX_IOV
||
2491 in_iovs
+ out_iovs
> FUSE_IOCTL_MAX_IOV
)
2494 vaddr
= kmap_atomic(pages
[0]);
2495 err
= fuse_copy_ioctl_iovec(fc
, iov_page
, vaddr
,
2496 transferred
, in_iovs
+ out_iovs
,
2497 (flags
& FUSE_IOCTL_COMPAT
) != 0);
2498 kunmap_atomic(vaddr
);
2503 out_iov
= in_iov
+ in_iovs
;
2505 err
= fuse_verify_ioctl_iov(in_iov
, in_iovs
);
2509 err
= fuse_verify_ioctl_iov(out_iov
, out_iovs
);
2517 if (transferred
> inarg
.out_size
)
2520 err
= fuse_ioctl_copy_user(pages
, out_iov
, out_iovs
, transferred
, true);
2523 fuse_put_request(fc
, req
);
2524 free_page((unsigned long) iov_page
);
2526 __free_page(pages
[--num_pages
]);
2529 return err
? err
: outarg
.result
;
2531 EXPORT_SYMBOL_GPL(fuse_do_ioctl
);
2533 long fuse_ioctl_common(struct file
*file
, unsigned int cmd
,
2534 unsigned long arg
, unsigned int flags
)
2536 struct inode
*inode
= file_inode(file
);
2537 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2539 if (!fuse_allow_current_process(fc
))
2542 if (is_bad_inode(inode
))
2545 return fuse_do_ioctl(file
, cmd
, arg
, flags
);
2548 static long fuse_file_ioctl(struct file
*file
, unsigned int cmd
,
2551 return fuse_ioctl_common(file
, cmd
, arg
, 0);
2554 static long fuse_file_compat_ioctl(struct file
*file
, unsigned int cmd
,
2557 return fuse_ioctl_common(file
, cmd
, arg
, FUSE_IOCTL_COMPAT
);
2561 * All files which have been polled are linked to RB tree
2562 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2563 * find the matching one.
2565 static struct rb_node
**fuse_find_polled_node(struct fuse_conn
*fc
, u64 kh
,
2566 struct rb_node
**parent_out
)
2568 struct rb_node
**link
= &fc
->polled_files
.rb_node
;
2569 struct rb_node
*last
= NULL
;
2572 struct fuse_file
*ff
;
2575 ff
= rb_entry(last
, struct fuse_file
, polled_node
);
2578 link
= &last
->rb_left
;
2579 else if (kh
> ff
->kh
)
2580 link
= &last
->rb_right
;
2591 * The file is about to be polled. Make sure it's on the polled_files
2592 * RB tree. Note that files once added to the polled_files tree are
2593 * not removed before the file is released. This is because a file
2594 * polled once is likely to be polled again.
2596 static void fuse_register_polled_file(struct fuse_conn
*fc
,
2597 struct fuse_file
*ff
)
2599 spin_lock(&fc
->lock
);
2600 if (RB_EMPTY_NODE(&ff
->polled_node
)) {
2601 struct rb_node
**link
, *parent
;
2603 link
= fuse_find_polled_node(fc
, ff
->kh
, &parent
);
2605 rb_link_node(&ff
->polled_node
, parent
, link
);
2606 rb_insert_color(&ff
->polled_node
, &fc
->polled_files
);
2608 spin_unlock(&fc
->lock
);
2611 unsigned fuse_file_poll(struct file
*file
, poll_table
*wait
)
2613 struct fuse_file
*ff
= file
->private_data
;
2614 struct fuse_conn
*fc
= ff
->fc
;
2615 struct fuse_poll_in inarg
= { .fh
= ff
->fh
, .kh
= ff
->kh
};
2616 struct fuse_poll_out outarg
;
2617 struct fuse_req
*req
;
2621 return DEFAULT_POLLMASK
;
2623 poll_wait(file
, &ff
->poll_wait
, wait
);
2624 inarg
.events
= (__u32
)poll_requested_events(wait
);
2627 * Ask for notification iff there's someone waiting for it.
2628 * The client may ignore the flag and always notify.
2630 if (waitqueue_active(&ff
->poll_wait
)) {
2631 inarg
.flags
|= FUSE_POLL_SCHEDULE_NOTIFY
;
2632 fuse_register_polled_file(fc
, ff
);
2635 req
= fuse_get_req_nopages(fc
);
2639 req
->in
.h
.opcode
= FUSE_POLL
;
2640 req
->in
.h
.nodeid
= ff
->nodeid
;
2641 req
->in
.numargs
= 1;
2642 req
->in
.args
[0].size
= sizeof(inarg
);
2643 req
->in
.args
[0].value
= &inarg
;
2644 req
->out
.numargs
= 1;
2645 req
->out
.args
[0].size
= sizeof(outarg
);
2646 req
->out
.args
[0].value
= &outarg
;
2647 fuse_request_send(fc
, req
);
2648 err
= req
->out
.h
.error
;
2649 fuse_put_request(fc
, req
);
2652 return outarg
.revents
;
2653 if (err
== -ENOSYS
) {
2655 return DEFAULT_POLLMASK
;
2659 EXPORT_SYMBOL_GPL(fuse_file_poll
);
2662 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2663 * wakes up the poll waiters.
2665 int fuse_notify_poll_wakeup(struct fuse_conn
*fc
,
2666 struct fuse_notify_poll_wakeup_out
*outarg
)
2668 u64 kh
= outarg
->kh
;
2669 struct rb_node
**link
;
2671 spin_lock(&fc
->lock
);
2673 link
= fuse_find_polled_node(fc
, kh
, NULL
);
2675 struct fuse_file
*ff
;
2677 ff
= rb_entry(*link
, struct fuse_file
, polled_node
);
2678 wake_up_interruptible_sync(&ff
->poll_wait
);
2681 spin_unlock(&fc
->lock
);
2685 static void fuse_do_truncate(struct file
*file
)
2687 struct inode
*inode
= file
->f_mapping
->host
;
2690 attr
.ia_valid
= ATTR_SIZE
;
2691 attr
.ia_size
= i_size_read(inode
);
2693 attr
.ia_file
= file
;
2694 attr
.ia_valid
|= ATTR_FILE
;
2696 fuse_do_setattr(inode
, &attr
, file
);
2699 static inline loff_t
fuse_round_up(loff_t off
)
2701 return round_up(off
, FUSE_MAX_PAGES_PER_REQ
<< PAGE_SHIFT
);
2705 fuse_direct_IO(int rw
, struct kiocb
*iocb
, const struct iovec
*iov
,
2706 loff_t offset
, unsigned long nr_segs
)
2709 struct file
*file
= iocb
->ki_filp
;
2710 struct fuse_file
*ff
= file
->private_data
;
2711 bool async_dio
= ff
->fc
->async_dio
;
2713 struct inode
*inode
;
2715 size_t count
= iov_length(iov
, nr_segs
);
2716 struct fuse_io_priv
*io
;
2717 bool is_sync
= is_sync_kiocb(iocb
);
2720 inode
= file
->f_mapping
->host
;
2721 i_size
= i_size_read(inode
);
2723 /* optimization for short read */
2724 if (async_dio
&& rw
!= WRITE
&& offset
+ count
> i_size
) {
2725 if (offset
>= i_size
)
2727 count
= min_t(loff_t
, count
, fuse_round_up(i_size
- offset
));
2730 io
= kmalloc(sizeof(struct fuse_io_priv
), GFP_KERNEL
);
2733 spin_lock_init(&io
->lock
);
2737 io
->offset
= offset
;
2738 io
->write
= (rw
== WRITE
);
2742 * By default, we want to optimize all I/Os with async request
2743 * submission to the client filesystem if supported.
2745 io
->async
= async_dio
;
2749 * We cannot asynchronously extend the size of a file. We have no method
2750 * to wait on real async I/O requests, so we must submit this request
2753 if (!is_sync
&& (offset
+ count
> i_size
) && rw
== WRITE
)
2757 ret
= __fuse_direct_write(io
, iov
, nr_segs
, &pos
);
2759 ret
= __fuse_direct_read(io
, iov
, nr_segs
, &pos
, count
);
2762 fuse_aio_complete(io
, ret
< 0 ? ret
: 0, -1);
2764 /* we have a non-extending, async request, so return */
2766 return -EIOCBQUEUED
;
2768 ret
= wait_on_sync_kiocb(iocb
);
2775 fuse_write_update_size(inode
, pos
);
2776 else if (ret
< 0 && offset
+ count
> i_size
)
2777 fuse_do_truncate(file
);
2783 static long fuse_file_fallocate(struct file
*file
, int mode
, loff_t offset
,
2786 struct fuse_file
*ff
= file
->private_data
;
2787 struct inode
*inode
= file
->f_inode
;
2788 struct fuse_inode
*fi
= get_fuse_inode(inode
);
2789 struct fuse_conn
*fc
= ff
->fc
;
2790 struct fuse_req
*req
;
2791 struct fuse_fallocate_in inarg
= {
2798 bool lock_inode
= !(mode
& FALLOC_FL_KEEP_SIZE
) ||
2799 (mode
& FALLOC_FL_PUNCH_HOLE
);
2801 if (fc
->no_fallocate
)
2805 mutex_lock(&inode
->i_mutex
);
2806 if (mode
& FALLOC_FL_PUNCH_HOLE
) {
2807 loff_t endbyte
= offset
+ length
- 1;
2808 err
= filemap_write_and_wait_range(inode
->i_mapping
,
2813 fuse_sync_writes(inode
);
2817 if (!(mode
& FALLOC_FL_KEEP_SIZE
))
2818 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
2820 req
= fuse_get_req_nopages(fc
);
2826 req
->in
.h
.opcode
= FUSE_FALLOCATE
;
2827 req
->in
.h
.nodeid
= ff
->nodeid
;
2828 req
->in
.numargs
= 1;
2829 req
->in
.args
[0].size
= sizeof(inarg
);
2830 req
->in
.args
[0].value
= &inarg
;
2831 fuse_request_send(fc
, req
);
2832 err
= req
->out
.h
.error
;
2833 if (err
== -ENOSYS
) {
2834 fc
->no_fallocate
= 1;
2837 fuse_put_request(fc
, req
);
2842 /* we could have extended the file */
2843 if (!(mode
& FALLOC_FL_KEEP_SIZE
))
2844 fuse_write_update_size(inode
, offset
+ length
);
2846 if (mode
& FALLOC_FL_PUNCH_HOLE
)
2847 truncate_pagecache_range(inode
, offset
, offset
+ length
- 1);
2849 fuse_invalidate_attr(inode
);
2852 if (!(mode
& FALLOC_FL_KEEP_SIZE
))
2853 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
2856 mutex_unlock(&inode
->i_mutex
);
2861 static const struct file_operations fuse_file_operations
= {
2862 .llseek
= fuse_file_llseek
,
2863 .read
= do_sync_read
,
2864 .aio_read
= fuse_file_aio_read
,
2865 .write
= do_sync_write
,
2866 .aio_write
= fuse_file_aio_write
,
2867 .mmap
= fuse_file_mmap
,
2869 .flush
= fuse_flush
,
2870 .release
= fuse_release
,
2871 .fsync
= fuse_fsync
,
2872 .lock
= fuse_file_lock
,
2873 .flock
= fuse_file_flock
,
2874 .splice_read
= generic_file_splice_read
,
2875 .unlocked_ioctl
= fuse_file_ioctl
,
2876 .compat_ioctl
= fuse_file_compat_ioctl
,
2877 .poll
= fuse_file_poll
,
2878 .fallocate
= fuse_file_fallocate
,
2881 static const struct file_operations fuse_direct_io_file_operations
= {
2882 .llseek
= fuse_file_llseek
,
2883 .read
= fuse_direct_read
,
2884 .write
= fuse_direct_write
,
2885 .mmap
= fuse_direct_mmap
,
2887 .flush
= fuse_flush
,
2888 .release
= fuse_release
,
2889 .fsync
= fuse_fsync
,
2890 .lock
= fuse_file_lock
,
2891 .flock
= fuse_file_flock
,
2892 .unlocked_ioctl
= fuse_file_ioctl
,
2893 .compat_ioctl
= fuse_file_compat_ioctl
,
2894 .poll
= fuse_file_poll
,
2895 .fallocate
= fuse_file_fallocate
,
2896 /* no splice_read */
2899 static const struct address_space_operations fuse_file_aops
= {
2900 .readpage
= fuse_readpage
,
2901 .writepage
= fuse_writepage
,
2902 .launder_page
= fuse_launder_page
,
2903 .readpages
= fuse_readpages
,
2904 .set_page_dirty
= __set_page_dirty_nobuffers
,
2906 .direct_IO
= fuse_direct_IO
,
2909 void fuse_init_file_inode(struct inode
*inode
)
2911 inode
->i_fop
= &fuse_file_operations
;
2912 inode
->i_data
.a_ops
= &fuse_file_aops
;