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>
22 #include <linux/aio.h>
24 MODULE_ALIAS_MISCDEV(FUSE_MINOR
);
25 MODULE_ALIAS("devname:fuse");
27 static struct kmem_cache
*fuse_req_cachep
;
29 static struct fuse_conn
*fuse_get_conn(struct file
*file
)
32 * Lockless access is OK, because file->private data is set
33 * once during mount and is valid until the file is released.
35 return file
->private_data
;
38 static void fuse_request_init(struct fuse_req
*req
, struct page
**pages
,
39 struct fuse_page_desc
*page_descs
,
42 memset(req
, 0, sizeof(*req
));
43 memset(pages
, 0, sizeof(*pages
) * npages
);
44 memset(page_descs
, 0, sizeof(*page_descs
) * npages
);
45 INIT_LIST_HEAD(&req
->list
);
46 INIT_LIST_HEAD(&req
->intr_entry
);
47 init_waitqueue_head(&req
->waitq
);
48 atomic_set(&req
->count
, 1);
50 req
->page_descs
= page_descs
;
51 req
->max_pages
= npages
;
54 static struct fuse_req
*__fuse_request_alloc(unsigned npages
, gfp_t flags
)
56 struct fuse_req
*req
= kmem_cache_alloc(fuse_req_cachep
, flags
);
59 struct fuse_page_desc
*page_descs
;
61 if (npages
<= FUSE_REQ_INLINE_PAGES
) {
62 pages
= req
->inline_pages
;
63 page_descs
= req
->inline_page_descs
;
65 pages
= kmalloc(sizeof(struct page
*) * npages
, flags
);
66 page_descs
= kmalloc(sizeof(struct fuse_page_desc
) *
70 if (!pages
|| !page_descs
) {
73 kmem_cache_free(fuse_req_cachep
, req
);
77 fuse_request_init(req
, pages
, page_descs
, npages
);
82 struct fuse_req
*fuse_request_alloc(unsigned npages
)
84 return __fuse_request_alloc(npages
, GFP_KERNEL
);
86 EXPORT_SYMBOL_GPL(fuse_request_alloc
);
88 struct fuse_req
*fuse_request_alloc_nofs(unsigned npages
)
90 return __fuse_request_alloc(npages
, GFP_NOFS
);
93 void fuse_request_free(struct fuse_req
*req
)
95 if (req
->pages
!= req
->inline_pages
) {
97 kfree(req
->page_descs
);
99 kmem_cache_free(fuse_req_cachep
, req
);
102 static void block_sigs(sigset_t
*oldset
)
106 siginitsetinv(&mask
, sigmask(SIGKILL
));
107 sigprocmask(SIG_BLOCK
, &mask
, oldset
);
110 static void restore_sigs(sigset_t
*oldset
)
112 sigprocmask(SIG_SETMASK
, oldset
, NULL
);
115 void __fuse_get_request(struct fuse_req
*req
)
117 atomic_inc(&req
->count
);
120 /* Must be called with > 1 refcount */
121 static void __fuse_put_request(struct fuse_req
*req
)
123 BUG_ON(atomic_read(&req
->count
) < 2);
124 atomic_dec(&req
->count
);
127 static void fuse_req_init_context(struct fuse_req
*req
)
129 req
->in
.h
.uid
= from_kuid_munged(&init_user_ns
, current_fsuid());
130 req
->in
.h
.gid
= from_kgid_munged(&init_user_ns
, current_fsgid());
131 req
->in
.h
.pid
= current
->pid
;
134 static bool fuse_block_alloc(struct fuse_conn
*fc
, bool for_background
)
136 return !fc
->initialized
|| (for_background
&& fc
->blocked
);
139 static struct fuse_req
*__fuse_get_req(struct fuse_conn
*fc
, unsigned npages
,
142 struct fuse_req
*req
;
144 atomic_inc(&fc
->num_waiting
);
146 if (fuse_block_alloc(fc
, for_background
)) {
151 intr
= wait_event_interruptible_exclusive(fc
->blocked_waitq
,
152 !fuse_block_alloc(fc
, for_background
));
153 restore_sigs(&oldset
);
163 req
= fuse_request_alloc(npages
);
167 wake_up(&fc
->blocked_waitq
);
171 fuse_req_init_context(req
);
173 req
->background
= for_background
;
177 atomic_dec(&fc
->num_waiting
);
181 struct fuse_req
*fuse_get_req(struct fuse_conn
*fc
, unsigned npages
)
183 return __fuse_get_req(fc
, npages
, false);
185 EXPORT_SYMBOL_GPL(fuse_get_req
);
187 struct fuse_req
*fuse_get_req_for_background(struct fuse_conn
*fc
,
190 return __fuse_get_req(fc
, npages
, true);
192 EXPORT_SYMBOL_GPL(fuse_get_req_for_background
);
195 * Return request in fuse_file->reserved_req. However that may
196 * currently be in use. If that is the case, wait for it to become
199 static struct fuse_req
*get_reserved_req(struct fuse_conn
*fc
,
202 struct fuse_req
*req
= NULL
;
203 struct fuse_file
*ff
= file
->private_data
;
206 wait_event(fc
->reserved_req_waitq
, ff
->reserved_req
);
207 spin_lock(&fc
->lock
);
208 if (ff
->reserved_req
) {
209 req
= ff
->reserved_req
;
210 ff
->reserved_req
= NULL
;
211 req
->stolen_file
= get_file(file
);
213 spin_unlock(&fc
->lock
);
220 * Put stolen request back into fuse_file->reserved_req
222 static void put_reserved_req(struct fuse_conn
*fc
, struct fuse_req
*req
)
224 struct file
*file
= req
->stolen_file
;
225 struct fuse_file
*ff
= file
->private_data
;
227 spin_lock(&fc
->lock
);
228 fuse_request_init(req
, req
->pages
, req
->page_descs
, req
->max_pages
);
229 BUG_ON(ff
->reserved_req
);
230 ff
->reserved_req
= req
;
231 wake_up_all(&fc
->reserved_req_waitq
);
232 spin_unlock(&fc
->lock
);
237 * Gets a requests for a file operation, always succeeds
239 * This is used for sending the FLUSH request, which must get to
240 * userspace, due to POSIX locks which may need to be unlocked.
242 * If allocation fails due to OOM, use the reserved request in
245 * This is very unlikely to deadlock accidentally, since the
246 * filesystem should not have it's own file open. If deadlock is
247 * intentional, it can still be broken by "aborting" the filesystem.
249 struct fuse_req
*fuse_get_req_nofail_nopages(struct fuse_conn
*fc
,
252 struct fuse_req
*req
;
254 atomic_inc(&fc
->num_waiting
);
255 wait_event(fc
->blocked_waitq
, fc
->initialized
);
256 req
= fuse_request_alloc(0);
258 req
= get_reserved_req(fc
, file
);
260 fuse_req_init_context(req
);
266 void fuse_put_request(struct fuse_conn
*fc
, struct fuse_req
*req
)
268 if (atomic_dec_and_test(&req
->count
)) {
269 if (unlikely(req
->background
)) {
271 * We get here in the unlikely case that a background
272 * request was allocated but not sent
274 spin_lock(&fc
->lock
);
276 wake_up(&fc
->blocked_waitq
);
277 spin_unlock(&fc
->lock
);
281 atomic_dec(&fc
->num_waiting
);
283 if (req
->stolen_file
)
284 put_reserved_req(fc
, req
);
286 fuse_request_free(req
);
289 EXPORT_SYMBOL_GPL(fuse_put_request
);
291 static unsigned len_args(unsigned numargs
, struct fuse_arg
*args
)
296 for (i
= 0; i
< numargs
; i
++)
297 nbytes
+= args
[i
].size
;
302 static u64
fuse_get_unique(struct fuse_conn
*fc
)
305 /* zero is special */
312 static void queue_request(struct fuse_conn
*fc
, struct fuse_req
*req
)
314 req
->in
.h
.len
= sizeof(struct fuse_in_header
) +
315 len_args(req
->in
.numargs
, (struct fuse_arg
*) req
->in
.args
);
316 list_add_tail(&req
->list
, &fc
->pending
);
317 req
->state
= FUSE_REQ_PENDING
;
320 atomic_inc(&fc
->num_waiting
);
323 kill_fasync(&fc
->fasync
, SIGIO
, POLL_IN
);
326 void fuse_queue_forget(struct fuse_conn
*fc
, struct fuse_forget_link
*forget
,
327 u64 nodeid
, u64 nlookup
)
329 forget
->forget_one
.nodeid
= nodeid
;
330 forget
->forget_one
.nlookup
= nlookup
;
332 spin_lock(&fc
->lock
);
334 fc
->forget_list_tail
->next
= forget
;
335 fc
->forget_list_tail
= forget
;
337 kill_fasync(&fc
->fasync
, SIGIO
, POLL_IN
);
341 spin_unlock(&fc
->lock
);
344 static void flush_bg_queue(struct fuse_conn
*fc
)
346 while (fc
->active_background
< fc
->max_background
&&
347 !list_empty(&fc
->bg_queue
)) {
348 struct fuse_req
*req
;
350 req
= list_entry(fc
->bg_queue
.next
, struct fuse_req
, list
);
351 list_del(&req
->list
);
352 fc
->active_background
++;
353 req
->in
.h
.unique
= fuse_get_unique(fc
);
354 queue_request(fc
, req
);
359 * This function is called when a request is finished. Either a reply
360 * has arrived or it was aborted (and not yet sent) or some error
361 * occurred during communication with userspace, or the device file
362 * was closed. The requester thread is woken up (if still waiting),
363 * the 'end' callback is called if given, else the reference to the
364 * request is released
366 * Called with fc->lock, unlocks it
368 static void request_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
371 void (*end
) (struct fuse_conn
*, struct fuse_req
*) = req
->end
;
373 list_del(&req
->list
);
374 list_del(&req
->intr_entry
);
375 req
->state
= FUSE_REQ_FINISHED
;
376 if (req
->background
) {
379 if (fc
->num_background
== fc
->max_background
)
382 /* Wake up next waiter, if any */
383 if (!fc
->blocked
&& waitqueue_active(&fc
->blocked_waitq
))
384 wake_up(&fc
->blocked_waitq
);
386 if (fc
->num_background
== fc
->congestion_threshold
&&
387 fc
->connected
&& fc
->bdi_initialized
) {
388 clear_bdi_congested(&fc
->bdi
, BLK_RW_SYNC
);
389 clear_bdi_congested(&fc
->bdi
, BLK_RW_ASYNC
);
391 fc
->num_background
--;
392 fc
->active_background
--;
395 spin_unlock(&fc
->lock
);
396 wake_up(&req
->waitq
);
399 fuse_put_request(fc
, req
);
402 static void wait_answer_interruptible(struct fuse_conn
*fc
,
403 struct fuse_req
*req
)
407 if (signal_pending(current
))
410 spin_unlock(&fc
->lock
);
411 wait_event_interruptible(req
->waitq
, req
->state
== FUSE_REQ_FINISHED
);
412 spin_lock(&fc
->lock
);
415 static void queue_interrupt(struct fuse_conn
*fc
, struct fuse_req
*req
)
417 list_add_tail(&req
->intr_entry
, &fc
->interrupts
);
419 kill_fasync(&fc
->fasync
, SIGIO
, POLL_IN
);
422 static void request_wait_answer(struct fuse_conn
*fc
, struct fuse_req
*req
)
426 if (!fc
->no_interrupt
) {
427 /* Any signal may interrupt this */
428 wait_answer_interruptible(fc
, req
);
432 if (req
->state
== FUSE_REQ_FINISHED
)
435 req
->interrupted
= 1;
436 if (req
->state
== FUSE_REQ_SENT
)
437 queue_interrupt(fc
, req
);
443 /* Only fatal signals may interrupt this */
445 wait_answer_interruptible(fc
, req
);
446 restore_sigs(&oldset
);
450 if (req
->state
== FUSE_REQ_FINISHED
)
453 /* Request is not yet in userspace, bail out */
454 if (req
->state
== FUSE_REQ_PENDING
) {
455 list_del(&req
->list
);
456 __fuse_put_request(req
);
457 req
->out
.h
.error
= -EINTR
;
463 * Either request is already in userspace, or it was forced.
466 spin_unlock(&fc
->lock
);
467 wait_event(req
->waitq
, req
->state
== FUSE_REQ_FINISHED
);
468 spin_lock(&fc
->lock
);
474 BUG_ON(req
->state
!= FUSE_REQ_FINISHED
);
476 /* This is uninterruptible sleep, because data is
477 being copied to/from the buffers of req. During
478 locked state, there mustn't be any filesystem
479 operation (e.g. page fault), since that could lead
481 spin_unlock(&fc
->lock
);
482 wait_event(req
->waitq
, !req
->locked
);
483 spin_lock(&fc
->lock
);
487 static void __fuse_request_send(struct fuse_conn
*fc
, struct fuse_req
*req
)
489 BUG_ON(req
->background
);
490 spin_lock(&fc
->lock
);
492 req
->out
.h
.error
= -ENOTCONN
;
493 else if (fc
->conn_error
)
494 req
->out
.h
.error
= -ECONNREFUSED
;
496 req
->in
.h
.unique
= fuse_get_unique(fc
);
497 queue_request(fc
, req
);
498 /* acquire extra reference, since request is still needed
499 after request_end() */
500 __fuse_get_request(req
);
502 request_wait_answer(fc
, req
);
504 spin_unlock(&fc
->lock
);
507 void fuse_request_send(struct fuse_conn
*fc
, struct fuse_req
*req
)
510 __fuse_request_send(fc
, req
);
512 EXPORT_SYMBOL_GPL(fuse_request_send
);
514 static void fuse_request_send_nowait_locked(struct fuse_conn
*fc
,
515 struct fuse_req
*req
)
517 BUG_ON(!req
->background
);
518 fc
->num_background
++;
519 if (fc
->num_background
== fc
->max_background
)
521 if (fc
->num_background
== fc
->congestion_threshold
&&
522 fc
->bdi_initialized
) {
523 set_bdi_congested(&fc
->bdi
, BLK_RW_SYNC
);
524 set_bdi_congested(&fc
->bdi
, BLK_RW_ASYNC
);
526 list_add_tail(&req
->list
, &fc
->bg_queue
);
530 static void fuse_request_send_nowait(struct fuse_conn
*fc
, struct fuse_req
*req
)
532 spin_lock(&fc
->lock
);
534 fuse_request_send_nowait_locked(fc
, req
);
535 spin_unlock(&fc
->lock
);
537 req
->out
.h
.error
= -ENOTCONN
;
538 request_end(fc
, req
);
542 void fuse_request_send_background(struct fuse_conn
*fc
, struct fuse_req
*req
)
545 fuse_request_send_nowait(fc
, req
);
547 EXPORT_SYMBOL_GPL(fuse_request_send_background
);
549 static int fuse_request_send_notify_reply(struct fuse_conn
*fc
,
550 struct fuse_req
*req
, u64 unique
)
555 req
->in
.h
.unique
= unique
;
556 spin_lock(&fc
->lock
);
558 queue_request(fc
, req
);
561 spin_unlock(&fc
->lock
);
567 * Called under fc->lock
569 * fc->connected must have been checked previously
571 void fuse_request_send_background_locked(struct fuse_conn
*fc
,
572 struct fuse_req
*req
)
575 fuse_request_send_nowait_locked(fc
, req
);
578 void fuse_force_forget(struct file
*file
, u64 nodeid
)
580 struct inode
*inode
= file_inode(file
);
581 struct fuse_conn
*fc
= get_fuse_conn(inode
);
582 struct fuse_req
*req
;
583 struct fuse_forget_in inarg
;
585 memset(&inarg
, 0, sizeof(inarg
));
587 req
= fuse_get_req_nofail_nopages(fc
, file
);
588 req
->in
.h
.opcode
= FUSE_FORGET
;
589 req
->in
.h
.nodeid
= nodeid
;
591 req
->in
.args
[0].size
= sizeof(inarg
);
592 req
->in
.args
[0].value
= &inarg
;
594 __fuse_request_send(fc
, req
);
596 fuse_put_request(fc
, req
);
600 * Lock the request. Up to the next unlock_request() there mustn't be
601 * anything that could cause a page-fault. If the request was already
604 static int lock_request(struct fuse_conn
*fc
, struct fuse_req
*req
)
608 spin_lock(&fc
->lock
);
613 spin_unlock(&fc
->lock
);
619 * Unlock request. If it was aborted during being locked, the
620 * requester thread is currently waiting for it to be unlocked, so
623 static void unlock_request(struct fuse_conn
*fc
, struct fuse_req
*req
)
626 spin_lock(&fc
->lock
);
629 wake_up(&req
->waitq
);
630 spin_unlock(&fc
->lock
);
634 struct fuse_copy_state
{
635 struct fuse_conn
*fc
;
637 struct fuse_req
*req
;
638 const struct iovec
*iov
;
639 struct pipe_buffer
*pipebufs
;
640 struct pipe_buffer
*currbuf
;
641 struct pipe_inode_info
*pipe
;
642 unsigned long nr_segs
;
643 unsigned long seglen
;
649 unsigned move_pages
:1;
652 static void fuse_copy_init(struct fuse_copy_state
*cs
, struct fuse_conn
*fc
,
654 const struct iovec
*iov
, unsigned long nr_segs
)
656 memset(cs
, 0, sizeof(*cs
));
660 cs
->nr_segs
= nr_segs
;
663 /* Unmap and put previous page of userspace buffer */
664 static void fuse_copy_finish(struct fuse_copy_state
*cs
)
667 struct pipe_buffer
*buf
= cs
->currbuf
;
670 buf
->ops
->unmap(cs
->pipe
, buf
, cs
->mapaddr
);
673 buf
->len
= PAGE_SIZE
- cs
->len
;
677 } else if (cs
->mapaddr
) {
680 flush_dcache_page(cs
->pg
);
681 set_page_dirty_lock(cs
->pg
);
689 * Get another pagefull of userspace buffer, and map it to kernel
690 * address space, and lock request
692 static int fuse_copy_fill(struct fuse_copy_state
*cs
)
694 unsigned long offset
;
697 unlock_request(cs
->fc
, cs
->req
);
698 fuse_copy_finish(cs
);
700 struct pipe_buffer
*buf
= cs
->pipebufs
;
703 err
= buf
->ops
->confirm(cs
->pipe
, buf
);
707 BUG_ON(!cs
->nr_segs
);
709 cs
->mapaddr
= buf
->ops
->map(cs
->pipe
, buf
, 0);
711 cs
->buf
= cs
->mapaddr
+ buf
->offset
;
717 if (cs
->nr_segs
== cs
->pipe
->buffers
)
720 page
= alloc_page(GFP_HIGHUSER
);
729 cs
->mapaddr
= kmap(page
);
730 cs
->buf
= cs
->mapaddr
;
737 BUG_ON(!cs
->nr_segs
);
738 cs
->seglen
= cs
->iov
[0].iov_len
;
739 cs
->addr
= (unsigned long) cs
->iov
[0].iov_base
;
743 err
= get_user_pages_fast(cs
->addr
, 1, cs
->write
, &cs
->pg
);
747 offset
= cs
->addr
% PAGE_SIZE
;
748 cs
->mapaddr
= kmap(cs
->pg
);
749 cs
->buf
= cs
->mapaddr
+ offset
;
750 cs
->len
= min(PAGE_SIZE
- offset
, cs
->seglen
);
751 cs
->seglen
-= cs
->len
;
755 return lock_request(cs
->fc
, cs
->req
);
758 /* Do as much copy to/from userspace buffer as we can */
759 static int fuse_copy_do(struct fuse_copy_state
*cs
, void **val
, unsigned *size
)
761 unsigned ncpy
= min(*size
, cs
->len
);
764 memcpy(cs
->buf
, *val
, ncpy
);
766 memcpy(*val
, cs
->buf
, ncpy
);
775 static int fuse_check_page(struct page
*page
)
777 if (page_mapcount(page
) ||
778 page
->mapping
!= NULL
||
779 page_count(page
) != 1 ||
780 (page
->flags
& PAGE_FLAGS_CHECK_AT_PREP
&
787 printk(KERN_WARNING
"fuse: trying to steal weird page\n");
788 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
);
794 static int fuse_try_move_page(struct fuse_copy_state
*cs
, struct page
**pagep
)
797 struct page
*oldpage
= *pagep
;
798 struct page
*newpage
;
799 struct pipe_buffer
*buf
= cs
->pipebufs
;
801 unlock_request(cs
->fc
, cs
->req
);
802 fuse_copy_finish(cs
);
804 err
= buf
->ops
->confirm(cs
->pipe
, buf
);
808 BUG_ON(!cs
->nr_segs
);
814 if (cs
->len
!= PAGE_SIZE
)
817 if (buf
->ops
->steal(cs
->pipe
, buf
) != 0)
822 if (WARN_ON(!PageUptodate(newpage
)))
825 ClearPageMappedToDisk(newpage
);
827 if (fuse_check_page(newpage
) != 0)
828 goto out_fallback_unlock
;
831 * This is a new and locked page, it shouldn't be mapped or
832 * have any special flags on it
834 if (WARN_ON(page_mapped(oldpage
)))
835 goto out_fallback_unlock
;
836 if (WARN_ON(page_has_private(oldpage
)))
837 goto out_fallback_unlock
;
838 if (WARN_ON(PageDirty(oldpage
) || PageWriteback(oldpage
)))
839 goto out_fallback_unlock
;
840 if (WARN_ON(PageMlocked(oldpage
)))
841 goto out_fallback_unlock
;
843 err
= replace_page_cache_page(oldpage
, newpage
, GFP_KERNEL
);
845 unlock_page(newpage
);
849 page_cache_get(newpage
);
851 if (!(buf
->flags
& PIPE_BUF_FLAG_LRU
))
852 lru_cache_add_file(newpage
);
855 spin_lock(&cs
->fc
->lock
);
856 if (cs
->req
->aborted
)
860 spin_unlock(&cs
->fc
->lock
);
863 unlock_page(newpage
);
864 page_cache_release(newpage
);
868 unlock_page(oldpage
);
869 page_cache_release(oldpage
);
875 unlock_page(newpage
);
877 cs
->mapaddr
= buf
->ops
->map(cs
->pipe
, buf
, 1);
878 cs
->buf
= cs
->mapaddr
+ buf
->offset
;
880 err
= lock_request(cs
->fc
, cs
->req
);
887 static int fuse_ref_page(struct fuse_copy_state
*cs
, struct page
*page
,
888 unsigned offset
, unsigned count
)
890 struct pipe_buffer
*buf
;
892 if (cs
->nr_segs
== cs
->pipe
->buffers
)
895 unlock_request(cs
->fc
, cs
->req
);
896 fuse_copy_finish(cs
);
899 page_cache_get(page
);
901 buf
->offset
= offset
;
912 * Copy a page in the request to/from the userspace buffer. Must be
915 static int fuse_copy_page(struct fuse_copy_state
*cs
, struct page
**pagep
,
916 unsigned offset
, unsigned count
, int zeroing
)
919 struct page
*page
= *pagep
;
921 if (page
&& zeroing
&& count
< PAGE_SIZE
)
922 clear_highpage(page
);
925 if (cs
->write
&& cs
->pipebufs
&& page
) {
926 return fuse_ref_page(cs
, page
, offset
, count
);
927 } else if (!cs
->len
) {
928 if (cs
->move_pages
&& page
&&
929 offset
== 0 && count
== PAGE_SIZE
) {
930 err
= fuse_try_move_page(cs
, pagep
);
934 err
= fuse_copy_fill(cs
);
940 void *mapaddr
= kmap_atomic(page
);
941 void *buf
= mapaddr
+ offset
;
942 offset
+= fuse_copy_do(cs
, &buf
, &count
);
943 kunmap_atomic(mapaddr
);
945 offset
+= fuse_copy_do(cs
, NULL
, &count
);
947 if (page
&& !cs
->write
)
948 flush_dcache_page(page
);
952 /* Copy pages in the request to/from userspace buffer */
953 static int fuse_copy_pages(struct fuse_copy_state
*cs
, unsigned nbytes
,
957 struct fuse_req
*req
= cs
->req
;
959 for (i
= 0; i
< req
->num_pages
&& (nbytes
|| zeroing
); i
++) {
961 unsigned offset
= req
->page_descs
[i
].offset
;
962 unsigned count
= min(nbytes
, req
->page_descs
[i
].length
);
964 err
= fuse_copy_page(cs
, &req
->pages
[i
], offset
, count
,
974 /* Copy a single argument in the request to/from userspace buffer */
975 static int fuse_copy_one(struct fuse_copy_state
*cs
, void *val
, unsigned size
)
979 int err
= fuse_copy_fill(cs
);
983 fuse_copy_do(cs
, &val
, &size
);
988 /* Copy request arguments to/from userspace buffer */
989 static int fuse_copy_args(struct fuse_copy_state
*cs
, unsigned numargs
,
990 unsigned argpages
, struct fuse_arg
*args
,
996 for (i
= 0; !err
&& i
< numargs
; i
++) {
997 struct fuse_arg
*arg
= &args
[i
];
998 if (i
== numargs
- 1 && argpages
)
999 err
= fuse_copy_pages(cs
, arg
->size
, zeroing
);
1001 err
= fuse_copy_one(cs
, arg
->value
, arg
->size
);
1006 static int forget_pending(struct fuse_conn
*fc
)
1008 return fc
->forget_list_head
.next
!= NULL
;
1011 static int request_pending(struct fuse_conn
*fc
)
1013 return !list_empty(&fc
->pending
) || !list_empty(&fc
->interrupts
) ||
1017 /* Wait until a request is available on the pending list */
1018 static void request_wait(struct fuse_conn
*fc
)
1019 __releases(fc
->lock
)
1020 __acquires(fc
->lock
)
1022 DECLARE_WAITQUEUE(wait
, current
);
1024 add_wait_queue_exclusive(&fc
->waitq
, &wait
);
1025 while (fc
->connected
&& !request_pending(fc
)) {
1026 set_current_state(TASK_INTERRUPTIBLE
);
1027 if (signal_pending(current
))
1030 spin_unlock(&fc
->lock
);
1032 spin_lock(&fc
->lock
);
1034 set_current_state(TASK_RUNNING
);
1035 remove_wait_queue(&fc
->waitq
, &wait
);
1039 * Transfer an interrupt request to userspace
1041 * Unlike other requests this is assembled on demand, without a need
1042 * to allocate a separate fuse_req structure.
1044 * Called with fc->lock held, releases it
1046 static int fuse_read_interrupt(struct fuse_conn
*fc
, struct fuse_copy_state
*cs
,
1047 size_t nbytes
, struct fuse_req
*req
)
1048 __releases(fc
->lock
)
1050 struct fuse_in_header ih
;
1051 struct fuse_interrupt_in arg
;
1052 unsigned reqsize
= sizeof(ih
) + sizeof(arg
);
1055 list_del_init(&req
->intr_entry
);
1056 req
->intr_unique
= fuse_get_unique(fc
);
1057 memset(&ih
, 0, sizeof(ih
));
1058 memset(&arg
, 0, sizeof(arg
));
1060 ih
.opcode
= FUSE_INTERRUPT
;
1061 ih
.unique
= req
->intr_unique
;
1062 arg
.unique
= req
->in
.h
.unique
;
1064 spin_unlock(&fc
->lock
);
1065 if (nbytes
< reqsize
)
1068 err
= fuse_copy_one(cs
, &ih
, sizeof(ih
));
1070 err
= fuse_copy_one(cs
, &arg
, sizeof(arg
));
1071 fuse_copy_finish(cs
);
1073 return err
? err
: reqsize
;
1076 static struct fuse_forget_link
*dequeue_forget(struct fuse_conn
*fc
,
1080 struct fuse_forget_link
*head
= fc
->forget_list_head
.next
;
1081 struct fuse_forget_link
**newhead
= &head
;
1084 for (count
= 0; *newhead
!= NULL
&& count
< max
; count
++)
1085 newhead
= &(*newhead
)->next
;
1087 fc
->forget_list_head
.next
= *newhead
;
1089 if (fc
->forget_list_head
.next
== NULL
)
1090 fc
->forget_list_tail
= &fc
->forget_list_head
;
1098 static int fuse_read_single_forget(struct fuse_conn
*fc
,
1099 struct fuse_copy_state
*cs
,
1101 __releases(fc
->lock
)
1104 struct fuse_forget_link
*forget
= dequeue_forget(fc
, 1, NULL
);
1105 struct fuse_forget_in arg
= {
1106 .nlookup
= forget
->forget_one
.nlookup
,
1108 struct fuse_in_header ih
= {
1109 .opcode
= FUSE_FORGET
,
1110 .nodeid
= forget
->forget_one
.nodeid
,
1111 .unique
= fuse_get_unique(fc
),
1112 .len
= sizeof(ih
) + sizeof(arg
),
1115 spin_unlock(&fc
->lock
);
1117 if (nbytes
< ih
.len
)
1120 err
= fuse_copy_one(cs
, &ih
, sizeof(ih
));
1122 err
= fuse_copy_one(cs
, &arg
, sizeof(arg
));
1123 fuse_copy_finish(cs
);
1131 static int fuse_read_batch_forget(struct fuse_conn
*fc
,
1132 struct fuse_copy_state
*cs
, size_t nbytes
)
1133 __releases(fc
->lock
)
1136 unsigned max_forgets
;
1138 struct fuse_forget_link
*head
;
1139 struct fuse_batch_forget_in arg
= { .count
= 0 };
1140 struct fuse_in_header ih
= {
1141 .opcode
= FUSE_BATCH_FORGET
,
1142 .unique
= fuse_get_unique(fc
),
1143 .len
= sizeof(ih
) + sizeof(arg
),
1146 if (nbytes
< ih
.len
) {
1147 spin_unlock(&fc
->lock
);
1151 max_forgets
= (nbytes
- ih
.len
) / sizeof(struct fuse_forget_one
);
1152 head
= dequeue_forget(fc
, max_forgets
, &count
);
1153 spin_unlock(&fc
->lock
);
1156 ih
.len
+= count
* sizeof(struct fuse_forget_one
);
1157 err
= fuse_copy_one(cs
, &ih
, sizeof(ih
));
1159 err
= fuse_copy_one(cs
, &arg
, sizeof(arg
));
1162 struct fuse_forget_link
*forget
= head
;
1165 err
= fuse_copy_one(cs
, &forget
->forget_one
,
1166 sizeof(forget
->forget_one
));
1168 head
= forget
->next
;
1172 fuse_copy_finish(cs
);
1180 static int fuse_read_forget(struct fuse_conn
*fc
, struct fuse_copy_state
*cs
,
1182 __releases(fc
->lock
)
1184 if (fc
->minor
< 16 || fc
->forget_list_head
.next
->next
== NULL
)
1185 return fuse_read_single_forget(fc
, cs
, nbytes
);
1187 return fuse_read_batch_forget(fc
, cs
, nbytes
);
1191 * Read a single request into the userspace filesystem's buffer. This
1192 * function waits until a request is available, then removes it from
1193 * the pending list and copies request data to userspace buffer. If
1194 * no reply is needed (FORGET) or request has been aborted or there
1195 * was an error during the copying then it's finished by calling
1196 * request_end(). Otherwise add it to the processing list, and set
1199 static ssize_t
fuse_dev_do_read(struct fuse_conn
*fc
, struct file
*file
,
1200 struct fuse_copy_state
*cs
, size_t nbytes
)
1203 struct fuse_req
*req
;
1208 spin_lock(&fc
->lock
);
1210 if ((file
->f_flags
& O_NONBLOCK
) && fc
->connected
&&
1211 !request_pending(fc
))
1219 if (!request_pending(fc
))
1222 if (!list_empty(&fc
->interrupts
)) {
1223 req
= list_entry(fc
->interrupts
.next
, struct fuse_req
,
1225 return fuse_read_interrupt(fc
, cs
, nbytes
, req
);
1228 if (forget_pending(fc
)) {
1229 if (list_empty(&fc
->pending
) || fc
->forget_batch
-- > 0)
1230 return fuse_read_forget(fc
, cs
, nbytes
);
1232 if (fc
->forget_batch
<= -8)
1233 fc
->forget_batch
= 16;
1236 req
= list_entry(fc
->pending
.next
, struct fuse_req
, list
);
1237 req
->state
= FUSE_REQ_READING
;
1238 list_move(&req
->list
, &fc
->io
);
1241 reqsize
= in
->h
.len
;
1242 /* If request is too large, reply with an error and restart the read */
1243 if (nbytes
< reqsize
) {
1244 req
->out
.h
.error
= -EIO
;
1245 /* SETXATTR is special, since it may contain too large data */
1246 if (in
->h
.opcode
== FUSE_SETXATTR
)
1247 req
->out
.h
.error
= -E2BIG
;
1248 request_end(fc
, req
);
1251 spin_unlock(&fc
->lock
);
1253 err
= fuse_copy_one(cs
, &in
->h
, sizeof(in
->h
));
1255 err
= fuse_copy_args(cs
, in
->numargs
, in
->argpages
,
1256 (struct fuse_arg
*) in
->args
, 0);
1257 fuse_copy_finish(cs
);
1258 spin_lock(&fc
->lock
);
1261 request_end(fc
, req
);
1265 req
->out
.h
.error
= -EIO
;
1266 request_end(fc
, req
);
1270 request_end(fc
, req
);
1272 req
->state
= FUSE_REQ_SENT
;
1273 list_move_tail(&req
->list
, &fc
->processing
);
1274 if (req
->interrupted
)
1275 queue_interrupt(fc
, req
);
1276 spin_unlock(&fc
->lock
);
1281 spin_unlock(&fc
->lock
);
1285 static ssize_t
fuse_dev_read(struct kiocb
*iocb
, const struct iovec
*iov
,
1286 unsigned long nr_segs
, loff_t pos
)
1288 struct fuse_copy_state cs
;
1289 struct file
*file
= iocb
->ki_filp
;
1290 struct fuse_conn
*fc
= fuse_get_conn(file
);
1294 fuse_copy_init(&cs
, fc
, 1, iov
, nr_segs
);
1296 return fuse_dev_do_read(fc
, file
, &cs
, iov_length(iov
, nr_segs
));
1299 static int fuse_dev_pipe_buf_steal(struct pipe_inode_info
*pipe
,
1300 struct pipe_buffer
*buf
)
1305 static const struct pipe_buf_operations fuse_dev_pipe_buf_ops
= {
1307 .map
= generic_pipe_buf_map
,
1308 .unmap
= generic_pipe_buf_unmap
,
1309 .confirm
= generic_pipe_buf_confirm
,
1310 .release
= generic_pipe_buf_release
,
1311 .steal
= fuse_dev_pipe_buf_steal
,
1312 .get
= generic_pipe_buf_get
,
1315 static ssize_t
fuse_dev_splice_read(struct file
*in
, loff_t
*ppos
,
1316 struct pipe_inode_info
*pipe
,
1317 size_t len
, unsigned int flags
)
1322 struct pipe_buffer
*bufs
;
1323 struct fuse_copy_state cs
;
1324 struct fuse_conn
*fc
= fuse_get_conn(in
);
1328 bufs
= kmalloc(pipe
->buffers
* sizeof(struct pipe_buffer
), GFP_KERNEL
);
1332 fuse_copy_init(&cs
, fc
, 1, NULL
, 0);
1335 ret
= fuse_dev_do_read(fc
, in
, &cs
, len
);
1342 if (!pipe
->readers
) {
1343 send_sig(SIGPIPE
, current
, 0);
1349 if (pipe
->nrbufs
+ cs
.nr_segs
> pipe
->buffers
) {
1354 while (page_nr
< cs
.nr_segs
) {
1355 int newbuf
= (pipe
->curbuf
+ pipe
->nrbufs
) & (pipe
->buffers
- 1);
1356 struct pipe_buffer
*buf
= pipe
->bufs
+ newbuf
;
1358 buf
->page
= bufs
[page_nr
].page
;
1359 buf
->offset
= bufs
[page_nr
].offset
;
1360 buf
->len
= bufs
[page_nr
].len
;
1361 buf
->ops
= &fuse_dev_pipe_buf_ops
;
1376 if (waitqueue_active(&pipe
->wait
))
1377 wake_up_interruptible(&pipe
->wait
);
1378 kill_fasync(&pipe
->fasync_readers
, SIGIO
, POLL_IN
);
1382 for (; page_nr
< cs
.nr_segs
; page_nr
++)
1383 page_cache_release(bufs
[page_nr
].page
);
1389 static int fuse_notify_poll(struct fuse_conn
*fc
, unsigned int size
,
1390 struct fuse_copy_state
*cs
)
1392 struct fuse_notify_poll_wakeup_out outarg
;
1395 if (size
!= sizeof(outarg
))
1398 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1402 fuse_copy_finish(cs
);
1403 return fuse_notify_poll_wakeup(fc
, &outarg
);
1406 fuse_copy_finish(cs
);
1410 static int fuse_notify_inval_inode(struct fuse_conn
*fc
, unsigned int size
,
1411 struct fuse_copy_state
*cs
)
1413 struct fuse_notify_inval_inode_out outarg
;
1416 if (size
!= sizeof(outarg
))
1419 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1422 fuse_copy_finish(cs
);
1424 down_read(&fc
->killsb
);
1427 err
= fuse_reverse_inval_inode(fc
->sb
, outarg
.ino
,
1428 outarg
.off
, outarg
.len
);
1430 up_read(&fc
->killsb
);
1434 fuse_copy_finish(cs
);
1438 static int fuse_notify_inval_entry(struct fuse_conn
*fc
, unsigned int size
,
1439 struct fuse_copy_state
*cs
)
1441 struct fuse_notify_inval_entry_out outarg
;
1446 buf
= kzalloc(FUSE_NAME_MAX
+ 1, GFP_KERNEL
);
1451 if (size
< sizeof(outarg
))
1454 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1458 err
= -ENAMETOOLONG
;
1459 if (outarg
.namelen
> FUSE_NAME_MAX
)
1463 if (size
!= sizeof(outarg
) + outarg
.namelen
+ 1)
1467 name
.len
= outarg
.namelen
;
1468 err
= fuse_copy_one(cs
, buf
, outarg
.namelen
+ 1);
1471 fuse_copy_finish(cs
);
1472 buf
[outarg
.namelen
] = 0;
1473 name
.hash
= full_name_hash(name
.name
, name
.len
);
1475 down_read(&fc
->killsb
);
1478 err
= fuse_reverse_inval_entry(fc
->sb
, outarg
.parent
, 0, &name
);
1479 up_read(&fc
->killsb
);
1485 fuse_copy_finish(cs
);
1489 static int fuse_notify_delete(struct fuse_conn
*fc
, unsigned int size
,
1490 struct fuse_copy_state
*cs
)
1492 struct fuse_notify_delete_out outarg
;
1497 buf
= kzalloc(FUSE_NAME_MAX
+ 1, GFP_KERNEL
);
1502 if (size
< sizeof(outarg
))
1505 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1509 err
= -ENAMETOOLONG
;
1510 if (outarg
.namelen
> FUSE_NAME_MAX
)
1514 if (size
!= sizeof(outarg
) + outarg
.namelen
+ 1)
1518 name
.len
= outarg
.namelen
;
1519 err
= fuse_copy_one(cs
, buf
, outarg
.namelen
+ 1);
1522 fuse_copy_finish(cs
);
1523 buf
[outarg
.namelen
] = 0;
1524 name
.hash
= full_name_hash(name
.name
, name
.len
);
1526 down_read(&fc
->killsb
);
1529 err
= fuse_reverse_inval_entry(fc
->sb
, outarg
.parent
,
1530 outarg
.child
, &name
);
1531 up_read(&fc
->killsb
);
1537 fuse_copy_finish(cs
);
1541 static int fuse_notify_store(struct fuse_conn
*fc
, unsigned int size
,
1542 struct fuse_copy_state
*cs
)
1544 struct fuse_notify_store_out outarg
;
1545 struct inode
*inode
;
1546 struct address_space
*mapping
;
1550 unsigned int offset
;
1556 if (size
< sizeof(outarg
))
1559 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1564 if (size
- sizeof(outarg
) != outarg
.size
)
1567 nodeid
= outarg
.nodeid
;
1569 down_read(&fc
->killsb
);
1575 inode
= ilookup5(fc
->sb
, nodeid
, fuse_inode_eq
, &nodeid
);
1579 mapping
= inode
->i_mapping
;
1580 index
= outarg
.offset
>> PAGE_CACHE_SHIFT
;
1581 offset
= outarg
.offset
& ~PAGE_CACHE_MASK
;
1582 file_size
= i_size_read(inode
);
1583 end
= outarg
.offset
+ outarg
.size
;
1584 if (end
> file_size
) {
1586 fuse_write_update_size(inode
, file_size
);
1592 unsigned int this_num
;
1595 page
= find_or_create_page(mapping
, index
,
1596 mapping_gfp_mask(mapping
));
1600 this_num
= min_t(unsigned, num
, PAGE_CACHE_SIZE
- offset
);
1601 err
= fuse_copy_page(cs
, &page
, offset
, this_num
, 0);
1602 if (!err
&& offset
== 0 && (num
!= 0 || file_size
== end
))
1603 SetPageUptodate(page
);
1605 page_cache_release(page
);
1620 up_read(&fc
->killsb
);
1622 fuse_copy_finish(cs
);
1626 static void fuse_retrieve_end(struct fuse_conn
*fc
, struct fuse_req
*req
)
1628 release_pages(req
->pages
, req
->num_pages
, 0);
1631 static int fuse_retrieve(struct fuse_conn
*fc
, struct inode
*inode
,
1632 struct fuse_notify_retrieve_out
*outarg
)
1635 struct address_space
*mapping
= inode
->i_mapping
;
1636 struct fuse_req
*req
;
1640 unsigned int offset
;
1641 size_t total_len
= 0;
1644 offset
= outarg
->offset
& ~PAGE_CACHE_MASK
;
1645 file_size
= i_size_read(inode
);
1648 if (outarg
->offset
> file_size
)
1650 else if (outarg
->offset
+ num
> file_size
)
1651 num
= file_size
- outarg
->offset
;
1653 num_pages
= (num
+ offset
+ PAGE_SIZE
- 1) >> PAGE_SHIFT
;
1654 num_pages
= min(num_pages
, FUSE_MAX_PAGES_PER_REQ
);
1656 req
= fuse_get_req(fc
, num_pages
);
1658 return PTR_ERR(req
);
1660 req
->in
.h
.opcode
= FUSE_NOTIFY_REPLY
;
1661 req
->in
.h
.nodeid
= outarg
->nodeid
;
1662 req
->in
.numargs
= 2;
1663 req
->in
.argpages
= 1;
1664 req
->page_descs
[0].offset
= offset
;
1665 req
->end
= fuse_retrieve_end
;
1667 index
= outarg
->offset
>> PAGE_CACHE_SHIFT
;
1669 while (num
&& req
->num_pages
< num_pages
) {
1671 unsigned int this_num
;
1673 page
= find_get_page(mapping
, index
);
1677 this_num
= min_t(unsigned, num
, PAGE_CACHE_SIZE
- offset
);
1678 req
->pages
[req
->num_pages
] = page
;
1679 req
->page_descs
[req
->num_pages
].length
= this_num
;
1684 total_len
+= this_num
;
1687 req
->misc
.retrieve_in
.offset
= outarg
->offset
;
1688 req
->misc
.retrieve_in
.size
= total_len
;
1689 req
->in
.args
[0].size
= sizeof(req
->misc
.retrieve_in
);
1690 req
->in
.args
[0].value
= &req
->misc
.retrieve_in
;
1691 req
->in
.args
[1].size
= total_len
;
1693 err
= fuse_request_send_notify_reply(fc
, req
, outarg
->notify_unique
);
1695 fuse_retrieve_end(fc
, req
);
1700 static int fuse_notify_retrieve(struct fuse_conn
*fc
, unsigned int size
,
1701 struct fuse_copy_state
*cs
)
1703 struct fuse_notify_retrieve_out outarg
;
1704 struct inode
*inode
;
1708 if (size
!= sizeof(outarg
))
1711 err
= fuse_copy_one(cs
, &outarg
, sizeof(outarg
));
1715 fuse_copy_finish(cs
);
1717 down_read(&fc
->killsb
);
1720 u64 nodeid
= outarg
.nodeid
;
1722 inode
= ilookup5(fc
->sb
, nodeid
, fuse_inode_eq
, &nodeid
);
1724 err
= fuse_retrieve(fc
, inode
, &outarg
);
1728 up_read(&fc
->killsb
);
1733 fuse_copy_finish(cs
);
1737 static int fuse_notify(struct fuse_conn
*fc
, enum fuse_notify_code code
,
1738 unsigned int size
, struct fuse_copy_state
*cs
)
1741 case FUSE_NOTIFY_POLL
:
1742 return fuse_notify_poll(fc
, size
, cs
);
1744 case FUSE_NOTIFY_INVAL_INODE
:
1745 return fuse_notify_inval_inode(fc
, size
, cs
);
1747 case FUSE_NOTIFY_INVAL_ENTRY
:
1748 return fuse_notify_inval_entry(fc
, size
, cs
);
1750 case FUSE_NOTIFY_STORE
:
1751 return fuse_notify_store(fc
, size
, cs
);
1753 case FUSE_NOTIFY_RETRIEVE
:
1754 return fuse_notify_retrieve(fc
, size
, cs
);
1756 case FUSE_NOTIFY_DELETE
:
1757 return fuse_notify_delete(fc
, size
, cs
);
1760 fuse_copy_finish(cs
);
1765 /* Look up request on processing list by unique ID */
1766 static struct fuse_req
*request_find(struct fuse_conn
*fc
, u64 unique
)
1768 struct fuse_req
*req
;
1770 list_for_each_entry(req
, &fc
->processing
, list
) {
1771 if (req
->in
.h
.unique
== unique
|| req
->intr_unique
== unique
)
1777 static int copy_out_args(struct fuse_copy_state
*cs
, struct fuse_out
*out
,
1780 unsigned reqsize
= sizeof(struct fuse_out_header
);
1783 return nbytes
!= reqsize
? -EINVAL
: 0;
1785 reqsize
+= len_args(out
->numargs
, out
->args
);
1787 if (reqsize
< nbytes
|| (reqsize
> nbytes
&& !out
->argvar
))
1789 else if (reqsize
> nbytes
) {
1790 struct fuse_arg
*lastarg
= &out
->args
[out
->numargs
-1];
1791 unsigned diffsize
= reqsize
- nbytes
;
1792 if (diffsize
> lastarg
->size
)
1794 lastarg
->size
-= diffsize
;
1796 return fuse_copy_args(cs
, out
->numargs
, out
->argpages
, out
->args
,
1801 * Write a single reply to a request. First the header is copied from
1802 * the write buffer. The request is then searched on the processing
1803 * list by the unique ID found in the header. If found, then remove
1804 * it from the list and copy the rest of the buffer to the request.
1805 * The request is finished by calling request_end()
1807 static ssize_t
fuse_dev_do_write(struct fuse_conn
*fc
,
1808 struct fuse_copy_state
*cs
, size_t nbytes
)
1811 struct fuse_req
*req
;
1812 struct fuse_out_header oh
;
1814 if (nbytes
< sizeof(struct fuse_out_header
))
1817 err
= fuse_copy_one(cs
, &oh
, sizeof(oh
));
1822 if (oh
.len
!= nbytes
)
1826 * Zero oh.unique indicates unsolicited notification message
1827 * and error contains notification code.
1830 err
= fuse_notify(fc
, oh
.error
, nbytes
- sizeof(oh
), cs
);
1831 return err
? err
: nbytes
;
1835 if (oh
.error
<= -1000 || oh
.error
> 0)
1838 spin_lock(&fc
->lock
);
1843 req
= request_find(fc
, oh
.unique
);
1848 spin_unlock(&fc
->lock
);
1849 fuse_copy_finish(cs
);
1850 spin_lock(&fc
->lock
);
1851 request_end(fc
, req
);
1854 /* Is it an interrupt reply? */
1855 if (req
->intr_unique
== oh
.unique
) {
1857 if (nbytes
!= sizeof(struct fuse_out_header
))
1860 if (oh
.error
== -ENOSYS
)
1861 fc
->no_interrupt
= 1;
1862 else if (oh
.error
== -EAGAIN
)
1863 queue_interrupt(fc
, req
);
1865 spin_unlock(&fc
->lock
);
1866 fuse_copy_finish(cs
);
1870 req
->state
= FUSE_REQ_WRITING
;
1871 list_move(&req
->list
, &fc
->io
);
1875 if (!req
->out
.page_replace
)
1877 spin_unlock(&fc
->lock
);
1879 err
= copy_out_args(cs
, &req
->out
, nbytes
);
1880 fuse_copy_finish(cs
);
1882 spin_lock(&fc
->lock
);
1887 } else if (!req
->aborted
)
1888 req
->out
.h
.error
= -EIO
;
1889 request_end(fc
, req
);
1891 return err
? err
: nbytes
;
1894 spin_unlock(&fc
->lock
);
1896 fuse_copy_finish(cs
);
1900 static ssize_t
fuse_dev_write(struct kiocb
*iocb
, const struct iovec
*iov
,
1901 unsigned long nr_segs
, loff_t pos
)
1903 struct fuse_copy_state cs
;
1904 struct fuse_conn
*fc
= fuse_get_conn(iocb
->ki_filp
);
1908 fuse_copy_init(&cs
, fc
, 0, iov
, nr_segs
);
1910 return fuse_dev_do_write(fc
, &cs
, iov_length(iov
, nr_segs
));
1913 static ssize_t
fuse_dev_splice_write(struct pipe_inode_info
*pipe
,
1914 struct file
*out
, loff_t
*ppos
,
1915 size_t len
, unsigned int flags
)
1919 struct pipe_buffer
*bufs
;
1920 struct fuse_copy_state cs
;
1921 struct fuse_conn
*fc
;
1925 fc
= fuse_get_conn(out
);
1929 bufs
= kmalloc(pipe
->buffers
* sizeof(struct pipe_buffer
), GFP_KERNEL
);
1936 for (idx
= 0; idx
< pipe
->nrbufs
&& rem
< len
; idx
++)
1937 rem
+= pipe
->bufs
[(pipe
->curbuf
+ idx
) & (pipe
->buffers
- 1)].len
;
1947 struct pipe_buffer
*ibuf
;
1948 struct pipe_buffer
*obuf
;
1950 BUG_ON(nbuf
>= pipe
->buffers
);
1951 BUG_ON(!pipe
->nrbufs
);
1952 ibuf
= &pipe
->bufs
[pipe
->curbuf
];
1955 if (rem
>= ibuf
->len
) {
1958 pipe
->curbuf
= (pipe
->curbuf
+ 1) & (pipe
->buffers
- 1);
1961 ibuf
->ops
->get(pipe
, ibuf
);
1963 obuf
->flags
&= ~PIPE_BUF_FLAG_GIFT
;
1965 ibuf
->offset
+= obuf
->len
;
1966 ibuf
->len
-= obuf
->len
;
1973 fuse_copy_init(&cs
, fc
, 0, NULL
, nbuf
);
1977 if (flags
& SPLICE_F_MOVE
)
1980 ret
= fuse_dev_do_write(fc
, &cs
, len
);
1982 for (idx
= 0; idx
< nbuf
; idx
++) {
1983 struct pipe_buffer
*buf
= &bufs
[idx
];
1984 buf
->ops
->release(pipe
, buf
);
1991 static unsigned fuse_dev_poll(struct file
*file
, poll_table
*wait
)
1993 unsigned mask
= POLLOUT
| POLLWRNORM
;
1994 struct fuse_conn
*fc
= fuse_get_conn(file
);
1998 poll_wait(file
, &fc
->waitq
, wait
);
2000 spin_lock(&fc
->lock
);
2003 else if (request_pending(fc
))
2004 mask
|= POLLIN
| POLLRDNORM
;
2005 spin_unlock(&fc
->lock
);
2011 * Abort all requests on the given list (pending or processing)
2013 * This function releases and reacquires fc->lock
2015 static void end_requests(struct fuse_conn
*fc
, struct list_head
*head
)
2016 __releases(fc
->lock
)
2017 __acquires(fc
->lock
)
2019 while (!list_empty(head
)) {
2020 struct fuse_req
*req
;
2021 req
= list_entry(head
->next
, struct fuse_req
, list
);
2022 req
->out
.h
.error
= -ECONNABORTED
;
2023 request_end(fc
, req
);
2024 spin_lock(&fc
->lock
);
2029 * Abort requests under I/O
2031 * The requests are set to aborted and finished, and the request
2032 * waiter is woken up. This will make request_wait_answer() wait
2033 * until the request is unlocked and then return.
2035 * If the request is asynchronous, then the end function needs to be
2036 * called after waiting for the request to be unlocked (if it was
2039 static void end_io_requests(struct fuse_conn
*fc
)
2040 __releases(fc
->lock
)
2041 __acquires(fc
->lock
)
2043 while (!list_empty(&fc
->io
)) {
2044 struct fuse_req
*req
=
2045 list_entry(fc
->io
.next
, struct fuse_req
, list
);
2046 void (*end
) (struct fuse_conn
*, struct fuse_req
*) = req
->end
;
2049 req
->out
.h
.error
= -ECONNABORTED
;
2050 req
->state
= FUSE_REQ_FINISHED
;
2051 list_del_init(&req
->list
);
2052 wake_up(&req
->waitq
);
2055 __fuse_get_request(req
);
2056 spin_unlock(&fc
->lock
);
2057 wait_event(req
->waitq
, !req
->locked
);
2059 fuse_put_request(fc
, req
);
2060 spin_lock(&fc
->lock
);
2065 static void end_queued_requests(struct fuse_conn
*fc
)
2066 __releases(fc
->lock
)
2067 __acquires(fc
->lock
)
2069 fc
->max_background
= UINT_MAX
;
2071 end_requests(fc
, &fc
->pending
);
2072 end_requests(fc
, &fc
->processing
);
2073 while (forget_pending(fc
))
2074 kfree(dequeue_forget(fc
, 1, NULL
));
2077 static void end_polls(struct fuse_conn
*fc
)
2081 p
= rb_first(&fc
->polled_files
);
2084 struct fuse_file
*ff
;
2085 ff
= rb_entry(p
, struct fuse_file
, polled_node
);
2086 wake_up_interruptible_all(&ff
->poll_wait
);
2093 * Abort all requests.
2095 * Emergency exit in case of a malicious or accidental deadlock, or
2096 * just a hung filesystem.
2098 * The same effect is usually achievable through killing the
2099 * filesystem daemon and all users of the filesystem. The exception
2100 * is the combination of an asynchronous request and the tricky
2101 * deadlock (see Documentation/filesystems/fuse.txt).
2103 * During the aborting, progression of requests from the pending and
2104 * processing lists onto the io list, and progression of new requests
2105 * onto the pending list is prevented by req->connected being false.
2107 * Progression of requests under I/O to the processing list is
2108 * prevented by the req->aborted flag being true for these requests.
2109 * For this reason requests on the io list must be aborted first.
2111 void fuse_abort_conn(struct fuse_conn
*fc
)
2113 spin_lock(&fc
->lock
);
2114 if (fc
->connected
) {
2117 fc
->initialized
= 1;
2118 end_io_requests(fc
);
2119 end_queued_requests(fc
);
2121 wake_up_all(&fc
->waitq
);
2122 wake_up_all(&fc
->blocked_waitq
);
2123 kill_fasync(&fc
->fasync
, SIGIO
, POLL_IN
);
2125 spin_unlock(&fc
->lock
);
2127 EXPORT_SYMBOL_GPL(fuse_abort_conn
);
2129 int fuse_dev_release(struct inode
*inode
, struct file
*file
)
2131 struct fuse_conn
*fc
= fuse_get_conn(file
);
2133 spin_lock(&fc
->lock
);
2136 fc
->initialized
= 1;
2137 end_queued_requests(fc
);
2139 wake_up_all(&fc
->blocked_waitq
);
2140 spin_unlock(&fc
->lock
);
2146 EXPORT_SYMBOL_GPL(fuse_dev_release
);
2148 static int fuse_dev_fasync(int fd
, struct file
*file
, int on
)
2150 struct fuse_conn
*fc
= fuse_get_conn(file
);
2154 /* No locking - fasync_helper does its own locking */
2155 return fasync_helper(fd
, file
, on
, &fc
->fasync
);
2158 const struct file_operations fuse_dev_operations
= {
2159 .owner
= THIS_MODULE
,
2160 .llseek
= no_llseek
,
2161 .read
= do_sync_read
,
2162 .aio_read
= fuse_dev_read
,
2163 .splice_read
= fuse_dev_splice_read
,
2164 .write
= do_sync_write
,
2165 .aio_write
= fuse_dev_write
,
2166 .splice_write
= fuse_dev_splice_write
,
2167 .poll
= fuse_dev_poll
,
2168 .release
= fuse_dev_release
,
2169 .fasync
= fuse_dev_fasync
,
2171 EXPORT_SYMBOL_GPL(fuse_dev_operations
);
2173 static struct miscdevice fuse_miscdevice
= {
2174 .minor
= FUSE_MINOR
,
2176 .fops
= &fuse_dev_operations
,
2179 int __init
fuse_dev_init(void)
2182 fuse_req_cachep
= kmem_cache_create("fuse_request",
2183 sizeof(struct fuse_req
),
2185 if (!fuse_req_cachep
)
2188 err
= misc_register(&fuse_miscdevice
);
2190 goto out_cache_clean
;
2195 kmem_cache_destroy(fuse_req_cachep
);
2200 void fuse_dev_cleanup(void)
2202 misc_deregister(&fuse_miscdevice
);
2203 kmem_cache_destroy(fuse_req_cachep
);