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_async(struct work_struct
*work
)
95 req
= container_of(work
, struct fuse_req
, misc
.release
.work
);
96 path
= req
->misc
.release
.path
;
97 fc
= get_fuse_conn(path
.dentry
->d_inode
);
99 fuse_put_request(fc
, req
);
103 static void fuse_release_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
105 if (fc
->destroy_req
) {
107 * If this is a fuseblk mount, then it's possible that
108 * releasing the path will result in releasing the
109 * super block and sending the DESTROY request. If
110 * the server is single threaded, this would hang.
111 * For this reason do the path_put() in a separate
114 atomic_inc(&req
->count
);
115 INIT_WORK(&req
->misc
.release
.work
, fuse_release_async
);
116 schedule_work(&req
->misc
.release
.work
);
118 path_put(&req
->misc
.release
.path
);
122 static void fuse_file_put(struct fuse_file
*ff
, bool sync
)
124 if (atomic_dec_and_test(&ff
->count
)) {
125 struct fuse_req
*req
= ff
->reserved_req
;
128 fuse_request_send(ff
->fc
, req
);
129 path_put(&req
->misc
.release
.path
);
130 fuse_put_request(ff
->fc
, req
);
132 req
->end
= fuse_release_end
;
133 fuse_request_send_background(ff
->fc
, req
);
139 int fuse_do_open(struct fuse_conn
*fc
, u64 nodeid
, struct file
*file
,
142 struct fuse_open_out outarg
;
143 struct fuse_file
*ff
;
145 int opcode
= isdir
? FUSE_OPENDIR
: FUSE_OPEN
;
147 ff
= fuse_file_alloc(fc
);
151 err
= fuse_send_open(fc
, nodeid
, file
, opcode
, &outarg
);
158 outarg
.open_flags
&= ~FOPEN_DIRECT_IO
;
162 ff
->open_flags
= outarg
.open_flags
;
163 file
->private_data
= fuse_file_get(ff
);
167 EXPORT_SYMBOL_GPL(fuse_do_open
);
169 void fuse_finish_open(struct inode
*inode
, struct file
*file
)
171 struct fuse_file
*ff
= file
->private_data
;
172 struct fuse_conn
*fc
= get_fuse_conn(inode
);
174 if (ff
->open_flags
& FOPEN_DIRECT_IO
)
175 file
->f_op
= &fuse_direct_io_file_operations
;
176 if (!(ff
->open_flags
& FOPEN_KEEP_CACHE
))
177 invalidate_inode_pages2(inode
->i_mapping
);
178 if (ff
->open_flags
& FOPEN_NONSEEKABLE
)
179 nonseekable_open(inode
, file
);
180 if (fc
->atomic_o_trunc
&& (file
->f_flags
& O_TRUNC
)) {
181 struct fuse_inode
*fi
= get_fuse_inode(inode
);
183 spin_lock(&fc
->lock
);
184 fi
->attr_version
= ++fc
->attr_version
;
185 i_size_write(inode
, 0);
186 spin_unlock(&fc
->lock
);
187 fuse_invalidate_attr(inode
);
191 int fuse_open_common(struct inode
*inode
, struct file
*file
, bool isdir
)
193 struct fuse_conn
*fc
= get_fuse_conn(inode
);
196 /* VFS checks this, but only _after_ ->open() */
197 if (file
->f_flags
& O_DIRECT
)
200 err
= generic_file_open(inode
, file
);
204 err
= fuse_do_open(fc
, get_node_id(inode
), file
, isdir
);
208 fuse_finish_open(inode
, file
);
213 static void fuse_prepare_release(struct fuse_file
*ff
, int flags
, int opcode
)
215 struct fuse_conn
*fc
= ff
->fc
;
216 struct fuse_req
*req
= ff
->reserved_req
;
217 struct fuse_release_in
*inarg
= &req
->misc
.release
.in
;
219 spin_lock(&fc
->lock
);
220 list_del(&ff
->write_entry
);
221 if (!RB_EMPTY_NODE(&ff
->polled_node
))
222 rb_erase(&ff
->polled_node
, &fc
->polled_files
);
223 spin_unlock(&fc
->lock
);
225 wake_up_interruptible_all(&ff
->poll_wait
);
228 inarg
->flags
= flags
;
229 req
->in
.h
.opcode
= opcode
;
230 req
->in
.h
.nodeid
= ff
->nodeid
;
232 req
->in
.args
[0].size
= sizeof(struct fuse_release_in
);
233 req
->in
.args
[0].value
= inarg
;
236 void fuse_release_common(struct file
*file
, int opcode
)
238 struct fuse_file
*ff
;
239 struct fuse_req
*req
;
241 ff
= file
->private_data
;
245 req
= ff
->reserved_req
;
246 fuse_prepare_release(ff
, file
->f_flags
, opcode
);
248 /* Hold vfsmount and dentry until release is finished */
249 path_get(&file
->f_path
);
250 req
->misc
.release
.path
= file
->f_path
;
253 * Normally this will send the RELEASE request, however if
254 * some asynchronous READ or WRITE requests are outstanding,
255 * the sending will be delayed.
257 * Make the release synchronous if this is a fuseblk mount,
258 * synchronous RELEASE is allowed (and desirable) in this case
259 * because the server can be trusted not to screw up.
261 fuse_file_put(ff
, ff
->fc
->destroy_req
!= NULL
);
264 static int fuse_open(struct inode
*inode
, struct file
*file
)
266 return fuse_open_common(inode
, file
, false);
269 static int fuse_release(struct inode
*inode
, struct file
*file
)
271 fuse_release_common(file
, FUSE_RELEASE
);
273 /* return value is ignored by VFS */
277 void fuse_sync_release(struct fuse_file
*ff
, int flags
)
279 WARN_ON(atomic_read(&ff
->count
) > 1);
280 fuse_prepare_release(ff
, flags
, FUSE_RELEASE
);
281 ff
->reserved_req
->force
= 1;
282 fuse_request_send(ff
->fc
, ff
->reserved_req
);
283 fuse_put_request(ff
->fc
, ff
->reserved_req
);
286 EXPORT_SYMBOL_GPL(fuse_sync_release
);
289 * Scramble the ID space with XTEA, so that the value of the files_struct
290 * pointer is not exposed to userspace.
292 u64
fuse_lock_owner_id(struct fuse_conn
*fc
, fl_owner_t id
)
294 u32
*k
= fc
->scramble_key
;
295 u64 v
= (unsigned long) id
;
301 for (i
= 0; i
< 32; i
++) {
302 v0
+= ((v1
<< 4 ^ v1
>> 5) + v1
) ^ (sum
+ k
[sum
& 3]);
304 v1
+= ((v0
<< 4 ^ v0
>> 5) + v0
) ^ (sum
+ k
[sum
>>11 & 3]);
307 return (u64
) v0
+ ((u64
) v1
<< 32);
311 * Check if page is under writeback
313 * This is currently done by walking the list of writepage requests
314 * for the inode, which can be pretty inefficient.
316 static bool fuse_page_is_writeback(struct inode
*inode
, pgoff_t index
)
318 struct fuse_conn
*fc
= get_fuse_conn(inode
);
319 struct fuse_inode
*fi
= get_fuse_inode(inode
);
320 struct fuse_req
*req
;
323 spin_lock(&fc
->lock
);
324 list_for_each_entry(req
, &fi
->writepages
, writepages_entry
) {
327 BUG_ON(req
->inode
!= inode
);
328 curr_index
= req
->misc
.write
.in
.offset
>> PAGE_CACHE_SHIFT
;
329 if (curr_index
== index
) {
334 spin_unlock(&fc
->lock
);
340 * Wait for page writeback to be completed.
342 * Since fuse doesn't rely on the VM writeback tracking, this has to
343 * use some other means.
345 static int fuse_wait_on_page_writeback(struct inode
*inode
, pgoff_t index
)
347 struct fuse_inode
*fi
= get_fuse_inode(inode
);
349 wait_event(fi
->page_waitq
, !fuse_page_is_writeback(inode
, index
));
353 static int fuse_flush(struct file
*file
, fl_owner_t id
)
355 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
356 struct fuse_conn
*fc
= get_fuse_conn(inode
);
357 struct fuse_file
*ff
= file
->private_data
;
358 struct fuse_req
*req
;
359 struct fuse_flush_in inarg
;
362 if (is_bad_inode(inode
))
368 req
= fuse_get_req_nofail(fc
, file
);
369 memset(&inarg
, 0, sizeof(inarg
));
371 inarg
.lock_owner
= fuse_lock_owner_id(fc
, id
);
372 req
->in
.h
.opcode
= FUSE_FLUSH
;
373 req
->in
.h
.nodeid
= get_node_id(inode
);
375 req
->in
.args
[0].size
= sizeof(inarg
);
376 req
->in
.args
[0].value
= &inarg
;
378 fuse_request_send(fc
, req
);
379 err
= req
->out
.h
.error
;
380 fuse_put_request(fc
, req
);
381 if (err
== -ENOSYS
) {
389 * Wait for all pending writepages on the inode to finish.
391 * This is currently done by blocking further writes with FUSE_NOWRITE
392 * and waiting for all sent writes to complete.
394 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
395 * could conflict with truncation.
397 static void fuse_sync_writes(struct inode
*inode
)
399 fuse_set_nowrite(inode
);
400 fuse_release_nowrite(inode
);
403 int fuse_fsync_common(struct file
*file
, loff_t start
, loff_t end
,
404 int datasync
, int isdir
)
406 struct inode
*inode
= file
->f_mapping
->host
;
407 struct fuse_conn
*fc
= get_fuse_conn(inode
);
408 struct fuse_file
*ff
= file
->private_data
;
409 struct fuse_req
*req
;
410 struct fuse_fsync_in inarg
;
413 if (is_bad_inode(inode
))
416 err
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
420 if ((!isdir
&& fc
->no_fsync
) || (isdir
&& fc
->no_fsyncdir
))
423 mutex_lock(&inode
->i_mutex
);
426 * Start writeback against all dirty pages of the inode, then
427 * wait for all outstanding writes, before sending the FSYNC
430 err
= write_inode_now(inode
, 0);
434 fuse_sync_writes(inode
);
436 req
= fuse_get_req(fc
);
442 memset(&inarg
, 0, sizeof(inarg
));
444 inarg
.fsync_flags
= datasync
? 1 : 0;
445 req
->in
.h
.opcode
= isdir
? FUSE_FSYNCDIR
: FUSE_FSYNC
;
446 req
->in
.h
.nodeid
= get_node_id(inode
);
448 req
->in
.args
[0].size
= sizeof(inarg
);
449 req
->in
.args
[0].value
= &inarg
;
450 fuse_request_send(fc
, req
);
451 err
= req
->out
.h
.error
;
452 fuse_put_request(fc
, req
);
453 if (err
== -ENOSYS
) {
461 mutex_unlock(&inode
->i_mutex
);
465 static int fuse_fsync(struct file
*file
, loff_t start
, loff_t end
,
468 return fuse_fsync_common(file
, start
, end
, datasync
, 0);
471 void fuse_read_fill(struct fuse_req
*req
, struct file
*file
, loff_t pos
,
472 size_t count
, int opcode
)
474 struct fuse_read_in
*inarg
= &req
->misc
.read
.in
;
475 struct fuse_file
*ff
= file
->private_data
;
480 inarg
->flags
= file
->f_flags
;
481 req
->in
.h
.opcode
= opcode
;
482 req
->in
.h
.nodeid
= ff
->nodeid
;
484 req
->in
.args
[0].size
= sizeof(struct fuse_read_in
);
485 req
->in
.args
[0].value
= inarg
;
487 req
->out
.numargs
= 1;
488 req
->out
.args
[0].size
= count
;
491 static size_t fuse_send_read(struct fuse_req
*req
, struct file
*file
,
492 loff_t pos
, size_t count
, fl_owner_t owner
)
494 struct fuse_file
*ff
= file
->private_data
;
495 struct fuse_conn
*fc
= ff
->fc
;
497 fuse_read_fill(req
, file
, pos
, count
, FUSE_READ
);
499 struct fuse_read_in
*inarg
= &req
->misc
.read
.in
;
501 inarg
->read_flags
|= FUSE_READ_LOCKOWNER
;
502 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
504 fuse_request_send(fc
, req
);
505 return req
->out
.args
[0].size
;
508 static void fuse_read_update_size(struct inode
*inode
, loff_t size
,
511 struct fuse_conn
*fc
= get_fuse_conn(inode
);
512 struct fuse_inode
*fi
= get_fuse_inode(inode
);
514 spin_lock(&fc
->lock
);
515 if (attr_ver
== fi
->attr_version
&& size
< inode
->i_size
) {
516 fi
->attr_version
= ++fc
->attr_version
;
517 i_size_write(inode
, size
);
519 spin_unlock(&fc
->lock
);
522 static int fuse_readpage(struct file
*file
, struct page
*page
)
524 struct inode
*inode
= page
->mapping
->host
;
525 struct fuse_conn
*fc
= get_fuse_conn(inode
);
526 struct fuse_req
*req
;
528 loff_t pos
= page_offset(page
);
529 size_t count
= PAGE_CACHE_SIZE
;
534 if (is_bad_inode(inode
))
538 * Page writeback can extend beyond the lifetime of the
539 * page-cache page, so make sure we read a properly synced
542 fuse_wait_on_page_writeback(inode
, page
->index
);
544 req
= fuse_get_req(fc
);
549 attr_ver
= fuse_get_attr_version(fc
);
551 req
->out
.page_zeroing
= 1;
552 req
->out
.argpages
= 1;
554 req
->pages
[0] = page
;
555 num_read
= fuse_send_read(req
, file
, pos
, count
, NULL
);
556 err
= req
->out
.h
.error
;
557 fuse_put_request(fc
, req
);
561 * Short read means EOF. If file size is larger, truncate it
563 if (num_read
< count
)
564 fuse_read_update_size(inode
, pos
+ num_read
, attr_ver
);
566 SetPageUptodate(page
);
569 fuse_invalidate_attr(inode
); /* atime changed */
575 static void fuse_readpages_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
578 size_t count
= req
->misc
.read
.in
.size
;
579 size_t num_read
= req
->out
.args
[0].size
;
580 struct address_space
*mapping
= NULL
;
582 for (i
= 0; mapping
== NULL
&& i
< req
->num_pages
; i
++)
583 mapping
= req
->pages
[i
]->mapping
;
586 struct inode
*inode
= mapping
->host
;
589 * Short read means EOF. If file size is larger, truncate it
591 if (!req
->out
.h
.error
&& num_read
< count
) {
594 pos
= page_offset(req
->pages
[0]) + num_read
;
595 fuse_read_update_size(inode
, pos
,
596 req
->misc
.read
.attr_ver
);
598 fuse_invalidate_attr(inode
); /* atime changed */
601 for (i
= 0; i
< req
->num_pages
; i
++) {
602 struct page
*page
= req
->pages
[i
];
603 if (!req
->out
.h
.error
)
604 SetPageUptodate(page
);
608 page_cache_release(page
);
611 fuse_file_put(req
->ff
, false);
614 static void fuse_send_readpages(struct fuse_req
*req
, struct file
*file
)
616 struct fuse_file
*ff
= file
->private_data
;
617 struct fuse_conn
*fc
= ff
->fc
;
618 loff_t pos
= page_offset(req
->pages
[0]);
619 size_t count
= req
->num_pages
<< PAGE_CACHE_SHIFT
;
621 req
->out
.argpages
= 1;
622 req
->out
.page_zeroing
= 1;
623 req
->out
.page_replace
= 1;
624 fuse_read_fill(req
, file
, pos
, count
, FUSE_READ
);
625 req
->misc
.read
.attr_ver
= fuse_get_attr_version(fc
);
626 if (fc
->async_read
) {
627 req
->ff
= fuse_file_get(ff
);
628 req
->end
= fuse_readpages_end
;
629 fuse_request_send_background(fc
, req
);
631 fuse_request_send(fc
, req
);
632 fuse_readpages_end(fc
, req
);
633 fuse_put_request(fc
, req
);
637 struct fuse_fill_data
{
638 struct fuse_req
*req
;
643 static int fuse_readpages_fill(void *_data
, struct page
*page
)
645 struct fuse_fill_data
*data
= _data
;
646 struct fuse_req
*req
= data
->req
;
647 struct inode
*inode
= data
->inode
;
648 struct fuse_conn
*fc
= get_fuse_conn(inode
);
650 fuse_wait_on_page_writeback(inode
, page
->index
);
652 if (req
->num_pages
&&
653 (req
->num_pages
== FUSE_MAX_PAGES_PER_REQ
||
654 (req
->num_pages
+ 1) * PAGE_CACHE_SIZE
> fc
->max_read
||
655 req
->pages
[req
->num_pages
- 1]->index
+ 1 != page
->index
)) {
656 fuse_send_readpages(req
, data
->file
);
657 data
->req
= req
= fuse_get_req(fc
);
663 page_cache_get(page
);
664 req
->pages
[req
->num_pages
] = page
;
669 static int fuse_readpages(struct file
*file
, struct address_space
*mapping
,
670 struct list_head
*pages
, unsigned nr_pages
)
672 struct inode
*inode
= mapping
->host
;
673 struct fuse_conn
*fc
= get_fuse_conn(inode
);
674 struct fuse_fill_data data
;
678 if (is_bad_inode(inode
))
683 data
.req
= fuse_get_req(fc
);
684 err
= PTR_ERR(data
.req
);
685 if (IS_ERR(data
.req
))
688 err
= read_cache_pages(mapping
, pages
, fuse_readpages_fill
, &data
);
690 if (data
.req
->num_pages
)
691 fuse_send_readpages(data
.req
, file
);
693 fuse_put_request(fc
, data
.req
);
699 static ssize_t
fuse_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
700 unsigned long nr_segs
, loff_t pos
)
702 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
704 if (pos
+ iov_length(iov
, nr_segs
) > i_size_read(inode
)) {
707 * If trying to read past EOF, make sure the i_size
708 * attribute is up-to-date.
710 err
= fuse_update_attributes(inode
, NULL
, iocb
->ki_filp
, NULL
);
715 return generic_file_aio_read(iocb
, iov
, nr_segs
, pos
);
718 static void fuse_write_fill(struct fuse_req
*req
, struct fuse_file
*ff
,
719 loff_t pos
, size_t count
)
721 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
722 struct fuse_write_out
*outarg
= &req
->misc
.write
.out
;
727 req
->in
.h
.opcode
= FUSE_WRITE
;
728 req
->in
.h
.nodeid
= ff
->nodeid
;
730 if (ff
->fc
->minor
< 9)
731 req
->in
.args
[0].size
= FUSE_COMPAT_WRITE_IN_SIZE
;
733 req
->in
.args
[0].size
= sizeof(struct fuse_write_in
);
734 req
->in
.args
[0].value
= inarg
;
735 req
->in
.args
[1].size
= count
;
736 req
->out
.numargs
= 1;
737 req
->out
.args
[0].size
= sizeof(struct fuse_write_out
);
738 req
->out
.args
[0].value
= outarg
;
741 static size_t fuse_send_write(struct fuse_req
*req
, struct file
*file
,
742 loff_t pos
, size_t count
, fl_owner_t owner
)
744 struct fuse_file
*ff
= file
->private_data
;
745 struct fuse_conn
*fc
= ff
->fc
;
746 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
748 fuse_write_fill(req
, ff
, pos
, count
);
749 inarg
->flags
= file
->f_flags
;
751 inarg
->write_flags
|= FUSE_WRITE_LOCKOWNER
;
752 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
754 fuse_request_send(fc
, req
);
755 return req
->misc
.write
.out
.size
;
758 static int fuse_write_begin(struct file
*file
, struct address_space
*mapping
,
759 loff_t pos
, unsigned len
, unsigned flags
,
760 struct page
**pagep
, void **fsdata
)
762 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
764 *pagep
= grab_cache_page_write_begin(mapping
, index
, flags
);
770 void fuse_write_update_size(struct inode
*inode
, loff_t pos
)
772 struct fuse_conn
*fc
= get_fuse_conn(inode
);
773 struct fuse_inode
*fi
= get_fuse_inode(inode
);
775 spin_lock(&fc
->lock
);
776 fi
->attr_version
= ++fc
->attr_version
;
777 if (pos
> inode
->i_size
)
778 i_size_write(inode
, pos
);
779 spin_unlock(&fc
->lock
);
782 static int fuse_buffered_write(struct file
*file
, struct inode
*inode
,
783 loff_t pos
, unsigned count
, struct page
*page
)
787 struct fuse_conn
*fc
= get_fuse_conn(inode
);
788 unsigned offset
= pos
& (PAGE_CACHE_SIZE
- 1);
789 struct fuse_req
*req
;
791 if (is_bad_inode(inode
))
795 * Make sure writepages on the same page are not mixed up with
798 fuse_wait_on_page_writeback(inode
, page
->index
);
800 req
= fuse_get_req(fc
);
804 req
->in
.argpages
= 1;
806 req
->pages
[0] = page
;
807 req
->page_offset
= offset
;
808 nres
= fuse_send_write(req
, file
, pos
, count
, NULL
);
809 err
= req
->out
.h
.error
;
810 fuse_put_request(fc
, req
);
815 fuse_write_update_size(inode
, pos
);
816 if (count
== PAGE_CACHE_SIZE
)
817 SetPageUptodate(page
);
819 fuse_invalidate_attr(inode
);
820 return err
? err
: nres
;
823 static int fuse_write_end(struct file
*file
, struct address_space
*mapping
,
824 loff_t pos
, unsigned len
, unsigned copied
,
825 struct page
*page
, void *fsdata
)
827 struct inode
*inode
= mapping
->host
;
831 res
= fuse_buffered_write(file
, inode
, pos
, copied
, page
);
834 page_cache_release(page
);
838 static size_t fuse_send_write_pages(struct fuse_req
*req
, struct file
*file
,
839 struct inode
*inode
, loff_t pos
,
846 for (i
= 0; i
< req
->num_pages
; i
++)
847 fuse_wait_on_page_writeback(inode
, req
->pages
[i
]->index
);
849 res
= fuse_send_write(req
, file
, pos
, count
, NULL
);
851 offset
= req
->page_offset
;
853 for (i
= 0; i
< req
->num_pages
; i
++) {
854 struct page
*page
= req
->pages
[i
];
856 if (!req
->out
.h
.error
&& !offset
&& count
>= PAGE_CACHE_SIZE
)
857 SetPageUptodate(page
);
859 if (count
> PAGE_CACHE_SIZE
- offset
)
860 count
-= PAGE_CACHE_SIZE
- offset
;
866 page_cache_release(page
);
872 static ssize_t
fuse_fill_write_pages(struct fuse_req
*req
,
873 struct address_space
*mapping
,
874 struct iov_iter
*ii
, loff_t pos
)
876 struct fuse_conn
*fc
= get_fuse_conn(mapping
->host
);
877 unsigned offset
= pos
& (PAGE_CACHE_SIZE
- 1);
881 req
->in
.argpages
= 1;
882 req
->page_offset
= offset
;
887 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
888 size_t bytes
= min_t(size_t, PAGE_CACHE_SIZE
- offset
,
891 bytes
= min_t(size_t, bytes
, fc
->max_write
- count
);
895 if (iov_iter_fault_in_readable(ii
, bytes
))
899 page
= grab_cache_page_write_begin(mapping
, index
, 0);
903 if (mapping_writably_mapped(mapping
))
904 flush_dcache_page(page
);
907 tmp
= iov_iter_copy_from_user_atomic(page
, ii
, offset
, bytes
);
909 flush_dcache_page(page
);
913 page_cache_release(page
);
914 bytes
= min(bytes
, iov_iter_single_seg_count(ii
));
919 req
->pages
[req
->num_pages
] = page
;
922 iov_iter_advance(ii
, tmp
);
926 if (offset
== PAGE_CACHE_SIZE
)
931 } while (iov_iter_count(ii
) && count
< fc
->max_write
&&
932 req
->num_pages
< FUSE_MAX_PAGES_PER_REQ
&& offset
== 0);
934 return count
> 0 ? count
: err
;
937 static ssize_t
fuse_perform_write(struct file
*file
,
938 struct address_space
*mapping
,
939 struct iov_iter
*ii
, loff_t pos
)
941 struct inode
*inode
= mapping
->host
;
942 struct fuse_conn
*fc
= get_fuse_conn(inode
);
946 if (is_bad_inode(inode
))
950 struct fuse_req
*req
;
953 req
= fuse_get_req(fc
);
959 count
= fuse_fill_write_pages(req
, mapping
, ii
, pos
);
965 num_written
= fuse_send_write_pages(req
, file
, inode
,
967 err
= req
->out
.h
.error
;
972 /* break out of the loop on short write */
973 if (num_written
!= count
)
977 fuse_put_request(fc
, req
);
978 } while (!err
&& iov_iter_count(ii
));
981 fuse_write_update_size(inode
, pos
);
983 fuse_invalidate_attr(inode
);
985 return res
> 0 ? res
: err
;
988 static ssize_t
fuse_file_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
989 unsigned long nr_segs
, loff_t pos
)
991 struct file
*file
= iocb
->ki_filp
;
992 struct address_space
*mapping
= file
->f_mapping
;
995 struct inode
*inode
= mapping
->host
;
999 WARN_ON(iocb
->ki_pos
!= pos
);
1001 err
= generic_segment_checks(iov
, &nr_segs
, &count
, VERIFY_READ
);
1005 mutex_lock(&inode
->i_mutex
);
1006 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
1008 /* We can write back this queue in page reclaim */
1009 current
->backing_dev_info
= mapping
->backing_dev_info
;
1011 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
1018 err
= file_remove_suid(file
);
1022 file_update_time(file
);
1024 iov_iter_init(&i
, iov
, nr_segs
, count
, 0);
1025 written
= fuse_perform_write(file
, mapping
, &i
, pos
);
1027 iocb
->ki_pos
= pos
+ written
;
1030 current
->backing_dev_info
= NULL
;
1031 mutex_unlock(&inode
->i_mutex
);
1033 return written
? written
: err
;
1036 static void fuse_release_user_pages(struct fuse_req
*req
, int write
)
1040 for (i
= 0; i
< req
->num_pages
; i
++) {
1041 struct page
*page
= req
->pages
[i
];
1043 set_page_dirty_lock(page
);
1048 static int fuse_get_user_pages(struct fuse_req
*req
, const char __user
*buf
,
1049 size_t *nbytesp
, int write
)
1051 size_t nbytes
= *nbytesp
;
1052 unsigned long user_addr
= (unsigned long) buf
;
1053 unsigned offset
= user_addr
& ~PAGE_MASK
;
1056 /* Special case for kernel I/O: can copy directly into the buffer */
1057 if (segment_eq(get_fs(), KERNEL_DS
)) {
1059 req
->in
.args
[1].value
= (void *) user_addr
;
1061 req
->out
.args
[0].value
= (void *) user_addr
;
1066 nbytes
= min_t(size_t, nbytes
, FUSE_MAX_PAGES_PER_REQ
<< PAGE_SHIFT
);
1067 npages
= (nbytes
+ offset
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1068 npages
= clamp(npages
, 1, FUSE_MAX_PAGES_PER_REQ
);
1069 npages
= get_user_pages_fast(user_addr
, npages
, !write
, req
->pages
);
1073 req
->num_pages
= npages
;
1074 req
->page_offset
= offset
;
1077 req
->in
.argpages
= 1;
1079 req
->out
.argpages
= 1;
1081 nbytes
= (req
->num_pages
<< PAGE_SHIFT
) - req
->page_offset
;
1082 *nbytesp
= min(*nbytesp
, nbytes
);
1087 ssize_t
fuse_direct_io(struct file
*file
, const char __user
*buf
,
1088 size_t count
, loff_t
*ppos
, int write
)
1090 struct fuse_file
*ff
= file
->private_data
;
1091 struct fuse_conn
*fc
= ff
->fc
;
1092 size_t nmax
= write
? fc
->max_write
: fc
->max_read
;
1095 struct fuse_req
*req
;
1097 req
= fuse_get_req(fc
);
1099 return PTR_ERR(req
);
1103 fl_owner_t owner
= current
->files
;
1104 size_t nbytes
= min(count
, nmax
);
1105 int err
= fuse_get_user_pages(req
, buf
, &nbytes
, write
);
1112 nres
= fuse_send_write(req
, file
, pos
, nbytes
, owner
);
1114 nres
= fuse_send_read(req
, file
, pos
, nbytes
, owner
);
1116 fuse_release_user_pages(req
, !write
);
1117 if (req
->out
.h
.error
) {
1119 res
= req
->out
.h
.error
;
1121 } else if (nres
> nbytes
) {
1132 fuse_put_request(fc
, req
);
1133 req
= fuse_get_req(fc
);
1139 fuse_put_request(fc
, req
);
1145 EXPORT_SYMBOL_GPL(fuse_direct_io
);
1147 static ssize_t
fuse_direct_read(struct file
*file
, char __user
*buf
,
1148 size_t count
, loff_t
*ppos
)
1151 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1153 if (is_bad_inode(inode
))
1156 res
= fuse_direct_io(file
, buf
, count
, ppos
, 0);
1158 fuse_invalidate_attr(inode
);
1163 static ssize_t
fuse_direct_write(struct file
*file
, const char __user
*buf
,
1164 size_t count
, loff_t
*ppos
)
1166 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1169 if (is_bad_inode(inode
))
1172 /* Don't allow parallel writes to the same file */
1173 mutex_lock(&inode
->i_mutex
);
1174 res
= generic_write_checks(file
, ppos
, &count
, 0);
1176 res
= fuse_direct_io(file
, buf
, count
, ppos
, 1);
1178 fuse_write_update_size(inode
, *ppos
);
1180 mutex_unlock(&inode
->i_mutex
);
1182 fuse_invalidate_attr(inode
);
1187 static void fuse_writepage_free(struct fuse_conn
*fc
, struct fuse_req
*req
)
1189 __free_page(req
->pages
[0]);
1190 fuse_file_put(req
->ff
, false);
1193 static void fuse_writepage_finish(struct fuse_conn
*fc
, struct fuse_req
*req
)
1195 struct inode
*inode
= req
->inode
;
1196 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1197 struct backing_dev_info
*bdi
= inode
->i_mapping
->backing_dev_info
;
1199 list_del(&req
->writepages_entry
);
1200 dec_bdi_stat(bdi
, BDI_WRITEBACK
);
1201 dec_zone_page_state(req
->pages
[0], NR_WRITEBACK_TEMP
);
1202 bdi_writeout_inc(bdi
);
1203 wake_up(&fi
->page_waitq
);
1206 /* Called under fc->lock, may release and reacquire it */
1207 static void fuse_send_writepage(struct fuse_conn
*fc
, struct fuse_req
*req
)
1208 __releases(fc
->lock
)
1209 __acquires(fc
->lock
)
1211 struct fuse_inode
*fi
= get_fuse_inode(req
->inode
);
1212 loff_t size
= i_size_read(req
->inode
);
1213 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
1218 if (inarg
->offset
+ PAGE_CACHE_SIZE
<= size
) {
1219 inarg
->size
= PAGE_CACHE_SIZE
;
1220 } else if (inarg
->offset
< size
) {
1221 inarg
->size
= size
& (PAGE_CACHE_SIZE
- 1);
1223 /* Got truncated off completely */
1227 req
->in
.args
[1].size
= inarg
->size
;
1229 fuse_request_send_background_locked(fc
, req
);
1233 fuse_writepage_finish(fc
, req
);
1234 spin_unlock(&fc
->lock
);
1235 fuse_writepage_free(fc
, req
);
1236 fuse_put_request(fc
, req
);
1237 spin_lock(&fc
->lock
);
1241 * If fi->writectr is positive (no truncate or fsync going on) send
1242 * all queued writepage requests.
1244 * Called with fc->lock
1246 void fuse_flush_writepages(struct inode
*inode
)
1247 __releases(fc
->lock
)
1248 __acquires(fc
->lock
)
1250 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1251 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1252 struct fuse_req
*req
;
1254 while (fi
->writectr
>= 0 && !list_empty(&fi
->queued_writes
)) {
1255 req
= list_entry(fi
->queued_writes
.next
, struct fuse_req
, list
);
1256 list_del_init(&req
->list
);
1257 fuse_send_writepage(fc
, req
);
1261 static void fuse_writepage_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
1263 struct inode
*inode
= req
->inode
;
1264 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1266 mapping_set_error(inode
->i_mapping
, req
->out
.h
.error
);
1267 spin_lock(&fc
->lock
);
1269 fuse_writepage_finish(fc
, req
);
1270 spin_unlock(&fc
->lock
);
1271 fuse_writepage_free(fc
, req
);
1274 static int fuse_writepage_locked(struct page
*page
)
1276 struct address_space
*mapping
= page
->mapping
;
1277 struct inode
*inode
= mapping
->host
;
1278 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1279 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1280 struct fuse_req
*req
;
1281 struct fuse_file
*ff
;
1282 struct page
*tmp_page
;
1284 set_page_writeback(page
);
1286 req
= fuse_request_alloc_nofs();
1290 tmp_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
1294 spin_lock(&fc
->lock
);
1295 BUG_ON(list_empty(&fi
->write_files
));
1296 ff
= list_entry(fi
->write_files
.next
, struct fuse_file
, write_entry
);
1297 req
->ff
= fuse_file_get(ff
);
1298 spin_unlock(&fc
->lock
);
1300 fuse_write_fill(req
, ff
, page_offset(page
), 0);
1302 copy_highpage(tmp_page
, page
);
1303 req
->misc
.write
.in
.write_flags
|= FUSE_WRITE_CACHE
;
1304 req
->in
.argpages
= 1;
1306 req
->pages
[0] = tmp_page
;
1307 req
->page_offset
= 0;
1308 req
->end
= fuse_writepage_end
;
1311 inc_bdi_stat(mapping
->backing_dev_info
, BDI_WRITEBACK
);
1312 inc_zone_page_state(tmp_page
, NR_WRITEBACK_TEMP
);
1313 end_page_writeback(page
);
1315 spin_lock(&fc
->lock
);
1316 list_add(&req
->writepages_entry
, &fi
->writepages
);
1317 list_add_tail(&req
->list
, &fi
->queued_writes
);
1318 fuse_flush_writepages(inode
);
1319 spin_unlock(&fc
->lock
);
1324 fuse_request_free(req
);
1326 end_page_writeback(page
);
1330 static int fuse_writepage(struct page
*page
, struct writeback_control
*wbc
)
1334 err
= fuse_writepage_locked(page
);
1340 static int fuse_launder_page(struct page
*page
)
1343 if (clear_page_dirty_for_io(page
)) {
1344 struct inode
*inode
= page
->mapping
->host
;
1345 err
= fuse_writepage_locked(page
);
1347 fuse_wait_on_page_writeback(inode
, page
->index
);
1353 * Write back dirty pages now, because there may not be any suitable
1356 static void fuse_vma_close(struct vm_area_struct
*vma
)
1358 filemap_write_and_wait(vma
->vm_file
->f_mapping
);
1362 * Wait for writeback against this page to complete before allowing it
1363 * to be marked dirty again, and hence written back again, possibly
1364 * before the previous writepage completed.
1366 * Block here, instead of in ->writepage(), so that the userspace fs
1367 * can only block processes actually operating on the filesystem.
1369 * Otherwise unprivileged userspace fs would be able to block
1374 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
1376 static int fuse_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1378 struct page
*page
= vmf
->page
;
1380 * Don't use page->mapping as it may become NULL from a
1381 * concurrent truncate.
1383 struct inode
*inode
= vma
->vm_file
->f_mapping
->host
;
1385 fuse_wait_on_page_writeback(inode
, page
->index
);
1389 static const struct vm_operations_struct fuse_file_vm_ops
= {
1390 .close
= fuse_vma_close
,
1391 .fault
= filemap_fault
,
1392 .page_mkwrite
= fuse_page_mkwrite
,
1395 static int fuse_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1397 if ((vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
)) {
1398 struct inode
*inode
= file
->f_dentry
->d_inode
;
1399 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1400 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1401 struct fuse_file
*ff
= file
->private_data
;
1403 * file may be written through mmap, so chain it onto the
1404 * inodes's write_file list
1406 spin_lock(&fc
->lock
);
1407 if (list_empty(&ff
->write_entry
))
1408 list_add(&ff
->write_entry
, &fi
->write_files
);
1409 spin_unlock(&fc
->lock
);
1411 file_accessed(file
);
1412 vma
->vm_ops
= &fuse_file_vm_ops
;
1416 static int fuse_direct_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1418 /* Can't provide the coherency needed for MAP_SHARED */
1419 if (vma
->vm_flags
& VM_MAYSHARE
)
1422 invalidate_inode_pages2(file
->f_mapping
);
1424 return generic_file_mmap(file
, vma
);
1427 static int convert_fuse_file_lock(const struct fuse_file_lock
*ffl
,
1428 struct file_lock
*fl
)
1430 switch (ffl
->type
) {
1436 if (ffl
->start
> OFFSET_MAX
|| ffl
->end
> OFFSET_MAX
||
1437 ffl
->end
< ffl
->start
)
1440 fl
->fl_start
= ffl
->start
;
1441 fl
->fl_end
= ffl
->end
;
1442 fl
->fl_pid
= ffl
->pid
;
1448 fl
->fl_type
= ffl
->type
;
1452 static void fuse_lk_fill(struct fuse_req
*req
, struct file
*file
,
1453 const struct file_lock
*fl
, int opcode
, pid_t pid
,
1456 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1457 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1458 struct fuse_file
*ff
= file
->private_data
;
1459 struct fuse_lk_in
*arg
= &req
->misc
.lk_in
;
1462 arg
->owner
= fuse_lock_owner_id(fc
, fl
->fl_owner
);
1463 arg
->lk
.start
= fl
->fl_start
;
1464 arg
->lk
.end
= fl
->fl_end
;
1465 arg
->lk
.type
= fl
->fl_type
;
1468 arg
->lk_flags
|= FUSE_LK_FLOCK
;
1469 req
->in
.h
.opcode
= opcode
;
1470 req
->in
.h
.nodeid
= get_node_id(inode
);
1471 req
->in
.numargs
= 1;
1472 req
->in
.args
[0].size
= sizeof(*arg
);
1473 req
->in
.args
[0].value
= arg
;
1476 static int fuse_getlk(struct file
*file
, struct file_lock
*fl
)
1478 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1479 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1480 struct fuse_req
*req
;
1481 struct fuse_lk_out outarg
;
1484 req
= fuse_get_req(fc
);
1486 return PTR_ERR(req
);
1488 fuse_lk_fill(req
, file
, fl
, FUSE_GETLK
, 0, 0);
1489 req
->out
.numargs
= 1;
1490 req
->out
.args
[0].size
= sizeof(outarg
);
1491 req
->out
.args
[0].value
= &outarg
;
1492 fuse_request_send(fc
, req
);
1493 err
= req
->out
.h
.error
;
1494 fuse_put_request(fc
, req
);
1496 err
= convert_fuse_file_lock(&outarg
.lk
, fl
);
1501 static int fuse_setlk(struct file
*file
, struct file_lock
*fl
, int flock
)
1503 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1504 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1505 struct fuse_req
*req
;
1506 int opcode
= (fl
->fl_flags
& FL_SLEEP
) ? FUSE_SETLKW
: FUSE_SETLK
;
1507 pid_t pid
= fl
->fl_type
!= F_UNLCK
? current
->tgid
: 0;
1510 if (fl
->fl_lmops
&& fl
->fl_lmops
->lm_grant
) {
1511 /* NLM needs asynchronous locks, which we don't support yet */
1515 /* Unlock on close is handled by the flush method */
1516 if (fl
->fl_flags
& FL_CLOSE
)
1519 req
= fuse_get_req(fc
);
1521 return PTR_ERR(req
);
1523 fuse_lk_fill(req
, file
, fl
, opcode
, pid
, flock
);
1524 fuse_request_send(fc
, req
);
1525 err
= req
->out
.h
.error
;
1526 /* locking is restartable */
1529 fuse_put_request(fc
, req
);
1533 static int fuse_file_lock(struct file
*file
, int cmd
, struct file_lock
*fl
)
1535 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1536 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1539 if (cmd
== F_CANCELLK
) {
1541 } else if (cmd
== F_GETLK
) {
1543 posix_test_lock(file
, fl
);
1546 err
= fuse_getlk(file
, fl
);
1549 err
= posix_lock_file(file
, fl
, NULL
);
1551 err
= fuse_setlk(file
, fl
, 0);
1556 static int fuse_file_flock(struct file
*file
, int cmd
, struct file_lock
*fl
)
1558 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1559 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1563 err
= flock_lock_file_wait(file
, fl
);
1565 /* emulate flock with POSIX locks */
1566 fl
->fl_owner
= (fl_owner_t
) file
;
1567 err
= fuse_setlk(file
, fl
, 1);
1573 static sector_t
fuse_bmap(struct address_space
*mapping
, sector_t block
)
1575 struct inode
*inode
= mapping
->host
;
1576 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1577 struct fuse_req
*req
;
1578 struct fuse_bmap_in inarg
;
1579 struct fuse_bmap_out outarg
;
1582 if (!inode
->i_sb
->s_bdev
|| fc
->no_bmap
)
1585 req
= fuse_get_req(fc
);
1589 memset(&inarg
, 0, sizeof(inarg
));
1590 inarg
.block
= block
;
1591 inarg
.blocksize
= inode
->i_sb
->s_blocksize
;
1592 req
->in
.h
.opcode
= FUSE_BMAP
;
1593 req
->in
.h
.nodeid
= get_node_id(inode
);
1594 req
->in
.numargs
= 1;
1595 req
->in
.args
[0].size
= sizeof(inarg
);
1596 req
->in
.args
[0].value
= &inarg
;
1597 req
->out
.numargs
= 1;
1598 req
->out
.args
[0].size
= sizeof(outarg
);
1599 req
->out
.args
[0].value
= &outarg
;
1600 fuse_request_send(fc
, req
);
1601 err
= req
->out
.h
.error
;
1602 fuse_put_request(fc
, req
);
1606 return err
? 0 : outarg
.block
;
1609 static loff_t
fuse_file_llseek(struct file
*file
, loff_t offset
, int origin
)
1612 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1614 mutex_lock(&inode
->i_mutex
);
1615 if (origin
!= SEEK_CUR
|| origin
!= SEEK_SET
) {
1616 retval
= fuse_update_attributes(inode
, NULL
, file
, NULL
);
1623 offset
+= i_size_read(inode
);
1626 offset
+= file
->f_pos
;
1629 if (offset
>= i_size_read(inode
)) {
1635 if (offset
>= i_size_read(inode
)) {
1639 offset
= i_size_read(inode
);
1643 if (offset
>= 0 && offset
<= inode
->i_sb
->s_maxbytes
) {
1644 if (offset
!= file
->f_pos
) {
1645 file
->f_pos
= offset
;
1646 file
->f_version
= 0;
1651 mutex_unlock(&inode
->i_mutex
);
1655 static int fuse_ioctl_copy_user(struct page
**pages
, struct iovec
*iov
,
1656 unsigned int nr_segs
, size_t bytes
, bool to_user
)
1664 iov_iter_init(&ii
, iov
, nr_segs
, bytes
, 0);
1666 while (iov_iter_count(&ii
)) {
1667 struct page
*page
= pages
[page_idx
++];
1668 size_t todo
= min_t(size_t, PAGE_SIZE
, iov_iter_count(&ii
));
1674 char __user
*uaddr
= ii
.iov
->iov_base
+ ii
.iov_offset
;
1675 size_t iov_len
= ii
.iov
->iov_len
- ii
.iov_offset
;
1676 size_t copy
= min(todo
, iov_len
);
1680 left
= copy_from_user(kaddr
, uaddr
, copy
);
1682 left
= copy_to_user(uaddr
, kaddr
, copy
);
1687 iov_iter_advance(&ii
, copy
);
1699 * CUSE servers compiled on 32bit broke on 64bit kernels because the
1700 * ABI was defined to be 'struct iovec' which is different on 32bit
1701 * and 64bit. Fortunately we can determine which structure the server
1702 * used from the size of the reply.
1704 static int fuse_copy_ioctl_iovec_old(struct iovec
*dst
, void *src
,
1705 size_t transferred
, unsigned count
,
1708 #ifdef CONFIG_COMPAT
1709 if (count
* sizeof(struct compat_iovec
) == transferred
) {
1710 struct compat_iovec
*ciov
= src
;
1714 * With this interface a 32bit server cannot support
1715 * non-compat (i.e. ones coming from 64bit apps) ioctl
1721 for (i
= 0; i
< count
; i
++) {
1722 dst
[i
].iov_base
= compat_ptr(ciov
[i
].iov_base
);
1723 dst
[i
].iov_len
= ciov
[i
].iov_len
;
1729 if (count
* sizeof(struct iovec
) != transferred
)
1732 memcpy(dst
, src
, transferred
);
1736 /* Make sure iov_length() won't overflow */
1737 static int fuse_verify_ioctl_iov(struct iovec
*iov
, size_t count
)
1740 u32 max
= FUSE_MAX_PAGES_PER_REQ
<< PAGE_SHIFT
;
1742 for (n
= 0; n
< count
; n
++) {
1743 if (iov
->iov_len
> (size_t) max
)
1745 max
-= iov
->iov_len
;
1750 static int fuse_copy_ioctl_iovec(struct fuse_conn
*fc
, struct iovec
*dst
,
1751 void *src
, size_t transferred
, unsigned count
,
1755 struct fuse_ioctl_iovec
*fiov
= src
;
1757 if (fc
->minor
< 16) {
1758 return fuse_copy_ioctl_iovec_old(dst
, src
, transferred
,
1762 if (count
* sizeof(struct fuse_ioctl_iovec
) != transferred
)
1765 for (i
= 0; i
< count
; i
++) {
1766 /* Did the server supply an inappropriate value? */
1767 if (fiov
[i
].base
!= (unsigned long) fiov
[i
].base
||
1768 fiov
[i
].len
!= (unsigned long) fiov
[i
].len
)
1771 dst
[i
].iov_base
= (void __user
*) (unsigned long) fiov
[i
].base
;
1772 dst
[i
].iov_len
= (size_t) fiov
[i
].len
;
1774 #ifdef CONFIG_COMPAT
1776 (ptr_to_compat(dst
[i
].iov_base
) != fiov
[i
].base
||
1777 (compat_size_t
) dst
[i
].iov_len
!= fiov
[i
].len
))
1787 * For ioctls, there is no generic way to determine how much memory
1788 * needs to be read and/or written. Furthermore, ioctls are allowed
1789 * to dereference the passed pointer, so the parameter requires deep
1790 * copying but FUSE has no idea whatsoever about what to copy in or
1793 * This is solved by allowing FUSE server to retry ioctl with
1794 * necessary in/out iovecs. Let's assume the ioctl implementation
1795 * needs to read in the following structure.
1802 * On the first callout to FUSE server, inarg->in_size and
1803 * inarg->out_size will be NULL; then, the server completes the ioctl
1804 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
1805 * the actual iov array to
1807 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
1809 * which tells FUSE to copy in the requested area and retry the ioctl.
1810 * On the second round, the server has access to the structure and
1811 * from that it can tell what to look for next, so on the invocation,
1812 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
1814 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
1815 * { .iov_base = a.buf, .iov_len = a.buflen } }
1817 * FUSE will copy both struct a and the pointed buffer from the
1818 * process doing the ioctl and retry ioctl with both struct a and the
1821 * This time, FUSE server has everything it needs and completes ioctl
1822 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
1824 * Copying data out works the same way.
1826 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
1827 * automatically initializes in and out iovs by decoding @cmd with
1828 * _IOC_* macros and the server is not allowed to request RETRY. This
1829 * limits ioctl data transfers to well-formed ioctls and is the forced
1830 * behavior for all FUSE servers.
1832 long fuse_do_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
,
1835 struct fuse_file
*ff
= file
->private_data
;
1836 struct fuse_conn
*fc
= ff
->fc
;
1837 struct fuse_ioctl_in inarg
= {
1843 struct fuse_ioctl_out outarg
;
1844 struct fuse_req
*req
= NULL
;
1845 struct page
**pages
= NULL
;
1846 struct iovec
*iov_page
= NULL
;
1847 struct iovec
*in_iov
= NULL
, *out_iov
= NULL
;
1848 unsigned int in_iovs
= 0, out_iovs
= 0, num_pages
= 0, max_pages
;
1849 size_t in_size
, out_size
, transferred
;
1852 #if BITS_PER_LONG == 32
1853 inarg
.flags
|= FUSE_IOCTL_32BIT
;
1855 if (flags
& FUSE_IOCTL_COMPAT
)
1856 inarg
.flags
|= FUSE_IOCTL_32BIT
;
1859 /* assume all the iovs returned by client always fits in a page */
1860 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec
) * FUSE_IOCTL_MAX_IOV
> PAGE_SIZE
);
1863 pages
= kzalloc(sizeof(pages
[0]) * FUSE_MAX_PAGES_PER_REQ
, GFP_KERNEL
);
1864 iov_page
= (struct iovec
*) __get_free_page(GFP_KERNEL
);
1865 if (!pages
|| !iov_page
)
1869 * If restricted, initialize IO parameters as encoded in @cmd.
1870 * RETRY from server is not allowed.
1872 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
)) {
1873 struct iovec
*iov
= iov_page
;
1875 iov
->iov_base
= (void __user
*)arg
;
1876 iov
->iov_len
= _IOC_SIZE(cmd
);
1878 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
1883 if (_IOC_DIR(cmd
) & _IOC_READ
) {
1890 inarg
.in_size
= in_size
= iov_length(in_iov
, in_iovs
);
1891 inarg
.out_size
= out_size
= iov_length(out_iov
, out_iovs
);
1894 * Out data can be used either for actual out data or iovs,
1895 * make sure there always is at least one page.
1897 out_size
= max_t(size_t, out_size
, PAGE_SIZE
);
1898 max_pages
= DIV_ROUND_UP(max(in_size
, out_size
), PAGE_SIZE
);
1900 /* make sure there are enough buffer pages and init request with them */
1902 if (max_pages
> FUSE_MAX_PAGES_PER_REQ
)
1904 while (num_pages
< max_pages
) {
1905 pages
[num_pages
] = alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
1906 if (!pages
[num_pages
])
1911 req
= fuse_get_req(fc
);
1917 memcpy(req
->pages
, pages
, sizeof(req
->pages
[0]) * num_pages
);
1918 req
->num_pages
= num_pages
;
1920 /* okay, let's send it to the client */
1921 req
->in
.h
.opcode
= FUSE_IOCTL
;
1922 req
->in
.h
.nodeid
= ff
->nodeid
;
1923 req
->in
.numargs
= 1;
1924 req
->in
.args
[0].size
= sizeof(inarg
);
1925 req
->in
.args
[0].value
= &inarg
;
1928 req
->in
.args
[1].size
= in_size
;
1929 req
->in
.argpages
= 1;
1931 err
= fuse_ioctl_copy_user(pages
, in_iov
, in_iovs
, in_size
,
1937 req
->out
.numargs
= 2;
1938 req
->out
.args
[0].size
= sizeof(outarg
);
1939 req
->out
.args
[0].value
= &outarg
;
1940 req
->out
.args
[1].size
= out_size
;
1941 req
->out
.argpages
= 1;
1942 req
->out
.argvar
= 1;
1944 fuse_request_send(fc
, req
);
1945 err
= req
->out
.h
.error
;
1946 transferred
= req
->out
.args
[1].size
;
1947 fuse_put_request(fc
, req
);
1952 /* did it ask for retry? */
1953 if (outarg
.flags
& FUSE_IOCTL_RETRY
) {
1956 /* no retry if in restricted mode */
1958 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
))
1961 in_iovs
= outarg
.in_iovs
;
1962 out_iovs
= outarg
.out_iovs
;
1965 * Make sure things are in boundary, separate checks
1966 * are to protect against overflow.
1969 if (in_iovs
> FUSE_IOCTL_MAX_IOV
||
1970 out_iovs
> FUSE_IOCTL_MAX_IOV
||
1971 in_iovs
+ out_iovs
> FUSE_IOCTL_MAX_IOV
)
1974 vaddr
= kmap_atomic(pages
[0], KM_USER0
);
1975 err
= fuse_copy_ioctl_iovec(fc
, iov_page
, vaddr
,
1976 transferred
, in_iovs
+ out_iovs
,
1977 (flags
& FUSE_IOCTL_COMPAT
) != 0);
1978 kunmap_atomic(vaddr
, KM_USER0
);
1983 out_iov
= in_iov
+ in_iovs
;
1985 err
= fuse_verify_ioctl_iov(in_iov
, in_iovs
);
1989 err
= fuse_verify_ioctl_iov(out_iov
, out_iovs
);
1997 if (transferred
> inarg
.out_size
)
2000 err
= fuse_ioctl_copy_user(pages
, out_iov
, out_iovs
, transferred
, true);
2003 fuse_put_request(fc
, req
);
2004 free_page((unsigned long) iov_page
);
2006 __free_page(pages
[--num_pages
]);
2009 return err
? err
: outarg
.result
;
2011 EXPORT_SYMBOL_GPL(fuse_do_ioctl
);
2013 static long fuse_file_ioctl_common(struct file
*file
, unsigned int cmd
,
2014 unsigned long arg
, unsigned int flags
)
2016 struct inode
*inode
= file
->f_dentry
->d_inode
;
2017 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2019 if (!fuse_allow_task(fc
, current
))
2022 if (is_bad_inode(inode
))
2025 return fuse_do_ioctl(file
, cmd
, arg
, flags
);
2028 static long fuse_file_ioctl(struct file
*file
, unsigned int cmd
,
2031 return fuse_file_ioctl_common(file
, cmd
, arg
, 0);
2034 static long fuse_file_compat_ioctl(struct file
*file
, unsigned int cmd
,
2037 return fuse_file_ioctl_common(file
, cmd
, arg
, FUSE_IOCTL_COMPAT
);
2041 * All files which have been polled are linked to RB tree
2042 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2043 * find the matching one.
2045 static struct rb_node
**fuse_find_polled_node(struct fuse_conn
*fc
, u64 kh
,
2046 struct rb_node
**parent_out
)
2048 struct rb_node
**link
= &fc
->polled_files
.rb_node
;
2049 struct rb_node
*last
= NULL
;
2052 struct fuse_file
*ff
;
2055 ff
= rb_entry(last
, struct fuse_file
, polled_node
);
2058 link
= &last
->rb_left
;
2059 else if (kh
> ff
->kh
)
2060 link
= &last
->rb_right
;
2071 * The file is about to be polled. Make sure it's on the polled_files
2072 * RB tree. Note that files once added to the polled_files tree are
2073 * not removed before the file is released. This is because a file
2074 * polled once is likely to be polled again.
2076 static void fuse_register_polled_file(struct fuse_conn
*fc
,
2077 struct fuse_file
*ff
)
2079 spin_lock(&fc
->lock
);
2080 if (RB_EMPTY_NODE(&ff
->polled_node
)) {
2081 struct rb_node
**link
, *parent
;
2083 link
= fuse_find_polled_node(fc
, ff
->kh
, &parent
);
2085 rb_link_node(&ff
->polled_node
, parent
, link
);
2086 rb_insert_color(&ff
->polled_node
, &fc
->polled_files
);
2088 spin_unlock(&fc
->lock
);
2091 unsigned fuse_file_poll(struct file
*file
, poll_table
*wait
)
2093 struct fuse_file
*ff
= file
->private_data
;
2094 struct fuse_conn
*fc
= ff
->fc
;
2095 struct fuse_poll_in inarg
= { .fh
= ff
->fh
, .kh
= ff
->kh
};
2096 struct fuse_poll_out outarg
;
2097 struct fuse_req
*req
;
2101 return DEFAULT_POLLMASK
;
2103 poll_wait(file
, &ff
->poll_wait
, wait
);
2106 * Ask for notification iff there's someone waiting for it.
2107 * The client may ignore the flag and always notify.
2109 if (waitqueue_active(&ff
->poll_wait
)) {
2110 inarg
.flags
|= FUSE_POLL_SCHEDULE_NOTIFY
;
2111 fuse_register_polled_file(fc
, ff
);
2114 req
= fuse_get_req(fc
);
2118 req
->in
.h
.opcode
= FUSE_POLL
;
2119 req
->in
.h
.nodeid
= ff
->nodeid
;
2120 req
->in
.numargs
= 1;
2121 req
->in
.args
[0].size
= sizeof(inarg
);
2122 req
->in
.args
[0].value
= &inarg
;
2123 req
->out
.numargs
= 1;
2124 req
->out
.args
[0].size
= sizeof(outarg
);
2125 req
->out
.args
[0].value
= &outarg
;
2126 fuse_request_send(fc
, req
);
2127 err
= req
->out
.h
.error
;
2128 fuse_put_request(fc
, req
);
2131 return outarg
.revents
;
2132 if (err
== -ENOSYS
) {
2134 return DEFAULT_POLLMASK
;
2138 EXPORT_SYMBOL_GPL(fuse_file_poll
);
2141 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2142 * wakes up the poll waiters.
2144 int fuse_notify_poll_wakeup(struct fuse_conn
*fc
,
2145 struct fuse_notify_poll_wakeup_out
*outarg
)
2147 u64 kh
= outarg
->kh
;
2148 struct rb_node
**link
;
2150 spin_lock(&fc
->lock
);
2152 link
= fuse_find_polled_node(fc
, kh
, NULL
);
2154 struct fuse_file
*ff
;
2156 ff
= rb_entry(*link
, struct fuse_file
, polled_node
);
2157 wake_up_interruptible_sync(&ff
->poll_wait
);
2160 spin_unlock(&fc
->lock
);
2164 static const struct file_operations fuse_file_operations
= {
2165 .llseek
= fuse_file_llseek
,
2166 .read
= do_sync_read
,
2167 .aio_read
= fuse_file_aio_read
,
2168 .write
= do_sync_write
,
2169 .aio_write
= fuse_file_aio_write
,
2170 .mmap
= fuse_file_mmap
,
2172 .flush
= fuse_flush
,
2173 .release
= fuse_release
,
2174 .fsync
= fuse_fsync
,
2175 .lock
= fuse_file_lock
,
2176 .flock
= fuse_file_flock
,
2177 .splice_read
= generic_file_splice_read
,
2178 .unlocked_ioctl
= fuse_file_ioctl
,
2179 .compat_ioctl
= fuse_file_compat_ioctl
,
2180 .poll
= fuse_file_poll
,
2183 static const struct file_operations fuse_direct_io_file_operations
= {
2184 .llseek
= fuse_file_llseek
,
2185 .read
= fuse_direct_read
,
2186 .write
= fuse_direct_write
,
2187 .mmap
= fuse_direct_mmap
,
2189 .flush
= fuse_flush
,
2190 .release
= fuse_release
,
2191 .fsync
= fuse_fsync
,
2192 .lock
= fuse_file_lock
,
2193 .flock
= fuse_file_flock
,
2194 .unlocked_ioctl
= fuse_file_ioctl
,
2195 .compat_ioctl
= fuse_file_compat_ioctl
,
2196 .poll
= fuse_file_poll
,
2197 /* no splice_read */
2200 static const struct address_space_operations fuse_file_aops
= {
2201 .readpage
= fuse_readpage
,
2202 .writepage
= fuse_writepage
,
2203 .launder_page
= fuse_launder_page
,
2204 .write_begin
= fuse_write_begin
,
2205 .write_end
= fuse_write_end
,
2206 .readpages
= fuse_readpages
,
2207 .set_page_dirty
= __set_page_dirty_nobuffers
,
2211 void fuse_init_file_inode(struct inode
*inode
)
2213 inode
->i_fop
= &fuse_file_operations
;
2214 inode
->i_data
.a_ops
= &fuse_file_aops
;