2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU GPL.
11 #include <linux/pagemap.h>
12 #include <linux/slab.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/module.h>
16 #include <linux/compat.h>
18 static const struct file_operations fuse_direct_io_file_operations
;
20 static int fuse_send_open(struct fuse_conn
*fc
, u64 nodeid
, struct file
*file
,
21 int opcode
, struct fuse_open_out
*outargp
)
23 struct fuse_open_in inarg
;
27 req
= fuse_get_req(fc
);
31 memset(&inarg
, 0, sizeof(inarg
));
32 inarg
.flags
= file
->f_flags
& ~(O_CREAT
| O_EXCL
| O_NOCTTY
);
33 if (!fc
->atomic_o_trunc
)
34 inarg
.flags
&= ~O_TRUNC
;
35 req
->in
.h
.opcode
= opcode
;
36 req
->in
.h
.nodeid
= nodeid
;
38 req
->in
.args
[0].size
= sizeof(inarg
);
39 req
->in
.args
[0].value
= &inarg
;
41 req
->out
.args
[0].size
= sizeof(*outargp
);
42 req
->out
.args
[0].value
= outargp
;
43 fuse_request_send(fc
, req
);
44 err
= req
->out
.h
.error
;
45 fuse_put_request(fc
, req
);
50 struct fuse_file
*fuse_file_alloc(struct fuse_conn
*fc
)
54 ff
= kmalloc(sizeof(struct fuse_file
), GFP_KERNEL
);
59 ff
->reserved_req
= fuse_request_alloc();
60 if (unlikely(!ff
->reserved_req
)) {
65 INIT_LIST_HEAD(&ff
->write_entry
);
66 atomic_set(&ff
->count
, 0);
67 RB_CLEAR_NODE(&ff
->polled_node
);
68 init_waitqueue_head(&ff
->poll_wait
);
72 spin_unlock(&fc
->lock
);
77 void fuse_file_free(struct fuse_file
*ff
)
79 fuse_request_free(ff
->reserved_req
);
83 struct fuse_file
*fuse_file_get(struct fuse_file
*ff
)
85 atomic_inc(&ff
->count
);
89 static void fuse_release_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
91 path_put(&req
->misc
.release
.path
);
94 static void fuse_file_put(struct fuse_file
*ff
)
96 if (atomic_dec_and_test(&ff
->count
)) {
97 struct fuse_req
*req
= ff
->reserved_req
;
99 req
->end
= fuse_release_end
;
100 fuse_request_send_background(ff
->fc
, req
);
105 int fuse_do_open(struct fuse_conn
*fc
, u64 nodeid
, struct file
*file
,
108 struct fuse_open_out outarg
;
109 struct fuse_file
*ff
;
111 int opcode
= isdir
? FUSE_OPENDIR
: FUSE_OPEN
;
113 ff
= fuse_file_alloc(fc
);
117 err
= fuse_send_open(fc
, nodeid
, file
, opcode
, &outarg
);
124 outarg
.open_flags
&= ~FOPEN_DIRECT_IO
;
128 ff
->open_flags
= outarg
.open_flags
;
129 file
->private_data
= fuse_file_get(ff
);
133 EXPORT_SYMBOL_GPL(fuse_do_open
);
135 void fuse_finish_open(struct inode
*inode
, struct file
*file
)
137 struct fuse_file
*ff
= file
->private_data
;
138 struct fuse_conn
*fc
= get_fuse_conn(inode
);
140 if (ff
->open_flags
& FOPEN_DIRECT_IO
)
141 file
->f_op
= &fuse_direct_io_file_operations
;
142 if (!(ff
->open_flags
& FOPEN_KEEP_CACHE
))
143 invalidate_inode_pages2(inode
->i_mapping
);
144 if (ff
->open_flags
& FOPEN_NONSEEKABLE
)
145 nonseekable_open(inode
, file
);
146 if (fc
->atomic_o_trunc
&& (file
->f_flags
& O_TRUNC
)) {
147 struct fuse_inode
*fi
= get_fuse_inode(inode
);
149 spin_lock(&fc
->lock
);
150 fi
->attr_version
= ++fc
->attr_version
;
151 i_size_write(inode
, 0);
152 spin_unlock(&fc
->lock
);
153 fuse_invalidate_attr(inode
);
157 int fuse_open_common(struct inode
*inode
, struct file
*file
, bool isdir
)
159 struct fuse_conn
*fc
= get_fuse_conn(inode
);
162 /* VFS checks this, but only _after_ ->open() */
163 if (file
->f_flags
& O_DIRECT
)
166 err
= generic_file_open(inode
, file
);
170 err
= fuse_do_open(fc
, get_node_id(inode
), file
, isdir
);
174 fuse_finish_open(inode
, file
);
179 static void fuse_prepare_release(struct fuse_file
*ff
, int flags
, int opcode
)
181 struct fuse_conn
*fc
= ff
->fc
;
182 struct fuse_req
*req
= ff
->reserved_req
;
183 struct fuse_release_in
*inarg
= &req
->misc
.release
.in
;
185 spin_lock(&fc
->lock
);
186 list_del(&ff
->write_entry
);
187 if (!RB_EMPTY_NODE(&ff
->polled_node
))
188 rb_erase(&ff
->polled_node
, &fc
->polled_files
);
189 spin_unlock(&fc
->lock
);
191 wake_up_interruptible_sync(&ff
->poll_wait
);
194 inarg
->flags
= flags
;
195 req
->in
.h
.opcode
= opcode
;
196 req
->in
.h
.nodeid
= ff
->nodeid
;
198 req
->in
.args
[0].size
= sizeof(struct fuse_release_in
);
199 req
->in
.args
[0].value
= inarg
;
202 void fuse_release_common(struct file
*file
, int opcode
)
204 struct fuse_file
*ff
;
205 struct fuse_req
*req
;
207 ff
= file
->private_data
;
211 req
= ff
->reserved_req
;
212 fuse_prepare_release(ff
, file
->f_flags
, opcode
);
214 /* Hold vfsmount and dentry until release is finished */
215 path_get(&file
->f_path
);
216 req
->misc
.release
.path
= file
->f_path
;
219 * Normally this will send the RELEASE request, however if
220 * some asynchronous READ or WRITE requests are outstanding,
221 * the sending will be delayed.
226 static int fuse_open(struct inode
*inode
, struct file
*file
)
228 return fuse_open_common(inode
, file
, false);
231 static int fuse_release(struct inode
*inode
, struct file
*file
)
233 fuse_release_common(file
, FUSE_RELEASE
);
235 /* return value is ignored by VFS */
239 void fuse_sync_release(struct fuse_file
*ff
, int flags
)
241 WARN_ON(atomic_read(&ff
->count
) > 1);
242 fuse_prepare_release(ff
, flags
, FUSE_RELEASE
);
243 ff
->reserved_req
->force
= 1;
244 fuse_request_send(ff
->fc
, ff
->reserved_req
);
245 fuse_put_request(ff
->fc
, ff
->reserved_req
);
248 EXPORT_SYMBOL_GPL(fuse_sync_release
);
251 * Scramble the ID space with XTEA, so that the value of the files_struct
252 * pointer is not exposed to userspace.
254 u64
fuse_lock_owner_id(struct fuse_conn
*fc
, fl_owner_t id
)
256 u32
*k
= fc
->scramble_key
;
257 u64 v
= (unsigned long) id
;
263 for (i
= 0; i
< 32; i
++) {
264 v0
+= ((v1
<< 4 ^ v1
>> 5) + v1
) ^ (sum
+ k
[sum
& 3]);
266 v1
+= ((v0
<< 4 ^ v0
>> 5) + v0
) ^ (sum
+ k
[sum
>>11 & 3]);
269 return (u64
) v0
+ ((u64
) v1
<< 32);
273 * Check if page is under writeback
275 * This is currently done by walking the list of writepage requests
276 * for the inode, which can be pretty inefficient.
278 static bool fuse_page_is_writeback(struct inode
*inode
, pgoff_t index
)
280 struct fuse_conn
*fc
= get_fuse_conn(inode
);
281 struct fuse_inode
*fi
= get_fuse_inode(inode
);
282 struct fuse_req
*req
;
285 spin_lock(&fc
->lock
);
286 list_for_each_entry(req
, &fi
->writepages
, writepages_entry
) {
289 BUG_ON(req
->inode
!= inode
);
290 curr_index
= req
->misc
.write
.in
.offset
>> PAGE_CACHE_SHIFT
;
291 if (curr_index
== index
) {
296 spin_unlock(&fc
->lock
);
302 * Wait for page writeback to be completed.
304 * Since fuse doesn't rely on the VM writeback tracking, this has to
305 * use some other means.
307 static int fuse_wait_on_page_writeback(struct inode
*inode
, pgoff_t index
)
309 struct fuse_inode
*fi
= get_fuse_inode(inode
);
311 wait_event(fi
->page_waitq
, !fuse_page_is_writeback(inode
, index
));
315 static int fuse_flush(struct file
*file
, fl_owner_t id
)
317 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
318 struct fuse_conn
*fc
= get_fuse_conn(inode
);
319 struct fuse_file
*ff
= file
->private_data
;
320 struct fuse_req
*req
;
321 struct fuse_flush_in inarg
;
324 if (is_bad_inode(inode
))
330 req
= fuse_get_req_nofail(fc
, file
);
331 memset(&inarg
, 0, sizeof(inarg
));
333 inarg
.lock_owner
= fuse_lock_owner_id(fc
, id
);
334 req
->in
.h
.opcode
= FUSE_FLUSH
;
335 req
->in
.h
.nodeid
= get_node_id(inode
);
337 req
->in
.args
[0].size
= sizeof(inarg
);
338 req
->in
.args
[0].value
= &inarg
;
340 fuse_request_send(fc
, req
);
341 err
= req
->out
.h
.error
;
342 fuse_put_request(fc
, req
);
343 if (err
== -ENOSYS
) {
351 * Wait for all pending writepages on the inode to finish.
353 * This is currently done by blocking further writes with FUSE_NOWRITE
354 * and waiting for all sent writes to complete.
356 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
357 * could conflict with truncation.
359 static void fuse_sync_writes(struct inode
*inode
)
361 fuse_set_nowrite(inode
);
362 fuse_release_nowrite(inode
);
365 int fuse_fsync_common(struct file
*file
, int datasync
, int isdir
)
367 struct inode
*inode
= file
->f_mapping
->host
;
368 struct fuse_conn
*fc
= get_fuse_conn(inode
);
369 struct fuse_file
*ff
= file
->private_data
;
370 struct fuse_req
*req
;
371 struct fuse_fsync_in inarg
;
374 if (is_bad_inode(inode
))
377 if ((!isdir
&& fc
->no_fsync
) || (isdir
&& fc
->no_fsyncdir
))
381 * Start writeback against all dirty pages of the inode, then
382 * wait for all outstanding writes, before sending the FSYNC
385 err
= write_inode_now(inode
, 0);
389 fuse_sync_writes(inode
);
391 req
= fuse_get_req(fc
);
395 memset(&inarg
, 0, sizeof(inarg
));
397 inarg
.fsync_flags
= datasync
? 1 : 0;
398 req
->in
.h
.opcode
= isdir
? FUSE_FSYNCDIR
: FUSE_FSYNC
;
399 req
->in
.h
.nodeid
= get_node_id(inode
);
401 req
->in
.args
[0].size
= sizeof(inarg
);
402 req
->in
.args
[0].value
= &inarg
;
403 fuse_request_send(fc
, req
);
404 err
= req
->out
.h
.error
;
405 fuse_put_request(fc
, req
);
406 if (err
== -ENOSYS
) {
416 static int fuse_fsync(struct file
*file
, int datasync
)
418 return fuse_fsync_common(file
, datasync
, 0);
421 void fuse_read_fill(struct fuse_req
*req
, struct file
*file
, loff_t pos
,
422 size_t count
, int opcode
)
424 struct fuse_read_in
*inarg
= &req
->misc
.read
.in
;
425 struct fuse_file
*ff
= file
->private_data
;
430 inarg
->flags
= file
->f_flags
;
431 req
->in
.h
.opcode
= opcode
;
432 req
->in
.h
.nodeid
= ff
->nodeid
;
434 req
->in
.args
[0].size
= sizeof(struct fuse_read_in
);
435 req
->in
.args
[0].value
= inarg
;
437 req
->out
.numargs
= 1;
438 req
->out
.args
[0].size
= count
;
441 static size_t fuse_send_read(struct fuse_req
*req
, struct file
*file
,
442 loff_t pos
, size_t count
, fl_owner_t owner
)
444 struct fuse_file
*ff
= file
->private_data
;
445 struct fuse_conn
*fc
= ff
->fc
;
447 fuse_read_fill(req
, file
, pos
, count
, FUSE_READ
);
449 struct fuse_read_in
*inarg
= &req
->misc
.read
.in
;
451 inarg
->read_flags
|= FUSE_READ_LOCKOWNER
;
452 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
454 fuse_request_send(fc
, req
);
455 return req
->out
.args
[0].size
;
458 static void fuse_read_update_size(struct inode
*inode
, loff_t size
,
461 struct fuse_conn
*fc
= get_fuse_conn(inode
);
462 struct fuse_inode
*fi
= get_fuse_inode(inode
);
464 spin_lock(&fc
->lock
);
465 if (attr_ver
== fi
->attr_version
&& size
< inode
->i_size
) {
466 fi
->attr_version
= ++fc
->attr_version
;
467 i_size_write(inode
, size
);
469 spin_unlock(&fc
->lock
);
472 static int fuse_readpage(struct file
*file
, struct page
*page
)
474 struct inode
*inode
= page
->mapping
->host
;
475 struct fuse_conn
*fc
= get_fuse_conn(inode
);
476 struct fuse_req
*req
;
478 loff_t pos
= page_offset(page
);
479 size_t count
= PAGE_CACHE_SIZE
;
484 if (is_bad_inode(inode
))
488 * Page writeback can extend beyond the liftime of the
489 * page-cache page, so make sure we read a properly synced
492 fuse_wait_on_page_writeback(inode
, page
->index
);
494 req
= fuse_get_req(fc
);
499 attr_ver
= fuse_get_attr_version(fc
);
501 req
->out
.page_zeroing
= 1;
502 req
->out
.argpages
= 1;
504 req
->pages
[0] = page
;
505 num_read
= fuse_send_read(req
, file
, pos
, count
, NULL
);
506 err
= req
->out
.h
.error
;
507 fuse_put_request(fc
, req
);
511 * Short read means EOF. If file size is larger, truncate it
513 if (num_read
< count
)
514 fuse_read_update_size(inode
, pos
+ num_read
, attr_ver
);
516 SetPageUptodate(page
);
519 fuse_invalidate_attr(inode
); /* atime changed */
525 static void fuse_readpages_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
528 size_t count
= req
->misc
.read
.in
.size
;
529 size_t num_read
= req
->out
.args
[0].size
;
530 struct address_space
*mapping
= NULL
;
532 for (i
= 0; mapping
== NULL
&& i
< req
->num_pages
; i
++)
533 mapping
= req
->pages
[i
]->mapping
;
536 struct inode
*inode
= mapping
->host
;
539 * Short read means EOF. If file size is larger, truncate it
541 if (!req
->out
.h
.error
&& num_read
< count
) {
544 pos
= page_offset(req
->pages
[0]) + num_read
;
545 fuse_read_update_size(inode
, pos
,
546 req
->misc
.read
.attr_ver
);
548 fuse_invalidate_attr(inode
); /* atime changed */
551 for (i
= 0; i
< req
->num_pages
; i
++) {
552 struct page
*page
= req
->pages
[i
];
553 if (!req
->out
.h
.error
)
554 SetPageUptodate(page
);
558 page_cache_release(page
);
561 fuse_file_put(req
->ff
);
564 static void fuse_send_readpages(struct fuse_req
*req
, struct file
*file
)
566 struct fuse_file
*ff
= file
->private_data
;
567 struct fuse_conn
*fc
= ff
->fc
;
568 loff_t pos
= page_offset(req
->pages
[0]);
569 size_t count
= req
->num_pages
<< PAGE_CACHE_SHIFT
;
571 req
->out
.argpages
= 1;
572 req
->out
.page_zeroing
= 1;
573 req
->out
.page_replace
= 1;
574 fuse_read_fill(req
, file
, pos
, count
, FUSE_READ
);
575 req
->misc
.read
.attr_ver
= fuse_get_attr_version(fc
);
576 if (fc
->async_read
) {
577 req
->ff
= fuse_file_get(ff
);
578 req
->end
= fuse_readpages_end
;
579 fuse_request_send_background(fc
, req
);
581 fuse_request_send(fc
, req
);
582 fuse_readpages_end(fc
, req
);
583 fuse_put_request(fc
, req
);
587 struct fuse_fill_data
{
588 struct fuse_req
*req
;
593 static int fuse_readpages_fill(void *_data
, struct page
*page
)
595 struct fuse_fill_data
*data
= _data
;
596 struct fuse_req
*req
= data
->req
;
597 struct inode
*inode
= data
->inode
;
598 struct fuse_conn
*fc
= get_fuse_conn(inode
);
600 fuse_wait_on_page_writeback(inode
, page
->index
);
602 if (req
->num_pages
&&
603 (req
->num_pages
== FUSE_MAX_PAGES_PER_REQ
||
604 (req
->num_pages
+ 1) * PAGE_CACHE_SIZE
> fc
->max_read
||
605 req
->pages
[req
->num_pages
- 1]->index
+ 1 != page
->index
)) {
606 fuse_send_readpages(req
, data
->file
);
607 data
->req
= req
= fuse_get_req(fc
);
613 page_cache_get(page
);
614 req
->pages
[req
->num_pages
] = page
;
619 static int fuse_readpages(struct file
*file
, struct address_space
*mapping
,
620 struct list_head
*pages
, unsigned nr_pages
)
622 struct inode
*inode
= mapping
->host
;
623 struct fuse_conn
*fc
= get_fuse_conn(inode
);
624 struct fuse_fill_data data
;
628 if (is_bad_inode(inode
))
633 data
.req
= fuse_get_req(fc
);
634 err
= PTR_ERR(data
.req
);
635 if (IS_ERR(data
.req
))
638 err
= read_cache_pages(mapping
, pages
, fuse_readpages_fill
, &data
);
640 if (data
.req
->num_pages
)
641 fuse_send_readpages(data
.req
, file
);
643 fuse_put_request(fc
, data
.req
);
649 static ssize_t
fuse_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
650 unsigned long nr_segs
, loff_t pos
)
652 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
654 if (pos
+ iov_length(iov
, nr_segs
) > i_size_read(inode
)) {
657 * If trying to read past EOF, make sure the i_size
658 * attribute is up-to-date.
660 err
= fuse_update_attributes(inode
, NULL
, iocb
->ki_filp
, NULL
);
665 return generic_file_aio_read(iocb
, iov
, nr_segs
, pos
);
668 static void fuse_write_fill(struct fuse_req
*req
, struct fuse_file
*ff
,
669 loff_t pos
, size_t count
)
671 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
672 struct fuse_write_out
*outarg
= &req
->misc
.write
.out
;
677 req
->in
.h
.opcode
= FUSE_WRITE
;
678 req
->in
.h
.nodeid
= ff
->nodeid
;
680 if (ff
->fc
->minor
< 9)
681 req
->in
.args
[0].size
= FUSE_COMPAT_WRITE_IN_SIZE
;
683 req
->in
.args
[0].size
= sizeof(struct fuse_write_in
);
684 req
->in
.args
[0].value
= inarg
;
685 req
->in
.args
[1].size
= count
;
686 req
->out
.numargs
= 1;
687 req
->out
.args
[0].size
= sizeof(struct fuse_write_out
);
688 req
->out
.args
[0].value
= outarg
;
691 static size_t fuse_send_write(struct fuse_req
*req
, struct file
*file
,
692 loff_t pos
, size_t count
, fl_owner_t owner
)
694 struct fuse_file
*ff
= file
->private_data
;
695 struct fuse_conn
*fc
= ff
->fc
;
696 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
698 fuse_write_fill(req
, ff
, pos
, count
);
699 inarg
->flags
= file
->f_flags
;
701 inarg
->write_flags
|= FUSE_WRITE_LOCKOWNER
;
702 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
704 fuse_request_send(fc
, req
);
705 return req
->misc
.write
.out
.size
;
708 static int fuse_write_begin(struct file
*file
, struct address_space
*mapping
,
709 loff_t pos
, unsigned len
, unsigned flags
,
710 struct page
**pagep
, void **fsdata
)
712 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
714 *pagep
= grab_cache_page_write_begin(mapping
, index
, flags
);
720 void fuse_write_update_size(struct inode
*inode
, loff_t pos
)
722 struct fuse_conn
*fc
= get_fuse_conn(inode
);
723 struct fuse_inode
*fi
= get_fuse_inode(inode
);
725 spin_lock(&fc
->lock
);
726 fi
->attr_version
= ++fc
->attr_version
;
727 if (pos
> inode
->i_size
)
728 i_size_write(inode
, pos
);
729 spin_unlock(&fc
->lock
);
732 static int fuse_buffered_write(struct file
*file
, struct inode
*inode
,
733 loff_t pos
, unsigned count
, struct page
*page
)
737 struct fuse_conn
*fc
= get_fuse_conn(inode
);
738 unsigned offset
= pos
& (PAGE_CACHE_SIZE
- 1);
739 struct fuse_req
*req
;
741 if (is_bad_inode(inode
))
745 * Make sure writepages on the same page are not mixed up with
748 fuse_wait_on_page_writeback(inode
, page
->index
);
750 req
= fuse_get_req(fc
);
754 req
->in
.argpages
= 1;
756 req
->pages
[0] = page
;
757 req
->page_offset
= offset
;
758 nres
= fuse_send_write(req
, file
, pos
, count
, NULL
);
759 err
= req
->out
.h
.error
;
760 fuse_put_request(fc
, req
);
765 fuse_write_update_size(inode
, pos
);
766 if (count
== PAGE_CACHE_SIZE
)
767 SetPageUptodate(page
);
769 fuse_invalidate_attr(inode
);
770 return err
? err
: nres
;
773 static int fuse_write_end(struct file
*file
, struct address_space
*mapping
,
774 loff_t pos
, unsigned len
, unsigned copied
,
775 struct page
*page
, void *fsdata
)
777 struct inode
*inode
= mapping
->host
;
781 res
= fuse_buffered_write(file
, inode
, pos
, copied
, page
);
784 page_cache_release(page
);
788 static size_t fuse_send_write_pages(struct fuse_req
*req
, struct file
*file
,
789 struct inode
*inode
, loff_t pos
,
796 for (i
= 0; i
< req
->num_pages
; i
++)
797 fuse_wait_on_page_writeback(inode
, req
->pages
[i
]->index
);
799 res
= fuse_send_write(req
, file
, pos
, count
, NULL
);
801 offset
= req
->page_offset
;
803 for (i
= 0; i
< req
->num_pages
; i
++) {
804 struct page
*page
= req
->pages
[i
];
806 if (!req
->out
.h
.error
&& !offset
&& count
>= PAGE_CACHE_SIZE
)
807 SetPageUptodate(page
);
809 if (count
> PAGE_CACHE_SIZE
- offset
)
810 count
-= PAGE_CACHE_SIZE
- offset
;
816 page_cache_release(page
);
822 static ssize_t
fuse_fill_write_pages(struct fuse_req
*req
,
823 struct address_space
*mapping
,
824 struct iov_iter
*ii
, loff_t pos
)
826 struct fuse_conn
*fc
= get_fuse_conn(mapping
->host
);
827 unsigned offset
= pos
& (PAGE_CACHE_SIZE
- 1);
831 req
->in
.argpages
= 1;
832 req
->page_offset
= offset
;
837 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
838 size_t bytes
= min_t(size_t, PAGE_CACHE_SIZE
- offset
,
841 bytes
= min_t(size_t, bytes
, fc
->max_write
- count
);
845 if (iov_iter_fault_in_readable(ii
, bytes
))
849 page
= grab_cache_page_write_begin(mapping
, index
, 0);
853 if (mapping_writably_mapped(mapping
))
854 flush_dcache_page(page
);
857 tmp
= iov_iter_copy_from_user_atomic(page
, ii
, offset
, bytes
);
859 flush_dcache_page(page
);
863 page_cache_release(page
);
864 bytes
= min(bytes
, iov_iter_single_seg_count(ii
));
869 req
->pages
[req
->num_pages
] = page
;
872 iov_iter_advance(ii
, tmp
);
876 if (offset
== PAGE_CACHE_SIZE
)
881 } while (iov_iter_count(ii
) && count
< fc
->max_write
&&
882 req
->num_pages
< FUSE_MAX_PAGES_PER_REQ
&& offset
== 0);
884 return count
> 0 ? count
: err
;
887 static ssize_t
fuse_perform_write(struct file
*file
,
888 struct address_space
*mapping
,
889 struct iov_iter
*ii
, loff_t pos
)
891 struct inode
*inode
= mapping
->host
;
892 struct fuse_conn
*fc
= get_fuse_conn(inode
);
896 if (is_bad_inode(inode
))
900 struct fuse_req
*req
;
903 req
= fuse_get_req(fc
);
909 count
= fuse_fill_write_pages(req
, mapping
, ii
, pos
);
915 num_written
= fuse_send_write_pages(req
, file
, inode
,
917 err
= req
->out
.h
.error
;
922 /* break out of the loop on short write */
923 if (num_written
!= count
)
927 fuse_put_request(fc
, req
);
928 } while (!err
&& iov_iter_count(ii
));
931 fuse_write_update_size(inode
, pos
);
933 fuse_invalidate_attr(inode
);
935 return res
> 0 ? res
: err
;
938 static ssize_t
fuse_file_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
939 unsigned long nr_segs
, loff_t pos
)
941 struct file
*file
= iocb
->ki_filp
;
942 struct address_space
*mapping
= file
->f_mapping
;
945 struct inode
*inode
= mapping
->host
;
949 WARN_ON(iocb
->ki_pos
!= pos
);
951 err
= generic_segment_checks(iov
, &nr_segs
, &count
, VERIFY_READ
);
955 mutex_lock(&inode
->i_mutex
);
956 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
958 /* We can write back this queue in page reclaim */
959 current
->backing_dev_info
= mapping
->backing_dev_info
;
961 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
968 err
= file_remove_suid(file
);
972 file_update_time(file
);
974 iov_iter_init(&i
, iov
, nr_segs
, count
, 0);
975 written
= fuse_perform_write(file
, mapping
, &i
, pos
);
977 iocb
->ki_pos
= pos
+ written
;
980 current
->backing_dev_info
= NULL
;
981 mutex_unlock(&inode
->i_mutex
);
983 return written
? written
: err
;
986 static void fuse_release_user_pages(struct fuse_req
*req
, int write
)
990 for (i
= 0; i
< req
->num_pages
; i
++) {
991 struct page
*page
= req
->pages
[i
];
993 set_page_dirty_lock(page
);
998 static int fuse_get_user_pages(struct fuse_req
*req
, const char __user
*buf
,
999 size_t *nbytesp
, int write
)
1001 size_t nbytes
= *nbytesp
;
1002 unsigned long user_addr
= (unsigned long) buf
;
1003 unsigned offset
= user_addr
& ~PAGE_MASK
;
1006 /* Special case for kernel I/O: can copy directly into the buffer */
1007 if (segment_eq(get_fs(), KERNEL_DS
)) {
1009 req
->in
.args
[1].value
= (void *) user_addr
;
1011 req
->out
.args
[0].value
= (void *) user_addr
;
1016 nbytes
= min_t(size_t, nbytes
, FUSE_MAX_PAGES_PER_REQ
<< PAGE_SHIFT
);
1017 npages
= (nbytes
+ offset
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1018 npages
= clamp(npages
, 1, FUSE_MAX_PAGES_PER_REQ
);
1019 npages
= get_user_pages_fast(user_addr
, npages
, !write
, req
->pages
);
1023 req
->num_pages
= npages
;
1024 req
->page_offset
= offset
;
1027 req
->in
.argpages
= 1;
1029 req
->out
.argpages
= 1;
1031 nbytes
= (req
->num_pages
<< PAGE_SHIFT
) - req
->page_offset
;
1032 *nbytesp
= min(*nbytesp
, nbytes
);
1037 ssize_t
fuse_direct_io(struct file
*file
, const char __user
*buf
,
1038 size_t count
, loff_t
*ppos
, int write
)
1040 struct fuse_file
*ff
= file
->private_data
;
1041 struct fuse_conn
*fc
= ff
->fc
;
1042 size_t nmax
= write
? fc
->max_write
: fc
->max_read
;
1045 struct fuse_req
*req
;
1047 req
= fuse_get_req(fc
);
1049 return PTR_ERR(req
);
1053 fl_owner_t owner
= current
->files
;
1054 size_t nbytes
= min(count
, nmax
);
1055 int err
= fuse_get_user_pages(req
, buf
, &nbytes
, write
);
1062 nres
= fuse_send_write(req
, file
, pos
, nbytes
, owner
);
1064 nres
= fuse_send_read(req
, file
, pos
, nbytes
, owner
);
1066 fuse_release_user_pages(req
, !write
);
1067 if (req
->out
.h
.error
) {
1069 res
= req
->out
.h
.error
;
1071 } else if (nres
> nbytes
) {
1082 fuse_put_request(fc
, req
);
1083 req
= fuse_get_req(fc
);
1089 fuse_put_request(fc
, req
);
1095 EXPORT_SYMBOL_GPL(fuse_direct_io
);
1097 static ssize_t
fuse_direct_read(struct file
*file
, char __user
*buf
,
1098 size_t count
, loff_t
*ppos
)
1101 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1103 if (is_bad_inode(inode
))
1106 res
= fuse_direct_io(file
, buf
, count
, ppos
, 0);
1108 fuse_invalidate_attr(inode
);
1113 static ssize_t
fuse_direct_write(struct file
*file
, const char __user
*buf
,
1114 size_t count
, loff_t
*ppos
)
1116 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1119 if (is_bad_inode(inode
))
1122 /* Don't allow parallel writes to the same file */
1123 mutex_lock(&inode
->i_mutex
);
1124 res
= generic_write_checks(file
, ppos
, &count
, 0);
1126 res
= fuse_direct_io(file
, buf
, count
, ppos
, 1);
1128 fuse_write_update_size(inode
, *ppos
);
1130 mutex_unlock(&inode
->i_mutex
);
1132 fuse_invalidate_attr(inode
);
1137 static void fuse_writepage_free(struct fuse_conn
*fc
, struct fuse_req
*req
)
1139 __free_page(req
->pages
[0]);
1140 fuse_file_put(req
->ff
);
1143 static void fuse_writepage_finish(struct fuse_conn
*fc
, struct fuse_req
*req
)
1145 struct inode
*inode
= req
->inode
;
1146 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1147 struct backing_dev_info
*bdi
= inode
->i_mapping
->backing_dev_info
;
1149 list_del(&req
->writepages_entry
);
1150 dec_bdi_stat(bdi
, BDI_WRITEBACK
);
1151 dec_zone_page_state(req
->pages
[0], NR_WRITEBACK_TEMP
);
1152 bdi_writeout_inc(bdi
);
1153 wake_up(&fi
->page_waitq
);
1156 /* Called under fc->lock, may release and reacquire it */
1157 static void fuse_send_writepage(struct fuse_conn
*fc
, struct fuse_req
*req
)
1158 __releases(fc
->lock
)
1159 __acquires(fc
->lock
)
1161 struct fuse_inode
*fi
= get_fuse_inode(req
->inode
);
1162 loff_t size
= i_size_read(req
->inode
);
1163 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
1168 if (inarg
->offset
+ PAGE_CACHE_SIZE
<= size
) {
1169 inarg
->size
= PAGE_CACHE_SIZE
;
1170 } else if (inarg
->offset
< size
) {
1171 inarg
->size
= size
& (PAGE_CACHE_SIZE
- 1);
1173 /* Got truncated off completely */
1177 req
->in
.args
[1].size
= inarg
->size
;
1179 fuse_request_send_background_locked(fc
, req
);
1183 fuse_writepage_finish(fc
, req
);
1184 spin_unlock(&fc
->lock
);
1185 fuse_writepage_free(fc
, req
);
1186 fuse_put_request(fc
, req
);
1187 spin_lock(&fc
->lock
);
1191 * If fi->writectr is positive (no truncate or fsync going on) send
1192 * all queued writepage requests.
1194 * Called with fc->lock
1196 void fuse_flush_writepages(struct inode
*inode
)
1197 __releases(fc
->lock
)
1198 __acquires(fc
->lock
)
1200 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1201 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1202 struct fuse_req
*req
;
1204 while (fi
->writectr
>= 0 && !list_empty(&fi
->queued_writes
)) {
1205 req
= list_entry(fi
->queued_writes
.next
, struct fuse_req
, list
);
1206 list_del_init(&req
->list
);
1207 fuse_send_writepage(fc
, req
);
1211 static void fuse_writepage_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
1213 struct inode
*inode
= req
->inode
;
1214 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1216 mapping_set_error(inode
->i_mapping
, req
->out
.h
.error
);
1217 spin_lock(&fc
->lock
);
1219 fuse_writepage_finish(fc
, req
);
1220 spin_unlock(&fc
->lock
);
1221 fuse_writepage_free(fc
, req
);
1224 static int fuse_writepage_locked(struct page
*page
)
1226 struct address_space
*mapping
= page
->mapping
;
1227 struct inode
*inode
= mapping
->host
;
1228 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1229 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1230 struct fuse_req
*req
;
1231 struct fuse_file
*ff
;
1232 struct page
*tmp_page
;
1234 set_page_writeback(page
);
1236 req
= fuse_request_alloc_nofs();
1240 tmp_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
1244 spin_lock(&fc
->lock
);
1245 BUG_ON(list_empty(&fi
->write_files
));
1246 ff
= list_entry(fi
->write_files
.next
, struct fuse_file
, write_entry
);
1247 req
->ff
= fuse_file_get(ff
);
1248 spin_unlock(&fc
->lock
);
1250 fuse_write_fill(req
, ff
, page_offset(page
), 0);
1252 copy_highpage(tmp_page
, page
);
1253 req
->misc
.write
.in
.write_flags
|= FUSE_WRITE_CACHE
;
1254 req
->in
.argpages
= 1;
1256 req
->pages
[0] = tmp_page
;
1257 req
->page_offset
= 0;
1258 req
->end
= fuse_writepage_end
;
1261 inc_bdi_stat(mapping
->backing_dev_info
, BDI_WRITEBACK
);
1262 inc_zone_page_state(tmp_page
, NR_WRITEBACK_TEMP
);
1263 end_page_writeback(page
);
1265 spin_lock(&fc
->lock
);
1266 list_add(&req
->writepages_entry
, &fi
->writepages
);
1267 list_add_tail(&req
->list
, &fi
->queued_writes
);
1268 fuse_flush_writepages(inode
);
1269 spin_unlock(&fc
->lock
);
1274 fuse_request_free(req
);
1276 end_page_writeback(page
);
1280 static int fuse_writepage(struct page
*page
, struct writeback_control
*wbc
)
1284 err
= fuse_writepage_locked(page
);
1290 static int fuse_launder_page(struct page
*page
)
1293 if (clear_page_dirty_for_io(page
)) {
1294 struct inode
*inode
= page
->mapping
->host
;
1295 err
= fuse_writepage_locked(page
);
1297 fuse_wait_on_page_writeback(inode
, page
->index
);
1303 * Write back dirty pages now, because there may not be any suitable
1306 static void fuse_vma_close(struct vm_area_struct
*vma
)
1308 filemap_write_and_wait(vma
->vm_file
->f_mapping
);
1312 * Wait for writeback against this page to complete before allowing it
1313 * to be marked dirty again, and hence written back again, possibly
1314 * before the previous writepage completed.
1316 * Block here, instead of in ->writepage(), so that the userspace fs
1317 * can only block processes actually operating on the filesystem.
1319 * Otherwise unprivileged userspace fs would be able to block
1324 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
1326 static int fuse_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1328 struct page
*page
= vmf
->page
;
1330 * Don't use page->mapping as it may become NULL from a
1331 * concurrent truncate.
1333 struct inode
*inode
= vma
->vm_file
->f_mapping
->host
;
1335 fuse_wait_on_page_writeback(inode
, page
->index
);
1339 static const struct vm_operations_struct fuse_file_vm_ops
= {
1340 .close
= fuse_vma_close
,
1341 .fault
= filemap_fault
,
1342 .page_mkwrite
= fuse_page_mkwrite
,
1345 static int fuse_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1347 if ((vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
)) {
1348 struct inode
*inode
= file
->f_dentry
->d_inode
;
1349 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1350 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1351 struct fuse_file
*ff
= file
->private_data
;
1353 * file may be written through mmap, so chain it onto the
1354 * inodes's write_file list
1356 spin_lock(&fc
->lock
);
1357 if (list_empty(&ff
->write_entry
))
1358 list_add(&ff
->write_entry
, &fi
->write_files
);
1359 spin_unlock(&fc
->lock
);
1361 file_accessed(file
);
1362 vma
->vm_ops
= &fuse_file_vm_ops
;
1366 static int fuse_direct_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1368 /* Can't provide the coherency needed for MAP_SHARED */
1369 if (vma
->vm_flags
& VM_MAYSHARE
)
1372 invalidate_inode_pages2(file
->f_mapping
);
1374 return generic_file_mmap(file
, vma
);
1377 static int convert_fuse_file_lock(const struct fuse_file_lock
*ffl
,
1378 struct file_lock
*fl
)
1380 switch (ffl
->type
) {
1386 if (ffl
->start
> OFFSET_MAX
|| ffl
->end
> OFFSET_MAX
||
1387 ffl
->end
< ffl
->start
)
1390 fl
->fl_start
= ffl
->start
;
1391 fl
->fl_end
= ffl
->end
;
1392 fl
->fl_pid
= ffl
->pid
;
1398 fl
->fl_type
= ffl
->type
;
1402 static void fuse_lk_fill(struct fuse_req
*req
, struct file
*file
,
1403 const struct file_lock
*fl
, int opcode
, pid_t pid
,
1406 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1407 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1408 struct fuse_file
*ff
= file
->private_data
;
1409 struct fuse_lk_in
*arg
= &req
->misc
.lk_in
;
1412 arg
->owner
= fuse_lock_owner_id(fc
, fl
->fl_owner
);
1413 arg
->lk
.start
= fl
->fl_start
;
1414 arg
->lk
.end
= fl
->fl_end
;
1415 arg
->lk
.type
= fl
->fl_type
;
1418 arg
->lk_flags
|= FUSE_LK_FLOCK
;
1419 req
->in
.h
.opcode
= opcode
;
1420 req
->in
.h
.nodeid
= get_node_id(inode
);
1421 req
->in
.numargs
= 1;
1422 req
->in
.args
[0].size
= sizeof(*arg
);
1423 req
->in
.args
[0].value
= arg
;
1426 static int fuse_getlk(struct file
*file
, struct file_lock
*fl
)
1428 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1429 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1430 struct fuse_req
*req
;
1431 struct fuse_lk_out outarg
;
1434 req
= fuse_get_req(fc
);
1436 return PTR_ERR(req
);
1438 fuse_lk_fill(req
, file
, fl
, FUSE_GETLK
, 0, 0);
1439 req
->out
.numargs
= 1;
1440 req
->out
.args
[0].size
= sizeof(outarg
);
1441 req
->out
.args
[0].value
= &outarg
;
1442 fuse_request_send(fc
, req
);
1443 err
= req
->out
.h
.error
;
1444 fuse_put_request(fc
, req
);
1446 err
= convert_fuse_file_lock(&outarg
.lk
, fl
);
1451 static int fuse_setlk(struct file
*file
, struct file_lock
*fl
, int flock
)
1453 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1454 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1455 struct fuse_req
*req
;
1456 int opcode
= (fl
->fl_flags
& FL_SLEEP
) ? FUSE_SETLKW
: FUSE_SETLK
;
1457 pid_t pid
= fl
->fl_type
!= F_UNLCK
? current
->tgid
: 0;
1460 if (fl
->fl_lmops
&& fl
->fl_lmops
->fl_grant
) {
1461 /* NLM needs asynchronous locks, which we don't support yet */
1465 /* Unlock on close is handled by the flush method */
1466 if (fl
->fl_flags
& FL_CLOSE
)
1469 req
= fuse_get_req(fc
);
1471 return PTR_ERR(req
);
1473 fuse_lk_fill(req
, file
, fl
, opcode
, pid
, flock
);
1474 fuse_request_send(fc
, req
);
1475 err
= req
->out
.h
.error
;
1476 /* locking is restartable */
1479 fuse_put_request(fc
, req
);
1483 static int fuse_file_lock(struct file
*file
, int cmd
, struct file_lock
*fl
)
1485 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1486 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1489 if (cmd
== F_CANCELLK
) {
1491 } else if (cmd
== F_GETLK
) {
1493 posix_test_lock(file
, fl
);
1496 err
= fuse_getlk(file
, fl
);
1499 err
= posix_lock_file(file
, fl
, NULL
);
1501 err
= fuse_setlk(file
, fl
, 0);
1506 static int fuse_file_flock(struct file
*file
, int cmd
, struct file_lock
*fl
)
1508 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1509 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1513 err
= flock_lock_file_wait(file
, fl
);
1515 /* emulate flock with POSIX locks */
1516 fl
->fl_owner
= (fl_owner_t
) file
;
1517 err
= fuse_setlk(file
, fl
, 1);
1523 static sector_t
fuse_bmap(struct address_space
*mapping
, sector_t block
)
1525 struct inode
*inode
= mapping
->host
;
1526 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1527 struct fuse_req
*req
;
1528 struct fuse_bmap_in inarg
;
1529 struct fuse_bmap_out outarg
;
1532 if (!inode
->i_sb
->s_bdev
|| fc
->no_bmap
)
1535 req
= fuse_get_req(fc
);
1539 memset(&inarg
, 0, sizeof(inarg
));
1540 inarg
.block
= block
;
1541 inarg
.blocksize
= inode
->i_sb
->s_blocksize
;
1542 req
->in
.h
.opcode
= FUSE_BMAP
;
1543 req
->in
.h
.nodeid
= get_node_id(inode
);
1544 req
->in
.numargs
= 1;
1545 req
->in
.args
[0].size
= sizeof(inarg
);
1546 req
->in
.args
[0].value
= &inarg
;
1547 req
->out
.numargs
= 1;
1548 req
->out
.args
[0].size
= sizeof(outarg
);
1549 req
->out
.args
[0].value
= &outarg
;
1550 fuse_request_send(fc
, req
);
1551 err
= req
->out
.h
.error
;
1552 fuse_put_request(fc
, req
);
1556 return err
? 0 : outarg
.block
;
1559 static loff_t
fuse_file_llseek(struct file
*file
, loff_t offset
, int origin
)
1562 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1564 mutex_lock(&inode
->i_mutex
);
1567 retval
= fuse_update_attributes(inode
, NULL
, file
, NULL
);
1570 offset
+= i_size_read(inode
);
1573 offset
+= file
->f_pos
;
1576 if (offset
>= 0 && offset
<= inode
->i_sb
->s_maxbytes
) {
1577 if (offset
!= file
->f_pos
) {
1578 file
->f_pos
= offset
;
1579 file
->f_version
= 0;
1584 mutex_unlock(&inode
->i_mutex
);
1588 static int fuse_ioctl_copy_user(struct page
**pages
, struct iovec
*iov
,
1589 unsigned int nr_segs
, size_t bytes
, bool to_user
)
1597 iov_iter_init(&ii
, iov
, nr_segs
, bytes
, 0);
1599 while (iov_iter_count(&ii
)) {
1600 struct page
*page
= pages
[page_idx
++];
1601 size_t todo
= min_t(size_t, PAGE_SIZE
, iov_iter_count(&ii
));
1607 char __user
*uaddr
= ii
.iov
->iov_base
+ ii
.iov_offset
;
1608 size_t iov_len
= ii
.iov
->iov_len
- ii
.iov_offset
;
1609 size_t copy
= min(todo
, iov_len
);
1613 left
= copy_from_user(kaddr
, uaddr
, copy
);
1615 left
= copy_to_user(uaddr
, kaddr
, copy
);
1620 iov_iter_advance(&ii
, copy
);
1632 * CUSE servers compiled on 32bit broke on 64bit kernels because the
1633 * ABI was defined to be 'struct iovec' which is different on 32bit
1634 * and 64bit. Fortunately we can determine which structure the server
1635 * used from the size of the reply.
1637 static int fuse_copy_ioctl_iovec_old(struct iovec
*dst
, void *src
,
1638 size_t transferred
, unsigned count
,
1641 #ifdef CONFIG_COMPAT
1642 if (count
* sizeof(struct compat_iovec
) == transferred
) {
1643 struct compat_iovec
*ciov
= src
;
1647 * With this interface a 32bit server cannot support
1648 * non-compat (i.e. ones coming from 64bit apps) ioctl
1654 for (i
= 0; i
< count
; i
++) {
1655 dst
[i
].iov_base
= compat_ptr(ciov
[i
].iov_base
);
1656 dst
[i
].iov_len
= ciov
[i
].iov_len
;
1662 if (count
* sizeof(struct iovec
) != transferred
)
1665 memcpy(dst
, src
, transferred
);
1669 /* Make sure iov_length() won't overflow */
1670 static int fuse_verify_ioctl_iov(struct iovec
*iov
, size_t count
)
1673 u32 max
= FUSE_MAX_PAGES_PER_REQ
<< PAGE_SHIFT
;
1675 for (n
= 0; n
< count
; n
++) {
1676 if (iov
->iov_len
> (size_t) max
)
1678 max
-= iov
->iov_len
;
1683 static int fuse_copy_ioctl_iovec(struct fuse_conn
*fc
, struct iovec
*dst
,
1684 void *src
, size_t transferred
, unsigned count
,
1688 struct fuse_ioctl_iovec
*fiov
= src
;
1690 if (fc
->minor
< 16) {
1691 return fuse_copy_ioctl_iovec_old(dst
, src
, transferred
,
1695 if (count
* sizeof(struct fuse_ioctl_iovec
) != transferred
)
1698 for (i
= 0; i
< count
; i
++) {
1699 /* Did the server supply an inappropriate value? */
1700 if (fiov
[i
].base
!= (unsigned long) fiov
[i
].base
||
1701 fiov
[i
].len
!= (unsigned long) fiov
[i
].len
)
1704 dst
[i
].iov_base
= (void __user
*) (unsigned long) fiov
[i
].base
;
1705 dst
[i
].iov_len
= (size_t) fiov
[i
].len
;
1707 #ifdef CONFIG_COMPAT
1709 (ptr_to_compat(dst
[i
].iov_base
) != fiov
[i
].base
||
1710 (compat_size_t
) dst
[i
].iov_len
!= fiov
[i
].len
))
1720 * For ioctls, there is no generic way to determine how much memory
1721 * needs to be read and/or written. Furthermore, ioctls are allowed
1722 * to dereference the passed pointer, so the parameter requires deep
1723 * copying but FUSE has no idea whatsoever about what to copy in or
1726 * This is solved by allowing FUSE server to retry ioctl with
1727 * necessary in/out iovecs. Let's assume the ioctl implementation
1728 * needs to read in the following structure.
1735 * On the first callout to FUSE server, inarg->in_size and
1736 * inarg->out_size will be NULL; then, the server completes the ioctl
1737 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
1738 * the actual iov array to
1740 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
1742 * which tells FUSE to copy in the requested area and retry the ioctl.
1743 * On the second round, the server has access to the structure and
1744 * from that it can tell what to look for next, so on the invocation,
1745 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
1747 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
1748 * { .iov_base = a.buf, .iov_len = a.buflen } }
1750 * FUSE will copy both struct a and the pointed buffer from the
1751 * process doing the ioctl and retry ioctl with both struct a and the
1754 * This time, FUSE server has everything it needs and completes ioctl
1755 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
1757 * Copying data out works the same way.
1759 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
1760 * automatically initializes in and out iovs by decoding @cmd with
1761 * _IOC_* macros and the server is not allowed to request RETRY. This
1762 * limits ioctl data transfers to well-formed ioctls and is the forced
1763 * behavior for all FUSE servers.
1765 long fuse_do_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
,
1768 struct fuse_file
*ff
= file
->private_data
;
1769 struct fuse_conn
*fc
= ff
->fc
;
1770 struct fuse_ioctl_in inarg
= {
1776 struct fuse_ioctl_out outarg
;
1777 struct fuse_req
*req
= NULL
;
1778 struct page
**pages
= NULL
;
1779 struct iovec
*iov_page
= NULL
;
1780 struct iovec
*in_iov
= NULL
, *out_iov
= NULL
;
1781 unsigned int in_iovs
= 0, out_iovs
= 0, num_pages
= 0, max_pages
;
1782 size_t in_size
, out_size
, transferred
;
1785 #if BITS_PER_LONG == 32
1786 inarg
.flags
|= FUSE_IOCTL_32BIT
;
1788 if (flags
& FUSE_IOCTL_COMPAT
)
1789 inarg
.flags
|= FUSE_IOCTL_32BIT
;
1792 /* assume all the iovs returned by client always fits in a page */
1793 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec
) * FUSE_IOCTL_MAX_IOV
> PAGE_SIZE
);
1796 pages
= kzalloc(sizeof(pages
[0]) * FUSE_MAX_PAGES_PER_REQ
, GFP_KERNEL
);
1797 iov_page
= (struct iovec
*) __get_free_page(GFP_KERNEL
);
1798 if (!pages
|| !iov_page
)
1802 * If restricted, initialize IO parameters as encoded in @cmd.
1803 * RETRY from server is not allowed.
1805 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
)) {
1806 struct iovec
*iov
= iov_page
;
1808 iov
->iov_base
= (void __user
*)arg
;
1809 iov
->iov_len
= _IOC_SIZE(cmd
);
1811 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
1816 if (_IOC_DIR(cmd
) & _IOC_READ
) {
1823 inarg
.in_size
= in_size
= iov_length(in_iov
, in_iovs
);
1824 inarg
.out_size
= out_size
= iov_length(out_iov
, out_iovs
);
1827 * Out data can be used either for actual out data or iovs,
1828 * make sure there always is at least one page.
1830 out_size
= max_t(size_t, out_size
, PAGE_SIZE
);
1831 max_pages
= DIV_ROUND_UP(max(in_size
, out_size
), PAGE_SIZE
);
1833 /* make sure there are enough buffer pages and init request with them */
1835 if (max_pages
> FUSE_MAX_PAGES_PER_REQ
)
1837 while (num_pages
< max_pages
) {
1838 pages
[num_pages
] = alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
1839 if (!pages
[num_pages
])
1844 req
= fuse_get_req(fc
);
1850 memcpy(req
->pages
, pages
, sizeof(req
->pages
[0]) * num_pages
);
1851 req
->num_pages
= num_pages
;
1853 /* okay, let's send it to the client */
1854 req
->in
.h
.opcode
= FUSE_IOCTL
;
1855 req
->in
.h
.nodeid
= ff
->nodeid
;
1856 req
->in
.numargs
= 1;
1857 req
->in
.args
[0].size
= sizeof(inarg
);
1858 req
->in
.args
[0].value
= &inarg
;
1861 req
->in
.args
[1].size
= in_size
;
1862 req
->in
.argpages
= 1;
1864 err
= fuse_ioctl_copy_user(pages
, in_iov
, in_iovs
, in_size
,
1870 req
->out
.numargs
= 2;
1871 req
->out
.args
[0].size
= sizeof(outarg
);
1872 req
->out
.args
[0].value
= &outarg
;
1873 req
->out
.args
[1].size
= out_size
;
1874 req
->out
.argpages
= 1;
1875 req
->out
.argvar
= 1;
1877 fuse_request_send(fc
, req
);
1878 err
= req
->out
.h
.error
;
1879 transferred
= req
->out
.args
[1].size
;
1880 fuse_put_request(fc
, req
);
1885 /* did it ask for retry? */
1886 if (outarg
.flags
& FUSE_IOCTL_RETRY
) {
1889 /* no retry if in restricted mode */
1891 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
))
1894 in_iovs
= outarg
.in_iovs
;
1895 out_iovs
= outarg
.out_iovs
;
1898 * Make sure things are in boundary, separate checks
1899 * are to protect against overflow.
1902 if (in_iovs
> FUSE_IOCTL_MAX_IOV
||
1903 out_iovs
> FUSE_IOCTL_MAX_IOV
||
1904 in_iovs
+ out_iovs
> FUSE_IOCTL_MAX_IOV
)
1907 vaddr
= kmap_atomic(pages
[0], KM_USER0
);
1908 err
= fuse_copy_ioctl_iovec(fc
, iov_page
, vaddr
,
1909 transferred
, in_iovs
+ out_iovs
,
1910 (flags
& FUSE_IOCTL_COMPAT
) != 0);
1911 kunmap_atomic(vaddr
, KM_USER0
);
1916 out_iov
= in_iov
+ in_iovs
;
1918 err
= fuse_verify_ioctl_iov(in_iov
, in_iovs
);
1922 err
= fuse_verify_ioctl_iov(out_iov
, out_iovs
);
1930 if (transferred
> inarg
.out_size
)
1933 err
= fuse_ioctl_copy_user(pages
, out_iov
, out_iovs
, transferred
, true);
1936 fuse_put_request(fc
, req
);
1937 free_page((unsigned long) iov_page
);
1939 __free_page(pages
[--num_pages
]);
1942 return err
? err
: outarg
.result
;
1944 EXPORT_SYMBOL_GPL(fuse_do_ioctl
);
1946 static long fuse_file_ioctl_common(struct file
*file
, unsigned int cmd
,
1947 unsigned long arg
, unsigned int flags
)
1949 struct inode
*inode
= file
->f_dentry
->d_inode
;
1950 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1952 if (!fuse_allow_task(fc
, current
))
1955 if (is_bad_inode(inode
))
1958 return fuse_do_ioctl(file
, cmd
, arg
, flags
);
1961 static long fuse_file_ioctl(struct file
*file
, unsigned int cmd
,
1964 return fuse_file_ioctl_common(file
, cmd
, arg
, 0);
1967 static long fuse_file_compat_ioctl(struct file
*file
, unsigned int cmd
,
1970 return fuse_file_ioctl_common(file
, cmd
, arg
, FUSE_IOCTL_COMPAT
);
1974 * All files which have been polled are linked to RB tree
1975 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
1976 * find the matching one.
1978 static struct rb_node
**fuse_find_polled_node(struct fuse_conn
*fc
, u64 kh
,
1979 struct rb_node
**parent_out
)
1981 struct rb_node
**link
= &fc
->polled_files
.rb_node
;
1982 struct rb_node
*last
= NULL
;
1985 struct fuse_file
*ff
;
1988 ff
= rb_entry(last
, struct fuse_file
, polled_node
);
1991 link
= &last
->rb_left
;
1992 else if (kh
> ff
->kh
)
1993 link
= &last
->rb_right
;
2004 * The file is about to be polled. Make sure it's on the polled_files
2005 * RB tree. Note that files once added to the polled_files tree are
2006 * not removed before the file is released. This is because a file
2007 * polled once is likely to be polled again.
2009 static void fuse_register_polled_file(struct fuse_conn
*fc
,
2010 struct fuse_file
*ff
)
2012 spin_lock(&fc
->lock
);
2013 if (RB_EMPTY_NODE(&ff
->polled_node
)) {
2014 struct rb_node
**link
, *parent
;
2016 link
= fuse_find_polled_node(fc
, ff
->kh
, &parent
);
2018 rb_link_node(&ff
->polled_node
, parent
, link
);
2019 rb_insert_color(&ff
->polled_node
, &fc
->polled_files
);
2021 spin_unlock(&fc
->lock
);
2024 unsigned fuse_file_poll(struct file
*file
, poll_table
*wait
)
2026 struct fuse_file
*ff
= file
->private_data
;
2027 struct fuse_conn
*fc
= ff
->fc
;
2028 struct fuse_poll_in inarg
= { .fh
= ff
->fh
, .kh
= ff
->kh
};
2029 struct fuse_poll_out outarg
;
2030 struct fuse_req
*req
;
2034 return DEFAULT_POLLMASK
;
2036 poll_wait(file
, &ff
->poll_wait
, wait
);
2039 * Ask for notification iff there's someone waiting for it.
2040 * The client may ignore the flag and always notify.
2042 if (waitqueue_active(&ff
->poll_wait
)) {
2043 inarg
.flags
|= FUSE_POLL_SCHEDULE_NOTIFY
;
2044 fuse_register_polled_file(fc
, ff
);
2047 req
= fuse_get_req(fc
);
2051 req
->in
.h
.opcode
= FUSE_POLL
;
2052 req
->in
.h
.nodeid
= ff
->nodeid
;
2053 req
->in
.numargs
= 1;
2054 req
->in
.args
[0].size
= sizeof(inarg
);
2055 req
->in
.args
[0].value
= &inarg
;
2056 req
->out
.numargs
= 1;
2057 req
->out
.args
[0].size
= sizeof(outarg
);
2058 req
->out
.args
[0].value
= &outarg
;
2059 fuse_request_send(fc
, req
);
2060 err
= req
->out
.h
.error
;
2061 fuse_put_request(fc
, req
);
2064 return outarg
.revents
;
2065 if (err
== -ENOSYS
) {
2067 return DEFAULT_POLLMASK
;
2071 EXPORT_SYMBOL_GPL(fuse_file_poll
);
2074 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2075 * wakes up the poll waiters.
2077 int fuse_notify_poll_wakeup(struct fuse_conn
*fc
,
2078 struct fuse_notify_poll_wakeup_out
*outarg
)
2080 u64 kh
= outarg
->kh
;
2081 struct rb_node
**link
;
2083 spin_lock(&fc
->lock
);
2085 link
= fuse_find_polled_node(fc
, kh
, NULL
);
2087 struct fuse_file
*ff
;
2089 ff
= rb_entry(*link
, struct fuse_file
, polled_node
);
2090 wake_up_interruptible_sync(&ff
->poll_wait
);
2093 spin_unlock(&fc
->lock
);
2097 static const struct file_operations fuse_file_operations
= {
2098 .llseek
= fuse_file_llseek
,
2099 .read
= do_sync_read
,
2100 .aio_read
= fuse_file_aio_read
,
2101 .write
= do_sync_write
,
2102 .aio_write
= fuse_file_aio_write
,
2103 .mmap
= fuse_file_mmap
,
2105 .flush
= fuse_flush
,
2106 .release
= fuse_release
,
2107 .fsync
= fuse_fsync
,
2108 .lock
= fuse_file_lock
,
2109 .flock
= fuse_file_flock
,
2110 .splice_read
= generic_file_splice_read
,
2111 .unlocked_ioctl
= fuse_file_ioctl
,
2112 .compat_ioctl
= fuse_file_compat_ioctl
,
2113 .poll
= fuse_file_poll
,
2116 static const struct file_operations fuse_direct_io_file_operations
= {
2117 .llseek
= fuse_file_llseek
,
2118 .read
= fuse_direct_read
,
2119 .write
= fuse_direct_write
,
2120 .mmap
= fuse_direct_mmap
,
2122 .flush
= fuse_flush
,
2123 .release
= fuse_release
,
2124 .fsync
= fuse_fsync
,
2125 .lock
= fuse_file_lock
,
2126 .flock
= fuse_file_flock
,
2127 .unlocked_ioctl
= fuse_file_ioctl
,
2128 .compat_ioctl
= fuse_file_compat_ioctl
,
2129 .poll
= fuse_file_poll
,
2130 /* no splice_read */
2133 static const struct address_space_operations fuse_file_aops
= {
2134 .readpage
= fuse_readpage
,
2135 .writepage
= fuse_writepage
,
2136 .launder_page
= fuse_launder_page
,
2137 .write_begin
= fuse_write_begin
,
2138 .write_end
= fuse_write_end
,
2139 .readpages
= fuse_readpages
,
2140 .set_page_dirty
= __set_page_dirty_nobuffers
,
2144 void fuse_init_file_inode(struct inode
*inode
)
2146 inode
->i_fop
= &fuse_file_operations
;
2147 inode
->i_data
.a_ops
= &fuse_file_aops
;