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/slab.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/sched/signal.h>
16 #include <linux/module.h>
17 #include <linux/compat.h>
18 #include <linux/swap.h>
19 #include <linux/falloc.h>
20 #include <linux/uio.h>
23 static struct page
**fuse_pages_alloc(unsigned int npages
, gfp_t flags
,
24 struct fuse_page_desc
**desc
)
28 pages
= kzalloc(npages
* (sizeof(struct page
*) +
29 sizeof(struct fuse_page_desc
)), flags
);
30 *desc
= (void *) (pages
+ npages
);
35 static int fuse_send_open(struct fuse_mount
*fm
, u64 nodeid
, struct file
*file
,
36 int opcode
, struct fuse_open_out
*outargp
)
38 struct fuse_open_in inarg
;
41 memset(&inarg
, 0, sizeof(inarg
));
42 inarg
.flags
= file
->f_flags
& ~(O_CREAT
| O_EXCL
| O_NOCTTY
);
43 if (!fm
->fc
->atomic_o_trunc
)
44 inarg
.flags
&= ~O_TRUNC
;
48 args
.in_args
[0].size
= sizeof(inarg
);
49 args
.in_args
[0].value
= &inarg
;
51 args
.out_args
[0].size
= sizeof(*outargp
);
52 args
.out_args
[0].value
= outargp
;
54 return fuse_simple_request(fm
, &args
);
57 struct fuse_release_args
{
58 struct fuse_args args
;
59 struct fuse_release_in inarg
;
63 struct fuse_file
*fuse_file_alloc(struct fuse_mount
*fm
)
67 ff
= kzalloc(sizeof(struct fuse_file
), GFP_KERNEL_ACCOUNT
);
72 ff
->release_args
= kzalloc(sizeof(*ff
->release_args
),
74 if (!ff
->release_args
) {
79 INIT_LIST_HEAD(&ff
->write_entry
);
80 mutex_init(&ff
->readdir
.lock
);
81 refcount_set(&ff
->count
, 1);
82 RB_CLEAR_NODE(&ff
->polled_node
);
83 init_waitqueue_head(&ff
->poll_wait
);
85 ff
->kh
= atomic64_inc_return(&fm
->fc
->khctr
);
90 void fuse_file_free(struct fuse_file
*ff
)
92 kfree(ff
->release_args
);
93 mutex_destroy(&ff
->readdir
.lock
);
97 static struct fuse_file
*fuse_file_get(struct fuse_file
*ff
)
99 refcount_inc(&ff
->count
);
103 static void fuse_release_end(struct fuse_mount
*fm
, struct fuse_args
*args
,
106 struct fuse_release_args
*ra
= container_of(args
, typeof(*ra
), args
);
112 static void fuse_file_put(struct fuse_file
*ff
, bool sync
, bool isdir
)
114 if (refcount_dec_and_test(&ff
->count
)) {
115 struct fuse_args
*args
= &ff
->release_args
->args
;
117 if (isdir
? ff
->fm
->fc
->no_opendir
: ff
->fm
->fc
->no_open
) {
118 /* Do nothing when client does not implement 'open' */
119 fuse_release_end(ff
->fm
, args
, 0);
121 fuse_simple_request(ff
->fm
, args
);
122 fuse_release_end(ff
->fm
, args
, 0);
124 args
->end
= fuse_release_end
;
125 if (fuse_simple_background(ff
->fm
, args
,
126 GFP_KERNEL
| __GFP_NOFAIL
))
127 fuse_release_end(ff
->fm
, args
, -ENOTCONN
);
133 int fuse_do_open(struct fuse_mount
*fm
, u64 nodeid
, struct file
*file
,
136 struct fuse_conn
*fc
= fm
->fc
;
137 struct fuse_file
*ff
;
138 int opcode
= isdir
? FUSE_OPENDIR
: FUSE_OPEN
;
140 ff
= fuse_file_alloc(fm
);
145 /* Default for no-open */
146 ff
->open_flags
= FOPEN_KEEP_CACHE
| (isdir
? FOPEN_CACHE_DIR
: 0);
147 if (isdir
? !fc
->no_opendir
: !fc
->no_open
) {
148 struct fuse_open_out outarg
;
151 err
= fuse_send_open(fm
, nodeid
, file
, opcode
, &outarg
);
154 ff
->open_flags
= outarg
.open_flags
;
156 } else if (err
!= -ENOSYS
) {
168 ff
->open_flags
&= ~FOPEN_DIRECT_IO
;
171 file
->private_data
= ff
;
175 EXPORT_SYMBOL_GPL(fuse_do_open
);
177 static void fuse_link_write_file(struct file
*file
)
179 struct inode
*inode
= file_inode(file
);
180 struct fuse_inode
*fi
= get_fuse_inode(inode
);
181 struct fuse_file
*ff
= file
->private_data
;
183 * file may be written through mmap, so chain it onto the
184 * inodes's write_file list
186 spin_lock(&fi
->lock
);
187 if (list_empty(&ff
->write_entry
))
188 list_add(&ff
->write_entry
, &fi
->write_files
);
189 spin_unlock(&fi
->lock
);
192 void fuse_finish_open(struct inode
*inode
, struct file
*file
)
194 struct fuse_file
*ff
= file
->private_data
;
195 struct fuse_conn
*fc
= get_fuse_conn(inode
);
197 if (!(ff
->open_flags
& FOPEN_KEEP_CACHE
))
198 invalidate_inode_pages2(inode
->i_mapping
);
199 if (ff
->open_flags
& FOPEN_STREAM
)
200 stream_open(inode
, file
);
201 else if (ff
->open_flags
& FOPEN_NONSEEKABLE
)
202 nonseekable_open(inode
, file
);
203 if (fc
->atomic_o_trunc
&& (file
->f_flags
& O_TRUNC
)) {
204 struct fuse_inode
*fi
= get_fuse_inode(inode
);
206 spin_lock(&fi
->lock
);
207 fi
->attr_version
= atomic64_inc_return(&fc
->attr_version
);
208 i_size_write(inode
, 0);
209 spin_unlock(&fi
->lock
);
210 fuse_invalidate_attr(inode
);
211 if (fc
->writeback_cache
)
212 file_update_time(file
);
214 if ((file
->f_mode
& FMODE_WRITE
) && fc
->writeback_cache
)
215 fuse_link_write_file(file
);
218 int fuse_open_common(struct inode
*inode
, struct file
*file
, bool isdir
)
220 struct fuse_mount
*fm
= get_fuse_mount(inode
);
221 struct fuse_conn
*fc
= fm
->fc
;
223 bool is_wb_truncate
= (file
->f_flags
& O_TRUNC
) &&
224 fc
->atomic_o_trunc
&&
226 bool dax_truncate
= (file
->f_flags
& O_TRUNC
) &&
227 fc
->atomic_o_trunc
&& FUSE_IS_DAX(inode
);
229 if (fuse_is_bad(inode
))
232 err
= generic_file_open(inode
, file
);
236 if (is_wb_truncate
|| dax_truncate
) {
238 fuse_set_nowrite(inode
);
242 down_write(&get_fuse_inode(inode
)->i_mmap_sem
);
243 err
= fuse_dax_break_layouts(inode
, 0, 0);
248 err
= fuse_do_open(fm
, get_node_id(inode
), file
, isdir
);
250 fuse_finish_open(inode
, file
);
254 up_write(&get_fuse_inode(inode
)->i_mmap_sem
);
256 if (is_wb_truncate
| dax_truncate
) {
257 fuse_release_nowrite(inode
);
264 static void fuse_prepare_release(struct fuse_inode
*fi
, struct fuse_file
*ff
,
265 int flags
, int opcode
)
267 struct fuse_conn
*fc
= ff
->fm
->fc
;
268 struct fuse_release_args
*ra
= ff
->release_args
;
270 /* Inode is NULL on error path of fuse_create_open() */
272 spin_lock(&fi
->lock
);
273 list_del(&ff
->write_entry
);
274 spin_unlock(&fi
->lock
);
276 spin_lock(&fc
->lock
);
277 if (!RB_EMPTY_NODE(&ff
->polled_node
))
278 rb_erase(&ff
->polled_node
, &fc
->polled_files
);
279 spin_unlock(&fc
->lock
);
281 wake_up_interruptible_all(&ff
->poll_wait
);
283 ra
->inarg
.fh
= ff
->fh
;
284 ra
->inarg
.flags
= flags
;
285 ra
->args
.in_numargs
= 1;
286 ra
->args
.in_args
[0].size
= sizeof(struct fuse_release_in
);
287 ra
->args
.in_args
[0].value
= &ra
->inarg
;
288 ra
->args
.opcode
= opcode
;
289 ra
->args
.nodeid
= ff
->nodeid
;
290 ra
->args
.force
= true;
291 ra
->args
.nocreds
= true;
294 void fuse_release_common(struct file
*file
, bool isdir
)
296 struct fuse_inode
*fi
= get_fuse_inode(file_inode(file
));
297 struct fuse_file
*ff
= file
->private_data
;
298 struct fuse_release_args
*ra
= ff
->release_args
;
299 int opcode
= isdir
? FUSE_RELEASEDIR
: FUSE_RELEASE
;
301 fuse_prepare_release(fi
, ff
, file
->f_flags
, opcode
);
304 ra
->inarg
.release_flags
|= FUSE_RELEASE_FLOCK_UNLOCK
;
305 ra
->inarg
.lock_owner
= fuse_lock_owner_id(ff
->fm
->fc
,
308 /* Hold inode until release is finished */
309 ra
->inode
= igrab(file_inode(file
));
312 * Normally this will send the RELEASE request, however if
313 * some asynchronous READ or WRITE requests are outstanding,
314 * the sending will be delayed.
316 * Make the release synchronous if this is a fuseblk mount,
317 * synchronous RELEASE is allowed (and desirable) in this case
318 * because the server can be trusted not to screw up.
320 fuse_file_put(ff
, ff
->fm
->fc
->destroy
, isdir
);
323 static int fuse_open(struct inode
*inode
, struct file
*file
)
325 return fuse_open_common(inode
, file
, false);
328 static int fuse_release(struct inode
*inode
, struct file
*file
)
330 struct fuse_conn
*fc
= get_fuse_conn(inode
);
332 /* see fuse_vma_close() for !writeback_cache case */
333 if (fc
->writeback_cache
)
334 write_inode_now(inode
, 1);
336 fuse_release_common(file
, false);
338 /* return value is ignored by VFS */
342 void fuse_sync_release(struct fuse_inode
*fi
, struct fuse_file
*ff
, int flags
)
344 WARN_ON(refcount_read(&ff
->count
) > 1);
345 fuse_prepare_release(fi
, ff
, flags
, FUSE_RELEASE
);
347 * iput(NULL) is a no-op and since the refcount is 1 and everything's
348 * synchronous, we are fine with not doing igrab() here"
350 fuse_file_put(ff
, true, false);
352 EXPORT_SYMBOL_GPL(fuse_sync_release
);
355 * Scramble the ID space with XTEA, so that the value of the files_struct
356 * pointer is not exposed to userspace.
358 u64
fuse_lock_owner_id(struct fuse_conn
*fc
, fl_owner_t id
)
360 u32
*k
= fc
->scramble_key
;
361 u64 v
= (unsigned long) id
;
367 for (i
= 0; i
< 32; i
++) {
368 v0
+= ((v1
<< 4 ^ v1
>> 5) + v1
) ^ (sum
+ k
[sum
& 3]);
370 v1
+= ((v0
<< 4 ^ v0
>> 5) + v0
) ^ (sum
+ k
[sum
>>11 & 3]);
373 return (u64
) v0
+ ((u64
) v1
<< 32);
376 struct fuse_writepage_args
{
377 struct fuse_io_args ia
;
378 struct rb_node writepages_entry
;
379 struct list_head queue_entry
;
380 struct fuse_writepage_args
*next
;
384 static struct fuse_writepage_args
*fuse_find_writeback(struct fuse_inode
*fi
,
385 pgoff_t idx_from
, pgoff_t idx_to
)
389 n
= fi
->writepages
.rb_node
;
392 struct fuse_writepage_args
*wpa
;
395 wpa
= rb_entry(n
, struct fuse_writepage_args
, writepages_entry
);
396 WARN_ON(get_fuse_inode(wpa
->inode
) != fi
);
397 curr_index
= wpa
->ia
.write
.in
.offset
>> PAGE_SHIFT
;
398 if (idx_from
>= curr_index
+ wpa
->ia
.ap
.num_pages
)
400 else if (idx_to
< curr_index
)
409 * Check if any page in a range is under writeback
411 * This is currently done by walking the list of writepage requests
412 * for the inode, which can be pretty inefficient.
414 static bool fuse_range_is_writeback(struct inode
*inode
, pgoff_t idx_from
,
417 struct fuse_inode
*fi
= get_fuse_inode(inode
);
420 spin_lock(&fi
->lock
);
421 found
= fuse_find_writeback(fi
, idx_from
, idx_to
);
422 spin_unlock(&fi
->lock
);
427 static inline bool fuse_page_is_writeback(struct inode
*inode
, pgoff_t index
)
429 return fuse_range_is_writeback(inode
, index
, index
);
433 * Wait for page writeback to be completed.
435 * Since fuse doesn't rely on the VM writeback tracking, this has to
436 * use some other means.
438 static void fuse_wait_on_page_writeback(struct inode
*inode
, pgoff_t index
)
440 struct fuse_inode
*fi
= get_fuse_inode(inode
);
442 wait_event(fi
->page_waitq
, !fuse_page_is_writeback(inode
, index
));
446 * Wait for all pending writepages on the inode to finish.
448 * This is currently done by blocking further writes with FUSE_NOWRITE
449 * and waiting for all sent writes to complete.
451 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
452 * could conflict with truncation.
454 static void fuse_sync_writes(struct inode
*inode
)
456 fuse_set_nowrite(inode
);
457 fuse_release_nowrite(inode
);
460 static int fuse_flush(struct file
*file
, fl_owner_t id
)
462 struct inode
*inode
= file_inode(file
);
463 struct fuse_mount
*fm
= get_fuse_mount(inode
);
464 struct fuse_file
*ff
= file
->private_data
;
465 struct fuse_flush_in inarg
;
469 if (fuse_is_bad(inode
))
472 err
= write_inode_now(inode
, 1);
477 fuse_sync_writes(inode
);
480 err
= filemap_check_errors(file
->f_mapping
);
485 if (fm
->fc
->no_flush
)
488 memset(&inarg
, 0, sizeof(inarg
));
490 inarg
.lock_owner
= fuse_lock_owner_id(fm
->fc
, id
);
491 args
.opcode
= FUSE_FLUSH
;
492 args
.nodeid
= get_node_id(inode
);
494 args
.in_args
[0].size
= sizeof(inarg
);
495 args
.in_args
[0].value
= &inarg
;
498 err
= fuse_simple_request(fm
, &args
);
499 if (err
== -ENOSYS
) {
500 fm
->fc
->no_flush
= 1;
506 * In memory i_blocks is not maintained by fuse, if writeback cache is
507 * enabled, i_blocks from cached attr may not be accurate.
509 if (!err
&& fm
->fc
->writeback_cache
)
510 fuse_invalidate_attr(inode
);
514 int fuse_fsync_common(struct file
*file
, loff_t start
, loff_t end
,
515 int datasync
, int opcode
)
517 struct inode
*inode
= file
->f_mapping
->host
;
518 struct fuse_mount
*fm
= get_fuse_mount(inode
);
519 struct fuse_file
*ff
= file
->private_data
;
521 struct fuse_fsync_in inarg
;
523 memset(&inarg
, 0, sizeof(inarg
));
525 inarg
.fsync_flags
= datasync
? FUSE_FSYNC_FDATASYNC
: 0;
526 args
.opcode
= opcode
;
527 args
.nodeid
= get_node_id(inode
);
529 args
.in_args
[0].size
= sizeof(inarg
);
530 args
.in_args
[0].value
= &inarg
;
531 return fuse_simple_request(fm
, &args
);
534 static int fuse_fsync(struct file
*file
, loff_t start
, loff_t end
,
537 struct inode
*inode
= file
->f_mapping
->host
;
538 struct fuse_conn
*fc
= get_fuse_conn(inode
);
541 if (fuse_is_bad(inode
))
547 * Start writeback against all dirty pages of the inode, then
548 * wait for all outstanding writes, before sending the FSYNC
551 err
= file_write_and_wait_range(file
, start
, end
);
555 fuse_sync_writes(inode
);
558 * Due to implementation of fuse writeback
559 * file_write_and_wait_range() does not catch errors.
560 * We have to do this directly after fuse_sync_writes()
562 err
= file_check_and_advance_wb_err(file
);
566 err
= sync_inode_metadata(inode
, 1);
573 err
= fuse_fsync_common(file
, start
, end
, datasync
, FUSE_FSYNC
);
574 if (err
== -ENOSYS
) {
584 void fuse_read_args_fill(struct fuse_io_args
*ia
, struct file
*file
, loff_t pos
,
585 size_t count
, int opcode
)
587 struct fuse_file
*ff
= file
->private_data
;
588 struct fuse_args
*args
= &ia
->ap
.args
;
590 ia
->read
.in
.fh
= ff
->fh
;
591 ia
->read
.in
.offset
= pos
;
592 ia
->read
.in
.size
= count
;
593 ia
->read
.in
.flags
= file
->f_flags
;
594 args
->opcode
= opcode
;
595 args
->nodeid
= ff
->nodeid
;
596 args
->in_numargs
= 1;
597 args
->in_args
[0].size
= sizeof(ia
->read
.in
);
598 args
->in_args
[0].value
= &ia
->read
.in
;
599 args
->out_argvar
= true;
600 args
->out_numargs
= 1;
601 args
->out_args
[0].size
= count
;
604 static void fuse_release_user_pages(struct fuse_args_pages
*ap
,
609 for (i
= 0; i
< ap
->num_pages
; i
++) {
611 set_page_dirty_lock(ap
->pages
[i
]);
612 put_page(ap
->pages
[i
]);
616 static void fuse_io_release(struct kref
*kref
)
618 kfree(container_of(kref
, struct fuse_io_priv
, refcnt
));
621 static ssize_t
fuse_get_res_by_io(struct fuse_io_priv
*io
)
626 if (io
->bytes
>= 0 && io
->write
)
629 return io
->bytes
< 0 ? io
->size
: io
->bytes
;
633 * In case of short read, the caller sets 'pos' to the position of
634 * actual end of fuse request in IO request. Otherwise, if bytes_requested
635 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
638 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
639 * both submitted asynchronously. The first of them was ACKed by userspace as
640 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
641 * second request was ACKed as short, e.g. only 1K was read, resulting in
644 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
645 * will be equal to the length of the longest contiguous fragment of
646 * transferred data starting from the beginning of IO request.
648 static void fuse_aio_complete(struct fuse_io_priv
*io
, int err
, ssize_t pos
)
652 spin_lock(&io
->lock
);
654 io
->err
= io
->err
? : err
;
655 else if (pos
>= 0 && (io
->bytes
< 0 || pos
< io
->bytes
))
659 if (!left
&& io
->blocking
)
661 spin_unlock(&io
->lock
);
663 if (!left
&& !io
->blocking
) {
664 ssize_t res
= fuse_get_res_by_io(io
);
667 struct inode
*inode
= file_inode(io
->iocb
->ki_filp
);
668 struct fuse_conn
*fc
= get_fuse_conn(inode
);
669 struct fuse_inode
*fi
= get_fuse_inode(inode
);
671 spin_lock(&fi
->lock
);
672 fi
->attr_version
= atomic64_inc_return(&fc
->attr_version
);
673 spin_unlock(&fi
->lock
);
676 io
->iocb
->ki_complete(io
->iocb
, res
, 0);
679 kref_put(&io
->refcnt
, fuse_io_release
);
682 static struct fuse_io_args
*fuse_io_alloc(struct fuse_io_priv
*io
,
685 struct fuse_io_args
*ia
;
687 ia
= kzalloc(sizeof(*ia
), GFP_KERNEL
);
690 ia
->ap
.pages
= fuse_pages_alloc(npages
, GFP_KERNEL
,
700 static void fuse_io_free(struct fuse_io_args
*ia
)
706 static void fuse_aio_complete_req(struct fuse_mount
*fm
, struct fuse_args
*args
,
709 struct fuse_io_args
*ia
= container_of(args
, typeof(*ia
), ap
.args
);
710 struct fuse_io_priv
*io
= ia
->io
;
713 fuse_release_user_pages(&ia
->ap
, io
->should_dirty
);
717 } else if (io
->write
) {
718 if (ia
->write
.out
.size
> ia
->write
.in
.size
) {
720 } else if (ia
->write
.in
.size
!= ia
->write
.out
.size
) {
721 pos
= ia
->write
.in
.offset
- io
->offset
+
725 u32 outsize
= args
->out_args
[0].size
;
727 if (ia
->read
.in
.size
!= outsize
)
728 pos
= ia
->read
.in
.offset
- io
->offset
+ outsize
;
731 fuse_aio_complete(io
, err
, pos
);
735 static ssize_t
fuse_async_req_send(struct fuse_mount
*fm
,
736 struct fuse_io_args
*ia
, size_t num_bytes
)
739 struct fuse_io_priv
*io
= ia
->io
;
741 spin_lock(&io
->lock
);
742 kref_get(&io
->refcnt
);
743 io
->size
+= num_bytes
;
745 spin_unlock(&io
->lock
);
747 ia
->ap
.args
.end
= fuse_aio_complete_req
;
748 ia
->ap
.args
.may_block
= io
->should_dirty
;
749 err
= fuse_simple_background(fm
, &ia
->ap
.args
, GFP_KERNEL
);
751 fuse_aio_complete_req(fm
, &ia
->ap
.args
, err
);
756 static ssize_t
fuse_send_read(struct fuse_io_args
*ia
, loff_t pos
, size_t count
,
759 struct file
*file
= ia
->io
->iocb
->ki_filp
;
760 struct fuse_file
*ff
= file
->private_data
;
761 struct fuse_mount
*fm
= ff
->fm
;
763 fuse_read_args_fill(ia
, file
, pos
, count
, FUSE_READ
);
765 ia
->read
.in
.read_flags
|= FUSE_READ_LOCKOWNER
;
766 ia
->read
.in
.lock_owner
= fuse_lock_owner_id(fm
->fc
, owner
);
770 return fuse_async_req_send(fm
, ia
, count
);
772 return fuse_simple_request(fm
, &ia
->ap
.args
);
775 static void fuse_read_update_size(struct inode
*inode
, loff_t size
,
778 struct fuse_conn
*fc
= get_fuse_conn(inode
);
779 struct fuse_inode
*fi
= get_fuse_inode(inode
);
781 spin_lock(&fi
->lock
);
782 if (attr_ver
== fi
->attr_version
&& size
< inode
->i_size
&&
783 !test_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
)) {
784 fi
->attr_version
= atomic64_inc_return(&fc
->attr_version
);
785 i_size_write(inode
, size
);
787 spin_unlock(&fi
->lock
);
790 static void fuse_short_read(struct inode
*inode
, u64 attr_ver
, size_t num_read
,
791 struct fuse_args_pages
*ap
)
793 struct fuse_conn
*fc
= get_fuse_conn(inode
);
795 if (fc
->writeback_cache
) {
797 * A hole in a file. Some data after the hole are in page cache,
798 * but have not reached the client fs yet. So, the hole is not
802 int start_idx
= num_read
>> PAGE_SHIFT
;
803 size_t off
= num_read
& (PAGE_SIZE
- 1);
805 for (i
= start_idx
; i
< ap
->num_pages
; i
++) {
806 zero_user_segment(ap
->pages
[i
], off
, PAGE_SIZE
);
810 loff_t pos
= page_offset(ap
->pages
[0]) + num_read
;
811 fuse_read_update_size(inode
, pos
, attr_ver
);
815 static int fuse_do_readpage(struct file
*file
, struct page
*page
)
817 struct inode
*inode
= page
->mapping
->host
;
818 struct fuse_mount
*fm
= get_fuse_mount(inode
);
819 loff_t pos
= page_offset(page
);
820 struct fuse_page_desc desc
= { .length
= PAGE_SIZE
};
821 struct fuse_io_args ia
= {
822 .ap
.args
.page_zeroing
= true,
823 .ap
.args
.out_pages
= true,
832 * Page writeback can extend beyond the lifetime of the
833 * page-cache page, so make sure we read a properly synced
836 fuse_wait_on_page_writeback(inode
, page
->index
);
838 attr_ver
= fuse_get_attr_version(fm
->fc
);
840 /* Don't overflow end offset */
841 if (pos
+ (desc
.length
- 1) == LLONG_MAX
)
844 fuse_read_args_fill(&ia
, file
, pos
, desc
.length
, FUSE_READ
);
845 res
= fuse_simple_request(fm
, &ia
.ap
.args
);
849 * Short read means EOF. If file size is larger, truncate it
851 if (res
< desc
.length
)
852 fuse_short_read(inode
, attr_ver
, res
, &ia
.ap
);
854 SetPageUptodate(page
);
859 static int fuse_readpage(struct file
*file
, struct page
*page
)
861 struct inode
*inode
= page
->mapping
->host
;
865 if (fuse_is_bad(inode
))
868 err
= fuse_do_readpage(file
, page
);
869 fuse_invalidate_atime(inode
);
875 static void fuse_readpages_end(struct fuse_mount
*fm
, struct fuse_args
*args
,
879 struct fuse_io_args
*ia
= container_of(args
, typeof(*ia
), ap
.args
);
880 struct fuse_args_pages
*ap
= &ia
->ap
;
881 size_t count
= ia
->read
.in
.size
;
882 size_t num_read
= args
->out_args
[0].size
;
883 struct address_space
*mapping
= NULL
;
885 for (i
= 0; mapping
== NULL
&& i
< ap
->num_pages
; i
++)
886 mapping
= ap
->pages
[i
]->mapping
;
889 struct inode
*inode
= mapping
->host
;
892 * Short read means EOF. If file size is larger, truncate it
894 if (!err
&& num_read
< count
)
895 fuse_short_read(inode
, ia
->read
.attr_ver
, num_read
, ap
);
897 fuse_invalidate_atime(inode
);
900 for (i
= 0; i
< ap
->num_pages
; i
++) {
901 struct page
*page
= ap
->pages
[i
];
904 SetPageUptodate(page
);
911 fuse_file_put(ia
->ff
, false, false);
916 static void fuse_send_readpages(struct fuse_io_args
*ia
, struct file
*file
)
918 struct fuse_file
*ff
= file
->private_data
;
919 struct fuse_mount
*fm
= ff
->fm
;
920 struct fuse_args_pages
*ap
= &ia
->ap
;
921 loff_t pos
= page_offset(ap
->pages
[0]);
922 size_t count
= ap
->num_pages
<< PAGE_SHIFT
;
926 ap
->args
.out_pages
= true;
927 ap
->args
.page_zeroing
= true;
928 ap
->args
.page_replace
= true;
930 /* Don't overflow end offset */
931 if (pos
+ (count
- 1) == LLONG_MAX
) {
933 ap
->descs
[ap
->num_pages
- 1].length
--;
935 WARN_ON((loff_t
) (pos
+ count
) < 0);
937 fuse_read_args_fill(ia
, file
, pos
, count
, FUSE_READ
);
938 ia
->read
.attr_ver
= fuse_get_attr_version(fm
->fc
);
939 if (fm
->fc
->async_read
) {
940 ia
->ff
= fuse_file_get(ff
);
941 ap
->args
.end
= fuse_readpages_end
;
942 err
= fuse_simple_background(fm
, &ap
->args
, GFP_KERNEL
);
946 res
= fuse_simple_request(fm
, &ap
->args
);
947 err
= res
< 0 ? res
: 0;
949 fuse_readpages_end(fm
, &ap
->args
, err
);
952 static void fuse_readahead(struct readahead_control
*rac
)
954 struct inode
*inode
= rac
->mapping
->host
;
955 struct fuse_conn
*fc
= get_fuse_conn(inode
);
956 unsigned int i
, max_pages
, nr_pages
= 0;
958 if (fuse_is_bad(inode
))
961 max_pages
= min_t(unsigned int, fc
->max_pages
,
962 fc
->max_read
/ PAGE_SIZE
);
965 struct fuse_io_args
*ia
;
966 struct fuse_args_pages
*ap
;
968 nr_pages
= readahead_count(rac
) - nr_pages
;
969 if (nr_pages
> max_pages
)
970 nr_pages
= max_pages
;
973 ia
= fuse_io_alloc(NULL
, nr_pages
);
977 nr_pages
= __readahead_batch(rac
, ap
->pages
, nr_pages
);
978 for (i
= 0; i
< nr_pages
; i
++) {
979 fuse_wait_on_page_writeback(inode
,
980 readahead_index(rac
) + i
);
981 ap
->descs
[i
].length
= PAGE_SIZE
;
983 ap
->num_pages
= nr_pages
;
984 fuse_send_readpages(ia
, rac
->file
);
988 static ssize_t
fuse_cache_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
990 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
991 struct fuse_conn
*fc
= get_fuse_conn(inode
);
994 * In auto invalidate mode, always update attributes on read.
995 * Otherwise, only update if we attempt to read past EOF (to ensure
996 * i_size is up to date).
998 if (fc
->auto_inval_data
||
999 (iocb
->ki_pos
+ iov_iter_count(to
) > i_size_read(inode
))) {
1001 err
= fuse_update_attributes(inode
, iocb
->ki_filp
);
1006 return generic_file_read_iter(iocb
, to
);
1009 static void fuse_write_args_fill(struct fuse_io_args
*ia
, struct fuse_file
*ff
,
1010 loff_t pos
, size_t count
)
1012 struct fuse_args
*args
= &ia
->ap
.args
;
1014 ia
->write
.in
.fh
= ff
->fh
;
1015 ia
->write
.in
.offset
= pos
;
1016 ia
->write
.in
.size
= count
;
1017 args
->opcode
= FUSE_WRITE
;
1018 args
->nodeid
= ff
->nodeid
;
1019 args
->in_numargs
= 2;
1020 if (ff
->fm
->fc
->minor
< 9)
1021 args
->in_args
[0].size
= FUSE_COMPAT_WRITE_IN_SIZE
;
1023 args
->in_args
[0].size
= sizeof(ia
->write
.in
);
1024 args
->in_args
[0].value
= &ia
->write
.in
;
1025 args
->in_args
[1].size
= count
;
1026 args
->out_numargs
= 1;
1027 args
->out_args
[0].size
= sizeof(ia
->write
.out
);
1028 args
->out_args
[0].value
= &ia
->write
.out
;
1031 static unsigned int fuse_write_flags(struct kiocb
*iocb
)
1033 unsigned int flags
= iocb
->ki_filp
->f_flags
;
1035 if (iocb
->ki_flags
& IOCB_DSYNC
)
1037 if (iocb
->ki_flags
& IOCB_SYNC
)
1043 static ssize_t
fuse_send_write(struct fuse_io_args
*ia
, loff_t pos
,
1044 size_t count
, fl_owner_t owner
)
1046 struct kiocb
*iocb
= ia
->io
->iocb
;
1047 struct file
*file
= iocb
->ki_filp
;
1048 struct fuse_file
*ff
= file
->private_data
;
1049 struct fuse_mount
*fm
= ff
->fm
;
1050 struct fuse_write_in
*inarg
= &ia
->write
.in
;
1053 fuse_write_args_fill(ia
, ff
, pos
, count
);
1054 inarg
->flags
= fuse_write_flags(iocb
);
1055 if (owner
!= NULL
) {
1056 inarg
->write_flags
|= FUSE_WRITE_LOCKOWNER
;
1057 inarg
->lock_owner
= fuse_lock_owner_id(fm
->fc
, owner
);
1061 return fuse_async_req_send(fm
, ia
, count
);
1063 err
= fuse_simple_request(fm
, &ia
->ap
.args
);
1064 if (!err
&& ia
->write
.out
.size
> count
)
1067 return err
?: ia
->write
.out
.size
;
1070 bool fuse_write_update_size(struct inode
*inode
, loff_t pos
)
1072 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1073 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1076 spin_lock(&fi
->lock
);
1077 fi
->attr_version
= atomic64_inc_return(&fc
->attr_version
);
1078 if (pos
> inode
->i_size
) {
1079 i_size_write(inode
, pos
);
1082 spin_unlock(&fi
->lock
);
1087 static ssize_t
fuse_send_write_pages(struct fuse_io_args
*ia
,
1088 struct kiocb
*iocb
, struct inode
*inode
,
1089 loff_t pos
, size_t count
)
1091 struct fuse_args_pages
*ap
= &ia
->ap
;
1092 struct file
*file
= iocb
->ki_filp
;
1093 struct fuse_file
*ff
= file
->private_data
;
1094 struct fuse_mount
*fm
= ff
->fm
;
1095 unsigned int offset
, i
;
1098 for (i
= 0; i
< ap
->num_pages
; i
++)
1099 fuse_wait_on_page_writeback(inode
, ap
->pages
[i
]->index
);
1101 fuse_write_args_fill(ia
, ff
, pos
, count
);
1102 ia
->write
.in
.flags
= fuse_write_flags(iocb
);
1104 err
= fuse_simple_request(fm
, &ap
->args
);
1105 if (!err
&& ia
->write
.out
.size
> count
)
1108 offset
= ap
->descs
[0].offset
;
1109 count
= ia
->write
.out
.size
;
1110 for (i
= 0; i
< ap
->num_pages
; i
++) {
1111 struct page
*page
= ap
->pages
[i
];
1113 if (!err
&& !offset
&& count
>= PAGE_SIZE
)
1114 SetPageUptodate(page
);
1116 if (count
> PAGE_SIZE
- offset
)
1117 count
-= PAGE_SIZE
- offset
;
1129 static ssize_t
fuse_fill_write_pages(struct fuse_args_pages
*ap
,
1130 struct address_space
*mapping
,
1131 struct iov_iter
*ii
, loff_t pos
,
1132 unsigned int max_pages
)
1134 struct fuse_conn
*fc
= get_fuse_conn(mapping
->host
);
1135 unsigned offset
= pos
& (PAGE_SIZE
- 1);
1139 ap
->args
.in_pages
= true;
1140 ap
->descs
[0].offset
= offset
;
1145 pgoff_t index
= pos
>> PAGE_SHIFT
;
1146 size_t bytes
= min_t(size_t, PAGE_SIZE
- offset
,
1147 iov_iter_count(ii
));
1149 bytes
= min_t(size_t, bytes
, fc
->max_write
- count
);
1153 if (iov_iter_fault_in_readable(ii
, bytes
))
1157 page
= grab_cache_page_write_begin(mapping
, index
, 0);
1161 if (mapping_writably_mapped(mapping
))
1162 flush_dcache_page(page
);
1164 tmp
= iov_iter_copy_from_user_atomic(page
, ii
, offset
, bytes
);
1165 flush_dcache_page(page
);
1167 iov_iter_advance(ii
, tmp
);
1171 bytes
= min(bytes
, iov_iter_single_seg_count(ii
));
1176 ap
->pages
[ap
->num_pages
] = page
;
1177 ap
->descs
[ap
->num_pages
].length
= tmp
;
1183 if (offset
== PAGE_SIZE
)
1186 if (!fc
->big_writes
)
1188 } while (iov_iter_count(ii
) && count
< fc
->max_write
&&
1189 ap
->num_pages
< max_pages
&& offset
== 0);
1191 return count
> 0 ? count
: err
;
1194 static inline unsigned int fuse_wr_pages(loff_t pos
, size_t len
,
1195 unsigned int max_pages
)
1197 return min_t(unsigned int,
1198 ((pos
+ len
- 1) >> PAGE_SHIFT
) -
1199 (pos
>> PAGE_SHIFT
) + 1,
1203 static ssize_t
fuse_perform_write(struct kiocb
*iocb
,
1204 struct address_space
*mapping
,
1205 struct iov_iter
*ii
, loff_t pos
)
1207 struct inode
*inode
= mapping
->host
;
1208 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1209 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1213 if (inode
->i_size
< pos
+ iov_iter_count(ii
))
1214 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1218 struct fuse_io_args ia
= {};
1219 struct fuse_args_pages
*ap
= &ia
.ap
;
1220 unsigned int nr_pages
= fuse_wr_pages(pos
, iov_iter_count(ii
),
1223 ap
->pages
= fuse_pages_alloc(nr_pages
, GFP_KERNEL
, &ap
->descs
);
1229 count
= fuse_fill_write_pages(ap
, mapping
, ii
, pos
, nr_pages
);
1233 err
= fuse_send_write_pages(&ia
, iocb
, inode
,
1236 size_t num_written
= ia
.write
.out
.size
;
1241 /* break out of the loop on short write */
1242 if (num_written
!= count
)
1247 } while (!err
&& iov_iter_count(ii
));
1250 fuse_write_update_size(inode
, pos
);
1252 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1253 fuse_invalidate_attr(inode
);
1255 return res
> 0 ? res
: err
;
1258 static ssize_t
fuse_cache_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
1260 struct file
*file
= iocb
->ki_filp
;
1261 struct address_space
*mapping
= file
->f_mapping
;
1262 ssize_t written
= 0;
1263 ssize_t written_buffered
= 0;
1264 struct inode
*inode
= mapping
->host
;
1268 if (get_fuse_conn(inode
)->writeback_cache
) {
1269 /* Update size (EOF optimization) and mode (SUID clearing) */
1270 err
= fuse_update_attributes(mapping
->host
, file
);
1274 return generic_file_write_iter(iocb
, from
);
1279 /* We can write back this queue in page reclaim */
1280 current
->backing_dev_info
= inode_to_bdi(inode
);
1282 err
= generic_write_checks(iocb
, from
);
1286 err
= file_remove_privs(file
);
1290 err
= file_update_time(file
);
1294 if (iocb
->ki_flags
& IOCB_DIRECT
) {
1295 loff_t pos
= iocb
->ki_pos
;
1296 written
= generic_file_direct_write(iocb
, from
);
1297 if (written
< 0 || !iov_iter_count(from
))
1302 written_buffered
= fuse_perform_write(iocb
, mapping
, from
, pos
);
1303 if (written_buffered
< 0) {
1304 err
= written_buffered
;
1307 endbyte
= pos
+ written_buffered
- 1;
1309 err
= filemap_write_and_wait_range(file
->f_mapping
, pos
,
1314 invalidate_mapping_pages(file
->f_mapping
,
1316 endbyte
>> PAGE_SHIFT
);
1318 written
+= written_buffered
;
1319 iocb
->ki_pos
= pos
+ written_buffered
;
1321 written
= fuse_perform_write(iocb
, mapping
, from
, iocb
->ki_pos
);
1323 iocb
->ki_pos
+= written
;
1326 current
->backing_dev_info
= NULL
;
1327 inode_unlock(inode
);
1329 written
= generic_write_sync(iocb
, written
);
1331 return written
? written
: err
;
1334 static inline void fuse_page_descs_length_init(struct fuse_page_desc
*descs
,
1336 unsigned int nr_pages
)
1340 for (i
= index
; i
< index
+ nr_pages
; i
++)
1341 descs
[i
].length
= PAGE_SIZE
- descs
[i
].offset
;
1344 static inline unsigned long fuse_get_user_addr(const struct iov_iter
*ii
)
1346 return (unsigned long)ii
->iov
->iov_base
+ ii
->iov_offset
;
1349 static inline size_t fuse_get_frag_size(const struct iov_iter
*ii
,
1352 return min(iov_iter_single_seg_count(ii
), max_size
);
1355 static int fuse_get_user_pages(struct fuse_args_pages
*ap
, struct iov_iter
*ii
,
1356 size_t *nbytesp
, int write
,
1357 unsigned int max_pages
)
1359 size_t nbytes
= 0; /* # bytes already packed in req */
1362 /* Special case for kernel I/O: can copy directly into the buffer */
1363 if (iov_iter_is_kvec(ii
)) {
1364 unsigned long user_addr
= fuse_get_user_addr(ii
);
1365 size_t frag_size
= fuse_get_frag_size(ii
, *nbytesp
);
1368 ap
->args
.in_args
[1].value
= (void *) user_addr
;
1370 ap
->args
.out_args
[0].value
= (void *) user_addr
;
1372 iov_iter_advance(ii
, frag_size
);
1373 *nbytesp
= frag_size
;
1377 while (nbytes
< *nbytesp
&& ap
->num_pages
< max_pages
) {
1380 ret
= iov_iter_get_pages(ii
, &ap
->pages
[ap
->num_pages
],
1382 max_pages
- ap
->num_pages
,
1387 iov_iter_advance(ii
, ret
);
1391 npages
= (ret
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
1393 ap
->descs
[ap
->num_pages
].offset
= start
;
1394 fuse_page_descs_length_init(ap
->descs
, ap
->num_pages
, npages
);
1396 ap
->num_pages
+= npages
;
1397 ap
->descs
[ap
->num_pages
- 1].length
-=
1398 (PAGE_SIZE
- ret
) & (PAGE_SIZE
- 1);
1402 ap
->args
.in_pages
= true;
1404 ap
->args
.out_pages
= true;
1408 return ret
< 0 ? ret
: 0;
1411 ssize_t
fuse_direct_io(struct fuse_io_priv
*io
, struct iov_iter
*iter
,
1412 loff_t
*ppos
, int flags
)
1414 int write
= flags
& FUSE_DIO_WRITE
;
1415 int cuse
= flags
& FUSE_DIO_CUSE
;
1416 struct file
*file
= io
->iocb
->ki_filp
;
1417 struct inode
*inode
= file
->f_mapping
->host
;
1418 struct fuse_file
*ff
= file
->private_data
;
1419 struct fuse_conn
*fc
= ff
->fm
->fc
;
1420 size_t nmax
= write
? fc
->max_write
: fc
->max_read
;
1422 size_t count
= iov_iter_count(iter
);
1423 pgoff_t idx_from
= pos
>> PAGE_SHIFT
;
1424 pgoff_t idx_to
= (pos
+ count
- 1) >> PAGE_SHIFT
;
1427 struct fuse_io_args
*ia
;
1428 unsigned int max_pages
;
1430 max_pages
= iov_iter_npages(iter
, fc
->max_pages
);
1431 ia
= fuse_io_alloc(io
, max_pages
);
1436 if (!cuse
&& fuse_range_is_writeback(inode
, idx_from
, idx_to
)) {
1439 fuse_sync_writes(inode
);
1441 inode_unlock(inode
);
1444 io
->should_dirty
= !write
&& iter_is_iovec(iter
);
1447 fl_owner_t owner
= current
->files
;
1448 size_t nbytes
= min(count
, nmax
);
1450 err
= fuse_get_user_pages(&ia
->ap
, iter
, &nbytes
, write
,
1456 if (!capable(CAP_FSETID
))
1457 ia
->write
.in
.write_flags
|= FUSE_WRITE_KILL_PRIV
;
1459 nres
= fuse_send_write(ia
, pos
, nbytes
, owner
);
1461 nres
= fuse_send_read(ia
, pos
, nbytes
, owner
);
1464 if (!io
->async
|| nres
< 0) {
1465 fuse_release_user_pages(&ia
->ap
, io
->should_dirty
);
1470 iov_iter_revert(iter
, nbytes
);
1474 WARN_ON(nres
> nbytes
);
1479 if (nres
!= nbytes
) {
1480 iov_iter_revert(iter
, nbytes
- nres
);
1484 max_pages
= iov_iter_npages(iter
, fc
->max_pages
);
1485 ia
= fuse_io_alloc(io
, max_pages
);
1495 return res
> 0 ? res
: err
;
1497 EXPORT_SYMBOL_GPL(fuse_direct_io
);
1499 static ssize_t
__fuse_direct_read(struct fuse_io_priv
*io
,
1500 struct iov_iter
*iter
,
1504 struct inode
*inode
= file_inode(io
->iocb
->ki_filp
);
1506 res
= fuse_direct_io(io
, iter
, ppos
, 0);
1508 fuse_invalidate_atime(inode
);
1513 static ssize_t
fuse_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
);
1515 static ssize_t
fuse_direct_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
1519 if (!is_sync_kiocb(iocb
) && iocb
->ki_flags
& IOCB_DIRECT
) {
1520 res
= fuse_direct_IO(iocb
, to
);
1522 struct fuse_io_priv io
= FUSE_IO_PRIV_SYNC(iocb
);
1524 res
= __fuse_direct_read(&io
, to
, &iocb
->ki_pos
);
1530 static ssize_t
fuse_direct_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
1532 struct inode
*inode
= file_inode(iocb
->ki_filp
);
1533 struct fuse_io_priv io
= FUSE_IO_PRIV_SYNC(iocb
);
1536 /* Don't allow parallel writes to the same file */
1538 res
= generic_write_checks(iocb
, from
);
1540 if (!is_sync_kiocb(iocb
) && iocb
->ki_flags
& IOCB_DIRECT
) {
1541 res
= fuse_direct_IO(iocb
, from
);
1543 res
= fuse_direct_io(&io
, from
, &iocb
->ki_pos
,
1547 fuse_invalidate_attr(inode
);
1549 fuse_write_update_size(inode
, iocb
->ki_pos
);
1550 inode_unlock(inode
);
1555 static ssize_t
fuse_file_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
1557 struct file
*file
= iocb
->ki_filp
;
1558 struct fuse_file
*ff
= file
->private_data
;
1559 struct inode
*inode
= file_inode(file
);
1561 if (fuse_is_bad(inode
))
1564 if (FUSE_IS_DAX(inode
))
1565 return fuse_dax_read_iter(iocb
, to
);
1567 if (!(ff
->open_flags
& FOPEN_DIRECT_IO
))
1568 return fuse_cache_read_iter(iocb
, to
);
1570 return fuse_direct_read_iter(iocb
, to
);
1573 static ssize_t
fuse_file_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
1575 struct file
*file
= iocb
->ki_filp
;
1576 struct fuse_file
*ff
= file
->private_data
;
1577 struct inode
*inode
= file_inode(file
);
1579 if (fuse_is_bad(inode
))
1582 if (FUSE_IS_DAX(inode
))
1583 return fuse_dax_write_iter(iocb
, from
);
1585 if (!(ff
->open_flags
& FOPEN_DIRECT_IO
))
1586 return fuse_cache_write_iter(iocb
, from
);
1588 return fuse_direct_write_iter(iocb
, from
);
1591 static void fuse_writepage_free(struct fuse_writepage_args
*wpa
)
1593 struct fuse_args_pages
*ap
= &wpa
->ia
.ap
;
1596 for (i
= 0; i
< ap
->num_pages
; i
++)
1597 __free_page(ap
->pages
[i
]);
1600 fuse_file_put(wpa
->ia
.ff
, false, false);
1606 static void fuse_writepage_finish(struct fuse_mount
*fm
,
1607 struct fuse_writepage_args
*wpa
)
1609 struct fuse_args_pages
*ap
= &wpa
->ia
.ap
;
1610 struct inode
*inode
= wpa
->inode
;
1611 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1612 struct backing_dev_info
*bdi
= inode_to_bdi(inode
);
1615 for (i
= 0; i
< ap
->num_pages
; i
++) {
1616 dec_wb_stat(&bdi
->wb
, WB_WRITEBACK
);
1617 dec_node_page_state(ap
->pages
[i
], NR_WRITEBACK_TEMP
);
1618 wb_writeout_inc(&bdi
->wb
);
1620 wake_up(&fi
->page_waitq
);
1623 /* Called under fi->lock, may release and reacquire it */
1624 static void fuse_send_writepage(struct fuse_mount
*fm
,
1625 struct fuse_writepage_args
*wpa
, loff_t size
)
1626 __releases(fi
->lock
)
1627 __acquires(fi
->lock
)
1629 struct fuse_writepage_args
*aux
, *next
;
1630 struct fuse_inode
*fi
= get_fuse_inode(wpa
->inode
);
1631 struct fuse_write_in
*inarg
= &wpa
->ia
.write
.in
;
1632 struct fuse_args
*args
= &wpa
->ia
.ap
.args
;
1633 __u64 data_size
= wpa
->ia
.ap
.num_pages
* PAGE_SIZE
;
1637 if (inarg
->offset
+ data_size
<= size
) {
1638 inarg
->size
= data_size
;
1639 } else if (inarg
->offset
< size
) {
1640 inarg
->size
= size
- inarg
->offset
;
1642 /* Got truncated off completely */
1646 args
->in_args
[1].size
= inarg
->size
;
1648 args
->nocreds
= true;
1650 err
= fuse_simple_background(fm
, args
, GFP_ATOMIC
);
1651 if (err
== -ENOMEM
) {
1652 spin_unlock(&fi
->lock
);
1653 err
= fuse_simple_background(fm
, args
, GFP_NOFS
| __GFP_NOFAIL
);
1654 spin_lock(&fi
->lock
);
1657 /* Fails on broken connection only */
1665 rb_erase(&wpa
->writepages_entry
, &fi
->writepages
);
1666 fuse_writepage_finish(fm
, wpa
);
1667 spin_unlock(&fi
->lock
);
1669 /* After fuse_writepage_finish() aux request list is private */
1670 for (aux
= wpa
->next
; aux
; aux
= next
) {
1673 fuse_writepage_free(aux
);
1676 fuse_writepage_free(wpa
);
1677 spin_lock(&fi
->lock
);
1681 * If fi->writectr is positive (no truncate or fsync going on) send
1682 * all queued writepage requests.
1684 * Called with fi->lock
1686 void fuse_flush_writepages(struct inode
*inode
)
1687 __releases(fi
->lock
)
1688 __acquires(fi
->lock
)
1690 struct fuse_mount
*fm
= get_fuse_mount(inode
);
1691 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1692 loff_t crop
= i_size_read(inode
);
1693 struct fuse_writepage_args
*wpa
;
1695 while (fi
->writectr
>= 0 && !list_empty(&fi
->queued_writes
)) {
1696 wpa
= list_entry(fi
->queued_writes
.next
,
1697 struct fuse_writepage_args
, queue_entry
);
1698 list_del_init(&wpa
->queue_entry
);
1699 fuse_send_writepage(fm
, wpa
, crop
);
1703 static struct fuse_writepage_args
*fuse_insert_writeback(struct rb_root
*root
,
1704 struct fuse_writepage_args
*wpa
)
1706 pgoff_t idx_from
= wpa
->ia
.write
.in
.offset
>> PAGE_SHIFT
;
1707 pgoff_t idx_to
= idx_from
+ wpa
->ia
.ap
.num_pages
- 1;
1708 struct rb_node
**p
= &root
->rb_node
;
1709 struct rb_node
*parent
= NULL
;
1711 WARN_ON(!wpa
->ia
.ap
.num_pages
);
1713 struct fuse_writepage_args
*curr
;
1717 curr
= rb_entry(parent
, struct fuse_writepage_args
,
1719 WARN_ON(curr
->inode
!= wpa
->inode
);
1720 curr_index
= curr
->ia
.write
.in
.offset
>> PAGE_SHIFT
;
1722 if (idx_from
>= curr_index
+ curr
->ia
.ap
.num_pages
)
1723 p
= &(*p
)->rb_right
;
1724 else if (idx_to
< curr_index
)
1730 rb_link_node(&wpa
->writepages_entry
, parent
, p
);
1731 rb_insert_color(&wpa
->writepages_entry
, root
);
1735 static void tree_insert(struct rb_root
*root
, struct fuse_writepage_args
*wpa
)
1737 WARN_ON(fuse_insert_writeback(root
, wpa
));
1740 static void fuse_writepage_end(struct fuse_mount
*fm
, struct fuse_args
*args
,
1743 struct fuse_writepage_args
*wpa
=
1744 container_of(args
, typeof(*wpa
), ia
.ap
.args
);
1745 struct inode
*inode
= wpa
->inode
;
1746 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1748 mapping_set_error(inode
->i_mapping
, error
);
1749 spin_lock(&fi
->lock
);
1750 rb_erase(&wpa
->writepages_entry
, &fi
->writepages
);
1752 struct fuse_mount
*fm
= get_fuse_mount(inode
);
1753 struct fuse_write_in
*inarg
= &wpa
->ia
.write
.in
;
1754 struct fuse_writepage_args
*next
= wpa
->next
;
1756 wpa
->next
= next
->next
;
1758 next
->ia
.ff
= fuse_file_get(wpa
->ia
.ff
);
1759 tree_insert(&fi
->writepages
, next
);
1762 * Skip fuse_flush_writepages() to make it easy to crop requests
1763 * based on primary request size.
1765 * 1st case (trivial): there are no concurrent activities using
1766 * fuse_set/release_nowrite. Then we're on safe side because
1767 * fuse_flush_writepages() would call fuse_send_writepage()
1770 * 2nd case: someone called fuse_set_nowrite and it is waiting
1771 * now for completion of all in-flight requests. This happens
1772 * rarely and no more than once per page, so this should be
1775 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1776 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1777 * that fuse_set_nowrite returned implies that all in-flight
1778 * requests were completed along with all of their secondary
1779 * requests. Further primary requests are blocked by negative
1780 * writectr. Hence there cannot be any in-flight requests and
1781 * no invocations of fuse_writepage_end() while we're in
1782 * fuse_set_nowrite..fuse_release_nowrite section.
1784 fuse_send_writepage(fm
, next
, inarg
->offset
+ inarg
->size
);
1787 fuse_writepage_finish(fm
, wpa
);
1788 spin_unlock(&fi
->lock
);
1789 fuse_writepage_free(wpa
);
1792 static struct fuse_file
*__fuse_write_file_get(struct fuse_conn
*fc
,
1793 struct fuse_inode
*fi
)
1795 struct fuse_file
*ff
= NULL
;
1797 spin_lock(&fi
->lock
);
1798 if (!list_empty(&fi
->write_files
)) {
1799 ff
= list_entry(fi
->write_files
.next
, struct fuse_file
,
1803 spin_unlock(&fi
->lock
);
1808 static struct fuse_file
*fuse_write_file_get(struct fuse_conn
*fc
,
1809 struct fuse_inode
*fi
)
1811 struct fuse_file
*ff
= __fuse_write_file_get(fc
, fi
);
1816 int fuse_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
1818 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1819 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1820 struct fuse_file
*ff
;
1823 ff
= __fuse_write_file_get(fc
, fi
);
1824 err
= fuse_flush_times(inode
, ff
);
1826 fuse_file_put(ff
, false, false);
1831 static struct fuse_writepage_args
*fuse_writepage_args_alloc(void)
1833 struct fuse_writepage_args
*wpa
;
1834 struct fuse_args_pages
*ap
;
1836 wpa
= kzalloc(sizeof(*wpa
), GFP_NOFS
);
1840 ap
->pages
= fuse_pages_alloc(1, GFP_NOFS
, &ap
->descs
);
1850 static int fuse_writepage_locked(struct page
*page
)
1852 struct address_space
*mapping
= page
->mapping
;
1853 struct inode
*inode
= mapping
->host
;
1854 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1855 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1856 struct fuse_writepage_args
*wpa
;
1857 struct fuse_args_pages
*ap
;
1858 struct page
*tmp_page
;
1859 int error
= -ENOMEM
;
1861 set_page_writeback(page
);
1863 wpa
= fuse_writepage_args_alloc();
1868 tmp_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
1873 wpa
->ia
.ff
= fuse_write_file_get(fc
, fi
);
1877 fuse_write_args_fill(&wpa
->ia
, wpa
->ia
.ff
, page_offset(page
), 0);
1879 copy_highpage(tmp_page
, page
);
1880 wpa
->ia
.write
.in
.write_flags
|= FUSE_WRITE_CACHE
;
1882 ap
->args
.in_pages
= true;
1884 ap
->pages
[0] = tmp_page
;
1885 ap
->descs
[0].offset
= 0;
1886 ap
->descs
[0].length
= PAGE_SIZE
;
1887 ap
->args
.end
= fuse_writepage_end
;
1890 inc_wb_stat(&inode_to_bdi(inode
)->wb
, WB_WRITEBACK
);
1891 inc_node_page_state(tmp_page
, NR_WRITEBACK_TEMP
);
1893 spin_lock(&fi
->lock
);
1894 tree_insert(&fi
->writepages
, wpa
);
1895 list_add_tail(&wpa
->queue_entry
, &fi
->queued_writes
);
1896 fuse_flush_writepages(inode
);
1897 spin_unlock(&fi
->lock
);
1899 end_page_writeback(page
);
1904 __free_page(tmp_page
);
1908 mapping_set_error(page
->mapping
, error
);
1909 end_page_writeback(page
);
1913 static int fuse_writepage(struct page
*page
, struct writeback_control
*wbc
)
1917 if (fuse_page_is_writeback(page
->mapping
->host
, page
->index
)) {
1919 * ->writepages() should be called for sync() and friends. We
1920 * should only get here on direct reclaim and then we are
1921 * allowed to skip a page which is already in flight
1923 WARN_ON(wbc
->sync_mode
== WB_SYNC_ALL
);
1925 redirty_page_for_writepage(wbc
, page
);
1930 err
= fuse_writepage_locked(page
);
1936 struct fuse_fill_wb_data
{
1937 struct fuse_writepage_args
*wpa
;
1938 struct fuse_file
*ff
;
1939 struct inode
*inode
;
1940 struct page
**orig_pages
;
1941 unsigned int max_pages
;
1944 static bool fuse_pages_realloc(struct fuse_fill_wb_data
*data
)
1946 struct fuse_args_pages
*ap
= &data
->wpa
->ia
.ap
;
1947 struct fuse_conn
*fc
= get_fuse_conn(data
->inode
);
1948 struct page
**pages
;
1949 struct fuse_page_desc
*descs
;
1950 unsigned int npages
= min_t(unsigned int,
1951 max_t(unsigned int, data
->max_pages
* 2,
1952 FUSE_DEFAULT_MAX_PAGES_PER_REQ
),
1954 WARN_ON(npages
<= data
->max_pages
);
1956 pages
= fuse_pages_alloc(npages
, GFP_NOFS
, &descs
);
1960 memcpy(pages
, ap
->pages
, sizeof(struct page
*) * ap
->num_pages
);
1961 memcpy(descs
, ap
->descs
, sizeof(struct fuse_page_desc
) * ap
->num_pages
);
1965 data
->max_pages
= npages
;
1970 static void fuse_writepages_send(struct fuse_fill_wb_data
*data
)
1972 struct fuse_writepage_args
*wpa
= data
->wpa
;
1973 struct inode
*inode
= data
->inode
;
1974 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1975 int num_pages
= wpa
->ia
.ap
.num_pages
;
1978 wpa
->ia
.ff
= fuse_file_get(data
->ff
);
1979 spin_lock(&fi
->lock
);
1980 list_add_tail(&wpa
->queue_entry
, &fi
->queued_writes
);
1981 fuse_flush_writepages(inode
);
1982 spin_unlock(&fi
->lock
);
1984 for (i
= 0; i
< num_pages
; i
++)
1985 end_page_writeback(data
->orig_pages
[i
]);
1989 * Check under fi->lock if the page is under writeback, and insert it onto the
1990 * rb_tree if not. Otherwise iterate auxiliary write requests, to see if there's
1991 * one already added for a page at this offset. If there's none, then insert
1992 * this new request onto the auxiliary list, otherwise reuse the existing one by
1993 * swapping the new temp page with the old one.
1995 static bool fuse_writepage_add(struct fuse_writepage_args
*new_wpa
,
1998 struct fuse_inode
*fi
= get_fuse_inode(new_wpa
->inode
);
1999 struct fuse_writepage_args
*tmp
;
2000 struct fuse_writepage_args
*old_wpa
;
2001 struct fuse_args_pages
*new_ap
= &new_wpa
->ia
.ap
;
2003 WARN_ON(new_ap
->num_pages
!= 0);
2004 new_ap
->num_pages
= 1;
2006 spin_lock(&fi
->lock
);
2007 old_wpa
= fuse_insert_writeback(&fi
->writepages
, new_wpa
);
2009 spin_unlock(&fi
->lock
);
2013 for (tmp
= old_wpa
->next
; tmp
; tmp
= tmp
->next
) {
2016 WARN_ON(tmp
->inode
!= new_wpa
->inode
);
2017 curr_index
= tmp
->ia
.write
.in
.offset
>> PAGE_SHIFT
;
2018 if (curr_index
== page
->index
) {
2019 WARN_ON(tmp
->ia
.ap
.num_pages
!= 1);
2020 swap(tmp
->ia
.ap
.pages
[0], new_ap
->pages
[0]);
2026 new_wpa
->next
= old_wpa
->next
;
2027 old_wpa
->next
= new_wpa
;
2030 spin_unlock(&fi
->lock
);
2033 struct backing_dev_info
*bdi
= inode_to_bdi(new_wpa
->inode
);
2035 dec_wb_stat(&bdi
->wb
, WB_WRITEBACK
);
2036 dec_node_page_state(new_ap
->pages
[0], NR_WRITEBACK_TEMP
);
2037 wb_writeout_inc(&bdi
->wb
);
2038 fuse_writepage_free(new_wpa
);
2044 static bool fuse_writepage_need_send(struct fuse_conn
*fc
, struct page
*page
,
2045 struct fuse_args_pages
*ap
,
2046 struct fuse_fill_wb_data
*data
)
2048 WARN_ON(!ap
->num_pages
);
2051 * Being under writeback is unlikely but possible. For example direct
2052 * read to an mmaped fuse file will set the page dirty twice; once when
2053 * the pages are faulted with get_user_pages(), and then after the read
2056 if (fuse_page_is_writeback(data
->inode
, page
->index
))
2059 /* Reached max pages */
2060 if (ap
->num_pages
== fc
->max_pages
)
2063 /* Reached max write bytes */
2064 if ((ap
->num_pages
+ 1) * PAGE_SIZE
> fc
->max_write
)
2068 if (data
->orig_pages
[ap
->num_pages
- 1]->index
+ 1 != page
->index
)
2071 /* Need to grow the pages array? If so, did the expansion fail? */
2072 if (ap
->num_pages
== data
->max_pages
&& !fuse_pages_realloc(data
))
2078 static int fuse_writepages_fill(struct page
*page
,
2079 struct writeback_control
*wbc
, void *_data
)
2081 struct fuse_fill_wb_data
*data
= _data
;
2082 struct fuse_writepage_args
*wpa
= data
->wpa
;
2083 struct fuse_args_pages
*ap
= &wpa
->ia
.ap
;
2084 struct inode
*inode
= data
->inode
;
2085 struct fuse_inode
*fi
= get_fuse_inode(inode
);
2086 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2087 struct page
*tmp_page
;
2092 data
->ff
= fuse_write_file_get(fc
, fi
);
2097 if (wpa
&& fuse_writepage_need_send(fc
, page
, ap
, data
)) {
2098 fuse_writepages_send(data
);
2103 tmp_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
2108 * The page must not be redirtied until the writeout is completed
2109 * (i.e. userspace has sent a reply to the write request). Otherwise
2110 * there could be more than one temporary page instance for each real
2113 * This is ensured by holding the page lock in page_mkwrite() while
2114 * checking fuse_page_is_writeback(). We already hold the page lock
2115 * since clear_page_dirty_for_io() and keep it held until we add the
2116 * request to the fi->writepages list and increment ap->num_pages.
2117 * After this fuse_page_is_writeback() will indicate that the page is
2118 * under writeback, so we can release the page lock.
2120 if (data
->wpa
== NULL
) {
2122 wpa
= fuse_writepage_args_alloc();
2124 __free_page(tmp_page
);
2127 data
->max_pages
= 1;
2130 fuse_write_args_fill(&wpa
->ia
, data
->ff
, page_offset(page
), 0);
2131 wpa
->ia
.write
.in
.write_flags
|= FUSE_WRITE_CACHE
;
2133 ap
->args
.in_pages
= true;
2134 ap
->args
.end
= fuse_writepage_end
;
2138 set_page_writeback(page
);
2140 copy_highpage(tmp_page
, page
);
2141 ap
->pages
[ap
->num_pages
] = tmp_page
;
2142 ap
->descs
[ap
->num_pages
].offset
= 0;
2143 ap
->descs
[ap
->num_pages
].length
= PAGE_SIZE
;
2144 data
->orig_pages
[ap
->num_pages
] = page
;
2146 inc_wb_stat(&inode_to_bdi(inode
)->wb
, WB_WRITEBACK
);
2147 inc_node_page_state(tmp_page
, NR_WRITEBACK_TEMP
);
2152 * Protected by fi->lock against concurrent access by
2153 * fuse_page_is_writeback().
2155 spin_lock(&fi
->lock
);
2157 spin_unlock(&fi
->lock
);
2158 } else if (fuse_writepage_add(wpa
, page
)) {
2161 end_page_writeback(page
);
2169 static int fuse_writepages(struct address_space
*mapping
,
2170 struct writeback_control
*wbc
)
2172 struct inode
*inode
= mapping
->host
;
2173 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2174 struct fuse_fill_wb_data data
;
2178 if (fuse_is_bad(inode
))
2186 data
.orig_pages
= kcalloc(fc
->max_pages
,
2187 sizeof(struct page
*),
2189 if (!data
.orig_pages
)
2192 err
= write_cache_pages(mapping
, wbc
, fuse_writepages_fill
, &data
);
2194 WARN_ON(!data
.wpa
->ia
.ap
.num_pages
);
2195 fuse_writepages_send(&data
);
2198 fuse_file_put(data
.ff
, false, false);
2200 kfree(data
.orig_pages
);
2206 * It's worthy to make sure that space is reserved on disk for the write,
2207 * but how to implement it without killing performance need more thinking.
2209 static int fuse_write_begin(struct file
*file
, struct address_space
*mapping
,
2210 loff_t pos
, unsigned len
, unsigned flags
,
2211 struct page
**pagep
, void **fsdata
)
2213 pgoff_t index
= pos
>> PAGE_SHIFT
;
2214 struct fuse_conn
*fc
= get_fuse_conn(file_inode(file
));
2219 WARN_ON(!fc
->writeback_cache
);
2221 page
= grab_cache_page_write_begin(mapping
, index
, flags
);
2225 fuse_wait_on_page_writeback(mapping
->host
, page
->index
);
2227 if (PageUptodate(page
) || len
== PAGE_SIZE
)
2230 * Check if the start this page comes after the end of file, in which
2231 * case the readpage can be optimized away.
2233 fsize
= i_size_read(mapping
->host
);
2234 if (fsize
<= (pos
& PAGE_MASK
)) {
2235 size_t off
= pos
& ~PAGE_MASK
;
2237 zero_user_segment(page
, 0, off
);
2240 err
= fuse_do_readpage(file
, page
);
2254 static int fuse_write_end(struct file
*file
, struct address_space
*mapping
,
2255 loff_t pos
, unsigned len
, unsigned copied
,
2256 struct page
*page
, void *fsdata
)
2258 struct inode
*inode
= page
->mapping
->host
;
2260 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2264 if (!PageUptodate(page
)) {
2265 /* Zero any unwritten bytes at the end of the page */
2266 size_t endoff
= (pos
+ copied
) & ~PAGE_MASK
;
2268 zero_user_segment(page
, endoff
, PAGE_SIZE
);
2269 SetPageUptodate(page
);
2272 fuse_write_update_size(inode
, pos
+ copied
);
2273 set_page_dirty(page
);
2282 static int fuse_launder_page(struct page
*page
)
2285 if (clear_page_dirty_for_io(page
)) {
2286 struct inode
*inode
= page
->mapping
->host
;
2287 err
= fuse_writepage_locked(page
);
2289 fuse_wait_on_page_writeback(inode
, page
->index
);
2295 * Write back dirty pages now, because there may not be any suitable
2298 static void fuse_vma_close(struct vm_area_struct
*vma
)
2300 filemap_write_and_wait(vma
->vm_file
->f_mapping
);
2304 * Wait for writeback against this page to complete before allowing it
2305 * to be marked dirty again, and hence written back again, possibly
2306 * before the previous writepage completed.
2308 * Block here, instead of in ->writepage(), so that the userspace fs
2309 * can only block processes actually operating on the filesystem.
2311 * Otherwise unprivileged userspace fs would be able to block
2316 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2318 static vm_fault_t
fuse_page_mkwrite(struct vm_fault
*vmf
)
2320 struct page
*page
= vmf
->page
;
2321 struct inode
*inode
= file_inode(vmf
->vma
->vm_file
);
2323 file_update_time(vmf
->vma
->vm_file
);
2325 if (page
->mapping
!= inode
->i_mapping
) {
2327 return VM_FAULT_NOPAGE
;
2330 fuse_wait_on_page_writeback(inode
, page
->index
);
2331 return VM_FAULT_LOCKED
;
2334 static const struct vm_operations_struct fuse_file_vm_ops
= {
2335 .close
= fuse_vma_close
,
2336 .fault
= filemap_fault
,
2337 .map_pages
= filemap_map_pages
,
2338 .page_mkwrite
= fuse_page_mkwrite
,
2341 static int fuse_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2343 struct fuse_file
*ff
= file
->private_data
;
2345 /* DAX mmap is superior to direct_io mmap */
2346 if (FUSE_IS_DAX(file_inode(file
)))
2347 return fuse_dax_mmap(file
, vma
);
2349 if (ff
->open_flags
& FOPEN_DIRECT_IO
) {
2350 /* Can't provide the coherency needed for MAP_SHARED */
2351 if (vma
->vm_flags
& VM_MAYSHARE
)
2354 invalidate_inode_pages2(file
->f_mapping
);
2356 return generic_file_mmap(file
, vma
);
2359 if ((vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
))
2360 fuse_link_write_file(file
);
2362 file_accessed(file
);
2363 vma
->vm_ops
= &fuse_file_vm_ops
;
2367 static int convert_fuse_file_lock(struct fuse_conn
*fc
,
2368 const struct fuse_file_lock
*ffl
,
2369 struct file_lock
*fl
)
2371 switch (ffl
->type
) {
2377 if (ffl
->start
> OFFSET_MAX
|| ffl
->end
> OFFSET_MAX
||
2378 ffl
->end
< ffl
->start
)
2381 fl
->fl_start
= ffl
->start
;
2382 fl
->fl_end
= ffl
->end
;
2385 * Convert pid into init's pid namespace. The locks API will
2386 * translate it into the caller's pid namespace.
2389 fl
->fl_pid
= pid_nr_ns(find_pid_ns(ffl
->pid
, fc
->pid_ns
), &init_pid_ns
);
2396 fl
->fl_type
= ffl
->type
;
2400 static void fuse_lk_fill(struct fuse_args
*args
, struct file
*file
,
2401 const struct file_lock
*fl
, int opcode
, pid_t pid
,
2402 int flock
, struct fuse_lk_in
*inarg
)
2404 struct inode
*inode
= file_inode(file
);
2405 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2406 struct fuse_file
*ff
= file
->private_data
;
2408 memset(inarg
, 0, sizeof(*inarg
));
2410 inarg
->owner
= fuse_lock_owner_id(fc
, fl
->fl_owner
);
2411 inarg
->lk
.start
= fl
->fl_start
;
2412 inarg
->lk
.end
= fl
->fl_end
;
2413 inarg
->lk
.type
= fl
->fl_type
;
2414 inarg
->lk
.pid
= pid
;
2416 inarg
->lk_flags
|= FUSE_LK_FLOCK
;
2417 args
->opcode
= opcode
;
2418 args
->nodeid
= get_node_id(inode
);
2419 args
->in_numargs
= 1;
2420 args
->in_args
[0].size
= sizeof(*inarg
);
2421 args
->in_args
[0].value
= inarg
;
2424 static int fuse_getlk(struct file
*file
, struct file_lock
*fl
)
2426 struct inode
*inode
= file_inode(file
);
2427 struct fuse_mount
*fm
= get_fuse_mount(inode
);
2429 struct fuse_lk_in inarg
;
2430 struct fuse_lk_out outarg
;
2433 fuse_lk_fill(&args
, file
, fl
, FUSE_GETLK
, 0, 0, &inarg
);
2434 args
.out_numargs
= 1;
2435 args
.out_args
[0].size
= sizeof(outarg
);
2436 args
.out_args
[0].value
= &outarg
;
2437 err
= fuse_simple_request(fm
, &args
);
2439 err
= convert_fuse_file_lock(fm
->fc
, &outarg
.lk
, fl
);
2444 static int fuse_setlk(struct file
*file
, struct file_lock
*fl
, int flock
)
2446 struct inode
*inode
= file_inode(file
);
2447 struct fuse_mount
*fm
= get_fuse_mount(inode
);
2449 struct fuse_lk_in inarg
;
2450 int opcode
= (fl
->fl_flags
& FL_SLEEP
) ? FUSE_SETLKW
: FUSE_SETLK
;
2451 struct pid
*pid
= fl
->fl_type
!= F_UNLCK
? task_tgid(current
) : NULL
;
2452 pid_t pid_nr
= pid_nr_ns(pid
, fm
->fc
->pid_ns
);
2455 if (fl
->fl_lmops
&& fl
->fl_lmops
->lm_grant
) {
2456 /* NLM needs asynchronous locks, which we don't support yet */
2460 /* Unlock on close is handled by the flush method */
2461 if ((fl
->fl_flags
& FL_CLOSE_POSIX
) == FL_CLOSE_POSIX
)
2464 fuse_lk_fill(&args
, file
, fl
, opcode
, pid_nr
, flock
, &inarg
);
2465 err
= fuse_simple_request(fm
, &args
);
2467 /* locking is restartable */
2474 static int fuse_file_lock(struct file
*file
, int cmd
, struct file_lock
*fl
)
2476 struct inode
*inode
= file_inode(file
);
2477 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2480 if (cmd
== F_CANCELLK
) {
2482 } else if (cmd
== F_GETLK
) {
2484 posix_test_lock(file
, fl
);
2487 err
= fuse_getlk(file
, fl
);
2490 err
= posix_lock_file(file
, fl
, NULL
);
2492 err
= fuse_setlk(file
, fl
, 0);
2497 static int fuse_file_flock(struct file
*file
, int cmd
, struct file_lock
*fl
)
2499 struct inode
*inode
= file_inode(file
);
2500 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2504 err
= locks_lock_file_wait(file
, fl
);
2506 struct fuse_file
*ff
= file
->private_data
;
2508 /* emulate flock with POSIX locks */
2510 err
= fuse_setlk(file
, fl
, 1);
2516 static sector_t
fuse_bmap(struct address_space
*mapping
, sector_t block
)
2518 struct inode
*inode
= mapping
->host
;
2519 struct fuse_mount
*fm
= get_fuse_mount(inode
);
2521 struct fuse_bmap_in inarg
;
2522 struct fuse_bmap_out outarg
;
2525 if (!inode
->i_sb
->s_bdev
|| fm
->fc
->no_bmap
)
2528 memset(&inarg
, 0, sizeof(inarg
));
2529 inarg
.block
= block
;
2530 inarg
.blocksize
= inode
->i_sb
->s_blocksize
;
2531 args
.opcode
= FUSE_BMAP
;
2532 args
.nodeid
= get_node_id(inode
);
2533 args
.in_numargs
= 1;
2534 args
.in_args
[0].size
= sizeof(inarg
);
2535 args
.in_args
[0].value
= &inarg
;
2536 args
.out_numargs
= 1;
2537 args
.out_args
[0].size
= sizeof(outarg
);
2538 args
.out_args
[0].value
= &outarg
;
2539 err
= fuse_simple_request(fm
, &args
);
2541 fm
->fc
->no_bmap
= 1;
2543 return err
? 0 : outarg
.block
;
2546 static loff_t
fuse_lseek(struct file
*file
, loff_t offset
, int whence
)
2548 struct inode
*inode
= file
->f_mapping
->host
;
2549 struct fuse_mount
*fm
= get_fuse_mount(inode
);
2550 struct fuse_file
*ff
= file
->private_data
;
2552 struct fuse_lseek_in inarg
= {
2557 struct fuse_lseek_out outarg
;
2560 if (fm
->fc
->no_lseek
)
2563 args
.opcode
= FUSE_LSEEK
;
2564 args
.nodeid
= ff
->nodeid
;
2565 args
.in_numargs
= 1;
2566 args
.in_args
[0].size
= sizeof(inarg
);
2567 args
.in_args
[0].value
= &inarg
;
2568 args
.out_numargs
= 1;
2569 args
.out_args
[0].size
= sizeof(outarg
);
2570 args
.out_args
[0].value
= &outarg
;
2571 err
= fuse_simple_request(fm
, &args
);
2573 if (err
== -ENOSYS
) {
2574 fm
->fc
->no_lseek
= 1;
2580 return vfs_setpos(file
, outarg
.offset
, inode
->i_sb
->s_maxbytes
);
2583 err
= fuse_update_attributes(inode
, file
);
2585 return generic_file_llseek(file
, offset
, whence
);
2590 static loff_t
fuse_file_llseek(struct file
*file
, loff_t offset
, int whence
)
2593 struct inode
*inode
= file_inode(file
);
2598 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2599 retval
= generic_file_llseek(file
, offset
, whence
);
2603 retval
= fuse_update_attributes(inode
, file
);
2605 retval
= generic_file_llseek(file
, offset
, whence
);
2606 inode_unlock(inode
);
2611 retval
= fuse_lseek(file
, offset
, whence
);
2612 inode_unlock(inode
);
2622 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2623 * ABI was defined to be 'struct iovec' which is different on 32bit
2624 * and 64bit. Fortunately we can determine which structure the server
2625 * used from the size of the reply.
2627 static int fuse_copy_ioctl_iovec_old(struct iovec
*dst
, void *src
,
2628 size_t transferred
, unsigned count
,
2631 #ifdef CONFIG_COMPAT
2632 if (count
* sizeof(struct compat_iovec
) == transferred
) {
2633 struct compat_iovec
*ciov
= src
;
2637 * With this interface a 32bit server cannot support
2638 * non-compat (i.e. ones coming from 64bit apps) ioctl
2644 for (i
= 0; i
< count
; i
++) {
2645 dst
[i
].iov_base
= compat_ptr(ciov
[i
].iov_base
);
2646 dst
[i
].iov_len
= ciov
[i
].iov_len
;
2652 if (count
* sizeof(struct iovec
) != transferred
)
2655 memcpy(dst
, src
, transferred
);
2659 /* Make sure iov_length() won't overflow */
2660 static int fuse_verify_ioctl_iov(struct fuse_conn
*fc
, struct iovec
*iov
,
2664 u32 max
= fc
->max_pages
<< PAGE_SHIFT
;
2666 for (n
= 0; n
< count
; n
++, iov
++) {
2667 if (iov
->iov_len
> (size_t) max
)
2669 max
-= iov
->iov_len
;
2674 static int fuse_copy_ioctl_iovec(struct fuse_conn
*fc
, struct iovec
*dst
,
2675 void *src
, size_t transferred
, unsigned count
,
2679 struct fuse_ioctl_iovec
*fiov
= src
;
2681 if (fc
->minor
< 16) {
2682 return fuse_copy_ioctl_iovec_old(dst
, src
, transferred
,
2686 if (count
* sizeof(struct fuse_ioctl_iovec
) != transferred
)
2689 for (i
= 0; i
< count
; i
++) {
2690 /* Did the server supply an inappropriate value? */
2691 if (fiov
[i
].base
!= (unsigned long) fiov
[i
].base
||
2692 fiov
[i
].len
!= (unsigned long) fiov
[i
].len
)
2695 dst
[i
].iov_base
= (void __user
*) (unsigned long) fiov
[i
].base
;
2696 dst
[i
].iov_len
= (size_t) fiov
[i
].len
;
2698 #ifdef CONFIG_COMPAT
2700 (ptr_to_compat(dst
[i
].iov_base
) != fiov
[i
].base
||
2701 (compat_size_t
) dst
[i
].iov_len
!= fiov
[i
].len
))
2711 * For ioctls, there is no generic way to determine how much memory
2712 * needs to be read and/or written. Furthermore, ioctls are allowed
2713 * to dereference the passed pointer, so the parameter requires deep
2714 * copying but FUSE has no idea whatsoever about what to copy in or
2717 * This is solved by allowing FUSE server to retry ioctl with
2718 * necessary in/out iovecs. Let's assume the ioctl implementation
2719 * needs to read in the following structure.
2726 * On the first callout to FUSE server, inarg->in_size and
2727 * inarg->out_size will be NULL; then, the server completes the ioctl
2728 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2729 * the actual iov array to
2731 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2733 * which tells FUSE to copy in the requested area and retry the ioctl.
2734 * On the second round, the server has access to the structure and
2735 * from that it can tell what to look for next, so on the invocation,
2736 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2738 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2739 * { .iov_base = a.buf, .iov_len = a.buflen } }
2741 * FUSE will copy both struct a and the pointed buffer from the
2742 * process doing the ioctl and retry ioctl with both struct a and the
2745 * This time, FUSE server has everything it needs and completes ioctl
2746 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2748 * Copying data out works the same way.
2750 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2751 * automatically initializes in and out iovs by decoding @cmd with
2752 * _IOC_* macros and the server is not allowed to request RETRY. This
2753 * limits ioctl data transfers to well-formed ioctls and is the forced
2754 * behavior for all FUSE servers.
2756 long fuse_do_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
,
2759 struct fuse_file
*ff
= file
->private_data
;
2760 struct fuse_mount
*fm
= ff
->fm
;
2761 struct fuse_ioctl_in inarg
= {
2767 struct fuse_ioctl_out outarg
;
2768 struct iovec
*iov_page
= NULL
;
2769 struct iovec
*in_iov
= NULL
, *out_iov
= NULL
;
2770 unsigned int in_iovs
= 0, out_iovs
= 0, max_pages
;
2771 size_t in_size
, out_size
, c
;
2772 ssize_t transferred
;
2775 struct fuse_args_pages ap
= {};
2777 #if BITS_PER_LONG == 32
2778 inarg
.flags
|= FUSE_IOCTL_32BIT
;
2780 if (flags
& FUSE_IOCTL_COMPAT
) {
2781 inarg
.flags
|= FUSE_IOCTL_32BIT
;
2782 #ifdef CONFIG_X86_X32
2783 if (in_x32_syscall())
2784 inarg
.flags
|= FUSE_IOCTL_COMPAT_X32
;
2789 /* assume all the iovs returned by client always fits in a page */
2790 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec
) * FUSE_IOCTL_MAX_IOV
> PAGE_SIZE
);
2793 ap
.pages
= fuse_pages_alloc(fm
->fc
->max_pages
, GFP_KERNEL
, &ap
.descs
);
2794 iov_page
= (struct iovec
*) __get_free_page(GFP_KERNEL
);
2795 if (!ap
.pages
|| !iov_page
)
2798 fuse_page_descs_length_init(ap
.descs
, 0, fm
->fc
->max_pages
);
2801 * If restricted, initialize IO parameters as encoded in @cmd.
2802 * RETRY from server is not allowed.
2804 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
)) {
2805 struct iovec
*iov
= iov_page
;
2807 iov
->iov_base
= (void __user
*)arg
;
2810 case FS_IOC_GETFLAGS
:
2811 case FS_IOC_SETFLAGS
:
2812 iov
->iov_len
= sizeof(int);
2815 iov
->iov_len
= _IOC_SIZE(cmd
);
2819 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
2824 if (_IOC_DIR(cmd
) & _IOC_READ
) {
2831 inarg
.in_size
= in_size
= iov_length(in_iov
, in_iovs
);
2832 inarg
.out_size
= out_size
= iov_length(out_iov
, out_iovs
);
2835 * Out data can be used either for actual out data or iovs,
2836 * make sure there always is at least one page.
2838 out_size
= max_t(size_t, out_size
, PAGE_SIZE
);
2839 max_pages
= DIV_ROUND_UP(max(in_size
, out_size
), PAGE_SIZE
);
2841 /* make sure there are enough buffer pages and init request with them */
2843 if (max_pages
> fm
->fc
->max_pages
)
2845 while (ap
.num_pages
< max_pages
) {
2846 ap
.pages
[ap
.num_pages
] = alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
2847 if (!ap
.pages
[ap
.num_pages
])
2853 /* okay, let's send it to the client */
2854 ap
.args
.opcode
= FUSE_IOCTL
;
2855 ap
.args
.nodeid
= ff
->nodeid
;
2856 ap
.args
.in_numargs
= 1;
2857 ap
.args
.in_args
[0].size
= sizeof(inarg
);
2858 ap
.args
.in_args
[0].value
= &inarg
;
2860 ap
.args
.in_numargs
++;
2861 ap
.args
.in_args
[1].size
= in_size
;
2862 ap
.args
.in_pages
= true;
2865 iov_iter_init(&ii
, WRITE
, in_iov
, in_iovs
, in_size
);
2866 for (i
= 0; iov_iter_count(&ii
) && !WARN_ON(i
>= ap
.num_pages
); i
++) {
2867 c
= copy_page_from_iter(ap
.pages
[i
], 0, PAGE_SIZE
, &ii
);
2868 if (c
!= PAGE_SIZE
&& iov_iter_count(&ii
))
2873 ap
.args
.out_numargs
= 2;
2874 ap
.args
.out_args
[0].size
= sizeof(outarg
);
2875 ap
.args
.out_args
[0].value
= &outarg
;
2876 ap
.args
.out_args
[1].size
= out_size
;
2877 ap
.args
.out_pages
= true;
2878 ap
.args
.out_argvar
= true;
2880 transferred
= fuse_simple_request(fm
, &ap
.args
);
2882 if (transferred
< 0)
2885 /* did it ask for retry? */
2886 if (outarg
.flags
& FUSE_IOCTL_RETRY
) {
2889 /* no retry if in restricted mode */
2891 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
))
2894 in_iovs
= outarg
.in_iovs
;
2895 out_iovs
= outarg
.out_iovs
;
2898 * Make sure things are in boundary, separate checks
2899 * are to protect against overflow.
2902 if (in_iovs
> FUSE_IOCTL_MAX_IOV
||
2903 out_iovs
> FUSE_IOCTL_MAX_IOV
||
2904 in_iovs
+ out_iovs
> FUSE_IOCTL_MAX_IOV
)
2907 vaddr
= kmap_atomic(ap
.pages
[0]);
2908 err
= fuse_copy_ioctl_iovec(fm
->fc
, iov_page
, vaddr
,
2909 transferred
, in_iovs
+ out_iovs
,
2910 (flags
& FUSE_IOCTL_COMPAT
) != 0);
2911 kunmap_atomic(vaddr
);
2916 out_iov
= in_iov
+ in_iovs
;
2918 err
= fuse_verify_ioctl_iov(fm
->fc
, in_iov
, in_iovs
);
2922 err
= fuse_verify_ioctl_iov(fm
->fc
, out_iov
, out_iovs
);
2930 if (transferred
> inarg
.out_size
)
2934 iov_iter_init(&ii
, READ
, out_iov
, out_iovs
, transferred
);
2935 for (i
= 0; iov_iter_count(&ii
) && !WARN_ON(i
>= ap
.num_pages
); i
++) {
2936 c
= copy_page_to_iter(ap
.pages
[i
], 0, PAGE_SIZE
, &ii
);
2937 if (c
!= PAGE_SIZE
&& iov_iter_count(&ii
))
2942 free_page((unsigned long) iov_page
);
2943 while (ap
.num_pages
)
2944 __free_page(ap
.pages
[--ap
.num_pages
]);
2947 return err
? err
: outarg
.result
;
2949 EXPORT_SYMBOL_GPL(fuse_do_ioctl
);
2951 long fuse_ioctl_common(struct file
*file
, unsigned int cmd
,
2952 unsigned long arg
, unsigned int flags
)
2954 struct inode
*inode
= file_inode(file
);
2955 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2957 if (!fuse_allow_current_process(fc
))
2960 if (fuse_is_bad(inode
))
2963 return fuse_do_ioctl(file
, cmd
, arg
, flags
);
2966 static long fuse_file_ioctl(struct file
*file
, unsigned int cmd
,
2969 return fuse_ioctl_common(file
, cmd
, arg
, 0);
2972 static long fuse_file_compat_ioctl(struct file
*file
, unsigned int cmd
,
2975 return fuse_ioctl_common(file
, cmd
, arg
, FUSE_IOCTL_COMPAT
);
2979 * All files which have been polled are linked to RB tree
2980 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2981 * find the matching one.
2983 static struct rb_node
**fuse_find_polled_node(struct fuse_conn
*fc
, u64 kh
,
2984 struct rb_node
**parent_out
)
2986 struct rb_node
**link
= &fc
->polled_files
.rb_node
;
2987 struct rb_node
*last
= NULL
;
2990 struct fuse_file
*ff
;
2993 ff
= rb_entry(last
, struct fuse_file
, polled_node
);
2996 link
= &last
->rb_left
;
2997 else if (kh
> ff
->kh
)
2998 link
= &last
->rb_right
;
3009 * The file is about to be polled. Make sure it's on the polled_files
3010 * RB tree. Note that files once added to the polled_files tree are
3011 * not removed before the file is released. This is because a file
3012 * polled once is likely to be polled again.
3014 static void fuse_register_polled_file(struct fuse_conn
*fc
,
3015 struct fuse_file
*ff
)
3017 spin_lock(&fc
->lock
);
3018 if (RB_EMPTY_NODE(&ff
->polled_node
)) {
3019 struct rb_node
**link
, *parent
;
3021 link
= fuse_find_polled_node(fc
, ff
->kh
, &parent
);
3023 rb_link_node(&ff
->polled_node
, parent
, link
);
3024 rb_insert_color(&ff
->polled_node
, &fc
->polled_files
);
3026 spin_unlock(&fc
->lock
);
3029 __poll_t
fuse_file_poll(struct file
*file
, poll_table
*wait
)
3031 struct fuse_file
*ff
= file
->private_data
;
3032 struct fuse_mount
*fm
= ff
->fm
;
3033 struct fuse_poll_in inarg
= { .fh
= ff
->fh
, .kh
= ff
->kh
};
3034 struct fuse_poll_out outarg
;
3038 if (fm
->fc
->no_poll
)
3039 return DEFAULT_POLLMASK
;
3041 poll_wait(file
, &ff
->poll_wait
, wait
);
3042 inarg
.events
= mangle_poll(poll_requested_events(wait
));
3045 * Ask for notification iff there's someone waiting for it.
3046 * The client may ignore the flag and always notify.
3048 if (waitqueue_active(&ff
->poll_wait
)) {
3049 inarg
.flags
|= FUSE_POLL_SCHEDULE_NOTIFY
;
3050 fuse_register_polled_file(fm
->fc
, ff
);
3053 args
.opcode
= FUSE_POLL
;
3054 args
.nodeid
= ff
->nodeid
;
3055 args
.in_numargs
= 1;
3056 args
.in_args
[0].size
= sizeof(inarg
);
3057 args
.in_args
[0].value
= &inarg
;
3058 args
.out_numargs
= 1;
3059 args
.out_args
[0].size
= sizeof(outarg
);
3060 args
.out_args
[0].value
= &outarg
;
3061 err
= fuse_simple_request(fm
, &args
);
3064 return demangle_poll(outarg
.revents
);
3065 if (err
== -ENOSYS
) {
3066 fm
->fc
->no_poll
= 1;
3067 return DEFAULT_POLLMASK
;
3071 EXPORT_SYMBOL_GPL(fuse_file_poll
);
3074 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
3075 * wakes up the poll waiters.
3077 int fuse_notify_poll_wakeup(struct fuse_conn
*fc
,
3078 struct fuse_notify_poll_wakeup_out
*outarg
)
3080 u64 kh
= outarg
->kh
;
3081 struct rb_node
**link
;
3083 spin_lock(&fc
->lock
);
3085 link
= fuse_find_polled_node(fc
, kh
, NULL
);
3087 struct fuse_file
*ff
;
3089 ff
= rb_entry(*link
, struct fuse_file
, polled_node
);
3090 wake_up_interruptible_sync(&ff
->poll_wait
);
3093 spin_unlock(&fc
->lock
);
3097 static void fuse_do_truncate(struct file
*file
)
3099 struct inode
*inode
= file
->f_mapping
->host
;
3102 attr
.ia_valid
= ATTR_SIZE
;
3103 attr
.ia_size
= i_size_read(inode
);
3105 attr
.ia_file
= file
;
3106 attr
.ia_valid
|= ATTR_FILE
;
3108 fuse_do_setattr(file_dentry(file
), &attr
, file
);
3111 static inline loff_t
fuse_round_up(struct fuse_conn
*fc
, loff_t off
)
3113 return round_up(off
, fc
->max_pages
<< PAGE_SHIFT
);
3117 fuse_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
)
3119 DECLARE_COMPLETION_ONSTACK(wait
);
3121 struct file
*file
= iocb
->ki_filp
;
3122 struct fuse_file
*ff
= file
->private_data
;
3124 struct inode
*inode
;
3126 size_t count
= iov_iter_count(iter
), shortened
= 0;
3127 loff_t offset
= iocb
->ki_pos
;
3128 struct fuse_io_priv
*io
;
3131 inode
= file
->f_mapping
->host
;
3132 i_size
= i_size_read(inode
);
3134 if ((iov_iter_rw(iter
) == READ
) && (offset
>= i_size
))
3137 io
= kmalloc(sizeof(struct fuse_io_priv
), GFP_KERNEL
);
3140 spin_lock_init(&io
->lock
);
3141 kref_init(&io
->refcnt
);
3145 io
->offset
= offset
;
3146 io
->write
= (iov_iter_rw(iter
) == WRITE
);
3149 * By default, we want to optimize all I/Os with async request
3150 * submission to the client filesystem if supported.
3152 io
->async
= ff
->fm
->fc
->async_dio
;
3154 io
->blocking
= is_sync_kiocb(iocb
);
3156 /* optimization for short read */
3157 if (io
->async
&& !io
->write
&& offset
+ count
> i_size
) {
3158 iov_iter_truncate(iter
, fuse_round_up(ff
->fm
->fc
, i_size
- offset
));
3159 shortened
= count
- iov_iter_count(iter
);
3164 * We cannot asynchronously extend the size of a file.
3165 * In such case the aio will behave exactly like sync io.
3167 if ((offset
+ count
> i_size
) && io
->write
)
3168 io
->blocking
= true;
3170 if (io
->async
&& io
->blocking
) {
3172 * Additional reference to keep io around after
3173 * calling fuse_aio_complete()
3175 kref_get(&io
->refcnt
);
3179 if (iov_iter_rw(iter
) == WRITE
) {
3180 ret
= fuse_direct_io(io
, iter
, &pos
, FUSE_DIO_WRITE
);
3181 fuse_invalidate_attr(inode
);
3183 ret
= __fuse_direct_read(io
, iter
, &pos
);
3185 iov_iter_reexpand(iter
, iov_iter_count(iter
) + shortened
);
3188 bool blocking
= io
->blocking
;
3190 fuse_aio_complete(io
, ret
< 0 ? ret
: 0, -1);
3192 /* we have a non-extending, async request, so return */
3194 return -EIOCBQUEUED
;
3196 wait_for_completion(&wait
);
3197 ret
= fuse_get_res_by_io(io
);
3200 kref_put(&io
->refcnt
, fuse_io_release
);
3202 if (iov_iter_rw(iter
) == WRITE
) {
3204 fuse_write_update_size(inode
, pos
);
3205 else if (ret
< 0 && offset
+ count
> i_size
)
3206 fuse_do_truncate(file
);
3212 static int fuse_writeback_range(struct inode
*inode
, loff_t start
, loff_t end
)
3214 int err
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
3217 fuse_sync_writes(inode
);
3222 static long fuse_file_fallocate(struct file
*file
, int mode
, loff_t offset
,
3225 struct fuse_file
*ff
= file
->private_data
;
3226 struct inode
*inode
= file_inode(file
);
3227 struct fuse_inode
*fi
= get_fuse_inode(inode
);
3228 struct fuse_mount
*fm
= ff
->fm
;
3230 struct fuse_fallocate_in inarg
= {
3237 bool lock_inode
= !(mode
& FALLOC_FL_KEEP_SIZE
) ||
3238 (mode
& FALLOC_FL_PUNCH_HOLE
);
3240 bool block_faults
= FUSE_IS_DAX(inode
) && lock_inode
;
3242 if (mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
))
3245 if (fm
->fc
->no_fallocate
)
3251 down_write(&fi
->i_mmap_sem
);
3252 err
= fuse_dax_break_layouts(inode
, 0, 0);
3257 if (mode
& FALLOC_FL_PUNCH_HOLE
) {
3258 loff_t endbyte
= offset
+ length
- 1;
3260 err
= fuse_writeback_range(inode
, offset
, endbyte
);
3266 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
3267 offset
+ length
> i_size_read(inode
)) {
3268 err
= inode_newsize_ok(inode
, offset
+ length
);
3273 if (!(mode
& FALLOC_FL_KEEP_SIZE
))
3274 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
3276 args
.opcode
= FUSE_FALLOCATE
;
3277 args
.nodeid
= ff
->nodeid
;
3278 args
.in_numargs
= 1;
3279 args
.in_args
[0].size
= sizeof(inarg
);
3280 args
.in_args
[0].value
= &inarg
;
3281 err
= fuse_simple_request(fm
, &args
);
3282 if (err
== -ENOSYS
) {
3283 fm
->fc
->no_fallocate
= 1;
3289 /* we could have extended the file */
3290 if (!(mode
& FALLOC_FL_KEEP_SIZE
)) {
3291 bool changed
= fuse_write_update_size(inode
, offset
+ length
);
3293 if (changed
&& fm
->fc
->writeback_cache
)
3294 file_update_time(file
);
3297 if (mode
& FALLOC_FL_PUNCH_HOLE
)
3298 truncate_pagecache_range(inode
, offset
, offset
+ length
- 1);
3300 fuse_invalidate_attr(inode
);
3303 if (!(mode
& FALLOC_FL_KEEP_SIZE
))
3304 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
3307 up_write(&fi
->i_mmap_sem
);
3310 inode_unlock(inode
);
3315 static ssize_t
__fuse_copy_file_range(struct file
*file_in
, loff_t pos_in
,
3316 struct file
*file_out
, loff_t pos_out
,
3317 size_t len
, unsigned int flags
)
3319 struct fuse_file
*ff_in
= file_in
->private_data
;
3320 struct fuse_file
*ff_out
= file_out
->private_data
;
3321 struct inode
*inode_in
= file_inode(file_in
);
3322 struct inode
*inode_out
= file_inode(file_out
);
3323 struct fuse_inode
*fi_out
= get_fuse_inode(inode_out
);
3324 struct fuse_mount
*fm
= ff_in
->fm
;
3325 struct fuse_conn
*fc
= fm
->fc
;
3327 struct fuse_copy_file_range_in inarg
= {
3330 .nodeid_out
= ff_out
->nodeid
,
3331 .fh_out
= ff_out
->fh
,
3336 struct fuse_write_out outarg
;
3338 /* mark unstable when write-back is not used, and file_out gets
3340 bool is_unstable
= (!fc
->writeback_cache
) &&
3341 ((pos_out
+ len
) > inode_out
->i_size
);
3343 if (fc
->no_copy_file_range
)
3346 if (file_inode(file_in
)->i_sb
!= file_inode(file_out
)->i_sb
)
3349 inode_lock(inode_in
);
3350 err
= fuse_writeback_range(inode_in
, pos_in
, pos_in
+ len
- 1);
3351 inode_unlock(inode_in
);
3355 inode_lock(inode_out
);
3357 err
= file_modified(file_out
);
3362 * Write out dirty pages in the destination file before sending the COPY
3363 * request to userspace. After the request is completed, truncate off
3364 * pages (including partial ones) from the cache that have been copied,
3365 * since these contain stale data at that point.
3367 * This should be mostly correct, but if the COPY writes to partial
3368 * pages (at the start or end) and the parts not covered by the COPY are
3369 * written through a memory map after calling fuse_writeback_range(),
3370 * then these partial page modifications will be lost on truncation.
3372 * It is unlikely that someone would rely on such mixed style
3373 * modifications. Yet this does give less guarantees than if the
3374 * copying was performed with write(2).
3376 * To fix this a i_mmap_sem style lock could be used to prevent new
3377 * faults while the copy is ongoing.
3379 err
= fuse_writeback_range(inode_out
, pos_out
, pos_out
+ len
- 1);
3384 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi_out
->state
);
3386 args
.opcode
= FUSE_COPY_FILE_RANGE
;
3387 args
.nodeid
= ff_in
->nodeid
;
3388 args
.in_numargs
= 1;
3389 args
.in_args
[0].size
= sizeof(inarg
);
3390 args
.in_args
[0].value
= &inarg
;
3391 args
.out_numargs
= 1;
3392 args
.out_args
[0].size
= sizeof(outarg
);
3393 args
.out_args
[0].value
= &outarg
;
3394 err
= fuse_simple_request(fm
, &args
);
3395 if (err
== -ENOSYS
) {
3396 fc
->no_copy_file_range
= 1;
3402 truncate_inode_pages_range(inode_out
->i_mapping
,
3403 ALIGN_DOWN(pos_out
, PAGE_SIZE
),
3404 ALIGN(pos_out
+ outarg
.size
, PAGE_SIZE
) - 1);
3406 if (fc
->writeback_cache
) {
3407 fuse_write_update_size(inode_out
, pos_out
+ outarg
.size
);
3408 file_update_time(file_out
);
3411 fuse_invalidate_attr(inode_out
);
3416 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi_out
->state
);
3418 inode_unlock(inode_out
);
3419 file_accessed(file_in
);
3424 static ssize_t
fuse_copy_file_range(struct file
*src_file
, loff_t src_off
,
3425 struct file
*dst_file
, loff_t dst_off
,
3426 size_t len
, unsigned int flags
)
3430 ret
= __fuse_copy_file_range(src_file
, src_off
, dst_file
, dst_off
,
3433 if (ret
== -EOPNOTSUPP
|| ret
== -EXDEV
)
3434 ret
= generic_copy_file_range(src_file
, src_off
, dst_file
,
3435 dst_off
, len
, flags
);
3439 static const struct file_operations fuse_file_operations
= {
3440 .llseek
= fuse_file_llseek
,
3441 .read_iter
= fuse_file_read_iter
,
3442 .write_iter
= fuse_file_write_iter
,
3443 .mmap
= fuse_file_mmap
,
3445 .flush
= fuse_flush
,
3446 .release
= fuse_release
,
3447 .fsync
= fuse_fsync
,
3448 .lock
= fuse_file_lock
,
3449 .get_unmapped_area
= thp_get_unmapped_area
,
3450 .flock
= fuse_file_flock
,
3451 .splice_read
= generic_file_splice_read
,
3452 .splice_write
= iter_file_splice_write
,
3453 .unlocked_ioctl
= fuse_file_ioctl
,
3454 .compat_ioctl
= fuse_file_compat_ioctl
,
3455 .poll
= fuse_file_poll
,
3456 .fallocate
= fuse_file_fallocate
,
3457 .copy_file_range
= fuse_copy_file_range
,
3460 static const struct address_space_operations fuse_file_aops
= {
3461 .readpage
= fuse_readpage
,
3462 .readahead
= fuse_readahead
,
3463 .writepage
= fuse_writepage
,
3464 .writepages
= fuse_writepages
,
3465 .launder_page
= fuse_launder_page
,
3466 .set_page_dirty
= __set_page_dirty_nobuffers
,
3468 .direct_IO
= fuse_direct_IO
,
3469 .write_begin
= fuse_write_begin
,
3470 .write_end
= fuse_write_end
,
3473 void fuse_init_file_inode(struct inode
*inode
)
3475 struct fuse_inode
*fi
= get_fuse_inode(inode
);
3477 inode
->i_fop
= &fuse_file_operations
;
3478 inode
->i_data
.a_ops
= &fuse_file_aops
;
3480 INIT_LIST_HEAD(&fi
->write_files
);
3481 INIT_LIST_HEAD(&fi
->queued_writes
);
3483 init_waitqueue_head(&fi
->page_waitq
);
3484 fi
->writepages
= RB_ROOT
;
3486 if (IS_ENABLED(CONFIG_FUSE_DAX
))
3487 fuse_dax_inode_init(inode
);