2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU GPL.
11 #include <linux/pagemap.h>
12 #include <linux/slab.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/module.h>
16 #include <linux/compat.h>
17 #include <linux/swap.h>
18 #include <linux/aio.h>
19 #include <linux/falloc.h>
21 static const struct file_operations fuse_direct_io_file_operations
;
23 static int fuse_send_open(struct fuse_conn
*fc
, u64 nodeid
, struct file
*file
,
24 int opcode
, struct fuse_open_out
*outargp
)
26 struct fuse_open_in inarg
;
30 req
= fuse_get_req_nopages(fc
);
34 memset(&inarg
, 0, sizeof(inarg
));
35 inarg
.flags
= file
->f_flags
& ~(O_CREAT
| O_EXCL
| O_NOCTTY
);
36 if (!fc
->atomic_o_trunc
)
37 inarg
.flags
&= ~O_TRUNC
;
38 req
->in
.h
.opcode
= opcode
;
39 req
->in
.h
.nodeid
= nodeid
;
41 req
->in
.args
[0].size
= sizeof(inarg
);
42 req
->in
.args
[0].value
= &inarg
;
44 req
->out
.args
[0].size
= sizeof(*outargp
);
45 req
->out
.args
[0].value
= outargp
;
46 fuse_request_send(fc
, req
);
47 err
= req
->out
.h
.error
;
48 fuse_put_request(fc
, req
);
53 struct fuse_file
*fuse_file_alloc(struct fuse_conn
*fc
)
57 ff
= kmalloc(sizeof(struct fuse_file
), GFP_KERNEL
);
62 ff
->reserved_req
= fuse_request_alloc(0);
63 if (unlikely(!ff
->reserved_req
)) {
68 INIT_LIST_HEAD(&ff
->write_entry
);
69 atomic_set(&ff
->count
, 0);
70 RB_CLEAR_NODE(&ff
->polled_node
);
71 init_waitqueue_head(&ff
->poll_wait
);
75 spin_unlock(&fc
->lock
);
80 void fuse_file_free(struct fuse_file
*ff
)
82 fuse_request_free(ff
->reserved_req
);
86 struct fuse_file
*fuse_file_get(struct fuse_file
*ff
)
88 atomic_inc(&ff
->count
);
92 static void fuse_release_async(struct work_struct
*work
)
98 req
= container_of(work
, struct fuse_req
, misc
.release
.work
);
99 path
= req
->misc
.release
.path
;
100 fc
= get_fuse_conn(path
.dentry
->d_inode
);
102 fuse_put_request(fc
, req
);
106 static void fuse_release_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
108 if (fc
->destroy_req
) {
110 * If this is a fuseblk mount, then it's possible that
111 * releasing the path will result in releasing the
112 * super block and sending the DESTROY request. If
113 * the server is single threaded, this would hang.
114 * For this reason do the path_put() in a separate
117 atomic_inc(&req
->count
);
118 INIT_WORK(&req
->misc
.release
.work
, fuse_release_async
);
119 schedule_work(&req
->misc
.release
.work
);
121 path_put(&req
->misc
.release
.path
);
125 static void fuse_file_put(struct fuse_file
*ff
, bool sync
)
127 if (atomic_dec_and_test(&ff
->count
)) {
128 struct fuse_req
*req
= ff
->reserved_req
;
130 if (ff
->fc
->no_open
) {
132 * Drop the release request when client does not
136 path_put(&req
->misc
.release
.path
);
137 fuse_put_request(ff
->fc
, req
);
140 fuse_request_send(ff
->fc
, req
);
141 path_put(&req
->misc
.release
.path
);
142 fuse_put_request(ff
->fc
, req
);
144 req
->end
= fuse_release_end
;
146 fuse_request_send_background(ff
->fc
, req
);
152 int fuse_do_open(struct fuse_conn
*fc
, u64 nodeid
, struct file
*file
,
155 struct fuse_file
*ff
;
156 int opcode
= isdir
? FUSE_OPENDIR
: FUSE_OPEN
;
158 ff
= fuse_file_alloc(fc
);
163 ff
->open_flags
= FOPEN_KEEP_CACHE
; /* Default for no-open */
164 if (!fc
->no_open
|| isdir
) {
165 struct fuse_open_out outarg
;
168 err
= fuse_send_open(fc
, nodeid
, file
, opcode
, &outarg
);
171 ff
->open_flags
= outarg
.open_flags
;
173 } else if (err
!= -ENOSYS
|| isdir
) {
182 ff
->open_flags
&= ~FOPEN_DIRECT_IO
;
185 file
->private_data
= fuse_file_get(ff
);
189 EXPORT_SYMBOL_GPL(fuse_do_open
);
191 static void fuse_link_write_file(struct file
*file
)
193 struct inode
*inode
= file_inode(file
);
194 struct fuse_conn
*fc
= get_fuse_conn(inode
);
195 struct fuse_inode
*fi
= get_fuse_inode(inode
);
196 struct fuse_file
*ff
= file
->private_data
;
198 * file may be written through mmap, so chain it onto the
199 * inodes's write_file list
201 spin_lock(&fc
->lock
);
202 if (list_empty(&ff
->write_entry
))
203 list_add(&ff
->write_entry
, &fi
->write_files
);
204 spin_unlock(&fc
->lock
);
207 void fuse_finish_open(struct inode
*inode
, struct file
*file
)
209 struct fuse_file
*ff
= file
->private_data
;
210 struct fuse_conn
*fc
= get_fuse_conn(inode
);
212 if (ff
->open_flags
& FOPEN_DIRECT_IO
)
213 file
->f_op
= &fuse_direct_io_file_operations
;
214 if (!(ff
->open_flags
& FOPEN_KEEP_CACHE
))
215 invalidate_inode_pages2(inode
->i_mapping
);
216 if (ff
->open_flags
& FOPEN_NONSEEKABLE
)
217 nonseekable_open(inode
, file
);
218 if (fc
->atomic_o_trunc
&& (file
->f_flags
& O_TRUNC
)) {
219 struct fuse_inode
*fi
= get_fuse_inode(inode
);
221 spin_lock(&fc
->lock
);
222 fi
->attr_version
= ++fc
->attr_version
;
223 i_size_write(inode
, 0);
224 spin_unlock(&fc
->lock
);
225 fuse_invalidate_attr(inode
);
227 if ((file
->f_mode
& FMODE_WRITE
) && fc
->writeback_cache
)
228 fuse_link_write_file(file
);
231 int fuse_open_common(struct inode
*inode
, struct file
*file
, bool isdir
)
233 struct fuse_conn
*fc
= get_fuse_conn(inode
);
236 err
= generic_file_open(inode
, file
);
240 err
= fuse_do_open(fc
, get_node_id(inode
), file
, isdir
);
244 fuse_finish_open(inode
, file
);
249 static void fuse_prepare_release(struct fuse_file
*ff
, int flags
, int opcode
)
251 struct fuse_conn
*fc
= ff
->fc
;
252 struct fuse_req
*req
= ff
->reserved_req
;
253 struct fuse_release_in
*inarg
= &req
->misc
.release
.in
;
255 spin_lock(&fc
->lock
);
256 list_del(&ff
->write_entry
);
257 if (!RB_EMPTY_NODE(&ff
->polled_node
))
258 rb_erase(&ff
->polled_node
, &fc
->polled_files
);
259 spin_unlock(&fc
->lock
);
261 wake_up_interruptible_all(&ff
->poll_wait
);
264 inarg
->flags
= flags
;
265 req
->in
.h
.opcode
= opcode
;
266 req
->in
.h
.nodeid
= ff
->nodeid
;
268 req
->in
.args
[0].size
= sizeof(struct fuse_release_in
);
269 req
->in
.args
[0].value
= inarg
;
272 void fuse_release_common(struct file
*file
, int opcode
)
274 struct fuse_file
*ff
;
275 struct fuse_req
*req
;
277 ff
= file
->private_data
;
281 req
= ff
->reserved_req
;
282 fuse_prepare_release(ff
, file
->f_flags
, opcode
);
285 struct fuse_release_in
*inarg
= &req
->misc
.release
.in
;
286 inarg
->release_flags
|= FUSE_RELEASE_FLOCK_UNLOCK
;
287 inarg
->lock_owner
= fuse_lock_owner_id(ff
->fc
,
290 /* Hold vfsmount and dentry until release is finished */
291 path_get(&file
->f_path
);
292 req
->misc
.release
.path
= file
->f_path
;
295 * Normally this will send the RELEASE request, however if
296 * some asynchronous READ or WRITE requests are outstanding,
297 * the sending will be delayed.
299 * Make the release synchronous if this is a fuseblk mount,
300 * synchronous RELEASE is allowed (and desirable) in this case
301 * because the server can be trusted not to screw up.
303 fuse_file_put(ff
, ff
->fc
->destroy_req
!= NULL
);
306 static int fuse_open(struct inode
*inode
, struct file
*file
)
308 return fuse_open_common(inode
, file
, false);
311 static int fuse_release(struct inode
*inode
, struct file
*file
)
313 struct fuse_conn
*fc
= get_fuse_conn(inode
);
315 /* see fuse_vma_close() for !writeback_cache case */
316 if (fc
->writeback_cache
)
317 filemap_write_and_wait(file
->f_mapping
);
319 if (test_bit(FUSE_I_MTIME_DIRTY
, &get_fuse_inode(inode
)->state
))
320 fuse_flush_mtime(file
, true);
322 fuse_release_common(file
, FUSE_RELEASE
);
324 /* return value is ignored by VFS */
328 void fuse_sync_release(struct fuse_file
*ff
, int flags
)
330 WARN_ON(atomic_read(&ff
->count
) > 1);
331 fuse_prepare_release(ff
, flags
, FUSE_RELEASE
);
332 ff
->reserved_req
->force
= 1;
333 ff
->reserved_req
->background
= 0;
334 fuse_request_send(ff
->fc
, ff
->reserved_req
);
335 fuse_put_request(ff
->fc
, ff
->reserved_req
);
338 EXPORT_SYMBOL_GPL(fuse_sync_release
);
341 * Scramble the ID space with XTEA, so that the value of the files_struct
342 * pointer is not exposed to userspace.
344 u64
fuse_lock_owner_id(struct fuse_conn
*fc
, fl_owner_t id
)
346 u32
*k
= fc
->scramble_key
;
347 u64 v
= (unsigned long) id
;
353 for (i
= 0; i
< 32; i
++) {
354 v0
+= ((v1
<< 4 ^ v1
>> 5) + v1
) ^ (sum
+ k
[sum
& 3]);
356 v1
+= ((v0
<< 4 ^ v0
>> 5) + v0
) ^ (sum
+ k
[sum
>>11 & 3]);
359 return (u64
) v0
+ ((u64
) v1
<< 32);
363 * Check if any page in a range is under writeback
365 * This is currently done by walking the list of writepage requests
366 * for the inode, which can be pretty inefficient.
368 static bool fuse_range_is_writeback(struct inode
*inode
, pgoff_t idx_from
,
371 struct fuse_conn
*fc
= get_fuse_conn(inode
);
372 struct fuse_inode
*fi
= get_fuse_inode(inode
);
373 struct fuse_req
*req
;
376 spin_lock(&fc
->lock
);
377 list_for_each_entry(req
, &fi
->writepages
, writepages_entry
) {
380 BUG_ON(req
->inode
!= inode
);
381 curr_index
= req
->misc
.write
.in
.offset
>> PAGE_CACHE_SHIFT
;
382 if (idx_from
< curr_index
+ req
->num_pages
&&
383 curr_index
<= idx_to
) {
388 spin_unlock(&fc
->lock
);
393 static inline bool fuse_page_is_writeback(struct inode
*inode
, pgoff_t index
)
395 return fuse_range_is_writeback(inode
, index
, index
);
399 * Wait for page writeback to be completed.
401 * Since fuse doesn't rely on the VM writeback tracking, this has to
402 * use some other means.
404 static int fuse_wait_on_page_writeback(struct inode
*inode
, pgoff_t index
)
406 struct fuse_inode
*fi
= get_fuse_inode(inode
);
408 wait_event(fi
->page_waitq
, !fuse_page_is_writeback(inode
, index
));
413 * Wait for all pending writepages on the inode to finish.
415 * This is currently done by blocking further writes with FUSE_NOWRITE
416 * and waiting for all sent writes to complete.
418 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
419 * could conflict with truncation.
421 static void fuse_sync_writes(struct inode
*inode
)
423 fuse_set_nowrite(inode
);
424 fuse_release_nowrite(inode
);
427 static int fuse_flush(struct file
*file
, fl_owner_t id
)
429 struct inode
*inode
= file_inode(file
);
430 struct fuse_conn
*fc
= get_fuse_conn(inode
);
431 struct fuse_file
*ff
= file
->private_data
;
432 struct fuse_req
*req
;
433 struct fuse_flush_in inarg
;
436 if (is_bad_inode(inode
))
442 err
= filemap_write_and_wait(file
->f_mapping
);
446 mutex_lock(&inode
->i_mutex
);
447 fuse_sync_writes(inode
);
448 mutex_unlock(&inode
->i_mutex
);
450 req
= fuse_get_req_nofail_nopages(fc
, file
);
451 memset(&inarg
, 0, sizeof(inarg
));
453 inarg
.lock_owner
= fuse_lock_owner_id(fc
, id
);
454 req
->in
.h
.opcode
= FUSE_FLUSH
;
455 req
->in
.h
.nodeid
= get_node_id(inode
);
457 req
->in
.args
[0].size
= sizeof(inarg
);
458 req
->in
.args
[0].value
= &inarg
;
460 fuse_request_send(fc
, req
);
461 err
= req
->out
.h
.error
;
462 fuse_put_request(fc
, req
);
463 if (err
== -ENOSYS
) {
470 int fuse_fsync_common(struct file
*file
, loff_t start
, loff_t end
,
471 int datasync
, int isdir
)
473 struct inode
*inode
= file
->f_mapping
->host
;
474 struct fuse_conn
*fc
= get_fuse_conn(inode
);
475 struct fuse_file
*ff
= file
->private_data
;
476 struct fuse_req
*req
;
477 struct fuse_fsync_in inarg
;
480 if (is_bad_inode(inode
))
483 err
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
487 if ((!isdir
&& fc
->no_fsync
) || (isdir
&& fc
->no_fsyncdir
))
490 mutex_lock(&inode
->i_mutex
);
493 * Start writeback against all dirty pages of the inode, then
494 * wait for all outstanding writes, before sending the FSYNC
497 err
= write_inode_now(inode
, 0);
501 fuse_sync_writes(inode
);
503 if (test_bit(FUSE_I_MTIME_DIRTY
, &get_fuse_inode(inode
)->state
)) {
504 int err
= fuse_flush_mtime(file
, false);
509 req
= fuse_get_req_nopages(fc
);
515 memset(&inarg
, 0, sizeof(inarg
));
517 inarg
.fsync_flags
= datasync
? 1 : 0;
518 req
->in
.h
.opcode
= isdir
? FUSE_FSYNCDIR
: FUSE_FSYNC
;
519 req
->in
.h
.nodeid
= get_node_id(inode
);
521 req
->in
.args
[0].size
= sizeof(inarg
);
522 req
->in
.args
[0].value
= &inarg
;
523 fuse_request_send(fc
, req
);
524 err
= req
->out
.h
.error
;
525 fuse_put_request(fc
, req
);
526 if (err
== -ENOSYS
) {
534 mutex_unlock(&inode
->i_mutex
);
538 static int fuse_fsync(struct file
*file
, loff_t start
, loff_t end
,
541 return fuse_fsync_common(file
, start
, end
, datasync
, 0);
544 void fuse_read_fill(struct fuse_req
*req
, struct file
*file
, loff_t pos
,
545 size_t count
, int opcode
)
547 struct fuse_read_in
*inarg
= &req
->misc
.read
.in
;
548 struct fuse_file
*ff
= file
->private_data
;
553 inarg
->flags
= file
->f_flags
;
554 req
->in
.h
.opcode
= opcode
;
555 req
->in
.h
.nodeid
= ff
->nodeid
;
557 req
->in
.args
[0].size
= sizeof(struct fuse_read_in
);
558 req
->in
.args
[0].value
= inarg
;
560 req
->out
.numargs
= 1;
561 req
->out
.args
[0].size
= count
;
564 static void fuse_release_user_pages(struct fuse_req
*req
, int write
)
568 for (i
= 0; i
< req
->num_pages
; i
++) {
569 struct page
*page
= req
->pages
[i
];
571 set_page_dirty_lock(page
);
577 * In case of short read, the caller sets 'pos' to the position of
578 * actual end of fuse request in IO request. Otherwise, if bytes_requested
579 * == bytes_transferred or rw == WRITE, the caller sets 'pos' to -1.
582 * User requested DIO read of 64K. It was splitted into two 32K fuse requests,
583 * both submitted asynchronously. The first of them was ACKed by userspace as
584 * fully completed (req->out.args[0].size == 32K) resulting in pos == -1. The
585 * second request was ACKed as short, e.g. only 1K was read, resulting in
588 * Thus, when all fuse requests are completed, the minimal non-negative 'pos'
589 * will be equal to the length of the longest contiguous fragment of
590 * transferred data starting from the beginning of IO request.
592 static void fuse_aio_complete(struct fuse_io_priv
*io
, int err
, ssize_t pos
)
596 spin_lock(&io
->lock
);
598 io
->err
= io
->err
? : err
;
599 else if (pos
>= 0 && (io
->bytes
< 0 || pos
< io
->bytes
))
603 spin_unlock(&io
->lock
);
610 else if (io
->bytes
>= 0 && io
->write
)
613 res
= io
->bytes
< 0 ? io
->size
: io
->bytes
;
615 if (!is_sync_kiocb(io
->iocb
)) {
616 struct inode
*inode
= file_inode(io
->iocb
->ki_filp
);
617 struct fuse_conn
*fc
= get_fuse_conn(inode
);
618 struct fuse_inode
*fi
= get_fuse_inode(inode
);
620 spin_lock(&fc
->lock
);
621 fi
->attr_version
= ++fc
->attr_version
;
622 spin_unlock(&fc
->lock
);
626 aio_complete(io
->iocb
, res
, 0);
631 static void fuse_aio_complete_req(struct fuse_conn
*fc
, struct fuse_req
*req
)
633 struct fuse_io_priv
*io
= req
->io
;
636 fuse_release_user_pages(req
, !io
->write
);
639 if (req
->misc
.write
.in
.size
!= req
->misc
.write
.out
.size
)
640 pos
= req
->misc
.write
.in
.offset
- io
->offset
+
641 req
->misc
.write
.out
.size
;
643 if (req
->misc
.read
.in
.size
!= req
->out
.args
[0].size
)
644 pos
= req
->misc
.read
.in
.offset
- io
->offset
+
645 req
->out
.args
[0].size
;
648 fuse_aio_complete(io
, req
->out
.h
.error
, pos
);
651 static size_t fuse_async_req_send(struct fuse_conn
*fc
, struct fuse_req
*req
,
652 size_t num_bytes
, struct fuse_io_priv
*io
)
654 spin_lock(&io
->lock
);
655 io
->size
+= num_bytes
;
657 spin_unlock(&io
->lock
);
660 req
->end
= fuse_aio_complete_req
;
662 __fuse_get_request(req
);
663 fuse_request_send_background(fc
, req
);
668 static size_t fuse_send_read(struct fuse_req
*req
, struct fuse_io_priv
*io
,
669 loff_t pos
, size_t count
, fl_owner_t owner
)
671 struct file
*file
= io
->file
;
672 struct fuse_file
*ff
= file
->private_data
;
673 struct fuse_conn
*fc
= ff
->fc
;
675 fuse_read_fill(req
, file
, pos
, count
, FUSE_READ
);
677 struct fuse_read_in
*inarg
= &req
->misc
.read
.in
;
679 inarg
->read_flags
|= FUSE_READ_LOCKOWNER
;
680 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
684 return fuse_async_req_send(fc
, req
, count
, io
);
686 fuse_request_send(fc
, req
);
687 return req
->out
.args
[0].size
;
690 static void fuse_read_update_size(struct inode
*inode
, loff_t size
,
693 struct fuse_conn
*fc
= get_fuse_conn(inode
);
694 struct fuse_inode
*fi
= get_fuse_inode(inode
);
696 spin_lock(&fc
->lock
);
697 if (attr_ver
== fi
->attr_version
&& size
< inode
->i_size
&&
698 !test_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
)) {
699 fi
->attr_version
= ++fc
->attr_version
;
700 i_size_write(inode
, size
);
702 spin_unlock(&fc
->lock
);
705 static void fuse_short_read(struct fuse_req
*req
, struct inode
*inode
,
708 size_t num_read
= req
->out
.args
[0].size
;
709 struct fuse_conn
*fc
= get_fuse_conn(inode
);
711 if (fc
->writeback_cache
) {
713 * A hole in a file. Some data after the hole are in page cache,
714 * but have not reached the client fs yet. So, the hole is not
718 int start_idx
= num_read
>> PAGE_CACHE_SHIFT
;
719 size_t off
= num_read
& (PAGE_CACHE_SIZE
- 1);
721 for (i
= start_idx
; i
< req
->num_pages
; i
++) {
722 zero_user_segment(req
->pages
[i
], off
, PAGE_CACHE_SIZE
);
726 loff_t pos
= page_offset(req
->pages
[0]) + num_read
;
727 fuse_read_update_size(inode
, pos
, attr_ver
);
731 static int fuse_do_readpage(struct file
*file
, struct page
*page
)
733 struct fuse_io_priv io
= { .async
= 0, .file
= file
};
734 struct inode
*inode
= page
->mapping
->host
;
735 struct fuse_conn
*fc
= get_fuse_conn(inode
);
736 struct fuse_req
*req
;
738 loff_t pos
= page_offset(page
);
739 size_t count
= PAGE_CACHE_SIZE
;
744 * Page writeback can extend beyond the lifetime of the
745 * page-cache page, so make sure we read a properly synced
748 fuse_wait_on_page_writeback(inode
, page
->index
);
750 req
= fuse_get_req(fc
, 1);
754 attr_ver
= fuse_get_attr_version(fc
);
756 req
->out
.page_zeroing
= 1;
757 req
->out
.argpages
= 1;
759 req
->pages
[0] = page
;
760 req
->page_descs
[0].length
= count
;
761 num_read
= fuse_send_read(req
, &io
, pos
, count
, NULL
);
762 err
= req
->out
.h
.error
;
766 * Short read means EOF. If file size is larger, truncate it
768 if (num_read
< count
)
769 fuse_short_read(req
, inode
, attr_ver
);
771 SetPageUptodate(page
);
774 fuse_put_request(fc
, req
);
779 static int fuse_readpage(struct file
*file
, struct page
*page
)
781 struct inode
*inode
= page
->mapping
->host
;
785 if (is_bad_inode(inode
))
788 err
= fuse_do_readpage(file
, page
);
789 fuse_invalidate_atime(inode
);
795 static void fuse_readpages_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
798 size_t count
= req
->misc
.read
.in
.size
;
799 size_t num_read
= req
->out
.args
[0].size
;
800 struct address_space
*mapping
= NULL
;
802 for (i
= 0; mapping
== NULL
&& i
< req
->num_pages
; i
++)
803 mapping
= req
->pages
[i
]->mapping
;
806 struct inode
*inode
= mapping
->host
;
809 * Short read means EOF. If file size is larger, truncate it
811 if (!req
->out
.h
.error
&& num_read
< count
)
812 fuse_short_read(req
, inode
, req
->misc
.read
.attr_ver
);
814 fuse_invalidate_atime(inode
);
817 for (i
= 0; i
< req
->num_pages
; i
++) {
818 struct page
*page
= req
->pages
[i
];
819 if (!req
->out
.h
.error
)
820 SetPageUptodate(page
);
824 page_cache_release(page
);
827 fuse_file_put(req
->ff
, false);
830 static void fuse_send_readpages(struct fuse_req
*req
, struct file
*file
)
832 struct fuse_file
*ff
= file
->private_data
;
833 struct fuse_conn
*fc
= ff
->fc
;
834 loff_t pos
= page_offset(req
->pages
[0]);
835 size_t count
= req
->num_pages
<< PAGE_CACHE_SHIFT
;
837 req
->out
.argpages
= 1;
838 req
->out
.page_zeroing
= 1;
839 req
->out
.page_replace
= 1;
840 fuse_read_fill(req
, file
, pos
, count
, FUSE_READ
);
841 req
->misc
.read
.attr_ver
= fuse_get_attr_version(fc
);
842 if (fc
->async_read
) {
843 req
->ff
= fuse_file_get(ff
);
844 req
->end
= fuse_readpages_end
;
845 fuse_request_send_background(fc
, req
);
847 fuse_request_send(fc
, req
);
848 fuse_readpages_end(fc
, req
);
849 fuse_put_request(fc
, req
);
853 struct fuse_fill_data
{
854 struct fuse_req
*req
;
860 static int fuse_readpages_fill(void *_data
, struct page
*page
)
862 struct fuse_fill_data
*data
= _data
;
863 struct fuse_req
*req
= data
->req
;
864 struct inode
*inode
= data
->inode
;
865 struct fuse_conn
*fc
= get_fuse_conn(inode
);
867 fuse_wait_on_page_writeback(inode
, page
->index
);
869 if (req
->num_pages
&&
870 (req
->num_pages
== FUSE_MAX_PAGES_PER_REQ
||
871 (req
->num_pages
+ 1) * PAGE_CACHE_SIZE
> fc
->max_read
||
872 req
->pages
[req
->num_pages
- 1]->index
+ 1 != page
->index
)) {
873 int nr_alloc
= min_t(unsigned, data
->nr_pages
,
874 FUSE_MAX_PAGES_PER_REQ
);
875 fuse_send_readpages(req
, data
->file
);
877 req
= fuse_get_req_for_background(fc
, nr_alloc
);
879 req
= fuse_get_req(fc
, nr_alloc
);
888 if (WARN_ON(req
->num_pages
>= req
->max_pages
)) {
889 fuse_put_request(fc
, req
);
893 page_cache_get(page
);
894 req
->pages
[req
->num_pages
] = page
;
895 req
->page_descs
[req
->num_pages
].length
= PAGE_SIZE
;
901 static int fuse_readpages(struct file
*file
, struct address_space
*mapping
,
902 struct list_head
*pages
, unsigned nr_pages
)
904 struct inode
*inode
= mapping
->host
;
905 struct fuse_conn
*fc
= get_fuse_conn(inode
);
906 struct fuse_fill_data data
;
908 int nr_alloc
= min_t(unsigned, nr_pages
, FUSE_MAX_PAGES_PER_REQ
);
911 if (is_bad_inode(inode
))
917 data
.req
= fuse_get_req_for_background(fc
, nr_alloc
);
919 data
.req
= fuse_get_req(fc
, nr_alloc
);
920 data
.nr_pages
= nr_pages
;
921 err
= PTR_ERR(data
.req
);
922 if (IS_ERR(data
.req
))
925 err
= read_cache_pages(mapping
, pages
, fuse_readpages_fill
, &data
);
927 if (data
.req
->num_pages
)
928 fuse_send_readpages(data
.req
, file
);
930 fuse_put_request(fc
, data
.req
);
936 static ssize_t
fuse_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
937 unsigned long nr_segs
, loff_t pos
)
939 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
940 struct fuse_conn
*fc
= get_fuse_conn(inode
);
943 * In auto invalidate mode, always update attributes on read.
944 * Otherwise, only update if we attempt to read past EOF (to ensure
945 * i_size is up to date).
947 if (fc
->auto_inval_data
||
948 (pos
+ iov_length(iov
, nr_segs
) > i_size_read(inode
))) {
950 err
= fuse_update_attributes(inode
, NULL
, iocb
->ki_filp
, NULL
);
955 return generic_file_aio_read(iocb
, iov
, nr_segs
, pos
);
958 static void fuse_write_fill(struct fuse_req
*req
, struct fuse_file
*ff
,
959 loff_t pos
, size_t count
)
961 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
962 struct fuse_write_out
*outarg
= &req
->misc
.write
.out
;
967 req
->in
.h
.opcode
= FUSE_WRITE
;
968 req
->in
.h
.nodeid
= ff
->nodeid
;
970 if (ff
->fc
->minor
< 9)
971 req
->in
.args
[0].size
= FUSE_COMPAT_WRITE_IN_SIZE
;
973 req
->in
.args
[0].size
= sizeof(struct fuse_write_in
);
974 req
->in
.args
[0].value
= inarg
;
975 req
->in
.args
[1].size
= count
;
976 req
->out
.numargs
= 1;
977 req
->out
.args
[0].size
= sizeof(struct fuse_write_out
);
978 req
->out
.args
[0].value
= outarg
;
981 static size_t fuse_send_write(struct fuse_req
*req
, struct fuse_io_priv
*io
,
982 loff_t pos
, size_t count
, fl_owner_t owner
)
984 struct file
*file
= io
->file
;
985 struct fuse_file
*ff
= file
->private_data
;
986 struct fuse_conn
*fc
= ff
->fc
;
987 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
989 fuse_write_fill(req
, ff
, pos
, count
);
990 inarg
->flags
= file
->f_flags
;
992 inarg
->write_flags
|= FUSE_WRITE_LOCKOWNER
;
993 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
997 return fuse_async_req_send(fc
, req
, count
, io
);
999 fuse_request_send(fc
, req
);
1000 return req
->misc
.write
.out
.size
;
1003 bool fuse_write_update_size(struct inode
*inode
, loff_t pos
)
1005 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1006 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1009 spin_lock(&fc
->lock
);
1010 fi
->attr_version
= ++fc
->attr_version
;
1011 if (pos
> inode
->i_size
) {
1012 i_size_write(inode
, pos
);
1015 spin_unlock(&fc
->lock
);
1020 static size_t fuse_send_write_pages(struct fuse_req
*req
, struct file
*file
,
1021 struct inode
*inode
, loff_t pos
,
1027 struct fuse_io_priv io
= { .async
= 0, .file
= file
};
1029 for (i
= 0; i
< req
->num_pages
; i
++)
1030 fuse_wait_on_page_writeback(inode
, req
->pages
[i
]->index
);
1032 res
= fuse_send_write(req
, &io
, pos
, count
, NULL
);
1034 offset
= req
->page_descs
[0].offset
;
1036 for (i
= 0; i
< req
->num_pages
; i
++) {
1037 struct page
*page
= req
->pages
[i
];
1039 if (!req
->out
.h
.error
&& !offset
&& count
>= PAGE_CACHE_SIZE
)
1040 SetPageUptodate(page
);
1042 if (count
> PAGE_CACHE_SIZE
- offset
)
1043 count
-= PAGE_CACHE_SIZE
- offset
;
1049 page_cache_release(page
);
1055 static ssize_t
fuse_fill_write_pages(struct fuse_req
*req
,
1056 struct address_space
*mapping
,
1057 struct iov_iter
*ii
, loff_t pos
)
1059 struct fuse_conn
*fc
= get_fuse_conn(mapping
->host
);
1060 unsigned offset
= pos
& (PAGE_CACHE_SIZE
- 1);
1064 req
->in
.argpages
= 1;
1065 req
->page_descs
[0].offset
= offset
;
1070 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
1071 size_t bytes
= min_t(size_t, PAGE_CACHE_SIZE
- offset
,
1072 iov_iter_count(ii
));
1074 bytes
= min_t(size_t, bytes
, fc
->max_write
- count
);
1078 if (iov_iter_fault_in_readable(ii
, bytes
))
1082 page
= grab_cache_page_write_begin(mapping
, index
, 0);
1086 if (mapping_writably_mapped(mapping
))
1087 flush_dcache_page(page
);
1089 tmp
= iov_iter_copy_from_user_atomic(page
, ii
, offset
, bytes
);
1090 flush_dcache_page(page
);
1092 mark_page_accessed(page
);
1096 page_cache_release(page
);
1097 bytes
= min(bytes
, iov_iter_single_seg_count(ii
));
1102 req
->pages
[req
->num_pages
] = page
;
1103 req
->page_descs
[req
->num_pages
].length
= tmp
;
1106 iov_iter_advance(ii
, tmp
);
1110 if (offset
== PAGE_CACHE_SIZE
)
1113 if (!fc
->big_writes
)
1115 } while (iov_iter_count(ii
) && count
< fc
->max_write
&&
1116 req
->num_pages
< req
->max_pages
&& offset
== 0);
1118 return count
> 0 ? count
: err
;
1121 static inline unsigned fuse_wr_pages(loff_t pos
, size_t len
)
1123 return min_t(unsigned,
1124 ((pos
+ len
- 1) >> PAGE_CACHE_SHIFT
) -
1125 (pos
>> PAGE_CACHE_SHIFT
) + 1,
1126 FUSE_MAX_PAGES_PER_REQ
);
1129 static ssize_t
fuse_perform_write(struct file
*file
,
1130 struct address_space
*mapping
,
1131 struct iov_iter
*ii
, loff_t pos
)
1133 struct inode
*inode
= mapping
->host
;
1134 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1135 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1139 if (is_bad_inode(inode
))
1142 if (inode
->i_size
< pos
+ iov_iter_count(ii
))
1143 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1146 struct fuse_req
*req
;
1148 unsigned nr_pages
= fuse_wr_pages(pos
, iov_iter_count(ii
));
1150 req
= fuse_get_req(fc
, nr_pages
);
1156 count
= fuse_fill_write_pages(req
, mapping
, ii
, pos
);
1162 num_written
= fuse_send_write_pages(req
, file
, inode
,
1164 err
= req
->out
.h
.error
;
1169 /* break out of the loop on short write */
1170 if (num_written
!= count
)
1174 fuse_put_request(fc
, req
);
1175 } while (!err
&& iov_iter_count(ii
));
1178 fuse_write_update_size(inode
, pos
);
1180 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
1181 fuse_invalidate_attr(inode
);
1183 return res
> 0 ? res
: err
;
1186 static ssize_t
fuse_file_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
1187 unsigned long nr_segs
, loff_t pos
)
1189 struct file
*file
= iocb
->ki_filp
;
1190 struct address_space
*mapping
= file
->f_mapping
;
1193 ssize_t written
= 0;
1194 ssize_t written_buffered
= 0;
1195 struct inode
*inode
= mapping
->host
;
1200 if (get_fuse_conn(inode
)->writeback_cache
) {
1201 /* Update size (EOF optimization) and mode (SUID clearing) */
1202 err
= fuse_update_attributes(mapping
->host
, NULL
, file
, NULL
);
1206 return generic_file_aio_write(iocb
, iov
, nr_segs
, pos
);
1209 WARN_ON(iocb
->ki_pos
!= pos
);
1212 err
= generic_segment_checks(iov
, &nr_segs
, &ocount
, VERIFY_READ
);
1217 mutex_lock(&inode
->i_mutex
);
1219 /* We can write back this queue in page reclaim */
1220 current
->backing_dev_info
= mapping
->backing_dev_info
;
1222 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
1229 err
= file_remove_suid(file
);
1233 err
= file_update_time(file
);
1237 if (file
->f_flags
& O_DIRECT
) {
1238 written
= generic_file_direct_write(iocb
, iov
, &nr_segs
, pos
,
1240 if (written
< 0 || written
== count
)
1246 iov_iter_init(&i
, iov
, nr_segs
, count
, written
);
1247 written_buffered
= fuse_perform_write(file
, mapping
, &i
, pos
);
1248 if (written_buffered
< 0) {
1249 err
= written_buffered
;
1252 endbyte
= pos
+ written_buffered
- 1;
1254 err
= filemap_write_and_wait_range(file
->f_mapping
, pos
,
1259 invalidate_mapping_pages(file
->f_mapping
,
1260 pos
>> PAGE_CACHE_SHIFT
,
1261 endbyte
>> PAGE_CACHE_SHIFT
);
1263 written
+= written_buffered
;
1264 iocb
->ki_pos
= pos
+ written_buffered
;
1266 iov_iter_init(&i
, iov
, nr_segs
, count
, 0);
1267 written
= fuse_perform_write(file
, mapping
, &i
, pos
);
1269 iocb
->ki_pos
= pos
+ written
;
1272 current
->backing_dev_info
= NULL
;
1273 mutex_unlock(&inode
->i_mutex
);
1275 return written
? written
: err
;
1278 static inline void fuse_page_descs_length_init(struct fuse_req
*req
,
1279 unsigned index
, unsigned nr_pages
)
1283 for (i
= index
; i
< index
+ nr_pages
; i
++)
1284 req
->page_descs
[i
].length
= PAGE_SIZE
-
1285 req
->page_descs
[i
].offset
;
1288 static inline unsigned long fuse_get_user_addr(const struct iov_iter
*ii
)
1290 return (unsigned long)ii
->iov
->iov_base
+ ii
->iov_offset
;
1293 static inline size_t fuse_get_frag_size(const struct iov_iter
*ii
,
1296 return min(iov_iter_single_seg_count(ii
), max_size
);
1299 static int fuse_get_user_pages(struct fuse_req
*req
, struct iov_iter
*ii
,
1300 size_t *nbytesp
, int write
)
1302 size_t nbytes
= 0; /* # bytes already packed in req */
1304 /* Special case for kernel I/O: can copy directly into the buffer */
1305 if (segment_eq(get_fs(), KERNEL_DS
)) {
1306 unsigned long user_addr
= fuse_get_user_addr(ii
);
1307 size_t frag_size
= fuse_get_frag_size(ii
, *nbytesp
);
1310 req
->in
.args
[1].value
= (void *) user_addr
;
1312 req
->out
.args
[0].value
= (void *) user_addr
;
1314 iov_iter_advance(ii
, frag_size
);
1315 *nbytesp
= frag_size
;
1319 while (nbytes
< *nbytesp
&& req
->num_pages
< req
->max_pages
) {
1321 unsigned long user_addr
= fuse_get_user_addr(ii
);
1322 unsigned offset
= user_addr
& ~PAGE_MASK
;
1323 size_t frag_size
= fuse_get_frag_size(ii
, *nbytesp
- nbytes
);
1326 unsigned n
= req
->max_pages
- req
->num_pages
;
1327 frag_size
= min_t(size_t, frag_size
, n
<< PAGE_SHIFT
);
1329 npages
= (frag_size
+ offset
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1330 npages
= clamp(npages
, 1U, n
);
1332 ret
= get_user_pages_fast(user_addr
, npages
, !write
,
1333 &req
->pages
[req
->num_pages
]);
1338 frag_size
= min_t(size_t, frag_size
,
1339 (npages
<< PAGE_SHIFT
) - offset
);
1340 iov_iter_advance(ii
, frag_size
);
1342 req
->page_descs
[req
->num_pages
].offset
= offset
;
1343 fuse_page_descs_length_init(req
, req
->num_pages
, npages
);
1345 req
->num_pages
+= npages
;
1346 req
->page_descs
[req
->num_pages
- 1].length
-=
1347 (npages
<< PAGE_SHIFT
) - offset
- frag_size
;
1349 nbytes
+= frag_size
;
1353 req
->in
.argpages
= 1;
1355 req
->out
.argpages
= 1;
1362 static inline int fuse_iter_npages(const struct iov_iter
*ii_p
)
1364 struct iov_iter ii
= *ii_p
;
1367 while (iov_iter_count(&ii
) && npages
< FUSE_MAX_PAGES_PER_REQ
) {
1368 unsigned long user_addr
= fuse_get_user_addr(&ii
);
1369 unsigned offset
= user_addr
& ~PAGE_MASK
;
1370 size_t frag_size
= iov_iter_single_seg_count(&ii
);
1372 npages
+= (frag_size
+ offset
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1373 iov_iter_advance(&ii
, frag_size
);
1376 return min(npages
, FUSE_MAX_PAGES_PER_REQ
);
1379 ssize_t
fuse_direct_io(struct fuse_io_priv
*io
, const struct iovec
*iov
,
1380 unsigned long nr_segs
, size_t count
, loff_t
*ppos
,
1383 int write
= flags
& FUSE_DIO_WRITE
;
1384 int cuse
= flags
& FUSE_DIO_CUSE
;
1385 struct file
*file
= io
->file
;
1386 struct inode
*inode
= file
->f_mapping
->host
;
1387 struct fuse_file
*ff
= file
->private_data
;
1388 struct fuse_conn
*fc
= ff
->fc
;
1389 size_t nmax
= write
? fc
->max_write
: fc
->max_read
;
1391 pgoff_t idx_from
= pos
>> PAGE_CACHE_SHIFT
;
1392 pgoff_t idx_to
= (pos
+ count
- 1) >> PAGE_CACHE_SHIFT
;
1394 struct fuse_req
*req
;
1397 iov_iter_init(&ii
, iov
, nr_segs
, count
, 0);
1400 req
= fuse_get_req_for_background(fc
, fuse_iter_npages(&ii
));
1402 req
= fuse_get_req(fc
, fuse_iter_npages(&ii
));
1404 return PTR_ERR(req
);
1406 if (!cuse
&& fuse_range_is_writeback(inode
, idx_from
, idx_to
)) {
1408 mutex_lock(&inode
->i_mutex
);
1409 fuse_sync_writes(inode
);
1411 mutex_unlock(&inode
->i_mutex
);
1416 fl_owner_t owner
= current
->files
;
1417 size_t nbytes
= min(count
, nmax
);
1418 int err
= fuse_get_user_pages(req
, &ii
, &nbytes
, write
);
1425 nres
= fuse_send_write(req
, io
, pos
, nbytes
, owner
);
1427 nres
= fuse_send_read(req
, io
, pos
, nbytes
, owner
);
1430 fuse_release_user_pages(req
, !write
);
1431 if (req
->out
.h
.error
) {
1433 res
= req
->out
.h
.error
;
1435 } else if (nres
> nbytes
) {
1445 fuse_put_request(fc
, req
);
1447 req
= fuse_get_req_for_background(fc
,
1448 fuse_iter_npages(&ii
));
1450 req
= fuse_get_req(fc
, fuse_iter_npages(&ii
));
1456 fuse_put_request(fc
, req
);
1462 EXPORT_SYMBOL_GPL(fuse_direct_io
);
1464 static ssize_t
__fuse_direct_read(struct fuse_io_priv
*io
,
1465 const struct iovec
*iov
,
1466 unsigned long nr_segs
, loff_t
*ppos
,
1470 struct file
*file
= io
->file
;
1471 struct inode
*inode
= file_inode(file
);
1473 if (is_bad_inode(inode
))
1476 res
= fuse_direct_io(io
, iov
, nr_segs
, count
, ppos
, 0);
1478 fuse_invalidate_attr(inode
);
1483 static ssize_t
fuse_direct_read(struct file
*file
, char __user
*buf
,
1484 size_t count
, loff_t
*ppos
)
1486 struct fuse_io_priv io
= { .async
= 0, .file
= file
};
1487 struct iovec iov
= { .iov_base
= buf
, .iov_len
= count
};
1488 return __fuse_direct_read(&io
, &iov
, 1, ppos
, count
);
1491 static ssize_t
__fuse_direct_write(struct fuse_io_priv
*io
,
1492 const struct iovec
*iov
,
1493 unsigned long nr_segs
, loff_t
*ppos
)
1495 struct file
*file
= io
->file
;
1496 struct inode
*inode
= file_inode(file
);
1497 size_t count
= iov_length(iov
, nr_segs
);
1500 res
= generic_write_checks(file
, ppos
, &count
, 0);
1502 res
= fuse_direct_io(io
, iov
, nr_segs
, count
, ppos
,
1505 fuse_invalidate_attr(inode
);
1510 static ssize_t
fuse_direct_write(struct file
*file
, const char __user
*buf
,
1511 size_t count
, loff_t
*ppos
)
1513 struct iovec iov
= { .iov_base
= (void __user
*)buf
, .iov_len
= count
};
1514 struct inode
*inode
= file_inode(file
);
1516 struct fuse_io_priv io
= { .async
= 0, .file
= file
};
1518 if (is_bad_inode(inode
))
1521 /* Don't allow parallel writes to the same file */
1522 mutex_lock(&inode
->i_mutex
);
1523 res
= __fuse_direct_write(&io
, &iov
, 1, ppos
);
1525 fuse_write_update_size(inode
, *ppos
);
1526 mutex_unlock(&inode
->i_mutex
);
1531 static void fuse_writepage_free(struct fuse_conn
*fc
, struct fuse_req
*req
)
1535 for (i
= 0; i
< req
->num_pages
; i
++)
1536 __free_page(req
->pages
[i
]);
1539 fuse_file_put(req
->ff
, false);
1542 static void fuse_writepage_finish(struct fuse_conn
*fc
, struct fuse_req
*req
)
1544 struct inode
*inode
= req
->inode
;
1545 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1546 struct backing_dev_info
*bdi
= inode
->i_mapping
->backing_dev_info
;
1549 list_del(&req
->writepages_entry
);
1550 for (i
= 0; i
< req
->num_pages
; i
++) {
1551 dec_bdi_stat(bdi
, BDI_WRITEBACK
);
1552 dec_zone_page_state(req
->pages
[i
], NR_WRITEBACK_TEMP
);
1553 bdi_writeout_inc(bdi
);
1555 wake_up(&fi
->page_waitq
);
1558 /* Called under fc->lock, may release and reacquire it */
1559 static void fuse_send_writepage(struct fuse_conn
*fc
, struct fuse_req
*req
,
1561 __releases(fc
->lock
)
1562 __acquires(fc
->lock
)
1564 struct fuse_inode
*fi
= get_fuse_inode(req
->inode
);
1565 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
1566 __u64 data_size
= req
->num_pages
* PAGE_CACHE_SIZE
;
1571 if (inarg
->offset
+ data_size
<= size
) {
1572 inarg
->size
= data_size
;
1573 } else if (inarg
->offset
< size
) {
1574 inarg
->size
= size
- inarg
->offset
;
1576 /* Got truncated off completely */
1580 req
->in
.args
[1].size
= inarg
->size
;
1582 fuse_request_send_background_locked(fc
, req
);
1586 fuse_writepage_finish(fc
, req
);
1587 spin_unlock(&fc
->lock
);
1588 fuse_writepage_free(fc
, req
);
1589 fuse_put_request(fc
, req
);
1590 spin_lock(&fc
->lock
);
1594 * If fi->writectr is positive (no truncate or fsync going on) send
1595 * all queued writepage requests.
1597 * Called with fc->lock
1599 void fuse_flush_writepages(struct inode
*inode
)
1600 __releases(fc
->lock
)
1601 __acquires(fc
->lock
)
1603 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1604 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1605 size_t crop
= i_size_read(inode
);
1606 struct fuse_req
*req
;
1608 while (fi
->writectr
>= 0 && !list_empty(&fi
->queued_writes
)) {
1609 req
= list_entry(fi
->queued_writes
.next
, struct fuse_req
, list
);
1610 list_del_init(&req
->list
);
1611 fuse_send_writepage(fc
, req
, crop
);
1615 static void fuse_writepage_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
1617 struct inode
*inode
= req
->inode
;
1618 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1620 mapping_set_error(inode
->i_mapping
, req
->out
.h
.error
);
1621 spin_lock(&fc
->lock
);
1622 while (req
->misc
.write
.next
) {
1623 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1624 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
1625 struct fuse_req
*next
= req
->misc
.write
.next
;
1626 req
->misc
.write
.next
= next
->misc
.write
.next
;
1627 next
->misc
.write
.next
= NULL
;
1628 next
->ff
= fuse_file_get(req
->ff
);
1629 list_add(&next
->writepages_entry
, &fi
->writepages
);
1632 * Skip fuse_flush_writepages() to make it easy to crop requests
1633 * based on primary request size.
1635 * 1st case (trivial): there are no concurrent activities using
1636 * fuse_set/release_nowrite. Then we're on safe side because
1637 * fuse_flush_writepages() would call fuse_send_writepage()
1640 * 2nd case: someone called fuse_set_nowrite and it is waiting
1641 * now for completion of all in-flight requests. This happens
1642 * rarely and no more than once per page, so this should be
1645 * 3rd case: someone (e.g. fuse_do_setattr()) is in the middle
1646 * of fuse_set_nowrite..fuse_release_nowrite section. The fact
1647 * that fuse_set_nowrite returned implies that all in-flight
1648 * requests were completed along with all of their secondary
1649 * requests. Further primary requests are blocked by negative
1650 * writectr. Hence there cannot be any in-flight requests and
1651 * no invocations of fuse_writepage_end() while we're in
1652 * fuse_set_nowrite..fuse_release_nowrite section.
1654 fuse_send_writepage(fc
, next
, inarg
->offset
+ inarg
->size
);
1657 fuse_writepage_finish(fc
, req
);
1658 spin_unlock(&fc
->lock
);
1659 fuse_writepage_free(fc
, req
);
1662 static struct fuse_file
*fuse_write_file_get(struct fuse_conn
*fc
,
1663 struct fuse_inode
*fi
)
1665 struct fuse_file
*ff
= NULL
;
1667 spin_lock(&fc
->lock
);
1668 if (!WARN_ON(list_empty(&fi
->write_files
))) {
1669 ff
= list_entry(fi
->write_files
.next
, struct fuse_file
,
1673 spin_unlock(&fc
->lock
);
1678 static int fuse_writepage_locked(struct page
*page
)
1680 struct address_space
*mapping
= page
->mapping
;
1681 struct inode
*inode
= mapping
->host
;
1682 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1683 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1684 struct fuse_req
*req
;
1685 struct page
*tmp_page
;
1686 int error
= -ENOMEM
;
1688 set_page_writeback(page
);
1690 req
= fuse_request_alloc_nofs(1);
1694 req
->background
= 1; /* writeback always goes to bg_queue */
1695 tmp_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
1700 req
->ff
= fuse_write_file_get(fc
, fi
);
1704 fuse_write_fill(req
, req
->ff
, page_offset(page
), 0);
1706 copy_highpage(tmp_page
, page
);
1707 req
->misc
.write
.in
.write_flags
|= FUSE_WRITE_CACHE
;
1708 req
->misc
.write
.next
= NULL
;
1709 req
->in
.argpages
= 1;
1711 req
->pages
[0] = tmp_page
;
1712 req
->page_descs
[0].offset
= 0;
1713 req
->page_descs
[0].length
= PAGE_SIZE
;
1714 req
->end
= fuse_writepage_end
;
1717 inc_bdi_stat(mapping
->backing_dev_info
, BDI_WRITEBACK
);
1718 inc_zone_page_state(tmp_page
, NR_WRITEBACK_TEMP
);
1720 spin_lock(&fc
->lock
);
1721 list_add(&req
->writepages_entry
, &fi
->writepages
);
1722 list_add_tail(&req
->list
, &fi
->queued_writes
);
1723 fuse_flush_writepages(inode
);
1724 spin_unlock(&fc
->lock
);
1726 end_page_writeback(page
);
1731 fuse_request_free(req
);
1733 end_page_writeback(page
);
1737 static int fuse_writepage(struct page
*page
, struct writeback_control
*wbc
)
1741 if (fuse_page_is_writeback(page
->mapping
->host
, page
->index
)) {
1743 * ->writepages() should be called for sync() and friends. We
1744 * should only get here on direct reclaim and then we are
1745 * allowed to skip a page which is already in flight
1747 WARN_ON(wbc
->sync_mode
== WB_SYNC_ALL
);
1749 redirty_page_for_writepage(wbc
, page
);
1753 err
= fuse_writepage_locked(page
);
1759 struct fuse_fill_wb_data
{
1760 struct fuse_req
*req
;
1761 struct fuse_file
*ff
;
1762 struct inode
*inode
;
1763 struct page
**orig_pages
;
1766 static void fuse_writepages_send(struct fuse_fill_wb_data
*data
)
1768 struct fuse_req
*req
= data
->req
;
1769 struct inode
*inode
= data
->inode
;
1770 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1771 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1772 int num_pages
= req
->num_pages
;
1775 req
->ff
= fuse_file_get(data
->ff
);
1776 spin_lock(&fc
->lock
);
1777 list_add_tail(&req
->list
, &fi
->queued_writes
);
1778 fuse_flush_writepages(inode
);
1779 spin_unlock(&fc
->lock
);
1781 for (i
= 0; i
< num_pages
; i
++)
1782 end_page_writeback(data
->orig_pages
[i
]);
1785 static bool fuse_writepage_in_flight(struct fuse_req
*new_req
,
1788 struct fuse_conn
*fc
= get_fuse_conn(new_req
->inode
);
1789 struct fuse_inode
*fi
= get_fuse_inode(new_req
->inode
);
1790 struct fuse_req
*tmp
;
1791 struct fuse_req
*old_req
;
1795 BUG_ON(new_req
->num_pages
!= 0);
1797 spin_lock(&fc
->lock
);
1798 list_del(&new_req
->writepages_entry
);
1799 list_for_each_entry(old_req
, &fi
->writepages
, writepages_entry
) {
1800 BUG_ON(old_req
->inode
!= new_req
->inode
);
1801 curr_index
= old_req
->misc
.write
.in
.offset
>> PAGE_CACHE_SHIFT
;
1802 if (curr_index
<= page
->index
&&
1803 page
->index
< curr_index
+ old_req
->num_pages
) {
1809 list_add(&new_req
->writepages_entry
, &fi
->writepages
);
1813 new_req
->num_pages
= 1;
1814 for (tmp
= old_req
; tmp
!= NULL
; tmp
= tmp
->misc
.write
.next
) {
1815 BUG_ON(tmp
->inode
!= new_req
->inode
);
1816 curr_index
= tmp
->misc
.write
.in
.offset
>> PAGE_CACHE_SHIFT
;
1817 if (tmp
->num_pages
== 1 &&
1818 curr_index
== page
->index
) {
1823 if (old_req
->num_pages
== 1 && (old_req
->state
== FUSE_REQ_INIT
||
1824 old_req
->state
== FUSE_REQ_PENDING
)) {
1825 struct backing_dev_info
*bdi
= page
->mapping
->backing_dev_info
;
1827 copy_highpage(old_req
->pages
[0], page
);
1828 spin_unlock(&fc
->lock
);
1830 dec_bdi_stat(bdi
, BDI_WRITEBACK
);
1831 dec_zone_page_state(page
, NR_WRITEBACK_TEMP
);
1832 bdi_writeout_inc(bdi
);
1833 fuse_writepage_free(fc
, new_req
);
1834 fuse_request_free(new_req
);
1837 new_req
->misc
.write
.next
= old_req
->misc
.write
.next
;
1838 old_req
->misc
.write
.next
= new_req
;
1841 spin_unlock(&fc
->lock
);
1846 static int fuse_writepages_fill(struct page
*page
,
1847 struct writeback_control
*wbc
, void *_data
)
1849 struct fuse_fill_wb_data
*data
= _data
;
1850 struct fuse_req
*req
= data
->req
;
1851 struct inode
*inode
= data
->inode
;
1852 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1853 struct page
*tmp_page
;
1859 data
->ff
= fuse_write_file_get(fc
, get_fuse_inode(inode
));
1865 * Being under writeback is unlikely but possible. For example direct
1866 * read to an mmaped fuse file will set the page dirty twice; once when
1867 * the pages are faulted with get_user_pages(), and then after the read
1870 is_writeback
= fuse_page_is_writeback(inode
, page
->index
);
1872 if (req
&& req
->num_pages
&&
1873 (is_writeback
|| req
->num_pages
== FUSE_MAX_PAGES_PER_REQ
||
1874 (req
->num_pages
+ 1) * PAGE_CACHE_SIZE
> fc
->max_write
||
1875 data
->orig_pages
[req
->num_pages
- 1]->index
+ 1 != page
->index
)) {
1876 fuse_writepages_send(data
);
1880 tmp_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
1885 * The page must not be redirtied until the writeout is completed
1886 * (i.e. userspace has sent a reply to the write request). Otherwise
1887 * there could be more than one temporary page instance for each real
1890 * This is ensured by holding the page lock in page_mkwrite() while
1891 * checking fuse_page_is_writeback(). We already hold the page lock
1892 * since clear_page_dirty_for_io() and keep it held until we add the
1893 * request to the fi->writepages list and increment req->num_pages.
1894 * After this fuse_page_is_writeback() will indicate that the page is
1895 * under writeback, so we can release the page lock.
1897 if (data
->req
== NULL
) {
1898 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1901 req
= fuse_request_alloc_nofs(FUSE_MAX_PAGES_PER_REQ
);
1903 __free_page(tmp_page
);
1907 fuse_write_fill(req
, data
->ff
, page_offset(page
), 0);
1908 req
->misc
.write
.in
.write_flags
|= FUSE_WRITE_CACHE
;
1909 req
->misc
.write
.next
= NULL
;
1910 req
->in
.argpages
= 1;
1911 req
->background
= 1;
1913 req
->end
= fuse_writepage_end
;
1916 spin_lock(&fc
->lock
);
1917 list_add(&req
->writepages_entry
, &fi
->writepages
);
1918 spin_unlock(&fc
->lock
);
1922 set_page_writeback(page
);
1924 copy_highpage(tmp_page
, page
);
1925 req
->pages
[req
->num_pages
] = tmp_page
;
1926 req
->page_descs
[req
->num_pages
].offset
= 0;
1927 req
->page_descs
[req
->num_pages
].length
= PAGE_SIZE
;
1929 inc_bdi_stat(page
->mapping
->backing_dev_info
, BDI_WRITEBACK
);
1930 inc_zone_page_state(tmp_page
, NR_WRITEBACK_TEMP
);
1933 if (is_writeback
&& fuse_writepage_in_flight(req
, page
)) {
1934 end_page_writeback(page
);
1938 data
->orig_pages
[req
->num_pages
] = page
;
1941 * Protected by fc->lock against concurrent access by
1942 * fuse_page_is_writeback().
1944 spin_lock(&fc
->lock
);
1946 spin_unlock(&fc
->lock
);
1954 static int fuse_writepages(struct address_space
*mapping
,
1955 struct writeback_control
*wbc
)
1957 struct inode
*inode
= mapping
->host
;
1958 struct fuse_fill_wb_data data
;
1962 if (is_bad_inode(inode
))
1970 data
.orig_pages
= kzalloc(sizeof(struct page
*) *
1971 FUSE_MAX_PAGES_PER_REQ
,
1973 if (!data
.orig_pages
)
1976 err
= write_cache_pages(mapping
, wbc
, fuse_writepages_fill
, &data
);
1978 /* Ignore errors if we can write at least one page */
1979 BUG_ON(!data
.req
->num_pages
);
1980 fuse_writepages_send(&data
);
1984 fuse_file_put(data
.ff
, false);
1986 kfree(data
.orig_pages
);
1992 * It's worthy to make sure that space is reserved on disk for the write,
1993 * but how to implement it without killing performance need more thinking.
1995 static int fuse_write_begin(struct file
*file
, struct address_space
*mapping
,
1996 loff_t pos
, unsigned len
, unsigned flags
,
1997 struct page
**pagep
, void **fsdata
)
1999 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
2000 struct fuse_conn
*fc
= get_fuse_conn(file
->f_dentry
->d_inode
);
2005 WARN_ON(!fc
->writeback_cache
);
2007 page
= grab_cache_page_write_begin(mapping
, index
, flags
);
2011 fuse_wait_on_page_writeback(mapping
->host
, page
->index
);
2013 if (PageUptodate(page
) || len
== PAGE_CACHE_SIZE
)
2016 * Check if the start this page comes after the end of file, in which
2017 * case the readpage can be optimized away.
2019 fsize
= i_size_read(mapping
->host
);
2020 if (fsize
<= (pos
& PAGE_CACHE_MASK
)) {
2021 size_t off
= pos
& ~PAGE_CACHE_MASK
;
2023 zero_user_segment(page
, 0, off
);
2026 err
= fuse_do_readpage(file
, page
);
2035 page_cache_release(page
);
2040 static int fuse_write_end(struct file
*file
, struct address_space
*mapping
,
2041 loff_t pos
, unsigned len
, unsigned copied
,
2042 struct page
*page
, void *fsdata
)
2044 struct inode
*inode
= page
->mapping
->host
;
2046 if (!PageUptodate(page
)) {
2047 /* Zero any unwritten bytes at the end of the page */
2048 size_t endoff
= (pos
+ copied
) & ~PAGE_CACHE_MASK
;
2050 zero_user_segment(page
, endoff
, PAGE_CACHE_SIZE
);
2051 SetPageUptodate(page
);
2054 fuse_write_update_size(inode
, pos
+ copied
);
2055 set_page_dirty(page
);
2057 page_cache_release(page
);
2062 static int fuse_launder_page(struct page
*page
)
2065 if (clear_page_dirty_for_io(page
)) {
2066 struct inode
*inode
= page
->mapping
->host
;
2067 err
= fuse_writepage_locked(page
);
2069 fuse_wait_on_page_writeback(inode
, page
->index
);
2075 * Write back dirty pages now, because there may not be any suitable
2078 static void fuse_vma_close(struct vm_area_struct
*vma
)
2080 filemap_write_and_wait(vma
->vm_file
->f_mapping
);
2084 * Wait for writeback against this page to complete before allowing it
2085 * to be marked dirty again, and hence written back again, possibly
2086 * before the previous writepage completed.
2088 * Block here, instead of in ->writepage(), so that the userspace fs
2089 * can only block processes actually operating on the filesystem.
2091 * Otherwise unprivileged userspace fs would be able to block
2096 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
2098 static int fuse_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
2100 struct page
*page
= vmf
->page
;
2101 struct inode
*inode
= file_inode(vma
->vm_file
);
2103 file_update_time(vma
->vm_file
);
2105 if (page
->mapping
!= inode
->i_mapping
) {
2107 return VM_FAULT_NOPAGE
;
2110 fuse_wait_on_page_writeback(inode
, page
->index
);
2111 return VM_FAULT_LOCKED
;
2114 static const struct vm_operations_struct fuse_file_vm_ops
= {
2115 .close
= fuse_vma_close
,
2116 .fault
= filemap_fault
,
2117 .map_pages
= filemap_map_pages
,
2118 .page_mkwrite
= fuse_page_mkwrite
,
2119 .remap_pages
= generic_file_remap_pages
,
2122 static int fuse_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2124 if ((vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
))
2125 fuse_link_write_file(file
);
2127 file_accessed(file
);
2128 vma
->vm_ops
= &fuse_file_vm_ops
;
2132 static int fuse_direct_mmap(struct file
*file
, struct vm_area_struct
*vma
)
2134 /* Can't provide the coherency needed for MAP_SHARED */
2135 if (vma
->vm_flags
& VM_MAYSHARE
)
2138 invalidate_inode_pages2(file
->f_mapping
);
2140 return generic_file_mmap(file
, vma
);
2143 static int convert_fuse_file_lock(const struct fuse_file_lock
*ffl
,
2144 struct file_lock
*fl
)
2146 switch (ffl
->type
) {
2152 if (ffl
->start
> OFFSET_MAX
|| ffl
->end
> OFFSET_MAX
||
2153 ffl
->end
< ffl
->start
)
2156 fl
->fl_start
= ffl
->start
;
2157 fl
->fl_end
= ffl
->end
;
2158 fl
->fl_pid
= ffl
->pid
;
2164 fl
->fl_type
= ffl
->type
;
2168 static void fuse_lk_fill(struct fuse_req
*req
, struct file
*file
,
2169 const struct file_lock
*fl
, int opcode
, pid_t pid
,
2172 struct inode
*inode
= file_inode(file
);
2173 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2174 struct fuse_file
*ff
= file
->private_data
;
2175 struct fuse_lk_in
*arg
= &req
->misc
.lk_in
;
2178 arg
->owner
= fuse_lock_owner_id(fc
, fl
->fl_owner
);
2179 arg
->lk
.start
= fl
->fl_start
;
2180 arg
->lk
.end
= fl
->fl_end
;
2181 arg
->lk
.type
= fl
->fl_type
;
2184 arg
->lk_flags
|= FUSE_LK_FLOCK
;
2185 req
->in
.h
.opcode
= opcode
;
2186 req
->in
.h
.nodeid
= get_node_id(inode
);
2187 req
->in
.numargs
= 1;
2188 req
->in
.args
[0].size
= sizeof(*arg
);
2189 req
->in
.args
[0].value
= arg
;
2192 static int fuse_getlk(struct file
*file
, struct file_lock
*fl
)
2194 struct inode
*inode
= file_inode(file
);
2195 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2196 struct fuse_req
*req
;
2197 struct fuse_lk_out outarg
;
2200 req
= fuse_get_req_nopages(fc
);
2202 return PTR_ERR(req
);
2204 fuse_lk_fill(req
, file
, fl
, FUSE_GETLK
, 0, 0);
2205 req
->out
.numargs
= 1;
2206 req
->out
.args
[0].size
= sizeof(outarg
);
2207 req
->out
.args
[0].value
= &outarg
;
2208 fuse_request_send(fc
, req
);
2209 err
= req
->out
.h
.error
;
2210 fuse_put_request(fc
, req
);
2212 err
= convert_fuse_file_lock(&outarg
.lk
, fl
);
2217 static int fuse_setlk(struct file
*file
, struct file_lock
*fl
, int flock
)
2219 struct inode
*inode
= file_inode(file
);
2220 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2221 struct fuse_req
*req
;
2222 int opcode
= (fl
->fl_flags
& FL_SLEEP
) ? FUSE_SETLKW
: FUSE_SETLK
;
2223 pid_t pid
= fl
->fl_type
!= F_UNLCK
? current
->tgid
: 0;
2226 if (fl
->fl_lmops
&& fl
->fl_lmops
->lm_grant
) {
2227 /* NLM needs asynchronous locks, which we don't support yet */
2231 /* Unlock on close is handled by the flush method */
2232 if (fl
->fl_flags
& FL_CLOSE
)
2235 req
= fuse_get_req_nopages(fc
);
2237 return PTR_ERR(req
);
2239 fuse_lk_fill(req
, file
, fl
, opcode
, pid
, flock
);
2240 fuse_request_send(fc
, req
);
2241 err
= req
->out
.h
.error
;
2242 /* locking is restartable */
2245 fuse_put_request(fc
, req
);
2249 static int fuse_file_lock(struct file
*file
, int cmd
, struct file_lock
*fl
)
2251 struct inode
*inode
= file_inode(file
);
2252 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2255 if (cmd
== F_CANCELLK
) {
2257 } else if (cmd
== F_GETLK
) {
2259 posix_test_lock(file
, fl
);
2262 err
= fuse_getlk(file
, fl
);
2265 err
= posix_lock_file(file
, fl
, NULL
);
2267 err
= fuse_setlk(file
, fl
, 0);
2272 static int fuse_file_flock(struct file
*file
, int cmd
, struct file_lock
*fl
)
2274 struct inode
*inode
= file_inode(file
);
2275 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2279 err
= flock_lock_file_wait(file
, fl
);
2281 struct fuse_file
*ff
= file
->private_data
;
2283 /* emulate flock with POSIX locks */
2284 fl
->fl_owner
= (fl_owner_t
) file
;
2286 err
= fuse_setlk(file
, fl
, 1);
2292 static sector_t
fuse_bmap(struct address_space
*mapping
, sector_t block
)
2294 struct inode
*inode
= mapping
->host
;
2295 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2296 struct fuse_req
*req
;
2297 struct fuse_bmap_in inarg
;
2298 struct fuse_bmap_out outarg
;
2301 if (!inode
->i_sb
->s_bdev
|| fc
->no_bmap
)
2304 req
= fuse_get_req_nopages(fc
);
2308 memset(&inarg
, 0, sizeof(inarg
));
2309 inarg
.block
= block
;
2310 inarg
.blocksize
= inode
->i_sb
->s_blocksize
;
2311 req
->in
.h
.opcode
= FUSE_BMAP
;
2312 req
->in
.h
.nodeid
= get_node_id(inode
);
2313 req
->in
.numargs
= 1;
2314 req
->in
.args
[0].size
= sizeof(inarg
);
2315 req
->in
.args
[0].value
= &inarg
;
2316 req
->out
.numargs
= 1;
2317 req
->out
.args
[0].size
= sizeof(outarg
);
2318 req
->out
.args
[0].value
= &outarg
;
2319 fuse_request_send(fc
, req
);
2320 err
= req
->out
.h
.error
;
2321 fuse_put_request(fc
, req
);
2325 return err
? 0 : outarg
.block
;
2328 static loff_t
fuse_file_llseek(struct file
*file
, loff_t offset
, int whence
)
2331 struct inode
*inode
= file_inode(file
);
2333 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
2334 if (whence
== SEEK_CUR
|| whence
== SEEK_SET
)
2335 return generic_file_llseek(file
, offset
, whence
);
2337 mutex_lock(&inode
->i_mutex
);
2338 retval
= fuse_update_attributes(inode
, NULL
, file
, NULL
);
2340 retval
= generic_file_llseek(file
, offset
, whence
);
2341 mutex_unlock(&inode
->i_mutex
);
2346 static int fuse_ioctl_copy_user(struct page
**pages
, struct iovec
*iov
,
2347 unsigned int nr_segs
, size_t bytes
, bool to_user
)
2355 iov_iter_init(&ii
, iov
, nr_segs
, bytes
, 0);
2357 while (iov_iter_count(&ii
)) {
2358 struct page
*page
= pages
[page_idx
++];
2359 size_t todo
= min_t(size_t, PAGE_SIZE
, iov_iter_count(&ii
));
2365 char __user
*uaddr
= ii
.iov
->iov_base
+ ii
.iov_offset
;
2366 size_t iov_len
= ii
.iov
->iov_len
- ii
.iov_offset
;
2367 size_t copy
= min(todo
, iov_len
);
2371 left
= copy_from_user(kaddr
, uaddr
, copy
);
2373 left
= copy_to_user(uaddr
, kaddr
, copy
);
2378 iov_iter_advance(&ii
, copy
);
2390 * CUSE servers compiled on 32bit broke on 64bit kernels because the
2391 * ABI was defined to be 'struct iovec' which is different on 32bit
2392 * and 64bit. Fortunately we can determine which structure the server
2393 * used from the size of the reply.
2395 static int fuse_copy_ioctl_iovec_old(struct iovec
*dst
, void *src
,
2396 size_t transferred
, unsigned count
,
2399 #ifdef CONFIG_COMPAT
2400 if (count
* sizeof(struct compat_iovec
) == transferred
) {
2401 struct compat_iovec
*ciov
= src
;
2405 * With this interface a 32bit server cannot support
2406 * non-compat (i.e. ones coming from 64bit apps) ioctl
2412 for (i
= 0; i
< count
; i
++) {
2413 dst
[i
].iov_base
= compat_ptr(ciov
[i
].iov_base
);
2414 dst
[i
].iov_len
= ciov
[i
].iov_len
;
2420 if (count
* sizeof(struct iovec
) != transferred
)
2423 memcpy(dst
, src
, transferred
);
2427 /* Make sure iov_length() won't overflow */
2428 static int fuse_verify_ioctl_iov(struct iovec
*iov
, size_t count
)
2431 u32 max
= FUSE_MAX_PAGES_PER_REQ
<< PAGE_SHIFT
;
2433 for (n
= 0; n
< count
; n
++, iov
++) {
2434 if (iov
->iov_len
> (size_t) max
)
2436 max
-= iov
->iov_len
;
2441 static int fuse_copy_ioctl_iovec(struct fuse_conn
*fc
, struct iovec
*dst
,
2442 void *src
, size_t transferred
, unsigned count
,
2446 struct fuse_ioctl_iovec
*fiov
= src
;
2448 if (fc
->minor
< 16) {
2449 return fuse_copy_ioctl_iovec_old(dst
, src
, transferred
,
2453 if (count
* sizeof(struct fuse_ioctl_iovec
) != transferred
)
2456 for (i
= 0; i
< count
; i
++) {
2457 /* Did the server supply an inappropriate value? */
2458 if (fiov
[i
].base
!= (unsigned long) fiov
[i
].base
||
2459 fiov
[i
].len
!= (unsigned long) fiov
[i
].len
)
2462 dst
[i
].iov_base
= (void __user
*) (unsigned long) fiov
[i
].base
;
2463 dst
[i
].iov_len
= (size_t) fiov
[i
].len
;
2465 #ifdef CONFIG_COMPAT
2467 (ptr_to_compat(dst
[i
].iov_base
) != fiov
[i
].base
||
2468 (compat_size_t
) dst
[i
].iov_len
!= fiov
[i
].len
))
2478 * For ioctls, there is no generic way to determine how much memory
2479 * needs to be read and/or written. Furthermore, ioctls are allowed
2480 * to dereference the passed pointer, so the parameter requires deep
2481 * copying but FUSE has no idea whatsoever about what to copy in or
2484 * This is solved by allowing FUSE server to retry ioctl with
2485 * necessary in/out iovecs. Let's assume the ioctl implementation
2486 * needs to read in the following structure.
2493 * On the first callout to FUSE server, inarg->in_size and
2494 * inarg->out_size will be NULL; then, the server completes the ioctl
2495 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
2496 * the actual iov array to
2498 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
2500 * which tells FUSE to copy in the requested area and retry the ioctl.
2501 * On the second round, the server has access to the structure and
2502 * from that it can tell what to look for next, so on the invocation,
2503 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
2505 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
2506 * { .iov_base = a.buf, .iov_len = a.buflen } }
2508 * FUSE will copy both struct a and the pointed buffer from the
2509 * process doing the ioctl and retry ioctl with both struct a and the
2512 * This time, FUSE server has everything it needs and completes ioctl
2513 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
2515 * Copying data out works the same way.
2517 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
2518 * automatically initializes in and out iovs by decoding @cmd with
2519 * _IOC_* macros and the server is not allowed to request RETRY. This
2520 * limits ioctl data transfers to well-formed ioctls and is the forced
2521 * behavior for all FUSE servers.
2523 long fuse_do_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
,
2526 struct fuse_file
*ff
= file
->private_data
;
2527 struct fuse_conn
*fc
= ff
->fc
;
2528 struct fuse_ioctl_in inarg
= {
2534 struct fuse_ioctl_out outarg
;
2535 struct fuse_req
*req
= NULL
;
2536 struct page
**pages
= NULL
;
2537 struct iovec
*iov_page
= NULL
;
2538 struct iovec
*in_iov
= NULL
, *out_iov
= NULL
;
2539 unsigned int in_iovs
= 0, out_iovs
= 0, num_pages
= 0, max_pages
;
2540 size_t in_size
, out_size
, transferred
;
2543 #if BITS_PER_LONG == 32
2544 inarg
.flags
|= FUSE_IOCTL_32BIT
;
2546 if (flags
& FUSE_IOCTL_COMPAT
)
2547 inarg
.flags
|= FUSE_IOCTL_32BIT
;
2550 /* assume all the iovs returned by client always fits in a page */
2551 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec
) * FUSE_IOCTL_MAX_IOV
> PAGE_SIZE
);
2554 pages
= kcalloc(FUSE_MAX_PAGES_PER_REQ
, sizeof(pages
[0]), GFP_KERNEL
);
2555 iov_page
= (struct iovec
*) __get_free_page(GFP_KERNEL
);
2556 if (!pages
|| !iov_page
)
2560 * If restricted, initialize IO parameters as encoded in @cmd.
2561 * RETRY from server is not allowed.
2563 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
)) {
2564 struct iovec
*iov
= iov_page
;
2566 iov
->iov_base
= (void __user
*)arg
;
2567 iov
->iov_len
= _IOC_SIZE(cmd
);
2569 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
2574 if (_IOC_DIR(cmd
) & _IOC_READ
) {
2581 inarg
.in_size
= in_size
= iov_length(in_iov
, in_iovs
);
2582 inarg
.out_size
= out_size
= iov_length(out_iov
, out_iovs
);
2585 * Out data can be used either for actual out data or iovs,
2586 * make sure there always is at least one page.
2588 out_size
= max_t(size_t, out_size
, PAGE_SIZE
);
2589 max_pages
= DIV_ROUND_UP(max(in_size
, out_size
), PAGE_SIZE
);
2591 /* make sure there are enough buffer pages and init request with them */
2593 if (max_pages
> FUSE_MAX_PAGES_PER_REQ
)
2595 while (num_pages
< max_pages
) {
2596 pages
[num_pages
] = alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
2597 if (!pages
[num_pages
])
2602 req
= fuse_get_req(fc
, num_pages
);
2608 memcpy(req
->pages
, pages
, sizeof(req
->pages
[0]) * num_pages
);
2609 req
->num_pages
= num_pages
;
2610 fuse_page_descs_length_init(req
, 0, req
->num_pages
);
2612 /* okay, let's send it to the client */
2613 req
->in
.h
.opcode
= FUSE_IOCTL
;
2614 req
->in
.h
.nodeid
= ff
->nodeid
;
2615 req
->in
.numargs
= 1;
2616 req
->in
.args
[0].size
= sizeof(inarg
);
2617 req
->in
.args
[0].value
= &inarg
;
2620 req
->in
.args
[1].size
= in_size
;
2621 req
->in
.argpages
= 1;
2623 err
= fuse_ioctl_copy_user(pages
, in_iov
, in_iovs
, in_size
,
2629 req
->out
.numargs
= 2;
2630 req
->out
.args
[0].size
= sizeof(outarg
);
2631 req
->out
.args
[0].value
= &outarg
;
2632 req
->out
.args
[1].size
= out_size
;
2633 req
->out
.argpages
= 1;
2634 req
->out
.argvar
= 1;
2636 fuse_request_send(fc
, req
);
2637 err
= req
->out
.h
.error
;
2638 transferred
= req
->out
.args
[1].size
;
2639 fuse_put_request(fc
, req
);
2644 /* did it ask for retry? */
2645 if (outarg
.flags
& FUSE_IOCTL_RETRY
) {
2648 /* no retry if in restricted mode */
2650 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
))
2653 in_iovs
= outarg
.in_iovs
;
2654 out_iovs
= outarg
.out_iovs
;
2657 * Make sure things are in boundary, separate checks
2658 * are to protect against overflow.
2661 if (in_iovs
> FUSE_IOCTL_MAX_IOV
||
2662 out_iovs
> FUSE_IOCTL_MAX_IOV
||
2663 in_iovs
+ out_iovs
> FUSE_IOCTL_MAX_IOV
)
2666 vaddr
= kmap_atomic(pages
[0]);
2667 err
= fuse_copy_ioctl_iovec(fc
, iov_page
, vaddr
,
2668 transferred
, in_iovs
+ out_iovs
,
2669 (flags
& FUSE_IOCTL_COMPAT
) != 0);
2670 kunmap_atomic(vaddr
);
2675 out_iov
= in_iov
+ in_iovs
;
2677 err
= fuse_verify_ioctl_iov(in_iov
, in_iovs
);
2681 err
= fuse_verify_ioctl_iov(out_iov
, out_iovs
);
2689 if (transferred
> inarg
.out_size
)
2692 err
= fuse_ioctl_copy_user(pages
, out_iov
, out_iovs
, transferred
, true);
2695 fuse_put_request(fc
, req
);
2696 free_page((unsigned long) iov_page
);
2698 __free_page(pages
[--num_pages
]);
2701 return err
? err
: outarg
.result
;
2703 EXPORT_SYMBOL_GPL(fuse_do_ioctl
);
2705 long fuse_ioctl_common(struct file
*file
, unsigned int cmd
,
2706 unsigned long arg
, unsigned int flags
)
2708 struct inode
*inode
= file_inode(file
);
2709 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2711 if (!fuse_allow_current_process(fc
))
2714 if (is_bad_inode(inode
))
2717 return fuse_do_ioctl(file
, cmd
, arg
, flags
);
2720 static long fuse_file_ioctl(struct file
*file
, unsigned int cmd
,
2723 return fuse_ioctl_common(file
, cmd
, arg
, 0);
2726 static long fuse_file_compat_ioctl(struct file
*file
, unsigned int cmd
,
2729 return fuse_ioctl_common(file
, cmd
, arg
, FUSE_IOCTL_COMPAT
);
2733 * All files which have been polled are linked to RB tree
2734 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2735 * find the matching one.
2737 static struct rb_node
**fuse_find_polled_node(struct fuse_conn
*fc
, u64 kh
,
2738 struct rb_node
**parent_out
)
2740 struct rb_node
**link
= &fc
->polled_files
.rb_node
;
2741 struct rb_node
*last
= NULL
;
2744 struct fuse_file
*ff
;
2747 ff
= rb_entry(last
, struct fuse_file
, polled_node
);
2750 link
= &last
->rb_left
;
2751 else if (kh
> ff
->kh
)
2752 link
= &last
->rb_right
;
2763 * The file is about to be polled. Make sure it's on the polled_files
2764 * RB tree. Note that files once added to the polled_files tree are
2765 * not removed before the file is released. This is because a file
2766 * polled once is likely to be polled again.
2768 static void fuse_register_polled_file(struct fuse_conn
*fc
,
2769 struct fuse_file
*ff
)
2771 spin_lock(&fc
->lock
);
2772 if (RB_EMPTY_NODE(&ff
->polled_node
)) {
2773 struct rb_node
**link
, *uninitialized_var(parent
);
2775 link
= fuse_find_polled_node(fc
, ff
->kh
, &parent
);
2777 rb_link_node(&ff
->polled_node
, parent
, link
);
2778 rb_insert_color(&ff
->polled_node
, &fc
->polled_files
);
2780 spin_unlock(&fc
->lock
);
2783 unsigned fuse_file_poll(struct file
*file
, poll_table
*wait
)
2785 struct fuse_file
*ff
= file
->private_data
;
2786 struct fuse_conn
*fc
= ff
->fc
;
2787 struct fuse_poll_in inarg
= { .fh
= ff
->fh
, .kh
= ff
->kh
};
2788 struct fuse_poll_out outarg
;
2789 struct fuse_req
*req
;
2793 return DEFAULT_POLLMASK
;
2795 poll_wait(file
, &ff
->poll_wait
, wait
);
2796 inarg
.events
= (__u32
)poll_requested_events(wait
);
2799 * Ask for notification iff there's someone waiting for it.
2800 * The client may ignore the flag and always notify.
2802 if (waitqueue_active(&ff
->poll_wait
)) {
2803 inarg
.flags
|= FUSE_POLL_SCHEDULE_NOTIFY
;
2804 fuse_register_polled_file(fc
, ff
);
2807 req
= fuse_get_req_nopages(fc
);
2811 req
->in
.h
.opcode
= FUSE_POLL
;
2812 req
->in
.h
.nodeid
= ff
->nodeid
;
2813 req
->in
.numargs
= 1;
2814 req
->in
.args
[0].size
= sizeof(inarg
);
2815 req
->in
.args
[0].value
= &inarg
;
2816 req
->out
.numargs
= 1;
2817 req
->out
.args
[0].size
= sizeof(outarg
);
2818 req
->out
.args
[0].value
= &outarg
;
2819 fuse_request_send(fc
, req
);
2820 err
= req
->out
.h
.error
;
2821 fuse_put_request(fc
, req
);
2824 return outarg
.revents
;
2825 if (err
== -ENOSYS
) {
2827 return DEFAULT_POLLMASK
;
2831 EXPORT_SYMBOL_GPL(fuse_file_poll
);
2834 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2835 * wakes up the poll waiters.
2837 int fuse_notify_poll_wakeup(struct fuse_conn
*fc
,
2838 struct fuse_notify_poll_wakeup_out
*outarg
)
2840 u64 kh
= outarg
->kh
;
2841 struct rb_node
**link
;
2843 spin_lock(&fc
->lock
);
2845 link
= fuse_find_polled_node(fc
, kh
, NULL
);
2847 struct fuse_file
*ff
;
2849 ff
= rb_entry(*link
, struct fuse_file
, polled_node
);
2850 wake_up_interruptible_sync(&ff
->poll_wait
);
2853 spin_unlock(&fc
->lock
);
2857 static void fuse_do_truncate(struct file
*file
)
2859 struct inode
*inode
= file
->f_mapping
->host
;
2862 attr
.ia_valid
= ATTR_SIZE
;
2863 attr
.ia_size
= i_size_read(inode
);
2865 attr
.ia_file
= file
;
2866 attr
.ia_valid
|= ATTR_FILE
;
2868 fuse_do_setattr(inode
, &attr
, file
);
2871 static inline loff_t
fuse_round_up(loff_t off
)
2873 return round_up(off
, FUSE_MAX_PAGES_PER_REQ
<< PAGE_SHIFT
);
2877 fuse_direct_IO(int rw
, struct kiocb
*iocb
, const struct iovec
*iov
,
2878 loff_t offset
, unsigned long nr_segs
)
2881 struct file
*file
= iocb
->ki_filp
;
2882 struct fuse_file
*ff
= file
->private_data
;
2883 bool async_dio
= ff
->fc
->async_dio
;
2885 struct inode
*inode
;
2887 size_t count
= iov_length(iov
, nr_segs
);
2888 struct fuse_io_priv
*io
;
2891 inode
= file
->f_mapping
->host
;
2892 i_size
= i_size_read(inode
);
2894 if ((rw
== READ
) && (offset
> i_size
))
2897 /* optimization for short read */
2898 if (async_dio
&& rw
!= WRITE
&& offset
+ count
> i_size
) {
2899 if (offset
>= i_size
)
2901 count
= min_t(loff_t
, count
, fuse_round_up(i_size
- offset
));
2904 io
= kmalloc(sizeof(struct fuse_io_priv
), GFP_KERNEL
);
2907 spin_lock_init(&io
->lock
);
2911 io
->offset
= offset
;
2912 io
->write
= (rw
== WRITE
);
2916 * By default, we want to optimize all I/Os with async request
2917 * submission to the client filesystem if supported.
2919 io
->async
= async_dio
;
2923 * We cannot asynchronously extend the size of a file. We have no method
2924 * to wait on real async I/O requests, so we must submit this request
2927 if (!is_sync_kiocb(iocb
) && (offset
+ count
> i_size
) && rw
== WRITE
)
2931 ret
= __fuse_direct_write(io
, iov
, nr_segs
, &pos
);
2933 ret
= __fuse_direct_read(io
, iov
, nr_segs
, &pos
, count
);
2936 fuse_aio_complete(io
, ret
< 0 ? ret
: 0, -1);
2938 /* we have a non-extending, async request, so return */
2939 if (!is_sync_kiocb(iocb
))
2940 return -EIOCBQUEUED
;
2942 ret
= wait_on_sync_kiocb(iocb
);
2949 fuse_write_update_size(inode
, pos
);
2950 else if (ret
< 0 && offset
+ count
> i_size
)
2951 fuse_do_truncate(file
);
2957 static long fuse_file_fallocate(struct file
*file
, int mode
, loff_t offset
,
2960 struct fuse_file
*ff
= file
->private_data
;
2961 struct inode
*inode
= file
->f_inode
;
2962 struct fuse_inode
*fi
= get_fuse_inode(inode
);
2963 struct fuse_conn
*fc
= ff
->fc
;
2964 struct fuse_req
*req
;
2965 struct fuse_fallocate_in inarg
= {
2972 bool lock_inode
= !(mode
& FALLOC_FL_KEEP_SIZE
) ||
2973 (mode
& FALLOC_FL_PUNCH_HOLE
);
2975 if (fc
->no_fallocate
)
2979 mutex_lock(&inode
->i_mutex
);
2980 if (mode
& FALLOC_FL_PUNCH_HOLE
) {
2981 loff_t endbyte
= offset
+ length
- 1;
2982 err
= filemap_write_and_wait_range(inode
->i_mapping
,
2987 fuse_sync_writes(inode
);
2991 if (!(mode
& FALLOC_FL_KEEP_SIZE
))
2992 set_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
2994 req
= fuse_get_req_nopages(fc
);
3000 req
->in
.h
.opcode
= FUSE_FALLOCATE
;
3001 req
->in
.h
.nodeid
= ff
->nodeid
;
3002 req
->in
.numargs
= 1;
3003 req
->in
.args
[0].size
= sizeof(inarg
);
3004 req
->in
.args
[0].value
= &inarg
;
3005 fuse_request_send(fc
, req
);
3006 err
= req
->out
.h
.error
;
3007 if (err
== -ENOSYS
) {
3008 fc
->no_fallocate
= 1;
3011 fuse_put_request(fc
, req
);
3016 /* we could have extended the file */
3017 if (!(mode
& FALLOC_FL_KEEP_SIZE
)) {
3018 bool changed
= fuse_write_update_size(inode
, offset
+ length
);
3020 if (changed
&& fc
->writeback_cache
) {
3021 struct fuse_inode
*fi
= get_fuse_inode(inode
);
3023 inode
->i_mtime
= current_fs_time(inode
->i_sb
);
3024 set_bit(FUSE_I_MTIME_DIRTY
, &fi
->state
);
3028 if (mode
& FALLOC_FL_PUNCH_HOLE
)
3029 truncate_pagecache_range(inode
, offset
, offset
+ length
- 1);
3031 fuse_invalidate_attr(inode
);
3034 if (!(mode
& FALLOC_FL_KEEP_SIZE
))
3035 clear_bit(FUSE_I_SIZE_UNSTABLE
, &fi
->state
);
3038 mutex_unlock(&inode
->i_mutex
);
3043 static const struct file_operations fuse_file_operations
= {
3044 .llseek
= fuse_file_llseek
,
3045 .read
= do_sync_read
,
3046 .aio_read
= fuse_file_aio_read
,
3047 .write
= do_sync_write
,
3048 .aio_write
= fuse_file_aio_write
,
3049 .mmap
= fuse_file_mmap
,
3051 .flush
= fuse_flush
,
3052 .release
= fuse_release
,
3053 .fsync
= fuse_fsync
,
3054 .lock
= fuse_file_lock
,
3055 .flock
= fuse_file_flock
,
3056 .splice_read
= generic_file_splice_read
,
3057 .unlocked_ioctl
= fuse_file_ioctl
,
3058 .compat_ioctl
= fuse_file_compat_ioctl
,
3059 .poll
= fuse_file_poll
,
3060 .fallocate
= fuse_file_fallocate
,
3063 static const struct file_operations fuse_direct_io_file_operations
= {
3064 .llseek
= fuse_file_llseek
,
3065 .read
= fuse_direct_read
,
3066 .write
= fuse_direct_write
,
3067 .mmap
= fuse_direct_mmap
,
3069 .flush
= fuse_flush
,
3070 .release
= fuse_release
,
3071 .fsync
= fuse_fsync
,
3072 .lock
= fuse_file_lock
,
3073 .flock
= fuse_file_flock
,
3074 .unlocked_ioctl
= fuse_file_ioctl
,
3075 .compat_ioctl
= fuse_file_compat_ioctl
,
3076 .poll
= fuse_file_poll
,
3077 .fallocate
= fuse_file_fallocate
,
3078 /* no splice_read */
3081 static const struct address_space_operations fuse_file_aops
= {
3082 .readpage
= fuse_readpage
,
3083 .writepage
= fuse_writepage
,
3084 .writepages
= fuse_writepages
,
3085 .launder_page
= fuse_launder_page
,
3086 .readpages
= fuse_readpages
,
3087 .set_page_dirty
= __set_page_dirty_nobuffers
,
3089 .direct_IO
= fuse_direct_IO
,
3090 .write_begin
= fuse_write_begin
,
3091 .write_end
= fuse_write_end
,
3094 void fuse_init_file_inode(struct inode
*inode
)
3096 inode
->i_fop
= &fuse_file_operations
;
3097 inode
->i_data
.a_ops
= &fuse_file_aops
;