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
, 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
;
53 static struct fuse_req
*__fuse_request_alloc(unsigned npages
, gfp_t flags
)
55 struct fuse_req
*req
= kmem_cache_alloc(fuse_req_cachep
, flags
);
58 struct fuse_page_desc
*page_descs
;
60 if (npages
<= FUSE_REQ_INLINE_PAGES
) {
61 pages
= req
->inline_pages
;
62 page_descs
= req
->inline_page_descs
;
64 pages
= kmalloc(sizeof(struct page
*) * npages
, flags
);
65 page_descs
= kmalloc(sizeof(struct fuse_page_desc
) *
69 if (!pages
|| !page_descs
) {
72 kmem_cache_free(fuse_req_cachep
, req
);
76 fuse_request_init(req
, pages
, page_descs
, npages
);
81 struct fuse_req
*fuse_request_alloc(unsigned npages
)
83 return __fuse_request_alloc(npages
, GFP_KERNEL
);
85 EXPORT_SYMBOL_GPL(fuse_request_alloc
);
87 struct fuse_req
*fuse_request_alloc_nofs(unsigned npages
)
89 return __fuse_request_alloc(npages
, GFP_NOFS
);
92 void fuse_request_free(struct fuse_req
*req
)
94 if (req
->pages
!= req
->inline_pages
) {
96 kfree(req
->page_descs
);
98 kmem_cache_free(fuse_req_cachep
, req
);
101 static void block_sigs(sigset_t
*oldset
)
105 siginitsetinv(&mask
, sigmask(SIGKILL
));
106 sigprocmask(SIG_BLOCK
, &mask
, oldset
);
109 static void restore_sigs(sigset_t
*oldset
)
111 sigprocmask(SIG_SETMASK
, oldset
, NULL
);
114 void __fuse_get_request(struct fuse_req
*req
)
116 atomic_inc(&req
->count
);
119 /* Must be called with > 1 refcount */
120 static void __fuse_put_request(struct fuse_req
*req
)
122 BUG_ON(atomic_read(&req
->count
) < 2);
123 atomic_dec(&req
->count
);
126 static void fuse_req_init_context(struct fuse_req
*req
)
128 req
->in
.h
.uid
= from_kuid_munged(&init_user_ns
, current_fsuid());
129 req
->in
.h
.gid
= from_kgid_munged(&init_user_ns
, current_fsgid());
130 req
->in
.h
.pid
= current
->pid
;
133 static bool fuse_block_alloc(struct fuse_conn
*fc
, bool for_background
)
135 return !fc
->initialized
|| (for_background
&& fc
->blocked
);
138 static struct fuse_req
*__fuse_get_req(struct fuse_conn
*fc
, unsigned npages
,
141 struct fuse_req
*req
;
143 atomic_inc(&fc
->num_waiting
);
145 if (fuse_block_alloc(fc
, for_background
)) {
150 intr
= wait_event_interruptible_exclusive(fc
->blocked_waitq
,
151 !fuse_block_alloc(fc
, for_background
));
152 restore_sigs(&oldset
);
162 req
= fuse_request_alloc(npages
);
166 wake_up(&fc
->blocked_waitq
);
170 fuse_req_init_context(req
);
172 req
->background
= for_background
;
176 atomic_dec(&fc
->num_waiting
);
180 struct fuse_req
*fuse_get_req(struct fuse_conn
*fc
, unsigned npages
)
182 return __fuse_get_req(fc
, npages
, false);
184 EXPORT_SYMBOL_GPL(fuse_get_req
);
186 struct fuse_req
*fuse_get_req_for_background(struct fuse_conn
*fc
,
189 return __fuse_get_req(fc
, npages
, true);
191 EXPORT_SYMBOL_GPL(fuse_get_req_for_background
);
194 * Return request in fuse_file->reserved_req. However that may
195 * currently be in use. If that is the case, wait for it to become
198 static struct fuse_req
*get_reserved_req(struct fuse_conn
*fc
,
201 struct fuse_req
*req
= NULL
;
202 struct fuse_file
*ff
= file
->private_data
;
205 wait_event(fc
->reserved_req_waitq
, ff
->reserved_req
);
206 spin_lock(&fc
->lock
);
207 if (ff
->reserved_req
) {
208 req
= ff
->reserved_req
;
209 ff
->reserved_req
= NULL
;
210 req
->stolen_file
= get_file(file
);
212 spin_unlock(&fc
->lock
);
219 * Put stolen request back into fuse_file->reserved_req
221 static void put_reserved_req(struct fuse_conn
*fc
, struct fuse_req
*req
)
223 struct file
*file
= req
->stolen_file
;
224 struct fuse_file
*ff
= file
->private_data
;
226 spin_lock(&fc
->lock
);
227 fuse_request_init(req
, req
->pages
, req
->page_descs
, req
->max_pages
);
228 BUG_ON(ff
->reserved_req
);
229 ff
->reserved_req
= req
;
230 wake_up_all(&fc
->reserved_req_waitq
);
231 spin_unlock(&fc
->lock
);
236 * Gets a requests for a file operation, always succeeds
238 * This is used for sending the FLUSH request, which must get to
239 * userspace, due to POSIX locks which may need to be unlocked.
241 * If allocation fails due to OOM, use the reserved request in
244 * This is very unlikely to deadlock accidentally, since the
245 * filesystem should not have it's own file open. If deadlock is
246 * intentional, it can still be broken by "aborting" the filesystem.
248 struct fuse_req
*fuse_get_req_nofail_nopages(struct fuse_conn
*fc
,
251 struct fuse_req
*req
;
253 atomic_inc(&fc
->num_waiting
);
254 wait_event(fc
->blocked_waitq
, fc
->initialized
);
255 req
= fuse_request_alloc(0);
257 req
= get_reserved_req(fc
, file
);
259 fuse_req_init_context(req
);
265 void fuse_put_request(struct fuse_conn
*fc
, struct fuse_req
*req
)
267 if (atomic_dec_and_test(&req
->count
)) {
268 if (unlikely(req
->background
)) {
270 * We get here in the unlikely case that a background
271 * request was allocated but not sent
273 spin_lock(&fc
->lock
);
275 wake_up(&fc
->blocked_waitq
);
276 spin_unlock(&fc
->lock
);
280 atomic_dec(&fc
->num_waiting
);
282 if (req
->stolen_file
)
283 put_reserved_req(fc
, req
);
285 fuse_request_free(req
);
288 EXPORT_SYMBOL_GPL(fuse_put_request
);
290 static unsigned len_args(unsigned numargs
, struct fuse_arg
*args
)
295 for (i
= 0; i
< numargs
; i
++)
296 nbytes
+= args
[i
].size
;
301 static u64
fuse_get_unique(struct fuse_conn
*fc
)
304 /* zero is special */
311 static void queue_request(struct fuse_conn
*fc
, struct fuse_req
*req
)
313 req
->in
.h
.len
= sizeof(struct fuse_in_header
) +
314 len_args(req
->in
.numargs
, (struct fuse_arg
*) req
->in
.args
);
315 list_add_tail(&req
->list
, &fc
->pending
);
316 req
->state
= FUSE_REQ_PENDING
;
319 atomic_inc(&fc
->num_waiting
);
322 kill_fasync(&fc
->fasync
, SIGIO
, POLL_IN
);
325 void fuse_queue_forget(struct fuse_conn
*fc
, struct fuse_forget_link
*forget
,
326 u64 nodeid
, u64 nlookup
)
328 forget
->forget_one
.nodeid
= nodeid
;
329 forget
->forget_one
.nlookup
= nlookup
;
331 spin_lock(&fc
->lock
);
333 fc
->forget_list_tail
->next
= forget
;
334 fc
->forget_list_tail
= forget
;
336 kill_fasync(&fc
->fasync
, SIGIO
, POLL_IN
);
340 spin_unlock(&fc
->lock
);
343 static void flush_bg_queue(struct fuse_conn
*fc
)
345 while (fc
->active_background
< fc
->max_background
&&
346 !list_empty(&fc
->bg_queue
)) {
347 struct fuse_req
*req
;
349 req
= list_entry(fc
->bg_queue
.next
, struct fuse_req
, list
);
350 list_del(&req
->list
);
351 fc
->active_background
++;
352 req
->in
.h
.unique
= fuse_get_unique(fc
);
353 queue_request(fc
, req
);
358 * This function is called when a request is finished. Either a reply
359 * has arrived or it was aborted (and not yet sent) or some error
360 * occurred during communication with userspace, or the device file
361 * was closed. The requester thread is woken up (if still waiting),
362 * the 'end' callback is called if given, else the reference to the
363 * request is released
365 * Called with fc->lock, unlocks it
367 static void request_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
370 void (*end
) (struct fuse_conn
*, struct fuse_req
*) = req
->end
;
372 list_del(&req
->list
);
373 list_del(&req
->intr_entry
);
374 req
->state
= FUSE_REQ_FINISHED
;
375 if (req
->background
) {
378 if (fc
->num_background
== fc
->max_background
)
381 /* Wake up next waiter, if any */
382 if (!fc
->blocked
&& waitqueue_active(&fc
->blocked_waitq
))
383 wake_up(&fc
->blocked_waitq
);
385 if (fc
->num_background
== fc
->congestion_threshold
&&
386 fc
->connected
&& fc
->bdi_initialized
) {
387 clear_bdi_congested(&fc
->bdi
, BLK_RW_SYNC
);
388 clear_bdi_congested(&fc
->bdi
, BLK_RW_ASYNC
);
390 fc
->num_background
--;
391 fc
->active_background
--;
394 spin_unlock(&fc
->lock
);
395 wake_up(&req
->waitq
);
398 fuse_put_request(fc
, req
);
401 static void wait_answer_interruptible(struct fuse_conn
*fc
,
402 struct fuse_req
*req
)
406 if (signal_pending(current
))
409 spin_unlock(&fc
->lock
);
410 wait_event_interruptible(req
->waitq
, req
->state
== FUSE_REQ_FINISHED
);
411 spin_lock(&fc
->lock
);
414 static void queue_interrupt(struct fuse_conn
*fc
, struct fuse_req
*req
)
416 list_add_tail(&req
->intr_entry
, &fc
->interrupts
);
418 kill_fasync(&fc
->fasync
, SIGIO
, POLL_IN
);
421 static void request_wait_answer(struct fuse_conn
*fc
, struct fuse_req
*req
)
425 if (!fc
->no_interrupt
) {
426 /* Any signal may interrupt this */
427 wait_answer_interruptible(fc
, req
);
431 if (req
->state
== FUSE_REQ_FINISHED
)
434 req
->interrupted
= 1;
435 if (req
->state
== FUSE_REQ_SENT
)
436 queue_interrupt(fc
, req
);
442 /* Only fatal signals may interrupt this */
444 wait_answer_interruptible(fc
, req
);
445 restore_sigs(&oldset
);
449 if (req
->state
== FUSE_REQ_FINISHED
)
452 /* Request is not yet in userspace, bail out */
453 if (req
->state
== FUSE_REQ_PENDING
) {
454 list_del(&req
->list
);
455 __fuse_put_request(req
);
456 req
->out
.h
.error
= -EINTR
;
462 * Either request is already in userspace, or it was forced.
465 spin_unlock(&fc
->lock
);
466 wait_event(req
->waitq
, req
->state
== FUSE_REQ_FINISHED
);
467 spin_lock(&fc
->lock
);
473 BUG_ON(req
->state
!= FUSE_REQ_FINISHED
);
475 /* This is uninterruptible sleep, because data is
476 being copied to/from the buffers of req. During
477 locked state, there mustn't be any filesystem
478 operation (e.g. page fault), since that could lead
480 spin_unlock(&fc
->lock
);
481 wait_event(req
->waitq
, !req
->locked
);
482 spin_lock(&fc
->lock
);
486 static void __fuse_request_send(struct fuse_conn
*fc
, struct fuse_req
*req
)
488 BUG_ON(req
->background
);
489 spin_lock(&fc
->lock
);
491 req
->out
.h
.error
= -ENOTCONN
;
492 else if (fc
->conn_error
)
493 req
->out
.h
.error
= -ECONNREFUSED
;
495 req
->in
.h
.unique
= fuse_get_unique(fc
);
496 queue_request(fc
, req
);
497 /* acquire extra reference, since request is still needed
498 after request_end() */
499 __fuse_get_request(req
);
501 request_wait_answer(fc
, req
);
503 spin_unlock(&fc
->lock
);
506 void fuse_request_send(struct fuse_conn
*fc
, struct fuse_req
*req
)
509 __fuse_request_send(fc
, req
);
511 EXPORT_SYMBOL_GPL(fuse_request_send
);
513 static void fuse_request_send_nowait_locked(struct fuse_conn
*fc
,
514 struct fuse_req
*req
)
516 BUG_ON(!req
->background
);
517 fc
->num_background
++;
518 if (fc
->num_background
== fc
->max_background
)
520 if (fc
->num_background
== fc
->congestion_threshold
&&
521 fc
->bdi_initialized
) {
522 set_bdi_congested(&fc
->bdi
, BLK_RW_SYNC
);
523 set_bdi_congested(&fc
->bdi
, BLK_RW_ASYNC
);
525 list_add_tail(&req
->list
, &fc
->bg_queue
);
529 static void fuse_request_send_nowait(struct fuse_conn
*fc
, struct fuse_req
*req
)
531 spin_lock(&fc
->lock
);
533 fuse_request_send_nowait_locked(fc
, req
);
534 spin_unlock(&fc
->lock
);
536 req
->out
.h
.error
= -ENOTCONN
;
537 request_end(fc
, req
);
541 void fuse_request_send_background(struct fuse_conn
*fc
, struct fuse_req
*req
)
544 fuse_request_send_nowait(fc
, req
);
546 EXPORT_SYMBOL_GPL(fuse_request_send_background
);
548 static int fuse_request_send_notify_reply(struct fuse_conn
*fc
,
549 struct fuse_req
*req
, u64 unique
)
554 req
->in
.h
.unique
= unique
;
555 spin_lock(&fc
->lock
);
557 queue_request(fc
, req
);
560 spin_unlock(&fc
->lock
);
566 * Called under fc->lock
568 * fc->connected must have been checked previously
570 void fuse_request_send_background_locked(struct fuse_conn
*fc
,
571 struct fuse_req
*req
)
574 fuse_request_send_nowait_locked(fc
, req
);
577 void fuse_force_forget(struct file
*file
, u64 nodeid
)
579 struct inode
*inode
= file_inode(file
);
580 struct fuse_conn
*fc
= get_fuse_conn(inode
);
581 struct fuse_req
*req
;
582 struct fuse_forget_in inarg
;
584 memset(&inarg
, 0, sizeof(inarg
));
586 req
= fuse_get_req_nofail_nopages(fc
, file
);
587 req
->in
.h
.opcode
= FUSE_FORGET
;
588 req
->in
.h
.nodeid
= nodeid
;
590 req
->in
.args
[0].size
= sizeof(inarg
);
591 req
->in
.args
[0].value
= &inarg
;
593 __fuse_request_send(fc
, req
);
595 fuse_put_request(fc
, req
);
599 * Lock the request. Up to the next unlock_request() there mustn't be
600 * anything that could cause a page-fault. If the request was already
603 static int lock_request(struct fuse_conn
*fc
, struct fuse_req
*req
)
607 spin_lock(&fc
->lock
);
612 spin_unlock(&fc
->lock
);
618 * Unlock request. If it was aborted during being locked, the
619 * requester thread is currently waiting for it to be unlocked, so
622 static void unlock_request(struct fuse_conn
*fc
, struct fuse_req
*req
)
625 spin_lock(&fc
->lock
);
628 wake_up(&req
->waitq
);
629 spin_unlock(&fc
->lock
);
633 struct fuse_copy_state
{
634 struct fuse_conn
*fc
;
636 struct fuse_req
*req
;
637 const struct iovec
*iov
;
638 struct pipe_buffer
*pipebufs
;
639 struct pipe_buffer
*currbuf
;
640 struct pipe_inode_info
*pipe
;
641 unsigned long nr_segs
;
642 unsigned long seglen
;
648 unsigned move_pages
:1;
651 static void fuse_copy_init(struct fuse_copy_state
*cs
, struct fuse_conn
*fc
,
653 const struct iovec
*iov
, unsigned long nr_segs
)
655 memset(cs
, 0, sizeof(*cs
));
659 cs
->nr_segs
= nr_segs
;
662 /* Unmap and put previous page of userspace buffer */
663 static void fuse_copy_finish(struct fuse_copy_state
*cs
)
666 struct pipe_buffer
*buf
= cs
->currbuf
;
669 buf
->ops
->unmap(cs
->pipe
, buf
, cs
->mapaddr
);
672 buf
->len
= PAGE_SIZE
- cs
->len
;
676 } else if (cs
->mapaddr
) {
679 flush_dcache_page(cs
->pg
);
680 set_page_dirty_lock(cs
->pg
);
688 * Get another pagefull of userspace buffer, and map it to kernel
689 * address space, and lock request
691 static int fuse_copy_fill(struct fuse_copy_state
*cs
)
693 unsigned long offset
;
696 unlock_request(cs
->fc
, cs
->req
);
697 fuse_copy_finish(cs
);
699 struct pipe_buffer
*buf
= cs
->pipebufs
;
702 err
= buf
->ops
->confirm(cs
->pipe
, buf
);
706 BUG_ON(!cs
->nr_segs
);
708 cs
->mapaddr
= buf
->ops
->map(cs
->pipe
, buf
, 0);
710 cs
->buf
= cs
->mapaddr
+ buf
->offset
;
716 if (cs
->nr_segs
== cs
->pipe
->buffers
)
719 page
= alloc_page(GFP_HIGHUSER
);
728 cs
->mapaddr
= kmap(page
);
729 cs
->buf
= cs
->mapaddr
;
736 BUG_ON(!cs
->nr_segs
);
737 cs
->seglen
= cs
->iov
[0].iov_len
;
738 cs
->addr
= (unsigned long) cs
->iov
[0].iov_base
;
742 err
= get_user_pages_fast(cs
->addr
, 1, cs
->write
, &cs
->pg
);
746 offset
= cs
->addr
% PAGE_SIZE
;
747 cs
->mapaddr
= kmap(cs
->pg
);
748 cs
->buf
= cs
->mapaddr
+ offset
;
749 cs
->len
= min(PAGE_SIZE
- offset
, cs
->seglen
);
750 cs
->seglen
-= cs
->len
;
754 return lock_request(cs
->fc
, cs
->req
);
757 /* Do as much copy to/from userspace buffer as we can */
758 static int fuse_copy_do(struct fuse_copy_state
*cs
, void **val
, unsigned *size
)
760 unsigned ncpy
= min(*size
, cs
->len
);
763 memcpy(cs
->buf
, *val
, ncpy
);
765 memcpy(*val
, cs
->buf
, ncpy
);
774 static int fuse_check_page(struct page
*page
)
776 if (page_mapcount(page
) ||
777 page
->mapping
!= NULL
||
778 page_count(page
) != 1 ||
779 (page
->flags
& PAGE_FLAGS_CHECK_AT_PREP
&
786 printk(KERN_WARNING
"fuse: trying to steal weird page\n");
787 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
);
793 static int fuse_try_move_page(struct fuse_copy_state
*cs
, struct page
**pagep
)
796 struct page
*oldpage
= *pagep
;
797 struct page
*newpage
;
798 struct pipe_buffer
*buf
= cs
->pipebufs
;
800 unlock_request(cs
->fc
, cs
->req
);
801 fuse_copy_finish(cs
);
803 err
= buf
->ops
->confirm(cs
->pipe
, buf
);
807 BUG_ON(!cs
->nr_segs
);
813 if (cs
->len
!= PAGE_SIZE
)
816 if (buf
->ops
->steal(cs
->pipe
, buf
) != 0)
821 if (WARN_ON(!PageUptodate(newpage
)))
824 ClearPageMappedToDisk(newpage
);
826 if (fuse_check_page(newpage
) != 0)
827 goto out_fallback_unlock
;
830 * This is a new and locked page, it shouldn't be mapped or
831 * have any special flags on it
833 if (WARN_ON(page_mapped(oldpage
)))
834 goto out_fallback_unlock
;
835 if (WARN_ON(page_has_private(oldpage
)))
836 goto out_fallback_unlock
;
837 if (WARN_ON(PageDirty(oldpage
) || PageWriteback(oldpage
)))
838 goto out_fallback_unlock
;
839 if (WARN_ON(PageMlocked(oldpage
)))
840 goto out_fallback_unlock
;
842 err
= replace_page_cache_page(oldpage
, newpage
, GFP_KERNEL
);
844 unlock_page(newpage
);
848 page_cache_get(newpage
);
850 if (!(buf
->flags
& PIPE_BUF_FLAG_LRU
))
851 lru_cache_add_file(newpage
);
854 spin_lock(&cs
->fc
->lock
);
855 if (cs
->req
->aborted
)
859 spin_unlock(&cs
->fc
->lock
);
862 unlock_page(newpage
);
863 page_cache_release(newpage
);
867 unlock_page(oldpage
);
868 page_cache_release(oldpage
);
874 unlock_page(newpage
);
876 cs
->mapaddr
= buf
->ops
->map(cs
->pipe
, buf
, 1);
877 cs
->buf
= cs
->mapaddr
+ buf
->offset
;
879 err
= lock_request(cs
->fc
, cs
->req
);
886 static int fuse_ref_page(struct fuse_copy_state
*cs
, struct page
*page
,
887 unsigned offset
, unsigned count
)
889 struct pipe_buffer
*buf
;
891 if (cs
->nr_segs
== cs
->pipe
->buffers
)
894 unlock_request(cs
->fc
, cs
->req
);
895 fuse_copy_finish(cs
);
898 page_cache_get(page
);
900 buf
->offset
= offset
;
911 * Copy a page in the request to/from the userspace buffer. Must be
914 static int fuse_copy_page(struct fuse_copy_state
*cs
, struct page
**pagep
,
915 unsigned offset
, unsigned count
, int zeroing
)
918 struct page
*page
= *pagep
;
920 if (page
&& zeroing
&& count
< PAGE_SIZE
)
921 clear_highpage(page
);
924 if (cs
->write
&& cs
->pipebufs
&& page
) {
925 return fuse_ref_page(cs
, page
, offset
, count
);
926 } else if (!cs
->len
) {
927 if (cs
->move_pages
&& page
&&
928 offset
== 0 && count
== PAGE_SIZE
) {
929 err
= fuse_try_move_page(cs
, pagep
);
933 err
= fuse_copy_fill(cs
);
939 void *mapaddr
= kmap_atomic(page
);
940 void *buf
= mapaddr
+ offset
;
941 offset
+= fuse_copy_do(cs
, &buf
, &count
);
942 kunmap_atomic(mapaddr
);
944 offset
+= fuse_copy_do(cs
, NULL
, &count
);
946 if (page
&& !cs
->write
)
947 flush_dcache_page(page
);
951 /* Copy pages in the request to/from userspace buffer */
952 static int fuse_copy_pages(struct fuse_copy_state
*cs
, unsigned nbytes
,
956 struct fuse_req
*req
= cs
->req
;
958 for (i
= 0; i
< req
->num_pages
&& (nbytes
|| zeroing
); i
++) {
960 unsigned offset
= req
->page_descs
[i
].offset
;
961 unsigned count
= min(nbytes
, req
->page_descs
[i
].length
);
963 err
= fuse_copy_page(cs
, &req
->pages
[i
], offset
, count
,
973 /* Copy a single argument in the request to/from userspace buffer */
974 static int fuse_copy_one(struct fuse_copy_state
*cs
, void *val
, unsigned size
)
978 int err
= fuse_copy_fill(cs
);
982 fuse_copy_do(cs
, &val
, &size
);
987 /* Copy request arguments to/from userspace buffer */
988 static int fuse_copy_args(struct fuse_copy_state
*cs
, unsigned numargs
,
989 unsigned argpages
, struct fuse_arg
*args
,
995 for (i
= 0; !err
&& i
< numargs
; i
++) {
996 struct fuse_arg
*arg
= &args
[i
];
997 if (i
== numargs
- 1 && argpages
)
998 err
= fuse_copy_pages(cs
, arg
->size
, zeroing
);
1000 err
= fuse_copy_one(cs
, arg
->value
, arg
->size
);
1005 static int forget_pending(struct fuse_conn
*fc
)
1007 return fc
->forget_list_head
.next
!= NULL
;
1010 static int request_pending(struct fuse_conn
*fc
)
1012 return !list_empty(&fc
->pending
) || !list_empty(&fc
->interrupts
) ||
1016 /* Wait until a request is available on the pending list */
1017 static void request_wait(struct fuse_conn
*fc
)
1018 __releases(fc
->lock
)
1019 __acquires(fc
->lock
)
1021 DECLARE_WAITQUEUE(wait
, current
);
1023 add_wait_queue_exclusive(&fc
->waitq
, &wait
);
1024 while (fc
->connected
&& !request_pending(fc
)) {
1025 set_current_state(TASK_INTERRUPTIBLE
);
1026 if (signal_pending(current
))
1029 spin_unlock(&fc
->lock
);
1031 spin_lock(&fc
->lock
);
1033 set_current_state(TASK_RUNNING
);
1034 remove_wait_queue(&fc
->waitq
, &wait
);
1038 * Transfer an interrupt request to userspace
1040 * Unlike other requests this is assembled on demand, without a need
1041 * to allocate a separate fuse_req structure.
1043 * Called with fc->lock held, releases it
1045 static int fuse_read_interrupt(struct fuse_conn
*fc
, struct fuse_copy_state
*cs
,
1046 size_t nbytes
, struct fuse_req
*req
)
1047 __releases(fc
->lock
)
1049 struct fuse_in_header ih
;
1050 struct fuse_interrupt_in arg
;
1051 unsigned reqsize
= sizeof(ih
) + sizeof(arg
);
1054 list_del_init(&req
->intr_entry
);
1055 req
->intr_unique
= fuse_get_unique(fc
);
1056 memset(&ih
, 0, sizeof(ih
));
1057 memset(&arg
, 0, sizeof(arg
));
1059 ih
.opcode
= FUSE_INTERRUPT
;
1060 ih
.unique
= req
->intr_unique
;
1061 arg
.unique
= req
->in
.h
.unique
;
1063 spin_unlock(&fc
->lock
);
1064 if (nbytes
< reqsize
)
1067 err
= fuse_copy_one(cs
, &ih
, sizeof(ih
));
1069 err
= fuse_copy_one(cs
, &arg
, sizeof(arg
));
1070 fuse_copy_finish(cs
);
1072 return err
? err
: reqsize
;
1075 static struct fuse_forget_link
*dequeue_forget(struct fuse_conn
*fc
,
1079 struct fuse_forget_link
*head
= fc
->forget_list_head
.next
;
1080 struct fuse_forget_link
**newhead
= &head
;
1083 for (count
= 0; *newhead
!= NULL
&& count
< max
; count
++)
1084 newhead
= &(*newhead
)->next
;
1086 fc
->forget_list_head
.next
= *newhead
;
1088 if (fc
->forget_list_head
.next
== NULL
)
1089 fc
->forget_list_tail
= &fc
->forget_list_head
;
1097 static int fuse_read_single_forget(struct fuse_conn
*fc
,
1098 struct fuse_copy_state
*cs
,
1100 __releases(fc
->lock
)
1103 struct fuse_forget_link
*forget
= dequeue_forget(fc
, 1, NULL
);
1104 struct fuse_forget_in arg
= {
1105 .nlookup
= forget
->forget_one
.nlookup
,
1107 struct fuse_in_header ih
= {
1108 .opcode
= FUSE_FORGET
,
1109 .nodeid
= forget
->forget_one
.nodeid
,
1110 .unique
= fuse_get_unique(fc
),
1111 .len
= sizeof(ih
) + sizeof(arg
),
1114 spin_unlock(&fc
->lock
);
1116 if (nbytes
< ih
.len
)
1119 err
= fuse_copy_one(cs
, &ih
, sizeof(ih
));
1121 err
= fuse_copy_one(cs
, &arg
, sizeof(arg
));
1122 fuse_copy_finish(cs
);
1130 static int fuse_read_batch_forget(struct fuse_conn
*fc
,
1131 struct fuse_copy_state
*cs
, size_t nbytes
)
1132 __releases(fc
->lock
)
1135 unsigned max_forgets
;
1137 struct fuse_forget_link
*head
;
1138 struct fuse_batch_forget_in arg
= { .count
= 0 };
1139 struct fuse_in_header ih
= {
1140 .opcode
= FUSE_BATCH_FORGET
,
1141 .unique
= fuse_get_unique(fc
),
1142 .len
= sizeof(ih
) + sizeof(arg
),
1145 if (nbytes
< ih
.len
) {
1146 spin_unlock(&fc
->lock
);
1150 max_forgets
= (nbytes
- ih
.len
) / sizeof(struct fuse_forget_one
);
1151 head
= dequeue_forget(fc
, max_forgets
, &count
);
1152 spin_unlock(&fc
->lock
);
1155 ih
.len
+= count
* sizeof(struct fuse_forget_one
);
1156 err
= fuse_copy_one(cs
, &ih
, sizeof(ih
));
1158 err
= fuse_copy_one(cs
, &arg
, sizeof(arg
));
1161 struct fuse_forget_link
*forget
= head
;
1164 err
= fuse_copy_one(cs
, &forget
->forget_one
,
1165 sizeof(forget
->forget_one
));
1167 head
= forget
->next
;
1171 fuse_copy_finish(cs
);
1179 static int fuse_read_forget(struct fuse_conn
*fc
, struct fuse_copy_state
*cs
,
1181 __releases(fc
->lock
)
1183 if (fc
->minor
< 16 || fc
->forget_list_head
.next
->next
== NULL
)
1184 return fuse_read_single_forget(fc
, cs
, nbytes
);
1186 return fuse_read_batch_forget(fc
, cs
, nbytes
);
1190 * Read a single request into the userspace filesystem's buffer. This
1191 * function waits until a request is available, then removes it from
1192 * the pending list and copies request data to userspace buffer. If
1193 * no reply is needed (FORGET) or request has been aborted or there
1194 * was an error during the copying then it's finished by calling
1195 * request_end(). Otherwise add it to the processing list, and set
1198 static ssize_t
fuse_dev_do_read(struct fuse_conn
*fc
, struct file
*file
,
1199 struct fuse_copy_state
*cs
, size_t nbytes
)
1202 struct fuse_req
*req
;
1207 spin_lock(&fc
->lock
);
1209 if ((file
->f_flags
& O_NONBLOCK
) && fc
->connected
&&
1210 !request_pending(fc
))
1218 if (!request_pending(fc
))
1221 if (!list_empty(&fc
->interrupts
)) {
1222 req
= list_entry(fc
->interrupts
.next
, struct fuse_req
,
1224 return fuse_read_interrupt(fc
, cs
, nbytes
, req
);
1227 if (forget_pending(fc
)) {
1228 if (list_empty(&fc
->pending
) || fc
->forget_batch
-- > 0)
1229 return fuse_read_forget(fc
, cs
, nbytes
);
1231 if (fc
->forget_batch
<= -8)
1232 fc
->forget_batch
= 16;
1235 req
= list_entry(fc
->pending
.next
, struct fuse_req
, list
);
1236 req
->state
= FUSE_REQ_READING
;
1237 list_move(&req
->list
, &fc
->io
);
1240 reqsize
= in
->h
.len
;
1241 /* If request is too large, reply with an error and restart the read */
1242 if (nbytes
< reqsize
) {
1243 req
->out
.h
.error
= -EIO
;
1244 /* SETXATTR is special, since it may contain too large data */
1245 if (in
->h
.opcode
== FUSE_SETXATTR
)
1246 req
->out
.h
.error
= -E2BIG
;
1247 request_end(fc
, req
);
1250 spin_unlock(&fc
->lock
);
1252 err
= fuse_copy_one(cs
, &in
->h
, sizeof(in
->h
));
1254 err
= fuse_copy_args(cs
, in
->numargs
, in
->argpages
,
1255 (struct fuse_arg
*) in
->args
, 0);
1256 fuse_copy_finish(cs
);
1257 spin_lock(&fc
->lock
);
1260 request_end(fc
, req
);
1264 req
->out
.h
.error
= -EIO
;
1265 request_end(fc
, req
);
1269 request_end(fc
, req
);
1271 req
->state
= FUSE_REQ_SENT
;
1272 list_move_tail(&req
->list
, &fc
->processing
);
1273 if (req
->interrupted
)
1274 queue_interrupt(fc
, req
);
1275 spin_unlock(&fc
->lock
);
1280 spin_unlock(&fc
->lock
);
1284 static ssize_t
fuse_dev_read(struct kiocb
*iocb
, const struct iovec
*iov
,
1285 unsigned long nr_segs
, loff_t pos
)
1287 struct fuse_copy_state cs
;
1288 struct file
*file
= iocb
->ki_filp
;
1289 struct fuse_conn
*fc
= fuse_get_conn(file
);
1293 fuse_copy_init(&cs
, fc
, 1, iov
, nr_segs
);
1295 return fuse_dev_do_read(fc
, file
, &cs
, iov_length(iov
, nr_segs
));
1298 static int fuse_dev_pipe_buf_steal(struct pipe_inode_info
*pipe
,
1299 struct pipe_buffer
*buf
)
1304 static const struct pipe_buf_operations fuse_dev_pipe_buf_ops
= {
1306 .map
= generic_pipe_buf_map
,
1307 .unmap
= generic_pipe_buf_unmap
,
1308 .confirm
= generic_pipe_buf_confirm
,
1309 .release
= generic_pipe_buf_release
,
1310 .steal
= fuse_dev_pipe_buf_steal
,
1311 .get
= generic_pipe_buf_get
,
1314 static ssize_t
fuse_dev_splice_read(struct file
*in
, loff_t
*ppos
,
1315 struct pipe_inode_info
*pipe
,
1316 size_t len
, unsigned int flags
)
1321 struct pipe_buffer
*bufs
;
1322 struct fuse_copy_state cs
;
1323 struct fuse_conn
*fc
= fuse_get_conn(in
);
1327 bufs
= kmalloc(pipe
->buffers
* sizeof(struct pipe_buffer
), GFP_KERNEL
);
1331 fuse_copy_init(&cs
, fc
, 1, NULL
, 0);
1334 ret
= fuse_dev_do_read(fc
, in
, &cs
, len
);
1341 if (!pipe
->readers
) {
1342 send_sig(SIGPIPE
, current
, 0);
1348 if (pipe
->nrbufs
+ cs
.nr_segs
> pipe
->buffers
) {
1353 while (page_nr
< cs
.nr_segs
) {
1354 int newbuf
= (pipe
->curbuf
+ pipe
->nrbufs
) & (pipe
->buffers
- 1);
1355 struct pipe_buffer
*buf
= pipe
->bufs
+ newbuf
;
1357 buf
->page
= bufs
[page_nr
].page
;
1358 buf
->offset
= bufs
[page_nr
].offset
;
1359 buf
->len
= bufs
[page_nr
].len
;
1360 buf
->ops
= &fuse_dev_pipe_buf_ops
;
1375 if (waitqueue_active(&pipe
->wait
))
1376 wake_up_interruptible(&pipe
->wait
);
1377 kill_fasync(&pipe
->fasync_readers
, SIGIO
, POLL_IN
);
1381 for (; page_nr
< cs
.nr_segs
; page_nr
++)
1382 page_cache_release(bufs
[page_nr
].page
);
1388 static int fuse_notify_poll(struct fuse_conn
*fc
, unsigned int size
,
1389 struct fuse_copy_state
*cs
)
1391 struct fuse_notify_poll_wakeup_out outarg
;
1394 if (size
!= sizeof(outarg
))
1397 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1401 fuse_copy_finish(cs
);
1402 return fuse_notify_poll_wakeup(fc
, &outarg
);
1405 fuse_copy_finish(cs
);
1409 static int fuse_notify_inval_inode(struct fuse_conn
*fc
, unsigned int size
,
1410 struct fuse_copy_state
*cs
)
1412 struct fuse_notify_inval_inode_out outarg
;
1415 if (size
!= sizeof(outarg
))
1418 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1421 fuse_copy_finish(cs
);
1423 down_read(&fc
->killsb
);
1426 err
= fuse_reverse_inval_inode(fc
->sb
, outarg
.ino
,
1427 outarg
.off
, outarg
.len
);
1429 up_read(&fc
->killsb
);
1433 fuse_copy_finish(cs
);
1437 static int fuse_notify_inval_entry(struct fuse_conn
*fc
, unsigned int size
,
1438 struct fuse_copy_state
*cs
)
1440 struct fuse_notify_inval_entry_out outarg
;
1445 buf
= kzalloc(FUSE_NAME_MAX
+ 1, GFP_KERNEL
);
1450 if (size
< sizeof(outarg
))
1453 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1457 err
= -ENAMETOOLONG
;
1458 if (outarg
.namelen
> FUSE_NAME_MAX
)
1462 if (size
!= sizeof(outarg
) + outarg
.namelen
+ 1)
1466 name
.len
= outarg
.namelen
;
1467 err
= fuse_copy_one(cs
, buf
, outarg
.namelen
+ 1);
1470 fuse_copy_finish(cs
);
1471 buf
[outarg
.namelen
] = 0;
1472 name
.hash
= full_name_hash(name
.name
, name
.len
);
1474 down_read(&fc
->killsb
);
1477 err
= fuse_reverse_inval_entry(fc
->sb
, outarg
.parent
, 0, &name
);
1478 up_read(&fc
->killsb
);
1484 fuse_copy_finish(cs
);
1488 static int fuse_notify_delete(struct fuse_conn
*fc
, unsigned int size
,
1489 struct fuse_copy_state
*cs
)
1491 struct fuse_notify_delete_out outarg
;
1496 buf
= kzalloc(FUSE_NAME_MAX
+ 1, GFP_KERNEL
);
1501 if (size
< sizeof(outarg
))
1504 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1508 err
= -ENAMETOOLONG
;
1509 if (outarg
.namelen
> FUSE_NAME_MAX
)
1513 if (size
!= sizeof(outarg
) + outarg
.namelen
+ 1)
1517 name
.len
= outarg
.namelen
;
1518 err
= fuse_copy_one(cs
, buf
, outarg
.namelen
+ 1);
1521 fuse_copy_finish(cs
);
1522 buf
[outarg
.namelen
] = 0;
1523 name
.hash
= full_name_hash(name
.name
, name
.len
);
1525 down_read(&fc
->killsb
);
1528 err
= fuse_reverse_inval_entry(fc
->sb
, outarg
.parent
,
1529 outarg
.child
, &name
);
1530 up_read(&fc
->killsb
);
1536 fuse_copy_finish(cs
);
1540 static int fuse_notify_store(struct fuse_conn
*fc
, unsigned int size
,
1541 struct fuse_copy_state
*cs
)
1543 struct fuse_notify_store_out outarg
;
1544 struct inode
*inode
;
1545 struct address_space
*mapping
;
1549 unsigned int offset
;
1555 if (size
< sizeof(outarg
))
1558 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1563 if (size
- sizeof(outarg
) != outarg
.size
)
1566 nodeid
= outarg
.nodeid
;
1568 down_read(&fc
->killsb
);
1574 inode
= ilookup5(fc
->sb
, nodeid
, fuse_inode_eq
, &nodeid
);
1578 mapping
= inode
->i_mapping
;
1579 index
= outarg
.offset
>> PAGE_CACHE_SHIFT
;
1580 offset
= outarg
.offset
& ~PAGE_CACHE_MASK
;
1581 file_size
= i_size_read(inode
);
1582 end
= outarg
.offset
+ outarg
.size
;
1583 if (end
> file_size
) {
1585 fuse_write_update_size(inode
, file_size
);
1591 unsigned int this_num
;
1594 page
= find_or_create_page(mapping
, index
,
1595 mapping_gfp_mask(mapping
));
1599 this_num
= min_t(unsigned, num
, PAGE_CACHE_SIZE
- offset
);
1600 err
= fuse_copy_page(cs
, &page
, offset
, this_num
, 0);
1601 if (!err
&& offset
== 0 && (num
!= 0 || file_size
== end
))
1602 SetPageUptodate(page
);
1604 page_cache_release(page
);
1619 up_read(&fc
->killsb
);
1621 fuse_copy_finish(cs
);
1625 static void fuse_retrieve_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
1627 release_pages(req
->pages
, req
->num_pages
, 0);
1630 static int fuse_retrieve(struct fuse_conn
*fc
, struct inode
*inode
,
1631 struct fuse_notify_retrieve_out
*outarg
)
1634 struct address_space
*mapping
= inode
->i_mapping
;
1635 struct fuse_req
*req
;
1639 unsigned int offset
;
1640 size_t total_len
= 0;
1643 offset
= outarg
->offset
& ~PAGE_CACHE_MASK
;
1644 file_size
= i_size_read(inode
);
1647 if (outarg
->offset
> file_size
)
1649 else if (outarg
->offset
+ num
> file_size
)
1650 num
= file_size
- outarg
->offset
;
1652 num_pages
= (num
+ offset
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1653 num_pages
= min(num_pages
, FUSE_MAX_PAGES_PER_REQ
);
1655 req
= fuse_get_req(fc
, num_pages
);
1657 return PTR_ERR(req
);
1659 req
->in
.h
.opcode
= FUSE_NOTIFY_REPLY
;
1660 req
->in
.h
.nodeid
= outarg
->nodeid
;
1661 req
->in
.numargs
= 2;
1662 req
->in
.argpages
= 1;
1663 req
->page_descs
[0].offset
= offset
;
1664 req
->end
= fuse_retrieve_end
;
1666 index
= outarg
->offset
>> PAGE_CACHE_SHIFT
;
1668 while (num
&& req
->num_pages
< num_pages
) {
1670 unsigned int this_num
;
1672 page
= find_get_page(mapping
, index
);
1676 this_num
= min_t(unsigned, num
, PAGE_CACHE_SIZE
- offset
);
1677 req
->pages
[req
->num_pages
] = page
;
1678 req
->page_descs
[req
->num_pages
].length
= this_num
;
1683 total_len
+= this_num
;
1686 req
->misc
.retrieve_in
.offset
= outarg
->offset
;
1687 req
->misc
.retrieve_in
.size
= total_len
;
1688 req
->in
.args
[0].size
= sizeof(req
->misc
.retrieve_in
);
1689 req
->in
.args
[0].value
= &req
->misc
.retrieve_in
;
1690 req
->in
.args
[1].size
= total_len
;
1692 err
= fuse_request_send_notify_reply(fc
, req
, outarg
->notify_unique
);
1694 fuse_retrieve_end(fc
, req
);
1699 static int fuse_notify_retrieve(struct fuse_conn
*fc
, unsigned int size
,
1700 struct fuse_copy_state
*cs
)
1702 struct fuse_notify_retrieve_out outarg
;
1703 struct inode
*inode
;
1707 if (size
!= sizeof(outarg
))
1710 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1714 fuse_copy_finish(cs
);
1716 down_read(&fc
->killsb
);
1719 u64 nodeid
= outarg
.nodeid
;
1721 inode
= ilookup5(fc
->sb
, nodeid
, fuse_inode_eq
, &nodeid
);
1723 err
= fuse_retrieve(fc
, inode
, &outarg
);
1727 up_read(&fc
->killsb
);
1732 fuse_copy_finish(cs
);
1736 static int fuse_notify(struct fuse_conn
*fc
, enum fuse_notify_code code
,
1737 unsigned int size
, struct fuse_copy_state
*cs
)
1740 case FUSE_NOTIFY_POLL
:
1741 return fuse_notify_poll(fc
, size
, cs
);
1743 case FUSE_NOTIFY_INVAL_INODE
:
1744 return fuse_notify_inval_inode(fc
, size
, cs
);
1746 case FUSE_NOTIFY_INVAL_ENTRY
:
1747 return fuse_notify_inval_entry(fc
, size
, cs
);
1749 case FUSE_NOTIFY_STORE
:
1750 return fuse_notify_store(fc
, size
, cs
);
1752 case FUSE_NOTIFY_RETRIEVE
:
1753 return fuse_notify_retrieve(fc
, size
, cs
);
1755 case FUSE_NOTIFY_DELETE
:
1756 return fuse_notify_delete(fc
, size
, cs
);
1759 fuse_copy_finish(cs
);
1764 /* Look up request on processing list by unique ID */
1765 static struct fuse_req
*request_find(struct fuse_conn
*fc
, u64 unique
)
1767 struct list_head
*entry
;
1769 list_for_each(entry
, &fc
->processing
) {
1770 struct fuse_req
*req
;
1771 req
= list_entry(entry
, struct fuse_req
, list
);
1772 if (req
->in
.h
.unique
== unique
|| req
->intr_unique
== unique
)
1778 static int copy_out_args(struct fuse_copy_state
*cs
, struct fuse_out
*out
,
1781 unsigned reqsize
= sizeof(struct fuse_out_header
);
1784 return nbytes
!= reqsize
? -EINVAL
: 0;
1786 reqsize
+= len_args(out
->numargs
, out
->args
);
1788 if (reqsize
< nbytes
|| (reqsize
> nbytes
&& !out
->argvar
))
1790 else if (reqsize
> nbytes
) {
1791 struct fuse_arg
*lastarg
= &out
->args
[out
->numargs
-1];
1792 unsigned diffsize
= reqsize
- nbytes
;
1793 if (diffsize
> lastarg
->size
)
1795 lastarg
->size
-= diffsize
;
1797 return fuse_copy_args(cs
, out
->numargs
, out
->argpages
, out
->args
,
1802 * Write a single reply to a request. First the header is copied from
1803 * the write buffer. The request is then searched on the processing
1804 * list by the unique ID found in the header. If found, then remove
1805 * it from the list and copy the rest of the buffer to the request.
1806 * The request is finished by calling request_end()
1808 static ssize_t
fuse_dev_do_write(struct fuse_conn
*fc
,
1809 struct fuse_copy_state
*cs
, size_t nbytes
)
1812 struct fuse_req
*req
;
1813 struct fuse_out_header oh
;
1815 if (nbytes
< sizeof(struct fuse_out_header
))
1818 err
= fuse_copy_one(cs
, &oh
, sizeof(oh
));
1823 if (oh
.len
!= nbytes
)
1827 * Zero oh.unique indicates unsolicited notification message
1828 * and error contains notification code.
1831 err
= fuse_notify(fc
, oh
.error
, nbytes
- sizeof(oh
), cs
);
1832 return err
? err
: nbytes
;
1836 if (oh
.error
<= -1000 || oh
.error
> 0)
1839 spin_lock(&fc
->lock
);
1844 req
= request_find(fc
, oh
.unique
);
1849 spin_unlock(&fc
->lock
);
1850 fuse_copy_finish(cs
);
1851 spin_lock(&fc
->lock
);
1852 request_end(fc
, req
);
1855 /* Is it an interrupt reply? */
1856 if (req
->intr_unique
== oh
.unique
) {
1858 if (nbytes
!= sizeof(struct fuse_out_header
))
1861 if (oh
.error
== -ENOSYS
)
1862 fc
->no_interrupt
= 1;
1863 else if (oh
.error
== -EAGAIN
)
1864 queue_interrupt(fc
, req
);
1866 spin_unlock(&fc
->lock
);
1867 fuse_copy_finish(cs
);
1871 req
->state
= FUSE_REQ_WRITING
;
1872 list_move(&req
->list
, &fc
->io
);
1876 if (!req
->out
.page_replace
)
1878 spin_unlock(&fc
->lock
);
1880 err
= copy_out_args(cs
, &req
->out
, nbytes
);
1881 fuse_copy_finish(cs
);
1883 spin_lock(&fc
->lock
);
1888 } else if (!req
->aborted
)
1889 req
->out
.h
.error
= -EIO
;
1890 request_end(fc
, req
);
1892 return err
? err
: nbytes
;
1895 spin_unlock(&fc
->lock
);
1897 fuse_copy_finish(cs
);
1901 static ssize_t
fuse_dev_write(struct kiocb
*iocb
, const struct iovec
*iov
,
1902 unsigned long nr_segs
, loff_t pos
)
1904 struct fuse_copy_state cs
;
1905 struct fuse_conn
*fc
= fuse_get_conn(iocb
->ki_filp
);
1909 fuse_copy_init(&cs
, fc
, 0, iov
, nr_segs
);
1911 return fuse_dev_do_write(fc
, &cs
, iov_length(iov
, nr_segs
));
1914 static ssize_t
fuse_dev_splice_write(struct pipe_inode_info
*pipe
,
1915 struct file
*out
, loff_t
*ppos
,
1916 size_t len
, unsigned int flags
)
1920 struct pipe_buffer
*bufs
;
1921 struct fuse_copy_state cs
;
1922 struct fuse_conn
*fc
;
1926 fc
= fuse_get_conn(out
);
1930 bufs
= kmalloc(pipe
->buffers
* sizeof(struct pipe_buffer
), GFP_KERNEL
);
1937 for (idx
= 0; idx
< pipe
->nrbufs
&& rem
< len
; idx
++)
1938 rem
+= pipe
->bufs
[(pipe
->curbuf
+ idx
) & (pipe
->buffers
- 1)].len
;
1948 struct pipe_buffer
*ibuf
;
1949 struct pipe_buffer
*obuf
;
1951 BUG_ON(nbuf
>= pipe
->buffers
);
1952 BUG_ON(!pipe
->nrbufs
);
1953 ibuf
= &pipe
->bufs
[pipe
->curbuf
];
1956 if (rem
>= ibuf
->len
) {
1959 pipe
->curbuf
= (pipe
->curbuf
+ 1) & (pipe
->buffers
- 1);
1962 ibuf
->ops
->get(pipe
, ibuf
);
1964 obuf
->flags
&= ~PIPE_BUF_FLAG_GIFT
;
1966 ibuf
->offset
+= obuf
->len
;
1967 ibuf
->len
-= obuf
->len
;
1974 fuse_copy_init(&cs
, fc
, 0, NULL
, nbuf
);
1978 if (flags
& SPLICE_F_MOVE
)
1981 ret
= fuse_dev_do_write(fc
, &cs
, len
);
1983 for (idx
= 0; idx
< nbuf
; idx
++) {
1984 struct pipe_buffer
*buf
= &bufs
[idx
];
1985 buf
->ops
->release(pipe
, buf
);
1992 static unsigned fuse_dev_poll(struct file
*file
, poll_table
*wait
)
1994 unsigned mask
= POLLOUT
| POLLWRNORM
;
1995 struct fuse_conn
*fc
= fuse_get_conn(file
);
1999 poll_wait(file
, &fc
->waitq
, wait
);
2001 spin_lock(&fc
->lock
);
2004 else if (request_pending(fc
))
2005 mask
|= POLLIN
| POLLRDNORM
;
2006 spin_unlock(&fc
->lock
);
2012 * Abort all requests on the given list (pending or processing)
2014 * This function releases and reacquires fc->lock
2016 static void end_requests(struct fuse_conn
*fc
, struct list_head
*head
)
2017 __releases(fc
->lock
)
2018 __acquires(fc
->lock
)
2020 while (!list_empty(head
)) {
2021 struct fuse_req
*req
;
2022 req
= list_entry(head
->next
, struct fuse_req
, list
);
2023 req
->out
.h
.error
= -ECONNABORTED
;
2024 request_end(fc
, req
);
2025 spin_lock(&fc
->lock
);
2030 * Abort requests under I/O
2032 * The requests are set to aborted and finished, and the request
2033 * waiter is woken up. This will make request_wait_answer() wait
2034 * until the request is unlocked and then return.
2036 * If the request is asynchronous, then the end function needs to be
2037 * called after waiting for the request to be unlocked (if it was
2040 static void end_io_requests(struct fuse_conn
*fc
)
2041 __releases(fc
->lock
)
2042 __acquires(fc
->lock
)
2044 while (!list_empty(&fc
->io
)) {
2045 struct fuse_req
*req
=
2046 list_entry(fc
->io
.next
, struct fuse_req
, list
);
2047 void (*end
) (struct fuse_conn
*, struct fuse_req
*) = req
->end
;
2050 req
->out
.h
.error
= -ECONNABORTED
;
2051 req
->state
= FUSE_REQ_FINISHED
;
2052 list_del_init(&req
->list
);
2053 wake_up(&req
->waitq
);
2056 __fuse_get_request(req
);
2057 spin_unlock(&fc
->lock
);
2058 wait_event(req
->waitq
, !req
->locked
);
2060 fuse_put_request(fc
, req
);
2061 spin_lock(&fc
->lock
);
2066 static void end_queued_requests(struct fuse_conn
*fc
)
2067 __releases(fc
->lock
)
2068 __acquires(fc
->lock
)
2070 fc
->max_background
= UINT_MAX
;
2072 end_requests(fc
, &fc
->pending
);
2073 end_requests(fc
, &fc
->processing
);
2074 while (forget_pending(fc
))
2075 kfree(dequeue_forget(fc
, 1, NULL
));
2078 static void end_polls(struct fuse_conn
*fc
)
2082 p
= rb_first(&fc
->polled_files
);
2085 struct fuse_file
*ff
;
2086 ff
= rb_entry(p
, struct fuse_file
, polled_node
);
2087 wake_up_interruptible_all(&ff
->poll_wait
);
2094 * Abort all requests.
2096 * Emergency exit in case of a malicious or accidental deadlock, or
2097 * just a hung filesystem.
2099 * The same effect is usually achievable through killing the
2100 * filesystem daemon and all users of the filesystem. The exception
2101 * is the combination of an asynchronous request and the tricky
2102 * deadlock (see Documentation/filesystems/fuse.txt).
2104 * During the aborting, progression of requests from the pending and
2105 * processing lists onto the io list, and progression of new requests
2106 * onto the pending list is prevented by req->connected being false.
2108 * Progression of requests under I/O to the processing list is
2109 * prevented by the req->aborted flag being true for these requests.
2110 * For this reason requests on the io list must be aborted first.
2112 void fuse_abort_conn(struct fuse_conn
*fc
)
2114 spin_lock(&fc
->lock
);
2115 if (fc
->connected
) {
2118 fc
->initialized
= 1;
2119 end_io_requests(fc
);
2120 end_queued_requests(fc
);
2122 wake_up_all(&fc
->waitq
);
2123 wake_up_all(&fc
->blocked_waitq
);
2124 kill_fasync(&fc
->fasync
, SIGIO
, POLL_IN
);
2126 spin_unlock(&fc
->lock
);
2128 EXPORT_SYMBOL_GPL(fuse_abort_conn
);
2130 int fuse_dev_release(struct inode
*inode
, struct file
*file
)
2132 struct fuse_conn
*fc
= fuse_get_conn(file
);
2134 spin_lock(&fc
->lock
);
2137 fc
->initialized
= 1;
2138 end_queued_requests(fc
);
2140 wake_up_all(&fc
->blocked_waitq
);
2141 spin_unlock(&fc
->lock
);
2147 EXPORT_SYMBOL_GPL(fuse_dev_release
);
2149 static int fuse_dev_fasync(int fd
, struct file
*file
, int on
)
2151 struct fuse_conn
*fc
= fuse_get_conn(file
);
2155 /* No locking - fasync_helper does its own locking */
2156 return fasync_helper(fd
, file
, on
, &fc
->fasync
);
2159 const struct file_operations fuse_dev_operations
= {
2160 .owner
= THIS_MODULE
,
2161 .llseek
= no_llseek
,
2162 .read
= do_sync_read
,
2163 .aio_read
= fuse_dev_read
,
2164 .splice_read
= fuse_dev_splice_read
,
2165 .write
= do_sync_write
,
2166 .aio_write
= fuse_dev_write
,
2167 .splice_write
= fuse_dev_splice_write
,
2168 .poll
= fuse_dev_poll
,
2169 .release
= fuse_dev_release
,
2170 .fasync
= fuse_dev_fasync
,
2172 EXPORT_SYMBOL_GPL(fuse_dev_operations
);
2174 static struct miscdevice fuse_miscdevice
= {
2175 .minor
= FUSE_MINOR
,
2177 .fops
= &fuse_dev_operations
,
2180 int __init
fuse_dev_init(void)
2183 fuse_req_cachep
= kmem_cache_create("fuse_request",
2184 sizeof(struct fuse_req
),
2186 if (!fuse_req_cachep
)
2189 err
= misc_register(&fuse_miscdevice
);
2191 goto out_cache_clean
;
2196 kmem_cache_destroy(fuse_req_cachep
);
2201 void fuse_dev_cleanup(void)
2203 misc_deregister(&fuse_miscdevice
);
2204 kmem_cache_destroy(fuse_req_cachep
);