2 FUSE: Filesystem in Userspace
3 Copyright (C) 2001-2005 Miklos Szeredi <miklos@szeredi.hu>
5 This program can be distributed under the terms of the GNU GPL.
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/poll.h>
14 #include <linux/uio.h>
15 #include <linux/miscdevice.h>
16 #include <linux/pagemap.h>
17 #include <linux/file.h>
18 #include <linux/slab.h>
20 MODULE_ALIAS_MISCDEV(FUSE_MINOR);
22 static kmem_cache_t *fuse_req_cachep;
24 static inline struct fuse_conn *fuse_get_conn(struct file *file)
27 spin_lock(&fuse_lock);
28 fc = file->private_data;
29 if (fc && !fc->mounted)
31 spin_unlock(&fuse_lock);
35 static inline void fuse_request_init(struct fuse_req *req)
37 memset(req, 0, sizeof(*req));
38 INIT_LIST_HEAD(&req->list);
39 init_waitqueue_head(&req->waitq);
40 atomic_set(&req->count, 1);
43 struct fuse_req *fuse_request_alloc(void)
45 struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, SLAB_KERNEL);
47 fuse_request_init(req);
51 void fuse_request_free(struct fuse_req *req)
53 kmem_cache_free(fuse_req_cachep, req);
56 static inline void block_sigs(sigset_t *oldset)
60 siginitsetinv(&mask, sigmask(SIGKILL));
61 sigprocmask(SIG_BLOCK, &mask, oldset);
64 static inline void restore_sigs(sigset_t *oldset)
66 sigprocmask(SIG_SETMASK, oldset, NULL);
69 void fuse_reset_request(struct fuse_req *req)
71 int preallocated = req->preallocated;
72 BUG_ON(atomic_read(&req->count) != 1);
73 fuse_request_init(req);
74 req->preallocated = preallocated;
77 static void __fuse_get_request(struct fuse_req *req)
79 atomic_inc(&req->count);
82 /* Must be called with > 1 refcount */
83 static void __fuse_put_request(struct fuse_req *req)
85 BUG_ON(atomic_read(&req->count) < 2);
86 atomic_dec(&req->count);
89 static struct fuse_req *do_get_request(struct fuse_conn *fc)
93 spin_lock(&fuse_lock);
94 BUG_ON(list_empty(&fc->unused_list));
95 req = list_entry(fc->unused_list.next, struct fuse_req, list);
96 list_del_init(&req->list);
97 spin_unlock(&fuse_lock);
98 fuse_request_init(req);
99 req->preallocated = 1;
100 req->in.h.uid = current->fsuid;
101 req->in.h.gid = current->fsgid;
102 req->in.h.pid = current->pid;
106 /* This can return NULL, but only in case it's interrupted by a SIGKILL */
107 struct fuse_req *fuse_get_request(struct fuse_conn *fc)
113 intr = down_interruptible(&fc->outstanding_sem);
114 restore_sigs(&oldset);
115 return intr ? NULL : do_get_request(fc);
118 static void fuse_putback_request(struct fuse_conn *fc, struct fuse_req *req)
120 spin_lock(&fuse_lock);
121 if (req->preallocated)
122 list_add(&req->list, &fc->unused_list);
124 fuse_request_free(req);
126 /* If we are in debt decrease that first */
127 if (fc->outstanding_debt)
128 fc->outstanding_debt--;
130 up(&fc->outstanding_sem);
131 spin_unlock(&fuse_lock);
134 void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
136 if (atomic_dec_and_test(&req->count))
137 fuse_putback_request(fc, req);
140 void fuse_release_background(struct fuse_req *req)
146 spin_lock(&fuse_lock);
147 list_del(&req->bg_entry);
148 spin_unlock(&fuse_lock);
152 * This function is called when a request is finished. Either a reply
153 * has arrived or it was interrupted (and not yet sent) or some error
154 * occurred during communication with userspace, or the device file was
155 * closed. It decreases the reference count for the request. In case
156 * of a background request the reference to the stored objects are
157 * released. The requester thread is woken up (if still waiting), and
158 * finally the request is either freed or put on the unused_list
160 * Called with fuse_lock, unlocks it
162 static void request_end(struct fuse_conn *fc, struct fuse_req *req)
166 putback = atomic_dec_and_test(&req->count);
167 spin_unlock(&fuse_lock);
168 if (req->background) {
169 down_read(&fc->sbput_sem);
171 fuse_release_background(req);
172 up_read(&fc->sbput_sem);
174 wake_up(&req->waitq);
175 if (req->in.h.opcode == FUSE_INIT) {
178 if (req->misc.init_in_out.major != FUSE_KERNEL_VERSION)
181 /* After INIT reply is received other requests can go
182 out. So do (FUSE_MAX_OUTSTANDING - 1) number of
183 up()s on outstanding_sem. The last up() is done in
184 fuse_putback_request() */
185 for (i = 1; i < FUSE_MAX_OUTSTANDING; i++)
186 up(&fc->outstanding_sem);
187 } else if (req->in.h.opcode == FUSE_RELEASE && req->inode == NULL) {
188 /* Special case for failed iget in CREATE */
189 u64 nodeid = req->in.h.nodeid;
190 __fuse_get_request(req);
191 fuse_reset_request(req);
192 fuse_send_forget(fc, req, nodeid, 1);
196 fuse_putback_request(fc, req);
200 * Unfortunately request interruption not just solves the deadlock
201 * problem, it causes problems too. These stem from the fact, that an
202 * interrupted request is continued to be processed in userspace,
203 * while all the locks and object references (inode and file) held
204 * during the operation are released.
206 * To release the locks is exactly why there's a need to interrupt the
207 * request, so there's not a lot that can be done about this, except
208 * introduce additional locking in userspace.
210 * More important is to keep inode and file references until userspace
211 * has replied, otherwise FORGET and RELEASE could be sent while the
212 * inode/file is still used by the filesystem.
214 * For this reason the concept of "background" request is introduced.
215 * An interrupted request is backgrounded if it has been already sent
216 * to userspace. Backgrounding involves getting an extra reference to
217 * inode(s) or file used in the request, and adding the request to
218 * fc->background list. When a reply is received for a background
219 * request, the object references are released, and the request is
220 * removed from the list. If the filesystem is unmounted while there
221 * are still background requests, the list is walked and references
222 * are released as if a reply was received.
224 * There's one more use for a background request. The RELEASE message is
225 * always sent as background, since it doesn't return an error or
228 static void background_request(struct fuse_conn *fc, struct fuse_req *req)
231 list_add(&req->bg_entry, &fc->background);
233 req->inode = igrab(req->inode);
235 req->inode2 = igrab(req->inode2);
240 /* Called with fuse_lock held. Releases, and then reacquires it. */
241 static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
245 spin_unlock(&fuse_lock);
247 wait_event_interruptible(req->waitq, req->finished);
248 restore_sigs(&oldset);
249 spin_lock(&fuse_lock);
253 req->out.h.error = -EINTR;
254 req->interrupted = 1;
256 /* This is uninterruptible sleep, because data is
257 being copied to/from the buffers of req. During
258 locked state, there mustn't be any filesystem
259 operation (e.g. page fault), since that could lead
261 spin_unlock(&fuse_lock);
262 wait_event(req->waitq, !req->locked);
263 spin_lock(&fuse_lock);
265 if (!req->sent && !list_empty(&req->list)) {
266 list_del(&req->list);
267 __fuse_put_request(req);
268 } else if (!req->finished && req->sent)
269 background_request(fc, req);
272 static unsigned len_args(unsigned numargs, struct fuse_arg *args)
277 for (i = 0; i < numargs; i++)
278 nbytes += args[i].size;
283 static void queue_request(struct fuse_conn *fc, struct fuse_req *req)
286 /* zero is special */
289 req->in.h.unique = fc->reqctr;
290 req->in.h.len = sizeof(struct fuse_in_header) +
291 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
292 if (!req->preallocated) {
293 /* If request is not preallocated (either FORGET or
294 RELEASE), then still decrease outstanding_sem, so
295 user can't open infinite number of files while not
296 processing the RELEASE requests. However for
297 efficiency do it without blocking, so if down()
298 would block, just increase the debt instead */
299 if (down_trylock(&fc->outstanding_sem))
300 fc->outstanding_debt++;
302 list_add_tail(&req->list, &fc->pending);
307 * This can only be interrupted by a SIGKILL
309 void request_send(struct fuse_conn *fc, struct fuse_req *req)
312 spin_lock(&fuse_lock);
314 req->out.h.error = -ENOTCONN;
315 else if (fc->conn_error)
316 req->out.h.error = -ECONNREFUSED;
318 queue_request(fc, req);
319 /* acquire extra reference, since request is still needed
320 after request_end() */
321 __fuse_get_request(req);
323 request_wait_answer(fc, req);
325 spin_unlock(&fuse_lock);
328 static void request_send_nowait(struct fuse_conn *fc, struct fuse_req *req)
330 spin_lock(&fuse_lock);
332 queue_request(fc, req);
333 spin_unlock(&fuse_lock);
335 req->out.h.error = -ENOTCONN;
336 request_end(fc, req);
340 void request_send_noreply(struct fuse_conn *fc, struct fuse_req *req)
343 request_send_nowait(fc, req);
346 void request_send_background(struct fuse_conn *fc, struct fuse_req *req)
349 spin_lock(&fuse_lock);
350 background_request(fc, req);
351 spin_unlock(&fuse_lock);
352 request_send_nowait(fc, req);
355 void fuse_send_init(struct fuse_conn *fc)
357 /* This is called from fuse_read_super() so there's guaranteed
358 to be a request available */
359 struct fuse_req *req = do_get_request(fc);
360 struct fuse_init_in_out *arg = &req->misc.init_in_out;
361 arg->major = FUSE_KERNEL_VERSION;
362 arg->minor = FUSE_KERNEL_MINOR_VERSION;
363 req->in.h.opcode = FUSE_INIT;
365 req->in.args[0].size = sizeof(*arg);
366 req->in.args[0].value = arg;
367 req->out.numargs = 1;
368 req->out.args[0].size = sizeof(*arg);
369 req->out.args[0].value = arg;
370 request_send_background(fc, req);
374 * Lock the request. Up to the next unlock_request() there mustn't be
375 * anything that could cause a page-fault. If the request was already
376 * interrupted bail out.
378 static inline int lock_request(struct fuse_req *req)
382 spin_lock(&fuse_lock);
383 if (req->interrupted)
387 spin_unlock(&fuse_lock);
393 * Unlock request. If it was interrupted during being locked, the
394 * requester thread is currently waiting for it to be unlocked, so
397 static inline void unlock_request(struct fuse_req *req)
400 spin_lock(&fuse_lock);
402 if (req->interrupted)
403 wake_up(&req->waitq);
404 spin_unlock(&fuse_lock);
408 struct fuse_copy_state {
410 struct fuse_req *req;
411 const struct iovec *iov;
412 unsigned long nr_segs;
413 unsigned long seglen;
421 static void fuse_copy_init(struct fuse_copy_state *cs, int write,
422 struct fuse_req *req, const struct iovec *iov,
423 unsigned long nr_segs)
425 memset(cs, 0, sizeof(*cs));
429 cs->nr_segs = nr_segs;
432 /* Unmap and put previous page of userspace buffer */
433 static inline void fuse_copy_finish(struct fuse_copy_state *cs)
436 kunmap_atomic(cs->mapaddr, KM_USER0);
438 flush_dcache_page(cs->pg);
439 set_page_dirty_lock(cs->pg);
447 * Get another pagefull of userspace buffer, and map it to kernel
448 * address space, and lock request
450 static int fuse_copy_fill(struct fuse_copy_state *cs)
452 unsigned long offset;
455 unlock_request(cs->req);
456 fuse_copy_finish(cs);
458 BUG_ON(!cs->nr_segs);
459 cs->seglen = cs->iov[0].iov_len;
460 cs->addr = (unsigned long) cs->iov[0].iov_base;
464 down_read(¤t->mm->mmap_sem);
465 err = get_user_pages(current, current->mm, cs->addr, 1, cs->write, 0,
467 up_read(¤t->mm->mmap_sem);
471 offset = cs->addr % PAGE_SIZE;
472 cs->mapaddr = kmap_atomic(cs->pg, KM_USER0);
473 cs->buf = cs->mapaddr + offset;
474 cs->len = min(PAGE_SIZE - offset, cs->seglen);
475 cs->seglen -= cs->len;
478 return lock_request(cs->req);
481 /* Do as much copy to/from userspace buffer as we can */
482 static inline int fuse_copy_do(struct fuse_copy_state *cs, void **val,
485 unsigned ncpy = min(*size, cs->len);
488 memcpy(cs->buf, *val, ncpy);
490 memcpy(*val, cs->buf, ncpy);
500 * Copy a page in the request to/from the userspace buffer. Must be
503 static inline int fuse_copy_page(struct fuse_copy_state *cs, struct page *page,
504 unsigned offset, unsigned count, int zeroing)
506 if (page && zeroing && count < PAGE_SIZE) {
507 void *mapaddr = kmap_atomic(page, KM_USER1);
508 memset(mapaddr, 0, PAGE_SIZE);
509 kunmap_atomic(mapaddr, KM_USER1);
513 if (!cs->len && (err = fuse_copy_fill(cs)))
516 void *mapaddr = kmap_atomic(page, KM_USER1);
517 void *buf = mapaddr + offset;
518 offset += fuse_copy_do(cs, &buf, &count);
519 kunmap_atomic(mapaddr, KM_USER1);
521 offset += fuse_copy_do(cs, NULL, &count);
523 if (page && !cs->write)
524 flush_dcache_page(page);
528 /* Copy pages in the request to/from userspace buffer */
529 static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
533 struct fuse_req *req = cs->req;
534 unsigned offset = req->page_offset;
535 unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset);
537 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
538 struct page *page = req->pages[i];
539 int err = fuse_copy_page(cs, page, offset, count, zeroing);
544 count = min(nbytes, (unsigned) PAGE_SIZE);
550 /* Copy a single argument in the request to/from userspace buffer */
551 static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
555 if (!cs->len && (err = fuse_copy_fill(cs)))
557 fuse_copy_do(cs, &val, &size);
562 /* Copy request arguments to/from userspace buffer */
563 static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
564 unsigned argpages, struct fuse_arg *args,
570 for (i = 0; !err && i < numargs; i++) {
571 struct fuse_arg *arg = &args[i];
572 if (i == numargs - 1 && argpages)
573 err = fuse_copy_pages(cs, arg->size, zeroing);
575 err = fuse_copy_one(cs, arg->value, arg->size);
580 /* Wait until a request is available on the pending list */
581 static void request_wait(struct fuse_conn *fc)
583 DECLARE_WAITQUEUE(wait, current);
585 add_wait_queue_exclusive(&fc->waitq, &wait);
586 while (fc->mounted && list_empty(&fc->pending)) {
587 set_current_state(TASK_INTERRUPTIBLE);
588 if (signal_pending(current))
591 spin_unlock(&fuse_lock);
593 spin_lock(&fuse_lock);
595 set_current_state(TASK_RUNNING);
596 remove_wait_queue(&fc->waitq, &wait);
600 * Read a single request into the userspace filesystem's buffer. This
601 * function waits until a request is available, then removes it from
602 * the pending list and copies request data to userspace buffer. If
603 * no reply is needed (FORGET) or request has been interrupted or
604 * there was an error during the copying then it's finished by calling
605 * request_end(). Otherwise add it to the processing list, and set
608 static ssize_t fuse_dev_readv(struct file *file, const struct iovec *iov,
609 unsigned long nr_segs, loff_t *off)
612 struct fuse_conn *fc;
613 struct fuse_req *req;
615 struct fuse_copy_state cs;
618 spin_lock(&fuse_lock);
619 fc = file->private_data;
628 if (list_empty(&fc->pending))
631 req = list_entry(fc->pending.next, struct fuse_req, list);
632 list_del_init(&req->list);
633 spin_unlock(&fuse_lock);
636 reqsize = req->in.h.len;
637 fuse_copy_init(&cs, 1, req, iov, nr_segs);
639 if (iov_length(iov, nr_segs) >= reqsize) {
640 err = fuse_copy_one(&cs, &in->h, sizeof(in->h));
642 err = fuse_copy_args(&cs, in->numargs, in->argpages,
643 (struct fuse_arg *) in->args, 0);
645 fuse_copy_finish(&cs);
647 spin_lock(&fuse_lock);
649 if (!err && req->interrupted)
652 if (!req->interrupted)
653 req->out.h.error = -EIO;
654 request_end(fc, req);
658 request_end(fc, req);
661 list_add_tail(&req->list, &fc->processing);
662 spin_unlock(&fuse_lock);
667 spin_unlock(&fuse_lock);
671 static ssize_t fuse_dev_read(struct file *file, char __user *buf,
672 size_t nbytes, loff_t *off)
675 iov.iov_len = nbytes;
677 return fuse_dev_readv(file, &iov, 1, off);
680 /* Look up request on processing list by unique ID */
681 static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique)
683 struct list_head *entry;
685 list_for_each(entry, &fc->processing) {
686 struct fuse_req *req;
687 req = list_entry(entry, struct fuse_req, list);
688 if (req->in.h.unique == unique)
694 static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
697 unsigned reqsize = sizeof(struct fuse_out_header);
700 return nbytes != reqsize ? -EINVAL : 0;
702 reqsize += len_args(out->numargs, out->args);
704 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
706 else if (reqsize > nbytes) {
707 struct fuse_arg *lastarg = &out->args[out->numargs-1];
708 unsigned diffsize = reqsize - nbytes;
709 if (diffsize > lastarg->size)
711 lastarg->size -= diffsize;
713 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
718 * Write a single reply to a request. First the header is copied from
719 * the write buffer. The request is then searched on the processing
720 * list by the unique ID found in the header. If found, then remove
721 * it from the list and copy the rest of the buffer to the request.
722 * The request is finished by calling request_end()
724 static ssize_t fuse_dev_writev(struct file *file, const struct iovec *iov,
725 unsigned long nr_segs, loff_t *off)
728 unsigned nbytes = iov_length(iov, nr_segs);
729 struct fuse_req *req;
730 struct fuse_out_header oh;
731 struct fuse_copy_state cs;
732 struct fuse_conn *fc = fuse_get_conn(file);
736 fuse_copy_init(&cs, 0, NULL, iov, nr_segs);
737 if (nbytes < sizeof(struct fuse_out_header))
740 err = fuse_copy_one(&cs, &oh, sizeof(oh));
744 if (!oh.unique || oh.error <= -1000 || oh.error > 0 ||
748 spin_lock(&fuse_lock);
749 req = request_find(fc, oh.unique);
754 list_del_init(&req->list);
755 if (req->interrupted) {
756 request_end(fc, req);
757 fuse_copy_finish(&cs);
763 spin_unlock(&fuse_lock);
765 err = copy_out_args(&cs, &req->out, nbytes);
766 fuse_copy_finish(&cs);
768 spin_lock(&fuse_lock);
771 if (req->interrupted)
773 } else if (!req->interrupted)
774 req->out.h.error = -EIO;
775 request_end(fc, req);
777 return err ? err : nbytes;
780 spin_unlock(&fuse_lock);
782 fuse_copy_finish(&cs);
786 static ssize_t fuse_dev_write(struct file *file, const char __user *buf,
787 size_t nbytes, loff_t *off)
790 iov.iov_len = nbytes;
791 iov.iov_base = (char __user *) buf;
792 return fuse_dev_writev(file, &iov, 1, off);
795 static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
797 struct fuse_conn *fc = fuse_get_conn(file);
798 unsigned mask = POLLOUT | POLLWRNORM;
803 poll_wait(file, &fc->waitq, wait);
805 spin_lock(&fuse_lock);
806 if (!list_empty(&fc->pending))
807 mask |= POLLIN | POLLRDNORM;
808 spin_unlock(&fuse_lock);
813 /* Abort all requests on the given list (pending or processing) */
814 static void end_requests(struct fuse_conn *fc, struct list_head *head)
816 while (!list_empty(head)) {
817 struct fuse_req *req;
818 req = list_entry(head->next, struct fuse_req, list);
819 list_del_init(&req->list);
820 req->out.h.error = -ECONNABORTED;
821 request_end(fc, req);
822 spin_lock(&fuse_lock);
826 static int fuse_dev_release(struct inode *inode, struct file *file)
828 struct fuse_conn *fc;
830 spin_lock(&fuse_lock);
831 fc = file->private_data;
834 end_requests(fc, &fc->pending);
835 end_requests(fc, &fc->processing);
836 fuse_release_conn(fc);
838 spin_unlock(&fuse_lock);
842 struct file_operations fuse_dev_operations = {
843 .owner = THIS_MODULE,
845 .read = fuse_dev_read,
846 .readv = fuse_dev_readv,
847 .write = fuse_dev_write,
848 .writev = fuse_dev_writev,
849 .poll = fuse_dev_poll,
850 .release = fuse_dev_release,
853 static struct miscdevice fuse_miscdevice = {
856 .fops = &fuse_dev_operations,
859 int __init fuse_dev_init(void)
862 fuse_req_cachep = kmem_cache_create("fuse_request",
863 sizeof(struct fuse_req),
865 if (!fuse_req_cachep)
868 err = misc_register(&fuse_miscdevice);
870 goto out_cache_clean;
875 kmem_cache_destroy(fuse_req_cachep);
880 void fuse_dev_cleanup(void)
882 misc_deregister(&fuse_miscdevice);
883 kmem_cache_destroy(fuse_req_cachep);