2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU GPL.
11 #include <linux/pagemap.h>
12 #include <linux/slab.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
15 #include <linux/module.h>
16 #include <linux/compat.h>
17 #include <linux/swap.h>
19 static const struct file_operations fuse_direct_io_file_operations
;
21 static int fuse_send_open(struct fuse_conn
*fc
, u64 nodeid
, struct file
*file
,
22 int opcode
, struct fuse_open_out
*outargp
)
24 struct fuse_open_in inarg
;
28 req
= fuse_get_req_nopages(fc
);
32 memset(&inarg
, 0, sizeof(inarg
));
33 inarg
.flags
= file
->f_flags
& ~(O_CREAT
| O_EXCL
| O_NOCTTY
);
34 if (!fc
->atomic_o_trunc
)
35 inarg
.flags
&= ~O_TRUNC
;
36 req
->in
.h
.opcode
= opcode
;
37 req
->in
.h
.nodeid
= nodeid
;
39 req
->in
.args
[0].size
= sizeof(inarg
);
40 req
->in
.args
[0].value
= &inarg
;
42 req
->out
.args
[0].size
= sizeof(*outargp
);
43 req
->out
.args
[0].value
= outargp
;
44 fuse_request_send(fc
, req
);
45 err
= req
->out
.h
.error
;
46 fuse_put_request(fc
, req
);
51 struct fuse_file
*fuse_file_alloc(struct fuse_conn
*fc
)
55 ff
= kmalloc(sizeof(struct fuse_file
), GFP_KERNEL
);
60 ff
->reserved_req
= fuse_request_alloc(0);
61 if (unlikely(!ff
->reserved_req
)) {
66 INIT_LIST_HEAD(&ff
->write_entry
);
67 atomic_set(&ff
->count
, 0);
68 RB_CLEAR_NODE(&ff
->polled_node
);
69 init_waitqueue_head(&ff
->poll_wait
);
73 spin_unlock(&fc
->lock
);
78 void fuse_file_free(struct fuse_file
*ff
)
80 fuse_request_free(ff
->reserved_req
);
84 struct fuse_file
*fuse_file_get(struct fuse_file
*ff
)
86 atomic_inc(&ff
->count
);
90 static void fuse_release_async(struct work_struct
*work
)
96 req
= container_of(work
, struct fuse_req
, misc
.release
.work
);
97 path
= req
->misc
.release
.path
;
98 fc
= get_fuse_conn(path
.dentry
->d_inode
);
100 fuse_put_request(fc
, req
);
104 static void fuse_release_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
106 if (fc
->destroy_req
) {
108 * If this is a fuseblk mount, then it's possible that
109 * releasing the path will result in releasing the
110 * super block and sending the DESTROY request. If
111 * the server is single threaded, this would hang.
112 * For this reason do the path_put() in a separate
115 atomic_inc(&req
->count
);
116 INIT_WORK(&req
->misc
.release
.work
, fuse_release_async
);
117 schedule_work(&req
->misc
.release
.work
);
119 path_put(&req
->misc
.release
.path
);
123 static void fuse_file_put(struct fuse_file
*ff
, bool sync
)
125 if (atomic_dec_and_test(&ff
->count
)) {
126 struct fuse_req
*req
= ff
->reserved_req
;
130 fuse_request_send(ff
->fc
, req
);
131 path_put(&req
->misc
.release
.path
);
132 fuse_put_request(ff
->fc
, req
);
134 req
->end
= fuse_release_end
;
136 fuse_request_send_background(ff
->fc
, req
);
142 int fuse_do_open(struct fuse_conn
*fc
, u64 nodeid
, struct file
*file
,
145 struct fuse_open_out outarg
;
146 struct fuse_file
*ff
;
148 int opcode
= isdir
? FUSE_OPENDIR
: FUSE_OPEN
;
150 ff
= fuse_file_alloc(fc
);
154 err
= fuse_send_open(fc
, nodeid
, file
, opcode
, &outarg
);
161 outarg
.open_flags
&= ~FOPEN_DIRECT_IO
;
165 ff
->open_flags
= outarg
.open_flags
;
166 file
->private_data
= fuse_file_get(ff
);
170 EXPORT_SYMBOL_GPL(fuse_do_open
);
172 void fuse_finish_open(struct inode
*inode
, struct file
*file
)
174 struct fuse_file
*ff
= file
->private_data
;
175 struct fuse_conn
*fc
= get_fuse_conn(inode
);
177 if (ff
->open_flags
& FOPEN_DIRECT_IO
)
178 file
->f_op
= &fuse_direct_io_file_operations
;
179 if (!(ff
->open_flags
& FOPEN_KEEP_CACHE
))
180 invalidate_inode_pages2(inode
->i_mapping
);
181 if (ff
->open_flags
& FOPEN_NONSEEKABLE
)
182 nonseekable_open(inode
, file
);
183 if (fc
->atomic_o_trunc
&& (file
->f_flags
& O_TRUNC
)) {
184 struct fuse_inode
*fi
= get_fuse_inode(inode
);
186 spin_lock(&fc
->lock
);
187 fi
->attr_version
= ++fc
->attr_version
;
188 i_size_write(inode
, 0);
189 spin_unlock(&fc
->lock
);
190 fuse_invalidate_attr(inode
);
194 int fuse_open_common(struct inode
*inode
, struct file
*file
, bool isdir
)
196 struct fuse_conn
*fc
= get_fuse_conn(inode
);
199 err
= generic_file_open(inode
, file
);
203 err
= fuse_do_open(fc
, get_node_id(inode
), file
, isdir
);
207 fuse_finish_open(inode
, file
);
212 static void fuse_prepare_release(struct fuse_file
*ff
, int flags
, int opcode
)
214 struct fuse_conn
*fc
= ff
->fc
;
215 struct fuse_req
*req
= ff
->reserved_req
;
216 struct fuse_release_in
*inarg
= &req
->misc
.release
.in
;
218 spin_lock(&fc
->lock
);
219 list_del(&ff
->write_entry
);
220 if (!RB_EMPTY_NODE(&ff
->polled_node
))
221 rb_erase(&ff
->polled_node
, &fc
->polled_files
);
222 spin_unlock(&fc
->lock
);
224 wake_up_interruptible_all(&ff
->poll_wait
);
227 inarg
->flags
= flags
;
228 req
->in
.h
.opcode
= opcode
;
229 req
->in
.h
.nodeid
= ff
->nodeid
;
231 req
->in
.args
[0].size
= sizeof(struct fuse_release_in
);
232 req
->in
.args
[0].value
= inarg
;
235 void fuse_release_common(struct file
*file
, int opcode
)
237 struct fuse_file
*ff
;
238 struct fuse_req
*req
;
240 ff
= file
->private_data
;
244 req
= ff
->reserved_req
;
245 fuse_prepare_release(ff
, file
->f_flags
, opcode
);
248 struct fuse_release_in
*inarg
= &req
->misc
.release
.in
;
249 inarg
->release_flags
|= FUSE_RELEASE_FLOCK_UNLOCK
;
250 inarg
->lock_owner
= fuse_lock_owner_id(ff
->fc
,
253 /* Hold vfsmount and dentry until release is finished */
254 path_get(&file
->f_path
);
255 req
->misc
.release
.path
= file
->f_path
;
258 * Normally this will send the RELEASE request, however if
259 * some asynchronous READ or WRITE requests are outstanding,
260 * the sending will be delayed.
262 * Make the release synchronous if this is a fuseblk mount,
263 * synchronous RELEASE is allowed (and desirable) in this case
264 * because the server can be trusted not to screw up.
266 fuse_file_put(ff
, ff
->fc
->destroy_req
!= NULL
);
269 static int fuse_open(struct inode
*inode
, struct file
*file
)
271 return fuse_open_common(inode
, file
, false);
274 static int fuse_release(struct inode
*inode
, struct file
*file
)
276 fuse_release_common(file
, FUSE_RELEASE
);
278 /* return value is ignored by VFS */
282 void fuse_sync_release(struct fuse_file
*ff
, int flags
)
284 WARN_ON(atomic_read(&ff
->count
) > 1);
285 fuse_prepare_release(ff
, flags
, FUSE_RELEASE
);
286 ff
->reserved_req
->force
= 1;
287 ff
->reserved_req
->background
= 0;
288 fuse_request_send(ff
->fc
, ff
->reserved_req
);
289 fuse_put_request(ff
->fc
, ff
->reserved_req
);
292 EXPORT_SYMBOL_GPL(fuse_sync_release
);
295 * Scramble the ID space with XTEA, so that the value of the files_struct
296 * pointer is not exposed to userspace.
298 u64
fuse_lock_owner_id(struct fuse_conn
*fc
, fl_owner_t id
)
300 u32
*k
= fc
->scramble_key
;
301 u64 v
= (unsigned long) id
;
307 for (i
= 0; i
< 32; i
++) {
308 v0
+= ((v1
<< 4 ^ v1
>> 5) + v1
) ^ (sum
+ k
[sum
& 3]);
310 v1
+= ((v0
<< 4 ^ v0
>> 5) + v0
) ^ (sum
+ k
[sum
>>11 & 3]);
313 return (u64
) v0
+ ((u64
) v1
<< 32);
317 * Check if page is under writeback
319 * This is currently done by walking the list of writepage requests
320 * for the inode, which can be pretty inefficient.
322 static bool fuse_page_is_writeback(struct inode
*inode
, pgoff_t index
)
324 struct fuse_conn
*fc
= get_fuse_conn(inode
);
325 struct fuse_inode
*fi
= get_fuse_inode(inode
);
326 struct fuse_req
*req
;
329 spin_lock(&fc
->lock
);
330 list_for_each_entry(req
, &fi
->writepages
, writepages_entry
) {
333 BUG_ON(req
->inode
!= inode
);
334 curr_index
= req
->misc
.write
.in
.offset
>> PAGE_CACHE_SHIFT
;
335 if (curr_index
== index
) {
340 spin_unlock(&fc
->lock
);
346 * Wait for page writeback to be completed.
348 * Since fuse doesn't rely on the VM writeback tracking, this has to
349 * use some other means.
351 static int fuse_wait_on_page_writeback(struct inode
*inode
, pgoff_t index
)
353 struct fuse_inode
*fi
= get_fuse_inode(inode
);
355 wait_event(fi
->page_waitq
, !fuse_page_is_writeback(inode
, index
));
359 static int fuse_flush(struct file
*file
, fl_owner_t id
)
361 struct inode
*inode
= file_inode(file
);
362 struct fuse_conn
*fc
= get_fuse_conn(inode
);
363 struct fuse_file
*ff
= file
->private_data
;
364 struct fuse_req
*req
;
365 struct fuse_flush_in inarg
;
368 if (is_bad_inode(inode
))
374 req
= fuse_get_req_nofail_nopages(fc
, file
);
375 memset(&inarg
, 0, sizeof(inarg
));
377 inarg
.lock_owner
= fuse_lock_owner_id(fc
, id
);
378 req
->in
.h
.opcode
= FUSE_FLUSH
;
379 req
->in
.h
.nodeid
= get_node_id(inode
);
381 req
->in
.args
[0].size
= sizeof(inarg
);
382 req
->in
.args
[0].value
= &inarg
;
384 fuse_request_send(fc
, req
);
385 err
= req
->out
.h
.error
;
386 fuse_put_request(fc
, req
);
387 if (err
== -ENOSYS
) {
395 * Wait for all pending writepages on the inode to finish.
397 * This is currently done by blocking further writes with FUSE_NOWRITE
398 * and waiting for all sent writes to complete.
400 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
401 * could conflict with truncation.
403 static void fuse_sync_writes(struct inode
*inode
)
405 fuse_set_nowrite(inode
);
406 fuse_release_nowrite(inode
);
409 int fuse_fsync_common(struct file
*file
, loff_t start
, loff_t end
,
410 int datasync
, int isdir
)
412 struct inode
*inode
= file
->f_mapping
->host
;
413 struct fuse_conn
*fc
= get_fuse_conn(inode
);
414 struct fuse_file
*ff
= file
->private_data
;
415 struct fuse_req
*req
;
416 struct fuse_fsync_in inarg
;
419 if (is_bad_inode(inode
))
422 err
= filemap_write_and_wait_range(inode
->i_mapping
, start
, end
);
426 if ((!isdir
&& fc
->no_fsync
) || (isdir
&& fc
->no_fsyncdir
))
429 mutex_lock(&inode
->i_mutex
);
432 * Start writeback against all dirty pages of the inode, then
433 * wait for all outstanding writes, before sending the FSYNC
436 err
= write_inode_now(inode
, 0);
440 fuse_sync_writes(inode
);
442 req
= fuse_get_req_nopages(fc
);
448 memset(&inarg
, 0, sizeof(inarg
));
450 inarg
.fsync_flags
= datasync
? 1 : 0;
451 req
->in
.h
.opcode
= isdir
? FUSE_FSYNCDIR
: FUSE_FSYNC
;
452 req
->in
.h
.nodeid
= get_node_id(inode
);
454 req
->in
.args
[0].size
= sizeof(inarg
);
455 req
->in
.args
[0].value
= &inarg
;
456 fuse_request_send(fc
, req
);
457 err
= req
->out
.h
.error
;
458 fuse_put_request(fc
, req
);
459 if (err
== -ENOSYS
) {
467 mutex_unlock(&inode
->i_mutex
);
471 static int fuse_fsync(struct file
*file
, loff_t start
, loff_t end
,
474 return fuse_fsync_common(file
, start
, end
, datasync
, 0);
477 void fuse_read_fill(struct fuse_req
*req
, struct file
*file
, loff_t pos
,
478 size_t count
, int opcode
)
480 struct fuse_read_in
*inarg
= &req
->misc
.read
.in
;
481 struct fuse_file
*ff
= file
->private_data
;
486 inarg
->flags
= file
->f_flags
;
487 req
->in
.h
.opcode
= opcode
;
488 req
->in
.h
.nodeid
= ff
->nodeid
;
490 req
->in
.args
[0].size
= sizeof(struct fuse_read_in
);
491 req
->in
.args
[0].value
= inarg
;
493 req
->out
.numargs
= 1;
494 req
->out
.args
[0].size
= count
;
497 static void fuse_release_user_pages(struct fuse_req
*req
, int write
)
501 for (i
= 0; i
< req
->num_pages
; i
++) {
502 struct page
*page
= req
->pages
[i
];
504 set_page_dirty_lock(page
);
509 static size_t fuse_send_read(struct fuse_req
*req
, struct file
*file
,
510 loff_t pos
, size_t count
, fl_owner_t owner
)
512 struct fuse_file
*ff
= file
->private_data
;
513 struct fuse_conn
*fc
= ff
->fc
;
515 fuse_read_fill(req
, file
, pos
, count
, FUSE_READ
);
517 struct fuse_read_in
*inarg
= &req
->misc
.read
.in
;
519 inarg
->read_flags
|= FUSE_READ_LOCKOWNER
;
520 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
522 fuse_request_send(fc
, req
);
523 return req
->out
.args
[0].size
;
526 static void fuse_read_update_size(struct inode
*inode
, loff_t size
,
529 struct fuse_conn
*fc
= get_fuse_conn(inode
);
530 struct fuse_inode
*fi
= get_fuse_inode(inode
);
532 spin_lock(&fc
->lock
);
533 if (attr_ver
== fi
->attr_version
&& size
< inode
->i_size
) {
534 fi
->attr_version
= ++fc
->attr_version
;
535 i_size_write(inode
, size
);
537 spin_unlock(&fc
->lock
);
540 static int fuse_readpage(struct file
*file
, struct page
*page
)
542 struct inode
*inode
= page
->mapping
->host
;
543 struct fuse_conn
*fc
= get_fuse_conn(inode
);
544 struct fuse_req
*req
;
546 loff_t pos
= page_offset(page
);
547 size_t count
= PAGE_CACHE_SIZE
;
552 if (is_bad_inode(inode
))
556 * Page writeback can extend beyond the lifetime of the
557 * page-cache page, so make sure we read a properly synced
560 fuse_wait_on_page_writeback(inode
, page
->index
);
562 req
= fuse_get_req(fc
, 1);
567 attr_ver
= fuse_get_attr_version(fc
);
569 req
->out
.page_zeroing
= 1;
570 req
->out
.argpages
= 1;
572 req
->pages
[0] = page
;
573 req
->page_descs
[0].length
= count
;
574 num_read
= fuse_send_read(req
, file
, pos
, count
, NULL
);
575 err
= req
->out
.h
.error
;
576 fuse_put_request(fc
, req
);
580 * Short read means EOF. If file size is larger, truncate it
582 if (num_read
< count
)
583 fuse_read_update_size(inode
, pos
+ num_read
, attr_ver
);
585 SetPageUptodate(page
);
588 fuse_invalidate_attr(inode
); /* atime changed */
594 static void fuse_readpages_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
597 size_t count
= req
->misc
.read
.in
.size
;
598 size_t num_read
= req
->out
.args
[0].size
;
599 struct address_space
*mapping
= NULL
;
601 for (i
= 0; mapping
== NULL
&& i
< req
->num_pages
; i
++)
602 mapping
= req
->pages
[i
]->mapping
;
605 struct inode
*inode
= mapping
->host
;
608 * Short read means EOF. If file size is larger, truncate it
610 if (!req
->out
.h
.error
&& num_read
< count
) {
613 pos
= page_offset(req
->pages
[0]) + num_read
;
614 fuse_read_update_size(inode
, pos
,
615 req
->misc
.read
.attr_ver
);
617 fuse_invalidate_attr(inode
); /* atime changed */
620 for (i
= 0; i
< req
->num_pages
; i
++) {
621 struct page
*page
= req
->pages
[i
];
622 if (!req
->out
.h
.error
)
623 SetPageUptodate(page
);
627 page_cache_release(page
);
630 fuse_file_put(req
->ff
, false);
633 static void fuse_send_readpages(struct fuse_req
*req
, struct file
*file
)
635 struct fuse_file
*ff
= file
->private_data
;
636 struct fuse_conn
*fc
= ff
->fc
;
637 loff_t pos
= page_offset(req
->pages
[0]);
638 size_t count
= req
->num_pages
<< PAGE_CACHE_SHIFT
;
640 req
->out
.argpages
= 1;
641 req
->out
.page_zeroing
= 1;
642 req
->out
.page_replace
= 1;
643 fuse_read_fill(req
, file
, pos
, count
, FUSE_READ
);
644 req
->misc
.read
.attr_ver
= fuse_get_attr_version(fc
);
645 if (fc
->async_read
) {
646 req
->ff
= fuse_file_get(ff
);
647 req
->end
= fuse_readpages_end
;
648 fuse_request_send_background(fc
, req
);
650 fuse_request_send(fc
, req
);
651 fuse_readpages_end(fc
, req
);
652 fuse_put_request(fc
, req
);
656 struct fuse_fill_data
{
657 struct fuse_req
*req
;
663 static int fuse_readpages_fill(void *_data
, struct page
*page
)
665 struct fuse_fill_data
*data
= _data
;
666 struct fuse_req
*req
= data
->req
;
667 struct inode
*inode
= data
->inode
;
668 struct fuse_conn
*fc
= get_fuse_conn(inode
);
670 fuse_wait_on_page_writeback(inode
, page
->index
);
672 if (req
->num_pages
&&
673 (req
->num_pages
== FUSE_MAX_PAGES_PER_REQ
||
674 (req
->num_pages
+ 1) * PAGE_CACHE_SIZE
> fc
->max_read
||
675 req
->pages
[req
->num_pages
- 1]->index
+ 1 != page
->index
)) {
676 int nr_alloc
= min_t(unsigned, data
->nr_pages
,
677 FUSE_MAX_PAGES_PER_REQ
);
678 fuse_send_readpages(req
, data
->file
);
680 req
= fuse_get_req_for_background(fc
, nr_alloc
);
682 req
= fuse_get_req(fc
, nr_alloc
);
691 if (WARN_ON(req
->num_pages
>= req
->max_pages
)) {
692 fuse_put_request(fc
, req
);
696 page_cache_get(page
);
697 req
->pages
[req
->num_pages
] = page
;
698 req
->page_descs
[req
->num_pages
].length
= PAGE_SIZE
;
704 static int fuse_readpages(struct file
*file
, struct address_space
*mapping
,
705 struct list_head
*pages
, unsigned nr_pages
)
707 struct inode
*inode
= mapping
->host
;
708 struct fuse_conn
*fc
= get_fuse_conn(inode
);
709 struct fuse_fill_data data
;
711 int nr_alloc
= min_t(unsigned, nr_pages
, FUSE_MAX_PAGES_PER_REQ
);
714 if (is_bad_inode(inode
))
720 data
.req
= fuse_get_req_for_background(fc
, nr_alloc
);
722 data
.req
= fuse_get_req(fc
, nr_alloc
);
723 data
.nr_pages
= nr_pages
;
724 err
= PTR_ERR(data
.req
);
725 if (IS_ERR(data
.req
))
728 err
= read_cache_pages(mapping
, pages
, fuse_readpages_fill
, &data
);
730 if (data
.req
->num_pages
)
731 fuse_send_readpages(data
.req
, file
);
733 fuse_put_request(fc
, data
.req
);
739 static ssize_t
fuse_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
740 unsigned long nr_segs
, loff_t pos
)
742 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
743 struct fuse_conn
*fc
= get_fuse_conn(inode
);
746 * In auto invalidate mode, always update attributes on read.
747 * Otherwise, only update if we attempt to read past EOF (to ensure
748 * i_size is up to date).
750 if (fc
->auto_inval_data
||
751 (pos
+ iov_length(iov
, nr_segs
) > i_size_read(inode
))) {
753 err
= fuse_update_attributes(inode
, NULL
, iocb
->ki_filp
, NULL
);
758 return generic_file_aio_read(iocb
, iov
, nr_segs
, pos
);
761 static void fuse_write_fill(struct fuse_req
*req
, struct fuse_file
*ff
,
762 loff_t pos
, size_t count
)
764 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
765 struct fuse_write_out
*outarg
= &req
->misc
.write
.out
;
770 req
->in
.h
.opcode
= FUSE_WRITE
;
771 req
->in
.h
.nodeid
= ff
->nodeid
;
773 if (ff
->fc
->minor
< 9)
774 req
->in
.args
[0].size
= FUSE_COMPAT_WRITE_IN_SIZE
;
776 req
->in
.args
[0].size
= sizeof(struct fuse_write_in
);
777 req
->in
.args
[0].value
= inarg
;
778 req
->in
.args
[1].size
= count
;
779 req
->out
.numargs
= 1;
780 req
->out
.args
[0].size
= sizeof(struct fuse_write_out
);
781 req
->out
.args
[0].value
= outarg
;
784 static size_t fuse_send_write(struct fuse_req
*req
, struct file
*file
,
785 loff_t pos
, size_t count
, fl_owner_t owner
)
787 struct fuse_file
*ff
= file
->private_data
;
788 struct fuse_conn
*fc
= ff
->fc
;
789 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
791 fuse_write_fill(req
, ff
, pos
, count
);
792 inarg
->flags
= file
->f_flags
;
794 inarg
->write_flags
|= FUSE_WRITE_LOCKOWNER
;
795 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
797 fuse_request_send(fc
, req
);
798 return req
->misc
.write
.out
.size
;
801 void fuse_write_update_size(struct inode
*inode
, loff_t pos
)
803 struct fuse_conn
*fc
= get_fuse_conn(inode
);
804 struct fuse_inode
*fi
= get_fuse_inode(inode
);
806 spin_lock(&fc
->lock
);
807 fi
->attr_version
= ++fc
->attr_version
;
808 if (pos
> inode
->i_size
)
809 i_size_write(inode
, pos
);
810 spin_unlock(&fc
->lock
);
813 static size_t fuse_send_write_pages(struct fuse_req
*req
, struct file
*file
,
814 struct inode
*inode
, loff_t pos
,
821 for (i
= 0; i
< req
->num_pages
; i
++)
822 fuse_wait_on_page_writeback(inode
, req
->pages
[i
]->index
);
824 res
= fuse_send_write(req
, file
, pos
, count
, NULL
);
826 offset
= req
->page_descs
[0].offset
;
828 for (i
= 0; i
< req
->num_pages
; i
++) {
829 struct page
*page
= req
->pages
[i
];
831 if (!req
->out
.h
.error
&& !offset
&& count
>= PAGE_CACHE_SIZE
)
832 SetPageUptodate(page
);
834 if (count
> PAGE_CACHE_SIZE
- offset
)
835 count
-= PAGE_CACHE_SIZE
- offset
;
841 page_cache_release(page
);
847 static ssize_t
fuse_fill_write_pages(struct fuse_req
*req
,
848 struct address_space
*mapping
,
849 struct iov_iter
*ii
, loff_t pos
)
851 struct fuse_conn
*fc
= get_fuse_conn(mapping
->host
);
852 unsigned offset
= pos
& (PAGE_CACHE_SIZE
- 1);
856 req
->in
.argpages
= 1;
857 req
->page_descs
[0].offset
= offset
;
862 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
863 size_t bytes
= min_t(size_t, PAGE_CACHE_SIZE
- offset
,
866 bytes
= min_t(size_t, bytes
, fc
->max_write
- count
);
870 if (iov_iter_fault_in_readable(ii
, bytes
))
874 page
= grab_cache_page_write_begin(mapping
, index
, 0);
878 if (mapping_writably_mapped(mapping
))
879 flush_dcache_page(page
);
882 tmp
= iov_iter_copy_from_user_atomic(page
, ii
, offset
, bytes
);
884 flush_dcache_page(page
);
886 mark_page_accessed(page
);
890 page_cache_release(page
);
891 bytes
= min(bytes
, iov_iter_single_seg_count(ii
));
896 req
->pages
[req
->num_pages
] = page
;
897 req
->page_descs
[req
->num_pages
].length
= tmp
;
900 iov_iter_advance(ii
, tmp
);
904 if (offset
== PAGE_CACHE_SIZE
)
909 } while (iov_iter_count(ii
) && count
< fc
->max_write
&&
910 req
->num_pages
< req
->max_pages
&& offset
== 0);
912 return count
> 0 ? count
: err
;
915 static inline unsigned fuse_wr_pages(loff_t pos
, size_t len
)
917 return min_t(unsigned,
918 ((pos
+ len
- 1) >> PAGE_CACHE_SHIFT
) -
919 (pos
>> PAGE_CACHE_SHIFT
) + 1,
920 FUSE_MAX_PAGES_PER_REQ
);
923 static ssize_t
fuse_perform_write(struct file
*file
,
924 struct address_space
*mapping
,
925 struct iov_iter
*ii
, loff_t pos
)
927 struct inode
*inode
= mapping
->host
;
928 struct fuse_conn
*fc
= get_fuse_conn(inode
);
932 if (is_bad_inode(inode
))
936 struct fuse_req
*req
;
938 unsigned nr_pages
= fuse_wr_pages(pos
, iov_iter_count(ii
));
940 req
= fuse_get_req(fc
, nr_pages
);
946 count
= fuse_fill_write_pages(req
, mapping
, ii
, pos
);
952 num_written
= fuse_send_write_pages(req
, file
, inode
,
954 err
= req
->out
.h
.error
;
959 /* break out of the loop on short write */
960 if (num_written
!= count
)
964 fuse_put_request(fc
, req
);
965 } while (!err
&& iov_iter_count(ii
));
968 fuse_write_update_size(inode
, pos
);
970 fuse_invalidate_attr(inode
);
972 return res
> 0 ? res
: err
;
975 static ssize_t
fuse_file_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
976 unsigned long nr_segs
, loff_t pos
)
978 struct file
*file
= iocb
->ki_filp
;
979 struct address_space
*mapping
= file
->f_mapping
;
983 ssize_t written_buffered
= 0;
984 struct inode
*inode
= mapping
->host
;
989 WARN_ON(iocb
->ki_pos
!= pos
);
992 err
= generic_segment_checks(iov
, &nr_segs
, &ocount
, VERIFY_READ
);
997 sb_start_write(inode
->i_sb
);
998 mutex_lock(&inode
->i_mutex
);
1000 /* We can write back this queue in page reclaim */
1001 current
->backing_dev_info
= mapping
->backing_dev_info
;
1003 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
1010 err
= file_remove_suid(file
);
1014 err
= file_update_time(file
);
1018 if (file
->f_flags
& O_DIRECT
) {
1019 written
= generic_file_direct_write(iocb
, iov
, &nr_segs
,
1022 if (written
< 0 || written
== count
)
1028 iov_iter_init(&i
, iov
, nr_segs
, count
, written
);
1029 written_buffered
= fuse_perform_write(file
, mapping
, &i
, pos
);
1030 if (written_buffered
< 0) {
1031 err
= written_buffered
;
1034 endbyte
= pos
+ written_buffered
- 1;
1036 err
= filemap_write_and_wait_range(file
->f_mapping
, pos
,
1041 invalidate_mapping_pages(file
->f_mapping
,
1042 pos
>> PAGE_CACHE_SHIFT
,
1043 endbyte
>> PAGE_CACHE_SHIFT
);
1045 written
+= written_buffered
;
1046 iocb
->ki_pos
= pos
+ written_buffered
;
1048 iov_iter_init(&i
, iov
, nr_segs
, count
, 0);
1049 written
= fuse_perform_write(file
, mapping
, &i
, pos
);
1051 iocb
->ki_pos
= pos
+ written
;
1054 current
->backing_dev_info
= NULL
;
1055 mutex_unlock(&inode
->i_mutex
);
1056 sb_end_write(inode
->i_sb
);
1058 return written
? written
: err
;
1061 static inline void fuse_page_descs_length_init(struct fuse_req
*req
,
1062 unsigned index
, unsigned nr_pages
)
1066 for (i
= index
; i
< index
+ nr_pages
; i
++)
1067 req
->page_descs
[i
].length
= PAGE_SIZE
-
1068 req
->page_descs
[i
].offset
;
1071 static inline unsigned long fuse_get_user_addr(const struct iov_iter
*ii
)
1073 return (unsigned long)ii
->iov
->iov_base
+ ii
->iov_offset
;
1076 static inline size_t fuse_get_frag_size(const struct iov_iter
*ii
,
1079 return min(iov_iter_single_seg_count(ii
), max_size
);
1082 static int fuse_get_user_pages(struct fuse_req
*req
, struct iov_iter
*ii
,
1083 size_t *nbytesp
, int write
)
1085 size_t nbytes
= 0; /* # bytes already packed in req */
1087 /* Special case for kernel I/O: can copy directly into the buffer */
1088 if (segment_eq(get_fs(), KERNEL_DS
)) {
1089 unsigned long user_addr
= fuse_get_user_addr(ii
);
1090 size_t frag_size
= fuse_get_frag_size(ii
, *nbytesp
);
1093 req
->in
.args
[1].value
= (void *) user_addr
;
1095 req
->out
.args
[0].value
= (void *) user_addr
;
1097 iov_iter_advance(ii
, frag_size
);
1098 *nbytesp
= frag_size
;
1102 while (nbytes
< *nbytesp
&& req
->num_pages
< req
->max_pages
) {
1104 unsigned long user_addr
= fuse_get_user_addr(ii
);
1105 unsigned offset
= user_addr
& ~PAGE_MASK
;
1106 size_t frag_size
= fuse_get_frag_size(ii
, *nbytesp
- nbytes
);
1109 unsigned n
= req
->max_pages
- req
->num_pages
;
1110 frag_size
= min_t(size_t, frag_size
, n
<< PAGE_SHIFT
);
1112 npages
= (frag_size
+ offset
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1113 npages
= clamp(npages
, 1U, n
);
1115 ret
= get_user_pages_fast(user_addr
, npages
, !write
,
1116 &req
->pages
[req
->num_pages
]);
1121 frag_size
= min_t(size_t, frag_size
,
1122 (npages
<< PAGE_SHIFT
) - offset
);
1123 iov_iter_advance(ii
, frag_size
);
1125 req
->page_descs
[req
->num_pages
].offset
= offset
;
1126 fuse_page_descs_length_init(req
, req
->num_pages
, npages
);
1128 req
->num_pages
+= npages
;
1129 req
->page_descs
[req
->num_pages
- 1].length
-=
1130 (npages
<< PAGE_SHIFT
) - offset
- frag_size
;
1132 nbytes
+= frag_size
;
1136 req
->in
.argpages
= 1;
1138 req
->out
.argpages
= 1;
1145 static inline int fuse_iter_npages(const struct iov_iter
*ii_p
)
1147 struct iov_iter ii
= *ii_p
;
1150 while (iov_iter_count(&ii
) && npages
< FUSE_MAX_PAGES_PER_REQ
) {
1151 unsigned long user_addr
= fuse_get_user_addr(&ii
);
1152 unsigned offset
= user_addr
& ~PAGE_MASK
;
1153 size_t frag_size
= iov_iter_single_seg_count(&ii
);
1155 npages
+= (frag_size
+ offset
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1156 iov_iter_advance(&ii
, frag_size
);
1159 return min(npages
, FUSE_MAX_PAGES_PER_REQ
);
1162 ssize_t
fuse_direct_io(struct file
*file
, const struct iovec
*iov
,
1163 unsigned long nr_segs
, size_t count
, loff_t
*ppos
,
1166 struct fuse_file
*ff
= file
->private_data
;
1167 struct fuse_conn
*fc
= ff
->fc
;
1168 size_t nmax
= write
? fc
->max_write
: fc
->max_read
;
1171 struct fuse_req
*req
;
1174 iov_iter_init(&ii
, iov
, nr_segs
, count
, 0);
1176 req
= fuse_get_req(fc
, fuse_iter_npages(&ii
));
1178 return PTR_ERR(req
);
1182 fl_owner_t owner
= current
->files
;
1183 size_t nbytes
= min(count
, nmax
);
1184 int err
= fuse_get_user_pages(req
, &ii
, &nbytes
, write
);
1191 nres
= fuse_send_write(req
, file
, pos
, nbytes
, owner
);
1193 nres
= fuse_send_read(req
, file
, pos
, nbytes
, owner
);
1195 fuse_release_user_pages(req
, !write
);
1196 if (req
->out
.h
.error
) {
1198 res
= req
->out
.h
.error
;
1200 } else if (nres
> nbytes
) {
1210 fuse_put_request(fc
, req
);
1211 req
= fuse_get_req(fc
, fuse_iter_npages(&ii
));
1217 fuse_put_request(fc
, req
);
1223 EXPORT_SYMBOL_GPL(fuse_direct_io
);
1225 static ssize_t
__fuse_direct_read(struct file
*file
, const struct iovec
*iov
,
1226 unsigned long nr_segs
, loff_t
*ppos
)
1229 struct inode
*inode
= file_inode(file
);
1231 if (is_bad_inode(inode
))
1234 res
= fuse_direct_io(file
, iov
, nr_segs
, iov_length(iov
, nr_segs
),
1237 fuse_invalidate_attr(inode
);
1242 static ssize_t
fuse_direct_read(struct file
*file
, char __user
*buf
,
1243 size_t count
, loff_t
*ppos
)
1245 struct iovec iov
= { .iov_base
= buf
, .iov_len
= count
};
1246 return __fuse_direct_read(file
, &iov
, 1, ppos
);
1249 static ssize_t
__fuse_direct_write(struct file
*file
, const struct iovec
*iov
,
1250 unsigned long nr_segs
, loff_t
*ppos
)
1252 struct inode
*inode
= file_inode(file
);
1253 size_t count
= iov_length(iov
, nr_segs
);
1256 res
= generic_write_checks(file
, ppos
, &count
, 0);
1258 res
= fuse_direct_io(file
, iov
, nr_segs
, count
, ppos
, 1);
1260 fuse_write_update_size(inode
, *ppos
);
1263 fuse_invalidate_attr(inode
);
1268 static ssize_t
fuse_direct_write(struct file
*file
, const char __user
*buf
,
1269 size_t count
, loff_t
*ppos
)
1271 struct iovec iov
= { .iov_base
= (void __user
*)buf
, .iov_len
= count
};
1272 struct inode
*inode
= file_inode(file
);
1275 if (is_bad_inode(inode
))
1278 /* Don't allow parallel writes to the same file */
1279 mutex_lock(&inode
->i_mutex
);
1280 res
= __fuse_direct_write(file
, &iov
, 1, ppos
);
1281 mutex_unlock(&inode
->i_mutex
);
1286 static void fuse_writepage_free(struct fuse_conn
*fc
, struct fuse_req
*req
)
1288 __free_page(req
->pages
[0]);
1289 fuse_file_put(req
->ff
, false);
1292 static void fuse_writepage_finish(struct fuse_conn
*fc
, struct fuse_req
*req
)
1294 struct inode
*inode
= req
->inode
;
1295 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1296 struct backing_dev_info
*bdi
= inode
->i_mapping
->backing_dev_info
;
1298 list_del(&req
->writepages_entry
);
1299 dec_bdi_stat(bdi
, BDI_WRITEBACK
);
1300 dec_zone_page_state(req
->pages
[0], NR_WRITEBACK_TEMP
);
1301 bdi_writeout_inc(bdi
);
1302 wake_up(&fi
->page_waitq
);
1305 /* Called under fc->lock, may release and reacquire it */
1306 static void fuse_send_writepage(struct fuse_conn
*fc
, struct fuse_req
*req
)
1307 __releases(fc
->lock
)
1308 __acquires(fc
->lock
)
1310 struct fuse_inode
*fi
= get_fuse_inode(req
->inode
);
1311 loff_t size
= i_size_read(req
->inode
);
1312 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
1317 if (inarg
->offset
+ PAGE_CACHE_SIZE
<= size
) {
1318 inarg
->size
= PAGE_CACHE_SIZE
;
1319 } else if (inarg
->offset
< size
) {
1320 inarg
->size
= size
& (PAGE_CACHE_SIZE
- 1);
1322 /* Got truncated off completely */
1326 req
->in
.args
[1].size
= inarg
->size
;
1328 fuse_request_send_background_locked(fc
, req
);
1332 fuse_writepage_finish(fc
, req
);
1333 spin_unlock(&fc
->lock
);
1334 fuse_writepage_free(fc
, req
);
1335 fuse_put_request(fc
, req
);
1336 spin_lock(&fc
->lock
);
1340 * If fi->writectr is positive (no truncate or fsync going on) send
1341 * all queued writepage requests.
1343 * Called with fc->lock
1345 void fuse_flush_writepages(struct inode
*inode
)
1346 __releases(fc
->lock
)
1347 __acquires(fc
->lock
)
1349 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1350 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1351 struct fuse_req
*req
;
1353 while (fi
->writectr
>= 0 && !list_empty(&fi
->queued_writes
)) {
1354 req
= list_entry(fi
->queued_writes
.next
, struct fuse_req
, list
);
1355 list_del_init(&req
->list
);
1356 fuse_send_writepage(fc
, req
);
1360 static void fuse_writepage_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
1362 struct inode
*inode
= req
->inode
;
1363 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1365 mapping_set_error(inode
->i_mapping
, req
->out
.h
.error
);
1366 spin_lock(&fc
->lock
);
1368 fuse_writepage_finish(fc
, req
);
1369 spin_unlock(&fc
->lock
);
1370 fuse_writepage_free(fc
, req
);
1373 static int fuse_writepage_locked(struct page
*page
)
1375 struct address_space
*mapping
= page
->mapping
;
1376 struct inode
*inode
= mapping
->host
;
1377 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1378 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1379 struct fuse_req
*req
;
1380 struct fuse_file
*ff
;
1381 struct page
*tmp_page
;
1383 set_page_writeback(page
);
1385 req
= fuse_request_alloc_nofs(1);
1389 req
->background
= 1; /* writeback always goes to bg_queue */
1390 tmp_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
1394 spin_lock(&fc
->lock
);
1395 BUG_ON(list_empty(&fi
->write_files
));
1396 ff
= list_entry(fi
->write_files
.next
, struct fuse_file
, write_entry
);
1397 req
->ff
= fuse_file_get(ff
);
1398 spin_unlock(&fc
->lock
);
1400 fuse_write_fill(req
, ff
, page_offset(page
), 0);
1402 copy_highpage(tmp_page
, page
);
1403 req
->misc
.write
.in
.write_flags
|= FUSE_WRITE_CACHE
;
1404 req
->in
.argpages
= 1;
1406 req
->pages
[0] = tmp_page
;
1407 req
->page_descs
[0].offset
= 0;
1408 req
->page_descs
[0].length
= PAGE_SIZE
;
1409 req
->end
= fuse_writepage_end
;
1412 inc_bdi_stat(mapping
->backing_dev_info
, BDI_WRITEBACK
);
1413 inc_zone_page_state(tmp_page
, NR_WRITEBACK_TEMP
);
1414 end_page_writeback(page
);
1416 spin_lock(&fc
->lock
);
1417 list_add(&req
->writepages_entry
, &fi
->writepages
);
1418 list_add_tail(&req
->list
, &fi
->queued_writes
);
1419 fuse_flush_writepages(inode
);
1420 spin_unlock(&fc
->lock
);
1425 fuse_request_free(req
);
1427 end_page_writeback(page
);
1431 static int fuse_writepage(struct page
*page
, struct writeback_control
*wbc
)
1435 err
= fuse_writepage_locked(page
);
1441 static int fuse_launder_page(struct page
*page
)
1444 if (clear_page_dirty_for_io(page
)) {
1445 struct inode
*inode
= page
->mapping
->host
;
1446 err
= fuse_writepage_locked(page
);
1448 fuse_wait_on_page_writeback(inode
, page
->index
);
1454 * Write back dirty pages now, because there may not be any suitable
1457 static void fuse_vma_close(struct vm_area_struct
*vma
)
1459 filemap_write_and_wait(vma
->vm_file
->f_mapping
);
1463 * Wait for writeback against this page to complete before allowing it
1464 * to be marked dirty again, and hence written back again, possibly
1465 * before the previous writepage completed.
1467 * Block here, instead of in ->writepage(), so that the userspace fs
1468 * can only block processes actually operating on the filesystem.
1470 * Otherwise unprivileged userspace fs would be able to block
1475 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
1477 static int fuse_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1479 struct page
*page
= vmf
->page
;
1481 * Don't use page->mapping as it may become NULL from a
1482 * concurrent truncate.
1484 struct inode
*inode
= vma
->vm_file
->f_mapping
->host
;
1486 fuse_wait_on_page_writeback(inode
, page
->index
);
1490 static const struct vm_operations_struct fuse_file_vm_ops
= {
1491 .close
= fuse_vma_close
,
1492 .fault
= filemap_fault
,
1493 .page_mkwrite
= fuse_page_mkwrite
,
1494 .remap_pages
= generic_file_remap_pages
,
1497 static int fuse_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1499 if ((vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
)) {
1500 struct inode
*inode
= file_inode(file
);
1501 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1502 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1503 struct fuse_file
*ff
= file
->private_data
;
1505 * file may be written through mmap, so chain it onto the
1506 * inodes's write_file list
1508 spin_lock(&fc
->lock
);
1509 if (list_empty(&ff
->write_entry
))
1510 list_add(&ff
->write_entry
, &fi
->write_files
);
1511 spin_unlock(&fc
->lock
);
1513 file_accessed(file
);
1514 vma
->vm_ops
= &fuse_file_vm_ops
;
1518 static int fuse_direct_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1520 /* Can't provide the coherency needed for MAP_SHARED */
1521 if (vma
->vm_flags
& VM_MAYSHARE
)
1524 invalidate_inode_pages2(file
->f_mapping
);
1526 return generic_file_mmap(file
, vma
);
1529 static int convert_fuse_file_lock(const struct fuse_file_lock
*ffl
,
1530 struct file_lock
*fl
)
1532 switch (ffl
->type
) {
1538 if (ffl
->start
> OFFSET_MAX
|| ffl
->end
> OFFSET_MAX
||
1539 ffl
->end
< ffl
->start
)
1542 fl
->fl_start
= ffl
->start
;
1543 fl
->fl_end
= ffl
->end
;
1544 fl
->fl_pid
= ffl
->pid
;
1550 fl
->fl_type
= ffl
->type
;
1554 static void fuse_lk_fill(struct fuse_req
*req
, struct file
*file
,
1555 const struct file_lock
*fl
, int opcode
, pid_t pid
,
1558 struct inode
*inode
= file_inode(file
);
1559 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1560 struct fuse_file
*ff
= file
->private_data
;
1561 struct fuse_lk_in
*arg
= &req
->misc
.lk_in
;
1564 arg
->owner
= fuse_lock_owner_id(fc
, fl
->fl_owner
);
1565 arg
->lk
.start
= fl
->fl_start
;
1566 arg
->lk
.end
= fl
->fl_end
;
1567 arg
->lk
.type
= fl
->fl_type
;
1570 arg
->lk_flags
|= FUSE_LK_FLOCK
;
1571 req
->in
.h
.opcode
= opcode
;
1572 req
->in
.h
.nodeid
= get_node_id(inode
);
1573 req
->in
.numargs
= 1;
1574 req
->in
.args
[0].size
= sizeof(*arg
);
1575 req
->in
.args
[0].value
= arg
;
1578 static int fuse_getlk(struct file
*file
, struct file_lock
*fl
)
1580 struct inode
*inode
= file_inode(file
);
1581 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1582 struct fuse_req
*req
;
1583 struct fuse_lk_out outarg
;
1586 req
= fuse_get_req_nopages(fc
);
1588 return PTR_ERR(req
);
1590 fuse_lk_fill(req
, file
, fl
, FUSE_GETLK
, 0, 0);
1591 req
->out
.numargs
= 1;
1592 req
->out
.args
[0].size
= sizeof(outarg
);
1593 req
->out
.args
[0].value
= &outarg
;
1594 fuse_request_send(fc
, req
);
1595 err
= req
->out
.h
.error
;
1596 fuse_put_request(fc
, req
);
1598 err
= convert_fuse_file_lock(&outarg
.lk
, fl
);
1603 static int fuse_setlk(struct file
*file
, struct file_lock
*fl
, int flock
)
1605 struct inode
*inode
= file_inode(file
);
1606 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1607 struct fuse_req
*req
;
1608 int opcode
= (fl
->fl_flags
& FL_SLEEP
) ? FUSE_SETLKW
: FUSE_SETLK
;
1609 pid_t pid
= fl
->fl_type
!= F_UNLCK
? current
->tgid
: 0;
1612 if (fl
->fl_lmops
&& fl
->fl_lmops
->lm_grant
) {
1613 /* NLM needs asynchronous locks, which we don't support yet */
1617 /* Unlock on close is handled by the flush method */
1618 if (fl
->fl_flags
& FL_CLOSE
)
1621 req
= fuse_get_req_nopages(fc
);
1623 return PTR_ERR(req
);
1625 fuse_lk_fill(req
, file
, fl
, opcode
, pid
, flock
);
1626 fuse_request_send(fc
, req
);
1627 err
= req
->out
.h
.error
;
1628 /* locking is restartable */
1631 fuse_put_request(fc
, req
);
1635 static int fuse_file_lock(struct file
*file
, int cmd
, struct file_lock
*fl
)
1637 struct inode
*inode
= file_inode(file
);
1638 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1641 if (cmd
== F_CANCELLK
) {
1643 } else if (cmd
== F_GETLK
) {
1645 posix_test_lock(file
, fl
);
1648 err
= fuse_getlk(file
, fl
);
1651 err
= posix_lock_file(file
, fl
, NULL
);
1653 err
= fuse_setlk(file
, fl
, 0);
1658 static int fuse_file_flock(struct file
*file
, int cmd
, struct file_lock
*fl
)
1660 struct inode
*inode
= file_inode(file
);
1661 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1665 err
= flock_lock_file_wait(file
, fl
);
1667 struct fuse_file
*ff
= file
->private_data
;
1669 /* emulate flock with POSIX locks */
1670 fl
->fl_owner
= (fl_owner_t
) file
;
1672 err
= fuse_setlk(file
, fl
, 1);
1678 static sector_t
fuse_bmap(struct address_space
*mapping
, sector_t block
)
1680 struct inode
*inode
= mapping
->host
;
1681 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1682 struct fuse_req
*req
;
1683 struct fuse_bmap_in inarg
;
1684 struct fuse_bmap_out outarg
;
1687 if (!inode
->i_sb
->s_bdev
|| fc
->no_bmap
)
1690 req
= fuse_get_req_nopages(fc
);
1694 memset(&inarg
, 0, sizeof(inarg
));
1695 inarg
.block
= block
;
1696 inarg
.blocksize
= inode
->i_sb
->s_blocksize
;
1697 req
->in
.h
.opcode
= FUSE_BMAP
;
1698 req
->in
.h
.nodeid
= get_node_id(inode
);
1699 req
->in
.numargs
= 1;
1700 req
->in
.args
[0].size
= sizeof(inarg
);
1701 req
->in
.args
[0].value
= &inarg
;
1702 req
->out
.numargs
= 1;
1703 req
->out
.args
[0].size
= sizeof(outarg
);
1704 req
->out
.args
[0].value
= &outarg
;
1705 fuse_request_send(fc
, req
);
1706 err
= req
->out
.h
.error
;
1707 fuse_put_request(fc
, req
);
1711 return err
? 0 : outarg
.block
;
1714 static loff_t
fuse_file_llseek(struct file
*file
, loff_t offset
, int whence
)
1717 struct inode
*inode
= file_inode(file
);
1719 /* No i_mutex protection necessary for SEEK_CUR and SEEK_SET */
1720 if (whence
== SEEK_CUR
|| whence
== SEEK_SET
)
1721 return generic_file_llseek(file
, offset
, whence
);
1723 mutex_lock(&inode
->i_mutex
);
1724 retval
= fuse_update_attributes(inode
, NULL
, file
, NULL
);
1726 retval
= generic_file_llseek(file
, offset
, whence
);
1727 mutex_unlock(&inode
->i_mutex
);
1732 static int fuse_ioctl_copy_user(struct page
**pages
, struct iovec
*iov
,
1733 unsigned int nr_segs
, size_t bytes
, bool to_user
)
1741 iov_iter_init(&ii
, iov
, nr_segs
, bytes
, 0);
1743 while (iov_iter_count(&ii
)) {
1744 struct page
*page
= pages
[page_idx
++];
1745 size_t todo
= min_t(size_t, PAGE_SIZE
, iov_iter_count(&ii
));
1751 char __user
*uaddr
= ii
.iov
->iov_base
+ ii
.iov_offset
;
1752 size_t iov_len
= ii
.iov
->iov_len
- ii
.iov_offset
;
1753 size_t copy
= min(todo
, iov_len
);
1757 left
= copy_from_user(kaddr
, uaddr
, copy
);
1759 left
= copy_to_user(uaddr
, kaddr
, copy
);
1764 iov_iter_advance(&ii
, copy
);
1776 * CUSE servers compiled on 32bit broke on 64bit kernels because the
1777 * ABI was defined to be 'struct iovec' which is different on 32bit
1778 * and 64bit. Fortunately we can determine which structure the server
1779 * used from the size of the reply.
1781 static int fuse_copy_ioctl_iovec_old(struct iovec
*dst
, void *src
,
1782 size_t transferred
, unsigned count
,
1785 #ifdef CONFIG_COMPAT
1786 if (count
* sizeof(struct compat_iovec
) == transferred
) {
1787 struct compat_iovec
*ciov
= src
;
1791 * With this interface a 32bit server cannot support
1792 * non-compat (i.e. ones coming from 64bit apps) ioctl
1798 for (i
= 0; i
< count
; i
++) {
1799 dst
[i
].iov_base
= compat_ptr(ciov
[i
].iov_base
);
1800 dst
[i
].iov_len
= ciov
[i
].iov_len
;
1806 if (count
* sizeof(struct iovec
) != transferred
)
1809 memcpy(dst
, src
, transferred
);
1813 /* Make sure iov_length() won't overflow */
1814 static int fuse_verify_ioctl_iov(struct iovec
*iov
, size_t count
)
1817 u32 max
= FUSE_MAX_PAGES_PER_REQ
<< PAGE_SHIFT
;
1819 for (n
= 0; n
< count
; n
++, iov
++) {
1820 if (iov
->iov_len
> (size_t) max
)
1822 max
-= iov
->iov_len
;
1827 static int fuse_copy_ioctl_iovec(struct fuse_conn
*fc
, struct iovec
*dst
,
1828 void *src
, size_t transferred
, unsigned count
,
1832 struct fuse_ioctl_iovec
*fiov
= src
;
1834 if (fc
->minor
< 16) {
1835 return fuse_copy_ioctl_iovec_old(dst
, src
, transferred
,
1839 if (count
* sizeof(struct fuse_ioctl_iovec
) != transferred
)
1842 for (i
= 0; i
< count
; i
++) {
1843 /* Did the server supply an inappropriate value? */
1844 if (fiov
[i
].base
!= (unsigned long) fiov
[i
].base
||
1845 fiov
[i
].len
!= (unsigned long) fiov
[i
].len
)
1848 dst
[i
].iov_base
= (void __user
*) (unsigned long) fiov
[i
].base
;
1849 dst
[i
].iov_len
= (size_t) fiov
[i
].len
;
1851 #ifdef CONFIG_COMPAT
1853 (ptr_to_compat(dst
[i
].iov_base
) != fiov
[i
].base
||
1854 (compat_size_t
) dst
[i
].iov_len
!= fiov
[i
].len
))
1864 * For ioctls, there is no generic way to determine how much memory
1865 * needs to be read and/or written. Furthermore, ioctls are allowed
1866 * to dereference the passed pointer, so the parameter requires deep
1867 * copying but FUSE has no idea whatsoever about what to copy in or
1870 * This is solved by allowing FUSE server to retry ioctl with
1871 * necessary in/out iovecs. Let's assume the ioctl implementation
1872 * needs to read in the following structure.
1879 * On the first callout to FUSE server, inarg->in_size and
1880 * inarg->out_size will be NULL; then, the server completes the ioctl
1881 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
1882 * the actual iov array to
1884 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
1886 * which tells FUSE to copy in the requested area and retry the ioctl.
1887 * On the second round, the server has access to the structure and
1888 * from that it can tell what to look for next, so on the invocation,
1889 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
1891 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
1892 * { .iov_base = a.buf, .iov_len = a.buflen } }
1894 * FUSE will copy both struct a and the pointed buffer from the
1895 * process doing the ioctl and retry ioctl with both struct a and the
1898 * This time, FUSE server has everything it needs and completes ioctl
1899 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
1901 * Copying data out works the same way.
1903 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
1904 * automatically initializes in and out iovs by decoding @cmd with
1905 * _IOC_* macros and the server is not allowed to request RETRY. This
1906 * limits ioctl data transfers to well-formed ioctls and is the forced
1907 * behavior for all FUSE servers.
1909 long fuse_do_ioctl(struct file
*file
, unsigned int cmd
, unsigned long arg
,
1912 struct fuse_file
*ff
= file
->private_data
;
1913 struct fuse_conn
*fc
= ff
->fc
;
1914 struct fuse_ioctl_in inarg
= {
1920 struct fuse_ioctl_out outarg
;
1921 struct fuse_req
*req
= NULL
;
1922 struct page
**pages
= NULL
;
1923 struct iovec
*iov_page
= NULL
;
1924 struct iovec
*in_iov
= NULL
, *out_iov
= NULL
;
1925 unsigned int in_iovs
= 0, out_iovs
= 0, num_pages
= 0, max_pages
;
1926 size_t in_size
, out_size
, transferred
;
1929 #if BITS_PER_LONG == 32
1930 inarg
.flags
|= FUSE_IOCTL_32BIT
;
1932 if (flags
& FUSE_IOCTL_COMPAT
)
1933 inarg
.flags
|= FUSE_IOCTL_32BIT
;
1936 /* assume all the iovs returned by client always fits in a page */
1937 BUILD_BUG_ON(sizeof(struct fuse_ioctl_iovec
) * FUSE_IOCTL_MAX_IOV
> PAGE_SIZE
);
1940 pages
= kcalloc(FUSE_MAX_PAGES_PER_REQ
, sizeof(pages
[0]), GFP_KERNEL
);
1941 iov_page
= (struct iovec
*) __get_free_page(GFP_KERNEL
);
1942 if (!pages
|| !iov_page
)
1946 * If restricted, initialize IO parameters as encoded in @cmd.
1947 * RETRY from server is not allowed.
1949 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
)) {
1950 struct iovec
*iov
= iov_page
;
1952 iov
->iov_base
= (void __user
*)arg
;
1953 iov
->iov_len
= _IOC_SIZE(cmd
);
1955 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
1960 if (_IOC_DIR(cmd
) & _IOC_READ
) {
1967 inarg
.in_size
= in_size
= iov_length(in_iov
, in_iovs
);
1968 inarg
.out_size
= out_size
= iov_length(out_iov
, out_iovs
);
1971 * Out data can be used either for actual out data or iovs,
1972 * make sure there always is at least one page.
1974 out_size
= max_t(size_t, out_size
, PAGE_SIZE
);
1975 max_pages
= DIV_ROUND_UP(max(in_size
, out_size
), PAGE_SIZE
);
1977 /* make sure there are enough buffer pages and init request with them */
1979 if (max_pages
> FUSE_MAX_PAGES_PER_REQ
)
1981 while (num_pages
< max_pages
) {
1982 pages
[num_pages
] = alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
1983 if (!pages
[num_pages
])
1988 req
= fuse_get_req(fc
, num_pages
);
1994 memcpy(req
->pages
, pages
, sizeof(req
->pages
[0]) * num_pages
);
1995 req
->num_pages
= num_pages
;
1996 fuse_page_descs_length_init(req
, 0, req
->num_pages
);
1998 /* okay, let's send it to the client */
1999 req
->in
.h
.opcode
= FUSE_IOCTL
;
2000 req
->in
.h
.nodeid
= ff
->nodeid
;
2001 req
->in
.numargs
= 1;
2002 req
->in
.args
[0].size
= sizeof(inarg
);
2003 req
->in
.args
[0].value
= &inarg
;
2006 req
->in
.args
[1].size
= in_size
;
2007 req
->in
.argpages
= 1;
2009 err
= fuse_ioctl_copy_user(pages
, in_iov
, in_iovs
, in_size
,
2015 req
->out
.numargs
= 2;
2016 req
->out
.args
[0].size
= sizeof(outarg
);
2017 req
->out
.args
[0].value
= &outarg
;
2018 req
->out
.args
[1].size
= out_size
;
2019 req
->out
.argpages
= 1;
2020 req
->out
.argvar
= 1;
2022 fuse_request_send(fc
, req
);
2023 err
= req
->out
.h
.error
;
2024 transferred
= req
->out
.args
[1].size
;
2025 fuse_put_request(fc
, req
);
2030 /* did it ask for retry? */
2031 if (outarg
.flags
& FUSE_IOCTL_RETRY
) {
2034 /* no retry if in restricted mode */
2036 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
))
2039 in_iovs
= outarg
.in_iovs
;
2040 out_iovs
= outarg
.out_iovs
;
2043 * Make sure things are in boundary, separate checks
2044 * are to protect against overflow.
2047 if (in_iovs
> FUSE_IOCTL_MAX_IOV
||
2048 out_iovs
> FUSE_IOCTL_MAX_IOV
||
2049 in_iovs
+ out_iovs
> FUSE_IOCTL_MAX_IOV
)
2052 vaddr
= kmap_atomic(pages
[0]);
2053 err
= fuse_copy_ioctl_iovec(fc
, iov_page
, vaddr
,
2054 transferred
, in_iovs
+ out_iovs
,
2055 (flags
& FUSE_IOCTL_COMPAT
) != 0);
2056 kunmap_atomic(vaddr
);
2061 out_iov
= in_iov
+ in_iovs
;
2063 err
= fuse_verify_ioctl_iov(in_iov
, in_iovs
);
2067 err
= fuse_verify_ioctl_iov(out_iov
, out_iovs
);
2075 if (transferred
> inarg
.out_size
)
2078 err
= fuse_ioctl_copy_user(pages
, out_iov
, out_iovs
, transferred
, true);
2081 fuse_put_request(fc
, req
);
2082 free_page((unsigned long) iov_page
);
2084 __free_page(pages
[--num_pages
]);
2087 return err
? err
: outarg
.result
;
2089 EXPORT_SYMBOL_GPL(fuse_do_ioctl
);
2091 long fuse_ioctl_common(struct file
*file
, unsigned int cmd
,
2092 unsigned long arg
, unsigned int flags
)
2094 struct inode
*inode
= file_inode(file
);
2095 struct fuse_conn
*fc
= get_fuse_conn(inode
);
2097 if (!fuse_allow_current_process(fc
))
2100 if (is_bad_inode(inode
))
2103 return fuse_do_ioctl(file
, cmd
, arg
, flags
);
2106 static long fuse_file_ioctl(struct file
*file
, unsigned int cmd
,
2109 return fuse_ioctl_common(file
, cmd
, arg
, 0);
2112 static long fuse_file_compat_ioctl(struct file
*file
, unsigned int cmd
,
2115 return fuse_ioctl_common(file
, cmd
, arg
, FUSE_IOCTL_COMPAT
);
2119 * All files which have been polled are linked to RB tree
2120 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
2121 * find the matching one.
2123 static struct rb_node
**fuse_find_polled_node(struct fuse_conn
*fc
, u64 kh
,
2124 struct rb_node
**parent_out
)
2126 struct rb_node
**link
= &fc
->polled_files
.rb_node
;
2127 struct rb_node
*last
= NULL
;
2130 struct fuse_file
*ff
;
2133 ff
= rb_entry(last
, struct fuse_file
, polled_node
);
2136 link
= &last
->rb_left
;
2137 else if (kh
> ff
->kh
)
2138 link
= &last
->rb_right
;
2149 * The file is about to be polled. Make sure it's on the polled_files
2150 * RB tree. Note that files once added to the polled_files tree are
2151 * not removed before the file is released. This is because a file
2152 * polled once is likely to be polled again.
2154 static void fuse_register_polled_file(struct fuse_conn
*fc
,
2155 struct fuse_file
*ff
)
2157 spin_lock(&fc
->lock
);
2158 if (RB_EMPTY_NODE(&ff
->polled_node
)) {
2159 struct rb_node
**link
, *parent
;
2161 link
= fuse_find_polled_node(fc
, ff
->kh
, &parent
);
2163 rb_link_node(&ff
->polled_node
, parent
, link
);
2164 rb_insert_color(&ff
->polled_node
, &fc
->polled_files
);
2166 spin_unlock(&fc
->lock
);
2169 unsigned fuse_file_poll(struct file
*file
, poll_table
*wait
)
2171 struct fuse_file
*ff
= file
->private_data
;
2172 struct fuse_conn
*fc
= ff
->fc
;
2173 struct fuse_poll_in inarg
= { .fh
= ff
->fh
, .kh
= ff
->kh
};
2174 struct fuse_poll_out outarg
;
2175 struct fuse_req
*req
;
2179 return DEFAULT_POLLMASK
;
2181 poll_wait(file
, &ff
->poll_wait
, wait
);
2182 inarg
.events
= (__u32
)poll_requested_events(wait
);
2185 * Ask for notification iff there's someone waiting for it.
2186 * The client may ignore the flag and always notify.
2188 if (waitqueue_active(&ff
->poll_wait
)) {
2189 inarg
.flags
|= FUSE_POLL_SCHEDULE_NOTIFY
;
2190 fuse_register_polled_file(fc
, ff
);
2193 req
= fuse_get_req_nopages(fc
);
2197 req
->in
.h
.opcode
= FUSE_POLL
;
2198 req
->in
.h
.nodeid
= ff
->nodeid
;
2199 req
->in
.numargs
= 1;
2200 req
->in
.args
[0].size
= sizeof(inarg
);
2201 req
->in
.args
[0].value
= &inarg
;
2202 req
->out
.numargs
= 1;
2203 req
->out
.args
[0].size
= sizeof(outarg
);
2204 req
->out
.args
[0].value
= &outarg
;
2205 fuse_request_send(fc
, req
);
2206 err
= req
->out
.h
.error
;
2207 fuse_put_request(fc
, req
);
2210 return outarg
.revents
;
2211 if (err
== -ENOSYS
) {
2213 return DEFAULT_POLLMASK
;
2217 EXPORT_SYMBOL_GPL(fuse_file_poll
);
2220 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
2221 * wakes up the poll waiters.
2223 int fuse_notify_poll_wakeup(struct fuse_conn
*fc
,
2224 struct fuse_notify_poll_wakeup_out
*outarg
)
2226 u64 kh
= outarg
->kh
;
2227 struct rb_node
**link
;
2229 spin_lock(&fc
->lock
);
2231 link
= fuse_find_polled_node(fc
, kh
, NULL
);
2233 struct fuse_file
*ff
;
2235 ff
= rb_entry(*link
, struct fuse_file
, polled_node
);
2236 wake_up_interruptible_sync(&ff
->poll_wait
);
2239 spin_unlock(&fc
->lock
);
2244 fuse_direct_IO(int rw
, struct kiocb
*iocb
, const struct iovec
*iov
,
2245 loff_t offset
, unsigned long nr_segs
)
2248 struct file
*file
= NULL
;
2251 file
= iocb
->ki_filp
;
2255 ret
= __fuse_direct_write(file
, iov
, nr_segs
, &pos
);
2257 ret
= __fuse_direct_read(file
, iov
, nr_segs
, &pos
);
2262 static long fuse_file_fallocate(struct file
*file
, int mode
, loff_t offset
,
2265 struct fuse_file
*ff
= file
->private_data
;
2266 struct fuse_conn
*fc
= ff
->fc
;
2267 struct fuse_req
*req
;
2268 struct fuse_fallocate_in inarg
= {
2276 if (fc
->no_fallocate
)
2279 req
= fuse_get_req_nopages(fc
);
2281 return PTR_ERR(req
);
2283 req
->in
.h
.opcode
= FUSE_FALLOCATE
;
2284 req
->in
.h
.nodeid
= ff
->nodeid
;
2285 req
->in
.numargs
= 1;
2286 req
->in
.args
[0].size
= sizeof(inarg
);
2287 req
->in
.args
[0].value
= &inarg
;
2288 fuse_request_send(fc
, req
);
2289 err
= req
->out
.h
.error
;
2290 if (err
== -ENOSYS
) {
2291 fc
->no_fallocate
= 1;
2294 fuse_put_request(fc
, req
);
2299 static const struct file_operations fuse_file_operations
= {
2300 .llseek
= fuse_file_llseek
,
2301 .read
= do_sync_read
,
2302 .aio_read
= fuse_file_aio_read
,
2303 .write
= do_sync_write
,
2304 .aio_write
= fuse_file_aio_write
,
2305 .mmap
= fuse_file_mmap
,
2307 .flush
= fuse_flush
,
2308 .release
= fuse_release
,
2309 .fsync
= fuse_fsync
,
2310 .lock
= fuse_file_lock
,
2311 .flock
= fuse_file_flock
,
2312 .splice_read
= generic_file_splice_read
,
2313 .unlocked_ioctl
= fuse_file_ioctl
,
2314 .compat_ioctl
= fuse_file_compat_ioctl
,
2315 .poll
= fuse_file_poll
,
2316 .fallocate
= fuse_file_fallocate
,
2319 static const struct file_operations fuse_direct_io_file_operations
= {
2320 .llseek
= fuse_file_llseek
,
2321 .read
= fuse_direct_read
,
2322 .write
= fuse_direct_write
,
2323 .mmap
= fuse_direct_mmap
,
2325 .flush
= fuse_flush
,
2326 .release
= fuse_release
,
2327 .fsync
= fuse_fsync
,
2328 .lock
= fuse_file_lock
,
2329 .flock
= fuse_file_flock
,
2330 .unlocked_ioctl
= fuse_file_ioctl
,
2331 .compat_ioctl
= fuse_file_compat_ioctl
,
2332 .poll
= fuse_file_poll
,
2333 .fallocate
= fuse_file_fallocate
,
2334 /* no splice_read */
2337 static const struct address_space_operations fuse_file_aops
= {
2338 .readpage
= fuse_readpage
,
2339 .writepage
= fuse_writepage
,
2340 .launder_page
= fuse_launder_page
,
2341 .readpages
= fuse_readpages
,
2342 .set_page_dirty
= __set_page_dirty_nobuffers
,
2344 .direct_IO
= fuse_direct_IO
,
2347 void fuse_init_file_inode(struct inode
*inode
)
2349 inode
->i_fop
= &fuse_file_operations
;
2350 inode
->i_data
.a_ops
= &fuse_file_aops
;