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/module.h>
16 #include <linux/compat.h>
17 #include <linux/swap.h>
18 #include <linux/aio.h>
19 #include <linux/falloc.h>
21 static const struct file_operations fuse_direct_io_file_operations
;
23 static int fuse_send_open(struct fuse_conn
*fc
, u64 nodeid
, struct file
*file
,
24 int opcode
, struct fuse_open_out
*outargp
)
26 struct fuse_open_in inarg
;
30 req
= fuse_get_req_nopages(fc
);
34 memset(&inarg
, 0, sizeof(inarg
));
35 inarg
.flags
= file
->f_flags
& ~(O_CREAT
| O_EXCL
| O_NOCTTY
);
36 if (!fc
->atomic_o_trunc
)
37 inarg
.flags
&= ~O_TRUNC
;
38 req
->in
.h
.opcode
= opcode
;
39 req
->in
.h
.nodeid
= nodeid
;
41 req
->in
.args
[0].size
= sizeof(inarg
);
42 req
->in
.args
[0].value
= &inarg
;
44 req
->out
.args
[0].size
= sizeof(*outargp
);
45 req
->out
.args
[0].value
= outargp
;
46 fuse_request_send(fc
, req
);
47 err
= req
->out
.h
.error
;
48 fuse_put_request(fc
, req
);
53 struct fuse_file
*fuse_file_alloc(struct fuse_conn
*fc
)
57 ff
= kmalloc(sizeof(struct fuse_file
), GFP_KERNEL
);
62 ff
->reserved_req
= fuse_request_alloc(0);
63 if (unlikely(!ff
->reserved_req
)) {
68 INIT_LIST_HEAD(&ff
->write_entry
);
69 atomic_set(&ff
->count
, 0);
70 RB_CLEAR_NODE(&ff
->polled_node
);
71 init_waitqueue_head(&ff
->poll_wait
);
75 spin_unlock(&fc
->lock
);
80 void fuse_file_free(struct fuse_file
*ff
)
82 fuse_request_free(ff
->reserved_req
);
86 struct fuse_file
*fuse_file_get(struct fuse_file
*ff
)
88 atomic_inc(&ff
->count
);
92 static void fuse_release_async(struct work_struct
*work
)
98 req
= container_of(work
, struct fuse_req
, misc
.release
.work
);
99 path
= req
->misc
.release
.path
;
100 fc
= get_fuse_conn(path
.dentry
->d_inode
);
102 fuse_put_request(fc
, req
);
106 static void fuse_release_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
108 if (fc
->destroy_req
) {
110 * If this is a fuseblk mount, then it's possible that
111 * releasing the path will result in releasing the
112 * super block and sending the DESTROY request. If
113 * the server is single threaded, this would hang.
114 * For this reason do the path_put() in a separate
117 atomic_inc(&req
->count
);
118 INIT_WORK(&req
->misc
.release
.work
, fuse_release_async
);
119 schedule_work(&req
->misc
.release
.work
);
121 path_put(&req
->misc
.release
.path
);
125 static void fuse_file_put(struct fuse_file
*ff
, bool sync
)
127 if (atomic_dec_and_test(&ff
->count
)) {
128 struct fuse_req
*req
= ff
->reserved_req
;
130 if (ff
->fc
->no_open
) {
132 * Drop the release request when client does not
136 path_put(&req
->misc
.release
.path
);
137 fuse_put_request(ff
->fc
, req
);
140 fuse_request_send(ff
->fc
, req
);
141 path_put(&req
->misc
.release
.path
);
142 fuse_put_request(ff
->fc
, req
);
144 req
->end
= fuse_release_end
;
146 fuse_request_send_background(ff
->fc
, req
);
152 int fuse_do_open(struct fuse_conn
*fc
, u64 nodeid
, struct file
*file
,
155 struct fuse_file
*ff
;
156 int opcode
= isdir
? FUSE_OPENDIR
: FUSE_OPEN
;
158 ff
= fuse_file_alloc(fc
);
163 ff
->open_flags
= FOPEN_KEEP_CACHE
; /* Default for no-open */
164 if (!fc
->no_open
|| isdir
) {
165 struct fuse_open_out outarg
;
168 err
= fuse_send_open(fc
, nodeid
, file
, opcode
, &outarg
);
171 ff
->open_flags
= outarg
.open_flags
;
173 } else if (err
!= -ENOSYS
|| isdir
) {
182 ff
->open_flags
&= ~FOPEN_DIRECT_IO
;
185 file
->private_data
= fuse_file_get(ff
);
189 EXPORT_SYMBOL_GPL(fuse_do_open
);
191 static void fuse_link_write_file(struct file
*file
)
193 struct inode
*inode
= file_inode(file
);
194 struct fuse_conn
*fc
= get_fuse_conn(inode
);
195 struct fuse_inode
*fi
= get_fuse_inode(inode
);
196 struct fuse_file
*ff
= file
->private_data
;
198 * file may be written through mmap, so chain it onto the
199 * inodes's write_file list
201 spin_lock(&fc
->lock
);
202 if (list_empty(&ff
->write_entry
))
203 list_add(&ff
->write_entry
, &fi
->write_files
);
204 spin_unlock(&fc
->lock
);
207 void fuse_finish_open(struct inode
*inode
, struct file
*file
)
209 struct fuse_file
*ff
= file
->private_data
;
210 struct fuse_conn
*fc
= get_fuse_conn(inode
);
212 if (ff
->open_flags
& FOPEN_DIRECT_IO
)
213 file
->f_op
= &fuse_direct_io_file_operations
;
214 if (!(ff
->open_flags
& FOPEN_KEEP_CACHE
))
215 invalidate_inode_pages2(inode
->i_mapping
);
216 if (ff
->open_flags
& FOPEN_NONSEEKABLE
)
217 nonseekable_open(inode
, file
);
218 if (fc
->atomic_o_trunc
&& (file
->f_flags
& O_TRUNC
)) {
219 struct fuse_inode
*fi
= get_fuse_inode(inode
);
221 spin_lock(&fc
->lock
);
222 fi
->attr_version
= ++fc
->attr_version
;
223 i_size_write(inode
, 0);
224 spin_unlock(&fc
->lock
);
225 fuse_invalidate_attr(inode
);
226 if (fc
->writeback_cache
)
227 file_update_time(file
);
229 if ((file
->f_mode
& FMODE_WRITE
) && fc
->writeback_cache
)
230 fuse_link_write_file(file
);
233 int fuse_open_common(struct inode
*inode
, struct file
*file
, bool isdir
)
235 struct fuse_conn
*fc
= get_fuse_conn(inode
);
237 bool lock_inode
= (file
->f_flags
& O_TRUNC
) &&
238 fc
->atomic_o_trunc
&&
241 err
= generic_file_open(inode
, file
);
246 mutex_lock(&inode
->i_mutex
);
248 err
= fuse_do_open(fc
, get_node_id(inode
), file
, isdir
);
251 fuse_finish_open(inode
, file
);
254 mutex_unlock(&inode
->i_mutex
);
259 static void fuse_prepare_release(struct fuse_file
*ff
, int flags
, int opcode
)
261 struct fuse_conn
*fc
= ff
->fc
;
262 struct fuse_req
*req
= ff
->reserved_req
;
263 struct fuse_release_in
*inarg
= &req
->misc
.release
.in
;
265 spin_lock(&fc
->lock
);
266 list_del(&ff
->write_entry
);
267 if (!RB_EMPTY_NODE(&ff
->polled_node
))
268 rb_erase(&ff
->polled_node
, &fc
->polled_files
);
269 spin_unlock(&fc
->lock
);
271 wake_up_interruptible_all(&ff
->poll_wait
);
274 inarg
->flags
= flags
;
275 req
->in
.h
.opcode
= opcode
;
276 req
->in
.h
.nodeid
= ff
->nodeid
;
278 req
->in
.args
[0].size
= sizeof(struct fuse_release_in
);
279 req
->in
.args
[0].value
= inarg
;
282 void fuse_release_common(struct file
*file
, int opcode
)
284 struct fuse_file
*ff
;
285 struct fuse_req
*req
;
287 ff
= file
->private_data
;
291 req
= ff
->reserved_req
;
292 fuse_prepare_release(ff
, file
->f_flags
, opcode
);
295 struct fuse_release_in
*inarg
= &req
->misc
.release
.in
;
296 inarg
->release_flags
|= FUSE_RELEASE_FLOCK_UNLOCK
;
297 inarg
->lock_owner
= fuse_lock_owner_id(ff
->fc
,
300 /* Hold vfsmount and dentry until release is finished */
301 path_get(&file
->f_path
);
302 req
->misc
.release
.path
= file
->f_path
;
305 * Normally this will send the RELEASE request, however if
306 * some asynchronous READ or WRITE requests are outstanding,
307 * the sending will be delayed.
309 * Make the release synchronous if this is a fuseblk mount,
310 * synchronous RELEASE is allowed (and desirable) in this case
311 * because the server can be trusted not to screw up.
313 fuse_file_put(ff
, ff
->fc
->destroy_req
!= NULL
);
316 static int fuse_open(struct inode
*inode
, struct file
*file
)
318 return fuse_open_common(inode
, file
, false);
321 static int fuse_release(struct inode
*inode
, struct file
*file
)
323 struct fuse_conn
*fc
= get_fuse_conn(inode
);
325 /* see fuse_vma_close() for !writeback_cache case */
326 if (fc
->writeback_cache
)
327 filemap_write_and_wait(file
->f_mapping
);
329 if (test_bit(FUSE_I_MTIME_DIRTY
, &get_fuse_inode(inode
)->state
))
330 fuse_flush_mtime(file
, true);
332 fuse_release_common(file
, FUSE_RELEASE
);
334 /* return value is ignored by VFS */
338 void fuse_sync_release(struct fuse_file
*ff
, int flags
)
340 WARN_ON(atomic_read(&ff
->count
) > 1);
341 fuse_prepare_release(ff
, flags
, FUSE_RELEASE
);
342 ff
->reserved_req
->force
= 1;
343 ff
->reserved_req
->background
= 0;
344 fuse_request_send(ff
->fc
, ff
->reserved_req
);
345 fuse_put_request(ff
->fc
, ff
->reserved_req
);
348 EXPORT_SYMBOL_GPL(fuse_sync_release
);
351 * Scramble the ID space with XTEA, so that the value of the files_struct
352 * pointer is not exposed to userspace.
354 u64
fuse_lock_owner_id(struct fuse_conn
*fc
, fl_owner_t id
)
356 u32
*k
= fc
->scramble_key
;
357 u64 v
= (unsigned long) id
;
363 for (i
= 0; i
< 32; i
++) {
364 v0
+= ((v1
<< 4 ^ v1
>> 5) + v1
) ^ (sum
+ k
[sum
& 3]);
366 v1
+= ((v0
<< 4 ^ v0
>> 5) + v0
) ^ (sum
+ k
[sum
>>11 & 3]);
369 return (u64
) v0
+ ((u64
) v1
<< 32);
373 * Check if any page in a range is under writeback
375 * This is currently done by walking the list of writepage requests
376 * for the inode, which can be pretty inefficient.
378 static bool fuse_range_is_writeback(struct inode
*inode
, pgoff_t idx_from
,
381 struct fuse_conn
*fc
= get_fuse_conn(inode
);
382 struct fuse_inode
*fi
= get_fuse_inode(inode
);
383 struct fuse_req
*req
;
386 spin_lock(&fc
->lock
);
387 list_for_each_entry(req
, &fi
->writepages
, writepages_entry
) {
390 BUG_ON(req
->inode
!= inode
);
391 curr_index
= req
->misc
.write
.in
.offset
>> PAGE_CACHE_SHIFT
;
392 if (idx_from
< curr_index
+ req
->num_pages
&&
393 curr_index
<= idx_to
) {
398 spin_unlock(&fc
->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 int 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
));
423 * Wait for all pending writepages on the inode to finish.
425 * This is currently done by blocking further writes with FUSE_NOWRITE
426 * and waiting for all sent writes to complete.
428 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
429 * could conflict with truncation.
431 static void fuse_sync_writes(struct inode
*inode
)
433 fuse_set_nowrite(inode
);
434 fuse_release_nowrite(inode
);
437 static int fuse_flush(struct file
*file
, fl_owner_t id
)
439 struct inode
*inode
= file_inode(file
);
440 struct fuse_conn
*fc
= get_fuse_conn(inode
);
441 struct fuse_file
*ff
= file
->private_data
;
442 struct fuse_req
*req
;
443 struct fuse_flush_in inarg
;
446 if (is_bad_inode(inode
))
452 err
= filemap_write_and_wait(file
->f_mapping
);
456 mutex_lock(&inode
->i_mutex
);
457 fuse_sync_writes(inode
);
458 mutex_unlock(&inode
->i_mutex
);
460 req
= fuse_get_req_nofail_nopages(fc
, file
);
461 memset(&inarg
, 0, sizeof(inarg
));
463 inarg
.lock_owner
= fuse_lock_owner_id(fc
, id
);
464 req
->in
.h
.opcode
= FUSE_FLUSH
;
465 req
->in
.h
.nodeid
= get_node_id(inode
);
467 req
->in
.args
[0].size
= sizeof(inarg
);
468 req
->in
.args
[0].value
= &inarg
;
470 fuse_request_send(fc
, req
);
471 err
= req
->out
.h
.error
;
472 fuse_put_request(fc
, req
);
473 if (err
== -ENOSYS
) {
480 int fuse_fsync_common(struct file
*file
, loff_t start
, loff_t end
,
481 int datasync
, int isdir
)
483 struct inode
*inode
= file
->f_mapping
->host
;
484 struct fuse_conn
*fc
= get_fuse_conn(inode
);
485 struct fuse_file
*ff
= file
->private_data
;
486 struct fuse_req
*req
;
487 struct fuse_fsync_in inarg
;
490 if (is_bad_inode(inode
))
493 err
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
497 if ((!isdir
&& fc
->no_fsync
) || (isdir
&& fc
->no_fsyncdir
))
500 mutex_lock(&inode
->i_mutex
);
503 * Start writeback against all dirty pages of the inode, then
504 * wait for all outstanding writes, before sending the FSYNC
507 err
= write_inode_now(inode
, 0);
511 fuse_sync_writes(inode
);
513 if (test_bit(FUSE_I_MTIME_DIRTY
, &get_fuse_inode(inode
)->state
)) {
514 err
= fuse_flush_mtime(file
, false);
519 req
= fuse_get_req_nopages(fc
);
525 memset(&inarg
, 0, sizeof(inarg
));
527 inarg
.fsync_flags
= datasync
? 1 : 0;
528 req
->in
.h
.opcode
= isdir
? FUSE_FSYNCDIR
: FUSE_FSYNC
;
529 req
->in
.h
.nodeid
= get_node_id(inode
);
531 req
->in
.args
[0].size
= sizeof(inarg
);
532 req
->in
.args
[0].value
= &inarg
;
533 fuse_request_send(fc
, req
);
534 err
= req
->out
.h
.error
;
535 fuse_put_request(fc
, req
);
536 if (err
== -ENOSYS
) {
544 mutex_unlock(&inode
->i_mutex
);
548 static int fuse_fsync(struct file
*file
, loff_t start
, loff_t end
,
551 return fuse_fsync_common(file
, start
, end
, datasync
, 0);
554 void fuse_read_fill(struct fuse_req
*req
, struct file
*file
, loff_t pos
,
555 size_t count
, int opcode
)
557 struct fuse_read_in
*inarg
= &req
->misc
.read
.in
;
558 struct fuse_file
*ff
= file
->private_data
;
563 inarg
->flags
= file
->f_flags
;
564 req
->in
.h
.opcode
= opcode
;
565 req
->in
.h
.nodeid
= ff
->nodeid
;
567 req
->in
.args
[0].size
= sizeof(struct fuse_read_in
);
568 req
->in
.args
[0].value
= inarg
;
570 req
->out
.numargs
= 1;
571 req
->out
.args
[0].size
= count
;
574 static void fuse_release_user_pages(struct fuse_req
*req
, int write
)
578 for (i
= 0; i
< req
->num_pages
; i
++) {
579 struct page
*page
= req
->pages
[i
];
581 set_page_dirty_lock(page
);
587 * In case of short read, the caller sets 'pos' to the position of
588 * actual end of fuse request in IO request. Otherwise, if bytes_requested
589 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
592 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
593 * both submitted asynchronously. The first of them was ACKed by userspace as
594 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
595 * second request was ACKed as short, e.g. only 1K was read, resulting in
598 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
599 * will be equal to the length of the longest contiguous fragment of
600 * transferred data starting from the beginning of IO request.
602 static void fuse_aio_complete(struct fuse_io_priv
*io
, int err
, ssize_t pos
)
606 spin_lock(&io
->lock
);
608 io
->err
= io
->err
? : err
;
609 else if (pos
>= 0 && (io
->bytes
< 0 || pos
< io
->bytes
))
613 spin_unlock(&io
->lock
);
620 else if (io
->bytes
>= 0 && io
->write
)
623 res
= io
->bytes
< 0 ? io
->size
: io
->bytes
;
625 if (!is_sync_kiocb(io
->iocb
)) {
626 struct inode
*inode
= file_inode(io
->iocb
->ki_filp
);
627 struct fuse_conn
*fc
= get_fuse_conn(inode
);
628 struct fuse_inode
*fi
= get_fuse_inode(inode
);
630 spin_lock(&fc
->lock
);
631 fi
->attr_version
= ++fc
->attr_version
;
632 spin_unlock(&fc
->lock
);
636 aio_complete(io
->iocb
, res
, 0);
641 static void fuse_aio_complete_req(struct fuse_conn
*fc
, struct fuse_req
*req
)
643 struct fuse_io_priv
*io
= req
->io
;
646 fuse_release_user_pages(req
, !io
->write
);
649 if (req
->misc
.write
.in
.size
!= req
->misc
.write
.out
.size
)
650 pos
= req
->misc
.write
.in
.offset
- io
->offset
+
651 req
->misc
.write
.out
.size
;
653 if (req
->misc
.read
.in
.size
!= req
->out
.args
[0].size
)
654 pos
= req
->misc
.read
.in
.offset
- io
->offset
+
655 req
->out
.args
[0].size
;
658 fuse_aio_complete(io
, req
->out
.h
.error
, pos
);
661 static size_t fuse_async_req_send(struct fuse_conn
*fc
, struct fuse_req
*req
,
662 size_t num_bytes
, struct fuse_io_priv
*io
)
664 spin_lock(&io
->lock
);
665 io
->size
+= num_bytes
;
667 spin_unlock(&io
->lock
);
670 req
->end
= fuse_aio_complete_req
;
672 __fuse_get_request(req
);
673 fuse_request_send_background(fc
, req
);
678 static size_t fuse_send_read(struct fuse_req
*req
, struct fuse_io_priv
*io
,
679 loff_t pos
, size_t count
, fl_owner_t owner
)
681 struct file
*file
= io
->file
;
682 struct fuse_file
*ff
= file
->private_data
;
683 struct fuse_conn
*fc
= ff
->fc
;
685 fuse_read_fill(req
, file
, pos
, count
, FUSE_READ
);
687 struct fuse_read_in
*inarg
= &req
->misc
.read
.in
;
689 inarg
->read_flags
|= FUSE_READ_LOCKOWNER
;
690 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
694 return fuse_async_req_send(fc
, req
, count
, io
);
696 fuse_request_send(fc
, req
);
697 return req
->out
.args
[0].size
;
700 static void fuse_read_update_size(struct inode
*inode
, loff_t size
,
703 struct fuse_conn
*fc
= get_fuse_conn(inode
);
704 struct fuse_inode
*fi
= get_fuse_inode(inode
);
706 spin_lock(&fc
->lock
);
707 if (attr_ver
== fi
->attr_version
&& size
< inode
->i_size
&&
708 !test_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
)) {
709 fi
->attr_version
= ++fc
->attr_version
;
710 i_size_write(inode
, size
);
712 spin_unlock(&fc
->lock
);
715 static void fuse_short_read(struct fuse_req
*req
, struct inode
*inode
,
718 size_t num_read
= req
->out
.args
[0].size
;
719 struct fuse_conn
*fc
= get_fuse_conn(inode
);
721 if (fc
->writeback_cache
) {
723 * A hole in a file. Some data after the hole are in page cache,
724 * but have not reached the client fs yet. So, the hole is not
728 int start_idx
= num_read
>> PAGE_CACHE_SHIFT
;
729 size_t off
= num_read
& (PAGE_CACHE_SIZE
- 1);
731 for (i
= start_idx
; i
< req
->num_pages
; i
++) {
732 zero_user_segment(req
->pages
[i
], off
, PAGE_CACHE_SIZE
);
736 loff_t pos
= page_offset(req
->pages
[0]) + num_read
;
737 fuse_read_update_size(inode
, pos
, attr_ver
);
741 static int fuse_do_readpage(struct file
*file
, struct page
*page
)
743 struct fuse_io_priv io
= { .async
= 0, .file
= file
};
744 struct inode
*inode
= page
->mapping
->host
;
745 struct fuse_conn
*fc
= get_fuse_conn(inode
);
746 struct fuse_req
*req
;
748 loff_t pos
= page_offset(page
);
749 size_t count
= PAGE_CACHE_SIZE
;
754 * Page writeback can extend beyond the lifetime of the
755 * page-cache page, so make sure we read a properly synced
758 fuse_wait_on_page_writeback(inode
, page
->index
);
760 req
= fuse_get_req(fc
, 1);
764 attr_ver
= fuse_get_attr_version(fc
);
766 req
->out
.page_zeroing
= 1;
767 req
->out
.argpages
= 1;
769 req
->pages
[0] = page
;
770 req
->page_descs
[0].length
= count
;
771 num_read
= fuse_send_read(req
, &io
, pos
, count
, NULL
);
772 err
= req
->out
.h
.error
;
776 * Short read means EOF. If file size is larger, truncate it
778 if (num_read
< count
)
779 fuse_short_read(req
, inode
, attr_ver
);
781 SetPageUptodate(page
);
784 fuse_put_request(fc
, req
);
789 static int fuse_readpage(struct file
*file
, struct page
*page
)
791 struct inode
*inode
= page
->mapping
->host
;
795 if (is_bad_inode(inode
))
798 err
= fuse_do_readpage(file
, page
);
799 fuse_invalidate_atime(inode
);
805 static void fuse_readpages_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
808 size_t count
= req
->misc
.read
.in
.size
;
809 size_t num_read
= req
->out
.args
[0].size
;
810 struct address_space
*mapping
= NULL
;
812 for (i
= 0; mapping
== NULL
&& i
< req
->num_pages
; i
++)
813 mapping
= req
->pages
[i
]->mapping
;
816 struct inode
*inode
= mapping
->host
;
819 * Short read means EOF. If file size is larger, truncate it
821 if (!req
->out
.h
.error
&& num_read
< count
)
822 fuse_short_read(req
, inode
, req
->misc
.read
.attr_ver
);
824 fuse_invalidate_atime(inode
);
827 for (i
= 0; i
< req
->num_pages
; i
++) {
828 struct page
*page
= req
->pages
[i
];
829 if (!req
->out
.h
.error
)
830 SetPageUptodate(page
);
834 page_cache_release(page
);
837 fuse_file_put(req
->ff
, false);
840 static void fuse_send_readpages(struct fuse_req
*req
, struct file
*file
)
842 struct fuse_file
*ff
= file
->private_data
;
843 struct fuse_conn
*fc
= ff
->fc
;
844 loff_t pos
= page_offset(req
->pages
[0]);
845 size_t count
= req
->num_pages
<< PAGE_CACHE_SHIFT
;
847 req
->out
.argpages
= 1;
848 req
->out
.page_zeroing
= 1;
849 req
->out
.page_replace
= 1;
850 fuse_read_fill(req
, file
, pos
, count
, FUSE_READ
);
851 req
->misc
.read
.attr_ver
= fuse_get_attr_version(fc
);
852 if (fc
->async_read
) {
853 req
->ff
= fuse_file_get(ff
);
854 req
->end
= fuse_readpages_end
;
855 fuse_request_send_background(fc
, req
);
857 fuse_request_send(fc
, req
);
858 fuse_readpages_end(fc
, req
);
859 fuse_put_request(fc
, req
);
863 struct fuse_fill_data
{
864 struct fuse_req
*req
;
870 static int fuse_readpages_fill(void *_data
, struct page
*page
)
872 struct fuse_fill_data
*data
= _data
;
873 struct fuse_req
*req
= data
->req
;
874 struct inode
*inode
= data
->inode
;
875 struct fuse_conn
*fc
= get_fuse_conn(inode
);
877 fuse_wait_on_page_writeback(inode
, page
->index
);
879 if (req
->num_pages
&&
880 (req
->num_pages
== FUSE_MAX_PAGES_PER_REQ
||
881 (req
->num_pages
+ 1) * PAGE_CACHE_SIZE
> fc
->max_read
||
882 req
->pages
[req
->num_pages
- 1]->index
+ 1 != page
->index
)) {
883 int nr_alloc
= min_t(unsigned, data
->nr_pages
,
884 FUSE_MAX_PAGES_PER_REQ
);
885 fuse_send_readpages(req
, data
->file
);
887 req
= fuse_get_req_for_background(fc
, nr_alloc
);
889 req
= fuse_get_req(fc
, nr_alloc
);
898 if (WARN_ON(req
->num_pages
>= req
->max_pages
)) {
899 fuse_put_request(fc
, req
);
903 page_cache_get(page
);
904 req
->pages
[req
->num_pages
] = page
;
905 req
->page_descs
[req
->num_pages
].length
= PAGE_SIZE
;
911 static int fuse_readpages(struct file
*file
, struct address_space
*mapping
,
912 struct list_head
*pages
, unsigned nr_pages
)
914 struct inode
*inode
= mapping
->host
;
915 struct fuse_conn
*fc
= get_fuse_conn(inode
);
916 struct fuse_fill_data data
;
918 int nr_alloc
= min_t(unsigned, nr_pages
, FUSE_MAX_PAGES_PER_REQ
);
921 if (is_bad_inode(inode
))
927 data
.req
= fuse_get_req_for_background(fc
, nr_alloc
);
929 data
.req
= fuse_get_req(fc
, nr_alloc
);
930 data
.nr_pages
= nr_pages
;
931 err
= PTR_ERR(data
.req
);
932 if (IS_ERR(data
.req
))
935 err
= read_cache_pages(mapping
, pages
, fuse_readpages_fill
, &data
);
937 if (data
.req
->num_pages
)
938 fuse_send_readpages(data
.req
, file
);
940 fuse_put_request(fc
, data
.req
);
946 static ssize_t
fuse_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
947 unsigned long nr_segs
, loff_t pos
)
949 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
950 struct fuse_conn
*fc
= get_fuse_conn(inode
);
953 * In auto invalidate mode, always update attributes on read.
954 * Otherwise, only update if we attempt to read past EOF (to ensure
955 * i_size is up to date).
957 if (fc
->auto_inval_data
||
958 (pos
+ iov_length(iov
, nr_segs
) > i_size_read(inode
))) {
960 err
= fuse_update_attributes(inode
, NULL
, iocb
->ki_filp
, NULL
);
965 return generic_file_aio_read(iocb
, iov
, nr_segs
, pos
);
968 static void fuse_write_fill(struct fuse_req
*req
, struct fuse_file
*ff
,
969 loff_t pos
, size_t count
)
971 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
972 struct fuse_write_out
*outarg
= &req
->misc
.write
.out
;
977 req
->in
.h
.opcode
= FUSE_WRITE
;
978 req
->in
.h
.nodeid
= ff
->nodeid
;
980 if (ff
->fc
->minor
< 9)
981 req
->in
.args
[0].size
= FUSE_COMPAT_WRITE_IN_SIZE
;
983 req
->in
.args
[0].size
= sizeof(struct fuse_write_in
);
984 req
->in
.args
[0].value
= inarg
;
985 req
->in
.args
[1].size
= count
;
986 req
->out
.numargs
= 1;
987 req
->out
.args
[0].size
= sizeof(struct fuse_write_out
);
988 req
->out
.args
[0].value
= outarg
;
991 static size_t fuse_send_write(struct fuse_req
*req
, struct fuse_io_priv
*io
,
992 loff_t pos
, size_t count
, fl_owner_t owner
)
994 struct file
*file
= io
->file
;
995 struct fuse_file
*ff
= file
->private_data
;
996 struct fuse_conn
*fc
= ff
->fc
;
997 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
999 fuse_write_fill(req
, ff
, pos
, count
);
1000 inarg
->flags
= file
->f_flags
;
1001 if (owner
!= NULL
) {
1002 inarg
->write_flags
|= FUSE_WRITE_LOCKOWNER
;
1003 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
1007 return fuse_async_req_send(fc
, req
, count
, io
);
1009 fuse_request_send(fc
, req
);
1010 return req
->misc
.write
.out
.size
;
1013 bool fuse_write_update_size(struct inode
*inode
, loff_t pos
)
1015 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1016 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1019 spin_lock(&fc
->lock
);
1020 fi
->attr_version
= ++fc
->attr_version
;
1021 if (pos
> inode
->i_size
) {
1022 i_size_write(inode
, pos
);
1025 spin_unlock(&fc
->lock
);
1030 static size_t fuse_send_write_pages(struct fuse_req
*req
, struct file
*file
,
1031 struct inode
*inode
, loff_t pos
,
1037 struct fuse_io_priv io
= { .async
= 0, .file
= file
};
1039 for (i
= 0; i
< req
->num_pages
; i
++)
1040 fuse_wait_on_page_writeback(inode
, req
->pages
[i
]->index
);
1042 res
= fuse_send_write(req
, &io
, pos
, count
, NULL
);
1044 offset
= req
->page_descs
[0].offset
;
1046 for (i
= 0; i
< req
->num_pages
; i
++) {
1047 struct page
*page
= req
->pages
[i
];
1049 if (!req
->out
.h
.error
&& !offset
&& count
>= PAGE_CACHE_SIZE
)
1050 SetPageUptodate(page
);
1052 if (count
> PAGE_CACHE_SIZE
- offset
)
1053 count
-= PAGE_CACHE_SIZE
- offset
;
1059 page_cache_release(page
);
1065 static ssize_t
fuse_fill_write_pages(struct fuse_req
*req
,
1066 struct address_space
*mapping
,
1067 struct iov_iter
*ii
, loff_t pos
)
1069 struct fuse_conn
*fc
= get_fuse_conn(mapping
->host
);
1070 unsigned offset
= pos
& (PAGE_CACHE_SIZE
- 1);
1074 req
->in
.argpages
= 1;
1075 req
->page_descs
[0].offset
= offset
;
1080 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
1081 size_t bytes
= min_t(size_t, PAGE_CACHE_SIZE
- offset
,
1082 iov_iter_count(ii
));
1084 bytes
= min_t(size_t, bytes
, fc
->max_write
- count
);
1088 if (iov_iter_fault_in_readable(ii
, bytes
))
1092 page
= grab_cache_page_write_begin(mapping
, index
, 0);
1096 if (mapping_writably_mapped(mapping
))
1097 flush_dcache_page(page
);
1099 tmp
= iov_iter_copy_from_user_atomic(page
, ii
, offset
, bytes
);
1100 flush_dcache_page(page
);
1102 mark_page_accessed(page
);
1106 page_cache_release(page
);
1107 bytes
= min(bytes
, iov_iter_single_seg_count(ii
));
1112 req
->pages
[req
->num_pages
] = page
;
1113 req
->page_descs
[req
->num_pages
].length
= tmp
;
1116 iov_iter_advance(ii
, tmp
);
1120 if (offset
== PAGE_CACHE_SIZE
)
1123 if (!fc
->big_writes
)
1125 } while (iov_iter_count(ii
) && count
< fc
->max_write
&&
1126 req
->num_pages
< req
->max_pages
&& offset
== 0);
1128 return count
> 0 ? count
: err
;
1131 static inline unsigned fuse_wr_pages(loff_t pos
, size_t len
)
1133 return min_t(unsigned,
1134 ((pos
+ len
- 1) >> PAGE_CACHE_SHIFT
) -
1135 (pos
>> PAGE_CACHE_SHIFT
) + 1,
1136 FUSE_MAX_PAGES_PER_REQ
);
1139 static ssize_t
fuse_perform_write(struct file
*file
,
1140 struct address_space
*mapping
,
1141 struct iov_iter
*ii
, loff_t pos
)
1143 struct inode
*inode
= mapping
->host
;
1144 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1145 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1149 if (is_bad_inode(inode
))
1152 if (inode
->i_size
< pos
+ iov_iter_count(ii
))
1153 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1156 struct fuse_req
*req
;
1158 unsigned nr_pages
= fuse_wr_pages(pos
, iov_iter_count(ii
));
1160 req
= fuse_get_req(fc
, nr_pages
);
1166 count
= fuse_fill_write_pages(req
, mapping
, ii
, pos
);
1172 num_written
= fuse_send_write_pages(req
, file
, inode
,
1174 err
= req
->out
.h
.error
;
1179 /* break out of the loop on short write */
1180 if (num_written
!= count
)
1184 fuse_put_request(fc
, req
);
1185 } while (!err
&& iov_iter_count(ii
));
1188 fuse_write_update_size(inode
, pos
);
1190 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1191 fuse_invalidate_attr(inode
);
1193 return res
> 0 ? res
: err
;
1196 static ssize_t
fuse_file_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
1197 unsigned long nr_segs
, loff_t pos
)
1199 struct file
*file
= iocb
->ki_filp
;
1200 struct address_space
*mapping
= file
->f_mapping
;
1203 ssize_t written
= 0;
1204 ssize_t written_buffered
= 0;
1205 struct inode
*inode
= mapping
->host
;
1210 if (get_fuse_conn(inode
)->writeback_cache
) {
1211 /* Update size (EOF optimization) and mode (SUID clearing) */
1212 err
= fuse_update_attributes(mapping
->host
, NULL
, file
, NULL
);
1216 return generic_file_aio_write(iocb
, iov
, nr_segs
, pos
);
1219 WARN_ON(iocb
->ki_pos
!= pos
);
1222 err
= generic_segment_checks(iov
, &nr_segs
, &ocount
, VERIFY_READ
);
1227 mutex_lock(&inode
->i_mutex
);
1229 /* We can write back this queue in page reclaim */
1230 current
->backing_dev_info
= mapping
->backing_dev_info
;
1232 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
1239 err
= file_remove_suid(file
);
1243 err
= file_update_time(file
);
1247 if (file
->f_flags
& O_DIRECT
) {
1248 written
= generic_file_direct_write(iocb
, iov
, &nr_segs
, pos
,
1250 if (written
< 0 || written
== count
)
1256 iov_iter_init(&i
, iov
, nr_segs
, count
, written
);
1257 written_buffered
= fuse_perform_write(file
, mapping
, &i
, pos
);
1258 if (written_buffered
< 0) {
1259 err
= written_buffered
;
1262 endbyte
= pos
+ written_buffered
- 1;
1264 err
= filemap_write_and_wait_range(file
->f_mapping
, pos
,
1269 invalidate_mapping_pages(file
->f_mapping
,
1270 pos
>> PAGE_CACHE_SHIFT
,
1271 endbyte
>> PAGE_CACHE_SHIFT
);
1273 written
+= written_buffered
;
1274 iocb
->ki_pos
= pos
+ written_buffered
;
1276 iov_iter_init(&i
, iov
, nr_segs
, count
, 0);
1277 written
= fuse_perform_write(file
, mapping
, &i
, pos
);
1279 iocb
->ki_pos
= pos
+ written
;
1282 current
->backing_dev_info
= NULL
;
1283 mutex_unlock(&inode
->i_mutex
);
1285 return written
? written
: err
;
1288 static inline void fuse_page_descs_length_init(struct fuse_req
*req
,
1289 unsigned index
, unsigned nr_pages
)
1293 for (i
= index
; i
< index
+ nr_pages
; i
++)
1294 req
->page_descs
[i
].length
= PAGE_SIZE
-
1295 req
->page_descs
[i
].offset
;
1298 static inline unsigned long fuse_get_user_addr(const struct iov_iter
*ii
)
1300 return (unsigned long)ii
->iov
->iov_base
+ ii
->iov_offset
;
1303 static inline size_t fuse_get_frag_size(const struct iov_iter
*ii
,
1306 return min(iov_iter_single_seg_count(ii
), max_size
);
1309 static int fuse_get_user_pages(struct fuse_req
*req
, struct iov_iter
*ii
,
1310 size_t *nbytesp
, int write
)
1312 size_t nbytes
= 0; /* # bytes already packed in req */
1314 /* Special case for kernel I/O: can copy directly into the buffer */
1315 if (segment_eq(get_fs(), KERNEL_DS
)) {
1316 unsigned long user_addr
= fuse_get_user_addr(ii
);
1317 size_t frag_size
= fuse_get_frag_size(ii
, *nbytesp
);
1320 req
->in
.args
[1].value
= (void *) user_addr
;
1322 req
->out
.args
[0].value
= (void *) user_addr
;
1324 iov_iter_advance(ii
, frag_size
);
1325 *nbytesp
= frag_size
;
1329 while (nbytes
< *nbytesp
&& req
->num_pages
< req
->max_pages
) {
1331 unsigned long user_addr
= fuse_get_user_addr(ii
);
1332 unsigned offset
= user_addr
& ~PAGE_MASK
;
1333 size_t frag_size
= fuse_get_frag_size(ii
, *nbytesp
- nbytes
);
1336 unsigned n
= req
->max_pages
- req
->num_pages
;
1337 frag_size
= min_t(size_t, frag_size
, n
<< PAGE_SHIFT
);
1339 npages
= (frag_size
+ offset
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1340 npages
= clamp(npages
, 1U, n
);
1342 ret
= get_user_pages_fast(user_addr
, npages
, !write
,
1343 &req
->pages
[req
->num_pages
]);
1348 frag_size
= min_t(size_t, frag_size
,
1349 (npages
<< PAGE_SHIFT
) - offset
);
1350 iov_iter_advance(ii
, frag_size
);
1352 req
->page_descs
[req
->num_pages
].offset
= offset
;
1353 fuse_page_descs_length_init(req
, req
->num_pages
, npages
);
1355 req
->num_pages
+= npages
;
1356 req
->page_descs
[req
->num_pages
- 1].length
-=
1357 (npages
<< PAGE_SHIFT
) - offset
- frag_size
;
1359 nbytes
+= frag_size
;
1363 req
->in
.argpages
= 1;
1365 req
->out
.argpages
= 1;
1372 static inline int fuse_iter_npages(const struct iov_iter
*ii_p
)
1374 struct iov_iter ii
= *ii_p
;
1377 while (iov_iter_count(&ii
) && npages
< FUSE_MAX_PAGES_PER_REQ
) {
1378 unsigned long user_addr
= fuse_get_user_addr(&ii
);
1379 unsigned offset
= user_addr
& ~PAGE_MASK
;
1380 size_t frag_size
= iov_iter_single_seg_count(&ii
);
1382 npages
+= (frag_size
+ offset
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1383 iov_iter_advance(&ii
, frag_size
);
1386 return min(npages
, FUSE_MAX_PAGES_PER_REQ
);
1389 ssize_t
fuse_direct_io(struct fuse_io_priv
*io
, const struct iovec
*iov
,
1390 unsigned long nr_segs
, size_t count
, loff_t
*ppos
,
1393 int write
= flags
& FUSE_DIO_WRITE
;
1394 int cuse
= flags
& FUSE_DIO_CUSE
;
1395 struct file
*file
= io
->file
;
1396 struct inode
*inode
= file
->f_mapping
->host
;
1397 struct fuse_file
*ff
= file
->private_data
;
1398 struct fuse_conn
*fc
= ff
->fc
;
1399 size_t nmax
= write
? fc
->max_write
: fc
->max_read
;
1401 pgoff_t idx_from
= pos
>> PAGE_CACHE_SHIFT
;
1402 pgoff_t idx_to
= (pos
+ count
- 1) >> PAGE_CACHE_SHIFT
;
1404 struct fuse_req
*req
;
1407 iov_iter_init(&ii
, iov
, nr_segs
, count
, 0);
1410 req
= fuse_get_req_for_background(fc
, fuse_iter_npages(&ii
));
1412 req
= fuse_get_req(fc
, fuse_iter_npages(&ii
));
1414 return PTR_ERR(req
);
1416 if (!cuse
&& fuse_range_is_writeback(inode
, idx_from
, idx_to
)) {
1418 mutex_lock(&inode
->i_mutex
);
1419 fuse_sync_writes(inode
);
1421 mutex_unlock(&inode
->i_mutex
);
1426 fl_owner_t owner
= current
->files
;
1427 size_t nbytes
= min(count
, nmax
);
1428 int err
= fuse_get_user_pages(req
, &ii
, &nbytes
, write
);
1435 nres
= fuse_send_write(req
, io
, pos
, nbytes
, owner
);
1437 nres
= fuse_send_read(req
, io
, pos
, nbytes
, owner
);
1440 fuse_release_user_pages(req
, !write
);
1441 if (req
->out
.h
.error
) {
1443 res
= req
->out
.h
.error
;
1445 } else if (nres
> nbytes
) {
1455 fuse_put_request(fc
, req
);
1457 req
= fuse_get_req_for_background(fc
,
1458 fuse_iter_npages(&ii
));
1460 req
= fuse_get_req(fc
, fuse_iter_npages(&ii
));
1466 fuse_put_request(fc
, req
);
1472 EXPORT_SYMBOL_GPL(fuse_direct_io
);
1474 static ssize_t
__fuse_direct_read(struct fuse_io_priv
*io
,
1475 const struct iovec
*iov
,
1476 unsigned long nr_segs
, loff_t
*ppos
,
1480 struct file
*file
= io
->file
;
1481 struct inode
*inode
= file_inode(file
);
1483 if (is_bad_inode(inode
))
1486 res
= fuse_direct_io(io
, iov
, nr_segs
, count
, ppos
, 0);
1488 fuse_invalidate_attr(inode
);
1493 static ssize_t
fuse_direct_read(struct file
*file
, char __user
*buf
,
1494 size_t count
, loff_t
*ppos
)
1496 struct fuse_io_priv io
= { .async
= 0, .file
= file
};
1497 struct iovec iov
= { .iov_base
= buf
, .iov_len
= count
};
1498 return __fuse_direct_read(&io
, &iov
, 1, ppos
, count
);
1501 static ssize_t
__fuse_direct_write(struct fuse_io_priv
*io
,
1502 const struct iovec
*iov
,
1503 unsigned long nr_segs
, loff_t
*ppos
)
1505 struct file
*file
= io
->file
;
1506 struct inode
*inode
= file_inode(file
);
1507 size_t count
= iov_length(iov
, nr_segs
);
1510 res
= generic_write_checks(file
, ppos
, &count
, 0);
1512 res
= fuse_direct_io(io
, iov
, nr_segs
, count
, ppos
,
1515 fuse_invalidate_attr(inode
);
1520 static ssize_t
fuse_direct_write(struct file
*file
, const char __user
*buf
,
1521 size_t count
, loff_t
*ppos
)
1523 struct iovec iov
= { .iov_base
= (void __user
*)buf
, .iov_len
= count
};
1524 struct inode
*inode
= file_inode(file
);
1526 struct fuse_io_priv io
= { .async
= 0, .file
= file
};
1528 if (is_bad_inode(inode
))
1531 /* Don't allow parallel writes to the same file */
1532 mutex_lock(&inode
->i_mutex
);
1533 res
= __fuse_direct_write(&io
, &iov
, 1, ppos
);
1535 fuse_write_update_size(inode
, *ppos
);
1536 mutex_unlock(&inode
->i_mutex
);
1541 static void fuse_writepage_free(struct fuse_conn
*fc
, struct fuse_req
*req
)
1545 for (i
= 0; i
< req
->num_pages
; i
++)
1546 __free_page(req
->pages
[i
]);
1549 fuse_file_put(req
->ff
, false);
1552 static void fuse_writepage_finish(struct fuse_conn
*fc
, struct fuse_req
*req
)
1554 struct inode
*inode
= req
->inode
;
1555 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1556 struct backing_dev_info
*bdi
= inode
->i_mapping
->backing_dev_info
;
1559 list_del(&req
->writepages_entry
);
1560 for (i
= 0; i
< req
->num_pages
; i
++) {
1561 dec_bdi_stat(bdi
, BDI_WRITEBACK
);
1562 dec_zone_page_state(req
->pages
[i
], NR_WRITEBACK_TEMP
);
1563 bdi_writeout_inc(bdi
);
1565 wake_up(&fi
->page_waitq
);
1568 /* Called under fc->lock, may release and reacquire it */
1569 static void fuse_send_writepage(struct fuse_conn
*fc
, struct fuse_req
*req
,
1571 __releases(fc
->lock
)
1572 __acquires(fc
->lock
)
1574 struct fuse_inode
*fi
= get_fuse_inode(req
->inode
);
1575 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
1576 __u64 data_size
= req
->num_pages
* PAGE_CACHE_SIZE
;
1581 if (inarg
->offset
+ data_size
<= size
) {
1582 inarg
->size
= data_size
;
1583 } else if (inarg
->offset
< size
) {
1584 inarg
->size
= size
- inarg
->offset
;
1586 /* Got truncated off completely */
1590 req
->in
.args
[1].size
= inarg
->size
;
1592 fuse_request_send_background_locked(fc
, req
);
1596 fuse_writepage_finish(fc
, req
);
1597 spin_unlock(&fc
->lock
);
1598 fuse_writepage_free(fc
, req
);
1599 fuse_put_request(fc
, req
);
1600 spin_lock(&fc
->lock
);
1604 * If fi->writectr is positive (no truncate or fsync going on) send
1605 * all queued writepage requests.
1607 * Called with fc->lock
1609 void fuse_flush_writepages(struct inode
*inode
)
1610 __releases(fc
->lock
)
1611 __acquires(fc
->lock
)
1613 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1614 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1615 size_t crop
= i_size_read(inode
);
1616 struct fuse_req
*req
;
1618 while (fi
->writectr
>= 0 && !list_empty(&fi
->queued_writes
)) {
1619 req
= list_entry(fi
->queued_writes
.next
, struct fuse_req
, list
);
1620 list_del_init(&req
->list
);
1621 fuse_send_writepage(fc
, req
, crop
);
1625 static void fuse_writepage_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
1627 struct inode
*inode
= req
->inode
;
1628 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1630 mapping_set_error(inode
->i_mapping
, req
->out
.h
.error
);
1631 spin_lock(&fc
->lock
);
1632 while (req
->misc
.write
.next
) {
1633 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1634 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
1635 struct fuse_req
*next
= req
->misc
.write
.next
;
1636 req
->misc
.write
.next
= next
->misc
.write
.next
;
1637 next
->misc
.write
.next
= NULL
;
1638 next
->ff
= fuse_file_get(req
->ff
);
1639 list_add(&next
->writepages_entry
, &fi
->writepages
);
1642 * Skip fuse_flush_writepages() to make it easy to crop requests
1643 * based on primary request size.
1645 * 1st case (trivial): there are no concurrent activities using
1646 * fuse_set/release_nowrite. Then we're on safe side because
1647 * fuse_flush_writepages() would call fuse_send_writepage()
1650 * 2nd case: someone called fuse_set_nowrite and it is waiting
1651 * now for completion of all in-flight requests. This happens
1652 * rarely and no more than once per page, so this should be
1655 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1656 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1657 * that fuse_set_nowrite returned implies that all in-flight
1658 * requests were completed along with all of their secondary
1659 * requests. Further primary requests are blocked by negative
1660 * writectr. Hence there cannot be any in-flight requests and
1661 * no invocations of fuse_writepage_end() while we're in
1662 * fuse_set_nowrite..fuse_release_nowrite section.
1664 fuse_send_writepage(fc
, next
, inarg
->offset
+ inarg
->size
);
1667 fuse_writepage_finish(fc
, req
);
1668 spin_unlock(&fc
->lock
);
1669 fuse_writepage_free(fc
, req
);
1672 static struct fuse_file
*fuse_write_file_get(struct fuse_conn
*fc
,
1673 struct fuse_inode
*fi
)
1675 struct fuse_file
*ff
= NULL
;
1677 spin_lock(&fc
->lock
);
1678 if (!WARN_ON(list_empty(&fi
->write_files
))) {
1679 ff
= list_entry(fi
->write_files
.next
, struct fuse_file
,
1683 spin_unlock(&fc
->lock
);
1688 static int fuse_writepage_locked(struct page
*page
)
1690 struct address_space
*mapping
= page
->mapping
;
1691 struct inode
*inode
= mapping
->host
;
1692 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1693 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1694 struct fuse_req
*req
;
1695 struct page
*tmp_page
;
1696 int error
= -ENOMEM
;
1698 set_page_writeback(page
);
1700 req
= fuse_request_alloc_nofs(1);
1704 req
->background
= 1; /* writeback always goes to bg_queue */
1705 tmp_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
1710 req
->ff
= fuse_write_file_get(fc
, fi
);
1714 fuse_write_fill(req
, req
->ff
, page_offset(page
), 0);
1716 copy_highpage(tmp_page
, page
);
1717 req
->misc
.write
.in
.write_flags
|= FUSE_WRITE_CACHE
;
1718 req
->misc
.write
.next
= NULL
;
1719 req
->in
.argpages
= 1;
1721 req
->pages
[0] = tmp_page
;
1722 req
->page_descs
[0].offset
= 0;
1723 req
->page_descs
[0].length
= PAGE_SIZE
;
1724 req
->end
= fuse_writepage_end
;
1727 inc_bdi_stat(mapping
->backing_dev_info
, BDI_WRITEBACK
);
1728 inc_zone_page_state(tmp_page
, NR_WRITEBACK_TEMP
);
1730 spin_lock(&fc
->lock
);
1731 list_add(&req
->writepages_entry
, &fi
->writepages
);
1732 list_add_tail(&req
->list
, &fi
->queued_writes
);
1733 fuse_flush_writepages(inode
);
1734 spin_unlock(&fc
->lock
);
1736 end_page_writeback(page
);
1741 fuse_request_free(req
);
1743 end_page_writeback(page
);
1747 static int fuse_writepage(struct page
*page
, struct writeback_control
*wbc
)
1751 if (fuse_page_is_writeback(page
->mapping
->host
, page
->index
)) {
1753 * ->writepages() should be called for sync() and friends. We
1754 * should only get here on direct reclaim and then we are
1755 * allowed to skip a page which is already in flight
1757 WARN_ON(wbc
->sync_mode
== WB_SYNC_ALL
);
1759 redirty_page_for_writepage(wbc
, page
);
1763 err
= fuse_writepage_locked(page
);
1769 struct fuse_fill_wb_data
{
1770 struct fuse_req
*req
;
1771 struct fuse_file
*ff
;
1772 struct inode
*inode
;
1773 struct page
**orig_pages
;
1776 static void fuse_writepages_send(struct fuse_fill_wb_data
*data
)
1778 struct fuse_req
*req
= data
->req
;
1779 struct inode
*inode
= data
->inode
;
1780 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1781 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1782 int num_pages
= req
->num_pages
;
1785 req
->ff
= fuse_file_get(data
->ff
);
1786 spin_lock(&fc
->lock
);
1787 list_add_tail(&req
->list
, &fi
->queued_writes
);
1788 fuse_flush_writepages(inode
);
1789 spin_unlock(&fc
->lock
);
1791 for (i
= 0; i
< num_pages
; i
++)
1792 end_page_writeback(data
->orig_pages
[i
]);
1795 static bool fuse_writepage_in_flight(struct fuse_req
*new_req
,
1798 struct fuse_conn
*fc
= get_fuse_conn(new_req
->inode
);
1799 struct fuse_inode
*fi
= get_fuse_inode(new_req
->inode
);
1800 struct fuse_req
*tmp
;
1801 struct fuse_req
*old_req
;
1805 BUG_ON(new_req
->num_pages
!= 0);
1807 spin_lock(&fc
->lock
);
1808 list_del(&new_req
->writepages_entry
);
1809 list_for_each_entry(old_req
, &fi
->writepages
, writepages_entry
) {
1810 BUG_ON(old_req
->inode
!= new_req
->inode
);
1811 curr_index
= old_req
->misc
.write
.in
.offset
>> PAGE_CACHE_SHIFT
;
1812 if (curr_index
<= page
->index
&&
1813 page
->index
< curr_index
+ old_req
->num_pages
) {
1819 list_add(&new_req
->writepages_entry
, &fi
->writepages
);
1823 new_req
->num_pages
= 1;
1824 for (tmp
= old_req
; tmp
!= NULL
; tmp
= tmp
->misc
.write
.next
) {
1825 BUG_ON(tmp
->inode
!= new_req
->inode
);
1826 curr_index
= tmp
->misc
.write
.in
.offset
>> PAGE_CACHE_SHIFT
;
1827 if (tmp
->num_pages
== 1 &&
1828 curr_index
== page
->index
) {
1833 if (old_req
->num_pages
== 1 && (old_req
->state
== FUSE_REQ_INIT
||
1834 old_req
->state
== FUSE_REQ_PENDING
)) {
1835 struct backing_dev_info
*bdi
= page
->mapping
->backing_dev_info
;
1837 copy_highpage(old_req
->pages
[0], page
);
1838 spin_unlock(&fc
->lock
);
1840 dec_bdi_stat(bdi
, BDI_WRITEBACK
);
1841 dec_zone_page_state(page
, NR_WRITEBACK_TEMP
);
1842 bdi_writeout_inc(bdi
);
1843 fuse_writepage_free(fc
, new_req
);
1844 fuse_request_free(new_req
);
1847 new_req
->misc
.write
.next
= old_req
->misc
.write
.next
;
1848 old_req
->misc
.write
.next
= new_req
;
1851 spin_unlock(&fc
->lock
);
1856 static int fuse_writepages_fill(struct page
*page
,
1857 struct writeback_control
*wbc
, void *_data
)
1859 struct fuse_fill_wb_data
*data
= _data
;
1860 struct fuse_req
*req
= data
->req
;
1861 struct inode
*inode
= data
->inode
;
1862 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1863 struct page
*tmp_page
;
1869 data
->ff
= fuse_write_file_get(fc
, get_fuse_inode(inode
));
1875 * Being under writeback is unlikely but possible. For example direct
1876 * read to an mmaped fuse file will set the page dirty twice; once when
1877 * the pages are faulted with get_user_pages(), and then after the read
1880 is_writeback
= fuse_page_is_writeback(inode
, page
->index
);
1882 if (req
&& req
->num_pages
&&
1883 (is_writeback
|| req
->num_pages
== FUSE_MAX_PAGES_PER_REQ
||
1884 (req
->num_pages
+ 1) * PAGE_CACHE_SIZE
> fc
->max_write
||
1885 data
->orig_pages
[req
->num_pages
- 1]->index
+ 1 != page
->index
)) {
1886 fuse_writepages_send(data
);
1890 tmp_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
1895 * The page must not be redirtied until the writeout is completed
1896 * (i.e. userspace has sent a reply to the write request). Otherwise
1897 * there could be more than one temporary page instance for each real
1900 * This is ensured by holding the page lock in page_mkwrite() while
1901 * checking fuse_page_is_writeback(). We already hold the page lock
1902 * since clear_page_dirty_for_io() and keep it held until we add the
1903 * request to the fi->writepages list and increment req->num_pages.
1904 * After this fuse_page_is_writeback() will indicate that the page is
1905 * under writeback, so we can release the page lock.
1907 if (data
->req
== NULL
) {
1908 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1911 req
= fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ
);
1913 __free_page(tmp_page
);
1917 fuse_write_fill(req
, data
->ff
, page_offset(page
), 0);
1918 req
->misc
.write
.in
.write_flags
|= FUSE_WRITE_CACHE
;
1919 req
->misc
.write
.next
= NULL
;
1920 req
->in
.argpages
= 1;
1921 req
->background
= 1;
1923 req
->end
= fuse_writepage_end
;
1926 spin_lock(&fc
->lock
);
1927 list_add(&req
->writepages_entry
, &fi
->writepages
);
1928 spin_unlock(&fc
->lock
);
1932 set_page_writeback(page
);
1934 copy_highpage(tmp_page
, page
);
1935 req
->pages
[req
->num_pages
] = tmp_page
;
1936 req
->page_descs
[req
->num_pages
].offset
= 0;
1937 req
->page_descs
[req
->num_pages
].length
= PAGE_SIZE
;
1939 inc_bdi_stat(page
->mapping
->backing_dev_info
, BDI_WRITEBACK
);
1940 inc_zone_page_state(tmp_page
, NR_WRITEBACK_TEMP
);
1943 if (is_writeback
&& fuse_writepage_in_flight(req
, page
)) {
1944 end_page_writeback(page
);
1948 data
->orig_pages
[req
->num_pages
] = page
;
1951 * Protected by fc->lock against concurrent access by
1952 * fuse_page_is_writeback().
1954 spin_lock(&fc
->lock
);
1956 spin_unlock(&fc
->lock
);
1964 static int fuse_writepages(struct address_space
*mapping
,
1965 struct writeback_control
*wbc
)
1967 struct inode
*inode
= mapping
->host
;
1968 struct fuse_fill_wb_data data
;
1972 if (is_bad_inode(inode
))
1980 data
.orig_pages
= kzalloc(sizeof(struct page
*) *
1981 FUSE_MAX_PAGES_PER_REQ
,
1983 if (!data
.orig_pages
)
1986 err
= write_cache_pages(mapping
, wbc
, fuse_writepages_fill
, &data
);
1988 /* Ignore errors if we can write at least one page */
1989 BUG_ON(!data
.req
->num_pages
);
1990 fuse_writepages_send(&data
);
1994 fuse_file_put(data
.ff
, false);
1996 kfree(data
.orig_pages
);
2002 * It's worthy to make sure that space is reserved on disk for the write,
2003 * but how to implement it without killing performance need more thinking.
2005 static int fuse_write_begin(struct file
*file
, struct address_space
*mapping
,
2006 loff_t pos
, unsigned len
, unsigned flags
,
2007 struct page
**pagep
, void **fsdata
)
2009 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
2010 struct fuse_conn
*fc
= get_fuse_conn(file
->f_dentry
->d_inode
);
2015 WARN_ON(!fc
->writeback_cache
);
2017 page
= grab_cache_page_write_begin(mapping
, index
, flags
);
2021 fuse_wait_on_page_writeback(mapping
->host
, page
->index
);
2023 if (PageUptodate(page
) || len
== PAGE_CACHE_SIZE
)
2026 * Check if the start this page comes after the end of file, in which
2027 * case the readpage can be optimized away.
2029 fsize
= i_size_read(mapping
->host
);
2030 if (fsize
<= (pos
& PAGE_CACHE_MASK
)) {
2031 size_t off
= pos
& ~PAGE_CACHE_MASK
;
2033 zero_user_segment(page
, 0, off
);
2036 err
= fuse_do_readpage(file
, page
);
2045 page_cache_release(page
);
2050 static int fuse_write_end(struct file
*file
, struct address_space
*mapping
,
2051 loff_t pos
, unsigned len
, unsigned copied
,
2052 struct page
*page
, void *fsdata
)
2054 struct inode
*inode
= page
->mapping
->host
;
2056 if (!PageUptodate(page
)) {
2057 /* Zero any unwritten bytes at the end of the page */
2058 size_t endoff
= (pos
+ copied
) & ~PAGE_CACHE_MASK
;
2060 zero_user_segment(page
, endoff
, PAGE_CACHE_SIZE
);
2061 SetPageUptodate(page
);
2064 fuse_write_update_size(inode
, pos
+ copied
);
2065 set_page_dirty(page
);
2067 page_cache_release(page
);
2072 static int fuse_launder_page(struct page
*page
)
2075 if (clear_page_dirty_for_io(page
)) {
2076 struct inode
*inode
= page
->mapping
->host
;
2077 err
= fuse_writepage_locked(page
);
2079 fuse_wait_on_page_writeback(inode
, page
->index
);
2085 * Write back dirty pages now, because there may not be any suitable
2088 static void fuse_vma_close(struct vm_area_struct
*vma
)
2090 filemap_write_and_wait(vma
->vm_file
->f_mapping
);
2094 * Wait for writeback against this page to complete before allowing it
2095 * to be marked dirty again, and hence written back again, possibly
2096 * before the previous writepage completed.
2098 * Block here, instead of in ->writepage(), so that the userspace fs
2099 * can only block processes actually operating on the filesystem.
2101 * Otherwise unprivileged userspace fs would be able to block
2106 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2108 static int fuse_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
2110 struct page
*page
= vmf
->page
;
2111 struct inode
*inode
= file_inode(vma
->vm_file
);
2113 file_update_time(vma
->vm_file
);
2115 if (page
->mapping
!= inode
->i_mapping
) {
2117 return VM_FAULT_NOPAGE
;
2120 fuse_wait_on_page_writeback(inode
, page
->index
);
2121 return VM_FAULT_LOCKED
;
2124 static const struct vm_operations_struct fuse_file_vm_ops
= {
2125 .close
= fuse_vma_close
,
2126 .fault
= filemap_fault
,
2127 .map_pages
= filemap_map_pages
,
2128 .page_mkwrite
= fuse_page_mkwrite
,
2129 .remap_pages
= generic_file_remap_pages
,
2132 static int fuse_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2134 if ((vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
))
2135 fuse_link_write_file(file
);
2137 file_accessed(file
);
2138 vma
->vm_ops
= &fuse_file_vm_ops
;
2142 static int fuse_direct_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2144 /* Can't provide the coherency needed for MAP_SHARED */
2145 if (vma
->vm_flags
& VM_MAYSHARE
)
2148 invalidate_inode_pages2(file
->f_mapping
);
2150 return generic_file_mmap(file
, vma
);
2153 static int convert_fuse_file_lock(const struct fuse_file_lock
*ffl
,
2154 struct file_lock
*fl
)
2156 switch (ffl
->type
) {
2162 if (ffl
->start
> OFFSET_MAX
|| ffl
->end
> OFFSET_MAX
||
2163 ffl
->end
< ffl
->start
)
2166 fl
->fl_start
= ffl
->start
;
2167 fl
->fl_end
= ffl
->end
;
2168 fl
->fl_pid
= ffl
->pid
;
2174 fl
->fl_type
= ffl
->type
;
2178 static void fuse_lk_fill(struct fuse_req
*req
, struct file
*file
,
2179 const struct file_lock
*fl
, int opcode
, pid_t pid
,
2182 struct inode
*inode
= file_inode(file
);
2183 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2184 struct fuse_file
*ff
= file
->private_data
;
2185 struct fuse_lk_in
*arg
= &req
->misc
.lk_in
;
2188 arg
->owner
= fuse_lock_owner_id(fc
, fl
->fl_owner
);
2189 arg
->lk
.start
= fl
->fl_start
;
2190 arg
->lk
.end
= fl
->fl_end
;
2191 arg
->lk
.type
= fl
->fl_type
;
2194 arg
->lk_flags
|= FUSE_LK_FLOCK
;
2195 req
->in
.h
.opcode
= opcode
;
2196 req
->in
.h
.nodeid
= get_node_id(inode
);
2197 req
->in
.numargs
= 1;
2198 req
->in
.args
[0].size
= sizeof(*arg
);
2199 req
->in
.args
[0].value
= arg
;
2202 static int fuse_getlk(struct file
*file
, struct file_lock
*fl
)
2204 struct inode
*inode
= file_inode(file
);
2205 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2206 struct fuse_req
*req
;
2207 struct fuse_lk_out outarg
;
2210 req
= fuse_get_req_nopages(fc
);
2212 return PTR_ERR(req
);
2214 fuse_lk_fill(req
, file
, fl
, FUSE_GETLK
, 0, 0);
2215 req
->out
.numargs
= 1;
2216 req
->out
.args
[0].size
= sizeof(outarg
);
2217 req
->out
.args
[0].value
= &outarg
;
2218 fuse_request_send(fc
, req
);
2219 err
= req
->out
.h
.error
;
2220 fuse_put_request(fc
, req
);
2222 err
= convert_fuse_file_lock(&outarg
.lk
, fl
);
2227 static int fuse_setlk(struct file
*file
, struct file_lock
*fl
, int flock
)
2229 struct inode
*inode
= file_inode(file
);
2230 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2231 struct fuse_req
*req
;
2232 int opcode
= (fl
->fl_flags
& FL_SLEEP
) ? FUSE_SETLKW
: FUSE_SETLK
;
2233 pid_t pid
= fl
->fl_type
!= F_UNLCK
? current
->tgid
: 0;
2236 if (fl
->fl_lmops
&& fl
->fl_lmops
->lm_grant
) {
2237 /* NLM needs asynchronous locks, which we don't support yet */
2241 /* Unlock on close is handled by the flush method */
2242 if (fl
->fl_flags
& FL_CLOSE
)
2245 req
= fuse_get_req_nopages(fc
);
2247 return PTR_ERR(req
);
2249 fuse_lk_fill(req
, file
, fl
, opcode
, pid
, flock
);
2250 fuse_request_send(fc
, req
);
2251 err
= req
->out
.h
.error
;
2252 /* locking is restartable */
2255 fuse_put_request(fc
, req
);
2259 static int fuse_file_lock(struct file
*file
, int cmd
, struct file_lock
*fl
)
2261 struct inode
*inode
= file_inode(file
);
2262 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2265 if (cmd
== F_CANCELLK
) {
2267 } else if (cmd
== F_GETLK
) {
2269 posix_test_lock(file
, fl
);
2272 err
= fuse_getlk(file
, fl
);
2275 err
= posix_lock_file(file
, fl
, NULL
);
2277 err
= fuse_setlk(file
, fl
, 0);
2282 static int fuse_file_flock(struct file
*file
, int cmd
, struct file_lock
*fl
)
2284 struct inode
*inode
= file_inode(file
);
2285 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2289 err
= flock_lock_file_wait(file
, fl
);
2291 struct fuse_file
*ff
= file
->private_data
;
2293 /* emulate flock with POSIX locks */
2294 fl
->fl_owner
= (fl_owner_t
) file
;
2296 err
= fuse_setlk(file
, fl
, 1);
2302 static sector_t
fuse_bmap(struct address_space
*mapping
, sector_t block
)
2304 struct inode
*inode
= mapping
->host
;
2305 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2306 struct fuse_req
*req
;
2307 struct fuse_bmap_in inarg
;
2308 struct fuse_bmap_out outarg
;
2311 if (!inode
->i_sb
->s_bdev
|| fc
->no_bmap
)
2314 req
= fuse_get_req_nopages(fc
);
2318 memset(&inarg
, 0, sizeof(inarg
));
2319 inarg
.block
= block
;
2320 inarg
.blocksize
= inode
->i_sb
->s_blocksize
;
2321 req
->in
.h
.opcode
= FUSE_BMAP
;
2322 req
->in
.h
.nodeid
= get_node_id(inode
);
2323 req
->in
.numargs
= 1;
2324 req
->in
.args
[0].size
= sizeof(inarg
);
2325 req
->in
.args
[0].value
= &inarg
;
2326 req
->out
.numargs
= 1;
2327 req
->out
.args
[0].size
= sizeof(outarg
);
2328 req
->out
.args
[0].value
= &outarg
;
2329 fuse_request_send(fc
, req
);
2330 err
= req
->out
.h
.error
;
2331 fuse_put_request(fc
, req
);
2335 return err
? 0 : outarg
.block
;
2338 static loff_t
fuse_file_llseek(struct file
*file
, loff_t offset
, int whence
)
2341 struct inode
*inode
= file_inode(file
);
2343 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2344 if (whence
== SEEK_CUR
|| whence
== SEEK_SET
)
2345 return generic_file_llseek(file
, offset
, whence
);
2347 mutex_lock(&inode
->i_mutex
);
2348 retval
= fuse_update_attributes(inode
, NULL
, file
, NULL
);
2350 retval
= generic_file_llseek(file
, offset
, whence
);
2351 mutex_unlock(&inode
->i_mutex
);
2356 static int fuse_ioctl_copy_user(struct page
**pages
, struct iovec
*iov
,
2357 unsigned int nr_segs
, size_t bytes
, bool to_user
)
2365 iov_iter_init(&ii
, iov
, nr_segs
, bytes
, 0);
2367 while (iov_iter_count(&ii
)) {
2368 struct page
*page
= pages
[page_idx
++];
2369 size_t todo
= min_t(size_t, PAGE_SIZE
, iov_iter_count(&ii
));
2375 char __user
*uaddr
= ii
.iov
->iov_base
+ ii
.iov_offset
;
2376 size_t iov_len
= ii
.iov
->iov_len
- ii
.iov_offset
;
2377 size_t copy
= min(todo
, iov_len
);
2381 left
= copy_from_user(kaddr
, uaddr
, copy
);
2383 left
= copy_to_user(uaddr
, kaddr
, copy
);
2388 iov_iter_advance(&ii
, copy
);
2400 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2401 * ABI was defined to be 'struct iovec' which is different on 32bit
2402 * and 64bit. Fortunately we can determine which structure the server
2403 * used from the size of the reply.
2405 static int fuse_copy_ioctl_iovec_old(struct iovec
*dst
, void *src
,
2406 size_t transferred
, unsigned count
,
2409 #ifdef CONFIG_COMPAT
2410 if (count
* sizeof(struct compat_iovec
) == transferred
) {
2411 struct compat_iovec
*ciov
= src
;
2415 * With this interface a 32bit server cannot support
2416 * non-compat (i.e. ones coming from 64bit apps) ioctl
2422 for (i
= 0; i
< count
; i
++) {
2423 dst
[i
].iov_base
= compat_ptr(ciov
[i
].iov_base
);
2424 dst
[i
].iov_len
= ciov
[i
].iov_len
;
2430 if (count
* sizeof(struct iovec
) != transferred
)
2433 memcpy(dst
, src
, transferred
);
2437 /* Make sure iov_length() won't overflow */
2438 static int fuse_verify_ioctl_iov(struct iovec
*iov
, size_t count
)
2441 u32 max
= FUSE_MAX_PAGES_PER_REQ
<< PAGE_SHIFT
;
2443 for (n
= 0; n
< count
; n
++, iov
++) {
2444 if (iov
->iov_len
> (size_t) max
)
2446 max
-= iov
->iov_len
;
2451 static int fuse_copy_ioctl_iovec(struct fuse_conn
*fc
, struct iovec
*dst
,
2452 void *src
, size_t transferred
, unsigned count
,
2456 struct fuse_ioctl_iovec
*fiov
= src
;
2458 if (fc
->minor
< 16) {
2459 return fuse_copy_ioctl_iovec_old(dst
, src
, transferred
,
2463 if (count
* sizeof(struct fuse_ioctl_iovec
) != transferred
)
2466 for (i
= 0; i
< count
; i
++) {
2467 /* Did the server supply an inappropriate value? */
2468 if (fiov
[i
].base
!= (unsigned long) fiov
[i
].base
||
2469 fiov
[i
].len
!= (unsigned long) fiov
[i
].len
)
2472 dst
[i
].iov_base
= (void __user
*) (unsigned long) fiov
[i
].base
;
2473 dst
[i
].iov_len
= (size_t) fiov
[i
].len
;
2475 #ifdef CONFIG_COMPAT
2477 (ptr_to_compat(dst
[i
].iov_base
) != fiov
[i
].base
||
2478 (compat_size_t
) dst
[i
].iov_len
!= fiov
[i
].len
))
2488 * For ioctls, there is no generic way to determine how much memory
2489 * needs to be read and/or written. Furthermore, ioctls are allowed
2490 * to dereference the passed pointer, so the parameter requires deep
2491 * copying but FUSE has no idea whatsoever about what to copy in or
2494 * This is solved by allowing FUSE server to retry ioctl with
2495 * necessary in/out iovecs. Let's assume the ioctl implementation
2496 * needs to read in the following structure.
2503 * On the first callout to FUSE server, inarg->in_size and
2504 * inarg->out_size will be NULL; then, the server completes the ioctl
2505 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2506 * the actual iov array to
2508 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2510 * which tells FUSE to copy in the requested area and retry the ioctl.
2511 * On the second round, the server has access to the structure and
2512 * from that it can tell what to look for next, so on the invocation,
2513 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2515 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2516 * { .iov_base = a.buf, .iov_len = a.buflen } }
2518 * FUSE will copy both struct a and the pointed buffer from the
2519 * process doing the ioctl and retry ioctl with both struct a and the
2522 * This time, FUSE server has everything it needs and completes ioctl
2523 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2525 * Copying data out works the same way.
2527 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2528 * automatically initializes in and out iovs by decoding @cmd with
2529 * _IOC_* macros and the server is not allowed to request RETRY. This
2530 * limits ioctl data transfers to well-formed ioctls and is the forced
2531 * behavior for all FUSE servers.
2533 long fuse_do_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
,
2536 struct fuse_file
*ff
= file
->private_data
;
2537 struct fuse_conn
*fc
= ff
->fc
;
2538 struct fuse_ioctl_in inarg
= {
2544 struct fuse_ioctl_out outarg
;
2545 struct fuse_req
*req
= NULL
;
2546 struct page
**pages
= NULL
;
2547 struct iovec
*iov_page
= NULL
;
2548 struct iovec
*in_iov
= NULL
, *out_iov
= NULL
;
2549 unsigned int in_iovs
= 0, out_iovs
= 0, num_pages
= 0, max_pages
;
2550 size_t in_size
, out_size
, transferred
;
2553 #if BITS_PER_LONG == 32
2554 inarg
.flags
|= FUSE_IOCTL_32BIT
;
2556 if (flags
& FUSE_IOCTL_COMPAT
)
2557 inarg
.flags
|= FUSE_IOCTL_32BIT
;
2560 /* assume all the iovs returned by client always fits in a page */
2561 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec
) * FUSE_IOCTL_MAX_IOV
> PAGE_SIZE
);
2564 pages
= kcalloc(FUSE_MAX_PAGES_PER_REQ
, sizeof(pages
[0]), GFP_KERNEL
);
2565 iov_page
= (struct iovec
*) __get_free_page(GFP_KERNEL
);
2566 if (!pages
|| !iov_page
)
2570 * If restricted, initialize IO parameters as encoded in @cmd.
2571 * RETRY from server is not allowed.
2573 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
)) {
2574 struct iovec
*iov
= iov_page
;
2576 iov
->iov_base
= (void __user
*)arg
;
2577 iov
->iov_len
= _IOC_SIZE(cmd
);
2579 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
2584 if (_IOC_DIR(cmd
) & _IOC_READ
) {
2591 inarg
.in_size
= in_size
= iov_length(in_iov
, in_iovs
);
2592 inarg
.out_size
= out_size
= iov_length(out_iov
, out_iovs
);
2595 * Out data can be used either for actual out data or iovs,
2596 * make sure there always is at least one page.
2598 out_size
= max_t(size_t, out_size
, PAGE_SIZE
);
2599 max_pages
= DIV_ROUND_UP(max(in_size
, out_size
), PAGE_SIZE
);
2601 /* make sure there are enough buffer pages and init request with them */
2603 if (max_pages
> FUSE_MAX_PAGES_PER_REQ
)
2605 while (num_pages
< max_pages
) {
2606 pages
[num_pages
] = alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
2607 if (!pages
[num_pages
])
2612 req
= fuse_get_req(fc
, num_pages
);
2618 memcpy(req
->pages
, pages
, sizeof(req
->pages
[0]) * num_pages
);
2619 req
->num_pages
= num_pages
;
2620 fuse_page_descs_length_init(req
, 0, req
->num_pages
);
2622 /* okay, let's send it to the client */
2623 req
->in
.h
.opcode
= FUSE_IOCTL
;
2624 req
->in
.h
.nodeid
= ff
->nodeid
;
2625 req
->in
.numargs
= 1;
2626 req
->in
.args
[0].size
= sizeof(inarg
);
2627 req
->in
.args
[0].value
= &inarg
;
2630 req
->in
.args
[1].size
= in_size
;
2631 req
->in
.argpages
= 1;
2633 err
= fuse_ioctl_copy_user(pages
, in_iov
, in_iovs
, in_size
,
2639 req
->out
.numargs
= 2;
2640 req
->out
.args
[0].size
= sizeof(outarg
);
2641 req
->out
.args
[0].value
= &outarg
;
2642 req
->out
.args
[1].size
= out_size
;
2643 req
->out
.argpages
= 1;
2644 req
->out
.argvar
= 1;
2646 fuse_request_send(fc
, req
);
2647 err
= req
->out
.h
.error
;
2648 transferred
= req
->out
.args
[1].size
;
2649 fuse_put_request(fc
, req
);
2654 /* did it ask for retry? */
2655 if (outarg
.flags
& FUSE_IOCTL_RETRY
) {
2658 /* no retry if in restricted mode */
2660 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
))
2663 in_iovs
= outarg
.in_iovs
;
2664 out_iovs
= outarg
.out_iovs
;
2667 * Make sure things are in boundary, separate checks
2668 * are to protect against overflow.
2671 if (in_iovs
> FUSE_IOCTL_MAX_IOV
||
2672 out_iovs
> FUSE_IOCTL_MAX_IOV
||
2673 in_iovs
+ out_iovs
> FUSE_IOCTL_MAX_IOV
)
2676 vaddr
= kmap_atomic(pages
[0]);
2677 err
= fuse_copy_ioctl_iovec(fc
, iov_page
, vaddr
,
2678 transferred
, in_iovs
+ out_iovs
,
2679 (flags
& FUSE_IOCTL_COMPAT
) != 0);
2680 kunmap_atomic(vaddr
);
2685 out_iov
= in_iov
+ in_iovs
;
2687 err
= fuse_verify_ioctl_iov(in_iov
, in_iovs
);
2691 err
= fuse_verify_ioctl_iov(out_iov
, out_iovs
);
2699 if (transferred
> inarg
.out_size
)
2702 err
= fuse_ioctl_copy_user(pages
, out_iov
, out_iovs
, transferred
, true);
2705 fuse_put_request(fc
, req
);
2706 free_page((unsigned long) iov_page
);
2708 __free_page(pages
[--num_pages
]);
2711 return err
? err
: outarg
.result
;
2713 EXPORT_SYMBOL_GPL(fuse_do_ioctl
);
2715 long fuse_ioctl_common(struct file
*file
, unsigned int cmd
,
2716 unsigned long arg
, unsigned int flags
)
2718 struct inode
*inode
= file_inode(file
);
2719 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2721 if (!fuse_allow_current_process(fc
))
2724 if (is_bad_inode(inode
))
2727 return fuse_do_ioctl(file
, cmd
, arg
, flags
);
2730 static long fuse_file_ioctl(struct file
*file
, unsigned int cmd
,
2733 return fuse_ioctl_common(file
, cmd
, arg
, 0);
2736 static long fuse_file_compat_ioctl(struct file
*file
, unsigned int cmd
,
2739 return fuse_ioctl_common(file
, cmd
, arg
, FUSE_IOCTL_COMPAT
);
2743 * All files which have been polled are linked to RB tree
2744 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2745 * find the matching one.
2747 static struct rb_node
**fuse_find_polled_node(struct fuse_conn
*fc
, u64 kh
,
2748 struct rb_node
**parent_out
)
2750 struct rb_node
**link
= &fc
->polled_files
.rb_node
;
2751 struct rb_node
*last
= NULL
;
2754 struct fuse_file
*ff
;
2757 ff
= rb_entry(last
, struct fuse_file
, polled_node
);
2760 link
= &last
->rb_left
;
2761 else if (kh
> ff
->kh
)
2762 link
= &last
->rb_right
;
2773 * The file is about to be polled. Make sure it's on the polled_files
2774 * RB tree. Note that files once added to the polled_files tree are
2775 * not removed before the file is released. This is because a file
2776 * polled once is likely to be polled again.
2778 static void fuse_register_polled_file(struct fuse_conn
*fc
,
2779 struct fuse_file
*ff
)
2781 spin_lock(&fc
->lock
);
2782 if (RB_EMPTY_NODE(&ff
->polled_node
)) {
2783 struct rb_node
**link
, *uninitialized_var(parent
);
2785 link
= fuse_find_polled_node(fc
, ff
->kh
, &parent
);
2787 rb_link_node(&ff
->polled_node
, parent
, link
);
2788 rb_insert_color(&ff
->polled_node
, &fc
->polled_files
);
2790 spin_unlock(&fc
->lock
);
2793 unsigned fuse_file_poll(struct file
*file
, poll_table
*wait
)
2795 struct fuse_file
*ff
= file
->private_data
;
2796 struct fuse_conn
*fc
= ff
->fc
;
2797 struct fuse_poll_in inarg
= { .fh
= ff
->fh
, .kh
= ff
->kh
};
2798 struct fuse_poll_out outarg
;
2799 struct fuse_req
*req
;
2803 return DEFAULT_POLLMASK
;
2805 poll_wait(file
, &ff
->poll_wait
, wait
);
2806 inarg
.events
= (__u32
)poll_requested_events(wait
);
2809 * Ask for notification iff there's someone waiting for it.
2810 * The client may ignore the flag and always notify.
2812 if (waitqueue_active(&ff
->poll_wait
)) {
2813 inarg
.flags
|= FUSE_POLL_SCHEDULE_NOTIFY
;
2814 fuse_register_polled_file(fc
, ff
);
2817 req
= fuse_get_req_nopages(fc
);
2821 req
->in
.h
.opcode
= FUSE_POLL
;
2822 req
->in
.h
.nodeid
= ff
->nodeid
;
2823 req
->in
.numargs
= 1;
2824 req
->in
.args
[0].size
= sizeof(inarg
);
2825 req
->in
.args
[0].value
= &inarg
;
2826 req
->out
.numargs
= 1;
2827 req
->out
.args
[0].size
= sizeof(outarg
);
2828 req
->out
.args
[0].value
= &outarg
;
2829 fuse_request_send(fc
, req
);
2830 err
= req
->out
.h
.error
;
2831 fuse_put_request(fc
, req
);
2834 return outarg
.revents
;
2835 if (err
== -ENOSYS
) {
2837 return DEFAULT_POLLMASK
;
2841 EXPORT_SYMBOL_GPL(fuse_file_poll
);
2844 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2845 * wakes up the poll waiters.
2847 int fuse_notify_poll_wakeup(struct fuse_conn
*fc
,
2848 struct fuse_notify_poll_wakeup_out
*outarg
)
2850 u64 kh
= outarg
->kh
;
2851 struct rb_node
**link
;
2853 spin_lock(&fc
->lock
);
2855 link
= fuse_find_polled_node(fc
, kh
, NULL
);
2857 struct fuse_file
*ff
;
2859 ff
= rb_entry(*link
, struct fuse_file
, polled_node
);
2860 wake_up_interruptible_sync(&ff
->poll_wait
);
2863 spin_unlock(&fc
->lock
);
2867 static void fuse_do_truncate(struct file
*file
)
2869 struct inode
*inode
= file
->f_mapping
->host
;
2872 attr
.ia_valid
= ATTR_SIZE
;
2873 attr
.ia_size
= i_size_read(inode
);
2875 attr
.ia_file
= file
;
2876 attr
.ia_valid
|= ATTR_FILE
;
2878 fuse_do_setattr(inode
, &attr
, file
);
2881 static inline loff_t
fuse_round_up(loff_t off
)
2883 return round_up(off
, FUSE_MAX_PAGES_PER_REQ
<< PAGE_SHIFT
);
2887 fuse_direct_IO(int rw
, struct kiocb
*iocb
, const struct iovec
*iov
,
2888 loff_t offset
, unsigned long nr_segs
)
2891 struct file
*file
= iocb
->ki_filp
;
2892 struct fuse_file
*ff
= file
->private_data
;
2893 bool async_dio
= ff
->fc
->async_dio
;
2895 struct inode
*inode
;
2897 size_t count
= iov_length(iov
, nr_segs
);
2898 struct fuse_io_priv
*io
;
2901 inode
= file
->f_mapping
->host
;
2902 i_size
= i_size_read(inode
);
2904 if ((rw
== READ
) && (offset
> i_size
))
2907 /* optimization for short read */
2908 if (async_dio
&& rw
!= WRITE
&& offset
+ count
> i_size
) {
2909 if (offset
>= i_size
)
2911 count
= min_t(loff_t
, count
, fuse_round_up(i_size
- offset
));
2914 io
= kmalloc(sizeof(struct fuse_io_priv
), GFP_KERNEL
);
2917 spin_lock_init(&io
->lock
);
2921 io
->offset
= offset
;
2922 io
->write
= (rw
== WRITE
);
2926 * By default, we want to optimize all I/Os with async request
2927 * submission to the client filesystem if supported.
2929 io
->async
= async_dio
;
2933 * We cannot asynchronously extend the size of a file. We have no method
2934 * to wait on real async I/O requests, so we must submit this request
2937 if (!is_sync_kiocb(iocb
) && (offset
+ count
> i_size
) && rw
== WRITE
)
2941 ret
= __fuse_direct_write(io
, iov
, nr_segs
, &pos
);
2943 ret
= __fuse_direct_read(io
, iov
, nr_segs
, &pos
, count
);
2946 fuse_aio_complete(io
, ret
< 0 ? ret
: 0, -1);
2948 /* we have a non-extending, async request, so return */
2949 if (!is_sync_kiocb(iocb
))
2950 return -EIOCBQUEUED
;
2952 ret
= wait_on_sync_kiocb(iocb
);
2959 fuse_write_update_size(inode
, pos
);
2960 else if (ret
< 0 && offset
+ count
> i_size
)
2961 fuse_do_truncate(file
);
2967 static long fuse_file_fallocate(struct file
*file
, int mode
, loff_t offset
,
2970 struct fuse_file
*ff
= file
->private_data
;
2971 struct inode
*inode
= file
->f_inode
;
2972 struct fuse_inode
*fi
= get_fuse_inode(inode
);
2973 struct fuse_conn
*fc
= ff
->fc
;
2974 struct fuse_req
*req
;
2975 struct fuse_fallocate_in inarg
= {
2982 bool lock_inode
= !(mode
& FALLOC_FL_KEEP_SIZE
) ||
2983 (mode
& FALLOC_FL_PUNCH_HOLE
);
2985 if (mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
))
2988 if (fc
->no_fallocate
)
2992 mutex_lock(&inode
->i_mutex
);
2993 if (mode
& FALLOC_FL_PUNCH_HOLE
) {
2994 loff_t endbyte
= offset
+ length
- 1;
2995 err
= filemap_write_and_wait_range(inode
->i_mapping
,
3000 fuse_sync_writes(inode
);
3004 if (!(mode
& FALLOC_FL_KEEP_SIZE
))
3005 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
3007 req
= fuse_get_req_nopages(fc
);
3013 req
->in
.h
.opcode
= FUSE_FALLOCATE
;
3014 req
->in
.h
.nodeid
= ff
->nodeid
;
3015 req
->in
.numargs
= 1;
3016 req
->in
.args
[0].size
= sizeof(inarg
);
3017 req
->in
.args
[0].value
= &inarg
;
3018 fuse_request_send(fc
, req
);
3019 err
= req
->out
.h
.error
;
3020 if (err
== -ENOSYS
) {
3021 fc
->no_fallocate
= 1;
3024 fuse_put_request(fc
, req
);
3029 /* we could have extended the file */
3030 if (!(mode
& FALLOC_FL_KEEP_SIZE
)) {
3031 bool changed
= fuse_write_update_size(inode
, offset
+ length
);
3033 if (changed
&& fc
->writeback_cache
)
3034 file_update_time(file
);
3037 if (mode
& FALLOC_FL_PUNCH_HOLE
)
3038 truncate_pagecache_range(inode
, offset
, offset
+ length
- 1);
3040 fuse_invalidate_attr(inode
);
3043 if (!(mode
& FALLOC_FL_KEEP_SIZE
))
3044 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
3047 mutex_unlock(&inode
->i_mutex
);
3052 static const struct file_operations fuse_file_operations
= {
3053 .llseek
= fuse_file_llseek
,
3054 .read
= do_sync_read
,
3055 .aio_read
= fuse_file_aio_read
,
3056 .write
= do_sync_write
,
3057 .aio_write
= fuse_file_aio_write
,
3058 .mmap
= fuse_file_mmap
,
3060 .flush
= fuse_flush
,
3061 .release
= fuse_release
,
3062 .fsync
= fuse_fsync
,
3063 .lock
= fuse_file_lock
,
3064 .flock
= fuse_file_flock
,
3065 .splice_read
= generic_file_splice_read
,
3066 .unlocked_ioctl
= fuse_file_ioctl
,
3067 .compat_ioctl
= fuse_file_compat_ioctl
,
3068 .poll
= fuse_file_poll
,
3069 .fallocate
= fuse_file_fallocate
,
3072 static const struct file_operations fuse_direct_io_file_operations
= {
3073 .llseek
= fuse_file_llseek
,
3074 .read
= fuse_direct_read
,
3075 .write
= fuse_direct_write
,
3076 .mmap
= fuse_direct_mmap
,
3078 .flush
= fuse_flush
,
3079 .release
= fuse_release
,
3080 .fsync
= fuse_fsync
,
3081 .lock
= fuse_file_lock
,
3082 .flock
= fuse_file_flock
,
3083 .unlocked_ioctl
= fuse_file_ioctl
,
3084 .compat_ioctl
= fuse_file_compat_ioctl
,
3085 .poll
= fuse_file_poll
,
3086 .fallocate
= fuse_file_fallocate
,
3087 /* no splice_read */
3090 static const struct address_space_operations fuse_file_aops
= {
3091 .readpage
= fuse_readpage
,
3092 .writepage
= fuse_writepage
,
3093 .writepages
= fuse_writepages
,
3094 .launder_page
= fuse_launder_page
,
3095 .readpages
= fuse_readpages
,
3096 .set_page_dirty
= __set_page_dirty_nobuffers
,
3098 .direct_IO
= fuse_direct_IO
,
3099 .write_begin
= fuse_write_begin
,
3100 .write_end
= fuse_write_end
,
3103 void fuse_init_file_inode(struct inode
*inode
)
3105 inode
->i_fop
= &fuse_file_operations
;
3106 inode
->i_data
.a_ops
= &fuse_file_aops
;