2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU GPL.
11 #include <linux/pagemap.h>
12 #include <linux/slab.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/module.h>
16 #include <linux/compat.h>
17 #include <linux/swap.h>
18 #include <linux/aio.h>
19 #include <linux/falloc.h>
21 static const struct file_operations fuse_direct_io_file_operations
;
23 static int fuse_send_open(struct fuse_conn
*fc
, u64 nodeid
, struct file
*file
,
24 int opcode
, struct fuse_open_out
*outargp
)
26 struct fuse_open_in inarg
;
30 req
= fuse_get_req_nopages(fc
);
34 memset(&inarg
, 0, sizeof(inarg
));
35 inarg
.flags
= file
->f_flags
& ~(O_CREAT
| O_EXCL
| O_NOCTTY
);
36 if (!fc
->atomic_o_trunc
)
37 inarg
.flags
&= ~O_TRUNC
;
38 req
->in
.h
.opcode
= opcode
;
39 req
->in
.h
.nodeid
= nodeid
;
41 req
->in
.args
[0].size
= sizeof(inarg
);
42 req
->in
.args
[0].value
= &inarg
;
44 req
->out
.args
[0].size
= sizeof(*outargp
);
45 req
->out
.args
[0].value
= outargp
;
46 fuse_request_send(fc
, req
);
47 err
= req
->out
.h
.error
;
48 fuse_put_request(fc
, req
);
53 struct fuse_file
*fuse_file_alloc(struct fuse_conn
*fc
)
57 ff
= kmalloc(sizeof(struct fuse_file
), GFP_KERNEL
);
62 ff
->reserved_req
= fuse_request_alloc(0);
63 if (unlikely(!ff
->reserved_req
)) {
68 INIT_LIST_HEAD(&ff
->write_entry
);
69 atomic_set(&ff
->count
, 0);
70 RB_CLEAR_NODE(&ff
->polled_node
);
71 init_waitqueue_head(&ff
->poll_wait
);
75 spin_unlock(&fc
->lock
);
80 void fuse_file_free(struct fuse_file
*ff
)
82 fuse_request_free(ff
->reserved_req
);
86 struct fuse_file
*fuse_file_get(struct fuse_file
*ff
)
88 atomic_inc(&ff
->count
);
92 static void fuse_release_async(struct work_struct
*work
)
98 req
= container_of(work
, struct fuse_req
, misc
.release
.work
);
99 path
= req
->misc
.release
.path
;
100 fc
= get_fuse_conn(path
.dentry
->d_inode
);
102 fuse_put_request(fc
, req
);
106 static void fuse_release_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
108 if (fc
->destroy_req
) {
110 * If this is a fuseblk mount, then it's possible that
111 * releasing the path will result in releasing the
112 * super block and sending the DESTROY request. If
113 * the server is single threaded, this would hang.
114 * For this reason do the path_put() in a separate
117 atomic_inc(&req
->count
);
118 INIT_WORK(&req
->misc
.release
.work
, fuse_release_async
);
119 schedule_work(&req
->misc
.release
.work
);
121 path_put(&req
->misc
.release
.path
);
125 static void fuse_file_put(struct fuse_file
*ff
, bool sync
)
127 if (atomic_dec_and_test(&ff
->count
)) {
128 struct fuse_req
*req
= ff
->reserved_req
;
130 if (ff
->fc
->no_open
) {
132 * Drop the release request when client does not
136 path_put(&req
->misc
.release
.path
);
137 fuse_put_request(ff
->fc
, req
);
140 fuse_request_send(ff
->fc
, req
);
141 path_put(&req
->misc
.release
.path
);
142 fuse_put_request(ff
->fc
, req
);
144 req
->end
= fuse_release_end
;
146 fuse_request_send_background(ff
->fc
, req
);
152 int fuse_do_open(struct fuse_conn
*fc
, u64 nodeid
, struct file
*file
,
155 struct fuse_file
*ff
;
156 int opcode
= isdir
? FUSE_OPENDIR
: FUSE_OPEN
;
158 ff
= fuse_file_alloc(fc
);
163 ff
->open_flags
= FOPEN_KEEP_CACHE
; /* Default for no-open */
164 if (!fc
->no_open
|| isdir
) {
165 struct fuse_open_out outarg
;
168 err
= fuse_send_open(fc
, nodeid
, file
, opcode
, &outarg
);
171 ff
->open_flags
= outarg
.open_flags
;
173 } else if (err
!= -ENOSYS
|| isdir
) {
182 ff
->open_flags
&= ~FOPEN_DIRECT_IO
;
185 file
->private_data
= fuse_file_get(ff
);
189 EXPORT_SYMBOL_GPL(fuse_do_open
);
191 static void fuse_link_write_file(struct file
*file
)
193 struct inode
*inode
= file_inode(file
);
194 struct fuse_conn
*fc
= get_fuse_conn(inode
);
195 struct fuse_inode
*fi
= get_fuse_inode(inode
);
196 struct fuse_file
*ff
= file
->private_data
;
198 * file may be written through mmap, so chain it onto the
199 * inodes's write_file list
201 spin_lock(&fc
->lock
);
202 if (list_empty(&ff
->write_entry
))
203 list_add(&ff
->write_entry
, &fi
->write_files
);
204 spin_unlock(&fc
->lock
);
207 void fuse_finish_open(struct inode
*inode
, struct file
*file
)
209 struct fuse_file
*ff
= file
->private_data
;
210 struct fuse_conn
*fc
= get_fuse_conn(inode
);
212 if (ff
->open_flags
& FOPEN_DIRECT_IO
)
213 file
->f_op
= &fuse_direct_io_file_operations
;
214 if (!(ff
->open_flags
& FOPEN_KEEP_CACHE
))
215 invalidate_inode_pages2(inode
->i_mapping
);
216 if (ff
->open_flags
& FOPEN_NONSEEKABLE
)
217 nonseekable_open(inode
, file
);
218 if (fc
->atomic_o_trunc
&& (file
->f_flags
& O_TRUNC
)) {
219 struct fuse_inode
*fi
= get_fuse_inode(inode
);
221 spin_lock(&fc
->lock
);
222 fi
->attr_version
= ++fc
->attr_version
;
223 i_size_write(inode
, 0);
224 spin_unlock(&fc
->lock
);
225 fuse_invalidate_attr(inode
);
226 if (fc
->writeback_cache
)
227 file_update_time(file
);
229 if ((file
->f_mode
& FMODE_WRITE
) && fc
->writeback_cache
)
230 fuse_link_write_file(file
);
233 int fuse_open_common(struct inode
*inode
, struct file
*file
, bool isdir
)
235 struct fuse_conn
*fc
= get_fuse_conn(inode
);
237 bool lock_inode
= (file
->f_flags
& O_TRUNC
) &&
238 fc
->atomic_o_trunc
&&
241 err
= generic_file_open(inode
, file
);
246 mutex_lock(&inode
->i_mutex
);
248 err
= fuse_do_open(fc
, get_node_id(inode
), file
, isdir
);
251 fuse_finish_open(inode
, file
);
254 mutex_unlock(&inode
->i_mutex
);
259 static void fuse_prepare_release(struct fuse_file
*ff
, int flags
, int opcode
)
261 struct fuse_conn
*fc
= ff
->fc
;
262 struct fuse_req
*req
= ff
->reserved_req
;
263 struct fuse_release_in
*inarg
= &req
->misc
.release
.in
;
265 spin_lock(&fc
->lock
);
266 list_del(&ff
->write_entry
);
267 if (!RB_EMPTY_NODE(&ff
->polled_node
))
268 rb_erase(&ff
->polled_node
, &fc
->polled_files
);
269 spin_unlock(&fc
->lock
);
271 wake_up_interruptible_all(&ff
->poll_wait
);
274 inarg
->flags
= flags
;
275 req
->in
.h
.opcode
= opcode
;
276 req
->in
.h
.nodeid
= ff
->nodeid
;
278 req
->in
.args
[0].size
= sizeof(struct fuse_release_in
);
279 req
->in
.args
[0].value
= inarg
;
282 void fuse_release_common(struct file
*file
, int opcode
)
284 struct fuse_file
*ff
;
285 struct fuse_req
*req
;
287 ff
= file
->private_data
;
291 req
= ff
->reserved_req
;
292 fuse_prepare_release(ff
, file
->f_flags
, opcode
);
295 struct fuse_release_in
*inarg
= &req
->misc
.release
.in
;
296 inarg
->release_flags
|= FUSE_RELEASE_FLOCK_UNLOCK
;
297 inarg
->lock_owner
= fuse_lock_owner_id(ff
->fc
,
300 /* Hold vfsmount and dentry until release is finished */
301 path_get(&file
->f_path
);
302 req
->misc
.release
.path
= file
->f_path
;
305 * Normally this will send the RELEASE request, however if
306 * some asynchronous READ or WRITE requests are outstanding,
307 * the sending will be delayed.
309 * Make the release synchronous if this is a fuseblk mount,
310 * synchronous RELEASE is allowed (and desirable) in this case
311 * because the server can be trusted not to screw up.
313 fuse_file_put(ff
, ff
->fc
->destroy_req
!= NULL
);
316 static int fuse_open(struct inode
*inode
, struct file
*file
)
318 return fuse_open_common(inode
, file
, false);
321 static int fuse_release(struct inode
*inode
, struct file
*file
)
323 struct fuse_conn
*fc
= get_fuse_conn(inode
);
325 /* see fuse_vma_close() for !writeback_cache case */
326 if (fc
->writeback_cache
)
327 write_inode_now(inode
, 1);
329 fuse_release_common(file
, FUSE_RELEASE
);
331 /* return value is ignored by VFS */
335 void fuse_sync_release(struct fuse_file
*ff
, int flags
)
337 WARN_ON(atomic_read(&ff
->count
) > 1);
338 fuse_prepare_release(ff
, flags
, FUSE_RELEASE
);
339 ff
->reserved_req
->force
= 1;
340 ff
->reserved_req
->background
= 0;
341 fuse_request_send(ff
->fc
, ff
->reserved_req
);
342 fuse_put_request(ff
->fc
, ff
->reserved_req
);
345 EXPORT_SYMBOL_GPL(fuse_sync_release
);
348 * Scramble the ID space with XTEA, so that the value of the files_struct
349 * pointer is not exposed to userspace.
351 u64
fuse_lock_owner_id(struct fuse_conn
*fc
, fl_owner_t id
)
353 u32
*k
= fc
->scramble_key
;
354 u64 v
= (unsigned long) id
;
360 for (i
= 0; i
< 32; i
++) {
361 v0
+= ((v1
<< 4 ^ v1
>> 5) + v1
) ^ (sum
+ k
[sum
& 3]);
363 v1
+= ((v0
<< 4 ^ v0
>> 5) + v0
) ^ (sum
+ k
[sum
>>11 & 3]);
366 return (u64
) v0
+ ((u64
) v1
<< 32);
370 * Check if any page in a range is under writeback
372 * This is currently done by walking the list of writepage requests
373 * for the inode, which can be pretty inefficient.
375 static bool fuse_range_is_writeback(struct inode
*inode
, pgoff_t idx_from
,
378 struct fuse_conn
*fc
= get_fuse_conn(inode
);
379 struct fuse_inode
*fi
= get_fuse_inode(inode
);
380 struct fuse_req
*req
;
383 spin_lock(&fc
->lock
);
384 list_for_each_entry(req
, &fi
->writepages
, writepages_entry
) {
387 BUG_ON(req
->inode
!= inode
);
388 curr_index
= req
->misc
.write
.in
.offset
>> PAGE_CACHE_SHIFT
;
389 if (idx_from
< curr_index
+ req
->num_pages
&&
390 curr_index
<= idx_to
) {
395 spin_unlock(&fc
->lock
);
400 static inline bool fuse_page_is_writeback(struct inode
*inode
, pgoff_t index
)
402 return fuse_range_is_writeback(inode
, index
, index
);
406 * Wait for page writeback to be completed.
408 * Since fuse doesn't rely on the VM writeback tracking, this has to
409 * use some other means.
411 static int fuse_wait_on_page_writeback(struct inode
*inode
, pgoff_t index
)
413 struct fuse_inode
*fi
= get_fuse_inode(inode
);
415 wait_event(fi
->page_waitq
, !fuse_page_is_writeback(inode
, index
));
420 * Wait for all pending writepages on the inode to finish.
422 * This is currently done by blocking further writes with FUSE_NOWRITE
423 * and waiting for all sent writes to complete.
425 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
426 * could conflict with truncation.
428 static void fuse_sync_writes(struct inode
*inode
)
430 fuse_set_nowrite(inode
);
431 fuse_release_nowrite(inode
);
434 static int fuse_flush(struct file
*file
, fl_owner_t id
)
436 struct inode
*inode
= file_inode(file
);
437 struct fuse_conn
*fc
= get_fuse_conn(inode
);
438 struct fuse_file
*ff
= file
->private_data
;
439 struct fuse_req
*req
;
440 struct fuse_flush_in inarg
;
443 if (is_bad_inode(inode
))
449 err
= write_inode_now(inode
, 1);
453 mutex_lock(&inode
->i_mutex
);
454 fuse_sync_writes(inode
);
455 mutex_unlock(&inode
->i_mutex
);
457 req
= fuse_get_req_nofail_nopages(fc
, file
);
458 memset(&inarg
, 0, sizeof(inarg
));
460 inarg
.lock_owner
= fuse_lock_owner_id(fc
, id
);
461 req
->in
.h
.opcode
= FUSE_FLUSH
;
462 req
->in
.h
.nodeid
= get_node_id(inode
);
464 req
->in
.args
[0].size
= sizeof(inarg
);
465 req
->in
.args
[0].value
= &inarg
;
467 fuse_request_send(fc
, req
);
468 err
= req
->out
.h
.error
;
469 fuse_put_request(fc
, req
);
470 if (err
== -ENOSYS
) {
477 int fuse_fsync_common(struct file
*file
, loff_t start
, loff_t end
,
478 int datasync
, int isdir
)
480 struct inode
*inode
= file
->f_mapping
->host
;
481 struct fuse_conn
*fc
= get_fuse_conn(inode
);
482 struct fuse_file
*ff
= file
->private_data
;
483 struct fuse_req
*req
;
484 struct fuse_fsync_in inarg
;
487 if (is_bad_inode(inode
))
490 mutex_lock(&inode
->i_mutex
);
493 * Start writeback against all dirty pages of the inode, then
494 * wait for all outstanding writes, before sending the FSYNC
497 err
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
501 fuse_sync_writes(inode
);
502 err
= sync_inode_metadata(inode
, 1);
506 if ((!isdir
&& fc
->no_fsync
) || (isdir
&& fc
->no_fsyncdir
))
509 req
= fuse_get_req_nopages(fc
);
515 memset(&inarg
, 0, sizeof(inarg
));
517 inarg
.fsync_flags
= datasync
? 1 : 0;
518 req
->in
.h
.opcode
= isdir
? FUSE_FSYNCDIR
: FUSE_FSYNC
;
519 req
->in
.h
.nodeid
= get_node_id(inode
);
521 req
->in
.args
[0].size
= sizeof(inarg
);
522 req
->in
.args
[0].value
= &inarg
;
523 fuse_request_send(fc
, req
);
524 err
= req
->out
.h
.error
;
525 fuse_put_request(fc
, req
);
526 if (err
== -ENOSYS
) {
534 mutex_unlock(&inode
->i_mutex
);
538 static int fuse_fsync(struct file
*file
, loff_t start
, loff_t end
,
541 return fuse_fsync_common(file
, start
, end
, datasync
, 0);
544 void fuse_read_fill(struct fuse_req
*req
, struct file
*file
, loff_t pos
,
545 size_t count
, int opcode
)
547 struct fuse_read_in
*inarg
= &req
->misc
.read
.in
;
548 struct fuse_file
*ff
= file
->private_data
;
553 inarg
->flags
= file
->f_flags
;
554 req
->in
.h
.opcode
= opcode
;
555 req
->in
.h
.nodeid
= ff
->nodeid
;
557 req
->in
.args
[0].size
= sizeof(struct fuse_read_in
);
558 req
->in
.args
[0].value
= inarg
;
560 req
->out
.numargs
= 1;
561 req
->out
.args
[0].size
= count
;
564 static void fuse_release_user_pages(struct fuse_req
*req
, int write
)
568 for (i
= 0; i
< req
->num_pages
; i
++) {
569 struct page
*page
= req
->pages
[i
];
571 set_page_dirty_lock(page
);
577 * In case of short read, the caller sets 'pos' to the position of
578 * actual end of fuse request in IO request. Otherwise, if bytes_requested
579 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
582 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
583 * both submitted asynchronously. The first of them was ACKed by userspace as
584 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
585 * second request was ACKed as short, e.g. only 1K was read, resulting in
588 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
589 * will be equal to the length of the longest contiguous fragment of
590 * transferred data starting from the beginning of IO request.
592 static void fuse_aio_complete(struct fuse_io_priv
*io
, int err
, ssize_t pos
)
596 spin_lock(&io
->lock
);
598 io
->err
= io
->err
? : err
;
599 else if (pos
>= 0 && (io
->bytes
< 0 || pos
< io
->bytes
))
603 spin_unlock(&io
->lock
);
610 else if (io
->bytes
>= 0 && io
->write
)
613 res
= io
->bytes
< 0 ? io
->size
: io
->bytes
;
615 if (!is_sync_kiocb(io
->iocb
)) {
616 struct inode
*inode
= file_inode(io
->iocb
->ki_filp
);
617 struct fuse_conn
*fc
= get_fuse_conn(inode
);
618 struct fuse_inode
*fi
= get_fuse_inode(inode
);
620 spin_lock(&fc
->lock
);
621 fi
->attr_version
= ++fc
->attr_version
;
622 spin_unlock(&fc
->lock
);
626 aio_complete(io
->iocb
, res
, 0);
631 static void fuse_aio_complete_req(struct fuse_conn
*fc
, struct fuse_req
*req
)
633 struct fuse_io_priv
*io
= req
->io
;
636 fuse_release_user_pages(req
, !io
->write
);
639 if (req
->misc
.write
.in
.size
!= req
->misc
.write
.out
.size
)
640 pos
= req
->misc
.write
.in
.offset
- io
->offset
+
641 req
->misc
.write
.out
.size
;
643 if (req
->misc
.read
.in
.size
!= req
->out
.args
[0].size
)
644 pos
= req
->misc
.read
.in
.offset
- io
->offset
+
645 req
->out
.args
[0].size
;
648 fuse_aio_complete(io
, req
->out
.h
.error
, pos
);
651 static size_t fuse_async_req_send(struct fuse_conn
*fc
, struct fuse_req
*req
,
652 size_t num_bytes
, struct fuse_io_priv
*io
)
654 spin_lock(&io
->lock
);
655 io
->size
+= num_bytes
;
657 spin_unlock(&io
->lock
);
660 req
->end
= fuse_aio_complete_req
;
662 __fuse_get_request(req
);
663 fuse_request_send_background(fc
, req
);
668 static size_t fuse_send_read(struct fuse_req
*req
, struct fuse_io_priv
*io
,
669 loff_t pos
, size_t count
, fl_owner_t owner
)
671 struct file
*file
= io
->file
;
672 struct fuse_file
*ff
= file
->private_data
;
673 struct fuse_conn
*fc
= ff
->fc
;
675 fuse_read_fill(req
, file
, pos
, count
, FUSE_READ
);
677 struct fuse_read_in
*inarg
= &req
->misc
.read
.in
;
679 inarg
->read_flags
|= FUSE_READ_LOCKOWNER
;
680 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
684 return fuse_async_req_send(fc
, req
, count
, io
);
686 fuse_request_send(fc
, req
);
687 return req
->out
.args
[0].size
;
690 static void fuse_read_update_size(struct inode
*inode
, loff_t size
,
693 struct fuse_conn
*fc
= get_fuse_conn(inode
);
694 struct fuse_inode
*fi
= get_fuse_inode(inode
);
696 spin_lock(&fc
->lock
);
697 if (attr_ver
== fi
->attr_version
&& size
< inode
->i_size
&&
698 !test_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
)) {
699 fi
->attr_version
= ++fc
->attr_version
;
700 i_size_write(inode
, size
);
702 spin_unlock(&fc
->lock
);
705 static void fuse_short_read(struct fuse_req
*req
, struct inode
*inode
,
708 size_t num_read
= req
->out
.args
[0].size
;
709 struct fuse_conn
*fc
= get_fuse_conn(inode
);
711 if (fc
->writeback_cache
) {
713 * A hole in a file. Some data after the hole are in page cache,
714 * but have not reached the client fs yet. So, the hole is not
718 int start_idx
= num_read
>> PAGE_CACHE_SHIFT
;
719 size_t off
= num_read
& (PAGE_CACHE_SIZE
- 1);
721 for (i
= start_idx
; i
< req
->num_pages
; i
++) {
722 zero_user_segment(req
->pages
[i
], off
, PAGE_CACHE_SIZE
);
726 loff_t pos
= page_offset(req
->pages
[0]) + num_read
;
727 fuse_read_update_size(inode
, pos
, attr_ver
);
731 static int fuse_do_readpage(struct file
*file
, struct page
*page
)
733 struct fuse_io_priv io
= { .async
= 0, .file
= file
};
734 struct inode
*inode
= page
->mapping
->host
;
735 struct fuse_conn
*fc
= get_fuse_conn(inode
);
736 struct fuse_req
*req
;
738 loff_t pos
= page_offset(page
);
739 size_t count
= PAGE_CACHE_SIZE
;
744 * Page writeback can extend beyond the lifetime of the
745 * page-cache page, so make sure we read a properly synced
748 fuse_wait_on_page_writeback(inode
, page
->index
);
750 req
= fuse_get_req(fc
, 1);
754 attr_ver
= fuse_get_attr_version(fc
);
756 req
->out
.page_zeroing
= 1;
757 req
->out
.argpages
= 1;
759 req
->pages
[0] = page
;
760 req
->page_descs
[0].length
= count
;
761 num_read
= fuse_send_read(req
, &io
, pos
, count
, NULL
);
762 err
= req
->out
.h
.error
;
766 * Short read means EOF. If file size is larger, truncate it
768 if (num_read
< count
)
769 fuse_short_read(req
, inode
, attr_ver
);
771 SetPageUptodate(page
);
774 fuse_put_request(fc
, req
);
779 static int fuse_readpage(struct file
*file
, struct page
*page
)
781 struct inode
*inode
= page
->mapping
->host
;
785 if (is_bad_inode(inode
))
788 err
= fuse_do_readpage(file
, page
);
789 fuse_invalidate_atime(inode
);
795 static void fuse_readpages_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
798 size_t count
= req
->misc
.read
.in
.size
;
799 size_t num_read
= req
->out
.args
[0].size
;
800 struct address_space
*mapping
= NULL
;
802 for (i
= 0; mapping
== NULL
&& i
< req
->num_pages
; i
++)
803 mapping
= req
->pages
[i
]->mapping
;
806 struct inode
*inode
= mapping
->host
;
809 * Short read means EOF. If file size is larger, truncate it
811 if (!req
->out
.h
.error
&& num_read
< count
)
812 fuse_short_read(req
, inode
, req
->misc
.read
.attr_ver
);
814 fuse_invalidate_atime(inode
);
817 for (i
= 0; i
< req
->num_pages
; i
++) {
818 struct page
*page
= req
->pages
[i
];
819 if (!req
->out
.h
.error
)
820 SetPageUptodate(page
);
824 page_cache_release(page
);
827 fuse_file_put(req
->ff
, false);
830 static void fuse_send_readpages(struct fuse_req
*req
, struct file
*file
)
832 struct fuse_file
*ff
= file
->private_data
;
833 struct fuse_conn
*fc
= ff
->fc
;
834 loff_t pos
= page_offset(req
->pages
[0]);
835 size_t count
= req
->num_pages
<< PAGE_CACHE_SHIFT
;
837 req
->out
.argpages
= 1;
838 req
->out
.page_zeroing
= 1;
839 req
->out
.page_replace
= 1;
840 fuse_read_fill(req
, file
, pos
, count
, FUSE_READ
);
841 req
->misc
.read
.attr_ver
= fuse_get_attr_version(fc
);
842 if (fc
->async_read
) {
843 req
->ff
= fuse_file_get(ff
);
844 req
->end
= fuse_readpages_end
;
845 fuse_request_send_background(fc
, req
);
847 fuse_request_send(fc
, req
);
848 fuse_readpages_end(fc
, req
);
849 fuse_put_request(fc
, req
);
853 struct fuse_fill_data
{
854 struct fuse_req
*req
;
860 static int fuse_readpages_fill(void *_data
, struct page
*page
)
862 struct fuse_fill_data
*data
= _data
;
863 struct fuse_req
*req
= data
->req
;
864 struct inode
*inode
= data
->inode
;
865 struct fuse_conn
*fc
= get_fuse_conn(inode
);
867 fuse_wait_on_page_writeback(inode
, page
->index
);
869 if (req
->num_pages
&&
870 (req
->num_pages
== FUSE_MAX_PAGES_PER_REQ
||
871 (req
->num_pages
+ 1) * PAGE_CACHE_SIZE
> fc
->max_read
||
872 req
->pages
[req
->num_pages
- 1]->index
+ 1 != page
->index
)) {
873 int nr_alloc
= min_t(unsigned, data
->nr_pages
,
874 FUSE_MAX_PAGES_PER_REQ
);
875 fuse_send_readpages(req
, data
->file
);
877 req
= fuse_get_req_for_background(fc
, nr_alloc
);
879 req
= fuse_get_req(fc
, nr_alloc
);
888 if (WARN_ON(req
->num_pages
>= req
->max_pages
)) {
889 fuse_put_request(fc
, req
);
893 page_cache_get(page
);
894 req
->pages
[req
->num_pages
] = page
;
895 req
->page_descs
[req
->num_pages
].length
= PAGE_SIZE
;
901 static int fuse_readpages(struct file
*file
, struct address_space
*mapping
,
902 struct list_head
*pages
, unsigned nr_pages
)
904 struct inode
*inode
= mapping
->host
;
905 struct fuse_conn
*fc
= get_fuse_conn(inode
);
906 struct fuse_fill_data data
;
908 int nr_alloc
= min_t(unsigned, nr_pages
, FUSE_MAX_PAGES_PER_REQ
);
911 if (is_bad_inode(inode
))
917 data
.req
= fuse_get_req_for_background(fc
, nr_alloc
);
919 data
.req
= fuse_get_req(fc
, nr_alloc
);
920 data
.nr_pages
= nr_pages
;
921 err
= PTR_ERR(data
.req
);
922 if (IS_ERR(data
.req
))
925 err
= read_cache_pages(mapping
, pages
, fuse_readpages_fill
, &data
);
927 if (data
.req
->num_pages
)
928 fuse_send_readpages(data
.req
, file
);
930 fuse_put_request(fc
, data
.req
);
936 static ssize_t
fuse_file_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
938 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
939 struct fuse_conn
*fc
= get_fuse_conn(inode
);
942 * In auto invalidate mode, always update attributes on read.
943 * Otherwise, only update if we attempt to read past EOF (to ensure
944 * i_size is up to date).
946 if (fc
->auto_inval_data
||
947 (iocb
->ki_pos
+ iov_iter_count(to
) > i_size_read(inode
))) {
949 err
= fuse_update_attributes(inode
, NULL
, iocb
->ki_filp
, NULL
);
954 return generic_file_read_iter(iocb
, to
);
957 static void fuse_write_fill(struct fuse_req
*req
, struct fuse_file
*ff
,
958 loff_t pos
, size_t count
)
960 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
961 struct fuse_write_out
*outarg
= &req
->misc
.write
.out
;
966 req
->in
.h
.opcode
= FUSE_WRITE
;
967 req
->in
.h
.nodeid
= ff
->nodeid
;
969 if (ff
->fc
->minor
< 9)
970 req
->in
.args
[0].size
= FUSE_COMPAT_WRITE_IN_SIZE
;
972 req
->in
.args
[0].size
= sizeof(struct fuse_write_in
);
973 req
->in
.args
[0].value
= inarg
;
974 req
->in
.args
[1].size
= count
;
975 req
->out
.numargs
= 1;
976 req
->out
.args
[0].size
= sizeof(struct fuse_write_out
);
977 req
->out
.args
[0].value
= outarg
;
980 static size_t fuse_send_write(struct fuse_req
*req
, struct fuse_io_priv
*io
,
981 loff_t pos
, size_t count
, fl_owner_t owner
)
983 struct file
*file
= io
->file
;
984 struct fuse_file
*ff
= file
->private_data
;
985 struct fuse_conn
*fc
= ff
->fc
;
986 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
988 fuse_write_fill(req
, ff
, pos
, count
);
989 inarg
->flags
= file
->f_flags
;
991 inarg
->write_flags
|= FUSE_WRITE_LOCKOWNER
;
992 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
996 return fuse_async_req_send(fc
, req
, count
, io
);
998 fuse_request_send(fc
, req
);
999 return req
->misc
.write
.out
.size
;
1002 bool fuse_write_update_size(struct inode
*inode
, loff_t pos
)
1004 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1005 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1008 spin_lock(&fc
->lock
);
1009 fi
->attr_version
= ++fc
->attr_version
;
1010 if (pos
> inode
->i_size
) {
1011 i_size_write(inode
, pos
);
1014 spin_unlock(&fc
->lock
);
1019 static size_t fuse_send_write_pages(struct fuse_req
*req
, struct file
*file
,
1020 struct inode
*inode
, loff_t pos
,
1026 struct fuse_io_priv io
= { .async
= 0, .file
= file
};
1028 for (i
= 0; i
< req
->num_pages
; i
++)
1029 fuse_wait_on_page_writeback(inode
, req
->pages
[i
]->index
);
1031 res
= fuse_send_write(req
, &io
, pos
, count
, NULL
);
1033 offset
= req
->page_descs
[0].offset
;
1035 for (i
= 0; i
< req
->num_pages
; i
++) {
1036 struct page
*page
= req
->pages
[i
];
1038 if (!req
->out
.h
.error
&& !offset
&& count
>= PAGE_CACHE_SIZE
)
1039 SetPageUptodate(page
);
1041 if (count
> PAGE_CACHE_SIZE
- offset
)
1042 count
-= PAGE_CACHE_SIZE
- offset
;
1048 page_cache_release(page
);
1054 static ssize_t
fuse_fill_write_pages(struct fuse_req
*req
,
1055 struct address_space
*mapping
,
1056 struct iov_iter
*ii
, loff_t pos
)
1058 struct fuse_conn
*fc
= get_fuse_conn(mapping
->host
);
1059 unsigned offset
= pos
& (PAGE_CACHE_SIZE
- 1);
1063 req
->in
.argpages
= 1;
1064 req
->page_descs
[0].offset
= offset
;
1069 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
1070 size_t bytes
= min_t(size_t, PAGE_CACHE_SIZE
- offset
,
1071 iov_iter_count(ii
));
1073 bytes
= min_t(size_t, bytes
, fc
->max_write
- count
);
1077 if (iov_iter_fault_in_readable(ii
, bytes
))
1081 page
= grab_cache_page_write_begin(mapping
, index
, 0);
1085 if (mapping_writably_mapped(mapping
))
1086 flush_dcache_page(page
);
1088 tmp
= iov_iter_copy_from_user_atomic(page
, ii
, offset
, bytes
);
1089 flush_dcache_page(page
);
1093 page_cache_release(page
);
1094 bytes
= min(bytes
, iov_iter_single_seg_count(ii
));
1099 req
->pages
[req
->num_pages
] = page
;
1100 req
->page_descs
[req
->num_pages
].length
= tmp
;
1103 iov_iter_advance(ii
, tmp
);
1107 if (offset
== PAGE_CACHE_SIZE
)
1110 if (!fc
->big_writes
)
1112 } while (iov_iter_count(ii
) && count
< fc
->max_write
&&
1113 req
->num_pages
< req
->max_pages
&& offset
== 0);
1115 return count
> 0 ? count
: err
;
1118 static inline unsigned fuse_wr_pages(loff_t pos
, size_t len
)
1120 return min_t(unsigned,
1121 ((pos
+ len
- 1) >> PAGE_CACHE_SHIFT
) -
1122 (pos
>> PAGE_CACHE_SHIFT
) + 1,
1123 FUSE_MAX_PAGES_PER_REQ
);
1126 static ssize_t
fuse_perform_write(struct file
*file
,
1127 struct address_space
*mapping
,
1128 struct iov_iter
*ii
, loff_t pos
)
1130 struct inode
*inode
= mapping
->host
;
1131 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1132 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1136 if (is_bad_inode(inode
))
1139 if (inode
->i_size
< pos
+ iov_iter_count(ii
))
1140 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1143 struct fuse_req
*req
;
1145 unsigned nr_pages
= fuse_wr_pages(pos
, iov_iter_count(ii
));
1147 req
= fuse_get_req(fc
, nr_pages
);
1153 count
= fuse_fill_write_pages(req
, mapping
, ii
, pos
);
1159 num_written
= fuse_send_write_pages(req
, file
, inode
,
1161 err
= req
->out
.h
.error
;
1166 /* break out of the loop on short write */
1167 if (num_written
!= count
)
1171 fuse_put_request(fc
, req
);
1172 } while (!err
&& iov_iter_count(ii
));
1175 fuse_write_update_size(inode
, pos
);
1177 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1178 fuse_invalidate_attr(inode
);
1180 return res
> 0 ? res
: err
;
1183 static ssize_t
fuse_file_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
1185 struct file
*file
= iocb
->ki_filp
;
1186 struct address_space
*mapping
= file
->f_mapping
;
1187 size_t count
= iov_iter_count(from
);
1188 ssize_t written
= 0;
1189 ssize_t written_buffered
= 0;
1190 struct inode
*inode
= mapping
->host
;
1193 loff_t pos
= iocb
->ki_pos
;
1195 if (get_fuse_conn(inode
)->writeback_cache
) {
1196 /* Update size (EOF optimization) and mode (SUID clearing) */
1197 err
= fuse_update_attributes(mapping
->host
, NULL
, file
, NULL
);
1201 return generic_file_write_iter(iocb
, from
);
1204 mutex_lock(&inode
->i_mutex
);
1206 /* We can write back this queue in page reclaim */
1207 current
->backing_dev_info
= mapping
->backing_dev_info
;
1209 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
1216 iov_iter_truncate(from
, count
);
1217 err
= file_remove_suid(file
);
1221 err
= file_update_time(file
);
1225 if (file
->f_flags
& O_DIRECT
) {
1226 written
= generic_file_direct_write(iocb
, from
, pos
);
1227 if (written
< 0 || !iov_iter_count(from
))
1232 written_buffered
= fuse_perform_write(file
, mapping
, from
, pos
);
1233 if (written_buffered
< 0) {
1234 err
= written_buffered
;
1237 endbyte
= pos
+ written_buffered
- 1;
1239 err
= filemap_write_and_wait_range(file
->f_mapping
, pos
,
1244 invalidate_mapping_pages(file
->f_mapping
,
1245 pos
>> PAGE_CACHE_SHIFT
,
1246 endbyte
>> PAGE_CACHE_SHIFT
);
1248 written
+= written_buffered
;
1249 iocb
->ki_pos
= pos
+ written_buffered
;
1251 written
= fuse_perform_write(file
, mapping
, from
, pos
);
1253 iocb
->ki_pos
= pos
+ written
;
1256 current
->backing_dev_info
= NULL
;
1257 mutex_unlock(&inode
->i_mutex
);
1259 return written
? written
: err
;
1262 static inline void fuse_page_descs_length_init(struct fuse_req
*req
,
1263 unsigned index
, unsigned nr_pages
)
1267 for (i
= index
; i
< index
+ nr_pages
; i
++)
1268 req
->page_descs
[i
].length
= PAGE_SIZE
-
1269 req
->page_descs
[i
].offset
;
1272 static inline unsigned long fuse_get_user_addr(const struct iov_iter
*ii
)
1274 return (unsigned long)ii
->iov
->iov_base
+ ii
->iov_offset
;
1277 static inline size_t fuse_get_frag_size(const struct iov_iter
*ii
,
1280 return min(iov_iter_single_seg_count(ii
), max_size
);
1283 static int fuse_get_user_pages(struct fuse_req
*req
, struct iov_iter
*ii
,
1284 size_t *nbytesp
, int write
)
1286 size_t nbytes
= 0; /* # bytes already packed in req */
1288 /* Special case for kernel I/O: can copy directly into the buffer */
1289 if (ii
->type
& ITER_KVEC
) {
1290 unsigned long user_addr
= fuse_get_user_addr(ii
);
1291 size_t frag_size
= fuse_get_frag_size(ii
, *nbytesp
);
1294 req
->in
.args
[1].value
= (void *) user_addr
;
1296 req
->out
.args
[0].value
= (void *) user_addr
;
1298 iov_iter_advance(ii
, frag_size
);
1299 *nbytesp
= frag_size
;
1303 while (nbytes
< *nbytesp
&& req
->num_pages
< req
->max_pages
) {
1306 unsigned n
= req
->max_pages
- req
->num_pages
;
1307 ssize_t ret
= iov_iter_get_pages(ii
,
1308 &req
->pages
[req
->num_pages
],
1309 n
* PAGE_SIZE
, &start
);
1313 iov_iter_advance(ii
, ret
);
1317 npages
= (ret
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
1319 req
->page_descs
[req
->num_pages
].offset
= start
;
1320 fuse_page_descs_length_init(req
, req
->num_pages
, npages
);
1322 req
->num_pages
+= npages
;
1323 req
->page_descs
[req
->num_pages
- 1].length
-=
1324 (PAGE_SIZE
- ret
) & (PAGE_SIZE
- 1);
1328 req
->in
.argpages
= 1;
1330 req
->out
.argpages
= 1;
1337 static inline int fuse_iter_npages(const struct iov_iter
*ii_p
)
1339 return iov_iter_npages(ii_p
, FUSE_MAX_PAGES_PER_REQ
);
1342 ssize_t
fuse_direct_io(struct fuse_io_priv
*io
, struct iov_iter
*iter
,
1343 loff_t
*ppos
, int flags
)
1345 int write
= flags
& FUSE_DIO_WRITE
;
1346 int cuse
= flags
& FUSE_DIO_CUSE
;
1347 struct file
*file
= io
->file
;
1348 struct inode
*inode
= file
->f_mapping
->host
;
1349 struct fuse_file
*ff
= file
->private_data
;
1350 struct fuse_conn
*fc
= ff
->fc
;
1351 size_t nmax
= write
? fc
->max_write
: fc
->max_read
;
1353 size_t count
= iov_iter_count(iter
);
1354 pgoff_t idx_from
= pos
>> PAGE_CACHE_SHIFT
;
1355 pgoff_t idx_to
= (pos
+ count
- 1) >> PAGE_CACHE_SHIFT
;
1357 struct fuse_req
*req
;
1360 req
= fuse_get_req_for_background(fc
, fuse_iter_npages(iter
));
1362 req
= fuse_get_req(fc
, fuse_iter_npages(iter
));
1364 return PTR_ERR(req
);
1366 if (!cuse
&& fuse_range_is_writeback(inode
, idx_from
, idx_to
)) {
1368 mutex_lock(&inode
->i_mutex
);
1369 fuse_sync_writes(inode
);
1371 mutex_unlock(&inode
->i_mutex
);
1376 fl_owner_t owner
= current
->files
;
1377 size_t nbytes
= min(count
, nmax
);
1378 int err
= fuse_get_user_pages(req
, iter
, &nbytes
, write
);
1385 nres
= fuse_send_write(req
, io
, pos
, nbytes
, owner
);
1387 nres
= fuse_send_read(req
, io
, pos
, nbytes
, owner
);
1390 fuse_release_user_pages(req
, !write
);
1391 if (req
->out
.h
.error
) {
1393 res
= req
->out
.h
.error
;
1395 } else if (nres
> nbytes
) {
1405 fuse_put_request(fc
, req
);
1407 req
= fuse_get_req_for_background(fc
,
1408 fuse_iter_npages(iter
));
1410 req
= fuse_get_req(fc
, fuse_iter_npages(iter
));
1416 fuse_put_request(fc
, req
);
1422 EXPORT_SYMBOL_GPL(fuse_direct_io
);
1424 static ssize_t
__fuse_direct_read(struct fuse_io_priv
*io
,
1425 struct iov_iter
*iter
,
1429 struct file
*file
= io
->file
;
1430 struct inode
*inode
= file_inode(file
);
1432 if (is_bad_inode(inode
))
1435 res
= fuse_direct_io(io
, iter
, ppos
, 0);
1437 fuse_invalidate_attr(inode
);
1442 static ssize_t
fuse_direct_read(struct file
*file
, char __user
*buf
,
1443 size_t count
, loff_t
*ppos
)
1445 struct fuse_io_priv io
= { .async
= 0, .file
= file
};
1446 struct iovec iov
= { .iov_base
= buf
, .iov_len
= count
};
1448 iov_iter_init(&ii
, READ
, &iov
, 1, count
);
1449 return __fuse_direct_read(&io
, &ii
, ppos
);
1452 static ssize_t
__fuse_direct_write(struct fuse_io_priv
*io
,
1453 struct iov_iter
*iter
,
1456 struct file
*file
= io
->file
;
1457 struct inode
*inode
= file_inode(file
);
1458 size_t count
= iov_iter_count(iter
);
1462 res
= generic_write_checks(file
, ppos
, &count
, 0);
1464 iov_iter_truncate(iter
, count
);
1465 res
= fuse_direct_io(io
, iter
, ppos
, FUSE_DIO_WRITE
);
1468 fuse_invalidate_attr(inode
);
1473 static ssize_t
fuse_direct_write(struct file
*file
, const char __user
*buf
,
1474 size_t count
, loff_t
*ppos
)
1476 struct iovec iov
= { .iov_base
= (void __user
*)buf
, .iov_len
= count
};
1477 struct inode
*inode
= file_inode(file
);
1479 struct fuse_io_priv io
= { .async
= 0, .file
= file
};
1481 iov_iter_init(&ii
, WRITE
, &iov
, 1, count
);
1483 if (is_bad_inode(inode
))
1486 /* Don't allow parallel writes to the same file */
1487 mutex_lock(&inode
->i_mutex
);
1488 res
= __fuse_direct_write(&io
, &ii
, ppos
);
1490 fuse_write_update_size(inode
, *ppos
);
1491 mutex_unlock(&inode
->i_mutex
);
1496 static void fuse_writepage_free(struct fuse_conn
*fc
, struct fuse_req
*req
)
1500 for (i
= 0; i
< req
->num_pages
; i
++)
1501 __free_page(req
->pages
[i
]);
1504 fuse_file_put(req
->ff
, false);
1507 static void fuse_writepage_finish(struct fuse_conn
*fc
, struct fuse_req
*req
)
1509 struct inode
*inode
= req
->inode
;
1510 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1511 struct backing_dev_info
*bdi
= inode
->i_mapping
->backing_dev_info
;
1514 list_del(&req
->writepages_entry
);
1515 for (i
= 0; i
< req
->num_pages
; i
++) {
1516 dec_bdi_stat(bdi
, BDI_WRITEBACK
);
1517 dec_zone_page_state(req
->pages
[i
], NR_WRITEBACK_TEMP
);
1518 bdi_writeout_inc(bdi
);
1520 wake_up(&fi
->page_waitq
);
1523 /* Called under fc->lock, may release and reacquire it */
1524 static void fuse_send_writepage(struct fuse_conn
*fc
, struct fuse_req
*req
,
1526 __releases(fc
->lock
)
1527 __acquires(fc
->lock
)
1529 struct fuse_inode
*fi
= get_fuse_inode(req
->inode
);
1530 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
1531 __u64 data_size
= req
->num_pages
* PAGE_CACHE_SIZE
;
1536 if (inarg
->offset
+ data_size
<= size
) {
1537 inarg
->size
= data_size
;
1538 } else if (inarg
->offset
< size
) {
1539 inarg
->size
= size
- inarg
->offset
;
1541 /* Got truncated off completely */
1545 req
->in
.args
[1].size
= inarg
->size
;
1547 fuse_request_send_background_locked(fc
, req
);
1551 fuse_writepage_finish(fc
, req
);
1552 spin_unlock(&fc
->lock
);
1553 fuse_writepage_free(fc
, req
);
1554 fuse_put_request(fc
, req
);
1555 spin_lock(&fc
->lock
);
1559 * If fi->writectr is positive (no truncate or fsync going on) send
1560 * all queued writepage requests.
1562 * Called with fc->lock
1564 void fuse_flush_writepages(struct inode
*inode
)
1565 __releases(fc
->lock
)
1566 __acquires(fc
->lock
)
1568 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1569 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1570 size_t crop
= i_size_read(inode
);
1571 struct fuse_req
*req
;
1573 while (fi
->writectr
>= 0 && !list_empty(&fi
->queued_writes
)) {
1574 req
= list_entry(fi
->queued_writes
.next
, struct fuse_req
, list
);
1575 list_del_init(&req
->list
);
1576 fuse_send_writepage(fc
, req
, crop
);
1580 static void fuse_writepage_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
1582 struct inode
*inode
= req
->inode
;
1583 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1585 mapping_set_error(inode
->i_mapping
, req
->out
.h
.error
);
1586 spin_lock(&fc
->lock
);
1587 while (req
->misc
.write
.next
) {
1588 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1589 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
1590 struct fuse_req
*next
= req
->misc
.write
.next
;
1591 req
->misc
.write
.next
= next
->misc
.write
.next
;
1592 next
->misc
.write
.next
= NULL
;
1593 next
->ff
= fuse_file_get(req
->ff
);
1594 list_add(&next
->writepages_entry
, &fi
->writepages
);
1597 * Skip fuse_flush_writepages() to make it easy to crop requests
1598 * based on primary request size.
1600 * 1st case (trivial): there are no concurrent activities using
1601 * fuse_set/release_nowrite. Then we're on safe side because
1602 * fuse_flush_writepages() would call fuse_send_writepage()
1605 * 2nd case: someone called fuse_set_nowrite and it is waiting
1606 * now for completion of all in-flight requests. This happens
1607 * rarely and no more than once per page, so this should be
1610 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1611 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1612 * that fuse_set_nowrite returned implies that all in-flight
1613 * requests were completed along with all of their secondary
1614 * requests. Further primary requests are blocked by negative
1615 * writectr. Hence there cannot be any in-flight requests and
1616 * no invocations of fuse_writepage_end() while we're in
1617 * fuse_set_nowrite..fuse_release_nowrite section.
1619 fuse_send_writepage(fc
, next
, inarg
->offset
+ inarg
->size
);
1622 fuse_writepage_finish(fc
, req
);
1623 spin_unlock(&fc
->lock
);
1624 fuse_writepage_free(fc
, req
);
1627 static struct fuse_file
*__fuse_write_file_get(struct fuse_conn
*fc
,
1628 struct fuse_inode
*fi
)
1630 struct fuse_file
*ff
= NULL
;
1632 spin_lock(&fc
->lock
);
1633 if (!list_empty(&fi
->write_files
)) {
1634 ff
= list_entry(fi
->write_files
.next
, struct fuse_file
,
1638 spin_unlock(&fc
->lock
);
1643 static struct fuse_file
*fuse_write_file_get(struct fuse_conn
*fc
,
1644 struct fuse_inode
*fi
)
1646 struct fuse_file
*ff
= __fuse_write_file_get(fc
, fi
);
1651 int fuse_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
1653 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1654 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1655 struct fuse_file
*ff
;
1658 ff
= __fuse_write_file_get(fc
, fi
);
1659 err
= fuse_flush_times(inode
, ff
);
1661 fuse_file_put(ff
, 0);
1666 static int fuse_writepage_locked(struct page
*page
)
1668 struct address_space
*mapping
= page
->mapping
;
1669 struct inode
*inode
= mapping
->host
;
1670 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1671 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1672 struct fuse_req
*req
;
1673 struct page
*tmp_page
;
1674 int error
= -ENOMEM
;
1676 set_page_writeback(page
);
1678 req
= fuse_request_alloc_nofs(1);
1682 req
->background
= 1; /* writeback always goes to bg_queue */
1683 tmp_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
1688 req
->ff
= fuse_write_file_get(fc
, fi
);
1692 fuse_write_fill(req
, req
->ff
, page_offset(page
), 0);
1694 copy_highpage(tmp_page
, page
);
1695 req
->misc
.write
.in
.write_flags
|= FUSE_WRITE_CACHE
;
1696 req
->misc
.write
.next
= NULL
;
1697 req
->in
.argpages
= 1;
1699 req
->pages
[0] = tmp_page
;
1700 req
->page_descs
[0].offset
= 0;
1701 req
->page_descs
[0].length
= PAGE_SIZE
;
1702 req
->end
= fuse_writepage_end
;
1705 inc_bdi_stat(mapping
->backing_dev_info
, BDI_WRITEBACK
);
1706 inc_zone_page_state(tmp_page
, NR_WRITEBACK_TEMP
);
1708 spin_lock(&fc
->lock
);
1709 list_add(&req
->writepages_entry
, &fi
->writepages
);
1710 list_add_tail(&req
->list
, &fi
->queued_writes
);
1711 fuse_flush_writepages(inode
);
1712 spin_unlock(&fc
->lock
);
1714 end_page_writeback(page
);
1719 fuse_request_free(req
);
1721 end_page_writeback(page
);
1725 static int fuse_writepage(struct page
*page
, struct writeback_control
*wbc
)
1729 if (fuse_page_is_writeback(page
->mapping
->host
, page
->index
)) {
1731 * ->writepages() should be called for sync() and friends. We
1732 * should only get here on direct reclaim and then we are
1733 * allowed to skip a page which is already in flight
1735 WARN_ON(wbc
->sync_mode
== WB_SYNC_ALL
);
1737 redirty_page_for_writepage(wbc
, page
);
1741 err
= fuse_writepage_locked(page
);
1747 struct fuse_fill_wb_data
{
1748 struct fuse_req
*req
;
1749 struct fuse_file
*ff
;
1750 struct inode
*inode
;
1751 struct page
**orig_pages
;
1754 static void fuse_writepages_send(struct fuse_fill_wb_data
*data
)
1756 struct fuse_req
*req
= data
->req
;
1757 struct inode
*inode
= data
->inode
;
1758 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1759 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1760 int num_pages
= req
->num_pages
;
1763 req
->ff
= fuse_file_get(data
->ff
);
1764 spin_lock(&fc
->lock
);
1765 list_add_tail(&req
->list
, &fi
->queued_writes
);
1766 fuse_flush_writepages(inode
);
1767 spin_unlock(&fc
->lock
);
1769 for (i
= 0; i
< num_pages
; i
++)
1770 end_page_writeback(data
->orig_pages
[i
]);
1773 static bool fuse_writepage_in_flight(struct fuse_req
*new_req
,
1776 struct fuse_conn
*fc
= get_fuse_conn(new_req
->inode
);
1777 struct fuse_inode
*fi
= get_fuse_inode(new_req
->inode
);
1778 struct fuse_req
*tmp
;
1779 struct fuse_req
*old_req
;
1783 BUG_ON(new_req
->num_pages
!= 0);
1785 spin_lock(&fc
->lock
);
1786 list_del(&new_req
->writepages_entry
);
1787 list_for_each_entry(old_req
, &fi
->writepages
, writepages_entry
) {
1788 BUG_ON(old_req
->inode
!= new_req
->inode
);
1789 curr_index
= old_req
->misc
.write
.in
.offset
>> PAGE_CACHE_SHIFT
;
1790 if (curr_index
<= page
->index
&&
1791 page
->index
< curr_index
+ old_req
->num_pages
) {
1797 list_add(&new_req
->writepages_entry
, &fi
->writepages
);
1801 new_req
->num_pages
= 1;
1802 for (tmp
= old_req
; tmp
!= NULL
; tmp
= tmp
->misc
.write
.next
) {
1803 BUG_ON(tmp
->inode
!= new_req
->inode
);
1804 curr_index
= tmp
->misc
.write
.in
.offset
>> PAGE_CACHE_SHIFT
;
1805 if (tmp
->num_pages
== 1 &&
1806 curr_index
== page
->index
) {
1811 if (old_req
->num_pages
== 1 && (old_req
->state
== FUSE_REQ_INIT
||
1812 old_req
->state
== FUSE_REQ_PENDING
)) {
1813 struct backing_dev_info
*bdi
= page
->mapping
->backing_dev_info
;
1815 copy_highpage(old_req
->pages
[0], page
);
1816 spin_unlock(&fc
->lock
);
1818 dec_bdi_stat(bdi
, BDI_WRITEBACK
);
1819 dec_zone_page_state(page
, NR_WRITEBACK_TEMP
);
1820 bdi_writeout_inc(bdi
);
1821 fuse_writepage_free(fc
, new_req
);
1822 fuse_request_free(new_req
);
1825 new_req
->misc
.write
.next
= old_req
->misc
.write
.next
;
1826 old_req
->misc
.write
.next
= new_req
;
1829 spin_unlock(&fc
->lock
);
1834 static int fuse_writepages_fill(struct page
*page
,
1835 struct writeback_control
*wbc
, void *_data
)
1837 struct fuse_fill_wb_data
*data
= _data
;
1838 struct fuse_req
*req
= data
->req
;
1839 struct inode
*inode
= data
->inode
;
1840 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1841 struct page
*tmp_page
;
1847 data
->ff
= fuse_write_file_get(fc
, get_fuse_inode(inode
));
1853 * Being under writeback is unlikely but possible. For example direct
1854 * read to an mmaped fuse file will set the page dirty twice; once when
1855 * the pages are faulted with get_user_pages(), and then after the read
1858 is_writeback
= fuse_page_is_writeback(inode
, page
->index
);
1860 if (req
&& req
->num_pages
&&
1861 (is_writeback
|| req
->num_pages
== FUSE_MAX_PAGES_PER_REQ
||
1862 (req
->num_pages
+ 1) * PAGE_CACHE_SIZE
> fc
->max_write
||
1863 data
->orig_pages
[req
->num_pages
- 1]->index
+ 1 != page
->index
)) {
1864 fuse_writepages_send(data
);
1868 tmp_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
1873 * The page must not be redirtied until the writeout is completed
1874 * (i.e. userspace has sent a reply to the write request). Otherwise
1875 * there could be more than one temporary page instance for each real
1878 * This is ensured by holding the page lock in page_mkwrite() while
1879 * checking fuse_page_is_writeback(). We already hold the page lock
1880 * since clear_page_dirty_for_io() and keep it held until we add the
1881 * request to the fi->writepages list and increment req->num_pages.
1882 * After this fuse_page_is_writeback() will indicate that the page is
1883 * under writeback, so we can release the page lock.
1885 if (data
->req
== NULL
) {
1886 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1889 req
= fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ
);
1891 __free_page(tmp_page
);
1895 fuse_write_fill(req
, data
->ff
, page_offset(page
), 0);
1896 req
->misc
.write
.in
.write_flags
|= FUSE_WRITE_CACHE
;
1897 req
->misc
.write
.next
= NULL
;
1898 req
->in
.argpages
= 1;
1899 req
->background
= 1;
1901 req
->end
= fuse_writepage_end
;
1904 spin_lock(&fc
->lock
);
1905 list_add(&req
->writepages_entry
, &fi
->writepages
);
1906 spin_unlock(&fc
->lock
);
1910 set_page_writeback(page
);
1912 copy_highpage(tmp_page
, page
);
1913 req
->pages
[req
->num_pages
] = tmp_page
;
1914 req
->page_descs
[req
->num_pages
].offset
= 0;
1915 req
->page_descs
[req
->num_pages
].length
= PAGE_SIZE
;
1917 inc_bdi_stat(page
->mapping
->backing_dev_info
, BDI_WRITEBACK
);
1918 inc_zone_page_state(tmp_page
, NR_WRITEBACK_TEMP
);
1921 if (is_writeback
&& fuse_writepage_in_flight(req
, page
)) {
1922 end_page_writeback(page
);
1926 data
->orig_pages
[req
->num_pages
] = page
;
1929 * Protected by fc->lock against concurrent access by
1930 * fuse_page_is_writeback().
1932 spin_lock(&fc
->lock
);
1934 spin_unlock(&fc
->lock
);
1942 static int fuse_writepages(struct address_space
*mapping
,
1943 struct writeback_control
*wbc
)
1945 struct inode
*inode
= mapping
->host
;
1946 struct fuse_fill_wb_data data
;
1950 if (is_bad_inode(inode
))
1958 data
.orig_pages
= kzalloc(sizeof(struct page
*) *
1959 FUSE_MAX_PAGES_PER_REQ
,
1961 if (!data
.orig_pages
)
1964 err
= write_cache_pages(mapping
, wbc
, fuse_writepages_fill
, &data
);
1966 /* Ignore errors if we can write at least one page */
1967 BUG_ON(!data
.req
->num_pages
);
1968 fuse_writepages_send(&data
);
1972 fuse_file_put(data
.ff
, false);
1974 kfree(data
.orig_pages
);
1980 * It's worthy to make sure that space is reserved on disk for the write,
1981 * but how to implement it without killing performance need more thinking.
1983 static int fuse_write_begin(struct file
*file
, struct address_space
*mapping
,
1984 loff_t pos
, unsigned len
, unsigned flags
,
1985 struct page
**pagep
, void **fsdata
)
1987 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
1988 struct fuse_conn
*fc
= get_fuse_conn(file
->f_dentry
->d_inode
);
1993 WARN_ON(!fc
->writeback_cache
);
1995 page
= grab_cache_page_write_begin(mapping
, index
, flags
);
1999 fuse_wait_on_page_writeback(mapping
->host
, page
->index
);
2001 if (PageUptodate(page
) || len
== PAGE_CACHE_SIZE
)
2004 * Check if the start this page comes after the end of file, in which
2005 * case the readpage can be optimized away.
2007 fsize
= i_size_read(mapping
->host
);
2008 if (fsize
<= (pos
& PAGE_CACHE_MASK
)) {
2009 size_t off
= pos
& ~PAGE_CACHE_MASK
;
2011 zero_user_segment(page
, 0, off
);
2014 err
= fuse_do_readpage(file
, page
);
2023 page_cache_release(page
);
2028 static int fuse_write_end(struct file
*file
, struct address_space
*mapping
,
2029 loff_t pos
, unsigned len
, unsigned copied
,
2030 struct page
*page
, void *fsdata
)
2032 struct inode
*inode
= page
->mapping
->host
;
2034 if (!PageUptodate(page
)) {
2035 /* Zero any unwritten bytes at the end of the page */
2036 size_t endoff
= (pos
+ copied
) & ~PAGE_CACHE_MASK
;
2038 zero_user_segment(page
, endoff
, PAGE_CACHE_SIZE
);
2039 SetPageUptodate(page
);
2042 fuse_write_update_size(inode
, pos
+ copied
);
2043 set_page_dirty(page
);
2045 page_cache_release(page
);
2050 static int fuse_launder_page(struct page
*page
)
2053 if (clear_page_dirty_for_io(page
)) {
2054 struct inode
*inode
= page
->mapping
->host
;
2055 err
= fuse_writepage_locked(page
);
2057 fuse_wait_on_page_writeback(inode
, page
->index
);
2063 * Write back dirty pages now, because there may not be any suitable
2066 static void fuse_vma_close(struct vm_area_struct
*vma
)
2068 filemap_write_and_wait(vma
->vm_file
->f_mapping
);
2072 * Wait for writeback against this page to complete before allowing it
2073 * to be marked dirty again, and hence written back again, possibly
2074 * before the previous writepage completed.
2076 * Block here, instead of in ->writepage(), so that the userspace fs
2077 * can only block processes actually operating on the filesystem.
2079 * Otherwise unprivileged userspace fs would be able to block
2084 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2086 static int fuse_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
2088 struct page
*page
= vmf
->page
;
2089 struct inode
*inode
= file_inode(vma
->vm_file
);
2091 file_update_time(vma
->vm_file
);
2093 if (page
->mapping
!= inode
->i_mapping
) {
2095 return VM_FAULT_NOPAGE
;
2098 fuse_wait_on_page_writeback(inode
, page
->index
);
2099 return VM_FAULT_LOCKED
;
2102 static const struct vm_operations_struct fuse_file_vm_ops
= {
2103 .close
= fuse_vma_close
,
2104 .fault
= filemap_fault
,
2105 .map_pages
= filemap_map_pages
,
2106 .page_mkwrite
= fuse_page_mkwrite
,
2107 .remap_pages
= generic_file_remap_pages
,
2110 static int fuse_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2112 if ((vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
))
2113 fuse_link_write_file(file
);
2115 file_accessed(file
);
2116 vma
->vm_ops
= &fuse_file_vm_ops
;
2120 static int fuse_direct_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2122 /* Can't provide the coherency needed for MAP_SHARED */
2123 if (vma
->vm_flags
& VM_MAYSHARE
)
2126 invalidate_inode_pages2(file
->f_mapping
);
2128 return generic_file_mmap(file
, vma
);
2131 static int convert_fuse_file_lock(const struct fuse_file_lock
*ffl
,
2132 struct file_lock
*fl
)
2134 switch (ffl
->type
) {
2140 if (ffl
->start
> OFFSET_MAX
|| ffl
->end
> OFFSET_MAX
||
2141 ffl
->end
< ffl
->start
)
2144 fl
->fl_start
= ffl
->start
;
2145 fl
->fl_end
= ffl
->end
;
2146 fl
->fl_pid
= ffl
->pid
;
2152 fl
->fl_type
= ffl
->type
;
2156 static void fuse_lk_fill(struct fuse_req
*req
, struct file
*file
,
2157 const struct file_lock
*fl
, int opcode
, pid_t pid
,
2160 struct inode
*inode
= file_inode(file
);
2161 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2162 struct fuse_file
*ff
= file
->private_data
;
2163 struct fuse_lk_in
*arg
= &req
->misc
.lk_in
;
2166 arg
->owner
= fuse_lock_owner_id(fc
, fl
->fl_owner
);
2167 arg
->lk
.start
= fl
->fl_start
;
2168 arg
->lk
.end
= fl
->fl_end
;
2169 arg
->lk
.type
= fl
->fl_type
;
2172 arg
->lk_flags
|= FUSE_LK_FLOCK
;
2173 req
->in
.h
.opcode
= opcode
;
2174 req
->in
.h
.nodeid
= get_node_id(inode
);
2175 req
->in
.numargs
= 1;
2176 req
->in
.args
[0].size
= sizeof(*arg
);
2177 req
->in
.args
[0].value
= arg
;
2180 static int fuse_getlk(struct file
*file
, struct file_lock
*fl
)
2182 struct inode
*inode
= file_inode(file
);
2183 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2184 struct fuse_req
*req
;
2185 struct fuse_lk_out outarg
;
2188 req
= fuse_get_req_nopages(fc
);
2190 return PTR_ERR(req
);
2192 fuse_lk_fill(req
, file
, fl
, FUSE_GETLK
, 0, 0);
2193 req
->out
.numargs
= 1;
2194 req
->out
.args
[0].size
= sizeof(outarg
);
2195 req
->out
.args
[0].value
= &outarg
;
2196 fuse_request_send(fc
, req
);
2197 err
= req
->out
.h
.error
;
2198 fuse_put_request(fc
, req
);
2200 err
= convert_fuse_file_lock(&outarg
.lk
, fl
);
2205 static int fuse_setlk(struct file
*file
, struct file_lock
*fl
, int flock
)
2207 struct inode
*inode
= file_inode(file
);
2208 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2209 struct fuse_req
*req
;
2210 int opcode
= (fl
->fl_flags
& FL_SLEEP
) ? FUSE_SETLKW
: FUSE_SETLK
;
2211 pid_t pid
= fl
->fl_type
!= F_UNLCK
? current
->tgid
: 0;
2214 if (fl
->fl_lmops
&& fl
->fl_lmops
->lm_grant
) {
2215 /* NLM needs asynchronous locks, which we don't support yet */
2219 /* Unlock on close is handled by the flush method */
2220 if (fl
->fl_flags
& FL_CLOSE
)
2223 req
= fuse_get_req_nopages(fc
);
2225 return PTR_ERR(req
);
2227 fuse_lk_fill(req
, file
, fl
, opcode
, pid
, flock
);
2228 fuse_request_send(fc
, req
);
2229 err
= req
->out
.h
.error
;
2230 /* locking is restartable */
2233 fuse_put_request(fc
, req
);
2237 static int fuse_file_lock(struct file
*file
, int cmd
, struct file_lock
*fl
)
2239 struct inode
*inode
= file_inode(file
);
2240 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2243 if (cmd
== F_CANCELLK
) {
2245 } else if (cmd
== F_GETLK
) {
2247 posix_test_lock(file
, fl
);
2250 err
= fuse_getlk(file
, fl
);
2253 err
= posix_lock_file(file
, fl
, NULL
);
2255 err
= fuse_setlk(file
, fl
, 0);
2260 static int fuse_file_flock(struct file
*file
, int cmd
, struct file_lock
*fl
)
2262 struct inode
*inode
= file_inode(file
);
2263 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2267 err
= flock_lock_file_wait(file
, fl
);
2269 struct fuse_file
*ff
= file
->private_data
;
2271 /* emulate flock with POSIX locks */
2273 err
= fuse_setlk(file
, fl
, 1);
2279 static sector_t
fuse_bmap(struct address_space
*mapping
, sector_t block
)
2281 struct inode
*inode
= mapping
->host
;
2282 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2283 struct fuse_req
*req
;
2284 struct fuse_bmap_in inarg
;
2285 struct fuse_bmap_out outarg
;
2288 if (!inode
->i_sb
->s_bdev
|| fc
->no_bmap
)
2291 req
= fuse_get_req_nopages(fc
);
2295 memset(&inarg
, 0, sizeof(inarg
));
2296 inarg
.block
= block
;
2297 inarg
.blocksize
= inode
->i_sb
->s_blocksize
;
2298 req
->in
.h
.opcode
= FUSE_BMAP
;
2299 req
->in
.h
.nodeid
= get_node_id(inode
);
2300 req
->in
.numargs
= 1;
2301 req
->in
.args
[0].size
= sizeof(inarg
);
2302 req
->in
.args
[0].value
= &inarg
;
2303 req
->out
.numargs
= 1;
2304 req
->out
.args
[0].size
= sizeof(outarg
);
2305 req
->out
.args
[0].value
= &outarg
;
2306 fuse_request_send(fc
, req
);
2307 err
= req
->out
.h
.error
;
2308 fuse_put_request(fc
, req
);
2312 return err
? 0 : outarg
.block
;
2315 static loff_t
fuse_file_llseek(struct file
*file
, loff_t offset
, int whence
)
2318 struct inode
*inode
= file_inode(file
);
2320 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2321 if (whence
== SEEK_CUR
|| whence
== SEEK_SET
)
2322 return generic_file_llseek(file
, offset
, whence
);
2324 mutex_lock(&inode
->i_mutex
);
2325 retval
= fuse_update_attributes(inode
, NULL
, file
, NULL
);
2327 retval
= generic_file_llseek(file
, offset
, whence
);
2328 mutex_unlock(&inode
->i_mutex
);
2333 static int fuse_ioctl_copy_user(struct page
**pages
, struct iovec
*iov
,
2334 unsigned int nr_segs
, size_t bytes
, bool to_user
)
2342 iov_iter_init(&ii
, to_user
? READ
: WRITE
, iov
, nr_segs
, bytes
);
2344 while (iov_iter_count(&ii
)) {
2345 struct page
*page
= pages
[page_idx
++];
2346 size_t todo
= min_t(size_t, PAGE_SIZE
, iov_iter_count(&ii
));
2352 char __user
*uaddr
= ii
.iov
->iov_base
+ ii
.iov_offset
;
2353 size_t iov_len
= ii
.iov
->iov_len
- ii
.iov_offset
;
2354 size_t copy
= min(todo
, iov_len
);
2358 left
= copy_from_user(kaddr
, uaddr
, copy
);
2360 left
= copy_to_user(uaddr
, kaddr
, copy
);
2365 iov_iter_advance(&ii
, copy
);
2377 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2378 * ABI was defined to be 'struct iovec' which is different on 32bit
2379 * and 64bit. Fortunately we can determine which structure the server
2380 * used from the size of the reply.
2382 static int fuse_copy_ioctl_iovec_old(struct iovec
*dst
, void *src
,
2383 size_t transferred
, unsigned count
,
2386 #ifdef CONFIG_COMPAT
2387 if (count
* sizeof(struct compat_iovec
) == transferred
) {
2388 struct compat_iovec
*ciov
= src
;
2392 * With this interface a 32bit server cannot support
2393 * non-compat (i.e. ones coming from 64bit apps) ioctl
2399 for (i
= 0; i
< count
; i
++) {
2400 dst
[i
].iov_base
= compat_ptr(ciov
[i
].iov_base
);
2401 dst
[i
].iov_len
= ciov
[i
].iov_len
;
2407 if (count
* sizeof(struct iovec
) != transferred
)
2410 memcpy(dst
, src
, transferred
);
2414 /* Make sure iov_length() won't overflow */
2415 static int fuse_verify_ioctl_iov(struct iovec
*iov
, size_t count
)
2418 u32 max
= FUSE_MAX_PAGES_PER_REQ
<< PAGE_SHIFT
;
2420 for (n
= 0; n
< count
; n
++, iov
++) {
2421 if (iov
->iov_len
> (size_t) max
)
2423 max
-= iov
->iov_len
;
2428 static int fuse_copy_ioctl_iovec(struct fuse_conn
*fc
, struct iovec
*dst
,
2429 void *src
, size_t transferred
, unsigned count
,
2433 struct fuse_ioctl_iovec
*fiov
= src
;
2435 if (fc
->minor
< 16) {
2436 return fuse_copy_ioctl_iovec_old(dst
, src
, transferred
,
2440 if (count
* sizeof(struct fuse_ioctl_iovec
) != transferred
)
2443 for (i
= 0; i
< count
; i
++) {
2444 /* Did the server supply an inappropriate value? */
2445 if (fiov
[i
].base
!= (unsigned long) fiov
[i
].base
||
2446 fiov
[i
].len
!= (unsigned long) fiov
[i
].len
)
2449 dst
[i
].iov_base
= (void __user
*) (unsigned long) fiov
[i
].base
;
2450 dst
[i
].iov_len
= (size_t) fiov
[i
].len
;
2452 #ifdef CONFIG_COMPAT
2454 (ptr_to_compat(dst
[i
].iov_base
) != fiov
[i
].base
||
2455 (compat_size_t
) dst
[i
].iov_len
!= fiov
[i
].len
))
2465 * For ioctls, there is no generic way to determine how much memory
2466 * needs to be read and/or written. Furthermore, ioctls are allowed
2467 * to dereference the passed pointer, so the parameter requires deep
2468 * copying but FUSE has no idea whatsoever about what to copy in or
2471 * This is solved by allowing FUSE server to retry ioctl with
2472 * necessary in/out iovecs. Let's assume the ioctl implementation
2473 * needs to read in the following structure.
2480 * On the first callout to FUSE server, inarg->in_size and
2481 * inarg->out_size will be NULL; then, the server completes the ioctl
2482 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2483 * the actual iov array to
2485 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2487 * which tells FUSE to copy in the requested area and retry the ioctl.
2488 * On the second round, the server has access to the structure and
2489 * from that it can tell what to look for next, so on the invocation,
2490 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2492 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2493 * { .iov_base = a.buf, .iov_len = a.buflen } }
2495 * FUSE will copy both struct a and the pointed buffer from the
2496 * process doing the ioctl and retry ioctl with both struct a and the
2499 * This time, FUSE server has everything it needs and completes ioctl
2500 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2502 * Copying data out works the same way.
2504 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2505 * automatically initializes in and out iovs by decoding @cmd with
2506 * _IOC_* macros and the server is not allowed to request RETRY. This
2507 * limits ioctl data transfers to well-formed ioctls and is the forced
2508 * behavior for all FUSE servers.
2510 long fuse_do_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
,
2513 struct fuse_file
*ff
= file
->private_data
;
2514 struct fuse_conn
*fc
= ff
->fc
;
2515 struct fuse_ioctl_in inarg
= {
2521 struct fuse_ioctl_out outarg
;
2522 struct fuse_req
*req
= NULL
;
2523 struct page
**pages
= NULL
;
2524 struct iovec
*iov_page
= NULL
;
2525 struct iovec
*in_iov
= NULL
, *out_iov
= NULL
;
2526 unsigned int in_iovs
= 0, out_iovs
= 0, num_pages
= 0, max_pages
;
2527 size_t in_size
, out_size
, transferred
;
2530 #if BITS_PER_LONG == 32
2531 inarg
.flags
|= FUSE_IOCTL_32BIT
;
2533 if (flags
& FUSE_IOCTL_COMPAT
)
2534 inarg
.flags
|= FUSE_IOCTL_32BIT
;
2537 /* assume all the iovs returned by client always fits in a page */
2538 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec
) * FUSE_IOCTL_MAX_IOV
> PAGE_SIZE
);
2541 pages
= kcalloc(FUSE_MAX_PAGES_PER_REQ
, sizeof(pages
[0]), GFP_KERNEL
);
2542 iov_page
= (struct iovec
*) __get_free_page(GFP_KERNEL
);
2543 if (!pages
|| !iov_page
)
2547 * If restricted, initialize IO parameters as encoded in @cmd.
2548 * RETRY from server is not allowed.
2550 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
)) {
2551 struct iovec
*iov
= iov_page
;
2553 iov
->iov_base
= (void __user
*)arg
;
2554 iov
->iov_len
= _IOC_SIZE(cmd
);
2556 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
2561 if (_IOC_DIR(cmd
) & _IOC_READ
) {
2568 inarg
.in_size
= in_size
= iov_length(in_iov
, in_iovs
);
2569 inarg
.out_size
= out_size
= iov_length(out_iov
, out_iovs
);
2572 * Out data can be used either for actual out data or iovs,
2573 * make sure there always is at least one page.
2575 out_size
= max_t(size_t, out_size
, PAGE_SIZE
);
2576 max_pages
= DIV_ROUND_UP(max(in_size
, out_size
), PAGE_SIZE
);
2578 /* make sure there are enough buffer pages and init request with them */
2580 if (max_pages
> FUSE_MAX_PAGES_PER_REQ
)
2582 while (num_pages
< max_pages
) {
2583 pages
[num_pages
] = alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
2584 if (!pages
[num_pages
])
2589 req
= fuse_get_req(fc
, num_pages
);
2595 memcpy(req
->pages
, pages
, sizeof(req
->pages
[0]) * num_pages
);
2596 req
->num_pages
= num_pages
;
2597 fuse_page_descs_length_init(req
, 0, req
->num_pages
);
2599 /* okay, let's send it to the client */
2600 req
->in
.h
.opcode
= FUSE_IOCTL
;
2601 req
->in
.h
.nodeid
= ff
->nodeid
;
2602 req
->in
.numargs
= 1;
2603 req
->in
.args
[0].size
= sizeof(inarg
);
2604 req
->in
.args
[0].value
= &inarg
;
2607 req
->in
.args
[1].size
= in_size
;
2608 req
->in
.argpages
= 1;
2610 err
= fuse_ioctl_copy_user(pages
, in_iov
, in_iovs
, in_size
,
2616 req
->out
.numargs
= 2;
2617 req
->out
.args
[0].size
= sizeof(outarg
);
2618 req
->out
.args
[0].value
= &outarg
;
2619 req
->out
.args
[1].size
= out_size
;
2620 req
->out
.argpages
= 1;
2621 req
->out
.argvar
= 1;
2623 fuse_request_send(fc
, req
);
2624 err
= req
->out
.h
.error
;
2625 transferred
= req
->out
.args
[1].size
;
2626 fuse_put_request(fc
, req
);
2631 /* did it ask for retry? */
2632 if (outarg
.flags
& FUSE_IOCTL_RETRY
) {
2635 /* no retry if in restricted mode */
2637 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
))
2640 in_iovs
= outarg
.in_iovs
;
2641 out_iovs
= outarg
.out_iovs
;
2644 * Make sure things are in boundary, separate checks
2645 * are to protect against overflow.
2648 if (in_iovs
> FUSE_IOCTL_MAX_IOV
||
2649 out_iovs
> FUSE_IOCTL_MAX_IOV
||
2650 in_iovs
+ out_iovs
> FUSE_IOCTL_MAX_IOV
)
2653 vaddr
= kmap_atomic(pages
[0]);
2654 err
= fuse_copy_ioctl_iovec(fc
, iov_page
, vaddr
,
2655 transferred
, in_iovs
+ out_iovs
,
2656 (flags
& FUSE_IOCTL_COMPAT
) != 0);
2657 kunmap_atomic(vaddr
);
2662 out_iov
= in_iov
+ in_iovs
;
2664 err
= fuse_verify_ioctl_iov(in_iov
, in_iovs
);
2668 err
= fuse_verify_ioctl_iov(out_iov
, out_iovs
);
2676 if (transferred
> inarg
.out_size
)
2679 err
= fuse_ioctl_copy_user(pages
, out_iov
, out_iovs
, transferred
, true);
2682 fuse_put_request(fc
, req
);
2683 free_page((unsigned long) iov_page
);
2685 __free_page(pages
[--num_pages
]);
2688 return err
? err
: outarg
.result
;
2690 EXPORT_SYMBOL_GPL(fuse_do_ioctl
);
2692 long fuse_ioctl_common(struct file
*file
, unsigned int cmd
,
2693 unsigned long arg
, unsigned int flags
)
2695 struct inode
*inode
= file_inode(file
);
2696 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2698 if (!fuse_allow_current_process(fc
))
2701 if (is_bad_inode(inode
))
2704 return fuse_do_ioctl(file
, cmd
, arg
, flags
);
2707 static long fuse_file_ioctl(struct file
*file
, unsigned int cmd
,
2710 return fuse_ioctl_common(file
, cmd
, arg
, 0);
2713 static long fuse_file_compat_ioctl(struct file
*file
, unsigned int cmd
,
2716 return fuse_ioctl_common(file
, cmd
, arg
, FUSE_IOCTL_COMPAT
);
2720 * All files which have been polled are linked to RB tree
2721 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2722 * find the matching one.
2724 static struct rb_node
**fuse_find_polled_node(struct fuse_conn
*fc
, u64 kh
,
2725 struct rb_node
**parent_out
)
2727 struct rb_node
**link
= &fc
->polled_files
.rb_node
;
2728 struct rb_node
*last
= NULL
;
2731 struct fuse_file
*ff
;
2734 ff
= rb_entry(last
, struct fuse_file
, polled_node
);
2737 link
= &last
->rb_left
;
2738 else if (kh
> ff
->kh
)
2739 link
= &last
->rb_right
;
2750 * The file is about to be polled. Make sure it's on the polled_files
2751 * RB tree. Note that files once added to the polled_files tree are
2752 * not removed before the file is released. This is because a file
2753 * polled once is likely to be polled again.
2755 static void fuse_register_polled_file(struct fuse_conn
*fc
,
2756 struct fuse_file
*ff
)
2758 spin_lock(&fc
->lock
);
2759 if (RB_EMPTY_NODE(&ff
->polled_node
)) {
2760 struct rb_node
**link
, *uninitialized_var(parent
);
2762 link
= fuse_find_polled_node(fc
, ff
->kh
, &parent
);
2764 rb_link_node(&ff
->polled_node
, parent
, link
);
2765 rb_insert_color(&ff
->polled_node
, &fc
->polled_files
);
2767 spin_unlock(&fc
->lock
);
2770 unsigned fuse_file_poll(struct file
*file
, poll_table
*wait
)
2772 struct fuse_file
*ff
= file
->private_data
;
2773 struct fuse_conn
*fc
= ff
->fc
;
2774 struct fuse_poll_in inarg
= { .fh
= ff
->fh
, .kh
= ff
->kh
};
2775 struct fuse_poll_out outarg
;
2776 struct fuse_req
*req
;
2780 return DEFAULT_POLLMASK
;
2782 poll_wait(file
, &ff
->poll_wait
, wait
);
2783 inarg
.events
= (__u32
)poll_requested_events(wait
);
2786 * Ask for notification iff there's someone waiting for it.
2787 * The client may ignore the flag and always notify.
2789 if (waitqueue_active(&ff
->poll_wait
)) {
2790 inarg
.flags
|= FUSE_POLL_SCHEDULE_NOTIFY
;
2791 fuse_register_polled_file(fc
, ff
);
2794 req
= fuse_get_req_nopages(fc
);
2798 req
->in
.h
.opcode
= FUSE_POLL
;
2799 req
->in
.h
.nodeid
= ff
->nodeid
;
2800 req
->in
.numargs
= 1;
2801 req
->in
.args
[0].size
= sizeof(inarg
);
2802 req
->in
.args
[0].value
= &inarg
;
2803 req
->out
.numargs
= 1;
2804 req
->out
.args
[0].size
= sizeof(outarg
);
2805 req
->out
.args
[0].value
= &outarg
;
2806 fuse_request_send(fc
, req
);
2807 err
= req
->out
.h
.error
;
2808 fuse_put_request(fc
, req
);
2811 return outarg
.revents
;
2812 if (err
== -ENOSYS
) {
2814 return DEFAULT_POLLMASK
;
2818 EXPORT_SYMBOL_GPL(fuse_file_poll
);
2821 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2822 * wakes up the poll waiters.
2824 int fuse_notify_poll_wakeup(struct fuse_conn
*fc
,
2825 struct fuse_notify_poll_wakeup_out
*outarg
)
2827 u64 kh
= outarg
->kh
;
2828 struct rb_node
**link
;
2830 spin_lock(&fc
->lock
);
2832 link
= fuse_find_polled_node(fc
, kh
, NULL
);
2834 struct fuse_file
*ff
;
2836 ff
= rb_entry(*link
, struct fuse_file
, polled_node
);
2837 wake_up_interruptible_sync(&ff
->poll_wait
);
2840 spin_unlock(&fc
->lock
);
2844 static void fuse_do_truncate(struct file
*file
)
2846 struct inode
*inode
= file
->f_mapping
->host
;
2849 attr
.ia_valid
= ATTR_SIZE
;
2850 attr
.ia_size
= i_size_read(inode
);
2852 attr
.ia_file
= file
;
2853 attr
.ia_valid
|= ATTR_FILE
;
2855 fuse_do_setattr(inode
, &attr
, file
);
2858 static inline loff_t
fuse_round_up(loff_t off
)
2860 return round_up(off
, FUSE_MAX_PAGES_PER_REQ
<< PAGE_SHIFT
);
2864 fuse_direct_IO(int rw
, struct kiocb
*iocb
, struct iov_iter
*iter
,
2868 struct file
*file
= iocb
->ki_filp
;
2869 struct fuse_file
*ff
= file
->private_data
;
2870 bool async_dio
= ff
->fc
->async_dio
;
2872 struct inode
*inode
;
2874 size_t count
= iov_iter_count(iter
);
2875 struct fuse_io_priv
*io
;
2878 inode
= file
->f_mapping
->host
;
2879 i_size
= i_size_read(inode
);
2881 if ((rw
== READ
) && (offset
> i_size
))
2884 /* optimization for short read */
2885 if (async_dio
&& rw
!= WRITE
&& offset
+ count
> i_size
) {
2886 if (offset
>= i_size
)
2888 count
= min_t(loff_t
, count
, fuse_round_up(i_size
- offset
));
2889 iov_iter_truncate(iter
, count
);
2892 io
= kmalloc(sizeof(struct fuse_io_priv
), GFP_KERNEL
);
2895 spin_lock_init(&io
->lock
);
2899 io
->offset
= offset
;
2900 io
->write
= (rw
== WRITE
);
2904 * By default, we want to optimize all I/Os with async request
2905 * submission to the client filesystem if supported.
2907 io
->async
= async_dio
;
2911 * We cannot asynchronously extend the size of a file. We have no method
2912 * to wait on real async I/O requests, so we must submit this request
2915 if (!is_sync_kiocb(iocb
) && (offset
+ count
> i_size
) && rw
== WRITE
)
2919 ret
= __fuse_direct_write(io
, iter
, &pos
);
2921 ret
= __fuse_direct_read(io
, iter
, &pos
);
2924 fuse_aio_complete(io
, ret
< 0 ? ret
: 0, -1);
2926 /* we have a non-extending, async request, so return */
2927 if (!is_sync_kiocb(iocb
))
2928 return -EIOCBQUEUED
;
2930 ret
= wait_on_sync_kiocb(iocb
);
2937 fuse_write_update_size(inode
, pos
);
2938 else if (ret
< 0 && offset
+ count
> i_size
)
2939 fuse_do_truncate(file
);
2945 static long fuse_file_fallocate(struct file
*file
, int mode
, loff_t offset
,
2948 struct fuse_file
*ff
= file
->private_data
;
2949 struct inode
*inode
= file
->f_inode
;
2950 struct fuse_inode
*fi
= get_fuse_inode(inode
);
2951 struct fuse_conn
*fc
= ff
->fc
;
2952 struct fuse_req
*req
;
2953 struct fuse_fallocate_in inarg
= {
2960 bool lock_inode
= !(mode
& FALLOC_FL_KEEP_SIZE
) ||
2961 (mode
& FALLOC_FL_PUNCH_HOLE
);
2963 if (mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
))
2966 if (fc
->no_fallocate
)
2970 mutex_lock(&inode
->i_mutex
);
2971 if (mode
& FALLOC_FL_PUNCH_HOLE
) {
2972 loff_t endbyte
= offset
+ length
- 1;
2973 err
= filemap_write_and_wait_range(inode
->i_mapping
,
2978 fuse_sync_writes(inode
);
2982 if (!(mode
& FALLOC_FL_KEEP_SIZE
))
2983 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
2985 req
= fuse_get_req_nopages(fc
);
2991 req
->in
.h
.opcode
= FUSE_FALLOCATE
;
2992 req
->in
.h
.nodeid
= ff
->nodeid
;
2993 req
->in
.numargs
= 1;
2994 req
->in
.args
[0].size
= sizeof(inarg
);
2995 req
->in
.args
[0].value
= &inarg
;
2996 fuse_request_send(fc
, req
);
2997 err
= req
->out
.h
.error
;
2998 if (err
== -ENOSYS
) {
2999 fc
->no_fallocate
= 1;
3002 fuse_put_request(fc
, req
);
3007 /* we could have extended the file */
3008 if (!(mode
& FALLOC_FL_KEEP_SIZE
)) {
3009 bool changed
= fuse_write_update_size(inode
, offset
+ length
);
3011 if (changed
&& fc
->writeback_cache
)
3012 file_update_time(file
);
3015 if (mode
& FALLOC_FL_PUNCH_HOLE
)
3016 truncate_pagecache_range(inode
, offset
, offset
+ length
- 1);
3018 fuse_invalidate_attr(inode
);
3021 if (!(mode
& FALLOC_FL_KEEP_SIZE
))
3022 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
3025 mutex_unlock(&inode
->i_mutex
);
3030 static const struct file_operations fuse_file_operations
= {
3031 .llseek
= fuse_file_llseek
,
3032 .read
= new_sync_read
,
3033 .read_iter
= fuse_file_read_iter
,
3034 .write
= new_sync_write
,
3035 .write_iter
= fuse_file_write_iter
,
3036 .mmap
= fuse_file_mmap
,
3038 .flush
= fuse_flush
,
3039 .release
= fuse_release
,
3040 .fsync
= fuse_fsync
,
3041 .lock
= fuse_file_lock
,
3042 .flock
= fuse_file_flock
,
3043 .splice_read
= generic_file_splice_read
,
3044 .unlocked_ioctl
= fuse_file_ioctl
,
3045 .compat_ioctl
= fuse_file_compat_ioctl
,
3046 .poll
= fuse_file_poll
,
3047 .fallocate
= fuse_file_fallocate
,
3050 static const struct file_operations fuse_direct_io_file_operations
= {
3051 .llseek
= fuse_file_llseek
,
3052 .read
= fuse_direct_read
,
3053 .write
= fuse_direct_write
,
3054 .mmap
= fuse_direct_mmap
,
3056 .flush
= fuse_flush
,
3057 .release
= fuse_release
,
3058 .fsync
= fuse_fsync
,
3059 .lock
= fuse_file_lock
,
3060 .flock
= fuse_file_flock
,
3061 .unlocked_ioctl
= fuse_file_ioctl
,
3062 .compat_ioctl
= fuse_file_compat_ioctl
,
3063 .poll
= fuse_file_poll
,
3064 .fallocate
= fuse_file_fallocate
,
3065 /* no splice_read */
3068 static const struct address_space_operations fuse_file_aops
= {
3069 .readpage
= fuse_readpage
,
3070 .writepage
= fuse_writepage
,
3071 .writepages
= fuse_writepages
,
3072 .launder_page
= fuse_launder_page
,
3073 .readpages
= fuse_readpages
,
3074 .set_page_dirty
= __set_page_dirty_nobuffers
,
3076 .direct_IO
= fuse_direct_IO
,
3077 .write_begin
= fuse_write_begin
,
3078 .write_end
= fuse_write_end
,
3081 void fuse_init_file_inode(struct inode
*inode
)
3083 inode
->i_fop
= &fuse_file_operations
;
3084 inode
->i_data
.a_ops
= &fuse_file_aops
;