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
;
132 fuse_request_send(ff
->fc
, req
);
133 path_put(&req
->misc
.release
.path
);
134 fuse_put_request(ff
->fc
, req
);
136 req
->end
= fuse_release_end
;
138 fuse_request_send_background(ff
->fc
, req
);
144 int fuse_do_open(struct fuse_conn
*fc
, u64 nodeid
, struct file
*file
,
147 struct fuse_open_out outarg
;
148 struct fuse_file
*ff
;
150 int opcode
= isdir
? FUSE_OPENDIR
: FUSE_OPEN
;
152 ff
= fuse_file_alloc(fc
);
156 err
= fuse_send_open(fc
, nodeid
, file
, opcode
, &outarg
);
163 outarg
.open_flags
&= ~FOPEN_DIRECT_IO
;
167 ff
->open_flags
= outarg
.open_flags
;
168 file
->private_data
= fuse_file_get(ff
);
172 EXPORT_SYMBOL_GPL(fuse_do_open
);
174 void fuse_finish_open(struct inode
*inode
, struct file
*file
)
176 struct fuse_file
*ff
= file
->private_data
;
177 struct fuse_conn
*fc
= get_fuse_conn(inode
);
179 if (ff
->open_flags
& FOPEN_DIRECT_IO
)
180 file
->f_op
= &fuse_direct_io_file_operations
;
181 if (!(ff
->open_flags
& FOPEN_KEEP_CACHE
))
182 invalidate_inode_pages2(inode
->i_mapping
);
183 if (ff
->open_flags
& FOPEN_NONSEEKABLE
)
184 nonseekable_open(inode
, file
);
185 if (fc
->atomic_o_trunc
&& (file
->f_flags
& O_TRUNC
)) {
186 struct fuse_inode
*fi
= get_fuse_inode(inode
);
188 spin_lock(&fc
->lock
);
189 fi
->attr_version
= ++fc
->attr_version
;
190 i_size_write(inode
, 0);
191 spin_unlock(&fc
->lock
);
192 fuse_invalidate_attr(inode
);
196 int fuse_open_common(struct inode
*inode
, struct file
*file
, bool isdir
)
198 struct fuse_conn
*fc
= get_fuse_conn(inode
);
201 err
= generic_file_open(inode
, file
);
205 err
= fuse_do_open(fc
, get_node_id(inode
), file
, isdir
);
209 fuse_finish_open(inode
, file
);
214 static void fuse_prepare_release(struct fuse_file
*ff
, int flags
, int opcode
)
216 struct fuse_conn
*fc
= ff
->fc
;
217 struct fuse_req
*req
= ff
->reserved_req
;
218 struct fuse_release_in
*inarg
= &req
->misc
.release
.in
;
220 spin_lock(&fc
->lock
);
221 list_del(&ff
->write_entry
);
222 if (!RB_EMPTY_NODE(&ff
->polled_node
))
223 rb_erase(&ff
->polled_node
, &fc
->polled_files
);
224 spin_unlock(&fc
->lock
);
226 wake_up_interruptible_all(&ff
->poll_wait
);
229 inarg
->flags
= flags
;
230 req
->in
.h
.opcode
= opcode
;
231 req
->in
.h
.nodeid
= ff
->nodeid
;
233 req
->in
.args
[0].size
= sizeof(struct fuse_release_in
);
234 req
->in
.args
[0].value
= inarg
;
237 void fuse_release_common(struct file
*file
, int opcode
)
239 struct fuse_file
*ff
;
240 struct fuse_req
*req
;
242 ff
= file
->private_data
;
246 req
= ff
->reserved_req
;
247 fuse_prepare_release(ff
, file
->f_flags
, opcode
);
250 struct fuse_release_in
*inarg
= &req
->misc
.release
.in
;
251 inarg
->release_flags
|= FUSE_RELEASE_FLOCK_UNLOCK
;
252 inarg
->lock_owner
= fuse_lock_owner_id(ff
->fc
,
255 /* Hold vfsmount and dentry until release is finished */
256 path_get(&file
->f_path
);
257 req
->misc
.release
.path
= file
->f_path
;
260 * Normally this will send the RELEASE request, however if
261 * some asynchronous READ or WRITE requests are outstanding,
262 * the sending will be delayed.
264 * Make the release synchronous if this is a fuseblk mount,
265 * synchronous RELEASE is allowed (and desirable) in this case
266 * because the server can be trusted not to screw up.
268 fuse_file_put(ff
, ff
->fc
->destroy_req
!= NULL
);
271 static int fuse_open(struct inode
*inode
, struct file
*file
)
273 return fuse_open_common(inode
, file
, false);
276 static int fuse_release(struct inode
*inode
, struct file
*file
)
278 fuse_release_common(file
, FUSE_RELEASE
);
280 /* return value is ignored by VFS */
284 void fuse_sync_release(struct fuse_file
*ff
, int flags
)
286 WARN_ON(atomic_read(&ff
->count
) > 1);
287 fuse_prepare_release(ff
, flags
, FUSE_RELEASE
);
288 ff
->reserved_req
->force
= 1;
289 ff
->reserved_req
->background
= 0;
290 fuse_request_send(ff
->fc
, ff
->reserved_req
);
291 fuse_put_request(ff
->fc
, ff
->reserved_req
);
294 EXPORT_SYMBOL_GPL(fuse_sync_release
);
297 * Scramble the ID space with XTEA, so that the value of the files_struct
298 * pointer is not exposed to userspace.
300 u64
fuse_lock_owner_id(struct fuse_conn
*fc
, fl_owner_t id
)
302 u32
*k
= fc
->scramble_key
;
303 u64 v
= (unsigned long) id
;
309 for (i
= 0; i
< 32; i
++) {
310 v0
+= ((v1
<< 4 ^ v1
>> 5) + v1
) ^ (sum
+ k
[sum
& 3]);
312 v1
+= ((v0
<< 4 ^ v0
>> 5) + v0
) ^ (sum
+ k
[sum
>>11 & 3]);
315 return (u64
) v0
+ ((u64
) v1
<< 32);
319 * Check if page is under writeback
321 * This is currently done by walking the list of writepage requests
322 * for the inode, which can be pretty inefficient.
324 static bool fuse_page_is_writeback(struct inode
*inode
, pgoff_t index
)
326 struct fuse_conn
*fc
= get_fuse_conn(inode
);
327 struct fuse_inode
*fi
= get_fuse_inode(inode
);
328 struct fuse_req
*req
;
331 spin_lock(&fc
->lock
);
332 list_for_each_entry(req
, &fi
->writepages
, writepages_entry
) {
335 BUG_ON(req
->inode
!= inode
);
336 curr_index
= req
->misc
.write
.in
.offset
>> PAGE_CACHE_SHIFT
;
337 if (curr_index
<= index
&&
338 index
< curr_index
+ req
->num_pages
) {
343 spin_unlock(&fc
->lock
);
349 * Wait for page writeback to be completed.
351 * Since fuse doesn't rely on the VM writeback tracking, this has to
352 * use some other means.
354 static int fuse_wait_on_page_writeback(struct inode
*inode
, pgoff_t index
)
356 struct fuse_inode
*fi
= get_fuse_inode(inode
);
358 wait_event(fi
->page_waitq
, !fuse_page_is_writeback(inode
, index
));
362 static int fuse_flush(struct file
*file
, fl_owner_t id
)
364 struct inode
*inode
= file_inode(file
);
365 struct fuse_conn
*fc
= get_fuse_conn(inode
);
366 struct fuse_file
*ff
= file
->private_data
;
367 struct fuse_req
*req
;
368 struct fuse_flush_in inarg
;
371 if (is_bad_inode(inode
))
377 req
= fuse_get_req_nofail_nopages(fc
, file
);
378 memset(&inarg
, 0, sizeof(inarg
));
380 inarg
.lock_owner
= fuse_lock_owner_id(fc
, id
);
381 req
->in
.h
.opcode
= FUSE_FLUSH
;
382 req
->in
.h
.nodeid
= get_node_id(inode
);
384 req
->in
.args
[0].size
= sizeof(inarg
);
385 req
->in
.args
[0].value
= &inarg
;
387 fuse_request_send(fc
, req
);
388 err
= req
->out
.h
.error
;
389 fuse_put_request(fc
, req
);
390 if (err
== -ENOSYS
) {
398 * Wait for all pending writepages on the inode to finish.
400 * This is currently done by blocking further writes with FUSE_NOWRITE
401 * and waiting for all sent writes to complete.
403 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
404 * could conflict with truncation.
406 static void fuse_sync_writes(struct inode
*inode
)
408 fuse_set_nowrite(inode
);
409 fuse_release_nowrite(inode
);
412 int fuse_fsync_common(struct file
*file
, loff_t start
, loff_t end
,
413 int datasync
, int isdir
)
415 struct inode
*inode
= file
->f_mapping
->host
;
416 struct fuse_conn
*fc
= get_fuse_conn(inode
);
417 struct fuse_file
*ff
= file
->private_data
;
418 struct fuse_req
*req
;
419 struct fuse_fsync_in inarg
;
422 if (is_bad_inode(inode
))
425 err
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
429 if ((!isdir
&& fc
->no_fsync
) || (isdir
&& fc
->no_fsyncdir
))
432 mutex_lock(&inode
->i_mutex
);
435 * Start writeback against all dirty pages of the inode, then
436 * wait for all outstanding writes, before sending the FSYNC
439 err
= write_inode_now(inode
, 0);
443 fuse_sync_writes(inode
);
445 req
= fuse_get_req_nopages(fc
);
451 memset(&inarg
, 0, sizeof(inarg
));
453 inarg
.fsync_flags
= datasync
? 1 : 0;
454 req
->in
.h
.opcode
= isdir
? FUSE_FSYNCDIR
: FUSE_FSYNC
;
455 req
->in
.h
.nodeid
= get_node_id(inode
);
457 req
->in
.args
[0].size
= sizeof(inarg
);
458 req
->in
.args
[0].value
= &inarg
;
459 fuse_request_send(fc
, req
);
460 err
= req
->out
.h
.error
;
461 fuse_put_request(fc
, req
);
462 if (err
== -ENOSYS
) {
470 mutex_unlock(&inode
->i_mutex
);
474 static int fuse_fsync(struct file
*file
, loff_t start
, loff_t end
,
477 return fuse_fsync_common(file
, start
, end
, datasync
, 0);
480 void fuse_read_fill(struct fuse_req
*req
, struct file
*file
, loff_t pos
,
481 size_t count
, int opcode
)
483 struct fuse_read_in
*inarg
= &req
->misc
.read
.in
;
484 struct fuse_file
*ff
= file
->private_data
;
489 inarg
->flags
= file
->f_flags
;
490 req
->in
.h
.opcode
= opcode
;
491 req
->in
.h
.nodeid
= ff
->nodeid
;
493 req
->in
.args
[0].size
= sizeof(struct fuse_read_in
);
494 req
->in
.args
[0].value
= inarg
;
496 req
->out
.numargs
= 1;
497 req
->out
.args
[0].size
= count
;
500 static void fuse_release_user_pages(struct fuse_req
*req
, int write
)
504 for (i
= 0; i
< req
->num_pages
; i
++) {
505 struct page
*page
= req
->pages
[i
];
507 set_page_dirty_lock(page
);
513 * In case of short read, the caller sets 'pos' to the position of
514 * actual end of fuse request in IO request. Otherwise, if bytes_requested
515 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
518 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
519 * both submitted asynchronously. The first of them was ACKed by userspace as
520 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
521 * second request was ACKed as short, e.g. only 1K was read, resulting in
524 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
525 * will be equal to the length of the longest contiguous fragment of
526 * transferred data starting from the beginning of IO request.
528 static void fuse_aio_complete(struct fuse_io_priv
*io
, int err
, ssize_t pos
)
532 spin_lock(&io
->lock
);
534 io
->err
= io
->err
? : err
;
535 else if (pos
>= 0 && (io
->bytes
< 0 || pos
< io
->bytes
))
539 spin_unlock(&io
->lock
);
546 else if (io
->bytes
>= 0 && io
->write
)
549 res
= io
->bytes
< 0 ? io
->size
: io
->bytes
;
551 if (!is_sync_kiocb(io
->iocb
)) {
552 struct inode
*inode
= file_inode(io
->iocb
->ki_filp
);
553 struct fuse_conn
*fc
= get_fuse_conn(inode
);
554 struct fuse_inode
*fi
= get_fuse_inode(inode
);
556 spin_lock(&fc
->lock
);
557 fi
->attr_version
= ++fc
->attr_version
;
558 spin_unlock(&fc
->lock
);
562 aio_complete(io
->iocb
, res
, 0);
567 static void fuse_aio_complete_req(struct fuse_conn
*fc
, struct fuse_req
*req
)
569 struct fuse_io_priv
*io
= req
->io
;
572 fuse_release_user_pages(req
, !io
->write
);
575 if (req
->misc
.write
.in
.size
!= req
->misc
.write
.out
.size
)
576 pos
= req
->misc
.write
.in
.offset
- io
->offset
+
577 req
->misc
.write
.out
.size
;
579 if (req
->misc
.read
.in
.size
!= req
->out
.args
[0].size
)
580 pos
= req
->misc
.read
.in
.offset
- io
->offset
+
581 req
->out
.args
[0].size
;
584 fuse_aio_complete(io
, req
->out
.h
.error
, pos
);
587 static size_t fuse_async_req_send(struct fuse_conn
*fc
, struct fuse_req
*req
,
588 size_t num_bytes
, struct fuse_io_priv
*io
)
590 spin_lock(&io
->lock
);
591 io
->size
+= num_bytes
;
593 spin_unlock(&io
->lock
);
596 req
->end
= fuse_aio_complete_req
;
598 __fuse_get_request(req
);
599 fuse_request_send_background(fc
, req
);
604 static size_t fuse_send_read(struct fuse_req
*req
, struct fuse_io_priv
*io
,
605 loff_t pos
, size_t count
, fl_owner_t owner
)
607 struct file
*file
= io
->file
;
608 struct fuse_file
*ff
= file
->private_data
;
609 struct fuse_conn
*fc
= ff
->fc
;
611 fuse_read_fill(req
, file
, pos
, count
, FUSE_READ
);
613 struct fuse_read_in
*inarg
= &req
->misc
.read
.in
;
615 inarg
->read_flags
|= FUSE_READ_LOCKOWNER
;
616 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
620 return fuse_async_req_send(fc
, req
, count
, io
);
622 fuse_request_send(fc
, req
);
623 return req
->out
.args
[0].size
;
626 static void fuse_read_update_size(struct inode
*inode
, loff_t size
,
629 struct fuse_conn
*fc
= get_fuse_conn(inode
);
630 struct fuse_inode
*fi
= get_fuse_inode(inode
);
632 spin_lock(&fc
->lock
);
633 if (attr_ver
== fi
->attr_version
&& size
< inode
->i_size
&&
634 !test_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
)) {
635 fi
->attr_version
= ++fc
->attr_version
;
636 i_size_write(inode
, size
);
638 spin_unlock(&fc
->lock
);
641 static int fuse_readpage(struct file
*file
, struct page
*page
)
643 struct fuse_io_priv io
= { .async
= 0, .file
= file
};
644 struct inode
*inode
= page
->mapping
->host
;
645 struct fuse_conn
*fc
= get_fuse_conn(inode
);
646 struct fuse_req
*req
;
648 loff_t pos
= page_offset(page
);
649 size_t count
= PAGE_CACHE_SIZE
;
654 if (is_bad_inode(inode
))
658 * Page writeback can extend beyond the lifetime of the
659 * page-cache page, so make sure we read a properly synced
662 fuse_wait_on_page_writeback(inode
, page
->index
);
664 req
= fuse_get_req(fc
, 1);
669 attr_ver
= fuse_get_attr_version(fc
);
671 req
->out
.page_zeroing
= 1;
672 req
->out
.argpages
= 1;
674 req
->pages
[0] = page
;
675 req
->page_descs
[0].length
= count
;
676 num_read
= fuse_send_read(req
, &io
, pos
, count
, NULL
);
677 err
= req
->out
.h
.error
;
678 fuse_put_request(fc
, req
);
682 * Short read means EOF. If file size is larger, truncate it
684 if (num_read
< count
)
685 fuse_read_update_size(inode
, pos
+ num_read
, attr_ver
);
687 SetPageUptodate(page
);
690 fuse_invalidate_attr(inode
); /* atime changed */
696 static void fuse_readpages_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
699 size_t count
= req
->misc
.read
.in
.size
;
700 size_t num_read
= req
->out
.args
[0].size
;
701 struct address_space
*mapping
= NULL
;
703 for (i
= 0; mapping
== NULL
&& i
< req
->num_pages
; i
++)
704 mapping
= req
->pages
[i
]->mapping
;
707 struct inode
*inode
= mapping
->host
;
710 * Short read means EOF. If file size is larger, truncate it
712 if (!req
->out
.h
.error
&& num_read
< count
) {
715 pos
= page_offset(req
->pages
[0]) + num_read
;
716 fuse_read_update_size(inode
, pos
,
717 req
->misc
.read
.attr_ver
);
719 fuse_invalidate_attr(inode
); /* atime changed */
722 for (i
= 0; i
< req
->num_pages
; i
++) {
723 struct page
*page
= req
->pages
[i
];
724 if (!req
->out
.h
.error
)
725 SetPageUptodate(page
);
729 page_cache_release(page
);
732 fuse_file_put(req
->ff
, false);
735 static void fuse_send_readpages(struct fuse_req
*req
, struct file
*file
)
737 struct fuse_file
*ff
= file
->private_data
;
738 struct fuse_conn
*fc
= ff
->fc
;
739 loff_t pos
= page_offset(req
->pages
[0]);
740 size_t count
= req
->num_pages
<< PAGE_CACHE_SHIFT
;
742 req
->out
.argpages
= 1;
743 req
->out
.page_zeroing
= 1;
744 req
->out
.page_replace
= 1;
745 fuse_read_fill(req
, file
, pos
, count
, FUSE_READ
);
746 req
->misc
.read
.attr_ver
= fuse_get_attr_version(fc
);
747 if (fc
->async_read
) {
748 req
->ff
= fuse_file_get(ff
);
749 req
->end
= fuse_readpages_end
;
750 fuse_request_send_background(fc
, req
);
752 fuse_request_send(fc
, req
);
753 fuse_readpages_end(fc
, req
);
754 fuse_put_request(fc
, req
);
758 struct fuse_fill_data
{
759 struct fuse_req
*req
;
765 static int fuse_readpages_fill(void *_data
, struct page
*page
)
767 struct fuse_fill_data
*data
= _data
;
768 struct fuse_req
*req
= data
->req
;
769 struct inode
*inode
= data
->inode
;
770 struct fuse_conn
*fc
= get_fuse_conn(inode
);
772 fuse_wait_on_page_writeback(inode
, page
->index
);
774 if (req
->num_pages
&&
775 (req
->num_pages
== FUSE_MAX_PAGES_PER_REQ
||
776 (req
->num_pages
+ 1) * PAGE_CACHE_SIZE
> fc
->max_read
||
777 req
->pages
[req
->num_pages
- 1]->index
+ 1 != page
->index
)) {
778 int nr_alloc
= min_t(unsigned, data
->nr_pages
,
779 FUSE_MAX_PAGES_PER_REQ
);
780 fuse_send_readpages(req
, data
->file
);
782 req
= fuse_get_req_for_background(fc
, nr_alloc
);
784 req
= fuse_get_req(fc
, nr_alloc
);
793 if (WARN_ON(req
->num_pages
>= req
->max_pages
)) {
794 fuse_put_request(fc
, req
);
798 page_cache_get(page
);
799 req
->pages
[req
->num_pages
] = page
;
800 req
->page_descs
[req
->num_pages
].length
= PAGE_SIZE
;
806 static int fuse_readpages(struct file
*file
, struct address_space
*mapping
,
807 struct list_head
*pages
, unsigned nr_pages
)
809 struct inode
*inode
= mapping
->host
;
810 struct fuse_conn
*fc
= get_fuse_conn(inode
);
811 struct fuse_fill_data data
;
813 int nr_alloc
= min_t(unsigned, nr_pages
, FUSE_MAX_PAGES_PER_REQ
);
816 if (is_bad_inode(inode
))
822 data
.req
= fuse_get_req_for_background(fc
, nr_alloc
);
824 data
.req
= fuse_get_req(fc
, nr_alloc
);
825 data
.nr_pages
= nr_pages
;
826 err
= PTR_ERR(data
.req
);
827 if (IS_ERR(data
.req
))
830 err
= read_cache_pages(mapping
, pages
, fuse_readpages_fill
, &data
);
832 if (data
.req
->num_pages
)
833 fuse_send_readpages(data
.req
, file
);
835 fuse_put_request(fc
, data
.req
);
841 static ssize_t
fuse_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
842 unsigned long nr_segs
, loff_t pos
)
844 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
845 struct fuse_conn
*fc
= get_fuse_conn(inode
);
848 * In auto invalidate mode, always update attributes on read.
849 * Otherwise, only update if we attempt to read past EOF (to ensure
850 * i_size is up to date).
852 if (fc
->auto_inval_data
||
853 (pos
+ iov_length(iov
, nr_segs
) > i_size_read(inode
))) {
855 err
= fuse_update_attributes(inode
, NULL
, iocb
->ki_filp
, NULL
);
860 return generic_file_aio_read(iocb
, iov
, nr_segs
, pos
);
863 static void fuse_write_fill(struct fuse_req
*req
, struct fuse_file
*ff
,
864 loff_t pos
, size_t count
)
866 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
867 struct fuse_write_out
*outarg
= &req
->misc
.write
.out
;
872 req
->in
.h
.opcode
= FUSE_WRITE
;
873 req
->in
.h
.nodeid
= ff
->nodeid
;
875 if (ff
->fc
->minor
< 9)
876 req
->in
.args
[0].size
= FUSE_COMPAT_WRITE_IN_SIZE
;
878 req
->in
.args
[0].size
= sizeof(struct fuse_write_in
);
879 req
->in
.args
[0].value
= inarg
;
880 req
->in
.args
[1].size
= count
;
881 req
->out
.numargs
= 1;
882 req
->out
.args
[0].size
= sizeof(struct fuse_write_out
);
883 req
->out
.args
[0].value
= outarg
;
886 static size_t fuse_send_write(struct fuse_req
*req
, struct fuse_io_priv
*io
,
887 loff_t pos
, size_t count
, fl_owner_t owner
)
889 struct file
*file
= io
->file
;
890 struct fuse_file
*ff
= file
->private_data
;
891 struct fuse_conn
*fc
= ff
->fc
;
892 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
894 fuse_write_fill(req
, ff
, pos
, count
);
895 inarg
->flags
= file
->f_flags
;
897 inarg
->write_flags
|= FUSE_WRITE_LOCKOWNER
;
898 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
902 return fuse_async_req_send(fc
, req
, count
, io
);
904 fuse_request_send(fc
, req
);
905 return req
->misc
.write
.out
.size
;
908 void fuse_write_update_size(struct inode
*inode
, loff_t pos
)
910 struct fuse_conn
*fc
= get_fuse_conn(inode
);
911 struct fuse_inode
*fi
= get_fuse_inode(inode
);
913 spin_lock(&fc
->lock
);
914 fi
->attr_version
= ++fc
->attr_version
;
915 if (pos
> inode
->i_size
)
916 i_size_write(inode
, pos
);
917 spin_unlock(&fc
->lock
);
920 static size_t fuse_send_write_pages(struct fuse_req
*req
, struct file
*file
,
921 struct inode
*inode
, loff_t pos
,
927 struct fuse_io_priv io
= { .async
= 0, .file
= file
};
929 for (i
= 0; i
< req
->num_pages
; i
++)
930 fuse_wait_on_page_writeback(inode
, req
->pages
[i
]->index
);
932 res
= fuse_send_write(req
, &io
, pos
, count
, NULL
);
934 offset
= req
->page_descs
[0].offset
;
936 for (i
= 0; i
< req
->num_pages
; i
++) {
937 struct page
*page
= req
->pages
[i
];
939 if (!req
->out
.h
.error
&& !offset
&& count
>= PAGE_CACHE_SIZE
)
940 SetPageUptodate(page
);
942 if (count
> PAGE_CACHE_SIZE
- offset
)
943 count
-= PAGE_CACHE_SIZE
- offset
;
949 page_cache_release(page
);
955 static ssize_t
fuse_fill_write_pages(struct fuse_req
*req
,
956 struct address_space
*mapping
,
957 struct iov_iter
*ii
, loff_t pos
)
959 struct fuse_conn
*fc
= get_fuse_conn(mapping
->host
);
960 unsigned offset
= pos
& (PAGE_CACHE_SIZE
- 1);
964 req
->in
.argpages
= 1;
965 req
->page_descs
[0].offset
= offset
;
970 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
971 size_t bytes
= min_t(size_t, PAGE_CACHE_SIZE
- offset
,
974 bytes
= min_t(size_t, bytes
, fc
->max_write
- count
);
978 if (iov_iter_fault_in_readable(ii
, bytes
))
982 page
= grab_cache_page_write_begin(mapping
, index
, 0);
986 if (mapping_writably_mapped(mapping
))
987 flush_dcache_page(page
);
990 tmp
= iov_iter_copy_from_user_atomic(page
, ii
, offset
, bytes
);
992 flush_dcache_page(page
);
994 mark_page_accessed(page
);
998 page_cache_release(page
);
999 bytes
= min(bytes
, iov_iter_single_seg_count(ii
));
1004 req
->pages
[req
->num_pages
] = page
;
1005 req
->page_descs
[req
->num_pages
].length
= tmp
;
1008 iov_iter_advance(ii
, tmp
);
1012 if (offset
== PAGE_CACHE_SIZE
)
1015 if (!fc
->big_writes
)
1017 } while (iov_iter_count(ii
) && count
< fc
->max_write
&&
1018 req
->num_pages
< req
->max_pages
&& offset
== 0);
1020 return count
> 0 ? count
: err
;
1023 static inline unsigned fuse_wr_pages(loff_t pos
, size_t len
)
1025 return min_t(unsigned,
1026 ((pos
+ len
- 1) >> PAGE_CACHE_SHIFT
) -
1027 (pos
>> PAGE_CACHE_SHIFT
) + 1,
1028 FUSE_MAX_PAGES_PER_REQ
);
1031 static ssize_t
fuse_perform_write(struct file
*file
,
1032 struct address_space
*mapping
,
1033 struct iov_iter
*ii
, loff_t pos
)
1035 struct inode
*inode
= mapping
->host
;
1036 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1037 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1041 if (is_bad_inode(inode
))
1044 if (inode
->i_size
< pos
+ iov_iter_count(ii
))
1045 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1048 struct fuse_req
*req
;
1050 unsigned nr_pages
= fuse_wr_pages(pos
, iov_iter_count(ii
));
1052 req
= fuse_get_req(fc
, nr_pages
);
1058 count
= fuse_fill_write_pages(req
, mapping
, ii
, pos
);
1064 num_written
= fuse_send_write_pages(req
, file
, inode
,
1066 err
= req
->out
.h
.error
;
1071 /* break out of the loop on short write */
1072 if (num_written
!= count
)
1076 fuse_put_request(fc
, req
);
1077 } while (!err
&& iov_iter_count(ii
));
1080 fuse_write_update_size(inode
, pos
);
1082 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1083 fuse_invalidate_attr(inode
);
1085 return res
> 0 ? res
: err
;
1088 static ssize_t
fuse_file_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
1089 unsigned long nr_segs
, loff_t pos
)
1091 struct file
*file
= iocb
->ki_filp
;
1092 struct address_space
*mapping
= file
->f_mapping
;
1095 ssize_t written
= 0;
1096 ssize_t written_buffered
= 0;
1097 struct inode
*inode
= mapping
->host
;
1102 WARN_ON(iocb
->ki_pos
!= pos
);
1105 err
= generic_segment_checks(iov
, &nr_segs
, &ocount
, VERIFY_READ
);
1110 mutex_lock(&inode
->i_mutex
);
1112 /* We can write back this queue in page reclaim */
1113 current
->backing_dev_info
= mapping
->backing_dev_info
;
1115 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
1122 err
= file_remove_suid(file
);
1126 err
= file_update_time(file
);
1130 if (file
->f_flags
& O_DIRECT
) {
1131 written
= generic_file_direct_write(iocb
, iov
, &nr_segs
,
1134 if (written
< 0 || written
== count
)
1140 iov_iter_init(&i
, iov
, nr_segs
, count
, written
);
1141 written_buffered
= fuse_perform_write(file
, mapping
, &i
, pos
);
1142 if (written_buffered
< 0) {
1143 err
= written_buffered
;
1146 endbyte
= pos
+ written_buffered
- 1;
1148 err
= filemap_write_and_wait_range(file
->f_mapping
, pos
,
1153 invalidate_mapping_pages(file
->f_mapping
,
1154 pos
>> PAGE_CACHE_SHIFT
,
1155 endbyte
>> PAGE_CACHE_SHIFT
);
1157 written
+= written_buffered
;
1158 iocb
->ki_pos
= pos
+ written_buffered
;
1160 iov_iter_init(&i
, iov
, nr_segs
, count
, 0);
1161 written
= fuse_perform_write(file
, mapping
, &i
, pos
);
1163 iocb
->ki_pos
= pos
+ written
;
1166 current
->backing_dev_info
= NULL
;
1167 mutex_unlock(&inode
->i_mutex
);
1169 return written
? written
: err
;
1172 static inline void fuse_page_descs_length_init(struct fuse_req
*req
,
1173 unsigned index
, unsigned nr_pages
)
1177 for (i
= index
; i
< index
+ nr_pages
; i
++)
1178 req
->page_descs
[i
].length
= PAGE_SIZE
-
1179 req
->page_descs
[i
].offset
;
1182 static inline unsigned long fuse_get_user_addr(const struct iov_iter
*ii
)
1184 return (unsigned long)ii
->iov
->iov_base
+ ii
->iov_offset
;
1187 static inline size_t fuse_get_frag_size(const struct iov_iter
*ii
,
1190 return min(iov_iter_single_seg_count(ii
), max_size
);
1193 static int fuse_get_user_pages(struct fuse_req
*req
, struct iov_iter
*ii
,
1194 size_t *nbytesp
, int write
)
1196 size_t nbytes
= 0; /* # bytes already packed in req */
1198 /* Special case for kernel I/O: can copy directly into the buffer */
1199 if (segment_eq(get_fs(), KERNEL_DS
)) {
1200 unsigned long user_addr
= fuse_get_user_addr(ii
);
1201 size_t frag_size
= fuse_get_frag_size(ii
, *nbytesp
);
1204 req
->in
.args
[1].value
= (void *) user_addr
;
1206 req
->out
.args
[0].value
= (void *) user_addr
;
1208 iov_iter_advance(ii
, frag_size
);
1209 *nbytesp
= frag_size
;
1213 while (nbytes
< *nbytesp
&& req
->num_pages
< req
->max_pages
) {
1215 unsigned long user_addr
= fuse_get_user_addr(ii
);
1216 unsigned offset
= user_addr
& ~PAGE_MASK
;
1217 size_t frag_size
= fuse_get_frag_size(ii
, *nbytesp
- nbytes
);
1220 unsigned n
= req
->max_pages
- req
->num_pages
;
1221 frag_size
= min_t(size_t, frag_size
, n
<< PAGE_SHIFT
);
1223 npages
= (frag_size
+ offset
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1224 npages
= clamp(npages
, 1U, n
);
1226 ret
= get_user_pages_fast(user_addr
, npages
, !write
,
1227 &req
->pages
[req
->num_pages
]);
1232 frag_size
= min_t(size_t, frag_size
,
1233 (npages
<< PAGE_SHIFT
) - offset
);
1234 iov_iter_advance(ii
, frag_size
);
1236 req
->page_descs
[req
->num_pages
].offset
= offset
;
1237 fuse_page_descs_length_init(req
, req
->num_pages
, npages
);
1239 req
->num_pages
+= npages
;
1240 req
->page_descs
[req
->num_pages
- 1].length
-=
1241 (npages
<< PAGE_SHIFT
) - offset
- frag_size
;
1243 nbytes
+= frag_size
;
1247 req
->in
.argpages
= 1;
1249 req
->out
.argpages
= 1;
1256 static inline int fuse_iter_npages(const struct iov_iter
*ii_p
)
1258 struct iov_iter ii
= *ii_p
;
1261 while (iov_iter_count(&ii
) && npages
< FUSE_MAX_PAGES_PER_REQ
) {
1262 unsigned long user_addr
= fuse_get_user_addr(&ii
);
1263 unsigned offset
= user_addr
& ~PAGE_MASK
;
1264 size_t frag_size
= iov_iter_single_seg_count(&ii
);
1266 npages
+= (frag_size
+ offset
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1267 iov_iter_advance(&ii
, frag_size
);
1270 return min(npages
, FUSE_MAX_PAGES_PER_REQ
);
1273 ssize_t
fuse_direct_io(struct fuse_io_priv
*io
, const struct iovec
*iov
,
1274 unsigned long nr_segs
, size_t count
, loff_t
*ppos
,
1277 struct file
*file
= io
->file
;
1278 struct fuse_file
*ff
= file
->private_data
;
1279 struct fuse_conn
*fc
= ff
->fc
;
1280 size_t nmax
= write
? fc
->max_write
: fc
->max_read
;
1283 struct fuse_req
*req
;
1286 iov_iter_init(&ii
, iov
, nr_segs
, count
, 0);
1289 req
= fuse_get_req_for_background(fc
, fuse_iter_npages(&ii
));
1291 req
= fuse_get_req(fc
, fuse_iter_npages(&ii
));
1293 return PTR_ERR(req
);
1297 fl_owner_t owner
= current
->files
;
1298 size_t nbytes
= min(count
, nmax
);
1299 int err
= fuse_get_user_pages(req
, &ii
, &nbytes
, write
);
1306 nres
= fuse_send_write(req
, io
, pos
, nbytes
, owner
);
1308 nres
= fuse_send_read(req
, io
, pos
, nbytes
, owner
);
1311 fuse_release_user_pages(req
, !write
);
1312 if (req
->out
.h
.error
) {
1314 res
= req
->out
.h
.error
;
1316 } else if (nres
> nbytes
) {
1326 fuse_put_request(fc
, req
);
1328 req
= fuse_get_req_for_background(fc
,
1329 fuse_iter_npages(&ii
));
1331 req
= fuse_get_req(fc
, fuse_iter_npages(&ii
));
1337 fuse_put_request(fc
, req
);
1343 EXPORT_SYMBOL_GPL(fuse_direct_io
);
1345 static ssize_t
__fuse_direct_read(struct fuse_io_priv
*io
,
1346 const struct iovec
*iov
,
1347 unsigned long nr_segs
, loff_t
*ppos
,
1351 struct file
*file
= io
->file
;
1352 struct inode
*inode
= file_inode(file
);
1354 if (is_bad_inode(inode
))
1357 res
= fuse_direct_io(io
, iov
, nr_segs
, count
, ppos
, 0);
1359 fuse_invalidate_attr(inode
);
1364 static ssize_t
fuse_direct_read(struct file
*file
, char __user
*buf
,
1365 size_t count
, loff_t
*ppos
)
1367 struct fuse_io_priv io
= { .async
= 0, .file
= file
};
1368 struct iovec iov
= { .iov_base
= buf
, .iov_len
= count
};
1369 return __fuse_direct_read(&io
, &iov
, 1, ppos
, count
);
1372 static ssize_t
__fuse_direct_write(struct fuse_io_priv
*io
,
1373 const struct iovec
*iov
,
1374 unsigned long nr_segs
, loff_t
*ppos
)
1376 struct file
*file
= io
->file
;
1377 struct inode
*inode
= file_inode(file
);
1378 size_t count
= iov_length(iov
, nr_segs
);
1381 res
= generic_write_checks(file
, ppos
, &count
, 0);
1383 res
= fuse_direct_io(io
, iov
, nr_segs
, count
, ppos
, 1);
1385 fuse_invalidate_attr(inode
);
1390 static ssize_t
fuse_direct_write(struct file
*file
, const char __user
*buf
,
1391 size_t count
, loff_t
*ppos
)
1393 struct iovec iov
= { .iov_base
= (void __user
*)buf
, .iov_len
= count
};
1394 struct inode
*inode
= file_inode(file
);
1396 struct fuse_io_priv io
= { .async
= 0, .file
= file
};
1398 if (is_bad_inode(inode
))
1401 /* Don't allow parallel writes to the same file */
1402 mutex_lock(&inode
->i_mutex
);
1403 res
= __fuse_direct_write(&io
, &iov
, 1, ppos
);
1405 fuse_write_update_size(inode
, *ppos
);
1406 mutex_unlock(&inode
->i_mutex
);
1411 static void fuse_writepage_free(struct fuse_conn
*fc
, struct fuse_req
*req
)
1415 for (i
= 0; i
< req
->num_pages
; i
++)
1416 __free_page(req
->pages
[i
]);
1419 fuse_file_put(req
->ff
, false);
1422 static void fuse_writepage_finish(struct fuse_conn
*fc
, struct fuse_req
*req
)
1424 struct inode
*inode
= req
->inode
;
1425 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1426 struct backing_dev_info
*bdi
= inode
->i_mapping
->backing_dev_info
;
1429 list_del(&req
->writepages_entry
);
1430 for (i
= 0; i
< req
->num_pages
; i
++) {
1431 dec_bdi_stat(bdi
, BDI_WRITEBACK
);
1432 dec_zone_page_state(req
->pages
[i
], NR_WRITEBACK_TEMP
);
1433 bdi_writeout_inc(bdi
);
1435 wake_up(&fi
->page_waitq
);
1438 /* Called under fc->lock, may release and reacquire it */
1439 static void fuse_send_writepage(struct fuse_conn
*fc
, struct fuse_req
*req
)
1440 __releases(fc
->lock
)
1441 __acquires(fc
->lock
)
1443 struct fuse_inode
*fi
= get_fuse_inode(req
->inode
);
1444 loff_t size
= i_size_read(req
->inode
);
1445 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
1446 __u64 data_size
= req
->num_pages
* PAGE_CACHE_SIZE
;
1451 if (inarg
->offset
+ data_size
<= size
) {
1452 inarg
->size
= data_size
;
1453 } else if (inarg
->offset
< size
) {
1454 inarg
->size
= size
- inarg
->offset
;
1456 /* Got truncated off completely */
1460 req
->in
.args
[1].size
= inarg
->size
;
1462 fuse_request_send_background_locked(fc
, req
);
1466 fuse_writepage_finish(fc
, req
);
1467 spin_unlock(&fc
->lock
);
1468 fuse_writepage_free(fc
, req
);
1469 fuse_put_request(fc
, req
);
1470 spin_lock(&fc
->lock
);
1474 * If fi->writectr is positive (no truncate or fsync going on) send
1475 * all queued writepage requests.
1477 * Called with fc->lock
1479 void fuse_flush_writepages(struct inode
*inode
)
1480 __releases(fc
->lock
)
1481 __acquires(fc
->lock
)
1483 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1484 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1485 struct fuse_req
*req
;
1487 while (fi
->writectr
>= 0 && !list_empty(&fi
->queued_writes
)) {
1488 req
= list_entry(fi
->queued_writes
.next
, struct fuse_req
, list
);
1489 list_del_init(&req
->list
);
1490 fuse_send_writepage(fc
, req
);
1494 static void fuse_writepage_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
1496 struct inode
*inode
= req
->inode
;
1497 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1499 mapping_set_error(inode
->i_mapping
, req
->out
.h
.error
);
1500 spin_lock(&fc
->lock
);
1501 while (req
->misc
.write
.next
) {
1502 struct fuse_req
*next
= req
->misc
.write
.next
;
1503 req
->misc
.write
.next
= next
->misc
.write
.next
;
1504 next
->misc
.write
.next
= NULL
;
1505 list_add(&next
->writepages_entry
, &fi
->writepages
);
1506 list_add_tail(&next
->list
, &fi
->queued_writes
);
1507 fuse_flush_writepages(inode
);
1510 fuse_writepage_finish(fc
, req
);
1511 spin_unlock(&fc
->lock
);
1512 fuse_writepage_free(fc
, req
);
1515 static struct fuse_file
*fuse_write_file_get(struct fuse_conn
*fc
,
1516 struct fuse_inode
*fi
)
1518 struct fuse_file
*ff
= NULL
;
1520 spin_lock(&fc
->lock
);
1521 if (!WARN_ON(list_empty(&fi
->write_files
))) {
1522 ff
= list_entry(fi
->write_files
.next
, struct fuse_file
,
1526 spin_unlock(&fc
->lock
);
1531 static int fuse_writepage_locked(struct page
*page
)
1533 struct address_space
*mapping
= page
->mapping
;
1534 struct inode
*inode
= mapping
->host
;
1535 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1536 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1537 struct fuse_req
*req
;
1538 struct page
*tmp_page
;
1539 int error
= -ENOMEM
;
1541 set_page_writeback(page
);
1543 req
= fuse_request_alloc_nofs(1);
1547 req
->background
= 1; /* writeback always goes to bg_queue */
1548 tmp_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
1553 req
->ff
= fuse_write_file_get(fc
, fi
);
1557 fuse_write_fill(req
, req
->ff
, page_offset(page
), 0);
1559 copy_highpage(tmp_page
, page
);
1560 req
->misc
.write
.in
.write_flags
|= FUSE_WRITE_CACHE
;
1561 req
->misc
.write
.next
= NULL
;
1562 req
->in
.argpages
= 1;
1564 req
->pages
[0] = tmp_page
;
1565 req
->page_descs
[0].offset
= 0;
1566 req
->page_descs
[0].length
= PAGE_SIZE
;
1567 req
->end
= fuse_writepage_end
;
1570 inc_bdi_stat(mapping
->backing_dev_info
, BDI_WRITEBACK
);
1571 inc_zone_page_state(tmp_page
, NR_WRITEBACK_TEMP
);
1573 spin_lock(&fc
->lock
);
1574 list_add(&req
->writepages_entry
, &fi
->writepages
);
1575 list_add_tail(&req
->list
, &fi
->queued_writes
);
1576 fuse_flush_writepages(inode
);
1577 spin_unlock(&fc
->lock
);
1579 end_page_writeback(page
);
1584 fuse_request_free(req
);
1586 end_page_writeback(page
);
1590 static int fuse_writepage(struct page
*page
, struct writeback_control
*wbc
)
1594 err
= fuse_writepage_locked(page
);
1600 struct fuse_fill_wb_data
{
1601 struct fuse_req
*req
;
1602 struct fuse_file
*ff
;
1603 struct inode
*inode
;
1604 struct page
**orig_pages
;
1607 static void fuse_writepages_send(struct fuse_fill_wb_data
*data
)
1609 struct fuse_req
*req
= data
->req
;
1610 struct inode
*inode
= data
->inode
;
1611 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1612 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1613 int num_pages
= req
->num_pages
;
1616 req
->ff
= fuse_file_get(data
->ff
);
1617 spin_lock(&fc
->lock
);
1618 list_add_tail(&req
->list
, &fi
->queued_writes
);
1619 fuse_flush_writepages(inode
);
1620 spin_unlock(&fc
->lock
);
1622 for (i
= 0; i
< num_pages
; i
++)
1623 end_page_writeback(data
->orig_pages
[i
]);
1626 static bool fuse_writepage_in_flight(struct fuse_req
*new_req
,
1629 struct fuse_conn
*fc
= get_fuse_conn(new_req
->inode
);
1630 struct fuse_inode
*fi
= get_fuse_inode(new_req
->inode
);
1631 struct fuse_req
*tmp
;
1632 struct fuse_req
*old_req
;
1636 BUG_ON(new_req
->num_pages
!= 0);
1638 spin_lock(&fc
->lock
);
1639 list_del(&new_req
->writepages_entry
);
1640 new_req
->num_pages
= 1;
1641 list_for_each_entry(old_req
, &fi
->writepages
, writepages_entry
) {
1642 BUG_ON(old_req
->inode
!= new_req
->inode
);
1643 curr_index
= old_req
->misc
.write
.in
.offset
>> PAGE_CACHE_SHIFT
;
1644 if (curr_index
<= page
->index
&&
1645 page
->index
< curr_index
+ old_req
->num_pages
) {
1653 for (tmp
= old_req
; tmp
!= NULL
; tmp
= tmp
->misc
.write
.next
) {
1654 BUG_ON(tmp
->inode
!= new_req
->inode
);
1655 curr_index
= tmp
->misc
.write
.in
.offset
>> PAGE_CACHE_SHIFT
;
1656 if (tmp
->num_pages
== 1 &&
1657 curr_index
== page
->index
) {
1662 if (old_req
->num_pages
== 1 && (old_req
->state
== FUSE_REQ_INIT
||
1663 old_req
->state
== FUSE_REQ_PENDING
)) {
1664 copy_highpage(old_req
->pages
[0], page
);
1665 spin_unlock(&fc
->lock
);
1667 dec_bdi_stat(page
->mapping
->backing_dev_info
, BDI_WRITEBACK
);
1668 dec_zone_page_state(page
, NR_WRITEBACK_TEMP
);
1669 fuse_writepage_free(fc
, new_req
);
1670 fuse_request_free(new_req
);
1673 new_req
->misc
.write
.next
= old_req
->misc
.write
.next
;
1674 old_req
->misc
.write
.next
= new_req
;
1677 spin_unlock(&fc
->lock
);
1682 static int fuse_writepages_fill(struct page
*page
,
1683 struct writeback_control
*wbc
, void *_data
)
1685 struct fuse_fill_wb_data
*data
= _data
;
1686 struct fuse_req
*req
= data
->req
;
1687 struct inode
*inode
= data
->inode
;
1688 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1689 struct page
*tmp_page
;
1695 data
->ff
= fuse_write_file_get(fc
, get_fuse_inode(inode
));
1701 * Being under writeback is unlikely but possible. For example direct
1702 * read to an mmaped fuse file will set the page dirty twice; once when
1703 * the pages are faulted with get_user_pages(), and then after the read
1706 is_writeback
= fuse_page_is_writeback(inode
, page
->index
);
1708 if (req
&& req
->num_pages
&&
1709 (is_writeback
|| req
->num_pages
== FUSE_MAX_PAGES_PER_REQ
||
1710 (req
->num_pages
+ 1) * PAGE_CACHE_SIZE
> fc
->max_write
||
1711 data
->orig_pages
[req
->num_pages
- 1]->index
+ 1 != page
->index
)) {
1712 fuse_writepages_send(data
);
1716 tmp_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
1721 * The page must not be redirtied until the writeout is completed
1722 * (i.e. userspace has sent a reply to the write request). Otherwise
1723 * there could be more than one temporary page instance for each real
1726 * This is ensured by holding the page lock in page_mkwrite() while
1727 * checking fuse_page_is_writeback(). We already hold the page lock
1728 * since clear_page_dirty_for_io() and keep it held until we add the
1729 * request to the fi->writepages list and increment req->num_pages.
1730 * After this fuse_page_is_writeback() will indicate that the page is
1731 * under writeback, so we can release the page lock.
1733 if (data
->req
== NULL
) {
1734 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1737 req
= fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ
);
1739 __free_page(tmp_page
);
1743 fuse_write_fill(req
, data
->ff
, page_offset(page
), 0);
1744 req
->misc
.write
.in
.write_flags
|= FUSE_WRITE_CACHE
;
1745 req
->misc
.write
.next
= NULL
;
1746 req
->in
.argpages
= 1;
1747 req
->background
= 1;
1749 req
->end
= fuse_writepage_end
;
1752 spin_lock(&fc
->lock
);
1753 list_add(&req
->writepages_entry
, &fi
->writepages
);
1754 spin_unlock(&fc
->lock
);
1758 set_page_writeback(page
);
1760 copy_highpage(tmp_page
, page
);
1761 req
->pages
[req
->num_pages
] = tmp_page
;
1762 req
->page_descs
[req
->num_pages
].offset
= 0;
1763 req
->page_descs
[req
->num_pages
].length
= PAGE_SIZE
;
1765 inc_bdi_stat(page
->mapping
->backing_dev_info
, BDI_WRITEBACK
);
1766 inc_zone_page_state(tmp_page
, NR_WRITEBACK_TEMP
);
1769 if (is_writeback
&& fuse_writepage_in_flight(req
, page
)) {
1770 end_page_writeback(page
);
1774 data
->orig_pages
[req
->num_pages
] = page
;
1777 * Protected by fc->lock against concurrent access by
1778 * fuse_page_is_writeback().
1780 spin_lock(&fc
->lock
);
1782 spin_unlock(&fc
->lock
);
1790 static int fuse_writepages(struct address_space
*mapping
,
1791 struct writeback_control
*wbc
)
1793 struct inode
*inode
= mapping
->host
;
1794 struct fuse_fill_wb_data data
;
1798 if (is_bad_inode(inode
))
1806 data
.orig_pages
= kzalloc(sizeof(struct page
*) *
1807 FUSE_MAX_PAGES_PER_REQ
,
1809 if (!data
.orig_pages
)
1812 err
= write_cache_pages(mapping
, wbc
, fuse_writepages_fill
, &data
);
1814 /* Ignore errors if we can write at least one page */
1815 BUG_ON(!data
.req
->num_pages
);
1816 fuse_writepages_send(&data
);
1820 fuse_file_put(data
.ff
, false);
1822 kfree(data
.orig_pages
);
1827 static int fuse_launder_page(struct page
*page
)
1830 if (clear_page_dirty_for_io(page
)) {
1831 struct inode
*inode
= page
->mapping
->host
;
1832 err
= fuse_writepage_locked(page
);
1834 fuse_wait_on_page_writeback(inode
, page
->index
);
1840 * Write back dirty pages now, because there may not be any suitable
1843 static void fuse_vma_close(struct vm_area_struct
*vma
)
1845 filemap_write_and_wait(vma
->vm_file
->f_mapping
);
1849 * Wait for writeback against this page to complete before allowing it
1850 * to be marked dirty again, and hence written back again, possibly
1851 * before the previous writepage completed.
1853 * Block here, instead of in ->writepage(), so that the userspace fs
1854 * can only block processes actually operating on the filesystem.
1856 * Otherwise unprivileged userspace fs would be able to block
1861 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
1863 static int fuse_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1865 struct page
*page
= vmf
->page
;
1866 struct inode
*inode
= file_inode(vma
->vm_file
);
1868 file_update_time(vma
->vm_file
);
1870 if (page
->mapping
!= inode
->i_mapping
) {
1872 return VM_FAULT_NOPAGE
;
1875 fuse_wait_on_page_writeback(inode
, page
->index
);
1876 return VM_FAULT_LOCKED
;
1879 static const struct vm_operations_struct fuse_file_vm_ops
= {
1880 .close
= fuse_vma_close
,
1881 .fault
= filemap_fault
,
1882 .page_mkwrite
= fuse_page_mkwrite
,
1883 .remap_pages
= generic_file_remap_pages
,
1886 static int fuse_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1888 if ((vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
)) {
1889 struct inode
*inode
= file_inode(file
);
1890 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1891 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1892 struct fuse_file
*ff
= file
->private_data
;
1894 * file may be written through mmap, so chain it onto the
1895 * inodes's write_file list
1897 spin_lock(&fc
->lock
);
1898 if (list_empty(&ff
->write_entry
))
1899 list_add(&ff
->write_entry
, &fi
->write_files
);
1900 spin_unlock(&fc
->lock
);
1902 file_accessed(file
);
1903 vma
->vm_ops
= &fuse_file_vm_ops
;
1907 static int fuse_direct_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1909 /* Can't provide the coherency needed for MAP_SHARED */
1910 if (vma
->vm_flags
& VM_MAYSHARE
)
1913 invalidate_inode_pages2(file
->f_mapping
);
1915 return generic_file_mmap(file
, vma
);
1918 static int convert_fuse_file_lock(const struct fuse_file_lock
*ffl
,
1919 struct file_lock
*fl
)
1921 switch (ffl
->type
) {
1927 if (ffl
->start
> OFFSET_MAX
|| ffl
->end
> OFFSET_MAX
||
1928 ffl
->end
< ffl
->start
)
1931 fl
->fl_start
= ffl
->start
;
1932 fl
->fl_end
= ffl
->end
;
1933 fl
->fl_pid
= ffl
->pid
;
1939 fl
->fl_type
= ffl
->type
;
1943 static void fuse_lk_fill(struct fuse_req
*req
, struct file
*file
,
1944 const struct file_lock
*fl
, int opcode
, pid_t pid
,
1947 struct inode
*inode
= file_inode(file
);
1948 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1949 struct fuse_file
*ff
= file
->private_data
;
1950 struct fuse_lk_in
*arg
= &req
->misc
.lk_in
;
1953 arg
->owner
= fuse_lock_owner_id(fc
, fl
->fl_owner
);
1954 arg
->lk
.start
= fl
->fl_start
;
1955 arg
->lk
.end
= fl
->fl_end
;
1956 arg
->lk
.type
= fl
->fl_type
;
1959 arg
->lk_flags
|= FUSE_LK_FLOCK
;
1960 req
->in
.h
.opcode
= opcode
;
1961 req
->in
.h
.nodeid
= get_node_id(inode
);
1962 req
->in
.numargs
= 1;
1963 req
->in
.args
[0].size
= sizeof(*arg
);
1964 req
->in
.args
[0].value
= arg
;
1967 static int fuse_getlk(struct file
*file
, struct file_lock
*fl
)
1969 struct inode
*inode
= file_inode(file
);
1970 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1971 struct fuse_req
*req
;
1972 struct fuse_lk_out outarg
;
1975 req
= fuse_get_req_nopages(fc
);
1977 return PTR_ERR(req
);
1979 fuse_lk_fill(req
, file
, fl
, FUSE_GETLK
, 0, 0);
1980 req
->out
.numargs
= 1;
1981 req
->out
.args
[0].size
= sizeof(outarg
);
1982 req
->out
.args
[0].value
= &outarg
;
1983 fuse_request_send(fc
, req
);
1984 err
= req
->out
.h
.error
;
1985 fuse_put_request(fc
, req
);
1987 err
= convert_fuse_file_lock(&outarg
.lk
, fl
);
1992 static int fuse_setlk(struct file
*file
, struct file_lock
*fl
, int flock
)
1994 struct inode
*inode
= file_inode(file
);
1995 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1996 struct fuse_req
*req
;
1997 int opcode
= (fl
->fl_flags
& FL_SLEEP
) ? FUSE_SETLKW
: FUSE_SETLK
;
1998 pid_t pid
= fl
->fl_type
!= F_UNLCK
? current
->tgid
: 0;
2001 if (fl
->fl_lmops
&& fl
->fl_lmops
->lm_grant
) {
2002 /* NLM needs asynchronous locks, which we don't support yet */
2006 /* Unlock on close is handled by the flush method */
2007 if (fl
->fl_flags
& FL_CLOSE
)
2010 req
= fuse_get_req_nopages(fc
);
2012 return PTR_ERR(req
);
2014 fuse_lk_fill(req
, file
, fl
, opcode
, pid
, flock
);
2015 fuse_request_send(fc
, req
);
2016 err
= req
->out
.h
.error
;
2017 /* locking is restartable */
2020 fuse_put_request(fc
, req
);
2024 static int fuse_file_lock(struct file
*file
, int cmd
, struct file_lock
*fl
)
2026 struct inode
*inode
= file_inode(file
);
2027 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2030 if (cmd
== F_CANCELLK
) {
2032 } else if (cmd
== F_GETLK
) {
2034 posix_test_lock(file
, fl
);
2037 err
= fuse_getlk(file
, fl
);
2040 err
= posix_lock_file(file
, fl
, NULL
);
2042 err
= fuse_setlk(file
, fl
, 0);
2047 static int fuse_file_flock(struct file
*file
, int cmd
, struct file_lock
*fl
)
2049 struct inode
*inode
= file_inode(file
);
2050 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2054 err
= flock_lock_file_wait(file
, fl
);
2056 struct fuse_file
*ff
= file
->private_data
;
2058 /* emulate flock with POSIX locks */
2059 fl
->fl_owner
= (fl_owner_t
) file
;
2061 err
= fuse_setlk(file
, fl
, 1);
2067 static sector_t
fuse_bmap(struct address_space
*mapping
, sector_t block
)
2069 struct inode
*inode
= mapping
->host
;
2070 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2071 struct fuse_req
*req
;
2072 struct fuse_bmap_in inarg
;
2073 struct fuse_bmap_out outarg
;
2076 if (!inode
->i_sb
->s_bdev
|| fc
->no_bmap
)
2079 req
= fuse_get_req_nopages(fc
);
2083 memset(&inarg
, 0, sizeof(inarg
));
2084 inarg
.block
= block
;
2085 inarg
.blocksize
= inode
->i_sb
->s_blocksize
;
2086 req
->in
.h
.opcode
= FUSE_BMAP
;
2087 req
->in
.h
.nodeid
= get_node_id(inode
);
2088 req
->in
.numargs
= 1;
2089 req
->in
.args
[0].size
= sizeof(inarg
);
2090 req
->in
.args
[0].value
= &inarg
;
2091 req
->out
.numargs
= 1;
2092 req
->out
.args
[0].size
= sizeof(outarg
);
2093 req
->out
.args
[0].value
= &outarg
;
2094 fuse_request_send(fc
, req
);
2095 err
= req
->out
.h
.error
;
2096 fuse_put_request(fc
, req
);
2100 return err
? 0 : outarg
.block
;
2103 static loff_t
fuse_file_llseek(struct file
*file
, loff_t offset
, int whence
)
2106 struct inode
*inode
= file_inode(file
);
2108 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2109 if (whence
== SEEK_CUR
|| whence
== SEEK_SET
)
2110 return generic_file_llseek(file
, offset
, whence
);
2112 mutex_lock(&inode
->i_mutex
);
2113 retval
= fuse_update_attributes(inode
, NULL
, file
, NULL
);
2115 retval
= generic_file_llseek(file
, offset
, whence
);
2116 mutex_unlock(&inode
->i_mutex
);
2121 static int fuse_ioctl_copy_user(struct page
**pages
, struct iovec
*iov
,
2122 unsigned int nr_segs
, size_t bytes
, bool to_user
)
2130 iov_iter_init(&ii
, iov
, nr_segs
, bytes
, 0);
2132 while (iov_iter_count(&ii
)) {
2133 struct page
*page
= pages
[page_idx
++];
2134 size_t todo
= min_t(size_t, PAGE_SIZE
, iov_iter_count(&ii
));
2140 char __user
*uaddr
= ii
.iov
->iov_base
+ ii
.iov_offset
;
2141 size_t iov_len
= ii
.iov
->iov_len
- ii
.iov_offset
;
2142 size_t copy
= min(todo
, iov_len
);
2146 left
= copy_from_user(kaddr
, uaddr
, copy
);
2148 left
= copy_to_user(uaddr
, kaddr
, copy
);
2153 iov_iter_advance(&ii
, copy
);
2165 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2166 * ABI was defined to be 'struct iovec' which is different on 32bit
2167 * and 64bit. Fortunately we can determine which structure the server
2168 * used from the size of the reply.
2170 static int fuse_copy_ioctl_iovec_old(struct iovec
*dst
, void *src
,
2171 size_t transferred
, unsigned count
,
2174 #ifdef CONFIG_COMPAT
2175 if (count
* sizeof(struct compat_iovec
) == transferred
) {
2176 struct compat_iovec
*ciov
= src
;
2180 * With this interface a 32bit server cannot support
2181 * non-compat (i.e. ones coming from 64bit apps) ioctl
2187 for (i
= 0; i
< count
; i
++) {
2188 dst
[i
].iov_base
= compat_ptr(ciov
[i
].iov_base
);
2189 dst
[i
].iov_len
= ciov
[i
].iov_len
;
2195 if (count
* sizeof(struct iovec
) != transferred
)
2198 memcpy(dst
, src
, transferred
);
2202 /* Make sure iov_length() won't overflow */
2203 static int fuse_verify_ioctl_iov(struct iovec
*iov
, size_t count
)
2206 u32 max
= FUSE_MAX_PAGES_PER_REQ
<< PAGE_SHIFT
;
2208 for (n
= 0; n
< count
; n
++, iov
++) {
2209 if (iov
->iov_len
> (size_t) max
)
2211 max
-= iov
->iov_len
;
2216 static int fuse_copy_ioctl_iovec(struct fuse_conn
*fc
, struct iovec
*dst
,
2217 void *src
, size_t transferred
, unsigned count
,
2221 struct fuse_ioctl_iovec
*fiov
= src
;
2223 if (fc
->minor
< 16) {
2224 return fuse_copy_ioctl_iovec_old(dst
, src
, transferred
,
2228 if (count
* sizeof(struct fuse_ioctl_iovec
) != transferred
)
2231 for (i
= 0; i
< count
; i
++) {
2232 /* Did the server supply an inappropriate value? */
2233 if (fiov
[i
].base
!= (unsigned long) fiov
[i
].base
||
2234 fiov
[i
].len
!= (unsigned long) fiov
[i
].len
)
2237 dst
[i
].iov_base
= (void __user
*) (unsigned long) fiov
[i
].base
;
2238 dst
[i
].iov_len
= (size_t) fiov
[i
].len
;
2240 #ifdef CONFIG_COMPAT
2242 (ptr_to_compat(dst
[i
].iov_base
) != fiov
[i
].base
||
2243 (compat_size_t
) dst
[i
].iov_len
!= fiov
[i
].len
))
2253 * For ioctls, there is no generic way to determine how much memory
2254 * needs to be read and/or written. Furthermore, ioctls are allowed
2255 * to dereference the passed pointer, so the parameter requires deep
2256 * copying but FUSE has no idea whatsoever about what to copy in or
2259 * This is solved by allowing FUSE server to retry ioctl with
2260 * necessary in/out iovecs. Let's assume the ioctl implementation
2261 * needs to read in the following structure.
2268 * On the first callout to FUSE server, inarg->in_size and
2269 * inarg->out_size will be NULL; then, the server completes the ioctl
2270 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2271 * the actual iov array to
2273 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2275 * which tells FUSE to copy in the requested area and retry the ioctl.
2276 * On the second round, the server has access to the structure and
2277 * from that it can tell what to look for next, so on the invocation,
2278 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2280 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2281 * { .iov_base = a.buf, .iov_len = a.buflen } }
2283 * FUSE will copy both struct a and the pointed buffer from the
2284 * process doing the ioctl and retry ioctl with both struct a and the
2287 * This time, FUSE server has everything it needs and completes ioctl
2288 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2290 * Copying data out works the same way.
2292 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2293 * automatically initializes in and out iovs by decoding @cmd with
2294 * _IOC_* macros and the server is not allowed to request RETRY. This
2295 * limits ioctl data transfers to well-formed ioctls and is the forced
2296 * behavior for all FUSE servers.
2298 long fuse_do_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
,
2301 struct fuse_file
*ff
= file
->private_data
;
2302 struct fuse_conn
*fc
= ff
->fc
;
2303 struct fuse_ioctl_in inarg
= {
2309 struct fuse_ioctl_out outarg
;
2310 struct fuse_req
*req
= NULL
;
2311 struct page
**pages
= NULL
;
2312 struct iovec
*iov_page
= NULL
;
2313 struct iovec
*in_iov
= NULL
, *out_iov
= NULL
;
2314 unsigned int in_iovs
= 0, out_iovs
= 0, num_pages
= 0, max_pages
;
2315 size_t in_size
, out_size
, transferred
;
2318 #if BITS_PER_LONG == 32
2319 inarg
.flags
|= FUSE_IOCTL_32BIT
;
2321 if (flags
& FUSE_IOCTL_COMPAT
)
2322 inarg
.flags
|= FUSE_IOCTL_32BIT
;
2325 /* assume all the iovs returned by client always fits in a page */
2326 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec
) * FUSE_IOCTL_MAX_IOV
> PAGE_SIZE
);
2329 pages
= kcalloc(FUSE_MAX_PAGES_PER_REQ
, sizeof(pages
[0]), GFP_KERNEL
);
2330 iov_page
= (struct iovec
*) __get_free_page(GFP_KERNEL
);
2331 if (!pages
|| !iov_page
)
2335 * If restricted, initialize IO parameters as encoded in @cmd.
2336 * RETRY from server is not allowed.
2338 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
)) {
2339 struct iovec
*iov
= iov_page
;
2341 iov
->iov_base
= (void __user
*)arg
;
2342 iov
->iov_len
= _IOC_SIZE(cmd
);
2344 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
2349 if (_IOC_DIR(cmd
) & _IOC_READ
) {
2356 inarg
.in_size
= in_size
= iov_length(in_iov
, in_iovs
);
2357 inarg
.out_size
= out_size
= iov_length(out_iov
, out_iovs
);
2360 * Out data can be used either for actual out data or iovs,
2361 * make sure there always is at least one page.
2363 out_size
= max_t(size_t, out_size
, PAGE_SIZE
);
2364 max_pages
= DIV_ROUND_UP(max(in_size
, out_size
), PAGE_SIZE
);
2366 /* make sure there are enough buffer pages and init request with them */
2368 if (max_pages
> FUSE_MAX_PAGES_PER_REQ
)
2370 while (num_pages
< max_pages
) {
2371 pages
[num_pages
] = alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
2372 if (!pages
[num_pages
])
2377 req
= fuse_get_req(fc
, num_pages
);
2383 memcpy(req
->pages
, pages
, sizeof(req
->pages
[0]) * num_pages
);
2384 req
->num_pages
= num_pages
;
2385 fuse_page_descs_length_init(req
, 0, req
->num_pages
);
2387 /* okay, let's send it to the client */
2388 req
->in
.h
.opcode
= FUSE_IOCTL
;
2389 req
->in
.h
.nodeid
= ff
->nodeid
;
2390 req
->in
.numargs
= 1;
2391 req
->in
.args
[0].size
= sizeof(inarg
);
2392 req
->in
.args
[0].value
= &inarg
;
2395 req
->in
.args
[1].size
= in_size
;
2396 req
->in
.argpages
= 1;
2398 err
= fuse_ioctl_copy_user(pages
, in_iov
, in_iovs
, in_size
,
2404 req
->out
.numargs
= 2;
2405 req
->out
.args
[0].size
= sizeof(outarg
);
2406 req
->out
.args
[0].value
= &outarg
;
2407 req
->out
.args
[1].size
= out_size
;
2408 req
->out
.argpages
= 1;
2409 req
->out
.argvar
= 1;
2411 fuse_request_send(fc
, req
);
2412 err
= req
->out
.h
.error
;
2413 transferred
= req
->out
.args
[1].size
;
2414 fuse_put_request(fc
, req
);
2419 /* did it ask for retry? */
2420 if (outarg
.flags
& FUSE_IOCTL_RETRY
) {
2423 /* no retry if in restricted mode */
2425 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
))
2428 in_iovs
= outarg
.in_iovs
;
2429 out_iovs
= outarg
.out_iovs
;
2432 * Make sure things are in boundary, separate checks
2433 * are to protect against overflow.
2436 if (in_iovs
> FUSE_IOCTL_MAX_IOV
||
2437 out_iovs
> FUSE_IOCTL_MAX_IOV
||
2438 in_iovs
+ out_iovs
> FUSE_IOCTL_MAX_IOV
)
2441 vaddr
= kmap_atomic(pages
[0]);
2442 err
= fuse_copy_ioctl_iovec(fc
, iov_page
, vaddr
,
2443 transferred
, in_iovs
+ out_iovs
,
2444 (flags
& FUSE_IOCTL_COMPAT
) != 0);
2445 kunmap_atomic(vaddr
);
2450 out_iov
= in_iov
+ in_iovs
;
2452 err
= fuse_verify_ioctl_iov(in_iov
, in_iovs
);
2456 err
= fuse_verify_ioctl_iov(out_iov
, out_iovs
);
2464 if (transferred
> inarg
.out_size
)
2467 err
= fuse_ioctl_copy_user(pages
, out_iov
, out_iovs
, transferred
, true);
2470 fuse_put_request(fc
, req
);
2471 free_page((unsigned long) iov_page
);
2473 __free_page(pages
[--num_pages
]);
2476 return err
? err
: outarg
.result
;
2478 EXPORT_SYMBOL_GPL(fuse_do_ioctl
);
2480 long fuse_ioctl_common(struct file
*file
, unsigned int cmd
,
2481 unsigned long arg
, unsigned int flags
)
2483 struct inode
*inode
= file_inode(file
);
2484 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2486 if (!fuse_allow_current_process(fc
))
2489 if (is_bad_inode(inode
))
2492 return fuse_do_ioctl(file
, cmd
, arg
, flags
);
2495 static long fuse_file_ioctl(struct file
*file
, unsigned int cmd
,
2498 return fuse_ioctl_common(file
, cmd
, arg
, 0);
2501 static long fuse_file_compat_ioctl(struct file
*file
, unsigned int cmd
,
2504 return fuse_ioctl_common(file
, cmd
, arg
, FUSE_IOCTL_COMPAT
);
2508 * All files which have been polled are linked to RB tree
2509 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2510 * find the matching one.
2512 static struct rb_node
**fuse_find_polled_node(struct fuse_conn
*fc
, u64 kh
,
2513 struct rb_node
**parent_out
)
2515 struct rb_node
**link
= &fc
->polled_files
.rb_node
;
2516 struct rb_node
*last
= NULL
;
2519 struct fuse_file
*ff
;
2522 ff
= rb_entry(last
, struct fuse_file
, polled_node
);
2525 link
= &last
->rb_left
;
2526 else if (kh
> ff
->kh
)
2527 link
= &last
->rb_right
;
2538 * The file is about to be polled. Make sure it's on the polled_files
2539 * RB tree. Note that files once added to the polled_files tree are
2540 * not removed before the file is released. This is because a file
2541 * polled once is likely to be polled again.
2543 static void fuse_register_polled_file(struct fuse_conn
*fc
,
2544 struct fuse_file
*ff
)
2546 spin_lock(&fc
->lock
);
2547 if (RB_EMPTY_NODE(&ff
->polled_node
)) {
2548 struct rb_node
**link
, *parent
;
2550 link
= fuse_find_polled_node(fc
, ff
->kh
, &parent
);
2552 rb_link_node(&ff
->polled_node
, parent
, link
);
2553 rb_insert_color(&ff
->polled_node
, &fc
->polled_files
);
2555 spin_unlock(&fc
->lock
);
2558 unsigned fuse_file_poll(struct file
*file
, poll_table
*wait
)
2560 struct fuse_file
*ff
= file
->private_data
;
2561 struct fuse_conn
*fc
= ff
->fc
;
2562 struct fuse_poll_in inarg
= { .fh
= ff
->fh
, .kh
= ff
->kh
};
2563 struct fuse_poll_out outarg
;
2564 struct fuse_req
*req
;
2568 return DEFAULT_POLLMASK
;
2570 poll_wait(file
, &ff
->poll_wait
, wait
);
2571 inarg
.events
= (__u32
)poll_requested_events(wait
);
2574 * Ask for notification iff there's someone waiting for it.
2575 * The client may ignore the flag and always notify.
2577 if (waitqueue_active(&ff
->poll_wait
)) {
2578 inarg
.flags
|= FUSE_POLL_SCHEDULE_NOTIFY
;
2579 fuse_register_polled_file(fc
, ff
);
2582 req
= fuse_get_req_nopages(fc
);
2586 req
->in
.h
.opcode
= FUSE_POLL
;
2587 req
->in
.h
.nodeid
= ff
->nodeid
;
2588 req
->in
.numargs
= 1;
2589 req
->in
.args
[0].size
= sizeof(inarg
);
2590 req
->in
.args
[0].value
= &inarg
;
2591 req
->out
.numargs
= 1;
2592 req
->out
.args
[0].size
= sizeof(outarg
);
2593 req
->out
.args
[0].value
= &outarg
;
2594 fuse_request_send(fc
, req
);
2595 err
= req
->out
.h
.error
;
2596 fuse_put_request(fc
, req
);
2599 return outarg
.revents
;
2600 if (err
== -ENOSYS
) {
2602 return DEFAULT_POLLMASK
;
2606 EXPORT_SYMBOL_GPL(fuse_file_poll
);
2609 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2610 * wakes up the poll waiters.
2612 int fuse_notify_poll_wakeup(struct fuse_conn
*fc
,
2613 struct fuse_notify_poll_wakeup_out
*outarg
)
2615 u64 kh
= outarg
->kh
;
2616 struct rb_node
**link
;
2618 spin_lock(&fc
->lock
);
2620 link
= fuse_find_polled_node(fc
, kh
, NULL
);
2622 struct fuse_file
*ff
;
2624 ff
= rb_entry(*link
, struct fuse_file
, polled_node
);
2625 wake_up_interruptible_sync(&ff
->poll_wait
);
2628 spin_unlock(&fc
->lock
);
2632 static void fuse_do_truncate(struct file
*file
)
2634 struct inode
*inode
= file
->f_mapping
->host
;
2637 attr
.ia_valid
= ATTR_SIZE
;
2638 attr
.ia_size
= i_size_read(inode
);
2640 attr
.ia_file
= file
;
2641 attr
.ia_valid
|= ATTR_FILE
;
2643 fuse_do_setattr(inode
, &attr
, file
);
2646 static inline loff_t
fuse_round_up(loff_t off
)
2648 return round_up(off
, FUSE_MAX_PAGES_PER_REQ
<< PAGE_SHIFT
);
2652 fuse_direct_IO(int rw
, struct kiocb
*iocb
, const struct iovec
*iov
,
2653 loff_t offset
, unsigned long nr_segs
)
2656 struct file
*file
= iocb
->ki_filp
;
2657 struct fuse_file
*ff
= file
->private_data
;
2658 bool async_dio
= ff
->fc
->async_dio
;
2660 struct inode
*inode
;
2662 size_t count
= iov_length(iov
, nr_segs
);
2663 struct fuse_io_priv
*io
;
2666 inode
= file
->f_mapping
->host
;
2667 i_size
= i_size_read(inode
);
2669 /* optimization for short read */
2670 if (async_dio
&& rw
!= WRITE
&& offset
+ count
> i_size
) {
2671 if (offset
>= i_size
)
2673 count
= min_t(loff_t
, count
, fuse_round_up(i_size
- offset
));
2676 io
= kmalloc(sizeof(struct fuse_io_priv
), GFP_KERNEL
);
2679 spin_lock_init(&io
->lock
);
2683 io
->offset
= offset
;
2684 io
->write
= (rw
== WRITE
);
2688 * By default, we want to optimize all I/Os with async request
2689 * submission to the client filesystem if supported.
2691 io
->async
= async_dio
;
2695 * We cannot asynchronously extend the size of a file. We have no method
2696 * to wait on real async I/O requests, so we must submit this request
2699 if (!is_sync_kiocb(iocb
) && (offset
+ count
> i_size
) && rw
== WRITE
)
2703 ret
= __fuse_direct_write(io
, iov
, nr_segs
, &pos
);
2705 ret
= __fuse_direct_read(io
, iov
, nr_segs
, &pos
, count
);
2708 fuse_aio_complete(io
, ret
< 0 ? ret
: 0, -1);
2710 /* we have a non-extending, async request, so return */
2711 if (!is_sync_kiocb(iocb
))
2712 return -EIOCBQUEUED
;
2714 ret
= wait_on_sync_kiocb(iocb
);
2721 fuse_write_update_size(inode
, pos
);
2722 else if (ret
< 0 && offset
+ count
> i_size
)
2723 fuse_do_truncate(file
);
2729 static long fuse_file_fallocate(struct file
*file
, int mode
, loff_t offset
,
2732 struct fuse_file
*ff
= file
->private_data
;
2733 struct inode
*inode
= file
->f_inode
;
2734 struct fuse_inode
*fi
= get_fuse_inode(inode
);
2735 struct fuse_conn
*fc
= ff
->fc
;
2736 struct fuse_req
*req
;
2737 struct fuse_fallocate_in inarg
= {
2744 bool lock_inode
= !(mode
& FALLOC_FL_KEEP_SIZE
) ||
2745 (mode
& FALLOC_FL_PUNCH_HOLE
);
2747 if (fc
->no_fallocate
)
2751 mutex_lock(&inode
->i_mutex
);
2752 if (mode
& FALLOC_FL_PUNCH_HOLE
) {
2753 loff_t endbyte
= offset
+ length
- 1;
2754 err
= filemap_write_and_wait_range(inode
->i_mapping
,
2759 fuse_sync_writes(inode
);
2763 if (!(mode
& FALLOC_FL_KEEP_SIZE
))
2764 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
2766 req
= fuse_get_req_nopages(fc
);
2772 req
->in
.h
.opcode
= FUSE_FALLOCATE
;
2773 req
->in
.h
.nodeid
= ff
->nodeid
;
2774 req
->in
.numargs
= 1;
2775 req
->in
.args
[0].size
= sizeof(inarg
);
2776 req
->in
.args
[0].value
= &inarg
;
2777 fuse_request_send(fc
, req
);
2778 err
= req
->out
.h
.error
;
2779 if (err
== -ENOSYS
) {
2780 fc
->no_fallocate
= 1;
2783 fuse_put_request(fc
, req
);
2788 /* we could have extended the file */
2789 if (!(mode
& FALLOC_FL_KEEP_SIZE
))
2790 fuse_write_update_size(inode
, offset
+ length
);
2792 if (mode
& FALLOC_FL_PUNCH_HOLE
)
2793 truncate_pagecache_range(inode
, offset
, offset
+ length
- 1);
2795 fuse_invalidate_attr(inode
);
2798 if (!(mode
& FALLOC_FL_KEEP_SIZE
))
2799 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
2802 mutex_unlock(&inode
->i_mutex
);
2807 static const struct file_operations fuse_file_operations
= {
2808 .llseek
= fuse_file_llseek
,
2809 .read
= do_sync_read
,
2810 .aio_read
= fuse_file_aio_read
,
2811 .write
= do_sync_write
,
2812 .aio_write
= fuse_file_aio_write
,
2813 .mmap
= fuse_file_mmap
,
2815 .flush
= fuse_flush
,
2816 .release
= fuse_release
,
2817 .fsync
= fuse_fsync
,
2818 .lock
= fuse_file_lock
,
2819 .flock
= fuse_file_flock
,
2820 .splice_read
= generic_file_splice_read
,
2821 .unlocked_ioctl
= fuse_file_ioctl
,
2822 .compat_ioctl
= fuse_file_compat_ioctl
,
2823 .poll
= fuse_file_poll
,
2824 .fallocate
= fuse_file_fallocate
,
2827 static const struct file_operations fuse_direct_io_file_operations
= {
2828 .llseek
= fuse_file_llseek
,
2829 .read
= fuse_direct_read
,
2830 .write
= fuse_direct_write
,
2831 .mmap
= fuse_direct_mmap
,
2833 .flush
= fuse_flush
,
2834 .release
= fuse_release
,
2835 .fsync
= fuse_fsync
,
2836 .lock
= fuse_file_lock
,
2837 .flock
= fuse_file_flock
,
2838 .unlocked_ioctl
= fuse_file_ioctl
,
2839 .compat_ioctl
= fuse_file_compat_ioctl
,
2840 .poll
= fuse_file_poll
,
2841 .fallocate
= fuse_file_fallocate
,
2842 /* no splice_read */
2845 static const struct address_space_operations fuse_file_aops
= {
2846 .readpage
= fuse_readpage
,
2847 .writepage
= fuse_writepage
,
2848 .writepages
= fuse_writepages
,
2849 .launder_page
= fuse_launder_page
,
2850 .readpages
= fuse_readpages
,
2851 .set_page_dirty
= __set_page_dirty_nobuffers
,
2853 .direct_IO
= fuse_direct_IO
,
2856 void fuse_init_file_inode(struct inode
*inode
)
2858 inode
->i_fop
= &fuse_file_operations
;
2859 inode
->i_data
.a_ops
= &fuse_file_aops
;