2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU GPL.
11 #include <linux/pagemap.h>
12 #include <linux/slab.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/module.h>
16 #include <linux/compat.h>
17 #include <linux/swap.h>
18 #include <linux/aio.h>
19 #include <linux/falloc.h>
21 static const struct file_operations fuse_direct_io_file_operations
;
23 static int fuse_send_open(struct fuse_conn
*fc
, u64 nodeid
, struct file
*file
,
24 int opcode
, struct fuse_open_out
*outargp
)
26 struct fuse_open_in inarg
;
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
100 iput(req
->misc
.release
.inode
);
101 fuse_put_request(ff
->fc
, req
);
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
;
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
);
210 mutex_lock(&inode
->i_mutex
);
212 err
= fuse_do_open(fc
, get_node_id(inode
), file
, isdir
);
215 fuse_finish_open(inode
, file
);
218 mutex_unlock(&inode
->i_mutex
);
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 ff
->reserved_req
->force
= 1;
303 ff
->reserved_req
->background
= 0;
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_CACHE_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);
416 mutex_lock(&inode
->i_mutex
);
417 fuse_sync_writes(inode
);
418 mutex_unlock(&inode
->i_mutex
);
420 req
= fuse_get_req_nofail_nopages(fc
, file
);
421 memset(&inarg
, 0, sizeof(inarg
));
423 inarg
.lock_owner
= fuse_lock_owner_id(fc
, id
);
424 req
->in
.h
.opcode
= FUSE_FLUSH
;
425 req
->in
.h
.nodeid
= get_node_id(inode
);
427 req
->in
.args
[0].size
= sizeof(inarg
);
428 req
->in
.args
[0].value
= &inarg
;
430 fuse_request_send(fc
, req
);
431 err
= req
->out
.h
.error
;
432 fuse_put_request(fc
, req
);
433 if (err
== -ENOSYS
) {
440 int fuse_fsync_common(struct file
*file
, loff_t start
, loff_t end
,
441 int datasync
, int isdir
)
443 struct inode
*inode
= file
->f_mapping
->host
;
444 struct fuse_conn
*fc
= get_fuse_conn(inode
);
445 struct fuse_file
*ff
= file
->private_data
;
447 struct fuse_fsync_in inarg
;
450 if (is_bad_inode(inode
))
453 mutex_lock(&inode
->i_mutex
);
456 * Start writeback against all dirty pages of the inode, then
457 * wait for all outstanding writes, before sending the FSYNC
460 err
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
464 fuse_sync_writes(inode
);
465 err
= sync_inode_metadata(inode
, 1);
469 if ((!isdir
&& fc
->no_fsync
) || (isdir
&& fc
->no_fsyncdir
))
472 memset(&inarg
, 0, sizeof(inarg
));
474 inarg
.fsync_flags
= datasync
? 1 : 0;
475 args
.in
.h
.opcode
= isdir
? FUSE_FSYNCDIR
: FUSE_FSYNC
;
476 args
.in
.h
.nodeid
= get_node_id(inode
);
478 args
.in
.args
[0].size
= sizeof(inarg
);
479 args
.in
.args
[0].value
= &inarg
;
480 err
= fuse_simple_request(fc
, &args
);
481 if (err
== -ENOSYS
) {
489 mutex_unlock(&inode
->i_mutex
);
493 static int fuse_fsync(struct file
*file
, loff_t start
, loff_t end
,
496 return fuse_fsync_common(file
, start
, end
, datasync
, 0);
499 void fuse_read_fill(struct fuse_req
*req
, struct file
*file
, loff_t pos
,
500 size_t count
, int opcode
)
502 struct fuse_read_in
*inarg
= &req
->misc
.read
.in
;
503 struct fuse_file
*ff
= file
->private_data
;
508 inarg
->flags
= file
->f_flags
;
509 req
->in
.h
.opcode
= opcode
;
510 req
->in
.h
.nodeid
= ff
->nodeid
;
512 req
->in
.args
[0].size
= sizeof(struct fuse_read_in
);
513 req
->in
.args
[0].value
= inarg
;
515 req
->out
.numargs
= 1;
516 req
->out
.args
[0].size
= count
;
519 static void fuse_release_user_pages(struct fuse_req
*req
, int write
)
523 for (i
= 0; i
< req
->num_pages
; i
++) {
524 struct page
*page
= req
->pages
[i
];
526 set_page_dirty_lock(page
);
532 * In case of short read, the caller sets 'pos' to the position of
533 * actual end of fuse request in IO request. Otherwise, if bytes_requested
534 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
537 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
538 * both submitted asynchronously. The first of them was ACKed by userspace as
539 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
540 * second request was ACKed as short, e.g. only 1K was read, resulting in
543 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
544 * will be equal to the length of the longest contiguous fragment of
545 * transferred data starting from the beginning of IO request.
547 static void fuse_aio_complete(struct fuse_io_priv
*io
, int err
, ssize_t pos
)
551 spin_lock(&io
->lock
);
553 io
->err
= io
->err
? : err
;
554 else if (pos
>= 0 && (io
->bytes
< 0 || pos
< io
->bytes
))
558 spin_unlock(&io
->lock
);
565 else if (io
->bytes
>= 0 && io
->write
)
568 res
= io
->bytes
< 0 ? io
->size
: io
->bytes
;
570 if (!is_sync_kiocb(io
->iocb
)) {
571 struct inode
*inode
= file_inode(io
->iocb
->ki_filp
);
572 struct fuse_conn
*fc
= get_fuse_conn(inode
);
573 struct fuse_inode
*fi
= get_fuse_inode(inode
);
575 spin_lock(&fc
->lock
);
576 fi
->attr_version
= ++fc
->attr_version
;
577 spin_unlock(&fc
->lock
);
581 aio_complete(io
->iocb
, res
, 0);
586 static void fuse_aio_complete_req(struct fuse_conn
*fc
, struct fuse_req
*req
)
588 struct fuse_io_priv
*io
= req
->io
;
591 fuse_release_user_pages(req
, !io
->write
);
594 if (req
->misc
.write
.in
.size
!= req
->misc
.write
.out
.size
)
595 pos
= req
->misc
.write
.in
.offset
- io
->offset
+
596 req
->misc
.write
.out
.size
;
598 if (req
->misc
.read
.in
.size
!= req
->out
.args
[0].size
)
599 pos
= req
->misc
.read
.in
.offset
- io
->offset
+
600 req
->out
.args
[0].size
;
603 fuse_aio_complete(io
, req
->out
.h
.error
, pos
);
606 static size_t fuse_async_req_send(struct fuse_conn
*fc
, struct fuse_req
*req
,
607 size_t num_bytes
, struct fuse_io_priv
*io
)
609 spin_lock(&io
->lock
);
610 io
->size
+= num_bytes
;
612 spin_unlock(&io
->lock
);
615 req
->end
= fuse_aio_complete_req
;
617 __fuse_get_request(req
);
618 fuse_request_send_background(fc
, req
);
623 static size_t fuse_send_read(struct fuse_req
*req
, struct fuse_io_priv
*io
,
624 loff_t pos
, size_t count
, fl_owner_t owner
)
626 struct file
*file
= io
->file
;
627 struct fuse_file
*ff
= file
->private_data
;
628 struct fuse_conn
*fc
= ff
->fc
;
630 fuse_read_fill(req
, file
, pos
, count
, FUSE_READ
);
632 struct fuse_read_in
*inarg
= &req
->misc
.read
.in
;
634 inarg
->read_flags
|= FUSE_READ_LOCKOWNER
;
635 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
639 return fuse_async_req_send(fc
, req
, count
, io
);
641 fuse_request_send(fc
, req
);
642 return req
->out
.args
[0].size
;
645 static void fuse_read_update_size(struct inode
*inode
, loff_t size
,
648 struct fuse_conn
*fc
= get_fuse_conn(inode
);
649 struct fuse_inode
*fi
= get_fuse_inode(inode
);
651 spin_lock(&fc
->lock
);
652 if (attr_ver
== fi
->attr_version
&& size
< inode
->i_size
&&
653 !test_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
)) {
654 fi
->attr_version
= ++fc
->attr_version
;
655 i_size_write(inode
, size
);
657 spin_unlock(&fc
->lock
);
660 static void fuse_short_read(struct fuse_req
*req
, struct inode
*inode
,
663 size_t num_read
= req
->out
.args
[0].size
;
664 struct fuse_conn
*fc
= get_fuse_conn(inode
);
666 if (fc
->writeback_cache
) {
668 * A hole in a file. Some data after the hole are in page cache,
669 * but have not reached the client fs yet. So, the hole is not
673 int start_idx
= num_read
>> PAGE_CACHE_SHIFT
;
674 size_t off
= num_read
& (PAGE_CACHE_SIZE
- 1);
676 for (i
= start_idx
; i
< req
->num_pages
; i
++) {
677 zero_user_segment(req
->pages
[i
], off
, PAGE_CACHE_SIZE
);
681 loff_t pos
= page_offset(req
->pages
[0]) + num_read
;
682 fuse_read_update_size(inode
, pos
, attr_ver
);
686 static int fuse_do_readpage(struct file
*file
, struct page
*page
)
688 struct fuse_io_priv io
= { .async
= 0, .file
= file
};
689 struct inode
*inode
= page
->mapping
->host
;
690 struct fuse_conn
*fc
= get_fuse_conn(inode
);
691 struct fuse_req
*req
;
693 loff_t pos
= page_offset(page
);
694 size_t count
= PAGE_CACHE_SIZE
;
699 * Page writeback can extend beyond the lifetime of the
700 * page-cache page, so make sure we read a properly synced
703 fuse_wait_on_page_writeback(inode
, page
->index
);
705 req
= fuse_get_req(fc
, 1);
709 attr_ver
= fuse_get_attr_version(fc
);
711 req
->out
.page_zeroing
= 1;
712 req
->out
.argpages
= 1;
714 req
->pages
[0] = page
;
715 req
->page_descs
[0].length
= count
;
716 num_read
= fuse_send_read(req
, &io
, pos
, count
, NULL
);
717 err
= req
->out
.h
.error
;
721 * Short read means EOF. If file size is larger, truncate it
723 if (num_read
< count
)
724 fuse_short_read(req
, inode
, attr_ver
);
726 SetPageUptodate(page
);
729 fuse_put_request(fc
, req
);
734 static int fuse_readpage(struct file
*file
, struct page
*page
)
736 struct inode
*inode
= page
->mapping
->host
;
740 if (is_bad_inode(inode
))
743 err
= fuse_do_readpage(file
, page
);
744 fuse_invalidate_atime(inode
);
750 static void fuse_readpages_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
753 size_t count
= req
->misc
.read
.in
.size
;
754 size_t num_read
= req
->out
.args
[0].size
;
755 struct address_space
*mapping
= NULL
;
757 for (i
= 0; mapping
== NULL
&& i
< req
->num_pages
; i
++)
758 mapping
= req
->pages
[i
]->mapping
;
761 struct inode
*inode
= mapping
->host
;
764 * Short read means EOF. If file size is larger, truncate it
766 if (!req
->out
.h
.error
&& num_read
< count
)
767 fuse_short_read(req
, inode
, req
->misc
.read
.attr_ver
);
769 fuse_invalidate_atime(inode
);
772 for (i
= 0; i
< req
->num_pages
; i
++) {
773 struct page
*page
= req
->pages
[i
];
774 if (!req
->out
.h
.error
)
775 SetPageUptodate(page
);
779 page_cache_release(page
);
782 fuse_file_put(req
->ff
, false);
785 static void fuse_send_readpages(struct fuse_req
*req
, struct file
*file
)
787 struct fuse_file
*ff
= file
->private_data
;
788 struct fuse_conn
*fc
= ff
->fc
;
789 loff_t pos
= page_offset(req
->pages
[0]);
790 size_t count
= req
->num_pages
<< PAGE_CACHE_SHIFT
;
792 req
->out
.argpages
= 1;
793 req
->out
.page_zeroing
= 1;
794 req
->out
.page_replace
= 1;
795 fuse_read_fill(req
, file
, pos
, count
, FUSE_READ
);
796 req
->misc
.read
.attr_ver
= fuse_get_attr_version(fc
);
797 if (fc
->async_read
) {
798 req
->ff
= fuse_file_get(ff
);
799 req
->end
= fuse_readpages_end
;
800 fuse_request_send_background(fc
, req
);
802 fuse_request_send(fc
, req
);
803 fuse_readpages_end(fc
, req
);
804 fuse_put_request(fc
, req
);
808 struct fuse_fill_data
{
809 struct fuse_req
*req
;
815 static int fuse_readpages_fill(void *_data
, struct page
*page
)
817 struct fuse_fill_data
*data
= _data
;
818 struct fuse_req
*req
= data
->req
;
819 struct inode
*inode
= data
->inode
;
820 struct fuse_conn
*fc
= get_fuse_conn(inode
);
822 fuse_wait_on_page_writeback(inode
, page
->index
);
824 if (req
->num_pages
&&
825 (req
->num_pages
== FUSE_MAX_PAGES_PER_REQ
||
826 (req
->num_pages
+ 1) * PAGE_CACHE_SIZE
> fc
->max_read
||
827 req
->pages
[req
->num_pages
- 1]->index
+ 1 != page
->index
)) {
828 int nr_alloc
= min_t(unsigned, data
->nr_pages
,
829 FUSE_MAX_PAGES_PER_REQ
);
830 fuse_send_readpages(req
, data
->file
);
832 req
= fuse_get_req_for_background(fc
, nr_alloc
);
834 req
= fuse_get_req(fc
, nr_alloc
);
843 if (WARN_ON(req
->num_pages
>= req
->max_pages
)) {
844 fuse_put_request(fc
, req
);
848 page_cache_get(page
);
849 req
->pages
[req
->num_pages
] = page
;
850 req
->page_descs
[req
->num_pages
].length
= PAGE_SIZE
;
856 static int fuse_readpages(struct file
*file
, struct address_space
*mapping
,
857 struct list_head
*pages
, unsigned nr_pages
)
859 struct inode
*inode
= mapping
->host
;
860 struct fuse_conn
*fc
= get_fuse_conn(inode
);
861 struct fuse_fill_data data
;
863 int nr_alloc
= min_t(unsigned, nr_pages
, FUSE_MAX_PAGES_PER_REQ
);
866 if (is_bad_inode(inode
))
872 data
.req
= fuse_get_req_for_background(fc
, nr_alloc
);
874 data
.req
= fuse_get_req(fc
, nr_alloc
);
875 data
.nr_pages
= nr_pages
;
876 err
= PTR_ERR(data
.req
);
877 if (IS_ERR(data
.req
))
880 err
= read_cache_pages(mapping
, pages
, fuse_readpages_fill
, &data
);
882 if (data
.req
->num_pages
)
883 fuse_send_readpages(data
.req
, file
);
885 fuse_put_request(fc
, data
.req
);
891 static ssize_t
fuse_file_read_iter(struct kiocb
*iocb
, struct iov_iter
*to
)
893 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
894 struct fuse_conn
*fc
= get_fuse_conn(inode
);
897 * In auto invalidate mode, always update attributes on read.
898 * Otherwise, only update if we attempt to read past EOF (to ensure
899 * i_size is up to date).
901 if (fc
->auto_inval_data
||
902 (iocb
->ki_pos
+ iov_iter_count(to
) > i_size_read(inode
))) {
904 err
= fuse_update_attributes(inode
, NULL
, iocb
->ki_filp
, NULL
);
909 return generic_file_read_iter(iocb
, to
);
912 static void fuse_write_fill(struct fuse_req
*req
, struct fuse_file
*ff
,
913 loff_t pos
, size_t count
)
915 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
916 struct fuse_write_out
*outarg
= &req
->misc
.write
.out
;
921 req
->in
.h
.opcode
= FUSE_WRITE
;
922 req
->in
.h
.nodeid
= ff
->nodeid
;
924 if (ff
->fc
->minor
< 9)
925 req
->in
.args
[0].size
= FUSE_COMPAT_WRITE_IN_SIZE
;
927 req
->in
.args
[0].size
= sizeof(struct fuse_write_in
);
928 req
->in
.args
[0].value
= inarg
;
929 req
->in
.args
[1].size
= count
;
930 req
->out
.numargs
= 1;
931 req
->out
.args
[0].size
= sizeof(struct fuse_write_out
);
932 req
->out
.args
[0].value
= outarg
;
935 static size_t fuse_send_write(struct fuse_req
*req
, struct fuse_io_priv
*io
,
936 loff_t pos
, size_t count
, fl_owner_t owner
)
938 struct file
*file
= io
->file
;
939 struct fuse_file
*ff
= file
->private_data
;
940 struct fuse_conn
*fc
= ff
->fc
;
941 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
943 fuse_write_fill(req
, ff
, pos
, count
);
944 inarg
->flags
= file
->f_flags
;
946 inarg
->write_flags
|= FUSE_WRITE_LOCKOWNER
;
947 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
951 return fuse_async_req_send(fc
, req
, count
, io
);
953 fuse_request_send(fc
, req
);
954 return req
->misc
.write
.out
.size
;
957 bool fuse_write_update_size(struct inode
*inode
, loff_t pos
)
959 struct fuse_conn
*fc
= get_fuse_conn(inode
);
960 struct fuse_inode
*fi
= get_fuse_inode(inode
);
963 spin_lock(&fc
->lock
);
964 fi
->attr_version
= ++fc
->attr_version
;
965 if (pos
> inode
->i_size
) {
966 i_size_write(inode
, pos
);
969 spin_unlock(&fc
->lock
);
974 static size_t fuse_send_write_pages(struct fuse_req
*req
, struct file
*file
,
975 struct inode
*inode
, loff_t pos
,
981 struct fuse_io_priv io
= { .async
= 0, .file
= file
};
983 for (i
= 0; i
< req
->num_pages
; i
++)
984 fuse_wait_on_page_writeback(inode
, req
->pages
[i
]->index
);
986 res
= fuse_send_write(req
, &io
, pos
, count
, NULL
);
988 offset
= req
->page_descs
[0].offset
;
990 for (i
= 0; i
< req
->num_pages
; i
++) {
991 struct page
*page
= req
->pages
[i
];
993 if (!req
->out
.h
.error
&& !offset
&& count
>= PAGE_CACHE_SIZE
)
994 SetPageUptodate(page
);
996 if (count
> PAGE_CACHE_SIZE
- offset
)
997 count
-= PAGE_CACHE_SIZE
- offset
;
1003 page_cache_release(page
);
1009 static ssize_t
fuse_fill_write_pages(struct fuse_req
*req
,
1010 struct address_space
*mapping
,
1011 struct iov_iter
*ii
, loff_t pos
)
1013 struct fuse_conn
*fc
= get_fuse_conn(mapping
->host
);
1014 unsigned offset
= pos
& (PAGE_CACHE_SIZE
- 1);
1018 req
->in
.argpages
= 1;
1019 req
->page_descs
[0].offset
= offset
;
1024 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
1025 size_t bytes
= min_t(size_t, PAGE_CACHE_SIZE
- offset
,
1026 iov_iter_count(ii
));
1028 bytes
= min_t(size_t, bytes
, fc
->max_write
- count
);
1032 if (iov_iter_fault_in_readable(ii
, bytes
))
1036 page
= grab_cache_page_write_begin(mapping
, index
, 0);
1040 if (mapping_writably_mapped(mapping
))
1041 flush_dcache_page(page
);
1043 tmp
= iov_iter_copy_from_user_atomic(page
, ii
, offset
, bytes
);
1044 flush_dcache_page(page
);
1048 page_cache_release(page
);
1049 bytes
= min(bytes
, iov_iter_single_seg_count(ii
));
1054 req
->pages
[req
->num_pages
] = page
;
1055 req
->page_descs
[req
->num_pages
].length
= tmp
;
1058 iov_iter_advance(ii
, tmp
);
1062 if (offset
== PAGE_CACHE_SIZE
)
1065 if (!fc
->big_writes
)
1067 } while (iov_iter_count(ii
) && count
< fc
->max_write
&&
1068 req
->num_pages
< req
->max_pages
&& offset
== 0);
1070 return count
> 0 ? count
: err
;
1073 static inline unsigned fuse_wr_pages(loff_t pos
, size_t len
)
1075 return min_t(unsigned,
1076 ((pos
+ len
- 1) >> PAGE_CACHE_SHIFT
) -
1077 (pos
>> PAGE_CACHE_SHIFT
) + 1,
1078 FUSE_MAX_PAGES_PER_REQ
);
1081 static ssize_t
fuse_perform_write(struct file
*file
,
1082 struct address_space
*mapping
,
1083 struct iov_iter
*ii
, loff_t pos
)
1085 struct inode
*inode
= mapping
->host
;
1086 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1087 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1091 if (is_bad_inode(inode
))
1094 if (inode
->i_size
< pos
+ iov_iter_count(ii
))
1095 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1098 struct fuse_req
*req
;
1100 unsigned nr_pages
= fuse_wr_pages(pos
, iov_iter_count(ii
));
1102 req
= fuse_get_req(fc
, nr_pages
);
1108 count
= fuse_fill_write_pages(req
, mapping
, ii
, pos
);
1114 num_written
= fuse_send_write_pages(req
, file
, inode
,
1116 err
= req
->out
.h
.error
;
1121 /* break out of the loop on short write */
1122 if (num_written
!= count
)
1126 fuse_put_request(fc
, req
);
1127 } while (!err
&& iov_iter_count(ii
));
1130 fuse_write_update_size(inode
, pos
);
1132 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1133 fuse_invalidate_attr(inode
);
1135 return res
> 0 ? res
: err
;
1138 static ssize_t
fuse_file_write_iter(struct kiocb
*iocb
, struct iov_iter
*from
)
1140 struct file
*file
= iocb
->ki_filp
;
1141 struct address_space
*mapping
= file
->f_mapping
;
1142 size_t count
= iov_iter_count(from
);
1143 ssize_t written
= 0;
1144 ssize_t written_buffered
= 0;
1145 struct inode
*inode
= mapping
->host
;
1148 loff_t pos
= iocb
->ki_pos
;
1150 if (get_fuse_conn(inode
)->writeback_cache
) {
1151 /* Update size (EOF optimization) and mode (SUID clearing) */
1152 err
= fuse_update_attributes(mapping
->host
, NULL
, file
, NULL
);
1156 return generic_file_write_iter(iocb
, from
);
1159 mutex_lock(&inode
->i_mutex
);
1161 /* We can write back this queue in page reclaim */
1162 current
->backing_dev_info
= mapping
->backing_dev_info
;
1164 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
1171 iov_iter_truncate(from
, count
);
1172 err
= file_remove_suid(file
);
1176 err
= file_update_time(file
);
1180 if (file
->f_flags
& O_DIRECT
) {
1181 written
= generic_file_direct_write(iocb
, from
, pos
);
1182 if (written
< 0 || !iov_iter_count(from
))
1187 written_buffered
= fuse_perform_write(file
, mapping
, from
, pos
);
1188 if (written_buffered
< 0) {
1189 err
= written_buffered
;
1192 endbyte
= pos
+ written_buffered
- 1;
1194 err
= filemap_write_and_wait_range(file
->f_mapping
, pos
,
1199 invalidate_mapping_pages(file
->f_mapping
,
1200 pos
>> PAGE_CACHE_SHIFT
,
1201 endbyte
>> PAGE_CACHE_SHIFT
);
1203 written
+= written_buffered
;
1204 iocb
->ki_pos
= pos
+ written_buffered
;
1206 written
= fuse_perform_write(file
, mapping
, from
, pos
);
1208 iocb
->ki_pos
= pos
+ written
;
1211 current
->backing_dev_info
= NULL
;
1212 mutex_unlock(&inode
->i_mutex
);
1214 return written
? written
: err
;
1217 static inline void fuse_page_descs_length_init(struct fuse_req
*req
,
1218 unsigned index
, unsigned nr_pages
)
1222 for (i
= index
; i
< index
+ nr_pages
; i
++)
1223 req
->page_descs
[i
].length
= PAGE_SIZE
-
1224 req
->page_descs
[i
].offset
;
1227 static inline unsigned long fuse_get_user_addr(const struct iov_iter
*ii
)
1229 return (unsigned long)ii
->iov
->iov_base
+ ii
->iov_offset
;
1232 static inline size_t fuse_get_frag_size(const struct iov_iter
*ii
,
1235 return min(iov_iter_single_seg_count(ii
), max_size
);
1238 static int fuse_get_user_pages(struct fuse_req
*req
, struct iov_iter
*ii
,
1239 size_t *nbytesp
, int write
)
1241 size_t nbytes
= 0; /* # bytes already packed in req */
1243 /* Special case for kernel I/O: can copy directly into the buffer */
1244 if (ii
->type
& ITER_KVEC
) {
1245 unsigned long user_addr
= fuse_get_user_addr(ii
);
1246 size_t frag_size
= fuse_get_frag_size(ii
, *nbytesp
);
1249 req
->in
.args
[1].value
= (void *) user_addr
;
1251 req
->out
.args
[0].value
= (void *) user_addr
;
1253 iov_iter_advance(ii
, frag_size
);
1254 *nbytesp
= frag_size
;
1258 while (nbytes
< *nbytesp
&& req
->num_pages
< req
->max_pages
) {
1261 ssize_t ret
= iov_iter_get_pages(ii
,
1262 &req
->pages
[req
->num_pages
],
1264 req
->max_pages
- req
->num_pages
,
1269 iov_iter_advance(ii
, ret
);
1273 npages
= (ret
+ PAGE_SIZE
- 1) / PAGE_SIZE
;
1275 req
->page_descs
[req
->num_pages
].offset
= start
;
1276 fuse_page_descs_length_init(req
, req
->num_pages
, npages
);
1278 req
->num_pages
+= npages
;
1279 req
->page_descs
[req
->num_pages
- 1].length
-=
1280 (PAGE_SIZE
- ret
) & (PAGE_SIZE
- 1);
1284 req
->in
.argpages
= 1;
1286 req
->out
.argpages
= 1;
1293 static inline int fuse_iter_npages(const struct iov_iter
*ii_p
)
1295 return iov_iter_npages(ii_p
, FUSE_MAX_PAGES_PER_REQ
);
1298 ssize_t
fuse_direct_io(struct fuse_io_priv
*io
, struct iov_iter
*iter
,
1299 loff_t
*ppos
, int flags
)
1301 int write
= flags
& FUSE_DIO_WRITE
;
1302 int cuse
= flags
& FUSE_DIO_CUSE
;
1303 struct file
*file
= io
->file
;
1304 struct inode
*inode
= file
->f_mapping
->host
;
1305 struct fuse_file
*ff
= file
->private_data
;
1306 struct fuse_conn
*fc
= ff
->fc
;
1307 size_t nmax
= write
? fc
->max_write
: fc
->max_read
;
1309 size_t count
= iov_iter_count(iter
);
1310 pgoff_t idx_from
= pos
>> PAGE_CACHE_SHIFT
;
1311 pgoff_t idx_to
= (pos
+ count
- 1) >> PAGE_CACHE_SHIFT
;
1313 struct fuse_req
*req
;
1316 req
= fuse_get_req_for_background(fc
, fuse_iter_npages(iter
));
1318 req
= fuse_get_req(fc
, fuse_iter_npages(iter
));
1320 return PTR_ERR(req
);
1322 if (!cuse
&& fuse_range_is_writeback(inode
, idx_from
, idx_to
)) {
1324 mutex_lock(&inode
->i_mutex
);
1325 fuse_sync_writes(inode
);
1327 mutex_unlock(&inode
->i_mutex
);
1332 fl_owner_t owner
= current
->files
;
1333 size_t nbytes
= min(count
, nmax
);
1334 int err
= fuse_get_user_pages(req
, iter
, &nbytes
, write
);
1341 nres
= fuse_send_write(req
, io
, pos
, nbytes
, owner
);
1343 nres
= fuse_send_read(req
, io
, pos
, nbytes
, owner
);
1346 fuse_release_user_pages(req
, !write
);
1347 if (req
->out
.h
.error
) {
1349 res
= req
->out
.h
.error
;
1351 } else if (nres
> nbytes
) {
1361 fuse_put_request(fc
, req
);
1363 req
= fuse_get_req_for_background(fc
,
1364 fuse_iter_npages(iter
));
1366 req
= fuse_get_req(fc
, fuse_iter_npages(iter
));
1372 fuse_put_request(fc
, req
);
1378 EXPORT_SYMBOL_GPL(fuse_direct_io
);
1380 static ssize_t
__fuse_direct_read(struct fuse_io_priv
*io
,
1381 struct iov_iter
*iter
,
1385 struct file
*file
= io
->file
;
1386 struct inode
*inode
= file_inode(file
);
1388 if (is_bad_inode(inode
))
1391 res
= fuse_direct_io(io
, iter
, ppos
, 0);
1393 fuse_invalidate_attr(inode
);
1398 static ssize_t
fuse_direct_read(struct file
*file
, char __user
*buf
,
1399 size_t count
, loff_t
*ppos
)
1401 struct fuse_io_priv io
= { .async
= 0, .file
= file
};
1402 struct iovec iov
= { .iov_base
= buf
, .iov_len
= count
};
1404 iov_iter_init(&ii
, READ
, &iov
, 1, count
);
1405 return __fuse_direct_read(&io
, &ii
, ppos
);
1408 static ssize_t
__fuse_direct_write(struct fuse_io_priv
*io
,
1409 struct iov_iter
*iter
,
1412 struct file
*file
= io
->file
;
1413 struct inode
*inode
= file_inode(file
);
1414 size_t count
= iov_iter_count(iter
);
1418 res
= generic_write_checks(file
, ppos
, &count
, 0);
1420 iov_iter_truncate(iter
, count
);
1421 res
= fuse_direct_io(io
, iter
, ppos
, FUSE_DIO_WRITE
);
1424 fuse_invalidate_attr(inode
);
1429 static ssize_t
fuse_direct_write(struct file
*file
, const char __user
*buf
,
1430 size_t count
, loff_t
*ppos
)
1432 struct iovec iov
= { .iov_base
= (void __user
*)buf
, .iov_len
= count
};
1433 struct inode
*inode
= file_inode(file
);
1435 struct fuse_io_priv io
= { .async
= 0, .file
= file
};
1437 iov_iter_init(&ii
, WRITE
, &iov
, 1, count
);
1439 if (is_bad_inode(inode
))
1442 /* Don't allow parallel writes to the same file */
1443 mutex_lock(&inode
->i_mutex
);
1444 res
= __fuse_direct_write(&io
, &ii
, ppos
);
1446 fuse_write_update_size(inode
, *ppos
);
1447 mutex_unlock(&inode
->i_mutex
);
1452 static void fuse_writepage_free(struct fuse_conn
*fc
, struct fuse_req
*req
)
1456 for (i
= 0; i
< req
->num_pages
; i
++)
1457 __free_page(req
->pages
[i
]);
1460 fuse_file_put(req
->ff
, false);
1463 static void fuse_writepage_finish(struct fuse_conn
*fc
, struct fuse_req
*req
)
1465 struct inode
*inode
= req
->inode
;
1466 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1467 struct backing_dev_info
*bdi
= inode
->i_mapping
->backing_dev_info
;
1470 list_del(&req
->writepages_entry
);
1471 for (i
= 0; i
< req
->num_pages
; i
++) {
1472 dec_bdi_stat(bdi
, BDI_WRITEBACK
);
1473 dec_zone_page_state(req
->pages
[i
], NR_WRITEBACK_TEMP
);
1474 bdi_writeout_inc(bdi
);
1476 wake_up(&fi
->page_waitq
);
1479 /* Called under fc->lock, may release and reacquire it */
1480 static void fuse_send_writepage(struct fuse_conn
*fc
, struct fuse_req
*req
,
1482 __releases(fc
->lock
)
1483 __acquires(fc
->lock
)
1485 struct fuse_inode
*fi
= get_fuse_inode(req
->inode
);
1486 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
1487 __u64 data_size
= req
->num_pages
* PAGE_CACHE_SIZE
;
1492 if (inarg
->offset
+ data_size
<= size
) {
1493 inarg
->size
= data_size
;
1494 } else if (inarg
->offset
< size
) {
1495 inarg
->size
= size
- inarg
->offset
;
1497 /* Got truncated off completely */
1501 req
->in
.args
[1].size
= inarg
->size
;
1503 fuse_request_send_background_locked(fc
, req
);
1507 fuse_writepage_finish(fc
, req
);
1508 spin_unlock(&fc
->lock
);
1509 fuse_writepage_free(fc
, req
);
1510 fuse_put_request(fc
, req
);
1511 spin_lock(&fc
->lock
);
1515 * If fi->writectr is positive (no truncate or fsync going on) send
1516 * all queued writepage requests.
1518 * Called with fc->lock
1520 void fuse_flush_writepages(struct inode
*inode
)
1521 __releases(fc
->lock
)
1522 __acquires(fc
->lock
)
1524 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1525 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1526 size_t crop
= i_size_read(inode
);
1527 struct fuse_req
*req
;
1529 while (fi
->writectr
>= 0 && !list_empty(&fi
->queued_writes
)) {
1530 req
= list_entry(fi
->queued_writes
.next
, struct fuse_req
, list
);
1531 list_del_init(&req
->list
);
1532 fuse_send_writepage(fc
, req
, crop
);
1536 static void fuse_writepage_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
1538 struct inode
*inode
= req
->inode
;
1539 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1541 mapping_set_error(inode
->i_mapping
, req
->out
.h
.error
);
1542 spin_lock(&fc
->lock
);
1543 while (req
->misc
.write
.next
) {
1544 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1545 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
1546 struct fuse_req
*next
= req
->misc
.write
.next
;
1547 req
->misc
.write
.next
= next
->misc
.write
.next
;
1548 next
->misc
.write
.next
= NULL
;
1549 next
->ff
= fuse_file_get(req
->ff
);
1550 list_add(&next
->writepages_entry
, &fi
->writepages
);
1553 * Skip fuse_flush_writepages() to make it easy to crop requests
1554 * based on primary request size.
1556 * 1st case (trivial): there are no concurrent activities using
1557 * fuse_set/release_nowrite. Then we're on safe side because
1558 * fuse_flush_writepages() would call fuse_send_writepage()
1561 * 2nd case: someone called fuse_set_nowrite and it is waiting
1562 * now for completion of all in-flight requests. This happens
1563 * rarely and no more than once per page, so this should be
1566 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1567 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1568 * that fuse_set_nowrite returned implies that all in-flight
1569 * requests were completed along with all of their secondary
1570 * requests. Further primary requests are blocked by negative
1571 * writectr. Hence there cannot be any in-flight requests and
1572 * no invocations of fuse_writepage_end() while we're in
1573 * fuse_set_nowrite..fuse_release_nowrite section.
1575 fuse_send_writepage(fc
, next
, inarg
->offset
+ inarg
->size
);
1578 fuse_writepage_finish(fc
, req
);
1579 spin_unlock(&fc
->lock
);
1580 fuse_writepage_free(fc
, req
);
1583 static struct fuse_file
*__fuse_write_file_get(struct fuse_conn
*fc
,
1584 struct fuse_inode
*fi
)
1586 struct fuse_file
*ff
= NULL
;
1588 spin_lock(&fc
->lock
);
1589 if (!list_empty(&fi
->write_files
)) {
1590 ff
= list_entry(fi
->write_files
.next
, struct fuse_file
,
1594 spin_unlock(&fc
->lock
);
1599 static struct fuse_file
*fuse_write_file_get(struct fuse_conn
*fc
,
1600 struct fuse_inode
*fi
)
1602 struct fuse_file
*ff
= __fuse_write_file_get(fc
, fi
);
1607 int fuse_write_inode(struct inode
*inode
, struct writeback_control
*wbc
)
1609 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1610 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1611 struct fuse_file
*ff
;
1614 ff
= __fuse_write_file_get(fc
, fi
);
1615 err
= fuse_flush_times(inode
, ff
);
1617 fuse_file_put(ff
, 0);
1622 static int fuse_writepage_locked(struct page
*page
)
1624 struct address_space
*mapping
= page
->mapping
;
1625 struct inode
*inode
= mapping
->host
;
1626 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1627 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1628 struct fuse_req
*req
;
1629 struct page
*tmp_page
;
1630 int error
= -ENOMEM
;
1632 set_page_writeback(page
);
1634 req
= fuse_request_alloc_nofs(1);
1638 req
->background
= 1; /* writeback always goes to bg_queue */
1639 tmp_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
1644 req
->ff
= fuse_write_file_get(fc
, fi
);
1648 fuse_write_fill(req
, req
->ff
, page_offset(page
), 0);
1650 copy_highpage(tmp_page
, page
);
1651 req
->misc
.write
.in
.write_flags
|= FUSE_WRITE_CACHE
;
1652 req
->misc
.write
.next
= NULL
;
1653 req
->in
.argpages
= 1;
1655 req
->pages
[0] = tmp_page
;
1656 req
->page_descs
[0].offset
= 0;
1657 req
->page_descs
[0].length
= PAGE_SIZE
;
1658 req
->end
= fuse_writepage_end
;
1661 inc_bdi_stat(mapping
->backing_dev_info
, BDI_WRITEBACK
);
1662 inc_zone_page_state(tmp_page
, NR_WRITEBACK_TEMP
);
1664 spin_lock(&fc
->lock
);
1665 list_add(&req
->writepages_entry
, &fi
->writepages
);
1666 list_add_tail(&req
->list
, &fi
->queued_writes
);
1667 fuse_flush_writepages(inode
);
1668 spin_unlock(&fc
->lock
);
1670 end_page_writeback(page
);
1675 __free_page(tmp_page
);
1677 fuse_request_free(req
);
1679 end_page_writeback(page
);
1683 static int fuse_writepage(struct page
*page
, struct writeback_control
*wbc
)
1687 if (fuse_page_is_writeback(page
->mapping
->host
, page
->index
)) {
1689 * ->writepages() should be called for sync() and friends. We
1690 * should only get here on direct reclaim and then we are
1691 * allowed to skip a page which is already in flight
1693 WARN_ON(wbc
->sync_mode
== WB_SYNC_ALL
);
1695 redirty_page_for_writepage(wbc
, page
);
1699 err
= fuse_writepage_locked(page
);
1705 struct fuse_fill_wb_data
{
1706 struct fuse_req
*req
;
1707 struct fuse_file
*ff
;
1708 struct inode
*inode
;
1709 struct page
**orig_pages
;
1712 static void fuse_writepages_send(struct fuse_fill_wb_data
*data
)
1714 struct fuse_req
*req
= data
->req
;
1715 struct inode
*inode
= data
->inode
;
1716 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1717 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1718 int num_pages
= req
->num_pages
;
1721 req
->ff
= fuse_file_get(data
->ff
);
1722 spin_lock(&fc
->lock
);
1723 list_add_tail(&req
->list
, &fi
->queued_writes
);
1724 fuse_flush_writepages(inode
);
1725 spin_unlock(&fc
->lock
);
1727 for (i
= 0; i
< num_pages
; i
++)
1728 end_page_writeback(data
->orig_pages
[i
]);
1731 static bool fuse_writepage_in_flight(struct fuse_req
*new_req
,
1734 struct fuse_conn
*fc
= get_fuse_conn(new_req
->inode
);
1735 struct fuse_inode
*fi
= get_fuse_inode(new_req
->inode
);
1736 struct fuse_req
*tmp
;
1737 struct fuse_req
*old_req
;
1741 BUG_ON(new_req
->num_pages
!= 0);
1743 spin_lock(&fc
->lock
);
1744 list_del(&new_req
->writepages_entry
);
1745 list_for_each_entry(old_req
, &fi
->writepages
, writepages_entry
) {
1746 BUG_ON(old_req
->inode
!= new_req
->inode
);
1747 curr_index
= old_req
->misc
.write
.in
.offset
>> PAGE_CACHE_SHIFT
;
1748 if (curr_index
<= page
->index
&&
1749 page
->index
< curr_index
+ old_req
->num_pages
) {
1755 list_add(&new_req
->writepages_entry
, &fi
->writepages
);
1759 new_req
->num_pages
= 1;
1760 for (tmp
= old_req
; tmp
!= NULL
; tmp
= tmp
->misc
.write
.next
) {
1761 BUG_ON(tmp
->inode
!= new_req
->inode
);
1762 curr_index
= tmp
->misc
.write
.in
.offset
>> PAGE_CACHE_SHIFT
;
1763 if (tmp
->num_pages
== 1 &&
1764 curr_index
== page
->index
) {
1769 if (old_req
->num_pages
== 1 && (old_req
->state
== FUSE_REQ_INIT
||
1770 old_req
->state
== FUSE_REQ_PENDING
)) {
1771 struct backing_dev_info
*bdi
= page
->mapping
->backing_dev_info
;
1773 copy_highpage(old_req
->pages
[0], page
);
1774 spin_unlock(&fc
->lock
);
1776 dec_bdi_stat(bdi
, BDI_WRITEBACK
);
1777 dec_zone_page_state(page
, NR_WRITEBACK_TEMP
);
1778 bdi_writeout_inc(bdi
);
1779 fuse_writepage_free(fc
, new_req
);
1780 fuse_request_free(new_req
);
1783 new_req
->misc
.write
.next
= old_req
->misc
.write
.next
;
1784 old_req
->misc
.write
.next
= new_req
;
1787 spin_unlock(&fc
->lock
);
1792 static int fuse_writepages_fill(struct page
*page
,
1793 struct writeback_control
*wbc
, void *_data
)
1795 struct fuse_fill_wb_data
*data
= _data
;
1796 struct fuse_req
*req
= data
->req
;
1797 struct inode
*inode
= data
->inode
;
1798 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1799 struct page
*tmp_page
;
1805 data
->ff
= fuse_write_file_get(fc
, get_fuse_inode(inode
));
1811 * Being under writeback is unlikely but possible. For example direct
1812 * read to an mmaped fuse file will set the page dirty twice; once when
1813 * the pages are faulted with get_user_pages(), and then after the read
1816 is_writeback
= fuse_page_is_writeback(inode
, page
->index
);
1818 if (req
&& req
->num_pages
&&
1819 (is_writeback
|| req
->num_pages
== FUSE_MAX_PAGES_PER_REQ
||
1820 (req
->num_pages
+ 1) * PAGE_CACHE_SIZE
> fc
->max_write
||
1821 data
->orig_pages
[req
->num_pages
- 1]->index
+ 1 != page
->index
)) {
1822 fuse_writepages_send(data
);
1826 tmp_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
1831 * The page must not be redirtied until the writeout is completed
1832 * (i.e. userspace has sent a reply to the write request). Otherwise
1833 * there could be more than one temporary page instance for each real
1836 * This is ensured by holding the page lock in page_mkwrite() while
1837 * checking fuse_page_is_writeback(). We already hold the page lock
1838 * since clear_page_dirty_for_io() and keep it held until we add the
1839 * request to the fi->writepages list and increment req->num_pages.
1840 * After this fuse_page_is_writeback() will indicate that the page is
1841 * under writeback, so we can release the page lock.
1843 if (data
->req
== NULL
) {
1844 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1847 req
= fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ
);
1849 __free_page(tmp_page
);
1853 fuse_write_fill(req
, data
->ff
, page_offset(page
), 0);
1854 req
->misc
.write
.in
.write_flags
|= FUSE_WRITE_CACHE
;
1855 req
->misc
.write
.next
= NULL
;
1856 req
->in
.argpages
= 1;
1857 req
->background
= 1;
1859 req
->end
= fuse_writepage_end
;
1862 spin_lock(&fc
->lock
);
1863 list_add(&req
->writepages_entry
, &fi
->writepages
);
1864 spin_unlock(&fc
->lock
);
1868 set_page_writeback(page
);
1870 copy_highpage(tmp_page
, page
);
1871 req
->pages
[req
->num_pages
] = tmp_page
;
1872 req
->page_descs
[req
->num_pages
].offset
= 0;
1873 req
->page_descs
[req
->num_pages
].length
= PAGE_SIZE
;
1875 inc_bdi_stat(page
->mapping
->backing_dev_info
, BDI_WRITEBACK
);
1876 inc_zone_page_state(tmp_page
, NR_WRITEBACK_TEMP
);
1879 if (is_writeback
&& fuse_writepage_in_flight(req
, page
)) {
1880 end_page_writeback(page
);
1884 data
->orig_pages
[req
->num_pages
] = page
;
1887 * Protected by fc->lock against concurrent access by
1888 * fuse_page_is_writeback().
1890 spin_lock(&fc
->lock
);
1892 spin_unlock(&fc
->lock
);
1900 static int fuse_writepages(struct address_space
*mapping
,
1901 struct writeback_control
*wbc
)
1903 struct inode
*inode
= mapping
->host
;
1904 struct fuse_fill_wb_data data
;
1908 if (is_bad_inode(inode
))
1916 data
.orig_pages
= kcalloc(FUSE_MAX_PAGES_PER_REQ
,
1917 sizeof(struct page
*),
1919 if (!data
.orig_pages
)
1922 err
= write_cache_pages(mapping
, wbc
, fuse_writepages_fill
, &data
);
1924 /* Ignore errors if we can write at least one page */
1925 BUG_ON(!data
.req
->num_pages
);
1926 fuse_writepages_send(&data
);
1930 fuse_file_put(data
.ff
, false);
1932 kfree(data
.orig_pages
);
1938 * It's worthy to make sure that space is reserved on disk for the write,
1939 * but how to implement it without killing performance need more thinking.
1941 static int fuse_write_begin(struct file
*file
, struct address_space
*mapping
,
1942 loff_t pos
, unsigned len
, unsigned flags
,
1943 struct page
**pagep
, void **fsdata
)
1945 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
1946 struct fuse_conn
*fc
= get_fuse_conn(file_inode(file
));
1951 WARN_ON(!fc
->writeback_cache
);
1953 page
= grab_cache_page_write_begin(mapping
, index
, flags
);
1957 fuse_wait_on_page_writeback(mapping
->host
, page
->index
);
1959 if (PageUptodate(page
) || len
== PAGE_CACHE_SIZE
)
1962 * Check if the start this page comes after the end of file, in which
1963 * case the readpage can be optimized away.
1965 fsize
= i_size_read(mapping
->host
);
1966 if (fsize
<= (pos
& PAGE_CACHE_MASK
)) {
1967 size_t off
= pos
& ~PAGE_CACHE_MASK
;
1969 zero_user_segment(page
, 0, off
);
1972 err
= fuse_do_readpage(file
, page
);
1981 page_cache_release(page
);
1986 static int fuse_write_end(struct file
*file
, struct address_space
*mapping
,
1987 loff_t pos
, unsigned len
, unsigned copied
,
1988 struct page
*page
, void *fsdata
)
1990 struct inode
*inode
= page
->mapping
->host
;
1992 if (!PageUptodate(page
)) {
1993 /* Zero any unwritten bytes at the end of the page */
1994 size_t endoff
= (pos
+ copied
) & ~PAGE_CACHE_MASK
;
1996 zero_user_segment(page
, endoff
, PAGE_CACHE_SIZE
);
1997 SetPageUptodate(page
);
2000 fuse_write_update_size(inode
, pos
+ copied
);
2001 set_page_dirty(page
);
2003 page_cache_release(page
);
2008 static int fuse_launder_page(struct page
*page
)
2011 if (clear_page_dirty_for_io(page
)) {
2012 struct inode
*inode
= page
->mapping
->host
;
2013 err
= fuse_writepage_locked(page
);
2015 fuse_wait_on_page_writeback(inode
, page
->index
);
2021 * Write back dirty pages now, because there may not be any suitable
2024 static void fuse_vma_close(struct vm_area_struct
*vma
)
2026 filemap_write_and_wait(vma
->vm_file
->f_mapping
);
2030 * Wait for writeback against this page to complete before allowing it
2031 * to be marked dirty again, and hence written back again, possibly
2032 * before the previous writepage completed.
2034 * Block here, instead of in ->writepage(), so that the userspace fs
2035 * can only block processes actually operating on the filesystem.
2037 * Otherwise unprivileged userspace fs would be able to block
2042 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2044 static int fuse_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
2046 struct page
*page
= vmf
->page
;
2047 struct inode
*inode
= file_inode(vma
->vm_file
);
2049 file_update_time(vma
->vm_file
);
2051 if (page
->mapping
!= inode
->i_mapping
) {
2053 return VM_FAULT_NOPAGE
;
2056 fuse_wait_on_page_writeback(inode
, page
->index
);
2057 return VM_FAULT_LOCKED
;
2060 static const struct vm_operations_struct fuse_file_vm_ops
= {
2061 .close
= fuse_vma_close
,
2062 .fault
= filemap_fault
,
2063 .map_pages
= filemap_map_pages
,
2064 .page_mkwrite
= fuse_page_mkwrite
,
2065 .remap_pages
= generic_file_remap_pages
,
2068 static int fuse_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2070 if ((vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
))
2071 fuse_link_write_file(file
);
2073 file_accessed(file
);
2074 vma
->vm_ops
= &fuse_file_vm_ops
;
2078 static int fuse_direct_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2080 /* Can't provide the coherency needed for MAP_SHARED */
2081 if (vma
->vm_flags
& VM_MAYSHARE
)
2084 invalidate_inode_pages2(file
->f_mapping
);
2086 return generic_file_mmap(file
, vma
);
2089 static int convert_fuse_file_lock(const struct fuse_file_lock
*ffl
,
2090 struct file_lock
*fl
)
2092 switch (ffl
->type
) {
2098 if (ffl
->start
> OFFSET_MAX
|| ffl
->end
> OFFSET_MAX
||
2099 ffl
->end
< ffl
->start
)
2102 fl
->fl_start
= ffl
->start
;
2103 fl
->fl_end
= ffl
->end
;
2104 fl
->fl_pid
= ffl
->pid
;
2110 fl
->fl_type
= ffl
->type
;
2114 static void fuse_lk_fill(struct fuse_args
*args
, struct file
*file
,
2115 const struct file_lock
*fl
, int opcode
, pid_t pid
,
2116 int flock
, struct fuse_lk_in
*inarg
)
2118 struct inode
*inode
= file_inode(file
);
2119 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2120 struct fuse_file
*ff
= file
->private_data
;
2122 memset(inarg
, 0, sizeof(*inarg
));
2124 inarg
->owner
= fuse_lock_owner_id(fc
, fl
->fl_owner
);
2125 inarg
->lk
.start
= fl
->fl_start
;
2126 inarg
->lk
.end
= fl
->fl_end
;
2127 inarg
->lk
.type
= fl
->fl_type
;
2128 inarg
->lk
.pid
= pid
;
2130 inarg
->lk_flags
|= FUSE_LK_FLOCK
;
2131 args
->in
.h
.opcode
= opcode
;
2132 args
->in
.h
.nodeid
= get_node_id(inode
);
2133 args
->in
.numargs
= 1;
2134 args
->in
.args
[0].size
= sizeof(*inarg
);
2135 args
->in
.args
[0].value
= inarg
;
2138 static int fuse_getlk(struct file
*file
, struct file_lock
*fl
)
2140 struct inode
*inode
= file_inode(file
);
2141 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2143 struct fuse_lk_in inarg
;
2144 struct fuse_lk_out outarg
;
2147 fuse_lk_fill(&args
, file
, fl
, FUSE_GETLK
, 0, 0, &inarg
);
2148 args
.out
.numargs
= 1;
2149 args
.out
.args
[0].size
= sizeof(outarg
);
2150 args
.out
.args
[0].value
= &outarg
;
2151 err
= fuse_simple_request(fc
, &args
);
2153 err
= convert_fuse_file_lock(&outarg
.lk
, fl
);
2158 static int fuse_setlk(struct file
*file
, struct file_lock
*fl
, int flock
)
2160 struct inode
*inode
= file_inode(file
);
2161 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2163 struct fuse_lk_in inarg
;
2164 int opcode
= (fl
->fl_flags
& FL_SLEEP
) ? FUSE_SETLKW
: FUSE_SETLK
;
2165 pid_t pid
= fl
->fl_type
!= F_UNLCK
? current
->tgid
: 0;
2168 if (fl
->fl_lmops
&& fl
->fl_lmops
->lm_grant
) {
2169 /* NLM needs asynchronous locks, which we don't support yet */
2173 /* Unlock on close is handled by the flush method */
2174 if (fl
->fl_flags
& FL_CLOSE
)
2177 fuse_lk_fill(&args
, file
, fl
, opcode
, pid
, flock
, &inarg
);
2178 err
= fuse_simple_request(fc
, &args
);
2180 /* locking is restartable */
2187 static int fuse_file_lock(struct file
*file
, int cmd
, struct file_lock
*fl
)
2189 struct inode
*inode
= file_inode(file
);
2190 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2193 if (cmd
== F_CANCELLK
) {
2195 } else if (cmd
== F_GETLK
) {
2197 posix_test_lock(file
, fl
);
2200 err
= fuse_getlk(file
, fl
);
2203 err
= posix_lock_file(file
, fl
, NULL
);
2205 err
= fuse_setlk(file
, fl
, 0);
2210 static int fuse_file_flock(struct file
*file
, int cmd
, struct file_lock
*fl
)
2212 struct inode
*inode
= file_inode(file
);
2213 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2217 err
= flock_lock_file_wait(file
, fl
);
2219 struct fuse_file
*ff
= file
->private_data
;
2221 /* emulate flock with POSIX locks */
2223 err
= fuse_setlk(file
, fl
, 1);
2229 static sector_t
fuse_bmap(struct address_space
*mapping
, sector_t block
)
2231 struct inode
*inode
= mapping
->host
;
2232 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2234 struct fuse_bmap_in inarg
;
2235 struct fuse_bmap_out outarg
;
2238 if (!inode
->i_sb
->s_bdev
|| fc
->no_bmap
)
2241 memset(&inarg
, 0, sizeof(inarg
));
2242 inarg
.block
= block
;
2243 inarg
.blocksize
= inode
->i_sb
->s_blocksize
;
2244 args
.in
.h
.opcode
= FUSE_BMAP
;
2245 args
.in
.h
.nodeid
= get_node_id(inode
);
2246 args
.in
.numargs
= 1;
2247 args
.in
.args
[0].size
= sizeof(inarg
);
2248 args
.in
.args
[0].value
= &inarg
;
2249 args
.out
.numargs
= 1;
2250 args
.out
.args
[0].size
= sizeof(outarg
);
2251 args
.out
.args
[0].value
= &outarg
;
2252 err
= fuse_simple_request(fc
, &args
);
2256 return err
? 0 : outarg
.block
;
2259 static loff_t
fuse_file_llseek(struct file
*file
, loff_t offset
, int whence
)
2262 struct inode
*inode
= file_inode(file
);
2264 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2265 if (whence
== SEEK_CUR
|| whence
== SEEK_SET
)
2266 return generic_file_llseek(file
, offset
, whence
);
2268 mutex_lock(&inode
->i_mutex
);
2269 retval
= fuse_update_attributes(inode
, NULL
, file
, NULL
);
2271 retval
= generic_file_llseek(file
, offset
, whence
);
2272 mutex_unlock(&inode
->i_mutex
);
2277 static int fuse_ioctl_copy_user(struct page
**pages
, struct iovec
*iov
,
2278 unsigned int nr_segs
, size_t bytes
, bool to_user
)
2286 iov_iter_init(&ii
, to_user
? READ
: WRITE
, iov
, nr_segs
, bytes
);
2288 while (iov_iter_count(&ii
)) {
2289 struct page
*page
= pages
[page_idx
++];
2290 size_t todo
= min_t(size_t, PAGE_SIZE
, iov_iter_count(&ii
));
2296 char __user
*uaddr
= ii
.iov
->iov_base
+ ii
.iov_offset
;
2297 size_t iov_len
= ii
.iov
->iov_len
- ii
.iov_offset
;
2298 size_t copy
= min(todo
, iov_len
);
2302 left
= copy_from_user(kaddr
, uaddr
, copy
);
2304 left
= copy_to_user(uaddr
, kaddr
, copy
);
2309 iov_iter_advance(&ii
, copy
);
2321 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2322 * ABI was defined to be 'struct iovec' which is different on 32bit
2323 * and 64bit. Fortunately we can determine which structure the server
2324 * used from the size of the reply.
2326 static int fuse_copy_ioctl_iovec_old(struct iovec
*dst
, void *src
,
2327 size_t transferred
, unsigned count
,
2330 #ifdef CONFIG_COMPAT
2331 if (count
* sizeof(struct compat_iovec
) == transferred
) {
2332 struct compat_iovec
*ciov
= src
;
2336 * With this interface a 32bit server cannot support
2337 * non-compat (i.e. ones coming from 64bit apps) ioctl
2343 for (i
= 0; i
< count
; i
++) {
2344 dst
[i
].iov_base
= compat_ptr(ciov
[i
].iov_base
);
2345 dst
[i
].iov_len
= ciov
[i
].iov_len
;
2351 if (count
* sizeof(struct iovec
) != transferred
)
2354 memcpy(dst
, src
, transferred
);
2358 /* Make sure iov_length() won't overflow */
2359 static int fuse_verify_ioctl_iov(struct iovec
*iov
, size_t count
)
2362 u32 max
= FUSE_MAX_PAGES_PER_REQ
<< PAGE_SHIFT
;
2364 for (n
= 0; n
< count
; n
++, iov
++) {
2365 if (iov
->iov_len
> (size_t) max
)
2367 max
-= iov
->iov_len
;
2372 static int fuse_copy_ioctl_iovec(struct fuse_conn
*fc
, struct iovec
*dst
,
2373 void *src
, size_t transferred
, unsigned count
,
2377 struct fuse_ioctl_iovec
*fiov
= src
;
2379 if (fc
->minor
< 16) {
2380 return fuse_copy_ioctl_iovec_old(dst
, src
, transferred
,
2384 if (count
* sizeof(struct fuse_ioctl_iovec
) != transferred
)
2387 for (i
= 0; i
< count
; i
++) {
2388 /* Did the server supply an inappropriate value? */
2389 if (fiov
[i
].base
!= (unsigned long) fiov
[i
].base
||
2390 fiov
[i
].len
!= (unsigned long) fiov
[i
].len
)
2393 dst
[i
].iov_base
= (void __user
*) (unsigned long) fiov
[i
].base
;
2394 dst
[i
].iov_len
= (size_t) fiov
[i
].len
;
2396 #ifdef CONFIG_COMPAT
2398 (ptr_to_compat(dst
[i
].iov_base
) != fiov
[i
].base
||
2399 (compat_size_t
) dst
[i
].iov_len
!= fiov
[i
].len
))
2409 * For ioctls, there is no generic way to determine how much memory
2410 * needs to be read and/or written. Furthermore, ioctls are allowed
2411 * to dereference the passed pointer, so the parameter requires deep
2412 * copying but FUSE has no idea whatsoever about what to copy in or
2415 * This is solved by allowing FUSE server to retry ioctl with
2416 * necessary in/out iovecs. Let's assume the ioctl implementation
2417 * needs to read in the following structure.
2424 * On the first callout to FUSE server, inarg->in_size and
2425 * inarg->out_size will be NULL; then, the server completes the ioctl
2426 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2427 * the actual iov array to
2429 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2431 * which tells FUSE to copy in the requested area and retry the ioctl.
2432 * On the second round, the server has access to the structure and
2433 * from that it can tell what to look for next, so on the invocation,
2434 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2436 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2437 * { .iov_base = a.buf, .iov_len = a.buflen } }
2439 * FUSE will copy both struct a and the pointed buffer from the
2440 * process doing the ioctl and retry ioctl with both struct a and the
2443 * This time, FUSE server has everything it needs and completes ioctl
2444 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2446 * Copying data out works the same way.
2448 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2449 * automatically initializes in and out iovs by decoding @cmd with
2450 * _IOC_* macros and the server is not allowed to request RETRY. This
2451 * limits ioctl data transfers to well-formed ioctls and is the forced
2452 * behavior for all FUSE servers.
2454 long fuse_do_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
,
2457 struct fuse_file
*ff
= file
->private_data
;
2458 struct fuse_conn
*fc
= ff
->fc
;
2459 struct fuse_ioctl_in inarg
= {
2465 struct fuse_ioctl_out outarg
;
2466 struct fuse_req
*req
= NULL
;
2467 struct page
**pages
= NULL
;
2468 struct iovec
*iov_page
= NULL
;
2469 struct iovec
*in_iov
= NULL
, *out_iov
= NULL
;
2470 unsigned int in_iovs
= 0, out_iovs
= 0, num_pages
= 0, max_pages
;
2471 size_t in_size
, out_size
, transferred
;
2474 #if BITS_PER_LONG == 32
2475 inarg
.flags
|= FUSE_IOCTL_32BIT
;
2477 if (flags
& FUSE_IOCTL_COMPAT
)
2478 inarg
.flags
|= FUSE_IOCTL_32BIT
;
2481 /* assume all the iovs returned by client always fits in a page */
2482 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec
) * FUSE_IOCTL_MAX_IOV
> PAGE_SIZE
);
2485 pages
= kcalloc(FUSE_MAX_PAGES_PER_REQ
, sizeof(pages
[0]), GFP_KERNEL
);
2486 iov_page
= (struct iovec
*) __get_free_page(GFP_KERNEL
);
2487 if (!pages
|| !iov_page
)
2491 * If restricted, initialize IO parameters as encoded in @cmd.
2492 * RETRY from server is not allowed.
2494 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
)) {
2495 struct iovec
*iov
= iov_page
;
2497 iov
->iov_base
= (void __user
*)arg
;
2498 iov
->iov_len
= _IOC_SIZE(cmd
);
2500 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
2505 if (_IOC_DIR(cmd
) & _IOC_READ
) {
2512 inarg
.in_size
= in_size
= iov_length(in_iov
, in_iovs
);
2513 inarg
.out_size
= out_size
= iov_length(out_iov
, out_iovs
);
2516 * Out data can be used either for actual out data or iovs,
2517 * make sure there always is at least one page.
2519 out_size
= max_t(size_t, out_size
, PAGE_SIZE
);
2520 max_pages
= DIV_ROUND_UP(max(in_size
, out_size
), PAGE_SIZE
);
2522 /* make sure there are enough buffer pages and init request with them */
2524 if (max_pages
> FUSE_MAX_PAGES_PER_REQ
)
2526 while (num_pages
< max_pages
) {
2527 pages
[num_pages
] = alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
2528 if (!pages
[num_pages
])
2533 req
= fuse_get_req(fc
, num_pages
);
2539 memcpy(req
->pages
, pages
, sizeof(req
->pages
[0]) * num_pages
);
2540 req
->num_pages
= num_pages
;
2541 fuse_page_descs_length_init(req
, 0, req
->num_pages
);
2543 /* okay, let's send it to the client */
2544 req
->in
.h
.opcode
= FUSE_IOCTL
;
2545 req
->in
.h
.nodeid
= ff
->nodeid
;
2546 req
->in
.numargs
= 1;
2547 req
->in
.args
[0].size
= sizeof(inarg
);
2548 req
->in
.args
[0].value
= &inarg
;
2551 req
->in
.args
[1].size
= in_size
;
2552 req
->in
.argpages
= 1;
2554 err
= fuse_ioctl_copy_user(pages
, in_iov
, in_iovs
, in_size
,
2560 req
->out
.numargs
= 2;
2561 req
->out
.args
[0].size
= sizeof(outarg
);
2562 req
->out
.args
[0].value
= &outarg
;
2563 req
->out
.args
[1].size
= out_size
;
2564 req
->out
.argpages
= 1;
2565 req
->out
.argvar
= 1;
2567 fuse_request_send(fc
, req
);
2568 err
= req
->out
.h
.error
;
2569 transferred
= req
->out
.args
[1].size
;
2570 fuse_put_request(fc
, req
);
2575 /* did it ask for retry? */
2576 if (outarg
.flags
& FUSE_IOCTL_RETRY
) {
2579 /* no retry if in restricted mode */
2581 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
))
2584 in_iovs
= outarg
.in_iovs
;
2585 out_iovs
= outarg
.out_iovs
;
2588 * Make sure things are in boundary, separate checks
2589 * are to protect against overflow.
2592 if (in_iovs
> FUSE_IOCTL_MAX_IOV
||
2593 out_iovs
> FUSE_IOCTL_MAX_IOV
||
2594 in_iovs
+ out_iovs
> FUSE_IOCTL_MAX_IOV
)
2597 vaddr
= kmap_atomic(pages
[0]);
2598 err
= fuse_copy_ioctl_iovec(fc
, iov_page
, vaddr
,
2599 transferred
, in_iovs
+ out_iovs
,
2600 (flags
& FUSE_IOCTL_COMPAT
) != 0);
2601 kunmap_atomic(vaddr
);
2606 out_iov
= in_iov
+ in_iovs
;
2608 err
= fuse_verify_ioctl_iov(in_iov
, in_iovs
);
2612 err
= fuse_verify_ioctl_iov(out_iov
, out_iovs
);
2620 if (transferred
> inarg
.out_size
)
2623 err
= fuse_ioctl_copy_user(pages
, out_iov
, out_iovs
, transferred
, true);
2626 fuse_put_request(fc
, req
);
2627 free_page((unsigned long) iov_page
);
2629 __free_page(pages
[--num_pages
]);
2632 return err
? err
: outarg
.result
;
2634 EXPORT_SYMBOL_GPL(fuse_do_ioctl
);
2636 long fuse_ioctl_common(struct file
*file
, unsigned int cmd
,
2637 unsigned long arg
, unsigned int flags
)
2639 struct inode
*inode
= file_inode(file
);
2640 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2642 if (!fuse_allow_current_process(fc
))
2645 if (is_bad_inode(inode
))
2648 return fuse_do_ioctl(file
, cmd
, arg
, flags
);
2651 static long fuse_file_ioctl(struct file
*file
, unsigned int cmd
,
2654 return fuse_ioctl_common(file
, cmd
, arg
, 0);
2657 static long fuse_file_compat_ioctl(struct file
*file
, unsigned int cmd
,
2660 return fuse_ioctl_common(file
, cmd
, arg
, FUSE_IOCTL_COMPAT
);
2664 * All files which have been polled are linked to RB tree
2665 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2666 * find the matching one.
2668 static struct rb_node
**fuse_find_polled_node(struct fuse_conn
*fc
, u64 kh
,
2669 struct rb_node
**parent_out
)
2671 struct rb_node
**link
= &fc
->polled_files
.rb_node
;
2672 struct rb_node
*last
= NULL
;
2675 struct fuse_file
*ff
;
2678 ff
= rb_entry(last
, struct fuse_file
, polled_node
);
2681 link
= &last
->rb_left
;
2682 else if (kh
> ff
->kh
)
2683 link
= &last
->rb_right
;
2694 * The file is about to be polled. Make sure it's on the polled_files
2695 * RB tree. Note that files once added to the polled_files tree are
2696 * not removed before the file is released. This is because a file
2697 * polled once is likely to be polled again.
2699 static void fuse_register_polled_file(struct fuse_conn
*fc
,
2700 struct fuse_file
*ff
)
2702 spin_lock(&fc
->lock
);
2703 if (RB_EMPTY_NODE(&ff
->polled_node
)) {
2704 struct rb_node
**link
, *uninitialized_var(parent
);
2706 link
= fuse_find_polled_node(fc
, ff
->kh
, &parent
);
2708 rb_link_node(&ff
->polled_node
, parent
, link
);
2709 rb_insert_color(&ff
->polled_node
, &fc
->polled_files
);
2711 spin_unlock(&fc
->lock
);
2714 unsigned fuse_file_poll(struct file
*file
, poll_table
*wait
)
2716 struct fuse_file
*ff
= file
->private_data
;
2717 struct fuse_conn
*fc
= ff
->fc
;
2718 struct fuse_poll_in inarg
= { .fh
= ff
->fh
, .kh
= ff
->kh
};
2719 struct fuse_poll_out outarg
;
2724 return DEFAULT_POLLMASK
;
2726 poll_wait(file
, &ff
->poll_wait
, wait
);
2727 inarg
.events
= (__u32
)poll_requested_events(wait
);
2730 * Ask for notification iff there's someone waiting for it.
2731 * The client may ignore the flag and always notify.
2733 if (waitqueue_active(&ff
->poll_wait
)) {
2734 inarg
.flags
|= FUSE_POLL_SCHEDULE_NOTIFY
;
2735 fuse_register_polled_file(fc
, ff
);
2738 args
.in
.h
.opcode
= FUSE_POLL
;
2739 args
.in
.h
.nodeid
= ff
->nodeid
;
2740 args
.in
.numargs
= 1;
2741 args
.in
.args
[0].size
= sizeof(inarg
);
2742 args
.in
.args
[0].value
= &inarg
;
2743 args
.out
.numargs
= 1;
2744 args
.out
.args
[0].size
= sizeof(outarg
);
2745 args
.out
.args
[0].value
= &outarg
;
2746 err
= fuse_simple_request(fc
, &args
);
2749 return outarg
.revents
;
2750 if (err
== -ENOSYS
) {
2752 return DEFAULT_POLLMASK
;
2756 EXPORT_SYMBOL_GPL(fuse_file_poll
);
2759 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2760 * wakes up the poll waiters.
2762 int fuse_notify_poll_wakeup(struct fuse_conn
*fc
,
2763 struct fuse_notify_poll_wakeup_out
*outarg
)
2765 u64 kh
= outarg
->kh
;
2766 struct rb_node
**link
;
2768 spin_lock(&fc
->lock
);
2770 link
= fuse_find_polled_node(fc
, kh
, NULL
);
2772 struct fuse_file
*ff
;
2774 ff
= rb_entry(*link
, struct fuse_file
, polled_node
);
2775 wake_up_interruptible_sync(&ff
->poll_wait
);
2778 spin_unlock(&fc
->lock
);
2782 static void fuse_do_truncate(struct file
*file
)
2784 struct inode
*inode
= file
->f_mapping
->host
;
2787 attr
.ia_valid
= ATTR_SIZE
;
2788 attr
.ia_size
= i_size_read(inode
);
2790 attr
.ia_file
= file
;
2791 attr
.ia_valid
|= ATTR_FILE
;
2793 fuse_do_setattr(inode
, &attr
, file
);
2796 static inline loff_t
fuse_round_up(loff_t off
)
2798 return round_up(off
, FUSE_MAX_PAGES_PER_REQ
<< PAGE_SHIFT
);
2802 fuse_direct_IO(int rw
, struct kiocb
*iocb
, struct iov_iter
*iter
,
2806 struct file
*file
= iocb
->ki_filp
;
2807 struct fuse_file
*ff
= file
->private_data
;
2808 bool async_dio
= ff
->fc
->async_dio
;
2810 struct inode
*inode
;
2812 size_t count
= iov_iter_count(iter
);
2813 struct fuse_io_priv
*io
;
2816 inode
= file
->f_mapping
->host
;
2817 i_size
= i_size_read(inode
);
2819 if ((rw
== READ
) && (offset
> i_size
))
2822 /* optimization for short read */
2823 if (async_dio
&& rw
!= WRITE
&& offset
+ count
> i_size
) {
2824 if (offset
>= i_size
)
2826 count
= min_t(loff_t
, count
, fuse_round_up(i_size
- offset
));
2827 iov_iter_truncate(iter
, count
);
2830 io
= kmalloc(sizeof(struct fuse_io_priv
), GFP_KERNEL
);
2833 spin_lock_init(&io
->lock
);
2837 io
->offset
= offset
;
2838 io
->write
= (rw
== WRITE
);
2842 * By default, we want to optimize all I/Os with async request
2843 * submission to the client filesystem if supported.
2845 io
->async
= async_dio
;
2849 * We cannot asynchronously extend the size of a file. We have no method
2850 * to wait on real async I/O requests, so we must submit this request
2853 if (!is_sync_kiocb(iocb
) && (offset
+ count
> i_size
) && rw
== WRITE
)
2857 ret
= __fuse_direct_write(io
, iter
, &pos
);
2859 ret
= __fuse_direct_read(io
, iter
, &pos
);
2862 fuse_aio_complete(io
, ret
< 0 ? ret
: 0, -1);
2864 /* we have a non-extending, async request, so return */
2865 if (!is_sync_kiocb(iocb
))
2866 return -EIOCBQUEUED
;
2868 ret
= wait_on_sync_kiocb(iocb
);
2875 fuse_write_update_size(inode
, pos
);
2876 else if (ret
< 0 && offset
+ count
> i_size
)
2877 fuse_do_truncate(file
);
2883 static long fuse_file_fallocate(struct file
*file
, int mode
, loff_t offset
,
2886 struct fuse_file
*ff
= file
->private_data
;
2887 struct inode
*inode
= file_inode(file
);
2888 struct fuse_inode
*fi
= get_fuse_inode(inode
);
2889 struct fuse_conn
*fc
= ff
->fc
;
2891 struct fuse_fallocate_in inarg
= {
2898 bool lock_inode
= !(mode
& FALLOC_FL_KEEP_SIZE
) ||
2899 (mode
& FALLOC_FL_PUNCH_HOLE
);
2901 if (mode
& ~(FALLOC_FL_KEEP_SIZE
| FALLOC_FL_PUNCH_HOLE
))
2904 if (fc
->no_fallocate
)
2908 mutex_lock(&inode
->i_mutex
);
2909 if (mode
& FALLOC_FL_PUNCH_HOLE
) {
2910 loff_t endbyte
= offset
+ length
- 1;
2911 err
= filemap_write_and_wait_range(inode
->i_mapping
,
2916 fuse_sync_writes(inode
);
2920 if (!(mode
& FALLOC_FL_KEEP_SIZE
))
2921 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
2923 args
.in
.h
.opcode
= FUSE_FALLOCATE
;
2924 args
.in
.h
.nodeid
= ff
->nodeid
;
2925 args
.in
.numargs
= 1;
2926 args
.in
.args
[0].size
= sizeof(inarg
);
2927 args
.in
.args
[0].value
= &inarg
;
2928 err
= fuse_simple_request(fc
, &args
);
2929 if (err
== -ENOSYS
) {
2930 fc
->no_fallocate
= 1;
2936 /* we could have extended the file */
2937 if (!(mode
& FALLOC_FL_KEEP_SIZE
)) {
2938 bool changed
= fuse_write_update_size(inode
, offset
+ length
);
2940 if (changed
&& fc
->writeback_cache
)
2941 file_update_time(file
);
2944 if (mode
& FALLOC_FL_PUNCH_HOLE
)
2945 truncate_pagecache_range(inode
, offset
, offset
+ length
- 1);
2947 fuse_invalidate_attr(inode
);
2950 if (!(mode
& FALLOC_FL_KEEP_SIZE
))
2951 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
2954 mutex_unlock(&inode
->i_mutex
);
2959 static const struct file_operations fuse_file_operations
= {
2960 .llseek
= fuse_file_llseek
,
2961 .read
= new_sync_read
,
2962 .read_iter
= fuse_file_read_iter
,
2963 .write
= new_sync_write
,
2964 .write_iter
= fuse_file_write_iter
,
2965 .mmap
= fuse_file_mmap
,
2967 .flush
= fuse_flush
,
2968 .release
= fuse_release
,
2969 .fsync
= fuse_fsync
,
2970 .lock
= fuse_file_lock
,
2971 .flock
= fuse_file_flock
,
2972 .splice_read
= generic_file_splice_read
,
2973 .unlocked_ioctl
= fuse_file_ioctl
,
2974 .compat_ioctl
= fuse_file_compat_ioctl
,
2975 .poll
= fuse_file_poll
,
2976 .fallocate
= fuse_file_fallocate
,
2979 static const struct file_operations fuse_direct_io_file_operations
= {
2980 .llseek
= fuse_file_llseek
,
2981 .read
= fuse_direct_read
,
2982 .write
= fuse_direct_write
,
2983 .mmap
= fuse_direct_mmap
,
2985 .flush
= fuse_flush
,
2986 .release
= fuse_release
,
2987 .fsync
= fuse_fsync
,
2988 .lock
= fuse_file_lock
,
2989 .flock
= fuse_file_flock
,
2990 .unlocked_ioctl
= fuse_file_ioctl
,
2991 .compat_ioctl
= fuse_file_compat_ioctl
,
2992 .poll
= fuse_file_poll
,
2993 .fallocate
= fuse_file_fallocate
,
2994 /* no splice_read */
2997 static const struct address_space_operations fuse_file_aops
= {
2998 .readpage
= fuse_readpage
,
2999 .writepage
= fuse_writepage
,
3000 .writepages
= fuse_writepages
,
3001 .launder_page
= fuse_launder_page
,
3002 .readpages
= fuse_readpages
,
3003 .set_page_dirty
= __set_page_dirty_nobuffers
,
3005 .direct_IO
= fuse_direct_IO
,
3006 .write_begin
= fuse_write_begin
,
3007 .write_end
= fuse_write_end
,
3010 void fuse_init_file_inode(struct inode
*inode
)
3012 inode
->i_fop
= &fuse_file_operations
;
3013 inode
->i_data
.a_ops
= &fuse_file_aops
;