2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2006 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>
20 MODULE_ALIAS_MISCDEV(FUSE_MINOR
);
22 static kmem_cache_t
*fuse_req_cachep
;
24 static struct fuse_conn
*fuse_get_conn(struct file
*file
)
27 * Lockless access is OK, because file->private data is set
28 * once during mount and is valid until the file is released.
30 return file
->private_data
;
33 static void fuse_request_init(struct fuse_req
*req
)
35 memset(req
, 0, sizeof(*req
));
36 INIT_LIST_HEAD(&req
->list
);
37 INIT_LIST_HEAD(&req
->intr_entry
);
38 init_waitqueue_head(&req
->waitq
);
39 atomic_set(&req
->count
, 1);
42 struct fuse_req
*fuse_request_alloc(void)
44 struct fuse_req
*req
= kmem_cache_alloc(fuse_req_cachep
, SLAB_KERNEL
);
46 fuse_request_init(req
);
50 void fuse_request_free(struct fuse_req
*req
)
52 kmem_cache_free(fuse_req_cachep
, req
);
55 static void block_sigs(sigset_t
*oldset
)
59 siginitsetinv(&mask
, sigmask(SIGKILL
));
60 sigprocmask(SIG_BLOCK
, &mask
, oldset
);
63 static void restore_sigs(sigset_t
*oldset
)
65 sigprocmask(SIG_SETMASK
, oldset
, NULL
);
68 static void __fuse_get_request(struct fuse_req
*req
)
70 atomic_inc(&req
->count
);
73 /* Must be called with > 1 refcount */
74 static void __fuse_put_request(struct fuse_req
*req
)
76 BUG_ON(atomic_read(&req
->count
) < 2);
77 atomic_dec(&req
->count
);
80 static void fuse_req_init_context(struct fuse_req
*req
)
82 req
->in
.h
.uid
= current
->fsuid
;
83 req
->in
.h
.gid
= current
->fsgid
;
84 req
->in
.h
.pid
= current
->pid
;
87 struct fuse_req
*fuse_get_req(struct fuse_conn
*fc
)
94 atomic_inc(&fc
->num_waiting
);
96 intr
= wait_event_interruptible(fc
->blocked_waitq
, !fc
->blocked
);
97 restore_sigs(&oldset
);
106 req
= fuse_request_alloc();
111 fuse_req_init_context(req
);
116 atomic_dec(&fc
->num_waiting
);
121 * Return request in fuse_file->reserved_req. However that may
122 * currently be in use. If that is the case, wait for it to become
125 static struct fuse_req
*get_reserved_req(struct fuse_conn
*fc
,
128 struct fuse_req
*req
= NULL
;
129 struct fuse_file
*ff
= file
->private_data
;
132 wait_event(fc
->blocked_waitq
, ff
->reserved_req
);
133 spin_lock(&fc
->lock
);
134 if (ff
->reserved_req
) {
135 req
= ff
->reserved_req
;
136 ff
->reserved_req
= NULL
;
138 req
->stolen_file
= file
;
140 spin_unlock(&fc
->lock
);
147 * Put stolen request back into fuse_file->reserved_req
149 static void put_reserved_req(struct fuse_conn
*fc
, struct fuse_req
*req
)
151 struct file
*file
= req
->stolen_file
;
152 struct fuse_file
*ff
= file
->private_data
;
154 spin_lock(&fc
->lock
);
155 fuse_request_init(req
);
156 BUG_ON(ff
->reserved_req
);
157 ff
->reserved_req
= req
;
158 wake_up(&fc
->blocked_waitq
);
159 spin_unlock(&fc
->lock
);
164 * Gets a requests for a file operation, always succeeds
166 * This is used for sending the FLUSH request, which must get to
167 * userspace, due to POSIX locks which may need to be unlocked.
169 * If allocation fails due to OOM, use the reserved request in
172 * This is very unlikely to deadlock accidentally, since the
173 * filesystem should not have it's own file open. If deadlock is
174 * intentional, it can still be broken by "aborting" the filesystem.
176 struct fuse_req
*fuse_get_req_nofail(struct fuse_conn
*fc
, struct file
*file
)
178 struct fuse_req
*req
;
180 atomic_inc(&fc
->num_waiting
);
181 wait_event(fc
->blocked_waitq
, !fc
->blocked
);
182 req
= fuse_request_alloc();
184 req
= get_reserved_req(fc
, file
);
186 fuse_req_init_context(req
);
191 void fuse_put_request(struct fuse_conn
*fc
, struct fuse_req
*req
)
193 if (atomic_dec_and_test(&req
->count
)) {
195 atomic_dec(&fc
->num_waiting
);
197 if (req
->stolen_file
)
198 put_reserved_req(fc
, req
);
200 fuse_request_free(req
);
205 * This function is called when a request is finished. Either a reply
206 * has arrived or it was aborted (and not yet sent) or some error
207 * occurred during communication with userspace, or the device file
208 * was closed. The requester thread is woken up (if still waiting),
209 * the 'end' callback is called if given, else the reference to the
210 * request is released
212 * Called with fc->lock, unlocks it
214 static void request_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
216 void (*end
) (struct fuse_conn
*, struct fuse_req
*) = req
->end
;
218 list_del(&req
->list
);
219 list_del(&req
->intr_entry
);
220 req
->state
= FUSE_REQ_FINISHED
;
221 if (req
->background
) {
222 if (fc
->num_background
== FUSE_MAX_BACKGROUND
) {
224 wake_up_all(&fc
->blocked_waitq
);
226 fc
->num_background
--;
228 spin_unlock(&fc
->lock
);
230 mntput(req
->vfsmount
);
233 wake_up(&req
->waitq
);
237 fuse_put_request(fc
, req
);
240 static void wait_answer_interruptible(struct fuse_conn
*fc
,
241 struct fuse_req
*req
)
243 if (signal_pending(current
))
246 spin_unlock(&fc
->lock
);
247 wait_event_interruptible(req
->waitq
, req
->state
== FUSE_REQ_FINISHED
);
248 spin_lock(&fc
->lock
);
251 static void queue_interrupt(struct fuse_conn
*fc
, struct fuse_req
*req
)
253 list_add_tail(&req
->intr_entry
, &fc
->interrupts
);
255 kill_fasync(&fc
->fasync
, SIGIO
, POLL_IN
);
258 /* Called with fc->lock held. Releases, and then reacquires it. */
259 static void request_wait_answer(struct fuse_conn
*fc
, struct fuse_req
*req
)
261 if (!fc
->no_interrupt
) {
262 /* Any signal may interrupt this */
263 wait_answer_interruptible(fc
, req
);
267 if (req
->state
== FUSE_REQ_FINISHED
)
270 req
->interrupted
= 1;
271 if (req
->state
== FUSE_REQ_SENT
)
272 queue_interrupt(fc
, req
);
276 spin_unlock(&fc
->lock
);
277 wait_event(req
->waitq
, req
->state
== FUSE_REQ_FINISHED
);
278 spin_lock(&fc
->lock
);
282 /* Only fatal signals may interrupt this */
284 wait_answer_interruptible(fc
, req
);
285 restore_sigs(&oldset
);
290 if (req
->state
== FUSE_REQ_FINISHED
)
293 req
->out
.h
.error
= -EINTR
;
298 /* This is uninterruptible sleep, because data is
299 being copied to/from the buffers of req. During
300 locked state, there mustn't be any filesystem
301 operation (e.g. page fault), since that could lead
303 spin_unlock(&fc
->lock
);
304 wait_event(req
->waitq
, !req
->locked
);
305 spin_lock(&fc
->lock
);
307 if (req
->state
== FUSE_REQ_PENDING
) {
308 list_del(&req
->list
);
309 __fuse_put_request(req
);
310 } else if (req
->state
== FUSE_REQ_SENT
) {
311 spin_unlock(&fc
->lock
);
312 wait_event(req
->waitq
, req
->state
== FUSE_REQ_FINISHED
);
313 spin_lock(&fc
->lock
);
317 static unsigned len_args(unsigned numargs
, struct fuse_arg
*args
)
322 for (i
= 0; i
< numargs
; i
++)
323 nbytes
+= args
[i
].size
;
328 static u64
fuse_get_unique(struct fuse_conn
*fc
)
331 /* zero is special */
338 static void queue_request(struct fuse_conn
*fc
, struct fuse_req
*req
)
340 req
->in
.h
.unique
= fuse_get_unique(fc
);
341 req
->in
.h
.len
= sizeof(struct fuse_in_header
) +
342 len_args(req
->in
.numargs
, (struct fuse_arg
*) req
->in
.args
);
343 list_add_tail(&req
->list
, &fc
->pending
);
344 req
->state
= FUSE_REQ_PENDING
;
347 atomic_inc(&fc
->num_waiting
);
350 kill_fasync(&fc
->fasync
, SIGIO
, POLL_IN
);
353 void request_send(struct fuse_conn
*fc
, struct fuse_req
*req
)
356 spin_lock(&fc
->lock
);
358 req
->out
.h
.error
= -ENOTCONN
;
359 else if (fc
->conn_error
)
360 req
->out
.h
.error
= -ECONNREFUSED
;
362 queue_request(fc
, req
);
363 /* acquire extra reference, since request is still needed
364 after request_end() */
365 __fuse_get_request(req
);
367 request_wait_answer(fc
, req
);
369 spin_unlock(&fc
->lock
);
372 static void request_send_nowait(struct fuse_conn
*fc
, struct fuse_req
*req
)
374 spin_lock(&fc
->lock
);
377 fc
->num_background
++;
378 if (fc
->num_background
== FUSE_MAX_BACKGROUND
)
381 queue_request(fc
, req
);
382 spin_unlock(&fc
->lock
);
384 req
->out
.h
.error
= -ENOTCONN
;
385 request_end(fc
, req
);
389 void request_send_noreply(struct fuse_conn
*fc
, struct fuse_req
*req
)
392 request_send_nowait(fc
, req
);
395 void request_send_background(struct fuse_conn
*fc
, struct fuse_req
*req
)
398 request_send_nowait(fc
, req
);
402 * Lock the request. Up to the next unlock_request() there mustn't be
403 * anything that could cause a page-fault. If the request was already
406 static int lock_request(struct fuse_conn
*fc
, struct fuse_req
*req
)
410 spin_lock(&fc
->lock
);
415 spin_unlock(&fc
->lock
);
421 * Unlock request. If it was aborted during being locked, the
422 * requester thread is currently waiting for it to be unlocked, so
425 static void unlock_request(struct fuse_conn
*fc
, struct fuse_req
*req
)
428 spin_lock(&fc
->lock
);
431 wake_up(&req
->waitq
);
432 spin_unlock(&fc
->lock
);
436 struct fuse_copy_state
{
437 struct fuse_conn
*fc
;
439 struct fuse_req
*req
;
440 const struct iovec
*iov
;
441 unsigned long nr_segs
;
442 unsigned long seglen
;
450 static void fuse_copy_init(struct fuse_copy_state
*cs
, struct fuse_conn
*fc
,
451 int write
, struct fuse_req
*req
,
452 const struct iovec
*iov
, unsigned long nr_segs
)
454 memset(cs
, 0, sizeof(*cs
));
459 cs
->nr_segs
= nr_segs
;
462 /* Unmap and put previous page of userspace buffer */
463 static void fuse_copy_finish(struct fuse_copy_state
*cs
)
466 kunmap_atomic(cs
->mapaddr
, KM_USER0
);
468 flush_dcache_page(cs
->pg
);
469 set_page_dirty_lock(cs
->pg
);
477 * Get another pagefull of userspace buffer, and map it to kernel
478 * address space, and lock request
480 static int fuse_copy_fill(struct fuse_copy_state
*cs
)
482 unsigned long offset
;
485 unlock_request(cs
->fc
, cs
->req
);
486 fuse_copy_finish(cs
);
488 BUG_ON(!cs
->nr_segs
);
489 cs
->seglen
= cs
->iov
[0].iov_len
;
490 cs
->addr
= (unsigned long) cs
->iov
[0].iov_base
;
494 down_read(¤t
->mm
->mmap_sem
);
495 err
= get_user_pages(current
, current
->mm
, cs
->addr
, 1, cs
->write
, 0,
497 up_read(¤t
->mm
->mmap_sem
);
501 offset
= cs
->addr
% PAGE_SIZE
;
502 cs
->mapaddr
= kmap_atomic(cs
->pg
, KM_USER0
);
503 cs
->buf
= cs
->mapaddr
+ offset
;
504 cs
->len
= min(PAGE_SIZE
- offset
, cs
->seglen
);
505 cs
->seglen
-= cs
->len
;
508 return lock_request(cs
->fc
, cs
->req
);
511 /* Do as much copy to/from userspace buffer as we can */
512 static int fuse_copy_do(struct fuse_copy_state
*cs
, void **val
, unsigned *size
)
514 unsigned ncpy
= min(*size
, cs
->len
);
517 memcpy(cs
->buf
, *val
, ncpy
);
519 memcpy(*val
, cs
->buf
, ncpy
);
529 * Copy a page in the request to/from the userspace buffer. Must be
532 static int fuse_copy_page(struct fuse_copy_state
*cs
, struct page
*page
,
533 unsigned offset
, unsigned count
, int zeroing
)
535 if (page
&& zeroing
&& count
< PAGE_SIZE
) {
536 void *mapaddr
= kmap_atomic(page
, KM_USER1
);
537 memset(mapaddr
, 0, PAGE_SIZE
);
538 kunmap_atomic(mapaddr
, KM_USER1
);
542 if (!cs
->len
&& (err
= fuse_copy_fill(cs
)))
545 void *mapaddr
= kmap_atomic(page
, KM_USER1
);
546 void *buf
= mapaddr
+ offset
;
547 offset
+= fuse_copy_do(cs
, &buf
, &count
);
548 kunmap_atomic(mapaddr
, KM_USER1
);
550 offset
+= fuse_copy_do(cs
, NULL
, &count
);
552 if (page
&& !cs
->write
)
553 flush_dcache_page(page
);
557 /* Copy pages in the request to/from userspace buffer */
558 static int fuse_copy_pages(struct fuse_copy_state
*cs
, unsigned nbytes
,
562 struct fuse_req
*req
= cs
->req
;
563 unsigned offset
= req
->page_offset
;
564 unsigned count
= min(nbytes
, (unsigned) PAGE_SIZE
- offset
);
566 for (i
= 0; i
< req
->num_pages
&& (nbytes
|| zeroing
); i
++) {
567 struct page
*page
= req
->pages
[i
];
568 int err
= fuse_copy_page(cs
, page
, offset
, count
, zeroing
);
573 count
= min(nbytes
, (unsigned) PAGE_SIZE
);
579 /* Copy a single argument in the request to/from userspace buffer */
580 static int fuse_copy_one(struct fuse_copy_state
*cs
, void *val
, unsigned size
)
584 if (!cs
->len
&& (err
= fuse_copy_fill(cs
)))
586 fuse_copy_do(cs
, &val
, &size
);
591 /* Copy request arguments to/from userspace buffer */
592 static int fuse_copy_args(struct fuse_copy_state
*cs
, unsigned numargs
,
593 unsigned argpages
, struct fuse_arg
*args
,
599 for (i
= 0; !err
&& i
< numargs
; i
++) {
600 struct fuse_arg
*arg
= &args
[i
];
601 if (i
== numargs
- 1 && argpages
)
602 err
= fuse_copy_pages(cs
, arg
->size
, zeroing
);
604 err
= fuse_copy_one(cs
, arg
->value
, arg
->size
);
609 static int request_pending(struct fuse_conn
*fc
)
611 return !list_empty(&fc
->pending
) || !list_empty(&fc
->interrupts
);
614 /* Wait until a request is available on the pending list */
615 static void request_wait(struct fuse_conn
*fc
)
617 DECLARE_WAITQUEUE(wait
, current
);
619 add_wait_queue_exclusive(&fc
->waitq
, &wait
);
620 while (fc
->connected
&& !request_pending(fc
)) {
621 set_current_state(TASK_INTERRUPTIBLE
);
622 if (signal_pending(current
))
625 spin_unlock(&fc
->lock
);
627 spin_lock(&fc
->lock
);
629 set_current_state(TASK_RUNNING
);
630 remove_wait_queue(&fc
->waitq
, &wait
);
634 * Transfer an interrupt request to userspace
636 * Unlike other requests this is assembled on demand, without a need
637 * to allocate a separate fuse_req structure.
639 * Called with fc->lock held, releases it
641 static int fuse_read_interrupt(struct fuse_conn
*fc
, struct fuse_req
*req
,
642 const struct iovec
*iov
, unsigned long nr_segs
)
644 struct fuse_copy_state cs
;
645 struct fuse_in_header ih
;
646 struct fuse_interrupt_in arg
;
647 unsigned reqsize
= sizeof(ih
) + sizeof(arg
);
650 list_del_init(&req
->intr_entry
);
651 req
->intr_unique
= fuse_get_unique(fc
);
652 memset(&ih
, 0, sizeof(ih
));
653 memset(&arg
, 0, sizeof(arg
));
655 ih
.opcode
= FUSE_INTERRUPT
;
656 ih
.unique
= req
->intr_unique
;
657 arg
.unique
= req
->in
.h
.unique
;
659 spin_unlock(&fc
->lock
);
660 if (iov_length(iov
, nr_segs
) < reqsize
)
663 fuse_copy_init(&cs
, fc
, 1, NULL
, iov
, nr_segs
);
664 err
= fuse_copy_one(&cs
, &ih
, sizeof(ih
));
666 err
= fuse_copy_one(&cs
, &arg
, sizeof(arg
));
667 fuse_copy_finish(&cs
);
669 return err
? err
: reqsize
;
673 * Read a single request into the userspace filesystem's buffer. This
674 * function waits until a request is available, then removes it from
675 * the pending list and copies request data to userspace buffer. If
676 * no reply is needed (FORGET) or request has been aborted or there
677 * was an error during the copying then it's finished by calling
678 * request_end(). Otherwise add it to the processing list, and set
681 static ssize_t
fuse_dev_readv(struct file
*file
, const struct iovec
*iov
,
682 unsigned long nr_segs
, loff_t
*off
)
685 struct fuse_req
*req
;
687 struct fuse_copy_state cs
;
689 struct fuse_conn
*fc
= fuse_get_conn(file
);
694 spin_lock(&fc
->lock
);
696 if ((file
->f_flags
& O_NONBLOCK
) && fc
->connected
&&
697 !request_pending(fc
))
705 if (!request_pending(fc
))
708 if (!list_empty(&fc
->interrupts
)) {
709 req
= list_entry(fc
->interrupts
.next
, struct fuse_req
,
711 return fuse_read_interrupt(fc
, req
, iov
, nr_segs
);
714 req
= list_entry(fc
->pending
.next
, struct fuse_req
, list
);
715 req
->state
= FUSE_REQ_READING
;
716 list_move(&req
->list
, &fc
->io
);
720 /* If request is too large, reply with an error and restart the read */
721 if (iov_length(iov
, nr_segs
) < reqsize
) {
722 req
->out
.h
.error
= -EIO
;
723 /* SETXATTR is special, since it may contain too large data */
724 if (in
->h
.opcode
== FUSE_SETXATTR
)
725 req
->out
.h
.error
= -E2BIG
;
726 request_end(fc
, req
);
729 spin_unlock(&fc
->lock
);
730 fuse_copy_init(&cs
, fc
, 1, req
, iov
, nr_segs
);
731 err
= fuse_copy_one(&cs
, &in
->h
, sizeof(in
->h
));
733 err
= fuse_copy_args(&cs
, in
->numargs
, in
->argpages
,
734 (struct fuse_arg
*) in
->args
, 0);
735 fuse_copy_finish(&cs
);
736 spin_lock(&fc
->lock
);
738 if (!err
&& req
->aborted
)
742 req
->out
.h
.error
= -EIO
;
743 request_end(fc
, req
);
747 request_end(fc
, req
);
749 req
->state
= FUSE_REQ_SENT
;
750 list_move_tail(&req
->list
, &fc
->processing
);
751 if (req
->interrupted
)
752 queue_interrupt(fc
, req
);
753 spin_unlock(&fc
->lock
);
758 spin_unlock(&fc
->lock
);
762 static ssize_t
fuse_dev_read(struct file
*file
, char __user
*buf
,
763 size_t nbytes
, loff_t
*off
)
766 iov
.iov_len
= nbytes
;
768 return fuse_dev_readv(file
, &iov
, 1, off
);
771 /* Look up request on processing list by unique ID */
772 static struct fuse_req
*request_find(struct fuse_conn
*fc
, u64 unique
)
774 struct list_head
*entry
;
776 list_for_each(entry
, &fc
->processing
) {
777 struct fuse_req
*req
;
778 req
= list_entry(entry
, struct fuse_req
, list
);
779 if (req
->in
.h
.unique
== unique
|| req
->intr_unique
== unique
)
785 static int copy_out_args(struct fuse_copy_state
*cs
, struct fuse_out
*out
,
788 unsigned reqsize
= sizeof(struct fuse_out_header
);
791 return nbytes
!= reqsize
? -EINVAL
: 0;
793 reqsize
+= len_args(out
->numargs
, out
->args
);
795 if (reqsize
< nbytes
|| (reqsize
> nbytes
&& !out
->argvar
))
797 else if (reqsize
> nbytes
) {
798 struct fuse_arg
*lastarg
= &out
->args
[out
->numargs
-1];
799 unsigned diffsize
= reqsize
- nbytes
;
800 if (diffsize
> lastarg
->size
)
802 lastarg
->size
-= diffsize
;
804 return fuse_copy_args(cs
, out
->numargs
, out
->argpages
, out
->args
,
809 * Write a single reply to a request. First the header is copied from
810 * the write buffer. The request is then searched on the processing
811 * list by the unique ID found in the header. If found, then remove
812 * it from the list and copy the rest of the buffer to the request.
813 * The request is finished by calling request_end()
815 static ssize_t
fuse_dev_writev(struct file
*file
, const struct iovec
*iov
,
816 unsigned long nr_segs
, loff_t
*off
)
819 unsigned nbytes
= iov_length(iov
, nr_segs
);
820 struct fuse_req
*req
;
821 struct fuse_out_header oh
;
822 struct fuse_copy_state cs
;
823 struct fuse_conn
*fc
= fuse_get_conn(file
);
827 fuse_copy_init(&cs
, fc
, 0, NULL
, iov
, nr_segs
);
828 if (nbytes
< sizeof(struct fuse_out_header
))
831 err
= fuse_copy_one(&cs
, &oh
, sizeof(oh
));
835 if (!oh
.unique
|| oh
.error
<= -1000 || oh
.error
> 0 ||
839 spin_lock(&fc
->lock
);
844 req
= request_find(fc
, oh
.unique
);
849 spin_unlock(&fc
->lock
);
850 fuse_copy_finish(&cs
);
851 spin_lock(&fc
->lock
);
852 request_end(fc
, req
);
855 /* Is it an interrupt reply? */
856 if (req
->intr_unique
== oh
.unique
) {
858 if (nbytes
!= sizeof(struct fuse_out_header
))
861 if (oh
.error
== -ENOSYS
)
862 fc
->no_interrupt
= 1;
863 else if (oh
.error
== -EAGAIN
)
864 queue_interrupt(fc
, req
);
866 spin_unlock(&fc
->lock
);
867 fuse_copy_finish(&cs
);
871 req
->state
= FUSE_REQ_WRITING
;
872 list_move(&req
->list
, &fc
->io
);
876 spin_unlock(&fc
->lock
);
878 err
= copy_out_args(&cs
, &req
->out
, nbytes
);
879 fuse_copy_finish(&cs
);
881 spin_lock(&fc
->lock
);
886 } else if (!req
->aborted
)
887 req
->out
.h
.error
= -EIO
;
888 request_end(fc
, req
);
890 return err
? err
: nbytes
;
893 spin_unlock(&fc
->lock
);
895 fuse_copy_finish(&cs
);
899 static ssize_t
fuse_dev_write(struct file
*file
, const char __user
*buf
,
900 size_t nbytes
, loff_t
*off
)
903 iov
.iov_len
= nbytes
;
904 iov
.iov_base
= (char __user
*) buf
;
905 return fuse_dev_writev(file
, &iov
, 1, off
);
908 static unsigned fuse_dev_poll(struct file
*file
, poll_table
*wait
)
910 unsigned mask
= POLLOUT
| POLLWRNORM
;
911 struct fuse_conn
*fc
= fuse_get_conn(file
);
915 poll_wait(file
, &fc
->waitq
, wait
);
917 spin_lock(&fc
->lock
);
920 else if (request_pending(fc
))
921 mask
|= POLLIN
| POLLRDNORM
;
922 spin_unlock(&fc
->lock
);
928 * Abort all requests on the given list (pending or processing)
930 * This function releases and reacquires fc->lock
932 static void end_requests(struct fuse_conn
*fc
, struct list_head
*head
)
934 while (!list_empty(head
)) {
935 struct fuse_req
*req
;
936 req
= list_entry(head
->next
, struct fuse_req
, list
);
937 req
->out
.h
.error
= -ECONNABORTED
;
938 request_end(fc
, req
);
939 spin_lock(&fc
->lock
);
944 * Abort requests under I/O
946 * The requests are set to aborted and finished, and the request
947 * waiter is woken up. This will make request_wait_answer() wait
948 * until the request is unlocked and then return.
950 * If the request is asynchronous, then the end function needs to be
951 * called after waiting for the request to be unlocked (if it was
954 static void end_io_requests(struct fuse_conn
*fc
)
956 while (!list_empty(&fc
->io
)) {
957 struct fuse_req
*req
=
958 list_entry(fc
->io
.next
, struct fuse_req
, list
);
959 void (*end
) (struct fuse_conn
*, struct fuse_req
*) = req
->end
;
962 req
->out
.h
.error
= -ECONNABORTED
;
963 req
->state
= FUSE_REQ_FINISHED
;
964 list_del_init(&req
->list
);
965 wake_up(&req
->waitq
);
968 /* The end function will consume this reference */
969 __fuse_get_request(req
);
970 spin_unlock(&fc
->lock
);
971 wait_event(req
->waitq
, !req
->locked
);
973 spin_lock(&fc
->lock
);
979 * Abort all requests.
981 * Emergency exit in case of a malicious or accidental deadlock, or
982 * just a hung filesystem.
984 * The same effect is usually achievable through killing the
985 * filesystem daemon and all users of the filesystem. The exception
986 * is the combination of an asynchronous request and the tricky
987 * deadlock (see Documentation/filesystems/fuse.txt).
989 * During the aborting, progression of requests from the pending and
990 * processing lists onto the io list, and progression of new requests
991 * onto the pending list is prevented by req->connected being false.
993 * Progression of requests under I/O to the processing list is
994 * prevented by the req->aborted flag being true for these requests.
995 * For this reason requests on the io list must be aborted first.
997 void fuse_abort_conn(struct fuse_conn
*fc
)
999 spin_lock(&fc
->lock
);
1000 if (fc
->connected
) {
1003 end_io_requests(fc
);
1004 end_requests(fc
, &fc
->pending
);
1005 end_requests(fc
, &fc
->processing
);
1006 wake_up_all(&fc
->waitq
);
1007 wake_up_all(&fc
->blocked_waitq
);
1008 kill_fasync(&fc
->fasync
, SIGIO
, POLL_IN
);
1010 spin_unlock(&fc
->lock
);
1013 static int fuse_dev_release(struct inode
*inode
, struct file
*file
)
1015 struct fuse_conn
*fc
= fuse_get_conn(file
);
1017 spin_lock(&fc
->lock
);
1019 end_requests(fc
, &fc
->pending
);
1020 end_requests(fc
, &fc
->processing
);
1021 spin_unlock(&fc
->lock
);
1022 fasync_helper(-1, file
, 0, &fc
->fasync
);
1029 static int fuse_dev_fasync(int fd
, struct file
*file
, int on
)
1031 struct fuse_conn
*fc
= fuse_get_conn(file
);
1035 /* No locking - fasync_helper does its own locking */
1036 return fasync_helper(fd
, file
, on
, &fc
->fasync
);
1039 const struct file_operations fuse_dev_operations
= {
1040 .owner
= THIS_MODULE
,
1041 .llseek
= no_llseek
,
1042 .read
= fuse_dev_read
,
1043 .readv
= fuse_dev_readv
,
1044 .write
= fuse_dev_write
,
1045 .writev
= fuse_dev_writev
,
1046 .poll
= fuse_dev_poll
,
1047 .release
= fuse_dev_release
,
1048 .fasync
= fuse_dev_fasync
,
1051 static struct miscdevice fuse_miscdevice
= {
1052 .minor
= FUSE_MINOR
,
1054 .fops
= &fuse_dev_operations
,
1057 int __init
fuse_dev_init(void)
1060 fuse_req_cachep
= kmem_cache_create("fuse_request",
1061 sizeof(struct fuse_req
),
1063 if (!fuse_req_cachep
)
1066 err
= misc_register(&fuse_miscdevice
);
1068 goto out_cache_clean
;
1073 kmem_cache_destroy(fuse_req_cachep
);
1078 void fuse_dev_cleanup(void)
1080 misc_deregister(&fuse_miscdevice
);
1081 kmem_cache_destroy(fuse_req_cachep
);