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
;
46 if (fm
->fc
->handle_killpriv_v2
&&
47 (inarg
.flags
& O_TRUNC
) && !capable(CAP_FSETID
)) {
48 inarg
.open_flags
|= FUSE_OPEN_KILL_SUIDGID
;
54 args
.in_args
[0].size
= sizeof(inarg
);
55 args
.in_args
[0].value
= &inarg
;
57 args
.out_args
[0].size
= sizeof(*outargp
);
58 args
.out_args
[0].value
= outargp
;
60 return fuse_simple_request(fm
, &args
);
63 struct fuse_release_args
{
64 struct fuse_args args
;
65 struct fuse_release_in inarg
;
69 struct fuse_file
*fuse_file_alloc(struct fuse_mount
*fm
)
73 ff
= kzalloc(sizeof(struct fuse_file
), GFP_KERNEL_ACCOUNT
);
78 ff
->release_args
= kzalloc(sizeof(*ff
->release_args
),
80 if (!ff
->release_args
) {
85 INIT_LIST_HEAD(&ff
->write_entry
);
86 mutex_init(&ff
->readdir
.lock
);
87 refcount_set(&ff
->count
, 1);
88 RB_CLEAR_NODE(&ff
->polled_node
);
89 init_waitqueue_head(&ff
->poll_wait
);
91 ff
->kh
= atomic64_inc_return(&fm
->fc
->khctr
);
96 void fuse_file_free(struct fuse_file
*ff
)
98 kfree(ff
->release_args
);
99 mutex_destroy(&ff
->readdir
.lock
);
103 static struct fuse_file
*fuse_file_get(struct fuse_file
*ff
)
105 refcount_inc(&ff
->count
);
109 static void fuse_release_end(struct fuse_mount
*fm
, struct fuse_args
*args
,
112 struct fuse_release_args
*ra
= container_of(args
, typeof(*ra
), args
);
118 static void fuse_file_put(struct fuse_file
*ff
, bool sync
, bool isdir
)
120 if (refcount_dec_and_test(&ff
->count
)) {
121 struct fuse_args
*args
= &ff
->release_args
->args
;
123 if (isdir
? ff
->fm
->fc
->no_opendir
: ff
->fm
->fc
->no_open
) {
124 /* Do nothing when client does not implement 'open' */
125 fuse_release_end(ff
->fm
, args
, 0);
127 fuse_simple_request(ff
->fm
, args
);
128 fuse_release_end(ff
->fm
, args
, 0);
130 args
->end
= fuse_release_end
;
131 if (fuse_simple_background(ff
->fm
, args
,
132 GFP_KERNEL
| __GFP_NOFAIL
))
133 fuse_release_end(ff
->fm
, args
, -ENOTCONN
);
139 int fuse_do_open(struct fuse_mount
*fm
, u64 nodeid
, struct file
*file
,
142 struct fuse_conn
*fc
= fm
->fc
;
143 struct fuse_file
*ff
;
144 int opcode
= isdir
? FUSE_OPENDIR
: FUSE_OPEN
;
146 ff
= fuse_file_alloc(fm
);
151 /* Default for no-open */
152 ff
->open_flags
= FOPEN_KEEP_CACHE
| (isdir
? FOPEN_CACHE_DIR
: 0);
153 if (isdir
? !fc
->no_opendir
: !fc
->no_open
) {
154 struct fuse_open_out outarg
;
157 err
= fuse_send_open(fm
, nodeid
, file
, opcode
, &outarg
);
160 ff
->open_flags
= outarg
.open_flags
;
162 } else if (err
!= -ENOSYS
) {
174 ff
->open_flags
&= ~FOPEN_DIRECT_IO
;
177 file
->private_data
= ff
;
181 EXPORT_SYMBOL_GPL(fuse_do_open
);
183 static void fuse_link_write_file(struct file
*file
)
185 struct inode
*inode
= file_inode(file
);
186 struct fuse_inode
*fi
= get_fuse_inode(inode
);
187 struct fuse_file
*ff
= file
->private_data
;
189 * file may be written through mmap, so chain it onto the
190 * inodes's write_file list
192 spin_lock(&fi
->lock
);
193 if (list_empty(&ff
->write_entry
))
194 list_add(&ff
->write_entry
, &fi
->write_files
);
195 spin_unlock(&fi
->lock
);
198 void fuse_finish_open(struct inode
*inode
, struct file
*file
)
200 struct fuse_file
*ff
= file
->private_data
;
201 struct fuse_conn
*fc
= get_fuse_conn(inode
);
203 if (!(ff
->open_flags
& FOPEN_KEEP_CACHE
))
204 invalidate_inode_pages2(inode
->i_mapping
);
205 if (ff
->open_flags
& FOPEN_STREAM
)
206 stream_open(inode
, file
);
207 else if (ff
->open_flags
& FOPEN_NONSEEKABLE
)
208 nonseekable_open(inode
, file
);
209 if (fc
->atomic_o_trunc
&& (file
->f_flags
& O_TRUNC
)) {
210 struct fuse_inode
*fi
= get_fuse_inode(inode
);
212 spin_lock(&fi
->lock
);
213 fi
->attr_version
= atomic64_inc_return(&fc
->attr_version
);
214 i_size_write(inode
, 0);
215 spin_unlock(&fi
->lock
);
216 fuse_invalidate_attr(inode
);
217 if (fc
->writeback_cache
)
218 file_update_time(file
);
220 if ((file
->f_mode
& FMODE_WRITE
) && fc
->writeback_cache
)
221 fuse_link_write_file(file
);
224 int fuse_open_common(struct inode
*inode
, struct file
*file
, bool isdir
)
226 struct fuse_mount
*fm
= get_fuse_mount(inode
);
227 struct fuse_conn
*fc
= fm
->fc
;
229 bool is_wb_truncate
= (file
->f_flags
& O_TRUNC
) &&
230 fc
->atomic_o_trunc
&&
232 bool dax_truncate
= (file
->f_flags
& O_TRUNC
) &&
233 fc
->atomic_o_trunc
&& FUSE_IS_DAX(inode
);
235 if (fuse_is_bad(inode
))
238 err
= generic_file_open(inode
, file
);
242 if (is_wb_truncate
|| dax_truncate
) {
244 fuse_set_nowrite(inode
);
248 down_write(&get_fuse_inode(inode
)->i_mmap_sem
);
249 err
= fuse_dax_break_layouts(inode
, 0, 0);
254 err
= fuse_do_open(fm
, get_node_id(inode
), file
, isdir
);
256 fuse_finish_open(inode
, file
);
260 up_write(&get_fuse_inode(inode
)->i_mmap_sem
);
262 if (is_wb_truncate
| dax_truncate
) {
263 fuse_release_nowrite(inode
);
270 static void fuse_prepare_release(struct fuse_inode
*fi
, struct fuse_file
*ff
,
271 int flags
, int opcode
)
273 struct fuse_conn
*fc
= ff
->fm
->fc
;
274 struct fuse_release_args
*ra
= ff
->release_args
;
276 /* Inode is NULL on error path of fuse_create_open() */
278 spin_lock(&fi
->lock
);
279 list_del(&ff
->write_entry
);
280 spin_unlock(&fi
->lock
);
282 spin_lock(&fc
->lock
);
283 if (!RB_EMPTY_NODE(&ff
->polled_node
))
284 rb_erase(&ff
->polled_node
, &fc
->polled_files
);
285 spin_unlock(&fc
->lock
);
287 wake_up_interruptible_all(&ff
->poll_wait
);
289 ra
->inarg
.fh
= ff
->fh
;
290 ra
->inarg
.flags
= flags
;
291 ra
->args
.in_numargs
= 1;
292 ra
->args
.in_args
[0].size
= sizeof(struct fuse_release_in
);
293 ra
->args
.in_args
[0].value
= &ra
->inarg
;
294 ra
->args
.opcode
= opcode
;
295 ra
->args
.nodeid
= ff
->nodeid
;
296 ra
->args
.force
= true;
297 ra
->args
.nocreds
= true;
300 void fuse_release_common(struct file
*file
, bool isdir
)
302 struct fuse_inode
*fi
= get_fuse_inode(file_inode(file
));
303 struct fuse_file
*ff
= file
->private_data
;
304 struct fuse_release_args
*ra
= ff
->release_args
;
305 int opcode
= isdir
? FUSE_RELEASEDIR
: FUSE_RELEASE
;
307 fuse_prepare_release(fi
, ff
, file
->f_flags
, opcode
);
310 ra
->inarg
.release_flags
|= FUSE_RELEASE_FLOCK_UNLOCK
;
311 ra
->inarg
.lock_owner
= fuse_lock_owner_id(ff
->fm
->fc
,
314 /* Hold inode until release is finished */
315 ra
->inode
= igrab(file_inode(file
));
318 * Normally this will send the RELEASE request, however if
319 * some asynchronous READ or WRITE requests are outstanding,
320 * the sending will be delayed.
322 * Make the release synchronous if this is a fuseblk mount,
323 * synchronous RELEASE is allowed (and desirable) in this case
324 * because the server can be trusted not to screw up.
326 fuse_file_put(ff
, ff
->fm
->fc
->destroy
, isdir
);
329 static int fuse_open(struct inode
*inode
, struct file
*file
)
331 return fuse_open_common(inode
, file
, false);
334 static int fuse_release(struct inode
*inode
, struct file
*file
)
336 struct fuse_conn
*fc
= get_fuse_conn(inode
);
338 /* see fuse_vma_close() for !writeback_cache case */
339 if (fc
->writeback_cache
)
340 write_inode_now(inode
, 1);
342 fuse_release_common(file
, false);
344 /* return value is ignored by VFS */
348 void fuse_sync_release(struct fuse_inode
*fi
, struct fuse_file
*ff
, int flags
)
350 WARN_ON(refcount_read(&ff
->count
) > 1);
351 fuse_prepare_release(fi
, ff
, flags
, FUSE_RELEASE
);
353 * iput(NULL) is a no-op and since the refcount is 1 and everything's
354 * synchronous, we are fine with not doing igrab() here"
356 fuse_file_put(ff
, true, false);
358 EXPORT_SYMBOL_GPL(fuse_sync_release
);
361 * Scramble the ID space with XTEA, so that the value of the files_struct
362 * pointer is not exposed to userspace.
364 u64
fuse_lock_owner_id(struct fuse_conn
*fc
, fl_owner_t id
)
366 u32
*k
= fc
->scramble_key
;
367 u64 v
= (unsigned long) id
;
373 for (i
= 0; i
< 32; i
++) {
374 v0
+= ((v1
<< 4 ^ v1
>> 5) + v1
) ^ (sum
+ k
[sum
& 3]);
376 v1
+= ((v0
<< 4 ^ v0
>> 5) + v0
) ^ (sum
+ k
[sum
>>11 & 3]);
379 return (u64
) v0
+ ((u64
) v1
<< 32);
382 struct fuse_writepage_args
{
383 struct fuse_io_args ia
;
384 struct rb_node writepages_entry
;
385 struct list_head queue_entry
;
386 struct fuse_writepage_args
*next
;
390 static struct fuse_writepage_args
*fuse_find_writeback(struct fuse_inode
*fi
,
391 pgoff_t idx_from
, pgoff_t idx_to
)
395 n
= fi
->writepages
.rb_node
;
398 struct fuse_writepage_args
*wpa
;
401 wpa
= rb_entry(n
, struct fuse_writepage_args
, writepages_entry
);
402 WARN_ON(get_fuse_inode(wpa
->inode
) != fi
);
403 curr_index
= wpa
->ia
.write
.in
.offset
>> PAGE_SHIFT
;
404 if (idx_from
>= curr_index
+ wpa
->ia
.ap
.num_pages
)
406 else if (idx_to
< curr_index
)
415 * Check if any page in a range is under writeback
417 * This is currently done by walking the list of writepage requests
418 * for the inode, which can be pretty inefficient.
420 static bool fuse_range_is_writeback(struct inode
*inode
, pgoff_t idx_from
,
423 struct fuse_inode
*fi
= get_fuse_inode(inode
);
426 spin_lock(&fi
->lock
);
427 found
= fuse_find_writeback(fi
, idx_from
, idx_to
);
428 spin_unlock(&fi
->lock
);
433 static inline bool fuse_page_is_writeback(struct inode
*inode
, pgoff_t index
)
435 return fuse_range_is_writeback(inode
, index
, index
);
439 * Wait for page writeback to be completed.
441 * Since fuse doesn't rely on the VM writeback tracking, this has to
442 * use some other means.
444 static void fuse_wait_on_page_writeback(struct inode
*inode
, pgoff_t index
)
446 struct fuse_inode
*fi
= get_fuse_inode(inode
);
448 wait_event(fi
->page_waitq
, !fuse_page_is_writeback(inode
, index
));
452 * Wait for all pending writepages on the inode to finish.
454 * This is currently done by blocking further writes with FUSE_NOWRITE
455 * and waiting for all sent writes to complete.
457 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
458 * could conflict with truncation.
460 static void fuse_sync_writes(struct inode
*inode
)
462 fuse_set_nowrite(inode
);
463 fuse_release_nowrite(inode
);
466 static int fuse_flush(struct file
*file
, fl_owner_t id
)
468 struct inode
*inode
= file_inode(file
);
469 struct fuse_mount
*fm
= get_fuse_mount(inode
);
470 struct fuse_file
*ff
= file
->private_data
;
471 struct fuse_flush_in inarg
;
475 if (fuse_is_bad(inode
))
478 err
= write_inode_now(inode
, 1);
483 fuse_sync_writes(inode
);
486 err
= filemap_check_errors(file
->f_mapping
);
491 if (fm
->fc
->no_flush
)
494 memset(&inarg
, 0, sizeof(inarg
));
496 inarg
.lock_owner
= fuse_lock_owner_id(fm
->fc
, id
);
497 args
.opcode
= FUSE_FLUSH
;
498 args
.nodeid
= get_node_id(inode
);
500 args
.in_args
[0].size
= sizeof(inarg
);
501 args
.in_args
[0].value
= &inarg
;
504 err
= fuse_simple_request(fm
, &args
);
505 if (err
== -ENOSYS
) {
506 fm
->fc
->no_flush
= 1;
512 * In memory i_blocks is not maintained by fuse, if writeback cache is
513 * enabled, i_blocks from cached attr may not be accurate.
515 if (!err
&& fm
->fc
->writeback_cache
)
516 fuse_invalidate_attr(inode
);
520 int fuse_fsync_common(struct file
*file
, loff_t start
, loff_t end
,
521 int datasync
, int opcode
)
523 struct inode
*inode
= file
->f_mapping
->host
;
524 struct fuse_mount
*fm
= get_fuse_mount(inode
);
525 struct fuse_file
*ff
= file
->private_data
;
527 struct fuse_fsync_in inarg
;
529 memset(&inarg
, 0, sizeof(inarg
));
531 inarg
.fsync_flags
= datasync
? FUSE_FSYNC_FDATASYNC
: 0;
532 args
.opcode
= opcode
;
533 args
.nodeid
= get_node_id(inode
);
535 args
.in_args
[0].size
= sizeof(inarg
);
536 args
.in_args
[0].value
= &inarg
;
537 return fuse_simple_request(fm
, &args
);
540 static int fuse_fsync(struct file
*file
, loff_t start
, loff_t end
,
543 struct inode
*inode
= file
->f_mapping
->host
;
544 struct fuse_conn
*fc
= get_fuse_conn(inode
);
547 if (fuse_is_bad(inode
))
553 * Start writeback against all dirty pages of the inode, then
554 * wait for all outstanding writes, before sending the FSYNC
557 err
= file_write_and_wait_range(file
, start
, end
);
561 fuse_sync_writes(inode
);
564 * Due to implementation of fuse writeback
565 * file_write_and_wait_range() does not catch errors.
566 * We have to do this directly after fuse_sync_writes()
568 err
= file_check_and_advance_wb_err(file
);
572 err
= sync_inode_metadata(inode
, 1);
579 err
= fuse_fsync_common(file
, start
, end
, datasync
, FUSE_FSYNC
);
580 if (err
== -ENOSYS
) {
590 void fuse_read_args_fill(struct fuse_io_args
*ia
, struct file
*file
, loff_t pos
,
591 size_t count
, int opcode
)
593 struct fuse_file
*ff
= file
->private_data
;
594 struct fuse_args
*args
= &ia
->ap
.args
;
596 ia
->read
.in
.fh
= ff
->fh
;
597 ia
->read
.in
.offset
= pos
;
598 ia
->read
.in
.size
= count
;
599 ia
->read
.in
.flags
= file
->f_flags
;
600 args
->opcode
= opcode
;
601 args
->nodeid
= ff
->nodeid
;
602 args
->in_numargs
= 1;
603 args
->in_args
[0].size
= sizeof(ia
->read
.in
);
604 args
->in_args
[0].value
= &ia
->read
.in
;
605 args
->out_argvar
= true;
606 args
->out_numargs
= 1;
607 args
->out_args
[0].size
= count
;
610 static void fuse_release_user_pages(struct fuse_args_pages
*ap
,
615 for (i
= 0; i
< ap
->num_pages
; i
++) {
617 set_page_dirty_lock(ap
->pages
[i
]);
618 put_page(ap
->pages
[i
]);
622 static void fuse_io_release(struct kref
*kref
)
624 kfree(container_of(kref
, struct fuse_io_priv
, refcnt
));
627 static ssize_t
fuse_get_res_by_io(struct fuse_io_priv
*io
)
632 if (io
->bytes
>= 0 && io
->write
)
635 return io
->bytes
< 0 ? io
->size
: io
->bytes
;
639 * In case of short read, the caller sets 'pos' to the position of
640 * actual end of fuse request in IO request. Otherwise, if bytes_requested
641 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
644 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
645 * both submitted asynchronously. The first of them was ACKed by userspace as
646 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
647 * second request was ACKed as short, e.g. only 1K was read, resulting in
650 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
651 * will be equal to the length of the longest contiguous fragment of
652 * transferred data starting from the beginning of IO request.
654 static void fuse_aio_complete(struct fuse_io_priv
*io
, int err
, ssize_t pos
)
658 spin_lock(&io
->lock
);
660 io
->err
= io
->err
? : err
;
661 else if (pos
>= 0 && (io
->bytes
< 0 || pos
< io
->bytes
))
665 if (!left
&& io
->blocking
)
667 spin_unlock(&io
->lock
);
669 if (!left
&& !io
->blocking
) {
670 ssize_t res
= fuse_get_res_by_io(io
);
673 struct inode
*inode
= file_inode(io
->iocb
->ki_filp
);
674 struct fuse_conn
*fc
= get_fuse_conn(inode
);
675 struct fuse_inode
*fi
= get_fuse_inode(inode
);
677 spin_lock(&fi
->lock
);
678 fi
->attr_version
= atomic64_inc_return(&fc
->attr_version
);
679 spin_unlock(&fi
->lock
);
682 io
->iocb
->ki_complete(io
->iocb
, res
, 0);
685 kref_put(&io
->refcnt
, fuse_io_release
);
688 static struct fuse_io_args
*fuse_io_alloc(struct fuse_io_priv
*io
,
691 struct fuse_io_args
*ia
;
693 ia
= kzalloc(sizeof(*ia
), GFP_KERNEL
);
696 ia
->ap
.pages
= fuse_pages_alloc(npages
, GFP_KERNEL
,
706 static void fuse_io_free(struct fuse_io_args
*ia
)
712 static void fuse_aio_complete_req(struct fuse_mount
*fm
, struct fuse_args
*args
,
715 struct fuse_io_args
*ia
= container_of(args
, typeof(*ia
), ap
.args
);
716 struct fuse_io_priv
*io
= ia
->io
;
719 fuse_release_user_pages(&ia
->ap
, io
->should_dirty
);
723 } else if (io
->write
) {
724 if (ia
->write
.out
.size
> ia
->write
.in
.size
) {
726 } else if (ia
->write
.in
.size
!= ia
->write
.out
.size
) {
727 pos
= ia
->write
.in
.offset
- io
->offset
+
731 u32 outsize
= args
->out_args
[0].size
;
733 if (ia
->read
.in
.size
!= outsize
)
734 pos
= ia
->read
.in
.offset
- io
->offset
+ outsize
;
737 fuse_aio_complete(io
, err
, pos
);
741 static ssize_t
fuse_async_req_send(struct fuse_mount
*fm
,
742 struct fuse_io_args
*ia
, size_t num_bytes
)
745 struct fuse_io_priv
*io
= ia
->io
;
747 spin_lock(&io
->lock
);
748 kref_get(&io
->refcnt
);
749 io
->size
+= num_bytes
;
751 spin_unlock(&io
->lock
);
753 ia
->ap
.args
.end
= fuse_aio_complete_req
;
754 ia
->ap
.args
.may_block
= io
->should_dirty
;
755 err
= fuse_simple_background(fm
, &ia
->ap
.args
, GFP_KERNEL
);
757 fuse_aio_complete_req(fm
, &ia
->ap
.args
, err
);
762 static ssize_t
fuse_send_read(struct fuse_io_args
*ia
, loff_t pos
, size_t count
,
765 struct file
*file
= ia
->io
->iocb
->ki_filp
;
766 struct fuse_file
*ff
= file
->private_data
;
767 struct fuse_mount
*fm
= ff
->fm
;
769 fuse_read_args_fill(ia
, file
, pos
, count
, FUSE_READ
);
771 ia
->read
.in
.read_flags
|= FUSE_READ_LOCKOWNER
;
772 ia
->read
.in
.lock_owner
= fuse_lock_owner_id(fm
->fc
, owner
);
776 return fuse_async_req_send(fm
, ia
, count
);
778 return fuse_simple_request(fm
, &ia
->ap
.args
);
781 static void fuse_read_update_size(struct inode
*inode
, loff_t size
,
784 struct fuse_conn
*fc
= get_fuse_conn(inode
);
785 struct fuse_inode
*fi
= get_fuse_inode(inode
);
787 spin_lock(&fi
->lock
);
788 if (attr_ver
== fi
->attr_version
&& size
< inode
->i_size
&&
789 !test_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
)) {
790 fi
->attr_version
= atomic64_inc_return(&fc
->attr_version
);
791 i_size_write(inode
, size
);
793 spin_unlock(&fi
->lock
);
796 static void fuse_short_read(struct inode
*inode
, u64 attr_ver
, size_t num_read
,
797 struct fuse_args_pages
*ap
)
799 struct fuse_conn
*fc
= get_fuse_conn(inode
);
801 if (fc
->writeback_cache
) {
803 * A hole in a file. Some data after the hole are in page cache,
804 * but have not reached the client fs yet. So, the hole is not
808 int start_idx
= num_read
>> PAGE_SHIFT
;
809 size_t off
= num_read
& (PAGE_SIZE
- 1);
811 for (i
= start_idx
; i
< ap
->num_pages
; i
++) {
812 zero_user_segment(ap
->pages
[i
], off
, PAGE_SIZE
);
816 loff_t pos
= page_offset(ap
->pages
[0]) + num_read
;
817 fuse_read_update_size(inode
, pos
, attr_ver
);
821 static int fuse_do_readpage(struct file
*file
, struct page
*page
)
823 struct inode
*inode
= page
->mapping
->host
;
824 struct fuse_mount
*fm
= get_fuse_mount(inode
);
825 loff_t pos
= page_offset(page
);
826 struct fuse_page_desc desc
= { .length
= PAGE_SIZE
};
827 struct fuse_io_args ia
= {
828 .ap
.args
.page_zeroing
= true,
829 .ap
.args
.out_pages
= true,
838 * Page writeback can extend beyond the lifetime of the
839 * page-cache page, so make sure we read a properly synced
842 fuse_wait_on_page_writeback(inode
, page
->index
);
844 attr_ver
= fuse_get_attr_version(fm
->fc
);
846 /* Don't overflow end offset */
847 if (pos
+ (desc
.length
- 1) == LLONG_MAX
)
850 fuse_read_args_fill(&ia
, file
, pos
, desc
.length
, FUSE_READ
);
851 res
= fuse_simple_request(fm
, &ia
.ap
.args
);
855 * Short read means EOF. If file size is larger, truncate it
857 if (res
< desc
.length
)
858 fuse_short_read(inode
, attr_ver
, res
, &ia
.ap
);
860 SetPageUptodate(page
);
865 static int fuse_readpage(struct file
*file
, struct page
*page
)
867 struct inode
*inode
= page
->mapping
->host
;
871 if (fuse_is_bad(inode
))
874 err
= fuse_do_readpage(file
, page
);
875 fuse_invalidate_atime(inode
);
881 static void fuse_readpages_end(struct fuse_mount
*fm
, struct fuse_args
*args
,
885 struct fuse_io_args
*ia
= container_of(args
, typeof(*ia
), ap
.args
);
886 struct fuse_args_pages
*ap
= &ia
->ap
;
887 size_t count
= ia
->read
.in
.size
;
888 size_t num_read
= args
->out_args
[0].size
;
889 struct address_space
*mapping
= NULL
;
891 for (i
= 0; mapping
== NULL
&& i
< ap
->num_pages
; i
++)
892 mapping
= ap
->pages
[i
]->mapping
;
895 struct inode
*inode
= mapping
->host
;
898 * Short read means EOF. If file size is larger, truncate it
900 if (!err
&& num_read
< count
)
901 fuse_short_read(inode
, ia
->read
.attr_ver
, num_read
, ap
);
903 fuse_invalidate_atime(inode
);
906 for (i
= 0; i
< ap
->num_pages
; i
++) {
907 struct page
*page
= ap
->pages
[i
];
910 SetPageUptodate(page
);
917 fuse_file_put(ia
->ff
, false, false);
922 static void fuse_send_readpages(struct fuse_io_args
*ia
, struct file
*file
)
924 struct fuse_file
*ff
= file
->private_data
;
925 struct fuse_mount
*fm
= ff
->fm
;
926 struct fuse_args_pages
*ap
= &ia
->ap
;
927 loff_t pos
= page_offset(ap
->pages
[0]);
928 size_t count
= ap
->num_pages
<< PAGE_SHIFT
;
932 ap
->args
.out_pages
= true;
933 ap
->args
.page_zeroing
= true;
934 ap
->args
.page_replace
= true;
936 /* Don't overflow end offset */
937 if (pos
+ (count
- 1) == LLONG_MAX
) {
939 ap
->descs
[ap
->num_pages
- 1].length
--;
941 WARN_ON((loff_t
) (pos
+ count
) < 0);
943 fuse_read_args_fill(ia
, file
, pos
, count
, FUSE_READ
);
944 ia
->read
.attr_ver
= fuse_get_attr_version(fm
->fc
);
945 if (fm
->fc
->async_read
) {
946 ia
->ff
= fuse_file_get(ff
);
947 ap
->args
.end
= fuse_readpages_end
;
948 err
= fuse_simple_background(fm
, &ap
->args
, GFP_KERNEL
);
952 res
= fuse_simple_request(fm
, &ap
->args
);
953 err
= res
< 0 ? res
: 0;
955 fuse_readpages_end(fm
, &ap
->args
, err
);
958 static void fuse_readahead(struct readahead_control
*rac
)
960 struct inode
*inode
= rac
->mapping
->host
;
961 struct fuse_conn
*fc
= get_fuse_conn(inode
);
962 unsigned int i
, max_pages
, nr_pages
= 0;
964 if (fuse_is_bad(inode
))
967 max_pages
= min_t(unsigned int, fc
->max_pages
,
968 fc
->max_read
/ PAGE_SIZE
);
971 struct fuse_io_args
*ia
;
972 struct fuse_args_pages
*ap
;
974 nr_pages
= readahead_count(rac
) - nr_pages
;
975 if (nr_pages
> max_pages
)
976 nr_pages
= max_pages
;
979 ia
= fuse_io_alloc(NULL
, nr_pages
);
983 nr_pages
= __readahead_batch(rac
, ap
->pages
, nr_pages
);
984 for (i
= 0; i
< nr_pages
; i
++) {
985 fuse_wait_on_page_writeback(inode
,
986 readahead_index(rac
) + i
);
987 ap
->descs
[i
].length
= PAGE_SIZE
;
989 ap
->num_pages
= nr_pages
;
990 fuse_send_readpages(ia
, rac
->file
);
994 static ssize_t
fuse_cache_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
996 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
997 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1000 * In auto invalidate mode, always update attributes on read.
1001 * Otherwise, only update if we attempt to read past EOF (to ensure
1002 * i_size is up to date).
1004 if (fc
->auto_inval_data
||
1005 (iocb
->ki_pos
+ iov_iter_count(to
) > i_size_read(inode
))) {
1007 err
= fuse_update_attributes(inode
, iocb
->ki_filp
);
1012 return generic_file_read_iter(iocb
, to
);
1015 static void fuse_write_args_fill(struct fuse_io_args
*ia
, struct fuse_file
*ff
,
1016 loff_t pos
, size_t count
)
1018 struct fuse_args
*args
= &ia
->ap
.args
;
1020 ia
->write
.in
.fh
= ff
->fh
;
1021 ia
->write
.in
.offset
= pos
;
1022 ia
->write
.in
.size
= count
;
1023 args
->opcode
= FUSE_WRITE
;
1024 args
->nodeid
= ff
->nodeid
;
1025 args
->in_numargs
= 2;
1026 if (ff
->fm
->fc
->minor
< 9)
1027 args
->in_args
[0].size
= FUSE_COMPAT_WRITE_IN_SIZE
;
1029 args
->in_args
[0].size
= sizeof(ia
->write
.in
);
1030 args
->in_args
[0].value
= &ia
->write
.in
;
1031 args
->in_args
[1].size
= count
;
1032 args
->out_numargs
= 1;
1033 args
->out_args
[0].size
= sizeof(ia
->write
.out
);
1034 args
->out_args
[0].value
= &ia
->write
.out
;
1037 static unsigned int fuse_write_flags(struct kiocb
*iocb
)
1039 unsigned int flags
= iocb
->ki_filp
->f_flags
;
1041 if (iocb
->ki_flags
& IOCB_DSYNC
)
1043 if (iocb
->ki_flags
& IOCB_SYNC
)
1049 static ssize_t
fuse_send_write(struct fuse_io_args
*ia
, loff_t pos
,
1050 size_t count
, fl_owner_t owner
)
1052 struct kiocb
*iocb
= ia
->io
->iocb
;
1053 struct file
*file
= iocb
->ki_filp
;
1054 struct fuse_file
*ff
= file
->private_data
;
1055 struct fuse_mount
*fm
= ff
->fm
;
1056 struct fuse_write_in
*inarg
= &ia
->write
.in
;
1059 fuse_write_args_fill(ia
, ff
, pos
, count
);
1060 inarg
->flags
= fuse_write_flags(iocb
);
1061 if (owner
!= NULL
) {
1062 inarg
->write_flags
|= FUSE_WRITE_LOCKOWNER
;
1063 inarg
->lock_owner
= fuse_lock_owner_id(fm
->fc
, owner
);
1067 return fuse_async_req_send(fm
, ia
, count
);
1069 err
= fuse_simple_request(fm
, &ia
->ap
.args
);
1070 if (!err
&& ia
->write
.out
.size
> count
)
1073 return err
?: ia
->write
.out
.size
;
1076 bool fuse_write_update_size(struct inode
*inode
, loff_t pos
)
1078 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1079 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1082 spin_lock(&fi
->lock
);
1083 fi
->attr_version
= atomic64_inc_return(&fc
->attr_version
);
1084 if (pos
> inode
->i_size
) {
1085 i_size_write(inode
, pos
);
1088 spin_unlock(&fi
->lock
);
1093 static ssize_t
fuse_send_write_pages(struct fuse_io_args
*ia
,
1094 struct kiocb
*iocb
, struct inode
*inode
,
1095 loff_t pos
, size_t count
)
1097 struct fuse_args_pages
*ap
= &ia
->ap
;
1098 struct file
*file
= iocb
->ki_filp
;
1099 struct fuse_file
*ff
= file
->private_data
;
1100 struct fuse_mount
*fm
= ff
->fm
;
1101 unsigned int offset
, i
;
1104 for (i
= 0; i
< ap
->num_pages
; i
++)
1105 fuse_wait_on_page_writeback(inode
, ap
->pages
[i
]->index
);
1107 fuse_write_args_fill(ia
, ff
, pos
, count
);
1108 ia
->write
.in
.flags
= fuse_write_flags(iocb
);
1109 if (fm
->fc
->handle_killpriv_v2
&& !capable(CAP_FSETID
))
1110 ia
->write
.in
.write_flags
|= FUSE_WRITE_KILL_SUIDGID
;
1112 err
= fuse_simple_request(fm
, &ap
->args
);
1113 if (!err
&& ia
->write
.out
.size
> count
)
1116 offset
= ap
->descs
[0].offset
;
1117 count
= ia
->write
.out
.size
;
1118 for (i
= 0; i
< ap
->num_pages
; i
++) {
1119 struct page
*page
= ap
->pages
[i
];
1121 if (!err
&& !offset
&& count
>= PAGE_SIZE
)
1122 SetPageUptodate(page
);
1124 if (count
> PAGE_SIZE
- offset
)
1125 count
-= PAGE_SIZE
- offset
;
1137 static ssize_t
fuse_fill_write_pages(struct fuse_args_pages
*ap
,
1138 struct address_space
*mapping
,
1139 struct iov_iter
*ii
, loff_t pos
,
1140 unsigned int max_pages
)
1142 struct fuse_conn
*fc
= get_fuse_conn(mapping
->host
);
1143 unsigned offset
= pos
& (PAGE_SIZE
- 1);
1147 ap
->args
.in_pages
= true;
1148 ap
->descs
[0].offset
= offset
;
1153 pgoff_t index
= pos
>> PAGE_SHIFT
;
1154 size_t bytes
= min_t(size_t, PAGE_SIZE
- offset
,
1155 iov_iter_count(ii
));
1157 bytes
= min_t(size_t, bytes
, fc
->max_write
- count
);
1161 if (iov_iter_fault_in_readable(ii
, bytes
))
1165 page
= grab_cache_page_write_begin(mapping
, index
, 0);
1169 if (mapping_writably_mapped(mapping
))
1170 flush_dcache_page(page
);
1172 tmp
= iov_iter_copy_from_user_atomic(page
, ii
, offset
, bytes
);
1173 flush_dcache_page(page
);
1175 iov_iter_advance(ii
, tmp
);
1179 bytes
= min(bytes
, iov_iter_single_seg_count(ii
));
1184 ap
->pages
[ap
->num_pages
] = page
;
1185 ap
->descs
[ap
->num_pages
].length
= tmp
;
1191 if (offset
== PAGE_SIZE
)
1194 if (!fc
->big_writes
)
1196 } while (iov_iter_count(ii
) && count
< fc
->max_write
&&
1197 ap
->num_pages
< max_pages
&& offset
== 0);
1199 return count
> 0 ? count
: err
;
1202 static inline unsigned int fuse_wr_pages(loff_t pos
, size_t len
,
1203 unsigned int max_pages
)
1205 return min_t(unsigned int,
1206 ((pos
+ len
- 1) >> PAGE_SHIFT
) -
1207 (pos
>> PAGE_SHIFT
) + 1,
1211 static ssize_t
fuse_perform_write(struct kiocb
*iocb
,
1212 struct address_space
*mapping
,
1213 struct iov_iter
*ii
, loff_t pos
)
1215 struct inode
*inode
= mapping
->host
;
1216 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1217 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1221 if (inode
->i_size
< pos
+ iov_iter_count(ii
))
1222 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1226 struct fuse_io_args ia
= {};
1227 struct fuse_args_pages
*ap
= &ia
.ap
;
1228 unsigned int nr_pages
= fuse_wr_pages(pos
, iov_iter_count(ii
),
1231 ap
->pages
= fuse_pages_alloc(nr_pages
, GFP_KERNEL
, &ap
->descs
);
1237 count
= fuse_fill_write_pages(ap
, mapping
, ii
, pos
, nr_pages
);
1241 err
= fuse_send_write_pages(&ia
, iocb
, inode
,
1244 size_t num_written
= ia
.write
.out
.size
;
1249 /* break out of the loop on short write */
1250 if (num_written
!= count
)
1255 } while (!err
&& iov_iter_count(ii
));
1258 fuse_write_update_size(inode
, pos
);
1260 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1261 fuse_invalidate_attr(inode
);
1263 return res
> 0 ? res
: err
;
1266 static ssize_t
fuse_cache_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
1268 struct file
*file
= iocb
->ki_filp
;
1269 struct address_space
*mapping
= file
->f_mapping
;
1270 ssize_t written
= 0;
1271 ssize_t written_buffered
= 0;
1272 struct inode
*inode
= mapping
->host
;
1274 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1277 if (fc
->writeback_cache
) {
1278 /* Update size (EOF optimization) and mode (SUID clearing) */
1279 err
= fuse_update_attributes(mapping
->host
, file
);
1283 if (fc
->handle_killpriv_v2
&&
1284 should_remove_suid(file_dentry(file
))) {
1288 return generic_file_write_iter(iocb
, from
);
1294 /* We can write back this queue in page reclaim */
1295 current
->backing_dev_info
= inode_to_bdi(inode
);
1297 err
= generic_write_checks(iocb
, from
);
1301 err
= file_remove_privs(file
);
1305 err
= file_update_time(file
);
1309 if (iocb
->ki_flags
& IOCB_DIRECT
) {
1310 loff_t pos
= iocb
->ki_pos
;
1311 written
= generic_file_direct_write(iocb
, from
);
1312 if (written
< 0 || !iov_iter_count(from
))
1317 written_buffered
= fuse_perform_write(iocb
, mapping
, from
, pos
);
1318 if (written_buffered
< 0) {
1319 err
= written_buffered
;
1322 endbyte
= pos
+ written_buffered
- 1;
1324 err
= filemap_write_and_wait_range(file
->f_mapping
, pos
,
1329 invalidate_mapping_pages(file
->f_mapping
,
1331 endbyte
>> PAGE_SHIFT
);
1333 written
+= written_buffered
;
1334 iocb
->ki_pos
= pos
+ written_buffered
;
1336 written
= fuse_perform_write(iocb
, mapping
, from
, iocb
->ki_pos
);
1338 iocb
->ki_pos
+= written
;
1341 current
->backing_dev_info
= NULL
;
1342 inode_unlock(inode
);
1344 written
= generic_write_sync(iocb
, written
);
1346 return written
? written
: err
;
1349 static inline void fuse_page_descs_length_init(struct fuse_page_desc
*descs
,
1351 unsigned int nr_pages
)
1355 for (i
= index
; i
< index
+ nr_pages
; i
++)
1356 descs
[i
].length
= PAGE_SIZE
- descs
[i
].offset
;
1359 static inline unsigned long fuse_get_user_addr(const struct iov_iter
*ii
)
1361 return (unsigned long)ii
->iov
->iov_base
+ ii
->iov_offset
;
1364 static inline size_t fuse_get_frag_size(const struct iov_iter
*ii
,
1367 return min(iov_iter_single_seg_count(ii
), max_size
);
1370 static int fuse_get_user_pages(struct fuse_args_pages
*ap
, struct iov_iter
*ii
,
1371 size_t *nbytesp
, int write
,
1372 unsigned int max_pages
)
1374 size_t nbytes
= 0; /* # bytes already packed in req */
1377 /* Special case for kernel I/O: can copy directly into the buffer */
1378 if (iov_iter_is_kvec(ii
)) {
1379 unsigned long user_addr
= fuse_get_user_addr(ii
);
1380 size_t frag_size
= fuse_get_frag_size(ii
, *nbytesp
);
1383 ap
->args
.in_args
[1].value
= (void *) user_addr
;
1385 ap
->args
.out_args
[0].value
= (void *) user_addr
;
1387 iov_iter_advance(ii
, frag_size
);
1388 *nbytesp
= frag_size
;
1392 while (nbytes
< *nbytesp
&& ap
->num_pages
< max_pages
) {
1395 ret
= iov_iter_get_pages(ii
, &ap
->pages
[ap
->num_pages
],
1397 max_pages
- ap
->num_pages
,
1402 iov_iter_advance(ii
, ret
);
1406 npages
= (ret
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
1408 ap
->descs
[ap
->num_pages
].offset
= start
;
1409 fuse_page_descs_length_init(ap
->descs
, ap
->num_pages
, npages
);
1411 ap
->num_pages
+= npages
;
1412 ap
->descs
[ap
->num_pages
- 1].length
-=
1413 (PAGE_SIZE
- ret
) & (PAGE_SIZE
- 1);
1417 ap
->args
.in_pages
= true;
1419 ap
->args
.out_pages
= true;
1423 return ret
< 0 ? ret
: 0;
1426 ssize_t
fuse_direct_io(struct fuse_io_priv
*io
, struct iov_iter
*iter
,
1427 loff_t
*ppos
, int flags
)
1429 int write
= flags
& FUSE_DIO_WRITE
;
1430 int cuse
= flags
& FUSE_DIO_CUSE
;
1431 struct file
*file
= io
->iocb
->ki_filp
;
1432 struct inode
*inode
= file
->f_mapping
->host
;
1433 struct fuse_file
*ff
= file
->private_data
;
1434 struct fuse_conn
*fc
= ff
->fm
->fc
;
1435 size_t nmax
= write
? fc
->max_write
: fc
->max_read
;
1437 size_t count
= iov_iter_count(iter
);
1438 pgoff_t idx_from
= pos
>> PAGE_SHIFT
;
1439 pgoff_t idx_to
= (pos
+ count
- 1) >> PAGE_SHIFT
;
1442 struct fuse_io_args
*ia
;
1443 unsigned int max_pages
;
1445 max_pages
= iov_iter_npages(iter
, fc
->max_pages
);
1446 ia
= fuse_io_alloc(io
, max_pages
);
1451 if (!cuse
&& fuse_range_is_writeback(inode
, idx_from
, idx_to
)) {
1454 fuse_sync_writes(inode
);
1456 inode_unlock(inode
);
1459 io
->should_dirty
= !write
&& iter_is_iovec(iter
);
1462 fl_owner_t owner
= current
->files
;
1463 size_t nbytes
= min(count
, nmax
);
1465 err
= fuse_get_user_pages(&ia
->ap
, iter
, &nbytes
, write
,
1471 if (!capable(CAP_FSETID
))
1472 ia
->write
.in
.write_flags
|= FUSE_WRITE_KILL_SUIDGID
;
1474 nres
= fuse_send_write(ia
, pos
, nbytes
, owner
);
1476 nres
= fuse_send_read(ia
, pos
, nbytes
, owner
);
1479 if (!io
->async
|| nres
< 0) {
1480 fuse_release_user_pages(&ia
->ap
, io
->should_dirty
);
1485 iov_iter_revert(iter
, nbytes
);
1489 WARN_ON(nres
> nbytes
);
1494 if (nres
!= nbytes
) {
1495 iov_iter_revert(iter
, nbytes
- nres
);
1499 max_pages
= iov_iter_npages(iter
, fc
->max_pages
);
1500 ia
= fuse_io_alloc(io
, max_pages
);
1510 return res
> 0 ? res
: err
;
1512 EXPORT_SYMBOL_GPL(fuse_direct_io
);
1514 static ssize_t
__fuse_direct_read(struct fuse_io_priv
*io
,
1515 struct iov_iter
*iter
,
1519 struct inode
*inode
= file_inode(io
->iocb
->ki_filp
);
1521 res
= fuse_direct_io(io
, iter
, ppos
, 0);
1523 fuse_invalidate_atime(inode
);
1528 static ssize_t
fuse_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
);
1530 static ssize_t
fuse_direct_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
1534 if (!is_sync_kiocb(iocb
) && iocb
->ki_flags
& IOCB_DIRECT
) {
1535 res
= fuse_direct_IO(iocb
, to
);
1537 struct fuse_io_priv io
= FUSE_IO_PRIV_SYNC(iocb
);
1539 res
= __fuse_direct_read(&io
, to
, &iocb
->ki_pos
);
1545 static ssize_t
fuse_direct_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
1547 struct inode
*inode
= file_inode(iocb
->ki_filp
);
1548 struct fuse_io_priv io
= FUSE_IO_PRIV_SYNC(iocb
);
1551 /* Don't allow parallel writes to the same file */
1553 res
= generic_write_checks(iocb
, from
);
1555 if (!is_sync_kiocb(iocb
) && iocb
->ki_flags
& IOCB_DIRECT
) {
1556 res
= fuse_direct_IO(iocb
, from
);
1558 res
= fuse_direct_io(&io
, from
, &iocb
->ki_pos
,
1562 fuse_invalidate_attr(inode
);
1564 fuse_write_update_size(inode
, iocb
->ki_pos
);
1565 inode_unlock(inode
);
1570 static ssize_t
fuse_file_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
1572 struct file
*file
= iocb
->ki_filp
;
1573 struct fuse_file
*ff
= file
->private_data
;
1574 struct inode
*inode
= file_inode(file
);
1576 if (fuse_is_bad(inode
))
1579 if (FUSE_IS_DAX(inode
))
1580 return fuse_dax_read_iter(iocb
, to
);
1582 if (!(ff
->open_flags
& FOPEN_DIRECT_IO
))
1583 return fuse_cache_read_iter(iocb
, to
);
1585 return fuse_direct_read_iter(iocb
, to
);
1588 static ssize_t
fuse_file_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
1590 struct file
*file
= iocb
->ki_filp
;
1591 struct fuse_file
*ff
= file
->private_data
;
1592 struct inode
*inode
= file_inode(file
);
1594 if (fuse_is_bad(inode
))
1597 if (FUSE_IS_DAX(inode
))
1598 return fuse_dax_write_iter(iocb
, from
);
1600 if (!(ff
->open_flags
& FOPEN_DIRECT_IO
))
1601 return fuse_cache_write_iter(iocb
, from
);
1603 return fuse_direct_write_iter(iocb
, from
);
1606 static void fuse_writepage_free(struct fuse_writepage_args
*wpa
)
1608 struct fuse_args_pages
*ap
= &wpa
->ia
.ap
;
1611 for (i
= 0; i
< ap
->num_pages
; i
++)
1612 __free_page(ap
->pages
[i
]);
1615 fuse_file_put(wpa
->ia
.ff
, false, false);
1621 static void fuse_writepage_finish(struct fuse_mount
*fm
,
1622 struct fuse_writepage_args
*wpa
)
1624 struct fuse_args_pages
*ap
= &wpa
->ia
.ap
;
1625 struct inode
*inode
= wpa
->inode
;
1626 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1627 struct backing_dev_info
*bdi
= inode_to_bdi(inode
);
1630 for (i
= 0; i
< ap
->num_pages
; i
++) {
1631 dec_wb_stat(&bdi
->wb
, WB_WRITEBACK
);
1632 dec_node_page_state(ap
->pages
[i
], NR_WRITEBACK_TEMP
);
1633 wb_writeout_inc(&bdi
->wb
);
1635 wake_up(&fi
->page_waitq
);
1638 /* Called under fi->lock, may release and reacquire it */
1639 static void fuse_send_writepage(struct fuse_mount
*fm
,
1640 struct fuse_writepage_args
*wpa
, loff_t size
)
1641 __releases(fi
->lock
)
1642 __acquires(fi
->lock
)
1644 struct fuse_writepage_args
*aux
, *next
;
1645 struct fuse_inode
*fi
= get_fuse_inode(wpa
->inode
);
1646 struct fuse_write_in
*inarg
= &wpa
->ia
.write
.in
;
1647 struct fuse_args
*args
= &wpa
->ia
.ap
.args
;
1648 __u64 data_size
= wpa
->ia
.ap
.num_pages
* PAGE_SIZE
;
1652 if (inarg
->offset
+ data_size
<= size
) {
1653 inarg
->size
= data_size
;
1654 } else if (inarg
->offset
< size
) {
1655 inarg
->size
= size
- inarg
->offset
;
1657 /* Got truncated off completely */
1661 args
->in_args
[1].size
= inarg
->size
;
1663 args
->nocreds
= true;
1665 err
= fuse_simple_background(fm
, args
, GFP_ATOMIC
);
1666 if (err
== -ENOMEM
) {
1667 spin_unlock(&fi
->lock
);
1668 err
= fuse_simple_background(fm
, args
, GFP_NOFS
| __GFP_NOFAIL
);
1669 spin_lock(&fi
->lock
);
1672 /* Fails on broken connection only */
1680 rb_erase(&wpa
->writepages_entry
, &fi
->writepages
);
1681 fuse_writepage_finish(fm
, wpa
);
1682 spin_unlock(&fi
->lock
);
1684 /* After fuse_writepage_finish() aux request list is private */
1685 for (aux
= wpa
->next
; aux
; aux
= next
) {
1688 fuse_writepage_free(aux
);
1691 fuse_writepage_free(wpa
);
1692 spin_lock(&fi
->lock
);
1696 * If fi->writectr is positive (no truncate or fsync going on) send
1697 * all queued writepage requests.
1699 * Called with fi->lock
1701 void fuse_flush_writepages(struct inode
*inode
)
1702 __releases(fi
->lock
)
1703 __acquires(fi
->lock
)
1705 struct fuse_mount
*fm
= get_fuse_mount(inode
);
1706 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1707 loff_t crop
= i_size_read(inode
);
1708 struct fuse_writepage_args
*wpa
;
1710 while (fi
->writectr
>= 0 && !list_empty(&fi
->queued_writes
)) {
1711 wpa
= list_entry(fi
->queued_writes
.next
,
1712 struct fuse_writepage_args
, queue_entry
);
1713 list_del_init(&wpa
->queue_entry
);
1714 fuse_send_writepage(fm
, wpa
, crop
);
1718 static struct fuse_writepage_args
*fuse_insert_writeback(struct rb_root
*root
,
1719 struct fuse_writepage_args
*wpa
)
1721 pgoff_t idx_from
= wpa
->ia
.write
.in
.offset
>> PAGE_SHIFT
;
1722 pgoff_t idx_to
= idx_from
+ wpa
->ia
.ap
.num_pages
- 1;
1723 struct rb_node
**p
= &root
->rb_node
;
1724 struct rb_node
*parent
= NULL
;
1726 WARN_ON(!wpa
->ia
.ap
.num_pages
);
1728 struct fuse_writepage_args
*curr
;
1732 curr
= rb_entry(parent
, struct fuse_writepage_args
,
1734 WARN_ON(curr
->inode
!= wpa
->inode
);
1735 curr_index
= curr
->ia
.write
.in
.offset
>> PAGE_SHIFT
;
1737 if (idx_from
>= curr_index
+ curr
->ia
.ap
.num_pages
)
1738 p
= &(*p
)->rb_right
;
1739 else if (idx_to
< curr_index
)
1745 rb_link_node(&wpa
->writepages_entry
, parent
, p
);
1746 rb_insert_color(&wpa
->writepages_entry
, root
);
1750 static void tree_insert(struct rb_root
*root
, struct fuse_writepage_args
*wpa
)
1752 WARN_ON(fuse_insert_writeback(root
, wpa
));
1755 static void fuse_writepage_end(struct fuse_mount
*fm
, struct fuse_args
*args
,
1758 struct fuse_writepage_args
*wpa
=
1759 container_of(args
, typeof(*wpa
), ia
.ap
.args
);
1760 struct inode
*inode
= wpa
->inode
;
1761 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1763 mapping_set_error(inode
->i_mapping
, error
);
1764 spin_lock(&fi
->lock
);
1765 rb_erase(&wpa
->writepages_entry
, &fi
->writepages
);
1767 struct fuse_mount
*fm
= get_fuse_mount(inode
);
1768 struct fuse_write_in
*inarg
= &wpa
->ia
.write
.in
;
1769 struct fuse_writepage_args
*next
= wpa
->next
;
1771 wpa
->next
= next
->next
;
1773 next
->ia
.ff
= fuse_file_get(wpa
->ia
.ff
);
1774 tree_insert(&fi
->writepages
, next
);
1777 * Skip fuse_flush_writepages() to make it easy to crop requests
1778 * based on primary request size.
1780 * 1st case (trivial): there are no concurrent activities using
1781 * fuse_set/release_nowrite. Then we're on safe side because
1782 * fuse_flush_writepages() would call fuse_send_writepage()
1785 * 2nd case: someone called fuse_set_nowrite and it is waiting
1786 * now for completion of all in-flight requests. This happens
1787 * rarely and no more than once per page, so this should be
1790 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1791 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1792 * that fuse_set_nowrite returned implies that all in-flight
1793 * requests were completed along with all of their secondary
1794 * requests. Further primary requests are blocked by negative
1795 * writectr. Hence there cannot be any in-flight requests and
1796 * no invocations of fuse_writepage_end() while we're in
1797 * fuse_set_nowrite..fuse_release_nowrite section.
1799 fuse_send_writepage(fm
, next
, inarg
->offset
+ inarg
->size
);
1802 fuse_writepage_finish(fm
, wpa
);
1803 spin_unlock(&fi
->lock
);
1804 fuse_writepage_free(wpa
);
1807 static struct fuse_file
*__fuse_write_file_get(struct fuse_conn
*fc
,
1808 struct fuse_inode
*fi
)
1810 struct fuse_file
*ff
= NULL
;
1812 spin_lock(&fi
->lock
);
1813 if (!list_empty(&fi
->write_files
)) {
1814 ff
= list_entry(fi
->write_files
.next
, struct fuse_file
,
1818 spin_unlock(&fi
->lock
);
1823 static struct fuse_file
*fuse_write_file_get(struct fuse_conn
*fc
,
1824 struct fuse_inode
*fi
)
1826 struct fuse_file
*ff
= __fuse_write_file_get(fc
, fi
);
1831 int fuse_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
1833 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1834 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1835 struct fuse_file
*ff
;
1838 ff
= __fuse_write_file_get(fc
, fi
);
1839 err
= fuse_flush_times(inode
, ff
);
1841 fuse_file_put(ff
, false, false);
1846 static struct fuse_writepage_args
*fuse_writepage_args_alloc(void)
1848 struct fuse_writepage_args
*wpa
;
1849 struct fuse_args_pages
*ap
;
1851 wpa
= kzalloc(sizeof(*wpa
), GFP_NOFS
);
1855 ap
->pages
= fuse_pages_alloc(1, GFP_NOFS
, &ap
->descs
);
1865 static int fuse_writepage_locked(struct page
*page
)
1867 struct address_space
*mapping
= page
->mapping
;
1868 struct inode
*inode
= mapping
->host
;
1869 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1870 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1871 struct fuse_writepage_args
*wpa
;
1872 struct fuse_args_pages
*ap
;
1873 struct page
*tmp_page
;
1874 int error
= -ENOMEM
;
1876 set_page_writeback(page
);
1878 wpa
= fuse_writepage_args_alloc();
1883 tmp_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
1888 wpa
->ia
.ff
= fuse_write_file_get(fc
, fi
);
1892 fuse_write_args_fill(&wpa
->ia
, wpa
->ia
.ff
, page_offset(page
), 0);
1894 copy_highpage(tmp_page
, page
);
1895 wpa
->ia
.write
.in
.write_flags
|= FUSE_WRITE_CACHE
;
1897 ap
->args
.in_pages
= true;
1899 ap
->pages
[0] = tmp_page
;
1900 ap
->descs
[0].offset
= 0;
1901 ap
->descs
[0].length
= PAGE_SIZE
;
1902 ap
->args
.end
= fuse_writepage_end
;
1905 inc_wb_stat(&inode_to_bdi(inode
)->wb
, WB_WRITEBACK
);
1906 inc_node_page_state(tmp_page
, NR_WRITEBACK_TEMP
);
1908 spin_lock(&fi
->lock
);
1909 tree_insert(&fi
->writepages
, wpa
);
1910 list_add_tail(&wpa
->queue_entry
, &fi
->queued_writes
);
1911 fuse_flush_writepages(inode
);
1912 spin_unlock(&fi
->lock
);
1914 end_page_writeback(page
);
1919 __free_page(tmp_page
);
1923 mapping_set_error(page
->mapping
, error
);
1924 end_page_writeback(page
);
1928 static int fuse_writepage(struct page
*page
, struct writeback_control
*wbc
)
1932 if (fuse_page_is_writeback(page
->mapping
->host
, page
->index
)) {
1934 * ->writepages() should be called for sync() and friends. We
1935 * should only get here on direct reclaim and then we are
1936 * allowed to skip a page which is already in flight
1938 WARN_ON(wbc
->sync_mode
== WB_SYNC_ALL
);
1940 redirty_page_for_writepage(wbc
, page
);
1945 err
= fuse_writepage_locked(page
);
1951 struct fuse_fill_wb_data
{
1952 struct fuse_writepage_args
*wpa
;
1953 struct fuse_file
*ff
;
1954 struct inode
*inode
;
1955 struct page
**orig_pages
;
1956 unsigned int max_pages
;
1959 static bool fuse_pages_realloc(struct fuse_fill_wb_data
*data
)
1961 struct fuse_args_pages
*ap
= &data
->wpa
->ia
.ap
;
1962 struct fuse_conn
*fc
= get_fuse_conn(data
->inode
);
1963 struct page
**pages
;
1964 struct fuse_page_desc
*descs
;
1965 unsigned int npages
= min_t(unsigned int,
1966 max_t(unsigned int, data
->max_pages
* 2,
1967 FUSE_DEFAULT_MAX_PAGES_PER_REQ
),
1969 WARN_ON(npages
<= data
->max_pages
);
1971 pages
= fuse_pages_alloc(npages
, GFP_NOFS
, &descs
);
1975 memcpy(pages
, ap
->pages
, sizeof(struct page
*) * ap
->num_pages
);
1976 memcpy(descs
, ap
->descs
, sizeof(struct fuse_page_desc
) * ap
->num_pages
);
1980 data
->max_pages
= npages
;
1985 static void fuse_writepages_send(struct fuse_fill_wb_data
*data
)
1987 struct fuse_writepage_args
*wpa
= data
->wpa
;
1988 struct inode
*inode
= data
->inode
;
1989 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1990 int num_pages
= wpa
->ia
.ap
.num_pages
;
1993 wpa
->ia
.ff
= fuse_file_get(data
->ff
);
1994 spin_lock(&fi
->lock
);
1995 list_add_tail(&wpa
->queue_entry
, &fi
->queued_writes
);
1996 fuse_flush_writepages(inode
);
1997 spin_unlock(&fi
->lock
);
1999 for (i
= 0; i
< num_pages
; i
++)
2000 end_page_writeback(data
->orig_pages
[i
]);
2004 * Check under fi->lock if the page is under writeback, and insert it onto the
2005 * rb_tree if not. Otherwise iterate auxiliary write requests, to see if there's
2006 * one already added for a page at this offset. If there's none, then insert
2007 * this new request onto the auxiliary list, otherwise reuse the existing one by
2008 * swapping the new temp page with the old one.
2010 static bool fuse_writepage_add(struct fuse_writepage_args
*new_wpa
,
2013 struct fuse_inode
*fi
= get_fuse_inode(new_wpa
->inode
);
2014 struct fuse_writepage_args
*tmp
;
2015 struct fuse_writepage_args
*old_wpa
;
2016 struct fuse_args_pages
*new_ap
= &new_wpa
->ia
.ap
;
2018 WARN_ON(new_ap
->num_pages
!= 0);
2019 new_ap
->num_pages
= 1;
2021 spin_lock(&fi
->lock
);
2022 old_wpa
= fuse_insert_writeback(&fi
->writepages
, new_wpa
);
2024 spin_unlock(&fi
->lock
);
2028 for (tmp
= old_wpa
->next
; tmp
; tmp
= tmp
->next
) {
2031 WARN_ON(tmp
->inode
!= new_wpa
->inode
);
2032 curr_index
= tmp
->ia
.write
.in
.offset
>> PAGE_SHIFT
;
2033 if (curr_index
== page
->index
) {
2034 WARN_ON(tmp
->ia
.ap
.num_pages
!= 1);
2035 swap(tmp
->ia
.ap
.pages
[0], new_ap
->pages
[0]);
2041 new_wpa
->next
= old_wpa
->next
;
2042 old_wpa
->next
= new_wpa
;
2045 spin_unlock(&fi
->lock
);
2048 struct backing_dev_info
*bdi
= inode_to_bdi(new_wpa
->inode
);
2050 dec_wb_stat(&bdi
->wb
, WB_WRITEBACK
);
2051 dec_node_page_state(new_ap
->pages
[0], NR_WRITEBACK_TEMP
);
2052 wb_writeout_inc(&bdi
->wb
);
2053 fuse_writepage_free(new_wpa
);
2059 static bool fuse_writepage_need_send(struct fuse_conn
*fc
, struct page
*page
,
2060 struct fuse_args_pages
*ap
,
2061 struct fuse_fill_wb_data
*data
)
2063 WARN_ON(!ap
->num_pages
);
2066 * Being under writeback is unlikely but possible. For example direct
2067 * read to an mmaped fuse file will set the page dirty twice; once when
2068 * the pages are faulted with get_user_pages(), and then after the read
2071 if (fuse_page_is_writeback(data
->inode
, page
->index
))
2074 /* Reached max pages */
2075 if (ap
->num_pages
== fc
->max_pages
)
2078 /* Reached max write bytes */
2079 if ((ap
->num_pages
+ 1) * PAGE_SIZE
> fc
->max_write
)
2083 if (data
->orig_pages
[ap
->num_pages
- 1]->index
+ 1 != page
->index
)
2086 /* Need to grow the pages array? If so, did the expansion fail? */
2087 if (ap
->num_pages
== data
->max_pages
&& !fuse_pages_realloc(data
))
2093 static int fuse_writepages_fill(struct page
*page
,
2094 struct writeback_control
*wbc
, void *_data
)
2096 struct fuse_fill_wb_data
*data
= _data
;
2097 struct fuse_writepage_args
*wpa
= data
->wpa
;
2098 struct fuse_args_pages
*ap
= &wpa
->ia
.ap
;
2099 struct inode
*inode
= data
->inode
;
2100 struct fuse_inode
*fi
= get_fuse_inode(inode
);
2101 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2102 struct page
*tmp_page
;
2107 data
->ff
= fuse_write_file_get(fc
, fi
);
2112 if (wpa
&& fuse_writepage_need_send(fc
, page
, ap
, data
)) {
2113 fuse_writepages_send(data
);
2118 tmp_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
2123 * The page must not be redirtied until the writeout is completed
2124 * (i.e. userspace has sent a reply to the write request). Otherwise
2125 * there could be more than one temporary page instance for each real
2128 * This is ensured by holding the page lock in page_mkwrite() while
2129 * checking fuse_page_is_writeback(). We already hold the page lock
2130 * since clear_page_dirty_for_io() and keep it held until we add the
2131 * request to the fi->writepages list and increment ap->num_pages.
2132 * After this fuse_page_is_writeback() will indicate that the page is
2133 * under writeback, so we can release the page lock.
2135 if (data
->wpa
== NULL
) {
2137 wpa
= fuse_writepage_args_alloc();
2139 __free_page(tmp_page
);
2142 data
->max_pages
= 1;
2145 fuse_write_args_fill(&wpa
->ia
, data
->ff
, page_offset(page
), 0);
2146 wpa
->ia
.write
.in
.write_flags
|= FUSE_WRITE_CACHE
;
2148 ap
->args
.in_pages
= true;
2149 ap
->args
.end
= fuse_writepage_end
;
2153 set_page_writeback(page
);
2155 copy_highpage(tmp_page
, page
);
2156 ap
->pages
[ap
->num_pages
] = tmp_page
;
2157 ap
->descs
[ap
->num_pages
].offset
= 0;
2158 ap
->descs
[ap
->num_pages
].length
= PAGE_SIZE
;
2159 data
->orig_pages
[ap
->num_pages
] = page
;
2161 inc_wb_stat(&inode_to_bdi(inode
)->wb
, WB_WRITEBACK
);
2162 inc_node_page_state(tmp_page
, NR_WRITEBACK_TEMP
);
2167 * Protected by fi->lock against concurrent access by
2168 * fuse_page_is_writeback().
2170 spin_lock(&fi
->lock
);
2172 spin_unlock(&fi
->lock
);
2173 } else if (fuse_writepage_add(wpa
, page
)) {
2176 end_page_writeback(page
);
2184 static int fuse_writepages(struct address_space
*mapping
,
2185 struct writeback_control
*wbc
)
2187 struct inode
*inode
= mapping
->host
;
2188 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2189 struct fuse_fill_wb_data data
;
2193 if (fuse_is_bad(inode
))
2201 data
.orig_pages
= kcalloc(fc
->max_pages
,
2202 sizeof(struct page
*),
2204 if (!data
.orig_pages
)
2207 err
= write_cache_pages(mapping
, wbc
, fuse_writepages_fill
, &data
);
2209 WARN_ON(!data
.wpa
->ia
.ap
.num_pages
);
2210 fuse_writepages_send(&data
);
2213 fuse_file_put(data
.ff
, false, false);
2215 kfree(data
.orig_pages
);
2221 * It's worthy to make sure that space is reserved on disk for the write,
2222 * but how to implement it without killing performance need more thinking.
2224 static int fuse_write_begin(struct file
*file
, struct address_space
*mapping
,
2225 loff_t pos
, unsigned len
, unsigned flags
,
2226 struct page
**pagep
, void **fsdata
)
2228 pgoff_t index
= pos
>> PAGE_SHIFT
;
2229 struct fuse_conn
*fc
= get_fuse_conn(file_inode(file
));
2234 WARN_ON(!fc
->writeback_cache
);
2236 page
= grab_cache_page_write_begin(mapping
, index
, flags
);
2240 fuse_wait_on_page_writeback(mapping
->host
, page
->index
);
2242 if (PageUptodate(page
) || len
== PAGE_SIZE
)
2245 * Check if the start this page comes after the end of file, in which
2246 * case the readpage can be optimized away.
2248 fsize
= i_size_read(mapping
->host
);
2249 if (fsize
<= (pos
& PAGE_MASK
)) {
2250 size_t off
= pos
& ~PAGE_MASK
;
2252 zero_user_segment(page
, 0, off
);
2255 err
= fuse_do_readpage(file
, page
);
2269 static int fuse_write_end(struct file
*file
, struct address_space
*mapping
,
2270 loff_t pos
, unsigned len
, unsigned copied
,
2271 struct page
*page
, void *fsdata
)
2273 struct inode
*inode
= page
->mapping
->host
;
2275 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2279 if (!PageUptodate(page
)) {
2280 /* Zero any unwritten bytes at the end of the page */
2281 size_t endoff
= (pos
+ copied
) & ~PAGE_MASK
;
2283 zero_user_segment(page
, endoff
, PAGE_SIZE
);
2284 SetPageUptodate(page
);
2287 fuse_write_update_size(inode
, pos
+ copied
);
2288 set_page_dirty(page
);
2297 static int fuse_launder_page(struct page
*page
)
2300 if (clear_page_dirty_for_io(page
)) {
2301 struct inode
*inode
= page
->mapping
->host
;
2303 /* Serialize with pending writeback for the same page */
2304 fuse_wait_on_page_writeback(inode
, page
->index
);
2305 err
= fuse_writepage_locked(page
);
2307 fuse_wait_on_page_writeback(inode
, page
->index
);
2313 * Write back dirty pages now, because there may not be any suitable
2316 static void fuse_vma_close(struct vm_area_struct
*vma
)
2318 filemap_write_and_wait(vma
->vm_file
->f_mapping
);
2322 * Wait for writeback against this page to complete before allowing it
2323 * to be marked dirty again, and hence written back again, possibly
2324 * before the previous writepage completed.
2326 * Block here, instead of in ->writepage(), so that the userspace fs
2327 * can only block processes actually operating on the filesystem.
2329 * Otherwise unprivileged userspace fs would be able to block
2334 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2336 static vm_fault_t
fuse_page_mkwrite(struct vm_fault
*vmf
)
2338 struct page
*page
= vmf
->page
;
2339 struct inode
*inode
= file_inode(vmf
->vma
->vm_file
);
2341 file_update_time(vmf
->vma
->vm_file
);
2343 if (page
->mapping
!= inode
->i_mapping
) {
2345 return VM_FAULT_NOPAGE
;
2348 fuse_wait_on_page_writeback(inode
, page
->index
);
2349 return VM_FAULT_LOCKED
;
2352 static const struct vm_operations_struct fuse_file_vm_ops
= {
2353 .close
= fuse_vma_close
,
2354 .fault
= filemap_fault
,
2355 .map_pages
= filemap_map_pages
,
2356 .page_mkwrite
= fuse_page_mkwrite
,
2359 static int fuse_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2361 struct fuse_file
*ff
= file
->private_data
;
2363 /* DAX mmap is superior to direct_io mmap */
2364 if (FUSE_IS_DAX(file_inode(file
)))
2365 return fuse_dax_mmap(file
, vma
);
2367 if (ff
->open_flags
& FOPEN_DIRECT_IO
) {
2368 /* Can't provide the coherency needed for MAP_SHARED */
2369 if (vma
->vm_flags
& VM_MAYSHARE
)
2372 invalidate_inode_pages2(file
->f_mapping
);
2374 return generic_file_mmap(file
, vma
);
2377 if ((vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
))
2378 fuse_link_write_file(file
);
2380 file_accessed(file
);
2381 vma
->vm_ops
= &fuse_file_vm_ops
;
2385 static int convert_fuse_file_lock(struct fuse_conn
*fc
,
2386 const struct fuse_file_lock
*ffl
,
2387 struct file_lock
*fl
)
2389 switch (ffl
->type
) {
2395 if (ffl
->start
> OFFSET_MAX
|| ffl
->end
> OFFSET_MAX
||
2396 ffl
->end
< ffl
->start
)
2399 fl
->fl_start
= ffl
->start
;
2400 fl
->fl_end
= ffl
->end
;
2403 * Convert pid into init's pid namespace. The locks API will
2404 * translate it into the caller's pid namespace.
2407 fl
->fl_pid
= pid_nr_ns(find_pid_ns(ffl
->pid
, fc
->pid_ns
), &init_pid_ns
);
2414 fl
->fl_type
= ffl
->type
;
2418 static void fuse_lk_fill(struct fuse_args
*args
, struct file
*file
,
2419 const struct file_lock
*fl
, int opcode
, pid_t pid
,
2420 int flock
, struct fuse_lk_in
*inarg
)
2422 struct inode
*inode
= file_inode(file
);
2423 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2424 struct fuse_file
*ff
= file
->private_data
;
2426 memset(inarg
, 0, sizeof(*inarg
));
2428 inarg
->owner
= fuse_lock_owner_id(fc
, fl
->fl_owner
);
2429 inarg
->lk
.start
= fl
->fl_start
;
2430 inarg
->lk
.end
= fl
->fl_end
;
2431 inarg
->lk
.type
= fl
->fl_type
;
2432 inarg
->lk
.pid
= pid
;
2434 inarg
->lk_flags
|= FUSE_LK_FLOCK
;
2435 args
->opcode
= opcode
;
2436 args
->nodeid
= get_node_id(inode
);
2437 args
->in_numargs
= 1;
2438 args
->in_args
[0].size
= sizeof(*inarg
);
2439 args
->in_args
[0].value
= inarg
;
2442 static int fuse_getlk(struct file
*file
, struct file_lock
*fl
)
2444 struct inode
*inode
= file_inode(file
);
2445 struct fuse_mount
*fm
= get_fuse_mount(inode
);
2447 struct fuse_lk_in inarg
;
2448 struct fuse_lk_out outarg
;
2451 fuse_lk_fill(&args
, file
, fl
, FUSE_GETLK
, 0, 0, &inarg
);
2452 args
.out_numargs
= 1;
2453 args
.out_args
[0].size
= sizeof(outarg
);
2454 args
.out_args
[0].value
= &outarg
;
2455 err
= fuse_simple_request(fm
, &args
);
2457 err
= convert_fuse_file_lock(fm
->fc
, &outarg
.lk
, fl
);
2462 static int fuse_setlk(struct file
*file
, struct file_lock
*fl
, int flock
)
2464 struct inode
*inode
= file_inode(file
);
2465 struct fuse_mount
*fm
= get_fuse_mount(inode
);
2467 struct fuse_lk_in inarg
;
2468 int opcode
= (fl
->fl_flags
& FL_SLEEP
) ? FUSE_SETLKW
: FUSE_SETLK
;
2469 struct pid
*pid
= fl
->fl_type
!= F_UNLCK
? task_tgid(current
) : NULL
;
2470 pid_t pid_nr
= pid_nr_ns(pid
, fm
->fc
->pid_ns
);
2473 if (fl
->fl_lmops
&& fl
->fl_lmops
->lm_grant
) {
2474 /* NLM needs asynchronous locks, which we don't support yet */
2478 /* Unlock on close is handled by the flush method */
2479 if ((fl
->fl_flags
& FL_CLOSE_POSIX
) == FL_CLOSE_POSIX
)
2482 fuse_lk_fill(&args
, file
, fl
, opcode
, pid_nr
, flock
, &inarg
);
2483 err
= fuse_simple_request(fm
, &args
);
2485 /* locking is restartable */
2492 static int fuse_file_lock(struct file
*file
, int cmd
, struct file_lock
*fl
)
2494 struct inode
*inode
= file_inode(file
);
2495 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2498 if (cmd
== F_CANCELLK
) {
2500 } else if (cmd
== F_GETLK
) {
2502 posix_test_lock(file
, fl
);
2505 err
= fuse_getlk(file
, fl
);
2508 err
= posix_lock_file(file
, fl
, NULL
);
2510 err
= fuse_setlk(file
, fl
, 0);
2515 static int fuse_file_flock(struct file
*file
, int cmd
, struct file_lock
*fl
)
2517 struct inode
*inode
= file_inode(file
);
2518 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2522 err
= locks_lock_file_wait(file
, fl
);
2524 struct fuse_file
*ff
= file
->private_data
;
2526 /* emulate flock with POSIX locks */
2528 err
= fuse_setlk(file
, fl
, 1);
2534 static sector_t
fuse_bmap(struct address_space
*mapping
, sector_t block
)
2536 struct inode
*inode
= mapping
->host
;
2537 struct fuse_mount
*fm
= get_fuse_mount(inode
);
2539 struct fuse_bmap_in inarg
;
2540 struct fuse_bmap_out outarg
;
2543 if (!inode
->i_sb
->s_bdev
|| fm
->fc
->no_bmap
)
2546 memset(&inarg
, 0, sizeof(inarg
));
2547 inarg
.block
= block
;
2548 inarg
.blocksize
= inode
->i_sb
->s_blocksize
;
2549 args
.opcode
= FUSE_BMAP
;
2550 args
.nodeid
= get_node_id(inode
);
2551 args
.in_numargs
= 1;
2552 args
.in_args
[0].size
= sizeof(inarg
);
2553 args
.in_args
[0].value
= &inarg
;
2554 args
.out_numargs
= 1;
2555 args
.out_args
[0].size
= sizeof(outarg
);
2556 args
.out_args
[0].value
= &outarg
;
2557 err
= fuse_simple_request(fm
, &args
);
2559 fm
->fc
->no_bmap
= 1;
2561 return err
? 0 : outarg
.block
;
2564 static loff_t
fuse_lseek(struct file
*file
, loff_t offset
, int whence
)
2566 struct inode
*inode
= file
->f_mapping
->host
;
2567 struct fuse_mount
*fm
= get_fuse_mount(inode
);
2568 struct fuse_file
*ff
= file
->private_data
;
2570 struct fuse_lseek_in inarg
= {
2575 struct fuse_lseek_out outarg
;
2578 if (fm
->fc
->no_lseek
)
2581 args
.opcode
= FUSE_LSEEK
;
2582 args
.nodeid
= ff
->nodeid
;
2583 args
.in_numargs
= 1;
2584 args
.in_args
[0].size
= sizeof(inarg
);
2585 args
.in_args
[0].value
= &inarg
;
2586 args
.out_numargs
= 1;
2587 args
.out_args
[0].size
= sizeof(outarg
);
2588 args
.out_args
[0].value
= &outarg
;
2589 err
= fuse_simple_request(fm
, &args
);
2591 if (err
== -ENOSYS
) {
2592 fm
->fc
->no_lseek
= 1;
2598 return vfs_setpos(file
, outarg
.offset
, inode
->i_sb
->s_maxbytes
);
2601 err
= fuse_update_attributes(inode
, file
);
2603 return generic_file_llseek(file
, offset
, whence
);
2608 static loff_t
fuse_file_llseek(struct file
*file
, loff_t offset
, int whence
)
2611 struct inode
*inode
= file_inode(file
);
2616 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2617 retval
= generic_file_llseek(file
, offset
, whence
);
2621 retval
= fuse_update_attributes(inode
, file
);
2623 retval
= generic_file_llseek(file
, offset
, whence
);
2624 inode_unlock(inode
);
2629 retval
= fuse_lseek(file
, offset
, whence
);
2630 inode_unlock(inode
);
2640 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2641 * ABI was defined to be 'struct iovec' which is different on 32bit
2642 * and 64bit. Fortunately we can determine which structure the server
2643 * used from the size of the reply.
2645 static int fuse_copy_ioctl_iovec_old(struct iovec
*dst
, void *src
,
2646 size_t transferred
, unsigned count
,
2649 #ifdef CONFIG_COMPAT
2650 if (count
* sizeof(struct compat_iovec
) == transferred
) {
2651 struct compat_iovec
*ciov
= src
;
2655 * With this interface a 32bit server cannot support
2656 * non-compat (i.e. ones coming from 64bit apps) ioctl
2662 for (i
= 0; i
< count
; i
++) {
2663 dst
[i
].iov_base
= compat_ptr(ciov
[i
].iov_base
);
2664 dst
[i
].iov_len
= ciov
[i
].iov_len
;
2670 if (count
* sizeof(struct iovec
) != transferred
)
2673 memcpy(dst
, src
, transferred
);
2677 /* Make sure iov_length() won't overflow */
2678 static int fuse_verify_ioctl_iov(struct fuse_conn
*fc
, struct iovec
*iov
,
2682 u32 max
= fc
->max_pages
<< PAGE_SHIFT
;
2684 for (n
= 0; n
< count
; n
++, iov
++) {
2685 if (iov
->iov_len
> (size_t) max
)
2687 max
-= iov
->iov_len
;
2692 static int fuse_copy_ioctl_iovec(struct fuse_conn
*fc
, struct iovec
*dst
,
2693 void *src
, size_t transferred
, unsigned count
,
2697 struct fuse_ioctl_iovec
*fiov
= src
;
2699 if (fc
->minor
< 16) {
2700 return fuse_copy_ioctl_iovec_old(dst
, src
, transferred
,
2704 if (count
* sizeof(struct fuse_ioctl_iovec
) != transferred
)
2707 for (i
= 0; i
< count
; i
++) {
2708 /* Did the server supply an inappropriate value? */
2709 if (fiov
[i
].base
!= (unsigned long) fiov
[i
].base
||
2710 fiov
[i
].len
!= (unsigned long) fiov
[i
].len
)
2713 dst
[i
].iov_base
= (void __user
*) (unsigned long) fiov
[i
].base
;
2714 dst
[i
].iov_len
= (size_t) fiov
[i
].len
;
2716 #ifdef CONFIG_COMPAT
2718 (ptr_to_compat(dst
[i
].iov_base
) != fiov
[i
].base
||
2719 (compat_size_t
) dst
[i
].iov_len
!= fiov
[i
].len
))
2729 * For ioctls, there is no generic way to determine how much memory
2730 * needs to be read and/or written. Furthermore, ioctls are allowed
2731 * to dereference the passed pointer, so the parameter requires deep
2732 * copying but FUSE has no idea whatsoever about what to copy in or
2735 * This is solved by allowing FUSE server to retry ioctl with
2736 * necessary in/out iovecs. Let's assume the ioctl implementation
2737 * needs to read in the following structure.
2744 * On the first callout to FUSE server, inarg->in_size and
2745 * inarg->out_size will be NULL; then, the server completes the ioctl
2746 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2747 * the actual iov array to
2749 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2751 * which tells FUSE to copy in the requested area and retry the ioctl.
2752 * On the second round, the server has access to the structure and
2753 * from that it can tell what to look for next, so on the invocation,
2754 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2756 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2757 * { .iov_base = a.buf, .iov_len = a.buflen } }
2759 * FUSE will copy both struct a and the pointed buffer from the
2760 * process doing the ioctl and retry ioctl with both struct a and the
2763 * This time, FUSE server has everything it needs and completes ioctl
2764 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2766 * Copying data out works the same way.
2768 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2769 * automatically initializes in and out iovs by decoding @cmd with
2770 * _IOC_* macros and the server is not allowed to request RETRY. This
2771 * limits ioctl data transfers to well-formed ioctls and is the forced
2772 * behavior for all FUSE servers.
2774 long fuse_do_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
,
2777 struct fuse_file
*ff
= file
->private_data
;
2778 struct fuse_mount
*fm
= ff
->fm
;
2779 struct fuse_ioctl_in inarg
= {
2785 struct fuse_ioctl_out outarg
;
2786 struct iovec
*iov_page
= NULL
;
2787 struct iovec
*in_iov
= NULL
, *out_iov
= NULL
;
2788 unsigned int in_iovs
= 0, out_iovs
= 0, max_pages
;
2789 size_t in_size
, out_size
, c
;
2790 ssize_t transferred
;
2793 struct fuse_args_pages ap
= {};
2795 #if BITS_PER_LONG == 32
2796 inarg
.flags
|= FUSE_IOCTL_32BIT
;
2798 if (flags
& FUSE_IOCTL_COMPAT
) {
2799 inarg
.flags
|= FUSE_IOCTL_32BIT
;
2800 #ifdef CONFIG_X86_X32
2801 if (in_x32_syscall())
2802 inarg
.flags
|= FUSE_IOCTL_COMPAT_X32
;
2807 /* assume all the iovs returned by client always fits in a page */
2808 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec
) * FUSE_IOCTL_MAX_IOV
> PAGE_SIZE
);
2811 ap
.pages
= fuse_pages_alloc(fm
->fc
->max_pages
, GFP_KERNEL
, &ap
.descs
);
2812 iov_page
= (struct iovec
*) __get_free_page(GFP_KERNEL
);
2813 if (!ap
.pages
|| !iov_page
)
2816 fuse_page_descs_length_init(ap
.descs
, 0, fm
->fc
->max_pages
);
2819 * If restricted, initialize IO parameters as encoded in @cmd.
2820 * RETRY from server is not allowed.
2822 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
)) {
2823 struct iovec
*iov
= iov_page
;
2825 iov
->iov_base
= (void __user
*)arg
;
2828 case FS_IOC_GETFLAGS
:
2829 case FS_IOC_SETFLAGS
:
2830 iov
->iov_len
= sizeof(int);
2833 iov
->iov_len
= _IOC_SIZE(cmd
);
2837 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
2842 if (_IOC_DIR(cmd
) & _IOC_READ
) {
2849 inarg
.in_size
= in_size
= iov_length(in_iov
, in_iovs
);
2850 inarg
.out_size
= out_size
= iov_length(out_iov
, out_iovs
);
2853 * Out data can be used either for actual out data or iovs,
2854 * make sure there always is at least one page.
2856 out_size
= max_t(size_t, out_size
, PAGE_SIZE
);
2857 max_pages
= DIV_ROUND_UP(max(in_size
, out_size
), PAGE_SIZE
);
2859 /* make sure there are enough buffer pages and init request with them */
2861 if (max_pages
> fm
->fc
->max_pages
)
2863 while (ap
.num_pages
< max_pages
) {
2864 ap
.pages
[ap
.num_pages
] = alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
2865 if (!ap
.pages
[ap
.num_pages
])
2871 /* okay, let's send it to the client */
2872 ap
.args
.opcode
= FUSE_IOCTL
;
2873 ap
.args
.nodeid
= ff
->nodeid
;
2874 ap
.args
.in_numargs
= 1;
2875 ap
.args
.in_args
[0].size
= sizeof(inarg
);
2876 ap
.args
.in_args
[0].value
= &inarg
;
2878 ap
.args
.in_numargs
++;
2879 ap
.args
.in_args
[1].size
= in_size
;
2880 ap
.args
.in_pages
= true;
2883 iov_iter_init(&ii
, WRITE
, in_iov
, in_iovs
, in_size
);
2884 for (i
= 0; iov_iter_count(&ii
) && !WARN_ON(i
>= ap
.num_pages
); i
++) {
2885 c
= copy_page_from_iter(ap
.pages
[i
], 0, PAGE_SIZE
, &ii
);
2886 if (c
!= PAGE_SIZE
&& iov_iter_count(&ii
))
2891 ap
.args
.out_numargs
= 2;
2892 ap
.args
.out_args
[0].size
= sizeof(outarg
);
2893 ap
.args
.out_args
[0].value
= &outarg
;
2894 ap
.args
.out_args
[1].size
= out_size
;
2895 ap
.args
.out_pages
= true;
2896 ap
.args
.out_argvar
= true;
2898 transferred
= fuse_simple_request(fm
, &ap
.args
);
2900 if (transferred
< 0)
2903 /* did it ask for retry? */
2904 if (outarg
.flags
& FUSE_IOCTL_RETRY
) {
2907 /* no retry if in restricted mode */
2909 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
))
2912 in_iovs
= outarg
.in_iovs
;
2913 out_iovs
= outarg
.out_iovs
;
2916 * Make sure things are in boundary, separate checks
2917 * are to protect against overflow.
2920 if (in_iovs
> FUSE_IOCTL_MAX_IOV
||
2921 out_iovs
> FUSE_IOCTL_MAX_IOV
||
2922 in_iovs
+ out_iovs
> FUSE_IOCTL_MAX_IOV
)
2925 vaddr
= kmap_atomic(ap
.pages
[0]);
2926 err
= fuse_copy_ioctl_iovec(fm
->fc
, iov_page
, vaddr
,
2927 transferred
, in_iovs
+ out_iovs
,
2928 (flags
& FUSE_IOCTL_COMPAT
) != 0);
2929 kunmap_atomic(vaddr
);
2934 out_iov
= in_iov
+ in_iovs
;
2936 err
= fuse_verify_ioctl_iov(fm
->fc
, in_iov
, in_iovs
);
2940 err
= fuse_verify_ioctl_iov(fm
->fc
, out_iov
, out_iovs
);
2948 if (transferred
> inarg
.out_size
)
2952 iov_iter_init(&ii
, READ
, out_iov
, out_iovs
, transferred
);
2953 for (i
= 0; iov_iter_count(&ii
) && !WARN_ON(i
>= ap
.num_pages
); i
++) {
2954 c
= copy_page_to_iter(ap
.pages
[i
], 0, PAGE_SIZE
, &ii
);
2955 if (c
!= PAGE_SIZE
&& iov_iter_count(&ii
))
2960 free_page((unsigned long) iov_page
);
2961 while (ap
.num_pages
)
2962 __free_page(ap
.pages
[--ap
.num_pages
]);
2965 return err
? err
: outarg
.result
;
2967 EXPORT_SYMBOL_GPL(fuse_do_ioctl
);
2969 long fuse_ioctl_common(struct file
*file
, unsigned int cmd
,
2970 unsigned long arg
, unsigned int flags
)
2972 struct inode
*inode
= file_inode(file
);
2973 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2975 if (!fuse_allow_current_process(fc
))
2978 if (fuse_is_bad(inode
))
2981 return fuse_do_ioctl(file
, cmd
, arg
, flags
);
2984 static long fuse_file_ioctl(struct file
*file
, unsigned int cmd
,
2987 return fuse_ioctl_common(file
, cmd
, arg
, 0);
2990 static long fuse_file_compat_ioctl(struct file
*file
, unsigned int cmd
,
2993 return fuse_ioctl_common(file
, cmd
, arg
, FUSE_IOCTL_COMPAT
);
2997 * All files which have been polled are linked to RB tree
2998 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2999 * find the matching one.
3001 static struct rb_node
**fuse_find_polled_node(struct fuse_conn
*fc
, u64 kh
,
3002 struct rb_node
**parent_out
)
3004 struct rb_node
**link
= &fc
->polled_files
.rb_node
;
3005 struct rb_node
*last
= NULL
;
3008 struct fuse_file
*ff
;
3011 ff
= rb_entry(last
, struct fuse_file
, polled_node
);
3014 link
= &last
->rb_left
;
3015 else if (kh
> ff
->kh
)
3016 link
= &last
->rb_right
;
3027 * The file is about to be polled. Make sure it's on the polled_files
3028 * RB tree. Note that files once added to the polled_files tree are
3029 * not removed before the file is released. This is because a file
3030 * polled once is likely to be polled again.
3032 static void fuse_register_polled_file(struct fuse_conn
*fc
,
3033 struct fuse_file
*ff
)
3035 spin_lock(&fc
->lock
);
3036 if (RB_EMPTY_NODE(&ff
->polled_node
)) {
3037 struct rb_node
**link
, *parent
;
3039 link
= fuse_find_polled_node(fc
, ff
->kh
, &parent
);
3041 rb_link_node(&ff
->polled_node
, parent
, link
);
3042 rb_insert_color(&ff
->polled_node
, &fc
->polled_files
);
3044 spin_unlock(&fc
->lock
);
3047 __poll_t
fuse_file_poll(struct file
*file
, poll_table
*wait
)
3049 struct fuse_file
*ff
= file
->private_data
;
3050 struct fuse_mount
*fm
= ff
->fm
;
3051 struct fuse_poll_in inarg
= { .fh
= ff
->fh
, .kh
= ff
->kh
};
3052 struct fuse_poll_out outarg
;
3056 if (fm
->fc
->no_poll
)
3057 return DEFAULT_POLLMASK
;
3059 poll_wait(file
, &ff
->poll_wait
, wait
);
3060 inarg
.events
= mangle_poll(poll_requested_events(wait
));
3063 * Ask for notification iff there's someone waiting for it.
3064 * The client may ignore the flag and always notify.
3066 if (waitqueue_active(&ff
->poll_wait
)) {
3067 inarg
.flags
|= FUSE_POLL_SCHEDULE_NOTIFY
;
3068 fuse_register_polled_file(fm
->fc
, ff
);
3071 args
.opcode
= FUSE_POLL
;
3072 args
.nodeid
= ff
->nodeid
;
3073 args
.in_numargs
= 1;
3074 args
.in_args
[0].size
= sizeof(inarg
);
3075 args
.in_args
[0].value
= &inarg
;
3076 args
.out_numargs
= 1;
3077 args
.out_args
[0].size
= sizeof(outarg
);
3078 args
.out_args
[0].value
= &outarg
;
3079 err
= fuse_simple_request(fm
, &args
);
3082 return demangle_poll(outarg
.revents
);
3083 if (err
== -ENOSYS
) {
3084 fm
->fc
->no_poll
= 1;
3085 return DEFAULT_POLLMASK
;
3089 EXPORT_SYMBOL_GPL(fuse_file_poll
);
3092 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
3093 * wakes up the poll waiters.
3095 int fuse_notify_poll_wakeup(struct fuse_conn
*fc
,
3096 struct fuse_notify_poll_wakeup_out
*outarg
)
3098 u64 kh
= outarg
->kh
;
3099 struct rb_node
**link
;
3101 spin_lock(&fc
->lock
);
3103 link
= fuse_find_polled_node(fc
, kh
, NULL
);
3105 struct fuse_file
*ff
;
3107 ff
= rb_entry(*link
, struct fuse_file
, polled_node
);
3108 wake_up_interruptible_sync(&ff
->poll_wait
);
3111 spin_unlock(&fc
->lock
);
3115 static void fuse_do_truncate(struct file
*file
)
3117 struct inode
*inode
= file
->f_mapping
->host
;
3120 attr
.ia_valid
= ATTR_SIZE
;
3121 attr
.ia_size
= i_size_read(inode
);
3123 attr
.ia_file
= file
;
3124 attr
.ia_valid
|= ATTR_FILE
;
3126 fuse_do_setattr(file_dentry(file
), &attr
, file
);
3129 static inline loff_t
fuse_round_up(struct fuse_conn
*fc
, loff_t off
)
3131 return round_up(off
, fc
->max_pages
<< PAGE_SHIFT
);
3135 fuse_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
)
3137 DECLARE_COMPLETION_ONSTACK(wait
);
3139 struct file
*file
= iocb
->ki_filp
;
3140 struct fuse_file
*ff
= file
->private_data
;
3142 struct inode
*inode
;
3144 size_t count
= iov_iter_count(iter
), shortened
= 0;
3145 loff_t offset
= iocb
->ki_pos
;
3146 struct fuse_io_priv
*io
;
3149 inode
= file
->f_mapping
->host
;
3150 i_size
= i_size_read(inode
);
3152 if ((iov_iter_rw(iter
) == READ
) && (offset
>= i_size
))
3155 io
= kmalloc(sizeof(struct fuse_io_priv
), GFP_KERNEL
);
3158 spin_lock_init(&io
->lock
);
3159 kref_init(&io
->refcnt
);
3163 io
->offset
= offset
;
3164 io
->write
= (iov_iter_rw(iter
) == WRITE
);
3167 * By default, we want to optimize all I/Os with async request
3168 * submission to the client filesystem if supported.
3170 io
->async
= ff
->fm
->fc
->async_dio
;
3172 io
->blocking
= is_sync_kiocb(iocb
);
3174 /* optimization for short read */
3175 if (io
->async
&& !io
->write
&& offset
+ count
> i_size
) {
3176 iov_iter_truncate(iter
, fuse_round_up(ff
->fm
->fc
, i_size
- offset
));
3177 shortened
= count
- iov_iter_count(iter
);
3182 * We cannot asynchronously extend the size of a file.
3183 * In such case the aio will behave exactly like sync io.
3185 if ((offset
+ count
> i_size
) && io
->write
)
3186 io
->blocking
= true;
3188 if (io
->async
&& io
->blocking
) {
3190 * Additional reference to keep io around after
3191 * calling fuse_aio_complete()
3193 kref_get(&io
->refcnt
);
3197 if (iov_iter_rw(iter
) == WRITE
) {
3198 ret
= fuse_direct_io(io
, iter
, &pos
, FUSE_DIO_WRITE
);
3199 fuse_invalidate_attr(inode
);
3201 ret
= __fuse_direct_read(io
, iter
, &pos
);
3203 iov_iter_reexpand(iter
, iov_iter_count(iter
) + shortened
);
3206 bool blocking
= io
->blocking
;
3208 fuse_aio_complete(io
, ret
< 0 ? ret
: 0, -1);
3210 /* we have a non-extending, async request, so return */
3212 return -EIOCBQUEUED
;
3214 wait_for_completion(&wait
);
3215 ret
= fuse_get_res_by_io(io
);
3218 kref_put(&io
->refcnt
, fuse_io_release
);
3220 if (iov_iter_rw(iter
) == WRITE
) {
3222 fuse_write_update_size(inode
, pos
);
3223 else if (ret
< 0 && offset
+ count
> i_size
)
3224 fuse_do_truncate(file
);
3230 static int fuse_writeback_range(struct inode
*inode
, loff_t start
, loff_t end
)
3232 int err
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
3235 fuse_sync_writes(inode
);
3240 static long fuse_file_fallocate(struct file
*file
, int mode
, loff_t offset
,
3243 struct fuse_file
*ff
= file
->private_data
;
3244 struct inode
*inode
= file_inode(file
);
3245 struct fuse_inode
*fi
= get_fuse_inode(inode
);
3246 struct fuse_mount
*fm
= ff
->fm
;
3248 struct fuse_fallocate_in inarg
= {
3255 bool lock_inode
= !(mode
& FALLOC_FL_KEEP_SIZE
) ||
3256 (mode
& FALLOC_FL_PUNCH_HOLE
);
3258 bool block_faults
= FUSE_IS_DAX(inode
) && lock_inode
;
3260 if (mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
))
3263 if (fm
->fc
->no_fallocate
)
3269 down_write(&fi
->i_mmap_sem
);
3270 err
= fuse_dax_break_layouts(inode
, 0, 0);
3275 if (mode
& FALLOC_FL_PUNCH_HOLE
) {
3276 loff_t endbyte
= offset
+ length
- 1;
3278 err
= fuse_writeback_range(inode
, offset
, endbyte
);
3284 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
3285 offset
+ length
> i_size_read(inode
)) {
3286 err
= inode_newsize_ok(inode
, offset
+ length
);
3291 if (!(mode
& FALLOC_FL_KEEP_SIZE
))
3292 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
3294 args
.opcode
= FUSE_FALLOCATE
;
3295 args
.nodeid
= ff
->nodeid
;
3296 args
.in_numargs
= 1;
3297 args
.in_args
[0].size
= sizeof(inarg
);
3298 args
.in_args
[0].value
= &inarg
;
3299 err
= fuse_simple_request(fm
, &args
);
3300 if (err
== -ENOSYS
) {
3301 fm
->fc
->no_fallocate
= 1;
3307 /* we could have extended the file */
3308 if (!(mode
& FALLOC_FL_KEEP_SIZE
)) {
3309 bool changed
= fuse_write_update_size(inode
, offset
+ length
);
3311 if (changed
&& fm
->fc
->writeback_cache
)
3312 file_update_time(file
);
3315 if (mode
& FALLOC_FL_PUNCH_HOLE
)
3316 truncate_pagecache_range(inode
, offset
, offset
+ length
- 1);
3318 fuse_invalidate_attr(inode
);
3321 if (!(mode
& FALLOC_FL_KEEP_SIZE
))
3322 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
3325 up_write(&fi
->i_mmap_sem
);
3328 inode_unlock(inode
);
3333 static ssize_t
__fuse_copy_file_range(struct file
*file_in
, loff_t pos_in
,
3334 struct file
*file_out
, loff_t pos_out
,
3335 size_t len
, unsigned int flags
)
3337 struct fuse_file
*ff_in
= file_in
->private_data
;
3338 struct fuse_file
*ff_out
= file_out
->private_data
;
3339 struct inode
*inode_in
= file_inode(file_in
);
3340 struct inode
*inode_out
= file_inode(file_out
);
3341 struct fuse_inode
*fi_out
= get_fuse_inode(inode_out
);
3342 struct fuse_mount
*fm
= ff_in
->fm
;
3343 struct fuse_conn
*fc
= fm
->fc
;
3345 struct fuse_copy_file_range_in inarg
= {
3348 .nodeid_out
= ff_out
->nodeid
,
3349 .fh_out
= ff_out
->fh
,
3354 struct fuse_write_out outarg
;
3356 /* mark unstable when write-back is not used, and file_out gets
3358 bool is_unstable
= (!fc
->writeback_cache
) &&
3359 ((pos_out
+ len
) > inode_out
->i_size
);
3361 if (fc
->no_copy_file_range
)
3364 if (file_inode(file_in
)->i_sb
!= file_inode(file_out
)->i_sb
)
3367 inode_lock(inode_in
);
3368 err
= fuse_writeback_range(inode_in
, pos_in
, pos_in
+ len
- 1);
3369 inode_unlock(inode_in
);
3373 inode_lock(inode_out
);
3375 err
= file_modified(file_out
);
3380 * Write out dirty pages in the destination file before sending the COPY
3381 * request to userspace. After the request is completed, truncate off
3382 * pages (including partial ones) from the cache that have been copied,
3383 * since these contain stale data at that point.
3385 * This should be mostly correct, but if the COPY writes to partial
3386 * pages (at the start or end) and the parts not covered by the COPY are
3387 * written through a memory map after calling fuse_writeback_range(),
3388 * then these partial page modifications will be lost on truncation.
3390 * It is unlikely that someone would rely on such mixed style
3391 * modifications. Yet this does give less guarantees than if the
3392 * copying was performed with write(2).
3394 * To fix this a i_mmap_sem style lock could be used to prevent new
3395 * faults while the copy is ongoing.
3397 err
= fuse_writeback_range(inode_out
, pos_out
, pos_out
+ len
- 1);
3402 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi_out
->state
);
3404 args
.opcode
= FUSE_COPY_FILE_RANGE
;
3405 args
.nodeid
= ff_in
->nodeid
;
3406 args
.in_numargs
= 1;
3407 args
.in_args
[0].size
= sizeof(inarg
);
3408 args
.in_args
[0].value
= &inarg
;
3409 args
.out_numargs
= 1;
3410 args
.out_args
[0].size
= sizeof(outarg
);
3411 args
.out_args
[0].value
= &outarg
;
3412 err
= fuse_simple_request(fm
, &args
);
3413 if (err
== -ENOSYS
) {
3414 fc
->no_copy_file_range
= 1;
3420 truncate_inode_pages_range(inode_out
->i_mapping
,
3421 ALIGN_DOWN(pos_out
, PAGE_SIZE
),
3422 ALIGN(pos_out
+ outarg
.size
, PAGE_SIZE
) - 1);
3424 if (fc
->writeback_cache
) {
3425 fuse_write_update_size(inode_out
, pos_out
+ outarg
.size
);
3426 file_update_time(file_out
);
3429 fuse_invalidate_attr(inode_out
);
3434 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi_out
->state
);
3436 inode_unlock(inode_out
);
3437 file_accessed(file_in
);
3442 static ssize_t
fuse_copy_file_range(struct file
*src_file
, loff_t src_off
,
3443 struct file
*dst_file
, loff_t dst_off
,
3444 size_t len
, unsigned int flags
)
3448 ret
= __fuse_copy_file_range(src_file
, src_off
, dst_file
, dst_off
,
3451 if (ret
== -EOPNOTSUPP
|| ret
== -EXDEV
)
3452 ret
= generic_copy_file_range(src_file
, src_off
, dst_file
,
3453 dst_off
, len
, flags
);
3457 static const struct file_operations fuse_file_operations
= {
3458 .llseek
= fuse_file_llseek
,
3459 .read_iter
= fuse_file_read_iter
,
3460 .write_iter
= fuse_file_write_iter
,
3461 .mmap
= fuse_file_mmap
,
3463 .flush
= fuse_flush
,
3464 .release
= fuse_release
,
3465 .fsync
= fuse_fsync
,
3466 .lock
= fuse_file_lock
,
3467 .get_unmapped_area
= thp_get_unmapped_area
,
3468 .flock
= fuse_file_flock
,
3469 .splice_read
= generic_file_splice_read
,
3470 .splice_write
= iter_file_splice_write
,
3471 .unlocked_ioctl
= fuse_file_ioctl
,
3472 .compat_ioctl
= fuse_file_compat_ioctl
,
3473 .poll
= fuse_file_poll
,
3474 .fallocate
= fuse_file_fallocate
,
3475 .copy_file_range
= fuse_copy_file_range
,
3478 static const struct address_space_operations fuse_file_aops
= {
3479 .readpage
= fuse_readpage
,
3480 .readahead
= fuse_readahead
,
3481 .writepage
= fuse_writepage
,
3482 .writepages
= fuse_writepages
,
3483 .launder_page
= fuse_launder_page
,
3484 .set_page_dirty
= __set_page_dirty_nobuffers
,
3486 .direct_IO
= fuse_direct_IO
,
3487 .write_begin
= fuse_write_begin
,
3488 .write_end
= fuse_write_end
,
3491 void fuse_init_file_inode(struct inode
*inode
)
3493 struct fuse_inode
*fi
= get_fuse_inode(inode
);
3495 inode
->i_fop
= &fuse_file_operations
;
3496 inode
->i_data
.a_ops
= &fuse_file_aops
;
3498 INIT_LIST_HEAD(&fi
->write_files
);
3499 INIT_LIST_HEAD(&fi
->queued_writes
);
3501 init_waitqueue_head(&fi
->page_waitq
);
3502 fi
->writepages
= RB_ROOT
;
3504 if (IS_ENABLED(CONFIG_FUSE_DAX
))
3505 fuse_dax_inode_init(inode
);