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/freezer.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)
40 memset(req, 0, sizeof(*req));
41 INIT_LIST_HEAD(&req->list);
42 INIT_LIST_HEAD(&req->intr_entry);
43 init_waitqueue_head(&req->waitq);
44 atomic_set(&req->count, 1);
47 struct fuse_req *fuse_request_alloc(void)
49 struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, GFP_KERNEL);
51 fuse_request_init(req);
54 EXPORT_SYMBOL_GPL(fuse_request_alloc);
56 struct fuse_req *fuse_request_alloc_nofs(void)
58 struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, GFP_NOFS);
60 fuse_request_init(req);
64 void fuse_request_free(struct fuse_req *req)
66 kmem_cache_free(fuse_req_cachep, req);
69 static void block_sigs(sigset_t *oldset)
73 siginitsetinv(&mask, sigmask(SIGKILL));
74 sigprocmask(SIG_BLOCK, &mask, oldset);
77 static void restore_sigs(sigset_t *oldset)
79 sigprocmask(SIG_SETMASK, oldset, NULL);
82 static void __fuse_get_request(struct fuse_req *req)
84 atomic_inc(&req->count);
87 /* Must be called with > 1 refcount */
88 static void __fuse_put_request(struct fuse_req *req)
90 BUG_ON(atomic_read(&req->count) < 2);
91 atomic_dec(&req->count);
94 static void fuse_req_init_context(struct fuse_req *req)
96 req->in.h.uid = current_fsuid();
97 req->in.h.gid = current_fsgid();
98 req->in.h.pid = current->pid;
101 struct fuse_req *fuse_get_req(struct fuse_conn *fc)
103 struct fuse_req *req;
108 atomic_inc(&fc->num_waiting);
110 intr = wait_event_interruptible(fc->blocked_waitq, !fc->blocked);
111 restore_sigs(&oldset);
120 req = fuse_request_alloc();
125 fuse_req_init_context(req);
130 atomic_dec(&fc->num_waiting);
133 EXPORT_SYMBOL_GPL(fuse_get_req);
136 * Return request in fuse_file->reserved_req. However that may
137 * currently be in use. If that is the case, wait for it to become
140 static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
143 struct fuse_req *req = NULL;
144 struct fuse_file *ff = file->private_data;
147 wait_event(fc->reserved_req_waitq, ff->reserved_req);
148 spin_lock(&fc->lock);
149 if (ff->reserved_req) {
150 req = ff->reserved_req;
151 ff->reserved_req = NULL;
153 req->stolen_file = file;
155 spin_unlock(&fc->lock);
162 * Put stolen request back into fuse_file->reserved_req
164 static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
166 struct file *file = req->stolen_file;
167 struct fuse_file *ff = file->private_data;
169 spin_lock(&fc->lock);
170 fuse_request_init(req);
171 BUG_ON(ff->reserved_req);
172 ff->reserved_req = req;
173 wake_up_all(&fc->reserved_req_waitq);
174 spin_unlock(&fc->lock);
179 * Gets a requests for a file operation, always succeeds
181 * This is used for sending the FLUSH request, which must get to
182 * userspace, due to POSIX locks which may need to be unlocked.
184 * If allocation fails due to OOM, use the reserved request in
187 * This is very unlikely to deadlock accidentally, since the
188 * filesystem should not have it's own file open. If deadlock is
189 * intentional, it can still be broken by "aborting" the filesystem.
191 struct fuse_req *fuse_get_req_nofail(struct fuse_conn *fc, struct file *file)
193 struct fuse_req *req;
195 atomic_inc(&fc->num_waiting);
196 wait_event(fc->blocked_waitq, !fc->blocked);
197 req = fuse_request_alloc();
199 req = get_reserved_req(fc, file);
201 fuse_req_init_context(req);
206 void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
208 if (atomic_dec_and_test(&req->count)) {
210 atomic_dec(&fc->num_waiting);
212 if (req->stolen_file)
213 put_reserved_req(fc, req);
215 fuse_request_free(req);
218 EXPORT_SYMBOL_GPL(fuse_put_request);
220 static unsigned len_args(unsigned numargs, struct fuse_arg *args)
225 for (i = 0; i < numargs; i++)
226 nbytes += args[i].size;
231 static u64 fuse_get_unique(struct fuse_conn *fc)
234 /* zero is special */
241 static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
243 req->in.h.len = sizeof(struct fuse_in_header) +
244 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
245 list_add_tail(&req->list, &fc->pending);
246 req->state = FUSE_REQ_PENDING;
249 atomic_inc(&fc->num_waiting);
252 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
255 void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
256 u64 nodeid, u64 nlookup)
258 forget->forget_one.nodeid = nodeid;
259 forget->forget_one.nlookup = nlookup;
261 spin_lock(&fc->lock);
263 fc->forget_list_tail->next = forget;
264 fc->forget_list_tail = forget;
266 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
270 spin_unlock(&fc->lock);
273 static void flush_bg_queue(struct fuse_conn *fc)
275 while (fc->active_background < fc->max_background &&
276 !list_empty(&fc->bg_queue)) {
277 struct fuse_req *req;
279 req = list_entry(fc->bg_queue.next, struct fuse_req, list);
280 list_del(&req->list);
281 fc->active_background++;
282 req->in.h.unique = fuse_get_unique(fc);
283 queue_request(fc, req);
288 * This function is called when a request is finished. Either a reply
289 * has arrived or it was aborted (and not yet sent) or some error
290 * occurred during communication with userspace, or the device file
291 * was closed. The requester thread is woken up (if still waiting),
292 * the 'end' callback is called if given, else the reference to the
293 * request is released
295 * Called with fc->lock, unlocks it
297 static void request_end(struct fuse_conn *fc, struct fuse_req *req)
300 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
302 list_del(&req->list);
303 list_del(&req->intr_entry);
304 req->state = FUSE_REQ_FINISHED;
305 if (req->background) {
306 if (fc->num_background == fc->max_background) {
308 wake_up_all(&fc->blocked_waitq);
310 if (fc->num_background == fc->congestion_threshold &&
311 fc->connected && fc->bdi_initialized) {
312 clear_bdi_congested(&fc->bdi, BLK_RW_SYNC);
313 clear_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
315 fc->num_background--;
316 fc->active_background--;
319 spin_unlock(&fc->lock);
320 wake_up(&req->waitq);
323 fuse_put_request(fc, req);
326 static void wait_answer_interruptible(struct fuse_conn *fc,
327 struct fuse_req *req)
331 if (signal_pending(current))
334 spin_unlock(&fc->lock);
335 wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED);
336 spin_lock(&fc->lock);
339 static void queue_interrupt(struct fuse_conn *fc, struct fuse_req *req)
341 list_add_tail(&req->intr_entry, &fc->interrupts);
343 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
346 static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
350 if (!fc->no_interrupt) {
351 /* Any signal may interrupt this */
352 wait_answer_interruptible(fc, req);
356 if (req->state == FUSE_REQ_FINISHED)
359 req->interrupted = 1;
360 if (req->state == FUSE_REQ_SENT)
361 queue_interrupt(fc, req);
367 /* Only fatal signals may interrupt this */
369 wait_answer_interruptible(fc, req);
370 restore_sigs(&oldset);
374 if (req->state == FUSE_REQ_FINISHED)
377 /* Request is not yet in userspace, bail out */
378 if (req->state == FUSE_REQ_PENDING) {
379 list_del(&req->list);
380 __fuse_put_request(req);
381 req->out.h.error = -EINTR;
387 * Either request is already in userspace, or it was forced.
390 spin_unlock(&fc->lock);
392 while (req->state != FUSE_REQ_FINISHED)
393 wait_event_freezable(req->waitq,
394 req->state == FUSE_REQ_FINISHED);
395 spin_lock(&fc->lock);
401 BUG_ON(req->state != FUSE_REQ_FINISHED);
403 /* This is uninterruptible sleep, because data is
404 being copied to/from the buffers of req. During
405 locked state, there mustn't be any filesystem
406 operation (e.g. page fault), since that could lead
408 spin_unlock(&fc->lock);
409 wait_event(req->waitq, !req->locked);
410 spin_lock(&fc->lock);
414 void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
417 spin_lock(&fc->lock);
419 req->out.h.error = -ENOTCONN;
420 else if (fc->conn_error)
421 req->out.h.error = -ECONNREFUSED;
423 req->in.h.unique = fuse_get_unique(fc);
424 queue_request(fc, req);
425 /* acquire extra reference, since request is still needed
426 after request_end() */
427 __fuse_get_request(req);
429 request_wait_answer(fc, req);
431 spin_unlock(&fc->lock);
433 EXPORT_SYMBOL_GPL(fuse_request_send);
435 static void fuse_request_send_nowait_locked(struct fuse_conn *fc,
436 struct fuse_req *req)
439 fc->num_background++;
440 if (fc->num_background == fc->max_background)
442 if (fc->num_background == fc->congestion_threshold &&
443 fc->bdi_initialized) {
444 set_bdi_congested(&fc->bdi, BLK_RW_SYNC);
445 set_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
447 list_add_tail(&req->list, &fc->bg_queue);
451 static void fuse_request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
453 spin_lock(&fc->lock);
455 fuse_request_send_nowait_locked(fc, req);
456 spin_unlock(&fc->lock);
458 req->out.h.error = -ENOTCONN;
459 request_end(fc, req);
463 void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
466 fuse_request_send_nowait(fc, req);
468 EXPORT_SYMBOL_GPL(fuse_request_send_background);
470 static int fuse_request_send_notify_reply(struct fuse_conn *fc,
471 struct fuse_req *req, u64 unique)
476 req->in.h.unique = unique;
477 spin_lock(&fc->lock);
479 queue_request(fc, req);
482 spin_unlock(&fc->lock);
488 * Called under fc->lock
490 * fc->connected must have been checked previously
492 void fuse_request_send_background_locked(struct fuse_conn *fc,
493 struct fuse_req *req)
496 fuse_request_send_nowait_locked(fc, req);
500 * Lock the request. Up to the next unlock_request() there mustn't be
501 * anything that could cause a page-fault. If the request was already
504 static int lock_request(struct fuse_conn *fc, struct fuse_req *req)
508 spin_lock(&fc->lock);
513 spin_unlock(&fc->lock);
519 * Unlock request. If it was aborted during being locked, the
520 * requester thread is currently waiting for it to be unlocked, so
523 static void unlock_request(struct fuse_conn *fc, struct fuse_req *req)
526 spin_lock(&fc->lock);
529 wake_up(&req->waitq);
530 spin_unlock(&fc->lock);
534 struct fuse_copy_state {
535 struct fuse_conn *fc;
537 struct fuse_req *req;
538 const struct iovec *iov;
539 struct pipe_buffer *pipebufs;
540 struct pipe_buffer *currbuf;
541 struct pipe_inode_info *pipe;
542 unsigned long nr_segs;
543 unsigned long seglen;
549 unsigned move_pages:1;
552 static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc,
554 const struct iovec *iov, unsigned long nr_segs)
556 memset(cs, 0, sizeof(*cs));
560 cs->nr_segs = nr_segs;
563 /* Unmap and put previous page of userspace buffer */
564 static void fuse_copy_finish(struct fuse_copy_state *cs)
567 struct pipe_buffer *buf = cs->currbuf;
570 buf->ops->unmap(cs->pipe, buf, cs->mapaddr);
573 buf->len = PAGE_SIZE - cs->len;
577 } else if (cs->mapaddr) {
580 flush_dcache_page(cs->pg);
581 set_page_dirty_lock(cs->pg);
589 * Get another pagefull of userspace buffer, and map it to kernel
590 * address space, and lock request
592 static int fuse_copy_fill(struct fuse_copy_state *cs)
594 unsigned long offset;
597 unlock_request(cs->fc, cs->req);
598 fuse_copy_finish(cs);
600 struct pipe_buffer *buf = cs->pipebufs;
603 err = buf->ops->confirm(cs->pipe, buf);
607 BUG_ON(!cs->nr_segs);
609 cs->mapaddr = buf->ops->map(cs->pipe, buf, 0);
611 cs->buf = cs->mapaddr + buf->offset;
617 if (cs->nr_segs == cs->pipe->buffers)
620 page = alloc_page(GFP_HIGHUSER);
629 cs->mapaddr = kmap(page);
630 cs->buf = cs->mapaddr;
637 BUG_ON(!cs->nr_segs);
638 cs->seglen = cs->iov[0].iov_len;
639 cs->addr = (unsigned long) cs->iov[0].iov_base;
643 err = get_user_pages_fast(cs->addr, 1, cs->write, &cs->pg);
647 offset = cs->addr % PAGE_SIZE;
648 cs->mapaddr = kmap(cs->pg);
649 cs->buf = cs->mapaddr + offset;
650 cs->len = min(PAGE_SIZE - offset, cs->seglen);
651 cs->seglen -= cs->len;
655 return lock_request(cs->fc, cs->req);
658 /* Do as much copy to/from userspace buffer as we can */
659 static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
661 unsigned ncpy = min(*size, cs->len);
664 memcpy(cs->buf, *val, ncpy);
666 memcpy(*val, cs->buf, ncpy);
675 static int fuse_check_page(struct page *page)
677 if (page_mapcount(page) ||
678 page->mapping != NULL ||
679 page_count(page) != 1 ||
680 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
687 printk(KERN_WARNING "fuse: trying to steal weird page\n");
688 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);
694 static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
697 struct page *oldpage = *pagep;
698 struct page *newpage;
699 struct pipe_buffer *buf = cs->pipebufs;
700 struct address_space *mapping;
703 unlock_request(cs->fc, cs->req);
704 fuse_copy_finish(cs);
706 err = buf->ops->confirm(cs->pipe, buf);
710 BUG_ON(!cs->nr_segs);
716 if (cs->len != PAGE_SIZE)
719 if (buf->ops->steal(cs->pipe, buf) != 0)
724 if (WARN_ON(!PageUptodate(newpage)))
727 ClearPageMappedToDisk(newpage);
729 if (fuse_check_page(newpage) != 0)
730 goto out_fallback_unlock;
732 mapping = oldpage->mapping;
733 index = oldpage->index;
736 * This is a new and locked page, it shouldn't be mapped or
737 * have any special flags on it
739 if (WARN_ON(page_mapped(oldpage)))
740 goto out_fallback_unlock;
741 if (WARN_ON(page_has_private(oldpage)))
742 goto out_fallback_unlock;
743 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
744 goto out_fallback_unlock;
745 if (WARN_ON(PageMlocked(oldpage)))
746 goto out_fallback_unlock;
748 err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
750 unlock_page(newpage);
754 page_cache_get(newpage);
756 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
757 lru_cache_add_file(newpage);
760 spin_lock(&cs->fc->lock);
761 if (cs->req->aborted)
765 spin_unlock(&cs->fc->lock);
768 unlock_page(newpage);
769 page_cache_release(newpage);
773 unlock_page(oldpage);
774 page_cache_release(oldpage);
780 unlock_page(newpage);
782 cs->mapaddr = buf->ops->map(cs->pipe, buf, 1);
783 cs->buf = cs->mapaddr + buf->offset;
785 err = lock_request(cs->fc, cs->req);
792 static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
793 unsigned offset, unsigned count)
795 struct pipe_buffer *buf;
797 if (cs->nr_segs == cs->pipe->buffers)
800 unlock_request(cs->fc, cs->req);
801 fuse_copy_finish(cs);
804 page_cache_get(page);
806 buf->offset = offset;
817 * Copy a page in the request to/from the userspace buffer. Must be
820 static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
821 unsigned offset, unsigned count, int zeroing)
824 struct page *page = *pagep;
826 if (page && zeroing && count < PAGE_SIZE)
827 clear_highpage(page);
830 if (cs->write && cs->pipebufs && page) {
831 return fuse_ref_page(cs, page, offset, count);
832 } else if (!cs->len) {
833 if (cs->move_pages && page &&
834 offset == 0 && count == PAGE_SIZE) {
835 err = fuse_try_move_page(cs, pagep);
839 err = fuse_copy_fill(cs);
845 void *mapaddr = kmap_atomic(page, KM_USER0);
846 void *buf = mapaddr + offset;
847 offset += fuse_copy_do(cs, &buf, &count);
848 kunmap_atomic(mapaddr, KM_USER0);
850 offset += fuse_copy_do(cs, NULL, &count);
852 if (page && !cs->write)
853 flush_dcache_page(page);
857 /* Copy pages in the request to/from userspace buffer */
858 static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
862 struct fuse_req *req = cs->req;
863 unsigned offset = req->page_offset;
864 unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset);
866 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
869 err = fuse_copy_page(cs, &req->pages[i], offset, count,
875 count = min(nbytes, (unsigned) PAGE_SIZE);
881 /* Copy a single argument in the request to/from userspace buffer */
882 static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
886 int err = fuse_copy_fill(cs);
890 fuse_copy_do(cs, &val, &size);
895 /* Copy request arguments to/from userspace buffer */
896 static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
897 unsigned argpages, struct fuse_arg *args,
903 for (i = 0; !err && i < numargs; i++) {
904 struct fuse_arg *arg = &args[i];
905 if (i == numargs - 1 && argpages)
906 err = fuse_copy_pages(cs, arg->size, zeroing);
908 err = fuse_copy_one(cs, arg->value, arg->size);
913 static int forget_pending(struct fuse_conn *fc)
915 return fc->forget_list_head.next != NULL;
918 static int request_pending(struct fuse_conn *fc)
920 return !list_empty(&fc->pending) || !list_empty(&fc->interrupts) ||
924 /* Wait until a request is available on the pending list */
925 static void request_wait(struct fuse_conn *fc)
929 DECLARE_WAITQUEUE(wait, current);
931 add_wait_queue_exclusive(&fc->waitq, &wait);
932 while (fc->connected && !request_pending(fc)) {
933 set_current_state(TASK_INTERRUPTIBLE);
934 if (signal_pending(current))
937 spin_unlock(&fc->lock);
939 spin_lock(&fc->lock);
941 set_current_state(TASK_RUNNING);
942 remove_wait_queue(&fc->waitq, &wait);
946 * Transfer an interrupt request to userspace
948 * Unlike other requests this is assembled on demand, without a need
949 * to allocate a separate fuse_req structure.
951 * Called with fc->lock held, releases it
953 static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_copy_state *cs,
954 size_t nbytes, struct fuse_req *req)
957 struct fuse_in_header ih;
958 struct fuse_interrupt_in arg;
959 unsigned reqsize = sizeof(ih) + sizeof(arg);
962 list_del_init(&req->intr_entry);
963 req->intr_unique = fuse_get_unique(fc);
964 memset(&ih, 0, sizeof(ih));
965 memset(&arg, 0, sizeof(arg));
967 ih.opcode = FUSE_INTERRUPT;
968 ih.unique = req->intr_unique;
969 arg.unique = req->in.h.unique;
971 spin_unlock(&fc->lock);
972 if (nbytes < reqsize)
975 err = fuse_copy_one(cs, &ih, sizeof(ih));
977 err = fuse_copy_one(cs, &arg, sizeof(arg));
978 fuse_copy_finish(cs);
980 return err ? err : reqsize;
983 static struct fuse_forget_link *dequeue_forget(struct fuse_conn *fc,
987 struct fuse_forget_link *head = fc->forget_list_head.next;
988 struct fuse_forget_link **newhead = &head;
991 for (count = 0; *newhead != NULL && count < max; count++)
992 newhead = &(*newhead)->next;
994 fc->forget_list_head.next = *newhead;
996 if (fc->forget_list_head.next == NULL)
997 fc->forget_list_tail = &fc->forget_list_head;
1005 static int fuse_read_single_forget(struct fuse_conn *fc,
1006 struct fuse_copy_state *cs,
1008 __releases(fc->lock)
1011 struct fuse_forget_link *forget = dequeue_forget(fc, 1, NULL);
1012 struct fuse_forget_in arg = {
1013 .nlookup = forget->forget_one.nlookup,
1015 struct fuse_in_header ih = {
1016 .opcode = FUSE_FORGET,
1017 .nodeid = forget->forget_one.nodeid,
1018 .unique = fuse_get_unique(fc),
1019 .len = sizeof(ih) + sizeof(arg),
1022 spin_unlock(&fc->lock);
1024 if (nbytes < ih.len)
1027 err = fuse_copy_one(cs, &ih, sizeof(ih));
1029 err = fuse_copy_one(cs, &arg, sizeof(arg));
1030 fuse_copy_finish(cs);
1038 static int fuse_read_batch_forget(struct fuse_conn *fc,
1039 struct fuse_copy_state *cs, size_t nbytes)
1040 __releases(fc->lock)
1043 unsigned max_forgets;
1045 struct fuse_forget_link *head;
1046 struct fuse_batch_forget_in arg = { .count = 0 };
1047 struct fuse_in_header ih = {
1048 .opcode = FUSE_BATCH_FORGET,
1049 .unique = fuse_get_unique(fc),
1050 .len = sizeof(ih) + sizeof(arg),
1053 if (nbytes < ih.len) {
1054 spin_unlock(&fc->lock);
1058 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
1059 head = dequeue_forget(fc, max_forgets, &count);
1060 spin_unlock(&fc->lock);
1063 ih.len += count * sizeof(struct fuse_forget_one);
1064 err = fuse_copy_one(cs, &ih, sizeof(ih));
1066 err = fuse_copy_one(cs, &arg, sizeof(arg));
1069 struct fuse_forget_link *forget = head;
1072 err = fuse_copy_one(cs, &forget->forget_one,
1073 sizeof(forget->forget_one));
1075 head = forget->next;
1079 fuse_copy_finish(cs);
1087 static int fuse_read_forget(struct fuse_conn *fc, struct fuse_copy_state *cs,
1089 __releases(fc->lock)
1091 if (fc->minor < 16 || fc->forget_list_head.next->next == NULL)
1092 return fuse_read_single_forget(fc, cs, nbytes);
1094 return fuse_read_batch_forget(fc, cs, nbytes);
1098 * Read a single request into the userspace filesystem's buffer. This
1099 * function waits until a request is available, then removes it from
1100 * the pending list and copies request data to userspace buffer. If
1101 * no reply is needed (FORGET) or request has been aborted or there
1102 * was an error during the copying then it's finished by calling
1103 * request_end(). Otherwise add it to the processing list, and set
1106 static ssize_t fuse_dev_do_read(struct fuse_conn *fc, struct file *file,
1107 struct fuse_copy_state *cs, size_t nbytes)
1110 struct fuse_req *req;
1115 spin_lock(&fc->lock);
1117 if ((file->f_flags & O_NONBLOCK) && fc->connected &&
1118 !request_pending(fc))
1126 if (!request_pending(fc))
1129 if (!list_empty(&fc->interrupts)) {
1130 req = list_entry(fc->interrupts.next, struct fuse_req,
1132 return fuse_read_interrupt(fc, cs, nbytes, req);
1135 if (forget_pending(fc)) {
1136 if (list_empty(&fc->pending) || fc->forget_batch-- > 0)
1137 return fuse_read_forget(fc, cs, nbytes);
1139 if (fc->forget_batch <= -8)
1140 fc->forget_batch = 16;
1143 req = list_entry(fc->pending.next, struct fuse_req, list);
1144 req->state = FUSE_REQ_READING;
1145 list_move(&req->list, &fc->io);
1148 reqsize = in->h.len;
1149 /* If request is too large, reply with an error and restart the read */
1150 if (nbytes < reqsize) {
1151 req->out.h.error = -EIO;
1152 /* SETXATTR is special, since it may contain too large data */
1153 if (in->h.opcode == FUSE_SETXATTR)
1154 req->out.h.error = -E2BIG;
1155 request_end(fc, req);
1158 spin_unlock(&fc->lock);
1160 err = fuse_copy_one(cs, &in->h, sizeof(in->h));
1162 err = fuse_copy_args(cs, in->numargs, in->argpages,
1163 (struct fuse_arg *) in->args, 0);
1164 fuse_copy_finish(cs);
1165 spin_lock(&fc->lock);
1168 request_end(fc, req);
1172 req->out.h.error = -EIO;
1173 request_end(fc, req);
1177 request_end(fc, req);
1179 req->state = FUSE_REQ_SENT;
1180 list_move_tail(&req->list, &fc->processing);
1181 if (req->interrupted)
1182 queue_interrupt(fc, req);
1183 spin_unlock(&fc->lock);
1188 spin_unlock(&fc->lock);
1192 static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov,
1193 unsigned long nr_segs, loff_t pos)
1195 struct fuse_copy_state cs;
1196 struct file *file = iocb->ki_filp;
1197 struct fuse_conn *fc = fuse_get_conn(file);
1201 fuse_copy_init(&cs, fc, 1, iov, nr_segs);
1203 return fuse_dev_do_read(fc, file, &cs, iov_length(iov, nr_segs));
1206 static int fuse_dev_pipe_buf_steal(struct pipe_inode_info *pipe,
1207 struct pipe_buffer *buf)
1212 static const struct pipe_buf_operations fuse_dev_pipe_buf_ops = {
1214 .map = generic_pipe_buf_map,
1215 .unmap = generic_pipe_buf_unmap,
1216 .confirm = generic_pipe_buf_confirm,
1217 .release = generic_pipe_buf_release,
1218 .steal = fuse_dev_pipe_buf_steal,
1219 .get = generic_pipe_buf_get,
1222 static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1223 struct pipe_inode_info *pipe,
1224 size_t len, unsigned int flags)
1229 struct pipe_buffer *bufs;
1230 struct fuse_copy_state cs;
1231 struct fuse_conn *fc = fuse_get_conn(in);
1235 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
1239 fuse_copy_init(&cs, fc, 1, NULL, 0);
1242 ret = fuse_dev_do_read(fc, in, &cs, len);
1249 if (!pipe->readers) {
1250 send_sig(SIGPIPE, current, 0);
1256 if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
1261 while (page_nr < cs.nr_segs) {
1262 int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
1263 struct pipe_buffer *buf = pipe->bufs + newbuf;
1265 buf->page = bufs[page_nr].page;
1266 buf->offset = bufs[page_nr].offset;
1267 buf->len = bufs[page_nr].len;
1268 buf->ops = &fuse_dev_pipe_buf_ops;
1283 if (waitqueue_active(&pipe->wait))
1284 wake_up_interruptible(&pipe->wait);
1285 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1289 for (; page_nr < cs.nr_segs; page_nr++)
1290 page_cache_release(bufs[page_nr].page);
1296 static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1297 struct fuse_copy_state *cs)
1299 struct fuse_notify_poll_wakeup_out outarg;
1302 if (size != sizeof(outarg))
1305 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1309 fuse_copy_finish(cs);
1310 return fuse_notify_poll_wakeup(fc, &outarg);
1313 fuse_copy_finish(cs);
1317 static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1318 struct fuse_copy_state *cs)
1320 struct fuse_notify_inval_inode_out outarg;
1323 if (size != sizeof(outarg))
1326 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1329 fuse_copy_finish(cs);
1331 down_read(&fc->killsb);
1334 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1335 outarg.off, outarg.len);
1337 up_read(&fc->killsb);
1341 fuse_copy_finish(cs);
1345 static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1346 struct fuse_copy_state *cs)
1348 struct fuse_notify_inval_entry_out outarg;
1353 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1358 if (size < sizeof(outarg))
1361 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1365 err = -ENAMETOOLONG;
1366 if (outarg.namelen > FUSE_NAME_MAX)
1370 if (size != sizeof(outarg) + outarg.namelen + 1)
1374 name.len = outarg.namelen;
1375 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1378 fuse_copy_finish(cs);
1379 buf[outarg.namelen] = 0;
1380 name.hash = full_name_hash(name.name, name.len);
1382 down_read(&fc->killsb);
1385 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, &name);
1386 up_read(&fc->killsb);
1392 fuse_copy_finish(cs);
1396 static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1397 struct fuse_copy_state *cs)
1399 struct fuse_notify_store_out outarg;
1400 struct inode *inode;
1401 struct address_space *mapping;
1405 unsigned int offset;
1411 if (size < sizeof(outarg))
1414 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1419 if (size - sizeof(outarg) != outarg.size)
1422 nodeid = outarg.nodeid;
1424 down_read(&fc->killsb);
1430 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1434 mapping = inode->i_mapping;
1435 index = outarg.offset >> PAGE_CACHE_SHIFT;
1436 offset = outarg.offset & ~PAGE_CACHE_MASK;
1437 file_size = i_size_read(inode);
1438 end = outarg.offset + outarg.size;
1439 if (end > file_size) {
1441 fuse_write_update_size(inode, file_size);
1447 unsigned int this_num;
1450 page = find_or_create_page(mapping, index,
1451 mapping_gfp_mask(mapping));
1455 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1456 err = fuse_copy_page(cs, &page, offset, this_num, 0);
1457 if (!err && offset == 0 && (num != 0 || file_size == end))
1458 SetPageUptodate(page);
1460 page_cache_release(page);
1475 up_read(&fc->killsb);
1477 fuse_copy_finish(cs);
1481 static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_req *req)
1483 release_pages(req->pages, req->num_pages, 0);
1486 static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
1487 struct fuse_notify_retrieve_out *outarg)
1490 struct address_space *mapping = inode->i_mapping;
1491 struct fuse_req *req;
1495 unsigned int offset;
1496 size_t total_len = 0;
1498 req = fuse_get_req(fc);
1500 return PTR_ERR(req);
1502 offset = outarg->offset & ~PAGE_CACHE_MASK;
1504 req->in.h.opcode = FUSE_NOTIFY_REPLY;
1505 req->in.h.nodeid = outarg->nodeid;
1506 req->in.numargs = 2;
1507 req->in.argpages = 1;
1508 req->page_offset = offset;
1509 req->end = fuse_retrieve_end;
1511 index = outarg->offset >> PAGE_CACHE_SHIFT;
1512 file_size = i_size_read(inode);
1514 if (outarg->offset > file_size)
1516 else if (outarg->offset + num > file_size)
1517 num = file_size - outarg->offset;
1519 while (num && req->num_pages < FUSE_MAX_PAGES_PER_REQ) {
1521 unsigned int this_num;
1523 page = find_get_page(mapping, index);
1527 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1528 req->pages[req->num_pages] = page;
1533 total_len += this_num;
1536 req->misc.retrieve_in.offset = outarg->offset;
1537 req->misc.retrieve_in.size = total_len;
1538 req->in.args[0].size = sizeof(req->misc.retrieve_in);
1539 req->in.args[0].value = &req->misc.retrieve_in;
1540 req->in.args[1].size = total_len;
1542 err = fuse_request_send_notify_reply(fc, req, outarg->notify_unique);
1544 fuse_retrieve_end(fc, req);
1549 static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1550 struct fuse_copy_state *cs)
1552 struct fuse_notify_retrieve_out outarg;
1553 struct inode *inode;
1557 if (size != sizeof(outarg))
1560 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1564 fuse_copy_finish(cs);
1566 down_read(&fc->killsb);
1569 u64 nodeid = outarg.nodeid;
1571 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1573 err = fuse_retrieve(fc, inode, &outarg);
1577 up_read(&fc->killsb);
1582 fuse_copy_finish(cs);
1586 static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1587 unsigned int size, struct fuse_copy_state *cs)
1590 case FUSE_NOTIFY_POLL:
1591 return fuse_notify_poll(fc, size, cs);
1593 case FUSE_NOTIFY_INVAL_INODE:
1594 return fuse_notify_inval_inode(fc, size, cs);
1596 case FUSE_NOTIFY_INVAL_ENTRY:
1597 return fuse_notify_inval_entry(fc, size, cs);
1599 case FUSE_NOTIFY_STORE:
1600 return fuse_notify_store(fc, size, cs);
1602 case FUSE_NOTIFY_RETRIEVE:
1603 return fuse_notify_retrieve(fc, size, cs);
1606 fuse_copy_finish(cs);
1611 /* Look up request on processing list by unique ID */
1612 static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
1614 struct list_head *entry;
1616 list_for_each(entry, &fc->processing) {
1617 struct fuse_req *req;
1618 req = list_entry(entry, struct fuse_req, list);
1619 if (req->in.h.unique == unique || req->intr_unique == unique)
1625 static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
1628 unsigned reqsize = sizeof(struct fuse_out_header);
1631 return nbytes != reqsize ? -EINVAL : 0;
1633 reqsize += len_args(out->numargs, out->args);
1635 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
1637 else if (reqsize > nbytes) {
1638 struct fuse_arg *lastarg = &out->args[out->numargs-1];
1639 unsigned diffsize = reqsize - nbytes;
1640 if (diffsize > lastarg->size)
1642 lastarg->size -= diffsize;
1644 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
1649 * Write a single reply to a request. First the header is copied from
1650 * the write buffer. The request is then searched on the processing
1651 * list by the unique ID found in the header. If found, then remove
1652 * it from the list and copy the rest of the buffer to the request.
1653 * The request is finished by calling request_end()
1655 static ssize_t fuse_dev_do_write(struct fuse_conn *fc,
1656 struct fuse_copy_state *cs, size_t nbytes)
1659 struct fuse_req *req;
1660 struct fuse_out_header oh;
1662 if (nbytes < sizeof(struct fuse_out_header))
1665 err = fuse_copy_one(cs, &oh, sizeof(oh));
1670 if (oh.len != nbytes)
1674 * Zero oh.unique indicates unsolicited notification message
1675 * and error contains notification code.
1678 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
1679 return err ? err : nbytes;
1683 if (oh.error <= -1000 || oh.error > 0)
1686 spin_lock(&fc->lock);
1691 req = request_find(fc, oh.unique);
1696 spin_unlock(&fc->lock);
1697 fuse_copy_finish(cs);
1698 spin_lock(&fc->lock);
1699 request_end(fc, req);
1702 /* Is it an interrupt reply? */
1703 if (req->intr_unique == oh.unique) {
1705 if (nbytes != sizeof(struct fuse_out_header))
1708 if (oh.error == -ENOSYS)
1709 fc->no_interrupt = 1;
1710 else if (oh.error == -EAGAIN)
1711 queue_interrupt(fc, req);
1713 spin_unlock(&fc->lock);
1714 fuse_copy_finish(cs);
1718 req->state = FUSE_REQ_WRITING;
1719 list_move(&req->list, &fc->io);
1723 if (!req->out.page_replace)
1725 spin_unlock(&fc->lock);
1727 err = copy_out_args(cs, &req->out, nbytes);
1728 fuse_copy_finish(cs);
1730 spin_lock(&fc->lock);
1735 } else if (!req->aborted)
1736 req->out.h.error = -EIO;
1737 request_end(fc, req);
1739 return err ? err : nbytes;
1742 spin_unlock(&fc->lock);
1744 fuse_copy_finish(cs);
1748 static ssize_t fuse_dev_write(struct kiocb *iocb, const struct iovec *iov,
1749 unsigned long nr_segs, loff_t pos)
1751 struct fuse_copy_state cs;
1752 struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp);
1756 fuse_copy_init(&cs, fc, 0, iov, nr_segs);
1758 return fuse_dev_do_write(fc, &cs, iov_length(iov, nr_segs));
1761 static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
1762 struct file *out, loff_t *ppos,
1763 size_t len, unsigned int flags)
1767 struct pipe_buffer *bufs;
1768 struct fuse_copy_state cs;
1769 struct fuse_conn *fc;
1773 fc = fuse_get_conn(out);
1777 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
1784 for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
1785 rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
1795 struct pipe_buffer *ibuf;
1796 struct pipe_buffer *obuf;
1798 BUG_ON(nbuf >= pipe->buffers);
1799 BUG_ON(!pipe->nrbufs);
1800 ibuf = &pipe->bufs[pipe->curbuf];
1803 if (rem >= ibuf->len) {
1806 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
1809 ibuf->ops->get(pipe, ibuf);
1811 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1813 ibuf->offset += obuf->len;
1814 ibuf->len -= obuf->len;
1821 fuse_copy_init(&cs, fc, 0, NULL, nbuf);
1825 if (flags & SPLICE_F_MOVE)
1828 ret = fuse_dev_do_write(fc, &cs, len);
1830 for (idx = 0; idx < nbuf; idx++) {
1831 struct pipe_buffer *buf = &bufs[idx];
1832 buf->ops->release(pipe, buf);
1839 static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
1841 unsigned mask = POLLOUT | POLLWRNORM;
1842 struct fuse_conn *fc = fuse_get_conn(file);
1846 poll_wait(file, &fc->waitq, wait);
1848 spin_lock(&fc->lock);
1851 else if (request_pending(fc))
1852 mask |= POLLIN | POLLRDNORM;
1853 spin_unlock(&fc->lock);
1859 * Abort all requests on the given list (pending or processing)
1861 * This function releases and reacquires fc->lock
1863 static void end_requests(struct fuse_conn *fc, struct list_head *head)
1864 __releases(fc->lock)
1865 __acquires(fc->lock)
1867 while (!list_empty(head)) {
1868 struct fuse_req *req;
1869 req = list_entry(head->next, struct fuse_req, list);
1870 req->out.h.error = -ECONNABORTED;
1871 request_end(fc, req);
1872 spin_lock(&fc->lock);
1877 * Abort requests under I/O
1879 * The requests are set to aborted and finished, and the request
1880 * waiter is woken up. This will make request_wait_answer() wait
1881 * until the request is unlocked and then return.
1883 * If the request is asynchronous, then the end function needs to be
1884 * called after waiting for the request to be unlocked (if it was
1887 static void end_io_requests(struct fuse_conn *fc)
1888 __releases(fc->lock)
1889 __acquires(fc->lock)
1891 while (!list_empty(&fc->io)) {
1892 struct fuse_req *req =
1893 list_entry(fc->io.next, struct fuse_req, list);
1894 void (*end) (struct fuse_conn *, struct fuse_req *) = req->end;
1897 req->out.h.error = -ECONNABORTED;
1898 req->state = FUSE_REQ_FINISHED;
1899 list_del_init(&req->list);
1900 wake_up(&req->waitq);
1903 __fuse_get_request(req);
1904 spin_unlock(&fc->lock);
1905 wait_event(req->waitq, !req->locked);
1907 fuse_put_request(fc, req);
1908 spin_lock(&fc->lock);
1913 static void end_queued_requests(struct fuse_conn *fc)
1914 __releases(fc->lock)
1915 __acquires(fc->lock)
1917 fc->max_background = UINT_MAX;
1919 end_requests(fc, &fc->pending);
1920 end_requests(fc, &fc->processing);
1921 while (forget_pending(fc))
1922 kfree(dequeue_forget(fc, 1, NULL));
1925 static void end_polls(struct fuse_conn *fc)
1929 p = rb_first(&fc->polled_files);
1932 struct fuse_file *ff;
1933 ff = rb_entry(p, struct fuse_file, polled_node);
1934 wake_up_interruptible_all(&ff->poll_wait);
1941 * Abort all requests.
1943 * Emergency exit in case of a malicious or accidental deadlock, or
1944 * just a hung filesystem.
1946 * The same effect is usually achievable through killing the
1947 * filesystem daemon and all users of the filesystem. The exception
1948 * is the combination of an asynchronous request and the tricky
1949 * deadlock (see Documentation/filesystems/fuse.txt).
1951 * During the aborting, progression of requests from the pending and
1952 * processing lists onto the io list, and progression of new requests
1953 * onto the pending list is prevented by req->connected being false.
1955 * Progression of requests under I/O to the processing list is
1956 * prevented by the req->aborted flag being true for these requests.
1957 * For this reason requests on the io list must be aborted first.
1959 void fuse_abort_conn(struct fuse_conn *fc)
1961 spin_lock(&fc->lock);
1962 if (fc->connected) {
1965 end_io_requests(fc);
1966 end_queued_requests(fc);
1968 wake_up_all(&fc->waitq);
1969 wake_up_all(&fc->blocked_waitq);
1970 kill_fasync(&fc->fasync, SIGIO, POLL_IN);
1972 spin_unlock(&fc->lock);
1974 EXPORT_SYMBOL_GPL(fuse_abort_conn);
1976 int fuse_dev_release(struct inode *inode, struct file *file)
1978 struct fuse_conn *fc = fuse_get_conn(file);
1980 spin_lock(&fc->lock);
1983 end_queued_requests(fc);
1985 wake_up_all(&fc->blocked_waitq);
1986 spin_unlock(&fc->lock);
1992 EXPORT_SYMBOL_GPL(fuse_dev_release);
1994 static int fuse_dev_fasync(int fd, struct file *file, int on)
1996 struct fuse_conn *fc = fuse_get_conn(file);
2000 /* No locking - fasync_helper does its own locking */
2001 return fasync_helper(fd, file, on, &fc->fasync);
2004 const struct file_operations fuse_dev_operations = {
2005 .owner = THIS_MODULE,
2006 .llseek = no_llseek,
2007 .read = do_sync_read,
2008 .aio_read = fuse_dev_read,
2009 .splice_read = fuse_dev_splice_read,
2010 .write = do_sync_write,
2011 .aio_write = fuse_dev_write,
2012 .splice_write = fuse_dev_splice_write,
2013 .poll = fuse_dev_poll,
2014 .release = fuse_dev_release,
2015 .fasync = fuse_dev_fasync,
2017 EXPORT_SYMBOL_GPL(fuse_dev_operations);
2019 static struct miscdevice fuse_miscdevice = {
2020 .minor = FUSE_MINOR,
2022 .fops = &fuse_dev_operations,
2025 int __init fuse_dev_init(void)
2028 fuse_req_cachep = kmem_cache_create("fuse_request",
2029 sizeof(struct fuse_req),
2031 if (!fuse_req_cachep)
2034 err = misc_register(&fuse_miscdevice);
2036 goto out_cache_clean;
2041 kmem_cache_destroy(fuse_req_cachep);
2046 void fuse_dev_cleanup(void)
2048 misc_deregister(&fuse_miscdevice);
2049 kmem_cache_destroy(fuse_req_cachep);