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 const struct file_operations fuse_direct_io_file_operations
;
24 static int fuse_send_open(struct fuse_conn
*fc
, u64 nodeid
, struct file
*file
,
25 int opcode
, struct fuse_open_out
*outargp
)
27 struct fuse_open_in inarg
;
30 memset(&inarg
, 0, sizeof(inarg
));
31 inarg
.flags
= file
->f_flags
& ~(O_CREAT
| O_EXCL
| O_NOCTTY
);
32 if (!fc
->atomic_o_trunc
)
33 inarg
.flags
&= ~O_TRUNC
;
34 args
.in
.h
.opcode
= opcode
;
35 args
.in
.h
.nodeid
= nodeid
;
37 args
.in
.args
[0].size
= sizeof(inarg
);
38 args
.in
.args
[0].value
= &inarg
;
40 args
.out
.args
[0].size
= sizeof(*outargp
);
41 args
.out
.args
[0].value
= outargp
;
43 return fuse_simple_request(fc
, &args
);
46 struct fuse_file
*fuse_file_alloc(struct fuse_conn
*fc
)
50 ff
= kzalloc(sizeof(struct fuse_file
), GFP_KERNEL
);
55 ff
->reserved_req
= fuse_request_alloc(0);
56 if (unlikely(!ff
->reserved_req
)) {
61 INIT_LIST_HEAD(&ff
->write_entry
);
62 refcount_set(&ff
->count
, 1);
63 RB_CLEAR_NODE(&ff
->polled_node
);
64 init_waitqueue_head(&ff
->poll_wait
);
68 spin_unlock(&fc
->lock
);
73 void fuse_file_free(struct fuse_file
*ff
)
75 fuse_request_free(ff
->reserved_req
);
79 static struct fuse_file
*fuse_file_get(struct fuse_file
*ff
)
81 refcount_inc(&ff
->count
);
85 static void fuse_release_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
87 iput(req
->misc
.release
.inode
);
90 static void fuse_file_put(struct fuse_file
*ff
, bool sync
)
92 if (refcount_dec_and_test(&ff
->count
)) {
93 struct fuse_req
*req
= ff
->reserved_req
;
95 if (ff
->fc
->no_open
) {
97 * Drop the release request when client does not
100 __clear_bit(FR_BACKGROUND
, &req
->flags
);
101 iput(req
->misc
.release
.inode
);
102 fuse_put_request(ff
->fc
, req
);
104 __set_bit(FR_FORCE
, &req
->flags
);
105 __clear_bit(FR_BACKGROUND
, &req
->flags
);
106 fuse_request_send(ff
->fc
, req
);
107 iput(req
->misc
.release
.inode
);
108 fuse_put_request(ff
->fc
, req
);
110 req
->end
= fuse_release_end
;
111 __set_bit(FR_BACKGROUND
, &req
->flags
);
112 fuse_request_send_background(ff
->fc
, req
);
118 int fuse_do_open(struct fuse_conn
*fc
, u64 nodeid
, struct file
*file
,
121 struct fuse_file
*ff
;
122 int opcode
= isdir
? FUSE_OPENDIR
: FUSE_OPEN
;
124 ff
= fuse_file_alloc(fc
);
129 ff
->open_flags
= FOPEN_KEEP_CACHE
; /* Default for no-open */
130 if (!fc
->no_open
|| isdir
) {
131 struct fuse_open_out outarg
;
134 err
= fuse_send_open(fc
, nodeid
, file
, opcode
, &outarg
);
137 ff
->open_flags
= outarg
.open_flags
;
139 } else if (err
!= -ENOSYS
|| isdir
) {
148 ff
->open_flags
&= ~FOPEN_DIRECT_IO
;
151 file
->private_data
= ff
;
155 EXPORT_SYMBOL_GPL(fuse_do_open
);
157 static void fuse_link_write_file(struct file
*file
)
159 struct inode
*inode
= file_inode(file
);
160 struct fuse_conn
*fc
= get_fuse_conn(inode
);
161 struct fuse_inode
*fi
= get_fuse_inode(inode
);
162 struct fuse_file
*ff
= file
->private_data
;
164 * file may be written through mmap, so chain it onto the
165 * inodes's write_file list
167 spin_lock(&fc
->lock
);
168 if (list_empty(&ff
->write_entry
))
169 list_add(&ff
->write_entry
, &fi
->write_files
);
170 spin_unlock(&fc
->lock
);
173 void fuse_finish_open(struct inode
*inode
, struct file
*file
)
175 struct fuse_file
*ff
= file
->private_data
;
176 struct fuse_conn
*fc
= get_fuse_conn(inode
);
178 if (ff
->open_flags
& FOPEN_DIRECT_IO
)
179 file
->f_op
= &fuse_direct_io_file_operations
;
180 if (!(ff
->open_flags
& FOPEN_KEEP_CACHE
))
181 invalidate_inode_pages2(inode
->i_mapping
);
182 if (ff
->open_flags
& FOPEN_NONSEEKABLE
)
183 nonseekable_open(inode
, file
);
184 if (fc
->atomic_o_trunc
&& (file
->f_flags
& O_TRUNC
)) {
185 struct fuse_inode
*fi
= get_fuse_inode(inode
);
187 spin_lock(&fc
->lock
);
188 fi
->attr_version
= ++fc
->attr_version
;
189 i_size_write(inode
, 0);
190 spin_unlock(&fc
->lock
);
191 fuse_invalidate_attr(inode
);
192 if (fc
->writeback_cache
)
193 file_update_time(file
);
195 if ((file
->f_mode
& FMODE_WRITE
) && fc
->writeback_cache
)
196 fuse_link_write_file(file
);
199 int fuse_open_common(struct inode
*inode
, struct file
*file
, bool isdir
)
201 struct fuse_conn
*fc
= get_fuse_conn(inode
);
203 bool lock_inode
= (file
->f_flags
& O_TRUNC
) &&
204 fc
->atomic_o_trunc
&&
207 err
= generic_file_open(inode
, file
);
214 err
= fuse_do_open(fc
, get_node_id(inode
), file
, isdir
);
217 fuse_finish_open(inode
, file
);
225 static void fuse_prepare_release(struct fuse_file
*ff
, int flags
, int opcode
)
227 struct fuse_conn
*fc
= ff
->fc
;
228 struct fuse_req
*req
= ff
->reserved_req
;
229 struct fuse_release_in
*inarg
= &req
->misc
.release
.in
;
231 spin_lock(&fc
->lock
);
232 list_del(&ff
->write_entry
);
233 if (!RB_EMPTY_NODE(&ff
->polled_node
))
234 rb_erase(&ff
->polled_node
, &fc
->polled_files
);
235 spin_unlock(&fc
->lock
);
237 wake_up_interruptible_all(&ff
->poll_wait
);
240 inarg
->flags
= flags
;
241 req
->in
.h
.opcode
= opcode
;
242 req
->in
.h
.nodeid
= ff
->nodeid
;
244 req
->in
.args
[0].size
= sizeof(struct fuse_release_in
);
245 req
->in
.args
[0].value
= inarg
;
248 void fuse_release_common(struct file
*file
, int opcode
)
250 struct fuse_file
*ff
= file
->private_data
;
251 struct fuse_req
*req
= ff
->reserved_req
;
253 fuse_prepare_release(ff
, file
->f_flags
, opcode
);
256 struct fuse_release_in
*inarg
= &req
->misc
.release
.in
;
257 inarg
->release_flags
|= FUSE_RELEASE_FLOCK_UNLOCK
;
258 inarg
->lock_owner
= fuse_lock_owner_id(ff
->fc
,
261 /* Hold inode until release is finished */
262 req
->misc
.release
.inode
= igrab(file_inode(file
));
265 * Normally this will send the RELEASE request, however if
266 * some asynchronous READ or WRITE requests are outstanding,
267 * the sending will be delayed.
269 * Make the release synchronous if this is a fuseblk mount,
270 * synchronous RELEASE is allowed (and desirable) in this case
271 * because the server can be trusted not to screw up.
273 fuse_file_put(ff
, ff
->fc
->destroy_req
!= NULL
);
276 static int fuse_open(struct inode
*inode
, struct file
*file
)
278 return fuse_open_common(inode
, file
, false);
281 static int fuse_release(struct inode
*inode
, struct file
*file
)
283 struct fuse_conn
*fc
= get_fuse_conn(inode
);
285 /* see fuse_vma_close() for !writeback_cache case */
286 if (fc
->writeback_cache
)
287 write_inode_now(inode
, 1);
289 fuse_release_common(file
, FUSE_RELEASE
);
291 /* return value is ignored by VFS */
295 void fuse_sync_release(struct fuse_file
*ff
, int flags
)
297 WARN_ON(refcount_read(&ff
->count
) > 1);
298 fuse_prepare_release(ff
, flags
, FUSE_RELEASE
);
300 * iput(NULL) is a no-op and since the refcount is 1 and everything's
301 * synchronous, we are fine with not doing igrab() here"
303 fuse_file_put(ff
, true);
305 EXPORT_SYMBOL_GPL(fuse_sync_release
);
308 * Scramble the ID space with XTEA, so that the value of the files_struct
309 * pointer is not exposed to userspace.
311 u64
fuse_lock_owner_id(struct fuse_conn
*fc
, fl_owner_t id
)
313 u32
*k
= fc
->scramble_key
;
314 u64 v
= (unsigned long) id
;
320 for (i
= 0; i
< 32; i
++) {
321 v0
+= ((v1
<< 4 ^ v1
>> 5) + v1
) ^ (sum
+ k
[sum
& 3]);
323 v1
+= ((v0
<< 4 ^ v0
>> 5) + v0
) ^ (sum
+ k
[sum
>>11 & 3]);
326 return (u64
) v0
+ ((u64
) v1
<< 32);
330 * Check if any page in a range is under writeback
332 * This is currently done by walking the list of writepage requests
333 * for the inode, which can be pretty inefficient.
335 static bool fuse_range_is_writeback(struct inode
*inode
, pgoff_t idx_from
,
338 struct fuse_conn
*fc
= get_fuse_conn(inode
);
339 struct fuse_inode
*fi
= get_fuse_inode(inode
);
340 struct fuse_req
*req
;
343 spin_lock(&fc
->lock
);
344 list_for_each_entry(req
, &fi
->writepages
, writepages_entry
) {
347 BUG_ON(req
->inode
!= inode
);
348 curr_index
= req
->misc
.write
.in
.offset
>> PAGE_SHIFT
;
349 if (idx_from
< curr_index
+ req
->num_pages
&&
350 curr_index
<= idx_to
) {
355 spin_unlock(&fc
->lock
);
360 static inline bool fuse_page_is_writeback(struct inode
*inode
, pgoff_t index
)
362 return fuse_range_is_writeback(inode
, index
, index
);
366 * Wait for page writeback to be completed.
368 * Since fuse doesn't rely on the VM writeback tracking, this has to
369 * use some other means.
371 static int fuse_wait_on_page_writeback(struct inode
*inode
, pgoff_t index
)
373 struct fuse_inode
*fi
= get_fuse_inode(inode
);
375 wait_event(fi
->page_waitq
, !fuse_page_is_writeback(inode
, index
));
380 * Wait for all pending writepages on the inode to finish.
382 * This is currently done by blocking further writes with FUSE_NOWRITE
383 * and waiting for all sent writes to complete.
385 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
386 * could conflict with truncation.
388 static void fuse_sync_writes(struct inode
*inode
)
390 fuse_set_nowrite(inode
);
391 fuse_release_nowrite(inode
);
394 static int fuse_flush(struct file
*file
, fl_owner_t id
)
396 struct inode
*inode
= file_inode(file
);
397 struct fuse_conn
*fc
= get_fuse_conn(inode
);
398 struct fuse_file
*ff
= file
->private_data
;
399 struct fuse_req
*req
;
400 struct fuse_flush_in inarg
;
403 if (is_bad_inode(inode
))
409 err
= write_inode_now(inode
, 1);
414 fuse_sync_writes(inode
);
417 err
= filemap_check_errors(file
->f_mapping
);
421 req
= fuse_get_req_nofail_nopages(fc
, file
);
422 memset(&inarg
, 0, sizeof(inarg
));
424 inarg
.lock_owner
= fuse_lock_owner_id(fc
, id
);
425 req
->in
.h
.opcode
= FUSE_FLUSH
;
426 req
->in
.h
.nodeid
= get_node_id(inode
);
428 req
->in
.args
[0].size
= sizeof(inarg
);
429 req
->in
.args
[0].value
= &inarg
;
430 __set_bit(FR_FORCE
, &req
->flags
);
431 fuse_request_send(fc
, req
);
432 err
= req
->out
.h
.error
;
433 fuse_put_request(fc
, req
);
434 if (err
== -ENOSYS
) {
441 int fuse_fsync_common(struct file
*file
, loff_t start
, loff_t end
,
442 int datasync
, int isdir
)
444 struct inode
*inode
= file
->f_mapping
->host
;
445 struct fuse_conn
*fc
= get_fuse_conn(inode
);
446 struct fuse_file
*ff
= file
->private_data
;
448 struct fuse_fsync_in inarg
;
451 if (is_bad_inode(inode
))
457 * Start writeback against all dirty pages of the inode, then
458 * wait for all outstanding writes, before sending the FSYNC
461 err
= file_write_and_wait_range(file
, start
, end
);
465 fuse_sync_writes(inode
);
468 * Due to implementation of fuse writeback
469 * file_write_and_wait_range() does not catch errors.
470 * We have to do this directly after fuse_sync_writes()
472 err
= file_check_and_advance_wb_err(file
);
476 err
= sync_inode_metadata(inode
, 1);
480 if ((!isdir
&& fc
->no_fsync
) || (isdir
&& fc
->no_fsyncdir
))
483 memset(&inarg
, 0, sizeof(inarg
));
485 inarg
.fsync_flags
= datasync
? 1 : 0;
486 args
.in
.h
.opcode
= isdir
? FUSE_FSYNCDIR
: FUSE_FSYNC
;
487 args
.in
.h
.nodeid
= get_node_id(inode
);
489 args
.in
.args
[0].size
= sizeof(inarg
);
490 args
.in
.args
[0].value
= &inarg
;
491 err
= fuse_simple_request(fc
, &args
);
492 if (err
== -ENOSYS
) {
504 static int fuse_fsync(struct file
*file
, loff_t start
, loff_t end
,
507 return fuse_fsync_common(file
, start
, end
, datasync
, 0);
510 void fuse_read_fill(struct fuse_req
*req
, struct file
*file
, loff_t pos
,
511 size_t count
, int opcode
)
513 struct fuse_read_in
*inarg
= &req
->misc
.read
.in
;
514 struct fuse_file
*ff
= file
->private_data
;
519 inarg
->flags
= file
->f_flags
;
520 req
->in
.h
.opcode
= opcode
;
521 req
->in
.h
.nodeid
= ff
->nodeid
;
523 req
->in
.args
[0].size
= sizeof(struct fuse_read_in
);
524 req
->in
.args
[0].value
= inarg
;
526 req
->out
.numargs
= 1;
527 req
->out
.args
[0].size
= count
;
530 static void fuse_release_user_pages(struct fuse_req
*req
, bool should_dirty
)
534 for (i
= 0; i
< req
->num_pages
; i
++) {
535 struct page
*page
= req
->pages
[i
];
537 set_page_dirty_lock(page
);
542 static void fuse_io_release(struct kref
*kref
)
544 kfree(container_of(kref
, struct fuse_io_priv
, refcnt
));
547 static ssize_t
fuse_get_res_by_io(struct fuse_io_priv
*io
)
552 if (io
->bytes
>= 0 && io
->write
)
555 return io
->bytes
< 0 ? io
->size
: io
->bytes
;
559 * In case of short read, the caller sets 'pos' to the position of
560 * actual end of fuse request in IO request. Otherwise, if bytes_requested
561 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
564 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
565 * both submitted asynchronously. The first of them was ACKed by userspace as
566 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
567 * second request was ACKed as short, e.g. only 1K was read, resulting in
570 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
571 * will be equal to the length of the longest contiguous fragment of
572 * transferred data starting from the beginning of IO request.
574 static void fuse_aio_complete(struct fuse_io_priv
*io
, int err
, ssize_t pos
)
578 spin_lock(&io
->lock
);
580 io
->err
= io
->err
? : err
;
581 else if (pos
>= 0 && (io
->bytes
< 0 || pos
< io
->bytes
))
585 if (!left
&& io
->blocking
)
587 spin_unlock(&io
->lock
);
589 if (!left
&& !io
->blocking
) {
590 ssize_t res
= fuse_get_res_by_io(io
);
593 struct inode
*inode
= file_inode(io
->iocb
->ki_filp
);
594 struct fuse_conn
*fc
= get_fuse_conn(inode
);
595 struct fuse_inode
*fi
= get_fuse_inode(inode
);
597 spin_lock(&fc
->lock
);
598 fi
->attr_version
= ++fc
->attr_version
;
599 spin_unlock(&fc
->lock
);
602 io
->iocb
->ki_complete(io
->iocb
, res
, 0);
605 kref_put(&io
->refcnt
, fuse_io_release
);
608 static void fuse_aio_complete_req(struct fuse_conn
*fc
, struct fuse_req
*req
)
610 struct fuse_io_priv
*io
= req
->io
;
613 fuse_release_user_pages(req
, io
->should_dirty
);
616 if (req
->misc
.write
.in
.size
!= req
->misc
.write
.out
.size
)
617 pos
= req
->misc
.write
.in
.offset
- io
->offset
+
618 req
->misc
.write
.out
.size
;
620 if (req
->misc
.read
.in
.size
!= req
->out
.args
[0].size
)
621 pos
= req
->misc
.read
.in
.offset
- io
->offset
+
622 req
->out
.args
[0].size
;
625 fuse_aio_complete(io
, req
->out
.h
.error
, pos
);
628 static size_t fuse_async_req_send(struct fuse_conn
*fc
, struct fuse_req
*req
,
629 size_t num_bytes
, struct fuse_io_priv
*io
)
631 spin_lock(&io
->lock
);
632 kref_get(&io
->refcnt
);
633 io
->size
+= num_bytes
;
635 spin_unlock(&io
->lock
);
638 req
->end
= fuse_aio_complete_req
;
640 __fuse_get_request(req
);
641 fuse_request_send_background(fc
, req
);
646 static size_t fuse_send_read(struct fuse_req
*req
, struct fuse_io_priv
*io
,
647 loff_t pos
, size_t count
, fl_owner_t owner
)
649 struct file
*file
= io
->iocb
->ki_filp
;
650 struct fuse_file
*ff
= file
->private_data
;
651 struct fuse_conn
*fc
= ff
->fc
;
653 fuse_read_fill(req
, file
, pos
, count
, FUSE_READ
);
655 struct fuse_read_in
*inarg
= &req
->misc
.read
.in
;
657 inarg
->read_flags
|= FUSE_READ_LOCKOWNER
;
658 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
662 return fuse_async_req_send(fc
, req
, count
, io
);
664 fuse_request_send(fc
, req
);
665 return req
->out
.args
[0].size
;
668 static void fuse_read_update_size(struct inode
*inode
, loff_t size
,
671 struct fuse_conn
*fc
= get_fuse_conn(inode
);
672 struct fuse_inode
*fi
= get_fuse_inode(inode
);
674 spin_lock(&fc
->lock
);
675 if (attr_ver
== fi
->attr_version
&& size
< inode
->i_size
&&
676 !test_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
)) {
677 fi
->attr_version
= ++fc
->attr_version
;
678 i_size_write(inode
, size
);
680 spin_unlock(&fc
->lock
);
683 static void fuse_short_read(struct fuse_req
*req
, struct inode
*inode
,
686 size_t num_read
= req
->out
.args
[0].size
;
687 struct fuse_conn
*fc
= get_fuse_conn(inode
);
689 if (fc
->writeback_cache
) {
691 * A hole in a file. Some data after the hole are in page cache,
692 * but have not reached the client fs yet. So, the hole is not
696 int start_idx
= num_read
>> PAGE_SHIFT
;
697 size_t off
= num_read
& (PAGE_SIZE
- 1);
699 for (i
= start_idx
; i
< req
->num_pages
; i
++) {
700 zero_user_segment(req
->pages
[i
], off
, PAGE_SIZE
);
704 loff_t pos
= page_offset(req
->pages
[0]) + num_read
;
705 fuse_read_update_size(inode
, pos
, attr_ver
);
709 static int fuse_do_readpage(struct file
*file
, struct page
*page
)
712 struct fuse_io_priv io
;
713 struct inode
*inode
= page
->mapping
->host
;
714 struct fuse_conn
*fc
= get_fuse_conn(inode
);
715 struct fuse_req
*req
;
717 loff_t pos
= page_offset(page
);
718 size_t count
= PAGE_SIZE
;
723 * Page writeback can extend beyond the lifetime of the
724 * page-cache page, so make sure we read a properly synced
727 fuse_wait_on_page_writeback(inode
, page
->index
);
729 req
= fuse_get_req(fc
, 1);
733 attr_ver
= fuse_get_attr_version(fc
);
735 req
->out
.page_zeroing
= 1;
736 req
->out
.argpages
= 1;
738 req
->pages
[0] = page
;
739 req
->page_descs
[0].length
= count
;
740 init_sync_kiocb(&iocb
, file
);
741 io
= (struct fuse_io_priv
) FUSE_IO_PRIV_SYNC(&iocb
);
742 num_read
= fuse_send_read(req
, &io
, pos
, count
, NULL
);
743 err
= req
->out
.h
.error
;
747 * Short read means EOF. If file size is larger, truncate it
749 if (num_read
< count
)
750 fuse_short_read(req
, inode
, attr_ver
);
752 SetPageUptodate(page
);
755 fuse_put_request(fc
, req
);
760 static int fuse_readpage(struct file
*file
, struct page
*page
)
762 struct inode
*inode
= page
->mapping
->host
;
766 if (is_bad_inode(inode
))
769 err
= fuse_do_readpage(file
, page
);
770 fuse_invalidate_atime(inode
);
776 static void fuse_readpages_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
779 size_t count
= req
->misc
.read
.in
.size
;
780 size_t num_read
= req
->out
.args
[0].size
;
781 struct address_space
*mapping
= NULL
;
783 for (i
= 0; mapping
== NULL
&& i
< req
->num_pages
; i
++)
784 mapping
= req
->pages
[i
]->mapping
;
787 struct inode
*inode
= mapping
->host
;
790 * Short read means EOF. If file size is larger, truncate it
792 if (!req
->out
.h
.error
&& num_read
< count
)
793 fuse_short_read(req
, inode
, req
->misc
.read
.attr_ver
);
795 fuse_invalidate_atime(inode
);
798 for (i
= 0; i
< req
->num_pages
; i
++) {
799 struct page
*page
= req
->pages
[i
];
800 if (!req
->out
.h
.error
)
801 SetPageUptodate(page
);
808 fuse_file_put(req
->ff
, false);
811 static void fuse_send_readpages(struct fuse_req
*req
, struct file
*file
)
813 struct fuse_file
*ff
= file
->private_data
;
814 struct fuse_conn
*fc
= ff
->fc
;
815 loff_t pos
= page_offset(req
->pages
[0]);
816 size_t count
= req
->num_pages
<< PAGE_SHIFT
;
818 req
->out
.argpages
= 1;
819 req
->out
.page_zeroing
= 1;
820 req
->out
.page_replace
= 1;
821 fuse_read_fill(req
, file
, pos
, count
, FUSE_READ
);
822 req
->misc
.read
.attr_ver
= fuse_get_attr_version(fc
);
823 if (fc
->async_read
) {
824 req
->ff
= fuse_file_get(ff
);
825 req
->end
= fuse_readpages_end
;
826 fuse_request_send_background(fc
, req
);
828 fuse_request_send(fc
, req
);
829 fuse_readpages_end(fc
, req
);
830 fuse_put_request(fc
, req
);
834 struct fuse_fill_data
{
835 struct fuse_req
*req
;
841 static int fuse_readpages_fill(void *_data
, struct page
*page
)
843 struct fuse_fill_data
*data
= _data
;
844 struct fuse_req
*req
= data
->req
;
845 struct inode
*inode
= data
->inode
;
846 struct fuse_conn
*fc
= get_fuse_conn(inode
);
848 fuse_wait_on_page_writeback(inode
, page
->index
);
850 if (req
->num_pages
&&
851 (req
->num_pages
== FUSE_MAX_PAGES_PER_REQ
||
852 (req
->num_pages
+ 1) * PAGE_SIZE
> fc
->max_read
||
853 req
->pages
[req
->num_pages
- 1]->index
+ 1 != page
->index
)) {
854 int nr_alloc
= min_t(unsigned, data
->nr_pages
,
855 FUSE_MAX_PAGES_PER_REQ
);
856 fuse_send_readpages(req
, data
->file
);
858 req
= fuse_get_req_for_background(fc
, nr_alloc
);
860 req
= fuse_get_req(fc
, nr_alloc
);
869 if (WARN_ON(req
->num_pages
>= req
->max_pages
)) {
871 fuse_put_request(fc
, req
);
876 req
->pages
[req
->num_pages
] = page
;
877 req
->page_descs
[req
->num_pages
].length
= PAGE_SIZE
;
883 static int fuse_readpages(struct file
*file
, struct address_space
*mapping
,
884 struct list_head
*pages
, unsigned nr_pages
)
886 struct inode
*inode
= mapping
->host
;
887 struct fuse_conn
*fc
= get_fuse_conn(inode
);
888 struct fuse_fill_data data
;
890 int nr_alloc
= min_t(unsigned, nr_pages
, FUSE_MAX_PAGES_PER_REQ
);
893 if (is_bad_inode(inode
))
899 data
.req
= fuse_get_req_for_background(fc
, nr_alloc
);
901 data
.req
= fuse_get_req(fc
, nr_alloc
);
902 data
.nr_pages
= nr_pages
;
903 err
= PTR_ERR(data
.req
);
904 if (IS_ERR(data
.req
))
907 err
= read_cache_pages(mapping
, pages
, fuse_readpages_fill
, &data
);
909 if (data
.req
->num_pages
)
910 fuse_send_readpages(data
.req
, file
);
912 fuse_put_request(fc
, data
.req
);
918 static ssize_t
fuse_file_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
920 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
921 struct fuse_conn
*fc
= get_fuse_conn(inode
);
924 * In auto invalidate mode, always update attributes on read.
925 * Otherwise, only update if we attempt to read past EOF (to ensure
926 * i_size is up to date).
928 if (fc
->auto_inval_data
||
929 (iocb
->ki_pos
+ iov_iter_count(to
) > i_size_read(inode
))) {
931 err
= fuse_update_attributes(inode
, iocb
->ki_filp
);
936 return generic_file_read_iter(iocb
, to
);
939 static void fuse_write_fill(struct fuse_req
*req
, struct fuse_file
*ff
,
940 loff_t pos
, size_t count
)
942 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
943 struct fuse_write_out
*outarg
= &req
->misc
.write
.out
;
948 req
->in
.h
.opcode
= FUSE_WRITE
;
949 req
->in
.h
.nodeid
= ff
->nodeid
;
951 if (ff
->fc
->minor
< 9)
952 req
->in
.args
[0].size
= FUSE_COMPAT_WRITE_IN_SIZE
;
954 req
->in
.args
[0].size
= sizeof(struct fuse_write_in
);
955 req
->in
.args
[0].value
= inarg
;
956 req
->in
.args
[1].size
= count
;
957 req
->out
.numargs
= 1;
958 req
->out
.args
[0].size
= sizeof(struct fuse_write_out
);
959 req
->out
.args
[0].value
= outarg
;
962 static size_t fuse_send_write(struct fuse_req
*req
, struct fuse_io_priv
*io
,
963 loff_t pos
, size_t count
, fl_owner_t owner
)
965 struct kiocb
*iocb
= io
->iocb
;
966 struct file
*file
= iocb
->ki_filp
;
967 struct fuse_file
*ff
= file
->private_data
;
968 struct fuse_conn
*fc
= ff
->fc
;
969 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
971 fuse_write_fill(req
, ff
, pos
, count
);
972 inarg
->flags
= file
->f_flags
;
973 if (iocb
->ki_flags
& IOCB_DSYNC
)
974 inarg
->flags
|= O_DSYNC
;
975 if (iocb
->ki_flags
& IOCB_SYNC
)
976 inarg
->flags
|= O_SYNC
;
978 inarg
->write_flags
|= FUSE_WRITE_LOCKOWNER
;
979 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
983 return fuse_async_req_send(fc
, req
, count
, io
);
985 fuse_request_send(fc
, req
);
986 return req
->misc
.write
.out
.size
;
989 bool fuse_write_update_size(struct inode
*inode
, loff_t pos
)
991 struct fuse_conn
*fc
= get_fuse_conn(inode
);
992 struct fuse_inode
*fi
= get_fuse_inode(inode
);
995 spin_lock(&fc
->lock
);
996 fi
->attr_version
= ++fc
->attr_version
;
997 if (pos
> inode
->i_size
) {
998 i_size_write(inode
, pos
);
1001 spin_unlock(&fc
->lock
);
1006 static size_t fuse_send_write_pages(struct fuse_req
*req
, struct kiocb
*iocb
,
1007 struct inode
*inode
, loff_t pos
,
1013 struct fuse_io_priv io
= FUSE_IO_PRIV_SYNC(iocb
);
1015 for (i
= 0; i
< req
->num_pages
; i
++)
1016 fuse_wait_on_page_writeback(inode
, req
->pages
[i
]->index
);
1018 res
= fuse_send_write(req
, &io
, pos
, count
, NULL
);
1020 offset
= req
->page_descs
[0].offset
;
1022 for (i
= 0; i
< req
->num_pages
; i
++) {
1023 struct page
*page
= req
->pages
[i
];
1025 if (!req
->out
.h
.error
&& !offset
&& count
>= PAGE_SIZE
)
1026 SetPageUptodate(page
);
1028 if (count
> PAGE_SIZE
- offset
)
1029 count
-= PAGE_SIZE
- offset
;
1041 static ssize_t
fuse_fill_write_pages(struct fuse_req
*req
,
1042 struct address_space
*mapping
,
1043 struct iov_iter
*ii
, loff_t pos
)
1045 struct fuse_conn
*fc
= get_fuse_conn(mapping
->host
);
1046 unsigned offset
= pos
& (PAGE_SIZE
- 1);
1050 req
->in
.argpages
= 1;
1051 req
->page_descs
[0].offset
= offset
;
1056 pgoff_t index
= pos
>> PAGE_SHIFT
;
1057 size_t bytes
= min_t(size_t, PAGE_SIZE
- offset
,
1058 iov_iter_count(ii
));
1060 bytes
= min_t(size_t, bytes
, fc
->max_write
- count
);
1064 if (iov_iter_fault_in_readable(ii
, bytes
))
1068 page
= grab_cache_page_write_begin(mapping
, index
, 0);
1072 if (mapping_writably_mapped(mapping
))
1073 flush_dcache_page(page
);
1075 tmp
= iov_iter_copy_from_user_atomic(page
, ii
, offset
, bytes
);
1076 flush_dcache_page(page
);
1078 iov_iter_advance(ii
, tmp
);
1082 bytes
= min(bytes
, iov_iter_single_seg_count(ii
));
1087 req
->pages
[req
->num_pages
] = page
;
1088 req
->page_descs
[req
->num_pages
].length
= tmp
;
1094 if (offset
== PAGE_SIZE
)
1097 if (!fc
->big_writes
)
1099 } while (iov_iter_count(ii
) && count
< fc
->max_write
&&
1100 req
->num_pages
< req
->max_pages
&& offset
== 0);
1102 return count
> 0 ? count
: err
;
1105 static inline unsigned fuse_wr_pages(loff_t pos
, size_t len
)
1107 return min_t(unsigned,
1108 ((pos
+ len
- 1) >> PAGE_SHIFT
) -
1109 (pos
>> PAGE_SHIFT
) + 1,
1110 FUSE_MAX_PAGES_PER_REQ
);
1113 static ssize_t
fuse_perform_write(struct kiocb
*iocb
,
1114 struct address_space
*mapping
,
1115 struct iov_iter
*ii
, loff_t pos
)
1117 struct inode
*inode
= mapping
->host
;
1118 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1119 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1123 if (is_bad_inode(inode
))
1126 if (inode
->i_size
< pos
+ iov_iter_count(ii
))
1127 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1130 struct fuse_req
*req
;
1132 unsigned nr_pages
= fuse_wr_pages(pos
, iov_iter_count(ii
));
1134 req
= fuse_get_req(fc
, nr_pages
);
1140 count
= fuse_fill_write_pages(req
, mapping
, ii
, pos
);
1146 num_written
= fuse_send_write_pages(req
, iocb
, inode
,
1148 err
= req
->out
.h
.error
;
1153 /* break out of the loop on short write */
1154 if (num_written
!= count
)
1158 fuse_put_request(fc
, req
);
1159 } while (!err
&& iov_iter_count(ii
));
1162 fuse_write_update_size(inode
, pos
);
1164 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1165 fuse_invalidate_attr(inode
);
1167 return res
> 0 ? res
: err
;
1170 static ssize_t
fuse_file_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
1172 struct file
*file
= iocb
->ki_filp
;
1173 struct address_space
*mapping
= file
->f_mapping
;
1174 ssize_t written
= 0;
1175 ssize_t written_buffered
= 0;
1176 struct inode
*inode
= mapping
->host
;
1180 if (get_fuse_conn(inode
)->writeback_cache
) {
1181 /* Update size (EOF optimization) and mode (SUID clearing) */
1182 err
= fuse_update_attributes(mapping
->host
, file
);
1186 return generic_file_write_iter(iocb
, from
);
1191 /* We can write back this queue in page reclaim */
1192 current
->backing_dev_info
= inode_to_bdi(inode
);
1194 err
= generic_write_checks(iocb
, from
);
1198 err
= file_remove_privs(file
);
1202 err
= file_update_time(file
);
1206 if (iocb
->ki_flags
& IOCB_DIRECT
) {
1207 loff_t pos
= iocb
->ki_pos
;
1208 written
= generic_file_direct_write(iocb
, from
);
1209 if (written
< 0 || !iov_iter_count(from
))
1214 written_buffered
= fuse_perform_write(iocb
, mapping
, from
, pos
);
1215 if (written_buffered
< 0) {
1216 err
= written_buffered
;
1219 endbyte
= pos
+ written_buffered
- 1;
1221 err
= filemap_write_and_wait_range(file
->f_mapping
, pos
,
1226 invalidate_mapping_pages(file
->f_mapping
,
1228 endbyte
>> PAGE_SHIFT
);
1230 written
+= written_buffered
;
1231 iocb
->ki_pos
= pos
+ written_buffered
;
1233 written
= fuse_perform_write(iocb
, mapping
, from
, iocb
->ki_pos
);
1235 iocb
->ki_pos
+= written
;
1238 current
->backing_dev_info
= NULL
;
1239 inode_unlock(inode
);
1241 written
= generic_write_sync(iocb
, written
);
1243 return written
? written
: err
;
1246 static inline void fuse_page_descs_length_init(struct fuse_req
*req
,
1247 unsigned index
, unsigned nr_pages
)
1251 for (i
= index
; i
< index
+ nr_pages
; i
++)
1252 req
->page_descs
[i
].length
= PAGE_SIZE
-
1253 req
->page_descs
[i
].offset
;
1256 static inline unsigned long fuse_get_user_addr(const struct iov_iter
*ii
)
1258 return (unsigned long)ii
->iov
->iov_base
+ ii
->iov_offset
;
1261 static inline size_t fuse_get_frag_size(const struct iov_iter
*ii
,
1264 return min(iov_iter_single_seg_count(ii
), max_size
);
1267 static int fuse_get_user_pages(struct fuse_req
*req
, struct iov_iter
*ii
,
1268 size_t *nbytesp
, int write
)
1270 size_t nbytes
= 0; /* # bytes already packed in req */
1273 /* Special case for kernel I/O: can copy directly into the buffer */
1274 if (ii
->type
& ITER_KVEC
) {
1275 unsigned long user_addr
= fuse_get_user_addr(ii
);
1276 size_t frag_size
= fuse_get_frag_size(ii
, *nbytesp
);
1279 req
->in
.args
[1].value
= (void *) user_addr
;
1281 req
->out
.args
[0].value
= (void *) user_addr
;
1283 iov_iter_advance(ii
, frag_size
);
1284 *nbytesp
= frag_size
;
1288 while (nbytes
< *nbytesp
&& req
->num_pages
< req
->max_pages
) {
1291 ret
= iov_iter_get_pages(ii
, &req
->pages
[req
->num_pages
],
1293 req
->max_pages
- req
->num_pages
,
1298 iov_iter_advance(ii
, ret
);
1302 npages
= (ret
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
1304 req
->page_descs
[req
->num_pages
].offset
= start
;
1305 fuse_page_descs_length_init(req
, req
->num_pages
, npages
);
1307 req
->num_pages
+= npages
;
1308 req
->page_descs
[req
->num_pages
- 1].length
-=
1309 (PAGE_SIZE
- ret
) & (PAGE_SIZE
- 1);
1313 req
->in
.argpages
= 1;
1315 req
->out
.argpages
= 1;
1319 return ret
< 0 ? ret
: 0;
1322 static inline int fuse_iter_npages(const struct iov_iter
*ii_p
)
1324 return iov_iter_npages(ii_p
, FUSE_MAX_PAGES_PER_REQ
);
1327 ssize_t
fuse_direct_io(struct fuse_io_priv
*io
, struct iov_iter
*iter
,
1328 loff_t
*ppos
, int flags
)
1330 int write
= flags
& FUSE_DIO_WRITE
;
1331 int cuse
= flags
& FUSE_DIO_CUSE
;
1332 struct file
*file
= io
->iocb
->ki_filp
;
1333 struct inode
*inode
= file
->f_mapping
->host
;
1334 struct fuse_file
*ff
= file
->private_data
;
1335 struct fuse_conn
*fc
= ff
->fc
;
1336 size_t nmax
= write
? fc
->max_write
: fc
->max_read
;
1338 size_t count
= iov_iter_count(iter
);
1339 pgoff_t idx_from
= pos
>> PAGE_SHIFT
;
1340 pgoff_t idx_to
= (pos
+ count
- 1) >> PAGE_SHIFT
;
1342 struct fuse_req
*req
;
1346 req
= fuse_get_req_for_background(fc
, fuse_iter_npages(iter
));
1348 req
= fuse_get_req(fc
, fuse_iter_npages(iter
));
1350 return PTR_ERR(req
);
1352 if (!cuse
&& fuse_range_is_writeback(inode
, idx_from
, idx_to
)) {
1355 fuse_sync_writes(inode
);
1357 inode_unlock(inode
);
1360 io
->should_dirty
= !write
&& iter_is_iovec(iter
);
1363 fl_owner_t owner
= current
->files
;
1364 size_t nbytes
= min(count
, nmax
);
1365 err
= fuse_get_user_pages(req
, iter
, &nbytes
, write
);
1370 nres
= fuse_send_write(req
, io
, pos
, nbytes
, owner
);
1372 nres
= fuse_send_read(req
, io
, pos
, nbytes
, owner
);
1375 fuse_release_user_pages(req
, io
->should_dirty
);
1376 if (req
->out
.h
.error
) {
1377 err
= req
->out
.h
.error
;
1379 } else if (nres
> nbytes
) {
1390 fuse_put_request(fc
, req
);
1392 req
= fuse_get_req_for_background(fc
,
1393 fuse_iter_npages(iter
));
1395 req
= fuse_get_req(fc
, fuse_iter_npages(iter
));
1401 fuse_put_request(fc
, req
);
1405 return res
> 0 ? res
: err
;
1407 EXPORT_SYMBOL_GPL(fuse_direct_io
);
1409 static ssize_t
__fuse_direct_read(struct fuse_io_priv
*io
,
1410 struct iov_iter
*iter
,
1414 struct inode
*inode
= file_inode(io
->iocb
->ki_filp
);
1416 if (is_bad_inode(inode
))
1419 res
= fuse_direct_io(io
, iter
, ppos
, 0);
1421 fuse_invalidate_attr(inode
);
1426 static ssize_t
fuse_direct_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
1428 struct fuse_io_priv io
= FUSE_IO_PRIV_SYNC(iocb
);
1429 return __fuse_direct_read(&io
, to
, &iocb
->ki_pos
);
1432 static ssize_t
fuse_direct_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
1434 struct inode
*inode
= file_inode(iocb
->ki_filp
);
1435 struct fuse_io_priv io
= FUSE_IO_PRIV_SYNC(iocb
);
1438 if (is_bad_inode(inode
))
1441 /* Don't allow parallel writes to the same file */
1443 res
= generic_write_checks(iocb
, from
);
1445 res
= fuse_direct_io(&io
, from
, &iocb
->ki_pos
, FUSE_DIO_WRITE
);
1446 fuse_invalidate_attr(inode
);
1448 fuse_write_update_size(inode
, iocb
->ki_pos
);
1449 inode_unlock(inode
);
1454 static void fuse_writepage_free(struct fuse_conn
*fc
, struct fuse_req
*req
)
1458 for (i
= 0; i
< req
->num_pages
; i
++)
1459 __free_page(req
->pages
[i
]);
1462 fuse_file_put(req
->ff
, false);
1465 static void fuse_writepage_finish(struct fuse_conn
*fc
, struct fuse_req
*req
)
1467 struct inode
*inode
= req
->inode
;
1468 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1469 struct backing_dev_info
*bdi
= inode_to_bdi(inode
);
1472 list_del(&req
->writepages_entry
);
1473 for (i
= 0; i
< req
->num_pages
; i
++) {
1474 dec_wb_stat(&bdi
->wb
, WB_WRITEBACK
);
1475 dec_node_page_state(req
->pages
[i
], NR_WRITEBACK_TEMP
);
1476 wb_writeout_inc(&bdi
->wb
);
1478 wake_up(&fi
->page_waitq
);
1481 /* Called under fc->lock, may release and reacquire it */
1482 static void fuse_send_writepage(struct fuse_conn
*fc
, struct fuse_req
*req
,
1484 __releases(fc
->lock
)
1485 __acquires(fc
->lock
)
1487 struct fuse_inode
*fi
= get_fuse_inode(req
->inode
);
1488 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
1489 __u64 data_size
= req
->num_pages
* PAGE_SIZE
;
1494 if (inarg
->offset
+ data_size
<= size
) {
1495 inarg
->size
= data_size
;
1496 } else if (inarg
->offset
< size
) {
1497 inarg
->size
= size
- inarg
->offset
;
1499 /* Got truncated off completely */
1503 req
->in
.args
[1].size
= inarg
->size
;
1505 fuse_request_send_background_locked(fc
, req
);
1509 fuse_writepage_finish(fc
, req
);
1510 spin_unlock(&fc
->lock
);
1511 fuse_writepage_free(fc
, req
);
1512 fuse_put_request(fc
, req
);
1513 spin_lock(&fc
->lock
);
1517 * If fi->writectr is positive (no truncate or fsync going on) send
1518 * all queued writepage requests.
1520 * Called with fc->lock
1522 void fuse_flush_writepages(struct inode
*inode
)
1523 __releases(fc
->lock
)
1524 __acquires(fc
->lock
)
1526 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1527 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1528 size_t crop
= i_size_read(inode
);
1529 struct fuse_req
*req
;
1531 while (fi
->writectr
>= 0 && !list_empty(&fi
->queued_writes
)) {
1532 req
= list_entry(fi
->queued_writes
.next
, struct fuse_req
, list
);
1533 list_del_init(&req
->list
);
1534 fuse_send_writepage(fc
, req
, crop
);
1538 static void fuse_writepage_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
1540 struct inode
*inode
= req
->inode
;
1541 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1543 mapping_set_error(inode
->i_mapping
, req
->out
.h
.error
);
1544 spin_lock(&fc
->lock
);
1545 while (req
->misc
.write
.next
) {
1546 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1547 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
1548 struct fuse_req
*next
= req
->misc
.write
.next
;
1549 req
->misc
.write
.next
= next
->misc
.write
.next
;
1550 next
->misc
.write
.next
= NULL
;
1551 next
->ff
= fuse_file_get(req
->ff
);
1552 list_add(&next
->writepages_entry
, &fi
->writepages
);
1555 * Skip fuse_flush_writepages() to make it easy to crop requests
1556 * based on primary request size.
1558 * 1st case (trivial): there are no concurrent activities using
1559 * fuse_set/release_nowrite. Then we're on safe side because
1560 * fuse_flush_writepages() would call fuse_send_writepage()
1563 * 2nd case: someone called fuse_set_nowrite and it is waiting
1564 * now for completion of all in-flight requests. This happens
1565 * rarely and no more than once per page, so this should be
1568 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1569 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1570 * that fuse_set_nowrite returned implies that all in-flight
1571 * requests were completed along with all of their secondary
1572 * requests. Further primary requests are blocked by negative
1573 * writectr. Hence there cannot be any in-flight requests and
1574 * no invocations of fuse_writepage_end() while we're in
1575 * fuse_set_nowrite..fuse_release_nowrite section.
1577 fuse_send_writepage(fc
, next
, inarg
->offset
+ inarg
->size
);
1580 fuse_writepage_finish(fc
, req
);
1581 spin_unlock(&fc
->lock
);
1582 fuse_writepage_free(fc
, req
);
1585 static struct fuse_file
*__fuse_write_file_get(struct fuse_conn
*fc
,
1586 struct fuse_inode
*fi
)
1588 struct fuse_file
*ff
= NULL
;
1590 spin_lock(&fc
->lock
);
1591 if (!list_empty(&fi
->write_files
)) {
1592 ff
= list_entry(fi
->write_files
.next
, struct fuse_file
,
1596 spin_unlock(&fc
->lock
);
1601 static struct fuse_file
*fuse_write_file_get(struct fuse_conn
*fc
,
1602 struct fuse_inode
*fi
)
1604 struct fuse_file
*ff
= __fuse_write_file_get(fc
, fi
);
1609 int fuse_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
1611 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1612 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1613 struct fuse_file
*ff
;
1616 ff
= __fuse_write_file_get(fc
, fi
);
1617 err
= fuse_flush_times(inode
, ff
);
1619 fuse_file_put(ff
, 0);
1624 static int fuse_writepage_locked(struct page
*page
)
1626 struct address_space
*mapping
= page
->mapping
;
1627 struct inode
*inode
= mapping
->host
;
1628 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1629 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1630 struct fuse_req
*req
;
1631 struct page
*tmp_page
;
1632 int error
= -ENOMEM
;
1634 set_page_writeback(page
);
1636 req
= fuse_request_alloc_nofs(1);
1640 /* writeback always goes to bg_queue */
1641 __set_bit(FR_BACKGROUND
, &req
->flags
);
1642 tmp_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
1647 req
->ff
= fuse_write_file_get(fc
, fi
);
1651 fuse_write_fill(req
, req
->ff
, page_offset(page
), 0);
1653 copy_highpage(tmp_page
, page
);
1654 req
->misc
.write
.in
.write_flags
|= FUSE_WRITE_CACHE
;
1655 req
->misc
.write
.next
= NULL
;
1656 req
->in
.argpages
= 1;
1658 req
->pages
[0] = tmp_page
;
1659 req
->page_descs
[0].offset
= 0;
1660 req
->page_descs
[0].length
= PAGE_SIZE
;
1661 req
->end
= fuse_writepage_end
;
1664 inc_wb_stat(&inode_to_bdi(inode
)->wb
, WB_WRITEBACK
);
1665 inc_node_page_state(tmp_page
, NR_WRITEBACK_TEMP
);
1667 spin_lock(&fc
->lock
);
1668 list_add(&req
->writepages_entry
, &fi
->writepages
);
1669 list_add_tail(&req
->list
, &fi
->queued_writes
);
1670 fuse_flush_writepages(inode
);
1671 spin_unlock(&fc
->lock
);
1673 end_page_writeback(page
);
1678 __free_page(tmp_page
);
1680 fuse_request_free(req
);
1682 mapping_set_error(page
->mapping
, error
);
1683 end_page_writeback(page
);
1687 static int fuse_writepage(struct page
*page
, struct writeback_control
*wbc
)
1691 if (fuse_page_is_writeback(page
->mapping
->host
, page
->index
)) {
1693 * ->writepages() should be called for sync() and friends. We
1694 * should only get here on direct reclaim and then we are
1695 * allowed to skip a page which is already in flight
1697 WARN_ON(wbc
->sync_mode
== WB_SYNC_ALL
);
1699 redirty_page_for_writepage(wbc
, page
);
1703 err
= fuse_writepage_locked(page
);
1709 struct fuse_fill_wb_data
{
1710 struct fuse_req
*req
;
1711 struct fuse_file
*ff
;
1712 struct inode
*inode
;
1713 struct page
**orig_pages
;
1716 static void fuse_writepages_send(struct fuse_fill_wb_data
*data
)
1718 struct fuse_req
*req
= data
->req
;
1719 struct inode
*inode
= data
->inode
;
1720 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1721 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1722 int num_pages
= req
->num_pages
;
1725 req
->ff
= fuse_file_get(data
->ff
);
1726 spin_lock(&fc
->lock
);
1727 list_add_tail(&req
->list
, &fi
->queued_writes
);
1728 fuse_flush_writepages(inode
);
1729 spin_unlock(&fc
->lock
);
1731 for (i
= 0; i
< num_pages
; i
++)
1732 end_page_writeback(data
->orig_pages
[i
]);
1735 static bool fuse_writepage_in_flight(struct fuse_req
*new_req
,
1738 struct fuse_conn
*fc
= get_fuse_conn(new_req
->inode
);
1739 struct fuse_inode
*fi
= get_fuse_inode(new_req
->inode
);
1740 struct fuse_req
*tmp
;
1741 struct fuse_req
*old_req
;
1745 BUG_ON(new_req
->num_pages
!= 0);
1747 spin_lock(&fc
->lock
);
1748 list_del(&new_req
->writepages_entry
);
1749 list_for_each_entry(old_req
, &fi
->writepages
, writepages_entry
) {
1750 BUG_ON(old_req
->inode
!= new_req
->inode
);
1751 curr_index
= old_req
->misc
.write
.in
.offset
>> PAGE_SHIFT
;
1752 if (curr_index
<= page
->index
&&
1753 page
->index
< curr_index
+ old_req
->num_pages
) {
1759 list_add(&new_req
->writepages_entry
, &fi
->writepages
);
1763 new_req
->num_pages
= 1;
1764 for (tmp
= old_req
; tmp
!= NULL
; tmp
= tmp
->misc
.write
.next
) {
1765 BUG_ON(tmp
->inode
!= new_req
->inode
);
1766 curr_index
= tmp
->misc
.write
.in
.offset
>> PAGE_SHIFT
;
1767 if (tmp
->num_pages
== 1 &&
1768 curr_index
== page
->index
) {
1773 if (old_req
->num_pages
== 1 && test_bit(FR_PENDING
, &old_req
->flags
)) {
1774 struct backing_dev_info
*bdi
= inode_to_bdi(page
->mapping
->host
);
1776 copy_highpage(old_req
->pages
[0], page
);
1777 spin_unlock(&fc
->lock
);
1779 dec_wb_stat(&bdi
->wb
, WB_WRITEBACK
);
1780 dec_node_page_state(page
, NR_WRITEBACK_TEMP
);
1781 wb_writeout_inc(&bdi
->wb
);
1782 fuse_writepage_free(fc
, new_req
);
1783 fuse_request_free(new_req
);
1786 new_req
->misc
.write
.next
= old_req
->misc
.write
.next
;
1787 old_req
->misc
.write
.next
= new_req
;
1790 spin_unlock(&fc
->lock
);
1795 static int fuse_writepages_fill(struct page
*page
,
1796 struct writeback_control
*wbc
, void *_data
)
1798 struct fuse_fill_wb_data
*data
= _data
;
1799 struct fuse_req
*req
= data
->req
;
1800 struct inode
*inode
= data
->inode
;
1801 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1802 struct page
*tmp_page
;
1808 data
->ff
= fuse_write_file_get(fc
, get_fuse_inode(inode
));
1814 * Being under writeback is unlikely but possible. For example direct
1815 * read to an mmaped fuse file will set the page dirty twice; once when
1816 * the pages are faulted with get_user_pages(), and then after the read
1819 is_writeback
= fuse_page_is_writeback(inode
, page
->index
);
1821 if (req
&& req
->num_pages
&&
1822 (is_writeback
|| req
->num_pages
== FUSE_MAX_PAGES_PER_REQ
||
1823 (req
->num_pages
+ 1) * PAGE_SIZE
> fc
->max_write
||
1824 data
->orig_pages
[req
->num_pages
- 1]->index
+ 1 != page
->index
)) {
1825 fuse_writepages_send(data
);
1829 tmp_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
1834 * The page must not be redirtied until the writeout is completed
1835 * (i.e. userspace has sent a reply to the write request). Otherwise
1836 * there could be more than one temporary page instance for each real
1839 * This is ensured by holding the page lock in page_mkwrite() while
1840 * checking fuse_page_is_writeback(). We already hold the page lock
1841 * since clear_page_dirty_for_io() and keep it held until we add the
1842 * request to the fi->writepages list and increment req->num_pages.
1843 * After this fuse_page_is_writeback() will indicate that the page is
1844 * under writeback, so we can release the page lock.
1846 if (data
->req
== NULL
) {
1847 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1850 req
= fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ
);
1852 __free_page(tmp_page
);
1856 fuse_write_fill(req
, data
->ff
, page_offset(page
), 0);
1857 req
->misc
.write
.in
.write_flags
|= FUSE_WRITE_CACHE
;
1858 req
->misc
.write
.next
= NULL
;
1859 req
->in
.argpages
= 1;
1860 __set_bit(FR_BACKGROUND
, &req
->flags
);
1862 req
->end
= fuse_writepage_end
;
1865 spin_lock(&fc
->lock
);
1866 list_add(&req
->writepages_entry
, &fi
->writepages
);
1867 spin_unlock(&fc
->lock
);
1871 set_page_writeback(page
);
1873 copy_highpage(tmp_page
, page
);
1874 req
->pages
[req
->num_pages
] = tmp_page
;
1875 req
->page_descs
[req
->num_pages
].offset
= 0;
1876 req
->page_descs
[req
->num_pages
].length
= PAGE_SIZE
;
1878 inc_wb_stat(&inode_to_bdi(inode
)->wb
, WB_WRITEBACK
);
1879 inc_node_page_state(tmp_page
, NR_WRITEBACK_TEMP
);
1882 if (is_writeback
&& fuse_writepage_in_flight(req
, page
)) {
1883 end_page_writeback(page
);
1887 data
->orig_pages
[req
->num_pages
] = page
;
1890 * Protected by fc->lock against concurrent access by
1891 * fuse_page_is_writeback().
1893 spin_lock(&fc
->lock
);
1895 spin_unlock(&fc
->lock
);
1903 static int fuse_writepages(struct address_space
*mapping
,
1904 struct writeback_control
*wbc
)
1906 struct inode
*inode
= mapping
->host
;
1907 struct fuse_fill_wb_data data
;
1911 if (is_bad_inode(inode
))
1919 data
.orig_pages
= kcalloc(FUSE_MAX_PAGES_PER_REQ
,
1920 sizeof(struct page
*),
1922 if (!data
.orig_pages
)
1925 err
= write_cache_pages(mapping
, wbc
, fuse_writepages_fill
, &data
);
1927 /* Ignore errors if we can write at least one page */
1928 BUG_ON(!data
.req
->num_pages
);
1929 fuse_writepages_send(&data
);
1933 fuse_file_put(data
.ff
, false);
1935 kfree(data
.orig_pages
);
1941 * It's worthy to make sure that space is reserved on disk for the write,
1942 * but how to implement it without killing performance need more thinking.
1944 static int fuse_write_begin(struct file
*file
, struct address_space
*mapping
,
1945 loff_t pos
, unsigned len
, unsigned flags
,
1946 struct page
**pagep
, void **fsdata
)
1948 pgoff_t index
= pos
>> PAGE_SHIFT
;
1949 struct fuse_conn
*fc
= get_fuse_conn(file_inode(file
));
1954 WARN_ON(!fc
->writeback_cache
);
1956 page
= grab_cache_page_write_begin(mapping
, index
, flags
);
1960 fuse_wait_on_page_writeback(mapping
->host
, page
->index
);
1962 if (PageUptodate(page
) || len
== PAGE_SIZE
)
1965 * Check if the start this page comes after the end of file, in which
1966 * case the readpage can be optimized away.
1968 fsize
= i_size_read(mapping
->host
);
1969 if (fsize
<= (pos
& PAGE_MASK
)) {
1970 size_t off
= pos
& ~PAGE_MASK
;
1972 zero_user_segment(page
, 0, off
);
1975 err
= fuse_do_readpage(file
, page
);
1989 static int fuse_write_end(struct file
*file
, struct address_space
*mapping
,
1990 loff_t pos
, unsigned len
, unsigned copied
,
1991 struct page
*page
, void *fsdata
)
1993 struct inode
*inode
= page
->mapping
->host
;
1995 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
1999 if (!PageUptodate(page
)) {
2000 /* Zero any unwritten bytes at the end of the page */
2001 size_t endoff
= (pos
+ copied
) & ~PAGE_MASK
;
2003 zero_user_segment(page
, endoff
, PAGE_SIZE
);
2004 SetPageUptodate(page
);
2007 fuse_write_update_size(inode
, pos
+ copied
);
2008 set_page_dirty(page
);
2017 static int fuse_launder_page(struct page
*page
)
2020 if (clear_page_dirty_for_io(page
)) {
2021 struct inode
*inode
= page
->mapping
->host
;
2022 err
= fuse_writepage_locked(page
);
2024 fuse_wait_on_page_writeback(inode
, page
->index
);
2030 * Write back dirty pages now, because there may not be any suitable
2033 static void fuse_vma_close(struct vm_area_struct
*vma
)
2035 filemap_write_and_wait(vma
->vm_file
->f_mapping
);
2039 * Wait for writeback against this page to complete before allowing it
2040 * to be marked dirty again, and hence written back again, possibly
2041 * before the previous writepage completed.
2043 * Block here, instead of in ->writepage(), so that the userspace fs
2044 * can only block processes actually operating on the filesystem.
2046 * Otherwise unprivileged userspace fs would be able to block
2051 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2053 static vm_fault_t
fuse_page_mkwrite(struct vm_fault
*vmf
)
2055 struct page
*page
= vmf
->page
;
2056 struct inode
*inode
= file_inode(vmf
->vma
->vm_file
);
2058 file_update_time(vmf
->vma
->vm_file
);
2060 if (page
->mapping
!= inode
->i_mapping
) {
2062 return VM_FAULT_NOPAGE
;
2065 fuse_wait_on_page_writeback(inode
, page
->index
);
2066 return VM_FAULT_LOCKED
;
2069 static const struct vm_operations_struct fuse_file_vm_ops
= {
2070 .close
= fuse_vma_close
,
2071 .fault
= filemap_fault
,
2072 .map_pages
= filemap_map_pages
,
2073 .page_mkwrite
= fuse_page_mkwrite
,
2076 static int fuse_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2078 if ((vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
))
2079 fuse_link_write_file(file
);
2081 file_accessed(file
);
2082 vma
->vm_ops
= &fuse_file_vm_ops
;
2086 static int fuse_direct_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2088 /* Can't provide the coherency needed for MAP_SHARED */
2089 if (vma
->vm_flags
& VM_MAYSHARE
)
2092 invalidate_inode_pages2(file
->f_mapping
);
2094 return generic_file_mmap(file
, vma
);
2097 static int convert_fuse_file_lock(struct fuse_conn
*fc
,
2098 const struct fuse_file_lock
*ffl
,
2099 struct file_lock
*fl
)
2101 switch (ffl
->type
) {
2107 if (ffl
->start
> OFFSET_MAX
|| ffl
->end
> OFFSET_MAX
||
2108 ffl
->end
< ffl
->start
)
2111 fl
->fl_start
= ffl
->start
;
2112 fl
->fl_end
= ffl
->end
;
2115 * Convert pid into init's pid namespace. The locks API will
2116 * translate it into the caller's pid namespace.
2119 fl
->fl_pid
= pid_nr_ns(find_pid_ns(ffl
->pid
, fc
->pid_ns
), &init_pid_ns
);
2126 fl
->fl_type
= ffl
->type
;
2130 static void fuse_lk_fill(struct fuse_args
*args
, struct file
*file
,
2131 const struct file_lock
*fl
, int opcode
, pid_t pid
,
2132 int flock
, struct fuse_lk_in
*inarg
)
2134 struct inode
*inode
= file_inode(file
);
2135 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2136 struct fuse_file
*ff
= file
->private_data
;
2138 memset(inarg
, 0, sizeof(*inarg
));
2140 inarg
->owner
= fuse_lock_owner_id(fc
, fl
->fl_owner
);
2141 inarg
->lk
.start
= fl
->fl_start
;
2142 inarg
->lk
.end
= fl
->fl_end
;
2143 inarg
->lk
.type
= fl
->fl_type
;
2144 inarg
->lk
.pid
= pid
;
2146 inarg
->lk_flags
|= FUSE_LK_FLOCK
;
2147 args
->in
.h
.opcode
= opcode
;
2148 args
->in
.h
.nodeid
= get_node_id(inode
);
2149 args
->in
.numargs
= 1;
2150 args
->in
.args
[0].size
= sizeof(*inarg
);
2151 args
->in
.args
[0].value
= inarg
;
2154 static int fuse_getlk(struct file
*file
, struct file_lock
*fl
)
2156 struct inode
*inode
= file_inode(file
);
2157 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2159 struct fuse_lk_in inarg
;
2160 struct fuse_lk_out outarg
;
2163 fuse_lk_fill(&args
, file
, fl
, FUSE_GETLK
, 0, 0, &inarg
);
2164 args
.out
.numargs
= 1;
2165 args
.out
.args
[0].size
= sizeof(outarg
);
2166 args
.out
.args
[0].value
= &outarg
;
2167 err
= fuse_simple_request(fc
, &args
);
2169 err
= convert_fuse_file_lock(fc
, &outarg
.lk
, fl
);
2174 static int fuse_setlk(struct file
*file
, struct file_lock
*fl
, int flock
)
2176 struct inode
*inode
= file_inode(file
);
2177 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2179 struct fuse_lk_in inarg
;
2180 int opcode
= (fl
->fl_flags
& FL_SLEEP
) ? FUSE_SETLKW
: FUSE_SETLK
;
2181 struct pid
*pid
= fl
->fl_type
!= F_UNLCK
? task_tgid(current
) : NULL
;
2182 pid_t pid_nr
= pid_nr_ns(pid
, fc
->pid_ns
);
2185 if (fl
->fl_lmops
&& fl
->fl_lmops
->lm_grant
) {
2186 /* NLM needs asynchronous locks, which we don't support yet */
2190 /* Unlock on close is handled by the flush method */
2191 if ((fl
->fl_flags
& FL_CLOSE_POSIX
) == FL_CLOSE_POSIX
)
2194 fuse_lk_fill(&args
, file
, fl
, opcode
, pid_nr
, flock
, &inarg
);
2195 err
= fuse_simple_request(fc
, &args
);
2197 /* locking is restartable */
2204 static int fuse_file_lock(struct file
*file
, int cmd
, struct file_lock
*fl
)
2206 struct inode
*inode
= file_inode(file
);
2207 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2210 if (cmd
== F_CANCELLK
) {
2212 } else if (cmd
== F_GETLK
) {
2214 posix_test_lock(file
, fl
);
2217 err
= fuse_getlk(file
, fl
);
2220 err
= posix_lock_file(file
, fl
, NULL
);
2222 err
= fuse_setlk(file
, fl
, 0);
2227 static int fuse_file_flock(struct file
*file
, int cmd
, struct file_lock
*fl
)
2229 struct inode
*inode
= file_inode(file
);
2230 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2234 err
= locks_lock_file_wait(file
, fl
);
2236 struct fuse_file
*ff
= file
->private_data
;
2238 /* emulate flock with POSIX locks */
2240 err
= fuse_setlk(file
, fl
, 1);
2246 static sector_t
fuse_bmap(struct address_space
*mapping
, sector_t block
)
2248 struct inode
*inode
= mapping
->host
;
2249 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2251 struct fuse_bmap_in inarg
;
2252 struct fuse_bmap_out outarg
;
2255 if (!inode
->i_sb
->s_bdev
|| fc
->no_bmap
)
2258 memset(&inarg
, 0, sizeof(inarg
));
2259 inarg
.block
= block
;
2260 inarg
.blocksize
= inode
->i_sb
->s_blocksize
;
2261 args
.in
.h
.opcode
= FUSE_BMAP
;
2262 args
.in
.h
.nodeid
= get_node_id(inode
);
2263 args
.in
.numargs
= 1;
2264 args
.in
.args
[0].size
= sizeof(inarg
);
2265 args
.in
.args
[0].value
= &inarg
;
2266 args
.out
.numargs
= 1;
2267 args
.out
.args
[0].size
= sizeof(outarg
);
2268 args
.out
.args
[0].value
= &outarg
;
2269 err
= fuse_simple_request(fc
, &args
);
2273 return err
? 0 : outarg
.block
;
2276 static loff_t
fuse_lseek(struct file
*file
, loff_t offset
, int whence
)
2278 struct inode
*inode
= file
->f_mapping
->host
;
2279 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2280 struct fuse_file
*ff
= file
->private_data
;
2282 struct fuse_lseek_in inarg
= {
2287 struct fuse_lseek_out outarg
;
2293 args
.in
.h
.opcode
= FUSE_LSEEK
;
2294 args
.in
.h
.nodeid
= ff
->nodeid
;
2295 args
.in
.numargs
= 1;
2296 args
.in
.args
[0].size
= sizeof(inarg
);
2297 args
.in
.args
[0].value
= &inarg
;
2298 args
.out
.numargs
= 1;
2299 args
.out
.args
[0].size
= sizeof(outarg
);
2300 args
.out
.args
[0].value
= &outarg
;
2301 err
= fuse_simple_request(fc
, &args
);
2303 if (err
== -ENOSYS
) {
2310 return vfs_setpos(file
, outarg
.offset
, inode
->i_sb
->s_maxbytes
);
2313 err
= fuse_update_attributes(inode
, file
);
2315 return generic_file_llseek(file
, offset
, whence
);
2320 static loff_t
fuse_file_llseek(struct file
*file
, loff_t offset
, int whence
)
2323 struct inode
*inode
= file_inode(file
);
2328 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2329 retval
= generic_file_llseek(file
, offset
, whence
);
2333 retval
= fuse_update_attributes(inode
, file
);
2335 retval
= generic_file_llseek(file
, offset
, whence
);
2336 inode_unlock(inode
);
2341 retval
= fuse_lseek(file
, offset
, whence
);
2342 inode_unlock(inode
);
2352 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2353 * ABI was defined to be 'struct iovec' which is different on 32bit
2354 * and 64bit. Fortunately we can determine which structure the server
2355 * used from the size of the reply.
2357 static int fuse_copy_ioctl_iovec_old(struct iovec
*dst
, void *src
,
2358 size_t transferred
, unsigned count
,
2361 #ifdef CONFIG_COMPAT
2362 if (count
* sizeof(struct compat_iovec
) == transferred
) {
2363 struct compat_iovec
*ciov
= src
;
2367 * With this interface a 32bit server cannot support
2368 * non-compat (i.e. ones coming from 64bit apps) ioctl
2374 for (i
= 0; i
< count
; i
++) {
2375 dst
[i
].iov_base
= compat_ptr(ciov
[i
].iov_base
);
2376 dst
[i
].iov_len
= ciov
[i
].iov_len
;
2382 if (count
* sizeof(struct iovec
) != transferred
)
2385 memcpy(dst
, src
, transferred
);
2389 /* Make sure iov_length() won't overflow */
2390 static int fuse_verify_ioctl_iov(struct iovec
*iov
, size_t count
)
2393 u32 max
= FUSE_MAX_PAGES_PER_REQ
<< PAGE_SHIFT
;
2395 for (n
= 0; n
< count
; n
++, iov
++) {
2396 if (iov
->iov_len
> (size_t) max
)
2398 max
-= iov
->iov_len
;
2403 static int fuse_copy_ioctl_iovec(struct fuse_conn
*fc
, struct iovec
*dst
,
2404 void *src
, size_t transferred
, unsigned count
,
2408 struct fuse_ioctl_iovec
*fiov
= src
;
2410 if (fc
->minor
< 16) {
2411 return fuse_copy_ioctl_iovec_old(dst
, src
, transferred
,
2415 if (count
* sizeof(struct fuse_ioctl_iovec
) != transferred
)
2418 for (i
= 0; i
< count
; i
++) {
2419 /* Did the server supply an inappropriate value? */
2420 if (fiov
[i
].base
!= (unsigned long) fiov
[i
].base
||
2421 fiov
[i
].len
!= (unsigned long) fiov
[i
].len
)
2424 dst
[i
].iov_base
= (void __user
*) (unsigned long) fiov
[i
].base
;
2425 dst
[i
].iov_len
= (size_t) fiov
[i
].len
;
2427 #ifdef CONFIG_COMPAT
2429 (ptr_to_compat(dst
[i
].iov_base
) != fiov
[i
].base
||
2430 (compat_size_t
) dst
[i
].iov_len
!= fiov
[i
].len
))
2440 * For ioctls, there is no generic way to determine how much memory
2441 * needs to be read and/or written. Furthermore, ioctls are allowed
2442 * to dereference the passed pointer, so the parameter requires deep
2443 * copying but FUSE has no idea whatsoever about what to copy in or
2446 * This is solved by allowing FUSE server to retry ioctl with
2447 * necessary in/out iovecs. Let's assume the ioctl implementation
2448 * needs to read in the following structure.
2455 * On the first callout to FUSE server, inarg->in_size and
2456 * inarg->out_size will be NULL; then, the server completes the ioctl
2457 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2458 * the actual iov array to
2460 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2462 * which tells FUSE to copy in the requested area and retry the ioctl.
2463 * On the second round, the server has access to the structure and
2464 * from that it can tell what to look for next, so on the invocation,
2465 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2467 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2468 * { .iov_base = a.buf, .iov_len = a.buflen } }
2470 * FUSE will copy both struct a and the pointed buffer from the
2471 * process doing the ioctl and retry ioctl with both struct a and the
2474 * This time, FUSE server has everything it needs and completes ioctl
2475 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2477 * Copying data out works the same way.
2479 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2480 * automatically initializes in and out iovs by decoding @cmd with
2481 * _IOC_* macros and the server is not allowed to request RETRY. This
2482 * limits ioctl data transfers to well-formed ioctls and is the forced
2483 * behavior for all FUSE servers.
2485 long fuse_do_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
,
2488 struct fuse_file
*ff
= file
->private_data
;
2489 struct fuse_conn
*fc
= ff
->fc
;
2490 struct fuse_ioctl_in inarg
= {
2496 struct fuse_ioctl_out outarg
;
2497 struct fuse_req
*req
= NULL
;
2498 struct page
**pages
= NULL
;
2499 struct iovec
*iov_page
= NULL
;
2500 struct iovec
*in_iov
= NULL
, *out_iov
= NULL
;
2501 unsigned int in_iovs
= 0, out_iovs
= 0, num_pages
= 0, max_pages
;
2502 size_t in_size
, out_size
, transferred
, c
;
2506 #if BITS_PER_LONG == 32
2507 inarg
.flags
|= FUSE_IOCTL_32BIT
;
2509 if (flags
& FUSE_IOCTL_COMPAT
)
2510 inarg
.flags
|= FUSE_IOCTL_32BIT
;
2513 /* assume all the iovs returned by client always fits in a page */
2514 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec
) * FUSE_IOCTL_MAX_IOV
> PAGE_SIZE
);
2517 pages
= kcalloc(FUSE_MAX_PAGES_PER_REQ
, sizeof(pages
[0]), GFP_KERNEL
);
2518 iov_page
= (struct iovec
*) __get_free_page(GFP_KERNEL
);
2519 if (!pages
|| !iov_page
)
2523 * If restricted, initialize IO parameters as encoded in @cmd.
2524 * RETRY from server is not allowed.
2526 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
)) {
2527 struct iovec
*iov
= iov_page
;
2529 iov
->iov_base
= (void __user
*)arg
;
2530 iov
->iov_len
= _IOC_SIZE(cmd
);
2532 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
2537 if (_IOC_DIR(cmd
) & _IOC_READ
) {
2544 inarg
.in_size
= in_size
= iov_length(in_iov
, in_iovs
);
2545 inarg
.out_size
= out_size
= iov_length(out_iov
, out_iovs
);
2548 * Out data can be used either for actual out data or iovs,
2549 * make sure there always is at least one page.
2551 out_size
= max_t(size_t, out_size
, PAGE_SIZE
);
2552 max_pages
= DIV_ROUND_UP(max(in_size
, out_size
), PAGE_SIZE
);
2554 /* make sure there are enough buffer pages and init request with them */
2556 if (max_pages
> FUSE_MAX_PAGES_PER_REQ
)
2558 while (num_pages
< max_pages
) {
2559 pages
[num_pages
] = alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
2560 if (!pages
[num_pages
])
2565 req
= fuse_get_req(fc
, num_pages
);
2571 memcpy(req
->pages
, pages
, sizeof(req
->pages
[0]) * num_pages
);
2572 req
->num_pages
= num_pages
;
2573 fuse_page_descs_length_init(req
, 0, req
->num_pages
);
2575 /* okay, let's send it to the client */
2576 req
->in
.h
.opcode
= FUSE_IOCTL
;
2577 req
->in
.h
.nodeid
= ff
->nodeid
;
2578 req
->in
.numargs
= 1;
2579 req
->in
.args
[0].size
= sizeof(inarg
);
2580 req
->in
.args
[0].value
= &inarg
;
2583 req
->in
.args
[1].size
= in_size
;
2584 req
->in
.argpages
= 1;
2587 iov_iter_init(&ii
, WRITE
, in_iov
, in_iovs
, in_size
);
2588 for (i
= 0; iov_iter_count(&ii
) && !WARN_ON(i
>= num_pages
); i
++) {
2589 c
= copy_page_from_iter(pages
[i
], 0, PAGE_SIZE
, &ii
);
2590 if (c
!= PAGE_SIZE
&& iov_iter_count(&ii
))
2595 req
->out
.numargs
= 2;
2596 req
->out
.args
[0].size
= sizeof(outarg
);
2597 req
->out
.args
[0].value
= &outarg
;
2598 req
->out
.args
[1].size
= out_size
;
2599 req
->out
.argpages
= 1;
2600 req
->out
.argvar
= 1;
2602 fuse_request_send(fc
, req
);
2603 err
= req
->out
.h
.error
;
2604 transferred
= req
->out
.args
[1].size
;
2605 fuse_put_request(fc
, req
);
2610 /* did it ask for retry? */
2611 if (outarg
.flags
& FUSE_IOCTL_RETRY
) {
2614 /* no retry if in restricted mode */
2616 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
))
2619 in_iovs
= outarg
.in_iovs
;
2620 out_iovs
= outarg
.out_iovs
;
2623 * Make sure things are in boundary, separate checks
2624 * are to protect against overflow.
2627 if (in_iovs
> FUSE_IOCTL_MAX_IOV
||
2628 out_iovs
> FUSE_IOCTL_MAX_IOV
||
2629 in_iovs
+ out_iovs
> FUSE_IOCTL_MAX_IOV
)
2632 vaddr
= kmap_atomic(pages
[0]);
2633 err
= fuse_copy_ioctl_iovec(fc
, iov_page
, vaddr
,
2634 transferred
, in_iovs
+ out_iovs
,
2635 (flags
& FUSE_IOCTL_COMPAT
) != 0);
2636 kunmap_atomic(vaddr
);
2641 out_iov
= in_iov
+ in_iovs
;
2643 err
= fuse_verify_ioctl_iov(in_iov
, in_iovs
);
2647 err
= fuse_verify_ioctl_iov(out_iov
, out_iovs
);
2655 if (transferred
> inarg
.out_size
)
2659 iov_iter_init(&ii
, READ
, out_iov
, out_iovs
, transferred
);
2660 for (i
= 0; iov_iter_count(&ii
) && !WARN_ON(i
>= num_pages
); i
++) {
2661 c
= copy_page_to_iter(pages
[i
], 0, PAGE_SIZE
, &ii
);
2662 if (c
!= PAGE_SIZE
&& iov_iter_count(&ii
))
2668 fuse_put_request(fc
, req
);
2669 free_page((unsigned long) iov_page
);
2671 __free_page(pages
[--num_pages
]);
2674 return err
? err
: outarg
.result
;
2676 EXPORT_SYMBOL_GPL(fuse_do_ioctl
);
2678 long fuse_ioctl_common(struct file
*file
, unsigned int cmd
,
2679 unsigned long arg
, unsigned int flags
)
2681 struct inode
*inode
= file_inode(file
);
2682 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2684 if (!fuse_allow_current_process(fc
))
2687 if (is_bad_inode(inode
))
2690 return fuse_do_ioctl(file
, cmd
, arg
, flags
);
2693 static long fuse_file_ioctl(struct file
*file
, unsigned int cmd
,
2696 return fuse_ioctl_common(file
, cmd
, arg
, 0);
2699 static long fuse_file_compat_ioctl(struct file
*file
, unsigned int cmd
,
2702 return fuse_ioctl_common(file
, cmd
, arg
, FUSE_IOCTL_COMPAT
);
2706 * All files which have been polled are linked to RB tree
2707 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2708 * find the matching one.
2710 static struct rb_node
**fuse_find_polled_node(struct fuse_conn
*fc
, u64 kh
,
2711 struct rb_node
**parent_out
)
2713 struct rb_node
**link
= &fc
->polled_files
.rb_node
;
2714 struct rb_node
*last
= NULL
;
2717 struct fuse_file
*ff
;
2720 ff
= rb_entry(last
, struct fuse_file
, polled_node
);
2723 link
= &last
->rb_left
;
2724 else if (kh
> ff
->kh
)
2725 link
= &last
->rb_right
;
2736 * The file is about to be polled. Make sure it's on the polled_files
2737 * RB tree. Note that files once added to the polled_files tree are
2738 * not removed before the file is released. This is because a file
2739 * polled once is likely to be polled again.
2741 static void fuse_register_polled_file(struct fuse_conn
*fc
,
2742 struct fuse_file
*ff
)
2744 spin_lock(&fc
->lock
);
2745 if (RB_EMPTY_NODE(&ff
->polled_node
)) {
2746 struct rb_node
**link
, *uninitialized_var(parent
);
2748 link
= fuse_find_polled_node(fc
, ff
->kh
, &parent
);
2750 rb_link_node(&ff
->polled_node
, parent
, link
);
2751 rb_insert_color(&ff
->polled_node
, &fc
->polled_files
);
2753 spin_unlock(&fc
->lock
);
2756 __poll_t
fuse_file_poll(struct file
*file
, poll_table
*wait
)
2758 struct fuse_file
*ff
= file
->private_data
;
2759 struct fuse_conn
*fc
= ff
->fc
;
2760 struct fuse_poll_in inarg
= { .fh
= ff
->fh
, .kh
= ff
->kh
};
2761 struct fuse_poll_out outarg
;
2766 return DEFAULT_POLLMASK
;
2768 poll_wait(file
, &ff
->poll_wait
, wait
);
2769 inarg
.events
= mangle_poll(poll_requested_events(wait
));
2772 * Ask for notification iff there's someone waiting for it.
2773 * The client may ignore the flag and always notify.
2775 if (waitqueue_active(&ff
->poll_wait
)) {
2776 inarg
.flags
|= FUSE_POLL_SCHEDULE_NOTIFY
;
2777 fuse_register_polled_file(fc
, ff
);
2780 args
.in
.h
.opcode
= FUSE_POLL
;
2781 args
.in
.h
.nodeid
= ff
->nodeid
;
2782 args
.in
.numargs
= 1;
2783 args
.in
.args
[0].size
= sizeof(inarg
);
2784 args
.in
.args
[0].value
= &inarg
;
2785 args
.out
.numargs
= 1;
2786 args
.out
.args
[0].size
= sizeof(outarg
);
2787 args
.out
.args
[0].value
= &outarg
;
2788 err
= fuse_simple_request(fc
, &args
);
2791 return demangle_poll(outarg
.revents
);
2792 if (err
== -ENOSYS
) {
2794 return DEFAULT_POLLMASK
;
2798 EXPORT_SYMBOL_GPL(fuse_file_poll
);
2801 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2802 * wakes up the poll waiters.
2804 int fuse_notify_poll_wakeup(struct fuse_conn
*fc
,
2805 struct fuse_notify_poll_wakeup_out
*outarg
)
2807 u64 kh
= outarg
->kh
;
2808 struct rb_node
**link
;
2810 spin_lock(&fc
->lock
);
2812 link
= fuse_find_polled_node(fc
, kh
, NULL
);
2814 struct fuse_file
*ff
;
2816 ff
= rb_entry(*link
, struct fuse_file
, polled_node
);
2817 wake_up_interruptible_sync(&ff
->poll_wait
);
2820 spin_unlock(&fc
->lock
);
2824 static void fuse_do_truncate(struct file
*file
)
2826 struct inode
*inode
= file
->f_mapping
->host
;
2829 attr
.ia_valid
= ATTR_SIZE
;
2830 attr
.ia_size
= i_size_read(inode
);
2832 attr
.ia_file
= file
;
2833 attr
.ia_valid
|= ATTR_FILE
;
2835 fuse_do_setattr(file_dentry(file
), &attr
, file
);
2838 static inline loff_t
fuse_round_up(loff_t off
)
2840 return round_up(off
, FUSE_MAX_PAGES_PER_REQ
<< PAGE_SHIFT
);
2844 fuse_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
)
2846 DECLARE_COMPLETION_ONSTACK(wait
);
2848 struct file
*file
= iocb
->ki_filp
;
2849 struct fuse_file
*ff
= file
->private_data
;
2850 bool async_dio
= ff
->fc
->async_dio
;
2852 struct inode
*inode
;
2854 size_t count
= iov_iter_count(iter
);
2855 loff_t offset
= iocb
->ki_pos
;
2856 struct fuse_io_priv
*io
;
2859 inode
= file
->f_mapping
->host
;
2860 i_size
= i_size_read(inode
);
2862 if ((iov_iter_rw(iter
) == READ
) && (offset
> i_size
))
2865 /* optimization for short read */
2866 if (async_dio
&& iov_iter_rw(iter
) != WRITE
&& offset
+ count
> i_size
) {
2867 if (offset
>= i_size
)
2869 iov_iter_truncate(iter
, fuse_round_up(i_size
- offset
));
2870 count
= iov_iter_count(iter
);
2873 io
= kmalloc(sizeof(struct fuse_io_priv
), GFP_KERNEL
);
2876 spin_lock_init(&io
->lock
);
2877 kref_init(&io
->refcnt
);
2881 io
->offset
= offset
;
2882 io
->write
= (iov_iter_rw(iter
) == WRITE
);
2885 * By default, we want to optimize all I/Os with async request
2886 * submission to the client filesystem if supported.
2888 io
->async
= async_dio
;
2890 io
->blocking
= is_sync_kiocb(iocb
);
2893 * We cannot asynchronously extend the size of a file.
2894 * In such case the aio will behave exactly like sync io.
2896 if ((offset
+ count
> i_size
) && iov_iter_rw(iter
) == WRITE
)
2897 io
->blocking
= true;
2899 if (io
->async
&& io
->blocking
) {
2901 * Additional reference to keep io around after
2902 * calling fuse_aio_complete()
2904 kref_get(&io
->refcnt
);
2908 if (iov_iter_rw(iter
) == WRITE
) {
2909 ret
= fuse_direct_io(io
, iter
, &pos
, FUSE_DIO_WRITE
);
2910 fuse_invalidate_attr(inode
);
2912 ret
= __fuse_direct_read(io
, iter
, &pos
);
2916 fuse_aio_complete(io
, ret
< 0 ? ret
: 0, -1);
2918 /* we have a non-extending, async request, so return */
2920 return -EIOCBQUEUED
;
2922 wait_for_completion(&wait
);
2923 ret
= fuse_get_res_by_io(io
);
2926 kref_put(&io
->refcnt
, fuse_io_release
);
2928 if (iov_iter_rw(iter
) == WRITE
) {
2930 fuse_write_update_size(inode
, pos
);
2931 else if (ret
< 0 && offset
+ count
> i_size
)
2932 fuse_do_truncate(file
);
2938 static long fuse_file_fallocate(struct file
*file
, int mode
, loff_t offset
,
2941 struct fuse_file
*ff
= file
->private_data
;
2942 struct inode
*inode
= file_inode(file
);
2943 struct fuse_inode
*fi
= get_fuse_inode(inode
);
2944 struct fuse_conn
*fc
= ff
->fc
;
2946 struct fuse_fallocate_in inarg
= {
2953 bool lock_inode
= !(mode
& FALLOC_FL_KEEP_SIZE
) ||
2954 (mode
& FALLOC_FL_PUNCH_HOLE
);
2956 if (mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
))
2959 if (fc
->no_fallocate
)
2964 if (mode
& FALLOC_FL_PUNCH_HOLE
) {
2965 loff_t endbyte
= offset
+ length
- 1;
2966 err
= filemap_write_and_wait_range(inode
->i_mapping
,
2971 fuse_sync_writes(inode
);
2975 if (!(mode
& FALLOC_FL_KEEP_SIZE
))
2976 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
2978 args
.in
.h
.opcode
= FUSE_FALLOCATE
;
2979 args
.in
.h
.nodeid
= ff
->nodeid
;
2980 args
.in
.numargs
= 1;
2981 args
.in
.args
[0].size
= sizeof(inarg
);
2982 args
.in
.args
[0].value
= &inarg
;
2983 err
= fuse_simple_request(fc
, &args
);
2984 if (err
== -ENOSYS
) {
2985 fc
->no_fallocate
= 1;
2991 /* we could have extended the file */
2992 if (!(mode
& FALLOC_FL_KEEP_SIZE
)) {
2993 bool changed
= fuse_write_update_size(inode
, offset
+ length
);
2995 if (changed
&& fc
->writeback_cache
)
2996 file_update_time(file
);
2999 if (mode
& FALLOC_FL_PUNCH_HOLE
)
3000 truncate_pagecache_range(inode
, offset
, offset
+ length
- 1);
3002 fuse_invalidate_attr(inode
);
3005 if (!(mode
& FALLOC_FL_KEEP_SIZE
))
3006 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
3009 inode_unlock(inode
);
3014 static const struct file_operations fuse_file_operations
= {
3015 .llseek
= fuse_file_llseek
,
3016 .read_iter
= fuse_file_read_iter
,
3017 .write_iter
= fuse_file_write_iter
,
3018 .mmap
= fuse_file_mmap
,
3020 .flush
= fuse_flush
,
3021 .release
= fuse_release
,
3022 .fsync
= fuse_fsync
,
3023 .lock
= fuse_file_lock
,
3024 .flock
= fuse_file_flock
,
3025 .splice_read
= generic_file_splice_read
,
3026 .unlocked_ioctl
= fuse_file_ioctl
,
3027 .compat_ioctl
= fuse_file_compat_ioctl
,
3028 .poll
= fuse_file_poll
,
3029 .fallocate
= fuse_file_fallocate
,
3032 static const struct file_operations fuse_direct_io_file_operations
= {
3033 .llseek
= fuse_file_llseek
,
3034 .read_iter
= fuse_direct_read_iter
,
3035 .write_iter
= fuse_direct_write_iter
,
3036 .mmap
= fuse_direct_mmap
,
3038 .flush
= fuse_flush
,
3039 .release
= fuse_release
,
3040 .fsync
= fuse_fsync
,
3041 .lock
= fuse_file_lock
,
3042 .flock
= fuse_file_flock
,
3043 .unlocked_ioctl
= fuse_file_ioctl
,
3044 .compat_ioctl
= fuse_file_compat_ioctl
,
3045 .poll
= fuse_file_poll
,
3046 .fallocate
= fuse_file_fallocate
,
3047 /* no splice_read */
3050 static const struct address_space_operations fuse_file_aops
= {
3051 .readpage
= fuse_readpage
,
3052 .writepage
= fuse_writepage
,
3053 .writepages
= fuse_writepages
,
3054 .launder_page
= fuse_launder_page
,
3055 .readpages
= fuse_readpages
,
3056 .set_page_dirty
= __set_page_dirty_nobuffers
,
3058 .direct_IO
= fuse_direct_IO
,
3059 .write_begin
= fuse_write_begin
,
3060 .write_end
= fuse_write_end
,
3063 void fuse_init_file_inode(struct inode
*inode
)
3065 inode
->i_fop
= &fuse_file_operations
;
3066 inode
->i_data
.a_ops
= &fuse_file_aops
;