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/falloc.h>
19 #include <linux/uio.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
;
29 memset(&inarg
, 0, sizeof(inarg
));
30 inarg
.flags
= file
->f_flags
& ~(O_CREAT
| O_EXCL
| O_NOCTTY
);
31 if (!fc
->atomic_o_trunc
)
32 inarg
.flags
&= ~O_TRUNC
;
33 args
.in
.h
.opcode
= opcode
;
34 args
.in
.h
.nodeid
= nodeid
;
36 args
.in
.args
[0].size
= sizeof(inarg
);
37 args
.in
.args
[0].value
= &inarg
;
39 args
.out
.args
[0].size
= sizeof(*outargp
);
40 args
.out
.args
[0].value
= outargp
;
42 return fuse_simple_request(fc
, &args
);
45 struct fuse_file
*fuse_file_alloc(struct fuse_conn
*fc
)
49 ff
= kzalloc(sizeof(struct fuse_file
), GFP_KERNEL
);
54 ff
->reserved_req
= fuse_request_alloc(0);
55 if (unlikely(!ff
->reserved_req
)) {
60 INIT_LIST_HEAD(&ff
->write_entry
);
61 atomic_set(&ff
->count
, 0);
62 RB_CLEAR_NODE(&ff
->polled_node
);
63 init_waitqueue_head(&ff
->poll_wait
);
67 spin_unlock(&fc
->lock
);
72 void fuse_file_free(struct fuse_file
*ff
)
74 fuse_request_free(ff
->reserved_req
);
78 struct fuse_file
*fuse_file_get(struct fuse_file
*ff
)
80 atomic_inc(&ff
->count
);
84 static void fuse_release_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
86 iput(req
->misc
.release
.inode
);
89 static void fuse_file_put(struct fuse_file
*ff
, bool sync
)
91 if (atomic_dec_and_test(&ff
->count
)) {
92 struct fuse_req
*req
= ff
->reserved_req
;
94 if (ff
->fc
->no_open
) {
96 * Drop the release request when client does not
99 __clear_bit(FR_BACKGROUND
, &req
->flags
);
100 iput(req
->misc
.release
.inode
);
101 fuse_put_request(ff
->fc
, req
);
103 __set_bit(FR_FORCE
, &req
->flags
);
104 __clear_bit(FR_BACKGROUND
, &req
->flags
);
105 fuse_request_send(ff
->fc
, req
);
106 iput(req
->misc
.release
.inode
);
107 fuse_put_request(ff
->fc
, req
);
109 req
->end
= fuse_release_end
;
110 __set_bit(FR_BACKGROUND
, &req
->flags
);
111 fuse_request_send_background(ff
->fc
, req
);
117 int fuse_do_open(struct fuse_conn
*fc
, u64 nodeid
, struct file
*file
,
120 struct fuse_file
*ff
;
121 int opcode
= isdir
? FUSE_OPENDIR
: FUSE_OPEN
;
123 ff
= fuse_file_alloc(fc
);
128 ff
->open_flags
= FOPEN_KEEP_CACHE
; /* Default for no-open */
129 if (!fc
->no_open
|| isdir
) {
130 struct fuse_open_out outarg
;
133 err
= fuse_send_open(fc
, nodeid
, file
, opcode
, &outarg
);
136 ff
->open_flags
= outarg
.open_flags
;
138 } else if (err
!= -ENOSYS
|| isdir
) {
147 ff
->open_flags
&= ~FOPEN_DIRECT_IO
;
150 file
->private_data
= fuse_file_get(ff
);
154 EXPORT_SYMBOL_GPL(fuse_do_open
);
156 static void fuse_link_write_file(struct file
*file
)
158 struct inode
*inode
= file_inode(file
);
159 struct fuse_conn
*fc
= get_fuse_conn(inode
);
160 struct fuse_inode
*fi
= get_fuse_inode(inode
);
161 struct fuse_file
*ff
= file
->private_data
;
163 * file may be written through mmap, so chain it onto the
164 * inodes's write_file list
166 spin_lock(&fc
->lock
);
167 if (list_empty(&ff
->write_entry
))
168 list_add(&ff
->write_entry
, &fi
->write_files
);
169 spin_unlock(&fc
->lock
);
172 void fuse_finish_open(struct inode
*inode
, struct file
*file
)
174 struct fuse_file
*ff
= file
->private_data
;
175 struct fuse_conn
*fc
= get_fuse_conn(inode
);
177 if (ff
->open_flags
& FOPEN_DIRECT_IO
)
178 file
->f_op
= &fuse_direct_io_file_operations
;
179 if (!(ff
->open_flags
& FOPEN_KEEP_CACHE
))
180 invalidate_inode_pages2(inode
->i_mapping
);
181 if (ff
->open_flags
& FOPEN_NONSEEKABLE
)
182 nonseekable_open(inode
, file
);
183 if (fc
->atomic_o_trunc
&& (file
->f_flags
& O_TRUNC
)) {
184 struct fuse_inode
*fi
= get_fuse_inode(inode
);
186 spin_lock(&fc
->lock
);
187 fi
->attr_version
= ++fc
->attr_version
;
188 i_size_write(inode
, 0);
189 spin_unlock(&fc
->lock
);
190 fuse_invalidate_attr(inode
);
191 if (fc
->writeback_cache
)
192 file_update_time(file
);
194 if ((file
->f_mode
& FMODE_WRITE
) && fc
->writeback_cache
)
195 fuse_link_write_file(file
);
198 int fuse_open_common(struct inode
*inode
, struct file
*file
, bool isdir
)
200 struct fuse_conn
*fc
= get_fuse_conn(inode
);
202 bool lock_inode
= (file
->f_flags
& O_TRUNC
) &&
203 fc
->atomic_o_trunc
&&
206 err
= generic_file_open(inode
, file
);
211 mutex_lock(&inode
->i_mutex
);
213 err
= fuse_do_open(fc
, get_node_id(inode
), file
, isdir
);
216 fuse_finish_open(inode
, file
);
219 mutex_unlock(&inode
->i_mutex
);
224 static void fuse_prepare_release(struct fuse_file
*ff
, int flags
, int opcode
)
226 struct fuse_conn
*fc
= ff
->fc
;
227 struct fuse_req
*req
= ff
->reserved_req
;
228 struct fuse_release_in
*inarg
= &req
->misc
.release
.in
;
230 spin_lock(&fc
->lock
);
231 list_del(&ff
->write_entry
);
232 if (!RB_EMPTY_NODE(&ff
->polled_node
))
233 rb_erase(&ff
->polled_node
, &fc
->polled_files
);
234 spin_unlock(&fc
->lock
);
236 wake_up_interruptible_all(&ff
->poll_wait
);
239 inarg
->flags
= flags
;
240 req
->in
.h
.opcode
= opcode
;
241 req
->in
.h
.nodeid
= ff
->nodeid
;
243 req
->in
.args
[0].size
= sizeof(struct fuse_release_in
);
244 req
->in
.args
[0].value
= inarg
;
247 void fuse_release_common(struct file
*file
, int opcode
)
249 struct fuse_file
*ff
;
250 struct fuse_req
*req
;
252 ff
= file
->private_data
;
256 req
= ff
->reserved_req
;
257 fuse_prepare_release(ff
, file
->f_flags
, opcode
);
260 struct fuse_release_in
*inarg
= &req
->misc
.release
.in
;
261 inarg
->release_flags
|= FUSE_RELEASE_FLOCK_UNLOCK
;
262 inarg
->lock_owner
= fuse_lock_owner_id(ff
->fc
,
265 /* Hold inode until release is finished */
266 req
->misc
.release
.inode
= igrab(file_inode(file
));
269 * Normally this will send the RELEASE request, however if
270 * some asynchronous READ or WRITE requests are outstanding,
271 * the sending will be delayed.
273 * Make the release synchronous if this is a fuseblk mount,
274 * synchronous RELEASE is allowed (and desirable) in this case
275 * because the server can be trusted not to screw up.
277 fuse_file_put(ff
, ff
->fc
->destroy_req
!= NULL
);
280 static int fuse_open(struct inode
*inode
, struct file
*file
)
282 return fuse_open_common(inode
, file
, false);
285 static int fuse_release(struct inode
*inode
, struct file
*file
)
287 struct fuse_conn
*fc
= get_fuse_conn(inode
);
289 /* see fuse_vma_close() for !writeback_cache case */
290 if (fc
->writeback_cache
)
291 write_inode_now(inode
, 1);
293 fuse_release_common(file
, FUSE_RELEASE
);
295 /* return value is ignored by VFS */
299 void fuse_sync_release(struct fuse_file
*ff
, int flags
)
301 WARN_ON(atomic_read(&ff
->count
) > 1);
302 fuse_prepare_release(ff
, flags
, FUSE_RELEASE
);
303 __set_bit(FR_FORCE
, &ff
->reserved_req
->flags
);
304 __clear_bit(FR_BACKGROUND
, &ff
->reserved_req
->flags
);
305 fuse_request_send(ff
->fc
, ff
->reserved_req
);
306 fuse_put_request(ff
->fc
, ff
->reserved_req
);
309 EXPORT_SYMBOL_GPL(fuse_sync_release
);
312 * Scramble the ID space with XTEA, so that the value of the files_struct
313 * pointer is not exposed to userspace.
315 u64
fuse_lock_owner_id(struct fuse_conn
*fc
, fl_owner_t id
)
317 u32
*k
= fc
->scramble_key
;
318 u64 v
= (unsigned long) id
;
324 for (i
= 0; i
< 32; i
++) {
325 v0
+= ((v1
<< 4 ^ v1
>> 5) + v1
) ^ (sum
+ k
[sum
& 3]);
327 v1
+= ((v0
<< 4 ^ v0
>> 5) + v0
) ^ (sum
+ k
[sum
>>11 & 3]);
330 return (u64
) v0
+ ((u64
) v1
<< 32);
334 * Check if any page in a range is under writeback
336 * This is currently done by walking the list of writepage requests
337 * for the inode, which can be pretty inefficient.
339 static bool fuse_range_is_writeback(struct inode
*inode
, pgoff_t idx_from
,
342 struct fuse_conn
*fc
= get_fuse_conn(inode
);
343 struct fuse_inode
*fi
= get_fuse_inode(inode
);
344 struct fuse_req
*req
;
347 spin_lock(&fc
->lock
);
348 list_for_each_entry(req
, &fi
->writepages
, writepages_entry
) {
351 BUG_ON(req
->inode
!= inode
);
352 curr_index
= req
->misc
.write
.in
.offset
>> PAGE_CACHE_SHIFT
;
353 if (idx_from
< curr_index
+ req
->num_pages
&&
354 curr_index
<= idx_to
) {
359 spin_unlock(&fc
->lock
);
364 static inline bool fuse_page_is_writeback(struct inode
*inode
, pgoff_t index
)
366 return fuse_range_is_writeback(inode
, index
, index
);
370 * Wait for page writeback to be completed.
372 * Since fuse doesn't rely on the VM writeback tracking, this has to
373 * use some other means.
375 static int fuse_wait_on_page_writeback(struct inode
*inode
, pgoff_t index
)
377 struct fuse_inode
*fi
= get_fuse_inode(inode
);
379 wait_event(fi
->page_waitq
, !fuse_page_is_writeback(inode
, index
));
384 * Wait for all pending writepages on the inode to finish.
386 * This is currently done by blocking further writes with FUSE_NOWRITE
387 * and waiting for all sent writes to complete.
389 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
390 * could conflict with truncation.
392 static void fuse_sync_writes(struct inode
*inode
)
394 fuse_set_nowrite(inode
);
395 fuse_release_nowrite(inode
);
398 static int fuse_flush(struct file
*file
, fl_owner_t id
)
400 struct inode
*inode
= file_inode(file
);
401 struct fuse_conn
*fc
= get_fuse_conn(inode
);
402 struct fuse_file
*ff
= file
->private_data
;
403 struct fuse_req
*req
;
404 struct fuse_flush_in inarg
;
407 if (is_bad_inode(inode
))
413 err
= write_inode_now(inode
, 1);
417 mutex_lock(&inode
->i_mutex
);
418 fuse_sync_writes(inode
);
419 mutex_unlock(&inode
->i_mutex
);
421 if (test_bit(AS_ENOSPC
, &file
->f_mapping
->flags
) &&
422 test_and_clear_bit(AS_ENOSPC
, &file
->f_mapping
->flags
))
424 if (test_bit(AS_EIO
, &file
->f_mapping
->flags
) &&
425 test_and_clear_bit(AS_EIO
, &file
->f_mapping
->flags
))
430 req
= fuse_get_req_nofail_nopages(fc
, file
);
431 memset(&inarg
, 0, sizeof(inarg
));
433 inarg
.lock_owner
= fuse_lock_owner_id(fc
, id
);
434 req
->in
.h
.opcode
= FUSE_FLUSH
;
435 req
->in
.h
.nodeid
= get_node_id(inode
);
437 req
->in
.args
[0].size
= sizeof(inarg
);
438 req
->in
.args
[0].value
= &inarg
;
439 __set_bit(FR_FORCE
, &req
->flags
);
440 fuse_request_send(fc
, req
);
441 err
= req
->out
.h
.error
;
442 fuse_put_request(fc
, req
);
443 if (err
== -ENOSYS
) {
450 int fuse_fsync_common(struct file
*file
, loff_t start
, loff_t end
,
451 int datasync
, int isdir
)
453 struct inode
*inode
= file
->f_mapping
->host
;
454 struct fuse_conn
*fc
= get_fuse_conn(inode
);
455 struct fuse_file
*ff
= file
->private_data
;
457 struct fuse_fsync_in inarg
;
460 if (is_bad_inode(inode
))
463 mutex_lock(&inode
->i_mutex
);
466 * Start writeback against all dirty pages of the inode, then
467 * wait for all outstanding writes, before sending the FSYNC
470 err
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
474 fuse_sync_writes(inode
);
477 * Due to implementation of fuse writeback
478 * filemap_write_and_wait_range() does not catch errors.
479 * We have to do this directly after fuse_sync_writes()
481 if (test_bit(AS_ENOSPC
, &file
->f_mapping
->flags
) &&
482 test_and_clear_bit(AS_ENOSPC
, &file
->f_mapping
->flags
))
484 if (test_bit(AS_EIO
, &file
->f_mapping
->flags
) &&
485 test_and_clear_bit(AS_EIO
, &file
->f_mapping
->flags
))
490 err
= sync_inode_metadata(inode
, 1);
494 if ((!isdir
&& fc
->no_fsync
) || (isdir
&& fc
->no_fsyncdir
))
497 memset(&inarg
, 0, sizeof(inarg
));
499 inarg
.fsync_flags
= datasync
? 1 : 0;
500 args
.in
.h
.opcode
= isdir
? FUSE_FSYNCDIR
: FUSE_FSYNC
;
501 args
.in
.h
.nodeid
= get_node_id(inode
);
503 args
.in
.args
[0].size
= sizeof(inarg
);
504 args
.in
.args
[0].value
= &inarg
;
505 err
= fuse_simple_request(fc
, &args
);
506 if (err
== -ENOSYS
) {
514 mutex_unlock(&inode
->i_mutex
);
518 static int fuse_fsync(struct file
*file
, loff_t start
, loff_t end
,
521 return fuse_fsync_common(file
, start
, end
, datasync
, 0);
524 void fuse_read_fill(struct fuse_req
*req
, struct file
*file
, loff_t pos
,
525 size_t count
, int opcode
)
527 struct fuse_read_in
*inarg
= &req
->misc
.read
.in
;
528 struct fuse_file
*ff
= file
->private_data
;
533 inarg
->flags
= file
->f_flags
;
534 req
->in
.h
.opcode
= opcode
;
535 req
->in
.h
.nodeid
= ff
->nodeid
;
537 req
->in
.args
[0].size
= sizeof(struct fuse_read_in
);
538 req
->in
.args
[0].value
= inarg
;
540 req
->out
.numargs
= 1;
541 req
->out
.args
[0].size
= count
;
544 static void fuse_release_user_pages(struct fuse_req
*req
, bool should_dirty
)
548 for (i
= 0; i
< req
->num_pages
; i
++) {
549 struct page
*page
= req
->pages
[i
];
551 set_page_dirty_lock(page
);
556 static void fuse_io_release(struct kref
*kref
)
558 kfree(container_of(kref
, struct fuse_io_priv
, refcnt
));
561 static ssize_t
fuse_get_res_by_io(struct fuse_io_priv
*io
)
566 if (io
->bytes
>= 0 && io
->write
)
569 return io
->bytes
< 0 ? io
->size
: io
->bytes
;
573 * In case of short read, the caller sets 'pos' to the position of
574 * actual end of fuse request in IO request. Otherwise, if bytes_requested
575 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
578 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
579 * both submitted asynchronously. The first of them was ACKed by userspace as
580 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
581 * second request was ACKed as short, e.g. only 1K was read, resulting in
584 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
585 * will be equal to the length of the longest contiguous fragment of
586 * transferred data starting from the beginning of IO request.
588 static void fuse_aio_complete(struct fuse_io_priv
*io
, int err
, ssize_t pos
)
590 bool is_sync
= is_sync_kiocb(io
->iocb
);
593 spin_lock(&io
->lock
);
595 io
->err
= io
->err
? : err
;
596 else if (pos
>= 0 && (io
->bytes
< 0 || pos
< io
->bytes
))
600 if (!left
&& is_sync
)
602 spin_unlock(&io
->lock
);
604 if (!left
&& !is_sync
) {
605 ssize_t res
= fuse_get_res_by_io(io
);
608 struct inode
*inode
= file_inode(io
->iocb
->ki_filp
);
609 struct fuse_conn
*fc
= get_fuse_conn(inode
);
610 struct fuse_inode
*fi
= get_fuse_inode(inode
);
612 spin_lock(&fc
->lock
);
613 fi
->attr_version
= ++fc
->attr_version
;
614 spin_unlock(&fc
->lock
);
617 io
->iocb
->ki_complete(io
->iocb
, res
, 0);
620 kref_put(&io
->refcnt
, fuse_io_release
);
623 static void fuse_aio_complete_req(struct fuse_conn
*fc
, struct fuse_req
*req
)
625 struct fuse_io_priv
*io
= req
->io
;
628 fuse_release_user_pages(req
, !io
->write
);
631 if (req
->misc
.write
.in
.size
!= req
->misc
.write
.out
.size
)
632 pos
= req
->misc
.write
.in
.offset
- io
->offset
+
633 req
->misc
.write
.out
.size
;
635 if (req
->misc
.read
.in
.size
!= req
->out
.args
[0].size
)
636 pos
= req
->misc
.read
.in
.offset
- io
->offset
+
637 req
->out
.args
[0].size
;
640 fuse_aio_complete(io
, req
->out
.h
.error
, pos
);
643 static size_t fuse_async_req_send(struct fuse_conn
*fc
, struct fuse_req
*req
,
644 size_t num_bytes
, struct fuse_io_priv
*io
)
646 spin_lock(&io
->lock
);
647 kref_get(&io
->refcnt
);
648 io
->size
+= num_bytes
;
650 spin_unlock(&io
->lock
);
653 req
->end
= fuse_aio_complete_req
;
655 __fuse_get_request(req
);
656 fuse_request_send_background(fc
, req
);
661 static size_t fuse_send_read(struct fuse_req
*req
, struct fuse_io_priv
*io
,
662 loff_t pos
, size_t count
, fl_owner_t owner
)
664 struct file
*file
= io
->file
;
665 struct fuse_file
*ff
= file
->private_data
;
666 struct fuse_conn
*fc
= ff
->fc
;
668 fuse_read_fill(req
, file
, pos
, count
, FUSE_READ
);
670 struct fuse_read_in
*inarg
= &req
->misc
.read
.in
;
672 inarg
->read_flags
|= FUSE_READ_LOCKOWNER
;
673 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
677 return fuse_async_req_send(fc
, req
, count
, io
);
679 fuse_request_send(fc
, req
);
680 return req
->out
.args
[0].size
;
683 static void fuse_read_update_size(struct inode
*inode
, loff_t size
,
686 struct fuse_conn
*fc
= get_fuse_conn(inode
);
687 struct fuse_inode
*fi
= get_fuse_inode(inode
);
689 spin_lock(&fc
->lock
);
690 if (attr_ver
== fi
->attr_version
&& size
< inode
->i_size
&&
691 !test_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
)) {
692 fi
->attr_version
= ++fc
->attr_version
;
693 i_size_write(inode
, size
);
695 spin_unlock(&fc
->lock
);
698 static void fuse_short_read(struct fuse_req
*req
, struct inode
*inode
,
701 size_t num_read
= req
->out
.args
[0].size
;
702 struct fuse_conn
*fc
= get_fuse_conn(inode
);
704 if (fc
->writeback_cache
) {
706 * A hole in a file. Some data after the hole are in page cache,
707 * but have not reached the client fs yet. So, the hole is not
711 int start_idx
= num_read
>> PAGE_CACHE_SHIFT
;
712 size_t off
= num_read
& (PAGE_CACHE_SIZE
- 1);
714 for (i
= start_idx
; i
< req
->num_pages
; i
++) {
715 zero_user_segment(req
->pages
[i
], off
, PAGE_CACHE_SIZE
);
719 loff_t pos
= page_offset(req
->pages
[0]) + num_read
;
720 fuse_read_update_size(inode
, pos
, attr_ver
);
724 static int fuse_do_readpage(struct file
*file
, struct page
*page
)
726 struct fuse_io_priv io
= FUSE_IO_PRIV_SYNC(file
);
727 struct inode
*inode
= page
->mapping
->host
;
728 struct fuse_conn
*fc
= get_fuse_conn(inode
);
729 struct fuse_req
*req
;
731 loff_t pos
= page_offset(page
);
732 size_t count
= PAGE_CACHE_SIZE
;
737 * Page writeback can extend beyond the lifetime of the
738 * page-cache page, so make sure we read a properly synced
741 fuse_wait_on_page_writeback(inode
, page
->index
);
743 req
= fuse_get_req(fc
, 1);
747 attr_ver
= fuse_get_attr_version(fc
);
749 req
->out
.page_zeroing
= 1;
750 req
->out
.argpages
= 1;
752 req
->pages
[0] = page
;
753 req
->page_descs
[0].length
= count
;
754 num_read
= fuse_send_read(req
, &io
, pos
, count
, NULL
);
755 err
= req
->out
.h
.error
;
759 * Short read means EOF. If file size is larger, truncate it
761 if (num_read
< count
)
762 fuse_short_read(req
, inode
, attr_ver
);
764 SetPageUptodate(page
);
767 fuse_put_request(fc
, req
);
772 static int fuse_readpage(struct file
*file
, struct page
*page
)
774 struct inode
*inode
= page
->mapping
->host
;
778 if (is_bad_inode(inode
))
781 err
= fuse_do_readpage(file
, page
);
782 fuse_invalidate_atime(inode
);
788 static void fuse_readpages_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
791 size_t count
= req
->misc
.read
.in
.size
;
792 size_t num_read
= req
->out
.args
[0].size
;
793 struct address_space
*mapping
= NULL
;
795 for (i
= 0; mapping
== NULL
&& i
< req
->num_pages
; i
++)
796 mapping
= req
->pages
[i
]->mapping
;
799 struct inode
*inode
= mapping
->host
;
802 * Short read means EOF. If file size is larger, truncate it
804 if (!req
->out
.h
.error
&& num_read
< count
)
805 fuse_short_read(req
, inode
, req
->misc
.read
.attr_ver
);
807 fuse_invalidate_atime(inode
);
810 for (i
= 0; i
< req
->num_pages
; i
++) {
811 struct page
*page
= req
->pages
[i
];
812 if (!req
->out
.h
.error
)
813 SetPageUptodate(page
);
817 page_cache_release(page
);
820 fuse_file_put(req
->ff
, false);
823 static void fuse_send_readpages(struct fuse_req
*req
, struct file
*file
)
825 struct fuse_file
*ff
= file
->private_data
;
826 struct fuse_conn
*fc
= ff
->fc
;
827 loff_t pos
= page_offset(req
->pages
[0]);
828 size_t count
= req
->num_pages
<< PAGE_CACHE_SHIFT
;
830 req
->out
.argpages
= 1;
831 req
->out
.page_zeroing
= 1;
832 req
->out
.page_replace
= 1;
833 fuse_read_fill(req
, file
, pos
, count
, FUSE_READ
);
834 req
->misc
.read
.attr_ver
= fuse_get_attr_version(fc
);
835 if (fc
->async_read
) {
836 req
->ff
= fuse_file_get(ff
);
837 req
->end
= fuse_readpages_end
;
838 fuse_request_send_background(fc
, req
);
840 fuse_request_send(fc
, req
);
841 fuse_readpages_end(fc
, req
);
842 fuse_put_request(fc
, req
);
846 struct fuse_fill_data
{
847 struct fuse_req
*req
;
853 static int fuse_readpages_fill(void *_data
, struct page
*page
)
855 struct fuse_fill_data
*data
= _data
;
856 struct fuse_req
*req
= data
->req
;
857 struct inode
*inode
= data
->inode
;
858 struct fuse_conn
*fc
= get_fuse_conn(inode
);
860 fuse_wait_on_page_writeback(inode
, page
->index
);
862 if (req
->num_pages
&&
863 (req
->num_pages
== FUSE_MAX_PAGES_PER_REQ
||
864 (req
->num_pages
+ 1) * PAGE_CACHE_SIZE
> fc
->max_read
||
865 req
->pages
[req
->num_pages
- 1]->index
+ 1 != page
->index
)) {
866 int nr_alloc
= min_t(unsigned, data
->nr_pages
,
867 FUSE_MAX_PAGES_PER_REQ
);
868 fuse_send_readpages(req
, data
->file
);
870 req
= fuse_get_req_for_background(fc
, nr_alloc
);
872 req
= fuse_get_req(fc
, nr_alloc
);
881 if (WARN_ON(req
->num_pages
>= req
->max_pages
)) {
883 fuse_put_request(fc
, req
);
887 page_cache_get(page
);
888 req
->pages
[req
->num_pages
] = page
;
889 req
->page_descs
[req
->num_pages
].length
= PAGE_SIZE
;
895 static int fuse_readpages(struct file
*file
, struct address_space
*mapping
,
896 struct list_head
*pages
, unsigned nr_pages
)
898 struct inode
*inode
= mapping
->host
;
899 struct fuse_conn
*fc
= get_fuse_conn(inode
);
900 struct fuse_fill_data data
;
902 int nr_alloc
= min_t(unsigned, nr_pages
, FUSE_MAX_PAGES_PER_REQ
);
905 if (is_bad_inode(inode
))
911 data
.req
= fuse_get_req_for_background(fc
, nr_alloc
);
913 data
.req
= fuse_get_req(fc
, nr_alloc
);
914 data
.nr_pages
= nr_pages
;
915 err
= PTR_ERR(data
.req
);
916 if (IS_ERR(data
.req
))
919 err
= read_cache_pages(mapping
, pages
, fuse_readpages_fill
, &data
);
921 if (data
.req
->num_pages
)
922 fuse_send_readpages(data
.req
, file
);
924 fuse_put_request(fc
, data
.req
);
930 static ssize_t
fuse_file_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
932 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
933 struct fuse_conn
*fc
= get_fuse_conn(inode
);
936 * In auto invalidate mode, always update attributes on read.
937 * Otherwise, only update if we attempt to read past EOF (to ensure
938 * i_size is up to date).
940 if (fc
->auto_inval_data
||
941 (iocb
->ki_pos
+ iov_iter_count(to
) > i_size_read(inode
))) {
943 err
= fuse_update_attributes(inode
, NULL
, iocb
->ki_filp
, NULL
);
948 return generic_file_read_iter(iocb
, to
);
951 static void fuse_write_fill(struct fuse_req
*req
, struct fuse_file
*ff
,
952 loff_t pos
, size_t count
)
954 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
955 struct fuse_write_out
*outarg
= &req
->misc
.write
.out
;
960 req
->in
.h
.opcode
= FUSE_WRITE
;
961 req
->in
.h
.nodeid
= ff
->nodeid
;
963 if (ff
->fc
->minor
< 9)
964 req
->in
.args
[0].size
= FUSE_COMPAT_WRITE_IN_SIZE
;
966 req
->in
.args
[0].size
= sizeof(struct fuse_write_in
);
967 req
->in
.args
[0].value
= inarg
;
968 req
->in
.args
[1].size
= count
;
969 req
->out
.numargs
= 1;
970 req
->out
.args
[0].size
= sizeof(struct fuse_write_out
);
971 req
->out
.args
[0].value
= outarg
;
974 static size_t fuse_send_write(struct fuse_req
*req
, struct fuse_io_priv
*io
,
975 loff_t pos
, size_t count
, fl_owner_t owner
)
977 struct file
*file
= io
->file
;
978 struct fuse_file
*ff
= file
->private_data
;
979 struct fuse_conn
*fc
= ff
->fc
;
980 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
982 fuse_write_fill(req
, ff
, pos
, count
);
983 inarg
->flags
= file
->f_flags
;
985 inarg
->write_flags
|= FUSE_WRITE_LOCKOWNER
;
986 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
990 return fuse_async_req_send(fc
, req
, count
, io
);
992 fuse_request_send(fc
, req
);
993 return req
->misc
.write
.out
.size
;
996 bool fuse_write_update_size(struct inode
*inode
, loff_t pos
)
998 struct fuse_conn
*fc
= get_fuse_conn(inode
);
999 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1002 spin_lock(&fc
->lock
);
1003 fi
->attr_version
= ++fc
->attr_version
;
1004 if (pos
> inode
->i_size
) {
1005 i_size_write(inode
, pos
);
1008 spin_unlock(&fc
->lock
);
1013 static size_t fuse_send_write_pages(struct fuse_req
*req
, struct file
*file
,
1014 struct inode
*inode
, loff_t pos
,
1020 struct fuse_io_priv io
= FUSE_IO_PRIV_SYNC(file
);
1022 for (i
= 0; i
< req
->num_pages
; i
++)
1023 fuse_wait_on_page_writeback(inode
, req
->pages
[i
]->index
);
1025 res
= fuse_send_write(req
, &io
, pos
, count
, NULL
);
1027 offset
= req
->page_descs
[0].offset
;
1029 for (i
= 0; i
< req
->num_pages
; i
++) {
1030 struct page
*page
= req
->pages
[i
];
1032 if (!req
->out
.h
.error
&& !offset
&& count
>= PAGE_CACHE_SIZE
)
1033 SetPageUptodate(page
);
1035 if (count
> PAGE_CACHE_SIZE
- offset
)
1036 count
-= PAGE_CACHE_SIZE
- offset
;
1042 page_cache_release(page
);
1048 static ssize_t
fuse_fill_write_pages(struct fuse_req
*req
,
1049 struct address_space
*mapping
,
1050 struct iov_iter
*ii
, loff_t pos
)
1052 struct fuse_conn
*fc
= get_fuse_conn(mapping
->host
);
1053 unsigned offset
= pos
& (PAGE_CACHE_SIZE
- 1);
1057 req
->in
.argpages
= 1;
1058 req
->page_descs
[0].offset
= offset
;
1063 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
1064 size_t bytes
= min_t(size_t, PAGE_CACHE_SIZE
- offset
,
1065 iov_iter_count(ii
));
1067 bytes
= min_t(size_t, bytes
, fc
->max_write
- count
);
1071 if (iov_iter_fault_in_readable(ii
, bytes
))
1075 page
= grab_cache_page_write_begin(mapping
, index
, 0);
1079 if (mapping_writably_mapped(mapping
))
1080 flush_dcache_page(page
);
1082 tmp
= iov_iter_copy_from_user_atomic(page
, ii
, offset
, bytes
);
1083 flush_dcache_page(page
);
1085 iov_iter_advance(ii
, tmp
);
1088 page_cache_release(page
);
1089 bytes
= min(bytes
, iov_iter_single_seg_count(ii
));
1094 req
->pages
[req
->num_pages
] = page
;
1095 req
->page_descs
[req
->num_pages
].length
= tmp
;
1101 if (offset
== PAGE_CACHE_SIZE
)
1104 if (!fc
->big_writes
)
1106 } while (iov_iter_count(ii
) && count
< fc
->max_write
&&
1107 req
->num_pages
< req
->max_pages
&& offset
== 0);
1109 return count
> 0 ? count
: err
;
1112 static inline unsigned fuse_wr_pages(loff_t pos
, size_t len
)
1114 return min_t(unsigned,
1115 ((pos
+ len
- 1) >> PAGE_CACHE_SHIFT
) -
1116 (pos
>> PAGE_CACHE_SHIFT
) + 1,
1117 FUSE_MAX_PAGES_PER_REQ
);
1120 static ssize_t
fuse_perform_write(struct file
*file
,
1121 struct address_space
*mapping
,
1122 struct iov_iter
*ii
, loff_t pos
)
1124 struct inode
*inode
= mapping
->host
;
1125 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1126 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1130 if (is_bad_inode(inode
))
1133 if (inode
->i_size
< pos
+ iov_iter_count(ii
))
1134 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1137 struct fuse_req
*req
;
1139 unsigned nr_pages
= fuse_wr_pages(pos
, iov_iter_count(ii
));
1141 req
= fuse_get_req(fc
, nr_pages
);
1147 count
= fuse_fill_write_pages(req
, mapping
, ii
, pos
);
1153 num_written
= fuse_send_write_pages(req
, file
, inode
,
1155 err
= req
->out
.h
.error
;
1160 /* break out of the loop on short write */
1161 if (num_written
!= count
)
1165 fuse_put_request(fc
, req
);
1166 } while (!err
&& iov_iter_count(ii
));
1169 fuse_write_update_size(inode
, pos
);
1171 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1172 fuse_invalidate_attr(inode
);
1174 return res
> 0 ? res
: err
;
1177 static ssize_t
fuse_file_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
1179 struct file
*file
= iocb
->ki_filp
;
1180 struct address_space
*mapping
= file
->f_mapping
;
1181 ssize_t written
= 0;
1182 ssize_t written_buffered
= 0;
1183 struct inode
*inode
= mapping
->host
;
1187 if (get_fuse_conn(inode
)->writeback_cache
) {
1188 /* Update size (EOF optimization) and mode (SUID clearing) */
1189 err
= fuse_update_attributes(mapping
->host
, NULL
, file
, NULL
);
1193 return generic_file_write_iter(iocb
, from
);
1196 mutex_lock(&inode
->i_mutex
);
1198 /* We can write back this queue in page reclaim */
1199 current
->backing_dev_info
= inode_to_bdi(inode
);
1201 err
= generic_write_checks(iocb
, from
);
1205 err
= file_remove_privs(file
);
1209 err
= file_update_time(file
);
1213 if (iocb
->ki_flags
& IOCB_DIRECT
) {
1214 loff_t pos
= iocb
->ki_pos
;
1215 written
= generic_file_direct_write(iocb
, from
, pos
);
1216 if (written
< 0 || !iov_iter_count(from
))
1221 written_buffered
= fuse_perform_write(file
, mapping
, from
, pos
);
1222 if (written_buffered
< 0) {
1223 err
= written_buffered
;
1226 endbyte
= pos
+ written_buffered
- 1;
1228 err
= filemap_write_and_wait_range(file
->f_mapping
, pos
,
1233 invalidate_mapping_pages(file
->f_mapping
,
1234 pos
>> PAGE_CACHE_SHIFT
,
1235 endbyte
>> PAGE_CACHE_SHIFT
);
1237 written
+= written_buffered
;
1238 iocb
->ki_pos
= pos
+ written_buffered
;
1240 written
= fuse_perform_write(file
, mapping
, from
, iocb
->ki_pos
);
1242 iocb
->ki_pos
+= written
;
1245 current
->backing_dev_info
= NULL
;
1246 mutex_unlock(&inode
->i_mutex
);
1248 return written
? written
: err
;
1251 static inline void fuse_page_descs_length_init(struct fuse_req
*req
,
1252 unsigned index
, unsigned nr_pages
)
1256 for (i
= index
; i
< index
+ nr_pages
; i
++)
1257 req
->page_descs
[i
].length
= PAGE_SIZE
-
1258 req
->page_descs
[i
].offset
;
1261 static inline unsigned long fuse_get_user_addr(const struct iov_iter
*ii
)
1263 return (unsigned long)ii
->iov
->iov_base
+ ii
->iov_offset
;
1266 static inline size_t fuse_get_frag_size(const struct iov_iter
*ii
,
1269 return min(iov_iter_single_seg_count(ii
), max_size
);
1272 static int fuse_get_user_pages(struct fuse_req
*req
, struct iov_iter
*ii
,
1273 size_t *nbytesp
, int write
)
1275 size_t nbytes
= 0; /* # bytes already packed in req */
1277 /* Special case for kernel I/O: can copy directly into the buffer */
1278 if (ii
->type
& ITER_KVEC
) {
1279 unsigned long user_addr
= fuse_get_user_addr(ii
);
1280 size_t frag_size
= fuse_get_frag_size(ii
, *nbytesp
);
1283 req
->in
.args
[1].value
= (void *) user_addr
;
1285 req
->out
.args
[0].value
= (void *) user_addr
;
1287 iov_iter_advance(ii
, frag_size
);
1288 *nbytesp
= frag_size
;
1292 while (nbytes
< *nbytesp
&& req
->num_pages
< req
->max_pages
) {
1295 ssize_t ret
= iov_iter_get_pages(ii
,
1296 &req
->pages
[req
->num_pages
],
1298 req
->max_pages
- req
->num_pages
,
1303 iov_iter_advance(ii
, ret
);
1307 npages
= (ret
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
1309 req
->page_descs
[req
->num_pages
].offset
= start
;
1310 fuse_page_descs_length_init(req
, req
->num_pages
, npages
);
1312 req
->num_pages
+= npages
;
1313 req
->page_descs
[req
->num_pages
- 1].length
-=
1314 (PAGE_SIZE
- ret
) & (PAGE_SIZE
- 1);
1318 req
->in
.argpages
= 1;
1320 req
->out
.argpages
= 1;
1327 static inline int fuse_iter_npages(const struct iov_iter
*ii_p
)
1329 return iov_iter_npages(ii_p
, FUSE_MAX_PAGES_PER_REQ
);
1332 ssize_t
fuse_direct_io(struct fuse_io_priv
*io
, struct iov_iter
*iter
,
1333 loff_t
*ppos
, int flags
)
1335 int write
= flags
& FUSE_DIO_WRITE
;
1336 bool should_dirty
= !write
&& iter_is_iovec(iter
);
1337 int cuse
= flags
& FUSE_DIO_CUSE
;
1338 struct file
*file
= io
->file
;
1339 struct inode
*inode
= file
->f_mapping
->host
;
1340 struct fuse_file
*ff
= file
->private_data
;
1341 struct fuse_conn
*fc
= ff
->fc
;
1342 size_t nmax
= write
? fc
->max_write
: fc
->max_read
;
1344 size_t count
= iov_iter_count(iter
);
1345 pgoff_t idx_from
= pos
>> PAGE_CACHE_SHIFT
;
1346 pgoff_t idx_to
= (pos
+ count
- 1) >> PAGE_CACHE_SHIFT
;
1348 struct fuse_req
*req
;
1351 req
= fuse_get_req_for_background(fc
, fuse_iter_npages(iter
));
1353 req
= fuse_get_req(fc
, fuse_iter_npages(iter
));
1355 return PTR_ERR(req
);
1357 if (!cuse
&& fuse_range_is_writeback(inode
, idx_from
, idx_to
)) {
1359 mutex_lock(&inode
->i_mutex
);
1360 fuse_sync_writes(inode
);
1362 mutex_unlock(&inode
->i_mutex
);
1367 fl_owner_t owner
= current
->files
;
1368 size_t nbytes
= min(count
, nmax
);
1369 int err
= fuse_get_user_pages(req
, iter
, &nbytes
, write
);
1376 nres
= fuse_send_write(req
, io
, pos
, nbytes
, owner
);
1378 nres
= fuse_send_read(req
, io
, pos
, nbytes
, owner
);
1381 fuse_release_user_pages(req
, should_dirty
);
1382 if (req
->out
.h
.error
) {
1384 res
= req
->out
.h
.error
;
1386 } else if (nres
> nbytes
) {
1396 fuse_put_request(fc
, req
);
1398 req
= fuse_get_req_for_background(fc
,
1399 fuse_iter_npages(iter
));
1401 req
= fuse_get_req(fc
, fuse_iter_npages(iter
));
1407 fuse_put_request(fc
, req
);
1413 EXPORT_SYMBOL_GPL(fuse_direct_io
);
1415 static ssize_t
__fuse_direct_read(struct fuse_io_priv
*io
,
1416 struct iov_iter
*iter
,
1420 struct file
*file
= io
->file
;
1421 struct inode
*inode
= file_inode(file
);
1423 if (is_bad_inode(inode
))
1426 res
= fuse_direct_io(io
, iter
, ppos
, 0);
1428 fuse_invalidate_attr(inode
);
1433 static ssize_t
fuse_direct_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
1435 struct fuse_io_priv io
= FUSE_IO_PRIV_SYNC(iocb
->ki_filp
);
1436 return __fuse_direct_read(&io
, to
, &iocb
->ki_pos
);
1439 static ssize_t
fuse_direct_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
1441 struct file
*file
= iocb
->ki_filp
;
1442 struct inode
*inode
= file_inode(file
);
1443 struct fuse_io_priv io
= FUSE_IO_PRIV_SYNC(file
);
1446 if (is_bad_inode(inode
))
1449 /* Don't allow parallel writes to the same file */
1450 mutex_lock(&inode
->i_mutex
);
1451 res
= generic_write_checks(iocb
, from
);
1453 res
= fuse_direct_io(&io
, from
, &iocb
->ki_pos
, FUSE_DIO_WRITE
);
1454 fuse_invalidate_attr(inode
);
1456 fuse_write_update_size(inode
, iocb
->ki_pos
);
1457 mutex_unlock(&inode
->i_mutex
);
1462 static void fuse_writepage_free(struct fuse_conn
*fc
, struct fuse_req
*req
)
1466 for (i
= 0; i
< req
->num_pages
; i
++)
1467 __free_page(req
->pages
[i
]);
1470 fuse_file_put(req
->ff
, false);
1473 static void fuse_writepage_finish(struct fuse_conn
*fc
, struct fuse_req
*req
)
1475 struct inode
*inode
= req
->inode
;
1476 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1477 struct backing_dev_info
*bdi
= inode_to_bdi(inode
);
1480 list_del(&req
->writepages_entry
);
1481 for (i
= 0; i
< req
->num_pages
; i
++) {
1482 dec_wb_stat(&bdi
->wb
, WB_WRITEBACK
);
1483 dec_zone_page_state(req
->pages
[i
], NR_WRITEBACK_TEMP
);
1484 wb_writeout_inc(&bdi
->wb
);
1486 wake_up(&fi
->page_waitq
);
1489 /* Called under fc->lock, may release and reacquire it */
1490 static void fuse_send_writepage(struct fuse_conn
*fc
, struct fuse_req
*req
,
1492 __releases(fc
->lock
)
1493 __acquires(fc
->lock
)
1495 struct fuse_inode
*fi
= get_fuse_inode(req
->inode
);
1496 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
1497 __u64 data_size
= req
->num_pages
* PAGE_CACHE_SIZE
;
1502 if (inarg
->offset
+ data_size
<= size
) {
1503 inarg
->size
= data_size
;
1504 } else if (inarg
->offset
< size
) {
1505 inarg
->size
= size
- inarg
->offset
;
1507 /* Got truncated off completely */
1511 req
->in
.args
[1].size
= inarg
->size
;
1513 fuse_request_send_background_locked(fc
, req
);
1517 fuse_writepage_finish(fc
, req
);
1518 spin_unlock(&fc
->lock
);
1519 fuse_writepage_free(fc
, req
);
1520 fuse_put_request(fc
, req
);
1521 spin_lock(&fc
->lock
);
1525 * If fi->writectr is positive (no truncate or fsync going on) send
1526 * all queued writepage requests.
1528 * Called with fc->lock
1530 void fuse_flush_writepages(struct inode
*inode
)
1531 __releases(fc
->lock
)
1532 __acquires(fc
->lock
)
1534 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1535 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1536 size_t crop
= i_size_read(inode
);
1537 struct fuse_req
*req
;
1539 while (fi
->writectr
>= 0 && !list_empty(&fi
->queued_writes
)) {
1540 req
= list_entry(fi
->queued_writes
.next
, struct fuse_req
, list
);
1541 list_del_init(&req
->list
);
1542 fuse_send_writepage(fc
, req
, crop
);
1546 static void fuse_writepage_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
1548 struct inode
*inode
= req
->inode
;
1549 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1551 mapping_set_error(inode
->i_mapping
, req
->out
.h
.error
);
1552 spin_lock(&fc
->lock
);
1553 while (req
->misc
.write
.next
) {
1554 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1555 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
1556 struct fuse_req
*next
= req
->misc
.write
.next
;
1557 req
->misc
.write
.next
= next
->misc
.write
.next
;
1558 next
->misc
.write
.next
= NULL
;
1559 next
->ff
= fuse_file_get(req
->ff
);
1560 list_add(&next
->writepages_entry
, &fi
->writepages
);
1563 * Skip fuse_flush_writepages() to make it easy to crop requests
1564 * based on primary request size.
1566 * 1st case (trivial): there are no concurrent activities using
1567 * fuse_set/release_nowrite. Then we're on safe side because
1568 * fuse_flush_writepages() would call fuse_send_writepage()
1571 * 2nd case: someone called fuse_set_nowrite and it is waiting
1572 * now for completion of all in-flight requests. This happens
1573 * rarely and no more than once per page, so this should be
1576 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1577 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1578 * that fuse_set_nowrite returned implies that all in-flight
1579 * requests were completed along with all of their secondary
1580 * requests. Further primary requests are blocked by negative
1581 * writectr. Hence there cannot be any in-flight requests and
1582 * no invocations of fuse_writepage_end() while we're in
1583 * fuse_set_nowrite..fuse_release_nowrite section.
1585 fuse_send_writepage(fc
, next
, inarg
->offset
+ inarg
->size
);
1588 fuse_writepage_finish(fc
, req
);
1589 spin_unlock(&fc
->lock
);
1590 fuse_writepage_free(fc
, req
);
1593 static struct fuse_file
*__fuse_write_file_get(struct fuse_conn
*fc
,
1594 struct fuse_inode
*fi
)
1596 struct fuse_file
*ff
= NULL
;
1598 spin_lock(&fc
->lock
);
1599 if (!list_empty(&fi
->write_files
)) {
1600 ff
= list_entry(fi
->write_files
.next
, struct fuse_file
,
1604 spin_unlock(&fc
->lock
);
1609 static struct fuse_file
*fuse_write_file_get(struct fuse_conn
*fc
,
1610 struct fuse_inode
*fi
)
1612 struct fuse_file
*ff
= __fuse_write_file_get(fc
, fi
);
1617 int fuse_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
1619 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1620 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1621 struct fuse_file
*ff
;
1624 ff
= __fuse_write_file_get(fc
, fi
);
1625 err
= fuse_flush_times(inode
, ff
);
1627 fuse_file_put(ff
, 0);
1632 static int fuse_writepage_locked(struct page
*page
)
1634 struct address_space
*mapping
= page
->mapping
;
1635 struct inode
*inode
= mapping
->host
;
1636 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1637 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1638 struct fuse_req
*req
;
1639 struct page
*tmp_page
;
1640 int error
= -ENOMEM
;
1642 set_page_writeback(page
);
1644 req
= fuse_request_alloc_nofs(1);
1648 /* writeback always goes to bg_queue */
1649 __set_bit(FR_BACKGROUND
, &req
->flags
);
1650 tmp_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
1655 req
->ff
= fuse_write_file_get(fc
, fi
);
1659 fuse_write_fill(req
, req
->ff
, page_offset(page
), 0);
1661 copy_highpage(tmp_page
, page
);
1662 req
->misc
.write
.in
.write_flags
|= FUSE_WRITE_CACHE
;
1663 req
->misc
.write
.next
= NULL
;
1664 req
->in
.argpages
= 1;
1666 req
->pages
[0] = tmp_page
;
1667 req
->page_descs
[0].offset
= 0;
1668 req
->page_descs
[0].length
= PAGE_SIZE
;
1669 req
->end
= fuse_writepage_end
;
1672 inc_wb_stat(&inode_to_bdi(inode
)->wb
, WB_WRITEBACK
);
1673 inc_zone_page_state(tmp_page
, NR_WRITEBACK_TEMP
);
1675 spin_lock(&fc
->lock
);
1676 list_add(&req
->writepages_entry
, &fi
->writepages
);
1677 list_add_tail(&req
->list
, &fi
->queued_writes
);
1678 fuse_flush_writepages(inode
);
1679 spin_unlock(&fc
->lock
);
1681 end_page_writeback(page
);
1686 __free_page(tmp_page
);
1688 fuse_request_free(req
);
1690 end_page_writeback(page
);
1694 static int fuse_writepage(struct page
*page
, struct writeback_control
*wbc
)
1698 if (fuse_page_is_writeback(page
->mapping
->host
, page
->index
)) {
1700 * ->writepages() should be called for sync() and friends. We
1701 * should only get here on direct reclaim and then we are
1702 * allowed to skip a page which is already in flight
1704 WARN_ON(wbc
->sync_mode
== WB_SYNC_ALL
);
1706 redirty_page_for_writepage(wbc
, page
);
1710 err
= fuse_writepage_locked(page
);
1716 struct fuse_fill_wb_data
{
1717 struct fuse_req
*req
;
1718 struct fuse_file
*ff
;
1719 struct inode
*inode
;
1720 struct page
**orig_pages
;
1723 static void fuse_writepages_send(struct fuse_fill_wb_data
*data
)
1725 struct fuse_req
*req
= data
->req
;
1726 struct inode
*inode
= data
->inode
;
1727 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1728 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1729 int num_pages
= req
->num_pages
;
1732 req
->ff
= fuse_file_get(data
->ff
);
1733 spin_lock(&fc
->lock
);
1734 list_add_tail(&req
->list
, &fi
->queued_writes
);
1735 fuse_flush_writepages(inode
);
1736 spin_unlock(&fc
->lock
);
1738 for (i
= 0; i
< num_pages
; i
++)
1739 end_page_writeback(data
->orig_pages
[i
]);
1742 static bool fuse_writepage_in_flight(struct fuse_req
*new_req
,
1745 struct fuse_conn
*fc
= get_fuse_conn(new_req
->inode
);
1746 struct fuse_inode
*fi
= get_fuse_inode(new_req
->inode
);
1747 struct fuse_req
*tmp
;
1748 struct fuse_req
*old_req
;
1752 BUG_ON(new_req
->num_pages
!= 0);
1754 spin_lock(&fc
->lock
);
1755 list_del(&new_req
->writepages_entry
);
1756 list_for_each_entry(old_req
, &fi
->writepages
, writepages_entry
) {
1757 BUG_ON(old_req
->inode
!= new_req
->inode
);
1758 curr_index
= old_req
->misc
.write
.in
.offset
>> PAGE_CACHE_SHIFT
;
1759 if (curr_index
<= page
->index
&&
1760 page
->index
< curr_index
+ old_req
->num_pages
) {
1766 list_add(&new_req
->writepages_entry
, &fi
->writepages
);
1770 new_req
->num_pages
= 1;
1771 for (tmp
= old_req
; tmp
!= NULL
; tmp
= tmp
->misc
.write
.next
) {
1772 BUG_ON(tmp
->inode
!= new_req
->inode
);
1773 curr_index
= tmp
->misc
.write
.in
.offset
>> PAGE_CACHE_SHIFT
;
1774 if (tmp
->num_pages
== 1 &&
1775 curr_index
== page
->index
) {
1780 if (old_req
->num_pages
== 1 && test_bit(FR_PENDING
, &old_req
->flags
)) {
1781 struct backing_dev_info
*bdi
= inode_to_bdi(page
->mapping
->host
);
1783 copy_highpage(old_req
->pages
[0], page
);
1784 spin_unlock(&fc
->lock
);
1786 dec_wb_stat(&bdi
->wb
, WB_WRITEBACK
);
1787 dec_zone_page_state(page
, NR_WRITEBACK_TEMP
);
1788 wb_writeout_inc(&bdi
->wb
);
1789 fuse_writepage_free(fc
, new_req
);
1790 fuse_request_free(new_req
);
1793 new_req
->misc
.write
.next
= old_req
->misc
.write
.next
;
1794 old_req
->misc
.write
.next
= new_req
;
1797 spin_unlock(&fc
->lock
);
1802 static int fuse_writepages_fill(struct page
*page
,
1803 struct writeback_control
*wbc
, void *_data
)
1805 struct fuse_fill_wb_data
*data
= _data
;
1806 struct fuse_req
*req
= data
->req
;
1807 struct inode
*inode
= data
->inode
;
1808 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1809 struct page
*tmp_page
;
1815 data
->ff
= fuse_write_file_get(fc
, get_fuse_inode(inode
));
1821 * Being under writeback is unlikely but possible. For example direct
1822 * read to an mmaped fuse file will set the page dirty twice; once when
1823 * the pages are faulted with get_user_pages(), and then after the read
1826 is_writeback
= fuse_page_is_writeback(inode
, page
->index
);
1828 if (req
&& req
->num_pages
&&
1829 (is_writeback
|| req
->num_pages
== FUSE_MAX_PAGES_PER_REQ
||
1830 (req
->num_pages
+ 1) * PAGE_CACHE_SIZE
> fc
->max_write
||
1831 data
->orig_pages
[req
->num_pages
- 1]->index
+ 1 != page
->index
)) {
1832 fuse_writepages_send(data
);
1836 tmp_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
1841 * The page must not be redirtied until the writeout is completed
1842 * (i.e. userspace has sent a reply to the write request). Otherwise
1843 * there could be more than one temporary page instance for each real
1846 * This is ensured by holding the page lock in page_mkwrite() while
1847 * checking fuse_page_is_writeback(). We already hold the page lock
1848 * since clear_page_dirty_for_io() and keep it held until we add the
1849 * request to the fi->writepages list and increment req->num_pages.
1850 * After this fuse_page_is_writeback() will indicate that the page is
1851 * under writeback, so we can release the page lock.
1853 if (data
->req
== NULL
) {
1854 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1857 req
= fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ
);
1859 __free_page(tmp_page
);
1863 fuse_write_fill(req
, data
->ff
, page_offset(page
), 0);
1864 req
->misc
.write
.in
.write_flags
|= FUSE_WRITE_CACHE
;
1865 req
->misc
.write
.next
= NULL
;
1866 req
->in
.argpages
= 1;
1867 __set_bit(FR_BACKGROUND
, &req
->flags
);
1869 req
->end
= fuse_writepage_end
;
1872 spin_lock(&fc
->lock
);
1873 list_add(&req
->writepages_entry
, &fi
->writepages
);
1874 spin_unlock(&fc
->lock
);
1878 set_page_writeback(page
);
1880 copy_highpage(tmp_page
, page
);
1881 req
->pages
[req
->num_pages
] = tmp_page
;
1882 req
->page_descs
[req
->num_pages
].offset
= 0;
1883 req
->page_descs
[req
->num_pages
].length
= PAGE_SIZE
;
1885 inc_wb_stat(&inode_to_bdi(inode
)->wb
, WB_WRITEBACK
);
1886 inc_zone_page_state(tmp_page
, NR_WRITEBACK_TEMP
);
1889 if (is_writeback
&& fuse_writepage_in_flight(req
, page
)) {
1890 end_page_writeback(page
);
1894 data
->orig_pages
[req
->num_pages
] = page
;
1897 * Protected by fc->lock against concurrent access by
1898 * fuse_page_is_writeback().
1900 spin_lock(&fc
->lock
);
1902 spin_unlock(&fc
->lock
);
1910 static int fuse_writepages(struct address_space
*mapping
,
1911 struct writeback_control
*wbc
)
1913 struct inode
*inode
= mapping
->host
;
1914 struct fuse_fill_wb_data data
;
1918 if (is_bad_inode(inode
))
1926 data
.orig_pages
= kcalloc(FUSE_MAX_PAGES_PER_REQ
,
1927 sizeof(struct page
*),
1929 if (!data
.orig_pages
)
1932 err
= write_cache_pages(mapping
, wbc
, fuse_writepages_fill
, &data
);
1934 /* Ignore errors if we can write at least one page */
1935 BUG_ON(!data
.req
->num_pages
);
1936 fuse_writepages_send(&data
);
1940 fuse_file_put(data
.ff
, false);
1942 kfree(data
.orig_pages
);
1948 * It's worthy to make sure that space is reserved on disk for the write,
1949 * but how to implement it without killing performance need more thinking.
1951 static int fuse_write_begin(struct file
*file
, struct address_space
*mapping
,
1952 loff_t pos
, unsigned len
, unsigned flags
,
1953 struct page
**pagep
, void **fsdata
)
1955 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
1956 struct fuse_conn
*fc
= get_fuse_conn(file_inode(file
));
1961 WARN_ON(!fc
->writeback_cache
);
1963 page
= grab_cache_page_write_begin(mapping
, index
, flags
);
1967 fuse_wait_on_page_writeback(mapping
->host
, page
->index
);
1969 if (PageUptodate(page
) || len
== PAGE_CACHE_SIZE
)
1972 * Check if the start this page comes after the end of file, in which
1973 * case the readpage can be optimized away.
1975 fsize
= i_size_read(mapping
->host
);
1976 if (fsize
<= (pos
& PAGE_CACHE_MASK
)) {
1977 size_t off
= pos
& ~PAGE_CACHE_MASK
;
1979 zero_user_segment(page
, 0, off
);
1982 err
= fuse_do_readpage(file
, page
);
1991 page_cache_release(page
);
1996 static int fuse_write_end(struct file
*file
, struct address_space
*mapping
,
1997 loff_t pos
, unsigned len
, unsigned copied
,
1998 struct page
*page
, void *fsdata
)
2000 struct inode
*inode
= page
->mapping
->host
;
2002 /* Haven't copied anything? Skip zeroing, size extending, dirtying. */
2006 if (!PageUptodate(page
)) {
2007 /* Zero any unwritten bytes at the end of the page */
2008 size_t endoff
= (pos
+ copied
) & ~PAGE_CACHE_MASK
;
2010 zero_user_segment(page
, endoff
, PAGE_CACHE_SIZE
);
2011 SetPageUptodate(page
);
2014 fuse_write_update_size(inode
, pos
+ copied
);
2015 set_page_dirty(page
);
2019 page_cache_release(page
);
2024 static int fuse_launder_page(struct page
*page
)
2027 if (clear_page_dirty_for_io(page
)) {
2028 struct inode
*inode
= page
->mapping
->host
;
2029 err
= fuse_writepage_locked(page
);
2031 fuse_wait_on_page_writeback(inode
, page
->index
);
2037 * Write back dirty pages now, because there may not be any suitable
2040 static void fuse_vma_close(struct vm_area_struct
*vma
)
2042 filemap_write_and_wait(vma
->vm_file
->f_mapping
);
2046 * Wait for writeback against this page to complete before allowing it
2047 * to be marked dirty again, and hence written back again, possibly
2048 * before the previous writepage completed.
2050 * Block here, instead of in ->writepage(), so that the userspace fs
2051 * can only block processes actually operating on the filesystem.
2053 * Otherwise unprivileged userspace fs would be able to block
2058 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2060 static int fuse_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
2062 struct page
*page
= vmf
->page
;
2063 struct inode
*inode
= file_inode(vma
->vm_file
);
2065 file_update_time(vma
->vm_file
);
2067 if (page
->mapping
!= inode
->i_mapping
) {
2069 return VM_FAULT_NOPAGE
;
2072 fuse_wait_on_page_writeback(inode
, page
->index
);
2073 return VM_FAULT_LOCKED
;
2076 static const struct vm_operations_struct fuse_file_vm_ops
= {
2077 .close
= fuse_vma_close
,
2078 .fault
= filemap_fault
,
2079 .map_pages
= filemap_map_pages
,
2080 .page_mkwrite
= fuse_page_mkwrite
,
2083 static int fuse_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2085 if ((vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
))
2086 fuse_link_write_file(file
);
2088 file_accessed(file
);
2089 vma
->vm_ops
= &fuse_file_vm_ops
;
2093 static int fuse_direct_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2095 /* Can't provide the coherency needed for MAP_SHARED */
2096 if (vma
->vm_flags
& VM_MAYSHARE
)
2099 invalidate_inode_pages2(file
->f_mapping
);
2101 return generic_file_mmap(file
, vma
);
2104 static int convert_fuse_file_lock(const struct fuse_file_lock
*ffl
,
2105 struct file_lock
*fl
)
2107 switch (ffl
->type
) {
2113 if (ffl
->start
> OFFSET_MAX
|| ffl
->end
> OFFSET_MAX
||
2114 ffl
->end
< ffl
->start
)
2117 fl
->fl_start
= ffl
->start
;
2118 fl
->fl_end
= ffl
->end
;
2119 fl
->fl_pid
= ffl
->pid
;
2125 fl
->fl_type
= ffl
->type
;
2129 static void fuse_lk_fill(struct fuse_args
*args
, struct file
*file
,
2130 const struct file_lock
*fl
, int opcode
, pid_t pid
,
2131 int flock
, struct fuse_lk_in
*inarg
)
2133 struct inode
*inode
= file_inode(file
);
2134 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2135 struct fuse_file
*ff
= file
->private_data
;
2137 memset(inarg
, 0, sizeof(*inarg
));
2139 inarg
->owner
= fuse_lock_owner_id(fc
, fl
->fl_owner
);
2140 inarg
->lk
.start
= fl
->fl_start
;
2141 inarg
->lk
.end
= fl
->fl_end
;
2142 inarg
->lk
.type
= fl
->fl_type
;
2143 inarg
->lk
.pid
= pid
;
2145 inarg
->lk_flags
|= FUSE_LK_FLOCK
;
2146 args
->in
.h
.opcode
= opcode
;
2147 args
->in
.h
.nodeid
= get_node_id(inode
);
2148 args
->in
.numargs
= 1;
2149 args
->in
.args
[0].size
= sizeof(*inarg
);
2150 args
->in
.args
[0].value
= inarg
;
2153 static int fuse_getlk(struct file
*file
, struct file_lock
*fl
)
2155 struct inode
*inode
= file_inode(file
);
2156 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2158 struct fuse_lk_in inarg
;
2159 struct fuse_lk_out outarg
;
2162 fuse_lk_fill(&args
, file
, fl
, FUSE_GETLK
, 0, 0, &inarg
);
2163 args
.out
.numargs
= 1;
2164 args
.out
.args
[0].size
= sizeof(outarg
);
2165 args
.out
.args
[0].value
= &outarg
;
2166 err
= fuse_simple_request(fc
, &args
);
2168 err
= convert_fuse_file_lock(&outarg
.lk
, fl
);
2173 static int fuse_setlk(struct file
*file
, struct file_lock
*fl
, int flock
)
2175 struct inode
*inode
= file_inode(file
);
2176 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2178 struct fuse_lk_in inarg
;
2179 int opcode
= (fl
->fl_flags
& FL_SLEEP
) ? FUSE_SETLKW
: FUSE_SETLK
;
2180 pid_t pid
= fl
->fl_type
!= F_UNLCK
? current
->tgid
: 0;
2183 if (fl
->fl_lmops
&& fl
->fl_lmops
->lm_grant
) {
2184 /* NLM needs asynchronous locks, which we don't support yet */
2188 /* Unlock on close is handled by the flush method */
2189 if (fl
->fl_flags
& FL_CLOSE
)
2192 fuse_lk_fill(&args
, file
, fl
, opcode
, pid
, flock
, &inarg
);
2193 err
= fuse_simple_request(fc
, &args
);
2195 /* locking is restartable */
2202 static int fuse_file_lock(struct file
*file
, int cmd
, struct file_lock
*fl
)
2204 struct inode
*inode
= file_inode(file
);
2205 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2208 if (cmd
== F_CANCELLK
) {
2210 } else if (cmd
== F_GETLK
) {
2212 posix_test_lock(file
, fl
);
2215 err
= fuse_getlk(file
, fl
);
2218 err
= posix_lock_file(file
, fl
, NULL
);
2220 err
= fuse_setlk(file
, fl
, 0);
2225 static int fuse_file_flock(struct file
*file
, int cmd
, struct file_lock
*fl
)
2227 struct inode
*inode
= file_inode(file
);
2228 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2232 err
= locks_lock_file_wait(file
, fl
);
2234 struct fuse_file
*ff
= file
->private_data
;
2236 /* emulate flock with POSIX locks */
2238 err
= fuse_setlk(file
, fl
, 1);
2244 static sector_t
fuse_bmap(struct address_space
*mapping
, sector_t block
)
2246 struct inode
*inode
= mapping
->host
;
2247 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2249 struct fuse_bmap_in inarg
;
2250 struct fuse_bmap_out outarg
;
2253 if (!inode
->i_sb
->s_bdev
|| fc
->no_bmap
)
2256 memset(&inarg
, 0, sizeof(inarg
));
2257 inarg
.block
= block
;
2258 inarg
.blocksize
= inode
->i_sb
->s_blocksize
;
2259 args
.in
.h
.opcode
= FUSE_BMAP
;
2260 args
.in
.h
.nodeid
= get_node_id(inode
);
2261 args
.in
.numargs
= 1;
2262 args
.in
.args
[0].size
= sizeof(inarg
);
2263 args
.in
.args
[0].value
= &inarg
;
2264 args
.out
.numargs
= 1;
2265 args
.out
.args
[0].size
= sizeof(outarg
);
2266 args
.out
.args
[0].value
= &outarg
;
2267 err
= fuse_simple_request(fc
, &args
);
2271 return err
? 0 : outarg
.block
;
2274 static loff_t
fuse_file_llseek(struct file
*file
, loff_t offset
, int whence
)
2277 struct inode
*inode
= file_inode(file
);
2279 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2280 if (whence
== SEEK_CUR
|| whence
== SEEK_SET
)
2281 return generic_file_llseek(file
, offset
, whence
);
2283 mutex_lock(&inode
->i_mutex
);
2284 retval
= fuse_update_attributes(inode
, NULL
, file
, NULL
);
2286 retval
= generic_file_llseek(file
, offset
, whence
);
2287 mutex_unlock(&inode
->i_mutex
);
2292 static int fuse_ioctl_copy_user(struct page
**pages
, struct iovec
*iov
,
2293 unsigned int nr_segs
, size_t bytes
, bool to_user
)
2301 iov_iter_init(&ii
, to_user
? READ
: WRITE
, iov
, nr_segs
, bytes
);
2303 while (iov_iter_count(&ii
)) {
2304 struct page
*page
= pages
[page_idx
++];
2305 size_t todo
= min_t(size_t, PAGE_SIZE
, iov_iter_count(&ii
));
2311 char __user
*uaddr
= ii
.iov
->iov_base
+ ii
.iov_offset
;
2312 size_t iov_len
= ii
.iov
->iov_len
- ii
.iov_offset
;
2313 size_t copy
= min(todo
, iov_len
);
2317 left
= copy_from_user(kaddr
, uaddr
, copy
);
2319 left
= copy_to_user(uaddr
, kaddr
, copy
);
2324 iov_iter_advance(&ii
, copy
);
2336 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2337 * ABI was defined to be 'struct iovec' which is different on 32bit
2338 * and 64bit. Fortunately we can determine which structure the server
2339 * used from the size of the reply.
2341 static int fuse_copy_ioctl_iovec_old(struct iovec
*dst
, void *src
,
2342 size_t transferred
, unsigned count
,
2345 #ifdef CONFIG_COMPAT
2346 if (count
* sizeof(struct compat_iovec
) == transferred
) {
2347 struct compat_iovec
*ciov
= src
;
2351 * With this interface a 32bit server cannot support
2352 * non-compat (i.e. ones coming from 64bit apps) ioctl
2358 for (i
= 0; i
< count
; i
++) {
2359 dst
[i
].iov_base
= compat_ptr(ciov
[i
].iov_base
);
2360 dst
[i
].iov_len
= ciov
[i
].iov_len
;
2366 if (count
* sizeof(struct iovec
) != transferred
)
2369 memcpy(dst
, src
, transferred
);
2373 /* Make sure iov_length() won't overflow */
2374 static int fuse_verify_ioctl_iov(struct iovec
*iov
, size_t count
)
2377 u32 max
= FUSE_MAX_PAGES_PER_REQ
<< PAGE_SHIFT
;
2379 for (n
= 0; n
< count
; n
++, iov
++) {
2380 if (iov
->iov_len
> (size_t) max
)
2382 max
-= iov
->iov_len
;
2387 static int fuse_copy_ioctl_iovec(struct fuse_conn
*fc
, struct iovec
*dst
,
2388 void *src
, size_t transferred
, unsigned count
,
2392 struct fuse_ioctl_iovec
*fiov
= src
;
2394 if (fc
->minor
< 16) {
2395 return fuse_copy_ioctl_iovec_old(dst
, src
, transferred
,
2399 if (count
* sizeof(struct fuse_ioctl_iovec
) != transferred
)
2402 for (i
= 0; i
< count
; i
++) {
2403 /* Did the server supply an inappropriate value? */
2404 if (fiov
[i
].base
!= (unsigned long) fiov
[i
].base
||
2405 fiov
[i
].len
!= (unsigned long) fiov
[i
].len
)
2408 dst
[i
].iov_base
= (void __user
*) (unsigned long) fiov
[i
].base
;
2409 dst
[i
].iov_len
= (size_t) fiov
[i
].len
;
2411 #ifdef CONFIG_COMPAT
2413 (ptr_to_compat(dst
[i
].iov_base
) != fiov
[i
].base
||
2414 (compat_size_t
) dst
[i
].iov_len
!= fiov
[i
].len
))
2424 * For ioctls, there is no generic way to determine how much memory
2425 * needs to be read and/or written. Furthermore, ioctls are allowed
2426 * to dereference the passed pointer, so the parameter requires deep
2427 * copying but FUSE has no idea whatsoever about what to copy in or
2430 * This is solved by allowing FUSE server to retry ioctl with
2431 * necessary in/out iovecs. Let's assume the ioctl implementation
2432 * needs to read in the following structure.
2439 * On the first callout to FUSE server, inarg->in_size and
2440 * inarg->out_size will be NULL; then, the server completes the ioctl
2441 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2442 * the actual iov array to
2444 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2446 * which tells FUSE to copy in the requested area and retry the ioctl.
2447 * On the second round, the server has access to the structure and
2448 * from that it can tell what to look for next, so on the invocation,
2449 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2451 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2452 * { .iov_base = a.buf, .iov_len = a.buflen } }
2454 * FUSE will copy both struct a and the pointed buffer from the
2455 * process doing the ioctl and retry ioctl with both struct a and the
2458 * This time, FUSE server has everything it needs and completes ioctl
2459 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2461 * Copying data out works the same way.
2463 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2464 * automatically initializes in and out iovs by decoding @cmd with
2465 * _IOC_* macros and the server is not allowed to request RETRY. This
2466 * limits ioctl data transfers to well-formed ioctls and is the forced
2467 * behavior for all FUSE servers.
2469 long fuse_do_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
,
2472 struct fuse_file
*ff
= file
->private_data
;
2473 struct fuse_conn
*fc
= ff
->fc
;
2474 struct fuse_ioctl_in inarg
= {
2480 struct fuse_ioctl_out outarg
;
2481 struct fuse_req
*req
= NULL
;
2482 struct page
**pages
= NULL
;
2483 struct iovec
*iov_page
= NULL
;
2484 struct iovec
*in_iov
= NULL
, *out_iov
= NULL
;
2485 unsigned int in_iovs
= 0, out_iovs
= 0, num_pages
= 0, max_pages
;
2486 size_t in_size
, out_size
, transferred
;
2489 #if BITS_PER_LONG == 32
2490 inarg
.flags
|= FUSE_IOCTL_32BIT
;
2492 if (flags
& FUSE_IOCTL_COMPAT
)
2493 inarg
.flags
|= FUSE_IOCTL_32BIT
;
2496 /* assume all the iovs returned by client always fits in a page */
2497 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec
) * FUSE_IOCTL_MAX_IOV
> PAGE_SIZE
);
2500 pages
= kcalloc(FUSE_MAX_PAGES_PER_REQ
, sizeof(pages
[0]), GFP_KERNEL
);
2501 iov_page
= (struct iovec
*) __get_free_page(GFP_KERNEL
);
2502 if (!pages
|| !iov_page
)
2506 * If restricted, initialize IO parameters as encoded in @cmd.
2507 * RETRY from server is not allowed.
2509 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
)) {
2510 struct iovec
*iov
= iov_page
;
2512 iov
->iov_base
= (void __user
*)arg
;
2513 iov
->iov_len
= _IOC_SIZE(cmd
);
2515 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
2520 if (_IOC_DIR(cmd
) & _IOC_READ
) {
2527 inarg
.in_size
= in_size
= iov_length(in_iov
, in_iovs
);
2528 inarg
.out_size
= out_size
= iov_length(out_iov
, out_iovs
);
2531 * Out data can be used either for actual out data or iovs,
2532 * make sure there always is at least one page.
2534 out_size
= max_t(size_t, out_size
, PAGE_SIZE
);
2535 max_pages
= DIV_ROUND_UP(max(in_size
, out_size
), PAGE_SIZE
);
2537 /* make sure there are enough buffer pages and init request with them */
2539 if (max_pages
> FUSE_MAX_PAGES_PER_REQ
)
2541 while (num_pages
< max_pages
) {
2542 pages
[num_pages
] = alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
2543 if (!pages
[num_pages
])
2548 req
= fuse_get_req(fc
, num_pages
);
2554 memcpy(req
->pages
, pages
, sizeof(req
->pages
[0]) * num_pages
);
2555 req
->num_pages
= num_pages
;
2556 fuse_page_descs_length_init(req
, 0, req
->num_pages
);
2558 /* okay, let's send it to the client */
2559 req
->in
.h
.opcode
= FUSE_IOCTL
;
2560 req
->in
.h
.nodeid
= ff
->nodeid
;
2561 req
->in
.numargs
= 1;
2562 req
->in
.args
[0].size
= sizeof(inarg
);
2563 req
->in
.args
[0].value
= &inarg
;
2566 req
->in
.args
[1].size
= in_size
;
2567 req
->in
.argpages
= 1;
2569 err
= fuse_ioctl_copy_user(pages
, in_iov
, in_iovs
, in_size
,
2575 req
->out
.numargs
= 2;
2576 req
->out
.args
[0].size
= sizeof(outarg
);
2577 req
->out
.args
[0].value
= &outarg
;
2578 req
->out
.args
[1].size
= out_size
;
2579 req
->out
.argpages
= 1;
2580 req
->out
.argvar
= 1;
2582 fuse_request_send(fc
, req
);
2583 err
= req
->out
.h
.error
;
2584 transferred
= req
->out
.args
[1].size
;
2585 fuse_put_request(fc
, req
);
2590 /* did it ask for retry? */
2591 if (outarg
.flags
& FUSE_IOCTL_RETRY
) {
2594 /* no retry if in restricted mode */
2596 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
))
2599 in_iovs
= outarg
.in_iovs
;
2600 out_iovs
= outarg
.out_iovs
;
2603 * Make sure things are in boundary, separate checks
2604 * are to protect against overflow.
2607 if (in_iovs
> FUSE_IOCTL_MAX_IOV
||
2608 out_iovs
> FUSE_IOCTL_MAX_IOV
||
2609 in_iovs
+ out_iovs
> FUSE_IOCTL_MAX_IOV
)
2612 vaddr
= kmap_atomic(pages
[0]);
2613 err
= fuse_copy_ioctl_iovec(fc
, iov_page
, vaddr
,
2614 transferred
, in_iovs
+ out_iovs
,
2615 (flags
& FUSE_IOCTL_COMPAT
) != 0);
2616 kunmap_atomic(vaddr
);
2621 out_iov
= in_iov
+ in_iovs
;
2623 err
= fuse_verify_ioctl_iov(in_iov
, in_iovs
);
2627 err
= fuse_verify_ioctl_iov(out_iov
, out_iovs
);
2635 if (transferred
> inarg
.out_size
)
2638 err
= fuse_ioctl_copy_user(pages
, out_iov
, out_iovs
, transferred
, true);
2641 fuse_put_request(fc
, req
);
2642 free_page((unsigned long) iov_page
);
2644 __free_page(pages
[--num_pages
]);
2647 return err
? err
: outarg
.result
;
2649 EXPORT_SYMBOL_GPL(fuse_do_ioctl
);
2651 long fuse_ioctl_common(struct file
*file
, unsigned int cmd
,
2652 unsigned long arg
, unsigned int flags
)
2654 struct inode
*inode
= file_inode(file
);
2655 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2657 if (!fuse_allow_current_process(fc
))
2660 if (is_bad_inode(inode
))
2663 return fuse_do_ioctl(file
, cmd
, arg
, flags
);
2666 static long fuse_file_ioctl(struct file
*file
, unsigned int cmd
,
2669 return fuse_ioctl_common(file
, cmd
, arg
, 0);
2672 static long fuse_file_compat_ioctl(struct file
*file
, unsigned int cmd
,
2675 return fuse_ioctl_common(file
, cmd
, arg
, FUSE_IOCTL_COMPAT
);
2679 * All files which have been polled are linked to RB tree
2680 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2681 * find the matching one.
2683 static struct rb_node
**fuse_find_polled_node(struct fuse_conn
*fc
, u64 kh
,
2684 struct rb_node
**parent_out
)
2686 struct rb_node
**link
= &fc
->polled_files
.rb_node
;
2687 struct rb_node
*last
= NULL
;
2690 struct fuse_file
*ff
;
2693 ff
= rb_entry(last
, struct fuse_file
, polled_node
);
2696 link
= &last
->rb_left
;
2697 else if (kh
> ff
->kh
)
2698 link
= &last
->rb_right
;
2709 * The file is about to be polled. Make sure it's on the polled_files
2710 * RB tree. Note that files once added to the polled_files tree are
2711 * not removed before the file is released. This is because a file
2712 * polled once is likely to be polled again.
2714 static void fuse_register_polled_file(struct fuse_conn
*fc
,
2715 struct fuse_file
*ff
)
2717 spin_lock(&fc
->lock
);
2718 if (RB_EMPTY_NODE(&ff
->polled_node
)) {
2719 struct rb_node
**link
, *uninitialized_var(parent
);
2721 link
= fuse_find_polled_node(fc
, ff
->kh
, &parent
);
2723 rb_link_node(&ff
->polled_node
, parent
, link
);
2724 rb_insert_color(&ff
->polled_node
, &fc
->polled_files
);
2726 spin_unlock(&fc
->lock
);
2729 unsigned fuse_file_poll(struct file
*file
, poll_table
*wait
)
2731 struct fuse_file
*ff
= file
->private_data
;
2732 struct fuse_conn
*fc
= ff
->fc
;
2733 struct fuse_poll_in inarg
= { .fh
= ff
->fh
, .kh
= ff
->kh
};
2734 struct fuse_poll_out outarg
;
2739 return DEFAULT_POLLMASK
;
2741 poll_wait(file
, &ff
->poll_wait
, wait
);
2742 inarg
.events
= (__u32
)poll_requested_events(wait
);
2745 * Ask for notification iff there's someone waiting for it.
2746 * The client may ignore the flag and always notify.
2748 if (waitqueue_active(&ff
->poll_wait
)) {
2749 inarg
.flags
|= FUSE_POLL_SCHEDULE_NOTIFY
;
2750 fuse_register_polled_file(fc
, ff
);
2753 args
.in
.h
.opcode
= FUSE_POLL
;
2754 args
.in
.h
.nodeid
= ff
->nodeid
;
2755 args
.in
.numargs
= 1;
2756 args
.in
.args
[0].size
= sizeof(inarg
);
2757 args
.in
.args
[0].value
= &inarg
;
2758 args
.out
.numargs
= 1;
2759 args
.out
.args
[0].size
= sizeof(outarg
);
2760 args
.out
.args
[0].value
= &outarg
;
2761 err
= fuse_simple_request(fc
, &args
);
2764 return outarg
.revents
;
2765 if (err
== -ENOSYS
) {
2767 return DEFAULT_POLLMASK
;
2771 EXPORT_SYMBOL_GPL(fuse_file_poll
);
2774 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2775 * wakes up the poll waiters.
2777 int fuse_notify_poll_wakeup(struct fuse_conn
*fc
,
2778 struct fuse_notify_poll_wakeup_out
*outarg
)
2780 u64 kh
= outarg
->kh
;
2781 struct rb_node
**link
;
2783 spin_lock(&fc
->lock
);
2785 link
= fuse_find_polled_node(fc
, kh
, NULL
);
2787 struct fuse_file
*ff
;
2789 ff
= rb_entry(*link
, struct fuse_file
, polled_node
);
2790 wake_up_interruptible_sync(&ff
->poll_wait
);
2793 spin_unlock(&fc
->lock
);
2797 static void fuse_do_truncate(struct file
*file
)
2799 struct inode
*inode
= file
->f_mapping
->host
;
2802 attr
.ia_valid
= ATTR_SIZE
;
2803 attr
.ia_size
= i_size_read(inode
);
2805 attr
.ia_file
= file
;
2806 attr
.ia_valid
|= ATTR_FILE
;
2808 fuse_do_setattr(inode
, &attr
, file
);
2811 static inline loff_t
fuse_round_up(loff_t off
)
2813 return round_up(off
, FUSE_MAX_PAGES_PER_REQ
<< PAGE_SHIFT
);
2817 fuse_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
, loff_t offset
)
2819 DECLARE_COMPLETION_ONSTACK(wait
);
2821 struct file
*file
= iocb
->ki_filp
;
2822 struct fuse_file
*ff
= file
->private_data
;
2823 bool async_dio
= ff
->fc
->async_dio
;
2825 struct inode
*inode
;
2827 size_t count
= iov_iter_count(iter
);
2828 struct fuse_io_priv
*io
;
2829 bool is_sync
= is_sync_kiocb(iocb
);
2832 inode
= file
->f_mapping
->host
;
2833 i_size
= i_size_read(inode
);
2835 if ((iov_iter_rw(iter
) == READ
) && (offset
> i_size
))
2838 /* optimization for short read */
2839 if (async_dio
&& iov_iter_rw(iter
) != WRITE
&& offset
+ count
> i_size
) {
2840 if (offset
>= i_size
)
2842 iov_iter_truncate(iter
, fuse_round_up(i_size
- offset
));
2843 count
= iov_iter_count(iter
);
2846 io
= kmalloc(sizeof(struct fuse_io_priv
), GFP_KERNEL
);
2849 spin_lock_init(&io
->lock
);
2850 kref_init(&io
->refcnt
);
2854 io
->offset
= offset
;
2855 io
->write
= (iov_iter_rw(iter
) == WRITE
);
2859 * By default, we want to optimize all I/Os with async request
2860 * submission to the client filesystem if supported.
2862 io
->async
= async_dio
;
2866 * We cannot asynchronously extend the size of a file. We have no method
2867 * to wait on real async I/O requests, so we must submit this request
2870 if (!is_sync
&& (offset
+ count
> i_size
) &&
2871 iov_iter_rw(iter
) == WRITE
)
2874 if (io
->async
&& is_sync
) {
2876 * Additional reference to keep io around after
2877 * calling fuse_aio_complete()
2879 kref_get(&io
->refcnt
);
2883 if (iov_iter_rw(iter
) == WRITE
) {
2884 ret
= fuse_direct_io(io
, iter
, &pos
, FUSE_DIO_WRITE
);
2885 fuse_invalidate_attr(inode
);
2887 ret
= __fuse_direct_read(io
, iter
, &pos
);
2891 fuse_aio_complete(io
, ret
< 0 ? ret
: 0, -1);
2893 /* we have a non-extending, async request, so return */
2895 return -EIOCBQUEUED
;
2897 wait_for_completion(&wait
);
2898 ret
= fuse_get_res_by_io(io
);
2901 kref_put(&io
->refcnt
, fuse_io_release
);
2903 if (iov_iter_rw(iter
) == WRITE
) {
2905 fuse_write_update_size(inode
, pos
);
2906 else if (ret
< 0 && offset
+ count
> i_size
)
2907 fuse_do_truncate(file
);
2913 static long fuse_file_fallocate(struct file
*file
, int mode
, loff_t offset
,
2916 struct fuse_file
*ff
= file
->private_data
;
2917 struct inode
*inode
= file_inode(file
);
2918 struct fuse_inode
*fi
= get_fuse_inode(inode
);
2919 struct fuse_conn
*fc
= ff
->fc
;
2921 struct fuse_fallocate_in inarg
= {
2928 bool lock_inode
= !(mode
& FALLOC_FL_KEEP_SIZE
) ||
2929 (mode
& FALLOC_FL_PUNCH_HOLE
);
2931 if (mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
))
2934 if (fc
->no_fallocate
)
2938 mutex_lock(&inode
->i_mutex
);
2939 if (mode
& FALLOC_FL_PUNCH_HOLE
) {
2940 loff_t endbyte
= offset
+ length
- 1;
2941 err
= filemap_write_and_wait_range(inode
->i_mapping
,
2946 fuse_sync_writes(inode
);
2950 if (!(mode
& FALLOC_FL_KEEP_SIZE
))
2951 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
2953 args
.in
.h
.opcode
= FUSE_FALLOCATE
;
2954 args
.in
.h
.nodeid
= ff
->nodeid
;
2955 args
.in
.numargs
= 1;
2956 args
.in
.args
[0].size
= sizeof(inarg
);
2957 args
.in
.args
[0].value
= &inarg
;
2958 err
= fuse_simple_request(fc
, &args
);
2959 if (err
== -ENOSYS
) {
2960 fc
->no_fallocate
= 1;
2966 /* we could have extended the file */
2967 if (!(mode
& FALLOC_FL_KEEP_SIZE
)) {
2968 bool changed
= fuse_write_update_size(inode
, offset
+ length
);
2970 if (changed
&& fc
->writeback_cache
)
2971 file_update_time(file
);
2974 if (mode
& FALLOC_FL_PUNCH_HOLE
)
2975 truncate_pagecache_range(inode
, offset
, offset
+ length
- 1);
2977 fuse_invalidate_attr(inode
);
2980 if (!(mode
& FALLOC_FL_KEEP_SIZE
))
2981 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
2984 mutex_unlock(&inode
->i_mutex
);
2989 static const struct file_operations fuse_file_operations
= {
2990 .llseek
= fuse_file_llseek
,
2991 .read_iter
= fuse_file_read_iter
,
2992 .write_iter
= fuse_file_write_iter
,
2993 .mmap
= fuse_file_mmap
,
2995 .flush
= fuse_flush
,
2996 .release
= fuse_release
,
2997 .fsync
= fuse_fsync
,
2998 .lock
= fuse_file_lock
,
2999 .flock
= fuse_file_flock
,
3000 .splice_read
= generic_file_splice_read
,
3001 .unlocked_ioctl
= fuse_file_ioctl
,
3002 .compat_ioctl
= fuse_file_compat_ioctl
,
3003 .poll
= fuse_file_poll
,
3004 .fallocate
= fuse_file_fallocate
,
3007 static const struct file_operations fuse_direct_io_file_operations
= {
3008 .llseek
= fuse_file_llseek
,
3009 .read_iter
= fuse_direct_read_iter
,
3010 .write_iter
= fuse_direct_write_iter
,
3011 .mmap
= fuse_direct_mmap
,
3013 .flush
= fuse_flush
,
3014 .release
= fuse_release
,
3015 .fsync
= fuse_fsync
,
3016 .lock
= fuse_file_lock
,
3017 .flock
= fuse_file_flock
,
3018 .unlocked_ioctl
= fuse_file_ioctl
,
3019 .compat_ioctl
= fuse_file_compat_ioctl
,
3020 .poll
= fuse_file_poll
,
3021 .fallocate
= fuse_file_fallocate
,
3022 /* no splice_read */
3025 static const struct address_space_operations fuse_file_aops
= {
3026 .readpage
= fuse_readpage
,
3027 .writepage
= fuse_writepage
,
3028 .writepages
= fuse_writepages
,
3029 .launder_page
= fuse_launder_page
,
3030 .readpages
= fuse_readpages
,
3031 .set_page_dirty
= __set_page_dirty_nobuffers
,
3033 .direct_IO
= fuse_direct_IO
,
3034 .write_begin
= fuse_write_begin
,
3035 .write_end
= fuse_write_end
,
3038 void fuse_init_file_inode(struct inode
*inode
)
3040 inode
->i_fop
= &fuse_file_operations
;
3041 inode
->i_data
.a_ops
= &fuse_file_aops
;