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>
16 static const struct file_operations fuse_direct_io_file_operations
;
18 static int fuse_send_open(struct fuse_conn
*fc
, u64 nodeid
, struct file
*file
,
19 int opcode
, struct fuse_open_out
*outargp
)
21 struct fuse_open_in inarg
;
25 req
= fuse_get_req(fc
);
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 req
->in
.h
.opcode
= opcode
;
34 req
->in
.h
.nodeid
= nodeid
;
36 req
->in
.args
[0].size
= sizeof(inarg
);
37 req
->in
.args
[0].value
= &inarg
;
39 req
->out
.args
[0].size
= sizeof(*outargp
);
40 req
->out
.args
[0].value
= outargp
;
41 fuse_request_send(fc
, req
);
42 err
= req
->out
.h
.error
;
43 fuse_put_request(fc
, req
);
48 struct fuse_file
*fuse_file_alloc(struct fuse_conn
*fc
)
52 ff
= kmalloc(sizeof(struct fuse_file
), GFP_KERNEL
);
57 ff
->reserved_req
= fuse_request_alloc();
58 if (unlikely(!ff
->reserved_req
)) {
63 INIT_LIST_HEAD(&ff
->write_entry
);
64 atomic_set(&ff
->count
, 0);
65 RB_CLEAR_NODE(&ff
->polled_node
);
66 init_waitqueue_head(&ff
->poll_wait
);
70 spin_unlock(&fc
->lock
);
75 void fuse_file_free(struct fuse_file
*ff
)
77 fuse_request_free(ff
->reserved_req
);
81 struct fuse_file
*fuse_file_get(struct fuse_file
*ff
)
83 atomic_inc(&ff
->count
);
87 static void fuse_release_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
89 path_put(&req
->misc
.release
.path
);
92 static void fuse_file_put(struct fuse_file
*ff
)
94 if (atomic_dec_and_test(&ff
->count
)) {
95 struct fuse_req
*req
= ff
->reserved_req
;
97 req
->end
= fuse_release_end
;
98 fuse_request_send_background(ff
->fc
, req
);
103 static int fuse_do_open(struct fuse_conn
*fc
, u64 nodeid
, struct file
*file
,
106 struct fuse_open_out outarg
;
107 struct fuse_file
*ff
;
109 int opcode
= isdir
? FUSE_OPENDIR
: FUSE_OPEN
;
111 ff
= fuse_file_alloc(fc
);
115 err
= fuse_send_open(fc
, nodeid
, file
, opcode
, &outarg
);
122 outarg
.open_flags
&= ~FOPEN_DIRECT_IO
;
126 ff
->open_flags
= outarg
.open_flags
;
127 file
->private_data
= fuse_file_get(ff
);
132 void fuse_finish_open(struct inode
*inode
, struct file
*file
)
134 struct fuse_file
*ff
= file
->private_data
;
136 if (ff
->open_flags
& FOPEN_DIRECT_IO
)
137 file
->f_op
= &fuse_direct_io_file_operations
;
138 if (!(ff
->open_flags
& FOPEN_KEEP_CACHE
))
139 invalidate_inode_pages2(inode
->i_mapping
);
140 if (ff
->open_flags
& FOPEN_NONSEEKABLE
)
141 nonseekable_open(inode
, file
);
144 int fuse_open_common(struct inode
*inode
, struct file
*file
, bool isdir
)
146 struct fuse_conn
*fc
= get_fuse_conn(inode
);
149 /* VFS checks this, but only _after_ ->open() */
150 if (file
->f_flags
& O_DIRECT
)
153 err
= generic_file_open(inode
, file
);
157 err
= fuse_do_open(fc
, get_node_id(inode
), file
, isdir
);
161 fuse_finish_open(inode
, file
);
166 static void fuse_prepare_release(struct fuse_file
*ff
, int flags
, int opcode
)
168 struct fuse_conn
*fc
= ff
->fc
;
169 struct fuse_req
*req
= ff
->reserved_req
;
170 struct fuse_release_in
*inarg
= &req
->misc
.release
.in
;
172 spin_lock(&fc
->lock
);
173 list_del(&ff
->write_entry
);
174 if (!RB_EMPTY_NODE(&ff
->polled_node
))
175 rb_erase(&ff
->polled_node
, &fc
->polled_files
);
176 spin_unlock(&fc
->lock
);
178 wake_up_interruptible_sync(&ff
->poll_wait
);
181 inarg
->flags
= flags
;
182 req
->in
.h
.opcode
= opcode
;
183 req
->in
.h
.nodeid
= ff
->nodeid
;
185 req
->in
.args
[0].size
= sizeof(struct fuse_release_in
);
186 req
->in
.args
[0].value
= inarg
;
189 void fuse_release_common(struct file
*file
, int opcode
)
191 struct fuse_file
*ff
;
192 struct fuse_req
*req
;
194 ff
= file
->private_data
;
198 req
= ff
->reserved_req
;
199 fuse_prepare_release(ff
, file
->f_flags
, opcode
);
201 /* Hold vfsmount and dentry until release is finished */
202 path_get(&file
->f_path
);
203 req
->misc
.release
.path
= file
->f_path
;
206 * Normally this will send the RELEASE request, however if
207 * some asynchronous READ or WRITE requests are outstanding,
208 * the sending will be delayed.
213 static int fuse_open(struct inode
*inode
, struct file
*file
)
215 return fuse_open_common(inode
, file
, false);
218 static int fuse_release(struct inode
*inode
, struct file
*file
)
220 fuse_release_common(file
, FUSE_RELEASE
);
222 /* return value is ignored by VFS */
226 void fuse_sync_release(struct fuse_file
*ff
, int flags
)
228 WARN_ON(atomic_read(&ff
->count
) > 1);
229 fuse_prepare_release(ff
, flags
, FUSE_RELEASE
);
230 ff
->reserved_req
->force
= 1;
231 fuse_request_send(ff
->fc
, ff
->reserved_req
);
232 fuse_put_request(ff
->fc
, ff
->reserved_req
);
237 * Scramble the ID space with XTEA, so that the value of the files_struct
238 * pointer is not exposed to userspace.
240 u64
fuse_lock_owner_id(struct fuse_conn
*fc
, fl_owner_t id
)
242 u32
*k
= fc
->scramble_key
;
243 u64 v
= (unsigned long) id
;
249 for (i
= 0; i
< 32; i
++) {
250 v0
+= ((v1
<< 4 ^ v1
>> 5) + v1
) ^ (sum
+ k
[sum
& 3]);
252 v1
+= ((v0
<< 4 ^ v0
>> 5) + v0
) ^ (sum
+ k
[sum
>>11 & 3]);
255 return (u64
) v0
+ ((u64
) v1
<< 32);
259 * Check if page is under writeback
261 * This is currently done by walking the list of writepage requests
262 * for the inode, which can be pretty inefficient.
264 static bool fuse_page_is_writeback(struct inode
*inode
, pgoff_t index
)
266 struct fuse_conn
*fc
= get_fuse_conn(inode
);
267 struct fuse_inode
*fi
= get_fuse_inode(inode
);
268 struct fuse_req
*req
;
271 spin_lock(&fc
->lock
);
272 list_for_each_entry(req
, &fi
->writepages
, writepages_entry
) {
275 BUG_ON(req
->inode
!= inode
);
276 curr_index
= req
->misc
.write
.in
.offset
>> PAGE_CACHE_SHIFT
;
277 if (curr_index
== index
) {
282 spin_unlock(&fc
->lock
);
288 * Wait for page writeback to be completed.
290 * Since fuse doesn't rely on the VM writeback tracking, this has to
291 * use some other means.
293 static int fuse_wait_on_page_writeback(struct inode
*inode
, pgoff_t index
)
295 struct fuse_inode
*fi
= get_fuse_inode(inode
);
297 wait_event(fi
->page_waitq
, !fuse_page_is_writeback(inode
, index
));
301 static int fuse_flush(struct file
*file
, fl_owner_t id
)
303 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
304 struct fuse_conn
*fc
= get_fuse_conn(inode
);
305 struct fuse_file
*ff
= file
->private_data
;
306 struct fuse_req
*req
;
307 struct fuse_flush_in inarg
;
310 if (is_bad_inode(inode
))
316 req
= fuse_get_req_nofail(fc
, file
);
317 memset(&inarg
, 0, sizeof(inarg
));
319 inarg
.lock_owner
= fuse_lock_owner_id(fc
, id
);
320 req
->in
.h
.opcode
= FUSE_FLUSH
;
321 req
->in
.h
.nodeid
= get_node_id(inode
);
323 req
->in
.args
[0].size
= sizeof(inarg
);
324 req
->in
.args
[0].value
= &inarg
;
326 fuse_request_send(fc
, req
);
327 err
= req
->out
.h
.error
;
328 fuse_put_request(fc
, req
);
329 if (err
== -ENOSYS
) {
337 * Wait for all pending writepages on the inode to finish.
339 * This is currently done by blocking further writes with FUSE_NOWRITE
340 * and waiting for all sent writes to complete.
342 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
343 * could conflict with truncation.
345 static void fuse_sync_writes(struct inode
*inode
)
347 fuse_set_nowrite(inode
);
348 fuse_release_nowrite(inode
);
351 int fuse_fsync_common(struct file
*file
, struct dentry
*de
, int datasync
,
354 struct inode
*inode
= de
->d_inode
;
355 struct fuse_conn
*fc
= get_fuse_conn(inode
);
356 struct fuse_file
*ff
= file
->private_data
;
357 struct fuse_req
*req
;
358 struct fuse_fsync_in inarg
;
361 if (is_bad_inode(inode
))
364 if ((!isdir
&& fc
->no_fsync
) || (isdir
&& fc
->no_fsyncdir
))
368 * Start writeback against all dirty pages of the inode, then
369 * wait for all outstanding writes, before sending the FSYNC
372 err
= write_inode_now(inode
, 0);
376 fuse_sync_writes(inode
);
378 req
= fuse_get_req(fc
);
382 memset(&inarg
, 0, sizeof(inarg
));
384 inarg
.fsync_flags
= datasync
? 1 : 0;
385 req
->in
.h
.opcode
= isdir
? FUSE_FSYNCDIR
: FUSE_FSYNC
;
386 req
->in
.h
.nodeid
= get_node_id(inode
);
388 req
->in
.args
[0].size
= sizeof(inarg
);
389 req
->in
.args
[0].value
= &inarg
;
390 fuse_request_send(fc
, req
);
391 err
= req
->out
.h
.error
;
392 fuse_put_request(fc
, req
);
393 if (err
== -ENOSYS
) {
403 static int fuse_fsync(struct file
*file
, struct dentry
*de
, int datasync
)
405 return fuse_fsync_common(file
, de
, datasync
, 0);
408 void fuse_read_fill(struct fuse_req
*req
, struct file
*file
, loff_t pos
,
409 size_t count
, int opcode
)
411 struct fuse_read_in
*inarg
= &req
->misc
.read
.in
;
412 struct fuse_file
*ff
= file
->private_data
;
417 inarg
->flags
= file
->f_flags
;
418 req
->in
.h
.opcode
= opcode
;
419 req
->in
.h
.nodeid
= ff
->nodeid
;
421 req
->in
.args
[0].size
= sizeof(struct fuse_read_in
);
422 req
->in
.args
[0].value
= inarg
;
424 req
->out
.numargs
= 1;
425 req
->out
.args
[0].size
= count
;
428 static size_t fuse_send_read(struct fuse_req
*req
, struct file
*file
,
429 loff_t pos
, size_t count
, fl_owner_t owner
)
431 struct fuse_file
*ff
= file
->private_data
;
432 struct fuse_conn
*fc
= ff
->fc
;
434 fuse_read_fill(req
, file
, pos
, count
, FUSE_READ
);
436 struct fuse_read_in
*inarg
= &req
->misc
.read
.in
;
438 inarg
->read_flags
|= FUSE_READ_LOCKOWNER
;
439 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
441 fuse_request_send(fc
, req
);
442 return req
->out
.args
[0].size
;
445 static void fuse_read_update_size(struct inode
*inode
, loff_t size
,
448 struct fuse_conn
*fc
= get_fuse_conn(inode
);
449 struct fuse_inode
*fi
= get_fuse_inode(inode
);
451 spin_lock(&fc
->lock
);
452 if (attr_ver
== fi
->attr_version
&& size
< inode
->i_size
) {
453 fi
->attr_version
= ++fc
->attr_version
;
454 i_size_write(inode
, size
);
456 spin_unlock(&fc
->lock
);
459 static int fuse_readpage(struct file
*file
, struct page
*page
)
461 struct inode
*inode
= page
->mapping
->host
;
462 struct fuse_conn
*fc
= get_fuse_conn(inode
);
463 struct fuse_req
*req
;
465 loff_t pos
= page_offset(page
);
466 size_t count
= PAGE_CACHE_SIZE
;
471 if (is_bad_inode(inode
))
475 * Page writeback can extend beyond the liftime of the
476 * page-cache page, so make sure we read a properly synced
479 fuse_wait_on_page_writeback(inode
, page
->index
);
481 req
= fuse_get_req(fc
);
486 attr_ver
= fuse_get_attr_version(fc
);
488 req
->out
.page_zeroing
= 1;
489 req
->out
.argpages
= 1;
491 req
->pages
[0] = page
;
492 num_read
= fuse_send_read(req
, file
, pos
, count
, NULL
);
493 err
= req
->out
.h
.error
;
494 fuse_put_request(fc
, req
);
498 * Short read means EOF. If file size is larger, truncate it
500 if (num_read
< count
)
501 fuse_read_update_size(inode
, pos
+ num_read
, attr_ver
);
503 SetPageUptodate(page
);
506 fuse_invalidate_attr(inode
); /* atime changed */
512 static void fuse_readpages_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
515 size_t count
= req
->misc
.read
.in
.size
;
516 size_t num_read
= req
->out
.args
[0].size
;
517 struct inode
*inode
= req
->pages
[0]->mapping
->host
;
520 * Short read means EOF. If file size is larger, truncate it
522 if (!req
->out
.h
.error
&& num_read
< count
) {
523 loff_t pos
= page_offset(req
->pages
[0]) + num_read
;
524 fuse_read_update_size(inode
, pos
, req
->misc
.read
.attr_ver
);
527 fuse_invalidate_attr(inode
); /* atime changed */
529 for (i
= 0; i
< req
->num_pages
; i
++) {
530 struct page
*page
= req
->pages
[i
];
531 if (!req
->out
.h
.error
)
532 SetPageUptodate(page
);
538 fuse_file_put(req
->ff
);
541 static void fuse_send_readpages(struct fuse_req
*req
, struct file
*file
)
543 struct fuse_file
*ff
= file
->private_data
;
544 struct fuse_conn
*fc
= ff
->fc
;
545 loff_t pos
= page_offset(req
->pages
[0]);
546 size_t count
= req
->num_pages
<< PAGE_CACHE_SHIFT
;
548 req
->out
.argpages
= 1;
549 req
->out
.page_zeroing
= 1;
550 fuse_read_fill(req
, file
, pos
, count
, FUSE_READ
);
551 req
->misc
.read
.attr_ver
= fuse_get_attr_version(fc
);
552 if (fc
->async_read
) {
553 req
->ff
= fuse_file_get(ff
);
554 req
->end
= fuse_readpages_end
;
555 fuse_request_send_background(fc
, req
);
557 fuse_request_send(fc
, req
);
558 fuse_readpages_end(fc
, req
);
559 fuse_put_request(fc
, req
);
563 struct fuse_fill_data
{
564 struct fuse_req
*req
;
569 static int fuse_readpages_fill(void *_data
, struct page
*page
)
571 struct fuse_fill_data
*data
= _data
;
572 struct fuse_req
*req
= data
->req
;
573 struct inode
*inode
= data
->inode
;
574 struct fuse_conn
*fc
= get_fuse_conn(inode
);
576 fuse_wait_on_page_writeback(inode
, page
->index
);
578 if (req
->num_pages
&&
579 (req
->num_pages
== FUSE_MAX_PAGES_PER_REQ
||
580 (req
->num_pages
+ 1) * PAGE_CACHE_SIZE
> fc
->max_read
||
581 req
->pages
[req
->num_pages
- 1]->index
+ 1 != page
->index
)) {
582 fuse_send_readpages(req
, data
->file
);
583 data
->req
= req
= fuse_get_req(fc
);
589 req
->pages
[req
->num_pages
] = page
;
594 static int fuse_readpages(struct file
*file
, struct address_space
*mapping
,
595 struct list_head
*pages
, unsigned nr_pages
)
597 struct inode
*inode
= mapping
->host
;
598 struct fuse_conn
*fc
= get_fuse_conn(inode
);
599 struct fuse_fill_data data
;
603 if (is_bad_inode(inode
))
608 data
.req
= fuse_get_req(fc
);
609 err
= PTR_ERR(data
.req
);
610 if (IS_ERR(data
.req
))
613 err
= read_cache_pages(mapping
, pages
, fuse_readpages_fill
, &data
);
615 if (data
.req
->num_pages
)
616 fuse_send_readpages(data
.req
, file
);
618 fuse_put_request(fc
, data
.req
);
624 static ssize_t
fuse_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
625 unsigned long nr_segs
, loff_t pos
)
627 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
629 if (pos
+ iov_length(iov
, nr_segs
) > i_size_read(inode
)) {
632 * If trying to read past EOF, make sure the i_size
633 * attribute is up-to-date.
635 err
= fuse_update_attributes(inode
, NULL
, iocb
->ki_filp
, NULL
);
640 return generic_file_aio_read(iocb
, iov
, nr_segs
, pos
);
643 static void fuse_write_fill(struct fuse_req
*req
, struct fuse_file
*ff
,
644 loff_t pos
, size_t count
)
646 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
647 struct fuse_write_out
*outarg
= &req
->misc
.write
.out
;
652 req
->in
.h
.opcode
= FUSE_WRITE
;
653 req
->in
.h
.nodeid
= ff
->nodeid
;
655 if (ff
->fc
->minor
< 9)
656 req
->in
.args
[0].size
= FUSE_COMPAT_WRITE_IN_SIZE
;
658 req
->in
.args
[0].size
= sizeof(struct fuse_write_in
);
659 req
->in
.args
[0].value
= inarg
;
660 req
->in
.args
[1].size
= count
;
661 req
->out
.numargs
= 1;
662 req
->out
.args
[0].size
= sizeof(struct fuse_write_out
);
663 req
->out
.args
[0].value
= outarg
;
666 static size_t fuse_send_write(struct fuse_req
*req
, struct file
*file
,
667 loff_t pos
, size_t count
, fl_owner_t owner
)
669 struct fuse_file
*ff
= file
->private_data
;
670 struct fuse_conn
*fc
= ff
->fc
;
671 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
673 fuse_write_fill(req
, ff
, pos
, count
);
674 inarg
->flags
= file
->f_flags
;
676 inarg
->write_flags
|= FUSE_WRITE_LOCKOWNER
;
677 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
679 fuse_request_send(fc
, req
);
680 return req
->misc
.write
.out
.size
;
683 static int fuse_write_begin(struct file
*file
, struct address_space
*mapping
,
684 loff_t pos
, unsigned len
, unsigned flags
,
685 struct page
**pagep
, void **fsdata
)
687 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
689 *pagep
= grab_cache_page_write_begin(mapping
, index
, flags
);
695 static void fuse_write_update_size(struct inode
*inode
, loff_t pos
)
697 struct fuse_conn
*fc
= get_fuse_conn(inode
);
698 struct fuse_inode
*fi
= get_fuse_inode(inode
);
700 spin_lock(&fc
->lock
);
701 fi
->attr_version
= ++fc
->attr_version
;
702 if (pos
> inode
->i_size
)
703 i_size_write(inode
, pos
);
704 spin_unlock(&fc
->lock
);
707 static int fuse_buffered_write(struct file
*file
, struct inode
*inode
,
708 loff_t pos
, unsigned count
, struct page
*page
)
712 struct fuse_conn
*fc
= get_fuse_conn(inode
);
713 unsigned offset
= pos
& (PAGE_CACHE_SIZE
- 1);
714 struct fuse_req
*req
;
716 if (is_bad_inode(inode
))
720 * Make sure writepages on the same page are not mixed up with
723 fuse_wait_on_page_writeback(inode
, page
->index
);
725 req
= fuse_get_req(fc
);
729 req
->in
.argpages
= 1;
731 req
->pages
[0] = page
;
732 req
->page_offset
= offset
;
733 nres
= fuse_send_write(req
, file
, pos
, count
, NULL
);
734 err
= req
->out
.h
.error
;
735 fuse_put_request(fc
, req
);
740 fuse_write_update_size(inode
, pos
);
741 if (count
== PAGE_CACHE_SIZE
)
742 SetPageUptodate(page
);
744 fuse_invalidate_attr(inode
);
745 return err
? err
: nres
;
748 static int fuse_write_end(struct file
*file
, struct address_space
*mapping
,
749 loff_t pos
, unsigned len
, unsigned copied
,
750 struct page
*page
, void *fsdata
)
752 struct inode
*inode
= mapping
->host
;
756 res
= fuse_buffered_write(file
, inode
, pos
, copied
, page
);
759 page_cache_release(page
);
763 static size_t fuse_send_write_pages(struct fuse_req
*req
, struct file
*file
,
764 struct inode
*inode
, loff_t pos
,
771 for (i
= 0; i
< req
->num_pages
; i
++)
772 fuse_wait_on_page_writeback(inode
, req
->pages
[i
]->index
);
774 res
= fuse_send_write(req
, file
, pos
, count
, NULL
);
776 offset
= req
->page_offset
;
778 for (i
= 0; i
< req
->num_pages
; i
++) {
779 struct page
*page
= req
->pages
[i
];
781 if (!req
->out
.h
.error
&& !offset
&& count
>= PAGE_CACHE_SIZE
)
782 SetPageUptodate(page
);
784 if (count
> PAGE_CACHE_SIZE
- offset
)
785 count
-= PAGE_CACHE_SIZE
- offset
;
791 page_cache_release(page
);
797 static ssize_t
fuse_fill_write_pages(struct fuse_req
*req
,
798 struct address_space
*mapping
,
799 struct iov_iter
*ii
, loff_t pos
)
801 struct fuse_conn
*fc
= get_fuse_conn(mapping
->host
);
802 unsigned offset
= pos
& (PAGE_CACHE_SIZE
- 1);
806 req
->in
.argpages
= 1;
807 req
->page_offset
= offset
;
812 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
813 size_t bytes
= min_t(size_t, PAGE_CACHE_SIZE
- offset
,
816 bytes
= min_t(size_t, bytes
, fc
->max_write
- count
);
820 if (iov_iter_fault_in_readable(ii
, bytes
))
824 page
= grab_cache_page_write_begin(mapping
, index
, 0);
829 tmp
= iov_iter_copy_from_user_atomic(page
, ii
, offset
, bytes
);
831 flush_dcache_page(page
);
835 page_cache_release(page
);
836 bytes
= min(bytes
, iov_iter_single_seg_count(ii
));
841 req
->pages
[req
->num_pages
] = page
;
844 iov_iter_advance(ii
, tmp
);
848 if (offset
== PAGE_CACHE_SIZE
)
853 } while (iov_iter_count(ii
) && count
< fc
->max_write
&&
854 req
->num_pages
< FUSE_MAX_PAGES_PER_REQ
&& offset
== 0);
856 return count
> 0 ? count
: err
;
859 static ssize_t
fuse_perform_write(struct file
*file
,
860 struct address_space
*mapping
,
861 struct iov_iter
*ii
, loff_t pos
)
863 struct inode
*inode
= mapping
->host
;
864 struct fuse_conn
*fc
= get_fuse_conn(inode
);
868 if (is_bad_inode(inode
))
872 struct fuse_req
*req
;
875 req
= fuse_get_req(fc
);
881 count
= fuse_fill_write_pages(req
, mapping
, ii
, pos
);
887 num_written
= fuse_send_write_pages(req
, file
, inode
,
889 err
= req
->out
.h
.error
;
894 /* break out of the loop on short write */
895 if (num_written
!= count
)
899 fuse_put_request(fc
, req
);
900 } while (!err
&& iov_iter_count(ii
));
903 fuse_write_update_size(inode
, pos
);
905 fuse_invalidate_attr(inode
);
907 return res
> 0 ? res
: err
;
910 static ssize_t
fuse_file_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
911 unsigned long nr_segs
, loff_t pos
)
913 struct file
*file
= iocb
->ki_filp
;
914 struct address_space
*mapping
= file
->f_mapping
;
917 struct inode
*inode
= mapping
->host
;
921 WARN_ON(iocb
->ki_pos
!= pos
);
923 err
= generic_segment_checks(iov
, &nr_segs
, &count
, VERIFY_READ
);
927 mutex_lock(&inode
->i_mutex
);
928 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
930 /* We can write back this queue in page reclaim */
931 current
->backing_dev_info
= mapping
->backing_dev_info
;
933 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
940 err
= file_remove_suid(file
);
944 file_update_time(file
);
946 iov_iter_init(&i
, iov
, nr_segs
, count
, 0);
947 written
= fuse_perform_write(file
, mapping
, &i
, pos
);
949 iocb
->ki_pos
= pos
+ written
;
952 current
->backing_dev_info
= NULL
;
953 mutex_unlock(&inode
->i_mutex
);
955 return written
? written
: err
;
958 static void fuse_release_user_pages(struct fuse_req
*req
, int write
)
962 for (i
= 0; i
< req
->num_pages
; i
++) {
963 struct page
*page
= req
->pages
[i
];
965 set_page_dirty_lock(page
);
970 static int fuse_get_user_pages(struct fuse_req
*req
, const char __user
*buf
,
971 size_t *nbytesp
, int write
)
973 size_t nbytes
= *nbytesp
;
974 unsigned long user_addr
= (unsigned long) buf
;
975 unsigned offset
= user_addr
& ~PAGE_MASK
;
978 /* Special case for kernel I/O: can copy directly into the buffer */
979 if (segment_eq(get_fs(), KERNEL_DS
)) {
981 req
->in
.args
[1].value
= (void *) user_addr
;
983 req
->out
.args
[0].value
= (void *) user_addr
;
988 nbytes
= min_t(size_t, nbytes
, FUSE_MAX_PAGES_PER_REQ
<< PAGE_SHIFT
);
989 npages
= (nbytes
+ offset
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
990 npages
= clamp(npages
, 1, FUSE_MAX_PAGES_PER_REQ
);
991 down_read(¤t
->mm
->mmap_sem
);
992 npages
= get_user_pages(current
, current
->mm
, user_addr
, npages
, !write
,
993 0, req
->pages
, NULL
);
994 up_read(¤t
->mm
->mmap_sem
);
998 req
->num_pages
= npages
;
999 req
->page_offset
= offset
;
1002 req
->in
.argpages
= 1;
1004 req
->out
.argpages
= 1;
1006 nbytes
= (req
->num_pages
<< PAGE_SHIFT
) - req
->page_offset
;
1007 *nbytesp
= min(*nbytesp
, nbytes
);
1012 static ssize_t
fuse_direct_io(struct file
*file
, const char __user
*buf
,
1013 size_t count
, loff_t
*ppos
, int write
)
1015 struct fuse_file
*ff
= file
->private_data
;
1016 struct fuse_conn
*fc
= ff
->fc
;
1017 size_t nmax
= write
? fc
->max_write
: fc
->max_read
;
1020 struct fuse_req
*req
;
1022 req
= fuse_get_req(fc
);
1024 return PTR_ERR(req
);
1028 fl_owner_t owner
= current
->files
;
1029 size_t nbytes
= min(count
, nmax
);
1030 int err
= fuse_get_user_pages(req
, buf
, &nbytes
, write
);
1037 nres
= fuse_send_write(req
, file
, pos
, nbytes
, owner
);
1039 nres
= fuse_send_read(req
, file
, pos
, nbytes
, owner
);
1041 fuse_release_user_pages(req
, !write
);
1042 if (req
->out
.h
.error
) {
1044 res
= req
->out
.h
.error
;
1046 } else if (nres
> nbytes
) {
1057 fuse_put_request(fc
, req
);
1058 req
= fuse_get_req(fc
);
1063 fuse_put_request(fc
, req
);
1070 static ssize_t
fuse_direct_read(struct file
*file
, char __user
*buf
,
1071 size_t count
, loff_t
*ppos
)
1074 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1076 if (is_bad_inode(inode
))
1079 res
= fuse_direct_io(file
, buf
, count
, ppos
, 0);
1081 fuse_invalidate_attr(inode
);
1086 static ssize_t
fuse_direct_write(struct file
*file
, const char __user
*buf
,
1087 size_t count
, loff_t
*ppos
)
1089 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1092 if (is_bad_inode(inode
))
1095 /* Don't allow parallel writes to the same file */
1096 mutex_lock(&inode
->i_mutex
);
1097 res
= generic_write_checks(file
, ppos
, &count
, 0);
1099 res
= fuse_direct_io(file
, buf
, count
, ppos
, 1);
1101 fuse_write_update_size(inode
, *ppos
);
1103 mutex_unlock(&inode
->i_mutex
);
1105 fuse_invalidate_attr(inode
);
1110 static void fuse_writepage_free(struct fuse_conn
*fc
, struct fuse_req
*req
)
1112 __free_page(req
->pages
[0]);
1113 fuse_file_put(req
->ff
);
1116 static void fuse_writepage_finish(struct fuse_conn
*fc
, struct fuse_req
*req
)
1118 struct inode
*inode
= req
->inode
;
1119 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1120 struct backing_dev_info
*bdi
= inode
->i_mapping
->backing_dev_info
;
1122 list_del(&req
->writepages_entry
);
1123 dec_bdi_stat(bdi
, BDI_WRITEBACK
);
1124 dec_zone_page_state(req
->pages
[0], NR_WRITEBACK_TEMP
);
1125 bdi_writeout_inc(bdi
);
1126 wake_up(&fi
->page_waitq
);
1129 /* Called under fc->lock, may release and reacquire it */
1130 static void fuse_send_writepage(struct fuse_conn
*fc
, struct fuse_req
*req
)
1131 __releases(&fc
->lock
)
1132 __acquires(&fc
->lock
)
1134 struct fuse_inode
*fi
= get_fuse_inode(req
->inode
);
1135 loff_t size
= i_size_read(req
->inode
);
1136 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
1141 if (inarg
->offset
+ PAGE_CACHE_SIZE
<= size
) {
1142 inarg
->size
= PAGE_CACHE_SIZE
;
1143 } else if (inarg
->offset
< size
) {
1144 inarg
->size
= size
& (PAGE_CACHE_SIZE
- 1);
1146 /* Got truncated off completely */
1150 req
->in
.args
[1].size
= inarg
->size
;
1152 fuse_request_send_background_locked(fc
, req
);
1156 fuse_writepage_finish(fc
, req
);
1157 spin_unlock(&fc
->lock
);
1158 fuse_writepage_free(fc
, req
);
1159 fuse_put_request(fc
, req
);
1160 spin_lock(&fc
->lock
);
1164 * If fi->writectr is positive (no truncate or fsync going on) send
1165 * all queued writepage requests.
1167 * Called with fc->lock
1169 void fuse_flush_writepages(struct inode
*inode
)
1170 __releases(&fc
->lock
)
1171 __acquires(&fc
->lock
)
1173 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1174 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1175 struct fuse_req
*req
;
1177 while (fi
->writectr
>= 0 && !list_empty(&fi
->queued_writes
)) {
1178 req
= list_entry(fi
->queued_writes
.next
, struct fuse_req
, list
);
1179 list_del_init(&req
->list
);
1180 fuse_send_writepage(fc
, req
);
1184 static void fuse_writepage_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
1186 struct inode
*inode
= req
->inode
;
1187 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1189 mapping_set_error(inode
->i_mapping
, req
->out
.h
.error
);
1190 spin_lock(&fc
->lock
);
1192 fuse_writepage_finish(fc
, req
);
1193 spin_unlock(&fc
->lock
);
1194 fuse_writepage_free(fc
, req
);
1197 static int fuse_writepage_locked(struct page
*page
)
1199 struct address_space
*mapping
= page
->mapping
;
1200 struct inode
*inode
= mapping
->host
;
1201 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1202 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1203 struct fuse_req
*req
;
1204 struct fuse_file
*ff
;
1205 struct page
*tmp_page
;
1207 set_page_writeback(page
);
1209 req
= fuse_request_alloc_nofs();
1213 tmp_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
1217 spin_lock(&fc
->lock
);
1218 BUG_ON(list_empty(&fi
->write_files
));
1219 ff
= list_entry(fi
->write_files
.next
, struct fuse_file
, write_entry
);
1220 req
->ff
= fuse_file_get(ff
);
1221 spin_unlock(&fc
->lock
);
1223 fuse_write_fill(req
, ff
, page_offset(page
), 0);
1225 copy_highpage(tmp_page
, page
);
1226 req
->misc
.write
.in
.write_flags
|= FUSE_WRITE_CACHE
;
1227 req
->in
.argpages
= 1;
1229 req
->pages
[0] = tmp_page
;
1230 req
->page_offset
= 0;
1231 req
->end
= fuse_writepage_end
;
1234 inc_bdi_stat(mapping
->backing_dev_info
, BDI_WRITEBACK
);
1235 inc_zone_page_state(tmp_page
, NR_WRITEBACK_TEMP
);
1236 end_page_writeback(page
);
1238 spin_lock(&fc
->lock
);
1239 list_add(&req
->writepages_entry
, &fi
->writepages
);
1240 list_add_tail(&req
->list
, &fi
->queued_writes
);
1241 fuse_flush_writepages(inode
);
1242 spin_unlock(&fc
->lock
);
1247 fuse_request_free(req
);
1249 end_page_writeback(page
);
1253 static int fuse_writepage(struct page
*page
, struct writeback_control
*wbc
)
1257 err
= fuse_writepage_locked(page
);
1263 static int fuse_launder_page(struct page
*page
)
1266 if (clear_page_dirty_for_io(page
)) {
1267 struct inode
*inode
= page
->mapping
->host
;
1268 err
= fuse_writepage_locked(page
);
1270 fuse_wait_on_page_writeback(inode
, page
->index
);
1276 * Write back dirty pages now, because there may not be any suitable
1279 static void fuse_vma_close(struct vm_area_struct
*vma
)
1281 filemap_write_and_wait(vma
->vm_file
->f_mapping
);
1285 * Wait for writeback against this page to complete before allowing it
1286 * to be marked dirty again, and hence written back again, possibly
1287 * before the previous writepage completed.
1289 * Block here, instead of in ->writepage(), so that the userspace fs
1290 * can only block processes actually operating on the filesystem.
1292 * Otherwise unprivileged userspace fs would be able to block
1297 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
1299 static int fuse_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1301 struct page
*page
= vmf
->page
;
1303 * Don't use page->mapping as it may become NULL from a
1304 * concurrent truncate.
1306 struct inode
*inode
= vma
->vm_file
->f_mapping
->host
;
1308 fuse_wait_on_page_writeback(inode
, page
->index
);
1312 static struct vm_operations_struct fuse_file_vm_ops
= {
1313 .close
= fuse_vma_close
,
1314 .fault
= filemap_fault
,
1315 .page_mkwrite
= fuse_page_mkwrite
,
1318 static int fuse_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1320 if ((vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
)) {
1321 struct inode
*inode
= file
->f_dentry
->d_inode
;
1322 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1323 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1324 struct fuse_file
*ff
= file
->private_data
;
1326 * file may be written through mmap, so chain it onto the
1327 * inodes's write_file list
1329 spin_lock(&fc
->lock
);
1330 if (list_empty(&ff
->write_entry
))
1331 list_add(&ff
->write_entry
, &fi
->write_files
);
1332 spin_unlock(&fc
->lock
);
1334 file_accessed(file
);
1335 vma
->vm_ops
= &fuse_file_vm_ops
;
1339 static int fuse_direct_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1341 /* Can't provide the coherency needed for MAP_SHARED */
1342 if (vma
->vm_flags
& VM_MAYSHARE
)
1345 invalidate_inode_pages2(file
->f_mapping
);
1347 return generic_file_mmap(file
, vma
);
1350 static int convert_fuse_file_lock(const struct fuse_file_lock
*ffl
,
1351 struct file_lock
*fl
)
1353 switch (ffl
->type
) {
1359 if (ffl
->start
> OFFSET_MAX
|| ffl
->end
> OFFSET_MAX
||
1360 ffl
->end
< ffl
->start
)
1363 fl
->fl_start
= ffl
->start
;
1364 fl
->fl_end
= ffl
->end
;
1365 fl
->fl_pid
= ffl
->pid
;
1371 fl
->fl_type
= ffl
->type
;
1375 static void fuse_lk_fill(struct fuse_req
*req
, struct file
*file
,
1376 const struct file_lock
*fl
, int opcode
, pid_t pid
,
1379 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1380 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1381 struct fuse_file
*ff
= file
->private_data
;
1382 struct fuse_lk_in
*arg
= &req
->misc
.lk_in
;
1385 arg
->owner
= fuse_lock_owner_id(fc
, fl
->fl_owner
);
1386 arg
->lk
.start
= fl
->fl_start
;
1387 arg
->lk
.end
= fl
->fl_end
;
1388 arg
->lk
.type
= fl
->fl_type
;
1391 arg
->lk_flags
|= FUSE_LK_FLOCK
;
1392 req
->in
.h
.opcode
= opcode
;
1393 req
->in
.h
.nodeid
= get_node_id(inode
);
1394 req
->in
.numargs
= 1;
1395 req
->in
.args
[0].size
= sizeof(*arg
);
1396 req
->in
.args
[0].value
= arg
;
1399 static int fuse_getlk(struct file
*file
, struct file_lock
*fl
)
1401 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1402 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1403 struct fuse_req
*req
;
1404 struct fuse_lk_out outarg
;
1407 req
= fuse_get_req(fc
);
1409 return PTR_ERR(req
);
1411 fuse_lk_fill(req
, file
, fl
, FUSE_GETLK
, 0, 0);
1412 req
->out
.numargs
= 1;
1413 req
->out
.args
[0].size
= sizeof(outarg
);
1414 req
->out
.args
[0].value
= &outarg
;
1415 fuse_request_send(fc
, req
);
1416 err
= req
->out
.h
.error
;
1417 fuse_put_request(fc
, req
);
1419 err
= convert_fuse_file_lock(&outarg
.lk
, fl
);
1424 static int fuse_setlk(struct file
*file
, struct file_lock
*fl
, int flock
)
1426 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1427 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1428 struct fuse_req
*req
;
1429 int opcode
= (fl
->fl_flags
& FL_SLEEP
) ? FUSE_SETLKW
: FUSE_SETLK
;
1430 pid_t pid
= fl
->fl_type
!= F_UNLCK
? current
->tgid
: 0;
1433 if (fl
->fl_lmops
&& fl
->fl_lmops
->fl_grant
) {
1434 /* NLM needs asynchronous locks, which we don't support yet */
1438 /* Unlock on close is handled by the flush method */
1439 if (fl
->fl_flags
& FL_CLOSE
)
1442 req
= fuse_get_req(fc
);
1444 return PTR_ERR(req
);
1446 fuse_lk_fill(req
, file
, fl
, opcode
, pid
, flock
);
1447 fuse_request_send(fc
, req
);
1448 err
= req
->out
.h
.error
;
1449 /* locking is restartable */
1452 fuse_put_request(fc
, req
);
1456 static int fuse_file_lock(struct file
*file
, int cmd
, struct file_lock
*fl
)
1458 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1459 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1462 if (cmd
== F_CANCELLK
) {
1464 } else if (cmd
== F_GETLK
) {
1466 posix_test_lock(file
, fl
);
1469 err
= fuse_getlk(file
, fl
);
1472 err
= posix_lock_file(file
, fl
, NULL
);
1474 err
= fuse_setlk(file
, fl
, 0);
1479 static int fuse_file_flock(struct file
*file
, int cmd
, struct file_lock
*fl
)
1481 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1482 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1486 err
= flock_lock_file_wait(file
, fl
);
1488 /* emulate flock with POSIX locks */
1489 fl
->fl_owner
= (fl_owner_t
) file
;
1490 err
= fuse_setlk(file
, fl
, 1);
1496 static sector_t
fuse_bmap(struct address_space
*mapping
, sector_t block
)
1498 struct inode
*inode
= mapping
->host
;
1499 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1500 struct fuse_req
*req
;
1501 struct fuse_bmap_in inarg
;
1502 struct fuse_bmap_out outarg
;
1505 if (!inode
->i_sb
->s_bdev
|| fc
->no_bmap
)
1508 req
= fuse_get_req(fc
);
1512 memset(&inarg
, 0, sizeof(inarg
));
1513 inarg
.block
= block
;
1514 inarg
.blocksize
= inode
->i_sb
->s_blocksize
;
1515 req
->in
.h
.opcode
= FUSE_BMAP
;
1516 req
->in
.h
.nodeid
= get_node_id(inode
);
1517 req
->in
.numargs
= 1;
1518 req
->in
.args
[0].size
= sizeof(inarg
);
1519 req
->in
.args
[0].value
= &inarg
;
1520 req
->out
.numargs
= 1;
1521 req
->out
.args
[0].size
= sizeof(outarg
);
1522 req
->out
.args
[0].value
= &outarg
;
1523 fuse_request_send(fc
, req
);
1524 err
= req
->out
.h
.error
;
1525 fuse_put_request(fc
, req
);
1529 return err
? 0 : outarg
.block
;
1532 static loff_t
fuse_file_llseek(struct file
*file
, loff_t offset
, int origin
)
1535 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1537 mutex_lock(&inode
->i_mutex
);
1540 retval
= fuse_update_attributes(inode
, NULL
, file
, NULL
);
1543 offset
+= i_size_read(inode
);
1546 offset
+= file
->f_pos
;
1549 if (offset
>= 0 && offset
<= inode
->i_sb
->s_maxbytes
) {
1550 if (offset
!= file
->f_pos
) {
1551 file
->f_pos
= offset
;
1552 file
->f_version
= 0;
1557 mutex_unlock(&inode
->i_mutex
);
1561 static int fuse_ioctl_copy_user(struct page
**pages
, struct iovec
*iov
,
1562 unsigned int nr_segs
, size_t bytes
, bool to_user
)
1570 iov_iter_init(&ii
, iov
, nr_segs
, bytes
, 0);
1572 while (iov_iter_count(&ii
)) {
1573 struct page
*page
= pages
[page_idx
++];
1574 size_t todo
= min_t(size_t, PAGE_SIZE
, iov_iter_count(&ii
));
1577 kaddr
= map
= kmap(page
);
1580 char __user
*uaddr
= ii
.iov
->iov_base
+ ii
.iov_offset
;
1581 size_t iov_len
= ii
.iov
->iov_len
- ii
.iov_offset
;
1582 size_t copy
= min(todo
, iov_len
);
1586 left
= copy_from_user(kaddr
, uaddr
, copy
);
1588 left
= copy_to_user(uaddr
, kaddr
, copy
);
1593 iov_iter_advance(&ii
, copy
);
1605 * For ioctls, there is no generic way to determine how much memory
1606 * needs to be read and/or written. Furthermore, ioctls are allowed
1607 * to dereference the passed pointer, so the parameter requires deep
1608 * copying but FUSE has no idea whatsoever about what to copy in or
1611 * This is solved by allowing FUSE server to retry ioctl with
1612 * necessary in/out iovecs. Let's assume the ioctl implementation
1613 * needs to read in the following structure.
1620 * On the first callout to FUSE server, inarg->in_size and
1621 * inarg->out_size will be NULL; then, the server completes the ioctl
1622 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
1623 * the actual iov array to
1625 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
1627 * which tells FUSE to copy in the requested area and retry the ioctl.
1628 * On the second round, the server has access to the structure and
1629 * from that it can tell what to look for next, so on the invocation,
1630 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
1632 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
1633 * { .iov_base = a.buf, .iov_len = a.buflen } }
1635 * FUSE will copy both struct a and the pointed buffer from the
1636 * process doing the ioctl and retry ioctl with both struct a and the
1639 * This time, FUSE server has everything it needs and completes ioctl
1640 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
1642 * Copying data out works the same way.
1644 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
1645 * automatically initializes in and out iovs by decoding @cmd with
1646 * _IOC_* macros and the server is not allowed to request RETRY. This
1647 * limits ioctl data transfers to well-formed ioctls and is the forced
1648 * behavior for all FUSE servers.
1650 static long fuse_do_ioctl(struct file
*file
, unsigned int cmd
,
1651 unsigned long arg
, unsigned int flags
)
1653 struct fuse_file
*ff
= file
->private_data
;
1654 struct fuse_conn
*fc
= ff
->fc
;
1655 struct fuse_ioctl_in inarg
= {
1661 struct fuse_ioctl_out outarg
;
1662 struct fuse_req
*req
= NULL
;
1663 struct page
**pages
= NULL
;
1664 struct page
*iov_page
= NULL
;
1665 struct iovec
*in_iov
= NULL
, *out_iov
= NULL
;
1666 unsigned int in_iovs
= 0, out_iovs
= 0, num_pages
= 0, max_pages
;
1667 size_t in_size
, out_size
, transferred
;
1670 /* assume all the iovs returned by client always fits in a page */
1671 BUILD_BUG_ON(sizeof(struct iovec
) * FUSE_IOCTL_MAX_IOV
> PAGE_SIZE
);
1674 pages
= kzalloc(sizeof(pages
[0]) * FUSE_MAX_PAGES_PER_REQ
, GFP_KERNEL
);
1675 iov_page
= alloc_page(GFP_KERNEL
);
1676 if (!pages
|| !iov_page
)
1680 * If restricted, initialize IO parameters as encoded in @cmd.
1681 * RETRY from server is not allowed.
1683 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
)) {
1684 struct iovec
*iov
= page_address(iov_page
);
1686 iov
->iov_base
= (void __user
*)arg
;
1687 iov
->iov_len
= _IOC_SIZE(cmd
);
1689 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
1694 if (_IOC_DIR(cmd
) & _IOC_READ
) {
1701 inarg
.in_size
= in_size
= iov_length(in_iov
, in_iovs
);
1702 inarg
.out_size
= out_size
= iov_length(out_iov
, out_iovs
);
1705 * Out data can be used either for actual out data or iovs,
1706 * make sure there always is at least one page.
1708 out_size
= max_t(size_t, out_size
, PAGE_SIZE
);
1709 max_pages
= DIV_ROUND_UP(max(in_size
, out_size
), PAGE_SIZE
);
1711 /* make sure there are enough buffer pages and init request with them */
1713 if (max_pages
> FUSE_MAX_PAGES_PER_REQ
)
1715 while (num_pages
< max_pages
) {
1716 pages
[num_pages
] = alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
1717 if (!pages
[num_pages
])
1722 req
= fuse_get_req(fc
);
1728 memcpy(req
->pages
, pages
, sizeof(req
->pages
[0]) * num_pages
);
1729 req
->num_pages
= num_pages
;
1731 /* okay, let's send it to the client */
1732 req
->in
.h
.opcode
= FUSE_IOCTL
;
1733 req
->in
.h
.nodeid
= ff
->nodeid
;
1734 req
->in
.numargs
= 1;
1735 req
->in
.args
[0].size
= sizeof(inarg
);
1736 req
->in
.args
[0].value
= &inarg
;
1739 req
->in
.args
[1].size
= in_size
;
1740 req
->in
.argpages
= 1;
1742 err
= fuse_ioctl_copy_user(pages
, in_iov
, in_iovs
, in_size
,
1748 req
->out
.numargs
= 2;
1749 req
->out
.args
[0].size
= sizeof(outarg
);
1750 req
->out
.args
[0].value
= &outarg
;
1751 req
->out
.args
[1].size
= out_size
;
1752 req
->out
.argpages
= 1;
1753 req
->out
.argvar
= 1;
1755 fuse_request_send(fc
, req
);
1756 err
= req
->out
.h
.error
;
1757 transferred
= req
->out
.args
[1].size
;
1758 fuse_put_request(fc
, req
);
1763 /* did it ask for retry? */
1764 if (outarg
.flags
& FUSE_IOCTL_RETRY
) {
1767 /* no retry if in restricted mode */
1769 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
))
1772 in_iovs
= outarg
.in_iovs
;
1773 out_iovs
= outarg
.out_iovs
;
1776 * Make sure things are in boundary, separate checks
1777 * are to protect against overflow.
1780 if (in_iovs
> FUSE_IOCTL_MAX_IOV
||
1781 out_iovs
> FUSE_IOCTL_MAX_IOV
||
1782 in_iovs
+ out_iovs
> FUSE_IOCTL_MAX_IOV
)
1786 if ((in_iovs
+ out_iovs
) * sizeof(struct iovec
) != transferred
)
1789 /* okay, copy in iovs and retry */
1790 vaddr
= kmap_atomic(pages
[0], KM_USER0
);
1791 memcpy(page_address(iov_page
), vaddr
, transferred
);
1792 kunmap_atomic(vaddr
, KM_USER0
);
1794 in_iov
= page_address(iov_page
);
1795 out_iov
= in_iov
+ in_iovs
;
1801 if (transferred
> inarg
.out_size
)
1804 err
= fuse_ioctl_copy_user(pages
, out_iov
, out_iovs
, transferred
, true);
1807 fuse_put_request(fc
, req
);
1809 __free_page(iov_page
);
1811 __free_page(pages
[--num_pages
]);
1814 return err
? err
: outarg
.result
;
1817 static long fuse_file_ioctl_common(struct file
*file
, unsigned int cmd
,
1818 unsigned long arg
, unsigned int flags
)
1820 struct inode
*inode
= file
->f_dentry
->d_inode
;
1821 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1823 if (!fuse_allow_task(fc
, current
))
1826 if (is_bad_inode(inode
))
1829 return fuse_do_ioctl(file
, cmd
, arg
, flags
);
1832 static long fuse_file_ioctl(struct file
*file
, unsigned int cmd
,
1835 return fuse_file_ioctl_common(file
, cmd
, arg
, 0);
1838 static long fuse_file_compat_ioctl(struct file
*file
, unsigned int cmd
,
1841 return fuse_file_ioctl_common(file
, cmd
, arg
, FUSE_IOCTL_COMPAT
);
1845 * All files which have been polled are linked to RB tree
1846 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
1847 * find the matching one.
1849 static struct rb_node
**fuse_find_polled_node(struct fuse_conn
*fc
, u64 kh
,
1850 struct rb_node
**parent_out
)
1852 struct rb_node
**link
= &fc
->polled_files
.rb_node
;
1853 struct rb_node
*last
= NULL
;
1856 struct fuse_file
*ff
;
1859 ff
= rb_entry(last
, struct fuse_file
, polled_node
);
1862 link
= &last
->rb_left
;
1863 else if (kh
> ff
->kh
)
1864 link
= &last
->rb_right
;
1875 * The file is about to be polled. Make sure it's on the polled_files
1876 * RB tree. Note that files once added to the polled_files tree are
1877 * not removed before the file is released. This is because a file
1878 * polled once is likely to be polled again.
1880 static void fuse_register_polled_file(struct fuse_conn
*fc
,
1881 struct fuse_file
*ff
)
1883 spin_lock(&fc
->lock
);
1884 if (RB_EMPTY_NODE(&ff
->polled_node
)) {
1885 struct rb_node
**link
, *parent
;
1887 link
= fuse_find_polled_node(fc
, ff
->kh
, &parent
);
1889 rb_link_node(&ff
->polled_node
, parent
, link
);
1890 rb_insert_color(&ff
->polled_node
, &fc
->polled_files
);
1892 spin_unlock(&fc
->lock
);
1895 static unsigned fuse_file_poll(struct file
*file
, poll_table
*wait
)
1897 struct fuse_file
*ff
= file
->private_data
;
1898 struct fuse_conn
*fc
= ff
->fc
;
1899 struct fuse_poll_in inarg
= { .fh
= ff
->fh
, .kh
= ff
->kh
};
1900 struct fuse_poll_out outarg
;
1901 struct fuse_req
*req
;
1905 return DEFAULT_POLLMASK
;
1907 poll_wait(file
, &ff
->poll_wait
, wait
);
1910 * Ask for notification iff there's someone waiting for it.
1911 * The client may ignore the flag and always notify.
1913 if (waitqueue_active(&ff
->poll_wait
)) {
1914 inarg
.flags
|= FUSE_POLL_SCHEDULE_NOTIFY
;
1915 fuse_register_polled_file(fc
, ff
);
1918 req
= fuse_get_req(fc
);
1920 return PTR_ERR(req
);
1922 req
->in
.h
.opcode
= FUSE_POLL
;
1923 req
->in
.h
.nodeid
= ff
->nodeid
;
1924 req
->in
.numargs
= 1;
1925 req
->in
.args
[0].size
= sizeof(inarg
);
1926 req
->in
.args
[0].value
= &inarg
;
1927 req
->out
.numargs
= 1;
1928 req
->out
.args
[0].size
= sizeof(outarg
);
1929 req
->out
.args
[0].value
= &outarg
;
1930 fuse_request_send(fc
, req
);
1931 err
= req
->out
.h
.error
;
1932 fuse_put_request(fc
, req
);
1935 return outarg
.revents
;
1936 if (err
== -ENOSYS
) {
1938 return DEFAULT_POLLMASK
;
1944 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
1945 * wakes up the poll waiters.
1947 int fuse_notify_poll_wakeup(struct fuse_conn
*fc
,
1948 struct fuse_notify_poll_wakeup_out
*outarg
)
1950 u64 kh
= outarg
->kh
;
1951 struct rb_node
**link
;
1953 spin_lock(&fc
->lock
);
1955 link
= fuse_find_polled_node(fc
, kh
, NULL
);
1957 struct fuse_file
*ff
;
1959 ff
= rb_entry(*link
, struct fuse_file
, polled_node
);
1960 wake_up_interruptible_sync(&ff
->poll_wait
);
1963 spin_unlock(&fc
->lock
);
1967 static const struct file_operations fuse_file_operations
= {
1968 .llseek
= fuse_file_llseek
,
1969 .read
= do_sync_read
,
1970 .aio_read
= fuse_file_aio_read
,
1971 .write
= do_sync_write
,
1972 .aio_write
= fuse_file_aio_write
,
1973 .mmap
= fuse_file_mmap
,
1975 .flush
= fuse_flush
,
1976 .release
= fuse_release
,
1977 .fsync
= fuse_fsync
,
1978 .lock
= fuse_file_lock
,
1979 .flock
= fuse_file_flock
,
1980 .splice_read
= generic_file_splice_read
,
1981 .unlocked_ioctl
= fuse_file_ioctl
,
1982 .compat_ioctl
= fuse_file_compat_ioctl
,
1983 .poll
= fuse_file_poll
,
1986 static const struct file_operations fuse_direct_io_file_operations
= {
1987 .llseek
= fuse_file_llseek
,
1988 .read
= fuse_direct_read
,
1989 .write
= fuse_direct_write
,
1990 .mmap
= fuse_direct_mmap
,
1992 .flush
= fuse_flush
,
1993 .release
= fuse_release
,
1994 .fsync
= fuse_fsync
,
1995 .lock
= fuse_file_lock
,
1996 .flock
= fuse_file_flock
,
1997 .unlocked_ioctl
= fuse_file_ioctl
,
1998 .compat_ioctl
= fuse_file_compat_ioctl
,
1999 .poll
= fuse_file_poll
,
2000 /* no splice_read */
2003 static const struct address_space_operations fuse_file_aops
= {
2004 .readpage
= fuse_readpage
,
2005 .writepage
= fuse_writepage
,
2006 .launder_page
= fuse_launder_page
,
2007 .write_begin
= fuse_write_begin
,
2008 .write_end
= fuse_write_end
,
2009 .readpages
= fuse_readpages
,
2010 .set_page_dirty
= __set_page_dirty_nobuffers
,
2014 void fuse_init_file_inode(struct inode
*inode
)
2016 inode
->i_fop
= &fuse_file_operations
;
2017 inode
->i_data
.a_ops
= &fuse_file_aops
;