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
= kmalloc(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 __clear_bit(FR_BACKGROUND
, &req
->flags
);
104 fuse_request_send(ff
->fc
, req
);
105 iput(req
->misc
.release
.inode
);
106 fuse_put_request(ff
->fc
, req
);
108 req
->end
= fuse_release_end
;
109 __set_bit(FR_BACKGROUND
, &req
->flags
);
110 fuse_request_send_background(ff
->fc
, req
);
116 int fuse_do_open(struct fuse_conn
*fc
, u64 nodeid
, struct file
*file
,
119 struct fuse_file
*ff
;
120 int opcode
= isdir
? FUSE_OPENDIR
: FUSE_OPEN
;
122 ff
= fuse_file_alloc(fc
);
127 ff
->open_flags
= FOPEN_KEEP_CACHE
; /* Default for no-open */
128 if (!fc
->no_open
|| isdir
) {
129 struct fuse_open_out outarg
;
132 err
= fuse_send_open(fc
, nodeid
, file
, opcode
, &outarg
);
135 ff
->open_flags
= outarg
.open_flags
;
137 } else if (err
!= -ENOSYS
|| isdir
) {
146 ff
->open_flags
&= ~FOPEN_DIRECT_IO
;
149 file
->private_data
= fuse_file_get(ff
);
153 EXPORT_SYMBOL_GPL(fuse_do_open
);
155 static void fuse_link_write_file(struct file
*file
)
157 struct inode
*inode
= file_inode(file
);
158 struct fuse_conn
*fc
= get_fuse_conn(inode
);
159 struct fuse_inode
*fi
= get_fuse_inode(inode
);
160 struct fuse_file
*ff
= file
->private_data
;
162 * file may be written through mmap, so chain it onto the
163 * inodes's write_file list
165 spin_lock(&fc
->lock
);
166 if (list_empty(&ff
->write_entry
))
167 list_add(&ff
->write_entry
, &fi
->write_files
);
168 spin_unlock(&fc
->lock
);
171 void fuse_finish_open(struct inode
*inode
, struct file
*file
)
173 struct fuse_file
*ff
= file
->private_data
;
174 struct fuse_conn
*fc
= get_fuse_conn(inode
);
176 if (ff
->open_flags
& FOPEN_DIRECT_IO
)
177 file
->f_op
= &fuse_direct_io_file_operations
;
178 if (!(ff
->open_flags
& FOPEN_KEEP_CACHE
))
179 invalidate_inode_pages2(inode
->i_mapping
);
180 if (ff
->open_flags
& FOPEN_NONSEEKABLE
)
181 nonseekable_open(inode
, file
);
182 if (fc
->atomic_o_trunc
&& (file
->f_flags
& O_TRUNC
)) {
183 struct fuse_inode
*fi
= get_fuse_inode(inode
);
185 spin_lock(&fc
->lock
);
186 fi
->attr_version
= ++fc
->attr_version
;
187 i_size_write(inode
, 0);
188 spin_unlock(&fc
->lock
);
189 fuse_invalidate_attr(inode
);
190 if (fc
->writeback_cache
)
191 file_update_time(file
);
193 if ((file
->f_mode
& FMODE_WRITE
) && fc
->writeback_cache
)
194 fuse_link_write_file(file
);
197 int fuse_open_common(struct inode
*inode
, struct file
*file
, bool isdir
)
199 struct fuse_conn
*fc
= get_fuse_conn(inode
);
201 bool lock_inode
= (file
->f_flags
& O_TRUNC
) &&
202 fc
->atomic_o_trunc
&&
205 err
= generic_file_open(inode
, file
);
212 err
= fuse_do_open(fc
, get_node_id(inode
), file
, isdir
);
215 fuse_finish_open(inode
, file
);
223 static void fuse_prepare_release(struct fuse_file
*ff
, int flags
, int opcode
)
225 struct fuse_conn
*fc
= ff
->fc
;
226 struct fuse_req
*req
= ff
->reserved_req
;
227 struct fuse_release_in
*inarg
= &req
->misc
.release
.in
;
229 spin_lock(&fc
->lock
);
230 list_del(&ff
->write_entry
);
231 if (!RB_EMPTY_NODE(&ff
->polled_node
))
232 rb_erase(&ff
->polled_node
, &fc
->polled_files
);
233 spin_unlock(&fc
->lock
);
235 wake_up_interruptible_all(&ff
->poll_wait
);
238 inarg
->flags
= flags
;
239 req
->in
.h
.opcode
= opcode
;
240 req
->in
.h
.nodeid
= ff
->nodeid
;
242 req
->in
.args
[0].size
= sizeof(struct fuse_release_in
);
243 req
->in
.args
[0].value
= inarg
;
246 void fuse_release_common(struct file
*file
, int opcode
)
248 struct fuse_file
*ff
;
249 struct fuse_req
*req
;
251 ff
= file
->private_data
;
255 req
= ff
->reserved_req
;
256 fuse_prepare_release(ff
, file
->f_flags
, opcode
);
259 struct fuse_release_in
*inarg
= &req
->misc
.release
.in
;
260 inarg
->release_flags
|= FUSE_RELEASE_FLOCK_UNLOCK
;
261 inarg
->lock_owner
= fuse_lock_owner_id(ff
->fc
,
264 /* Hold inode until release is finished */
265 req
->misc
.release
.inode
= igrab(file_inode(file
));
268 * Normally this will send the RELEASE request, however if
269 * some asynchronous READ or WRITE requests are outstanding,
270 * the sending will be delayed.
272 * Make the release synchronous if this is a fuseblk mount,
273 * synchronous RELEASE is allowed (and desirable) in this case
274 * because the server can be trusted not to screw up.
276 fuse_file_put(ff
, ff
->fc
->destroy_req
!= NULL
);
279 static int fuse_open(struct inode
*inode
, struct file
*file
)
281 return fuse_open_common(inode
, file
, false);
284 static int fuse_release(struct inode
*inode
, struct file
*file
)
286 struct fuse_conn
*fc
= get_fuse_conn(inode
);
288 /* see fuse_vma_close() for !writeback_cache case */
289 if (fc
->writeback_cache
)
290 write_inode_now(inode
, 1);
292 fuse_release_common(file
, FUSE_RELEASE
);
294 /* return value is ignored by VFS */
298 void fuse_sync_release(struct fuse_file
*ff
, int flags
)
300 WARN_ON(atomic_read(&ff
->count
) > 1);
301 fuse_prepare_release(ff
, flags
, FUSE_RELEASE
);
302 __set_bit(FR_FORCE
, &ff
->reserved_req
->flags
);
303 __clear_bit(FR_BACKGROUND
, &ff
->reserved_req
->flags
);
304 fuse_request_send(ff
->fc
, ff
->reserved_req
);
305 fuse_put_request(ff
->fc
, ff
->reserved_req
);
308 EXPORT_SYMBOL_GPL(fuse_sync_release
);
311 * Scramble the ID space with XTEA, so that the value of the files_struct
312 * pointer is not exposed to userspace.
314 u64
fuse_lock_owner_id(struct fuse_conn
*fc
, fl_owner_t id
)
316 u32
*k
= fc
->scramble_key
;
317 u64 v
= (unsigned long) id
;
323 for (i
= 0; i
< 32; i
++) {
324 v0
+= ((v1
<< 4 ^ v1
>> 5) + v1
) ^ (sum
+ k
[sum
& 3]);
326 v1
+= ((v0
<< 4 ^ v0
>> 5) + v0
) ^ (sum
+ k
[sum
>>11 & 3]);
329 return (u64
) v0
+ ((u64
) v1
<< 32);
333 * Check if any page in a range is under writeback
335 * This is currently done by walking the list of writepage requests
336 * for the inode, which can be pretty inefficient.
338 static bool fuse_range_is_writeback(struct inode
*inode
, pgoff_t idx_from
,
341 struct fuse_conn
*fc
= get_fuse_conn(inode
);
342 struct fuse_inode
*fi
= get_fuse_inode(inode
);
343 struct fuse_req
*req
;
346 spin_lock(&fc
->lock
);
347 list_for_each_entry(req
, &fi
->writepages
, writepages_entry
) {
350 BUG_ON(req
->inode
!= inode
);
351 curr_index
= req
->misc
.write
.in
.offset
>> PAGE_SHIFT
;
352 if (idx_from
< curr_index
+ req
->num_pages
&&
353 curr_index
<= idx_to
) {
358 spin_unlock(&fc
->lock
);
363 static inline bool fuse_page_is_writeback(struct inode
*inode
, pgoff_t index
)
365 return fuse_range_is_writeback(inode
, index
, index
);
369 * Wait for page writeback to be completed.
371 * Since fuse doesn't rely on the VM writeback tracking, this has to
372 * use some other means.
374 static int fuse_wait_on_page_writeback(struct inode
*inode
, pgoff_t index
)
376 struct fuse_inode
*fi
= get_fuse_inode(inode
);
378 wait_event(fi
->page_waitq
, !fuse_page_is_writeback(inode
, index
));
383 * Wait for all pending writepages on the inode to finish.
385 * This is currently done by blocking further writes with FUSE_NOWRITE
386 * and waiting for all sent writes to complete.
388 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
389 * could conflict with truncation.
391 static void fuse_sync_writes(struct inode
*inode
)
393 fuse_set_nowrite(inode
);
394 fuse_release_nowrite(inode
);
397 static int fuse_flush(struct file
*file
, fl_owner_t id
)
399 struct inode
*inode
= file_inode(file
);
400 struct fuse_conn
*fc
= get_fuse_conn(inode
);
401 struct fuse_file
*ff
= file
->private_data
;
402 struct fuse_req
*req
;
403 struct fuse_flush_in inarg
;
406 if (is_bad_inode(inode
))
412 err
= write_inode_now(inode
, 1);
417 fuse_sync_writes(inode
);
420 err
= filemap_check_errors(file
->f_mapping
);
424 req
= fuse_get_req_nofail_nopages(fc
, file
);
425 memset(&inarg
, 0, sizeof(inarg
));
427 inarg
.lock_owner
= fuse_lock_owner_id(fc
, id
);
428 req
->in
.h
.opcode
= FUSE_FLUSH
;
429 req
->in
.h
.nodeid
= get_node_id(inode
);
431 req
->in
.args
[0].size
= sizeof(inarg
);
432 req
->in
.args
[0].value
= &inarg
;
433 __set_bit(FR_FORCE
, &req
->flags
);
434 fuse_request_send(fc
, req
);
435 err
= req
->out
.h
.error
;
436 fuse_put_request(fc
, req
);
437 if (err
== -ENOSYS
) {
444 int fuse_fsync_common(struct file
*file
, loff_t start
, loff_t end
,
445 int datasync
, int isdir
)
447 struct inode
*inode
= file
->f_mapping
->host
;
448 struct fuse_conn
*fc
= get_fuse_conn(inode
);
449 struct fuse_file
*ff
= file
->private_data
;
451 struct fuse_fsync_in inarg
;
454 if (is_bad_inode(inode
))
460 * Start writeback against all dirty pages of the inode, then
461 * wait for all outstanding writes, before sending the FSYNC
464 err
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
468 fuse_sync_writes(inode
);
471 * Due to implementation of fuse writeback
472 * filemap_write_and_wait_range() does not catch errors.
473 * We have to do this directly after fuse_sync_writes()
475 err
= filemap_check_errors(file
->f_mapping
);
479 err
= sync_inode_metadata(inode
, 1);
483 if ((!isdir
&& fc
->no_fsync
) || (isdir
&& fc
->no_fsyncdir
))
486 memset(&inarg
, 0, sizeof(inarg
));
488 inarg
.fsync_flags
= datasync
? 1 : 0;
489 args
.in
.h
.opcode
= isdir
? FUSE_FSYNCDIR
: FUSE_FSYNC
;
490 args
.in
.h
.nodeid
= get_node_id(inode
);
492 args
.in
.args
[0].size
= sizeof(inarg
);
493 args
.in
.args
[0].value
= &inarg
;
494 err
= fuse_simple_request(fc
, &args
);
495 if (err
== -ENOSYS
) {
507 static int fuse_fsync(struct file
*file
, loff_t start
, loff_t end
,
510 return fuse_fsync_common(file
, start
, end
, datasync
, 0);
513 void fuse_read_fill(struct fuse_req
*req
, struct file
*file
, loff_t pos
,
514 size_t count
, int opcode
)
516 struct fuse_read_in
*inarg
= &req
->misc
.read
.in
;
517 struct fuse_file
*ff
= file
->private_data
;
522 inarg
->flags
= file
->f_flags
;
523 req
->in
.h
.opcode
= opcode
;
524 req
->in
.h
.nodeid
= ff
->nodeid
;
526 req
->in
.args
[0].size
= sizeof(struct fuse_read_in
);
527 req
->in
.args
[0].value
= inarg
;
529 req
->out
.numargs
= 1;
530 req
->out
.args
[0].size
= count
;
533 static void fuse_release_user_pages(struct fuse_req
*req
, int write
)
537 for (i
= 0; i
< req
->num_pages
; i
++) {
538 struct page
*page
= req
->pages
[i
];
540 set_page_dirty_lock(page
);
545 static void fuse_io_release(struct kref
*kref
)
547 kfree(container_of(kref
, struct fuse_io_priv
, refcnt
));
550 static ssize_t
fuse_get_res_by_io(struct fuse_io_priv
*io
)
555 if (io
->bytes
>= 0 && io
->write
)
558 return io
->bytes
< 0 ? io
->size
: io
->bytes
;
562 * In case of short read, the caller sets 'pos' to the position of
563 * actual end of fuse request in IO request. Otherwise, if bytes_requested
564 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
567 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
568 * both submitted asynchronously. The first of them was ACKed by userspace as
569 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
570 * second request was ACKed as short, e.g. only 1K was read, resulting in
573 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
574 * will be equal to the length of the longest contiguous fragment of
575 * transferred data starting from the beginning of IO request.
577 static void fuse_aio_complete(struct fuse_io_priv
*io
, int err
, ssize_t pos
)
581 spin_lock(&io
->lock
);
583 io
->err
= io
->err
? : err
;
584 else if (pos
>= 0 && (io
->bytes
< 0 || pos
< io
->bytes
))
588 if (!left
&& io
->blocking
)
590 spin_unlock(&io
->lock
);
592 if (!left
&& !io
->blocking
) {
593 ssize_t res
= fuse_get_res_by_io(io
);
596 struct inode
*inode
= file_inode(io
->iocb
->ki_filp
);
597 struct fuse_conn
*fc
= get_fuse_conn(inode
);
598 struct fuse_inode
*fi
= get_fuse_inode(inode
);
600 spin_lock(&fc
->lock
);
601 fi
->attr_version
= ++fc
->attr_version
;
602 spin_unlock(&fc
->lock
);
605 io
->iocb
->ki_complete(io
->iocb
, res
, 0);
608 kref_put(&io
->refcnt
, fuse_io_release
);
611 static void fuse_aio_complete_req(struct fuse_conn
*fc
, struct fuse_req
*req
)
613 struct fuse_io_priv
*io
= req
->io
;
616 fuse_release_user_pages(req
, !io
->write
);
619 if (req
->misc
.write
.in
.size
!= req
->misc
.write
.out
.size
)
620 pos
= req
->misc
.write
.in
.offset
- io
->offset
+
621 req
->misc
.write
.out
.size
;
623 if (req
->misc
.read
.in
.size
!= req
->out
.args
[0].size
)
624 pos
= req
->misc
.read
.in
.offset
- io
->offset
+
625 req
->out
.args
[0].size
;
628 fuse_aio_complete(io
, req
->out
.h
.error
, pos
);
631 static size_t fuse_async_req_send(struct fuse_conn
*fc
, struct fuse_req
*req
,
632 size_t num_bytes
, struct fuse_io_priv
*io
)
634 spin_lock(&io
->lock
);
635 kref_get(&io
->refcnt
);
636 io
->size
+= num_bytes
;
638 spin_unlock(&io
->lock
);
641 req
->end
= fuse_aio_complete_req
;
643 __fuse_get_request(req
);
644 fuse_request_send_background(fc
, req
);
649 static size_t fuse_send_read(struct fuse_req
*req
, struct fuse_io_priv
*io
,
650 loff_t pos
, size_t count
, fl_owner_t owner
)
652 struct file
*file
= io
->file
;
653 struct fuse_file
*ff
= file
->private_data
;
654 struct fuse_conn
*fc
= ff
->fc
;
656 fuse_read_fill(req
, file
, pos
, count
, FUSE_READ
);
658 struct fuse_read_in
*inarg
= &req
->misc
.read
.in
;
660 inarg
->read_flags
|= FUSE_READ_LOCKOWNER
;
661 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
665 return fuse_async_req_send(fc
, req
, count
, io
);
667 fuse_request_send(fc
, req
);
668 return req
->out
.args
[0].size
;
671 static void fuse_read_update_size(struct inode
*inode
, loff_t size
,
674 struct fuse_conn
*fc
= get_fuse_conn(inode
);
675 struct fuse_inode
*fi
= get_fuse_inode(inode
);
677 spin_lock(&fc
->lock
);
678 if (attr_ver
== fi
->attr_version
&& size
< inode
->i_size
&&
679 !test_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
)) {
680 fi
->attr_version
= ++fc
->attr_version
;
681 i_size_write(inode
, size
);
683 spin_unlock(&fc
->lock
);
686 static void fuse_short_read(struct fuse_req
*req
, struct inode
*inode
,
689 size_t num_read
= req
->out
.args
[0].size
;
690 struct fuse_conn
*fc
= get_fuse_conn(inode
);
692 if (fc
->writeback_cache
) {
694 * A hole in a file. Some data after the hole are in page cache,
695 * but have not reached the client fs yet. So, the hole is not
699 int start_idx
= num_read
>> PAGE_SHIFT
;
700 size_t off
= num_read
& (PAGE_SIZE
- 1);
702 for (i
= start_idx
; i
< req
->num_pages
; i
++) {
703 zero_user_segment(req
->pages
[i
], off
, PAGE_SIZE
);
707 loff_t pos
= page_offset(req
->pages
[0]) + num_read
;
708 fuse_read_update_size(inode
, pos
, attr_ver
);
712 static int fuse_do_readpage(struct file
*file
, struct page
*page
)
714 struct fuse_io_priv io
= FUSE_IO_PRIV_SYNC(file
);
715 struct inode
*inode
= page
->mapping
->host
;
716 struct fuse_conn
*fc
= get_fuse_conn(inode
);
717 struct fuse_req
*req
;
719 loff_t pos
= page_offset(page
);
720 size_t count
= PAGE_SIZE
;
725 * Page writeback can extend beyond the lifetime of the
726 * page-cache page, so make sure we read a properly synced
729 fuse_wait_on_page_writeback(inode
, page
->index
);
731 req
= fuse_get_req(fc
, 1);
735 attr_ver
= fuse_get_attr_version(fc
);
737 req
->out
.page_zeroing
= 1;
738 req
->out
.argpages
= 1;
740 req
->pages
[0] = page
;
741 req
->page_descs
[0].length
= count
;
742 num_read
= fuse_send_read(req
, &io
, pos
, count
, NULL
);
743 err
= req
->out
.h
.error
;
747 * Short read means EOF. If file size is larger, truncate it
749 if (num_read
< count
)
750 fuse_short_read(req
, inode
, attr_ver
);
752 SetPageUptodate(page
);
755 fuse_put_request(fc
, req
);
760 static int fuse_readpage(struct file
*file
, struct page
*page
)
762 struct inode
*inode
= page
->mapping
->host
;
766 if (is_bad_inode(inode
))
769 err
= fuse_do_readpage(file
, page
);
770 fuse_invalidate_atime(inode
);
776 static void fuse_readpages_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
779 size_t count
= req
->misc
.read
.in
.size
;
780 size_t num_read
= req
->out
.args
[0].size
;
781 struct address_space
*mapping
= NULL
;
783 for (i
= 0; mapping
== NULL
&& i
< req
->num_pages
; i
++)
784 mapping
= req
->pages
[i
]->mapping
;
787 struct inode
*inode
= mapping
->host
;
790 * Short read means EOF. If file size is larger, truncate it
792 if (!req
->out
.h
.error
&& num_read
< count
)
793 fuse_short_read(req
, inode
, req
->misc
.read
.attr_ver
);
795 fuse_invalidate_atime(inode
);
798 for (i
= 0; i
< req
->num_pages
; i
++) {
799 struct page
*page
= req
->pages
[i
];
800 if (!req
->out
.h
.error
)
801 SetPageUptodate(page
);
808 fuse_file_put(req
->ff
, false);
811 static void fuse_send_readpages(struct fuse_req
*req
, struct file
*file
)
813 struct fuse_file
*ff
= file
->private_data
;
814 struct fuse_conn
*fc
= ff
->fc
;
815 loff_t pos
= page_offset(req
->pages
[0]);
816 size_t count
= req
->num_pages
<< PAGE_SHIFT
;
818 req
->out
.argpages
= 1;
819 req
->out
.page_zeroing
= 1;
820 req
->out
.page_replace
= 1;
821 fuse_read_fill(req
, file
, pos
, count
, FUSE_READ
);
822 req
->misc
.read
.attr_ver
= fuse_get_attr_version(fc
);
823 if (fc
->async_read
) {
824 req
->ff
= fuse_file_get(ff
);
825 req
->end
= fuse_readpages_end
;
826 fuse_request_send_background(fc
, req
);
828 fuse_request_send(fc
, req
);
829 fuse_readpages_end(fc
, req
);
830 fuse_put_request(fc
, req
);
834 struct fuse_fill_data
{
835 struct fuse_req
*req
;
841 static int fuse_readpages_fill(void *_data
, struct page
*page
)
843 struct fuse_fill_data
*data
= _data
;
844 struct fuse_req
*req
= data
->req
;
845 struct inode
*inode
= data
->inode
;
846 struct fuse_conn
*fc
= get_fuse_conn(inode
);
848 fuse_wait_on_page_writeback(inode
, page
->index
);
850 if (req
->num_pages
&&
851 (req
->num_pages
== FUSE_MAX_PAGES_PER_REQ
||
852 (req
->num_pages
+ 1) * PAGE_SIZE
> fc
->max_read
||
853 req
->pages
[req
->num_pages
- 1]->index
+ 1 != page
->index
)) {
854 int nr_alloc
= min_t(unsigned, data
->nr_pages
,
855 FUSE_MAX_PAGES_PER_REQ
);
856 fuse_send_readpages(req
, data
->file
);
858 req
= fuse_get_req_for_background(fc
, nr_alloc
);
860 req
= fuse_get_req(fc
, nr_alloc
);
869 if (WARN_ON(req
->num_pages
>= req
->max_pages
)) {
870 fuse_put_request(fc
, req
);
875 req
->pages
[req
->num_pages
] = page
;
876 req
->page_descs
[req
->num_pages
].length
= PAGE_SIZE
;
882 static int fuse_readpages(struct file
*file
, struct address_space
*mapping
,
883 struct list_head
*pages
, unsigned nr_pages
)
885 struct inode
*inode
= mapping
->host
;
886 struct fuse_conn
*fc
= get_fuse_conn(inode
);
887 struct fuse_fill_data data
;
889 int nr_alloc
= min_t(unsigned, nr_pages
, FUSE_MAX_PAGES_PER_REQ
);
892 if (is_bad_inode(inode
))
898 data
.req
= fuse_get_req_for_background(fc
, nr_alloc
);
900 data
.req
= fuse_get_req(fc
, nr_alloc
);
901 data
.nr_pages
= nr_pages
;
902 err
= PTR_ERR(data
.req
);
903 if (IS_ERR(data
.req
))
906 err
= read_cache_pages(mapping
, pages
, fuse_readpages_fill
, &data
);
908 if (data
.req
->num_pages
)
909 fuse_send_readpages(data
.req
, file
);
911 fuse_put_request(fc
, data
.req
);
917 static ssize_t
fuse_file_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
919 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
920 struct fuse_conn
*fc
= get_fuse_conn(inode
);
923 * In auto invalidate mode, always update attributes on read.
924 * Otherwise, only update if we attempt to read past EOF (to ensure
925 * i_size is up to date).
927 if (fc
->auto_inval_data
||
928 (iocb
->ki_pos
+ iov_iter_count(to
) > i_size_read(inode
))) {
930 err
= fuse_update_attributes(inode
, NULL
, iocb
->ki_filp
, NULL
);
935 return generic_file_read_iter(iocb
, to
);
938 static void fuse_write_fill(struct fuse_req
*req
, struct fuse_file
*ff
,
939 loff_t pos
, size_t count
)
941 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
942 struct fuse_write_out
*outarg
= &req
->misc
.write
.out
;
947 req
->in
.h
.opcode
= FUSE_WRITE
;
948 req
->in
.h
.nodeid
= ff
->nodeid
;
950 if (ff
->fc
->minor
< 9)
951 req
->in
.args
[0].size
= FUSE_COMPAT_WRITE_IN_SIZE
;
953 req
->in
.args
[0].size
= sizeof(struct fuse_write_in
);
954 req
->in
.args
[0].value
= inarg
;
955 req
->in
.args
[1].size
= count
;
956 req
->out
.numargs
= 1;
957 req
->out
.args
[0].size
= sizeof(struct fuse_write_out
);
958 req
->out
.args
[0].value
= outarg
;
961 static size_t fuse_send_write(struct fuse_req
*req
, struct fuse_io_priv
*io
,
962 loff_t pos
, size_t count
, fl_owner_t owner
)
964 struct file
*file
= io
->file
;
965 struct fuse_file
*ff
= file
->private_data
;
966 struct fuse_conn
*fc
= ff
->fc
;
967 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
969 fuse_write_fill(req
, ff
, pos
, count
);
970 inarg
->flags
= file
->f_flags
;
972 inarg
->write_flags
|= FUSE_WRITE_LOCKOWNER
;
973 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
977 return fuse_async_req_send(fc
, req
, count
, io
);
979 fuse_request_send(fc
, req
);
980 return req
->misc
.write
.out
.size
;
983 bool fuse_write_update_size(struct inode
*inode
, loff_t pos
)
985 struct fuse_conn
*fc
= get_fuse_conn(inode
);
986 struct fuse_inode
*fi
= get_fuse_inode(inode
);
989 spin_lock(&fc
->lock
);
990 fi
->attr_version
= ++fc
->attr_version
;
991 if (pos
> inode
->i_size
) {
992 i_size_write(inode
, pos
);
995 spin_unlock(&fc
->lock
);
1000 static size_t fuse_send_write_pages(struct fuse_req
*req
, struct file
*file
,
1001 struct inode
*inode
, loff_t pos
,
1007 struct fuse_io_priv io
= FUSE_IO_PRIV_SYNC(file
);
1009 for (i
= 0; i
< req
->num_pages
; i
++)
1010 fuse_wait_on_page_writeback(inode
, req
->pages
[i
]->index
);
1012 res
= fuse_send_write(req
, &io
, pos
, count
, NULL
);
1014 offset
= req
->page_descs
[0].offset
;
1016 for (i
= 0; i
< req
->num_pages
; i
++) {
1017 struct page
*page
= req
->pages
[i
];
1019 if (!req
->out
.h
.error
&& !offset
&& count
>= PAGE_SIZE
)
1020 SetPageUptodate(page
);
1022 if (count
> PAGE_SIZE
- offset
)
1023 count
-= PAGE_SIZE
- offset
;
1035 static ssize_t
fuse_fill_write_pages(struct fuse_req
*req
,
1036 struct address_space
*mapping
,
1037 struct iov_iter
*ii
, loff_t pos
)
1039 struct fuse_conn
*fc
= get_fuse_conn(mapping
->host
);
1040 unsigned offset
= pos
& (PAGE_SIZE
- 1);
1044 req
->in
.argpages
= 1;
1045 req
->page_descs
[0].offset
= offset
;
1050 pgoff_t index
= pos
>> PAGE_SHIFT
;
1051 size_t bytes
= min_t(size_t, PAGE_SIZE
- offset
,
1052 iov_iter_count(ii
));
1054 bytes
= min_t(size_t, bytes
, fc
->max_write
- count
);
1058 if (iov_iter_fault_in_readable(ii
, bytes
))
1062 page
= grab_cache_page_write_begin(mapping
, index
, 0);
1066 if (mapping_writably_mapped(mapping
))
1067 flush_dcache_page(page
);
1069 tmp
= iov_iter_copy_from_user_atomic(page
, ii
, offset
, bytes
);
1070 flush_dcache_page(page
);
1072 iov_iter_advance(ii
, tmp
);
1076 bytes
= min(bytes
, iov_iter_single_seg_count(ii
));
1081 req
->pages
[req
->num_pages
] = page
;
1082 req
->page_descs
[req
->num_pages
].length
= tmp
;
1088 if (offset
== PAGE_SIZE
)
1091 if (!fc
->big_writes
)
1093 } while (iov_iter_count(ii
) && count
< fc
->max_write
&&
1094 req
->num_pages
< req
->max_pages
&& offset
== 0);
1096 return count
> 0 ? count
: err
;
1099 static inline unsigned fuse_wr_pages(loff_t pos
, size_t len
)
1101 return min_t(unsigned,
1102 ((pos
+ len
- 1) >> PAGE_SHIFT
) -
1103 (pos
>> PAGE_SHIFT
) + 1,
1104 FUSE_MAX_PAGES_PER_REQ
);
1107 static ssize_t
fuse_perform_write(struct file
*file
,
1108 struct address_space
*mapping
,
1109 struct iov_iter
*ii
, loff_t pos
)
1111 struct inode
*inode
= mapping
->host
;
1112 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1113 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1117 if (is_bad_inode(inode
))
1120 if (inode
->i_size
< pos
+ iov_iter_count(ii
))
1121 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1124 struct fuse_req
*req
;
1126 unsigned nr_pages
= fuse_wr_pages(pos
, iov_iter_count(ii
));
1128 req
= fuse_get_req(fc
, nr_pages
);
1134 count
= fuse_fill_write_pages(req
, mapping
, ii
, pos
);
1140 num_written
= fuse_send_write_pages(req
, file
, inode
,
1142 err
= req
->out
.h
.error
;
1147 /* break out of the loop on short write */
1148 if (num_written
!= count
)
1152 fuse_put_request(fc
, req
);
1153 } while (!err
&& iov_iter_count(ii
));
1156 fuse_write_update_size(inode
, pos
);
1158 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1159 fuse_invalidate_attr(inode
);
1161 return res
> 0 ? res
: err
;
1164 static ssize_t
fuse_file_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
1166 struct file
*file
= iocb
->ki_filp
;
1167 struct address_space
*mapping
= file
->f_mapping
;
1168 ssize_t written
= 0;
1169 ssize_t written_buffered
= 0;
1170 struct inode
*inode
= mapping
->host
;
1174 if (get_fuse_conn(inode
)->writeback_cache
) {
1175 /* Update size (EOF optimization) and mode (SUID clearing) */
1176 err
= fuse_update_attributes(mapping
->host
, NULL
, file
, NULL
);
1180 return generic_file_write_iter(iocb
, from
);
1185 /* We can write back this queue in page reclaim */
1186 current
->backing_dev_info
= inode_to_bdi(inode
);
1188 err
= generic_write_checks(iocb
, from
);
1192 err
= file_remove_privs(file
);
1196 err
= file_update_time(file
);
1200 if (iocb
->ki_flags
& IOCB_DIRECT
) {
1201 loff_t pos
= iocb
->ki_pos
;
1202 written
= generic_file_direct_write(iocb
, from
);
1203 if (written
< 0 || !iov_iter_count(from
))
1208 written_buffered
= fuse_perform_write(file
, mapping
, from
, pos
);
1209 if (written_buffered
< 0) {
1210 err
= written_buffered
;
1213 endbyte
= pos
+ written_buffered
- 1;
1215 err
= filemap_write_and_wait_range(file
->f_mapping
, pos
,
1220 invalidate_mapping_pages(file
->f_mapping
,
1222 endbyte
>> PAGE_SHIFT
);
1224 written
+= written_buffered
;
1225 iocb
->ki_pos
= pos
+ written_buffered
;
1227 written
= fuse_perform_write(file
, mapping
, from
, iocb
->ki_pos
);
1229 iocb
->ki_pos
+= written
;
1232 current
->backing_dev_info
= NULL
;
1233 inode_unlock(inode
);
1235 return written
? written
: err
;
1238 static inline void fuse_page_descs_length_init(struct fuse_req
*req
,
1239 unsigned index
, unsigned nr_pages
)
1243 for (i
= index
; i
< index
+ nr_pages
; i
++)
1244 req
->page_descs
[i
].length
= PAGE_SIZE
-
1245 req
->page_descs
[i
].offset
;
1248 static inline unsigned long fuse_get_user_addr(const struct iov_iter
*ii
)
1250 return (unsigned long)ii
->iov
->iov_base
+ ii
->iov_offset
;
1253 static inline size_t fuse_get_frag_size(const struct iov_iter
*ii
,
1256 return min(iov_iter_single_seg_count(ii
), max_size
);
1259 static int fuse_get_user_pages(struct fuse_req
*req
, struct iov_iter
*ii
,
1260 size_t *nbytesp
, int write
)
1262 size_t nbytes
= 0; /* # bytes already packed in req */
1265 /* Special case for kernel I/O: can copy directly into the buffer */
1266 if (ii
->type
& ITER_KVEC
) {
1267 unsigned long user_addr
= fuse_get_user_addr(ii
);
1268 size_t frag_size
= fuse_get_frag_size(ii
, *nbytesp
);
1271 req
->in
.args
[1].value
= (void *) user_addr
;
1273 req
->out
.args
[0].value
= (void *) user_addr
;
1275 iov_iter_advance(ii
, frag_size
);
1276 *nbytesp
= frag_size
;
1280 while (nbytes
< *nbytesp
&& req
->num_pages
< req
->max_pages
) {
1283 ret
= iov_iter_get_pages(ii
, &req
->pages
[req
->num_pages
],
1285 req
->max_pages
- req
->num_pages
,
1290 iov_iter_advance(ii
, ret
);
1294 npages
= (ret
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
1296 req
->page_descs
[req
->num_pages
].offset
= start
;
1297 fuse_page_descs_length_init(req
, req
->num_pages
, npages
);
1299 req
->num_pages
+= npages
;
1300 req
->page_descs
[req
->num_pages
- 1].length
-=
1301 (PAGE_SIZE
- ret
) & (PAGE_SIZE
- 1);
1305 req
->in
.argpages
= 1;
1307 req
->out
.argpages
= 1;
1311 return ret
< 0 ? ret
: 0;
1314 static inline int fuse_iter_npages(const struct iov_iter
*ii_p
)
1316 return iov_iter_npages(ii_p
, FUSE_MAX_PAGES_PER_REQ
);
1319 ssize_t
fuse_direct_io(struct fuse_io_priv
*io
, struct iov_iter
*iter
,
1320 loff_t
*ppos
, int flags
)
1322 int write
= flags
& FUSE_DIO_WRITE
;
1323 int cuse
= flags
& FUSE_DIO_CUSE
;
1324 struct file
*file
= io
->file
;
1325 struct inode
*inode
= file
->f_mapping
->host
;
1326 struct fuse_file
*ff
= file
->private_data
;
1327 struct fuse_conn
*fc
= ff
->fc
;
1328 size_t nmax
= write
? fc
->max_write
: fc
->max_read
;
1330 size_t count
= iov_iter_count(iter
);
1331 pgoff_t idx_from
= pos
>> PAGE_SHIFT
;
1332 pgoff_t idx_to
= (pos
+ count
- 1) >> PAGE_SHIFT
;
1334 struct fuse_req
*req
;
1338 req
= fuse_get_req_for_background(fc
, fuse_iter_npages(iter
));
1340 req
= fuse_get_req(fc
, fuse_iter_npages(iter
));
1342 return PTR_ERR(req
);
1344 if (!cuse
&& fuse_range_is_writeback(inode
, idx_from
, idx_to
)) {
1347 fuse_sync_writes(inode
);
1349 inode_unlock(inode
);
1354 fl_owner_t owner
= current
->files
;
1355 size_t nbytes
= min(count
, nmax
);
1356 err
= fuse_get_user_pages(req
, iter
, &nbytes
, write
);
1361 nres
= fuse_send_write(req
, io
, pos
, nbytes
, owner
);
1363 nres
= fuse_send_read(req
, io
, pos
, nbytes
, owner
);
1366 fuse_release_user_pages(req
, !write
);
1367 if (req
->out
.h
.error
) {
1368 err
= req
->out
.h
.error
;
1370 } else if (nres
> nbytes
) {
1381 fuse_put_request(fc
, req
);
1383 req
= fuse_get_req_for_background(fc
,
1384 fuse_iter_npages(iter
));
1386 req
= fuse_get_req(fc
, fuse_iter_npages(iter
));
1392 fuse_put_request(fc
, req
);
1396 return res
> 0 ? res
: err
;
1398 EXPORT_SYMBOL_GPL(fuse_direct_io
);
1400 static ssize_t
__fuse_direct_read(struct fuse_io_priv
*io
,
1401 struct iov_iter
*iter
,
1405 struct file
*file
= io
->file
;
1406 struct inode
*inode
= file_inode(file
);
1408 if (is_bad_inode(inode
))
1411 res
= fuse_direct_io(io
, iter
, ppos
, 0);
1413 fuse_invalidate_attr(inode
);
1418 static ssize_t
fuse_direct_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
1420 struct fuse_io_priv io
= FUSE_IO_PRIV_SYNC(iocb
->ki_filp
);
1421 return __fuse_direct_read(&io
, to
, &iocb
->ki_pos
);
1424 static ssize_t
fuse_direct_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
1426 struct file
*file
= iocb
->ki_filp
;
1427 struct inode
*inode
= file_inode(file
);
1428 struct fuse_io_priv io
= FUSE_IO_PRIV_SYNC(file
);
1431 if (is_bad_inode(inode
))
1434 /* Don't allow parallel writes to the same file */
1436 res
= generic_write_checks(iocb
, from
);
1438 res
= fuse_direct_io(&io
, from
, &iocb
->ki_pos
, FUSE_DIO_WRITE
);
1439 fuse_invalidate_attr(inode
);
1441 fuse_write_update_size(inode
, iocb
->ki_pos
);
1442 inode_unlock(inode
);
1447 static void fuse_writepage_free(struct fuse_conn
*fc
, struct fuse_req
*req
)
1451 for (i
= 0; i
< req
->num_pages
; i
++)
1452 __free_page(req
->pages
[i
]);
1455 fuse_file_put(req
->ff
, false);
1458 static void fuse_writepage_finish(struct fuse_conn
*fc
, struct fuse_req
*req
)
1460 struct inode
*inode
= req
->inode
;
1461 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1462 struct backing_dev_info
*bdi
= inode_to_bdi(inode
);
1465 list_del(&req
->writepages_entry
);
1466 for (i
= 0; i
< req
->num_pages
; i
++) {
1467 dec_wb_stat(&bdi
->wb
, WB_WRITEBACK
);
1468 dec_node_page_state(req
->pages
[i
], NR_WRITEBACK_TEMP
);
1469 wb_writeout_inc(&bdi
->wb
);
1471 wake_up(&fi
->page_waitq
);
1474 /* Called under fc->lock, may release and reacquire it */
1475 static void fuse_send_writepage(struct fuse_conn
*fc
, struct fuse_req
*req
,
1477 __releases(fc
->lock
)
1478 __acquires(fc
->lock
)
1480 struct fuse_inode
*fi
= get_fuse_inode(req
->inode
);
1481 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
1482 __u64 data_size
= req
->num_pages
* PAGE_SIZE
;
1487 if (inarg
->offset
+ data_size
<= size
) {
1488 inarg
->size
= data_size
;
1489 } else if (inarg
->offset
< size
) {
1490 inarg
->size
= size
- inarg
->offset
;
1492 /* Got truncated off completely */
1496 req
->in
.args
[1].size
= inarg
->size
;
1498 fuse_request_send_background_locked(fc
, req
);
1502 fuse_writepage_finish(fc
, req
);
1503 spin_unlock(&fc
->lock
);
1504 fuse_writepage_free(fc
, req
);
1505 fuse_put_request(fc
, req
);
1506 spin_lock(&fc
->lock
);
1510 * If fi->writectr is positive (no truncate or fsync going on) send
1511 * all queued writepage requests.
1513 * Called with fc->lock
1515 void fuse_flush_writepages(struct inode
*inode
)
1516 __releases(fc
->lock
)
1517 __acquires(fc
->lock
)
1519 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1520 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1521 size_t crop
= i_size_read(inode
);
1522 struct fuse_req
*req
;
1524 while (fi
->writectr
>= 0 && !list_empty(&fi
->queued_writes
)) {
1525 req
= list_entry(fi
->queued_writes
.next
, struct fuse_req
, list
);
1526 list_del_init(&req
->list
);
1527 fuse_send_writepage(fc
, req
, crop
);
1531 static void fuse_writepage_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
1533 struct inode
*inode
= req
->inode
;
1534 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1536 mapping_set_error(inode
->i_mapping
, req
->out
.h
.error
);
1537 spin_lock(&fc
->lock
);
1538 while (req
->misc
.write
.next
) {
1539 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1540 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
1541 struct fuse_req
*next
= req
->misc
.write
.next
;
1542 req
->misc
.write
.next
= next
->misc
.write
.next
;
1543 next
->misc
.write
.next
= NULL
;
1544 next
->ff
= fuse_file_get(req
->ff
);
1545 list_add(&next
->writepages_entry
, &fi
->writepages
);
1548 * Skip fuse_flush_writepages() to make it easy to crop requests
1549 * based on primary request size.
1551 * 1st case (trivial): there are no concurrent activities using
1552 * fuse_set/release_nowrite. Then we're on safe side because
1553 * fuse_flush_writepages() would call fuse_send_writepage()
1556 * 2nd case: someone called fuse_set_nowrite and it is waiting
1557 * now for completion of all in-flight requests. This happens
1558 * rarely and no more than once per page, so this should be
1561 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1562 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1563 * that fuse_set_nowrite returned implies that all in-flight
1564 * requests were completed along with all of their secondary
1565 * requests. Further primary requests are blocked by negative
1566 * writectr. Hence there cannot be any in-flight requests and
1567 * no invocations of fuse_writepage_end() while we're in
1568 * fuse_set_nowrite..fuse_release_nowrite section.
1570 fuse_send_writepage(fc
, next
, inarg
->offset
+ inarg
->size
);
1573 fuse_writepage_finish(fc
, req
);
1574 spin_unlock(&fc
->lock
);
1575 fuse_writepage_free(fc
, req
);
1578 static struct fuse_file
*__fuse_write_file_get(struct fuse_conn
*fc
,
1579 struct fuse_inode
*fi
)
1581 struct fuse_file
*ff
= NULL
;
1583 spin_lock(&fc
->lock
);
1584 if (!list_empty(&fi
->write_files
)) {
1585 ff
= list_entry(fi
->write_files
.next
, struct fuse_file
,
1589 spin_unlock(&fc
->lock
);
1594 static struct fuse_file
*fuse_write_file_get(struct fuse_conn
*fc
,
1595 struct fuse_inode
*fi
)
1597 struct fuse_file
*ff
= __fuse_write_file_get(fc
, fi
);
1602 int fuse_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
1604 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1605 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1606 struct fuse_file
*ff
;
1609 ff
= __fuse_write_file_get(fc
, fi
);
1610 err
= fuse_flush_times(inode
, ff
);
1612 fuse_file_put(ff
, 0);
1617 static int fuse_writepage_locked(struct page
*page
)
1619 struct address_space
*mapping
= page
->mapping
;
1620 struct inode
*inode
= mapping
->host
;
1621 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1622 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1623 struct fuse_req
*req
;
1624 struct page
*tmp_page
;
1625 int error
= -ENOMEM
;
1627 set_page_writeback(page
);
1629 req
= fuse_request_alloc_nofs(1);
1633 /* writeback always goes to bg_queue */
1634 __set_bit(FR_BACKGROUND
, &req
->flags
);
1635 tmp_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
1640 req
->ff
= fuse_write_file_get(fc
, fi
);
1644 fuse_write_fill(req
, req
->ff
, page_offset(page
), 0);
1646 copy_highpage(tmp_page
, page
);
1647 req
->misc
.write
.in
.write_flags
|= FUSE_WRITE_CACHE
;
1648 req
->misc
.write
.next
= NULL
;
1649 req
->in
.argpages
= 1;
1651 req
->pages
[0] = tmp_page
;
1652 req
->page_descs
[0].offset
= 0;
1653 req
->page_descs
[0].length
= PAGE_SIZE
;
1654 req
->end
= fuse_writepage_end
;
1657 inc_wb_stat(&inode_to_bdi(inode
)->wb
, WB_WRITEBACK
);
1658 inc_node_page_state(tmp_page
, NR_WRITEBACK_TEMP
);
1660 spin_lock(&fc
->lock
);
1661 list_add(&req
->writepages_entry
, &fi
->writepages
);
1662 list_add_tail(&req
->list
, &fi
->queued_writes
);
1663 fuse_flush_writepages(inode
);
1664 spin_unlock(&fc
->lock
);
1666 end_page_writeback(page
);
1671 __free_page(tmp_page
);
1673 fuse_request_free(req
);
1675 end_page_writeback(page
);
1679 static int fuse_writepage(struct page
*page
, struct writeback_control
*wbc
)
1683 if (fuse_page_is_writeback(page
->mapping
->host
, page
->index
)) {
1685 * ->writepages() should be called for sync() and friends. We
1686 * should only get here on direct reclaim and then we are
1687 * allowed to skip a page which is already in flight
1689 WARN_ON(wbc
->sync_mode
== WB_SYNC_ALL
);
1691 redirty_page_for_writepage(wbc
, page
);
1695 err
= fuse_writepage_locked(page
);
1701 struct fuse_fill_wb_data
{
1702 struct fuse_req
*req
;
1703 struct fuse_file
*ff
;
1704 struct inode
*inode
;
1705 struct page
**orig_pages
;
1708 static void fuse_writepages_send(struct fuse_fill_wb_data
*data
)
1710 struct fuse_req
*req
= data
->req
;
1711 struct inode
*inode
= data
->inode
;
1712 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1713 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1714 int num_pages
= req
->num_pages
;
1717 req
->ff
= fuse_file_get(data
->ff
);
1718 spin_lock(&fc
->lock
);
1719 list_add_tail(&req
->list
, &fi
->queued_writes
);
1720 fuse_flush_writepages(inode
);
1721 spin_unlock(&fc
->lock
);
1723 for (i
= 0; i
< num_pages
; i
++)
1724 end_page_writeback(data
->orig_pages
[i
]);
1727 static bool fuse_writepage_in_flight(struct fuse_req
*new_req
,
1730 struct fuse_conn
*fc
= get_fuse_conn(new_req
->inode
);
1731 struct fuse_inode
*fi
= get_fuse_inode(new_req
->inode
);
1732 struct fuse_req
*tmp
;
1733 struct fuse_req
*old_req
;
1737 BUG_ON(new_req
->num_pages
!= 0);
1739 spin_lock(&fc
->lock
);
1740 list_del(&new_req
->writepages_entry
);
1741 list_for_each_entry(old_req
, &fi
->writepages
, writepages_entry
) {
1742 BUG_ON(old_req
->inode
!= new_req
->inode
);
1743 curr_index
= old_req
->misc
.write
.in
.offset
>> PAGE_SHIFT
;
1744 if (curr_index
<= page
->index
&&
1745 page
->index
< curr_index
+ old_req
->num_pages
) {
1751 list_add(&new_req
->writepages_entry
, &fi
->writepages
);
1755 new_req
->num_pages
= 1;
1756 for (tmp
= old_req
; tmp
!= NULL
; tmp
= tmp
->misc
.write
.next
) {
1757 BUG_ON(tmp
->inode
!= new_req
->inode
);
1758 curr_index
= tmp
->misc
.write
.in
.offset
>> PAGE_SHIFT
;
1759 if (tmp
->num_pages
== 1 &&
1760 curr_index
== page
->index
) {
1765 if (old_req
->num_pages
== 1 && test_bit(FR_PENDING
, &old_req
->flags
)) {
1766 struct backing_dev_info
*bdi
= inode_to_bdi(page
->mapping
->host
);
1768 copy_highpage(old_req
->pages
[0], page
);
1769 spin_unlock(&fc
->lock
);
1771 dec_wb_stat(&bdi
->wb
, WB_WRITEBACK
);
1772 dec_node_page_state(page
, NR_WRITEBACK_TEMP
);
1773 wb_writeout_inc(&bdi
->wb
);
1774 fuse_writepage_free(fc
, new_req
);
1775 fuse_request_free(new_req
);
1778 new_req
->misc
.write
.next
= old_req
->misc
.write
.next
;
1779 old_req
->misc
.write
.next
= new_req
;
1782 spin_unlock(&fc
->lock
);
1787 static int fuse_writepages_fill(struct page
*page
,
1788 struct writeback_control
*wbc
, void *_data
)
1790 struct fuse_fill_wb_data
*data
= _data
;
1791 struct fuse_req
*req
= data
->req
;
1792 struct inode
*inode
= data
->inode
;
1793 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1794 struct page
*tmp_page
;
1800 data
->ff
= fuse_write_file_get(fc
, get_fuse_inode(inode
));
1806 * Being under writeback is unlikely but possible. For example direct
1807 * read to an mmaped fuse file will set the page dirty twice; once when
1808 * the pages are faulted with get_user_pages(), and then after the read
1811 is_writeback
= fuse_page_is_writeback(inode
, page
->index
);
1813 if (req
&& req
->num_pages
&&
1814 (is_writeback
|| req
->num_pages
== FUSE_MAX_PAGES_PER_REQ
||
1815 (req
->num_pages
+ 1) * PAGE_SIZE
> fc
->max_write
||
1816 data
->orig_pages
[req
->num_pages
- 1]->index
+ 1 != page
->index
)) {
1817 fuse_writepages_send(data
);
1821 tmp_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
1826 * The page must not be redirtied until the writeout is completed
1827 * (i.e. userspace has sent a reply to the write request). Otherwise
1828 * there could be more than one temporary page instance for each real
1831 * This is ensured by holding the page lock in page_mkwrite() while
1832 * checking fuse_page_is_writeback(). We already hold the page lock
1833 * since clear_page_dirty_for_io() and keep it held until we add the
1834 * request to the fi->writepages list and increment req->num_pages.
1835 * After this fuse_page_is_writeback() will indicate that the page is
1836 * under writeback, so we can release the page lock.
1838 if (data
->req
== NULL
) {
1839 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1842 req
= fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ
);
1844 __free_page(tmp_page
);
1848 fuse_write_fill(req
, data
->ff
, page_offset(page
), 0);
1849 req
->misc
.write
.in
.write_flags
|= FUSE_WRITE_CACHE
;
1850 req
->misc
.write
.next
= NULL
;
1851 req
->in
.argpages
= 1;
1852 __set_bit(FR_BACKGROUND
, &req
->flags
);
1854 req
->end
= fuse_writepage_end
;
1857 spin_lock(&fc
->lock
);
1858 list_add(&req
->writepages_entry
, &fi
->writepages
);
1859 spin_unlock(&fc
->lock
);
1863 set_page_writeback(page
);
1865 copy_highpage(tmp_page
, page
);
1866 req
->pages
[req
->num_pages
] = tmp_page
;
1867 req
->page_descs
[req
->num_pages
].offset
= 0;
1868 req
->page_descs
[req
->num_pages
].length
= PAGE_SIZE
;
1870 inc_wb_stat(&inode_to_bdi(inode
)->wb
, WB_WRITEBACK
);
1871 inc_node_page_state(tmp_page
, NR_WRITEBACK_TEMP
);
1874 if (is_writeback
&& fuse_writepage_in_flight(req
, page
)) {
1875 end_page_writeback(page
);
1879 data
->orig_pages
[req
->num_pages
] = page
;
1882 * Protected by fc->lock against concurrent access by
1883 * fuse_page_is_writeback().
1885 spin_lock(&fc
->lock
);
1887 spin_unlock(&fc
->lock
);
1895 static int fuse_writepages(struct address_space
*mapping
,
1896 struct writeback_control
*wbc
)
1898 struct inode
*inode
= mapping
->host
;
1899 struct fuse_fill_wb_data data
;
1903 if (is_bad_inode(inode
))
1911 data
.orig_pages
= kcalloc(FUSE_MAX_PAGES_PER_REQ
,
1912 sizeof(struct page
*),
1914 if (!data
.orig_pages
)
1917 err
= write_cache_pages(mapping
, wbc
, fuse_writepages_fill
, &data
);
1919 /* Ignore errors if we can write at least one page */
1920 BUG_ON(!data
.req
->num_pages
);
1921 fuse_writepages_send(&data
);
1925 fuse_file_put(data
.ff
, false);
1927 kfree(data
.orig_pages
);
1933 * It's worthy to make sure that space is reserved on disk for the write,
1934 * but how to implement it without killing performance need more thinking.
1936 static int fuse_write_begin(struct file
*file
, struct address_space
*mapping
,
1937 loff_t pos
, unsigned len
, unsigned flags
,
1938 struct page
**pagep
, void **fsdata
)
1940 pgoff_t index
= pos
>> PAGE_SHIFT
;
1941 struct fuse_conn
*fc
= get_fuse_conn(file_inode(file
));
1946 WARN_ON(!fc
->writeback_cache
);
1948 page
= grab_cache_page_write_begin(mapping
, index
, flags
);
1952 fuse_wait_on_page_writeback(mapping
->host
, page
->index
);
1954 if (PageUptodate(page
) || len
== PAGE_SIZE
)
1957 * Check if the start this page comes after the end of file, in which
1958 * case the readpage can be optimized away.
1960 fsize
= i_size_read(mapping
->host
);
1961 if (fsize
<= (pos
& PAGE_MASK
)) {
1962 size_t off
= pos
& ~PAGE_MASK
;
1964 zero_user_segment(page
, 0, off
);
1967 err
= fuse_do_readpage(file
, page
);
1981 static int fuse_write_end(struct file
*file
, struct address_space
*mapping
,
1982 loff_t pos
, unsigned len
, unsigned copied
,
1983 struct page
*page
, void *fsdata
)
1985 struct inode
*inode
= page
->mapping
->host
;
1987 if (!PageUptodate(page
)) {
1988 /* Zero any unwritten bytes at the end of the page */
1989 size_t endoff
= (pos
+ copied
) & ~PAGE_MASK
;
1991 zero_user_segment(page
, endoff
, PAGE_SIZE
);
1992 SetPageUptodate(page
);
1995 fuse_write_update_size(inode
, pos
+ copied
);
1996 set_page_dirty(page
);
2003 static int fuse_launder_page(struct page
*page
)
2006 if (clear_page_dirty_for_io(page
)) {
2007 struct inode
*inode
= page
->mapping
->host
;
2008 err
= fuse_writepage_locked(page
);
2010 fuse_wait_on_page_writeback(inode
, page
->index
);
2016 * Write back dirty pages now, because there may not be any suitable
2019 static void fuse_vma_close(struct vm_area_struct
*vma
)
2021 filemap_write_and_wait(vma
->vm_file
->f_mapping
);
2025 * Wait for writeback against this page to complete before allowing it
2026 * to be marked dirty again, and hence written back again, possibly
2027 * before the previous writepage completed.
2029 * Block here, instead of in ->writepage(), so that the userspace fs
2030 * can only block processes actually operating on the filesystem.
2032 * Otherwise unprivileged userspace fs would be able to block
2037 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2039 static int fuse_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
2041 struct page
*page
= vmf
->page
;
2042 struct inode
*inode
= file_inode(vma
->vm_file
);
2044 file_update_time(vma
->vm_file
);
2046 if (page
->mapping
!= inode
->i_mapping
) {
2048 return VM_FAULT_NOPAGE
;
2051 fuse_wait_on_page_writeback(inode
, page
->index
);
2052 return VM_FAULT_LOCKED
;
2055 static const struct vm_operations_struct fuse_file_vm_ops
= {
2056 .close
= fuse_vma_close
,
2057 .fault
= filemap_fault
,
2058 .map_pages
= filemap_map_pages
,
2059 .page_mkwrite
= fuse_page_mkwrite
,
2062 static int fuse_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2064 if ((vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
))
2065 fuse_link_write_file(file
);
2067 file_accessed(file
);
2068 vma
->vm_ops
= &fuse_file_vm_ops
;
2072 static int fuse_direct_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2074 /* Can't provide the coherency needed for MAP_SHARED */
2075 if (vma
->vm_flags
& VM_MAYSHARE
)
2078 invalidate_inode_pages2(file
->f_mapping
);
2080 return generic_file_mmap(file
, vma
);
2083 static int convert_fuse_file_lock(const struct fuse_file_lock
*ffl
,
2084 struct file_lock
*fl
)
2086 switch (ffl
->type
) {
2092 if (ffl
->start
> OFFSET_MAX
|| ffl
->end
> OFFSET_MAX
||
2093 ffl
->end
< ffl
->start
)
2096 fl
->fl_start
= ffl
->start
;
2097 fl
->fl_end
= ffl
->end
;
2098 fl
->fl_pid
= ffl
->pid
;
2104 fl
->fl_type
= ffl
->type
;
2108 static void fuse_lk_fill(struct fuse_args
*args
, struct file
*file
,
2109 const struct file_lock
*fl
, int opcode
, pid_t pid
,
2110 int flock
, struct fuse_lk_in
*inarg
)
2112 struct inode
*inode
= file_inode(file
);
2113 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2114 struct fuse_file
*ff
= file
->private_data
;
2116 memset(inarg
, 0, sizeof(*inarg
));
2118 inarg
->owner
= fuse_lock_owner_id(fc
, fl
->fl_owner
);
2119 inarg
->lk
.start
= fl
->fl_start
;
2120 inarg
->lk
.end
= fl
->fl_end
;
2121 inarg
->lk
.type
= fl
->fl_type
;
2122 inarg
->lk
.pid
= pid
;
2124 inarg
->lk_flags
|= FUSE_LK_FLOCK
;
2125 args
->in
.h
.opcode
= opcode
;
2126 args
->in
.h
.nodeid
= get_node_id(inode
);
2127 args
->in
.numargs
= 1;
2128 args
->in
.args
[0].size
= sizeof(*inarg
);
2129 args
->in
.args
[0].value
= inarg
;
2132 static int fuse_getlk(struct file
*file
, struct file_lock
*fl
)
2134 struct inode
*inode
= file_inode(file
);
2135 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2137 struct fuse_lk_in inarg
;
2138 struct fuse_lk_out outarg
;
2141 fuse_lk_fill(&args
, file
, fl
, FUSE_GETLK
, 0, 0, &inarg
);
2142 args
.out
.numargs
= 1;
2143 args
.out
.args
[0].size
= sizeof(outarg
);
2144 args
.out
.args
[0].value
= &outarg
;
2145 err
= fuse_simple_request(fc
, &args
);
2147 err
= convert_fuse_file_lock(&outarg
.lk
, fl
);
2152 static int fuse_setlk(struct file
*file
, struct file_lock
*fl
, int flock
)
2154 struct inode
*inode
= file_inode(file
);
2155 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2157 struct fuse_lk_in inarg
;
2158 int opcode
= (fl
->fl_flags
& FL_SLEEP
) ? FUSE_SETLKW
: FUSE_SETLK
;
2159 pid_t pid
= fl
->fl_type
!= F_UNLCK
? current
->tgid
: 0;
2162 if (fl
->fl_lmops
&& fl
->fl_lmops
->lm_grant
) {
2163 /* NLM needs asynchronous locks, which we don't support yet */
2167 /* Unlock on close is handled by the flush method */
2168 if (fl
->fl_flags
& FL_CLOSE
)
2171 fuse_lk_fill(&args
, file
, fl
, opcode
, pid
, flock
, &inarg
);
2172 err
= fuse_simple_request(fc
, &args
);
2174 /* locking is restartable */
2181 static int fuse_file_lock(struct file
*file
, int cmd
, struct file_lock
*fl
)
2183 struct inode
*inode
= file_inode(file
);
2184 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2187 if (cmd
== F_CANCELLK
) {
2189 } else if (cmd
== F_GETLK
) {
2191 posix_test_lock(file
, fl
);
2194 err
= fuse_getlk(file
, fl
);
2197 err
= posix_lock_file(file
, fl
, NULL
);
2199 err
= fuse_setlk(file
, fl
, 0);
2204 static int fuse_file_flock(struct file
*file
, int cmd
, struct file_lock
*fl
)
2206 struct inode
*inode
= file_inode(file
);
2207 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2211 err
= locks_lock_file_wait(file
, fl
);
2213 struct fuse_file
*ff
= file
->private_data
;
2215 /* emulate flock with POSIX locks */
2217 err
= fuse_setlk(file
, fl
, 1);
2223 static sector_t
fuse_bmap(struct address_space
*mapping
, sector_t block
)
2225 struct inode
*inode
= mapping
->host
;
2226 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2228 struct fuse_bmap_in inarg
;
2229 struct fuse_bmap_out outarg
;
2232 if (!inode
->i_sb
->s_bdev
|| fc
->no_bmap
)
2235 memset(&inarg
, 0, sizeof(inarg
));
2236 inarg
.block
= block
;
2237 inarg
.blocksize
= inode
->i_sb
->s_blocksize
;
2238 args
.in
.h
.opcode
= FUSE_BMAP
;
2239 args
.in
.h
.nodeid
= get_node_id(inode
);
2240 args
.in
.numargs
= 1;
2241 args
.in
.args
[0].size
= sizeof(inarg
);
2242 args
.in
.args
[0].value
= &inarg
;
2243 args
.out
.numargs
= 1;
2244 args
.out
.args
[0].size
= sizeof(outarg
);
2245 args
.out
.args
[0].value
= &outarg
;
2246 err
= fuse_simple_request(fc
, &args
);
2250 return err
? 0 : outarg
.block
;
2253 static loff_t
fuse_lseek(struct file
*file
, loff_t offset
, int whence
)
2255 struct inode
*inode
= file
->f_mapping
->host
;
2256 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2257 struct fuse_file
*ff
= file
->private_data
;
2259 struct fuse_lseek_in inarg
= {
2264 struct fuse_lseek_out outarg
;
2270 args
.in
.h
.opcode
= FUSE_LSEEK
;
2271 args
.in
.h
.nodeid
= ff
->nodeid
;
2272 args
.in
.numargs
= 1;
2273 args
.in
.args
[0].size
= sizeof(inarg
);
2274 args
.in
.args
[0].value
= &inarg
;
2275 args
.out
.numargs
= 1;
2276 args
.out
.args
[0].size
= sizeof(outarg
);
2277 args
.out
.args
[0].value
= &outarg
;
2278 err
= fuse_simple_request(fc
, &args
);
2280 if (err
== -ENOSYS
) {
2287 return vfs_setpos(file
, outarg
.offset
, inode
->i_sb
->s_maxbytes
);
2290 err
= fuse_update_attributes(inode
, NULL
, file
, NULL
);
2292 return generic_file_llseek(file
, offset
, whence
);
2297 static loff_t
fuse_file_llseek(struct file
*file
, loff_t offset
, int whence
)
2300 struct inode
*inode
= file_inode(file
);
2305 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2306 retval
= generic_file_llseek(file
, offset
, whence
);
2310 retval
= fuse_update_attributes(inode
, NULL
, file
, NULL
);
2312 retval
= generic_file_llseek(file
, offset
, whence
);
2313 inode_unlock(inode
);
2318 retval
= fuse_lseek(file
, offset
, whence
);
2319 inode_unlock(inode
);
2328 static int fuse_ioctl_copy_user(struct page
**pages
, struct iovec
*iov
,
2329 unsigned int nr_segs
, size_t bytes
, bool to_user
)
2337 iov_iter_init(&ii
, to_user
? READ
: WRITE
, iov
, nr_segs
, bytes
);
2339 while (iov_iter_count(&ii
)) {
2340 struct page
*page
= pages
[page_idx
++];
2341 size_t todo
= min_t(size_t, PAGE_SIZE
, iov_iter_count(&ii
));
2347 char __user
*uaddr
= ii
.iov
->iov_base
+ ii
.iov_offset
;
2348 size_t iov_len
= ii
.iov
->iov_len
- ii
.iov_offset
;
2349 size_t copy
= min(todo
, iov_len
);
2353 left
= copy_from_user(kaddr
, uaddr
, copy
);
2355 left
= copy_to_user(uaddr
, kaddr
, copy
);
2360 iov_iter_advance(&ii
, copy
);
2372 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2373 * ABI was defined to be 'struct iovec' which is different on 32bit
2374 * and 64bit. Fortunately we can determine which structure the server
2375 * used from the size of the reply.
2377 static int fuse_copy_ioctl_iovec_old(struct iovec
*dst
, void *src
,
2378 size_t transferred
, unsigned count
,
2381 #ifdef CONFIG_COMPAT
2382 if (count
* sizeof(struct compat_iovec
) == transferred
) {
2383 struct compat_iovec
*ciov
= src
;
2387 * With this interface a 32bit server cannot support
2388 * non-compat (i.e. ones coming from 64bit apps) ioctl
2394 for (i
= 0; i
< count
; i
++) {
2395 dst
[i
].iov_base
= compat_ptr(ciov
[i
].iov_base
);
2396 dst
[i
].iov_len
= ciov
[i
].iov_len
;
2402 if (count
* sizeof(struct iovec
) != transferred
)
2405 memcpy(dst
, src
, transferred
);
2409 /* Make sure iov_length() won't overflow */
2410 static int fuse_verify_ioctl_iov(struct iovec
*iov
, size_t count
)
2413 u32 max
= FUSE_MAX_PAGES_PER_REQ
<< PAGE_SHIFT
;
2415 for (n
= 0; n
< count
; n
++, iov
++) {
2416 if (iov
->iov_len
> (size_t) max
)
2418 max
-= iov
->iov_len
;
2423 static int fuse_copy_ioctl_iovec(struct fuse_conn
*fc
, struct iovec
*dst
,
2424 void *src
, size_t transferred
, unsigned count
,
2428 struct fuse_ioctl_iovec
*fiov
= src
;
2430 if (fc
->minor
< 16) {
2431 return fuse_copy_ioctl_iovec_old(dst
, src
, transferred
,
2435 if (count
* sizeof(struct fuse_ioctl_iovec
) != transferred
)
2438 for (i
= 0; i
< count
; i
++) {
2439 /* Did the server supply an inappropriate value? */
2440 if (fiov
[i
].base
!= (unsigned long) fiov
[i
].base
||
2441 fiov
[i
].len
!= (unsigned long) fiov
[i
].len
)
2444 dst
[i
].iov_base
= (void __user
*) (unsigned long) fiov
[i
].base
;
2445 dst
[i
].iov_len
= (size_t) fiov
[i
].len
;
2447 #ifdef CONFIG_COMPAT
2449 (ptr_to_compat(dst
[i
].iov_base
) != fiov
[i
].base
||
2450 (compat_size_t
) dst
[i
].iov_len
!= fiov
[i
].len
))
2460 * For ioctls, there is no generic way to determine how much memory
2461 * needs to be read and/or written. Furthermore, ioctls are allowed
2462 * to dereference the passed pointer, so the parameter requires deep
2463 * copying but FUSE has no idea whatsoever about what to copy in or
2466 * This is solved by allowing FUSE server to retry ioctl with
2467 * necessary in/out iovecs. Let's assume the ioctl implementation
2468 * needs to read in the following structure.
2475 * On the first callout to FUSE server, inarg->in_size and
2476 * inarg->out_size will be NULL; then, the server completes the ioctl
2477 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2478 * the actual iov array to
2480 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2482 * which tells FUSE to copy in the requested area and retry the ioctl.
2483 * On the second round, the server has access to the structure and
2484 * from that it can tell what to look for next, so on the invocation,
2485 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2487 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2488 * { .iov_base = a.buf, .iov_len = a.buflen } }
2490 * FUSE will copy both struct a and the pointed buffer from the
2491 * process doing the ioctl and retry ioctl with both struct a and the
2494 * This time, FUSE server has everything it needs and completes ioctl
2495 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2497 * Copying data out works the same way.
2499 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2500 * automatically initializes in and out iovs by decoding @cmd with
2501 * _IOC_* macros and the server is not allowed to request RETRY. This
2502 * limits ioctl data transfers to well-formed ioctls and is the forced
2503 * behavior for all FUSE servers.
2505 long fuse_do_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
,
2508 struct fuse_file
*ff
= file
->private_data
;
2509 struct fuse_conn
*fc
= ff
->fc
;
2510 struct fuse_ioctl_in inarg
= {
2516 struct fuse_ioctl_out outarg
;
2517 struct fuse_req
*req
= NULL
;
2518 struct page
**pages
= NULL
;
2519 struct iovec
*iov_page
= NULL
;
2520 struct iovec
*in_iov
= NULL
, *out_iov
= NULL
;
2521 unsigned int in_iovs
= 0, out_iovs
= 0, num_pages
= 0, max_pages
;
2522 size_t in_size
, out_size
, transferred
;
2525 #if BITS_PER_LONG == 32
2526 inarg
.flags
|= FUSE_IOCTL_32BIT
;
2528 if (flags
& FUSE_IOCTL_COMPAT
)
2529 inarg
.flags
|= FUSE_IOCTL_32BIT
;
2532 /* assume all the iovs returned by client always fits in a page */
2533 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec
) * FUSE_IOCTL_MAX_IOV
> PAGE_SIZE
);
2536 pages
= kcalloc(FUSE_MAX_PAGES_PER_REQ
, sizeof(pages
[0]), GFP_KERNEL
);
2537 iov_page
= (struct iovec
*) __get_free_page(GFP_KERNEL
);
2538 if (!pages
|| !iov_page
)
2542 * If restricted, initialize IO parameters as encoded in @cmd.
2543 * RETRY from server is not allowed.
2545 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
)) {
2546 struct iovec
*iov
= iov_page
;
2548 iov
->iov_base
= (void __user
*)arg
;
2549 iov
->iov_len
= _IOC_SIZE(cmd
);
2551 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
2556 if (_IOC_DIR(cmd
) & _IOC_READ
) {
2563 inarg
.in_size
= in_size
= iov_length(in_iov
, in_iovs
);
2564 inarg
.out_size
= out_size
= iov_length(out_iov
, out_iovs
);
2567 * Out data can be used either for actual out data or iovs,
2568 * make sure there always is at least one page.
2570 out_size
= max_t(size_t, out_size
, PAGE_SIZE
);
2571 max_pages
= DIV_ROUND_UP(max(in_size
, out_size
), PAGE_SIZE
);
2573 /* make sure there are enough buffer pages and init request with them */
2575 if (max_pages
> FUSE_MAX_PAGES_PER_REQ
)
2577 while (num_pages
< max_pages
) {
2578 pages
[num_pages
] = alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
2579 if (!pages
[num_pages
])
2584 req
= fuse_get_req(fc
, num_pages
);
2590 memcpy(req
->pages
, pages
, sizeof(req
->pages
[0]) * num_pages
);
2591 req
->num_pages
= num_pages
;
2592 fuse_page_descs_length_init(req
, 0, req
->num_pages
);
2594 /* okay, let's send it to the client */
2595 req
->in
.h
.opcode
= FUSE_IOCTL
;
2596 req
->in
.h
.nodeid
= ff
->nodeid
;
2597 req
->in
.numargs
= 1;
2598 req
->in
.args
[0].size
= sizeof(inarg
);
2599 req
->in
.args
[0].value
= &inarg
;
2602 req
->in
.args
[1].size
= in_size
;
2603 req
->in
.argpages
= 1;
2605 err
= fuse_ioctl_copy_user(pages
, in_iov
, in_iovs
, in_size
,
2611 req
->out
.numargs
= 2;
2612 req
->out
.args
[0].size
= sizeof(outarg
);
2613 req
->out
.args
[0].value
= &outarg
;
2614 req
->out
.args
[1].size
= out_size
;
2615 req
->out
.argpages
= 1;
2616 req
->out
.argvar
= 1;
2618 fuse_request_send(fc
, req
);
2619 err
= req
->out
.h
.error
;
2620 transferred
= req
->out
.args
[1].size
;
2621 fuse_put_request(fc
, req
);
2626 /* did it ask for retry? */
2627 if (outarg
.flags
& FUSE_IOCTL_RETRY
) {
2630 /* no retry if in restricted mode */
2632 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
))
2635 in_iovs
= outarg
.in_iovs
;
2636 out_iovs
= outarg
.out_iovs
;
2639 * Make sure things are in boundary, separate checks
2640 * are to protect against overflow.
2643 if (in_iovs
> FUSE_IOCTL_MAX_IOV
||
2644 out_iovs
> FUSE_IOCTL_MAX_IOV
||
2645 in_iovs
+ out_iovs
> FUSE_IOCTL_MAX_IOV
)
2648 vaddr
= kmap_atomic(pages
[0]);
2649 err
= fuse_copy_ioctl_iovec(fc
, iov_page
, vaddr
,
2650 transferred
, in_iovs
+ out_iovs
,
2651 (flags
& FUSE_IOCTL_COMPAT
) != 0);
2652 kunmap_atomic(vaddr
);
2657 out_iov
= in_iov
+ in_iovs
;
2659 err
= fuse_verify_ioctl_iov(in_iov
, in_iovs
);
2663 err
= fuse_verify_ioctl_iov(out_iov
, out_iovs
);
2671 if (transferred
> inarg
.out_size
)
2674 err
= fuse_ioctl_copy_user(pages
, out_iov
, out_iovs
, transferred
, true);
2677 fuse_put_request(fc
, req
);
2678 free_page((unsigned long) iov_page
);
2680 __free_page(pages
[--num_pages
]);
2683 return err
? err
: outarg
.result
;
2685 EXPORT_SYMBOL_GPL(fuse_do_ioctl
);
2687 long fuse_ioctl_common(struct file
*file
, unsigned int cmd
,
2688 unsigned long arg
, unsigned int flags
)
2690 struct inode
*inode
= file_inode(file
);
2691 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2693 if (!fuse_allow_current_process(fc
))
2696 if (is_bad_inode(inode
))
2699 return fuse_do_ioctl(file
, cmd
, arg
, flags
);
2702 static long fuse_file_ioctl(struct file
*file
, unsigned int cmd
,
2705 return fuse_ioctl_common(file
, cmd
, arg
, 0);
2708 static long fuse_file_compat_ioctl(struct file
*file
, unsigned int cmd
,
2711 return fuse_ioctl_common(file
, cmd
, arg
, FUSE_IOCTL_COMPAT
);
2715 * All files which have been polled are linked to RB tree
2716 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2717 * find the matching one.
2719 static struct rb_node
**fuse_find_polled_node(struct fuse_conn
*fc
, u64 kh
,
2720 struct rb_node
**parent_out
)
2722 struct rb_node
**link
= &fc
->polled_files
.rb_node
;
2723 struct rb_node
*last
= NULL
;
2726 struct fuse_file
*ff
;
2729 ff
= rb_entry(last
, struct fuse_file
, polled_node
);
2732 link
= &last
->rb_left
;
2733 else if (kh
> ff
->kh
)
2734 link
= &last
->rb_right
;
2745 * The file is about to be polled. Make sure it's on the polled_files
2746 * RB tree. Note that files once added to the polled_files tree are
2747 * not removed before the file is released. This is because a file
2748 * polled once is likely to be polled again.
2750 static void fuse_register_polled_file(struct fuse_conn
*fc
,
2751 struct fuse_file
*ff
)
2753 spin_lock(&fc
->lock
);
2754 if (RB_EMPTY_NODE(&ff
->polled_node
)) {
2755 struct rb_node
**link
, *uninitialized_var(parent
);
2757 link
= fuse_find_polled_node(fc
, ff
->kh
, &parent
);
2759 rb_link_node(&ff
->polled_node
, parent
, link
);
2760 rb_insert_color(&ff
->polled_node
, &fc
->polled_files
);
2762 spin_unlock(&fc
->lock
);
2765 unsigned fuse_file_poll(struct file
*file
, poll_table
*wait
)
2767 struct fuse_file
*ff
= file
->private_data
;
2768 struct fuse_conn
*fc
= ff
->fc
;
2769 struct fuse_poll_in inarg
= { .fh
= ff
->fh
, .kh
= ff
->kh
};
2770 struct fuse_poll_out outarg
;
2775 return DEFAULT_POLLMASK
;
2777 poll_wait(file
, &ff
->poll_wait
, wait
);
2778 inarg
.events
= (__u32
)poll_requested_events(wait
);
2781 * Ask for notification iff there's someone waiting for it.
2782 * The client may ignore the flag and always notify.
2784 if (waitqueue_active(&ff
->poll_wait
)) {
2785 inarg
.flags
|= FUSE_POLL_SCHEDULE_NOTIFY
;
2786 fuse_register_polled_file(fc
, ff
);
2789 args
.in
.h
.opcode
= FUSE_POLL
;
2790 args
.in
.h
.nodeid
= ff
->nodeid
;
2791 args
.in
.numargs
= 1;
2792 args
.in
.args
[0].size
= sizeof(inarg
);
2793 args
.in
.args
[0].value
= &inarg
;
2794 args
.out
.numargs
= 1;
2795 args
.out
.args
[0].size
= sizeof(outarg
);
2796 args
.out
.args
[0].value
= &outarg
;
2797 err
= fuse_simple_request(fc
, &args
);
2800 return outarg
.revents
;
2801 if (err
== -ENOSYS
) {
2803 return DEFAULT_POLLMASK
;
2807 EXPORT_SYMBOL_GPL(fuse_file_poll
);
2810 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2811 * wakes up the poll waiters.
2813 int fuse_notify_poll_wakeup(struct fuse_conn
*fc
,
2814 struct fuse_notify_poll_wakeup_out
*outarg
)
2816 u64 kh
= outarg
->kh
;
2817 struct rb_node
**link
;
2819 spin_lock(&fc
->lock
);
2821 link
= fuse_find_polled_node(fc
, kh
, NULL
);
2823 struct fuse_file
*ff
;
2825 ff
= rb_entry(*link
, struct fuse_file
, polled_node
);
2826 wake_up_interruptible_sync(&ff
->poll_wait
);
2829 spin_unlock(&fc
->lock
);
2833 static void fuse_do_truncate(struct file
*file
)
2835 struct inode
*inode
= file
->f_mapping
->host
;
2838 attr
.ia_valid
= ATTR_SIZE
;
2839 attr
.ia_size
= i_size_read(inode
);
2841 attr
.ia_file
= file
;
2842 attr
.ia_valid
|= ATTR_FILE
;
2844 fuse_do_setattr(inode
, &attr
, file
);
2847 static inline loff_t
fuse_round_up(loff_t off
)
2849 return round_up(off
, FUSE_MAX_PAGES_PER_REQ
<< PAGE_SHIFT
);
2853 fuse_direct_IO(struct kiocb
*iocb
, struct iov_iter
*iter
)
2855 DECLARE_COMPLETION_ONSTACK(wait
);
2857 struct file
*file
= iocb
->ki_filp
;
2858 struct fuse_file
*ff
= file
->private_data
;
2859 bool async_dio
= ff
->fc
->async_dio
;
2861 struct inode
*inode
;
2863 size_t count
= iov_iter_count(iter
);
2864 loff_t offset
= iocb
->ki_pos
;
2865 struct fuse_io_priv
*io
;
2868 inode
= file
->f_mapping
->host
;
2869 i_size
= i_size_read(inode
);
2871 if ((iov_iter_rw(iter
) == READ
) && (offset
> i_size
))
2874 /* optimization for short read */
2875 if (async_dio
&& iov_iter_rw(iter
) != WRITE
&& offset
+ count
> i_size
) {
2876 if (offset
>= i_size
)
2878 iov_iter_truncate(iter
, fuse_round_up(i_size
- offset
));
2879 count
= iov_iter_count(iter
);
2882 io
= kmalloc(sizeof(struct fuse_io_priv
), GFP_KERNEL
);
2885 spin_lock_init(&io
->lock
);
2886 kref_init(&io
->refcnt
);
2890 io
->offset
= offset
;
2891 io
->write
= (iov_iter_rw(iter
) == WRITE
);
2895 * By default, we want to optimize all I/Os with async request
2896 * submission to the client filesystem if supported.
2898 io
->async
= async_dio
;
2900 io
->blocking
= is_sync_kiocb(iocb
);
2903 * We cannot asynchronously extend the size of a file.
2904 * In such case the aio will behave exactly like sync io.
2906 if ((offset
+ count
> i_size
) && iov_iter_rw(iter
) == WRITE
)
2907 io
->blocking
= true;
2909 if (io
->async
&& io
->blocking
) {
2911 * Additional reference to keep io around after
2912 * calling fuse_aio_complete()
2914 kref_get(&io
->refcnt
);
2918 if (iov_iter_rw(iter
) == WRITE
) {
2919 ret
= fuse_direct_io(io
, iter
, &pos
, FUSE_DIO_WRITE
);
2920 fuse_invalidate_attr(inode
);
2922 ret
= __fuse_direct_read(io
, iter
, &pos
);
2926 fuse_aio_complete(io
, ret
< 0 ? ret
: 0, -1);
2928 /* we have a non-extending, async request, so return */
2930 return -EIOCBQUEUED
;
2932 wait_for_completion(&wait
);
2933 ret
= fuse_get_res_by_io(io
);
2936 kref_put(&io
->refcnt
, fuse_io_release
);
2938 if (iov_iter_rw(iter
) == WRITE
) {
2940 fuse_write_update_size(inode
, pos
);
2941 else if (ret
< 0 && offset
+ count
> i_size
)
2942 fuse_do_truncate(file
);
2948 static long fuse_file_fallocate(struct file
*file
, int mode
, loff_t offset
,
2951 struct fuse_file
*ff
= file
->private_data
;
2952 struct inode
*inode
= file_inode(file
);
2953 struct fuse_inode
*fi
= get_fuse_inode(inode
);
2954 struct fuse_conn
*fc
= ff
->fc
;
2956 struct fuse_fallocate_in inarg
= {
2963 bool lock_inode
= !(mode
& FALLOC_FL_KEEP_SIZE
) ||
2964 (mode
& FALLOC_FL_PUNCH_HOLE
);
2966 if (mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
))
2969 if (fc
->no_fallocate
)
2974 if (mode
& FALLOC_FL_PUNCH_HOLE
) {
2975 loff_t endbyte
= offset
+ length
- 1;
2976 err
= filemap_write_and_wait_range(inode
->i_mapping
,
2981 fuse_sync_writes(inode
);
2985 if (!(mode
& FALLOC_FL_KEEP_SIZE
))
2986 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
2988 args
.in
.h
.opcode
= FUSE_FALLOCATE
;
2989 args
.in
.h
.nodeid
= ff
->nodeid
;
2990 args
.in
.numargs
= 1;
2991 args
.in
.args
[0].size
= sizeof(inarg
);
2992 args
.in
.args
[0].value
= &inarg
;
2993 err
= fuse_simple_request(fc
, &args
);
2994 if (err
== -ENOSYS
) {
2995 fc
->no_fallocate
= 1;
3001 /* we could have extended the file */
3002 if (!(mode
& FALLOC_FL_KEEP_SIZE
)) {
3003 bool changed
= fuse_write_update_size(inode
, offset
+ length
);
3005 if (changed
&& fc
->writeback_cache
)
3006 file_update_time(file
);
3009 if (mode
& FALLOC_FL_PUNCH_HOLE
)
3010 truncate_pagecache_range(inode
, offset
, offset
+ length
- 1);
3012 fuse_invalidate_attr(inode
);
3015 if (!(mode
& FALLOC_FL_KEEP_SIZE
))
3016 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
3019 inode_unlock(inode
);
3024 static const struct file_operations fuse_file_operations
= {
3025 .llseek
= fuse_file_llseek
,
3026 .read_iter
= fuse_file_read_iter
,
3027 .write_iter
= fuse_file_write_iter
,
3028 .mmap
= fuse_file_mmap
,
3030 .flush
= fuse_flush
,
3031 .release
= fuse_release
,
3032 .fsync
= fuse_fsync
,
3033 .lock
= fuse_file_lock
,
3034 .flock
= fuse_file_flock
,
3035 .splice_read
= generic_file_splice_read
,
3036 .unlocked_ioctl
= fuse_file_ioctl
,
3037 .compat_ioctl
= fuse_file_compat_ioctl
,
3038 .poll
= fuse_file_poll
,
3039 .fallocate
= fuse_file_fallocate
,
3042 static const struct file_operations fuse_direct_io_file_operations
= {
3043 .llseek
= fuse_file_llseek
,
3044 .read_iter
= fuse_direct_read_iter
,
3045 .write_iter
= fuse_direct_write_iter
,
3046 .mmap
= fuse_direct_mmap
,
3048 .flush
= fuse_flush
,
3049 .release
= fuse_release
,
3050 .fsync
= fuse_fsync
,
3051 .lock
= fuse_file_lock
,
3052 .flock
= fuse_file_flock
,
3053 .unlocked_ioctl
= fuse_file_ioctl
,
3054 .compat_ioctl
= fuse_file_compat_ioctl
,
3055 .poll
= fuse_file_poll
,
3056 .fallocate
= fuse_file_fallocate
,
3057 /* no splice_read */
3060 static const struct address_space_operations fuse_file_aops
= {
3061 .readpage
= fuse_readpage
,
3062 .writepage
= fuse_writepage
,
3063 .writepages
= fuse_writepages
,
3064 .launder_page
= fuse_launder_page
,
3065 .readpages
= fuse_readpages
,
3066 .set_page_dirty
= __set_page_dirty_nobuffers
,
3068 .direct_IO
= fuse_direct_IO
,
3069 .write_begin
= fuse_write_begin
,
3070 .write_end
= fuse_write_end
,
3073 void fuse_init_file_inode(struct inode
*inode
)
3075 inode
->i_fop
= &fuse_file_operations
;
3076 inode
->i_data
.a_ops
= &fuse_file_aops
;