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>
17 static const struct file_operations fuse_direct_io_file_operations
;
19 static int fuse_send_open(struct fuse_conn
*fc
, u64 nodeid
, struct file
*file
,
20 int opcode
, struct fuse_open_out
*outargp
)
22 struct fuse_open_in inarg
;
26 req
= fuse_get_req(fc
);
30 memset(&inarg
, 0, sizeof(inarg
));
31 inarg
.flags
= file
->f_flags
& ~(O_CREAT
| O_EXCL
| O_NOCTTY
);
32 if (!fc
->atomic_o_trunc
)
33 inarg
.flags
&= ~O_TRUNC
;
34 req
->in
.h
.opcode
= opcode
;
35 req
->in
.h
.nodeid
= nodeid
;
37 req
->in
.args
[0].size
= sizeof(inarg
);
38 req
->in
.args
[0].value
= &inarg
;
40 req
->out
.args
[0].size
= sizeof(*outargp
);
41 req
->out
.args
[0].value
= outargp
;
42 fuse_request_send(fc
, req
);
43 err
= req
->out
.h
.error
;
44 fuse_put_request(fc
, req
);
49 struct fuse_file
*fuse_file_alloc(struct fuse_conn
*fc
)
53 ff
= kmalloc(sizeof(struct fuse_file
), GFP_KERNEL
);
58 ff
->reserved_req
= fuse_request_alloc();
59 if (unlikely(!ff
->reserved_req
)) {
64 INIT_LIST_HEAD(&ff
->write_entry
);
65 atomic_set(&ff
->count
, 0);
66 RB_CLEAR_NODE(&ff
->polled_node
);
67 init_waitqueue_head(&ff
->poll_wait
);
71 spin_unlock(&fc
->lock
);
76 void fuse_file_free(struct fuse_file
*ff
)
78 fuse_request_free(ff
->reserved_req
);
82 struct fuse_file
*fuse_file_get(struct fuse_file
*ff
)
84 atomic_inc(&ff
->count
);
88 static void fuse_release_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
90 path_put(&req
->misc
.release
.path
);
93 static void fuse_file_put(struct fuse_file
*ff
)
95 if (atomic_dec_and_test(&ff
->count
)) {
96 struct fuse_req
*req
= ff
->reserved_req
;
98 req
->end
= fuse_release_end
;
99 fuse_request_send_background(ff
->fc
, req
);
104 int fuse_do_open(struct fuse_conn
*fc
, u64 nodeid
, struct file
*file
,
107 struct fuse_open_out outarg
;
108 struct fuse_file
*ff
;
110 int opcode
= isdir
? FUSE_OPENDIR
: FUSE_OPEN
;
112 ff
= fuse_file_alloc(fc
);
116 err
= fuse_send_open(fc
, nodeid
, file
, opcode
, &outarg
);
123 outarg
.open_flags
&= ~FOPEN_DIRECT_IO
;
127 ff
->open_flags
= outarg
.open_flags
;
128 file
->private_data
= fuse_file_get(ff
);
132 EXPORT_SYMBOL_GPL(fuse_do_open
);
134 void fuse_finish_open(struct inode
*inode
, struct file
*file
)
136 struct fuse_file
*ff
= file
->private_data
;
138 if (ff
->open_flags
& FOPEN_DIRECT_IO
)
139 file
->f_op
= &fuse_direct_io_file_operations
;
140 if (!(ff
->open_flags
& FOPEN_KEEP_CACHE
))
141 invalidate_inode_pages2(inode
->i_mapping
);
142 if (ff
->open_flags
& FOPEN_NONSEEKABLE
)
143 nonseekable_open(inode
, file
);
146 int fuse_open_common(struct inode
*inode
, struct file
*file
, bool isdir
)
148 struct fuse_conn
*fc
= get_fuse_conn(inode
);
151 /* VFS checks this, but only _after_ ->open() */
152 if (file
->f_flags
& O_DIRECT
)
155 err
= generic_file_open(inode
, file
);
159 err
= fuse_do_open(fc
, get_node_id(inode
), file
, isdir
);
163 fuse_finish_open(inode
, file
);
168 static void fuse_prepare_release(struct fuse_file
*ff
, int flags
, int opcode
)
170 struct fuse_conn
*fc
= ff
->fc
;
171 struct fuse_req
*req
= ff
->reserved_req
;
172 struct fuse_release_in
*inarg
= &req
->misc
.release
.in
;
174 spin_lock(&fc
->lock
);
175 list_del(&ff
->write_entry
);
176 if (!RB_EMPTY_NODE(&ff
->polled_node
))
177 rb_erase(&ff
->polled_node
, &fc
->polled_files
);
178 spin_unlock(&fc
->lock
);
180 wake_up_interruptible_sync(&ff
->poll_wait
);
183 inarg
->flags
= flags
;
184 req
->in
.h
.opcode
= opcode
;
185 req
->in
.h
.nodeid
= ff
->nodeid
;
187 req
->in
.args
[0].size
= sizeof(struct fuse_release_in
);
188 req
->in
.args
[0].value
= inarg
;
191 void fuse_release_common(struct file
*file
, int opcode
)
193 struct fuse_file
*ff
;
194 struct fuse_req
*req
;
196 ff
= file
->private_data
;
200 req
= ff
->reserved_req
;
201 fuse_prepare_release(ff
, file
->f_flags
, opcode
);
203 /* Hold vfsmount and dentry until release is finished */
204 path_get(&file
->f_path
);
205 req
->misc
.release
.path
= file
->f_path
;
208 * Normally this will send the RELEASE request, however if
209 * some asynchronous READ or WRITE requests are outstanding,
210 * the sending will be delayed.
215 static int fuse_open(struct inode
*inode
, struct file
*file
)
217 return fuse_open_common(inode
, file
, false);
220 static int fuse_release(struct inode
*inode
, struct file
*file
)
222 fuse_release_common(file
, FUSE_RELEASE
);
224 /* return value is ignored by VFS */
228 void fuse_sync_release(struct fuse_file
*ff
, int flags
)
230 WARN_ON(atomic_read(&ff
->count
) > 1);
231 fuse_prepare_release(ff
, flags
, FUSE_RELEASE
);
232 ff
->reserved_req
->force
= 1;
233 fuse_request_send(ff
->fc
, ff
->reserved_req
);
234 fuse_put_request(ff
->fc
, ff
->reserved_req
);
237 EXPORT_SYMBOL_GPL(fuse_sync_release
);
240 * Scramble the ID space with XTEA, so that the value of the files_struct
241 * pointer is not exposed to userspace.
243 u64
fuse_lock_owner_id(struct fuse_conn
*fc
, fl_owner_t id
)
245 u32
*k
= fc
->scramble_key
;
246 u64 v
= (unsigned long) id
;
252 for (i
= 0; i
< 32; i
++) {
253 v0
+= ((v1
<< 4 ^ v1
>> 5) + v1
) ^ (sum
+ k
[sum
& 3]);
255 v1
+= ((v0
<< 4 ^ v0
>> 5) + v0
) ^ (sum
+ k
[sum
>>11 & 3]);
258 return (u64
) v0
+ ((u64
) v1
<< 32);
262 * Check if page is under writeback
264 * This is currently done by walking the list of writepage requests
265 * for the inode, which can be pretty inefficient.
267 static bool fuse_page_is_writeback(struct inode
*inode
, pgoff_t index
)
269 struct fuse_conn
*fc
= get_fuse_conn(inode
);
270 struct fuse_inode
*fi
= get_fuse_inode(inode
);
271 struct fuse_req
*req
;
274 spin_lock(&fc
->lock
);
275 list_for_each_entry(req
, &fi
->writepages
, writepages_entry
) {
278 BUG_ON(req
->inode
!= inode
);
279 curr_index
= req
->misc
.write
.in
.offset
>> PAGE_CACHE_SHIFT
;
280 if (curr_index
== index
) {
285 spin_unlock(&fc
->lock
);
291 * Wait for page writeback to be completed.
293 * Since fuse doesn't rely on the VM writeback tracking, this has to
294 * use some other means.
296 static int fuse_wait_on_page_writeback(struct inode
*inode
, pgoff_t index
)
298 struct fuse_inode
*fi
= get_fuse_inode(inode
);
300 wait_event(fi
->page_waitq
, !fuse_page_is_writeback(inode
, index
));
304 static int fuse_flush(struct file
*file
, fl_owner_t id
)
306 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
307 struct fuse_conn
*fc
= get_fuse_conn(inode
);
308 struct fuse_file
*ff
= file
->private_data
;
309 struct fuse_req
*req
;
310 struct fuse_flush_in inarg
;
313 if (is_bad_inode(inode
))
319 req
= fuse_get_req_nofail(fc
, file
);
320 memset(&inarg
, 0, sizeof(inarg
));
322 inarg
.lock_owner
= fuse_lock_owner_id(fc
, id
);
323 req
->in
.h
.opcode
= FUSE_FLUSH
;
324 req
->in
.h
.nodeid
= get_node_id(inode
);
326 req
->in
.args
[0].size
= sizeof(inarg
);
327 req
->in
.args
[0].value
= &inarg
;
329 fuse_request_send(fc
, req
);
330 err
= req
->out
.h
.error
;
331 fuse_put_request(fc
, req
);
332 if (err
== -ENOSYS
) {
340 * Wait for all pending writepages on the inode to finish.
342 * This is currently done by blocking further writes with FUSE_NOWRITE
343 * and waiting for all sent writes to complete.
345 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
346 * could conflict with truncation.
348 static void fuse_sync_writes(struct inode
*inode
)
350 fuse_set_nowrite(inode
);
351 fuse_release_nowrite(inode
);
354 int fuse_fsync_common(struct file
*file
, struct dentry
*de
, int datasync
,
357 struct inode
*inode
= de
->d_inode
;
358 struct fuse_conn
*fc
= get_fuse_conn(inode
);
359 struct fuse_file
*ff
= file
->private_data
;
360 struct fuse_req
*req
;
361 struct fuse_fsync_in inarg
;
364 if (is_bad_inode(inode
))
367 if ((!isdir
&& fc
->no_fsync
) || (isdir
&& fc
->no_fsyncdir
))
371 * Start writeback against all dirty pages of the inode, then
372 * wait for all outstanding writes, before sending the FSYNC
375 err
= write_inode_now(inode
, 0);
379 fuse_sync_writes(inode
);
381 req
= fuse_get_req(fc
);
385 memset(&inarg
, 0, sizeof(inarg
));
387 inarg
.fsync_flags
= datasync
? 1 : 0;
388 req
->in
.h
.opcode
= isdir
? FUSE_FSYNCDIR
: FUSE_FSYNC
;
389 req
->in
.h
.nodeid
= get_node_id(inode
);
391 req
->in
.args
[0].size
= sizeof(inarg
);
392 req
->in
.args
[0].value
= &inarg
;
393 fuse_request_send(fc
, req
);
394 err
= req
->out
.h
.error
;
395 fuse_put_request(fc
, req
);
396 if (err
== -ENOSYS
) {
406 static int fuse_fsync(struct file
*file
, struct dentry
*de
, int datasync
)
408 return fuse_fsync_common(file
, de
, datasync
, 0);
411 void fuse_read_fill(struct fuse_req
*req
, struct file
*file
, loff_t pos
,
412 size_t count
, int opcode
)
414 struct fuse_read_in
*inarg
= &req
->misc
.read
.in
;
415 struct fuse_file
*ff
= file
->private_data
;
420 inarg
->flags
= file
->f_flags
;
421 req
->in
.h
.opcode
= opcode
;
422 req
->in
.h
.nodeid
= ff
->nodeid
;
424 req
->in
.args
[0].size
= sizeof(struct fuse_read_in
);
425 req
->in
.args
[0].value
= inarg
;
427 req
->out
.numargs
= 1;
428 req
->out
.args
[0].size
= count
;
431 static size_t fuse_send_read(struct fuse_req
*req
, struct file
*file
,
432 loff_t pos
, size_t count
, fl_owner_t owner
)
434 struct fuse_file
*ff
= file
->private_data
;
435 struct fuse_conn
*fc
= ff
->fc
;
437 fuse_read_fill(req
, file
, pos
, count
, FUSE_READ
);
439 struct fuse_read_in
*inarg
= &req
->misc
.read
.in
;
441 inarg
->read_flags
|= FUSE_READ_LOCKOWNER
;
442 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
444 fuse_request_send(fc
, req
);
445 return req
->out
.args
[0].size
;
448 static void fuse_read_update_size(struct inode
*inode
, loff_t size
,
451 struct fuse_conn
*fc
= get_fuse_conn(inode
);
452 struct fuse_inode
*fi
= get_fuse_inode(inode
);
454 spin_lock(&fc
->lock
);
455 if (attr_ver
== fi
->attr_version
&& size
< inode
->i_size
) {
456 fi
->attr_version
= ++fc
->attr_version
;
457 i_size_write(inode
, size
);
459 spin_unlock(&fc
->lock
);
462 static int fuse_readpage(struct file
*file
, struct page
*page
)
464 struct inode
*inode
= page
->mapping
->host
;
465 struct fuse_conn
*fc
= get_fuse_conn(inode
);
466 struct fuse_req
*req
;
468 loff_t pos
= page_offset(page
);
469 size_t count
= PAGE_CACHE_SIZE
;
474 if (is_bad_inode(inode
))
478 * Page writeback can extend beyond the liftime of the
479 * page-cache page, so make sure we read a properly synced
482 fuse_wait_on_page_writeback(inode
, page
->index
);
484 req
= fuse_get_req(fc
);
489 attr_ver
= fuse_get_attr_version(fc
);
491 req
->out
.page_zeroing
= 1;
492 req
->out
.argpages
= 1;
494 req
->pages
[0] = page
;
495 num_read
= fuse_send_read(req
, file
, pos
, count
, NULL
);
496 err
= req
->out
.h
.error
;
497 fuse_put_request(fc
, req
);
501 * Short read means EOF. If file size is larger, truncate it
503 if (num_read
< count
)
504 fuse_read_update_size(inode
, pos
+ num_read
, attr_ver
);
506 SetPageUptodate(page
);
509 fuse_invalidate_attr(inode
); /* atime changed */
515 static void fuse_readpages_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
518 size_t count
= req
->misc
.read
.in
.size
;
519 size_t num_read
= req
->out
.args
[0].size
;
520 struct address_space
*mapping
= NULL
;
522 for (i
= 0; mapping
== NULL
&& i
< req
->num_pages
; i
++)
523 mapping
= req
->pages
[i
]->mapping
;
526 struct inode
*inode
= mapping
->host
;
529 * Short read means EOF. If file size is larger, truncate it
531 if (!req
->out
.h
.error
&& num_read
< count
) {
534 pos
= page_offset(req
->pages
[0]) + num_read
;
535 fuse_read_update_size(inode
, pos
,
536 req
->misc
.read
.attr_ver
);
538 fuse_invalidate_attr(inode
); /* atime changed */
541 for (i
= 0; i
< req
->num_pages
; i
++) {
542 struct page
*page
= req
->pages
[i
];
543 if (!req
->out
.h
.error
)
544 SetPageUptodate(page
);
548 page_cache_release(page
);
551 fuse_file_put(req
->ff
);
554 static void fuse_send_readpages(struct fuse_req
*req
, struct file
*file
)
556 struct fuse_file
*ff
= file
->private_data
;
557 struct fuse_conn
*fc
= ff
->fc
;
558 loff_t pos
= page_offset(req
->pages
[0]);
559 size_t count
= req
->num_pages
<< PAGE_CACHE_SHIFT
;
561 req
->out
.argpages
= 1;
562 req
->out
.page_zeroing
= 1;
563 req
->out
.page_replace
= 1;
564 fuse_read_fill(req
, file
, pos
, count
, FUSE_READ
);
565 req
->misc
.read
.attr_ver
= fuse_get_attr_version(fc
);
566 if (fc
->async_read
) {
567 req
->ff
= fuse_file_get(ff
);
568 req
->end
= fuse_readpages_end
;
569 fuse_request_send_background(fc
, req
);
571 fuse_request_send(fc
, req
);
572 fuse_readpages_end(fc
, req
);
573 fuse_put_request(fc
, req
);
577 struct fuse_fill_data
{
578 struct fuse_req
*req
;
583 static int fuse_readpages_fill(void *_data
, struct page
*page
)
585 struct fuse_fill_data
*data
= _data
;
586 struct fuse_req
*req
= data
->req
;
587 struct inode
*inode
= data
->inode
;
588 struct fuse_conn
*fc
= get_fuse_conn(inode
);
590 fuse_wait_on_page_writeback(inode
, page
->index
);
592 if (req
->num_pages
&&
593 (req
->num_pages
== FUSE_MAX_PAGES_PER_REQ
||
594 (req
->num_pages
+ 1) * PAGE_CACHE_SIZE
> fc
->max_read
||
595 req
->pages
[req
->num_pages
- 1]->index
+ 1 != page
->index
)) {
596 fuse_send_readpages(req
, data
->file
);
597 data
->req
= req
= fuse_get_req(fc
);
603 page_cache_get(page
);
604 req
->pages
[req
->num_pages
] = page
;
609 static int fuse_readpages(struct file
*file
, struct address_space
*mapping
,
610 struct list_head
*pages
, unsigned nr_pages
)
612 struct inode
*inode
= mapping
->host
;
613 struct fuse_conn
*fc
= get_fuse_conn(inode
);
614 struct fuse_fill_data data
;
618 if (is_bad_inode(inode
))
623 data
.req
= fuse_get_req(fc
);
624 err
= PTR_ERR(data
.req
);
625 if (IS_ERR(data
.req
))
628 err
= read_cache_pages(mapping
, pages
, fuse_readpages_fill
, &data
);
630 if (data
.req
->num_pages
)
631 fuse_send_readpages(data
.req
, file
);
633 fuse_put_request(fc
, data
.req
);
639 static ssize_t
fuse_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
640 unsigned long nr_segs
, loff_t pos
)
642 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
644 if (pos
+ iov_length(iov
, nr_segs
) > i_size_read(inode
)) {
647 * If trying to read past EOF, make sure the i_size
648 * attribute is up-to-date.
650 err
= fuse_update_attributes(inode
, NULL
, iocb
->ki_filp
, NULL
);
655 return generic_file_aio_read(iocb
, iov
, nr_segs
, pos
);
658 static void fuse_write_fill(struct fuse_req
*req
, struct fuse_file
*ff
,
659 loff_t pos
, size_t count
)
661 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
662 struct fuse_write_out
*outarg
= &req
->misc
.write
.out
;
667 req
->in
.h
.opcode
= FUSE_WRITE
;
668 req
->in
.h
.nodeid
= ff
->nodeid
;
670 if (ff
->fc
->minor
< 9)
671 req
->in
.args
[0].size
= FUSE_COMPAT_WRITE_IN_SIZE
;
673 req
->in
.args
[0].size
= sizeof(struct fuse_write_in
);
674 req
->in
.args
[0].value
= inarg
;
675 req
->in
.args
[1].size
= count
;
676 req
->out
.numargs
= 1;
677 req
->out
.args
[0].size
= sizeof(struct fuse_write_out
);
678 req
->out
.args
[0].value
= outarg
;
681 static size_t fuse_send_write(struct fuse_req
*req
, struct file
*file
,
682 loff_t pos
, size_t count
, fl_owner_t owner
)
684 struct fuse_file
*ff
= file
->private_data
;
685 struct fuse_conn
*fc
= ff
->fc
;
686 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
688 fuse_write_fill(req
, ff
, pos
, count
);
689 inarg
->flags
= file
->f_flags
;
691 inarg
->write_flags
|= FUSE_WRITE_LOCKOWNER
;
692 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
694 fuse_request_send(fc
, req
);
695 return req
->misc
.write
.out
.size
;
698 static int fuse_write_begin(struct file
*file
, struct address_space
*mapping
,
699 loff_t pos
, unsigned len
, unsigned flags
,
700 struct page
**pagep
, void **fsdata
)
702 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
704 *pagep
= grab_cache_page_write_begin(mapping
, index
, flags
);
710 static void fuse_write_update_size(struct inode
*inode
, loff_t pos
)
712 struct fuse_conn
*fc
= get_fuse_conn(inode
);
713 struct fuse_inode
*fi
= get_fuse_inode(inode
);
715 spin_lock(&fc
->lock
);
716 fi
->attr_version
= ++fc
->attr_version
;
717 if (pos
> inode
->i_size
)
718 i_size_write(inode
, pos
);
719 spin_unlock(&fc
->lock
);
722 static int fuse_buffered_write(struct file
*file
, struct inode
*inode
,
723 loff_t pos
, unsigned count
, struct page
*page
)
727 struct fuse_conn
*fc
= get_fuse_conn(inode
);
728 unsigned offset
= pos
& (PAGE_CACHE_SIZE
- 1);
729 struct fuse_req
*req
;
731 if (is_bad_inode(inode
))
735 * Make sure writepages on the same page are not mixed up with
738 fuse_wait_on_page_writeback(inode
, page
->index
);
740 req
= fuse_get_req(fc
);
744 req
->in
.argpages
= 1;
746 req
->pages
[0] = page
;
747 req
->page_offset
= offset
;
748 nres
= fuse_send_write(req
, file
, pos
, count
, NULL
);
749 err
= req
->out
.h
.error
;
750 fuse_put_request(fc
, req
);
755 fuse_write_update_size(inode
, pos
);
756 if (count
== PAGE_CACHE_SIZE
)
757 SetPageUptodate(page
);
759 fuse_invalidate_attr(inode
);
760 return err
? err
: nres
;
763 static int fuse_write_end(struct file
*file
, struct address_space
*mapping
,
764 loff_t pos
, unsigned len
, unsigned copied
,
765 struct page
*page
, void *fsdata
)
767 struct inode
*inode
= mapping
->host
;
771 res
= fuse_buffered_write(file
, inode
, pos
, copied
, page
);
774 page_cache_release(page
);
778 static size_t fuse_send_write_pages(struct fuse_req
*req
, struct file
*file
,
779 struct inode
*inode
, loff_t pos
,
786 for (i
= 0; i
< req
->num_pages
; i
++)
787 fuse_wait_on_page_writeback(inode
, req
->pages
[i
]->index
);
789 res
= fuse_send_write(req
, file
, pos
, count
, NULL
);
791 offset
= req
->page_offset
;
793 for (i
= 0; i
< req
->num_pages
; i
++) {
794 struct page
*page
= req
->pages
[i
];
796 if (!req
->out
.h
.error
&& !offset
&& count
>= PAGE_CACHE_SIZE
)
797 SetPageUptodate(page
);
799 if (count
> PAGE_CACHE_SIZE
- offset
)
800 count
-= PAGE_CACHE_SIZE
- offset
;
806 page_cache_release(page
);
812 static ssize_t
fuse_fill_write_pages(struct fuse_req
*req
,
813 struct address_space
*mapping
,
814 struct iov_iter
*ii
, loff_t pos
)
816 struct fuse_conn
*fc
= get_fuse_conn(mapping
->host
);
817 unsigned offset
= pos
& (PAGE_CACHE_SIZE
- 1);
821 req
->in
.argpages
= 1;
822 req
->page_offset
= offset
;
827 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
828 size_t bytes
= min_t(size_t, PAGE_CACHE_SIZE
- offset
,
831 bytes
= min_t(size_t, bytes
, fc
->max_write
- count
);
835 if (iov_iter_fault_in_readable(ii
, bytes
))
839 page
= grab_cache_page_write_begin(mapping
, index
, 0);
843 if (mapping_writably_mapped(mapping
))
844 flush_dcache_page(page
);
847 tmp
= iov_iter_copy_from_user_atomic(page
, ii
, offset
, bytes
);
849 flush_dcache_page(page
);
853 page_cache_release(page
);
854 bytes
= min(bytes
, iov_iter_single_seg_count(ii
));
859 req
->pages
[req
->num_pages
] = page
;
862 iov_iter_advance(ii
, tmp
);
866 if (offset
== PAGE_CACHE_SIZE
)
871 } while (iov_iter_count(ii
) && count
< fc
->max_write
&&
872 req
->num_pages
< FUSE_MAX_PAGES_PER_REQ
&& offset
== 0);
874 return count
> 0 ? count
: err
;
877 static ssize_t
fuse_perform_write(struct file
*file
,
878 struct address_space
*mapping
,
879 struct iov_iter
*ii
, loff_t pos
)
881 struct inode
*inode
= mapping
->host
;
882 struct fuse_conn
*fc
= get_fuse_conn(inode
);
886 if (is_bad_inode(inode
))
890 struct fuse_req
*req
;
893 req
= fuse_get_req(fc
);
899 count
= fuse_fill_write_pages(req
, mapping
, ii
, pos
);
905 num_written
= fuse_send_write_pages(req
, file
, inode
,
907 err
= req
->out
.h
.error
;
912 /* break out of the loop on short write */
913 if (num_written
!= count
)
917 fuse_put_request(fc
, req
);
918 } while (!err
&& iov_iter_count(ii
));
921 fuse_write_update_size(inode
, pos
);
923 fuse_invalidate_attr(inode
);
925 return res
> 0 ? res
: err
;
928 static ssize_t
fuse_file_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
929 unsigned long nr_segs
, loff_t pos
)
931 struct file
*file
= iocb
->ki_filp
;
932 struct address_space
*mapping
= file
->f_mapping
;
935 struct inode
*inode
= mapping
->host
;
939 WARN_ON(iocb
->ki_pos
!= pos
);
941 err
= generic_segment_checks(iov
, &nr_segs
, &count
, VERIFY_READ
);
945 mutex_lock(&inode
->i_mutex
);
946 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
948 /* We can write back this queue in page reclaim */
949 current
->backing_dev_info
= mapping
->backing_dev_info
;
951 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
958 err
= file_remove_suid(file
);
962 file_update_time(file
);
964 iov_iter_init(&i
, iov
, nr_segs
, count
, 0);
965 written
= fuse_perform_write(file
, mapping
, &i
, pos
);
967 iocb
->ki_pos
= pos
+ written
;
970 current
->backing_dev_info
= NULL
;
971 mutex_unlock(&inode
->i_mutex
);
973 return written
? written
: err
;
976 static void fuse_release_user_pages(struct fuse_req
*req
, int write
)
980 for (i
= 0; i
< req
->num_pages
; i
++) {
981 struct page
*page
= req
->pages
[i
];
983 set_page_dirty_lock(page
);
988 static int fuse_get_user_pages(struct fuse_req
*req
, const char __user
*buf
,
989 size_t *nbytesp
, int write
)
991 size_t nbytes
= *nbytesp
;
992 unsigned long user_addr
= (unsigned long) buf
;
993 unsigned offset
= user_addr
& ~PAGE_MASK
;
996 /* Special case for kernel I/O: can copy directly into the buffer */
997 if (segment_eq(get_fs(), KERNEL_DS
)) {
999 req
->in
.args
[1].value
= (void *) user_addr
;
1001 req
->out
.args
[0].value
= (void *) user_addr
;
1006 nbytes
= min_t(size_t, nbytes
, FUSE_MAX_PAGES_PER_REQ
<< PAGE_SHIFT
);
1007 npages
= (nbytes
+ offset
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1008 npages
= clamp(npages
, 1, FUSE_MAX_PAGES_PER_REQ
);
1009 npages
= get_user_pages_fast(user_addr
, npages
, !write
, req
->pages
);
1013 req
->num_pages
= npages
;
1014 req
->page_offset
= offset
;
1017 req
->in
.argpages
= 1;
1019 req
->out
.argpages
= 1;
1021 nbytes
= (req
->num_pages
<< PAGE_SHIFT
) - req
->page_offset
;
1022 *nbytesp
= min(*nbytesp
, nbytes
);
1027 ssize_t
fuse_direct_io(struct file
*file
, const char __user
*buf
,
1028 size_t count
, loff_t
*ppos
, int write
)
1030 struct fuse_file
*ff
= file
->private_data
;
1031 struct fuse_conn
*fc
= ff
->fc
;
1032 size_t nmax
= write
? fc
->max_write
: fc
->max_read
;
1035 struct fuse_req
*req
;
1037 req
= fuse_get_req(fc
);
1039 return PTR_ERR(req
);
1043 fl_owner_t owner
= current
->files
;
1044 size_t nbytes
= min(count
, nmax
);
1045 int err
= fuse_get_user_pages(req
, buf
, &nbytes
, write
);
1052 nres
= fuse_send_write(req
, file
, pos
, nbytes
, owner
);
1054 nres
= fuse_send_read(req
, file
, pos
, nbytes
, owner
);
1056 fuse_release_user_pages(req
, !write
);
1057 if (req
->out
.h
.error
) {
1059 res
= req
->out
.h
.error
;
1061 } else if (nres
> nbytes
) {
1072 fuse_put_request(fc
, req
);
1073 req
= fuse_get_req(fc
);
1079 fuse_put_request(fc
, req
);
1085 EXPORT_SYMBOL_GPL(fuse_direct_io
);
1087 static ssize_t
fuse_direct_read(struct file
*file
, char __user
*buf
,
1088 size_t count
, loff_t
*ppos
)
1091 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1093 if (is_bad_inode(inode
))
1096 res
= fuse_direct_io(file
, buf
, count
, ppos
, 0);
1098 fuse_invalidate_attr(inode
);
1103 static ssize_t
fuse_direct_write(struct file
*file
, const char __user
*buf
,
1104 size_t count
, loff_t
*ppos
)
1106 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1109 if (is_bad_inode(inode
))
1112 /* Don't allow parallel writes to the same file */
1113 mutex_lock(&inode
->i_mutex
);
1114 res
= generic_write_checks(file
, ppos
, &count
, 0);
1116 res
= fuse_direct_io(file
, buf
, count
, ppos
, 1);
1118 fuse_write_update_size(inode
, *ppos
);
1120 mutex_unlock(&inode
->i_mutex
);
1122 fuse_invalidate_attr(inode
);
1127 static void fuse_writepage_free(struct fuse_conn
*fc
, struct fuse_req
*req
)
1129 __free_page(req
->pages
[0]);
1130 fuse_file_put(req
->ff
);
1133 static void fuse_writepage_finish(struct fuse_conn
*fc
, struct fuse_req
*req
)
1135 struct inode
*inode
= req
->inode
;
1136 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1137 struct backing_dev_info
*bdi
= inode
->i_mapping
->backing_dev_info
;
1139 list_del(&req
->writepages_entry
);
1140 dec_bdi_stat(bdi
, BDI_WRITEBACK
);
1141 dec_zone_page_state(req
->pages
[0], NR_WRITEBACK_TEMP
);
1142 bdi_writeout_inc(bdi
);
1143 wake_up(&fi
->page_waitq
);
1146 /* Called under fc->lock, may release and reacquire it */
1147 static void fuse_send_writepage(struct fuse_conn
*fc
, struct fuse_req
*req
)
1148 __releases(&fc
->lock
)
1149 __acquires(&fc
->lock
)
1151 struct fuse_inode
*fi
= get_fuse_inode(req
->inode
);
1152 loff_t size
= i_size_read(req
->inode
);
1153 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
1158 if (inarg
->offset
+ PAGE_CACHE_SIZE
<= size
) {
1159 inarg
->size
= PAGE_CACHE_SIZE
;
1160 } else if (inarg
->offset
< size
) {
1161 inarg
->size
= size
& (PAGE_CACHE_SIZE
- 1);
1163 /* Got truncated off completely */
1167 req
->in
.args
[1].size
= inarg
->size
;
1169 fuse_request_send_background_locked(fc
, req
);
1173 fuse_writepage_finish(fc
, req
);
1174 spin_unlock(&fc
->lock
);
1175 fuse_writepage_free(fc
, req
);
1176 fuse_put_request(fc
, req
);
1177 spin_lock(&fc
->lock
);
1181 * If fi->writectr is positive (no truncate or fsync going on) send
1182 * all queued writepage requests.
1184 * Called with fc->lock
1186 void fuse_flush_writepages(struct inode
*inode
)
1187 __releases(&fc
->lock
)
1188 __acquires(&fc
->lock
)
1190 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1191 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1192 struct fuse_req
*req
;
1194 while (fi
->writectr
>= 0 && !list_empty(&fi
->queued_writes
)) {
1195 req
= list_entry(fi
->queued_writes
.next
, struct fuse_req
, list
);
1196 list_del_init(&req
->list
);
1197 fuse_send_writepage(fc
, req
);
1201 static void fuse_writepage_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
1203 struct inode
*inode
= req
->inode
;
1204 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1206 mapping_set_error(inode
->i_mapping
, req
->out
.h
.error
);
1207 spin_lock(&fc
->lock
);
1209 fuse_writepage_finish(fc
, req
);
1210 spin_unlock(&fc
->lock
);
1211 fuse_writepage_free(fc
, req
);
1214 static int fuse_writepage_locked(struct page
*page
)
1216 struct address_space
*mapping
= page
->mapping
;
1217 struct inode
*inode
= mapping
->host
;
1218 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1219 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1220 struct fuse_req
*req
;
1221 struct fuse_file
*ff
;
1222 struct page
*tmp_page
;
1224 set_page_writeback(page
);
1226 req
= fuse_request_alloc_nofs();
1230 tmp_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
1234 spin_lock(&fc
->lock
);
1235 BUG_ON(list_empty(&fi
->write_files
));
1236 ff
= list_entry(fi
->write_files
.next
, struct fuse_file
, write_entry
);
1237 req
->ff
= fuse_file_get(ff
);
1238 spin_unlock(&fc
->lock
);
1240 fuse_write_fill(req
, ff
, page_offset(page
), 0);
1242 copy_highpage(tmp_page
, page
);
1243 req
->misc
.write
.in
.write_flags
|= FUSE_WRITE_CACHE
;
1244 req
->in
.argpages
= 1;
1246 req
->pages
[0] = tmp_page
;
1247 req
->page_offset
= 0;
1248 req
->end
= fuse_writepage_end
;
1251 inc_bdi_stat(mapping
->backing_dev_info
, BDI_WRITEBACK
);
1252 inc_zone_page_state(tmp_page
, NR_WRITEBACK_TEMP
);
1253 end_page_writeback(page
);
1255 spin_lock(&fc
->lock
);
1256 list_add(&req
->writepages_entry
, &fi
->writepages
);
1257 list_add_tail(&req
->list
, &fi
->queued_writes
);
1258 fuse_flush_writepages(inode
);
1259 spin_unlock(&fc
->lock
);
1264 fuse_request_free(req
);
1266 end_page_writeback(page
);
1270 static int fuse_writepage(struct page
*page
, struct writeback_control
*wbc
)
1274 err
= fuse_writepage_locked(page
);
1280 static int fuse_launder_page(struct page
*page
)
1283 if (clear_page_dirty_for_io(page
)) {
1284 struct inode
*inode
= page
->mapping
->host
;
1285 err
= fuse_writepage_locked(page
);
1287 fuse_wait_on_page_writeback(inode
, page
->index
);
1293 * Write back dirty pages now, because there may not be any suitable
1296 static void fuse_vma_close(struct vm_area_struct
*vma
)
1298 filemap_write_and_wait(vma
->vm_file
->f_mapping
);
1302 * Wait for writeback against this page to complete before allowing it
1303 * to be marked dirty again, and hence written back again, possibly
1304 * before the previous writepage completed.
1306 * Block here, instead of in ->writepage(), so that the userspace fs
1307 * can only block processes actually operating on the filesystem.
1309 * Otherwise unprivileged userspace fs would be able to block
1314 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
1316 static int fuse_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1318 struct page
*page
= vmf
->page
;
1320 * Don't use page->mapping as it may become NULL from a
1321 * concurrent truncate.
1323 struct inode
*inode
= vma
->vm_file
->f_mapping
->host
;
1325 fuse_wait_on_page_writeback(inode
, page
->index
);
1329 static const struct vm_operations_struct fuse_file_vm_ops
= {
1330 .close
= fuse_vma_close
,
1331 .fault
= filemap_fault
,
1332 .page_mkwrite
= fuse_page_mkwrite
,
1335 static int fuse_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1337 if ((vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
)) {
1338 struct inode
*inode
= file
->f_dentry
->d_inode
;
1339 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1340 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1341 struct fuse_file
*ff
= file
->private_data
;
1343 * file may be written through mmap, so chain it onto the
1344 * inodes's write_file list
1346 spin_lock(&fc
->lock
);
1347 if (list_empty(&ff
->write_entry
))
1348 list_add(&ff
->write_entry
, &fi
->write_files
);
1349 spin_unlock(&fc
->lock
);
1351 file_accessed(file
);
1352 vma
->vm_ops
= &fuse_file_vm_ops
;
1356 static int fuse_direct_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1358 /* Can't provide the coherency needed for MAP_SHARED */
1359 if (vma
->vm_flags
& VM_MAYSHARE
)
1362 invalidate_inode_pages2(file
->f_mapping
);
1364 return generic_file_mmap(file
, vma
);
1367 static int convert_fuse_file_lock(const struct fuse_file_lock
*ffl
,
1368 struct file_lock
*fl
)
1370 switch (ffl
->type
) {
1376 if (ffl
->start
> OFFSET_MAX
|| ffl
->end
> OFFSET_MAX
||
1377 ffl
->end
< ffl
->start
)
1380 fl
->fl_start
= ffl
->start
;
1381 fl
->fl_end
= ffl
->end
;
1382 fl
->fl_pid
= ffl
->pid
;
1388 fl
->fl_type
= ffl
->type
;
1392 static void fuse_lk_fill(struct fuse_req
*req
, struct file
*file
,
1393 const struct file_lock
*fl
, int opcode
, pid_t pid
,
1396 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1397 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1398 struct fuse_file
*ff
= file
->private_data
;
1399 struct fuse_lk_in
*arg
= &req
->misc
.lk_in
;
1402 arg
->owner
= fuse_lock_owner_id(fc
, fl
->fl_owner
);
1403 arg
->lk
.start
= fl
->fl_start
;
1404 arg
->lk
.end
= fl
->fl_end
;
1405 arg
->lk
.type
= fl
->fl_type
;
1408 arg
->lk_flags
|= FUSE_LK_FLOCK
;
1409 req
->in
.h
.opcode
= opcode
;
1410 req
->in
.h
.nodeid
= get_node_id(inode
);
1411 req
->in
.numargs
= 1;
1412 req
->in
.args
[0].size
= sizeof(*arg
);
1413 req
->in
.args
[0].value
= arg
;
1416 static int fuse_getlk(struct file
*file
, struct file_lock
*fl
)
1418 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1419 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1420 struct fuse_req
*req
;
1421 struct fuse_lk_out outarg
;
1424 req
= fuse_get_req(fc
);
1426 return PTR_ERR(req
);
1428 fuse_lk_fill(req
, file
, fl
, FUSE_GETLK
, 0, 0);
1429 req
->out
.numargs
= 1;
1430 req
->out
.args
[0].size
= sizeof(outarg
);
1431 req
->out
.args
[0].value
= &outarg
;
1432 fuse_request_send(fc
, req
);
1433 err
= req
->out
.h
.error
;
1434 fuse_put_request(fc
, req
);
1436 err
= convert_fuse_file_lock(&outarg
.lk
, fl
);
1441 static int fuse_setlk(struct file
*file
, struct file_lock
*fl
, int flock
)
1443 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1444 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1445 struct fuse_req
*req
;
1446 int opcode
= (fl
->fl_flags
& FL_SLEEP
) ? FUSE_SETLKW
: FUSE_SETLK
;
1447 pid_t pid
= fl
->fl_type
!= F_UNLCK
? current
->tgid
: 0;
1450 if (fl
->fl_lmops
&& fl
->fl_lmops
->fl_grant
) {
1451 /* NLM needs asynchronous locks, which we don't support yet */
1455 /* Unlock on close is handled by the flush method */
1456 if (fl
->fl_flags
& FL_CLOSE
)
1459 req
= fuse_get_req(fc
);
1461 return PTR_ERR(req
);
1463 fuse_lk_fill(req
, file
, fl
, opcode
, pid
, flock
);
1464 fuse_request_send(fc
, req
);
1465 err
= req
->out
.h
.error
;
1466 /* locking is restartable */
1469 fuse_put_request(fc
, req
);
1473 static int fuse_file_lock(struct file
*file
, int cmd
, struct file_lock
*fl
)
1475 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1476 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1479 if (cmd
== F_CANCELLK
) {
1481 } else if (cmd
== F_GETLK
) {
1483 posix_test_lock(file
, fl
);
1486 err
= fuse_getlk(file
, fl
);
1489 err
= posix_lock_file(file
, fl
, NULL
);
1491 err
= fuse_setlk(file
, fl
, 0);
1496 static int fuse_file_flock(struct file
*file
, int cmd
, struct file_lock
*fl
)
1498 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1499 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1503 err
= flock_lock_file_wait(file
, fl
);
1505 /* emulate flock with POSIX locks */
1506 fl
->fl_owner
= (fl_owner_t
) file
;
1507 err
= fuse_setlk(file
, fl
, 1);
1513 static sector_t
fuse_bmap(struct address_space
*mapping
, sector_t block
)
1515 struct inode
*inode
= mapping
->host
;
1516 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1517 struct fuse_req
*req
;
1518 struct fuse_bmap_in inarg
;
1519 struct fuse_bmap_out outarg
;
1522 if (!inode
->i_sb
->s_bdev
|| fc
->no_bmap
)
1525 req
= fuse_get_req(fc
);
1529 memset(&inarg
, 0, sizeof(inarg
));
1530 inarg
.block
= block
;
1531 inarg
.blocksize
= inode
->i_sb
->s_blocksize
;
1532 req
->in
.h
.opcode
= FUSE_BMAP
;
1533 req
->in
.h
.nodeid
= get_node_id(inode
);
1534 req
->in
.numargs
= 1;
1535 req
->in
.args
[0].size
= sizeof(inarg
);
1536 req
->in
.args
[0].value
= &inarg
;
1537 req
->out
.numargs
= 1;
1538 req
->out
.args
[0].size
= sizeof(outarg
);
1539 req
->out
.args
[0].value
= &outarg
;
1540 fuse_request_send(fc
, req
);
1541 err
= req
->out
.h
.error
;
1542 fuse_put_request(fc
, req
);
1546 return err
? 0 : outarg
.block
;
1549 static loff_t
fuse_file_llseek(struct file
*file
, loff_t offset
, int origin
)
1552 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1554 mutex_lock(&inode
->i_mutex
);
1557 retval
= fuse_update_attributes(inode
, NULL
, file
, NULL
);
1560 offset
+= i_size_read(inode
);
1563 offset
+= file
->f_pos
;
1566 if (offset
>= 0 && offset
<= inode
->i_sb
->s_maxbytes
) {
1567 if (offset
!= file
->f_pos
) {
1568 file
->f_pos
= offset
;
1569 file
->f_version
= 0;
1574 mutex_unlock(&inode
->i_mutex
);
1578 static int fuse_ioctl_copy_user(struct page
**pages
, struct iovec
*iov
,
1579 unsigned int nr_segs
, size_t bytes
, bool to_user
)
1587 iov_iter_init(&ii
, iov
, nr_segs
, bytes
, 0);
1589 while (iov_iter_count(&ii
)) {
1590 struct page
*page
= pages
[page_idx
++];
1591 size_t todo
= min_t(size_t, PAGE_SIZE
, iov_iter_count(&ii
));
1597 char __user
*uaddr
= ii
.iov
->iov_base
+ ii
.iov_offset
;
1598 size_t iov_len
= ii
.iov
->iov_len
- ii
.iov_offset
;
1599 size_t copy
= min(todo
, iov_len
);
1603 left
= copy_from_user(kaddr
, uaddr
, copy
);
1605 left
= copy_to_user(uaddr
, kaddr
, copy
);
1610 iov_iter_advance(&ii
, copy
);
1622 * For ioctls, there is no generic way to determine how much memory
1623 * needs to be read and/or written. Furthermore, ioctls are allowed
1624 * to dereference the passed pointer, so the parameter requires deep
1625 * copying but FUSE has no idea whatsoever about what to copy in or
1628 * This is solved by allowing FUSE server to retry ioctl with
1629 * necessary in/out iovecs. Let's assume the ioctl implementation
1630 * needs to read in the following structure.
1637 * On the first callout to FUSE server, inarg->in_size and
1638 * inarg->out_size will be NULL; then, the server completes the ioctl
1639 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
1640 * the actual iov array to
1642 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
1644 * which tells FUSE to copy in the requested area and retry the ioctl.
1645 * On the second round, the server has access to the structure and
1646 * from that it can tell what to look for next, so on the invocation,
1647 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
1649 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
1650 * { .iov_base = a.buf, .iov_len = a.buflen } }
1652 * FUSE will copy both struct a and the pointed buffer from the
1653 * process doing the ioctl and retry ioctl with both struct a and the
1656 * This time, FUSE server has everything it needs and completes ioctl
1657 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
1659 * Copying data out works the same way.
1661 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
1662 * automatically initializes in and out iovs by decoding @cmd with
1663 * _IOC_* macros and the server is not allowed to request RETRY. This
1664 * limits ioctl data transfers to well-formed ioctls and is the forced
1665 * behavior for all FUSE servers.
1667 long fuse_do_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
,
1670 struct fuse_file
*ff
= file
->private_data
;
1671 struct fuse_conn
*fc
= ff
->fc
;
1672 struct fuse_ioctl_in inarg
= {
1678 struct fuse_ioctl_out outarg
;
1679 struct fuse_req
*req
= NULL
;
1680 struct page
**pages
= NULL
;
1681 struct page
*iov_page
= NULL
;
1682 struct iovec
*in_iov
= NULL
, *out_iov
= NULL
;
1683 unsigned int in_iovs
= 0, out_iovs
= 0, num_pages
= 0, max_pages
;
1684 size_t in_size
, out_size
, transferred
;
1687 /* assume all the iovs returned by client always fits in a page */
1688 BUILD_BUG_ON(sizeof(struct iovec
) * FUSE_IOCTL_MAX_IOV
> PAGE_SIZE
);
1691 pages
= kzalloc(sizeof(pages
[0]) * FUSE_MAX_PAGES_PER_REQ
, GFP_KERNEL
);
1692 iov_page
= alloc_page(GFP_KERNEL
);
1693 if (!pages
|| !iov_page
)
1697 * If restricted, initialize IO parameters as encoded in @cmd.
1698 * RETRY from server is not allowed.
1700 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
)) {
1701 struct iovec
*iov
= page_address(iov_page
);
1703 iov
->iov_base
= (void __user
*)arg
;
1704 iov
->iov_len
= _IOC_SIZE(cmd
);
1706 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
1711 if (_IOC_DIR(cmd
) & _IOC_READ
) {
1718 inarg
.in_size
= in_size
= iov_length(in_iov
, in_iovs
);
1719 inarg
.out_size
= out_size
= iov_length(out_iov
, out_iovs
);
1722 * Out data can be used either for actual out data or iovs,
1723 * make sure there always is at least one page.
1725 out_size
= max_t(size_t, out_size
, PAGE_SIZE
);
1726 max_pages
= DIV_ROUND_UP(max(in_size
, out_size
), PAGE_SIZE
);
1728 /* make sure there are enough buffer pages and init request with them */
1730 if (max_pages
> FUSE_MAX_PAGES_PER_REQ
)
1732 while (num_pages
< max_pages
) {
1733 pages
[num_pages
] = alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
1734 if (!pages
[num_pages
])
1739 req
= fuse_get_req(fc
);
1745 memcpy(req
->pages
, pages
, sizeof(req
->pages
[0]) * num_pages
);
1746 req
->num_pages
= num_pages
;
1748 /* okay, let's send it to the client */
1749 req
->in
.h
.opcode
= FUSE_IOCTL
;
1750 req
->in
.h
.nodeid
= ff
->nodeid
;
1751 req
->in
.numargs
= 1;
1752 req
->in
.args
[0].size
= sizeof(inarg
);
1753 req
->in
.args
[0].value
= &inarg
;
1756 req
->in
.args
[1].size
= in_size
;
1757 req
->in
.argpages
= 1;
1759 err
= fuse_ioctl_copy_user(pages
, in_iov
, in_iovs
, in_size
,
1765 req
->out
.numargs
= 2;
1766 req
->out
.args
[0].size
= sizeof(outarg
);
1767 req
->out
.args
[0].value
= &outarg
;
1768 req
->out
.args
[1].size
= out_size
;
1769 req
->out
.argpages
= 1;
1770 req
->out
.argvar
= 1;
1772 fuse_request_send(fc
, req
);
1773 err
= req
->out
.h
.error
;
1774 transferred
= req
->out
.args
[1].size
;
1775 fuse_put_request(fc
, req
);
1780 /* did it ask for retry? */
1781 if (outarg
.flags
& FUSE_IOCTL_RETRY
) {
1784 /* no retry if in restricted mode */
1786 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
))
1789 in_iovs
= outarg
.in_iovs
;
1790 out_iovs
= outarg
.out_iovs
;
1793 * Make sure things are in boundary, separate checks
1794 * are to protect against overflow.
1797 if (in_iovs
> FUSE_IOCTL_MAX_IOV
||
1798 out_iovs
> FUSE_IOCTL_MAX_IOV
||
1799 in_iovs
+ out_iovs
> FUSE_IOCTL_MAX_IOV
)
1803 if ((in_iovs
+ out_iovs
) * sizeof(struct iovec
) != transferred
)
1806 /* okay, copy in iovs and retry */
1807 vaddr
= kmap_atomic(pages
[0], KM_USER0
);
1808 memcpy(page_address(iov_page
), vaddr
, transferred
);
1809 kunmap_atomic(vaddr
, KM_USER0
);
1811 in_iov
= page_address(iov_page
);
1812 out_iov
= in_iov
+ in_iovs
;
1818 if (transferred
> inarg
.out_size
)
1821 err
= fuse_ioctl_copy_user(pages
, out_iov
, out_iovs
, transferred
, true);
1824 fuse_put_request(fc
, req
);
1826 __free_page(iov_page
);
1828 __free_page(pages
[--num_pages
]);
1831 return err
? err
: outarg
.result
;
1833 EXPORT_SYMBOL_GPL(fuse_do_ioctl
);
1835 static long fuse_file_ioctl_common(struct file
*file
, unsigned int cmd
,
1836 unsigned long arg
, unsigned int flags
)
1838 struct inode
*inode
= file
->f_dentry
->d_inode
;
1839 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1841 if (!fuse_allow_task(fc
, current
))
1844 if (is_bad_inode(inode
))
1847 return fuse_do_ioctl(file
, cmd
, arg
, flags
);
1850 static long fuse_file_ioctl(struct file
*file
, unsigned int cmd
,
1853 return fuse_file_ioctl_common(file
, cmd
, arg
, 0);
1856 static long fuse_file_compat_ioctl(struct file
*file
, unsigned int cmd
,
1859 return fuse_file_ioctl_common(file
, cmd
, arg
, FUSE_IOCTL_COMPAT
);
1863 * All files which have been polled are linked to RB tree
1864 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
1865 * find the matching one.
1867 static struct rb_node
**fuse_find_polled_node(struct fuse_conn
*fc
, u64 kh
,
1868 struct rb_node
**parent_out
)
1870 struct rb_node
**link
= &fc
->polled_files
.rb_node
;
1871 struct rb_node
*last
= NULL
;
1874 struct fuse_file
*ff
;
1877 ff
= rb_entry(last
, struct fuse_file
, polled_node
);
1880 link
= &last
->rb_left
;
1881 else if (kh
> ff
->kh
)
1882 link
= &last
->rb_right
;
1893 * The file is about to be polled. Make sure it's on the polled_files
1894 * RB tree. Note that files once added to the polled_files tree are
1895 * not removed before the file is released. This is because a file
1896 * polled once is likely to be polled again.
1898 static void fuse_register_polled_file(struct fuse_conn
*fc
,
1899 struct fuse_file
*ff
)
1901 spin_lock(&fc
->lock
);
1902 if (RB_EMPTY_NODE(&ff
->polled_node
)) {
1903 struct rb_node
**link
, *parent
;
1905 link
= fuse_find_polled_node(fc
, ff
->kh
, &parent
);
1907 rb_link_node(&ff
->polled_node
, parent
, link
);
1908 rb_insert_color(&ff
->polled_node
, &fc
->polled_files
);
1910 spin_unlock(&fc
->lock
);
1913 unsigned fuse_file_poll(struct file
*file
, poll_table
*wait
)
1915 struct fuse_file
*ff
= file
->private_data
;
1916 struct fuse_conn
*fc
= ff
->fc
;
1917 struct fuse_poll_in inarg
= { .fh
= ff
->fh
, .kh
= ff
->kh
};
1918 struct fuse_poll_out outarg
;
1919 struct fuse_req
*req
;
1923 return DEFAULT_POLLMASK
;
1925 poll_wait(file
, &ff
->poll_wait
, wait
);
1928 * Ask for notification iff there's someone waiting for it.
1929 * The client may ignore the flag and always notify.
1931 if (waitqueue_active(&ff
->poll_wait
)) {
1932 inarg
.flags
|= FUSE_POLL_SCHEDULE_NOTIFY
;
1933 fuse_register_polled_file(fc
, ff
);
1936 req
= fuse_get_req(fc
);
1940 req
->in
.h
.opcode
= FUSE_POLL
;
1941 req
->in
.h
.nodeid
= ff
->nodeid
;
1942 req
->in
.numargs
= 1;
1943 req
->in
.args
[0].size
= sizeof(inarg
);
1944 req
->in
.args
[0].value
= &inarg
;
1945 req
->out
.numargs
= 1;
1946 req
->out
.args
[0].size
= sizeof(outarg
);
1947 req
->out
.args
[0].value
= &outarg
;
1948 fuse_request_send(fc
, req
);
1949 err
= req
->out
.h
.error
;
1950 fuse_put_request(fc
, req
);
1953 return outarg
.revents
;
1954 if (err
== -ENOSYS
) {
1956 return DEFAULT_POLLMASK
;
1960 EXPORT_SYMBOL_GPL(fuse_file_poll
);
1963 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
1964 * wakes up the poll waiters.
1966 int fuse_notify_poll_wakeup(struct fuse_conn
*fc
,
1967 struct fuse_notify_poll_wakeup_out
*outarg
)
1969 u64 kh
= outarg
->kh
;
1970 struct rb_node
**link
;
1972 spin_lock(&fc
->lock
);
1974 link
= fuse_find_polled_node(fc
, kh
, NULL
);
1976 struct fuse_file
*ff
;
1978 ff
= rb_entry(*link
, struct fuse_file
, polled_node
);
1979 wake_up_interruptible_sync(&ff
->poll_wait
);
1982 spin_unlock(&fc
->lock
);
1986 static const struct file_operations fuse_file_operations
= {
1987 .llseek
= fuse_file_llseek
,
1988 .read
= do_sync_read
,
1989 .aio_read
= fuse_file_aio_read
,
1990 .write
= do_sync_write
,
1991 .aio_write
= fuse_file_aio_write
,
1992 .mmap
= fuse_file_mmap
,
1994 .flush
= fuse_flush
,
1995 .release
= fuse_release
,
1996 .fsync
= fuse_fsync
,
1997 .lock
= fuse_file_lock
,
1998 .flock
= fuse_file_flock
,
1999 .splice_read
= generic_file_splice_read
,
2000 .unlocked_ioctl
= fuse_file_ioctl
,
2001 .compat_ioctl
= fuse_file_compat_ioctl
,
2002 .poll
= fuse_file_poll
,
2005 static const struct file_operations fuse_direct_io_file_operations
= {
2006 .llseek
= fuse_file_llseek
,
2007 .read
= fuse_direct_read
,
2008 .write
= fuse_direct_write
,
2009 .mmap
= fuse_direct_mmap
,
2011 .flush
= fuse_flush
,
2012 .release
= fuse_release
,
2013 .fsync
= fuse_fsync
,
2014 .lock
= fuse_file_lock
,
2015 .flock
= fuse_file_flock
,
2016 .unlocked_ioctl
= fuse_file_ioctl
,
2017 .compat_ioctl
= fuse_file_compat_ioctl
,
2018 .poll
= fuse_file_poll
,
2019 /* no splice_read */
2022 static const struct address_space_operations fuse_file_aops
= {
2023 .readpage
= fuse_readpage
,
2024 .writepage
= fuse_writepage
,
2025 .launder_page
= fuse_launder_page
,
2026 .write_begin
= fuse_write_begin
,
2027 .write_end
= fuse_write_end
,
2028 .readpages
= fuse_readpages
,
2029 .set_page_dirty
= __set_page_dirty_nobuffers
,
2033 void fuse_init_file_inode(struct inode
*inode
)
2035 inode
->i_fop
= &fuse_file_operations
;
2036 inode
->i_data
.a_ops
= &fuse_file_aops
;