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_conn
*fuse_get_conn(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 file
->private_data
;
37 static void fuse_request_init(struct fuse_req
*req
)
39 memset(req
, 0, sizeof(*req
));
40 INIT_LIST_HEAD(&req
->list
);
41 INIT_LIST_HEAD(&req
->intr_entry
);
42 init_waitqueue_head(&req
->waitq
);
43 atomic_set(&req
->count
, 1);
46 struct fuse_req
*fuse_request_alloc(void)
48 struct fuse_req
*req
= kmem_cache_alloc(fuse_req_cachep
, GFP_KERNEL
);
50 fuse_request_init(req
);
53 EXPORT_SYMBOL_GPL(fuse_request_alloc
);
55 struct fuse_req
*fuse_request_alloc_nofs(void)
57 struct fuse_req
*req
= kmem_cache_alloc(fuse_req_cachep
, GFP_NOFS
);
59 fuse_request_init(req
);
63 void fuse_request_free(struct fuse_req
*req
)
65 kmem_cache_free(fuse_req_cachep
, req
);
68 static void block_sigs(sigset_t
*oldset
)
72 siginitsetinv(&mask
, sigmask(SIGKILL
));
73 sigprocmask(SIG_BLOCK
, &mask
, oldset
);
76 static void restore_sigs(sigset_t
*oldset
)
78 sigprocmask(SIG_SETMASK
, oldset
, NULL
);
81 static void __fuse_get_request(struct fuse_req
*req
)
83 atomic_inc(&req
->count
);
86 /* Must be called with > 1 refcount */
87 static void __fuse_put_request(struct fuse_req
*req
)
89 BUG_ON(atomic_read(&req
->count
) < 2);
90 atomic_dec(&req
->count
);
93 static void fuse_req_init_context(struct fuse_req
*req
)
95 req
->in
.h
.uid
= from_kuid_munged(&init_user_ns
, current_fsuid());
96 req
->in
.h
.gid
= from_kgid_munged(&init_user_ns
, current_fsgid());
97 req
->in
.h
.pid
= current
->pid
;
100 struct fuse_req
*fuse_get_req(struct fuse_conn
*fc
)
102 struct fuse_req
*req
;
107 atomic_inc(&fc
->num_waiting
);
109 intr
= wait_event_interruptible(fc
->blocked_waitq
, !fc
->blocked
);
110 restore_sigs(&oldset
);
119 req
= fuse_request_alloc();
124 fuse_req_init_context(req
);
129 atomic_dec(&fc
->num_waiting
);
132 EXPORT_SYMBOL_GPL(fuse_get_req
);
135 * Return request in fuse_file->reserved_req. However that may
136 * currently be in use. If that is the case, wait for it to become
139 static struct fuse_req
*get_reserved_req(struct fuse_conn
*fc
,
142 struct fuse_req
*req
= NULL
;
143 struct fuse_file
*ff
= file
->private_data
;
146 wait_event(fc
->reserved_req_waitq
, ff
->reserved_req
);
147 spin_lock(&fc
->lock
);
148 if (ff
->reserved_req
) {
149 req
= ff
->reserved_req
;
150 ff
->reserved_req
= NULL
;
151 req
->stolen_file
= get_file(file
);
153 spin_unlock(&fc
->lock
);
160 * Put stolen request back into fuse_file->reserved_req
162 static void put_reserved_req(struct fuse_conn
*fc
, struct fuse_req
*req
)
164 struct file
*file
= req
->stolen_file
;
165 struct fuse_file
*ff
= file
->private_data
;
167 spin_lock(&fc
->lock
);
168 fuse_request_init(req
);
169 BUG_ON(ff
->reserved_req
);
170 ff
->reserved_req
= req
;
171 wake_up_all(&fc
->reserved_req_waitq
);
172 spin_unlock(&fc
->lock
);
177 * Gets a requests for a file operation, always succeeds
179 * This is used for sending the FLUSH request, which must get to
180 * userspace, due to POSIX locks which may need to be unlocked.
182 * If allocation fails due to OOM, use the reserved request in
185 * This is very unlikely to deadlock accidentally, since the
186 * filesystem should not have it's own file open. If deadlock is
187 * intentional, it can still be broken by "aborting" the filesystem.
189 struct fuse_req
*fuse_get_req_nofail(struct fuse_conn
*fc
, struct file
*file
)
191 struct fuse_req
*req
;
193 atomic_inc(&fc
->num_waiting
);
194 wait_event(fc
->blocked_waitq
, !fc
->blocked
);
195 req
= fuse_request_alloc();
197 req
= get_reserved_req(fc
, file
);
199 fuse_req_init_context(req
);
204 void fuse_put_request(struct fuse_conn
*fc
, struct fuse_req
*req
)
206 if (atomic_dec_and_test(&req
->count
)) {
208 atomic_dec(&fc
->num_waiting
);
210 if (req
->stolen_file
)
211 put_reserved_req(fc
, req
);
213 fuse_request_free(req
);
216 EXPORT_SYMBOL_GPL(fuse_put_request
);
218 static unsigned len_args(unsigned numargs
, struct fuse_arg
*args
)
223 for (i
= 0; i
< numargs
; i
++)
224 nbytes
+= args
[i
].size
;
229 static u64
fuse_get_unique(struct fuse_conn
*fc
)
232 /* zero is special */
239 static void queue_request(struct fuse_conn
*fc
, struct fuse_req
*req
)
241 req
->in
.h
.len
= sizeof(struct fuse_in_header
) +
242 len_args(req
->in
.numargs
, (struct fuse_arg
*) req
->in
.args
);
243 list_add_tail(&req
->list
, &fc
->pending
);
244 req
->state
= FUSE_REQ_PENDING
;
247 atomic_inc(&fc
->num_waiting
);
250 kill_fasync(&fc
->fasync
, SIGIO
, POLL_IN
);
253 void fuse_queue_forget(struct fuse_conn
*fc
, struct fuse_forget_link
*forget
,
254 u64 nodeid
, u64 nlookup
)
256 forget
->forget_one
.nodeid
= nodeid
;
257 forget
->forget_one
.nlookup
= nlookup
;
259 spin_lock(&fc
->lock
);
261 fc
->forget_list_tail
->next
= forget
;
262 fc
->forget_list_tail
= forget
;
264 kill_fasync(&fc
->fasync
, SIGIO
, POLL_IN
);
268 spin_unlock(&fc
->lock
);
271 static void flush_bg_queue(struct fuse_conn
*fc
)
273 while (fc
->active_background
< fc
->max_background
&&
274 !list_empty(&fc
->bg_queue
)) {
275 struct fuse_req
*req
;
277 req
= list_entry(fc
->bg_queue
.next
, struct fuse_req
, list
);
278 list_del(&req
->list
);
279 fc
->active_background
++;
280 req
->in
.h
.unique
= fuse_get_unique(fc
);
281 queue_request(fc
, req
);
286 * This function is called when a request is finished. Either a reply
287 * has arrived or it was aborted (and not yet sent) or some error
288 * occurred during communication with userspace, or the device file
289 * was closed. The requester thread is woken up (if still waiting),
290 * the 'end' callback is called if given, else the reference to the
291 * request is released
293 * Called with fc->lock, unlocks it
295 static void request_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
298 void (*end
) (struct fuse_conn
*, struct fuse_req
*) = req
->end
;
300 list_del(&req
->list
);
301 list_del(&req
->intr_entry
);
302 req
->state
= FUSE_REQ_FINISHED
;
303 if (req
->background
) {
304 if (fc
->num_background
== fc
->max_background
) {
306 wake_up_all(&fc
->blocked_waitq
);
308 if (fc
->num_background
== fc
->congestion_threshold
&&
309 fc
->connected
&& fc
->bdi_initialized
) {
310 clear_bdi_congested(&fc
->bdi
, BLK_RW_SYNC
);
311 clear_bdi_congested(&fc
->bdi
, BLK_RW_ASYNC
);
313 fc
->num_background
--;
314 fc
->active_background
--;
317 spin_unlock(&fc
->lock
);
318 wake_up(&req
->waitq
);
321 fuse_put_request(fc
, req
);
324 static void wait_answer_interruptible(struct fuse_conn
*fc
,
325 struct fuse_req
*req
)
329 if (signal_pending(current
))
332 spin_unlock(&fc
->lock
);
333 wait_event_interruptible(req
->waitq
, req
->state
== FUSE_REQ_FINISHED
);
334 spin_lock(&fc
->lock
);
337 static void queue_interrupt(struct fuse_conn
*fc
, struct fuse_req
*req
)
339 list_add_tail(&req
->intr_entry
, &fc
->interrupts
);
341 kill_fasync(&fc
->fasync
, SIGIO
, POLL_IN
);
344 static void request_wait_answer(struct fuse_conn
*fc
, struct fuse_req
*req
)
348 if (!fc
->no_interrupt
) {
349 /* Any signal may interrupt this */
350 wait_answer_interruptible(fc
, req
);
354 if (req
->state
== FUSE_REQ_FINISHED
)
357 req
->interrupted
= 1;
358 if (req
->state
== FUSE_REQ_SENT
)
359 queue_interrupt(fc
, req
);
365 /* Only fatal signals may interrupt this */
367 wait_answer_interruptible(fc
, req
);
368 restore_sigs(&oldset
);
372 if (req
->state
== FUSE_REQ_FINISHED
)
375 /* Request is not yet in userspace, bail out */
376 if (req
->state
== FUSE_REQ_PENDING
) {
377 list_del(&req
->list
);
378 __fuse_put_request(req
);
379 req
->out
.h
.error
= -EINTR
;
385 * Either request is already in userspace, or it was forced.
388 spin_unlock(&fc
->lock
);
389 wait_event(req
->waitq
, req
->state
== FUSE_REQ_FINISHED
);
390 spin_lock(&fc
->lock
);
396 BUG_ON(req
->state
!= FUSE_REQ_FINISHED
);
398 /* This is uninterruptible sleep, because data is
399 being copied to/from the buffers of req. During
400 locked state, there mustn't be any filesystem
401 operation (e.g. page fault), since that could lead
403 spin_unlock(&fc
->lock
);
404 wait_event(req
->waitq
, !req
->locked
);
405 spin_lock(&fc
->lock
);
409 void fuse_request_send(struct fuse_conn
*fc
, struct fuse_req
*req
)
412 spin_lock(&fc
->lock
);
414 req
->out
.h
.error
= -ENOTCONN
;
415 else if (fc
->conn_error
)
416 req
->out
.h
.error
= -ECONNREFUSED
;
418 req
->in
.h
.unique
= fuse_get_unique(fc
);
419 queue_request(fc
, req
);
420 /* acquire extra reference, since request is still needed
421 after request_end() */
422 __fuse_get_request(req
);
424 request_wait_answer(fc
, req
);
426 spin_unlock(&fc
->lock
);
428 EXPORT_SYMBOL_GPL(fuse_request_send
);
430 static void fuse_request_send_nowait_locked(struct fuse_conn
*fc
,
431 struct fuse_req
*req
)
434 fc
->num_background
++;
435 if (fc
->num_background
== fc
->max_background
)
437 if (fc
->num_background
== fc
->congestion_threshold
&&
438 fc
->bdi_initialized
) {
439 set_bdi_congested(&fc
->bdi
, BLK_RW_SYNC
);
440 set_bdi_congested(&fc
->bdi
, BLK_RW_ASYNC
);
442 list_add_tail(&req
->list
, &fc
->bg_queue
);
446 static void fuse_request_send_nowait(struct fuse_conn
*fc
, struct fuse_req
*req
)
448 spin_lock(&fc
->lock
);
450 fuse_request_send_nowait_locked(fc
, req
);
451 spin_unlock(&fc
->lock
);
453 req
->out
.h
.error
= -ENOTCONN
;
454 request_end(fc
, req
);
458 void fuse_request_send_background(struct fuse_conn
*fc
, struct fuse_req
*req
)
461 fuse_request_send_nowait(fc
, req
);
463 EXPORT_SYMBOL_GPL(fuse_request_send_background
);
465 static int fuse_request_send_notify_reply(struct fuse_conn
*fc
,
466 struct fuse_req
*req
, u64 unique
)
471 req
->in
.h
.unique
= unique
;
472 spin_lock(&fc
->lock
);
474 queue_request(fc
, req
);
477 spin_unlock(&fc
->lock
);
483 * Called under fc->lock
485 * fc->connected must have been checked previously
487 void fuse_request_send_background_locked(struct fuse_conn
*fc
,
488 struct fuse_req
*req
)
491 fuse_request_send_nowait_locked(fc
, req
);
494 void fuse_force_forget(struct file
*file
, u64 nodeid
)
496 struct inode
*inode
= file
->f_path
.dentry
->d_inode
;
497 struct fuse_conn
*fc
= get_fuse_conn(inode
);
498 struct fuse_req
*req
;
499 struct fuse_forget_in inarg
;
501 memset(&inarg
, 0, sizeof(inarg
));
503 req
= fuse_get_req_nofail(fc
, file
);
504 req
->in
.h
.opcode
= FUSE_FORGET
;
505 req
->in
.h
.nodeid
= nodeid
;
507 req
->in
.args
[0].size
= sizeof(inarg
);
508 req
->in
.args
[0].value
= &inarg
;
510 fuse_request_send_nowait(fc
, req
);
514 * Lock the request. Up to the next unlock_request() there mustn't be
515 * anything that could cause a page-fault. If the request was already
518 static int lock_request(struct fuse_conn
*fc
, struct fuse_req
*req
)
522 spin_lock(&fc
->lock
);
527 spin_unlock(&fc
->lock
);
533 * Unlock request. If it was aborted during being locked, the
534 * requester thread is currently waiting for it to be unlocked, so
537 static void unlock_request(struct fuse_conn
*fc
, struct fuse_req
*req
)
540 spin_lock(&fc
->lock
);
543 wake_up(&req
->waitq
);
544 spin_unlock(&fc
->lock
);
548 struct fuse_copy_state
{
549 struct fuse_conn
*fc
;
551 struct fuse_req
*req
;
552 const struct iovec
*iov
;
553 struct pipe_buffer
*pipebufs
;
554 struct pipe_buffer
*currbuf
;
555 struct pipe_inode_info
*pipe
;
556 unsigned long nr_segs
;
557 unsigned long seglen
;
563 unsigned move_pages
:1;
566 static void fuse_copy_init(struct fuse_copy_state
*cs
, struct fuse_conn
*fc
,
568 const struct iovec
*iov
, unsigned long nr_segs
)
570 memset(cs
, 0, sizeof(*cs
));
574 cs
->nr_segs
= nr_segs
;
577 /* Unmap and put previous page of userspace buffer */
578 static void fuse_copy_finish(struct fuse_copy_state
*cs
)
581 struct pipe_buffer
*buf
= cs
->currbuf
;
584 buf
->ops
->unmap(cs
->pipe
, buf
, cs
->mapaddr
);
587 buf
->len
= PAGE_SIZE
- cs
->len
;
591 } else if (cs
->mapaddr
) {
594 flush_dcache_page(cs
->pg
);
595 set_page_dirty_lock(cs
->pg
);
603 * Get another pagefull of userspace buffer, and map it to kernel
604 * address space, and lock request
606 static int fuse_copy_fill(struct fuse_copy_state
*cs
)
608 unsigned long offset
;
611 unlock_request(cs
->fc
, cs
->req
);
612 fuse_copy_finish(cs
);
614 struct pipe_buffer
*buf
= cs
->pipebufs
;
617 err
= buf
->ops
->confirm(cs
->pipe
, buf
);
621 BUG_ON(!cs
->nr_segs
);
623 cs
->mapaddr
= buf
->ops
->map(cs
->pipe
, buf
, 0);
625 cs
->buf
= cs
->mapaddr
+ buf
->offset
;
631 if (cs
->nr_segs
== cs
->pipe
->buffers
)
634 page
= alloc_page(GFP_HIGHUSER
);
643 cs
->mapaddr
= kmap(page
);
644 cs
->buf
= cs
->mapaddr
;
651 BUG_ON(!cs
->nr_segs
);
652 cs
->seglen
= cs
->iov
[0].iov_len
;
653 cs
->addr
= (unsigned long) cs
->iov
[0].iov_base
;
657 err
= get_user_pages_fast(cs
->addr
, 1, cs
->write
, &cs
->pg
);
661 offset
= cs
->addr
% PAGE_SIZE
;
662 cs
->mapaddr
= kmap(cs
->pg
);
663 cs
->buf
= cs
->mapaddr
+ offset
;
664 cs
->len
= min(PAGE_SIZE
- offset
, cs
->seglen
);
665 cs
->seglen
-= cs
->len
;
669 return lock_request(cs
->fc
, cs
->req
);
672 /* Do as much copy to/from userspace buffer as we can */
673 static int fuse_copy_do(struct fuse_copy_state
*cs
, void **val
, unsigned *size
)
675 unsigned ncpy
= min(*size
, cs
->len
);
678 memcpy(cs
->buf
, *val
, ncpy
);
680 memcpy(*val
, cs
->buf
, ncpy
);
689 static int fuse_check_page(struct page
*page
)
691 if (page_mapcount(page
) ||
692 page
->mapping
!= NULL
||
693 page_count(page
) != 1 ||
694 (page
->flags
& PAGE_FLAGS_CHECK_AT_PREP
&
701 printk(KERN_WARNING
"fuse: trying to steal weird page\n");
702 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
);
708 static int fuse_try_move_page(struct fuse_copy_state
*cs
, struct page
**pagep
)
711 struct page
*oldpage
= *pagep
;
712 struct page
*newpage
;
713 struct pipe_buffer
*buf
= cs
->pipebufs
;
715 unlock_request(cs
->fc
, cs
->req
);
716 fuse_copy_finish(cs
);
718 err
= buf
->ops
->confirm(cs
->pipe
, buf
);
722 BUG_ON(!cs
->nr_segs
);
728 if (cs
->len
!= PAGE_SIZE
)
731 if (buf
->ops
->steal(cs
->pipe
, buf
) != 0)
736 if (WARN_ON(!PageUptodate(newpage
)))
739 ClearPageMappedToDisk(newpage
);
741 if (fuse_check_page(newpage
) != 0)
742 goto out_fallback_unlock
;
745 * This is a new and locked page, it shouldn't be mapped or
746 * have any special flags on it
748 if (WARN_ON(page_mapped(oldpage
)))
749 goto out_fallback_unlock
;
750 if (WARN_ON(page_has_private(oldpage
)))
751 goto out_fallback_unlock
;
752 if (WARN_ON(PageDirty(oldpage
) || PageWriteback(oldpage
)))
753 goto out_fallback_unlock
;
754 if (WARN_ON(PageMlocked(oldpage
)))
755 goto out_fallback_unlock
;
757 err
= replace_page_cache_page(oldpage
, newpage
, GFP_KERNEL
);
759 unlock_page(newpage
);
763 page_cache_get(newpage
);
765 if (!(buf
->flags
& PIPE_BUF_FLAG_LRU
))
766 lru_cache_add_file(newpage
);
769 spin_lock(&cs
->fc
->lock
);
770 if (cs
->req
->aborted
)
774 spin_unlock(&cs
->fc
->lock
);
777 unlock_page(newpage
);
778 page_cache_release(newpage
);
782 unlock_page(oldpage
);
783 page_cache_release(oldpage
);
789 unlock_page(newpage
);
791 cs
->mapaddr
= buf
->ops
->map(cs
->pipe
, buf
, 1);
792 cs
->buf
= cs
->mapaddr
+ buf
->offset
;
794 err
= lock_request(cs
->fc
, cs
->req
);
801 static int fuse_ref_page(struct fuse_copy_state
*cs
, struct page
*page
,
802 unsigned offset
, unsigned count
)
804 struct pipe_buffer
*buf
;
806 if (cs
->nr_segs
== cs
->pipe
->buffers
)
809 unlock_request(cs
->fc
, cs
->req
);
810 fuse_copy_finish(cs
);
813 page_cache_get(page
);
815 buf
->offset
= offset
;
826 * Copy a page in the request to/from the userspace buffer. Must be
829 static int fuse_copy_page(struct fuse_copy_state
*cs
, struct page
**pagep
,
830 unsigned offset
, unsigned count
, int zeroing
)
833 struct page
*page
= *pagep
;
835 if (page
&& zeroing
&& count
< PAGE_SIZE
)
836 clear_highpage(page
);
839 if (cs
->write
&& cs
->pipebufs
&& page
) {
840 return fuse_ref_page(cs
, page
, offset
, count
);
841 } else if (!cs
->len
) {
842 if (cs
->move_pages
&& page
&&
843 offset
== 0 && count
== PAGE_SIZE
) {
844 err
= fuse_try_move_page(cs
, pagep
);
848 err
= fuse_copy_fill(cs
);
854 void *mapaddr
= kmap_atomic(page
);
855 void *buf
= mapaddr
+ offset
;
856 offset
+= fuse_copy_do(cs
, &buf
, &count
);
857 kunmap_atomic(mapaddr
);
859 offset
+= fuse_copy_do(cs
, NULL
, &count
);
861 if (page
&& !cs
->write
)
862 flush_dcache_page(page
);
866 /* Copy pages in the request to/from userspace buffer */
867 static int fuse_copy_pages(struct fuse_copy_state
*cs
, unsigned nbytes
,
871 struct fuse_req
*req
= cs
->req
;
872 unsigned offset
= req
->page_offset
;
873 unsigned count
= min(nbytes
, (unsigned) PAGE_SIZE
- offset
);
875 for (i
= 0; i
< req
->num_pages
&& (nbytes
|| zeroing
); i
++) {
878 err
= fuse_copy_page(cs
, &req
->pages
[i
], offset
, count
,
884 count
= min(nbytes
, (unsigned) PAGE_SIZE
);
890 /* Copy a single argument in the request to/from userspace buffer */
891 static int fuse_copy_one(struct fuse_copy_state
*cs
, void *val
, unsigned size
)
895 int err
= fuse_copy_fill(cs
);
899 fuse_copy_do(cs
, &val
, &size
);
904 /* Copy request arguments to/from userspace buffer */
905 static int fuse_copy_args(struct fuse_copy_state
*cs
, unsigned numargs
,
906 unsigned argpages
, struct fuse_arg
*args
,
912 for (i
= 0; !err
&& i
< numargs
; i
++) {
913 struct fuse_arg
*arg
= &args
[i
];
914 if (i
== numargs
- 1 && argpages
)
915 err
= fuse_copy_pages(cs
, arg
->size
, zeroing
);
917 err
= fuse_copy_one(cs
, arg
->value
, arg
->size
);
922 static int forget_pending(struct fuse_conn
*fc
)
924 return fc
->forget_list_head
.next
!= NULL
;
927 static int request_pending(struct fuse_conn
*fc
)
929 return !list_empty(&fc
->pending
) || !list_empty(&fc
->interrupts
) ||
933 /* Wait until a request is available on the pending list */
934 static void request_wait(struct fuse_conn
*fc
)
938 DECLARE_WAITQUEUE(wait
, current
);
940 add_wait_queue_exclusive(&fc
->waitq
, &wait
);
941 while (fc
->connected
&& !request_pending(fc
)) {
942 set_current_state(TASK_INTERRUPTIBLE
);
943 if (signal_pending(current
))
946 spin_unlock(&fc
->lock
);
948 spin_lock(&fc
->lock
);
950 set_current_state(TASK_RUNNING
);
951 remove_wait_queue(&fc
->waitq
, &wait
);
955 * Transfer an interrupt request to userspace
957 * Unlike other requests this is assembled on demand, without a need
958 * to allocate a separate fuse_req structure.
960 * Called with fc->lock held, releases it
962 static int fuse_read_interrupt(struct fuse_conn
*fc
, struct fuse_copy_state
*cs
,
963 size_t nbytes
, struct fuse_req
*req
)
966 struct fuse_in_header ih
;
967 struct fuse_interrupt_in arg
;
968 unsigned reqsize
= sizeof(ih
) + sizeof(arg
);
971 list_del_init(&req
->intr_entry
);
972 req
->intr_unique
= fuse_get_unique(fc
);
973 memset(&ih
, 0, sizeof(ih
));
974 memset(&arg
, 0, sizeof(arg
));
976 ih
.opcode
= FUSE_INTERRUPT
;
977 ih
.unique
= req
->intr_unique
;
978 arg
.unique
= req
->in
.h
.unique
;
980 spin_unlock(&fc
->lock
);
981 if (nbytes
< reqsize
)
984 err
= fuse_copy_one(cs
, &ih
, sizeof(ih
));
986 err
= fuse_copy_one(cs
, &arg
, sizeof(arg
));
987 fuse_copy_finish(cs
);
989 return err
? err
: reqsize
;
992 static struct fuse_forget_link
*dequeue_forget(struct fuse_conn
*fc
,
996 struct fuse_forget_link
*head
= fc
->forget_list_head
.next
;
997 struct fuse_forget_link
**newhead
= &head
;
1000 for (count
= 0; *newhead
!= NULL
&& count
< max
; count
++)
1001 newhead
= &(*newhead
)->next
;
1003 fc
->forget_list_head
.next
= *newhead
;
1005 if (fc
->forget_list_head
.next
== NULL
)
1006 fc
->forget_list_tail
= &fc
->forget_list_head
;
1014 static int fuse_read_single_forget(struct fuse_conn
*fc
,
1015 struct fuse_copy_state
*cs
,
1017 __releases(fc
->lock
)
1020 struct fuse_forget_link
*forget
= dequeue_forget(fc
, 1, NULL
);
1021 struct fuse_forget_in arg
= {
1022 .nlookup
= forget
->forget_one
.nlookup
,
1024 struct fuse_in_header ih
= {
1025 .opcode
= FUSE_FORGET
,
1026 .nodeid
= forget
->forget_one
.nodeid
,
1027 .unique
= fuse_get_unique(fc
),
1028 .len
= sizeof(ih
) + sizeof(arg
),
1031 spin_unlock(&fc
->lock
);
1033 if (nbytes
< ih
.len
)
1036 err
= fuse_copy_one(cs
, &ih
, sizeof(ih
));
1038 err
= fuse_copy_one(cs
, &arg
, sizeof(arg
));
1039 fuse_copy_finish(cs
);
1047 static int fuse_read_batch_forget(struct fuse_conn
*fc
,
1048 struct fuse_copy_state
*cs
, size_t nbytes
)
1049 __releases(fc
->lock
)
1052 unsigned max_forgets
;
1054 struct fuse_forget_link
*head
;
1055 struct fuse_batch_forget_in arg
= { .count
= 0 };
1056 struct fuse_in_header ih
= {
1057 .opcode
= FUSE_BATCH_FORGET
,
1058 .unique
= fuse_get_unique(fc
),
1059 .len
= sizeof(ih
) + sizeof(arg
),
1062 if (nbytes
< ih
.len
) {
1063 spin_unlock(&fc
->lock
);
1067 max_forgets
= (nbytes
- ih
.len
) / sizeof(struct fuse_forget_one
);
1068 head
= dequeue_forget(fc
, max_forgets
, &count
);
1069 spin_unlock(&fc
->lock
);
1072 ih
.len
+= count
* sizeof(struct fuse_forget_one
);
1073 err
= fuse_copy_one(cs
, &ih
, sizeof(ih
));
1075 err
= fuse_copy_one(cs
, &arg
, sizeof(arg
));
1078 struct fuse_forget_link
*forget
= head
;
1081 err
= fuse_copy_one(cs
, &forget
->forget_one
,
1082 sizeof(forget
->forget_one
));
1084 head
= forget
->next
;
1088 fuse_copy_finish(cs
);
1096 static int fuse_read_forget(struct fuse_conn
*fc
, struct fuse_copy_state
*cs
,
1098 __releases(fc
->lock
)
1100 if (fc
->minor
< 16 || fc
->forget_list_head
.next
->next
== NULL
)
1101 return fuse_read_single_forget(fc
, cs
, nbytes
);
1103 return fuse_read_batch_forget(fc
, cs
, nbytes
);
1107 * Read a single request into the userspace filesystem's buffer. This
1108 * function waits until a request is available, then removes it from
1109 * the pending list and copies request data to userspace buffer. If
1110 * no reply is needed (FORGET) or request has been aborted or there
1111 * was an error during the copying then it's finished by calling
1112 * request_end(). Otherwise add it to the processing list, and set
1115 static ssize_t
fuse_dev_do_read(struct fuse_conn
*fc
, struct file
*file
,
1116 struct fuse_copy_state
*cs
, size_t nbytes
)
1119 struct fuse_req
*req
;
1124 spin_lock(&fc
->lock
);
1126 if ((file
->f_flags
& O_NONBLOCK
) && fc
->connected
&&
1127 !request_pending(fc
))
1135 if (!request_pending(fc
))
1138 if (!list_empty(&fc
->interrupts
)) {
1139 req
= list_entry(fc
->interrupts
.next
, struct fuse_req
,
1141 return fuse_read_interrupt(fc
, cs
, nbytes
, req
);
1144 if (forget_pending(fc
)) {
1145 if (list_empty(&fc
->pending
) || fc
->forget_batch
-- > 0)
1146 return fuse_read_forget(fc
, cs
, nbytes
);
1148 if (fc
->forget_batch
<= -8)
1149 fc
->forget_batch
= 16;
1152 req
= list_entry(fc
->pending
.next
, struct fuse_req
, list
);
1153 req
->state
= FUSE_REQ_READING
;
1154 list_move(&req
->list
, &fc
->io
);
1157 reqsize
= in
->h
.len
;
1158 /* If request is too large, reply with an error and restart the read */
1159 if (nbytes
< reqsize
) {
1160 req
->out
.h
.error
= -EIO
;
1161 /* SETXATTR is special, since it may contain too large data */
1162 if (in
->h
.opcode
== FUSE_SETXATTR
)
1163 req
->out
.h
.error
= -E2BIG
;
1164 request_end(fc
, req
);
1167 spin_unlock(&fc
->lock
);
1169 err
= fuse_copy_one(cs
, &in
->h
, sizeof(in
->h
));
1171 err
= fuse_copy_args(cs
, in
->numargs
, in
->argpages
,
1172 (struct fuse_arg
*) in
->args
, 0);
1173 fuse_copy_finish(cs
);
1174 spin_lock(&fc
->lock
);
1177 request_end(fc
, req
);
1181 req
->out
.h
.error
= -EIO
;
1182 request_end(fc
, req
);
1186 request_end(fc
, req
);
1188 req
->state
= FUSE_REQ_SENT
;
1189 list_move_tail(&req
->list
, &fc
->processing
);
1190 if (req
->interrupted
)
1191 queue_interrupt(fc
, req
);
1192 spin_unlock(&fc
->lock
);
1197 spin_unlock(&fc
->lock
);
1201 static ssize_t
fuse_dev_read(struct kiocb
*iocb
, const struct iovec
*iov
,
1202 unsigned long nr_segs
, loff_t pos
)
1204 struct fuse_copy_state cs
;
1205 struct file
*file
= iocb
->ki_filp
;
1206 struct fuse_conn
*fc
= fuse_get_conn(file
);
1210 fuse_copy_init(&cs
, fc
, 1, iov
, nr_segs
);
1212 return fuse_dev_do_read(fc
, file
, &cs
, iov_length(iov
, nr_segs
));
1215 static int fuse_dev_pipe_buf_steal(struct pipe_inode_info
*pipe
,
1216 struct pipe_buffer
*buf
)
1221 static const struct pipe_buf_operations fuse_dev_pipe_buf_ops
= {
1223 .map
= generic_pipe_buf_map
,
1224 .unmap
= generic_pipe_buf_unmap
,
1225 .confirm
= generic_pipe_buf_confirm
,
1226 .release
= generic_pipe_buf_release
,
1227 .steal
= fuse_dev_pipe_buf_steal
,
1228 .get
= generic_pipe_buf_get
,
1231 static ssize_t
fuse_dev_splice_read(struct file
*in
, loff_t
*ppos
,
1232 struct pipe_inode_info
*pipe
,
1233 size_t len
, unsigned int flags
)
1238 struct pipe_buffer
*bufs
;
1239 struct fuse_copy_state cs
;
1240 struct fuse_conn
*fc
= fuse_get_conn(in
);
1244 bufs
= kmalloc(pipe
->buffers
* sizeof(struct pipe_buffer
), GFP_KERNEL
);
1248 fuse_copy_init(&cs
, fc
, 1, NULL
, 0);
1251 ret
= fuse_dev_do_read(fc
, in
, &cs
, len
);
1258 if (!pipe
->readers
) {
1259 send_sig(SIGPIPE
, current
, 0);
1265 if (pipe
->nrbufs
+ cs
.nr_segs
> pipe
->buffers
) {
1270 while (page_nr
< cs
.nr_segs
) {
1271 int newbuf
= (pipe
->curbuf
+ pipe
->nrbufs
) & (pipe
->buffers
- 1);
1272 struct pipe_buffer
*buf
= pipe
->bufs
+ newbuf
;
1274 buf
->page
= bufs
[page_nr
].page
;
1275 buf
->offset
= bufs
[page_nr
].offset
;
1276 buf
->len
= bufs
[page_nr
].len
;
1277 buf
->ops
= &fuse_dev_pipe_buf_ops
;
1292 if (waitqueue_active(&pipe
->wait
))
1293 wake_up_interruptible(&pipe
->wait
);
1294 kill_fasync(&pipe
->fasync_readers
, SIGIO
, POLL_IN
);
1298 for (; page_nr
< cs
.nr_segs
; page_nr
++)
1299 page_cache_release(bufs
[page_nr
].page
);
1305 static int fuse_notify_poll(struct fuse_conn
*fc
, unsigned int size
,
1306 struct fuse_copy_state
*cs
)
1308 struct fuse_notify_poll_wakeup_out outarg
;
1311 if (size
!= sizeof(outarg
))
1314 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1318 fuse_copy_finish(cs
);
1319 return fuse_notify_poll_wakeup(fc
, &outarg
);
1322 fuse_copy_finish(cs
);
1326 static int fuse_notify_inval_inode(struct fuse_conn
*fc
, unsigned int size
,
1327 struct fuse_copy_state
*cs
)
1329 struct fuse_notify_inval_inode_out outarg
;
1332 if (size
!= sizeof(outarg
))
1335 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1338 fuse_copy_finish(cs
);
1340 down_read(&fc
->killsb
);
1343 err
= fuse_reverse_inval_inode(fc
->sb
, outarg
.ino
,
1344 outarg
.off
, outarg
.len
);
1346 up_read(&fc
->killsb
);
1350 fuse_copy_finish(cs
);
1354 static int fuse_notify_inval_entry(struct fuse_conn
*fc
, unsigned int size
,
1355 struct fuse_copy_state
*cs
)
1357 struct fuse_notify_inval_entry_out outarg
;
1362 buf
= kzalloc(FUSE_NAME_MAX
+ 1, GFP_KERNEL
);
1367 if (size
< sizeof(outarg
))
1370 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1374 err
= -ENAMETOOLONG
;
1375 if (outarg
.namelen
> FUSE_NAME_MAX
)
1379 if (size
!= sizeof(outarg
) + outarg
.namelen
+ 1)
1383 name
.len
= outarg
.namelen
;
1384 err
= fuse_copy_one(cs
, buf
, outarg
.namelen
+ 1);
1387 fuse_copy_finish(cs
);
1388 buf
[outarg
.namelen
] = 0;
1389 name
.hash
= full_name_hash(name
.name
, name
.len
);
1391 down_read(&fc
->killsb
);
1394 err
= fuse_reverse_inval_entry(fc
->sb
, outarg
.parent
, 0, &name
);
1395 up_read(&fc
->killsb
);
1401 fuse_copy_finish(cs
);
1405 static int fuse_notify_delete(struct fuse_conn
*fc
, unsigned int size
,
1406 struct fuse_copy_state
*cs
)
1408 struct fuse_notify_delete_out outarg
;
1413 buf
= kzalloc(FUSE_NAME_MAX
+ 1, GFP_KERNEL
);
1418 if (size
< sizeof(outarg
))
1421 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1425 err
= -ENAMETOOLONG
;
1426 if (outarg
.namelen
> FUSE_NAME_MAX
)
1430 if (size
!= sizeof(outarg
) + outarg
.namelen
+ 1)
1434 name
.len
= outarg
.namelen
;
1435 err
= fuse_copy_one(cs
, buf
, outarg
.namelen
+ 1);
1438 fuse_copy_finish(cs
);
1439 buf
[outarg
.namelen
] = 0;
1440 name
.hash
= full_name_hash(name
.name
, name
.len
);
1442 down_read(&fc
->killsb
);
1445 err
= fuse_reverse_inval_entry(fc
->sb
, outarg
.parent
,
1446 outarg
.child
, &name
);
1447 up_read(&fc
->killsb
);
1453 fuse_copy_finish(cs
);
1457 static int fuse_notify_store(struct fuse_conn
*fc
, unsigned int size
,
1458 struct fuse_copy_state
*cs
)
1460 struct fuse_notify_store_out outarg
;
1461 struct inode
*inode
;
1462 struct address_space
*mapping
;
1466 unsigned int offset
;
1472 if (size
< sizeof(outarg
))
1475 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1480 if (size
- sizeof(outarg
) != outarg
.size
)
1483 nodeid
= outarg
.nodeid
;
1485 down_read(&fc
->killsb
);
1491 inode
= ilookup5(fc
->sb
, nodeid
, fuse_inode_eq
, &nodeid
);
1495 mapping
= inode
->i_mapping
;
1496 index
= outarg
.offset
>> PAGE_CACHE_SHIFT
;
1497 offset
= outarg
.offset
& ~PAGE_CACHE_MASK
;
1498 file_size
= i_size_read(inode
);
1499 end
= outarg
.offset
+ outarg
.size
;
1500 if (end
> file_size
) {
1502 fuse_write_update_size(inode
, file_size
);
1508 unsigned int this_num
;
1511 page
= find_or_create_page(mapping
, index
,
1512 mapping_gfp_mask(mapping
));
1516 this_num
= min_t(unsigned, num
, PAGE_CACHE_SIZE
- offset
);
1517 err
= fuse_copy_page(cs
, &page
, offset
, this_num
, 0);
1518 if (!err
&& offset
== 0 && (num
!= 0 || file_size
== end
))
1519 SetPageUptodate(page
);
1521 page_cache_release(page
);
1536 up_read(&fc
->killsb
);
1538 fuse_copy_finish(cs
);
1542 static void fuse_retrieve_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
1544 release_pages(req
->pages
, req
->num_pages
, 0);
1547 static int fuse_retrieve(struct fuse_conn
*fc
, struct inode
*inode
,
1548 struct fuse_notify_retrieve_out
*outarg
)
1551 struct address_space
*mapping
= inode
->i_mapping
;
1552 struct fuse_req
*req
;
1556 unsigned int offset
;
1557 size_t total_len
= 0;
1559 req
= fuse_get_req(fc
);
1561 return PTR_ERR(req
);
1563 offset
= outarg
->offset
& ~PAGE_CACHE_MASK
;
1565 req
->in
.h
.opcode
= FUSE_NOTIFY_REPLY
;
1566 req
->in
.h
.nodeid
= outarg
->nodeid
;
1567 req
->in
.numargs
= 2;
1568 req
->in
.argpages
= 1;
1569 req
->page_offset
= offset
;
1570 req
->end
= fuse_retrieve_end
;
1572 index
= outarg
->offset
>> PAGE_CACHE_SHIFT
;
1573 file_size
= i_size_read(inode
);
1575 if (outarg
->offset
> file_size
)
1577 else if (outarg
->offset
+ num
> file_size
)
1578 num
= file_size
- outarg
->offset
;
1580 while (num
&& req
->num_pages
< FUSE_MAX_PAGES_PER_REQ
) {
1582 unsigned int this_num
;
1584 page
= find_get_page(mapping
, index
);
1588 this_num
= min_t(unsigned, num
, PAGE_CACHE_SIZE
- offset
);
1589 req
->pages
[req
->num_pages
] = page
;
1594 total_len
+= this_num
;
1597 req
->misc
.retrieve_in
.offset
= outarg
->offset
;
1598 req
->misc
.retrieve_in
.size
= total_len
;
1599 req
->in
.args
[0].size
= sizeof(req
->misc
.retrieve_in
);
1600 req
->in
.args
[0].value
= &req
->misc
.retrieve_in
;
1601 req
->in
.args
[1].size
= total_len
;
1603 err
= fuse_request_send_notify_reply(fc
, req
, outarg
->notify_unique
);
1605 fuse_retrieve_end(fc
, req
);
1610 static int fuse_notify_retrieve(struct fuse_conn
*fc
, unsigned int size
,
1611 struct fuse_copy_state
*cs
)
1613 struct fuse_notify_retrieve_out outarg
;
1614 struct inode
*inode
;
1618 if (size
!= sizeof(outarg
))
1621 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1625 fuse_copy_finish(cs
);
1627 down_read(&fc
->killsb
);
1630 u64 nodeid
= outarg
.nodeid
;
1632 inode
= ilookup5(fc
->sb
, nodeid
, fuse_inode_eq
, &nodeid
);
1634 err
= fuse_retrieve(fc
, inode
, &outarg
);
1638 up_read(&fc
->killsb
);
1643 fuse_copy_finish(cs
);
1647 static int fuse_notify(struct fuse_conn
*fc
, enum fuse_notify_code code
,
1648 unsigned int size
, struct fuse_copy_state
*cs
)
1651 case FUSE_NOTIFY_POLL
:
1652 return fuse_notify_poll(fc
, size
, cs
);
1654 case FUSE_NOTIFY_INVAL_INODE
:
1655 return fuse_notify_inval_inode(fc
, size
, cs
);
1657 case FUSE_NOTIFY_INVAL_ENTRY
:
1658 return fuse_notify_inval_entry(fc
, size
, cs
);
1660 case FUSE_NOTIFY_STORE
:
1661 return fuse_notify_store(fc
, size
, cs
);
1663 case FUSE_NOTIFY_RETRIEVE
:
1664 return fuse_notify_retrieve(fc
, size
, cs
);
1666 case FUSE_NOTIFY_DELETE
:
1667 return fuse_notify_delete(fc
, size
, cs
);
1670 fuse_copy_finish(cs
);
1675 /* Look up request on processing list by unique ID */
1676 static struct fuse_req
*request_find(struct fuse_conn
*fc
, u64 unique
)
1678 struct list_head
*entry
;
1680 list_for_each(entry
, &fc
->processing
) {
1681 struct fuse_req
*req
;
1682 req
= list_entry(entry
, struct fuse_req
, list
);
1683 if (req
->in
.h
.unique
== unique
|| req
->intr_unique
== unique
)
1689 static int copy_out_args(struct fuse_copy_state
*cs
, struct fuse_out
*out
,
1692 unsigned reqsize
= sizeof(struct fuse_out_header
);
1695 return nbytes
!= reqsize
? -EINVAL
: 0;
1697 reqsize
+= len_args(out
->numargs
, out
->args
);
1699 if (reqsize
< nbytes
|| (reqsize
> nbytes
&& !out
->argvar
))
1701 else if (reqsize
> nbytes
) {
1702 struct fuse_arg
*lastarg
= &out
->args
[out
->numargs
-1];
1703 unsigned diffsize
= reqsize
- nbytes
;
1704 if (diffsize
> lastarg
->size
)
1706 lastarg
->size
-= diffsize
;
1708 return fuse_copy_args(cs
, out
->numargs
, out
->argpages
, out
->args
,
1713 * Write a single reply to a request. First the header is copied from
1714 * the write buffer. The request is then searched on the processing
1715 * list by the unique ID found in the header. If found, then remove
1716 * it from the list and copy the rest of the buffer to the request.
1717 * The request is finished by calling request_end()
1719 static ssize_t
fuse_dev_do_write(struct fuse_conn
*fc
,
1720 struct fuse_copy_state
*cs
, size_t nbytes
)
1723 struct fuse_req
*req
;
1724 struct fuse_out_header oh
;
1726 if (nbytes
< sizeof(struct fuse_out_header
))
1729 err
= fuse_copy_one(cs
, &oh
, sizeof(oh
));
1734 if (oh
.len
!= nbytes
)
1738 * Zero oh.unique indicates unsolicited notification message
1739 * and error contains notification code.
1742 err
= fuse_notify(fc
, oh
.error
, nbytes
- sizeof(oh
), cs
);
1743 return err
? err
: nbytes
;
1747 if (oh
.error
<= -1000 || oh
.error
> 0)
1750 spin_lock(&fc
->lock
);
1755 req
= request_find(fc
, oh
.unique
);
1760 spin_unlock(&fc
->lock
);
1761 fuse_copy_finish(cs
);
1762 spin_lock(&fc
->lock
);
1763 request_end(fc
, req
);
1766 /* Is it an interrupt reply? */
1767 if (req
->intr_unique
== oh
.unique
) {
1769 if (nbytes
!= sizeof(struct fuse_out_header
))
1772 if (oh
.error
== -ENOSYS
)
1773 fc
->no_interrupt
= 1;
1774 else if (oh
.error
== -EAGAIN
)
1775 queue_interrupt(fc
, req
);
1777 spin_unlock(&fc
->lock
);
1778 fuse_copy_finish(cs
);
1782 req
->state
= FUSE_REQ_WRITING
;
1783 list_move(&req
->list
, &fc
->io
);
1787 if (!req
->out
.page_replace
)
1789 spin_unlock(&fc
->lock
);
1791 err
= copy_out_args(cs
, &req
->out
, nbytes
);
1792 fuse_copy_finish(cs
);
1794 spin_lock(&fc
->lock
);
1799 } else if (!req
->aborted
)
1800 req
->out
.h
.error
= -EIO
;
1801 request_end(fc
, req
);
1803 return err
? err
: nbytes
;
1806 spin_unlock(&fc
->lock
);
1808 fuse_copy_finish(cs
);
1812 static ssize_t
fuse_dev_write(struct kiocb
*iocb
, const struct iovec
*iov
,
1813 unsigned long nr_segs
, loff_t pos
)
1815 struct fuse_copy_state cs
;
1816 struct fuse_conn
*fc
= fuse_get_conn(iocb
->ki_filp
);
1820 fuse_copy_init(&cs
, fc
, 0, iov
, nr_segs
);
1822 return fuse_dev_do_write(fc
, &cs
, iov_length(iov
, nr_segs
));
1825 static ssize_t
fuse_dev_splice_write(struct pipe_inode_info
*pipe
,
1826 struct file
*out
, loff_t
*ppos
,
1827 size_t len
, unsigned int flags
)
1831 struct pipe_buffer
*bufs
;
1832 struct fuse_copy_state cs
;
1833 struct fuse_conn
*fc
;
1837 fc
= fuse_get_conn(out
);
1841 bufs
= kmalloc(pipe
->buffers
* sizeof(struct pipe_buffer
), GFP_KERNEL
);
1848 for (idx
= 0; idx
< pipe
->nrbufs
&& rem
< len
; idx
++)
1849 rem
+= pipe
->bufs
[(pipe
->curbuf
+ idx
) & (pipe
->buffers
- 1)].len
;
1859 struct pipe_buffer
*ibuf
;
1860 struct pipe_buffer
*obuf
;
1862 BUG_ON(nbuf
>= pipe
->buffers
);
1863 BUG_ON(!pipe
->nrbufs
);
1864 ibuf
= &pipe
->bufs
[pipe
->curbuf
];
1867 if (rem
>= ibuf
->len
) {
1870 pipe
->curbuf
= (pipe
->curbuf
+ 1) & (pipe
->buffers
- 1);
1873 ibuf
->ops
->get(pipe
, ibuf
);
1875 obuf
->flags
&= ~PIPE_BUF_FLAG_GIFT
;
1877 ibuf
->offset
+= obuf
->len
;
1878 ibuf
->len
-= obuf
->len
;
1885 fuse_copy_init(&cs
, fc
, 0, NULL
, nbuf
);
1889 if (flags
& SPLICE_F_MOVE
)
1892 ret
= fuse_dev_do_write(fc
, &cs
, len
);
1894 for (idx
= 0; idx
< nbuf
; idx
++) {
1895 struct pipe_buffer
*buf
= &bufs
[idx
];
1896 buf
->ops
->release(pipe
, buf
);
1903 static unsigned fuse_dev_poll(struct file
*file
, poll_table
*wait
)
1905 unsigned mask
= POLLOUT
| POLLWRNORM
;
1906 struct fuse_conn
*fc
= fuse_get_conn(file
);
1910 poll_wait(file
, &fc
->waitq
, wait
);
1912 spin_lock(&fc
->lock
);
1915 else if (request_pending(fc
))
1916 mask
|= POLLIN
| POLLRDNORM
;
1917 spin_unlock(&fc
->lock
);
1923 * Abort all requests on the given list (pending or processing)
1925 * This function releases and reacquires fc->lock
1927 static void end_requests(struct fuse_conn
*fc
, struct list_head
*head
)
1928 __releases(fc
->lock
)
1929 __acquires(fc
->lock
)
1931 while (!list_empty(head
)) {
1932 struct fuse_req
*req
;
1933 req
= list_entry(head
->next
, struct fuse_req
, list
);
1934 req
->out
.h
.error
= -ECONNABORTED
;
1935 request_end(fc
, req
);
1936 spin_lock(&fc
->lock
);
1941 * Abort requests under I/O
1943 * The requests are set to aborted and finished, and the request
1944 * waiter is woken up. This will make request_wait_answer() wait
1945 * until the request is unlocked and then return.
1947 * If the request is asynchronous, then the end function needs to be
1948 * called after waiting for the request to be unlocked (if it was
1951 static void end_io_requests(struct fuse_conn
*fc
)
1952 __releases(fc
->lock
)
1953 __acquires(fc
->lock
)
1955 while (!list_empty(&fc
->io
)) {
1956 struct fuse_req
*req
=
1957 list_entry(fc
->io
.next
, struct fuse_req
, list
);
1958 void (*end
) (struct fuse_conn
*, struct fuse_req
*) = req
->end
;
1961 req
->out
.h
.error
= -ECONNABORTED
;
1962 req
->state
= FUSE_REQ_FINISHED
;
1963 list_del_init(&req
->list
);
1964 wake_up(&req
->waitq
);
1967 __fuse_get_request(req
);
1968 spin_unlock(&fc
->lock
);
1969 wait_event(req
->waitq
, !req
->locked
);
1971 fuse_put_request(fc
, req
);
1972 spin_lock(&fc
->lock
);
1977 static void end_queued_requests(struct fuse_conn
*fc
)
1978 __releases(fc
->lock
)
1979 __acquires(fc
->lock
)
1981 fc
->max_background
= UINT_MAX
;
1983 end_requests(fc
, &fc
->pending
);
1984 end_requests(fc
, &fc
->processing
);
1985 while (forget_pending(fc
))
1986 kfree(dequeue_forget(fc
, 1, NULL
));
1989 static void end_polls(struct fuse_conn
*fc
)
1993 p
= rb_first(&fc
->polled_files
);
1996 struct fuse_file
*ff
;
1997 ff
= rb_entry(p
, struct fuse_file
, polled_node
);
1998 wake_up_interruptible_all(&ff
->poll_wait
);
2005 * Abort all requests.
2007 * Emergency exit in case of a malicious or accidental deadlock, or
2008 * just a hung filesystem.
2010 * The same effect is usually achievable through killing the
2011 * filesystem daemon and all users of the filesystem. The exception
2012 * is the combination of an asynchronous request and the tricky
2013 * deadlock (see Documentation/filesystems/fuse.txt).
2015 * During the aborting, progression of requests from the pending and
2016 * processing lists onto the io list, and progression of new requests
2017 * onto the pending list is prevented by req->connected being false.
2019 * Progression of requests under I/O to the processing list is
2020 * prevented by the req->aborted flag being true for these requests.
2021 * For this reason requests on the io list must be aborted first.
2023 void fuse_abort_conn(struct fuse_conn
*fc
)
2025 spin_lock(&fc
->lock
);
2026 if (fc
->connected
) {
2029 end_io_requests(fc
);
2030 end_queued_requests(fc
);
2032 wake_up_all(&fc
->waitq
);
2033 wake_up_all(&fc
->blocked_waitq
);
2034 kill_fasync(&fc
->fasync
, SIGIO
, POLL_IN
);
2036 spin_unlock(&fc
->lock
);
2038 EXPORT_SYMBOL_GPL(fuse_abort_conn
);
2040 int fuse_dev_release(struct inode
*inode
, struct file
*file
)
2042 struct fuse_conn
*fc
= fuse_get_conn(file
);
2044 spin_lock(&fc
->lock
);
2047 end_queued_requests(fc
);
2049 wake_up_all(&fc
->blocked_waitq
);
2050 spin_unlock(&fc
->lock
);
2056 EXPORT_SYMBOL_GPL(fuse_dev_release
);
2058 static int fuse_dev_fasync(int fd
, struct file
*file
, int on
)
2060 struct fuse_conn
*fc
= fuse_get_conn(file
);
2064 /* No locking - fasync_helper does its own locking */
2065 return fasync_helper(fd
, file
, on
, &fc
->fasync
);
2068 const struct file_operations fuse_dev_operations
= {
2069 .owner
= THIS_MODULE
,
2070 .llseek
= no_llseek
,
2071 .read
= do_sync_read
,
2072 .aio_read
= fuse_dev_read
,
2073 .splice_read
= fuse_dev_splice_read
,
2074 .write
= do_sync_write
,
2075 .aio_write
= fuse_dev_write
,
2076 .splice_write
= fuse_dev_splice_write
,
2077 .poll
= fuse_dev_poll
,
2078 .release
= fuse_dev_release
,
2079 .fasync
= fuse_dev_fasync
,
2081 EXPORT_SYMBOL_GPL(fuse_dev_operations
);
2083 static struct miscdevice fuse_miscdevice
= {
2084 .minor
= FUSE_MINOR
,
2086 .fops
= &fuse_dev_operations
,
2089 int __init
fuse_dev_init(void)
2092 fuse_req_cachep
= kmem_cache_create("fuse_request",
2093 sizeof(struct fuse_req
),
2095 if (!fuse_req_cachep
)
2098 err
= misc_register(&fuse_miscdevice
);
2100 goto out_cache_clean
;
2105 kmem_cache_destroy(fuse_req_cachep
);
2110 void fuse_dev_cleanup(void)
2112 misc_deregister(&fuse_miscdevice
);
2113 kmem_cache_destroy(fuse_req_cachep
);