2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU GPL.
11 #include <linux/pagemap.h>
12 #include <linux/slab.h>
13 #include <linux/kernel.h>
14 #include <linux/sched.h>
16 static const struct file_operations fuse_direct_io_file_operations
;
18 static int fuse_send_open(struct inode
*inode
, struct file
*file
, int isdir
,
19 struct fuse_open_out
*outargp
)
21 struct fuse_conn
*fc
= get_fuse_conn(inode
);
22 struct fuse_open_in inarg
;
26 req
= fuse_get_req(fc
);
30 memset(&inarg
, 0, sizeof(inarg
));
31 inarg
.flags
= file
->f_flags
& ~(O_CREAT
| O_EXCL
| O_NOCTTY
);
32 if (!fc
->atomic_o_trunc
)
33 inarg
.flags
&= ~O_TRUNC
;
34 req
->in
.h
.opcode
= isdir
? FUSE_OPENDIR
: FUSE_OPEN
;
35 req
->in
.h
.nodeid
= get_node_id(inode
);
37 req
->in
.args
[0].size
= sizeof(inarg
);
38 req
->in
.args
[0].value
= &inarg
;
40 req
->out
.args
[0].size
= sizeof(*outargp
);
41 req
->out
.args
[0].value
= outargp
;
42 fuse_request_send(fc
, req
);
43 err
= req
->out
.h
.error
;
44 fuse_put_request(fc
, req
);
49 struct fuse_file
*fuse_file_alloc(struct fuse_conn
*fc
)
52 ff
= kmalloc(sizeof(struct fuse_file
), GFP_KERNEL
);
54 ff
->reserved_req
= fuse_request_alloc();
55 if (!ff
->reserved_req
) {
59 INIT_LIST_HEAD(&ff
->write_entry
);
60 atomic_set(&ff
->count
, 0);
63 spin_unlock(&fc
->lock
);
65 RB_CLEAR_NODE(&ff
->polled_node
);
66 init_waitqueue_head(&ff
->poll_wait
);
71 void fuse_file_free(struct fuse_file
*ff
)
73 fuse_request_free(ff
->reserved_req
);
77 static struct fuse_file
*fuse_file_get(struct fuse_file
*ff
)
79 atomic_inc(&ff
->count
);
83 static void fuse_release_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
85 dput(req
->misc
.release
.dentry
);
86 mntput(req
->misc
.release
.vfsmount
);
89 static void fuse_file_put(struct fuse_file
*ff
)
91 if (atomic_dec_and_test(&ff
->count
)) {
92 struct fuse_req
*req
= ff
->reserved_req
;
93 struct inode
*inode
= req
->misc
.release
.dentry
->d_inode
;
94 struct fuse_conn
*fc
= get_fuse_conn(inode
);
95 req
->end
= fuse_release_end
;
96 fuse_request_send_background(fc
, req
);
101 void fuse_finish_open(struct inode
*inode
, struct file
*file
,
102 struct fuse_file
*ff
, struct fuse_open_out
*outarg
)
104 if (outarg
->open_flags
& FOPEN_DIRECT_IO
)
105 file
->f_op
= &fuse_direct_io_file_operations
;
106 if (!(outarg
->open_flags
& FOPEN_KEEP_CACHE
))
107 invalidate_inode_pages2(inode
->i_mapping
);
108 if (outarg
->open_flags
& FOPEN_NONSEEKABLE
)
109 nonseekable_open(inode
, file
);
111 file
->private_data
= fuse_file_get(ff
);
114 int fuse_open_common(struct inode
*inode
, struct file
*file
, int isdir
)
116 struct fuse_conn
*fc
= get_fuse_conn(inode
);
117 struct fuse_open_out outarg
;
118 struct fuse_file
*ff
;
121 /* VFS checks this, but only _after_ ->open() */
122 if (file
->f_flags
& O_DIRECT
)
125 err
= generic_file_open(inode
, file
);
129 ff
= fuse_file_alloc(fc
);
133 err
= fuse_send_open(inode
, file
, isdir
, &outarg
);
138 outarg
.open_flags
&= ~FOPEN_DIRECT_IO
;
139 fuse_finish_open(inode
, file
, ff
, &outarg
);
145 void fuse_release_fill(struct fuse_file
*ff
, u64 nodeid
, int flags
, int opcode
)
147 struct fuse_req
*req
= ff
->reserved_req
;
148 struct fuse_release_in
*inarg
= &req
->misc
.release
.in
;
151 inarg
->flags
= flags
;
152 req
->in
.h
.opcode
= opcode
;
153 req
->in
.h
.nodeid
= nodeid
;
155 req
->in
.args
[0].size
= sizeof(struct fuse_release_in
);
156 req
->in
.args
[0].value
= inarg
;
159 int fuse_release_common(struct inode
*inode
, struct file
*file
, int isdir
)
161 struct fuse_file
*ff
= file
->private_data
;
163 struct fuse_conn
*fc
= get_fuse_conn(inode
);
164 struct fuse_req
*req
= ff
->reserved_req
;
166 fuse_release_fill(ff
, get_node_id(inode
), file
->f_flags
,
167 isdir
? FUSE_RELEASEDIR
: FUSE_RELEASE
);
169 /* Hold vfsmount and dentry until release is finished */
170 req
->misc
.release
.vfsmount
= mntget(file
->f_path
.mnt
);
171 req
->misc
.release
.dentry
= dget(file
->f_path
.dentry
);
173 spin_lock(&fc
->lock
);
174 list_del(&ff
->write_entry
);
175 if (!RB_EMPTY_NODE(&ff
->polled_node
))
176 rb_erase(&ff
->polled_node
, &fc
->polled_files
);
177 spin_unlock(&fc
->lock
);
179 wake_up_interruptible_sync(&ff
->poll_wait
);
181 * Normally this will send the RELEASE request,
182 * however if some asynchronous READ or WRITE requests
183 * are outstanding, the sending will be delayed
188 /* Return value is ignored by VFS */
192 static int fuse_open(struct inode
*inode
, struct file
*file
)
194 return fuse_open_common(inode
, file
, 0);
197 static int fuse_release(struct inode
*inode
, struct file
*file
)
199 return fuse_release_common(inode
, file
, 0);
203 * Scramble the ID space with XTEA, so that the value of the files_struct
204 * pointer is not exposed to userspace.
206 u64
fuse_lock_owner_id(struct fuse_conn
*fc
, fl_owner_t id
)
208 u32
*k
= fc
->scramble_key
;
209 u64 v
= (unsigned long) id
;
215 for (i
= 0; i
< 32; i
++) {
216 v0
+= ((v1
<< 4 ^ v1
>> 5) + v1
) ^ (sum
+ k
[sum
& 3]);
218 v1
+= ((v0
<< 4 ^ v0
>> 5) + v0
) ^ (sum
+ k
[sum
>>11 & 3]);
221 return (u64
) v0
+ ((u64
) v1
<< 32);
225 * Check if page is under writeback
227 * This is currently done by walking the list of writepage requests
228 * for the inode, which can be pretty inefficient.
230 static bool fuse_page_is_writeback(struct inode
*inode
, pgoff_t index
)
232 struct fuse_conn
*fc
= get_fuse_conn(inode
);
233 struct fuse_inode
*fi
= get_fuse_inode(inode
);
234 struct fuse_req
*req
;
237 spin_lock(&fc
->lock
);
238 list_for_each_entry(req
, &fi
->writepages
, writepages_entry
) {
241 BUG_ON(req
->inode
!= inode
);
242 curr_index
= req
->misc
.write
.in
.offset
>> PAGE_CACHE_SHIFT
;
243 if (curr_index
== index
) {
248 spin_unlock(&fc
->lock
);
254 * Wait for page writeback to be completed.
256 * Since fuse doesn't rely on the VM writeback tracking, this has to
257 * use some other means.
259 static int fuse_wait_on_page_writeback(struct inode
*inode
, pgoff_t index
)
261 struct fuse_inode
*fi
= get_fuse_inode(inode
);
263 wait_event(fi
->page_waitq
, !fuse_page_is_writeback(inode
, index
));
267 static int fuse_flush(struct file
*file
, fl_owner_t id
)
269 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
270 struct fuse_conn
*fc
= get_fuse_conn(inode
);
271 struct fuse_file
*ff
= file
->private_data
;
272 struct fuse_req
*req
;
273 struct fuse_flush_in inarg
;
276 if (is_bad_inode(inode
))
282 req
= fuse_get_req_nofail(fc
, file
);
283 memset(&inarg
, 0, sizeof(inarg
));
285 inarg
.lock_owner
= fuse_lock_owner_id(fc
, id
);
286 req
->in
.h
.opcode
= FUSE_FLUSH
;
287 req
->in
.h
.nodeid
= get_node_id(inode
);
289 req
->in
.args
[0].size
= sizeof(inarg
);
290 req
->in
.args
[0].value
= &inarg
;
292 fuse_request_send(fc
, req
);
293 err
= req
->out
.h
.error
;
294 fuse_put_request(fc
, req
);
295 if (err
== -ENOSYS
) {
303 * Wait for all pending writepages on the inode to finish.
305 * This is currently done by blocking further writes with FUSE_NOWRITE
306 * and waiting for all sent writes to complete.
308 * This must be called under i_mutex, otherwise the FUSE_NOWRITE usage
309 * could conflict with truncation.
311 static void fuse_sync_writes(struct inode
*inode
)
313 fuse_set_nowrite(inode
);
314 fuse_release_nowrite(inode
);
317 int fuse_fsync_common(struct file
*file
, struct dentry
*de
, int datasync
,
320 struct inode
*inode
= de
->d_inode
;
321 struct fuse_conn
*fc
= get_fuse_conn(inode
);
322 struct fuse_file
*ff
= file
->private_data
;
323 struct fuse_req
*req
;
324 struct fuse_fsync_in inarg
;
327 if (is_bad_inode(inode
))
330 if ((!isdir
&& fc
->no_fsync
) || (isdir
&& fc
->no_fsyncdir
))
334 * Start writeback against all dirty pages of the inode, then
335 * wait for all outstanding writes, before sending the FSYNC
338 err
= write_inode_now(inode
, 0);
342 fuse_sync_writes(inode
);
344 req
= fuse_get_req(fc
);
348 memset(&inarg
, 0, sizeof(inarg
));
350 inarg
.fsync_flags
= datasync
? 1 : 0;
351 req
->in
.h
.opcode
= isdir
? FUSE_FSYNCDIR
: FUSE_FSYNC
;
352 req
->in
.h
.nodeid
= get_node_id(inode
);
354 req
->in
.args
[0].size
= sizeof(inarg
);
355 req
->in
.args
[0].value
= &inarg
;
356 fuse_request_send(fc
, req
);
357 err
= req
->out
.h
.error
;
358 fuse_put_request(fc
, req
);
359 if (err
== -ENOSYS
) {
369 static int fuse_fsync(struct file
*file
, struct dentry
*de
, int datasync
)
371 return fuse_fsync_common(file
, de
, datasync
, 0);
374 void fuse_read_fill(struct fuse_req
*req
, struct file
*file
,
375 struct inode
*inode
, loff_t pos
, size_t count
, int opcode
)
377 struct fuse_read_in
*inarg
= &req
->misc
.read
.in
;
378 struct fuse_file
*ff
= file
->private_data
;
383 inarg
->flags
= file
->f_flags
;
384 req
->in
.h
.opcode
= opcode
;
385 req
->in
.h
.nodeid
= get_node_id(inode
);
387 req
->in
.args
[0].size
= sizeof(struct fuse_read_in
);
388 req
->in
.args
[0].value
= inarg
;
390 req
->out
.numargs
= 1;
391 req
->out
.args
[0].size
= count
;
394 static size_t fuse_send_read(struct fuse_req
*req
, struct file
*file
,
395 struct inode
*inode
, loff_t pos
, size_t count
,
398 struct fuse_conn
*fc
= get_fuse_conn(inode
);
400 fuse_read_fill(req
, file
, inode
, pos
, count
, FUSE_READ
);
402 struct fuse_read_in
*inarg
= &req
->misc
.read
.in
;
404 inarg
->read_flags
|= FUSE_READ_LOCKOWNER
;
405 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
407 fuse_request_send(fc
, req
);
408 return req
->out
.args
[0].size
;
411 static void fuse_read_update_size(struct inode
*inode
, loff_t size
,
414 struct fuse_conn
*fc
= get_fuse_conn(inode
);
415 struct fuse_inode
*fi
= get_fuse_inode(inode
);
417 spin_lock(&fc
->lock
);
418 if (attr_ver
== fi
->attr_version
&& size
< inode
->i_size
) {
419 fi
->attr_version
= ++fc
->attr_version
;
420 i_size_write(inode
, size
);
422 spin_unlock(&fc
->lock
);
425 static int fuse_readpage(struct file
*file
, struct page
*page
)
427 struct inode
*inode
= page
->mapping
->host
;
428 struct fuse_conn
*fc
= get_fuse_conn(inode
);
429 struct fuse_req
*req
;
431 loff_t pos
= page_offset(page
);
432 size_t count
= PAGE_CACHE_SIZE
;
437 if (is_bad_inode(inode
))
441 * Page writeback can extend beyond the liftime of the
442 * page-cache page, so make sure we read a properly synced
445 fuse_wait_on_page_writeback(inode
, page
->index
);
447 req
= fuse_get_req(fc
);
452 attr_ver
= fuse_get_attr_version(fc
);
454 req
->out
.page_zeroing
= 1;
455 req
->out
.argpages
= 1;
457 req
->pages
[0] = page
;
458 num_read
= fuse_send_read(req
, file
, inode
, pos
, count
, NULL
);
459 err
= req
->out
.h
.error
;
460 fuse_put_request(fc
, req
);
464 * Short read means EOF. If file size is larger, truncate it
466 if (num_read
< count
)
467 fuse_read_update_size(inode
, pos
+ num_read
, attr_ver
);
469 SetPageUptodate(page
);
472 fuse_invalidate_attr(inode
); /* atime changed */
478 static void fuse_readpages_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
481 size_t count
= req
->misc
.read
.in
.size
;
482 size_t num_read
= req
->out
.args
[0].size
;
483 struct inode
*inode
= req
->pages
[0]->mapping
->host
;
486 * Short read means EOF. If file size is larger, truncate it
488 if (!req
->out
.h
.error
&& num_read
< count
) {
489 loff_t pos
= page_offset(req
->pages
[0]) + num_read
;
490 fuse_read_update_size(inode
, pos
, req
->misc
.read
.attr_ver
);
493 fuse_invalidate_attr(inode
); /* atime changed */
495 for (i
= 0; i
< req
->num_pages
; i
++) {
496 struct page
*page
= req
->pages
[i
];
497 if (!req
->out
.h
.error
)
498 SetPageUptodate(page
);
504 fuse_file_put(req
->ff
);
507 static void fuse_send_readpages(struct fuse_req
*req
, struct file
*file
,
510 struct fuse_conn
*fc
= get_fuse_conn(inode
);
511 loff_t pos
= page_offset(req
->pages
[0]);
512 size_t count
= req
->num_pages
<< PAGE_CACHE_SHIFT
;
514 req
->out
.argpages
= 1;
515 req
->out
.page_zeroing
= 1;
516 fuse_read_fill(req
, file
, inode
, pos
, count
, FUSE_READ
);
517 req
->misc
.read
.attr_ver
= fuse_get_attr_version(fc
);
518 if (fc
->async_read
) {
519 struct fuse_file
*ff
= file
->private_data
;
520 req
->ff
= fuse_file_get(ff
);
521 req
->end
= fuse_readpages_end
;
522 fuse_request_send_background(fc
, req
);
524 fuse_request_send(fc
, req
);
525 fuse_readpages_end(fc
, req
);
526 fuse_put_request(fc
, req
);
530 struct fuse_fill_data
{
531 struct fuse_req
*req
;
536 static int fuse_readpages_fill(void *_data
, struct page
*page
)
538 struct fuse_fill_data
*data
= _data
;
539 struct fuse_req
*req
= data
->req
;
540 struct inode
*inode
= data
->inode
;
541 struct fuse_conn
*fc
= get_fuse_conn(inode
);
543 fuse_wait_on_page_writeback(inode
, page
->index
);
545 if (req
->num_pages
&&
546 (req
->num_pages
== FUSE_MAX_PAGES_PER_REQ
||
547 (req
->num_pages
+ 1) * PAGE_CACHE_SIZE
> fc
->max_read
||
548 req
->pages
[req
->num_pages
- 1]->index
+ 1 != page
->index
)) {
549 fuse_send_readpages(req
, data
->file
, inode
);
550 data
->req
= req
= fuse_get_req(fc
);
556 req
->pages
[req
->num_pages
] = page
;
561 static int fuse_readpages(struct file
*file
, struct address_space
*mapping
,
562 struct list_head
*pages
, unsigned nr_pages
)
564 struct inode
*inode
= mapping
->host
;
565 struct fuse_conn
*fc
= get_fuse_conn(inode
);
566 struct fuse_fill_data data
;
570 if (is_bad_inode(inode
))
575 data
.req
= fuse_get_req(fc
);
576 err
= PTR_ERR(data
.req
);
577 if (IS_ERR(data
.req
))
580 err
= read_cache_pages(mapping
, pages
, fuse_readpages_fill
, &data
);
582 if (data
.req
->num_pages
)
583 fuse_send_readpages(data
.req
, file
, inode
);
585 fuse_put_request(fc
, data
.req
);
591 static ssize_t
fuse_file_aio_read(struct kiocb
*iocb
, const struct iovec
*iov
,
592 unsigned long nr_segs
, loff_t pos
)
594 struct inode
*inode
= iocb
->ki_filp
->f_mapping
->host
;
596 if (pos
+ iov_length(iov
, nr_segs
) > i_size_read(inode
)) {
599 * If trying to read past EOF, make sure the i_size
600 * attribute is up-to-date.
602 err
= fuse_update_attributes(inode
, NULL
, iocb
->ki_filp
, NULL
);
607 return generic_file_aio_read(iocb
, iov
, nr_segs
, pos
);
610 static void fuse_write_fill(struct fuse_req
*req
, struct file
*file
,
611 struct fuse_file
*ff
, struct inode
*inode
,
612 loff_t pos
, size_t count
, int writepage
)
614 struct fuse_conn
*fc
= get_fuse_conn(inode
);
615 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
616 struct fuse_write_out
*outarg
= &req
->misc
.write
.out
;
618 memset(inarg
, 0, sizeof(struct fuse_write_in
));
622 inarg
->write_flags
= writepage
? FUSE_WRITE_CACHE
: 0;
623 inarg
->flags
= file
? file
->f_flags
: 0;
624 req
->in
.h
.opcode
= FUSE_WRITE
;
625 req
->in
.h
.nodeid
= get_node_id(inode
);
628 req
->in
.args
[0].size
= FUSE_COMPAT_WRITE_IN_SIZE
;
630 req
->in
.args
[0].size
= sizeof(struct fuse_write_in
);
631 req
->in
.args
[0].value
= inarg
;
632 req
->in
.args
[1].size
= count
;
633 req
->out
.numargs
= 1;
634 req
->out
.args
[0].size
= sizeof(struct fuse_write_out
);
635 req
->out
.args
[0].value
= outarg
;
638 static size_t fuse_send_write(struct fuse_req
*req
, struct file
*file
,
639 struct inode
*inode
, loff_t pos
, size_t count
,
642 struct fuse_conn
*fc
= get_fuse_conn(inode
);
643 fuse_write_fill(req
, file
, file
->private_data
, inode
, pos
, count
, 0);
645 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
646 inarg
->write_flags
|= FUSE_WRITE_LOCKOWNER
;
647 inarg
->lock_owner
= fuse_lock_owner_id(fc
, owner
);
649 fuse_request_send(fc
, req
);
650 return req
->misc
.write
.out
.size
;
653 static int fuse_write_begin(struct file
*file
, struct address_space
*mapping
,
654 loff_t pos
, unsigned len
, unsigned flags
,
655 struct page
**pagep
, void **fsdata
)
657 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
659 *pagep
= grab_cache_page_write_begin(mapping
, index
, flags
);
665 static void fuse_write_update_size(struct inode
*inode
, loff_t pos
)
667 struct fuse_conn
*fc
= get_fuse_conn(inode
);
668 struct fuse_inode
*fi
= get_fuse_inode(inode
);
670 spin_lock(&fc
->lock
);
671 fi
->attr_version
= ++fc
->attr_version
;
672 if (pos
> inode
->i_size
)
673 i_size_write(inode
, pos
);
674 spin_unlock(&fc
->lock
);
677 static int fuse_buffered_write(struct file
*file
, struct inode
*inode
,
678 loff_t pos
, unsigned count
, struct page
*page
)
682 struct fuse_conn
*fc
= get_fuse_conn(inode
);
683 unsigned offset
= pos
& (PAGE_CACHE_SIZE
- 1);
684 struct fuse_req
*req
;
686 if (is_bad_inode(inode
))
690 * Make sure writepages on the same page are not mixed up with
693 fuse_wait_on_page_writeback(inode
, page
->index
);
695 req
= fuse_get_req(fc
);
699 req
->in
.argpages
= 1;
701 req
->pages
[0] = page
;
702 req
->page_offset
= offset
;
703 nres
= fuse_send_write(req
, file
, inode
, pos
, count
, NULL
);
704 err
= req
->out
.h
.error
;
705 fuse_put_request(fc
, req
);
710 fuse_write_update_size(inode
, pos
);
711 if (count
== PAGE_CACHE_SIZE
)
712 SetPageUptodate(page
);
714 fuse_invalidate_attr(inode
);
715 return err
? err
: nres
;
718 static int fuse_write_end(struct file
*file
, struct address_space
*mapping
,
719 loff_t pos
, unsigned len
, unsigned copied
,
720 struct page
*page
, void *fsdata
)
722 struct inode
*inode
= mapping
->host
;
726 res
= fuse_buffered_write(file
, inode
, pos
, copied
, page
);
729 page_cache_release(page
);
733 static size_t fuse_send_write_pages(struct fuse_req
*req
, struct file
*file
,
734 struct inode
*inode
, loff_t pos
,
741 for (i
= 0; i
< req
->num_pages
; i
++)
742 fuse_wait_on_page_writeback(inode
, req
->pages
[i
]->index
);
744 res
= fuse_send_write(req
, file
, inode
, pos
, count
, NULL
);
746 offset
= req
->page_offset
;
748 for (i
= 0; i
< req
->num_pages
; i
++) {
749 struct page
*page
= req
->pages
[i
];
751 if (!req
->out
.h
.error
&& !offset
&& count
>= PAGE_CACHE_SIZE
)
752 SetPageUptodate(page
);
754 if (count
> PAGE_CACHE_SIZE
- offset
)
755 count
-= PAGE_CACHE_SIZE
- offset
;
761 page_cache_release(page
);
767 static ssize_t
fuse_fill_write_pages(struct fuse_req
*req
,
768 struct address_space
*mapping
,
769 struct iov_iter
*ii
, loff_t pos
)
771 struct fuse_conn
*fc
= get_fuse_conn(mapping
->host
);
772 unsigned offset
= pos
& (PAGE_CACHE_SIZE
- 1);
776 req
->in
.argpages
= 1;
777 req
->page_offset
= offset
;
782 pgoff_t index
= pos
>> PAGE_CACHE_SHIFT
;
783 size_t bytes
= min_t(size_t, PAGE_CACHE_SIZE
- offset
,
786 bytes
= min_t(size_t, bytes
, fc
->max_write
- count
);
790 if (iov_iter_fault_in_readable(ii
, bytes
))
794 page
= grab_cache_page_write_begin(mapping
, index
, 0);
799 tmp
= iov_iter_copy_from_user_atomic(page
, ii
, offset
, bytes
);
801 flush_dcache_page(page
);
805 page_cache_release(page
);
806 bytes
= min(bytes
, iov_iter_single_seg_count(ii
));
811 req
->pages
[req
->num_pages
] = page
;
814 iov_iter_advance(ii
, tmp
);
818 if (offset
== PAGE_CACHE_SIZE
)
823 } while (iov_iter_count(ii
) && count
< fc
->max_write
&&
824 req
->num_pages
< FUSE_MAX_PAGES_PER_REQ
&& offset
== 0);
826 return count
> 0 ? count
: err
;
829 static ssize_t
fuse_perform_write(struct file
*file
,
830 struct address_space
*mapping
,
831 struct iov_iter
*ii
, loff_t pos
)
833 struct inode
*inode
= mapping
->host
;
834 struct fuse_conn
*fc
= get_fuse_conn(inode
);
838 if (is_bad_inode(inode
))
842 struct fuse_req
*req
;
845 req
= fuse_get_req(fc
);
851 count
= fuse_fill_write_pages(req
, mapping
, ii
, pos
);
857 num_written
= fuse_send_write_pages(req
, file
, inode
,
859 err
= req
->out
.h
.error
;
864 /* break out of the loop on short write */
865 if (num_written
!= count
)
869 fuse_put_request(fc
, req
);
870 } while (!err
&& iov_iter_count(ii
));
873 fuse_write_update_size(inode
, pos
);
875 fuse_invalidate_attr(inode
);
877 return res
> 0 ? res
: err
;
880 static ssize_t
fuse_file_aio_write(struct kiocb
*iocb
, const struct iovec
*iov
,
881 unsigned long nr_segs
, loff_t pos
)
883 struct file
*file
= iocb
->ki_filp
;
884 struct address_space
*mapping
= file
->f_mapping
;
887 struct inode
*inode
= mapping
->host
;
891 WARN_ON(iocb
->ki_pos
!= pos
);
893 err
= generic_segment_checks(iov
, &nr_segs
, &count
, VERIFY_READ
);
897 mutex_lock(&inode
->i_mutex
);
898 vfs_check_frozen(inode
->i_sb
, SB_FREEZE_WRITE
);
900 /* We can write back this queue in page reclaim */
901 current
->backing_dev_info
= mapping
->backing_dev_info
;
903 err
= generic_write_checks(file
, &pos
, &count
, S_ISBLK(inode
->i_mode
));
910 err
= file_remove_suid(file
);
914 file_update_time(file
);
916 iov_iter_init(&i
, iov
, nr_segs
, count
, 0);
917 written
= fuse_perform_write(file
, mapping
, &i
, pos
);
919 iocb
->ki_pos
= pos
+ written
;
922 current
->backing_dev_info
= NULL
;
923 mutex_unlock(&inode
->i_mutex
);
925 return written
? written
: err
;
928 static void fuse_release_user_pages(struct fuse_req
*req
, int write
)
932 for (i
= 0; i
< req
->num_pages
; i
++) {
933 struct page
*page
= req
->pages
[i
];
935 set_page_dirty_lock(page
);
940 static int fuse_get_user_pages(struct fuse_req
*req
, const char __user
*buf
,
941 size_t *nbytesp
, int write
)
943 size_t nbytes
= *nbytesp
;
944 unsigned long user_addr
= (unsigned long) buf
;
945 unsigned offset
= user_addr
& ~PAGE_MASK
;
948 /* Special case for kernel I/O: can copy directly into the buffer */
949 if (segment_eq(get_fs(), KERNEL_DS
)) {
951 req
->in
.args
[1].value
= (void *) user_addr
;
953 req
->out
.args
[0].value
= (void *) user_addr
;
958 nbytes
= min_t(size_t, nbytes
, FUSE_MAX_PAGES_PER_REQ
<< PAGE_SHIFT
);
959 npages
= (nbytes
+ offset
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
960 npages
= clamp(npages
, 1, FUSE_MAX_PAGES_PER_REQ
);
961 down_read(¤t
->mm
->mmap_sem
);
962 npages
= get_user_pages(current
, current
->mm
, user_addr
, npages
, !write
,
963 0, req
->pages
, NULL
);
964 up_read(¤t
->mm
->mmap_sem
);
968 req
->num_pages
= npages
;
969 req
->page_offset
= offset
;
972 req
->in
.argpages
= 1;
974 req
->out
.argpages
= 1;
976 nbytes
= (req
->num_pages
<< PAGE_SHIFT
) - req
->page_offset
;
977 *nbytesp
= min(*nbytesp
, nbytes
);
982 static ssize_t
fuse_direct_io(struct file
*file
, const char __user
*buf
,
983 size_t count
, loff_t
*ppos
, int write
)
985 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
986 struct fuse_conn
*fc
= get_fuse_conn(inode
);
987 size_t nmax
= write
? fc
->max_write
: fc
->max_read
;
990 struct fuse_req
*req
;
992 if (is_bad_inode(inode
))
995 req
= fuse_get_req(fc
);
1001 size_t nbytes
= min(count
, nmax
);
1002 int err
= fuse_get_user_pages(req
, buf
, &nbytes
, write
);
1009 nres
= fuse_send_write(req
, file
, inode
, pos
, nbytes
,
1012 nres
= fuse_send_read(req
, file
, inode
, pos
, nbytes
,
1014 fuse_release_user_pages(req
, !write
);
1015 if (req
->out
.h
.error
) {
1017 res
= req
->out
.h
.error
;
1019 } else if (nres
> nbytes
) {
1030 fuse_put_request(fc
, req
);
1031 req
= fuse_get_req(fc
);
1036 fuse_put_request(fc
, req
);
1039 fuse_write_update_size(inode
, pos
);
1042 fuse_invalidate_attr(inode
);
1047 static ssize_t
fuse_direct_read(struct file
*file
, char __user
*buf
,
1048 size_t count
, loff_t
*ppos
)
1050 return fuse_direct_io(file
, buf
, count
, ppos
, 0);
1053 static ssize_t
fuse_direct_write(struct file
*file
, const char __user
*buf
,
1054 size_t count
, loff_t
*ppos
)
1056 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1058 /* Don't allow parallel writes to the same file */
1059 mutex_lock(&inode
->i_mutex
);
1060 res
= generic_write_checks(file
, ppos
, &count
, 0);
1062 res
= fuse_direct_io(file
, buf
, count
, ppos
, 1);
1063 mutex_unlock(&inode
->i_mutex
);
1067 static void fuse_writepage_free(struct fuse_conn
*fc
, struct fuse_req
*req
)
1069 __free_page(req
->pages
[0]);
1070 fuse_file_put(req
->ff
);
1073 static void fuse_writepage_finish(struct fuse_conn
*fc
, struct fuse_req
*req
)
1075 struct inode
*inode
= req
->inode
;
1076 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1077 struct backing_dev_info
*bdi
= inode
->i_mapping
->backing_dev_info
;
1079 list_del(&req
->writepages_entry
);
1080 dec_bdi_stat(bdi
, BDI_WRITEBACK
);
1081 dec_zone_page_state(req
->pages
[0], NR_WRITEBACK_TEMP
);
1082 bdi_writeout_inc(bdi
);
1083 wake_up(&fi
->page_waitq
);
1086 /* Called under fc->lock, may release and reacquire it */
1087 static void fuse_send_writepage(struct fuse_conn
*fc
, struct fuse_req
*req
)
1088 __releases(&fc
->lock
)
1089 __acquires(&fc
->lock
)
1091 struct fuse_inode
*fi
= get_fuse_inode(req
->inode
);
1092 loff_t size
= i_size_read(req
->inode
);
1093 struct fuse_write_in
*inarg
= &req
->misc
.write
.in
;
1098 if (inarg
->offset
+ PAGE_CACHE_SIZE
<= size
) {
1099 inarg
->size
= PAGE_CACHE_SIZE
;
1100 } else if (inarg
->offset
< size
) {
1101 inarg
->size
= size
& (PAGE_CACHE_SIZE
- 1);
1103 /* Got truncated off completely */
1107 req
->in
.args
[1].size
= inarg
->size
;
1109 fuse_request_send_background_locked(fc
, req
);
1113 fuse_writepage_finish(fc
, req
);
1114 spin_unlock(&fc
->lock
);
1115 fuse_writepage_free(fc
, req
);
1116 fuse_put_request(fc
, req
);
1117 spin_lock(&fc
->lock
);
1121 * If fi->writectr is positive (no truncate or fsync going on) send
1122 * all queued writepage requests.
1124 * Called with fc->lock
1126 void fuse_flush_writepages(struct inode
*inode
)
1127 __releases(&fc
->lock
)
1128 __acquires(&fc
->lock
)
1130 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1131 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1132 struct fuse_req
*req
;
1134 while (fi
->writectr
>= 0 && !list_empty(&fi
->queued_writes
)) {
1135 req
= list_entry(fi
->queued_writes
.next
, struct fuse_req
, list
);
1136 list_del_init(&req
->list
);
1137 fuse_send_writepage(fc
, req
);
1141 static void fuse_writepage_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
1143 struct inode
*inode
= req
->inode
;
1144 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1146 mapping_set_error(inode
->i_mapping
, req
->out
.h
.error
);
1147 spin_lock(&fc
->lock
);
1149 fuse_writepage_finish(fc
, req
);
1150 spin_unlock(&fc
->lock
);
1151 fuse_writepage_free(fc
, req
);
1154 static int fuse_writepage_locked(struct page
*page
)
1156 struct address_space
*mapping
= page
->mapping
;
1157 struct inode
*inode
= mapping
->host
;
1158 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1159 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1160 struct fuse_req
*req
;
1161 struct fuse_file
*ff
;
1162 struct page
*tmp_page
;
1164 set_page_writeback(page
);
1166 req
= fuse_request_alloc_nofs();
1170 tmp_page
= alloc_page(GFP_NOFS
| __GFP_HIGHMEM
);
1174 spin_lock(&fc
->lock
);
1175 BUG_ON(list_empty(&fi
->write_files
));
1176 ff
= list_entry(fi
->write_files
.next
, struct fuse_file
, write_entry
);
1177 req
->ff
= fuse_file_get(ff
);
1178 spin_unlock(&fc
->lock
);
1180 fuse_write_fill(req
, NULL
, ff
, inode
, page_offset(page
), 0, 1);
1182 copy_highpage(tmp_page
, page
);
1183 req
->in
.argpages
= 1;
1185 req
->pages
[0] = tmp_page
;
1186 req
->page_offset
= 0;
1187 req
->end
= fuse_writepage_end
;
1190 inc_bdi_stat(mapping
->backing_dev_info
, BDI_WRITEBACK
);
1191 inc_zone_page_state(tmp_page
, NR_WRITEBACK_TEMP
);
1192 end_page_writeback(page
);
1194 spin_lock(&fc
->lock
);
1195 list_add(&req
->writepages_entry
, &fi
->writepages
);
1196 list_add_tail(&req
->list
, &fi
->queued_writes
);
1197 fuse_flush_writepages(inode
);
1198 spin_unlock(&fc
->lock
);
1203 fuse_request_free(req
);
1205 end_page_writeback(page
);
1209 static int fuse_writepage(struct page
*page
, struct writeback_control
*wbc
)
1213 err
= fuse_writepage_locked(page
);
1219 static int fuse_launder_page(struct page
*page
)
1222 if (clear_page_dirty_for_io(page
)) {
1223 struct inode
*inode
= page
->mapping
->host
;
1224 err
= fuse_writepage_locked(page
);
1226 fuse_wait_on_page_writeback(inode
, page
->index
);
1232 * Write back dirty pages now, because there may not be any suitable
1235 static void fuse_vma_close(struct vm_area_struct
*vma
)
1237 filemap_write_and_wait(vma
->vm_file
->f_mapping
);
1241 * Wait for writeback against this page to complete before allowing it
1242 * to be marked dirty again, and hence written back again, possibly
1243 * before the previous writepage completed.
1245 * Block here, instead of in ->writepage(), so that the userspace fs
1246 * can only block processes actually operating on the filesystem.
1248 * Otherwise unprivileged userspace fs would be able to block
1253 * - try_to_free_pages() with order > PAGE_ALLOC_COSTLY_ORDER
1255 static int fuse_page_mkwrite(struct vm_area_struct
*vma
, struct vm_fault
*vmf
)
1257 struct page
*page
= vmf
->page
;
1259 * Don't use page->mapping as it may become NULL from a
1260 * concurrent truncate.
1262 struct inode
*inode
= vma
->vm_file
->f_mapping
->host
;
1264 fuse_wait_on_page_writeback(inode
, page
->index
);
1268 static struct vm_operations_struct fuse_file_vm_ops
= {
1269 .close
= fuse_vma_close
,
1270 .fault
= filemap_fault
,
1271 .page_mkwrite
= fuse_page_mkwrite
,
1274 static int fuse_file_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1276 if ((vma
->vm_flags
& VM_SHARED
) && (vma
->vm_flags
& VM_MAYWRITE
)) {
1277 struct inode
*inode
= file
->f_dentry
->d_inode
;
1278 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1279 struct fuse_inode
*fi
= get_fuse_inode(inode
);
1280 struct fuse_file
*ff
= file
->private_data
;
1282 * file may be written through mmap, so chain it onto the
1283 * inodes's write_file list
1285 spin_lock(&fc
->lock
);
1286 if (list_empty(&ff
->write_entry
))
1287 list_add(&ff
->write_entry
, &fi
->write_files
);
1288 spin_unlock(&fc
->lock
);
1290 file_accessed(file
);
1291 vma
->vm_ops
= &fuse_file_vm_ops
;
1295 static int fuse_direct_mmap(struct file
*file
, struct vm_area_struct
*vma
)
1297 /* Can't provide the coherency needed for MAP_SHARED */
1298 if (vma
->vm_flags
& VM_MAYSHARE
)
1301 invalidate_inode_pages2(file
->f_mapping
);
1303 return generic_file_mmap(file
, vma
);
1306 static int convert_fuse_file_lock(const struct fuse_file_lock
*ffl
,
1307 struct file_lock
*fl
)
1309 switch (ffl
->type
) {
1315 if (ffl
->start
> OFFSET_MAX
|| ffl
->end
> OFFSET_MAX
||
1316 ffl
->end
< ffl
->start
)
1319 fl
->fl_start
= ffl
->start
;
1320 fl
->fl_end
= ffl
->end
;
1321 fl
->fl_pid
= ffl
->pid
;
1327 fl
->fl_type
= ffl
->type
;
1331 static void fuse_lk_fill(struct fuse_req
*req
, struct file
*file
,
1332 const struct file_lock
*fl
, int opcode
, pid_t pid
,
1335 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1336 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1337 struct fuse_file
*ff
= file
->private_data
;
1338 struct fuse_lk_in
*arg
= &req
->misc
.lk_in
;
1341 arg
->owner
= fuse_lock_owner_id(fc
, fl
->fl_owner
);
1342 arg
->lk
.start
= fl
->fl_start
;
1343 arg
->lk
.end
= fl
->fl_end
;
1344 arg
->lk
.type
= fl
->fl_type
;
1347 arg
->lk_flags
|= FUSE_LK_FLOCK
;
1348 req
->in
.h
.opcode
= opcode
;
1349 req
->in
.h
.nodeid
= get_node_id(inode
);
1350 req
->in
.numargs
= 1;
1351 req
->in
.args
[0].size
= sizeof(*arg
);
1352 req
->in
.args
[0].value
= arg
;
1355 static int fuse_getlk(struct file
*file
, struct file_lock
*fl
)
1357 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1358 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1359 struct fuse_req
*req
;
1360 struct fuse_lk_out outarg
;
1363 req
= fuse_get_req(fc
);
1365 return PTR_ERR(req
);
1367 fuse_lk_fill(req
, file
, fl
, FUSE_GETLK
, 0, 0);
1368 req
->out
.numargs
= 1;
1369 req
->out
.args
[0].size
= sizeof(outarg
);
1370 req
->out
.args
[0].value
= &outarg
;
1371 fuse_request_send(fc
, req
);
1372 err
= req
->out
.h
.error
;
1373 fuse_put_request(fc
, req
);
1375 err
= convert_fuse_file_lock(&outarg
.lk
, fl
);
1380 static int fuse_setlk(struct file
*file
, struct file_lock
*fl
, int flock
)
1382 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1383 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1384 struct fuse_req
*req
;
1385 int opcode
= (fl
->fl_flags
& FL_SLEEP
) ? FUSE_SETLKW
: FUSE_SETLK
;
1386 pid_t pid
= fl
->fl_type
!= F_UNLCK
? current
->tgid
: 0;
1389 if (fl
->fl_lmops
&& fl
->fl_lmops
->fl_grant
) {
1390 /* NLM needs asynchronous locks, which we don't support yet */
1394 /* Unlock on close is handled by the flush method */
1395 if (fl
->fl_flags
& FL_CLOSE
)
1398 req
= fuse_get_req(fc
);
1400 return PTR_ERR(req
);
1402 fuse_lk_fill(req
, file
, fl
, opcode
, pid
, flock
);
1403 fuse_request_send(fc
, req
);
1404 err
= req
->out
.h
.error
;
1405 /* locking is restartable */
1408 fuse_put_request(fc
, req
);
1412 static int fuse_file_lock(struct file
*file
, int cmd
, struct file_lock
*fl
)
1414 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1415 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1418 if (cmd
== F_CANCELLK
) {
1420 } else if (cmd
== F_GETLK
) {
1422 posix_test_lock(file
, fl
);
1425 err
= fuse_getlk(file
, fl
);
1428 err
= posix_lock_file(file
, fl
, NULL
);
1430 err
= fuse_setlk(file
, fl
, 0);
1435 static int fuse_file_flock(struct file
*file
, int cmd
, struct file_lock
*fl
)
1437 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1438 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1442 err
= flock_lock_file_wait(file
, fl
);
1444 /* emulate flock with POSIX locks */
1445 fl
->fl_owner
= (fl_owner_t
) file
;
1446 err
= fuse_setlk(file
, fl
, 1);
1452 static sector_t
fuse_bmap(struct address_space
*mapping
, sector_t block
)
1454 struct inode
*inode
= mapping
->host
;
1455 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1456 struct fuse_req
*req
;
1457 struct fuse_bmap_in inarg
;
1458 struct fuse_bmap_out outarg
;
1461 if (!inode
->i_sb
->s_bdev
|| fc
->no_bmap
)
1464 req
= fuse_get_req(fc
);
1468 memset(&inarg
, 0, sizeof(inarg
));
1469 inarg
.block
= block
;
1470 inarg
.blocksize
= inode
->i_sb
->s_blocksize
;
1471 req
->in
.h
.opcode
= FUSE_BMAP
;
1472 req
->in
.h
.nodeid
= get_node_id(inode
);
1473 req
->in
.numargs
= 1;
1474 req
->in
.args
[0].size
= sizeof(inarg
);
1475 req
->in
.args
[0].value
= &inarg
;
1476 req
->out
.numargs
= 1;
1477 req
->out
.args
[0].size
= sizeof(outarg
);
1478 req
->out
.args
[0].value
= &outarg
;
1479 fuse_request_send(fc
, req
);
1480 err
= req
->out
.h
.error
;
1481 fuse_put_request(fc
, req
);
1485 return err
? 0 : outarg
.block
;
1488 static loff_t
fuse_file_llseek(struct file
*file
, loff_t offset
, int origin
)
1491 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
1493 mutex_lock(&inode
->i_mutex
);
1496 retval
= fuse_update_attributes(inode
, NULL
, file
, NULL
);
1499 offset
+= i_size_read(inode
);
1502 offset
+= file
->f_pos
;
1505 if (offset
>= 0 && offset
<= inode
->i_sb
->s_maxbytes
) {
1506 if (offset
!= file
->f_pos
) {
1507 file
->f_pos
= offset
;
1508 file
->f_version
= 0;
1513 mutex_unlock(&inode
->i_mutex
);
1517 static int fuse_ioctl_copy_user(struct page
**pages
, struct iovec
*iov
,
1518 unsigned int nr_segs
, size_t bytes
, bool to_user
)
1526 iov_iter_init(&ii
, iov
, nr_segs
, bytes
, 0);
1528 while (iov_iter_count(&ii
)) {
1529 struct page
*page
= pages
[page_idx
++];
1530 size_t todo
= min_t(size_t, PAGE_SIZE
, iov_iter_count(&ii
));
1533 kaddr
= map
= kmap(page
);
1536 char __user
*uaddr
= ii
.iov
->iov_base
+ ii
.iov_offset
;
1537 size_t iov_len
= ii
.iov
->iov_len
- ii
.iov_offset
;
1538 size_t copy
= min(todo
, iov_len
);
1542 left
= copy_from_user(kaddr
, uaddr
, copy
);
1544 left
= copy_to_user(uaddr
, kaddr
, copy
);
1549 iov_iter_advance(&ii
, copy
);
1561 * For ioctls, there is no generic way to determine how much memory
1562 * needs to be read and/or written. Furthermore, ioctls are allowed
1563 * to dereference the passed pointer, so the parameter requires deep
1564 * copying but FUSE has no idea whatsoever about what to copy in or
1567 * This is solved by allowing FUSE server to retry ioctl with
1568 * necessary in/out iovecs. Let's assume the ioctl implementation
1569 * needs to read in the following structure.
1576 * On the first callout to FUSE server, inarg->in_size and
1577 * inarg->out_size will be NULL; then, the server completes the ioctl
1578 * with FUSE_IOCTL_RETRY set in out->flags, out->in_iovs set to 1 and
1579 * the actual iov array to
1581 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) } }
1583 * which tells FUSE to copy in the requested area and retry the ioctl.
1584 * On the second round, the server has access to the structure and
1585 * from that it can tell what to look for next, so on the invocation,
1586 * it sets FUSE_IOCTL_RETRY, out->in_iovs to 2 and iov array to
1588 * { { .iov_base = inarg.arg, .iov_len = sizeof(struct a) },
1589 * { .iov_base = a.buf, .iov_len = a.buflen } }
1591 * FUSE will copy both struct a and the pointed buffer from the
1592 * process doing the ioctl and retry ioctl with both struct a and the
1595 * This time, FUSE server has everything it needs and completes ioctl
1596 * without FUSE_IOCTL_RETRY which finishes the ioctl call.
1598 * Copying data out works the same way.
1600 * Note that if FUSE_IOCTL_UNRESTRICTED is clear, the kernel
1601 * automatically initializes in and out iovs by decoding @cmd with
1602 * _IOC_* macros and the server is not allowed to request RETRY. This
1603 * limits ioctl data transfers to well-formed ioctls and is the forced
1604 * behavior for all FUSE servers.
1606 static long fuse_file_do_ioctl(struct file
*file
, unsigned int cmd
,
1607 unsigned long arg
, unsigned int flags
)
1609 struct inode
*inode
= file
->f_dentry
->d_inode
;
1610 struct fuse_file
*ff
= file
->private_data
;
1611 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1612 struct fuse_ioctl_in inarg
= {
1618 struct fuse_ioctl_out outarg
;
1619 struct fuse_req
*req
= NULL
;
1620 struct page
**pages
= NULL
;
1621 struct page
*iov_page
= NULL
;
1622 struct iovec
*in_iov
= NULL
, *out_iov
= NULL
;
1623 unsigned int in_iovs
= 0, out_iovs
= 0, num_pages
= 0, max_pages
;
1624 size_t in_size
, out_size
, transferred
;
1627 /* assume all the iovs returned by client always fits in a page */
1628 BUILD_BUG_ON(sizeof(struct iovec
) * FUSE_IOCTL_MAX_IOV
> PAGE_SIZE
);
1630 if (!fuse_allow_task(fc
, current
))
1634 if (is_bad_inode(inode
))
1638 pages
= kzalloc(sizeof(pages
[0]) * FUSE_MAX_PAGES_PER_REQ
, GFP_KERNEL
);
1639 iov_page
= alloc_page(GFP_KERNEL
);
1640 if (!pages
|| !iov_page
)
1644 * If restricted, initialize IO parameters as encoded in @cmd.
1645 * RETRY from server is not allowed.
1647 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
)) {
1648 struct iovec
*iov
= page_address(iov_page
);
1650 iov
->iov_base
= (void __user
*)arg
;
1651 iov
->iov_len
= _IOC_SIZE(cmd
);
1653 if (_IOC_DIR(cmd
) & _IOC_WRITE
) {
1658 if (_IOC_DIR(cmd
) & _IOC_READ
) {
1665 inarg
.in_size
= in_size
= iov_length(in_iov
, in_iovs
);
1666 inarg
.out_size
= out_size
= iov_length(out_iov
, out_iovs
);
1669 * Out data can be used either for actual out data or iovs,
1670 * make sure there always is at least one page.
1672 out_size
= max_t(size_t, out_size
, PAGE_SIZE
);
1673 max_pages
= DIV_ROUND_UP(max(in_size
, out_size
), PAGE_SIZE
);
1675 /* make sure there are enough buffer pages and init request with them */
1677 if (max_pages
> FUSE_MAX_PAGES_PER_REQ
)
1679 while (num_pages
< max_pages
) {
1680 pages
[num_pages
] = alloc_page(GFP_KERNEL
| __GFP_HIGHMEM
);
1681 if (!pages
[num_pages
])
1686 req
= fuse_get_req(fc
);
1692 memcpy(req
->pages
, pages
, sizeof(req
->pages
[0]) * num_pages
);
1693 req
->num_pages
= num_pages
;
1695 /* okay, let's send it to the client */
1696 req
->in
.h
.opcode
= FUSE_IOCTL
;
1697 req
->in
.h
.nodeid
= get_node_id(inode
);
1698 req
->in
.numargs
= 1;
1699 req
->in
.args
[0].size
= sizeof(inarg
);
1700 req
->in
.args
[0].value
= &inarg
;
1703 req
->in
.args
[1].size
= in_size
;
1704 req
->in
.argpages
= 1;
1706 err
= fuse_ioctl_copy_user(pages
, in_iov
, in_iovs
, in_size
,
1712 req
->out
.numargs
= 2;
1713 req
->out
.args
[0].size
= sizeof(outarg
);
1714 req
->out
.args
[0].value
= &outarg
;
1715 req
->out
.args
[1].size
= out_size
;
1716 req
->out
.argpages
= 1;
1717 req
->out
.argvar
= 1;
1719 fuse_request_send(fc
, req
);
1720 err
= req
->out
.h
.error
;
1721 transferred
= req
->out
.args
[1].size
;
1722 fuse_put_request(fc
, req
);
1727 /* did it ask for retry? */
1728 if (outarg
.flags
& FUSE_IOCTL_RETRY
) {
1731 /* no retry if in restricted mode */
1733 if (!(flags
& FUSE_IOCTL_UNRESTRICTED
))
1736 in_iovs
= outarg
.in_iovs
;
1737 out_iovs
= outarg
.out_iovs
;
1740 * Make sure things are in boundary, separate checks
1741 * are to protect against overflow.
1744 if (in_iovs
> FUSE_IOCTL_MAX_IOV
||
1745 out_iovs
> FUSE_IOCTL_MAX_IOV
||
1746 in_iovs
+ out_iovs
> FUSE_IOCTL_MAX_IOV
)
1750 if ((in_iovs
+ out_iovs
) * sizeof(struct iovec
) != transferred
)
1753 /* okay, copy in iovs and retry */
1754 vaddr
= kmap_atomic(pages
[0], KM_USER0
);
1755 memcpy(page_address(iov_page
), vaddr
, transferred
);
1756 kunmap_atomic(vaddr
, KM_USER0
);
1758 in_iov
= page_address(iov_page
);
1759 out_iov
= in_iov
+ in_iovs
;
1765 if (transferred
> inarg
.out_size
)
1768 err
= fuse_ioctl_copy_user(pages
, out_iov
, out_iovs
, transferred
, true);
1771 fuse_put_request(fc
, req
);
1773 __free_page(iov_page
);
1775 __free_page(pages
[--num_pages
]);
1778 return err
? err
: outarg
.result
;
1781 static long fuse_file_ioctl(struct file
*file
, unsigned int cmd
,
1784 return fuse_file_do_ioctl(file
, cmd
, arg
, 0);
1787 static long fuse_file_compat_ioctl(struct file
*file
, unsigned int cmd
,
1790 return fuse_file_do_ioctl(file
, cmd
, arg
, FUSE_IOCTL_COMPAT
);
1794 * All files which have been polled are linked to RB tree
1795 * fuse_conn->polled_files which is indexed by kh. Walk the tree and
1796 * find the matching one.
1798 static struct rb_node
**fuse_find_polled_node(struct fuse_conn
*fc
, u64 kh
,
1799 struct rb_node
**parent_out
)
1801 struct rb_node
**link
= &fc
->polled_files
.rb_node
;
1802 struct rb_node
*last
= NULL
;
1805 struct fuse_file
*ff
;
1808 ff
= rb_entry(last
, struct fuse_file
, polled_node
);
1811 link
= &last
->rb_left
;
1812 else if (kh
> ff
->kh
)
1813 link
= &last
->rb_right
;
1824 * The file is about to be polled. Make sure it's on the polled_files
1825 * RB tree. Note that files once added to the polled_files tree are
1826 * not removed before the file is released. This is because a file
1827 * polled once is likely to be polled again.
1829 static void fuse_register_polled_file(struct fuse_conn
*fc
,
1830 struct fuse_file
*ff
)
1832 spin_lock(&fc
->lock
);
1833 if (RB_EMPTY_NODE(&ff
->polled_node
)) {
1834 struct rb_node
**link
, *parent
;
1836 link
= fuse_find_polled_node(fc
, ff
->kh
, &parent
);
1838 rb_link_node(&ff
->polled_node
, parent
, link
);
1839 rb_insert_color(&ff
->polled_node
, &fc
->polled_files
);
1841 spin_unlock(&fc
->lock
);
1844 static unsigned fuse_file_poll(struct file
*file
, poll_table
*wait
)
1846 struct inode
*inode
= file
->f_dentry
->d_inode
;
1847 struct fuse_file
*ff
= file
->private_data
;
1848 struct fuse_conn
*fc
= get_fuse_conn(inode
);
1849 struct fuse_poll_in inarg
= { .fh
= ff
->fh
, .kh
= ff
->kh
};
1850 struct fuse_poll_out outarg
;
1851 struct fuse_req
*req
;
1855 return DEFAULT_POLLMASK
;
1857 poll_wait(file
, &ff
->poll_wait
, wait
);
1860 * Ask for notification iff there's someone waiting for it.
1861 * The client may ignore the flag and always notify.
1863 if (waitqueue_active(&ff
->poll_wait
)) {
1864 inarg
.flags
|= FUSE_POLL_SCHEDULE_NOTIFY
;
1865 fuse_register_polled_file(fc
, ff
);
1868 req
= fuse_get_req(fc
);
1870 return PTR_ERR(req
);
1872 req
->in
.h
.opcode
= FUSE_POLL
;
1873 req
->in
.h
.nodeid
= get_node_id(inode
);
1874 req
->in
.numargs
= 1;
1875 req
->in
.args
[0].size
= sizeof(inarg
);
1876 req
->in
.args
[0].value
= &inarg
;
1877 req
->out
.numargs
= 1;
1878 req
->out
.args
[0].size
= sizeof(outarg
);
1879 req
->out
.args
[0].value
= &outarg
;
1880 fuse_request_send(fc
, req
);
1881 err
= req
->out
.h
.error
;
1882 fuse_put_request(fc
, req
);
1885 return outarg
.revents
;
1886 if (err
== -ENOSYS
) {
1888 return DEFAULT_POLLMASK
;
1894 * This is called from fuse_handle_notify() on FUSE_NOTIFY_POLL and
1895 * wakes up the poll waiters.
1897 int fuse_notify_poll_wakeup(struct fuse_conn
*fc
,
1898 struct fuse_notify_poll_wakeup_out
*outarg
)
1900 u64 kh
= outarg
->kh
;
1901 struct rb_node
**link
;
1903 spin_lock(&fc
->lock
);
1905 link
= fuse_find_polled_node(fc
, kh
, NULL
);
1907 struct fuse_file
*ff
;
1909 ff
= rb_entry(*link
, struct fuse_file
, polled_node
);
1910 wake_up_interruptible_sync(&ff
->poll_wait
);
1913 spin_unlock(&fc
->lock
);
1917 static const struct file_operations fuse_file_operations
= {
1918 .llseek
= fuse_file_llseek
,
1919 .read
= do_sync_read
,
1920 .aio_read
= fuse_file_aio_read
,
1921 .write
= do_sync_write
,
1922 .aio_write
= fuse_file_aio_write
,
1923 .mmap
= fuse_file_mmap
,
1925 .flush
= fuse_flush
,
1926 .release
= fuse_release
,
1927 .fsync
= fuse_fsync
,
1928 .lock
= fuse_file_lock
,
1929 .flock
= fuse_file_flock
,
1930 .splice_read
= generic_file_splice_read
,
1931 .unlocked_ioctl
= fuse_file_ioctl
,
1932 .compat_ioctl
= fuse_file_compat_ioctl
,
1933 .poll
= fuse_file_poll
,
1936 static const struct file_operations fuse_direct_io_file_operations
= {
1937 .llseek
= fuse_file_llseek
,
1938 .read
= fuse_direct_read
,
1939 .write
= fuse_direct_write
,
1940 .mmap
= fuse_direct_mmap
,
1942 .flush
= fuse_flush
,
1943 .release
= fuse_release
,
1944 .fsync
= fuse_fsync
,
1945 .lock
= fuse_file_lock
,
1946 .flock
= fuse_file_flock
,
1947 .unlocked_ioctl
= fuse_file_ioctl
,
1948 .compat_ioctl
= fuse_file_compat_ioctl
,
1949 .poll
= fuse_file_poll
,
1950 /* no splice_read */
1953 static const struct address_space_operations fuse_file_aops
= {
1954 .readpage
= fuse_readpage
,
1955 .writepage
= fuse_writepage
,
1956 .launder_page
= fuse_launder_page
,
1957 .write_begin
= fuse_write_begin
,
1958 .write_end
= fuse_write_end
,
1959 .readpages
= fuse_readpages
,
1960 .set_page_dirty
= __set_page_dirty_nobuffers
,
1964 void fuse_init_file_inode(struct inode
*inode
)
1966 inode
->i_fop
= &fuse_file_operations
;
1967 inode
->i_data
.a_ops
= &fuse_file_aops
;