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 atomic_dec(&fc
->num_waiting
);
150 } else if (atomic_dec_and_test(&fc
->num_waiting
)) {
151 /* wake up aborters */
152 wake_up_all(&fc
->blocked_waitq
);
156 static struct fuse_req
*__fuse_get_req(struct fuse_conn
*fc
, unsigned npages
,
159 struct fuse_req
*req
;
161 atomic_inc(&fc
->num_waiting
);
163 if (fuse_block_alloc(fc
, for_background
)) {
168 intr
= wait_event_interruptible_exclusive(fc
->blocked_waitq
,
169 !fuse_block_alloc(fc
, for_background
));
170 restore_sigs(&oldset
);
175 /* Matches smp_wmb() in fuse_set_initialized() */
186 req
= fuse_request_alloc(npages
);
190 wake_up(&fc
->blocked_waitq
);
194 fuse_req_init_context(req
);
195 __set_bit(FR_WAITING
, &req
->flags
);
197 __set_bit(FR_BACKGROUND
, &req
->flags
);
202 fuse_drop_waiting(fc
);
206 struct fuse_req
*fuse_get_req(struct fuse_conn
*fc
, unsigned npages
)
208 return __fuse_get_req(fc
, npages
, false);
210 EXPORT_SYMBOL_GPL(fuse_get_req
);
212 struct fuse_req
*fuse_get_req_for_background(struct fuse_conn
*fc
,
215 return __fuse_get_req(fc
, npages
, true);
217 EXPORT_SYMBOL_GPL(fuse_get_req_for_background
);
220 * Return request in fuse_file->reserved_req. However that may
221 * currently be in use. If that is the case, wait for it to become
224 static struct fuse_req
*get_reserved_req(struct fuse_conn
*fc
,
227 struct fuse_req
*req
= NULL
;
228 struct fuse_file
*ff
= file
->private_data
;
231 wait_event(fc
->reserved_req_waitq
, ff
->reserved_req
);
232 spin_lock(&fc
->lock
);
233 if (ff
->reserved_req
) {
234 req
= ff
->reserved_req
;
235 ff
->reserved_req
= NULL
;
236 req
->stolen_file
= get_file(file
);
238 spin_unlock(&fc
->lock
);
245 * Put stolen request back into fuse_file->reserved_req
247 static void put_reserved_req(struct fuse_conn
*fc
, struct fuse_req
*req
)
249 struct file
*file
= req
->stolen_file
;
250 struct fuse_file
*ff
= file
->private_data
;
252 spin_lock(&fc
->lock
);
253 fuse_request_init(req
, req
->pages
, req
->page_descs
, req
->max_pages
);
254 BUG_ON(ff
->reserved_req
);
255 ff
->reserved_req
= req
;
256 wake_up_all(&fc
->reserved_req_waitq
);
257 spin_unlock(&fc
->lock
);
262 * Gets a requests for a file operation, always succeeds
264 * This is used for sending the FLUSH request, which must get to
265 * userspace, due to POSIX locks which may need to be unlocked.
267 * If allocation fails due to OOM, use the reserved request in
270 * This is very unlikely to deadlock accidentally, since the
271 * filesystem should not have it's own file open. If deadlock is
272 * intentional, it can still be broken by "aborting" the filesystem.
274 struct fuse_req
*fuse_get_req_nofail_nopages(struct fuse_conn
*fc
,
277 struct fuse_req
*req
;
279 atomic_inc(&fc
->num_waiting
);
280 wait_event(fc
->blocked_waitq
, fc
->initialized
);
281 /* Matches smp_wmb() in fuse_set_initialized() */
283 req
= fuse_request_alloc(0);
285 req
= get_reserved_req(fc
, file
);
287 fuse_req_init_context(req
);
288 __set_bit(FR_WAITING
, &req
->flags
);
289 __clear_bit(FR_BACKGROUND
, &req
->flags
);
293 void fuse_put_request(struct fuse_conn
*fc
, struct fuse_req
*req
)
295 if (atomic_dec_and_test(&req
->count
)) {
296 if (test_bit(FR_BACKGROUND
, &req
->flags
)) {
298 * We get here in the unlikely case that a background
299 * request was allocated but not sent
301 spin_lock(&fc
->lock
);
303 wake_up(&fc
->blocked_waitq
);
304 spin_unlock(&fc
->lock
);
307 if (test_bit(FR_WAITING
, &req
->flags
)) {
308 __clear_bit(FR_WAITING
, &req
->flags
);
309 fuse_drop_waiting(fc
);
312 if (req
->stolen_file
)
313 put_reserved_req(fc
, req
);
315 fuse_request_free(req
);
318 EXPORT_SYMBOL_GPL(fuse_put_request
);
320 static unsigned len_args(unsigned numargs
, struct fuse_arg
*args
)
325 for (i
= 0; i
< numargs
; i
++)
326 nbytes
+= args
[i
].size
;
331 static u64
fuse_get_unique(struct fuse_iqueue
*fiq
)
333 return ++fiq
->reqctr
;
336 static void queue_request(struct fuse_iqueue
*fiq
, struct fuse_req
*req
)
338 req
->in
.h
.len
= sizeof(struct fuse_in_header
) +
339 len_args(req
->in
.numargs
, (struct fuse_arg
*) req
->in
.args
);
340 list_add_tail(&req
->list
, &fiq
->pending
);
341 wake_up_locked(&fiq
->waitq
);
342 kill_fasync(&fiq
->fasync
, SIGIO
, POLL_IN
);
345 void fuse_queue_forget(struct fuse_conn
*fc
, struct fuse_forget_link
*forget
,
346 u64 nodeid
, u64 nlookup
)
348 struct fuse_iqueue
*fiq
= &fc
->iq
;
350 forget
->forget_one
.nodeid
= nodeid
;
351 forget
->forget_one
.nlookup
= nlookup
;
353 spin_lock(&fiq
->waitq
.lock
);
354 if (fiq
->connected
) {
355 fiq
->forget_list_tail
->next
= forget
;
356 fiq
->forget_list_tail
= forget
;
357 wake_up_locked(&fiq
->waitq
);
358 kill_fasync(&fiq
->fasync
, SIGIO
, POLL_IN
);
362 spin_unlock(&fiq
->waitq
.lock
);
365 static void flush_bg_queue(struct fuse_conn
*fc
)
367 while (fc
->active_background
< fc
->max_background
&&
368 !list_empty(&fc
->bg_queue
)) {
369 struct fuse_req
*req
;
370 struct fuse_iqueue
*fiq
= &fc
->iq
;
372 req
= list_entry(fc
->bg_queue
.next
, struct fuse_req
, list
);
373 list_del(&req
->list
);
374 fc
->active_background
++;
375 spin_lock(&fiq
->waitq
.lock
);
376 req
->in
.h
.unique
= fuse_get_unique(fiq
);
377 queue_request(fiq
, req
);
378 spin_unlock(&fiq
->waitq
.lock
);
383 * This function is called when a request is finished. Either a reply
384 * has arrived or it was aborted (and not yet sent) or some error
385 * occurred during communication with userspace, or the device file
386 * was closed. The requester thread is woken up (if still waiting),
387 * the 'end' callback is called if given, else the reference to the
388 * request is released
390 static void request_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
392 struct fuse_iqueue
*fiq
= &fc
->iq
;
394 if (test_and_set_bit(FR_FINISHED
, &req
->flags
))
397 spin_lock(&fiq
->waitq
.lock
);
398 list_del_init(&req
->intr_entry
);
399 spin_unlock(&fiq
->waitq
.lock
);
400 WARN_ON(test_bit(FR_PENDING
, &req
->flags
));
401 WARN_ON(test_bit(FR_SENT
, &req
->flags
));
402 if (test_bit(FR_BACKGROUND
, &req
->flags
)) {
403 spin_lock(&fc
->lock
);
404 clear_bit(FR_BACKGROUND
, &req
->flags
);
405 if (fc
->num_background
== fc
->max_background
)
408 /* Wake up next waiter, if any */
409 if (!fc
->blocked
&& waitqueue_active(&fc
->blocked_waitq
))
410 wake_up(&fc
->blocked_waitq
);
412 if (fc
->num_background
== fc
->congestion_threshold
&&
413 fc
->connected
&& fc
->bdi_initialized
) {
414 clear_bdi_congested(&fc
->bdi
, BLK_RW_SYNC
);
415 clear_bdi_congested(&fc
->bdi
, BLK_RW_ASYNC
);
417 fc
->num_background
--;
418 fc
->active_background
--;
420 spin_unlock(&fc
->lock
);
422 wake_up(&req
->waitq
);
426 fuse_put_request(fc
, req
);
429 static void queue_interrupt(struct fuse_iqueue
*fiq
, struct fuse_req
*req
)
431 spin_lock(&fiq
->waitq
.lock
);
432 if (test_bit(FR_FINISHED
, &req
->flags
)) {
433 spin_unlock(&fiq
->waitq
.lock
);
436 if (list_empty(&req
->intr_entry
)) {
437 list_add_tail(&req
->intr_entry
, &fiq
->interrupts
);
438 wake_up_locked(&fiq
->waitq
);
440 spin_unlock(&fiq
->waitq
.lock
);
441 kill_fasync(&fiq
->fasync
, SIGIO
, POLL_IN
);
444 static void request_wait_answer(struct fuse_conn
*fc
, struct fuse_req
*req
)
446 struct fuse_iqueue
*fiq
= &fc
->iq
;
449 if (!fc
->no_interrupt
) {
450 /* Any signal may interrupt this */
451 err
= wait_event_interruptible(req
->waitq
,
452 test_bit(FR_FINISHED
, &req
->flags
));
456 set_bit(FR_INTERRUPTED
, &req
->flags
);
457 /* matches barrier in fuse_dev_do_read() */
458 smp_mb__after_atomic();
459 if (test_bit(FR_SENT
, &req
->flags
))
460 queue_interrupt(fiq
, req
);
463 if (!test_bit(FR_FORCE
, &req
->flags
)) {
466 /* Only fatal signals may interrupt this */
468 err
= wait_event_interruptible(req
->waitq
,
469 test_bit(FR_FINISHED
, &req
->flags
));
470 restore_sigs(&oldset
);
475 spin_lock(&fiq
->waitq
.lock
);
476 /* Request is not yet in userspace, bail out */
477 if (test_bit(FR_PENDING
, &req
->flags
)) {
478 list_del(&req
->list
);
479 spin_unlock(&fiq
->waitq
.lock
);
480 __fuse_put_request(req
);
481 req
->out
.h
.error
= -EINTR
;
484 spin_unlock(&fiq
->waitq
.lock
);
488 * Either request is already in userspace, or it was forced.
491 wait_event(req
->waitq
, test_bit(FR_FINISHED
, &req
->flags
));
494 static void __fuse_request_send(struct fuse_conn
*fc
, struct fuse_req
*req
)
496 struct fuse_iqueue
*fiq
= &fc
->iq
;
498 BUG_ON(test_bit(FR_BACKGROUND
, &req
->flags
));
499 spin_lock(&fiq
->waitq
.lock
);
500 if (!fiq
->connected
) {
501 spin_unlock(&fiq
->waitq
.lock
);
502 req
->out
.h
.error
= -ENOTCONN
;
504 req
->in
.h
.unique
= fuse_get_unique(fiq
);
505 queue_request(fiq
, req
);
506 /* acquire extra reference, since request is still needed
507 after request_end() */
508 __fuse_get_request(req
);
509 spin_unlock(&fiq
->waitq
.lock
);
511 request_wait_answer(fc
, req
);
512 /* Pairs with smp_wmb() in request_end() */
517 void fuse_request_send(struct fuse_conn
*fc
, struct fuse_req
*req
)
519 __set_bit(FR_ISREPLY
, &req
->flags
);
520 if (!test_bit(FR_WAITING
, &req
->flags
)) {
521 __set_bit(FR_WAITING
, &req
->flags
);
522 atomic_inc(&fc
->num_waiting
);
524 __fuse_request_send(fc
, req
);
526 EXPORT_SYMBOL_GPL(fuse_request_send
);
528 static void fuse_adjust_compat(struct fuse_conn
*fc
, struct fuse_args
*args
)
530 if (fc
->minor
< 4 && args
->in
.h
.opcode
== FUSE_STATFS
)
531 args
->out
.args
[0].size
= FUSE_COMPAT_STATFS_SIZE
;
534 switch (args
->in
.h
.opcode
) {
541 args
->out
.args
[0].size
= FUSE_COMPAT_ENTRY_OUT_SIZE
;
545 args
->out
.args
[0].size
= FUSE_COMPAT_ATTR_OUT_SIZE
;
549 if (fc
->minor
< 12) {
550 switch (args
->in
.h
.opcode
) {
552 args
->in
.args
[0].size
= sizeof(struct fuse_open_in
);
555 args
->in
.args
[0].size
= FUSE_COMPAT_MKNOD_IN_SIZE
;
561 ssize_t
fuse_simple_request(struct fuse_conn
*fc
, struct fuse_args
*args
)
563 struct fuse_req
*req
;
566 req
= fuse_get_req(fc
, 0);
570 /* Needs to be done after fuse_get_req() so that fc->minor is valid */
571 fuse_adjust_compat(fc
, args
);
573 req
->in
.h
.opcode
= args
->in
.h
.opcode
;
574 req
->in
.h
.nodeid
= args
->in
.h
.nodeid
;
575 req
->in
.numargs
= args
->in
.numargs
;
576 memcpy(req
->in
.args
, args
->in
.args
,
577 args
->in
.numargs
* sizeof(struct fuse_in_arg
));
578 req
->out
.argvar
= args
->out
.argvar
;
579 req
->out
.numargs
= args
->out
.numargs
;
580 memcpy(req
->out
.args
, args
->out
.args
,
581 args
->out
.numargs
* sizeof(struct fuse_arg
));
582 fuse_request_send(fc
, req
);
583 ret
= req
->out
.h
.error
;
584 if (!ret
&& args
->out
.argvar
) {
585 BUG_ON(args
->out
.numargs
!= 1);
586 ret
= req
->out
.args
[0].size
;
588 fuse_put_request(fc
, req
);
594 * Called under fc->lock
596 * fc->connected must have been checked previously
598 void fuse_request_send_background_locked(struct fuse_conn
*fc
,
599 struct fuse_req
*req
)
601 BUG_ON(!test_bit(FR_BACKGROUND
, &req
->flags
));
602 if (!test_bit(FR_WAITING
, &req
->flags
)) {
603 __set_bit(FR_WAITING
, &req
->flags
);
604 atomic_inc(&fc
->num_waiting
);
606 __set_bit(FR_ISREPLY
, &req
->flags
);
607 fc
->num_background
++;
608 if (fc
->num_background
== fc
->max_background
)
610 if (fc
->num_background
== fc
->congestion_threshold
&&
611 fc
->bdi_initialized
) {
612 set_bdi_congested(&fc
->bdi
, BLK_RW_SYNC
);
613 set_bdi_congested(&fc
->bdi
, BLK_RW_ASYNC
);
615 list_add_tail(&req
->list
, &fc
->bg_queue
);
619 void fuse_request_send_background(struct fuse_conn
*fc
, struct fuse_req
*req
)
622 spin_lock(&fc
->lock
);
624 fuse_request_send_background_locked(fc
, req
);
625 spin_unlock(&fc
->lock
);
627 spin_unlock(&fc
->lock
);
628 req
->out
.h
.error
= -ENOTCONN
;
630 fuse_put_request(fc
, req
);
633 EXPORT_SYMBOL_GPL(fuse_request_send_background
);
635 static int fuse_request_send_notify_reply(struct fuse_conn
*fc
,
636 struct fuse_req
*req
, u64 unique
)
639 struct fuse_iqueue
*fiq
= &fc
->iq
;
641 __clear_bit(FR_ISREPLY
, &req
->flags
);
642 req
->in
.h
.unique
= unique
;
643 spin_lock(&fiq
->waitq
.lock
);
644 if (fiq
->connected
) {
645 queue_request(fiq
, req
);
648 spin_unlock(&fiq
->waitq
.lock
);
653 void fuse_force_forget(struct file
*file
, u64 nodeid
)
655 struct inode
*inode
= file_inode(file
);
656 struct fuse_conn
*fc
= get_fuse_conn(inode
);
657 struct fuse_req
*req
;
658 struct fuse_forget_in inarg
;
660 memset(&inarg
, 0, sizeof(inarg
));
662 req
= fuse_get_req_nofail_nopages(fc
, file
);
663 req
->in
.h
.opcode
= FUSE_FORGET
;
664 req
->in
.h
.nodeid
= nodeid
;
666 req
->in
.args
[0].size
= sizeof(inarg
);
667 req
->in
.args
[0].value
= &inarg
;
668 __clear_bit(FR_ISREPLY
, &req
->flags
);
669 __fuse_request_send(fc
, req
);
671 fuse_put_request(fc
, req
);
675 * Lock the request. Up to the next unlock_request() there mustn't be
676 * anything that could cause a page-fault. If the request was already
679 static int lock_request(struct fuse_req
*req
)
683 spin_lock(&req
->waitq
.lock
);
684 if (test_bit(FR_ABORTED
, &req
->flags
))
687 set_bit(FR_LOCKED
, &req
->flags
);
688 spin_unlock(&req
->waitq
.lock
);
694 * Unlock request. If it was aborted while locked, caller is responsible
695 * for unlocking and ending the request.
697 static int unlock_request(struct fuse_req
*req
)
701 spin_lock(&req
->waitq
.lock
);
702 if (test_bit(FR_ABORTED
, &req
->flags
))
705 clear_bit(FR_LOCKED
, &req
->flags
);
706 spin_unlock(&req
->waitq
.lock
);
711 struct fuse_copy_state
{
713 struct fuse_req
*req
;
714 struct iov_iter
*iter
;
715 struct pipe_buffer
*pipebufs
;
716 struct pipe_buffer
*currbuf
;
717 struct pipe_inode_info
*pipe
;
718 unsigned long nr_segs
;
722 unsigned move_pages
:1;
725 static void fuse_copy_init(struct fuse_copy_state
*cs
, int write
,
726 struct iov_iter
*iter
)
728 memset(cs
, 0, sizeof(*cs
));
733 /* Unmap and put previous page of userspace buffer */
734 static void fuse_copy_finish(struct fuse_copy_state
*cs
)
737 struct pipe_buffer
*buf
= cs
->currbuf
;
740 buf
->len
= PAGE_SIZE
- cs
->len
;
744 flush_dcache_page(cs
->pg
);
745 set_page_dirty_lock(cs
->pg
);
753 * Get another pagefull of userspace buffer, and map it to kernel
754 * address space, and lock request
756 static int fuse_copy_fill(struct fuse_copy_state
*cs
)
761 err
= unlock_request(cs
->req
);
765 fuse_copy_finish(cs
);
767 struct pipe_buffer
*buf
= cs
->pipebufs
;
770 err
= buf
->ops
->confirm(cs
->pipe
, buf
);
774 BUG_ON(!cs
->nr_segs
);
777 cs
->offset
= buf
->offset
;
782 if (cs
->nr_segs
== cs
->pipe
->buffers
)
785 page
= alloc_page(GFP_HIGHUSER
);
802 err
= iov_iter_get_pages(cs
->iter
, &page
, PAGE_SIZE
, 1, &off
);
810 iov_iter_advance(cs
->iter
, err
);
813 return lock_request(cs
->req
);
816 /* Do as much copy to/from userspace buffer as we can */
817 static int fuse_copy_do(struct fuse_copy_state
*cs
, void **val
, unsigned *size
)
819 unsigned ncpy
= min(*size
, cs
->len
);
821 void *pgaddr
= kmap_atomic(cs
->pg
);
822 void *buf
= pgaddr
+ cs
->offset
;
825 memcpy(buf
, *val
, ncpy
);
827 memcpy(*val
, buf
, ncpy
);
829 kunmap_atomic(pgaddr
);
838 static int fuse_check_page(struct page
*page
)
840 if (page_mapcount(page
) ||
841 page
->mapping
!= NULL
||
842 page_count(page
) != 1 ||
843 (page
->flags
& PAGE_FLAGS_CHECK_AT_PREP
&
850 printk(KERN_WARNING
"fuse: trying to steal weird page\n");
851 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
);
857 static int fuse_try_move_page(struct fuse_copy_state
*cs
, struct page
**pagep
)
860 struct page
*oldpage
= *pagep
;
861 struct page
*newpage
;
862 struct pipe_buffer
*buf
= cs
->pipebufs
;
864 err
= unlock_request(cs
->req
);
868 fuse_copy_finish(cs
);
870 err
= buf
->ops
->confirm(cs
->pipe
, buf
);
874 BUG_ON(!cs
->nr_segs
);
880 if (cs
->len
!= PAGE_SIZE
)
883 if (buf
->ops
->steal(cs
->pipe
, buf
) != 0)
888 if (!PageUptodate(newpage
))
889 SetPageUptodate(newpage
);
891 ClearPageMappedToDisk(newpage
);
893 if (fuse_check_page(newpage
) != 0)
894 goto out_fallback_unlock
;
897 * This is a new and locked page, it shouldn't be mapped or
898 * have any special flags on it
900 if (WARN_ON(page_mapped(oldpage
)))
901 goto out_fallback_unlock
;
902 if (WARN_ON(page_has_private(oldpage
)))
903 goto out_fallback_unlock
;
904 if (WARN_ON(PageDirty(oldpage
) || PageWriteback(oldpage
)))
905 goto out_fallback_unlock
;
906 if (WARN_ON(PageMlocked(oldpage
)))
907 goto out_fallback_unlock
;
909 err
= replace_page_cache_page(oldpage
, newpage
, GFP_KERNEL
);
911 unlock_page(newpage
);
915 page_cache_get(newpage
);
917 if (!(buf
->flags
& PIPE_BUF_FLAG_LRU
))
918 lru_cache_add_file(newpage
);
921 spin_lock(&cs
->req
->waitq
.lock
);
922 if (test_bit(FR_ABORTED
, &cs
->req
->flags
))
926 spin_unlock(&cs
->req
->waitq
.lock
);
929 unlock_page(newpage
);
930 page_cache_release(newpage
);
934 unlock_page(oldpage
);
935 page_cache_release(oldpage
);
941 unlock_page(newpage
);
944 cs
->offset
= buf
->offset
;
946 err
= lock_request(cs
->req
);
953 static int fuse_ref_page(struct fuse_copy_state
*cs
, struct page
*page
,
954 unsigned offset
, unsigned count
)
956 struct pipe_buffer
*buf
;
959 if (cs
->nr_segs
== cs
->pipe
->buffers
)
962 err
= unlock_request(cs
->req
);
966 fuse_copy_finish(cs
);
969 page_cache_get(page
);
971 buf
->offset
= offset
;
982 * Copy a page in the request to/from the userspace buffer. Must be
985 static int fuse_copy_page(struct fuse_copy_state
*cs
, struct page
**pagep
,
986 unsigned offset
, unsigned count
, int zeroing
)
989 struct page
*page
= *pagep
;
991 if (page
&& zeroing
&& count
< PAGE_SIZE
)
992 clear_highpage(page
);
995 if (cs
->write
&& cs
->pipebufs
&& page
) {
996 return fuse_ref_page(cs
, page
, offset
, count
);
997 } else if (!cs
->len
) {
998 if (cs
->move_pages
&& page
&&
999 offset
== 0 && count
== PAGE_SIZE
) {
1000 err
= fuse_try_move_page(cs
, pagep
);
1004 err
= fuse_copy_fill(cs
);
1010 void *mapaddr
= kmap_atomic(page
);
1011 void *buf
= mapaddr
+ offset
;
1012 offset
+= fuse_copy_do(cs
, &buf
, &count
);
1013 kunmap_atomic(mapaddr
);
1015 offset
+= fuse_copy_do(cs
, NULL
, &count
);
1017 if (page
&& !cs
->write
)
1018 flush_dcache_page(page
);
1022 /* Copy pages in the request to/from userspace buffer */
1023 static int fuse_copy_pages(struct fuse_copy_state
*cs
, unsigned nbytes
,
1027 struct fuse_req
*req
= cs
->req
;
1029 for (i
= 0; i
< req
->num_pages
&& (nbytes
|| zeroing
); i
++) {
1031 unsigned offset
= req
->page_descs
[i
].offset
;
1032 unsigned count
= min(nbytes
, req
->page_descs
[i
].length
);
1034 err
= fuse_copy_page(cs
, &req
->pages
[i
], offset
, count
,
1044 /* Copy a single argument in the request to/from userspace buffer */
1045 static int fuse_copy_one(struct fuse_copy_state
*cs
, void *val
, unsigned size
)
1049 int err
= fuse_copy_fill(cs
);
1053 fuse_copy_do(cs
, &val
, &size
);
1058 /* Copy request arguments to/from userspace buffer */
1059 static int fuse_copy_args(struct fuse_copy_state
*cs
, unsigned numargs
,
1060 unsigned argpages
, struct fuse_arg
*args
,
1066 for (i
= 0; !err
&& i
< numargs
; i
++) {
1067 struct fuse_arg
*arg
= &args
[i
];
1068 if (i
== numargs
- 1 && argpages
)
1069 err
= fuse_copy_pages(cs
, arg
->size
, zeroing
);
1071 err
= fuse_copy_one(cs
, arg
->value
, arg
->size
);
1076 static int forget_pending(struct fuse_iqueue
*fiq
)
1078 return fiq
->forget_list_head
.next
!= NULL
;
1081 static int request_pending(struct fuse_iqueue
*fiq
)
1083 return !list_empty(&fiq
->pending
) || !list_empty(&fiq
->interrupts
) ||
1084 forget_pending(fiq
);
1088 * Transfer an interrupt request to userspace
1090 * Unlike other requests this is assembled on demand, without a need
1091 * to allocate a separate fuse_req structure.
1093 * Called with fiq->waitq.lock held, releases it
1095 static int fuse_read_interrupt(struct fuse_iqueue
*fiq
,
1096 struct fuse_copy_state
*cs
,
1097 size_t nbytes
, struct fuse_req
*req
)
1098 __releases(fiq
->waitq
.lock
)
1100 struct fuse_in_header ih
;
1101 struct fuse_interrupt_in arg
;
1102 unsigned reqsize
= sizeof(ih
) + sizeof(arg
);
1105 list_del_init(&req
->intr_entry
);
1106 req
->intr_unique
= fuse_get_unique(fiq
);
1107 memset(&ih
, 0, sizeof(ih
));
1108 memset(&arg
, 0, sizeof(arg
));
1110 ih
.opcode
= FUSE_INTERRUPT
;
1111 ih
.unique
= req
->intr_unique
;
1112 arg
.unique
= req
->in
.h
.unique
;
1114 spin_unlock(&fiq
->waitq
.lock
);
1115 if (nbytes
< reqsize
)
1118 err
= fuse_copy_one(cs
, &ih
, sizeof(ih
));
1120 err
= fuse_copy_one(cs
, &arg
, sizeof(arg
));
1121 fuse_copy_finish(cs
);
1123 return err
? err
: reqsize
;
1126 static struct fuse_forget_link
*dequeue_forget(struct fuse_iqueue
*fiq
,
1130 struct fuse_forget_link
*head
= fiq
->forget_list_head
.next
;
1131 struct fuse_forget_link
**newhead
= &head
;
1134 for (count
= 0; *newhead
!= NULL
&& count
< max
; count
++)
1135 newhead
= &(*newhead
)->next
;
1137 fiq
->forget_list_head
.next
= *newhead
;
1139 if (fiq
->forget_list_head
.next
== NULL
)
1140 fiq
->forget_list_tail
= &fiq
->forget_list_head
;
1148 static int fuse_read_single_forget(struct fuse_iqueue
*fiq
,
1149 struct fuse_copy_state
*cs
,
1151 __releases(fiq
->waitq
.lock
)
1154 struct fuse_forget_link
*forget
= dequeue_forget(fiq
, 1, NULL
);
1155 struct fuse_forget_in arg
= {
1156 .nlookup
= forget
->forget_one
.nlookup
,
1158 struct fuse_in_header ih
= {
1159 .opcode
= FUSE_FORGET
,
1160 .nodeid
= forget
->forget_one
.nodeid
,
1161 .unique
= fuse_get_unique(fiq
),
1162 .len
= sizeof(ih
) + sizeof(arg
),
1165 spin_unlock(&fiq
->waitq
.lock
);
1167 if (nbytes
< ih
.len
)
1170 err
= fuse_copy_one(cs
, &ih
, sizeof(ih
));
1172 err
= fuse_copy_one(cs
, &arg
, sizeof(arg
));
1173 fuse_copy_finish(cs
);
1181 static int fuse_read_batch_forget(struct fuse_iqueue
*fiq
,
1182 struct fuse_copy_state
*cs
, size_t nbytes
)
1183 __releases(fiq
->waitq
.lock
)
1186 unsigned max_forgets
;
1188 struct fuse_forget_link
*head
;
1189 struct fuse_batch_forget_in arg
= { .count
= 0 };
1190 struct fuse_in_header ih
= {
1191 .opcode
= FUSE_BATCH_FORGET
,
1192 .unique
= fuse_get_unique(fiq
),
1193 .len
= sizeof(ih
) + sizeof(arg
),
1196 if (nbytes
< ih
.len
) {
1197 spin_unlock(&fiq
->waitq
.lock
);
1201 max_forgets
= (nbytes
- ih
.len
) / sizeof(struct fuse_forget_one
);
1202 head
= dequeue_forget(fiq
, max_forgets
, &count
);
1203 spin_unlock(&fiq
->waitq
.lock
);
1206 ih
.len
+= count
* sizeof(struct fuse_forget_one
);
1207 err
= fuse_copy_one(cs
, &ih
, sizeof(ih
));
1209 err
= fuse_copy_one(cs
, &arg
, sizeof(arg
));
1212 struct fuse_forget_link
*forget
= head
;
1215 err
= fuse_copy_one(cs
, &forget
->forget_one
,
1216 sizeof(forget
->forget_one
));
1218 head
= forget
->next
;
1222 fuse_copy_finish(cs
);
1230 static int fuse_read_forget(struct fuse_conn
*fc
, struct fuse_iqueue
*fiq
,
1231 struct fuse_copy_state
*cs
,
1233 __releases(fiq
->waitq
.lock
)
1235 if (fc
->minor
< 16 || fiq
->forget_list_head
.next
->next
== NULL
)
1236 return fuse_read_single_forget(fiq
, cs
, nbytes
);
1238 return fuse_read_batch_forget(fiq
, cs
, nbytes
);
1242 * Read a single request into the userspace filesystem's buffer. This
1243 * function waits until a request is available, then removes it from
1244 * the pending list and copies request data to userspace buffer. If
1245 * no reply is needed (FORGET) or request has been aborted or there
1246 * was an error during the copying then it's finished by calling
1247 * request_end(). Otherwise add it to the processing list, and set
1250 static ssize_t
fuse_dev_do_read(struct fuse_dev
*fud
, struct file
*file
,
1251 struct fuse_copy_state
*cs
, size_t nbytes
)
1254 struct fuse_conn
*fc
= fud
->fc
;
1255 struct fuse_iqueue
*fiq
= &fc
->iq
;
1256 struct fuse_pqueue
*fpq
= &fud
->pq
;
1257 struct fuse_req
*req
;
1262 spin_lock(&fiq
->waitq
.lock
);
1264 if ((file
->f_flags
& O_NONBLOCK
) && fiq
->connected
&&
1265 !request_pending(fiq
))
1268 err
= wait_event_interruptible_exclusive_locked(fiq
->waitq
,
1269 !fiq
->connected
|| request_pending(fiq
));
1274 if (!fiq
->connected
)
1277 if (!list_empty(&fiq
->interrupts
)) {
1278 req
= list_entry(fiq
->interrupts
.next
, struct fuse_req
,
1280 return fuse_read_interrupt(fiq
, cs
, nbytes
, req
);
1283 if (forget_pending(fiq
)) {
1284 if (list_empty(&fiq
->pending
) || fiq
->forget_batch
-- > 0)
1285 return fuse_read_forget(fc
, fiq
, cs
, nbytes
);
1287 if (fiq
->forget_batch
<= -8)
1288 fiq
->forget_batch
= 16;
1291 req
= list_entry(fiq
->pending
.next
, struct fuse_req
, list
);
1292 clear_bit(FR_PENDING
, &req
->flags
);
1293 list_del_init(&req
->list
);
1294 spin_unlock(&fiq
->waitq
.lock
);
1297 reqsize
= in
->h
.len
;
1298 /* If request is too large, reply with an error and restart the read */
1299 if (nbytes
< reqsize
) {
1300 req
->out
.h
.error
= -EIO
;
1301 /* SETXATTR is special, since it may contain too large data */
1302 if (in
->h
.opcode
== FUSE_SETXATTR
)
1303 req
->out
.h
.error
= -E2BIG
;
1304 request_end(fc
, req
);
1307 spin_lock(&fpq
->lock
);
1308 list_add(&req
->list
, &fpq
->io
);
1309 spin_unlock(&fpq
->lock
);
1311 err
= fuse_copy_one(cs
, &in
->h
, sizeof(in
->h
));
1313 err
= fuse_copy_args(cs
, in
->numargs
, in
->argpages
,
1314 (struct fuse_arg
*) in
->args
, 0);
1315 fuse_copy_finish(cs
);
1316 spin_lock(&fpq
->lock
);
1317 clear_bit(FR_LOCKED
, &req
->flags
);
1318 if (!fpq
->connected
) {
1323 req
->out
.h
.error
= -EIO
;
1326 if (!test_bit(FR_ISREPLY
, &req
->flags
)) {
1330 list_move_tail(&req
->list
, &fpq
->processing
);
1331 spin_unlock(&fpq
->lock
);
1332 set_bit(FR_SENT
, &req
->flags
);
1333 /* matches barrier in request_wait_answer() */
1334 smp_mb__after_atomic();
1335 if (test_bit(FR_INTERRUPTED
, &req
->flags
))
1336 queue_interrupt(fiq
, req
);
1341 if (!test_bit(FR_PRIVATE
, &req
->flags
))
1342 list_del_init(&req
->list
);
1343 spin_unlock(&fpq
->lock
);
1344 request_end(fc
, req
);
1348 spin_unlock(&fiq
->waitq
.lock
);
1352 static int fuse_dev_open(struct inode
*inode
, struct file
*file
)
1355 * The fuse device's file's private_data is used to hold
1356 * the fuse_conn(ection) when it is mounted, and is used to
1357 * keep track of whether the file has been mounted already.
1359 file
->private_data
= NULL
;
1363 static ssize_t
fuse_dev_read(struct kiocb
*iocb
, struct iov_iter
*to
)
1365 struct fuse_copy_state cs
;
1366 struct file
*file
= iocb
->ki_filp
;
1367 struct fuse_dev
*fud
= fuse_get_dev(file
);
1372 if (!iter_is_iovec(to
))
1375 fuse_copy_init(&cs
, 1, to
);
1377 return fuse_dev_do_read(fud
, file
, &cs
, iov_iter_count(to
));
1380 static ssize_t
fuse_dev_splice_read(struct file
*in
, loff_t
*ppos
,
1381 struct pipe_inode_info
*pipe
,
1382 size_t len
, unsigned int flags
)
1387 struct pipe_buffer
*bufs
;
1388 struct fuse_copy_state cs
;
1389 struct fuse_dev
*fud
= fuse_get_dev(in
);
1394 bufs
= kmalloc(pipe
->buffers
* sizeof(struct pipe_buffer
), GFP_KERNEL
);
1398 fuse_copy_init(&cs
, 1, NULL
);
1401 ret
= fuse_dev_do_read(fud
, in
, &cs
, len
);
1408 if (!pipe
->readers
) {
1409 send_sig(SIGPIPE
, current
, 0);
1415 if (pipe
->nrbufs
+ cs
.nr_segs
> pipe
->buffers
) {
1420 while (page_nr
< cs
.nr_segs
) {
1421 int newbuf
= (pipe
->curbuf
+ pipe
->nrbufs
) & (pipe
->buffers
- 1);
1422 struct pipe_buffer
*buf
= pipe
->bufs
+ newbuf
;
1424 buf
->page
= bufs
[page_nr
].page
;
1425 buf
->offset
= bufs
[page_nr
].offset
;
1426 buf
->len
= bufs
[page_nr
].len
;
1428 * Need to be careful about this. Having buf->ops in module
1429 * code can Oops if the buffer persists after module unload.
1431 buf
->ops
= &nosteal_pipe_buf_ops
;
1446 if (waitqueue_active(&pipe
->wait
))
1447 wake_up_interruptible(&pipe
->wait
);
1448 kill_fasync(&pipe
->fasync_readers
, SIGIO
, POLL_IN
);
1452 for (; page_nr
< cs
.nr_segs
; page_nr
++)
1453 page_cache_release(bufs
[page_nr
].page
);
1459 static int fuse_notify_poll(struct fuse_conn
*fc
, unsigned int size
,
1460 struct fuse_copy_state
*cs
)
1462 struct fuse_notify_poll_wakeup_out outarg
;
1465 if (size
!= sizeof(outarg
))
1468 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1472 fuse_copy_finish(cs
);
1473 return fuse_notify_poll_wakeup(fc
, &outarg
);
1476 fuse_copy_finish(cs
);
1480 static int fuse_notify_inval_inode(struct fuse_conn
*fc
, unsigned int size
,
1481 struct fuse_copy_state
*cs
)
1483 struct fuse_notify_inval_inode_out outarg
;
1486 if (size
!= sizeof(outarg
))
1489 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1492 fuse_copy_finish(cs
);
1494 down_read(&fc
->killsb
);
1497 err
= fuse_reverse_inval_inode(fc
->sb
, outarg
.ino
,
1498 outarg
.off
, outarg
.len
);
1500 up_read(&fc
->killsb
);
1504 fuse_copy_finish(cs
);
1508 static int fuse_notify_inval_entry(struct fuse_conn
*fc
, unsigned int size
,
1509 struct fuse_copy_state
*cs
)
1511 struct fuse_notify_inval_entry_out outarg
;
1516 buf
= kzalloc(FUSE_NAME_MAX
+ 1, GFP_KERNEL
);
1521 if (size
< sizeof(outarg
))
1524 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1528 err
= -ENAMETOOLONG
;
1529 if (outarg
.namelen
> FUSE_NAME_MAX
)
1533 if (size
!= sizeof(outarg
) + outarg
.namelen
+ 1)
1537 name
.len
= outarg
.namelen
;
1538 err
= fuse_copy_one(cs
, buf
, outarg
.namelen
+ 1);
1541 fuse_copy_finish(cs
);
1542 buf
[outarg
.namelen
] = 0;
1543 name
.hash
= full_name_hash(name
.name
, name
.len
);
1545 down_read(&fc
->killsb
);
1548 err
= fuse_reverse_inval_entry(fc
->sb
, outarg
.parent
, 0, &name
);
1549 up_read(&fc
->killsb
);
1555 fuse_copy_finish(cs
);
1559 static int fuse_notify_delete(struct fuse_conn
*fc
, unsigned int size
,
1560 struct fuse_copy_state
*cs
)
1562 struct fuse_notify_delete_out outarg
;
1567 buf
= kzalloc(FUSE_NAME_MAX
+ 1, GFP_KERNEL
);
1572 if (size
< sizeof(outarg
))
1575 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1579 err
= -ENAMETOOLONG
;
1580 if (outarg
.namelen
> FUSE_NAME_MAX
)
1584 if (size
!= sizeof(outarg
) + outarg
.namelen
+ 1)
1588 name
.len
= outarg
.namelen
;
1589 err
= fuse_copy_one(cs
, buf
, outarg
.namelen
+ 1);
1592 fuse_copy_finish(cs
);
1593 buf
[outarg
.namelen
] = 0;
1594 name
.hash
= full_name_hash(name
.name
, name
.len
);
1596 down_read(&fc
->killsb
);
1599 err
= fuse_reverse_inval_entry(fc
->sb
, outarg
.parent
,
1600 outarg
.child
, &name
);
1601 up_read(&fc
->killsb
);
1607 fuse_copy_finish(cs
);
1611 static int fuse_notify_store(struct fuse_conn
*fc
, unsigned int size
,
1612 struct fuse_copy_state
*cs
)
1614 struct fuse_notify_store_out outarg
;
1615 struct inode
*inode
;
1616 struct address_space
*mapping
;
1620 unsigned int offset
;
1626 if (size
< sizeof(outarg
))
1629 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1634 if (size
- sizeof(outarg
) != outarg
.size
)
1637 nodeid
= outarg
.nodeid
;
1639 down_read(&fc
->killsb
);
1645 inode
= ilookup5(fc
->sb
, nodeid
, fuse_inode_eq
, &nodeid
);
1649 mapping
= inode
->i_mapping
;
1650 index
= outarg
.offset
>> PAGE_CACHE_SHIFT
;
1651 offset
= outarg
.offset
& ~PAGE_CACHE_MASK
;
1652 file_size
= i_size_read(inode
);
1653 end
= outarg
.offset
+ outarg
.size
;
1654 if (end
> file_size
) {
1656 fuse_write_update_size(inode
, file_size
);
1662 unsigned int this_num
;
1665 page
= find_or_create_page(mapping
, index
,
1666 mapping_gfp_mask(mapping
));
1670 this_num
= min_t(unsigned, num
, PAGE_CACHE_SIZE
- offset
);
1671 err
= fuse_copy_page(cs
, &page
, offset
, this_num
, 0);
1672 if (!err
&& offset
== 0 &&
1673 (this_num
== PAGE_CACHE_SIZE
|| file_size
== end
))
1674 SetPageUptodate(page
);
1676 page_cache_release(page
);
1691 up_read(&fc
->killsb
);
1693 fuse_copy_finish(cs
);
1697 static void fuse_retrieve_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
1699 release_pages(req
->pages
, req
->num_pages
, false);
1702 static int fuse_retrieve(struct fuse_conn
*fc
, struct inode
*inode
,
1703 struct fuse_notify_retrieve_out
*outarg
)
1706 struct address_space
*mapping
= inode
->i_mapping
;
1707 struct fuse_req
*req
;
1711 unsigned int offset
;
1712 size_t total_len
= 0;
1715 offset
= outarg
->offset
& ~PAGE_CACHE_MASK
;
1716 file_size
= i_size_read(inode
);
1719 if (outarg
->offset
> file_size
)
1721 else if (outarg
->offset
+ num
> file_size
)
1722 num
= file_size
- outarg
->offset
;
1724 num_pages
= (num
+ offset
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1725 num_pages
= min(num_pages
, FUSE_MAX_PAGES_PER_REQ
);
1727 req
= fuse_get_req(fc
, num_pages
);
1729 return PTR_ERR(req
);
1731 req
->in
.h
.opcode
= FUSE_NOTIFY_REPLY
;
1732 req
->in
.h
.nodeid
= outarg
->nodeid
;
1733 req
->in
.numargs
= 2;
1734 req
->in
.argpages
= 1;
1735 req
->page_descs
[0].offset
= offset
;
1736 req
->end
= fuse_retrieve_end
;
1738 index
= outarg
->offset
>> PAGE_CACHE_SHIFT
;
1740 while (num
&& req
->num_pages
< num_pages
) {
1742 unsigned int this_num
;
1744 page
= find_get_page(mapping
, index
);
1748 this_num
= min_t(unsigned, num
, PAGE_CACHE_SIZE
- offset
);
1749 req
->pages
[req
->num_pages
] = page
;
1750 req
->page_descs
[req
->num_pages
].length
= this_num
;
1755 total_len
+= this_num
;
1758 req
->misc
.retrieve_in
.offset
= outarg
->offset
;
1759 req
->misc
.retrieve_in
.size
= total_len
;
1760 req
->in
.args
[0].size
= sizeof(req
->misc
.retrieve_in
);
1761 req
->in
.args
[0].value
= &req
->misc
.retrieve_in
;
1762 req
->in
.args
[1].size
= total_len
;
1764 err
= fuse_request_send_notify_reply(fc
, req
, outarg
->notify_unique
);
1766 fuse_retrieve_end(fc
, req
);
1771 static int fuse_notify_retrieve(struct fuse_conn
*fc
, unsigned int size
,
1772 struct fuse_copy_state
*cs
)
1774 struct fuse_notify_retrieve_out outarg
;
1775 struct inode
*inode
;
1779 if (size
!= sizeof(outarg
))
1782 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1786 fuse_copy_finish(cs
);
1788 down_read(&fc
->killsb
);
1791 u64 nodeid
= outarg
.nodeid
;
1793 inode
= ilookup5(fc
->sb
, nodeid
, fuse_inode_eq
, &nodeid
);
1795 err
= fuse_retrieve(fc
, inode
, &outarg
);
1799 up_read(&fc
->killsb
);
1804 fuse_copy_finish(cs
);
1808 static int fuse_notify(struct fuse_conn
*fc
, enum fuse_notify_code code
,
1809 unsigned int size
, struct fuse_copy_state
*cs
)
1811 /* Don't try to move pages (yet) */
1815 case FUSE_NOTIFY_POLL
:
1816 return fuse_notify_poll(fc
, size
, cs
);
1818 case FUSE_NOTIFY_INVAL_INODE
:
1819 return fuse_notify_inval_inode(fc
, size
, cs
);
1821 case FUSE_NOTIFY_INVAL_ENTRY
:
1822 return fuse_notify_inval_entry(fc
, size
, cs
);
1824 case FUSE_NOTIFY_STORE
:
1825 return fuse_notify_store(fc
, size
, cs
);
1827 case FUSE_NOTIFY_RETRIEVE
:
1828 return fuse_notify_retrieve(fc
, size
, cs
);
1830 case FUSE_NOTIFY_DELETE
:
1831 return fuse_notify_delete(fc
, size
, cs
);
1834 fuse_copy_finish(cs
);
1839 /* Look up request on processing list by unique ID */
1840 static struct fuse_req
*request_find(struct fuse_pqueue
*fpq
, u64 unique
)
1842 struct fuse_req
*req
;
1844 list_for_each_entry(req
, &fpq
->processing
, list
) {
1845 if (req
->in
.h
.unique
== unique
|| req
->intr_unique
== unique
)
1851 static int copy_out_args(struct fuse_copy_state
*cs
, struct fuse_out
*out
,
1854 unsigned reqsize
= sizeof(struct fuse_out_header
);
1857 return nbytes
!= reqsize
? -EINVAL
: 0;
1859 reqsize
+= len_args(out
->numargs
, out
->args
);
1861 if (reqsize
< nbytes
|| (reqsize
> nbytes
&& !out
->argvar
))
1863 else if (reqsize
> nbytes
) {
1864 struct fuse_arg
*lastarg
= &out
->args
[out
->numargs
-1];
1865 unsigned diffsize
= reqsize
- nbytes
;
1866 if (diffsize
> lastarg
->size
)
1868 lastarg
->size
-= diffsize
;
1870 return fuse_copy_args(cs
, out
->numargs
, out
->argpages
, out
->args
,
1875 * Write a single reply to a request. First the header is copied from
1876 * the write buffer. The request is then searched on the processing
1877 * list by the unique ID found in the header. If found, then remove
1878 * it from the list and copy the rest of the buffer to the request.
1879 * The request is finished by calling request_end()
1881 static ssize_t
fuse_dev_do_write(struct fuse_dev
*fud
,
1882 struct fuse_copy_state
*cs
, size_t nbytes
)
1885 struct fuse_conn
*fc
= fud
->fc
;
1886 struct fuse_pqueue
*fpq
= &fud
->pq
;
1887 struct fuse_req
*req
;
1888 struct fuse_out_header oh
;
1890 if (nbytes
< sizeof(struct fuse_out_header
))
1893 err
= fuse_copy_one(cs
, &oh
, sizeof(oh
));
1898 if (oh
.len
!= nbytes
)
1902 * Zero oh.unique indicates unsolicited notification message
1903 * and error contains notification code.
1906 err
= fuse_notify(fc
, oh
.error
, nbytes
- sizeof(oh
), cs
);
1907 return err
? err
: nbytes
;
1911 if (oh
.error
<= -1000 || oh
.error
> 0)
1914 spin_lock(&fpq
->lock
);
1916 if (!fpq
->connected
)
1919 req
= request_find(fpq
, oh
.unique
);
1923 /* Is it an interrupt reply? */
1924 if (req
->intr_unique
== oh
.unique
) {
1925 spin_unlock(&fpq
->lock
);
1928 if (nbytes
!= sizeof(struct fuse_out_header
))
1931 if (oh
.error
== -ENOSYS
)
1932 fc
->no_interrupt
= 1;
1933 else if (oh
.error
== -EAGAIN
)
1934 queue_interrupt(&fc
->iq
, req
);
1936 fuse_copy_finish(cs
);
1940 clear_bit(FR_SENT
, &req
->flags
);
1941 list_move(&req
->list
, &fpq
->io
);
1943 set_bit(FR_LOCKED
, &req
->flags
);
1944 spin_unlock(&fpq
->lock
);
1946 if (!req
->out
.page_replace
)
1949 err
= copy_out_args(cs
, &req
->out
, nbytes
);
1950 fuse_copy_finish(cs
);
1952 spin_lock(&fpq
->lock
);
1953 clear_bit(FR_LOCKED
, &req
->flags
);
1954 if (!fpq
->connected
)
1957 req
->out
.h
.error
= -EIO
;
1958 if (!test_bit(FR_PRIVATE
, &req
->flags
))
1959 list_del_init(&req
->list
);
1960 spin_unlock(&fpq
->lock
);
1962 request_end(fc
, req
);
1964 return err
? err
: nbytes
;
1967 spin_unlock(&fpq
->lock
);
1969 fuse_copy_finish(cs
);
1973 static ssize_t
fuse_dev_write(struct kiocb
*iocb
, struct iov_iter
*from
)
1975 struct fuse_copy_state cs
;
1976 struct fuse_dev
*fud
= fuse_get_dev(iocb
->ki_filp
);
1981 if (!iter_is_iovec(from
))
1984 fuse_copy_init(&cs
, 0, from
);
1986 return fuse_dev_do_write(fud
, &cs
, iov_iter_count(from
));
1989 static ssize_t
fuse_dev_splice_write(struct pipe_inode_info
*pipe
,
1990 struct file
*out
, loff_t
*ppos
,
1991 size_t len
, unsigned int flags
)
1995 struct pipe_buffer
*bufs
;
1996 struct fuse_copy_state cs
;
1997 struct fuse_dev
*fud
;
2001 fud
= fuse_get_dev(out
);
2007 bufs
= kmalloc(pipe
->buffers
* sizeof(struct pipe_buffer
), GFP_KERNEL
);
2015 for (idx
= 0; idx
< pipe
->nrbufs
&& rem
< len
; idx
++)
2016 rem
+= pipe
->bufs
[(pipe
->curbuf
+ idx
) & (pipe
->buffers
- 1)].len
;
2026 struct pipe_buffer
*ibuf
;
2027 struct pipe_buffer
*obuf
;
2029 BUG_ON(nbuf
>= pipe
->buffers
);
2030 BUG_ON(!pipe
->nrbufs
);
2031 ibuf
= &pipe
->bufs
[pipe
->curbuf
];
2034 if (rem
>= ibuf
->len
) {
2037 pipe
->curbuf
= (pipe
->curbuf
+ 1) & (pipe
->buffers
- 1);
2040 ibuf
->ops
->get(pipe
, ibuf
);
2042 obuf
->flags
&= ~PIPE_BUF_FLAG_GIFT
;
2044 ibuf
->offset
+= obuf
->len
;
2045 ibuf
->len
-= obuf
->len
;
2052 fuse_copy_init(&cs
, 0, NULL
);
2057 if (flags
& SPLICE_F_MOVE
)
2060 ret
= fuse_dev_do_write(fud
, &cs
, len
);
2062 for (idx
= 0; idx
< nbuf
; idx
++) {
2063 struct pipe_buffer
*buf
= &bufs
[idx
];
2064 buf
->ops
->release(pipe
, buf
);
2071 static unsigned fuse_dev_poll(struct file
*file
, poll_table
*wait
)
2073 unsigned mask
= POLLOUT
| POLLWRNORM
;
2074 struct fuse_iqueue
*fiq
;
2075 struct fuse_dev
*fud
= fuse_get_dev(file
);
2081 poll_wait(file
, &fiq
->waitq
, wait
);
2083 spin_lock(&fiq
->waitq
.lock
);
2084 if (!fiq
->connected
)
2086 else if (request_pending(fiq
))
2087 mask
|= POLLIN
| POLLRDNORM
;
2088 spin_unlock(&fiq
->waitq
.lock
);
2094 * Abort all requests on the given list (pending or processing)
2096 * This function releases and reacquires fc->lock
2098 static void end_requests(struct fuse_conn
*fc
, struct list_head
*head
)
2100 while (!list_empty(head
)) {
2101 struct fuse_req
*req
;
2102 req
= list_entry(head
->next
, struct fuse_req
, list
);
2103 req
->out
.h
.error
= -ECONNABORTED
;
2104 clear_bit(FR_SENT
, &req
->flags
);
2105 list_del_init(&req
->list
);
2106 request_end(fc
, req
);
2110 static void end_polls(struct fuse_conn
*fc
)
2114 p
= rb_first(&fc
->polled_files
);
2117 struct fuse_file
*ff
;
2118 ff
= rb_entry(p
, struct fuse_file
, polled_node
);
2119 wake_up_interruptible_all(&ff
->poll_wait
);
2126 * Abort all requests.
2128 * Emergency exit in case of a malicious or accidental deadlock, or just a hung
2131 * The same effect is usually achievable through killing the filesystem daemon
2132 * and all users of the filesystem. The exception is the combination of an
2133 * asynchronous request and the tricky deadlock (see
2134 * Documentation/filesystems/fuse.txt).
2136 * Aborting requests under I/O goes as follows: 1: Separate out unlocked
2137 * requests, they should be finished off immediately. Locked requests will be
2138 * finished after unlock; see unlock_request(). 2: Finish off the unlocked
2139 * requests. It is possible that some request will finish before we can. This
2140 * is OK, the request will in that case be removed from the list before we touch
2143 void fuse_abort_conn(struct fuse_conn
*fc
)
2145 struct fuse_iqueue
*fiq
= &fc
->iq
;
2147 spin_lock(&fc
->lock
);
2148 if (fc
->connected
) {
2149 struct fuse_dev
*fud
;
2150 struct fuse_req
*req
, *next
;
2156 fuse_set_initialized(fc
);
2157 list_for_each_entry(fud
, &fc
->devices
, entry
) {
2158 struct fuse_pqueue
*fpq
= &fud
->pq
;
2160 spin_lock(&fpq
->lock
);
2162 list_for_each_entry_safe(req
, next
, &fpq
->io
, list
) {
2163 req
->out
.h
.error
= -ECONNABORTED
;
2164 spin_lock(&req
->waitq
.lock
);
2165 set_bit(FR_ABORTED
, &req
->flags
);
2166 if (!test_bit(FR_LOCKED
, &req
->flags
)) {
2167 set_bit(FR_PRIVATE
, &req
->flags
);
2168 __fuse_get_request(req
);
2169 list_move(&req
->list
, &to_end1
);
2171 spin_unlock(&req
->waitq
.lock
);
2173 list_splice_init(&fpq
->processing
, &to_end2
);
2174 spin_unlock(&fpq
->lock
);
2176 fc
->max_background
= UINT_MAX
;
2179 spin_lock(&fiq
->waitq
.lock
);
2181 list_splice_init(&fiq
->pending
, &to_end2
);
2182 list_for_each_entry(req
, &to_end2
, list
)
2183 clear_bit(FR_PENDING
, &req
->flags
);
2184 while (forget_pending(fiq
))
2185 kfree(dequeue_forget(fiq
, 1, NULL
));
2186 wake_up_all_locked(&fiq
->waitq
);
2187 spin_unlock(&fiq
->waitq
.lock
);
2188 kill_fasync(&fiq
->fasync
, SIGIO
, POLL_IN
);
2190 wake_up_all(&fc
->blocked_waitq
);
2191 spin_unlock(&fc
->lock
);
2193 while (!list_empty(&to_end1
)) {
2194 req
= list_first_entry(&to_end1
, struct fuse_req
, list
);
2195 list_del_init(&req
->list
);
2196 request_end(fc
, req
);
2198 end_requests(fc
, &to_end2
);
2200 spin_unlock(&fc
->lock
);
2203 EXPORT_SYMBOL_GPL(fuse_abort_conn
);
2205 void fuse_wait_aborted(struct fuse_conn
*fc
)
2207 wait_event(fc
->blocked_waitq
, atomic_read(&fc
->num_waiting
) == 0);
2210 int fuse_dev_release(struct inode
*inode
, struct file
*file
)
2212 struct fuse_dev
*fud
= fuse_get_dev(file
);
2215 struct fuse_conn
*fc
= fud
->fc
;
2216 struct fuse_pqueue
*fpq
= &fud
->pq
;
2219 spin_lock(&fpq
->lock
);
2220 WARN_ON(!list_empty(&fpq
->io
));
2221 list_splice_init(&fpq
->processing
, &to_end
);
2222 spin_unlock(&fpq
->lock
);
2224 end_requests(fc
, &to_end
);
2226 /* Are we the last open device? */
2227 if (atomic_dec_and_test(&fc
->dev_count
)) {
2228 WARN_ON(fc
->iq
.fasync
!= NULL
);
2229 fuse_abort_conn(fc
);
2235 EXPORT_SYMBOL_GPL(fuse_dev_release
);
2237 static int fuse_dev_fasync(int fd
, struct file
*file
, int on
)
2239 struct fuse_dev
*fud
= fuse_get_dev(file
);
2244 /* No locking - fasync_helper does its own locking */
2245 return fasync_helper(fd
, file
, on
, &fud
->fc
->iq
.fasync
);
2248 static int fuse_device_clone(struct fuse_conn
*fc
, struct file
*new)
2250 struct fuse_dev
*fud
;
2252 if (new->private_data
)
2255 fud
= fuse_dev_alloc(fc
);
2259 new->private_data
= fud
;
2260 atomic_inc(&fc
->dev_count
);
2265 static long fuse_dev_ioctl(struct file
*file
, unsigned int cmd
,
2270 if (cmd
== FUSE_DEV_IOC_CLONE
) {
2274 if (!get_user(oldfd
, (__u32 __user
*) arg
)) {
2275 struct file
*old
= fget(oldfd
);
2279 struct fuse_dev
*fud
= NULL
;
2282 * Check against file->f_op because CUSE
2283 * uses the same ioctl handler.
2285 if (old
->f_op
== file
->f_op
&&
2286 old
->f_cred
->user_ns
== file
->f_cred
->user_ns
)
2287 fud
= fuse_get_dev(old
);
2290 mutex_lock(&fuse_mutex
);
2291 err
= fuse_device_clone(fud
->fc
, file
);
2292 mutex_unlock(&fuse_mutex
);
2301 const struct file_operations fuse_dev_operations
= {
2302 .owner
= THIS_MODULE
,
2303 .open
= fuse_dev_open
,
2304 .llseek
= no_llseek
,
2305 .read_iter
= fuse_dev_read
,
2306 .splice_read
= fuse_dev_splice_read
,
2307 .write_iter
= fuse_dev_write
,
2308 .splice_write
= fuse_dev_splice_write
,
2309 .poll
= fuse_dev_poll
,
2310 .release
= fuse_dev_release
,
2311 .fasync
= fuse_dev_fasync
,
2312 .unlocked_ioctl
= fuse_dev_ioctl
,
2313 .compat_ioctl
= fuse_dev_ioctl
,
2315 EXPORT_SYMBOL_GPL(fuse_dev_operations
);
2317 static struct miscdevice fuse_miscdevice
= {
2318 .minor
= FUSE_MINOR
,
2320 .fops
= &fuse_dev_operations
,
2323 int __init
fuse_dev_init(void)
2326 fuse_req_cachep
= kmem_cache_create("fuse_request",
2327 sizeof(struct fuse_req
),
2329 if (!fuse_req_cachep
)
2332 err
= misc_register(&fuse_miscdevice
);
2334 goto out_cache_clean
;
2339 kmem_cache_destroy(fuse_req_cachep
);
2344 void fuse_dev_cleanup(void)
2346 misc_deregister(&fuse_miscdevice
);
2347 kmem_cache_destroy(fuse_req_cachep
);