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
) {
342 spin_unlock(&fc
->lock
);
348 * Wait for page writeback to be completed.
350 * Since fuse doesn't rely on the VM writeback tracking, this has to
351 * use some other means.
353 static int fuse_wait_on_page_writeback(struct inode
*inode
, pgoff_t index
)
355 struct fuse_inode
*fi
= get_fuse_inode(inode
);
357 wait_event(fi
->page_waitq
, !fuse_page_is_writeback(inode
, index
));
361 static int fuse_flush(struct file
*file
, fl_owner_t id
)
363 struct inode
*inode
= file_inode(file
);
364 struct fuse_conn
*fc
= get_fuse_conn(inode
);
365 struct fuse_file
*ff
= file
->private_data
;
366 struct fuse_req
*req
;
367 struct fuse_flush_in inarg
;
370 if (is_bad_inode(inode
))
376 req
= fuse_get_req_nofail_nopages(fc
, file
);
377 memset(&inarg
, 0, sizeof(inarg
));
379 inarg
.lock_owner
= fuse_lock_owner_id(fc
, id
);
380 req
->in
.h
.opcode
= FUSE_FLUSH
;
381 req
->in
.h
.nodeid
= get_node_id(inode
);
383 req
->in
.args
[0].size
= sizeof(inarg
);
384 req
->in
.args
[0].value
= &inarg
;
386 fuse_request_send(fc
, req
);
387 err
= req
->out
.h
.error
;
388 fuse_put_request(fc
, req
);
389 if (err
== -ENOSYS
) {
397 * Wait for all pending writepages on the inode to finish.
399 * This is currently done by blocking further writes with FUSE_NOWRITE
400 * and waiting for all sent writes to complete.
402 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
403 * could conflict with truncation.
405 static void fuse_sync_writes(struct inode
*inode
)
407 fuse_set_nowrite(inode
);
408 fuse_release_nowrite(inode
);
411 int fuse_fsync_common(struct file
*file
, loff_t start
, loff_t end
,
412 int datasync
, int isdir
)
414 struct inode
*inode
= file
->f_mapping
->host
;
415 struct fuse_conn
*fc
= get_fuse_conn(inode
);
416 struct fuse_file
*ff
= file
->private_data
;
417 struct fuse_req
*req
;
418 struct fuse_fsync_in inarg
;
421 if (is_bad_inode(inode
))
424 err
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
428 if ((!isdir
&& fc
->no_fsync
) || (isdir
&& fc
->no_fsyncdir
))
431 mutex_lock(&inode
->i_mutex
);
434 * Start writeback against all dirty pages of the inode, then
435 * wait for all outstanding writes, before sending the FSYNC
438 err
= write_inode_now(inode
, 0);
442 fuse_sync_writes(inode
);
444 req
= fuse_get_req_nopages(fc
);
450 memset(&inarg
, 0, sizeof(inarg
));
452 inarg
.fsync_flags
= datasync
? 1 : 0;
453 req
->in
.h
.opcode
= isdir
? FUSE_FSYNCDIR
: FUSE_FSYNC
;
454 req
->in
.h
.nodeid
= get_node_id(inode
);
456 req
->in
.args
[0].size
= sizeof(inarg
);
457 req
->in
.args
[0].value
= &inarg
;
458 fuse_request_send(fc
, req
);
459 err
= req
->out
.h
.error
;
460 fuse_put_request(fc
, req
);
461 if (err
== -ENOSYS
) {
469 mutex_unlock(&inode
->i_mutex
);
473 static int fuse_fsync(struct file
*file
, loff_t start
, loff_t end
,
476 return fuse_fsync_common(file
, start
, end
, datasync
, 0);
479 void fuse_read_fill(struct fuse_req
*req
, struct file
*file
, loff_t pos
,
480 size_t count
, int opcode
)
482 struct fuse_read_in
*inarg
= &req
->misc
.read
.in
;
483 struct fuse_file
*ff
= file
->private_data
;
488 inarg
->flags
= file
->f_flags
;
489 req
->in
.h
.opcode
= opcode
;
490 req
->in
.h
.nodeid
= ff
->nodeid
;
492 req
->in
.args
[0].size
= sizeof(struct fuse_read_in
);
493 req
->in
.args
[0].value
= inarg
;
495 req
->out
.numargs
= 1;
496 req
->out
.args
[0].size
= count
;
499 static void fuse_release_user_pages(struct fuse_req
*req
, int write
)
503 for (i
= 0; i
< req
->num_pages
; i
++) {
504 struct page
*page
= req
->pages
[i
];
506 set_page_dirty_lock(page
);
512 * In case of short read, the caller sets 'pos' to the position of
513 * actual end of fuse request in IO request. Otherwise, if bytes_requested
514 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
517 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
518 * both submitted asynchronously. The first of them was ACKed by userspace as
519 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
520 * second request was ACKed as short, e.g. only 1K was read, resulting in
523 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
524 * will be equal to the length of the longest contiguous fragment of
525 * transferred data starting from the beginning of IO request.
527 static void fuse_aio_complete(struct fuse_io_priv
*io
, int err
, ssize_t pos
)
531 spin_lock(&io
->lock
);
533 io
->err
= io
->err
? : err
;
534 else if (pos
>= 0 && (io
->bytes
< 0 || pos
< io
->bytes
))
538 spin_unlock(&io
->lock
);
545 else if (io
->bytes
>= 0 && io
->write
)
548 res
= io
->bytes
< 0 ? io
->size
: io
->bytes
;
550 if (!is_sync_kiocb(io
->iocb
)) {
551 struct inode
*inode
= file_inode(io
->iocb
->ki_filp
);
552 struct fuse_conn
*fc
= get_fuse_conn(inode
);
553 struct fuse_inode
*fi
= get_fuse_inode(inode
);
555 spin_lock(&fc
->lock
);
556 fi
->attr_version
= ++fc
->attr_version
;
557 spin_unlock(&fc
->lock
);
561 aio_complete(io
->iocb
, res
, 0);
566 static void fuse_aio_complete_req(struct fuse_conn
*fc
, struct fuse_req
*req
)
568 struct fuse_io_priv
*io
= req
->io
;
571 fuse_release_user_pages(req
, !io
->write
);
574 if (req
->misc
.write
.in
.size
!= req
->misc
.write
.out
.size
)
575 pos
= req
->misc
.write
.in
.offset
- io
->offset
+
576 req
->misc
.write
.out
.size
;
578 if (req
->misc
.read
.in
.size
!= req
->out
.args
[0].size
)
579 pos
= req
->misc
.read
.in
.offset
- io
->offset
+
580 req
->out
.args
[0].size
;
583 fuse_aio_complete(io
, req
->out
.h
.error
, pos
);
586 static size_t fuse_async_req_send(struct fuse_conn
*fc
, struct fuse_req
*req
,
587 size_t num_bytes
, struct fuse_io_priv
*io
)
589 spin_lock(&io
->lock
);
590 io
->size
+= num_bytes
;
592 spin_unlock(&io
->lock
);
595 req
->end
= fuse_aio_complete_req
;
597 __fuse_get_request(req
);
598 fuse_request_send_background(fc
, req
);
603 static size_t fuse_send_read(struct fuse_req
*req
, struct fuse_io_priv
*io
,
604 loff_t pos
, size_t count
, fl_owner_t owner
)
606 struct file
*file
= io
->file
;
607 struct fuse_file
*ff
= file
->private_data
;
608 struct fuse_conn
*fc
= ff
->fc
;
610 fuse_read_fill(req
, file
, pos
, count
, FUSE_READ
);
612 struct fuse_read_in
*inarg
= &req
->misc
.read
.in
;
614 inarg
->read_flags
|= FUSE_READ_LOCKOWNER
;
615 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
619 return fuse_async_req_send(fc
, req
, count
, io
);
621 fuse_request_send(fc
, req
);
622 return req
->out
.args
[0].size
;
625 static void fuse_read_update_size(struct inode
*inode
, loff_t size
,
628 struct fuse_conn
*fc
= get_fuse_conn(inode
);
629 struct fuse_inode
*fi
= get_fuse_inode(inode
);
631 spin_lock(&fc
->lock
);
632 if (attr_ver
== fi
->attr_version
&& size
< inode
->i_size
&&
633 !test_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
)) {
634 fi
->attr_version
= ++fc
->attr_version
;
635 i_size_write(inode
, size
);
637 spin_unlock(&fc
->lock
);
640 static int fuse_readpage(struct file
*file
, struct page
*page
)
642 struct fuse_io_priv io
= { .async
= 0, .file
= file
};
643 struct inode
*inode
= page
->mapping
->host
;
644 struct fuse_conn
*fc
= get_fuse_conn(inode
);
645 struct fuse_req
*req
;
647 loff_t pos
= page_offset(page
);
648 size_t count
= PAGE_CACHE_SIZE
;
653 if (is_bad_inode(inode
))
657 * Page writeback can extend beyond the lifetime of the
658 * page-cache page, so make sure we read a properly synced
661 fuse_wait_on_page_writeback(inode
, page
->index
);
663 req
= fuse_get_req(fc
, 1);
668 attr_ver
= fuse_get_attr_version(fc
);
670 req
->out
.page_zeroing
= 1;
671 req
->out
.argpages
= 1;
673 req
->pages
[0] = page
;
674 req
->page_descs
[0].length
= count
;
675 num_read
= fuse_send_read(req
, &io
, pos
, count
, NULL
);
676 err
= req
->out
.h
.error
;
677 fuse_put_request(fc
, req
);
681 * Short read means EOF. If file size is larger, truncate it
683 if (num_read
< count
)
684 fuse_read_update_size(inode
, pos
+ num_read
, attr_ver
);
686 SetPageUptodate(page
);
689 fuse_invalidate_attr(inode
); /* atime changed */
695 static void fuse_readpages_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
698 size_t count
= req
->misc
.read
.in
.size
;
699 size_t num_read
= req
->out
.args
[0].size
;
700 struct address_space
*mapping
= NULL
;
702 for (i
= 0; mapping
== NULL
&& i
< req
->num_pages
; i
++)
703 mapping
= req
->pages
[i
]->mapping
;
706 struct inode
*inode
= mapping
->host
;
709 * Short read means EOF. If file size is larger, truncate it
711 if (!req
->out
.h
.error
&& num_read
< count
) {
714 pos
= page_offset(req
->pages
[0]) + num_read
;
715 fuse_read_update_size(inode
, pos
,
716 req
->misc
.read
.attr_ver
);
718 fuse_invalidate_attr(inode
); /* atime changed */
721 for (i
= 0; i
< req
->num_pages
; i
++) {
722 struct page
*page
= req
->pages
[i
];
723 if (!req
->out
.h
.error
)
724 SetPageUptodate(page
);
728 page_cache_release(page
);
731 fuse_file_put(req
->ff
, false);
734 static void fuse_send_readpages(struct fuse_req
*req
, struct file
*file
)
736 struct fuse_file
*ff
= file
->private_data
;
737 struct fuse_conn
*fc
= ff
->fc
;
738 loff_t pos
= page_offset(req
->pages
[0]);
739 size_t count
= req
->num_pages
<< PAGE_CACHE_SHIFT
;
741 req
->out
.argpages
= 1;
742 req
->out
.page_zeroing
= 1;
743 req
->out
.page_replace
= 1;
744 fuse_read_fill(req
, file
, pos
, count
, FUSE_READ
);
745 req
->misc
.read
.attr_ver
= fuse_get_attr_version(fc
);
746 if (fc
->async_read
) {
747 req
->ff
= fuse_file_get(ff
);
748 req
->end
= fuse_readpages_end
;
749 fuse_request_send_background(fc
, req
);
751 fuse_request_send(fc
, req
);
752 fuse_readpages_end(fc
, req
);
753 fuse_put_request(fc
, req
);
757 struct fuse_fill_data
{
758 struct fuse_req
*req
;
764 static int fuse_readpages_fill(void *_data
, struct page
*page
)
766 struct fuse_fill_data
*data
= _data
;
767 struct fuse_req
*req
= data
->req
;
768 struct inode
*inode
= data
->inode
;
769 struct fuse_conn
*fc
= get_fuse_conn(inode
);
771 fuse_wait_on_page_writeback(inode
, page
->index
);
773 if (req
->num_pages
&&
774 (req
->num_pages
== FUSE_MAX_PAGES_PER_REQ
||
775 (req
->num_pages
+ 1) * PAGE_CACHE_SIZE
> fc
->max_read
||
776 req
->pages
[req
->num_pages
- 1]->index
+ 1 != page
->index
)) {
777 int nr_alloc
= min_t(unsigned, data
->nr_pages
,
778 FUSE_MAX_PAGES_PER_REQ
);
779 fuse_send_readpages(req
, data
->file
);
781 req
= fuse_get_req_for_background(fc
, nr_alloc
);
783 req
= fuse_get_req(fc
, nr_alloc
);
792 if (WARN_ON(req
->num_pages
>= req
->max_pages
)) {
793 fuse_put_request(fc
, req
);
797 page_cache_get(page
);
798 req
->pages
[req
->num_pages
] = page
;
799 req
->page_descs
[req
->num_pages
].length
= PAGE_SIZE
;
805 static int fuse_readpages(struct file
*file
, struct address_space
*mapping
,
806 struct list_head
*pages
, unsigned nr_pages
)
808 struct inode
*inode
= mapping
->host
;
809 struct fuse_conn
*fc
= get_fuse_conn(inode
);
810 struct fuse_fill_data data
;
812 int nr_alloc
= min_t(unsigned, nr_pages
, FUSE_MAX_PAGES_PER_REQ
);
815 if (is_bad_inode(inode
))
821 data
.req
= fuse_get_req_for_background(fc
, nr_alloc
);
823 data
.req
= fuse_get_req(fc
, nr_alloc
);
824 data
.nr_pages
= nr_pages
;
825 err
= PTR_ERR(data
.req
);
826 if (IS_ERR(data
.req
))
829 err
= read_cache_pages(mapping
, pages
, fuse_readpages_fill
, &data
);
831 if (data
.req
->num_pages
)
832 fuse_send_readpages(data
.req
, file
);
834 fuse_put_request(fc
, data
.req
);
840 static ssize_t
fuse_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
841 unsigned long nr_segs
, loff_t pos
)
843 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
844 struct fuse_conn
*fc
= get_fuse_conn(inode
);
847 * In auto invalidate mode, always update attributes on read.
848 * Otherwise, only update if we attempt to read past EOF (to ensure
849 * i_size is up to date).
851 if (fc
->auto_inval_data
||
852 (pos
+ iov_length(iov
, nr_segs
) > i_size_read(inode
))) {
854 err
= fuse_update_attributes(inode
, NULL
, iocb
->ki_filp
, NULL
);
859 return generic_file_aio_read(iocb
, iov
, nr_segs
, pos
);
862 static void fuse_write_fill(struct fuse_req
*req
, struct fuse_file
*ff
,
863 loff_t pos
, size_t count
)
865 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
866 struct fuse_write_out
*outarg
= &req
->misc
.write
.out
;
871 req
->in
.h
.opcode
= FUSE_WRITE
;
872 req
->in
.h
.nodeid
= ff
->nodeid
;
874 if (ff
->fc
->minor
< 9)
875 req
->in
.args
[0].size
= FUSE_COMPAT_WRITE_IN_SIZE
;
877 req
->in
.args
[0].size
= sizeof(struct fuse_write_in
);
878 req
->in
.args
[0].value
= inarg
;
879 req
->in
.args
[1].size
= count
;
880 req
->out
.numargs
= 1;
881 req
->out
.args
[0].size
= sizeof(struct fuse_write_out
);
882 req
->out
.args
[0].value
= outarg
;
885 static size_t fuse_send_write(struct fuse_req
*req
, struct fuse_io_priv
*io
,
886 loff_t pos
, size_t count
, fl_owner_t owner
)
888 struct file
*file
= io
->file
;
889 struct fuse_file
*ff
= file
->private_data
;
890 struct fuse_conn
*fc
= ff
->fc
;
891 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
893 fuse_write_fill(req
, ff
, pos
, count
);
894 inarg
->flags
= file
->f_flags
;
896 inarg
->write_flags
|= FUSE_WRITE_LOCKOWNER
;
897 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
901 return fuse_async_req_send(fc
, req
, count
, io
);
903 fuse_request_send(fc
, req
);
904 return req
->misc
.write
.out
.size
;
907 void fuse_write_update_size(struct inode
*inode
, loff_t pos
)
909 struct fuse_conn
*fc
= get_fuse_conn(inode
);
910 struct fuse_inode
*fi
= get_fuse_inode(inode
);
912 spin_lock(&fc
->lock
);
913 fi
->attr_version
= ++fc
->attr_version
;
914 if (pos
> inode
->i_size
)
915 i_size_write(inode
, pos
);
916 spin_unlock(&fc
->lock
);
919 static size_t fuse_send_write_pages(struct fuse_req
*req
, struct file
*file
,
920 struct inode
*inode
, loff_t pos
,
926 struct fuse_io_priv io
= { .async
= 0, .file
= file
};
928 for (i
= 0; i
< req
->num_pages
; i
++)
929 fuse_wait_on_page_writeback(inode
, req
->pages
[i
]->index
);
931 res
= fuse_send_write(req
, &io
, pos
, count
, NULL
);
933 offset
= req
->page_descs
[0].offset
;
935 for (i
= 0; i
< req
->num_pages
; i
++) {
936 struct page
*page
= req
->pages
[i
];
938 if (!req
->out
.h
.error
&& !offset
&& count
>= PAGE_CACHE_SIZE
)
939 SetPageUptodate(page
);
941 if (count
> PAGE_CACHE_SIZE
- offset
)
942 count
-= PAGE_CACHE_SIZE
- offset
;
948 page_cache_release(page
);
954 static ssize_t
fuse_fill_write_pages(struct fuse_req
*req
,
955 struct address_space
*mapping
,
956 struct iov_iter
*ii
, loff_t pos
)
958 struct fuse_conn
*fc
= get_fuse_conn(mapping
->host
);
959 unsigned offset
= pos
& (PAGE_CACHE_SIZE
- 1);
963 req
->in
.argpages
= 1;
964 req
->page_descs
[0].offset
= offset
;
969 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
970 size_t bytes
= min_t(size_t, PAGE_CACHE_SIZE
- offset
,
973 bytes
= min_t(size_t, bytes
, fc
->max_write
- count
);
977 if (iov_iter_fault_in_readable(ii
, bytes
))
981 page
= grab_cache_page_write_begin(mapping
, index
, 0);
985 if (mapping_writably_mapped(mapping
))
986 flush_dcache_page(page
);
989 tmp
= iov_iter_copy_from_user_atomic(page
, ii
, offset
, bytes
);
991 flush_dcache_page(page
);
993 mark_page_accessed(page
);
997 page_cache_release(page
);
998 bytes
= min(bytes
, iov_iter_single_seg_count(ii
));
1003 req
->pages
[req
->num_pages
] = page
;
1004 req
->page_descs
[req
->num_pages
].length
= tmp
;
1007 iov_iter_advance(ii
, tmp
);
1011 if (offset
== PAGE_CACHE_SIZE
)
1014 if (!fc
->big_writes
)
1016 } while (iov_iter_count(ii
) && count
< fc
->max_write
&&
1017 req
->num_pages
< req
->max_pages
&& offset
== 0);
1019 return count
> 0 ? count
: err
;
1022 static inline unsigned fuse_wr_pages(loff_t pos
, size_t len
)
1024 return min_t(unsigned,
1025 ((pos
+ len
- 1) >> PAGE_CACHE_SHIFT
) -
1026 (pos
>> PAGE_CACHE_SHIFT
) + 1,
1027 FUSE_MAX_PAGES_PER_REQ
);
1030 static ssize_t
fuse_perform_write(struct file
*file
,
1031 struct address_space
*mapping
,
1032 struct iov_iter
*ii
, loff_t pos
)
1034 struct inode
*inode
= mapping
->host
;
1035 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1036 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1040 if (is_bad_inode(inode
))
1043 if (inode
->i_size
< pos
+ iov_iter_count(ii
))
1044 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1047 struct fuse_req
*req
;
1049 unsigned nr_pages
= fuse_wr_pages(pos
, iov_iter_count(ii
));
1051 req
= fuse_get_req(fc
, nr_pages
);
1057 count
= fuse_fill_write_pages(req
, mapping
, ii
, pos
);
1063 num_written
= fuse_send_write_pages(req
, file
, inode
,
1065 err
= req
->out
.h
.error
;
1070 /* break out of the loop on short write */
1071 if (num_written
!= count
)
1075 fuse_put_request(fc
, req
);
1076 } while (!err
&& iov_iter_count(ii
));
1079 fuse_write_update_size(inode
, pos
);
1081 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1082 fuse_invalidate_attr(inode
);
1084 return res
> 0 ? res
: err
;
1087 static ssize_t
fuse_file_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
1088 unsigned long nr_segs
, loff_t pos
)
1090 struct file
*file
= iocb
->ki_filp
;
1091 struct address_space
*mapping
= file
->f_mapping
;
1094 ssize_t written
= 0;
1095 ssize_t written_buffered
= 0;
1096 struct inode
*inode
= mapping
->host
;
1101 WARN_ON(iocb
->ki_pos
!= pos
);
1104 err
= generic_segment_checks(iov
, &nr_segs
, &ocount
, VERIFY_READ
);
1109 mutex_lock(&inode
->i_mutex
);
1111 /* We can write back this queue in page reclaim */
1112 current
->backing_dev_info
= mapping
->backing_dev_info
;
1114 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
1121 err
= file_remove_suid(file
);
1125 err
= file_update_time(file
);
1129 if (file
->f_flags
& O_DIRECT
) {
1130 written
= generic_file_direct_write(iocb
, iov
, &nr_segs
,
1133 if (written
< 0 || written
== count
)
1139 iov_iter_init(&i
, iov
, nr_segs
, count
, written
);
1140 written_buffered
= fuse_perform_write(file
, mapping
, &i
, pos
);
1141 if (written_buffered
< 0) {
1142 err
= written_buffered
;
1145 endbyte
= pos
+ written_buffered
- 1;
1147 err
= filemap_write_and_wait_range(file
->f_mapping
, pos
,
1152 invalidate_mapping_pages(file
->f_mapping
,
1153 pos
>> PAGE_CACHE_SHIFT
,
1154 endbyte
>> PAGE_CACHE_SHIFT
);
1156 written
+= written_buffered
;
1157 iocb
->ki_pos
= pos
+ written_buffered
;
1159 iov_iter_init(&i
, iov
, nr_segs
, count
, 0);
1160 written
= fuse_perform_write(file
, mapping
, &i
, pos
);
1162 iocb
->ki_pos
= pos
+ written
;
1165 current
->backing_dev_info
= NULL
;
1166 mutex_unlock(&inode
->i_mutex
);
1168 return written
? written
: err
;
1171 static inline void fuse_page_descs_length_init(struct fuse_req
*req
,
1172 unsigned index
, unsigned nr_pages
)
1176 for (i
= index
; i
< index
+ nr_pages
; i
++)
1177 req
->page_descs
[i
].length
= PAGE_SIZE
-
1178 req
->page_descs
[i
].offset
;
1181 static inline unsigned long fuse_get_user_addr(const struct iov_iter
*ii
)
1183 return (unsigned long)ii
->iov
->iov_base
+ ii
->iov_offset
;
1186 static inline size_t fuse_get_frag_size(const struct iov_iter
*ii
,
1189 return min(iov_iter_single_seg_count(ii
), max_size
);
1192 static int fuse_get_user_pages(struct fuse_req
*req
, struct iov_iter
*ii
,
1193 size_t *nbytesp
, int write
)
1195 size_t nbytes
= 0; /* # bytes already packed in req */
1197 /* Special case for kernel I/O: can copy directly into the buffer */
1198 if (segment_eq(get_fs(), KERNEL_DS
)) {
1199 unsigned long user_addr
= fuse_get_user_addr(ii
);
1200 size_t frag_size
= fuse_get_frag_size(ii
, *nbytesp
);
1203 req
->in
.args
[1].value
= (void *) user_addr
;
1205 req
->out
.args
[0].value
= (void *) user_addr
;
1207 iov_iter_advance(ii
, frag_size
);
1208 *nbytesp
= frag_size
;
1212 while (nbytes
< *nbytesp
&& req
->num_pages
< req
->max_pages
) {
1214 unsigned long user_addr
= fuse_get_user_addr(ii
);
1215 unsigned offset
= user_addr
& ~PAGE_MASK
;
1216 size_t frag_size
= fuse_get_frag_size(ii
, *nbytesp
- nbytes
);
1219 unsigned n
= req
->max_pages
- req
->num_pages
;
1220 frag_size
= min_t(size_t, frag_size
, n
<< PAGE_SHIFT
);
1222 npages
= (frag_size
+ offset
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1223 npages
= clamp(npages
, 1U, n
);
1225 ret
= get_user_pages_fast(user_addr
, npages
, !write
,
1226 &req
->pages
[req
->num_pages
]);
1231 frag_size
= min_t(size_t, frag_size
,
1232 (npages
<< PAGE_SHIFT
) - offset
);
1233 iov_iter_advance(ii
, frag_size
);
1235 req
->page_descs
[req
->num_pages
].offset
= offset
;
1236 fuse_page_descs_length_init(req
, req
->num_pages
, npages
);
1238 req
->num_pages
+= npages
;
1239 req
->page_descs
[req
->num_pages
- 1].length
-=
1240 (npages
<< PAGE_SHIFT
) - offset
- frag_size
;
1242 nbytes
+= frag_size
;
1246 req
->in
.argpages
= 1;
1248 req
->out
.argpages
= 1;
1255 static inline int fuse_iter_npages(const struct iov_iter
*ii_p
)
1257 struct iov_iter ii
= *ii_p
;
1260 while (iov_iter_count(&ii
) && npages
< FUSE_MAX_PAGES_PER_REQ
) {
1261 unsigned long user_addr
= fuse_get_user_addr(&ii
);
1262 unsigned offset
= user_addr
& ~PAGE_MASK
;
1263 size_t frag_size
= iov_iter_single_seg_count(&ii
);
1265 npages
+= (frag_size
+ offset
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1266 iov_iter_advance(&ii
, frag_size
);
1269 return min(npages
, FUSE_MAX_PAGES_PER_REQ
);
1272 ssize_t
fuse_direct_io(struct fuse_io_priv
*io
, const struct iovec
*iov
,
1273 unsigned long nr_segs
, size_t count
, loff_t
*ppos
,
1276 struct file
*file
= io
->file
;
1277 struct fuse_file
*ff
= file
->private_data
;
1278 struct fuse_conn
*fc
= ff
->fc
;
1279 size_t nmax
= write
? fc
->max_write
: fc
->max_read
;
1282 struct fuse_req
*req
;
1285 iov_iter_init(&ii
, iov
, nr_segs
, count
, 0);
1288 req
= fuse_get_req_for_background(fc
, fuse_iter_npages(&ii
));
1290 req
= fuse_get_req(fc
, fuse_iter_npages(&ii
));
1292 return PTR_ERR(req
);
1296 fl_owner_t owner
= current
->files
;
1297 size_t nbytes
= min(count
, nmax
);
1298 int err
= fuse_get_user_pages(req
, &ii
, &nbytes
, write
);
1305 nres
= fuse_send_write(req
, io
, pos
, nbytes
, owner
);
1307 nres
= fuse_send_read(req
, io
, pos
, nbytes
, owner
);
1310 fuse_release_user_pages(req
, !write
);
1311 if (req
->out
.h
.error
) {
1313 res
= req
->out
.h
.error
;
1315 } else if (nres
> nbytes
) {
1325 fuse_put_request(fc
, req
);
1327 req
= fuse_get_req_for_background(fc
,
1328 fuse_iter_npages(&ii
));
1330 req
= fuse_get_req(fc
, fuse_iter_npages(&ii
));
1336 fuse_put_request(fc
, req
);
1342 EXPORT_SYMBOL_GPL(fuse_direct_io
);
1344 static ssize_t
__fuse_direct_read(struct fuse_io_priv
*io
,
1345 const struct iovec
*iov
,
1346 unsigned long nr_segs
, loff_t
*ppos
,
1350 struct file
*file
= io
->file
;
1351 struct inode
*inode
= file_inode(file
);
1353 if (is_bad_inode(inode
))
1356 res
= fuse_direct_io(io
, iov
, nr_segs
, count
, ppos
, 0);
1358 fuse_invalidate_attr(inode
);
1363 static ssize_t
fuse_direct_read(struct file
*file
, char __user
*buf
,
1364 size_t count
, loff_t
*ppos
)
1366 struct fuse_io_priv io
= { .async
= 0, .file
= file
};
1367 struct iovec iov
= { .iov_base
= buf
, .iov_len
= count
};
1368 return __fuse_direct_read(&io
, &iov
, 1, ppos
, count
);
1371 static ssize_t
__fuse_direct_write(struct fuse_io_priv
*io
,
1372 const struct iovec
*iov
,
1373 unsigned long nr_segs
, loff_t
*ppos
)
1375 struct file
*file
= io
->file
;
1376 struct inode
*inode
= file_inode(file
);
1377 size_t count
= iov_length(iov
, nr_segs
);
1380 res
= generic_write_checks(file
, ppos
, &count
, 0);
1382 res
= fuse_direct_io(io
, iov
, nr_segs
, count
, ppos
, 1);
1384 fuse_invalidate_attr(inode
);
1389 static ssize_t
fuse_direct_write(struct file
*file
, const char __user
*buf
,
1390 size_t count
, loff_t
*ppos
)
1392 struct iovec iov
= { .iov_base
= (void __user
*)buf
, .iov_len
= count
};
1393 struct inode
*inode
= file_inode(file
);
1395 struct fuse_io_priv io
= { .async
= 0, .file
= file
};
1397 if (is_bad_inode(inode
))
1400 /* Don't allow parallel writes to the same file */
1401 mutex_lock(&inode
->i_mutex
);
1402 res
= __fuse_direct_write(&io
, &iov
, 1, ppos
);
1404 fuse_write_update_size(inode
, *ppos
);
1405 mutex_unlock(&inode
->i_mutex
);
1410 static void fuse_writepage_free(struct fuse_conn
*fc
, struct fuse_req
*req
)
1412 __free_page(req
->pages
[0]);
1413 fuse_file_put(req
->ff
, false);
1416 static void fuse_writepage_finish(struct fuse_conn
*fc
, struct fuse_req
*req
)
1418 struct inode
*inode
= req
->inode
;
1419 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1420 struct backing_dev_info
*bdi
= inode
->i_mapping
->backing_dev_info
;
1422 list_del(&req
->writepages_entry
);
1423 dec_bdi_stat(bdi
, BDI_WRITEBACK
);
1424 dec_zone_page_state(req
->pages
[0], NR_WRITEBACK_TEMP
);
1425 bdi_writeout_inc(bdi
);
1426 wake_up(&fi
->page_waitq
);
1429 /* Called under fc->lock, may release and reacquire it */
1430 static void fuse_send_writepage(struct fuse_conn
*fc
, struct fuse_req
*req
)
1431 __releases(fc
->lock
)
1432 __acquires(fc
->lock
)
1434 struct fuse_inode
*fi
= get_fuse_inode(req
->inode
);
1435 loff_t size
= i_size_read(req
->inode
);
1436 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
1441 if (inarg
->offset
+ PAGE_CACHE_SIZE
<= size
) {
1442 inarg
->size
= PAGE_CACHE_SIZE
;
1443 } else if (inarg
->offset
< size
) {
1444 inarg
->size
= size
& (PAGE_CACHE_SIZE
- 1);
1446 /* Got truncated off completely */
1450 req
->in
.args
[1].size
= inarg
->size
;
1452 fuse_request_send_background_locked(fc
, req
);
1456 fuse_writepage_finish(fc
, req
);
1457 spin_unlock(&fc
->lock
);
1458 fuse_writepage_free(fc
, req
);
1459 fuse_put_request(fc
, req
);
1460 spin_lock(&fc
->lock
);
1464 * If fi->writectr is positive (no truncate or fsync going on) send
1465 * all queued writepage requests.
1467 * Called with fc->lock
1469 void fuse_flush_writepages(struct inode
*inode
)
1470 __releases(fc
->lock
)
1471 __acquires(fc
->lock
)
1473 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1474 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1475 struct fuse_req
*req
;
1477 while (fi
->writectr
>= 0 && !list_empty(&fi
->queued_writes
)) {
1478 req
= list_entry(fi
->queued_writes
.next
, struct fuse_req
, list
);
1479 list_del_init(&req
->list
);
1480 fuse_send_writepage(fc
, req
);
1484 static void fuse_writepage_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
1486 struct inode
*inode
= req
->inode
;
1487 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1489 mapping_set_error(inode
->i_mapping
, req
->out
.h
.error
);
1490 spin_lock(&fc
->lock
);
1492 fuse_writepage_finish(fc
, req
);
1493 spin_unlock(&fc
->lock
);
1494 fuse_writepage_free(fc
, req
);
1497 static int fuse_writepage_locked(struct page
*page
)
1499 struct address_space
*mapping
= page
->mapping
;
1500 struct inode
*inode
= mapping
->host
;
1501 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1502 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1503 struct fuse_req
*req
;
1504 struct fuse_file
*ff
;
1505 struct page
*tmp_page
;
1507 set_page_writeback(page
);
1509 req
= fuse_request_alloc_nofs(1);
1513 req
->background
= 1; /* writeback always goes to bg_queue */
1514 tmp_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
1518 spin_lock(&fc
->lock
);
1519 BUG_ON(list_empty(&fi
->write_files
));
1520 ff
= list_entry(fi
->write_files
.next
, struct fuse_file
, write_entry
);
1521 req
->ff
= fuse_file_get(ff
);
1522 spin_unlock(&fc
->lock
);
1524 fuse_write_fill(req
, ff
, page_offset(page
), 0);
1526 copy_highpage(tmp_page
, page
);
1527 req
->misc
.write
.in
.write_flags
|= FUSE_WRITE_CACHE
;
1528 req
->in
.argpages
= 1;
1530 req
->pages
[0] = tmp_page
;
1531 req
->page_descs
[0].offset
= 0;
1532 req
->page_descs
[0].length
= PAGE_SIZE
;
1533 req
->end
= fuse_writepage_end
;
1536 inc_bdi_stat(mapping
->backing_dev_info
, BDI_WRITEBACK
);
1537 inc_zone_page_state(tmp_page
, NR_WRITEBACK_TEMP
);
1539 spin_lock(&fc
->lock
);
1540 list_add(&req
->writepages_entry
, &fi
->writepages
);
1541 list_add_tail(&req
->list
, &fi
->queued_writes
);
1542 fuse_flush_writepages(inode
);
1543 spin_unlock(&fc
->lock
);
1545 end_page_writeback(page
);
1550 fuse_request_free(req
);
1552 end_page_writeback(page
);
1556 static int fuse_writepage(struct page
*page
, struct writeback_control
*wbc
)
1560 err
= fuse_writepage_locked(page
);
1566 static int fuse_launder_page(struct page
*page
)
1569 if (clear_page_dirty_for_io(page
)) {
1570 struct inode
*inode
= page
->mapping
->host
;
1571 err
= fuse_writepage_locked(page
);
1573 fuse_wait_on_page_writeback(inode
, page
->index
);
1579 * Write back dirty pages now, because there may not be any suitable
1582 static void fuse_vma_close(struct vm_area_struct
*vma
)
1584 filemap_write_and_wait(vma
->vm_file
->f_mapping
);
1588 * Wait for writeback against this page to complete before allowing it
1589 * to be marked dirty again, and hence written back again, possibly
1590 * before the previous writepage completed.
1592 * Block here, instead of in ->writepage(), so that the userspace fs
1593 * can only block processes actually operating on the filesystem.
1595 * Otherwise unprivileged userspace fs would be able to block
1600 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
1602 static int fuse_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1604 struct page
*page
= vmf
->page
;
1606 * Don't use page->mapping as it may become NULL from a
1607 * concurrent truncate.
1609 struct inode
*inode
= vma
->vm_file
->f_mapping
->host
;
1611 fuse_wait_on_page_writeback(inode
, page
->index
);
1615 static const struct vm_operations_struct fuse_file_vm_ops
= {
1616 .close
= fuse_vma_close
,
1617 .fault
= filemap_fault
,
1618 .page_mkwrite
= fuse_page_mkwrite
,
1619 .remap_pages
= generic_file_remap_pages
,
1622 static int fuse_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1624 if ((vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
)) {
1625 struct inode
*inode
= file_inode(file
);
1626 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1627 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1628 struct fuse_file
*ff
= file
->private_data
;
1630 * file may be written through mmap, so chain it onto the
1631 * inodes's write_file list
1633 spin_lock(&fc
->lock
);
1634 if (list_empty(&ff
->write_entry
))
1635 list_add(&ff
->write_entry
, &fi
->write_files
);
1636 spin_unlock(&fc
->lock
);
1638 file_accessed(file
);
1639 vma
->vm_ops
= &fuse_file_vm_ops
;
1643 static int fuse_direct_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1645 /* Can't provide the coherency needed for MAP_SHARED */
1646 if (vma
->vm_flags
& VM_MAYSHARE
)
1649 invalidate_inode_pages2(file
->f_mapping
);
1651 return generic_file_mmap(file
, vma
);
1654 static int convert_fuse_file_lock(const struct fuse_file_lock
*ffl
,
1655 struct file_lock
*fl
)
1657 switch (ffl
->type
) {
1663 if (ffl
->start
> OFFSET_MAX
|| ffl
->end
> OFFSET_MAX
||
1664 ffl
->end
< ffl
->start
)
1667 fl
->fl_start
= ffl
->start
;
1668 fl
->fl_end
= ffl
->end
;
1669 fl
->fl_pid
= ffl
->pid
;
1675 fl
->fl_type
= ffl
->type
;
1679 static void fuse_lk_fill(struct fuse_req
*req
, struct file
*file
,
1680 const struct file_lock
*fl
, int opcode
, pid_t pid
,
1683 struct inode
*inode
= file_inode(file
);
1684 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1685 struct fuse_file
*ff
= file
->private_data
;
1686 struct fuse_lk_in
*arg
= &req
->misc
.lk_in
;
1689 arg
->owner
= fuse_lock_owner_id(fc
, fl
->fl_owner
);
1690 arg
->lk
.start
= fl
->fl_start
;
1691 arg
->lk
.end
= fl
->fl_end
;
1692 arg
->lk
.type
= fl
->fl_type
;
1695 arg
->lk_flags
|= FUSE_LK_FLOCK
;
1696 req
->in
.h
.opcode
= opcode
;
1697 req
->in
.h
.nodeid
= get_node_id(inode
);
1698 req
->in
.numargs
= 1;
1699 req
->in
.args
[0].size
= sizeof(*arg
);
1700 req
->in
.args
[0].value
= arg
;
1703 static int fuse_getlk(struct file
*file
, struct file_lock
*fl
)
1705 struct inode
*inode
= file_inode(file
);
1706 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1707 struct fuse_req
*req
;
1708 struct fuse_lk_out outarg
;
1711 req
= fuse_get_req_nopages(fc
);
1713 return PTR_ERR(req
);
1715 fuse_lk_fill(req
, file
, fl
, FUSE_GETLK
, 0, 0);
1716 req
->out
.numargs
= 1;
1717 req
->out
.args
[0].size
= sizeof(outarg
);
1718 req
->out
.args
[0].value
= &outarg
;
1719 fuse_request_send(fc
, req
);
1720 err
= req
->out
.h
.error
;
1721 fuse_put_request(fc
, req
);
1723 err
= convert_fuse_file_lock(&outarg
.lk
, fl
);
1728 static int fuse_setlk(struct file
*file
, struct file_lock
*fl
, int flock
)
1730 struct inode
*inode
= file_inode(file
);
1731 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1732 struct fuse_req
*req
;
1733 int opcode
= (fl
->fl_flags
& FL_SLEEP
) ? FUSE_SETLKW
: FUSE_SETLK
;
1734 pid_t pid
= fl
->fl_type
!= F_UNLCK
? current
->tgid
: 0;
1737 if (fl
->fl_lmops
&& fl
->fl_lmops
->lm_grant
) {
1738 /* NLM needs asynchronous locks, which we don't support yet */
1742 /* Unlock on close is handled by the flush method */
1743 if (fl
->fl_flags
& FL_CLOSE
)
1746 req
= fuse_get_req_nopages(fc
);
1748 return PTR_ERR(req
);
1750 fuse_lk_fill(req
, file
, fl
, opcode
, pid
, flock
);
1751 fuse_request_send(fc
, req
);
1752 err
= req
->out
.h
.error
;
1753 /* locking is restartable */
1756 fuse_put_request(fc
, req
);
1760 static int fuse_file_lock(struct file
*file
, int cmd
, struct file_lock
*fl
)
1762 struct inode
*inode
= file_inode(file
);
1763 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1766 if (cmd
== F_CANCELLK
) {
1768 } else if (cmd
== F_GETLK
) {
1770 posix_test_lock(file
, fl
);
1773 err
= fuse_getlk(file
, fl
);
1776 err
= posix_lock_file(file
, fl
, NULL
);
1778 err
= fuse_setlk(file
, fl
, 0);
1783 static int fuse_file_flock(struct file
*file
, int cmd
, struct file_lock
*fl
)
1785 struct inode
*inode
= file_inode(file
);
1786 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1790 err
= flock_lock_file_wait(file
, fl
);
1792 struct fuse_file
*ff
= file
->private_data
;
1794 /* emulate flock with POSIX locks */
1795 fl
->fl_owner
= (fl_owner_t
) file
;
1797 err
= fuse_setlk(file
, fl
, 1);
1803 static sector_t
fuse_bmap(struct address_space
*mapping
, sector_t block
)
1805 struct inode
*inode
= mapping
->host
;
1806 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1807 struct fuse_req
*req
;
1808 struct fuse_bmap_in inarg
;
1809 struct fuse_bmap_out outarg
;
1812 if (!inode
->i_sb
->s_bdev
|| fc
->no_bmap
)
1815 req
= fuse_get_req_nopages(fc
);
1819 memset(&inarg
, 0, sizeof(inarg
));
1820 inarg
.block
= block
;
1821 inarg
.blocksize
= inode
->i_sb
->s_blocksize
;
1822 req
->in
.h
.opcode
= FUSE_BMAP
;
1823 req
->in
.h
.nodeid
= get_node_id(inode
);
1824 req
->in
.numargs
= 1;
1825 req
->in
.args
[0].size
= sizeof(inarg
);
1826 req
->in
.args
[0].value
= &inarg
;
1827 req
->out
.numargs
= 1;
1828 req
->out
.args
[0].size
= sizeof(outarg
);
1829 req
->out
.args
[0].value
= &outarg
;
1830 fuse_request_send(fc
, req
);
1831 err
= req
->out
.h
.error
;
1832 fuse_put_request(fc
, req
);
1836 return err
? 0 : outarg
.block
;
1839 static loff_t
fuse_file_llseek(struct file
*file
, loff_t offset
, int whence
)
1842 struct inode
*inode
= file_inode(file
);
1844 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
1845 if (whence
== SEEK_CUR
|| whence
== SEEK_SET
)
1846 return generic_file_llseek(file
, offset
, whence
);
1848 mutex_lock(&inode
->i_mutex
);
1849 retval
= fuse_update_attributes(inode
, NULL
, file
, NULL
);
1851 retval
= generic_file_llseek(file
, offset
, whence
);
1852 mutex_unlock(&inode
->i_mutex
);
1857 static int fuse_ioctl_copy_user(struct page
**pages
, struct iovec
*iov
,
1858 unsigned int nr_segs
, size_t bytes
, bool to_user
)
1866 iov_iter_init(&ii
, iov
, nr_segs
, bytes
, 0);
1868 while (iov_iter_count(&ii
)) {
1869 struct page
*page
= pages
[page_idx
++];
1870 size_t todo
= min_t(size_t, PAGE_SIZE
, iov_iter_count(&ii
));
1876 char __user
*uaddr
= ii
.iov
->iov_base
+ ii
.iov_offset
;
1877 size_t iov_len
= ii
.iov
->iov_len
- ii
.iov_offset
;
1878 size_t copy
= min(todo
, iov_len
);
1882 left
= copy_from_user(kaddr
, uaddr
, copy
);
1884 left
= copy_to_user(uaddr
, kaddr
, copy
);
1889 iov_iter_advance(&ii
, copy
);
1901 * CUSE servers compiled on 32bit broke on 64bit kernels because the
1902 * ABI was defined to be 'struct iovec' which is different on 32bit
1903 * and 64bit. Fortunately we can determine which structure the server
1904 * used from the size of the reply.
1906 static int fuse_copy_ioctl_iovec_old(struct iovec
*dst
, void *src
,
1907 size_t transferred
, unsigned count
,
1910 #ifdef CONFIG_COMPAT
1911 if (count
* sizeof(struct compat_iovec
) == transferred
) {
1912 struct compat_iovec
*ciov
= src
;
1916 * With this interface a 32bit server cannot support
1917 * non-compat (i.e. ones coming from 64bit apps) ioctl
1923 for (i
= 0; i
< count
; i
++) {
1924 dst
[i
].iov_base
= compat_ptr(ciov
[i
].iov_base
);
1925 dst
[i
].iov_len
= ciov
[i
].iov_len
;
1931 if (count
* sizeof(struct iovec
) != transferred
)
1934 memcpy(dst
, src
, transferred
);
1938 /* Make sure iov_length() won't overflow */
1939 static int fuse_verify_ioctl_iov(struct iovec
*iov
, size_t count
)
1942 u32 max
= FUSE_MAX_PAGES_PER_REQ
<< PAGE_SHIFT
;
1944 for (n
= 0; n
< count
; n
++, iov
++) {
1945 if (iov
->iov_len
> (size_t) max
)
1947 max
-= iov
->iov_len
;
1952 static int fuse_copy_ioctl_iovec(struct fuse_conn
*fc
, struct iovec
*dst
,
1953 void *src
, size_t transferred
, unsigned count
,
1957 struct fuse_ioctl_iovec
*fiov
= src
;
1959 if (fc
->minor
< 16) {
1960 return fuse_copy_ioctl_iovec_old(dst
, src
, transferred
,
1964 if (count
* sizeof(struct fuse_ioctl_iovec
) != transferred
)
1967 for (i
= 0; i
< count
; i
++) {
1968 /* Did the server supply an inappropriate value? */
1969 if (fiov
[i
].base
!= (unsigned long) fiov
[i
].base
||
1970 fiov
[i
].len
!= (unsigned long) fiov
[i
].len
)
1973 dst
[i
].iov_base
= (void __user
*) (unsigned long) fiov
[i
].base
;
1974 dst
[i
].iov_len
= (size_t) fiov
[i
].len
;
1976 #ifdef CONFIG_COMPAT
1978 (ptr_to_compat(dst
[i
].iov_base
) != fiov
[i
].base
||
1979 (compat_size_t
) dst
[i
].iov_len
!= fiov
[i
].len
))
1989 * For ioctls, there is no generic way to determine how much memory
1990 * needs to be read and/or written. Furthermore, ioctls are allowed
1991 * to dereference the passed pointer, so the parameter requires deep
1992 * copying but FUSE has no idea whatsoever about what to copy in or
1995 * This is solved by allowing FUSE server to retry ioctl with
1996 * necessary in/out iovecs. Let's assume the ioctl implementation
1997 * needs to read in the following structure.
2004 * On the first callout to FUSE server, inarg->in_size and
2005 * inarg->out_size will be NULL; then, the server completes the ioctl
2006 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2007 * the actual iov array to
2009 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2011 * which tells FUSE to copy in the requested area and retry the ioctl.
2012 * On the second round, the server has access to the structure and
2013 * from that it can tell what to look for next, so on the invocation,
2014 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2016 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2017 * { .iov_base = a.buf, .iov_len = a.buflen } }
2019 * FUSE will copy both struct a and the pointed buffer from the
2020 * process doing the ioctl and retry ioctl with both struct a and the
2023 * This time, FUSE server has everything it needs and completes ioctl
2024 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2026 * Copying data out works the same way.
2028 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2029 * automatically initializes in and out iovs by decoding @cmd with
2030 * _IOC_* macros and the server is not allowed to request RETRY. This
2031 * limits ioctl data transfers to well-formed ioctls and is the forced
2032 * behavior for all FUSE servers.
2034 long fuse_do_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
,
2037 struct fuse_file
*ff
= file
->private_data
;
2038 struct fuse_conn
*fc
= ff
->fc
;
2039 struct fuse_ioctl_in inarg
= {
2045 struct fuse_ioctl_out outarg
;
2046 struct fuse_req
*req
= NULL
;
2047 struct page
**pages
= NULL
;
2048 struct iovec
*iov_page
= NULL
;
2049 struct iovec
*in_iov
= NULL
, *out_iov
= NULL
;
2050 unsigned int in_iovs
= 0, out_iovs
= 0, num_pages
= 0, max_pages
;
2051 size_t in_size
, out_size
, transferred
;
2054 #if BITS_PER_LONG == 32
2055 inarg
.flags
|= FUSE_IOCTL_32BIT
;
2057 if (flags
& FUSE_IOCTL_COMPAT
)
2058 inarg
.flags
|= FUSE_IOCTL_32BIT
;
2061 /* assume all the iovs returned by client always fits in a page */
2062 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec
) * FUSE_IOCTL_MAX_IOV
> PAGE_SIZE
);
2065 pages
= kcalloc(FUSE_MAX_PAGES_PER_REQ
, sizeof(pages
[0]), GFP_KERNEL
);
2066 iov_page
= (struct iovec
*) __get_free_page(GFP_KERNEL
);
2067 if (!pages
|| !iov_page
)
2071 * If restricted, initialize IO parameters as encoded in @cmd.
2072 * RETRY from server is not allowed.
2074 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
)) {
2075 struct iovec
*iov
= iov_page
;
2077 iov
->iov_base
= (void __user
*)arg
;
2078 iov
->iov_len
= _IOC_SIZE(cmd
);
2080 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
2085 if (_IOC_DIR(cmd
) & _IOC_READ
) {
2092 inarg
.in_size
= in_size
= iov_length(in_iov
, in_iovs
);
2093 inarg
.out_size
= out_size
= iov_length(out_iov
, out_iovs
);
2096 * Out data can be used either for actual out data or iovs,
2097 * make sure there always is at least one page.
2099 out_size
= max_t(size_t, out_size
, PAGE_SIZE
);
2100 max_pages
= DIV_ROUND_UP(max(in_size
, out_size
), PAGE_SIZE
);
2102 /* make sure there are enough buffer pages and init request with them */
2104 if (max_pages
> FUSE_MAX_PAGES_PER_REQ
)
2106 while (num_pages
< max_pages
) {
2107 pages
[num_pages
] = alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
2108 if (!pages
[num_pages
])
2113 req
= fuse_get_req(fc
, num_pages
);
2119 memcpy(req
->pages
, pages
, sizeof(req
->pages
[0]) * num_pages
);
2120 req
->num_pages
= num_pages
;
2121 fuse_page_descs_length_init(req
, 0, req
->num_pages
);
2123 /* okay, let's send it to the client */
2124 req
->in
.h
.opcode
= FUSE_IOCTL
;
2125 req
->in
.h
.nodeid
= ff
->nodeid
;
2126 req
->in
.numargs
= 1;
2127 req
->in
.args
[0].size
= sizeof(inarg
);
2128 req
->in
.args
[0].value
= &inarg
;
2131 req
->in
.args
[1].size
= in_size
;
2132 req
->in
.argpages
= 1;
2134 err
= fuse_ioctl_copy_user(pages
, in_iov
, in_iovs
, in_size
,
2140 req
->out
.numargs
= 2;
2141 req
->out
.args
[0].size
= sizeof(outarg
);
2142 req
->out
.args
[0].value
= &outarg
;
2143 req
->out
.args
[1].size
= out_size
;
2144 req
->out
.argpages
= 1;
2145 req
->out
.argvar
= 1;
2147 fuse_request_send(fc
, req
);
2148 err
= req
->out
.h
.error
;
2149 transferred
= req
->out
.args
[1].size
;
2150 fuse_put_request(fc
, req
);
2155 /* did it ask for retry? */
2156 if (outarg
.flags
& FUSE_IOCTL_RETRY
) {
2159 /* no retry if in restricted mode */
2161 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
))
2164 in_iovs
= outarg
.in_iovs
;
2165 out_iovs
= outarg
.out_iovs
;
2168 * Make sure things are in boundary, separate checks
2169 * are to protect against overflow.
2172 if (in_iovs
> FUSE_IOCTL_MAX_IOV
||
2173 out_iovs
> FUSE_IOCTL_MAX_IOV
||
2174 in_iovs
+ out_iovs
> FUSE_IOCTL_MAX_IOV
)
2177 vaddr
= kmap_atomic(pages
[0]);
2178 err
= fuse_copy_ioctl_iovec(fc
, iov_page
, vaddr
,
2179 transferred
, in_iovs
+ out_iovs
,
2180 (flags
& FUSE_IOCTL_COMPAT
) != 0);
2181 kunmap_atomic(vaddr
);
2186 out_iov
= in_iov
+ in_iovs
;
2188 err
= fuse_verify_ioctl_iov(in_iov
, in_iovs
);
2192 err
= fuse_verify_ioctl_iov(out_iov
, out_iovs
);
2200 if (transferred
> inarg
.out_size
)
2203 err
= fuse_ioctl_copy_user(pages
, out_iov
, out_iovs
, transferred
, true);
2206 fuse_put_request(fc
, req
);
2207 free_page((unsigned long) iov_page
);
2209 __free_page(pages
[--num_pages
]);
2212 return err
? err
: outarg
.result
;
2214 EXPORT_SYMBOL_GPL(fuse_do_ioctl
);
2216 long fuse_ioctl_common(struct file
*file
, unsigned int cmd
,
2217 unsigned long arg
, unsigned int flags
)
2219 struct inode
*inode
= file_inode(file
);
2220 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2222 if (!fuse_allow_current_process(fc
))
2225 if (is_bad_inode(inode
))
2228 return fuse_do_ioctl(file
, cmd
, arg
, flags
);
2231 static long fuse_file_ioctl(struct file
*file
, unsigned int cmd
,
2234 return fuse_ioctl_common(file
, cmd
, arg
, 0);
2237 static long fuse_file_compat_ioctl(struct file
*file
, unsigned int cmd
,
2240 return fuse_ioctl_common(file
, cmd
, arg
, FUSE_IOCTL_COMPAT
);
2244 * All files which have been polled are linked to RB tree
2245 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2246 * find the matching one.
2248 static struct rb_node
**fuse_find_polled_node(struct fuse_conn
*fc
, u64 kh
,
2249 struct rb_node
**parent_out
)
2251 struct rb_node
**link
= &fc
->polled_files
.rb_node
;
2252 struct rb_node
*last
= NULL
;
2255 struct fuse_file
*ff
;
2258 ff
= rb_entry(last
, struct fuse_file
, polled_node
);
2261 link
= &last
->rb_left
;
2262 else if (kh
> ff
->kh
)
2263 link
= &last
->rb_right
;
2274 * The file is about to be polled. Make sure it's on the polled_files
2275 * RB tree. Note that files once added to the polled_files tree are
2276 * not removed before the file is released. This is because a file
2277 * polled once is likely to be polled again.
2279 static void fuse_register_polled_file(struct fuse_conn
*fc
,
2280 struct fuse_file
*ff
)
2282 spin_lock(&fc
->lock
);
2283 if (RB_EMPTY_NODE(&ff
->polled_node
)) {
2284 struct rb_node
**link
, *parent
;
2286 link
= fuse_find_polled_node(fc
, ff
->kh
, &parent
);
2288 rb_link_node(&ff
->polled_node
, parent
, link
);
2289 rb_insert_color(&ff
->polled_node
, &fc
->polled_files
);
2291 spin_unlock(&fc
->lock
);
2294 unsigned fuse_file_poll(struct file
*file
, poll_table
*wait
)
2296 struct fuse_file
*ff
= file
->private_data
;
2297 struct fuse_conn
*fc
= ff
->fc
;
2298 struct fuse_poll_in inarg
= { .fh
= ff
->fh
, .kh
= ff
->kh
};
2299 struct fuse_poll_out outarg
;
2300 struct fuse_req
*req
;
2304 return DEFAULT_POLLMASK
;
2306 poll_wait(file
, &ff
->poll_wait
, wait
);
2307 inarg
.events
= (__u32
)poll_requested_events(wait
);
2310 * Ask for notification iff there's someone waiting for it.
2311 * The client may ignore the flag and always notify.
2313 if (waitqueue_active(&ff
->poll_wait
)) {
2314 inarg
.flags
|= FUSE_POLL_SCHEDULE_NOTIFY
;
2315 fuse_register_polled_file(fc
, ff
);
2318 req
= fuse_get_req_nopages(fc
);
2322 req
->in
.h
.opcode
= FUSE_POLL
;
2323 req
->in
.h
.nodeid
= ff
->nodeid
;
2324 req
->in
.numargs
= 1;
2325 req
->in
.args
[0].size
= sizeof(inarg
);
2326 req
->in
.args
[0].value
= &inarg
;
2327 req
->out
.numargs
= 1;
2328 req
->out
.args
[0].size
= sizeof(outarg
);
2329 req
->out
.args
[0].value
= &outarg
;
2330 fuse_request_send(fc
, req
);
2331 err
= req
->out
.h
.error
;
2332 fuse_put_request(fc
, req
);
2335 return outarg
.revents
;
2336 if (err
== -ENOSYS
) {
2338 return DEFAULT_POLLMASK
;
2342 EXPORT_SYMBOL_GPL(fuse_file_poll
);
2345 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2346 * wakes up the poll waiters.
2348 int fuse_notify_poll_wakeup(struct fuse_conn
*fc
,
2349 struct fuse_notify_poll_wakeup_out
*outarg
)
2351 u64 kh
= outarg
->kh
;
2352 struct rb_node
**link
;
2354 spin_lock(&fc
->lock
);
2356 link
= fuse_find_polled_node(fc
, kh
, NULL
);
2358 struct fuse_file
*ff
;
2360 ff
= rb_entry(*link
, struct fuse_file
, polled_node
);
2361 wake_up_interruptible_sync(&ff
->poll_wait
);
2364 spin_unlock(&fc
->lock
);
2368 static void fuse_do_truncate(struct file
*file
)
2370 struct inode
*inode
= file
->f_mapping
->host
;
2373 attr
.ia_valid
= ATTR_SIZE
;
2374 attr
.ia_size
= i_size_read(inode
);
2376 attr
.ia_file
= file
;
2377 attr
.ia_valid
|= ATTR_FILE
;
2379 fuse_do_setattr(inode
, &attr
, file
);
2382 static inline loff_t
fuse_round_up(loff_t off
)
2384 return round_up(off
, FUSE_MAX_PAGES_PER_REQ
<< PAGE_SHIFT
);
2388 fuse_direct_IO(int rw
, struct kiocb
*iocb
, const struct iovec
*iov
,
2389 loff_t offset
, unsigned long nr_segs
)
2392 struct file
*file
= iocb
->ki_filp
;
2393 struct fuse_file
*ff
= file
->private_data
;
2394 bool async_dio
= ff
->fc
->async_dio
;
2396 struct inode
*inode
;
2398 size_t count
= iov_length(iov
, nr_segs
);
2399 struct fuse_io_priv
*io
;
2402 inode
= file
->f_mapping
->host
;
2403 i_size
= i_size_read(inode
);
2405 /* optimization for short read */
2406 if (async_dio
&& rw
!= WRITE
&& offset
+ count
> i_size
) {
2407 if (offset
>= i_size
)
2409 count
= min_t(loff_t
, count
, fuse_round_up(i_size
- offset
));
2412 io
= kmalloc(sizeof(struct fuse_io_priv
), GFP_KERNEL
);
2415 spin_lock_init(&io
->lock
);
2419 io
->offset
= offset
;
2420 io
->write
= (rw
== WRITE
);
2424 * By default, we want to optimize all I/Os with async request
2425 * submission to the client filesystem if supported.
2427 io
->async
= async_dio
;
2431 * We cannot asynchronously extend the size of a file. We have no method
2432 * to wait on real async I/O requests, so we must submit this request
2435 if (!is_sync_kiocb(iocb
) && (offset
+ count
> i_size
) && rw
== WRITE
)
2439 ret
= __fuse_direct_write(io
, iov
, nr_segs
, &pos
);
2441 ret
= __fuse_direct_read(io
, iov
, nr_segs
, &pos
, count
);
2444 fuse_aio_complete(io
, ret
< 0 ? ret
: 0, -1);
2446 /* we have a non-extending, async request, so return */
2447 if (!is_sync_kiocb(iocb
))
2448 return -EIOCBQUEUED
;
2450 ret
= wait_on_sync_kiocb(iocb
);
2457 fuse_write_update_size(inode
, pos
);
2458 else if (ret
< 0 && offset
+ count
> i_size
)
2459 fuse_do_truncate(file
);
2465 static long fuse_file_fallocate(struct file
*file
, int mode
, loff_t offset
,
2468 struct fuse_file
*ff
= file
->private_data
;
2469 struct inode
*inode
= file
->f_inode
;
2470 struct fuse_inode
*fi
= get_fuse_inode(inode
);
2471 struct fuse_conn
*fc
= ff
->fc
;
2472 struct fuse_req
*req
;
2473 struct fuse_fallocate_in inarg
= {
2480 bool lock_inode
= !(mode
& FALLOC_FL_KEEP_SIZE
) ||
2481 (mode
& FALLOC_FL_PUNCH_HOLE
);
2483 if (fc
->no_fallocate
)
2487 mutex_lock(&inode
->i_mutex
);
2488 if (mode
& FALLOC_FL_PUNCH_HOLE
) {
2489 loff_t endbyte
= offset
+ length
- 1;
2490 err
= filemap_write_and_wait_range(inode
->i_mapping
,
2495 fuse_sync_writes(inode
);
2499 if (!(mode
& FALLOC_FL_KEEP_SIZE
))
2500 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
2502 req
= fuse_get_req_nopages(fc
);
2508 req
->in
.h
.opcode
= FUSE_FALLOCATE
;
2509 req
->in
.h
.nodeid
= ff
->nodeid
;
2510 req
->in
.numargs
= 1;
2511 req
->in
.args
[0].size
= sizeof(inarg
);
2512 req
->in
.args
[0].value
= &inarg
;
2513 fuse_request_send(fc
, req
);
2514 err
= req
->out
.h
.error
;
2515 if (err
== -ENOSYS
) {
2516 fc
->no_fallocate
= 1;
2519 fuse_put_request(fc
, req
);
2524 /* we could have extended the file */
2525 if (!(mode
& FALLOC_FL_KEEP_SIZE
))
2526 fuse_write_update_size(inode
, offset
+ length
);
2528 if (mode
& FALLOC_FL_PUNCH_HOLE
)
2529 truncate_pagecache_range(inode
, offset
, offset
+ length
- 1);
2531 fuse_invalidate_attr(inode
);
2534 if (!(mode
& FALLOC_FL_KEEP_SIZE
))
2535 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
2538 mutex_unlock(&inode
->i_mutex
);
2543 static const struct file_operations fuse_file_operations
= {
2544 .llseek
= fuse_file_llseek
,
2545 .read
= do_sync_read
,
2546 .aio_read
= fuse_file_aio_read
,
2547 .write
= do_sync_write
,
2548 .aio_write
= fuse_file_aio_write
,
2549 .mmap
= fuse_file_mmap
,
2551 .flush
= fuse_flush
,
2552 .release
= fuse_release
,
2553 .fsync
= fuse_fsync
,
2554 .lock
= fuse_file_lock
,
2555 .flock
= fuse_file_flock
,
2556 .splice_read
= generic_file_splice_read
,
2557 .unlocked_ioctl
= fuse_file_ioctl
,
2558 .compat_ioctl
= fuse_file_compat_ioctl
,
2559 .poll
= fuse_file_poll
,
2560 .fallocate
= fuse_file_fallocate
,
2563 static const struct file_operations fuse_direct_io_file_operations
= {
2564 .llseek
= fuse_file_llseek
,
2565 .read
= fuse_direct_read
,
2566 .write
= fuse_direct_write
,
2567 .mmap
= fuse_direct_mmap
,
2569 .flush
= fuse_flush
,
2570 .release
= fuse_release
,
2571 .fsync
= fuse_fsync
,
2572 .lock
= fuse_file_lock
,
2573 .flock
= fuse_file_flock
,
2574 .unlocked_ioctl
= fuse_file_ioctl
,
2575 .compat_ioctl
= fuse_file_compat_ioctl
,
2576 .poll
= fuse_file_poll
,
2577 .fallocate
= fuse_file_fallocate
,
2578 /* no splice_read */
2581 static const struct address_space_operations fuse_file_aops
= {
2582 .readpage
= fuse_readpage
,
2583 .writepage
= fuse_writepage
,
2584 .launder_page
= fuse_launder_page
,
2585 .readpages
= fuse_readpages
,
2586 .set_page_dirty
= __set_page_dirty_nobuffers
,
2588 .direct_IO
= fuse_direct_IO
,
2591 void fuse_init_file_inode(struct inode
*inode
)
2593 inode
->i_fop
= &fuse_file_operations
;
2594 inode
->i_data
.a_ops
= &fuse_file_aops
;