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/init.h>
12 #include <linux/module.h>
13 #include <linux/poll.h>
14 #include <linux/sched/signal.h>
15 #include <linux/uio.h>
16 #include <linux/miscdevice.h>
17 #include <linux/pagemap.h>
18 #include <linux/file.h>
19 #include <linux/slab.h>
20 #include <linux/pipe_fs_i.h>
21 #include <linux/swap.h>
22 #include <linux/splice.h>
23 #include <linux/sched.h>
24 #include <linux/freezer.h>
26 MODULE_ALIAS_MISCDEV(FUSE_MINOR
);
27 MODULE_ALIAS("devname:fuse");
29 static struct kmem_cache
*fuse_req_cachep
;
31 static struct fuse_dev
*fuse_get_dev(struct file
*file
)
34 * Lockless access is OK, because file->private data is set
35 * once during mount and is valid until the file is released.
37 return ACCESS_ONCE(file
->private_data
);
40 static void fuse_request_init(struct fuse_req
*req
, struct page
**pages
,
41 struct fuse_page_desc
*page_descs
,
44 memset(req
, 0, sizeof(*req
));
45 memset(pages
, 0, sizeof(*pages
) * npages
);
46 memset(page_descs
, 0, sizeof(*page_descs
) * npages
);
47 INIT_LIST_HEAD(&req
->list
);
48 INIT_LIST_HEAD(&req
->intr_entry
);
49 init_waitqueue_head(&req
->waitq
);
50 refcount_set(&req
->count
, 1);
52 req
->page_descs
= page_descs
;
53 req
->max_pages
= npages
;
54 __set_bit(FR_PENDING
, &req
->flags
);
57 static struct fuse_req
*__fuse_request_alloc(unsigned npages
, gfp_t flags
)
59 struct fuse_req
*req
= kmem_cache_alloc(fuse_req_cachep
, flags
);
62 struct fuse_page_desc
*page_descs
;
64 if (npages
<= FUSE_REQ_INLINE_PAGES
) {
65 pages
= req
->inline_pages
;
66 page_descs
= req
->inline_page_descs
;
68 pages
= kmalloc(sizeof(struct page
*) * npages
, flags
);
69 page_descs
= kmalloc(sizeof(struct fuse_page_desc
) *
73 if (!pages
|| !page_descs
) {
76 kmem_cache_free(fuse_req_cachep
, req
);
80 fuse_request_init(req
, pages
, page_descs
, npages
);
85 struct fuse_req
*fuse_request_alloc(unsigned npages
)
87 return __fuse_request_alloc(npages
, GFP_KERNEL
);
89 EXPORT_SYMBOL_GPL(fuse_request_alloc
);
91 struct fuse_req
*fuse_request_alloc_nofs(unsigned npages
)
93 return __fuse_request_alloc(npages
, GFP_NOFS
);
96 void fuse_request_free(struct fuse_req
*req
)
98 if (req
->pages
!= req
->inline_pages
) {
100 kfree(req
->page_descs
);
102 kmem_cache_free(fuse_req_cachep
, req
);
105 void __fuse_get_request(struct fuse_req
*req
)
107 refcount_inc(&req
->count
);
110 /* Must be called with > 1 refcount */
111 static void __fuse_put_request(struct fuse_req
*req
)
113 refcount_dec(&req
->count
);
116 static void fuse_req_init_context(struct fuse_conn
*fc
, struct fuse_req
*req
)
118 req
->in
.h
.uid
= from_kuid_munged(&init_user_ns
, current_fsuid());
119 req
->in
.h
.gid
= from_kgid_munged(&init_user_ns
, current_fsgid());
120 req
->in
.h
.pid
= pid_nr_ns(task_pid(current
), fc
->pid_ns
);
123 void fuse_set_initialized(struct fuse_conn
*fc
)
125 /* Make sure stores before this are seen on another CPU */
130 static bool fuse_block_alloc(struct fuse_conn
*fc
, bool for_background
)
132 return !fc
->initialized
|| (for_background
&& fc
->blocked
);
135 static struct fuse_req
*__fuse_get_req(struct fuse_conn
*fc
, unsigned npages
,
138 struct fuse_req
*req
;
140 atomic_inc(&fc
->num_waiting
);
142 if (fuse_block_alloc(fc
, for_background
)) {
144 if (wait_event_killable_exclusive(fc
->blocked_waitq
,
145 !fuse_block_alloc(fc
, for_background
)))
148 /* Matches smp_wmb() in fuse_set_initialized() */
159 req
= fuse_request_alloc(npages
);
163 wake_up(&fc
->blocked_waitq
);
167 fuse_req_init_context(fc
, req
);
168 __set_bit(FR_WAITING
, &req
->flags
);
170 __set_bit(FR_BACKGROUND
, &req
->flags
);
175 atomic_dec(&fc
->num_waiting
);
179 struct fuse_req
*fuse_get_req(struct fuse_conn
*fc
, unsigned npages
)
181 return __fuse_get_req(fc
, npages
, false);
183 EXPORT_SYMBOL_GPL(fuse_get_req
);
185 struct fuse_req
*fuse_get_req_for_background(struct fuse_conn
*fc
,
188 return __fuse_get_req(fc
, npages
, true);
190 EXPORT_SYMBOL_GPL(fuse_get_req_for_background
);
193 * Return request in fuse_file->reserved_req. However that may
194 * currently be in use. If that is the case, wait for it to become
197 static struct fuse_req
*get_reserved_req(struct fuse_conn
*fc
,
200 struct fuse_req
*req
= NULL
;
201 struct fuse_file
*ff
= file
->private_data
;
204 wait_event(fc
->reserved_req_waitq
, ff
->reserved_req
);
205 spin_lock(&fc
->lock
);
206 if (ff
->reserved_req
) {
207 req
= ff
->reserved_req
;
208 ff
->reserved_req
= NULL
;
209 req
->stolen_file
= get_file(file
);
211 spin_unlock(&fc
->lock
);
218 * Put stolen request back into fuse_file->reserved_req
220 static void put_reserved_req(struct fuse_conn
*fc
, struct fuse_req
*req
)
222 struct file
*file
= req
->stolen_file
;
223 struct fuse_file
*ff
= file
->private_data
;
225 spin_lock(&fc
->lock
);
226 fuse_request_init(req
, req
->pages
, req
->page_descs
, req
->max_pages
);
227 BUG_ON(ff
->reserved_req
);
228 ff
->reserved_req
= req
;
229 wake_up_all(&fc
->reserved_req_waitq
);
230 spin_unlock(&fc
->lock
);
235 * Gets a requests for a file operation, always succeeds
237 * This is used for sending the FLUSH request, which must get to
238 * userspace, due to POSIX locks which may need to be unlocked.
240 * If allocation fails due to OOM, use the reserved request in
243 * This is very unlikely to deadlock accidentally, since the
244 * filesystem should not have it's own file open. If deadlock is
245 * intentional, it can still be broken by "aborting" the filesystem.
247 struct fuse_req
*fuse_get_req_nofail_nopages(struct fuse_conn
*fc
,
250 struct fuse_req
*req
;
252 atomic_inc(&fc
->num_waiting
);
253 wait_event(fc
->blocked_waitq
, fc
->initialized
);
254 /* Matches smp_wmb() in fuse_set_initialized() */
256 req
= fuse_request_alloc(0);
258 req
= get_reserved_req(fc
, file
);
260 fuse_req_init_context(fc
, req
);
261 __set_bit(FR_WAITING
, &req
->flags
);
262 __clear_bit(FR_BACKGROUND
, &req
->flags
);
266 void fuse_put_request(struct fuse_conn
*fc
, struct fuse_req
*req
)
268 if (refcount_dec_and_test(&req
->count
)) {
269 if (test_bit(FR_BACKGROUND
, &req
->flags
)) {
271 * We get here in the unlikely case that a background
272 * request was allocated but not sent
274 spin_lock(&fc
->lock
);
276 wake_up(&fc
->blocked_waitq
);
277 spin_unlock(&fc
->lock
);
280 if (test_bit(FR_WAITING
, &req
->flags
)) {
281 __clear_bit(FR_WAITING
, &req
->flags
);
282 atomic_dec(&fc
->num_waiting
);
285 if (req
->stolen_file
)
286 put_reserved_req(fc
, req
);
288 fuse_request_free(req
);
291 EXPORT_SYMBOL_GPL(fuse_put_request
);
293 static unsigned len_args(unsigned numargs
, struct fuse_arg
*args
)
298 for (i
= 0; i
< numargs
; i
++)
299 nbytes
+= args
[i
].size
;
304 static u64
fuse_get_unique(struct fuse_iqueue
*fiq
)
306 return ++fiq
->reqctr
;
309 static void queue_request(struct fuse_iqueue
*fiq
, struct fuse_req
*req
)
311 req
->in
.h
.len
= sizeof(struct fuse_in_header
) +
312 len_args(req
->in
.numargs
, (struct fuse_arg
*) req
->in
.args
);
313 list_add_tail(&req
->list
, &fiq
->pending
);
314 wake_up_locked(&fiq
->waitq
);
315 kill_fasync(&fiq
->fasync
, SIGIO
, POLL_IN
);
318 void fuse_queue_forget(struct fuse_conn
*fc
, struct fuse_forget_link
*forget
,
319 u64 nodeid
, u64 nlookup
)
321 struct fuse_iqueue
*fiq
= &fc
->iq
;
323 forget
->forget_one
.nodeid
= nodeid
;
324 forget
->forget_one
.nlookup
= nlookup
;
326 spin_lock(&fiq
->waitq
.lock
);
327 if (fiq
->connected
) {
328 fiq
->forget_list_tail
->next
= forget
;
329 fiq
->forget_list_tail
= forget
;
330 wake_up_locked(&fiq
->waitq
);
331 kill_fasync(&fiq
->fasync
, SIGIO
, POLL_IN
);
335 spin_unlock(&fiq
->waitq
.lock
);
338 static void flush_bg_queue(struct fuse_conn
*fc
)
340 while (fc
->active_background
< fc
->max_background
&&
341 !list_empty(&fc
->bg_queue
)) {
342 struct fuse_req
*req
;
343 struct fuse_iqueue
*fiq
= &fc
->iq
;
345 req
= list_entry(fc
->bg_queue
.next
, struct fuse_req
, list
);
346 list_del(&req
->list
);
347 fc
->active_background
++;
348 spin_lock(&fiq
->waitq
.lock
);
349 req
->in
.h
.unique
= fuse_get_unique(fiq
);
350 queue_request(fiq
, req
);
351 spin_unlock(&fiq
->waitq
.lock
);
356 * This function is called when a request is finished. Either a reply
357 * has arrived or it was aborted (and not yet sent) or some error
358 * occurred during communication with userspace, or the device file
359 * was closed. The requester thread is woken up (if still waiting),
360 * the 'end' callback is called if given, else the reference to the
361 * request is released
363 static void request_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
365 struct fuse_iqueue
*fiq
= &fc
->iq
;
367 if (test_and_set_bit(FR_FINISHED
, &req
->flags
))
370 spin_lock(&fiq
->waitq
.lock
);
371 list_del_init(&req
->intr_entry
);
372 spin_unlock(&fiq
->waitq
.lock
);
373 WARN_ON(test_bit(FR_PENDING
, &req
->flags
));
374 WARN_ON(test_bit(FR_SENT
, &req
->flags
));
375 if (test_bit(FR_BACKGROUND
, &req
->flags
)) {
376 spin_lock(&fc
->lock
);
377 clear_bit(FR_BACKGROUND
, &req
->flags
);
378 if (fc
->num_background
== fc
->max_background
)
381 /* Wake up next waiter, if any */
382 if (!fc
->blocked
&& waitqueue_active(&fc
->blocked_waitq
))
383 wake_up(&fc
->blocked_waitq
);
385 if (fc
->num_background
== fc
->congestion_threshold
&&
386 fc
->connected
&& fc
->sb
) {
387 clear_bdi_congested(fc
->sb
->s_bdi
, BLK_RW_SYNC
);
388 clear_bdi_congested(fc
->sb
->s_bdi
, BLK_RW_ASYNC
);
390 fc
->num_background
--;
391 fc
->active_background
--;
393 spin_unlock(&fc
->lock
);
395 wake_up(&req
->waitq
);
398 fuse_put_request(fc
, req
);
401 static void queue_interrupt(struct fuse_iqueue
*fiq
, struct fuse_req
*req
)
403 spin_lock(&fiq
->waitq
.lock
);
404 if (test_bit(FR_FINISHED
, &req
->flags
)) {
405 spin_unlock(&fiq
->waitq
.lock
);
408 if (list_empty(&req
->intr_entry
)) {
409 list_add_tail(&req
->intr_entry
, &fiq
->interrupts
);
410 wake_up_locked(&fiq
->waitq
);
412 spin_unlock(&fiq
->waitq
.lock
);
413 kill_fasync(&fiq
->fasync
, SIGIO
, POLL_IN
);
416 static void request_wait_answer(struct fuse_conn
*fc
, struct fuse_req
*req
)
418 struct fuse_iqueue
*fiq
= &fc
->iq
;
421 if (!fc
->no_interrupt
) {
422 /* Any signal may interrupt this */
423 err
= wait_event_interruptible(req
->waitq
,
424 test_bit(FR_FINISHED
, &req
->flags
));
428 set_bit(FR_INTERRUPTED
, &req
->flags
);
429 /* matches barrier in fuse_dev_do_read() */
430 smp_mb__after_atomic();
431 if (test_bit(FR_SENT
, &req
->flags
))
432 queue_interrupt(fiq
, req
);
435 if (!test_bit(FR_FORCE
, &req
->flags
)) {
436 /* Only fatal signals may interrupt this */
437 err
= wait_event_killable(req
->waitq
,
438 test_bit(FR_FINISHED
, &req
->flags
));
442 spin_lock(&fiq
->waitq
.lock
);
443 /* Request is not yet in userspace, bail out */
444 if (test_bit(FR_PENDING
, &req
->flags
)) {
445 list_del(&req
->list
);
446 spin_unlock(&fiq
->waitq
.lock
);
447 __fuse_put_request(req
);
448 req
->out
.h
.error
= -EINTR
;
451 spin_unlock(&fiq
->waitq
.lock
);
455 * Either request is already in userspace, or it was forced.
458 while (!test_bit(FR_FINISHED
, &req
->flags
))
459 wait_event_freezable(req
->waitq
,
460 test_bit(FR_FINISHED
, &req
->flags
));
463 static void __fuse_request_send(struct fuse_conn
*fc
, struct fuse_req
*req
)
465 struct fuse_iqueue
*fiq
= &fc
->iq
;
467 BUG_ON(test_bit(FR_BACKGROUND
, &req
->flags
));
468 spin_lock(&fiq
->waitq
.lock
);
469 if (!fiq
->connected
) {
470 spin_unlock(&fiq
->waitq
.lock
);
471 req
->out
.h
.error
= -ENOTCONN
;
473 req
->in
.h
.unique
= fuse_get_unique(fiq
);
474 queue_request(fiq
, req
);
475 /* acquire extra reference, since request is still needed
476 after request_end() */
477 __fuse_get_request(req
);
478 spin_unlock(&fiq
->waitq
.lock
);
480 request_wait_answer(fc
, req
);
481 /* Pairs with smp_wmb() in request_end() */
486 void fuse_request_send(struct fuse_conn
*fc
, struct fuse_req
*req
)
488 __set_bit(FR_ISREPLY
, &req
->flags
);
489 if (!test_bit(FR_WAITING
, &req
->flags
)) {
490 __set_bit(FR_WAITING
, &req
->flags
);
491 atomic_inc(&fc
->num_waiting
);
493 __fuse_request_send(fc
, req
);
495 EXPORT_SYMBOL_GPL(fuse_request_send
);
497 static void fuse_adjust_compat(struct fuse_conn
*fc
, struct fuse_args
*args
)
499 if (fc
->minor
< 4 && args
->in
.h
.opcode
== FUSE_STATFS
)
500 args
->out
.args
[0].size
= FUSE_COMPAT_STATFS_SIZE
;
503 switch (args
->in
.h
.opcode
) {
510 args
->out
.args
[0].size
= FUSE_COMPAT_ENTRY_OUT_SIZE
;
514 args
->out
.args
[0].size
= FUSE_COMPAT_ATTR_OUT_SIZE
;
518 if (fc
->minor
< 12) {
519 switch (args
->in
.h
.opcode
) {
521 args
->in
.args
[0].size
= sizeof(struct fuse_open_in
);
524 args
->in
.args
[0].size
= FUSE_COMPAT_MKNOD_IN_SIZE
;
530 ssize_t
fuse_simple_request(struct fuse_conn
*fc
, struct fuse_args
*args
)
532 struct fuse_req
*req
;
535 req
= fuse_get_req(fc
, 0);
539 /* Needs to be done after fuse_get_req() so that fc->minor is valid */
540 fuse_adjust_compat(fc
, args
);
542 req
->in
.h
.opcode
= args
->in
.h
.opcode
;
543 req
->in
.h
.nodeid
= args
->in
.h
.nodeid
;
544 req
->in
.numargs
= args
->in
.numargs
;
545 memcpy(req
->in
.args
, args
->in
.args
,
546 args
->in
.numargs
* sizeof(struct fuse_in_arg
));
547 req
->out
.argvar
= args
->out
.argvar
;
548 req
->out
.numargs
= args
->out
.numargs
;
549 memcpy(req
->out
.args
, args
->out
.args
,
550 args
->out
.numargs
* sizeof(struct fuse_arg
));
551 fuse_request_send(fc
, req
);
552 ret
= req
->out
.h
.error
;
553 if (!ret
&& args
->out
.argvar
) {
554 BUG_ON(args
->out
.numargs
!= 1);
555 ret
= req
->out
.args
[0].size
;
557 fuse_put_request(fc
, req
);
563 * Called under fc->lock
565 * fc->connected must have been checked previously
567 void fuse_request_send_background_locked(struct fuse_conn
*fc
,
568 struct fuse_req
*req
)
570 BUG_ON(!test_bit(FR_BACKGROUND
, &req
->flags
));
571 if (!test_bit(FR_WAITING
, &req
->flags
)) {
572 __set_bit(FR_WAITING
, &req
->flags
);
573 atomic_inc(&fc
->num_waiting
);
575 __set_bit(FR_ISREPLY
, &req
->flags
);
576 fc
->num_background
++;
577 if (fc
->num_background
== fc
->max_background
)
579 if (fc
->num_background
== fc
->congestion_threshold
&& fc
->sb
) {
580 set_bdi_congested(fc
->sb
->s_bdi
, BLK_RW_SYNC
);
581 set_bdi_congested(fc
->sb
->s_bdi
, BLK_RW_ASYNC
);
583 list_add_tail(&req
->list
, &fc
->bg_queue
);
587 void fuse_request_send_background(struct fuse_conn
*fc
, struct fuse_req
*req
)
590 spin_lock(&fc
->lock
);
592 fuse_request_send_background_locked(fc
, req
);
593 spin_unlock(&fc
->lock
);
595 spin_unlock(&fc
->lock
);
596 req
->out
.h
.error
= -ENOTCONN
;
598 fuse_put_request(fc
, req
);
601 EXPORT_SYMBOL_GPL(fuse_request_send_background
);
603 static int fuse_request_send_notify_reply(struct fuse_conn
*fc
,
604 struct fuse_req
*req
, u64 unique
)
607 struct fuse_iqueue
*fiq
= &fc
->iq
;
609 __clear_bit(FR_ISREPLY
, &req
->flags
);
610 req
->in
.h
.unique
= unique
;
611 spin_lock(&fiq
->waitq
.lock
);
612 if (fiq
->connected
) {
613 queue_request(fiq
, req
);
616 spin_unlock(&fiq
->waitq
.lock
);
621 void fuse_force_forget(struct file
*file
, u64 nodeid
)
623 struct inode
*inode
= file_inode(file
);
624 struct fuse_conn
*fc
= get_fuse_conn(inode
);
625 struct fuse_req
*req
;
626 struct fuse_forget_in inarg
;
628 memset(&inarg
, 0, sizeof(inarg
));
630 req
= fuse_get_req_nofail_nopages(fc
, file
);
631 req
->in
.h
.opcode
= FUSE_FORGET
;
632 req
->in
.h
.nodeid
= nodeid
;
634 req
->in
.args
[0].size
= sizeof(inarg
);
635 req
->in
.args
[0].value
= &inarg
;
636 __clear_bit(FR_ISREPLY
, &req
->flags
);
637 __fuse_request_send(fc
, req
);
639 fuse_put_request(fc
, req
);
643 * Lock the request. Up to the next unlock_request() there mustn't be
644 * anything that could cause a page-fault. If the request was already
647 static int lock_request(struct fuse_req
*req
)
651 spin_lock(&req
->waitq
.lock
);
652 if (test_bit(FR_ABORTED
, &req
->flags
))
655 set_bit(FR_LOCKED
, &req
->flags
);
656 spin_unlock(&req
->waitq
.lock
);
662 * Unlock request. If it was aborted while locked, caller is responsible
663 * for unlocking and ending the request.
665 static int unlock_request(struct fuse_req
*req
)
669 spin_lock(&req
->waitq
.lock
);
670 if (test_bit(FR_ABORTED
, &req
->flags
))
673 clear_bit(FR_LOCKED
, &req
->flags
);
674 spin_unlock(&req
->waitq
.lock
);
679 struct fuse_copy_state
{
681 struct fuse_req
*req
;
682 struct iov_iter
*iter
;
683 struct pipe_buffer
*pipebufs
;
684 struct pipe_buffer
*currbuf
;
685 struct pipe_inode_info
*pipe
;
686 unsigned long nr_segs
;
690 unsigned move_pages
:1;
693 static void fuse_copy_init(struct fuse_copy_state
*cs
, int write
,
694 struct iov_iter
*iter
)
696 memset(cs
, 0, sizeof(*cs
));
701 /* Unmap and put previous page of userspace buffer */
702 static void fuse_copy_finish(struct fuse_copy_state
*cs
)
705 struct pipe_buffer
*buf
= cs
->currbuf
;
708 buf
->len
= PAGE_SIZE
- cs
->len
;
712 flush_dcache_page(cs
->pg
);
713 set_page_dirty_lock(cs
->pg
);
721 * Get another pagefull of userspace buffer, and map it to kernel
722 * address space, and lock request
724 static int fuse_copy_fill(struct fuse_copy_state
*cs
)
729 err
= unlock_request(cs
->req
);
733 fuse_copy_finish(cs
);
735 struct pipe_buffer
*buf
= cs
->pipebufs
;
738 err
= pipe_buf_confirm(cs
->pipe
, buf
);
742 BUG_ON(!cs
->nr_segs
);
745 cs
->offset
= buf
->offset
;
750 if (cs
->nr_segs
== cs
->pipe
->buffers
)
753 page
= alloc_page(GFP_HIGHUSER
);
770 err
= iov_iter_get_pages(cs
->iter
, &page
, PAGE_SIZE
, 1, &off
);
777 iov_iter_advance(cs
->iter
, err
);
780 return lock_request(cs
->req
);
783 /* Do as much copy to/from userspace buffer as we can */
784 static int fuse_copy_do(struct fuse_copy_state
*cs
, void **val
, unsigned *size
)
786 unsigned ncpy
= min(*size
, cs
->len
);
788 void *pgaddr
= kmap_atomic(cs
->pg
);
789 void *buf
= pgaddr
+ cs
->offset
;
792 memcpy(buf
, *val
, ncpy
);
794 memcpy(*val
, buf
, ncpy
);
796 kunmap_atomic(pgaddr
);
805 static int fuse_check_page(struct page
*page
)
807 if (page_mapcount(page
) ||
808 page
->mapping
!= NULL
||
809 page_count(page
) != 1 ||
810 (page
->flags
& PAGE_FLAGS_CHECK_AT_PREP
&
817 printk(KERN_WARNING
"fuse: trying to steal weird page\n");
818 printk(KERN_WARNING
" page=%p index=%li flags=%08lx, count=%i, mapcount=%i, mapping=%p\n", page
, page
->index
, page
->flags
, page_count(page
), page_mapcount(page
), page
->mapping
);
824 static int fuse_try_move_page(struct fuse_copy_state
*cs
, struct page
**pagep
)
827 struct page
*oldpage
= *pagep
;
828 struct page
*newpage
;
829 struct pipe_buffer
*buf
= cs
->pipebufs
;
831 err
= unlock_request(cs
->req
);
835 fuse_copy_finish(cs
);
837 err
= pipe_buf_confirm(cs
->pipe
, buf
);
841 BUG_ON(!cs
->nr_segs
);
847 if (cs
->len
!= PAGE_SIZE
)
850 if (pipe_buf_steal(cs
->pipe
, buf
) != 0)
855 if (!PageUptodate(newpage
))
856 SetPageUptodate(newpage
);
858 ClearPageMappedToDisk(newpage
);
860 if (fuse_check_page(newpage
) != 0)
861 goto out_fallback_unlock
;
864 * This is a new and locked page, it shouldn't be mapped or
865 * have any special flags on it
867 if (WARN_ON(page_mapped(oldpage
)))
868 goto out_fallback_unlock
;
869 if (WARN_ON(page_has_private(oldpage
)))
870 goto out_fallback_unlock
;
871 if (WARN_ON(PageDirty(oldpage
) || PageWriteback(oldpage
)))
872 goto out_fallback_unlock
;
873 if (WARN_ON(PageMlocked(oldpage
)))
874 goto out_fallback_unlock
;
876 err
= replace_page_cache_page(oldpage
, newpage
, GFP_KERNEL
);
878 unlock_page(newpage
);
884 if (!(buf
->flags
& PIPE_BUF_FLAG_LRU
))
885 lru_cache_add_file(newpage
);
888 spin_lock(&cs
->req
->waitq
.lock
);
889 if (test_bit(FR_ABORTED
, &cs
->req
->flags
))
893 spin_unlock(&cs
->req
->waitq
.lock
);
896 unlock_page(newpage
);
901 unlock_page(oldpage
);
908 unlock_page(newpage
);
911 cs
->offset
= buf
->offset
;
913 err
= lock_request(cs
->req
);
920 static int fuse_ref_page(struct fuse_copy_state
*cs
, struct page
*page
,
921 unsigned offset
, unsigned count
)
923 struct pipe_buffer
*buf
;
926 if (cs
->nr_segs
== cs
->pipe
->buffers
)
929 err
= unlock_request(cs
->req
);
933 fuse_copy_finish(cs
);
938 buf
->offset
= offset
;
949 * Copy a page in the request to/from the userspace buffer. Must be
952 static int fuse_copy_page(struct fuse_copy_state
*cs
, struct page
**pagep
,
953 unsigned offset
, unsigned count
, int zeroing
)
956 struct page
*page
= *pagep
;
958 if (page
&& zeroing
&& count
< PAGE_SIZE
)
959 clear_highpage(page
);
962 if (cs
->write
&& cs
->pipebufs
&& page
) {
963 return fuse_ref_page(cs
, page
, offset
, count
);
964 } else if (!cs
->len
) {
965 if (cs
->move_pages
&& page
&&
966 offset
== 0 && count
== PAGE_SIZE
) {
967 err
= fuse_try_move_page(cs
, pagep
);
971 err
= fuse_copy_fill(cs
);
977 void *mapaddr
= kmap_atomic(page
);
978 void *buf
= mapaddr
+ offset
;
979 offset
+= fuse_copy_do(cs
, &buf
, &count
);
980 kunmap_atomic(mapaddr
);
982 offset
+= fuse_copy_do(cs
, NULL
, &count
);
984 if (page
&& !cs
->write
)
985 flush_dcache_page(page
);
989 /* Copy pages in the request to/from userspace buffer */
990 static int fuse_copy_pages(struct fuse_copy_state
*cs
, unsigned nbytes
,
994 struct fuse_req
*req
= cs
->req
;
996 for (i
= 0; i
< req
->num_pages
&& (nbytes
|| zeroing
); i
++) {
998 unsigned offset
= req
->page_descs
[i
].offset
;
999 unsigned count
= min(nbytes
, req
->page_descs
[i
].length
);
1001 err
= fuse_copy_page(cs
, &req
->pages
[i
], offset
, count
,
1011 /* Copy a single argument in the request to/from userspace buffer */
1012 static int fuse_copy_one(struct fuse_copy_state
*cs
, void *val
, unsigned size
)
1016 int err
= fuse_copy_fill(cs
);
1020 fuse_copy_do(cs
, &val
, &size
);
1025 /* Copy request arguments to/from userspace buffer */
1026 static int fuse_copy_args(struct fuse_copy_state
*cs
, unsigned numargs
,
1027 unsigned argpages
, struct fuse_arg
*args
,
1033 for (i
= 0; !err
&& i
< numargs
; i
++) {
1034 struct fuse_arg
*arg
= &args
[i
];
1035 if (i
== numargs
- 1 && argpages
)
1036 err
= fuse_copy_pages(cs
, arg
->size
, zeroing
);
1038 err
= fuse_copy_one(cs
, arg
->value
, arg
->size
);
1043 static int forget_pending(struct fuse_iqueue
*fiq
)
1045 return fiq
->forget_list_head
.next
!= NULL
;
1048 static int request_pending(struct fuse_iqueue
*fiq
)
1050 return !list_empty(&fiq
->pending
) || !list_empty(&fiq
->interrupts
) ||
1051 forget_pending(fiq
);
1055 * Transfer an interrupt request to userspace
1057 * Unlike other requests this is assembled on demand, without a need
1058 * to allocate a separate fuse_req structure.
1060 * Called with fiq->waitq.lock held, releases it
1062 static int fuse_read_interrupt(struct fuse_iqueue
*fiq
,
1063 struct fuse_copy_state
*cs
,
1064 size_t nbytes
, struct fuse_req
*req
)
1065 __releases(fiq
->waitq
.lock
)
1067 struct fuse_in_header ih
;
1068 struct fuse_interrupt_in arg
;
1069 unsigned reqsize
= sizeof(ih
) + sizeof(arg
);
1072 list_del_init(&req
->intr_entry
);
1073 req
->intr_unique
= fuse_get_unique(fiq
);
1074 memset(&ih
, 0, sizeof(ih
));
1075 memset(&arg
, 0, sizeof(arg
));
1077 ih
.opcode
= FUSE_INTERRUPT
;
1078 ih
.unique
= req
->intr_unique
;
1079 arg
.unique
= req
->in
.h
.unique
;
1081 spin_unlock(&fiq
->waitq
.lock
);
1082 if (nbytes
< reqsize
)
1085 err
= fuse_copy_one(cs
, &ih
, sizeof(ih
));
1087 err
= fuse_copy_one(cs
, &arg
, sizeof(arg
));
1088 fuse_copy_finish(cs
);
1090 return err
? err
: reqsize
;
1093 static struct fuse_forget_link
*dequeue_forget(struct fuse_iqueue
*fiq
,
1097 struct fuse_forget_link
*head
= fiq
->forget_list_head
.next
;
1098 struct fuse_forget_link
**newhead
= &head
;
1101 for (count
= 0; *newhead
!= NULL
&& count
< max
; count
++)
1102 newhead
= &(*newhead
)->next
;
1104 fiq
->forget_list_head
.next
= *newhead
;
1106 if (fiq
->forget_list_head
.next
== NULL
)
1107 fiq
->forget_list_tail
= &fiq
->forget_list_head
;
1115 static int fuse_read_single_forget(struct fuse_iqueue
*fiq
,
1116 struct fuse_copy_state
*cs
,
1118 __releases(fiq
->waitq
.lock
)
1121 struct fuse_forget_link
*forget
= dequeue_forget(fiq
, 1, NULL
);
1122 struct fuse_forget_in arg
= {
1123 .nlookup
= forget
->forget_one
.nlookup
,
1125 struct fuse_in_header ih
= {
1126 .opcode
= FUSE_FORGET
,
1127 .nodeid
= forget
->forget_one
.nodeid
,
1128 .unique
= fuse_get_unique(fiq
),
1129 .len
= sizeof(ih
) + sizeof(arg
),
1132 spin_unlock(&fiq
->waitq
.lock
);
1134 if (nbytes
< ih
.len
)
1137 err
= fuse_copy_one(cs
, &ih
, sizeof(ih
));
1139 err
= fuse_copy_one(cs
, &arg
, sizeof(arg
));
1140 fuse_copy_finish(cs
);
1148 static int fuse_read_batch_forget(struct fuse_iqueue
*fiq
,
1149 struct fuse_copy_state
*cs
, size_t nbytes
)
1150 __releases(fiq
->waitq
.lock
)
1153 unsigned max_forgets
;
1155 struct fuse_forget_link
*head
;
1156 struct fuse_batch_forget_in arg
= { .count
= 0 };
1157 struct fuse_in_header ih
= {
1158 .opcode
= FUSE_BATCH_FORGET
,
1159 .unique
= fuse_get_unique(fiq
),
1160 .len
= sizeof(ih
) + sizeof(arg
),
1163 if (nbytes
< ih
.len
) {
1164 spin_unlock(&fiq
->waitq
.lock
);
1168 max_forgets
= (nbytes
- ih
.len
) / sizeof(struct fuse_forget_one
);
1169 head
= dequeue_forget(fiq
, max_forgets
, &count
);
1170 spin_unlock(&fiq
->waitq
.lock
);
1173 ih
.len
+= count
* sizeof(struct fuse_forget_one
);
1174 err
= fuse_copy_one(cs
, &ih
, sizeof(ih
));
1176 err
= fuse_copy_one(cs
, &arg
, sizeof(arg
));
1179 struct fuse_forget_link
*forget
= head
;
1182 err
= fuse_copy_one(cs
, &forget
->forget_one
,
1183 sizeof(forget
->forget_one
));
1185 head
= forget
->next
;
1189 fuse_copy_finish(cs
);
1197 static int fuse_read_forget(struct fuse_conn
*fc
, struct fuse_iqueue
*fiq
,
1198 struct fuse_copy_state
*cs
,
1200 __releases(fiq
->waitq
.lock
)
1202 if (fc
->minor
< 16 || fiq
->forget_list_head
.next
->next
== NULL
)
1203 return fuse_read_single_forget(fiq
, cs
, nbytes
);
1205 return fuse_read_batch_forget(fiq
, cs
, nbytes
);
1209 * Read a single request into the userspace filesystem's buffer. This
1210 * function waits until a request is available, then removes it from
1211 * the pending list and copies request data to userspace buffer. If
1212 * no reply is needed (FORGET) or request has been aborted or there
1213 * was an error during the copying then it's finished by calling
1214 * request_end(). Otherwise add it to the processing list, and set
1217 static ssize_t
fuse_dev_do_read(struct fuse_dev
*fud
, struct file
*file
,
1218 struct fuse_copy_state
*cs
, size_t nbytes
)
1221 struct fuse_conn
*fc
= fud
->fc
;
1222 struct fuse_iqueue
*fiq
= &fc
->iq
;
1223 struct fuse_pqueue
*fpq
= &fud
->pq
;
1224 struct fuse_req
*req
;
1229 spin_lock(&fiq
->waitq
.lock
);
1231 if ((file
->f_flags
& O_NONBLOCK
) && fiq
->connected
&&
1232 !request_pending(fiq
))
1235 err
= wait_event_interruptible_exclusive_locked(fiq
->waitq
,
1236 !fiq
->connected
|| request_pending(fiq
));
1241 if (!fiq
->connected
)
1244 if (!list_empty(&fiq
->interrupts
)) {
1245 req
= list_entry(fiq
->interrupts
.next
, struct fuse_req
,
1247 return fuse_read_interrupt(fiq
, cs
, nbytes
, req
);
1250 if (forget_pending(fiq
)) {
1251 if (list_empty(&fiq
->pending
) || fiq
->forget_batch
-- > 0)
1252 return fuse_read_forget(fc
, fiq
, cs
, nbytes
);
1254 if (fiq
->forget_batch
<= -8)
1255 fiq
->forget_batch
= 16;
1258 req
= list_entry(fiq
->pending
.next
, struct fuse_req
, list
);
1259 clear_bit(FR_PENDING
, &req
->flags
);
1260 list_del_init(&req
->list
);
1261 spin_unlock(&fiq
->waitq
.lock
);
1264 reqsize
= in
->h
.len
;
1266 if (task_active_pid_ns(current
) != fc
->pid_ns
) {
1268 in
->h
.pid
= pid_vnr(find_pid_ns(in
->h
.pid
, fc
->pid_ns
));
1272 /* If request is too large, reply with an error and restart the read */
1273 if (nbytes
< reqsize
) {
1274 req
->out
.h
.error
= -EIO
;
1275 /* SETXATTR is special, since it may contain too large data */
1276 if (in
->h
.opcode
== FUSE_SETXATTR
)
1277 req
->out
.h
.error
= -E2BIG
;
1278 request_end(fc
, req
);
1281 spin_lock(&fpq
->lock
);
1282 list_add(&req
->list
, &fpq
->io
);
1283 spin_unlock(&fpq
->lock
);
1285 err
= fuse_copy_one(cs
, &in
->h
, sizeof(in
->h
));
1287 err
= fuse_copy_args(cs
, in
->numargs
, in
->argpages
,
1288 (struct fuse_arg
*) in
->args
, 0);
1289 fuse_copy_finish(cs
);
1290 spin_lock(&fpq
->lock
);
1291 clear_bit(FR_LOCKED
, &req
->flags
);
1292 if (!fpq
->connected
) {
1297 req
->out
.h
.error
= -EIO
;
1300 if (!test_bit(FR_ISREPLY
, &req
->flags
)) {
1304 list_move_tail(&req
->list
, &fpq
->processing
);
1305 spin_unlock(&fpq
->lock
);
1306 set_bit(FR_SENT
, &req
->flags
);
1307 /* matches barrier in request_wait_answer() */
1308 smp_mb__after_atomic();
1309 if (test_bit(FR_INTERRUPTED
, &req
->flags
))
1310 queue_interrupt(fiq
, req
);
1315 if (!test_bit(FR_PRIVATE
, &req
->flags
))
1316 list_del_init(&req
->list
);
1317 spin_unlock(&fpq
->lock
);
1318 request_end(fc
, req
);
1322 spin_unlock(&fiq
->waitq
.lock
);
1326 static int fuse_dev_open(struct inode
*inode
, struct file
*file
)
1329 * The fuse device's file's private_data is used to hold
1330 * the fuse_conn(ection) when it is mounted, and is used to
1331 * keep track of whether the file has been mounted already.
1333 file
->private_data
= NULL
;
1337 static ssize_t
fuse_dev_read(struct kiocb
*iocb
, struct iov_iter
*to
)
1339 struct fuse_copy_state cs
;
1340 struct file
*file
= iocb
->ki_filp
;
1341 struct fuse_dev
*fud
= fuse_get_dev(file
);
1346 if (!iter_is_iovec(to
))
1349 fuse_copy_init(&cs
, 1, to
);
1351 return fuse_dev_do_read(fud
, file
, &cs
, iov_iter_count(to
));
1354 static ssize_t
fuse_dev_splice_read(struct file
*in
, loff_t
*ppos
,
1355 struct pipe_inode_info
*pipe
,
1356 size_t len
, unsigned int flags
)
1360 struct pipe_buffer
*bufs
;
1361 struct fuse_copy_state cs
;
1362 struct fuse_dev
*fud
= fuse_get_dev(in
);
1367 bufs
= kmalloc(pipe
->buffers
* sizeof(struct pipe_buffer
), GFP_KERNEL
);
1371 fuse_copy_init(&cs
, 1, NULL
);
1374 ret
= fuse_dev_do_read(fud
, in
, &cs
, len
);
1378 if (pipe
->nrbufs
+ cs
.nr_segs
> pipe
->buffers
) {
1383 for (ret
= total
= 0; page_nr
< cs
.nr_segs
; total
+= ret
) {
1385 * Need to be careful about this. Having buf->ops in module
1386 * code can Oops if the buffer persists after module unload.
1388 bufs
[page_nr
].ops
= &nosteal_pipe_buf_ops
;
1389 bufs
[page_nr
].flags
= 0;
1390 ret
= add_to_pipe(pipe
, &bufs
[page_nr
++]);
1391 if (unlikely(ret
< 0))
1397 for (; page_nr
< cs
.nr_segs
; page_nr
++)
1398 put_page(bufs
[page_nr
].page
);
1404 static int fuse_notify_poll(struct fuse_conn
*fc
, unsigned int size
,
1405 struct fuse_copy_state
*cs
)
1407 struct fuse_notify_poll_wakeup_out outarg
;
1410 if (size
!= sizeof(outarg
))
1413 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1417 fuse_copy_finish(cs
);
1418 return fuse_notify_poll_wakeup(fc
, &outarg
);
1421 fuse_copy_finish(cs
);
1425 static int fuse_notify_inval_inode(struct fuse_conn
*fc
, unsigned int size
,
1426 struct fuse_copy_state
*cs
)
1428 struct fuse_notify_inval_inode_out outarg
;
1431 if (size
!= sizeof(outarg
))
1434 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1437 fuse_copy_finish(cs
);
1439 down_read(&fc
->killsb
);
1442 err
= fuse_reverse_inval_inode(fc
->sb
, outarg
.ino
,
1443 outarg
.off
, outarg
.len
);
1445 up_read(&fc
->killsb
);
1449 fuse_copy_finish(cs
);
1453 static int fuse_notify_inval_entry(struct fuse_conn
*fc
, unsigned int size
,
1454 struct fuse_copy_state
*cs
)
1456 struct fuse_notify_inval_entry_out outarg
;
1461 buf
= kzalloc(FUSE_NAME_MAX
+ 1, GFP_KERNEL
);
1466 if (size
< sizeof(outarg
))
1469 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1473 err
= -ENAMETOOLONG
;
1474 if (outarg
.namelen
> FUSE_NAME_MAX
)
1478 if (size
!= sizeof(outarg
) + outarg
.namelen
+ 1)
1482 name
.len
= outarg
.namelen
;
1483 err
= fuse_copy_one(cs
, buf
, outarg
.namelen
+ 1);
1486 fuse_copy_finish(cs
);
1487 buf
[outarg
.namelen
] = 0;
1489 down_read(&fc
->killsb
);
1492 err
= fuse_reverse_inval_entry(fc
->sb
, outarg
.parent
, 0, &name
);
1493 up_read(&fc
->killsb
);
1499 fuse_copy_finish(cs
);
1503 static int fuse_notify_delete(struct fuse_conn
*fc
, unsigned int size
,
1504 struct fuse_copy_state
*cs
)
1506 struct fuse_notify_delete_out outarg
;
1511 buf
= kzalloc(FUSE_NAME_MAX
+ 1, GFP_KERNEL
);
1516 if (size
< sizeof(outarg
))
1519 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1523 err
= -ENAMETOOLONG
;
1524 if (outarg
.namelen
> FUSE_NAME_MAX
)
1528 if (size
!= sizeof(outarg
) + outarg
.namelen
+ 1)
1532 name
.len
= outarg
.namelen
;
1533 err
= fuse_copy_one(cs
, buf
, outarg
.namelen
+ 1);
1536 fuse_copy_finish(cs
);
1537 buf
[outarg
.namelen
] = 0;
1539 down_read(&fc
->killsb
);
1542 err
= fuse_reverse_inval_entry(fc
->sb
, outarg
.parent
,
1543 outarg
.child
, &name
);
1544 up_read(&fc
->killsb
);
1550 fuse_copy_finish(cs
);
1554 static int fuse_notify_store(struct fuse_conn
*fc
, unsigned int size
,
1555 struct fuse_copy_state
*cs
)
1557 struct fuse_notify_store_out outarg
;
1558 struct inode
*inode
;
1559 struct address_space
*mapping
;
1563 unsigned int offset
;
1569 if (size
< sizeof(outarg
))
1572 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1577 if (size
- sizeof(outarg
) != outarg
.size
)
1580 nodeid
= outarg
.nodeid
;
1582 down_read(&fc
->killsb
);
1588 inode
= ilookup5(fc
->sb
, nodeid
, fuse_inode_eq
, &nodeid
);
1592 mapping
= inode
->i_mapping
;
1593 index
= outarg
.offset
>> PAGE_SHIFT
;
1594 offset
= outarg
.offset
& ~PAGE_MASK
;
1595 file_size
= i_size_read(inode
);
1596 end
= outarg
.offset
+ outarg
.size
;
1597 if (end
> file_size
) {
1599 fuse_write_update_size(inode
, file_size
);
1605 unsigned int this_num
;
1608 page
= find_or_create_page(mapping
, index
,
1609 mapping_gfp_mask(mapping
));
1613 this_num
= min_t(unsigned, num
, PAGE_SIZE
- offset
);
1614 err
= fuse_copy_page(cs
, &page
, offset
, this_num
, 0);
1615 if (!err
&& offset
== 0 &&
1616 (this_num
== PAGE_SIZE
|| file_size
== end
))
1617 SetPageUptodate(page
);
1634 up_read(&fc
->killsb
);
1636 fuse_copy_finish(cs
);
1640 static void fuse_retrieve_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
1642 release_pages(req
->pages
, req
->num_pages
, false);
1645 static int fuse_retrieve(struct fuse_conn
*fc
, struct inode
*inode
,
1646 struct fuse_notify_retrieve_out
*outarg
)
1649 struct address_space
*mapping
= inode
->i_mapping
;
1650 struct fuse_req
*req
;
1654 unsigned int offset
;
1655 size_t total_len
= 0;
1658 offset
= outarg
->offset
& ~PAGE_MASK
;
1659 file_size
= i_size_read(inode
);
1662 if (outarg
->offset
> file_size
)
1664 else if (outarg
->offset
+ num
> file_size
)
1665 num
= file_size
- outarg
->offset
;
1667 num_pages
= (num
+ offset
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1668 num_pages
= min(num_pages
, FUSE_MAX_PAGES_PER_REQ
);
1670 req
= fuse_get_req(fc
, num_pages
);
1672 return PTR_ERR(req
);
1674 req
->in
.h
.opcode
= FUSE_NOTIFY_REPLY
;
1675 req
->in
.h
.nodeid
= outarg
->nodeid
;
1676 req
->in
.numargs
= 2;
1677 req
->in
.argpages
= 1;
1678 req
->page_descs
[0].offset
= offset
;
1679 req
->end
= fuse_retrieve_end
;
1681 index
= outarg
->offset
>> PAGE_SHIFT
;
1683 while (num
&& req
->num_pages
< num_pages
) {
1685 unsigned int this_num
;
1687 page
= find_get_page(mapping
, index
);
1691 this_num
= min_t(unsigned, num
, PAGE_SIZE
- offset
);
1692 req
->pages
[req
->num_pages
] = page
;
1693 req
->page_descs
[req
->num_pages
].length
= this_num
;
1698 total_len
+= this_num
;
1701 req
->misc
.retrieve_in
.offset
= outarg
->offset
;
1702 req
->misc
.retrieve_in
.size
= total_len
;
1703 req
->in
.args
[0].size
= sizeof(req
->misc
.retrieve_in
);
1704 req
->in
.args
[0].value
= &req
->misc
.retrieve_in
;
1705 req
->in
.args
[1].size
= total_len
;
1707 err
= fuse_request_send_notify_reply(fc
, req
, outarg
->notify_unique
);
1709 fuse_retrieve_end(fc
, req
);
1714 static int fuse_notify_retrieve(struct fuse_conn
*fc
, unsigned int size
,
1715 struct fuse_copy_state
*cs
)
1717 struct fuse_notify_retrieve_out outarg
;
1718 struct inode
*inode
;
1722 if (size
!= sizeof(outarg
))
1725 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1729 fuse_copy_finish(cs
);
1731 down_read(&fc
->killsb
);
1734 u64 nodeid
= outarg
.nodeid
;
1736 inode
= ilookup5(fc
->sb
, nodeid
, fuse_inode_eq
, &nodeid
);
1738 err
= fuse_retrieve(fc
, inode
, &outarg
);
1742 up_read(&fc
->killsb
);
1747 fuse_copy_finish(cs
);
1751 static int fuse_notify(struct fuse_conn
*fc
, enum fuse_notify_code code
,
1752 unsigned int size
, struct fuse_copy_state
*cs
)
1754 /* Don't try to move pages (yet) */
1758 case FUSE_NOTIFY_POLL
:
1759 return fuse_notify_poll(fc
, size
, cs
);
1761 case FUSE_NOTIFY_INVAL_INODE
:
1762 return fuse_notify_inval_inode(fc
, size
, cs
);
1764 case FUSE_NOTIFY_INVAL_ENTRY
:
1765 return fuse_notify_inval_entry(fc
, size
, cs
);
1767 case FUSE_NOTIFY_STORE
:
1768 return fuse_notify_store(fc
, size
, cs
);
1770 case FUSE_NOTIFY_RETRIEVE
:
1771 return fuse_notify_retrieve(fc
, size
, cs
);
1773 case FUSE_NOTIFY_DELETE
:
1774 return fuse_notify_delete(fc
, size
, cs
);
1777 fuse_copy_finish(cs
);
1782 /* Look up request on processing list by unique ID */
1783 static struct fuse_req
*request_find(struct fuse_pqueue
*fpq
, u64 unique
)
1785 struct fuse_req
*req
;
1787 list_for_each_entry(req
, &fpq
->processing
, list
) {
1788 if (req
->in
.h
.unique
== unique
|| req
->intr_unique
== unique
)
1794 static int copy_out_args(struct fuse_copy_state
*cs
, struct fuse_out
*out
,
1797 unsigned reqsize
= sizeof(struct fuse_out_header
);
1800 return nbytes
!= reqsize
? -EINVAL
: 0;
1802 reqsize
+= len_args(out
->numargs
, out
->args
);
1804 if (reqsize
< nbytes
|| (reqsize
> nbytes
&& !out
->argvar
))
1806 else if (reqsize
> nbytes
) {
1807 struct fuse_arg
*lastarg
= &out
->args
[out
->numargs
-1];
1808 unsigned diffsize
= reqsize
- nbytes
;
1809 if (diffsize
> lastarg
->size
)
1811 lastarg
->size
-= diffsize
;
1813 return fuse_copy_args(cs
, out
->numargs
, out
->argpages
, out
->args
,
1818 * Write a single reply to a request. First the header is copied from
1819 * the write buffer. The request is then searched on the processing
1820 * list by the unique ID found in the header. If found, then remove
1821 * it from the list and copy the rest of the buffer to the request.
1822 * The request is finished by calling request_end()
1824 static ssize_t
fuse_dev_do_write(struct fuse_dev
*fud
,
1825 struct fuse_copy_state
*cs
, size_t nbytes
)
1828 struct fuse_conn
*fc
= fud
->fc
;
1829 struct fuse_pqueue
*fpq
= &fud
->pq
;
1830 struct fuse_req
*req
;
1831 struct fuse_out_header oh
;
1833 if (nbytes
< sizeof(struct fuse_out_header
))
1836 err
= fuse_copy_one(cs
, &oh
, sizeof(oh
));
1841 if (oh
.len
!= nbytes
)
1845 * Zero oh.unique indicates unsolicited notification message
1846 * and error contains notification code.
1849 err
= fuse_notify(fc
, oh
.error
, nbytes
- sizeof(oh
), cs
);
1850 return err
? err
: nbytes
;
1854 if (oh
.error
<= -1000 || oh
.error
> 0)
1857 spin_lock(&fpq
->lock
);
1859 if (!fpq
->connected
)
1862 req
= request_find(fpq
, oh
.unique
);
1866 /* Is it an interrupt reply? */
1867 if (req
->intr_unique
== oh
.unique
) {
1868 spin_unlock(&fpq
->lock
);
1871 if (nbytes
!= sizeof(struct fuse_out_header
))
1874 if (oh
.error
== -ENOSYS
)
1875 fc
->no_interrupt
= 1;
1876 else if (oh
.error
== -EAGAIN
)
1877 queue_interrupt(&fc
->iq
, req
);
1879 fuse_copy_finish(cs
);
1883 clear_bit(FR_SENT
, &req
->flags
);
1884 list_move(&req
->list
, &fpq
->io
);
1886 set_bit(FR_LOCKED
, &req
->flags
);
1887 spin_unlock(&fpq
->lock
);
1889 if (!req
->out
.page_replace
)
1892 err
= copy_out_args(cs
, &req
->out
, nbytes
);
1893 fuse_copy_finish(cs
);
1895 spin_lock(&fpq
->lock
);
1896 clear_bit(FR_LOCKED
, &req
->flags
);
1897 if (!fpq
->connected
)
1900 req
->out
.h
.error
= -EIO
;
1901 if (!test_bit(FR_PRIVATE
, &req
->flags
))
1902 list_del_init(&req
->list
);
1903 spin_unlock(&fpq
->lock
);
1905 request_end(fc
, req
);
1907 return err
? err
: nbytes
;
1910 spin_unlock(&fpq
->lock
);
1912 fuse_copy_finish(cs
);
1916 static ssize_t
fuse_dev_write(struct kiocb
*iocb
, struct iov_iter
*from
)
1918 struct fuse_copy_state cs
;
1919 struct fuse_dev
*fud
= fuse_get_dev(iocb
->ki_filp
);
1924 if (!iter_is_iovec(from
))
1927 fuse_copy_init(&cs
, 0, from
);
1929 return fuse_dev_do_write(fud
, &cs
, iov_iter_count(from
));
1932 static ssize_t
fuse_dev_splice_write(struct pipe_inode_info
*pipe
,
1933 struct file
*out
, loff_t
*ppos
,
1934 size_t len
, unsigned int flags
)
1938 struct pipe_buffer
*bufs
;
1939 struct fuse_copy_state cs
;
1940 struct fuse_dev
*fud
;
1944 fud
= fuse_get_dev(out
);
1948 bufs
= kmalloc(pipe
->buffers
* sizeof(struct pipe_buffer
), GFP_KERNEL
);
1955 for (idx
= 0; idx
< pipe
->nrbufs
&& rem
< len
; idx
++)
1956 rem
+= pipe
->bufs
[(pipe
->curbuf
+ idx
) & (pipe
->buffers
- 1)].len
;
1966 struct pipe_buffer
*ibuf
;
1967 struct pipe_buffer
*obuf
;
1969 BUG_ON(nbuf
>= pipe
->buffers
);
1970 BUG_ON(!pipe
->nrbufs
);
1971 ibuf
= &pipe
->bufs
[pipe
->curbuf
];
1974 if (rem
>= ibuf
->len
) {
1977 pipe
->curbuf
= (pipe
->curbuf
+ 1) & (pipe
->buffers
- 1);
1980 pipe_buf_get(pipe
, ibuf
);
1982 obuf
->flags
&= ~PIPE_BUF_FLAG_GIFT
;
1984 ibuf
->offset
+= obuf
->len
;
1985 ibuf
->len
-= obuf
->len
;
1992 fuse_copy_init(&cs
, 0, NULL
);
1997 if (flags
& SPLICE_F_MOVE
)
2000 ret
= fuse_dev_do_write(fud
, &cs
, len
);
2002 for (idx
= 0; idx
< nbuf
; idx
++)
2003 pipe_buf_release(pipe
, &bufs
[idx
]);
2010 static unsigned fuse_dev_poll(struct file
*file
, poll_table
*wait
)
2012 unsigned mask
= POLLOUT
| POLLWRNORM
;
2013 struct fuse_iqueue
*fiq
;
2014 struct fuse_dev
*fud
= fuse_get_dev(file
);
2020 poll_wait(file
, &fiq
->waitq
, wait
);
2022 spin_lock(&fiq
->waitq
.lock
);
2023 if (!fiq
->connected
)
2025 else if (request_pending(fiq
))
2026 mask
|= POLLIN
| POLLRDNORM
;
2027 spin_unlock(&fiq
->waitq
.lock
);
2033 * Abort all requests on the given list (pending or processing)
2035 * This function releases and reacquires fc->lock
2037 static void end_requests(struct fuse_conn
*fc
, struct list_head
*head
)
2039 while (!list_empty(head
)) {
2040 struct fuse_req
*req
;
2041 req
= list_entry(head
->next
, struct fuse_req
, list
);
2042 req
->out
.h
.error
= -ECONNABORTED
;
2043 clear_bit(FR_SENT
, &req
->flags
);
2044 list_del_init(&req
->list
);
2045 request_end(fc
, req
);
2049 static void end_polls(struct fuse_conn
*fc
)
2053 p
= rb_first(&fc
->polled_files
);
2056 struct fuse_file
*ff
;
2057 ff
= rb_entry(p
, struct fuse_file
, polled_node
);
2058 wake_up_interruptible_all(&ff
->poll_wait
);
2065 * Abort all requests.
2067 * Emergency exit in case of a malicious or accidental deadlock, or just a hung
2070 * The same effect is usually achievable through killing the filesystem daemon
2071 * and all users of the filesystem. The exception is the combination of an
2072 * asynchronous request and the tricky deadlock (see
2073 * Documentation/filesystems/fuse.txt).
2075 * Aborting requests under I/O goes as follows: 1: Separate out unlocked
2076 * requests, they should be finished off immediately. Locked requests will be
2077 * finished after unlock; see unlock_request(). 2: Finish off the unlocked
2078 * requests. It is possible that some request will finish before we can. This
2079 * is OK, the request will in that case be removed from the list before we touch
2082 void fuse_abort_conn(struct fuse_conn
*fc
)
2084 struct fuse_iqueue
*fiq
= &fc
->iq
;
2086 spin_lock(&fc
->lock
);
2087 if (fc
->connected
) {
2088 struct fuse_dev
*fud
;
2089 struct fuse_req
*req
, *next
;
2095 fuse_set_initialized(fc
);
2096 list_for_each_entry(fud
, &fc
->devices
, entry
) {
2097 struct fuse_pqueue
*fpq
= &fud
->pq
;
2099 spin_lock(&fpq
->lock
);
2101 list_for_each_entry_safe(req
, next
, &fpq
->io
, list
) {
2102 req
->out
.h
.error
= -ECONNABORTED
;
2103 spin_lock(&req
->waitq
.lock
);
2104 set_bit(FR_ABORTED
, &req
->flags
);
2105 if (!test_bit(FR_LOCKED
, &req
->flags
)) {
2106 set_bit(FR_PRIVATE
, &req
->flags
);
2107 list_move(&req
->list
, &to_end1
);
2109 spin_unlock(&req
->waitq
.lock
);
2111 list_splice_init(&fpq
->processing
, &to_end2
);
2112 spin_unlock(&fpq
->lock
);
2114 fc
->max_background
= UINT_MAX
;
2117 spin_lock(&fiq
->waitq
.lock
);
2119 list_splice_init(&fiq
->pending
, &to_end2
);
2120 list_for_each_entry(req
, &to_end2
, list
)
2121 clear_bit(FR_PENDING
, &req
->flags
);
2122 while (forget_pending(fiq
))
2123 kfree(dequeue_forget(fiq
, 1, NULL
));
2124 wake_up_all_locked(&fiq
->waitq
);
2125 spin_unlock(&fiq
->waitq
.lock
);
2126 kill_fasync(&fiq
->fasync
, SIGIO
, POLL_IN
);
2128 wake_up_all(&fc
->blocked_waitq
);
2129 spin_unlock(&fc
->lock
);
2131 while (!list_empty(&to_end1
)) {
2132 req
= list_first_entry(&to_end1
, struct fuse_req
, list
);
2133 __fuse_get_request(req
);
2134 list_del_init(&req
->list
);
2135 request_end(fc
, req
);
2137 end_requests(fc
, &to_end2
);
2139 spin_unlock(&fc
->lock
);
2142 EXPORT_SYMBOL_GPL(fuse_abort_conn
);
2144 int fuse_dev_release(struct inode
*inode
, struct file
*file
)
2146 struct fuse_dev
*fud
= fuse_get_dev(file
);
2149 struct fuse_conn
*fc
= fud
->fc
;
2150 struct fuse_pqueue
*fpq
= &fud
->pq
;
2152 WARN_ON(!list_empty(&fpq
->io
));
2153 end_requests(fc
, &fpq
->processing
);
2154 /* Are we the last open device? */
2155 if (atomic_dec_and_test(&fc
->dev_count
)) {
2156 WARN_ON(fc
->iq
.fasync
!= NULL
);
2157 fuse_abort_conn(fc
);
2163 EXPORT_SYMBOL_GPL(fuse_dev_release
);
2165 static int fuse_dev_fasync(int fd
, struct file
*file
, int on
)
2167 struct fuse_dev
*fud
= fuse_get_dev(file
);
2172 /* No locking - fasync_helper does its own locking */
2173 return fasync_helper(fd
, file
, on
, &fud
->fc
->iq
.fasync
);
2176 static int fuse_device_clone(struct fuse_conn
*fc
, struct file
*new)
2178 struct fuse_dev
*fud
;
2180 if (new->private_data
)
2183 fud
= fuse_dev_alloc(fc
);
2187 new->private_data
= fud
;
2188 atomic_inc(&fc
->dev_count
);
2193 static long fuse_dev_ioctl(struct file
*file
, unsigned int cmd
,
2198 if (cmd
== FUSE_DEV_IOC_CLONE
) {
2202 if (!get_user(oldfd
, (__u32 __user
*) arg
)) {
2203 struct file
*old
= fget(oldfd
);
2207 struct fuse_dev
*fud
= NULL
;
2210 * Check against file->f_op because CUSE
2211 * uses the same ioctl handler.
2213 if (old
->f_op
== file
->f_op
&&
2214 old
->f_cred
->user_ns
== file
->f_cred
->user_ns
)
2215 fud
= fuse_get_dev(old
);
2218 mutex_lock(&fuse_mutex
);
2219 err
= fuse_device_clone(fud
->fc
, file
);
2220 mutex_unlock(&fuse_mutex
);
2229 const struct file_operations fuse_dev_operations
= {
2230 .owner
= THIS_MODULE
,
2231 .open
= fuse_dev_open
,
2232 .llseek
= no_llseek
,
2233 .read_iter
= fuse_dev_read
,
2234 .splice_read
= fuse_dev_splice_read
,
2235 .write_iter
= fuse_dev_write
,
2236 .splice_write
= fuse_dev_splice_write
,
2237 .poll
= fuse_dev_poll
,
2238 .release
= fuse_dev_release
,
2239 .fasync
= fuse_dev_fasync
,
2240 .unlocked_ioctl
= fuse_dev_ioctl
,
2241 .compat_ioctl
= fuse_dev_ioctl
,
2243 EXPORT_SYMBOL_GPL(fuse_dev_operations
);
2245 static struct miscdevice fuse_miscdevice
= {
2246 .minor
= FUSE_MINOR
,
2248 .fops
= &fuse_dev_operations
,
2251 int __init
fuse_dev_init(void)
2254 fuse_req_cachep
= kmem_cache_create("fuse_request",
2255 sizeof(struct fuse_req
),
2257 if (!fuse_req_cachep
)
2260 err
= misc_register(&fuse_miscdevice
);
2262 goto out_cache_clean
;
2267 kmem_cache_destroy(fuse_req_cachep
);
2272 void fuse_dev_cleanup(void)
2274 misc_deregister(&fuse_miscdevice
);
2275 kmem_cache_destroy(fuse_req_cachep
);