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/uio.h>
15 #include <linux/miscdevice.h>
16 #include <linux/pagemap.h>
17 #include <linux/file.h>
18 #include <linux/slab.h>
19 #include <linux/pipe_fs_i.h>
20 #include <linux/swap.h>
21 #include <linux/splice.h>
23 MODULE_ALIAS_MISCDEV(FUSE_MINOR
);
24 MODULE_ALIAS("devname:fuse");
26 static struct kmem_cache
*fuse_req_cachep
;
28 static struct fuse_dev
*fuse_get_dev(struct file
*file
)
31 * Lockless access is OK, because file->private data is set
32 * once during mount and is valid until the file is released.
34 return ACCESS_ONCE(file
->private_data
);
37 static void fuse_request_init(struct fuse_req
*req
, struct page
**pages
,
38 struct fuse_page_desc
*page_descs
,
41 memset(req
, 0, sizeof(*req
));
42 memset(pages
, 0, sizeof(*pages
) * npages
);
43 memset(page_descs
, 0, sizeof(*page_descs
) * npages
);
44 INIT_LIST_HEAD(&req
->list
);
45 INIT_LIST_HEAD(&req
->intr_entry
);
46 init_waitqueue_head(&req
->waitq
);
47 atomic_set(&req
->count
, 1);
49 req
->page_descs
= page_descs
;
50 req
->max_pages
= npages
;
51 __set_bit(FR_PENDING
, &req
->flags
);
54 static struct fuse_req
*__fuse_request_alloc(unsigned npages
, gfp_t flags
)
56 struct fuse_req
*req
= kmem_cache_alloc(fuse_req_cachep
, flags
);
59 struct fuse_page_desc
*page_descs
;
61 if (npages
<= FUSE_REQ_INLINE_PAGES
) {
62 pages
= req
->inline_pages
;
63 page_descs
= req
->inline_page_descs
;
65 pages
= kmalloc(sizeof(struct page
*) * npages
, flags
);
66 page_descs
= kmalloc(sizeof(struct fuse_page_desc
) *
70 if (!pages
|| !page_descs
) {
73 kmem_cache_free(fuse_req_cachep
, req
);
77 fuse_request_init(req
, pages
, page_descs
, npages
);
82 struct fuse_req
*fuse_request_alloc(unsigned npages
)
84 return __fuse_request_alloc(npages
, GFP_KERNEL
);
86 EXPORT_SYMBOL_GPL(fuse_request_alloc
);
88 struct fuse_req
*fuse_request_alloc_nofs(unsigned npages
)
90 return __fuse_request_alloc(npages
, GFP_NOFS
);
93 void fuse_request_free(struct fuse_req
*req
)
95 if (req
->pages
!= req
->inline_pages
) {
97 kfree(req
->page_descs
);
99 kmem_cache_free(fuse_req_cachep
, req
);
102 static void block_sigs(sigset_t
*oldset
)
106 siginitsetinv(&mask
, sigmask(SIGKILL
));
107 sigprocmask(SIG_BLOCK
, &mask
, oldset
);
110 static void restore_sigs(sigset_t
*oldset
)
112 sigprocmask(SIG_SETMASK
, oldset
, NULL
);
115 void __fuse_get_request(struct fuse_req
*req
)
117 atomic_inc(&req
->count
);
120 /* Must be called with > 1 refcount */
121 static void __fuse_put_request(struct fuse_req
*req
)
123 BUG_ON(atomic_read(&req
->count
) < 2);
124 atomic_dec(&req
->count
);
127 static void fuse_req_init_context(struct fuse_req
*req
)
129 req
->in
.h
.uid
= from_kuid_munged(&init_user_ns
, current_fsuid());
130 req
->in
.h
.gid
= from_kgid_munged(&init_user_ns
, current_fsgid());
131 req
->in
.h
.pid
= current
->pid
;
134 void fuse_set_initialized(struct fuse_conn
*fc
)
136 /* Make sure stores before this are seen on another CPU */
141 static bool fuse_block_alloc(struct fuse_conn
*fc
, bool for_background
)
143 return !fc
->initialized
|| (for_background
&& fc
->blocked
);
146 static void fuse_drop_waiting(struct fuse_conn
*fc
)
149 * lockess check of fc->connected is okay, because atomic_dec_and_test()
150 * provides a memory barrier mached with the one in fuse_wait_aborted()
151 * to ensure no wake-up is missed.
153 if (atomic_dec_and_test(&fc
->num_waiting
) &&
154 !READ_ONCE(fc
->connected
)) {
155 /* wake up aborters */
156 wake_up_all(&fc
->blocked_waitq
);
160 static struct fuse_req
*__fuse_get_req(struct fuse_conn
*fc
, unsigned npages
,
163 struct fuse_req
*req
;
165 atomic_inc(&fc
->num_waiting
);
167 if (fuse_block_alloc(fc
, for_background
)) {
172 intr
= wait_event_interruptible_exclusive(fc
->blocked_waitq
,
173 !fuse_block_alloc(fc
, for_background
));
174 restore_sigs(&oldset
);
179 /* Matches smp_wmb() in fuse_set_initialized() */
190 req
= fuse_request_alloc(npages
);
194 wake_up(&fc
->blocked_waitq
);
198 fuse_req_init_context(req
);
199 __set_bit(FR_WAITING
, &req
->flags
);
201 __set_bit(FR_BACKGROUND
, &req
->flags
);
206 fuse_drop_waiting(fc
);
210 struct fuse_req
*fuse_get_req(struct fuse_conn
*fc
, unsigned npages
)
212 return __fuse_get_req(fc
, npages
, false);
214 EXPORT_SYMBOL_GPL(fuse_get_req
);
216 struct fuse_req
*fuse_get_req_for_background(struct fuse_conn
*fc
,
219 return __fuse_get_req(fc
, npages
, true);
221 EXPORT_SYMBOL_GPL(fuse_get_req_for_background
);
224 * Return request in fuse_file->reserved_req. However that may
225 * currently be in use. If that is the case, wait for it to become
228 static struct fuse_req
*get_reserved_req(struct fuse_conn
*fc
,
231 struct fuse_req
*req
= NULL
;
232 struct fuse_file
*ff
= file
->private_data
;
235 wait_event(fc
->reserved_req_waitq
, ff
->reserved_req
);
236 spin_lock(&fc
->lock
);
237 if (ff
->reserved_req
) {
238 req
= ff
->reserved_req
;
239 ff
->reserved_req
= NULL
;
240 req
->stolen_file
= get_file(file
);
242 spin_unlock(&fc
->lock
);
249 * Put stolen request back into fuse_file->reserved_req
251 static void put_reserved_req(struct fuse_conn
*fc
, struct fuse_req
*req
)
253 struct file
*file
= req
->stolen_file
;
254 struct fuse_file
*ff
= file
->private_data
;
256 spin_lock(&fc
->lock
);
257 fuse_request_init(req
, req
->pages
, req
->page_descs
, req
->max_pages
);
258 BUG_ON(ff
->reserved_req
);
259 ff
->reserved_req
= req
;
260 wake_up_all(&fc
->reserved_req_waitq
);
261 spin_unlock(&fc
->lock
);
266 * Gets a requests for a file operation, always succeeds
268 * This is used for sending the FLUSH request, which must get to
269 * userspace, due to POSIX locks which may need to be unlocked.
271 * If allocation fails due to OOM, use the reserved request in
274 * This is very unlikely to deadlock accidentally, since the
275 * filesystem should not have it's own file open. If deadlock is
276 * intentional, it can still be broken by "aborting" the filesystem.
278 struct fuse_req
*fuse_get_req_nofail_nopages(struct fuse_conn
*fc
,
281 struct fuse_req
*req
;
283 atomic_inc(&fc
->num_waiting
);
284 wait_event(fc
->blocked_waitq
, fc
->initialized
);
285 /* Matches smp_wmb() in fuse_set_initialized() */
287 req
= fuse_request_alloc(0);
289 req
= get_reserved_req(fc
, file
);
291 fuse_req_init_context(req
);
292 __set_bit(FR_WAITING
, &req
->flags
);
293 __clear_bit(FR_BACKGROUND
, &req
->flags
);
297 void fuse_put_request(struct fuse_conn
*fc
, struct fuse_req
*req
)
299 if (atomic_dec_and_test(&req
->count
)) {
300 if (test_bit(FR_BACKGROUND
, &req
->flags
)) {
302 * We get here in the unlikely case that a background
303 * request was allocated but not sent
305 spin_lock(&fc
->lock
);
307 wake_up(&fc
->blocked_waitq
);
308 spin_unlock(&fc
->lock
);
311 if (test_bit(FR_WAITING
, &req
->flags
)) {
312 __clear_bit(FR_WAITING
, &req
->flags
);
313 fuse_drop_waiting(fc
);
316 if (req
->stolen_file
)
317 put_reserved_req(fc
, req
);
319 fuse_request_free(req
);
322 EXPORT_SYMBOL_GPL(fuse_put_request
);
324 static unsigned len_args(unsigned numargs
, struct fuse_arg
*args
)
329 for (i
= 0; i
< numargs
; i
++)
330 nbytes
+= args
[i
].size
;
335 static u64
fuse_get_unique(struct fuse_iqueue
*fiq
)
337 return ++fiq
->reqctr
;
340 static void queue_request(struct fuse_iqueue
*fiq
, struct fuse_req
*req
)
342 req
->in
.h
.len
= sizeof(struct fuse_in_header
) +
343 len_args(req
->in
.numargs
, (struct fuse_arg
*) req
->in
.args
);
344 list_add_tail(&req
->list
, &fiq
->pending
);
345 wake_up_locked(&fiq
->waitq
);
346 kill_fasync(&fiq
->fasync
, SIGIO
, POLL_IN
);
349 void fuse_queue_forget(struct fuse_conn
*fc
, struct fuse_forget_link
*forget
,
350 u64 nodeid
, u64 nlookup
)
352 struct fuse_iqueue
*fiq
= &fc
->iq
;
354 forget
->forget_one
.nodeid
= nodeid
;
355 forget
->forget_one
.nlookup
= nlookup
;
357 spin_lock(&fiq
->waitq
.lock
);
358 if (fiq
->connected
) {
359 fiq
->forget_list_tail
->next
= forget
;
360 fiq
->forget_list_tail
= forget
;
361 wake_up_locked(&fiq
->waitq
);
362 kill_fasync(&fiq
->fasync
, SIGIO
, POLL_IN
);
366 spin_unlock(&fiq
->waitq
.lock
);
369 static void flush_bg_queue(struct fuse_conn
*fc
)
371 while (fc
->active_background
< fc
->max_background
&&
372 !list_empty(&fc
->bg_queue
)) {
373 struct fuse_req
*req
;
374 struct fuse_iqueue
*fiq
= &fc
->iq
;
376 req
= list_entry(fc
->bg_queue
.next
, struct fuse_req
, list
);
377 list_del(&req
->list
);
378 fc
->active_background
++;
379 spin_lock(&fiq
->waitq
.lock
);
380 req
->in
.h
.unique
= fuse_get_unique(fiq
);
381 queue_request(fiq
, req
);
382 spin_unlock(&fiq
->waitq
.lock
);
387 * This function is called when a request is finished. Either a reply
388 * has arrived or it was aborted (and not yet sent) or some error
389 * occurred during communication with userspace, or the device file
390 * was closed. The requester thread is woken up (if still waiting),
391 * the 'end' callback is called if given, else the reference to the
392 * request is released
394 static void request_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
396 struct fuse_iqueue
*fiq
= &fc
->iq
;
398 if (test_and_set_bit(FR_FINISHED
, &req
->flags
))
401 spin_lock(&fiq
->waitq
.lock
);
402 list_del_init(&req
->intr_entry
);
403 spin_unlock(&fiq
->waitq
.lock
);
404 WARN_ON(test_bit(FR_PENDING
, &req
->flags
));
405 WARN_ON(test_bit(FR_SENT
, &req
->flags
));
406 if (test_bit(FR_BACKGROUND
, &req
->flags
)) {
407 spin_lock(&fc
->lock
);
408 clear_bit(FR_BACKGROUND
, &req
->flags
);
409 if (fc
->num_background
== fc
->max_background
) {
411 wake_up(&fc
->blocked_waitq
);
412 } else if (!fc
->blocked
) {
414 * Wake up next waiter, if any. It's okay to use
415 * waitqueue_active(), as we've already synced up
416 * fc->blocked with waiters with the wake_up() call
419 if (waitqueue_active(&fc
->blocked_waitq
))
420 wake_up(&fc
->blocked_waitq
);
423 if (fc
->num_background
== fc
->congestion_threshold
&&
424 fc
->connected
&& fc
->bdi_initialized
) {
425 clear_bdi_congested(&fc
->bdi
, BLK_RW_SYNC
);
426 clear_bdi_congested(&fc
->bdi
, BLK_RW_ASYNC
);
428 fc
->num_background
--;
429 fc
->active_background
--;
431 spin_unlock(&fc
->lock
);
433 wake_up(&req
->waitq
);
437 fuse_put_request(fc
, req
);
440 static void queue_interrupt(struct fuse_iqueue
*fiq
, struct fuse_req
*req
)
442 spin_lock(&fiq
->waitq
.lock
);
443 if (test_bit(FR_FINISHED
, &req
->flags
)) {
444 spin_unlock(&fiq
->waitq
.lock
);
447 if (list_empty(&req
->intr_entry
)) {
448 list_add_tail(&req
->intr_entry
, &fiq
->interrupts
);
449 wake_up_locked(&fiq
->waitq
);
451 spin_unlock(&fiq
->waitq
.lock
);
452 kill_fasync(&fiq
->fasync
, SIGIO
, POLL_IN
);
455 static void request_wait_answer(struct fuse_conn
*fc
, struct fuse_req
*req
)
457 struct fuse_iqueue
*fiq
= &fc
->iq
;
460 if (!fc
->no_interrupt
) {
461 /* Any signal may interrupt this */
462 err
= wait_event_interruptible(req
->waitq
,
463 test_bit(FR_FINISHED
, &req
->flags
));
467 set_bit(FR_INTERRUPTED
, &req
->flags
);
468 /* matches barrier in fuse_dev_do_read() */
469 smp_mb__after_atomic();
470 if (test_bit(FR_SENT
, &req
->flags
))
471 queue_interrupt(fiq
, req
);
474 if (!test_bit(FR_FORCE
, &req
->flags
)) {
477 /* Only fatal signals may interrupt this */
479 err
= wait_event_interruptible(req
->waitq
,
480 test_bit(FR_FINISHED
, &req
->flags
));
481 restore_sigs(&oldset
);
486 spin_lock(&fiq
->waitq
.lock
);
487 /* Request is not yet in userspace, bail out */
488 if (test_bit(FR_PENDING
, &req
->flags
)) {
489 list_del(&req
->list
);
490 spin_unlock(&fiq
->waitq
.lock
);
491 __fuse_put_request(req
);
492 req
->out
.h
.error
= -EINTR
;
495 spin_unlock(&fiq
->waitq
.lock
);
499 * Either request is already in userspace, or it was forced.
502 wait_event(req
->waitq
, test_bit(FR_FINISHED
, &req
->flags
));
505 static void __fuse_request_send(struct fuse_conn
*fc
, struct fuse_req
*req
)
507 struct fuse_iqueue
*fiq
= &fc
->iq
;
509 BUG_ON(test_bit(FR_BACKGROUND
, &req
->flags
));
510 spin_lock(&fiq
->waitq
.lock
);
511 if (!fiq
->connected
) {
512 spin_unlock(&fiq
->waitq
.lock
);
513 req
->out
.h
.error
= -ENOTCONN
;
515 req
->in
.h
.unique
= fuse_get_unique(fiq
);
516 queue_request(fiq
, req
);
517 /* acquire extra reference, since request is still needed
518 after request_end() */
519 __fuse_get_request(req
);
520 spin_unlock(&fiq
->waitq
.lock
);
522 request_wait_answer(fc
, req
);
523 /* Pairs with smp_wmb() in request_end() */
528 void fuse_request_send(struct fuse_conn
*fc
, struct fuse_req
*req
)
530 __set_bit(FR_ISREPLY
, &req
->flags
);
531 if (!test_bit(FR_WAITING
, &req
->flags
)) {
532 __set_bit(FR_WAITING
, &req
->flags
);
533 atomic_inc(&fc
->num_waiting
);
535 __fuse_request_send(fc
, req
);
537 EXPORT_SYMBOL_GPL(fuse_request_send
);
539 static void fuse_adjust_compat(struct fuse_conn
*fc
, struct fuse_args
*args
)
541 if (fc
->minor
< 4 && args
->in
.h
.opcode
== FUSE_STATFS
)
542 args
->out
.args
[0].size
= FUSE_COMPAT_STATFS_SIZE
;
545 switch (args
->in
.h
.opcode
) {
552 args
->out
.args
[0].size
= FUSE_COMPAT_ENTRY_OUT_SIZE
;
556 args
->out
.args
[0].size
= FUSE_COMPAT_ATTR_OUT_SIZE
;
560 if (fc
->minor
< 12) {
561 switch (args
->in
.h
.opcode
) {
563 args
->in
.args
[0].size
= sizeof(struct fuse_open_in
);
566 args
->in
.args
[0].size
= FUSE_COMPAT_MKNOD_IN_SIZE
;
572 ssize_t
fuse_simple_request(struct fuse_conn
*fc
, struct fuse_args
*args
)
574 struct fuse_req
*req
;
577 req
= fuse_get_req(fc
, 0);
581 /* Needs to be done after fuse_get_req() so that fc->minor is valid */
582 fuse_adjust_compat(fc
, args
);
584 req
->in
.h
.opcode
= args
->in
.h
.opcode
;
585 req
->in
.h
.nodeid
= args
->in
.h
.nodeid
;
586 req
->in
.numargs
= args
->in
.numargs
;
587 memcpy(req
->in
.args
, args
->in
.args
,
588 args
->in
.numargs
* sizeof(struct fuse_in_arg
));
589 req
->out
.argvar
= args
->out
.argvar
;
590 req
->out
.numargs
= args
->out
.numargs
;
591 memcpy(req
->out
.args
, args
->out
.args
,
592 args
->out
.numargs
* sizeof(struct fuse_arg
));
593 fuse_request_send(fc
, req
);
594 ret
= req
->out
.h
.error
;
595 if (!ret
&& args
->out
.argvar
) {
596 BUG_ON(args
->out
.numargs
!= 1);
597 ret
= req
->out
.args
[0].size
;
599 fuse_put_request(fc
, req
);
605 * Called under fc->lock
607 * fc->connected must have been checked previously
609 void fuse_request_send_background_locked(struct fuse_conn
*fc
,
610 struct fuse_req
*req
)
612 BUG_ON(!test_bit(FR_BACKGROUND
, &req
->flags
));
613 if (!test_bit(FR_WAITING
, &req
->flags
)) {
614 __set_bit(FR_WAITING
, &req
->flags
);
615 atomic_inc(&fc
->num_waiting
);
617 __set_bit(FR_ISREPLY
, &req
->flags
);
618 fc
->num_background
++;
619 if (fc
->num_background
== fc
->max_background
)
621 if (fc
->num_background
== fc
->congestion_threshold
&&
622 fc
->bdi_initialized
) {
623 set_bdi_congested(&fc
->bdi
, BLK_RW_SYNC
);
624 set_bdi_congested(&fc
->bdi
, BLK_RW_ASYNC
);
626 list_add_tail(&req
->list
, &fc
->bg_queue
);
630 void fuse_request_send_background(struct fuse_conn
*fc
, struct fuse_req
*req
)
633 spin_lock(&fc
->lock
);
635 fuse_request_send_background_locked(fc
, req
);
636 spin_unlock(&fc
->lock
);
638 spin_unlock(&fc
->lock
);
639 req
->out
.h
.error
= -ENOTCONN
;
641 fuse_put_request(fc
, req
);
644 EXPORT_SYMBOL_GPL(fuse_request_send_background
);
646 static int fuse_request_send_notify_reply(struct fuse_conn
*fc
,
647 struct fuse_req
*req
, u64 unique
)
650 struct fuse_iqueue
*fiq
= &fc
->iq
;
652 __clear_bit(FR_ISREPLY
, &req
->flags
);
653 req
->in
.h
.unique
= unique
;
654 spin_lock(&fiq
->waitq
.lock
);
655 if (fiq
->connected
) {
656 queue_request(fiq
, req
);
659 spin_unlock(&fiq
->waitq
.lock
);
664 void fuse_force_forget(struct file
*file
, u64 nodeid
)
666 struct inode
*inode
= file_inode(file
);
667 struct fuse_conn
*fc
= get_fuse_conn(inode
);
668 struct fuse_req
*req
;
669 struct fuse_forget_in inarg
;
671 memset(&inarg
, 0, sizeof(inarg
));
673 req
= fuse_get_req_nofail_nopages(fc
, file
);
674 req
->in
.h
.opcode
= FUSE_FORGET
;
675 req
->in
.h
.nodeid
= nodeid
;
677 req
->in
.args
[0].size
= sizeof(inarg
);
678 req
->in
.args
[0].value
= &inarg
;
679 __clear_bit(FR_ISREPLY
, &req
->flags
);
680 __fuse_request_send(fc
, req
);
682 fuse_put_request(fc
, req
);
686 * Lock the request. Up to the next unlock_request() there mustn't be
687 * anything that could cause a page-fault. If the request was already
690 static int lock_request(struct fuse_req
*req
)
694 spin_lock(&req
->waitq
.lock
);
695 if (test_bit(FR_ABORTED
, &req
->flags
))
698 set_bit(FR_LOCKED
, &req
->flags
);
699 spin_unlock(&req
->waitq
.lock
);
705 * Unlock request. If it was aborted while locked, caller is responsible
706 * for unlocking and ending the request.
708 static int unlock_request(struct fuse_req
*req
)
712 spin_lock(&req
->waitq
.lock
);
713 if (test_bit(FR_ABORTED
, &req
->flags
))
716 clear_bit(FR_LOCKED
, &req
->flags
);
717 spin_unlock(&req
->waitq
.lock
);
722 struct fuse_copy_state
{
724 struct fuse_req
*req
;
725 struct iov_iter
*iter
;
726 struct pipe_buffer
*pipebufs
;
727 struct pipe_buffer
*currbuf
;
728 struct pipe_inode_info
*pipe
;
729 unsigned long nr_segs
;
733 unsigned move_pages
:1;
736 static void fuse_copy_init(struct fuse_copy_state
*cs
, int write
,
737 struct iov_iter
*iter
)
739 memset(cs
, 0, sizeof(*cs
));
744 /* Unmap and put previous page of userspace buffer */
745 static void fuse_copy_finish(struct fuse_copy_state
*cs
)
748 struct pipe_buffer
*buf
= cs
->currbuf
;
751 buf
->len
= PAGE_SIZE
- cs
->len
;
755 flush_dcache_page(cs
->pg
);
756 set_page_dirty_lock(cs
->pg
);
764 * Get another pagefull of userspace buffer, and map it to kernel
765 * address space, and lock request
767 static int fuse_copy_fill(struct fuse_copy_state
*cs
)
772 err
= unlock_request(cs
->req
);
776 fuse_copy_finish(cs
);
778 struct pipe_buffer
*buf
= cs
->pipebufs
;
781 err
= buf
->ops
->confirm(cs
->pipe
, buf
);
785 BUG_ON(!cs
->nr_segs
);
788 cs
->offset
= buf
->offset
;
793 if (cs
->nr_segs
== cs
->pipe
->buffers
)
796 page
= alloc_page(GFP_HIGHUSER
);
813 err
= iov_iter_get_pages(cs
->iter
, &page
, PAGE_SIZE
, 1, &off
);
821 iov_iter_advance(cs
->iter
, err
);
824 return lock_request(cs
->req
);
827 /* Do as much copy to/from userspace buffer as we can */
828 static int fuse_copy_do(struct fuse_copy_state
*cs
, void **val
, unsigned *size
)
830 unsigned ncpy
= min(*size
, cs
->len
);
832 void *pgaddr
= kmap_atomic(cs
->pg
);
833 void *buf
= pgaddr
+ cs
->offset
;
836 memcpy(buf
, *val
, ncpy
);
838 memcpy(*val
, buf
, ncpy
);
840 kunmap_atomic(pgaddr
);
849 static int fuse_check_page(struct page
*page
)
851 if (page_mapcount(page
) ||
852 page
->mapping
!= NULL
||
853 page_count(page
) != 1 ||
854 (page
->flags
& PAGE_FLAGS_CHECK_AT_PREP
&
861 printk(KERN_WARNING
"fuse: trying to steal weird page\n");
862 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
);
868 static int fuse_try_move_page(struct fuse_copy_state
*cs
, struct page
**pagep
)
871 struct page
*oldpage
= *pagep
;
872 struct page
*newpage
;
873 struct pipe_buffer
*buf
= cs
->pipebufs
;
875 err
= unlock_request(cs
->req
);
879 fuse_copy_finish(cs
);
881 err
= buf
->ops
->confirm(cs
->pipe
, buf
);
885 BUG_ON(!cs
->nr_segs
);
891 if (cs
->len
!= PAGE_SIZE
)
894 if (buf
->ops
->steal(cs
->pipe
, buf
) != 0)
899 if (!PageUptodate(newpage
))
900 SetPageUptodate(newpage
);
902 ClearPageMappedToDisk(newpage
);
904 if (fuse_check_page(newpage
) != 0)
905 goto out_fallback_unlock
;
908 * This is a new and locked page, it shouldn't be mapped or
909 * have any special flags on it
911 if (WARN_ON(page_mapped(oldpage
)))
912 goto out_fallback_unlock
;
913 if (WARN_ON(page_has_private(oldpage
)))
914 goto out_fallback_unlock
;
915 if (WARN_ON(PageDirty(oldpage
) || PageWriteback(oldpage
)))
916 goto out_fallback_unlock
;
917 if (WARN_ON(PageMlocked(oldpage
)))
918 goto out_fallback_unlock
;
920 err
= replace_page_cache_page(oldpage
, newpage
, GFP_KERNEL
);
922 unlock_page(newpage
);
926 page_cache_get(newpage
);
928 if (!(buf
->flags
& PIPE_BUF_FLAG_LRU
))
929 lru_cache_add_file(newpage
);
932 spin_lock(&cs
->req
->waitq
.lock
);
933 if (test_bit(FR_ABORTED
, &cs
->req
->flags
))
937 spin_unlock(&cs
->req
->waitq
.lock
);
940 unlock_page(newpage
);
941 page_cache_release(newpage
);
945 unlock_page(oldpage
);
946 page_cache_release(oldpage
);
952 unlock_page(newpage
);
955 cs
->offset
= buf
->offset
;
957 err
= lock_request(cs
->req
);
964 static int fuse_ref_page(struct fuse_copy_state
*cs
, struct page
*page
,
965 unsigned offset
, unsigned count
)
967 struct pipe_buffer
*buf
;
970 if (cs
->nr_segs
== cs
->pipe
->buffers
)
973 err
= unlock_request(cs
->req
);
977 fuse_copy_finish(cs
);
980 page_cache_get(page
);
982 buf
->offset
= offset
;
993 * Copy a page in the request to/from the userspace buffer. Must be
996 static int fuse_copy_page(struct fuse_copy_state
*cs
, struct page
**pagep
,
997 unsigned offset
, unsigned count
, int zeroing
)
1000 struct page
*page
= *pagep
;
1002 if (page
&& zeroing
&& count
< PAGE_SIZE
)
1003 clear_highpage(page
);
1006 if (cs
->write
&& cs
->pipebufs
&& page
) {
1007 return fuse_ref_page(cs
, page
, offset
, count
);
1008 } else if (!cs
->len
) {
1009 if (cs
->move_pages
&& page
&&
1010 offset
== 0 && count
== PAGE_SIZE
) {
1011 err
= fuse_try_move_page(cs
, pagep
);
1015 err
= fuse_copy_fill(cs
);
1021 void *mapaddr
= kmap_atomic(page
);
1022 void *buf
= mapaddr
+ offset
;
1023 offset
+= fuse_copy_do(cs
, &buf
, &count
);
1024 kunmap_atomic(mapaddr
);
1026 offset
+= fuse_copy_do(cs
, NULL
, &count
);
1028 if (page
&& !cs
->write
)
1029 flush_dcache_page(page
);
1033 /* Copy pages in the request to/from userspace buffer */
1034 static int fuse_copy_pages(struct fuse_copy_state
*cs
, unsigned nbytes
,
1038 struct fuse_req
*req
= cs
->req
;
1040 for (i
= 0; i
< req
->num_pages
&& (nbytes
|| zeroing
); i
++) {
1042 unsigned offset
= req
->page_descs
[i
].offset
;
1043 unsigned count
= min(nbytes
, req
->page_descs
[i
].length
);
1045 err
= fuse_copy_page(cs
, &req
->pages
[i
], offset
, count
,
1055 /* Copy a single argument in the request to/from userspace buffer */
1056 static int fuse_copy_one(struct fuse_copy_state
*cs
, void *val
, unsigned size
)
1060 int err
= fuse_copy_fill(cs
);
1064 fuse_copy_do(cs
, &val
, &size
);
1069 /* Copy request arguments to/from userspace buffer */
1070 static int fuse_copy_args(struct fuse_copy_state
*cs
, unsigned numargs
,
1071 unsigned argpages
, struct fuse_arg
*args
,
1077 for (i
= 0; !err
&& i
< numargs
; i
++) {
1078 struct fuse_arg
*arg
= &args
[i
];
1079 if (i
== numargs
- 1 && argpages
)
1080 err
= fuse_copy_pages(cs
, arg
->size
, zeroing
);
1082 err
= fuse_copy_one(cs
, arg
->value
, arg
->size
);
1087 static int forget_pending(struct fuse_iqueue
*fiq
)
1089 return fiq
->forget_list_head
.next
!= NULL
;
1092 static int request_pending(struct fuse_iqueue
*fiq
)
1094 return !list_empty(&fiq
->pending
) || !list_empty(&fiq
->interrupts
) ||
1095 forget_pending(fiq
);
1099 * Transfer an interrupt request to userspace
1101 * Unlike other requests this is assembled on demand, without a need
1102 * to allocate a separate fuse_req structure.
1104 * Called with fiq->waitq.lock held, releases it
1106 static int fuse_read_interrupt(struct fuse_iqueue
*fiq
,
1107 struct fuse_copy_state
*cs
,
1108 size_t nbytes
, struct fuse_req
*req
)
1109 __releases(fiq
->waitq
.lock
)
1111 struct fuse_in_header ih
;
1112 struct fuse_interrupt_in arg
;
1113 unsigned reqsize
= sizeof(ih
) + sizeof(arg
);
1116 list_del_init(&req
->intr_entry
);
1117 req
->intr_unique
= fuse_get_unique(fiq
);
1118 memset(&ih
, 0, sizeof(ih
));
1119 memset(&arg
, 0, sizeof(arg
));
1121 ih
.opcode
= FUSE_INTERRUPT
;
1122 ih
.unique
= req
->intr_unique
;
1123 arg
.unique
= req
->in
.h
.unique
;
1125 spin_unlock(&fiq
->waitq
.lock
);
1126 if (nbytes
< reqsize
)
1129 err
= fuse_copy_one(cs
, &ih
, sizeof(ih
));
1131 err
= fuse_copy_one(cs
, &arg
, sizeof(arg
));
1132 fuse_copy_finish(cs
);
1134 return err
? err
: reqsize
;
1137 static struct fuse_forget_link
*dequeue_forget(struct fuse_iqueue
*fiq
,
1141 struct fuse_forget_link
*head
= fiq
->forget_list_head
.next
;
1142 struct fuse_forget_link
**newhead
= &head
;
1145 for (count
= 0; *newhead
!= NULL
&& count
< max
; count
++)
1146 newhead
= &(*newhead
)->next
;
1148 fiq
->forget_list_head
.next
= *newhead
;
1150 if (fiq
->forget_list_head
.next
== NULL
)
1151 fiq
->forget_list_tail
= &fiq
->forget_list_head
;
1159 static int fuse_read_single_forget(struct fuse_iqueue
*fiq
,
1160 struct fuse_copy_state
*cs
,
1162 __releases(fiq
->waitq
.lock
)
1165 struct fuse_forget_link
*forget
= dequeue_forget(fiq
, 1, NULL
);
1166 struct fuse_forget_in arg
= {
1167 .nlookup
= forget
->forget_one
.nlookup
,
1169 struct fuse_in_header ih
= {
1170 .opcode
= FUSE_FORGET
,
1171 .nodeid
= forget
->forget_one
.nodeid
,
1172 .unique
= fuse_get_unique(fiq
),
1173 .len
= sizeof(ih
) + sizeof(arg
),
1176 spin_unlock(&fiq
->waitq
.lock
);
1178 if (nbytes
< ih
.len
)
1181 err
= fuse_copy_one(cs
, &ih
, sizeof(ih
));
1183 err
= fuse_copy_one(cs
, &arg
, sizeof(arg
));
1184 fuse_copy_finish(cs
);
1192 static int fuse_read_batch_forget(struct fuse_iqueue
*fiq
,
1193 struct fuse_copy_state
*cs
, size_t nbytes
)
1194 __releases(fiq
->waitq
.lock
)
1197 unsigned max_forgets
;
1199 struct fuse_forget_link
*head
;
1200 struct fuse_batch_forget_in arg
= { .count
= 0 };
1201 struct fuse_in_header ih
= {
1202 .opcode
= FUSE_BATCH_FORGET
,
1203 .unique
= fuse_get_unique(fiq
),
1204 .len
= sizeof(ih
) + sizeof(arg
),
1207 if (nbytes
< ih
.len
) {
1208 spin_unlock(&fiq
->waitq
.lock
);
1212 max_forgets
= (nbytes
- ih
.len
) / sizeof(struct fuse_forget_one
);
1213 head
= dequeue_forget(fiq
, max_forgets
, &count
);
1214 spin_unlock(&fiq
->waitq
.lock
);
1217 ih
.len
+= count
* sizeof(struct fuse_forget_one
);
1218 err
= fuse_copy_one(cs
, &ih
, sizeof(ih
));
1220 err
= fuse_copy_one(cs
, &arg
, sizeof(arg
));
1223 struct fuse_forget_link
*forget
= head
;
1226 err
= fuse_copy_one(cs
, &forget
->forget_one
,
1227 sizeof(forget
->forget_one
));
1229 head
= forget
->next
;
1233 fuse_copy_finish(cs
);
1241 static int fuse_read_forget(struct fuse_conn
*fc
, struct fuse_iqueue
*fiq
,
1242 struct fuse_copy_state
*cs
,
1244 __releases(fiq
->waitq
.lock
)
1246 if (fc
->minor
< 16 || fiq
->forget_list_head
.next
->next
== NULL
)
1247 return fuse_read_single_forget(fiq
, cs
, nbytes
);
1249 return fuse_read_batch_forget(fiq
, cs
, nbytes
);
1253 * Read a single request into the userspace filesystem's buffer. This
1254 * function waits until a request is available, then removes it from
1255 * the pending list and copies request data to userspace buffer. If
1256 * no reply is needed (FORGET) or request has been aborted or there
1257 * was an error during the copying then it's finished by calling
1258 * request_end(). Otherwise add it to the processing list, and set
1261 static ssize_t
fuse_dev_do_read(struct fuse_dev
*fud
, struct file
*file
,
1262 struct fuse_copy_state
*cs
, size_t nbytes
)
1265 struct fuse_conn
*fc
= fud
->fc
;
1266 struct fuse_iqueue
*fiq
= &fc
->iq
;
1267 struct fuse_pqueue
*fpq
= &fud
->pq
;
1268 struct fuse_req
*req
;
1273 spin_lock(&fiq
->waitq
.lock
);
1275 if ((file
->f_flags
& O_NONBLOCK
) && fiq
->connected
&&
1276 !request_pending(fiq
))
1279 err
= wait_event_interruptible_exclusive_locked(fiq
->waitq
,
1280 !fiq
->connected
|| request_pending(fiq
));
1285 if (!fiq
->connected
)
1288 if (!list_empty(&fiq
->interrupts
)) {
1289 req
= list_entry(fiq
->interrupts
.next
, struct fuse_req
,
1291 return fuse_read_interrupt(fiq
, cs
, nbytes
, req
);
1294 if (forget_pending(fiq
)) {
1295 if (list_empty(&fiq
->pending
) || fiq
->forget_batch
-- > 0)
1296 return fuse_read_forget(fc
, fiq
, cs
, nbytes
);
1298 if (fiq
->forget_batch
<= -8)
1299 fiq
->forget_batch
= 16;
1302 req
= list_entry(fiq
->pending
.next
, struct fuse_req
, list
);
1303 clear_bit(FR_PENDING
, &req
->flags
);
1304 list_del_init(&req
->list
);
1305 spin_unlock(&fiq
->waitq
.lock
);
1308 reqsize
= in
->h
.len
;
1309 /* If request is too large, reply with an error and restart the read */
1310 if (nbytes
< reqsize
) {
1311 req
->out
.h
.error
= -EIO
;
1312 /* SETXATTR is special, since it may contain too large data */
1313 if (in
->h
.opcode
== FUSE_SETXATTR
)
1314 req
->out
.h
.error
= -E2BIG
;
1315 request_end(fc
, req
);
1318 spin_lock(&fpq
->lock
);
1319 list_add(&req
->list
, &fpq
->io
);
1320 spin_unlock(&fpq
->lock
);
1322 err
= fuse_copy_one(cs
, &in
->h
, sizeof(in
->h
));
1324 err
= fuse_copy_args(cs
, in
->numargs
, in
->argpages
,
1325 (struct fuse_arg
*) in
->args
, 0);
1326 fuse_copy_finish(cs
);
1327 spin_lock(&fpq
->lock
);
1328 clear_bit(FR_LOCKED
, &req
->flags
);
1329 if (!fpq
->connected
) {
1334 req
->out
.h
.error
= -EIO
;
1337 if (!test_bit(FR_ISREPLY
, &req
->flags
)) {
1341 list_move_tail(&req
->list
, &fpq
->processing
);
1342 __fuse_get_request(req
);
1343 set_bit(FR_SENT
, &req
->flags
);
1344 spin_unlock(&fpq
->lock
);
1345 /* matches barrier in request_wait_answer() */
1346 smp_mb__after_atomic();
1347 if (test_bit(FR_INTERRUPTED
, &req
->flags
))
1348 queue_interrupt(fiq
, req
);
1349 fuse_put_request(fc
, req
);
1354 if (!test_bit(FR_PRIVATE
, &req
->flags
))
1355 list_del_init(&req
->list
);
1356 spin_unlock(&fpq
->lock
);
1357 request_end(fc
, req
);
1361 spin_unlock(&fiq
->waitq
.lock
);
1365 static int fuse_dev_open(struct inode
*inode
, struct file
*file
)
1368 * The fuse device's file's private_data is used to hold
1369 * the fuse_conn(ection) when it is mounted, and is used to
1370 * keep track of whether the file has been mounted already.
1372 file
->private_data
= NULL
;
1376 static ssize_t
fuse_dev_read(struct kiocb
*iocb
, struct iov_iter
*to
)
1378 struct fuse_copy_state cs
;
1379 struct file
*file
= iocb
->ki_filp
;
1380 struct fuse_dev
*fud
= fuse_get_dev(file
);
1385 if (!iter_is_iovec(to
))
1388 fuse_copy_init(&cs
, 1, to
);
1390 return fuse_dev_do_read(fud
, file
, &cs
, iov_iter_count(to
));
1393 static ssize_t
fuse_dev_splice_read(struct file
*in
, loff_t
*ppos
,
1394 struct pipe_inode_info
*pipe
,
1395 size_t len
, unsigned int flags
)
1400 struct pipe_buffer
*bufs
;
1401 struct fuse_copy_state cs
;
1402 struct fuse_dev
*fud
= fuse_get_dev(in
);
1407 bufs
= kmalloc(pipe
->buffers
* sizeof(struct pipe_buffer
), GFP_KERNEL
);
1411 fuse_copy_init(&cs
, 1, NULL
);
1414 ret
= fuse_dev_do_read(fud
, in
, &cs
, len
);
1421 if (!pipe
->readers
) {
1422 send_sig(SIGPIPE
, current
, 0);
1428 if (pipe
->nrbufs
+ cs
.nr_segs
> pipe
->buffers
) {
1433 while (page_nr
< cs
.nr_segs
) {
1434 int newbuf
= (pipe
->curbuf
+ pipe
->nrbufs
) & (pipe
->buffers
- 1);
1435 struct pipe_buffer
*buf
= pipe
->bufs
+ newbuf
;
1437 buf
->page
= bufs
[page_nr
].page
;
1438 buf
->offset
= bufs
[page_nr
].offset
;
1439 buf
->len
= bufs
[page_nr
].len
;
1441 * Need to be careful about this. Having buf->ops in module
1442 * code can Oops if the buffer persists after module unload.
1444 buf
->ops
= &nosteal_pipe_buf_ops
;
1459 if (waitqueue_active(&pipe
->wait
))
1460 wake_up_interruptible(&pipe
->wait
);
1461 kill_fasync(&pipe
->fasync_readers
, SIGIO
, POLL_IN
);
1465 for (; page_nr
< cs
.nr_segs
; page_nr
++)
1466 page_cache_release(bufs
[page_nr
].page
);
1472 static int fuse_notify_poll(struct fuse_conn
*fc
, unsigned int size
,
1473 struct fuse_copy_state
*cs
)
1475 struct fuse_notify_poll_wakeup_out outarg
;
1478 if (size
!= sizeof(outarg
))
1481 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1485 fuse_copy_finish(cs
);
1486 return fuse_notify_poll_wakeup(fc
, &outarg
);
1489 fuse_copy_finish(cs
);
1493 static int fuse_notify_inval_inode(struct fuse_conn
*fc
, unsigned int size
,
1494 struct fuse_copy_state
*cs
)
1496 struct fuse_notify_inval_inode_out outarg
;
1499 if (size
!= sizeof(outarg
))
1502 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1505 fuse_copy_finish(cs
);
1507 down_read(&fc
->killsb
);
1510 err
= fuse_reverse_inval_inode(fc
->sb
, outarg
.ino
,
1511 outarg
.off
, outarg
.len
);
1513 up_read(&fc
->killsb
);
1517 fuse_copy_finish(cs
);
1521 static int fuse_notify_inval_entry(struct fuse_conn
*fc
, unsigned int size
,
1522 struct fuse_copy_state
*cs
)
1524 struct fuse_notify_inval_entry_out outarg
;
1529 buf
= kzalloc(FUSE_NAME_MAX
+ 1, GFP_KERNEL
);
1534 if (size
< sizeof(outarg
))
1537 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1541 err
= -ENAMETOOLONG
;
1542 if (outarg
.namelen
> FUSE_NAME_MAX
)
1546 if (size
!= sizeof(outarg
) + outarg
.namelen
+ 1)
1550 name
.len
= outarg
.namelen
;
1551 err
= fuse_copy_one(cs
, buf
, outarg
.namelen
+ 1);
1554 fuse_copy_finish(cs
);
1555 buf
[outarg
.namelen
] = 0;
1556 name
.hash
= full_name_hash(name
.name
, name
.len
);
1558 down_read(&fc
->killsb
);
1561 err
= fuse_reverse_inval_entry(fc
->sb
, outarg
.parent
, 0, &name
);
1562 up_read(&fc
->killsb
);
1568 fuse_copy_finish(cs
);
1572 static int fuse_notify_delete(struct fuse_conn
*fc
, unsigned int size
,
1573 struct fuse_copy_state
*cs
)
1575 struct fuse_notify_delete_out outarg
;
1580 buf
= kzalloc(FUSE_NAME_MAX
+ 1, GFP_KERNEL
);
1585 if (size
< sizeof(outarg
))
1588 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1592 err
= -ENAMETOOLONG
;
1593 if (outarg
.namelen
> FUSE_NAME_MAX
)
1597 if (size
!= sizeof(outarg
) + outarg
.namelen
+ 1)
1601 name
.len
= outarg
.namelen
;
1602 err
= fuse_copy_one(cs
, buf
, outarg
.namelen
+ 1);
1605 fuse_copy_finish(cs
);
1606 buf
[outarg
.namelen
] = 0;
1607 name
.hash
= full_name_hash(name
.name
, name
.len
);
1609 down_read(&fc
->killsb
);
1612 err
= fuse_reverse_inval_entry(fc
->sb
, outarg
.parent
,
1613 outarg
.child
, &name
);
1614 up_read(&fc
->killsb
);
1620 fuse_copy_finish(cs
);
1624 static int fuse_notify_store(struct fuse_conn
*fc
, unsigned int size
,
1625 struct fuse_copy_state
*cs
)
1627 struct fuse_notify_store_out outarg
;
1628 struct inode
*inode
;
1629 struct address_space
*mapping
;
1633 unsigned int offset
;
1639 if (size
< sizeof(outarg
))
1642 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1647 if (size
- sizeof(outarg
) != outarg
.size
)
1650 nodeid
= outarg
.nodeid
;
1652 down_read(&fc
->killsb
);
1658 inode
= ilookup5(fc
->sb
, nodeid
, fuse_inode_eq
, &nodeid
);
1662 mapping
= inode
->i_mapping
;
1663 index
= outarg
.offset
>> PAGE_CACHE_SHIFT
;
1664 offset
= outarg
.offset
& ~PAGE_CACHE_MASK
;
1665 file_size
= i_size_read(inode
);
1666 end
= outarg
.offset
+ outarg
.size
;
1667 if (end
> file_size
) {
1669 fuse_write_update_size(inode
, file_size
);
1675 unsigned int this_num
;
1678 page
= find_or_create_page(mapping
, index
,
1679 mapping_gfp_mask(mapping
));
1683 this_num
= min_t(unsigned, num
, PAGE_CACHE_SIZE
- offset
);
1684 err
= fuse_copy_page(cs
, &page
, offset
, this_num
, 0);
1685 if (!err
&& offset
== 0 &&
1686 (this_num
== PAGE_CACHE_SIZE
|| file_size
== end
))
1687 SetPageUptodate(page
);
1689 page_cache_release(page
);
1704 up_read(&fc
->killsb
);
1706 fuse_copy_finish(cs
);
1710 static void fuse_retrieve_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
1712 release_pages(req
->pages
, req
->num_pages
, false);
1715 static int fuse_retrieve(struct fuse_conn
*fc
, struct inode
*inode
,
1716 struct fuse_notify_retrieve_out
*outarg
)
1719 struct address_space
*mapping
= inode
->i_mapping
;
1720 struct fuse_req
*req
;
1724 unsigned int offset
;
1725 size_t total_len
= 0;
1728 offset
= outarg
->offset
& ~PAGE_CACHE_MASK
;
1729 file_size
= i_size_read(inode
);
1731 num
= min(outarg
->size
, fc
->max_write
);
1732 if (outarg
->offset
> file_size
)
1734 else if (outarg
->offset
+ num
> file_size
)
1735 num
= file_size
- outarg
->offset
;
1737 num_pages
= (num
+ offset
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1738 num_pages
= min(num_pages
, FUSE_MAX_PAGES_PER_REQ
);
1740 req
= fuse_get_req(fc
, num_pages
);
1742 return PTR_ERR(req
);
1744 req
->in
.h
.opcode
= FUSE_NOTIFY_REPLY
;
1745 req
->in
.h
.nodeid
= outarg
->nodeid
;
1746 req
->in
.numargs
= 2;
1747 req
->in
.argpages
= 1;
1748 req
->end
= fuse_retrieve_end
;
1750 index
= outarg
->offset
>> PAGE_CACHE_SHIFT
;
1752 while (num
&& req
->num_pages
< num_pages
) {
1754 unsigned int this_num
;
1756 page
= find_get_page(mapping
, index
);
1760 this_num
= min_t(unsigned, num
, PAGE_CACHE_SIZE
- offset
);
1761 req
->pages
[req
->num_pages
] = page
;
1762 req
->page_descs
[req
->num_pages
].offset
= offset
;
1763 req
->page_descs
[req
->num_pages
].length
= this_num
;
1768 total_len
+= this_num
;
1771 req
->misc
.retrieve_in
.offset
= outarg
->offset
;
1772 req
->misc
.retrieve_in
.size
= total_len
;
1773 req
->in
.args
[0].size
= sizeof(req
->misc
.retrieve_in
);
1774 req
->in
.args
[0].value
= &req
->misc
.retrieve_in
;
1775 req
->in
.args
[1].size
= total_len
;
1777 err
= fuse_request_send_notify_reply(fc
, req
, outarg
->notify_unique
);
1779 fuse_retrieve_end(fc
, req
);
1780 fuse_put_request(fc
, req
);
1786 static int fuse_notify_retrieve(struct fuse_conn
*fc
, unsigned int size
,
1787 struct fuse_copy_state
*cs
)
1789 struct fuse_notify_retrieve_out outarg
;
1790 struct inode
*inode
;
1794 if (size
!= sizeof(outarg
))
1797 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1801 fuse_copy_finish(cs
);
1803 down_read(&fc
->killsb
);
1806 u64 nodeid
= outarg
.nodeid
;
1808 inode
= ilookup5(fc
->sb
, nodeid
, fuse_inode_eq
, &nodeid
);
1810 err
= fuse_retrieve(fc
, inode
, &outarg
);
1814 up_read(&fc
->killsb
);
1819 fuse_copy_finish(cs
);
1823 static int fuse_notify(struct fuse_conn
*fc
, enum fuse_notify_code code
,
1824 unsigned int size
, struct fuse_copy_state
*cs
)
1826 /* Don't try to move pages (yet) */
1830 case FUSE_NOTIFY_POLL
:
1831 return fuse_notify_poll(fc
, size
, cs
);
1833 case FUSE_NOTIFY_INVAL_INODE
:
1834 return fuse_notify_inval_inode(fc
, size
, cs
);
1836 case FUSE_NOTIFY_INVAL_ENTRY
:
1837 return fuse_notify_inval_entry(fc
, size
, cs
);
1839 case FUSE_NOTIFY_STORE
:
1840 return fuse_notify_store(fc
, size
, cs
);
1842 case FUSE_NOTIFY_RETRIEVE
:
1843 return fuse_notify_retrieve(fc
, size
, cs
);
1845 case FUSE_NOTIFY_DELETE
:
1846 return fuse_notify_delete(fc
, size
, cs
);
1849 fuse_copy_finish(cs
);
1854 /* Look up request on processing list by unique ID */
1855 static struct fuse_req
*request_find(struct fuse_pqueue
*fpq
, u64 unique
)
1857 struct fuse_req
*req
;
1859 list_for_each_entry(req
, &fpq
->processing
, list
) {
1860 if (req
->in
.h
.unique
== unique
|| req
->intr_unique
== unique
)
1866 static int copy_out_args(struct fuse_copy_state
*cs
, struct fuse_out
*out
,
1869 unsigned reqsize
= sizeof(struct fuse_out_header
);
1872 return nbytes
!= reqsize
? -EINVAL
: 0;
1874 reqsize
+= len_args(out
->numargs
, out
->args
);
1876 if (reqsize
< nbytes
|| (reqsize
> nbytes
&& !out
->argvar
))
1878 else if (reqsize
> nbytes
) {
1879 struct fuse_arg
*lastarg
= &out
->args
[out
->numargs
-1];
1880 unsigned diffsize
= reqsize
- nbytes
;
1881 if (diffsize
> lastarg
->size
)
1883 lastarg
->size
-= diffsize
;
1885 return fuse_copy_args(cs
, out
->numargs
, out
->argpages
, out
->args
,
1890 * Write a single reply to a request. First the header is copied from
1891 * the write buffer. The request is then searched on the processing
1892 * list by the unique ID found in the header. If found, then remove
1893 * it from the list and copy the rest of the buffer to the request.
1894 * The request is finished by calling request_end()
1896 static ssize_t
fuse_dev_do_write(struct fuse_dev
*fud
,
1897 struct fuse_copy_state
*cs
, size_t nbytes
)
1900 struct fuse_conn
*fc
= fud
->fc
;
1901 struct fuse_pqueue
*fpq
= &fud
->pq
;
1902 struct fuse_req
*req
;
1903 struct fuse_out_header oh
;
1905 if (nbytes
< sizeof(struct fuse_out_header
))
1908 err
= fuse_copy_one(cs
, &oh
, sizeof(oh
));
1913 if (oh
.len
!= nbytes
)
1917 * Zero oh.unique indicates unsolicited notification message
1918 * and error contains notification code.
1921 err
= fuse_notify(fc
, oh
.error
, nbytes
- sizeof(oh
), cs
);
1922 return err
? err
: nbytes
;
1926 if (oh
.error
<= -1000 || oh
.error
> 0)
1929 spin_lock(&fpq
->lock
);
1931 if (!fpq
->connected
)
1934 req
= request_find(fpq
, oh
.unique
);
1938 /* Is it an interrupt reply? */
1939 if (req
->intr_unique
== oh
.unique
) {
1940 __fuse_get_request(req
);
1941 spin_unlock(&fpq
->lock
);
1944 if (nbytes
!= sizeof(struct fuse_out_header
)) {
1945 fuse_put_request(fc
, req
);
1949 if (oh
.error
== -ENOSYS
)
1950 fc
->no_interrupt
= 1;
1951 else if (oh
.error
== -EAGAIN
)
1952 queue_interrupt(&fc
->iq
, req
);
1953 fuse_put_request(fc
, req
);
1955 fuse_copy_finish(cs
);
1959 clear_bit(FR_SENT
, &req
->flags
);
1960 list_move(&req
->list
, &fpq
->io
);
1962 set_bit(FR_LOCKED
, &req
->flags
);
1963 spin_unlock(&fpq
->lock
);
1965 if (!req
->out
.page_replace
)
1968 err
= copy_out_args(cs
, &req
->out
, nbytes
);
1969 fuse_copy_finish(cs
);
1971 spin_lock(&fpq
->lock
);
1972 clear_bit(FR_LOCKED
, &req
->flags
);
1973 if (!fpq
->connected
)
1976 req
->out
.h
.error
= -EIO
;
1977 if (!test_bit(FR_PRIVATE
, &req
->flags
))
1978 list_del_init(&req
->list
);
1979 spin_unlock(&fpq
->lock
);
1981 request_end(fc
, req
);
1983 return err
? err
: nbytes
;
1986 spin_unlock(&fpq
->lock
);
1988 fuse_copy_finish(cs
);
1992 static ssize_t
fuse_dev_write(struct kiocb
*iocb
, struct iov_iter
*from
)
1994 struct fuse_copy_state cs
;
1995 struct fuse_dev
*fud
= fuse_get_dev(iocb
->ki_filp
);
2000 if (!iter_is_iovec(from
))
2003 fuse_copy_init(&cs
, 0, from
);
2005 return fuse_dev_do_write(fud
, &cs
, iov_iter_count(from
));
2008 static ssize_t
fuse_dev_splice_write(struct pipe_inode_info
*pipe
,
2009 struct file
*out
, loff_t
*ppos
,
2010 size_t len
, unsigned int flags
)
2014 struct pipe_buffer
*bufs
;
2015 struct fuse_copy_state cs
;
2016 struct fuse_dev
*fud
;
2020 fud
= fuse_get_dev(out
);
2026 bufs
= kmalloc(pipe
->buffers
* sizeof(struct pipe_buffer
), GFP_KERNEL
);
2034 for (idx
= 0; idx
< pipe
->nrbufs
&& rem
< len
; idx
++)
2035 rem
+= pipe
->bufs
[(pipe
->curbuf
+ idx
) & (pipe
->buffers
- 1)].len
;
2043 struct pipe_buffer
*ibuf
;
2044 struct pipe_buffer
*obuf
;
2046 BUG_ON(nbuf
>= pipe
->buffers
);
2047 BUG_ON(!pipe
->nrbufs
);
2048 ibuf
= &pipe
->bufs
[pipe
->curbuf
];
2051 if (rem
>= ibuf
->len
) {
2054 pipe
->curbuf
= (pipe
->curbuf
+ 1) & (pipe
->buffers
- 1);
2057 if (!pipe_buf_get(pipe
, ibuf
))
2061 obuf
->flags
&= ~PIPE_BUF_FLAG_GIFT
;
2063 ibuf
->offset
+= obuf
->len
;
2064 ibuf
->len
-= obuf
->len
;
2071 fuse_copy_init(&cs
, 0, NULL
);
2076 if (flags
& SPLICE_F_MOVE
)
2079 ret
= fuse_dev_do_write(fud
, &cs
, len
);
2083 for (idx
= 0; idx
< nbuf
; idx
++) {
2084 struct pipe_buffer
*buf
= &bufs
[idx
];
2085 buf
->ops
->release(pipe
, buf
);
2093 static unsigned fuse_dev_poll(struct file
*file
, poll_table
*wait
)
2095 unsigned mask
= POLLOUT
| POLLWRNORM
;
2096 struct fuse_iqueue
*fiq
;
2097 struct fuse_dev
*fud
= fuse_get_dev(file
);
2103 poll_wait(file
, &fiq
->waitq
, wait
);
2105 spin_lock(&fiq
->waitq
.lock
);
2106 if (!fiq
->connected
)
2108 else if (request_pending(fiq
))
2109 mask
|= POLLIN
| POLLRDNORM
;
2110 spin_unlock(&fiq
->waitq
.lock
);
2116 * Abort all requests on the given list (pending or processing)
2118 * This function releases and reacquires fc->lock
2120 static void end_requests(struct fuse_conn
*fc
, struct list_head
*head
)
2122 while (!list_empty(head
)) {
2123 struct fuse_req
*req
;
2124 req
= list_entry(head
->next
, struct fuse_req
, list
);
2125 req
->out
.h
.error
= -ECONNABORTED
;
2126 clear_bit(FR_SENT
, &req
->flags
);
2127 list_del_init(&req
->list
);
2128 request_end(fc
, req
);
2132 static void end_polls(struct fuse_conn
*fc
)
2136 p
= rb_first(&fc
->polled_files
);
2139 struct fuse_file
*ff
;
2140 ff
= rb_entry(p
, struct fuse_file
, polled_node
);
2141 wake_up_interruptible_all(&ff
->poll_wait
);
2148 * Abort all requests.
2150 * Emergency exit in case of a malicious or accidental deadlock, or just a hung
2153 * The same effect is usually achievable through killing the filesystem daemon
2154 * and all users of the filesystem. The exception is the combination of an
2155 * asynchronous request and the tricky deadlock (see
2156 * Documentation/filesystems/fuse.txt).
2158 * Aborting requests under I/O goes as follows: 1: Separate out unlocked
2159 * requests, they should be finished off immediately. Locked requests will be
2160 * finished after unlock; see unlock_request(). 2: Finish off the unlocked
2161 * requests. It is possible that some request will finish before we can. This
2162 * is OK, the request will in that case be removed from the list before we touch
2165 void fuse_abort_conn(struct fuse_conn
*fc
)
2167 struct fuse_iqueue
*fiq
= &fc
->iq
;
2169 spin_lock(&fc
->lock
);
2170 if (fc
->connected
) {
2171 struct fuse_dev
*fud
;
2172 struct fuse_req
*req
, *next
;
2178 fuse_set_initialized(fc
);
2179 list_for_each_entry(fud
, &fc
->devices
, entry
) {
2180 struct fuse_pqueue
*fpq
= &fud
->pq
;
2182 spin_lock(&fpq
->lock
);
2184 list_for_each_entry_safe(req
, next
, &fpq
->io
, list
) {
2185 req
->out
.h
.error
= -ECONNABORTED
;
2186 spin_lock(&req
->waitq
.lock
);
2187 set_bit(FR_ABORTED
, &req
->flags
);
2188 if (!test_bit(FR_LOCKED
, &req
->flags
)) {
2189 set_bit(FR_PRIVATE
, &req
->flags
);
2190 __fuse_get_request(req
);
2191 list_move(&req
->list
, &to_end1
);
2193 spin_unlock(&req
->waitq
.lock
);
2195 list_splice_init(&fpq
->processing
, &to_end2
);
2196 spin_unlock(&fpq
->lock
);
2198 fc
->max_background
= UINT_MAX
;
2201 spin_lock(&fiq
->waitq
.lock
);
2203 list_splice_init(&fiq
->pending
, &to_end2
);
2204 list_for_each_entry(req
, &to_end2
, list
)
2205 clear_bit(FR_PENDING
, &req
->flags
);
2206 while (forget_pending(fiq
))
2207 kfree(dequeue_forget(fiq
, 1, NULL
));
2208 wake_up_all_locked(&fiq
->waitq
);
2209 spin_unlock(&fiq
->waitq
.lock
);
2210 kill_fasync(&fiq
->fasync
, SIGIO
, POLL_IN
);
2212 wake_up_all(&fc
->blocked_waitq
);
2213 spin_unlock(&fc
->lock
);
2215 while (!list_empty(&to_end1
)) {
2216 req
= list_first_entry(&to_end1
, struct fuse_req
, list
);
2217 list_del_init(&req
->list
);
2218 request_end(fc
, req
);
2220 end_requests(fc
, &to_end2
);
2222 spin_unlock(&fc
->lock
);
2225 EXPORT_SYMBOL_GPL(fuse_abort_conn
);
2227 void fuse_wait_aborted(struct fuse_conn
*fc
)
2229 /* matches implicit memory barrier in fuse_drop_waiting() */
2231 wait_event(fc
->blocked_waitq
, atomic_read(&fc
->num_waiting
) == 0);
2234 int fuse_dev_release(struct inode
*inode
, struct file
*file
)
2236 struct fuse_dev
*fud
= fuse_get_dev(file
);
2239 struct fuse_conn
*fc
= fud
->fc
;
2240 struct fuse_pqueue
*fpq
= &fud
->pq
;
2243 spin_lock(&fpq
->lock
);
2244 WARN_ON(!list_empty(&fpq
->io
));
2245 list_splice_init(&fpq
->processing
, &to_end
);
2246 spin_unlock(&fpq
->lock
);
2248 end_requests(fc
, &to_end
);
2250 /* Are we the last open device? */
2251 if (atomic_dec_and_test(&fc
->dev_count
)) {
2252 WARN_ON(fc
->iq
.fasync
!= NULL
);
2253 fuse_abort_conn(fc
);
2259 EXPORT_SYMBOL_GPL(fuse_dev_release
);
2261 static int fuse_dev_fasync(int fd
, struct file
*file
, int on
)
2263 struct fuse_dev
*fud
= fuse_get_dev(file
);
2268 /* No locking - fasync_helper does its own locking */
2269 return fasync_helper(fd
, file
, on
, &fud
->fc
->iq
.fasync
);
2272 static int fuse_device_clone(struct fuse_conn
*fc
, struct file
*new)
2274 struct fuse_dev
*fud
;
2276 if (new->private_data
)
2279 fud
= fuse_dev_alloc(fc
);
2283 new->private_data
= fud
;
2284 atomic_inc(&fc
->dev_count
);
2289 static long fuse_dev_ioctl(struct file
*file
, unsigned int cmd
,
2294 if (cmd
== FUSE_DEV_IOC_CLONE
) {
2298 if (!get_user(oldfd
, (__u32 __user
*) arg
)) {
2299 struct file
*old
= fget(oldfd
);
2303 struct fuse_dev
*fud
= NULL
;
2306 * Check against file->f_op because CUSE
2307 * uses the same ioctl handler.
2309 if (old
->f_op
== file
->f_op
&&
2310 old
->f_cred
->user_ns
== file
->f_cred
->user_ns
)
2311 fud
= fuse_get_dev(old
);
2314 mutex_lock(&fuse_mutex
);
2315 err
= fuse_device_clone(fud
->fc
, file
);
2316 mutex_unlock(&fuse_mutex
);
2325 const struct file_operations fuse_dev_operations
= {
2326 .owner
= THIS_MODULE
,
2327 .open
= fuse_dev_open
,
2328 .llseek
= no_llseek
,
2329 .read_iter
= fuse_dev_read
,
2330 .splice_read
= fuse_dev_splice_read
,
2331 .write_iter
= fuse_dev_write
,
2332 .splice_write
= fuse_dev_splice_write
,
2333 .poll
= fuse_dev_poll
,
2334 .release
= fuse_dev_release
,
2335 .fasync
= fuse_dev_fasync
,
2336 .unlocked_ioctl
= fuse_dev_ioctl
,
2337 .compat_ioctl
= fuse_dev_ioctl
,
2339 EXPORT_SYMBOL_GPL(fuse_dev_operations
);
2341 static struct miscdevice fuse_miscdevice
= {
2342 .minor
= FUSE_MINOR
,
2344 .fops
= &fuse_dev_operations
,
2347 int __init
fuse_dev_init(void)
2350 fuse_req_cachep
= kmem_cache_create("fuse_request",
2351 sizeof(struct fuse_req
),
2353 if (!fuse_req_cachep
)
2356 err
= misc_register(&fuse_miscdevice
);
2358 goto out_cache_clean
;
2363 kmem_cache_destroy(fuse_req_cachep
);
2368 void fuse_dev_cleanup(void)
2370 misc_deregister(&fuse_miscdevice
);
2371 kmem_cache_destroy(fuse_req_cachep
);