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>
22 static struct page
**fuse_pages_alloc(unsigned int npages
, gfp_t flags
,
23 struct fuse_page_desc
**desc
)
27 pages
= kzalloc(npages
* (sizeof(struct page
*) +
28 sizeof(struct fuse_page_desc
)), flags
);
29 *desc
= (void *) (pages
+ npages
);
34 static int fuse_send_open(struct fuse_conn
*fc
, u64 nodeid
, struct file
*file
,
35 int opcode
, struct fuse_open_out
*outargp
)
37 struct fuse_open_in inarg
;
40 memset(&inarg
, 0, sizeof(inarg
));
41 inarg
.flags
= file
->f_flags
& ~(O_CREAT
| O_EXCL
| O_NOCTTY
);
42 if (!fc
->atomic_o_trunc
)
43 inarg
.flags
&= ~O_TRUNC
;
47 args
.in_args
[0].size
= sizeof(inarg
);
48 args
.in_args
[0].value
= &inarg
;
50 args
.out_args
[0].size
= sizeof(*outargp
);
51 args
.out_args
[0].value
= outargp
;
53 return fuse_simple_request(fc
, &args
);
56 struct fuse_release_args
{
57 struct fuse_args args
;
58 struct fuse_release_in inarg
;
62 struct fuse_file
*fuse_file_alloc(struct fuse_conn
*fc
)
66 ff
= kzalloc(sizeof(struct fuse_file
), GFP_KERNEL_ACCOUNT
);
71 ff
->release_args
= kzalloc(sizeof(*ff
->release_args
),
73 if (!ff
->release_args
) {
78 INIT_LIST_HEAD(&ff
->write_entry
);
79 mutex_init(&ff
->readdir
.lock
);
80 refcount_set(&ff
->count
, 1);
81 RB_CLEAR_NODE(&ff
->polled_node
);
82 init_waitqueue_head(&ff
->poll_wait
);
84 ff
->kh
= atomic64_inc_return(&fc
->khctr
);
89 void fuse_file_free(struct fuse_file
*ff
)
91 kfree(ff
->release_args
);
92 mutex_destroy(&ff
->readdir
.lock
);
96 static struct fuse_file
*fuse_file_get(struct fuse_file
*ff
)
98 refcount_inc(&ff
->count
);
102 static void fuse_release_end(struct fuse_conn
*fc
, struct fuse_args
*args
,
105 struct fuse_release_args
*ra
= container_of(args
, typeof(*ra
), args
);
111 static void fuse_file_put(struct fuse_file
*ff
, bool sync
, bool isdir
)
113 if (refcount_dec_and_test(&ff
->count
)) {
114 struct fuse_args
*args
= &ff
->release_args
->args
;
116 if (isdir
? ff
->fc
->no_opendir
: ff
->fc
->no_open
) {
117 /* Do nothing when client does not implement 'open' */
118 fuse_release_end(ff
->fc
, args
, 0);
120 fuse_simple_request(ff
->fc
, args
);
121 fuse_release_end(ff
->fc
, args
, 0);
123 args
->end
= fuse_release_end
;
124 if (fuse_simple_background(ff
->fc
, args
,
125 GFP_KERNEL
| __GFP_NOFAIL
))
126 fuse_release_end(ff
->fc
, args
, -ENOTCONN
);
132 int fuse_do_open(struct fuse_conn
*fc
, u64 nodeid
, struct file
*file
,
135 struct fuse_file
*ff
;
136 int opcode
= isdir
? FUSE_OPENDIR
: FUSE_OPEN
;
138 ff
= fuse_file_alloc(fc
);
143 /* Default for no-open */
144 ff
->open_flags
= FOPEN_KEEP_CACHE
| (isdir
? FOPEN_CACHE_DIR
: 0);
145 if (isdir
? !fc
->no_opendir
: !fc
->no_open
) {
146 struct fuse_open_out outarg
;
149 err
= fuse_send_open(fc
, nodeid
, file
, opcode
, &outarg
);
152 ff
->open_flags
= outarg
.open_flags
;
154 } else if (err
!= -ENOSYS
) {
166 ff
->open_flags
&= ~FOPEN_DIRECT_IO
;
169 file
->private_data
= ff
;
173 EXPORT_SYMBOL_GPL(fuse_do_open
);
175 static void fuse_link_write_file(struct file
*file
)
177 struct inode
*inode
= file_inode(file
);
178 struct fuse_inode
*fi
= get_fuse_inode(inode
);
179 struct fuse_file
*ff
= file
->private_data
;
181 * file may be written through mmap, so chain it onto the
182 * inodes's write_file list
184 spin_lock(&fi
->lock
);
185 if (list_empty(&ff
->write_entry
))
186 list_add(&ff
->write_entry
, &fi
->write_files
);
187 spin_unlock(&fi
->lock
);
190 void fuse_finish_open(struct inode
*inode
, struct file
*file
)
192 struct fuse_file
*ff
= file
->private_data
;
193 struct fuse_conn
*fc
= get_fuse_conn(inode
);
195 if (!(ff
->open_flags
& FOPEN_KEEP_CACHE
))
196 invalidate_inode_pages2(inode
->i_mapping
);
197 if (ff
->open_flags
& FOPEN_STREAM
)
198 stream_open(inode
, file
);
199 else if (ff
->open_flags
& FOPEN_NONSEEKABLE
)
200 nonseekable_open(inode
, file
);
201 if (fc
->atomic_o_trunc
&& (file
->f_flags
& O_TRUNC
)) {
202 struct fuse_inode
*fi
= get_fuse_inode(inode
);
204 spin_lock(&fi
->lock
);
205 fi
->attr_version
= atomic64_inc_return(&fc
->attr_version
);
206 i_size_write(inode
, 0);
207 spin_unlock(&fi
->lock
);
208 fuse_invalidate_attr(inode
);
209 if (fc
->writeback_cache
)
210 file_update_time(file
);
212 if ((file
->f_mode
& FMODE_WRITE
) && fc
->writeback_cache
)
213 fuse_link_write_file(file
);
216 int fuse_open_common(struct inode
*inode
, struct file
*file
, bool isdir
)
218 struct fuse_conn
*fc
= get_fuse_conn(inode
);
220 bool is_wb_truncate
= (file
->f_flags
& O_TRUNC
) &&
221 fc
->atomic_o_trunc
&&
224 err
= generic_file_open(inode
, file
);
228 if (is_wb_truncate
) {
230 fuse_set_nowrite(inode
);
233 err
= fuse_do_open(fc
, get_node_id(inode
), file
, isdir
);
236 fuse_finish_open(inode
, file
);
238 if (is_wb_truncate
) {
239 fuse_release_nowrite(inode
);
246 static void fuse_prepare_release(struct fuse_inode
*fi
, struct fuse_file
*ff
,
247 int flags
, int opcode
)
249 struct fuse_conn
*fc
= ff
->fc
;
250 struct fuse_release_args
*ra
= ff
->release_args
;
252 /* Inode is NULL on error path of fuse_create_open() */
254 spin_lock(&fi
->lock
);
255 list_del(&ff
->write_entry
);
256 spin_unlock(&fi
->lock
);
258 spin_lock(&fc
->lock
);
259 if (!RB_EMPTY_NODE(&ff
->polled_node
))
260 rb_erase(&ff
->polled_node
, &fc
->polled_files
);
261 spin_unlock(&fc
->lock
);
263 wake_up_interruptible_all(&ff
->poll_wait
);
265 ra
->inarg
.fh
= ff
->fh
;
266 ra
->inarg
.flags
= flags
;
267 ra
->args
.in_numargs
= 1;
268 ra
->args
.in_args
[0].size
= sizeof(struct fuse_release_in
);
269 ra
->args
.in_args
[0].value
= &ra
->inarg
;
270 ra
->args
.opcode
= opcode
;
271 ra
->args
.nodeid
= ff
->nodeid
;
272 ra
->args
.force
= true;
273 ra
->args
.nocreds
= true;
276 void fuse_release_common(struct file
*file
, bool isdir
)
278 struct fuse_inode
*fi
= get_fuse_inode(file_inode(file
));
279 struct fuse_file
*ff
= file
->private_data
;
280 struct fuse_release_args
*ra
= ff
->release_args
;
281 int opcode
= isdir
? FUSE_RELEASEDIR
: FUSE_RELEASE
;
283 fuse_prepare_release(fi
, ff
, file
->f_flags
, opcode
);
286 ra
->inarg
.release_flags
|= FUSE_RELEASE_FLOCK_UNLOCK
;
287 ra
->inarg
.lock_owner
= fuse_lock_owner_id(ff
->fc
,
290 /* Hold inode until release is finished */
291 ra
->inode
= igrab(file_inode(file
));
294 * Normally this will send the RELEASE request, however if
295 * some asynchronous READ or WRITE requests are outstanding,
296 * the sending will be delayed.
298 * Make the release synchronous if this is a fuseblk mount,
299 * synchronous RELEASE is allowed (and desirable) in this case
300 * because the server can be trusted not to screw up.
302 fuse_file_put(ff
, ff
->fc
->destroy
, isdir
);
305 static int fuse_open(struct inode
*inode
, struct file
*file
)
307 return fuse_open_common(inode
, file
, false);
310 static int fuse_release(struct inode
*inode
, struct file
*file
)
312 struct fuse_conn
*fc
= get_fuse_conn(inode
);
314 /* see fuse_vma_close() for !writeback_cache case */
315 if (fc
->writeback_cache
)
316 write_inode_now(inode
, 1);
318 fuse_release_common(file
, false);
320 /* return value is ignored by VFS */
324 void fuse_sync_release(struct fuse_inode
*fi
, struct fuse_file
*ff
, int flags
)
326 WARN_ON(refcount_read(&ff
->count
) > 1);
327 fuse_prepare_release(fi
, ff
, flags
, FUSE_RELEASE
);
329 * iput(NULL) is a no-op and since the refcount is 1 and everything's
330 * synchronous, we are fine with not doing igrab() here"
332 fuse_file_put(ff
, true, false);
334 EXPORT_SYMBOL_GPL(fuse_sync_release
);
337 * Scramble the ID space with XTEA, so that the value of the files_struct
338 * pointer is not exposed to userspace.
340 u64
fuse_lock_owner_id(struct fuse_conn
*fc
, fl_owner_t id
)
342 u32
*k
= fc
->scramble_key
;
343 u64 v
= (unsigned long) id
;
349 for (i
= 0; i
< 32; i
++) {
350 v0
+= ((v1
<< 4 ^ v1
>> 5) + v1
) ^ (sum
+ k
[sum
& 3]);
352 v1
+= ((v0
<< 4 ^ v0
>> 5) + v0
) ^ (sum
+ k
[sum
>>11 & 3]);
355 return (u64
) v0
+ ((u64
) v1
<< 32);
358 struct fuse_writepage_args
{
359 struct fuse_io_args ia
;
360 struct list_head writepages_entry
;
361 struct list_head queue_entry
;
362 struct fuse_writepage_args
*next
;
366 static struct fuse_writepage_args
*fuse_find_writeback(struct fuse_inode
*fi
,
367 pgoff_t idx_from
, pgoff_t idx_to
)
369 struct fuse_writepage_args
*wpa
;
371 list_for_each_entry(wpa
, &fi
->writepages
, writepages_entry
) {
374 WARN_ON(get_fuse_inode(wpa
->inode
) != fi
);
375 curr_index
= wpa
->ia
.write
.in
.offset
>> PAGE_SHIFT
;
376 if (idx_from
< curr_index
+ wpa
->ia
.ap
.num_pages
&&
377 curr_index
<= idx_to
) {
385 * Check if any page in a range is under writeback
387 * This is currently done by walking the list of writepage requests
388 * for the inode, which can be pretty inefficient.
390 static bool fuse_range_is_writeback(struct inode
*inode
, pgoff_t idx_from
,
393 struct fuse_inode
*fi
= get_fuse_inode(inode
);
396 spin_lock(&fi
->lock
);
397 found
= fuse_find_writeback(fi
, idx_from
, idx_to
);
398 spin_unlock(&fi
->lock
);
403 static inline bool fuse_page_is_writeback(struct inode
*inode
, pgoff_t index
)
405 return fuse_range_is_writeback(inode
, index
, index
);
409 * Wait for page writeback to be completed.
411 * Since fuse doesn't rely on the VM writeback tracking, this has to
412 * use some other means.
414 static void fuse_wait_on_page_writeback(struct inode
*inode
, pgoff_t index
)
416 struct fuse_inode
*fi
= get_fuse_inode(inode
);
418 wait_event(fi
->page_waitq
, !fuse_page_is_writeback(inode
, index
));
422 * Wait for all pending writepages on the inode to finish.
424 * This is currently done by blocking further writes with FUSE_NOWRITE
425 * and waiting for all sent writes to complete.
427 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
428 * could conflict with truncation.
430 static void fuse_sync_writes(struct inode
*inode
)
432 fuse_set_nowrite(inode
);
433 fuse_release_nowrite(inode
);
436 static int fuse_flush(struct file
*file
, fl_owner_t id
)
438 struct inode
*inode
= file_inode(file
);
439 struct fuse_conn
*fc
= get_fuse_conn(inode
);
440 struct fuse_file
*ff
= file
->private_data
;
441 struct fuse_flush_in inarg
;
445 if (is_bad_inode(inode
))
451 err
= write_inode_now(inode
, 1);
456 fuse_sync_writes(inode
);
459 err
= filemap_check_errors(file
->f_mapping
);
463 memset(&inarg
, 0, sizeof(inarg
));
465 inarg
.lock_owner
= fuse_lock_owner_id(fc
, id
);
466 args
.opcode
= FUSE_FLUSH
;
467 args
.nodeid
= get_node_id(inode
);
469 args
.in_args
[0].size
= sizeof(inarg
);
470 args
.in_args
[0].value
= &inarg
;
473 err
= fuse_simple_request(fc
, &args
);
474 if (err
== -ENOSYS
) {
481 int fuse_fsync_common(struct file
*file
, loff_t start
, loff_t end
,
482 int datasync
, int opcode
)
484 struct inode
*inode
= file
->f_mapping
->host
;
485 struct fuse_conn
*fc
= get_fuse_conn(inode
);
486 struct fuse_file
*ff
= file
->private_data
;
488 struct fuse_fsync_in inarg
;
490 memset(&inarg
, 0, sizeof(inarg
));
492 inarg
.fsync_flags
= datasync
? FUSE_FSYNC_FDATASYNC
: 0;
493 args
.opcode
= opcode
;
494 args
.nodeid
= get_node_id(inode
);
496 args
.in_args
[0].size
= sizeof(inarg
);
497 args
.in_args
[0].value
= &inarg
;
498 return fuse_simple_request(fc
, &args
);
501 static int fuse_fsync(struct file
*file
, loff_t start
, loff_t end
,
504 struct inode
*inode
= file
->f_mapping
->host
;
505 struct fuse_conn
*fc
= get_fuse_conn(inode
);
508 if (is_bad_inode(inode
))
514 * Start writeback against all dirty pages of the inode, then
515 * wait for all outstanding writes, before sending the FSYNC
518 err
= file_write_and_wait_range(file
, start
, end
);
522 fuse_sync_writes(inode
);
525 * Due to implementation of fuse writeback
526 * file_write_and_wait_range() does not catch errors.
527 * We have to do this directly after fuse_sync_writes()
529 err
= file_check_and_advance_wb_err(file
);
533 err
= sync_inode_metadata(inode
, 1);
540 err
= fuse_fsync_common(file
, start
, end
, datasync
, FUSE_FSYNC
);
541 if (err
== -ENOSYS
) {
551 void fuse_read_args_fill(struct fuse_io_args
*ia
, struct file
*file
, loff_t pos
,
552 size_t count
, int opcode
)
554 struct fuse_file
*ff
= file
->private_data
;
555 struct fuse_args
*args
= &ia
->ap
.args
;
557 ia
->read
.in
.fh
= ff
->fh
;
558 ia
->read
.in
.offset
= pos
;
559 ia
->read
.in
.size
= count
;
560 ia
->read
.in
.flags
= file
->f_flags
;
561 args
->opcode
= opcode
;
562 args
->nodeid
= ff
->nodeid
;
563 args
->in_numargs
= 1;
564 args
->in_args
[0].size
= sizeof(ia
->read
.in
);
565 args
->in_args
[0].value
= &ia
->read
.in
;
566 args
->out_argvar
= true;
567 args
->out_numargs
= 1;
568 args
->out_args
[0].size
= count
;
571 static void fuse_release_user_pages(struct fuse_args_pages
*ap
,
576 for (i
= 0; i
< ap
->num_pages
; i
++) {
578 set_page_dirty_lock(ap
->pages
[i
]);
579 put_page(ap
->pages
[i
]);
583 static void fuse_io_release(struct kref
*kref
)
585 kfree(container_of(kref
, struct fuse_io_priv
, refcnt
));
588 static ssize_t
fuse_get_res_by_io(struct fuse_io_priv
*io
)
593 if (io
->bytes
>= 0 && io
->write
)
596 return io
->bytes
< 0 ? io
->size
: io
->bytes
;
600 * In case of short read, the caller sets 'pos' to the position of
601 * actual end of fuse request in IO request. Otherwise, if bytes_requested
602 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
605 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
606 * both submitted asynchronously. The first of them was ACKed by userspace as
607 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
608 * second request was ACKed as short, e.g. only 1K was read, resulting in
611 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
612 * will be equal to the length of the longest contiguous fragment of
613 * transferred data starting from the beginning of IO request.
615 static void fuse_aio_complete(struct fuse_io_priv
*io
, int err
, ssize_t pos
)
619 spin_lock(&io
->lock
);
621 io
->err
= io
->err
? : err
;
622 else if (pos
>= 0 && (io
->bytes
< 0 || pos
< io
->bytes
))
626 if (!left
&& io
->blocking
)
628 spin_unlock(&io
->lock
);
630 if (!left
&& !io
->blocking
) {
631 ssize_t res
= fuse_get_res_by_io(io
);
634 struct inode
*inode
= file_inode(io
->iocb
->ki_filp
);
635 struct fuse_conn
*fc
= get_fuse_conn(inode
);
636 struct fuse_inode
*fi
= get_fuse_inode(inode
);
638 spin_lock(&fi
->lock
);
639 fi
->attr_version
= atomic64_inc_return(&fc
->attr_version
);
640 spin_unlock(&fi
->lock
);
643 io
->iocb
->ki_complete(io
->iocb
, res
, 0);
646 kref_put(&io
->refcnt
, fuse_io_release
);
649 static struct fuse_io_args
*fuse_io_alloc(struct fuse_io_priv
*io
,
652 struct fuse_io_args
*ia
;
654 ia
= kzalloc(sizeof(*ia
), GFP_KERNEL
);
657 ia
->ap
.pages
= fuse_pages_alloc(npages
, GFP_KERNEL
,
667 static void fuse_io_free(struct fuse_io_args
*ia
)
673 static void fuse_aio_complete_req(struct fuse_conn
*fc
, struct fuse_args
*args
,
676 struct fuse_io_args
*ia
= container_of(args
, typeof(*ia
), ap
.args
);
677 struct fuse_io_priv
*io
= ia
->io
;
680 fuse_release_user_pages(&ia
->ap
, io
->should_dirty
);
684 } else if (io
->write
) {
685 if (ia
->write
.out
.size
> ia
->write
.in
.size
) {
687 } else if (ia
->write
.in
.size
!= ia
->write
.out
.size
) {
688 pos
= ia
->write
.in
.offset
- io
->offset
+
692 u32 outsize
= args
->out_args
[0].size
;
694 if (ia
->read
.in
.size
!= outsize
)
695 pos
= ia
->read
.in
.offset
- io
->offset
+ outsize
;
698 fuse_aio_complete(io
, err
, pos
);
702 static ssize_t
fuse_async_req_send(struct fuse_conn
*fc
,
703 struct fuse_io_args
*ia
, size_t num_bytes
)
706 struct fuse_io_priv
*io
= ia
->io
;
708 spin_lock(&io
->lock
);
709 kref_get(&io
->refcnt
);
710 io
->size
+= num_bytes
;
712 spin_unlock(&io
->lock
);
714 ia
->ap
.args
.end
= fuse_aio_complete_req
;
715 err
= fuse_simple_background(fc
, &ia
->ap
.args
, GFP_KERNEL
);
717 fuse_aio_complete_req(fc
, &ia
->ap
.args
, err
);
722 static ssize_t
fuse_send_read(struct fuse_io_args
*ia
, loff_t pos
, size_t count
,
725 struct file
*file
= ia
->io
->iocb
->ki_filp
;
726 struct fuse_file
*ff
= file
->private_data
;
727 struct fuse_conn
*fc
= ff
->fc
;
729 fuse_read_args_fill(ia
, file
, pos
, count
, FUSE_READ
);
731 ia
->read
.in
.read_flags
|= FUSE_READ_LOCKOWNER
;
732 ia
->read
.in
.lock_owner
= fuse_lock_owner_id(fc
, owner
);
736 return fuse_async_req_send(fc
, ia
, count
);
738 return fuse_simple_request(fc
, &ia
->ap
.args
);
741 static void fuse_read_update_size(struct inode
*inode
, loff_t size
,
744 struct fuse_conn
*fc
= get_fuse_conn(inode
);
745 struct fuse_inode
*fi
= get_fuse_inode(inode
);
747 spin_lock(&fi
->lock
);
748 if (attr_ver
== fi
->attr_version
&& size
< inode
->i_size
&&
749 !test_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
)) {
750 fi
->attr_version
= atomic64_inc_return(&fc
->attr_version
);
751 i_size_write(inode
, size
);
753 spin_unlock(&fi
->lock
);
756 static void fuse_short_read(struct inode
*inode
, u64 attr_ver
, size_t num_read
,
757 struct fuse_args_pages
*ap
)
759 struct fuse_conn
*fc
= get_fuse_conn(inode
);
761 if (fc
->writeback_cache
) {
763 * A hole in a file. Some data after the hole are in page cache,
764 * but have not reached the client fs yet. So, the hole is not
768 int start_idx
= num_read
>> PAGE_SHIFT
;
769 size_t off
= num_read
& (PAGE_SIZE
- 1);
771 for (i
= start_idx
; i
< ap
->num_pages
; i
++) {
772 zero_user_segment(ap
->pages
[i
], off
, PAGE_SIZE
);
776 loff_t pos
= page_offset(ap
->pages
[0]) + num_read
;
777 fuse_read_update_size(inode
, pos
, attr_ver
);
781 static int fuse_do_readpage(struct file
*file
, struct page
*page
)
783 struct inode
*inode
= page
->mapping
->host
;
784 struct fuse_conn
*fc
= get_fuse_conn(inode
);
785 loff_t pos
= page_offset(page
);
786 struct fuse_page_desc desc
= { .length
= PAGE_SIZE
};
787 struct fuse_io_args ia
= {
788 .ap
.args
.page_zeroing
= true,
789 .ap
.args
.out_pages
= true,
798 * Page writeback can extend beyond the lifetime of the
799 * page-cache page, so make sure we read a properly synced
802 fuse_wait_on_page_writeback(inode
, page
->index
);
804 attr_ver
= fuse_get_attr_version(fc
);
806 /* Don't overflow end offset */
807 if (pos
+ (desc
.length
- 1) == LLONG_MAX
)
810 fuse_read_args_fill(&ia
, file
, pos
, desc
.length
, FUSE_READ
);
811 res
= fuse_simple_request(fc
, &ia
.ap
.args
);
815 * Short read means EOF. If file size is larger, truncate it
817 if (res
< desc
.length
)
818 fuse_short_read(inode
, attr_ver
, res
, &ia
.ap
);
820 SetPageUptodate(page
);
825 static int fuse_readpage(struct file
*file
, struct page
*page
)
827 struct inode
*inode
= page
->mapping
->host
;
831 if (is_bad_inode(inode
))
834 err
= fuse_do_readpage(file
, page
);
835 fuse_invalidate_atime(inode
);
841 static void fuse_readpages_end(struct fuse_conn
*fc
, struct fuse_args
*args
,
845 struct fuse_io_args
*ia
= container_of(args
, typeof(*ia
), ap
.args
);
846 struct fuse_args_pages
*ap
= &ia
->ap
;
847 size_t count
= ia
->read
.in
.size
;
848 size_t num_read
= args
->out_args
[0].size
;
849 struct address_space
*mapping
= NULL
;
851 for (i
= 0; mapping
== NULL
&& i
< ap
->num_pages
; i
++)
852 mapping
= ap
->pages
[i
]->mapping
;
855 struct inode
*inode
= mapping
->host
;
858 * Short read means EOF. If file size is larger, truncate it
860 if (!err
&& num_read
< count
)
861 fuse_short_read(inode
, ia
->read
.attr_ver
, num_read
, ap
);
863 fuse_invalidate_atime(inode
);
866 for (i
= 0; i
< ap
->num_pages
; i
++) {
867 struct page
*page
= ap
->pages
[i
];
870 SetPageUptodate(page
);
877 fuse_file_put(ia
->ff
, false, false);
882 static void fuse_send_readpages(struct fuse_io_args
*ia
, struct file
*file
)
884 struct fuse_file
*ff
= file
->private_data
;
885 struct fuse_conn
*fc
= ff
->fc
;
886 struct fuse_args_pages
*ap
= &ia
->ap
;
887 loff_t pos
= page_offset(ap
->pages
[0]);
888 size_t count
= ap
->num_pages
<< PAGE_SHIFT
;
892 ap
->args
.out_pages
= true;
893 ap
->args
.page_zeroing
= true;
894 ap
->args
.page_replace
= true;
896 /* Don't overflow end offset */
897 if (pos
+ (count
- 1) == LLONG_MAX
) {
899 ap
->descs
[ap
->num_pages
- 1].length
--;
901 WARN_ON((loff_t
) (pos
+ count
) < 0);
903 fuse_read_args_fill(ia
, file
, pos
, count
, FUSE_READ
);
904 ia
->read
.attr_ver
= fuse_get_attr_version(fc
);
905 if (fc
->async_read
) {
906 ia
->ff
= fuse_file_get(ff
);
907 ap
->args
.end
= fuse_readpages_end
;
908 err
= fuse_simple_background(fc
, &ap
->args
, GFP_KERNEL
);
912 res
= fuse_simple_request(fc
, &ap
->args
);
913 err
= res
< 0 ? res
: 0;
915 fuse_readpages_end(fc
, &ap
->args
, err
);
918 struct fuse_fill_data
{
919 struct fuse_io_args
*ia
;
922 unsigned int nr_pages
;
923 unsigned int max_pages
;
926 static int fuse_readpages_fill(void *_data
, struct page
*page
)
928 struct fuse_fill_data
*data
= _data
;
929 struct fuse_io_args
*ia
= data
->ia
;
930 struct fuse_args_pages
*ap
= &ia
->ap
;
931 struct inode
*inode
= data
->inode
;
932 struct fuse_conn
*fc
= get_fuse_conn(inode
);
934 fuse_wait_on_page_writeback(inode
, page
->index
);
937 (ap
->num_pages
== fc
->max_pages
||
938 (ap
->num_pages
+ 1) * PAGE_SIZE
> fc
->max_read
||
939 ap
->pages
[ap
->num_pages
- 1]->index
+ 1 != page
->index
)) {
940 data
->max_pages
= min_t(unsigned int, data
->nr_pages
,
942 fuse_send_readpages(ia
, data
->file
);
943 data
->ia
= ia
= fuse_io_alloc(NULL
, data
->max_pages
);
951 if (WARN_ON(ap
->num_pages
>= data
->max_pages
)) {
958 ap
->pages
[ap
->num_pages
] = page
;
959 ap
->descs
[ap
->num_pages
].length
= PAGE_SIZE
;
965 static int fuse_readpages(struct file
*file
, struct address_space
*mapping
,
966 struct list_head
*pages
, unsigned nr_pages
)
968 struct inode
*inode
= mapping
->host
;
969 struct fuse_conn
*fc
= get_fuse_conn(inode
);
970 struct fuse_fill_data data
;
974 if (is_bad_inode(inode
))
979 data
.nr_pages
= nr_pages
;
980 data
.max_pages
= min_t(unsigned int, nr_pages
, fc
->max_pages
);
982 data
.ia
= fuse_io_alloc(NULL
, data
.max_pages
);
987 err
= read_cache_pages(mapping
, pages
, fuse_readpages_fill
, &data
);
989 if (data
.ia
->ap
.num_pages
)
990 fuse_send_readpages(data
.ia
, file
);
992 fuse_io_free(data
.ia
);
998 static ssize_t
fuse_cache_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
1000 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
1001 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1004 * In auto invalidate mode, always update attributes on read.
1005 * Otherwise, only update if we attempt to read past EOF (to ensure
1006 * i_size is up to date).
1008 if (fc
->auto_inval_data
||
1009 (iocb
->ki_pos
+ iov_iter_count(to
) > i_size_read(inode
))) {
1011 err
= fuse_update_attributes(inode
, iocb
->ki_filp
);
1016 return generic_file_read_iter(iocb
, to
);
1019 static void fuse_write_args_fill(struct fuse_io_args
*ia
, struct fuse_file
*ff
,
1020 loff_t pos
, size_t count
)
1022 struct fuse_args
*args
= &ia
->ap
.args
;
1024 ia
->write
.in
.fh
= ff
->fh
;
1025 ia
->write
.in
.offset
= pos
;
1026 ia
->write
.in
.size
= count
;
1027 args
->opcode
= FUSE_WRITE
;
1028 args
->nodeid
= ff
->nodeid
;
1029 args
->in_numargs
= 2;
1030 if (ff
->fc
->minor
< 9)
1031 args
->in_args
[0].size
= FUSE_COMPAT_WRITE_IN_SIZE
;
1033 args
->in_args
[0].size
= sizeof(ia
->write
.in
);
1034 args
->in_args
[0].value
= &ia
->write
.in
;
1035 args
->in_args
[1].size
= count
;
1036 args
->out_numargs
= 1;
1037 args
->out_args
[0].size
= sizeof(ia
->write
.out
);
1038 args
->out_args
[0].value
= &ia
->write
.out
;
1041 static unsigned int fuse_write_flags(struct kiocb
*iocb
)
1043 unsigned int flags
= iocb
->ki_filp
->f_flags
;
1045 if (iocb
->ki_flags
& IOCB_DSYNC
)
1047 if (iocb
->ki_flags
& IOCB_SYNC
)
1053 static ssize_t
fuse_send_write(struct fuse_io_args
*ia
, loff_t pos
,
1054 size_t count
, fl_owner_t owner
)
1056 struct kiocb
*iocb
= ia
->io
->iocb
;
1057 struct file
*file
= iocb
->ki_filp
;
1058 struct fuse_file
*ff
= file
->private_data
;
1059 struct fuse_conn
*fc
= ff
->fc
;
1060 struct fuse_write_in
*inarg
= &ia
->write
.in
;
1063 fuse_write_args_fill(ia
, ff
, pos
, count
);
1064 inarg
->flags
= fuse_write_flags(iocb
);
1065 if (owner
!= NULL
) {
1066 inarg
->write_flags
|= FUSE_WRITE_LOCKOWNER
;
1067 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
1071 return fuse_async_req_send(fc
, ia
, count
);
1073 err
= fuse_simple_request(fc
, &ia
->ap
.args
);
1074 if (!err
&& ia
->write
.out
.size
> count
)
1077 return err
?: ia
->write
.out
.size
;
1080 bool fuse_write_update_size(struct inode
*inode
, loff_t pos
)
1082 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1083 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1086 spin_lock(&fi
->lock
);
1087 fi
->attr_version
= atomic64_inc_return(&fc
->attr_version
);
1088 if (pos
> inode
->i_size
) {
1089 i_size_write(inode
, pos
);
1092 spin_unlock(&fi
->lock
);
1097 static ssize_t
fuse_send_write_pages(struct fuse_io_args
*ia
,
1098 struct kiocb
*iocb
, struct inode
*inode
,
1099 loff_t pos
, size_t count
)
1101 struct fuse_args_pages
*ap
= &ia
->ap
;
1102 struct file
*file
= iocb
->ki_filp
;
1103 struct fuse_file
*ff
= file
->private_data
;
1104 struct fuse_conn
*fc
= ff
->fc
;
1105 unsigned int offset
, i
;
1108 for (i
= 0; i
< ap
->num_pages
; i
++)
1109 fuse_wait_on_page_writeback(inode
, ap
->pages
[i
]->index
);
1111 fuse_write_args_fill(ia
, ff
, pos
, count
);
1112 ia
->write
.in
.flags
= fuse_write_flags(iocb
);
1114 err
= fuse_simple_request(fc
, &ap
->args
);
1115 if (!err
&& ia
->write
.out
.size
> count
)
1118 offset
= ap
->descs
[0].offset
;
1119 count
= ia
->write
.out
.size
;
1120 for (i
= 0; i
< ap
->num_pages
; i
++) {
1121 struct page
*page
= ap
->pages
[i
];
1123 if (!err
&& !offset
&& count
>= PAGE_SIZE
)
1124 SetPageUptodate(page
);
1126 if (count
> PAGE_SIZE
- offset
)
1127 count
-= PAGE_SIZE
- offset
;
1139 static ssize_t
fuse_fill_write_pages(struct fuse_args_pages
*ap
,
1140 struct address_space
*mapping
,
1141 struct iov_iter
*ii
, loff_t pos
,
1142 unsigned int max_pages
)
1144 struct fuse_conn
*fc
= get_fuse_conn(mapping
->host
);
1145 unsigned offset
= pos
& (PAGE_SIZE
- 1);
1149 ap
->args
.in_pages
= true;
1150 ap
->descs
[0].offset
= offset
;
1155 pgoff_t index
= pos
>> PAGE_SHIFT
;
1156 size_t bytes
= min_t(size_t, PAGE_SIZE
- offset
,
1157 iov_iter_count(ii
));
1159 bytes
= min_t(size_t, bytes
, fc
->max_write
- count
);
1163 if (iov_iter_fault_in_readable(ii
, bytes
))
1167 page
= grab_cache_page_write_begin(mapping
, index
, 0);
1171 if (mapping_writably_mapped(mapping
))
1172 flush_dcache_page(page
);
1174 tmp
= iov_iter_copy_from_user_atomic(page
, ii
, offset
, bytes
);
1175 flush_dcache_page(page
);
1177 iov_iter_advance(ii
, tmp
);
1181 bytes
= min(bytes
, iov_iter_single_seg_count(ii
));
1186 ap
->pages
[ap
->num_pages
] = page
;
1187 ap
->descs
[ap
->num_pages
].length
= tmp
;
1193 if (offset
== PAGE_SIZE
)
1196 if (!fc
->big_writes
)
1198 } while (iov_iter_count(ii
) && count
< fc
->max_write
&&
1199 ap
->num_pages
< max_pages
&& offset
== 0);
1201 return count
> 0 ? count
: err
;
1204 static inline unsigned int fuse_wr_pages(loff_t pos
, size_t len
,
1205 unsigned int max_pages
)
1207 return min_t(unsigned int,
1208 ((pos
+ len
- 1) >> PAGE_SHIFT
) -
1209 (pos
>> PAGE_SHIFT
) + 1,
1213 static ssize_t
fuse_perform_write(struct kiocb
*iocb
,
1214 struct address_space
*mapping
,
1215 struct iov_iter
*ii
, loff_t pos
)
1217 struct inode
*inode
= mapping
->host
;
1218 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1219 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1223 if (inode
->i_size
< pos
+ iov_iter_count(ii
))
1224 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1228 struct fuse_io_args ia
= {};
1229 struct fuse_args_pages
*ap
= &ia
.ap
;
1230 unsigned int nr_pages
= fuse_wr_pages(pos
, iov_iter_count(ii
),
1233 ap
->pages
= fuse_pages_alloc(nr_pages
, GFP_KERNEL
, &ap
->descs
);
1239 count
= fuse_fill_write_pages(ap
, mapping
, ii
, pos
, nr_pages
);
1243 err
= fuse_send_write_pages(&ia
, iocb
, inode
,
1246 size_t num_written
= ia
.write
.out
.size
;
1251 /* break out of the loop on short write */
1252 if (num_written
!= count
)
1257 } while (!err
&& iov_iter_count(ii
));
1260 fuse_write_update_size(inode
, pos
);
1262 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1263 fuse_invalidate_attr(inode
);
1265 return res
> 0 ? res
: err
;
1268 static ssize_t
fuse_cache_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
1270 struct file
*file
= iocb
->ki_filp
;
1271 struct address_space
*mapping
= file
->f_mapping
;
1272 ssize_t written
= 0;
1273 ssize_t written_buffered
= 0;
1274 struct inode
*inode
= mapping
->host
;
1278 if (get_fuse_conn(inode
)->writeback_cache
) {
1279 /* Update size (EOF optimization) and mode (SUID clearing) */
1280 err
= fuse_update_attributes(mapping
->host
, file
);
1284 return generic_file_write_iter(iocb
, from
);
1289 /* We can write back this queue in page reclaim */
1290 current
->backing_dev_info
= inode_to_bdi(inode
);
1292 err
= generic_write_checks(iocb
, from
);
1296 err
= file_remove_privs(file
);
1300 err
= file_update_time(file
);
1304 if (iocb
->ki_flags
& IOCB_DIRECT
) {
1305 loff_t pos
= iocb
->ki_pos
;
1306 written
= generic_file_direct_write(iocb
, from
);
1307 if (written
< 0 || !iov_iter_count(from
))
1312 written_buffered
= fuse_perform_write(iocb
, mapping
, from
, pos
);
1313 if (written_buffered
< 0) {
1314 err
= written_buffered
;
1317 endbyte
= pos
+ written_buffered
- 1;
1319 err
= filemap_write_and_wait_range(file
->f_mapping
, pos
,
1324 invalidate_mapping_pages(file
->f_mapping
,
1326 endbyte
>> PAGE_SHIFT
);
1328 written
+= written_buffered
;
1329 iocb
->ki_pos
= pos
+ written_buffered
;
1331 written
= fuse_perform_write(iocb
, mapping
, from
, iocb
->ki_pos
);
1333 iocb
->ki_pos
+= written
;
1336 current
->backing_dev_info
= NULL
;
1337 inode_unlock(inode
);
1339 written
= generic_write_sync(iocb
, written
);
1341 return written
? written
: err
;
1344 static inline void fuse_page_descs_length_init(struct fuse_page_desc
*descs
,
1346 unsigned int nr_pages
)
1350 for (i
= index
; i
< index
+ nr_pages
; i
++)
1351 descs
[i
].length
= PAGE_SIZE
- descs
[i
].offset
;
1354 static inline unsigned long fuse_get_user_addr(const struct iov_iter
*ii
)
1356 return (unsigned long)ii
->iov
->iov_base
+ ii
->iov_offset
;
1359 static inline size_t fuse_get_frag_size(const struct iov_iter
*ii
,
1362 return min(iov_iter_single_seg_count(ii
), max_size
);
1365 static int fuse_get_user_pages(struct fuse_args_pages
*ap
, struct iov_iter
*ii
,
1366 size_t *nbytesp
, int write
,
1367 unsigned int max_pages
)
1369 size_t nbytes
= 0; /* # bytes already packed in req */
1372 /* Special case for kernel I/O: can copy directly into the buffer */
1373 if (iov_iter_is_kvec(ii
)) {
1374 unsigned long user_addr
= fuse_get_user_addr(ii
);
1375 size_t frag_size
= fuse_get_frag_size(ii
, *nbytesp
);
1378 ap
->args
.in_args
[1].value
= (void *) user_addr
;
1380 ap
->args
.out_args
[0].value
= (void *) user_addr
;
1382 iov_iter_advance(ii
, frag_size
);
1383 *nbytesp
= frag_size
;
1387 while (nbytes
< *nbytesp
&& ap
->num_pages
< max_pages
) {
1390 ret
= iov_iter_get_pages(ii
, &ap
->pages
[ap
->num_pages
],
1392 max_pages
- ap
->num_pages
,
1397 iov_iter_advance(ii
, ret
);
1401 npages
= (ret
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
1403 ap
->descs
[ap
->num_pages
].offset
= start
;
1404 fuse_page_descs_length_init(ap
->descs
, ap
->num_pages
, npages
);
1406 ap
->num_pages
+= npages
;
1407 ap
->descs
[ap
->num_pages
- 1].length
-=
1408 (PAGE_SIZE
- ret
) & (PAGE_SIZE
- 1);
1412 ap
->args
.in_pages
= 1;
1414 ap
->args
.out_pages
= 1;
1418 return ret
< 0 ? ret
: 0;
1421 ssize_t
fuse_direct_io(struct fuse_io_priv
*io
, struct iov_iter
*iter
,
1422 loff_t
*ppos
, int flags
)
1424 int write
= flags
& FUSE_DIO_WRITE
;
1425 int cuse
= flags
& FUSE_DIO_CUSE
;
1426 struct file
*file
= io
->iocb
->ki_filp
;
1427 struct inode
*inode
= file
->f_mapping
->host
;
1428 struct fuse_file
*ff
= file
->private_data
;
1429 struct fuse_conn
*fc
= ff
->fc
;
1430 size_t nmax
= write
? fc
->max_write
: fc
->max_read
;
1432 size_t count
= iov_iter_count(iter
);
1433 pgoff_t idx_from
= pos
>> PAGE_SHIFT
;
1434 pgoff_t idx_to
= (pos
+ count
- 1) >> PAGE_SHIFT
;
1437 struct fuse_io_args
*ia
;
1438 unsigned int max_pages
;
1440 max_pages
= iov_iter_npages(iter
, fc
->max_pages
);
1441 ia
= fuse_io_alloc(io
, max_pages
);
1446 if (!cuse
&& fuse_range_is_writeback(inode
, idx_from
, idx_to
)) {
1449 fuse_sync_writes(inode
);
1451 inode_unlock(inode
);
1454 io
->should_dirty
= !write
&& iter_is_iovec(iter
);
1457 fl_owner_t owner
= current
->files
;
1458 size_t nbytes
= min(count
, nmax
);
1460 err
= fuse_get_user_pages(&ia
->ap
, iter
, &nbytes
, write
,
1466 if (!capable(CAP_FSETID
))
1467 ia
->write
.in
.write_flags
|= FUSE_WRITE_KILL_PRIV
;
1469 nres
= fuse_send_write(ia
, pos
, nbytes
, owner
);
1471 nres
= fuse_send_read(ia
, pos
, nbytes
, owner
);
1474 if (!io
->async
|| nres
< 0) {
1475 fuse_release_user_pages(&ia
->ap
, io
->should_dirty
);
1480 iov_iter_revert(iter
, nbytes
);
1484 WARN_ON(nres
> nbytes
);
1489 if (nres
!= nbytes
) {
1490 iov_iter_revert(iter
, nbytes
- nres
);
1494 max_pages
= iov_iter_npages(iter
, fc
->max_pages
);
1495 ia
= fuse_io_alloc(io
, max_pages
);
1505 return res
> 0 ? res
: err
;
1507 EXPORT_SYMBOL_GPL(fuse_direct_io
);
1509 static ssize_t
__fuse_direct_read(struct fuse_io_priv
*io
,
1510 struct iov_iter
*iter
,
1514 struct inode
*inode
= file_inode(io
->iocb
->ki_filp
);
1516 res
= fuse_direct_io(io
, iter
, ppos
, 0);
1518 fuse_invalidate_atime(inode
);
1523 static ssize_t
fuse_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
);
1525 static ssize_t
fuse_direct_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
1529 if (!is_sync_kiocb(iocb
) && iocb
->ki_flags
& IOCB_DIRECT
) {
1530 res
= fuse_direct_IO(iocb
, to
);
1532 struct fuse_io_priv io
= FUSE_IO_PRIV_SYNC(iocb
);
1534 res
= __fuse_direct_read(&io
, to
, &iocb
->ki_pos
);
1540 static ssize_t
fuse_direct_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
1542 struct inode
*inode
= file_inode(iocb
->ki_filp
);
1543 struct fuse_io_priv io
= FUSE_IO_PRIV_SYNC(iocb
);
1546 /* Don't allow parallel writes to the same file */
1548 res
= generic_write_checks(iocb
, from
);
1550 if (!is_sync_kiocb(iocb
) && iocb
->ki_flags
& IOCB_DIRECT
) {
1551 res
= fuse_direct_IO(iocb
, from
);
1553 res
= fuse_direct_io(&io
, from
, &iocb
->ki_pos
,
1557 fuse_invalidate_attr(inode
);
1559 fuse_write_update_size(inode
, iocb
->ki_pos
);
1560 inode_unlock(inode
);
1565 static ssize_t
fuse_file_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
1567 struct file
*file
= iocb
->ki_filp
;
1568 struct fuse_file
*ff
= file
->private_data
;
1570 if (is_bad_inode(file_inode(file
)))
1573 if (!(ff
->open_flags
& FOPEN_DIRECT_IO
))
1574 return fuse_cache_read_iter(iocb
, to
);
1576 return fuse_direct_read_iter(iocb
, to
);
1579 static ssize_t
fuse_file_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
1581 struct file
*file
= iocb
->ki_filp
;
1582 struct fuse_file
*ff
= file
->private_data
;
1584 if (is_bad_inode(file_inode(file
)))
1587 if (!(ff
->open_flags
& FOPEN_DIRECT_IO
))
1588 return fuse_cache_write_iter(iocb
, from
);
1590 return fuse_direct_write_iter(iocb
, from
);
1593 static void fuse_writepage_free(struct fuse_writepage_args
*wpa
)
1595 struct fuse_args_pages
*ap
= &wpa
->ia
.ap
;
1598 for (i
= 0; i
< ap
->num_pages
; i
++)
1599 __free_page(ap
->pages
[i
]);
1602 fuse_file_put(wpa
->ia
.ff
, false, false);
1608 static void fuse_writepage_finish(struct fuse_conn
*fc
,
1609 struct fuse_writepage_args
*wpa
)
1611 struct fuse_args_pages
*ap
= &wpa
->ia
.ap
;
1612 struct inode
*inode
= wpa
->inode
;
1613 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1614 struct backing_dev_info
*bdi
= inode_to_bdi(inode
);
1617 list_del(&wpa
->writepages_entry
);
1618 for (i
= 0; i
< ap
->num_pages
; i
++) {
1619 dec_wb_stat(&bdi
->wb
, WB_WRITEBACK
);
1620 dec_node_page_state(ap
->pages
[i
], NR_WRITEBACK_TEMP
);
1621 wb_writeout_inc(&bdi
->wb
);
1623 wake_up(&fi
->page_waitq
);
1626 /* Called under fi->lock, may release and reacquire it */
1627 static void fuse_send_writepage(struct fuse_conn
*fc
,
1628 struct fuse_writepage_args
*wpa
, loff_t size
)
1629 __releases(fi
->lock
)
1630 __acquires(fi
->lock
)
1632 struct fuse_writepage_args
*aux
, *next
;
1633 struct fuse_inode
*fi
= get_fuse_inode(wpa
->inode
);
1634 struct fuse_write_in
*inarg
= &wpa
->ia
.write
.in
;
1635 struct fuse_args
*args
= &wpa
->ia
.ap
.args
;
1636 __u64 data_size
= wpa
->ia
.ap
.num_pages
* PAGE_SIZE
;
1640 if (inarg
->offset
+ data_size
<= size
) {
1641 inarg
->size
= data_size
;
1642 } else if (inarg
->offset
< size
) {
1643 inarg
->size
= size
- inarg
->offset
;
1645 /* Got truncated off completely */
1649 args
->in_args
[1].size
= inarg
->size
;
1651 args
->nocreds
= true;
1653 err
= fuse_simple_background(fc
, args
, GFP_ATOMIC
);
1654 if (err
== -ENOMEM
) {
1655 spin_unlock(&fi
->lock
);
1656 err
= fuse_simple_background(fc
, args
, GFP_NOFS
| __GFP_NOFAIL
);
1657 spin_lock(&fi
->lock
);
1660 /* Fails on broken connection only */
1668 fuse_writepage_finish(fc
, wpa
);
1669 spin_unlock(&fi
->lock
);
1671 /* After fuse_writepage_finish() aux request list is private */
1672 for (aux
= wpa
->next
; aux
; aux
= next
) {
1675 fuse_writepage_free(aux
);
1678 fuse_writepage_free(wpa
);
1679 spin_lock(&fi
->lock
);
1683 * If fi->writectr is positive (no truncate or fsync going on) send
1684 * all queued writepage requests.
1686 * Called with fi->lock
1688 void fuse_flush_writepages(struct inode
*inode
)
1689 __releases(fi
->lock
)
1690 __acquires(fi
->lock
)
1692 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1693 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1694 loff_t crop
= i_size_read(inode
);
1695 struct fuse_writepage_args
*wpa
;
1697 while (fi
->writectr
>= 0 && !list_empty(&fi
->queued_writes
)) {
1698 wpa
= list_entry(fi
->queued_writes
.next
,
1699 struct fuse_writepage_args
, queue_entry
);
1700 list_del_init(&wpa
->queue_entry
);
1701 fuse_send_writepage(fc
, wpa
, crop
);
1705 static void fuse_writepage_end(struct fuse_conn
*fc
, struct fuse_args
*args
,
1708 struct fuse_writepage_args
*wpa
=
1709 container_of(args
, typeof(*wpa
), ia
.ap
.args
);
1710 struct inode
*inode
= wpa
->inode
;
1711 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1713 mapping_set_error(inode
->i_mapping
, error
);
1714 spin_lock(&fi
->lock
);
1716 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1717 struct fuse_write_in
*inarg
= &wpa
->ia
.write
.in
;
1718 struct fuse_writepage_args
*next
= wpa
->next
;
1720 wpa
->next
= next
->next
;
1722 next
->ia
.ff
= fuse_file_get(wpa
->ia
.ff
);
1723 list_add(&next
->writepages_entry
, &fi
->writepages
);
1726 * Skip fuse_flush_writepages() to make it easy to crop requests
1727 * based on primary request size.
1729 * 1st case (trivial): there are no concurrent activities using
1730 * fuse_set/release_nowrite. Then we're on safe side because
1731 * fuse_flush_writepages() would call fuse_send_writepage()
1734 * 2nd case: someone called fuse_set_nowrite and it is waiting
1735 * now for completion of all in-flight requests. This happens
1736 * rarely and no more than once per page, so this should be
1739 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1740 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1741 * that fuse_set_nowrite returned implies that all in-flight
1742 * requests were completed along with all of their secondary
1743 * requests. Further primary requests are blocked by negative
1744 * writectr. Hence there cannot be any in-flight requests and
1745 * no invocations of fuse_writepage_end() while we're in
1746 * fuse_set_nowrite..fuse_release_nowrite section.
1748 fuse_send_writepage(fc
, next
, inarg
->offset
+ inarg
->size
);
1751 fuse_writepage_finish(fc
, wpa
);
1752 spin_unlock(&fi
->lock
);
1753 fuse_writepage_free(wpa
);
1756 static struct fuse_file
*__fuse_write_file_get(struct fuse_conn
*fc
,
1757 struct fuse_inode
*fi
)
1759 struct fuse_file
*ff
= NULL
;
1761 spin_lock(&fi
->lock
);
1762 if (!list_empty(&fi
->write_files
)) {
1763 ff
= list_entry(fi
->write_files
.next
, struct fuse_file
,
1767 spin_unlock(&fi
->lock
);
1772 static struct fuse_file
*fuse_write_file_get(struct fuse_conn
*fc
,
1773 struct fuse_inode
*fi
)
1775 struct fuse_file
*ff
= __fuse_write_file_get(fc
, fi
);
1780 int fuse_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
1782 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1783 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1784 struct fuse_file
*ff
;
1787 ff
= __fuse_write_file_get(fc
, fi
);
1788 err
= fuse_flush_times(inode
, ff
);
1790 fuse_file_put(ff
, false, false);
1795 static struct fuse_writepage_args
*fuse_writepage_args_alloc(void)
1797 struct fuse_writepage_args
*wpa
;
1798 struct fuse_args_pages
*ap
;
1800 wpa
= kzalloc(sizeof(*wpa
), GFP_NOFS
);
1804 ap
->pages
= fuse_pages_alloc(1, GFP_NOFS
, &ap
->descs
);
1814 static int fuse_writepage_locked(struct page
*page
)
1816 struct address_space
*mapping
= page
->mapping
;
1817 struct inode
*inode
= mapping
->host
;
1818 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1819 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1820 struct fuse_writepage_args
*wpa
;
1821 struct fuse_args_pages
*ap
;
1822 struct page
*tmp_page
;
1823 int error
= -ENOMEM
;
1825 set_page_writeback(page
);
1827 wpa
= fuse_writepage_args_alloc();
1832 tmp_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
1837 wpa
->ia
.ff
= fuse_write_file_get(fc
, fi
);
1841 fuse_write_args_fill(&wpa
->ia
, wpa
->ia
.ff
, page_offset(page
), 0);
1843 copy_highpage(tmp_page
, page
);
1844 wpa
->ia
.write
.in
.write_flags
|= FUSE_WRITE_CACHE
;
1846 ap
->args
.in_pages
= true;
1848 ap
->pages
[0] = tmp_page
;
1849 ap
->descs
[0].offset
= 0;
1850 ap
->descs
[0].length
= PAGE_SIZE
;
1851 ap
->args
.end
= fuse_writepage_end
;
1854 inc_wb_stat(&inode_to_bdi(inode
)->wb
, WB_WRITEBACK
);
1855 inc_node_page_state(tmp_page
, NR_WRITEBACK_TEMP
);
1857 spin_lock(&fi
->lock
);
1858 list_add(&wpa
->writepages_entry
, &fi
->writepages
);
1859 list_add_tail(&wpa
->queue_entry
, &fi
->queued_writes
);
1860 fuse_flush_writepages(inode
);
1861 spin_unlock(&fi
->lock
);
1863 end_page_writeback(page
);
1868 __free_page(tmp_page
);
1872 mapping_set_error(page
->mapping
, error
);
1873 end_page_writeback(page
);
1877 static int fuse_writepage(struct page
*page
, struct writeback_control
*wbc
)
1881 if (fuse_page_is_writeback(page
->mapping
->host
, page
->index
)) {
1883 * ->writepages() should be called for sync() and friends. We
1884 * should only get here on direct reclaim and then we are
1885 * allowed to skip a page which is already in flight
1887 WARN_ON(wbc
->sync_mode
== WB_SYNC_ALL
);
1889 redirty_page_for_writepage(wbc
, page
);
1894 err
= fuse_writepage_locked(page
);
1900 struct fuse_fill_wb_data
{
1901 struct fuse_writepage_args
*wpa
;
1902 struct fuse_file
*ff
;
1903 struct inode
*inode
;
1904 struct page
**orig_pages
;
1905 unsigned int max_pages
;
1908 static bool fuse_pages_realloc(struct fuse_fill_wb_data
*data
)
1910 struct fuse_args_pages
*ap
= &data
->wpa
->ia
.ap
;
1911 struct fuse_conn
*fc
= get_fuse_conn(data
->inode
);
1912 struct page
**pages
;
1913 struct fuse_page_desc
*descs
;
1914 unsigned int npages
= min_t(unsigned int,
1915 max_t(unsigned int, data
->max_pages
* 2,
1916 FUSE_DEFAULT_MAX_PAGES_PER_REQ
),
1918 WARN_ON(npages
<= data
->max_pages
);
1920 pages
= fuse_pages_alloc(npages
, GFP_NOFS
, &descs
);
1924 memcpy(pages
, ap
->pages
, sizeof(struct page
*) * ap
->num_pages
);
1925 memcpy(descs
, ap
->descs
, sizeof(struct fuse_page_desc
) * ap
->num_pages
);
1929 data
->max_pages
= npages
;
1934 static void fuse_writepages_send(struct fuse_fill_wb_data
*data
)
1936 struct fuse_writepage_args
*wpa
= data
->wpa
;
1937 struct inode
*inode
= data
->inode
;
1938 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1939 int num_pages
= wpa
->ia
.ap
.num_pages
;
1942 wpa
->ia
.ff
= fuse_file_get(data
->ff
);
1943 spin_lock(&fi
->lock
);
1944 list_add_tail(&wpa
->queue_entry
, &fi
->queued_writes
);
1945 fuse_flush_writepages(inode
);
1946 spin_unlock(&fi
->lock
);
1948 for (i
= 0; i
< num_pages
; i
++)
1949 end_page_writeback(data
->orig_pages
[i
]);
1953 * First recheck under fi->lock if the offending offset is still under
1954 * writeback. If yes, then iterate auxiliary write requests, to see if there's
1955 * one already added for a page at this offset. If there's none, then insert
1956 * this new request onto the auxiliary list, otherwise reuse the existing one by
1957 * copying the new page contents over to the old temporary page.
1959 static bool fuse_writepage_in_flight(struct fuse_writepage_args
*new_wpa
,
1962 struct fuse_inode
*fi
= get_fuse_inode(new_wpa
->inode
);
1963 struct fuse_writepage_args
*tmp
;
1964 struct fuse_writepage_args
*old_wpa
;
1965 struct fuse_args_pages
*new_ap
= &new_wpa
->ia
.ap
;
1967 WARN_ON(new_ap
->num_pages
!= 0);
1969 spin_lock(&fi
->lock
);
1970 list_del(&new_wpa
->writepages_entry
);
1971 old_wpa
= fuse_find_writeback(fi
, page
->index
, page
->index
);
1973 list_add(&new_wpa
->writepages_entry
, &fi
->writepages
);
1974 spin_unlock(&fi
->lock
);
1978 new_ap
->num_pages
= 1;
1979 for (tmp
= old_wpa
->next
; tmp
; tmp
= tmp
->next
) {
1982 WARN_ON(tmp
->inode
!= new_wpa
->inode
);
1983 curr_index
= tmp
->ia
.write
.in
.offset
>> PAGE_SHIFT
;
1984 if (curr_index
== page
->index
) {
1985 WARN_ON(tmp
->ia
.ap
.num_pages
!= 1);
1986 swap(tmp
->ia
.ap
.pages
[0], new_ap
->pages
[0]);
1992 new_wpa
->next
= old_wpa
->next
;
1993 old_wpa
->next
= new_wpa
;
1996 spin_unlock(&fi
->lock
);
1999 struct backing_dev_info
*bdi
= inode_to_bdi(new_wpa
->inode
);
2001 dec_wb_stat(&bdi
->wb
, WB_WRITEBACK
);
2002 dec_node_page_state(new_ap
->pages
[0], NR_WRITEBACK_TEMP
);
2003 wb_writeout_inc(&bdi
->wb
);
2004 fuse_writepage_free(new_wpa
);
2010 static int fuse_writepages_fill(struct page
*page
,
2011 struct writeback_control
*wbc
, void *_data
)
2013 struct fuse_fill_wb_data
*data
= _data
;
2014 struct fuse_writepage_args
*wpa
= data
->wpa
;
2015 struct fuse_args_pages
*ap
= &wpa
->ia
.ap
;
2016 struct inode
*inode
= data
->inode
;
2017 struct fuse_inode
*fi
= get_fuse_inode(inode
);
2018 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2019 struct page
*tmp_page
;
2025 data
->ff
= fuse_write_file_get(fc
, fi
);
2031 * Being under writeback is unlikely but possible. For example direct
2032 * read to an mmaped fuse file will set the page dirty twice; once when
2033 * the pages are faulted with get_user_pages(), and then after the read
2036 is_writeback
= fuse_page_is_writeback(inode
, page
->index
);
2038 if (wpa
&& ap
->num_pages
&&
2039 (is_writeback
|| ap
->num_pages
== fc
->max_pages
||
2040 (ap
->num_pages
+ 1) * PAGE_SIZE
> fc
->max_write
||
2041 data
->orig_pages
[ap
->num_pages
- 1]->index
+ 1 != page
->index
)) {
2042 fuse_writepages_send(data
);
2044 } else if (wpa
&& ap
->num_pages
== data
->max_pages
) {
2045 if (!fuse_pages_realloc(data
)) {
2046 fuse_writepages_send(data
);
2052 tmp_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
2057 * The page must not be redirtied until the writeout is completed
2058 * (i.e. userspace has sent a reply to the write request). Otherwise
2059 * there could be more than one temporary page instance for each real
2062 * This is ensured by holding the page lock in page_mkwrite() while
2063 * checking fuse_page_is_writeback(). We already hold the page lock
2064 * since clear_page_dirty_for_io() and keep it held until we add the
2065 * request to the fi->writepages list and increment ap->num_pages.
2066 * After this fuse_page_is_writeback() will indicate that the page is
2067 * under writeback, so we can release the page lock.
2069 if (data
->wpa
== NULL
) {
2071 wpa
= fuse_writepage_args_alloc();
2073 __free_page(tmp_page
);
2076 data
->max_pages
= 1;
2079 fuse_write_args_fill(&wpa
->ia
, data
->ff
, page_offset(page
), 0);
2080 wpa
->ia
.write
.in
.write_flags
|= FUSE_WRITE_CACHE
;
2082 ap
->args
.in_pages
= true;
2083 ap
->args
.end
= fuse_writepage_end
;
2087 spin_lock(&fi
->lock
);
2088 list_add(&wpa
->writepages_entry
, &fi
->writepages
);
2089 spin_unlock(&fi
->lock
);
2093 set_page_writeback(page
);
2095 copy_highpage(tmp_page
, page
);
2096 ap
->pages
[ap
->num_pages
] = tmp_page
;
2097 ap
->descs
[ap
->num_pages
].offset
= 0;
2098 ap
->descs
[ap
->num_pages
].length
= PAGE_SIZE
;
2100 inc_wb_stat(&inode_to_bdi(inode
)->wb
, WB_WRITEBACK
);
2101 inc_node_page_state(tmp_page
, NR_WRITEBACK_TEMP
);
2104 if (is_writeback
&& fuse_writepage_in_flight(wpa
, page
)) {
2105 end_page_writeback(page
);
2109 data
->orig_pages
[ap
->num_pages
] = page
;
2112 * Protected by fi->lock against concurrent access by
2113 * fuse_page_is_writeback().
2115 spin_lock(&fi
->lock
);
2117 spin_unlock(&fi
->lock
);
2125 static int fuse_writepages(struct address_space
*mapping
,
2126 struct writeback_control
*wbc
)
2128 struct inode
*inode
= mapping
->host
;
2129 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2130 struct fuse_fill_wb_data data
;
2134 if (is_bad_inode(inode
))
2142 data
.orig_pages
= kcalloc(fc
->max_pages
,
2143 sizeof(struct page
*),
2145 if (!data
.orig_pages
)
2148 err
= write_cache_pages(mapping
, wbc
, fuse_writepages_fill
, &data
);
2150 /* Ignore errors if we can write at least one page */
2151 WARN_ON(!data
.wpa
->ia
.ap
.num_pages
);
2152 fuse_writepages_send(&data
);
2156 fuse_file_put(data
.ff
, false, false);
2158 kfree(data
.orig_pages
);
2164 * It's worthy to make sure that space is reserved on disk for the write,
2165 * but how to implement it without killing performance need more thinking.
2167 static int fuse_write_begin(struct file
*file
, struct address_space
*mapping
,
2168 loff_t pos
, unsigned len
, unsigned flags
,
2169 struct page
**pagep
, void **fsdata
)
2171 pgoff_t index
= pos
>> PAGE_SHIFT
;
2172 struct fuse_conn
*fc
= get_fuse_conn(file_inode(file
));
2177 WARN_ON(!fc
->writeback_cache
);
2179 page
= grab_cache_page_write_begin(mapping
, index
, flags
);
2183 fuse_wait_on_page_writeback(mapping
->host
, page
->index
);
2185 if (PageUptodate(page
) || len
== PAGE_SIZE
)
2188 * Check if the start this page comes after the end of file, in which
2189 * case the readpage can be optimized away.
2191 fsize
= i_size_read(mapping
->host
);
2192 if (fsize
<= (pos
& PAGE_MASK
)) {
2193 size_t off
= pos
& ~PAGE_MASK
;
2195 zero_user_segment(page
, 0, off
);
2198 err
= fuse_do_readpage(file
, page
);
2212 static int fuse_write_end(struct file
*file
, struct address_space
*mapping
,
2213 loff_t pos
, unsigned len
, unsigned copied
,
2214 struct page
*page
, void *fsdata
)
2216 struct inode
*inode
= page
->mapping
->host
;
2218 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2222 if (!PageUptodate(page
)) {
2223 /* Zero any unwritten bytes at the end of the page */
2224 size_t endoff
= (pos
+ copied
) & ~PAGE_MASK
;
2226 zero_user_segment(page
, endoff
, PAGE_SIZE
);
2227 SetPageUptodate(page
);
2230 fuse_write_update_size(inode
, pos
+ copied
);
2231 set_page_dirty(page
);
2240 static int fuse_launder_page(struct page
*page
)
2243 if (clear_page_dirty_for_io(page
)) {
2244 struct inode
*inode
= page
->mapping
->host
;
2245 err
= fuse_writepage_locked(page
);
2247 fuse_wait_on_page_writeback(inode
, page
->index
);
2253 * Write back dirty pages now, because there may not be any suitable
2256 static void fuse_vma_close(struct vm_area_struct
*vma
)
2258 filemap_write_and_wait(vma
->vm_file
->f_mapping
);
2262 * Wait for writeback against this page to complete before allowing it
2263 * to be marked dirty again, and hence written back again, possibly
2264 * before the previous writepage completed.
2266 * Block here, instead of in ->writepage(), so that the userspace fs
2267 * can only block processes actually operating on the filesystem.
2269 * Otherwise unprivileged userspace fs would be able to block
2274 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2276 static vm_fault_t
fuse_page_mkwrite(struct vm_fault
*vmf
)
2278 struct page
*page
= vmf
->page
;
2279 struct inode
*inode
= file_inode(vmf
->vma
->vm_file
);
2281 file_update_time(vmf
->vma
->vm_file
);
2283 if (page
->mapping
!= inode
->i_mapping
) {
2285 return VM_FAULT_NOPAGE
;
2288 fuse_wait_on_page_writeback(inode
, page
->index
);
2289 return VM_FAULT_LOCKED
;
2292 static const struct vm_operations_struct fuse_file_vm_ops
= {
2293 .close
= fuse_vma_close
,
2294 .fault
= filemap_fault
,
2295 .map_pages
= filemap_map_pages
,
2296 .page_mkwrite
= fuse_page_mkwrite
,
2299 static int fuse_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2301 struct fuse_file
*ff
= file
->private_data
;
2303 if (ff
->open_flags
& FOPEN_DIRECT_IO
) {
2304 /* Can't provide the coherency needed for MAP_SHARED */
2305 if (vma
->vm_flags
& VM_MAYSHARE
)
2308 invalidate_inode_pages2(file
->f_mapping
);
2310 return generic_file_mmap(file
, vma
);
2313 if ((vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
))
2314 fuse_link_write_file(file
);
2316 file_accessed(file
);
2317 vma
->vm_ops
= &fuse_file_vm_ops
;
2321 static int convert_fuse_file_lock(struct fuse_conn
*fc
,
2322 const struct fuse_file_lock
*ffl
,
2323 struct file_lock
*fl
)
2325 switch (ffl
->type
) {
2331 if (ffl
->start
> OFFSET_MAX
|| ffl
->end
> OFFSET_MAX
||
2332 ffl
->end
< ffl
->start
)
2335 fl
->fl_start
= ffl
->start
;
2336 fl
->fl_end
= ffl
->end
;
2339 * Convert pid into init's pid namespace. The locks API will
2340 * translate it into the caller's pid namespace.
2343 fl
->fl_pid
= pid_nr_ns(find_pid_ns(ffl
->pid
, fc
->pid_ns
), &init_pid_ns
);
2350 fl
->fl_type
= ffl
->type
;
2354 static void fuse_lk_fill(struct fuse_args
*args
, struct file
*file
,
2355 const struct file_lock
*fl
, int opcode
, pid_t pid
,
2356 int flock
, struct fuse_lk_in
*inarg
)
2358 struct inode
*inode
= file_inode(file
);
2359 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2360 struct fuse_file
*ff
= file
->private_data
;
2362 memset(inarg
, 0, sizeof(*inarg
));
2364 inarg
->owner
= fuse_lock_owner_id(fc
, fl
->fl_owner
);
2365 inarg
->lk
.start
= fl
->fl_start
;
2366 inarg
->lk
.end
= fl
->fl_end
;
2367 inarg
->lk
.type
= fl
->fl_type
;
2368 inarg
->lk
.pid
= pid
;
2370 inarg
->lk_flags
|= FUSE_LK_FLOCK
;
2371 args
->opcode
= opcode
;
2372 args
->nodeid
= get_node_id(inode
);
2373 args
->in_numargs
= 1;
2374 args
->in_args
[0].size
= sizeof(*inarg
);
2375 args
->in_args
[0].value
= inarg
;
2378 static int fuse_getlk(struct file
*file
, struct file_lock
*fl
)
2380 struct inode
*inode
= file_inode(file
);
2381 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2383 struct fuse_lk_in inarg
;
2384 struct fuse_lk_out outarg
;
2387 fuse_lk_fill(&args
, file
, fl
, FUSE_GETLK
, 0, 0, &inarg
);
2388 args
.out_numargs
= 1;
2389 args
.out_args
[0].size
= sizeof(outarg
);
2390 args
.out_args
[0].value
= &outarg
;
2391 err
= fuse_simple_request(fc
, &args
);
2393 err
= convert_fuse_file_lock(fc
, &outarg
.lk
, fl
);
2398 static int fuse_setlk(struct file
*file
, struct file_lock
*fl
, int flock
)
2400 struct inode
*inode
= file_inode(file
);
2401 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2403 struct fuse_lk_in inarg
;
2404 int opcode
= (fl
->fl_flags
& FL_SLEEP
) ? FUSE_SETLKW
: FUSE_SETLK
;
2405 struct pid
*pid
= fl
->fl_type
!= F_UNLCK
? task_tgid(current
) : NULL
;
2406 pid_t pid_nr
= pid_nr_ns(pid
, fc
->pid_ns
);
2409 if (fl
->fl_lmops
&& fl
->fl_lmops
->lm_grant
) {
2410 /* NLM needs asynchronous locks, which we don't support yet */
2414 /* Unlock on close is handled by the flush method */
2415 if ((fl
->fl_flags
& FL_CLOSE_POSIX
) == FL_CLOSE_POSIX
)
2418 fuse_lk_fill(&args
, file
, fl
, opcode
, pid_nr
, flock
, &inarg
);
2419 err
= fuse_simple_request(fc
, &args
);
2421 /* locking is restartable */
2428 static int fuse_file_lock(struct file
*file
, int cmd
, struct file_lock
*fl
)
2430 struct inode
*inode
= file_inode(file
);
2431 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2434 if (cmd
== F_CANCELLK
) {
2436 } else if (cmd
== F_GETLK
) {
2438 posix_test_lock(file
, fl
);
2441 err
= fuse_getlk(file
, fl
);
2444 err
= posix_lock_file(file
, fl
, NULL
);
2446 err
= fuse_setlk(file
, fl
, 0);
2451 static int fuse_file_flock(struct file
*file
, int cmd
, struct file_lock
*fl
)
2453 struct inode
*inode
= file_inode(file
);
2454 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2458 err
= locks_lock_file_wait(file
, fl
);
2460 struct fuse_file
*ff
= file
->private_data
;
2462 /* emulate flock with POSIX locks */
2464 err
= fuse_setlk(file
, fl
, 1);
2470 static sector_t
fuse_bmap(struct address_space
*mapping
, sector_t block
)
2472 struct inode
*inode
= mapping
->host
;
2473 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2475 struct fuse_bmap_in inarg
;
2476 struct fuse_bmap_out outarg
;
2479 if (!inode
->i_sb
->s_bdev
|| fc
->no_bmap
)
2482 memset(&inarg
, 0, sizeof(inarg
));
2483 inarg
.block
= block
;
2484 inarg
.blocksize
= inode
->i_sb
->s_blocksize
;
2485 args
.opcode
= FUSE_BMAP
;
2486 args
.nodeid
= get_node_id(inode
);
2487 args
.in_numargs
= 1;
2488 args
.in_args
[0].size
= sizeof(inarg
);
2489 args
.in_args
[0].value
= &inarg
;
2490 args
.out_numargs
= 1;
2491 args
.out_args
[0].size
= sizeof(outarg
);
2492 args
.out_args
[0].value
= &outarg
;
2493 err
= fuse_simple_request(fc
, &args
);
2497 return err
? 0 : outarg
.block
;
2500 static loff_t
fuse_lseek(struct file
*file
, loff_t offset
, int whence
)
2502 struct inode
*inode
= file
->f_mapping
->host
;
2503 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2504 struct fuse_file
*ff
= file
->private_data
;
2506 struct fuse_lseek_in inarg
= {
2511 struct fuse_lseek_out outarg
;
2517 args
.opcode
= FUSE_LSEEK
;
2518 args
.nodeid
= ff
->nodeid
;
2519 args
.in_numargs
= 1;
2520 args
.in_args
[0].size
= sizeof(inarg
);
2521 args
.in_args
[0].value
= &inarg
;
2522 args
.out_numargs
= 1;
2523 args
.out_args
[0].size
= sizeof(outarg
);
2524 args
.out_args
[0].value
= &outarg
;
2525 err
= fuse_simple_request(fc
, &args
);
2527 if (err
== -ENOSYS
) {
2534 return vfs_setpos(file
, outarg
.offset
, inode
->i_sb
->s_maxbytes
);
2537 err
= fuse_update_attributes(inode
, file
);
2539 return generic_file_llseek(file
, offset
, whence
);
2544 static loff_t
fuse_file_llseek(struct file
*file
, loff_t offset
, int whence
)
2547 struct inode
*inode
= file_inode(file
);
2552 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2553 retval
= generic_file_llseek(file
, offset
, whence
);
2557 retval
= fuse_update_attributes(inode
, file
);
2559 retval
= generic_file_llseek(file
, offset
, whence
);
2560 inode_unlock(inode
);
2565 retval
= fuse_lseek(file
, offset
, whence
);
2566 inode_unlock(inode
);
2576 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2577 * ABI was defined to be 'struct iovec' which is different on 32bit
2578 * and 64bit. Fortunately we can determine which structure the server
2579 * used from the size of the reply.
2581 static int fuse_copy_ioctl_iovec_old(struct iovec
*dst
, void *src
,
2582 size_t transferred
, unsigned count
,
2585 #ifdef CONFIG_COMPAT
2586 if (count
* sizeof(struct compat_iovec
) == transferred
) {
2587 struct compat_iovec
*ciov
= src
;
2591 * With this interface a 32bit server cannot support
2592 * non-compat (i.e. ones coming from 64bit apps) ioctl
2598 for (i
= 0; i
< count
; i
++) {
2599 dst
[i
].iov_base
= compat_ptr(ciov
[i
].iov_base
);
2600 dst
[i
].iov_len
= ciov
[i
].iov_len
;
2606 if (count
* sizeof(struct iovec
) != transferred
)
2609 memcpy(dst
, src
, transferred
);
2613 /* Make sure iov_length() won't overflow */
2614 static int fuse_verify_ioctl_iov(struct fuse_conn
*fc
, struct iovec
*iov
,
2618 u32 max
= fc
->max_pages
<< PAGE_SHIFT
;
2620 for (n
= 0; n
< count
; n
++, iov
++) {
2621 if (iov
->iov_len
> (size_t) max
)
2623 max
-= iov
->iov_len
;
2628 static int fuse_copy_ioctl_iovec(struct fuse_conn
*fc
, struct iovec
*dst
,
2629 void *src
, size_t transferred
, unsigned count
,
2633 struct fuse_ioctl_iovec
*fiov
= src
;
2635 if (fc
->minor
< 16) {
2636 return fuse_copy_ioctl_iovec_old(dst
, src
, transferred
,
2640 if (count
* sizeof(struct fuse_ioctl_iovec
) != transferred
)
2643 for (i
= 0; i
< count
; i
++) {
2644 /* Did the server supply an inappropriate value? */
2645 if (fiov
[i
].base
!= (unsigned long) fiov
[i
].base
||
2646 fiov
[i
].len
!= (unsigned long) fiov
[i
].len
)
2649 dst
[i
].iov_base
= (void __user
*) (unsigned long) fiov
[i
].base
;
2650 dst
[i
].iov_len
= (size_t) fiov
[i
].len
;
2652 #ifdef CONFIG_COMPAT
2654 (ptr_to_compat(dst
[i
].iov_base
) != fiov
[i
].base
||
2655 (compat_size_t
) dst
[i
].iov_len
!= fiov
[i
].len
))
2665 * For ioctls, there is no generic way to determine how much memory
2666 * needs to be read and/or written. Furthermore, ioctls are allowed
2667 * to dereference the passed pointer, so the parameter requires deep
2668 * copying but FUSE has no idea whatsoever about what to copy in or
2671 * This is solved by allowing FUSE server to retry ioctl with
2672 * necessary in/out iovecs. Let's assume the ioctl implementation
2673 * needs to read in the following structure.
2680 * On the first callout to FUSE server, inarg->in_size and
2681 * inarg->out_size will be NULL; then, the server completes the ioctl
2682 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2683 * the actual iov array to
2685 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2687 * which tells FUSE to copy in the requested area and retry the ioctl.
2688 * On the second round, the server has access to the structure and
2689 * from that it can tell what to look for next, so on the invocation,
2690 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2692 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2693 * { .iov_base = a.buf, .iov_len = a.buflen } }
2695 * FUSE will copy both struct a and the pointed buffer from the
2696 * process doing the ioctl and retry ioctl with both struct a and the
2699 * This time, FUSE server has everything it needs and completes ioctl
2700 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2702 * Copying data out works the same way.
2704 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2705 * automatically initializes in and out iovs by decoding @cmd with
2706 * _IOC_* macros and the server is not allowed to request RETRY. This
2707 * limits ioctl data transfers to well-formed ioctls and is the forced
2708 * behavior for all FUSE servers.
2710 long fuse_do_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
,
2713 struct fuse_file
*ff
= file
->private_data
;
2714 struct fuse_conn
*fc
= ff
->fc
;
2715 struct fuse_ioctl_in inarg
= {
2721 struct fuse_ioctl_out outarg
;
2722 struct iovec
*iov_page
= NULL
;
2723 struct iovec
*in_iov
= NULL
, *out_iov
= NULL
;
2724 unsigned int in_iovs
= 0, out_iovs
= 0, max_pages
;
2725 size_t in_size
, out_size
, c
;
2726 ssize_t transferred
;
2729 struct fuse_args_pages ap
= {};
2731 #if BITS_PER_LONG == 32
2732 inarg
.flags
|= FUSE_IOCTL_32BIT
;
2734 if (flags
& FUSE_IOCTL_COMPAT
) {
2735 inarg
.flags
|= FUSE_IOCTL_32BIT
;
2736 #ifdef CONFIG_X86_X32
2737 if (in_x32_syscall())
2738 inarg
.flags
|= FUSE_IOCTL_COMPAT_X32
;
2743 /* assume all the iovs returned by client always fits in a page */
2744 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec
) * FUSE_IOCTL_MAX_IOV
> PAGE_SIZE
);
2747 ap
.pages
= fuse_pages_alloc(fc
->max_pages
, GFP_KERNEL
, &ap
.descs
);
2748 iov_page
= (struct iovec
*) __get_free_page(GFP_KERNEL
);
2749 if (!ap
.pages
|| !iov_page
)
2752 fuse_page_descs_length_init(ap
.descs
, 0, fc
->max_pages
);
2755 * If restricted, initialize IO parameters as encoded in @cmd.
2756 * RETRY from server is not allowed.
2758 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
)) {
2759 struct iovec
*iov
= iov_page
;
2761 iov
->iov_base
= (void __user
*)arg
;
2762 iov
->iov_len
= _IOC_SIZE(cmd
);
2764 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
2769 if (_IOC_DIR(cmd
) & _IOC_READ
) {
2776 inarg
.in_size
= in_size
= iov_length(in_iov
, in_iovs
);
2777 inarg
.out_size
= out_size
= iov_length(out_iov
, out_iovs
);
2780 * Out data can be used either for actual out data or iovs,
2781 * make sure there always is at least one page.
2783 out_size
= max_t(size_t, out_size
, PAGE_SIZE
);
2784 max_pages
= DIV_ROUND_UP(max(in_size
, out_size
), PAGE_SIZE
);
2786 /* make sure there are enough buffer pages and init request with them */
2788 if (max_pages
> fc
->max_pages
)
2790 while (ap
.num_pages
< max_pages
) {
2791 ap
.pages
[ap
.num_pages
] = alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
2792 if (!ap
.pages
[ap
.num_pages
])
2798 /* okay, let's send it to the client */
2799 ap
.args
.opcode
= FUSE_IOCTL
;
2800 ap
.args
.nodeid
= ff
->nodeid
;
2801 ap
.args
.in_numargs
= 1;
2802 ap
.args
.in_args
[0].size
= sizeof(inarg
);
2803 ap
.args
.in_args
[0].value
= &inarg
;
2805 ap
.args
.in_numargs
++;
2806 ap
.args
.in_args
[1].size
= in_size
;
2807 ap
.args
.in_pages
= true;
2810 iov_iter_init(&ii
, WRITE
, in_iov
, in_iovs
, in_size
);
2811 for (i
= 0; iov_iter_count(&ii
) && !WARN_ON(i
>= ap
.num_pages
); i
++) {
2812 c
= copy_page_from_iter(ap
.pages
[i
], 0, PAGE_SIZE
, &ii
);
2813 if (c
!= PAGE_SIZE
&& iov_iter_count(&ii
))
2818 ap
.args
.out_numargs
= 2;
2819 ap
.args
.out_args
[0].size
= sizeof(outarg
);
2820 ap
.args
.out_args
[0].value
= &outarg
;
2821 ap
.args
.out_args
[1].size
= out_size
;
2822 ap
.args
.out_pages
= true;
2823 ap
.args
.out_argvar
= true;
2825 transferred
= fuse_simple_request(fc
, &ap
.args
);
2827 if (transferred
< 0)
2830 /* did it ask for retry? */
2831 if (outarg
.flags
& FUSE_IOCTL_RETRY
) {
2834 /* no retry if in restricted mode */
2836 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
))
2839 in_iovs
= outarg
.in_iovs
;
2840 out_iovs
= outarg
.out_iovs
;
2843 * Make sure things are in boundary, separate checks
2844 * are to protect against overflow.
2847 if (in_iovs
> FUSE_IOCTL_MAX_IOV
||
2848 out_iovs
> FUSE_IOCTL_MAX_IOV
||
2849 in_iovs
+ out_iovs
> FUSE_IOCTL_MAX_IOV
)
2852 vaddr
= kmap_atomic(ap
.pages
[0]);
2853 err
= fuse_copy_ioctl_iovec(fc
, iov_page
, vaddr
,
2854 transferred
, in_iovs
+ out_iovs
,
2855 (flags
& FUSE_IOCTL_COMPAT
) != 0);
2856 kunmap_atomic(vaddr
);
2861 out_iov
= in_iov
+ in_iovs
;
2863 err
= fuse_verify_ioctl_iov(fc
, in_iov
, in_iovs
);
2867 err
= fuse_verify_ioctl_iov(fc
, out_iov
, out_iovs
);
2875 if (transferred
> inarg
.out_size
)
2879 iov_iter_init(&ii
, READ
, out_iov
, out_iovs
, transferred
);
2880 for (i
= 0; iov_iter_count(&ii
) && !WARN_ON(i
>= ap
.num_pages
); i
++) {
2881 c
= copy_page_to_iter(ap
.pages
[i
], 0, PAGE_SIZE
, &ii
);
2882 if (c
!= PAGE_SIZE
&& iov_iter_count(&ii
))
2887 free_page((unsigned long) iov_page
);
2888 while (ap
.num_pages
)
2889 __free_page(ap
.pages
[--ap
.num_pages
]);
2892 return err
? err
: outarg
.result
;
2894 EXPORT_SYMBOL_GPL(fuse_do_ioctl
);
2896 long fuse_ioctl_common(struct file
*file
, unsigned int cmd
,
2897 unsigned long arg
, unsigned int flags
)
2899 struct inode
*inode
= file_inode(file
);
2900 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2902 if (!fuse_allow_current_process(fc
))
2905 if (is_bad_inode(inode
))
2908 return fuse_do_ioctl(file
, cmd
, arg
, flags
);
2911 static long fuse_file_ioctl(struct file
*file
, unsigned int cmd
,
2914 return fuse_ioctl_common(file
, cmd
, arg
, 0);
2917 static long fuse_file_compat_ioctl(struct file
*file
, unsigned int cmd
,
2920 return fuse_ioctl_common(file
, cmd
, arg
, FUSE_IOCTL_COMPAT
);
2924 * All files which have been polled are linked to RB tree
2925 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2926 * find the matching one.
2928 static struct rb_node
**fuse_find_polled_node(struct fuse_conn
*fc
, u64 kh
,
2929 struct rb_node
**parent_out
)
2931 struct rb_node
**link
= &fc
->polled_files
.rb_node
;
2932 struct rb_node
*last
= NULL
;
2935 struct fuse_file
*ff
;
2938 ff
= rb_entry(last
, struct fuse_file
, polled_node
);
2941 link
= &last
->rb_left
;
2942 else if (kh
> ff
->kh
)
2943 link
= &last
->rb_right
;
2954 * The file is about to be polled. Make sure it's on the polled_files
2955 * RB tree. Note that files once added to the polled_files tree are
2956 * not removed before the file is released. This is because a file
2957 * polled once is likely to be polled again.
2959 static void fuse_register_polled_file(struct fuse_conn
*fc
,
2960 struct fuse_file
*ff
)
2962 spin_lock(&fc
->lock
);
2963 if (RB_EMPTY_NODE(&ff
->polled_node
)) {
2964 struct rb_node
**link
, *uninitialized_var(parent
);
2966 link
= fuse_find_polled_node(fc
, ff
->kh
, &parent
);
2968 rb_link_node(&ff
->polled_node
, parent
, link
);
2969 rb_insert_color(&ff
->polled_node
, &fc
->polled_files
);
2971 spin_unlock(&fc
->lock
);
2974 __poll_t
fuse_file_poll(struct file
*file
, poll_table
*wait
)
2976 struct fuse_file
*ff
= file
->private_data
;
2977 struct fuse_conn
*fc
= ff
->fc
;
2978 struct fuse_poll_in inarg
= { .fh
= ff
->fh
, .kh
= ff
->kh
};
2979 struct fuse_poll_out outarg
;
2984 return DEFAULT_POLLMASK
;
2986 poll_wait(file
, &ff
->poll_wait
, wait
);
2987 inarg
.events
= mangle_poll(poll_requested_events(wait
));
2990 * Ask for notification iff there's someone waiting for it.
2991 * The client may ignore the flag and always notify.
2993 if (waitqueue_active(&ff
->poll_wait
)) {
2994 inarg
.flags
|= FUSE_POLL_SCHEDULE_NOTIFY
;
2995 fuse_register_polled_file(fc
, ff
);
2998 args
.opcode
= FUSE_POLL
;
2999 args
.nodeid
= ff
->nodeid
;
3000 args
.in_numargs
= 1;
3001 args
.in_args
[0].size
= sizeof(inarg
);
3002 args
.in_args
[0].value
= &inarg
;
3003 args
.out_numargs
= 1;
3004 args
.out_args
[0].size
= sizeof(outarg
);
3005 args
.out_args
[0].value
= &outarg
;
3006 err
= fuse_simple_request(fc
, &args
);
3009 return demangle_poll(outarg
.revents
);
3010 if (err
== -ENOSYS
) {
3012 return DEFAULT_POLLMASK
;
3016 EXPORT_SYMBOL_GPL(fuse_file_poll
);
3019 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
3020 * wakes up the poll waiters.
3022 int fuse_notify_poll_wakeup(struct fuse_conn
*fc
,
3023 struct fuse_notify_poll_wakeup_out
*outarg
)
3025 u64 kh
= outarg
->kh
;
3026 struct rb_node
**link
;
3028 spin_lock(&fc
->lock
);
3030 link
= fuse_find_polled_node(fc
, kh
, NULL
);
3032 struct fuse_file
*ff
;
3034 ff
= rb_entry(*link
, struct fuse_file
, polled_node
);
3035 wake_up_interruptible_sync(&ff
->poll_wait
);
3038 spin_unlock(&fc
->lock
);
3042 static void fuse_do_truncate(struct file
*file
)
3044 struct inode
*inode
= file
->f_mapping
->host
;
3047 attr
.ia_valid
= ATTR_SIZE
;
3048 attr
.ia_size
= i_size_read(inode
);
3050 attr
.ia_file
= file
;
3051 attr
.ia_valid
|= ATTR_FILE
;
3053 fuse_do_setattr(file_dentry(file
), &attr
, file
);
3056 static inline loff_t
fuse_round_up(struct fuse_conn
*fc
, loff_t off
)
3058 return round_up(off
, fc
->max_pages
<< PAGE_SHIFT
);
3062 fuse_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
)
3064 DECLARE_COMPLETION_ONSTACK(wait
);
3066 struct file
*file
= iocb
->ki_filp
;
3067 struct fuse_file
*ff
= file
->private_data
;
3068 bool async_dio
= ff
->fc
->async_dio
;
3070 struct inode
*inode
;
3072 size_t count
= iov_iter_count(iter
);
3073 loff_t offset
= iocb
->ki_pos
;
3074 struct fuse_io_priv
*io
;
3077 inode
= file
->f_mapping
->host
;
3078 i_size
= i_size_read(inode
);
3080 if ((iov_iter_rw(iter
) == READ
) && (offset
> i_size
))
3083 /* optimization for short read */
3084 if (async_dio
&& iov_iter_rw(iter
) != WRITE
&& offset
+ count
> i_size
) {
3085 if (offset
>= i_size
)
3087 iov_iter_truncate(iter
, fuse_round_up(ff
->fc
, i_size
- offset
));
3088 count
= iov_iter_count(iter
);
3091 io
= kmalloc(sizeof(struct fuse_io_priv
), GFP_KERNEL
);
3094 spin_lock_init(&io
->lock
);
3095 kref_init(&io
->refcnt
);
3099 io
->offset
= offset
;
3100 io
->write
= (iov_iter_rw(iter
) == WRITE
);
3103 * By default, we want to optimize all I/Os with async request
3104 * submission to the client filesystem if supported.
3106 io
->async
= async_dio
;
3108 io
->blocking
= is_sync_kiocb(iocb
);
3111 * We cannot asynchronously extend the size of a file.
3112 * In such case the aio will behave exactly like sync io.
3114 if ((offset
+ count
> i_size
) && iov_iter_rw(iter
) == WRITE
)
3115 io
->blocking
= true;
3117 if (io
->async
&& io
->blocking
) {
3119 * Additional reference to keep io around after
3120 * calling fuse_aio_complete()
3122 kref_get(&io
->refcnt
);
3126 if (iov_iter_rw(iter
) == WRITE
) {
3127 ret
= fuse_direct_io(io
, iter
, &pos
, FUSE_DIO_WRITE
);
3128 fuse_invalidate_attr(inode
);
3130 ret
= __fuse_direct_read(io
, iter
, &pos
);
3134 bool blocking
= io
->blocking
;
3136 fuse_aio_complete(io
, ret
< 0 ? ret
: 0, -1);
3138 /* we have a non-extending, async request, so return */
3140 return -EIOCBQUEUED
;
3142 wait_for_completion(&wait
);
3143 ret
= fuse_get_res_by_io(io
);
3146 kref_put(&io
->refcnt
, fuse_io_release
);
3148 if (iov_iter_rw(iter
) == WRITE
) {
3150 fuse_write_update_size(inode
, pos
);
3151 else if (ret
< 0 && offset
+ count
> i_size
)
3152 fuse_do_truncate(file
);
3158 static int fuse_writeback_range(struct inode
*inode
, loff_t start
, loff_t end
)
3160 int err
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
3163 fuse_sync_writes(inode
);
3168 static long fuse_file_fallocate(struct file
*file
, int mode
, loff_t offset
,
3171 struct fuse_file
*ff
= file
->private_data
;
3172 struct inode
*inode
= file_inode(file
);
3173 struct fuse_inode
*fi
= get_fuse_inode(inode
);
3174 struct fuse_conn
*fc
= ff
->fc
;
3176 struct fuse_fallocate_in inarg
= {
3183 bool lock_inode
= !(mode
& FALLOC_FL_KEEP_SIZE
) ||
3184 (mode
& FALLOC_FL_PUNCH_HOLE
);
3186 if (mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
))
3189 if (fc
->no_fallocate
)
3194 if (mode
& FALLOC_FL_PUNCH_HOLE
) {
3195 loff_t endbyte
= offset
+ length
- 1;
3197 err
= fuse_writeback_range(inode
, offset
, endbyte
);
3203 if (!(mode
& FALLOC_FL_KEEP_SIZE
) &&
3204 offset
+ length
> i_size_read(inode
)) {
3205 err
= inode_newsize_ok(inode
, offset
+ length
);
3210 if (!(mode
& FALLOC_FL_KEEP_SIZE
))
3211 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
3213 args
.opcode
= FUSE_FALLOCATE
;
3214 args
.nodeid
= ff
->nodeid
;
3215 args
.in_numargs
= 1;
3216 args
.in_args
[0].size
= sizeof(inarg
);
3217 args
.in_args
[0].value
= &inarg
;
3218 err
= fuse_simple_request(fc
, &args
);
3219 if (err
== -ENOSYS
) {
3220 fc
->no_fallocate
= 1;
3226 /* we could have extended the file */
3227 if (!(mode
& FALLOC_FL_KEEP_SIZE
)) {
3228 bool changed
= fuse_write_update_size(inode
, offset
+ length
);
3230 if (changed
&& fc
->writeback_cache
)
3231 file_update_time(file
);
3234 if (mode
& FALLOC_FL_PUNCH_HOLE
)
3235 truncate_pagecache_range(inode
, offset
, offset
+ length
- 1);
3237 fuse_invalidate_attr(inode
);
3240 if (!(mode
& FALLOC_FL_KEEP_SIZE
))
3241 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
3244 inode_unlock(inode
);
3249 static ssize_t
__fuse_copy_file_range(struct file
*file_in
, loff_t pos_in
,
3250 struct file
*file_out
, loff_t pos_out
,
3251 size_t len
, unsigned int flags
)
3253 struct fuse_file
*ff_in
= file_in
->private_data
;
3254 struct fuse_file
*ff_out
= file_out
->private_data
;
3255 struct inode
*inode_in
= file_inode(file_in
);
3256 struct inode
*inode_out
= file_inode(file_out
);
3257 struct fuse_inode
*fi_out
= get_fuse_inode(inode_out
);
3258 struct fuse_conn
*fc
= ff_in
->fc
;
3260 struct fuse_copy_file_range_in inarg
= {
3263 .nodeid_out
= ff_out
->nodeid
,
3264 .fh_out
= ff_out
->fh
,
3269 struct fuse_write_out outarg
;
3271 /* mark unstable when write-back is not used, and file_out gets
3273 bool is_unstable
= (!fc
->writeback_cache
) &&
3274 ((pos_out
+ len
) > inode_out
->i_size
);
3276 if (fc
->no_copy_file_range
)
3279 if (file_inode(file_in
)->i_sb
!= file_inode(file_out
)->i_sb
)
3282 if (fc
->writeback_cache
) {
3283 inode_lock(inode_in
);
3284 err
= fuse_writeback_range(inode_in
, pos_in
, pos_in
+ len
);
3285 inode_unlock(inode_in
);
3290 inode_lock(inode_out
);
3292 err
= file_modified(file_out
);
3296 if (fc
->writeback_cache
) {
3297 err
= fuse_writeback_range(inode_out
, pos_out
, pos_out
+ len
);
3303 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi_out
->state
);
3305 args
.opcode
= FUSE_COPY_FILE_RANGE
;
3306 args
.nodeid
= ff_in
->nodeid
;
3307 args
.in_numargs
= 1;
3308 args
.in_args
[0].size
= sizeof(inarg
);
3309 args
.in_args
[0].value
= &inarg
;
3310 args
.out_numargs
= 1;
3311 args
.out_args
[0].size
= sizeof(outarg
);
3312 args
.out_args
[0].value
= &outarg
;
3313 err
= fuse_simple_request(fc
, &args
);
3314 if (err
== -ENOSYS
) {
3315 fc
->no_copy_file_range
= 1;
3321 if (fc
->writeback_cache
) {
3322 fuse_write_update_size(inode_out
, pos_out
+ outarg
.size
);
3323 file_update_time(file_out
);
3326 fuse_invalidate_attr(inode_out
);
3331 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi_out
->state
);
3333 inode_unlock(inode_out
);
3334 file_accessed(file_in
);
3339 static ssize_t
fuse_copy_file_range(struct file
*src_file
, loff_t src_off
,
3340 struct file
*dst_file
, loff_t dst_off
,
3341 size_t len
, unsigned int flags
)
3345 ret
= __fuse_copy_file_range(src_file
, src_off
, dst_file
, dst_off
,
3348 if (ret
== -EOPNOTSUPP
|| ret
== -EXDEV
)
3349 ret
= generic_copy_file_range(src_file
, src_off
, dst_file
,
3350 dst_off
, len
, flags
);
3354 static const struct file_operations fuse_file_operations
= {
3355 .llseek
= fuse_file_llseek
,
3356 .read_iter
= fuse_file_read_iter
,
3357 .write_iter
= fuse_file_write_iter
,
3358 .mmap
= fuse_file_mmap
,
3360 .flush
= fuse_flush
,
3361 .release
= fuse_release
,
3362 .fsync
= fuse_fsync
,
3363 .lock
= fuse_file_lock
,
3364 .flock
= fuse_file_flock
,
3365 .splice_read
= generic_file_splice_read
,
3366 .splice_write
= iter_file_splice_write
,
3367 .unlocked_ioctl
= fuse_file_ioctl
,
3368 .compat_ioctl
= fuse_file_compat_ioctl
,
3369 .poll
= fuse_file_poll
,
3370 .fallocate
= fuse_file_fallocate
,
3371 .copy_file_range
= fuse_copy_file_range
,
3374 static const struct address_space_operations fuse_file_aops
= {
3375 .readpage
= fuse_readpage
,
3376 .writepage
= fuse_writepage
,
3377 .writepages
= fuse_writepages
,
3378 .launder_page
= fuse_launder_page
,
3379 .readpages
= fuse_readpages
,
3380 .set_page_dirty
= __set_page_dirty_nobuffers
,
3382 .direct_IO
= fuse_direct_IO
,
3383 .write_begin
= fuse_write_begin
,
3384 .write_end
= fuse_write_end
,
3387 void fuse_init_file_inode(struct inode
*inode
)
3389 struct fuse_inode
*fi
= get_fuse_inode(inode
);
3391 inode
->i_fop
= &fuse_file_operations
;
3392 inode
->i_data
.a_ops
= &fuse_file_aops
;
3394 INIT_LIST_HEAD(&fi
->write_files
);
3395 INIT_LIST_HEAD(&fi
->queued_writes
);
3397 init_waitqueue_head(&fi
->page_waitq
);
3398 INIT_LIST_HEAD(&fi
->writepages
);