ocfs2: fix locking for res->tracking and dlm->tracking_list
[linux/fpc-iii.git] / fs / fuse / dev.c
blob2671e922c7208dd138b14c071a3b039b03d1e8ba
1 /*
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.
6 See the file COPYING.
7 */
9 #include "fuse_i.h"
11 #include <linux/init.h>
12 #include <linux/module.h>
13 #include <linux/poll.h>
14 #include <linux/uio.h>
15 #include <linux/miscdevice.h>
16 #include <linux/pagemap.h>
17 #include <linux/file.h>
18 #include <linux/slab.h>
19 #include <linux/pipe_fs_i.h>
20 #include <linux/swap.h>
21 #include <linux/splice.h>
23 MODULE_ALIAS_MISCDEV(FUSE_MINOR);
24 MODULE_ALIAS("devname:fuse");
26 static struct kmem_cache *fuse_req_cachep;
28 static struct fuse_dev *fuse_get_dev(struct file *file)
31 * Lockless access is OK, because file->private data is set
32 * once during mount and is valid until the file is released.
34 return ACCESS_ONCE(file->private_data);
37 static void fuse_request_init(struct fuse_req *req, struct page **pages,
38 struct fuse_page_desc *page_descs,
39 unsigned npages)
41 memset(req, 0, sizeof(*req));
42 memset(pages, 0, sizeof(*pages) * npages);
43 memset(page_descs, 0, sizeof(*page_descs) * npages);
44 INIT_LIST_HEAD(&req->list);
45 INIT_LIST_HEAD(&req->intr_entry);
46 init_waitqueue_head(&req->waitq);
47 atomic_set(&req->count, 1);
48 req->pages = pages;
49 req->page_descs = page_descs;
50 req->max_pages = npages;
51 __set_bit(FR_PENDING, &req->flags);
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);
57 if (req) {
58 struct page **pages;
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;
64 } else {
65 pages = kmalloc(sizeof(struct page *) * npages, flags);
66 page_descs = kmalloc(sizeof(struct fuse_page_desc) *
67 npages, flags);
70 if (!pages || !page_descs) {
71 kfree(pages);
72 kfree(page_descs);
73 kmem_cache_free(fuse_req_cachep, req);
74 return NULL;
77 fuse_request_init(req, pages, page_descs, npages);
79 return req;
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) {
96 kfree(req->pages);
97 kfree(req->page_descs);
99 kmem_cache_free(fuse_req_cachep, req);
102 static void block_sigs(sigset_t *oldset)
104 sigset_t mask;
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 void fuse_set_initialized(struct fuse_conn *fc)
136 /* Make sure stores before this are seen on another CPU */
137 smp_wmb();
138 fc->initialized = 1;
141 static bool fuse_block_alloc(struct fuse_conn *fc, bool for_background)
143 return !fc->initialized || (for_background && fc->blocked);
146 static void fuse_drop_waiting(struct fuse_conn *fc)
148 if (fc->connected) {
149 atomic_dec(&fc->num_waiting);
150 } else if (atomic_dec_and_test(&fc->num_waiting)) {
151 /* wake up aborters */
152 wake_up_all(&fc->blocked_waitq);
156 static struct fuse_req *__fuse_get_req(struct fuse_conn *fc, unsigned npages,
157 bool for_background)
159 struct fuse_req *req;
160 int err;
161 atomic_inc(&fc->num_waiting);
163 if (fuse_block_alloc(fc, for_background)) {
164 sigset_t oldset;
165 int intr;
167 block_sigs(&oldset);
168 intr = wait_event_interruptible_exclusive(fc->blocked_waitq,
169 !fuse_block_alloc(fc, for_background));
170 restore_sigs(&oldset);
171 err = -EINTR;
172 if (intr)
173 goto out;
175 /* Matches smp_wmb() in fuse_set_initialized() */
176 smp_rmb();
178 err = -ENOTCONN;
179 if (!fc->connected)
180 goto out;
182 err = -ECONNREFUSED;
183 if (fc->conn_error)
184 goto out;
186 req = fuse_request_alloc(npages);
187 err = -ENOMEM;
188 if (!req) {
189 if (for_background)
190 wake_up(&fc->blocked_waitq);
191 goto out;
194 fuse_req_init_context(req);
195 __set_bit(FR_WAITING, &req->flags);
196 if (for_background)
197 __set_bit(FR_BACKGROUND, &req->flags);
199 return req;
201 out:
202 fuse_drop_waiting(fc);
203 return ERR_PTR(err);
206 struct fuse_req *fuse_get_req(struct fuse_conn *fc, unsigned npages)
208 return __fuse_get_req(fc, npages, false);
210 EXPORT_SYMBOL_GPL(fuse_get_req);
212 struct fuse_req *fuse_get_req_for_background(struct fuse_conn *fc,
213 unsigned npages)
215 return __fuse_get_req(fc, npages, true);
217 EXPORT_SYMBOL_GPL(fuse_get_req_for_background);
220 * Return request in fuse_file->reserved_req. However that may
221 * currently be in use. If that is the case, wait for it to become
222 * available.
224 static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
225 struct file *file)
227 struct fuse_req *req = NULL;
228 struct fuse_file *ff = file->private_data;
230 do {
231 wait_event(fc->reserved_req_waitq, ff->reserved_req);
232 spin_lock(&fc->lock);
233 if (ff->reserved_req) {
234 req = ff->reserved_req;
235 ff->reserved_req = NULL;
236 req->stolen_file = get_file(file);
238 spin_unlock(&fc->lock);
239 } while (!req);
241 return req;
245 * Put stolen request back into fuse_file->reserved_req
247 static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
249 struct file *file = req->stolen_file;
250 struct fuse_file *ff = file->private_data;
252 spin_lock(&fc->lock);
253 fuse_request_init(req, req->pages, req->page_descs, req->max_pages);
254 BUG_ON(ff->reserved_req);
255 ff->reserved_req = req;
256 wake_up_all(&fc->reserved_req_waitq);
257 spin_unlock(&fc->lock);
258 fput(file);
262 * Gets a requests for a file operation, always succeeds
264 * This is used for sending the FLUSH request, which must get to
265 * userspace, due to POSIX locks which may need to be unlocked.
267 * If allocation fails due to OOM, use the reserved request in
268 * fuse_file.
270 * This is very unlikely to deadlock accidentally, since the
271 * filesystem should not have it's own file open. If deadlock is
272 * intentional, it can still be broken by "aborting" the filesystem.
274 struct fuse_req *fuse_get_req_nofail_nopages(struct fuse_conn *fc,
275 struct file *file)
277 struct fuse_req *req;
279 atomic_inc(&fc->num_waiting);
280 wait_event(fc->blocked_waitq, fc->initialized);
281 /* Matches smp_wmb() in fuse_set_initialized() */
282 smp_rmb();
283 req = fuse_request_alloc(0);
284 if (!req)
285 req = get_reserved_req(fc, file);
287 fuse_req_init_context(req);
288 __set_bit(FR_WAITING, &req->flags);
289 __clear_bit(FR_BACKGROUND, &req->flags);
290 return req;
293 void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
295 if (atomic_dec_and_test(&req->count)) {
296 if (test_bit(FR_BACKGROUND, &req->flags)) {
298 * We get here in the unlikely case that a background
299 * request was allocated but not sent
301 spin_lock(&fc->lock);
302 if (!fc->blocked)
303 wake_up(&fc->blocked_waitq);
304 spin_unlock(&fc->lock);
307 if (test_bit(FR_WAITING, &req->flags)) {
308 __clear_bit(FR_WAITING, &req->flags);
309 fuse_drop_waiting(fc);
312 if (req->stolen_file)
313 put_reserved_req(fc, req);
314 else
315 fuse_request_free(req);
318 EXPORT_SYMBOL_GPL(fuse_put_request);
320 static unsigned len_args(unsigned numargs, struct fuse_arg *args)
322 unsigned nbytes = 0;
323 unsigned i;
325 for (i = 0; i < numargs; i++)
326 nbytes += args[i].size;
328 return nbytes;
331 static u64 fuse_get_unique(struct fuse_iqueue *fiq)
333 return ++fiq->reqctr;
336 static void queue_request(struct fuse_iqueue *fiq, struct fuse_req *req)
338 req->in.h.len = sizeof(struct fuse_in_header) +
339 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
340 list_add_tail(&req->list, &fiq->pending);
341 wake_up_locked(&fiq->waitq);
342 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
345 void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
346 u64 nodeid, u64 nlookup)
348 struct fuse_iqueue *fiq = &fc->iq;
350 forget->forget_one.nodeid = nodeid;
351 forget->forget_one.nlookup = nlookup;
353 spin_lock(&fiq->waitq.lock);
354 if (fiq->connected) {
355 fiq->forget_list_tail->next = forget;
356 fiq->forget_list_tail = forget;
357 wake_up_locked(&fiq->waitq);
358 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
359 } else {
360 kfree(forget);
362 spin_unlock(&fiq->waitq.lock);
365 static void flush_bg_queue(struct fuse_conn *fc)
367 while (fc->active_background < fc->max_background &&
368 !list_empty(&fc->bg_queue)) {
369 struct fuse_req *req;
370 struct fuse_iqueue *fiq = &fc->iq;
372 req = list_entry(fc->bg_queue.next, struct fuse_req, list);
373 list_del(&req->list);
374 fc->active_background++;
375 spin_lock(&fiq->waitq.lock);
376 req->in.h.unique = fuse_get_unique(fiq);
377 queue_request(fiq, req);
378 spin_unlock(&fiq->waitq.lock);
383 * This function is called when a request is finished. Either a reply
384 * has arrived or it was aborted (and not yet sent) or some error
385 * occurred during communication with userspace, or the device file
386 * was closed. The requester thread is woken up (if still waiting),
387 * the 'end' callback is called if given, else the reference to the
388 * request is released
390 static void request_end(struct fuse_conn *fc, struct fuse_req *req)
392 struct fuse_iqueue *fiq = &fc->iq;
394 if (test_and_set_bit(FR_FINISHED, &req->flags))
395 goto put_request;
397 spin_lock(&fiq->waitq.lock);
398 list_del_init(&req->intr_entry);
399 spin_unlock(&fiq->waitq.lock);
400 WARN_ON(test_bit(FR_PENDING, &req->flags));
401 WARN_ON(test_bit(FR_SENT, &req->flags));
402 if (test_bit(FR_BACKGROUND, &req->flags)) {
403 spin_lock(&fc->lock);
404 clear_bit(FR_BACKGROUND, &req->flags);
405 if (fc->num_background == fc->max_background)
406 fc->blocked = 0;
408 /* Wake up next waiter, if any */
409 if (!fc->blocked && waitqueue_active(&fc->blocked_waitq))
410 wake_up(&fc->blocked_waitq);
412 if (fc->num_background == fc->congestion_threshold &&
413 fc->connected && fc->bdi_initialized) {
414 clear_bdi_congested(&fc->bdi, BLK_RW_SYNC);
415 clear_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
417 fc->num_background--;
418 fc->active_background--;
419 flush_bg_queue(fc);
420 spin_unlock(&fc->lock);
422 wake_up(&req->waitq);
423 if (req->end)
424 req->end(fc, req);
425 put_request:
426 fuse_put_request(fc, req);
429 static void queue_interrupt(struct fuse_iqueue *fiq, struct fuse_req *req)
431 spin_lock(&fiq->waitq.lock);
432 if (test_bit(FR_FINISHED, &req->flags)) {
433 spin_unlock(&fiq->waitq.lock);
434 return;
436 if (list_empty(&req->intr_entry)) {
437 list_add_tail(&req->intr_entry, &fiq->interrupts);
438 wake_up_locked(&fiq->waitq);
440 spin_unlock(&fiq->waitq.lock);
441 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
444 static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
446 struct fuse_iqueue *fiq = &fc->iq;
447 int err;
449 if (!fc->no_interrupt) {
450 /* Any signal may interrupt this */
451 err = wait_event_interruptible(req->waitq,
452 test_bit(FR_FINISHED, &req->flags));
453 if (!err)
454 return;
456 set_bit(FR_INTERRUPTED, &req->flags);
457 /* matches barrier in fuse_dev_do_read() */
458 smp_mb__after_atomic();
459 if (test_bit(FR_SENT, &req->flags))
460 queue_interrupt(fiq, req);
463 if (!test_bit(FR_FORCE, &req->flags)) {
464 sigset_t oldset;
466 /* Only fatal signals may interrupt this */
467 block_sigs(&oldset);
468 err = wait_event_interruptible(req->waitq,
469 test_bit(FR_FINISHED, &req->flags));
470 restore_sigs(&oldset);
472 if (!err)
473 return;
475 spin_lock(&fiq->waitq.lock);
476 /* Request is not yet in userspace, bail out */
477 if (test_bit(FR_PENDING, &req->flags)) {
478 list_del(&req->list);
479 spin_unlock(&fiq->waitq.lock);
480 __fuse_put_request(req);
481 req->out.h.error = -EINTR;
482 return;
484 spin_unlock(&fiq->waitq.lock);
488 * Either request is already in userspace, or it was forced.
489 * Wait it out.
491 wait_event(req->waitq, test_bit(FR_FINISHED, &req->flags));
494 static void __fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
496 struct fuse_iqueue *fiq = &fc->iq;
498 BUG_ON(test_bit(FR_BACKGROUND, &req->flags));
499 spin_lock(&fiq->waitq.lock);
500 if (!fiq->connected) {
501 spin_unlock(&fiq->waitq.lock);
502 req->out.h.error = -ENOTCONN;
503 } else {
504 req->in.h.unique = fuse_get_unique(fiq);
505 queue_request(fiq, req);
506 /* acquire extra reference, since request is still needed
507 after request_end() */
508 __fuse_get_request(req);
509 spin_unlock(&fiq->waitq.lock);
511 request_wait_answer(fc, req);
512 /* Pairs with smp_wmb() in request_end() */
513 smp_rmb();
517 void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
519 __set_bit(FR_ISREPLY, &req->flags);
520 if (!test_bit(FR_WAITING, &req->flags)) {
521 __set_bit(FR_WAITING, &req->flags);
522 atomic_inc(&fc->num_waiting);
524 __fuse_request_send(fc, req);
526 EXPORT_SYMBOL_GPL(fuse_request_send);
528 static void fuse_adjust_compat(struct fuse_conn *fc, struct fuse_args *args)
530 if (fc->minor < 4 && args->in.h.opcode == FUSE_STATFS)
531 args->out.args[0].size = FUSE_COMPAT_STATFS_SIZE;
533 if (fc->minor < 9) {
534 switch (args->in.h.opcode) {
535 case FUSE_LOOKUP:
536 case FUSE_CREATE:
537 case FUSE_MKNOD:
538 case FUSE_MKDIR:
539 case FUSE_SYMLINK:
540 case FUSE_LINK:
541 args->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
542 break;
543 case FUSE_GETATTR:
544 case FUSE_SETATTR:
545 args->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
546 break;
549 if (fc->minor < 12) {
550 switch (args->in.h.opcode) {
551 case FUSE_CREATE:
552 args->in.args[0].size = sizeof(struct fuse_open_in);
553 break;
554 case FUSE_MKNOD:
555 args->in.args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE;
556 break;
561 ssize_t fuse_simple_request(struct fuse_conn *fc, struct fuse_args *args)
563 struct fuse_req *req;
564 ssize_t ret;
566 req = fuse_get_req(fc, 0);
567 if (IS_ERR(req))
568 return PTR_ERR(req);
570 /* Needs to be done after fuse_get_req() so that fc->minor is valid */
571 fuse_adjust_compat(fc, args);
573 req->in.h.opcode = args->in.h.opcode;
574 req->in.h.nodeid = args->in.h.nodeid;
575 req->in.numargs = args->in.numargs;
576 memcpy(req->in.args, args->in.args,
577 args->in.numargs * sizeof(struct fuse_in_arg));
578 req->out.argvar = args->out.argvar;
579 req->out.numargs = args->out.numargs;
580 memcpy(req->out.args, args->out.args,
581 args->out.numargs * sizeof(struct fuse_arg));
582 fuse_request_send(fc, req);
583 ret = req->out.h.error;
584 if (!ret && args->out.argvar) {
585 BUG_ON(args->out.numargs != 1);
586 ret = req->out.args[0].size;
588 fuse_put_request(fc, req);
590 return ret;
594 * Called under fc->lock
596 * fc->connected must have been checked previously
598 void fuse_request_send_background_locked(struct fuse_conn *fc,
599 struct fuse_req *req)
601 BUG_ON(!test_bit(FR_BACKGROUND, &req->flags));
602 if (!test_bit(FR_WAITING, &req->flags)) {
603 __set_bit(FR_WAITING, &req->flags);
604 atomic_inc(&fc->num_waiting);
606 __set_bit(FR_ISREPLY, &req->flags);
607 fc->num_background++;
608 if (fc->num_background == fc->max_background)
609 fc->blocked = 1;
610 if (fc->num_background == fc->congestion_threshold &&
611 fc->bdi_initialized) {
612 set_bdi_congested(&fc->bdi, BLK_RW_SYNC);
613 set_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
615 list_add_tail(&req->list, &fc->bg_queue);
616 flush_bg_queue(fc);
619 void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
621 BUG_ON(!req->end);
622 spin_lock(&fc->lock);
623 if (fc->connected) {
624 fuse_request_send_background_locked(fc, req);
625 spin_unlock(&fc->lock);
626 } else {
627 spin_unlock(&fc->lock);
628 req->out.h.error = -ENOTCONN;
629 req->end(fc, req);
630 fuse_put_request(fc, req);
633 EXPORT_SYMBOL_GPL(fuse_request_send_background);
635 static int fuse_request_send_notify_reply(struct fuse_conn *fc,
636 struct fuse_req *req, u64 unique)
638 int err = -ENODEV;
639 struct fuse_iqueue *fiq = &fc->iq;
641 __clear_bit(FR_ISREPLY, &req->flags);
642 req->in.h.unique = unique;
643 spin_lock(&fiq->waitq.lock);
644 if (fiq->connected) {
645 queue_request(fiq, req);
646 err = 0;
648 spin_unlock(&fiq->waitq.lock);
650 return err;
653 void fuse_force_forget(struct file *file, u64 nodeid)
655 struct inode *inode = file_inode(file);
656 struct fuse_conn *fc = get_fuse_conn(inode);
657 struct fuse_req *req;
658 struct fuse_forget_in inarg;
660 memset(&inarg, 0, sizeof(inarg));
661 inarg.nlookup = 1;
662 req = fuse_get_req_nofail_nopages(fc, file);
663 req->in.h.opcode = FUSE_FORGET;
664 req->in.h.nodeid = nodeid;
665 req->in.numargs = 1;
666 req->in.args[0].size = sizeof(inarg);
667 req->in.args[0].value = &inarg;
668 __clear_bit(FR_ISREPLY, &req->flags);
669 __fuse_request_send(fc, req);
670 /* ignore errors */
671 fuse_put_request(fc, req);
675 * Lock the request. Up to the next unlock_request() there mustn't be
676 * anything that could cause a page-fault. If the request was already
677 * aborted bail out.
679 static int lock_request(struct fuse_req *req)
681 int err = 0;
682 if (req) {
683 spin_lock(&req->waitq.lock);
684 if (test_bit(FR_ABORTED, &req->flags))
685 err = -ENOENT;
686 else
687 set_bit(FR_LOCKED, &req->flags);
688 spin_unlock(&req->waitq.lock);
690 return err;
694 * Unlock request. If it was aborted while locked, caller is responsible
695 * for unlocking and ending the request.
697 static int unlock_request(struct fuse_req *req)
699 int err = 0;
700 if (req) {
701 spin_lock(&req->waitq.lock);
702 if (test_bit(FR_ABORTED, &req->flags))
703 err = -ENOENT;
704 else
705 clear_bit(FR_LOCKED, &req->flags);
706 spin_unlock(&req->waitq.lock);
708 return err;
711 struct fuse_copy_state {
712 int write;
713 struct fuse_req *req;
714 struct iov_iter *iter;
715 struct pipe_buffer *pipebufs;
716 struct pipe_buffer *currbuf;
717 struct pipe_inode_info *pipe;
718 unsigned long nr_segs;
719 struct page *pg;
720 unsigned len;
721 unsigned offset;
722 unsigned move_pages:1;
725 static void fuse_copy_init(struct fuse_copy_state *cs, int write,
726 struct iov_iter *iter)
728 memset(cs, 0, sizeof(*cs));
729 cs->write = write;
730 cs->iter = iter;
733 /* Unmap and put previous page of userspace buffer */
734 static void fuse_copy_finish(struct fuse_copy_state *cs)
736 if (cs->currbuf) {
737 struct pipe_buffer *buf = cs->currbuf;
739 if (cs->write)
740 buf->len = PAGE_SIZE - cs->len;
741 cs->currbuf = NULL;
742 } else if (cs->pg) {
743 if (cs->write) {
744 flush_dcache_page(cs->pg);
745 set_page_dirty_lock(cs->pg);
747 put_page(cs->pg);
749 cs->pg = NULL;
753 * Get another pagefull of userspace buffer, and map it to kernel
754 * address space, and lock request
756 static int fuse_copy_fill(struct fuse_copy_state *cs)
758 struct page *page;
759 int err;
761 err = unlock_request(cs->req);
762 if (err)
763 return err;
765 fuse_copy_finish(cs);
766 if (cs->pipebufs) {
767 struct pipe_buffer *buf = cs->pipebufs;
769 if (!cs->write) {
770 err = buf->ops->confirm(cs->pipe, buf);
771 if (err)
772 return err;
774 BUG_ON(!cs->nr_segs);
775 cs->currbuf = buf;
776 cs->pg = buf->page;
777 cs->offset = buf->offset;
778 cs->len = buf->len;
779 cs->pipebufs++;
780 cs->nr_segs--;
781 } else {
782 if (cs->nr_segs == cs->pipe->buffers)
783 return -EIO;
785 page = alloc_page(GFP_HIGHUSER);
786 if (!page)
787 return -ENOMEM;
789 buf->page = page;
790 buf->offset = 0;
791 buf->len = 0;
793 cs->currbuf = buf;
794 cs->pg = page;
795 cs->offset = 0;
796 cs->len = PAGE_SIZE;
797 cs->pipebufs++;
798 cs->nr_segs++;
800 } else {
801 size_t off;
802 err = iov_iter_get_pages(cs->iter, &page, PAGE_SIZE, 1, &off);
803 if (err < 0)
804 return err;
805 BUG_ON(!err);
806 cs->len = err;
807 cs->offset = off;
808 cs->pg = page;
809 cs->offset = off;
810 iov_iter_advance(cs->iter, err);
813 return lock_request(cs->req);
816 /* Do as much copy to/from userspace buffer as we can */
817 static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
819 unsigned ncpy = min(*size, cs->len);
820 if (val) {
821 void *pgaddr = kmap_atomic(cs->pg);
822 void *buf = pgaddr + cs->offset;
824 if (cs->write)
825 memcpy(buf, *val, ncpy);
826 else
827 memcpy(*val, buf, ncpy);
829 kunmap_atomic(pgaddr);
830 *val += ncpy;
832 *size -= ncpy;
833 cs->len -= ncpy;
834 cs->offset += ncpy;
835 return ncpy;
838 static int fuse_check_page(struct page *page)
840 if (page_mapcount(page) ||
841 page->mapping != NULL ||
842 page_count(page) != 1 ||
843 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
844 ~(1 << PG_locked |
845 1 << PG_referenced |
846 1 << PG_uptodate |
847 1 << PG_lru |
848 1 << PG_active |
849 1 << PG_reclaim))) {
850 printk(KERN_WARNING "fuse: trying to steal weird page\n");
851 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);
852 return 1;
854 return 0;
857 static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
859 int err;
860 struct page *oldpage = *pagep;
861 struct page *newpage;
862 struct pipe_buffer *buf = cs->pipebufs;
864 err = unlock_request(cs->req);
865 if (err)
866 return err;
868 fuse_copy_finish(cs);
870 err = buf->ops->confirm(cs->pipe, buf);
871 if (err)
872 return err;
874 BUG_ON(!cs->nr_segs);
875 cs->currbuf = buf;
876 cs->len = buf->len;
877 cs->pipebufs++;
878 cs->nr_segs--;
880 if (cs->len != PAGE_SIZE)
881 goto out_fallback;
883 if (buf->ops->steal(cs->pipe, buf) != 0)
884 goto out_fallback;
886 newpage = buf->page;
888 if (!PageUptodate(newpage))
889 SetPageUptodate(newpage);
891 ClearPageMappedToDisk(newpage);
893 if (fuse_check_page(newpage) != 0)
894 goto out_fallback_unlock;
897 * This is a new and locked page, it shouldn't be mapped or
898 * have any special flags on it
900 if (WARN_ON(page_mapped(oldpage)))
901 goto out_fallback_unlock;
902 if (WARN_ON(page_has_private(oldpage)))
903 goto out_fallback_unlock;
904 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
905 goto out_fallback_unlock;
906 if (WARN_ON(PageMlocked(oldpage)))
907 goto out_fallback_unlock;
909 err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
910 if (err) {
911 unlock_page(newpage);
912 return err;
915 page_cache_get(newpage);
917 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
918 lru_cache_add_file(newpage);
920 err = 0;
921 spin_lock(&cs->req->waitq.lock);
922 if (test_bit(FR_ABORTED, &cs->req->flags))
923 err = -ENOENT;
924 else
925 *pagep = newpage;
926 spin_unlock(&cs->req->waitq.lock);
928 if (err) {
929 unlock_page(newpage);
930 page_cache_release(newpage);
931 return err;
934 unlock_page(oldpage);
935 page_cache_release(oldpage);
936 cs->len = 0;
938 return 0;
940 out_fallback_unlock:
941 unlock_page(newpage);
942 out_fallback:
943 cs->pg = buf->page;
944 cs->offset = buf->offset;
946 err = lock_request(cs->req);
947 if (err)
948 return err;
950 return 1;
953 static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
954 unsigned offset, unsigned count)
956 struct pipe_buffer *buf;
957 int err;
959 if (cs->nr_segs == cs->pipe->buffers)
960 return -EIO;
962 err = unlock_request(cs->req);
963 if (err)
964 return err;
966 fuse_copy_finish(cs);
968 buf = cs->pipebufs;
969 page_cache_get(page);
970 buf->page = page;
971 buf->offset = offset;
972 buf->len = count;
974 cs->pipebufs++;
975 cs->nr_segs++;
976 cs->len = 0;
978 return 0;
982 * Copy a page in the request to/from the userspace buffer. Must be
983 * done atomically
985 static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
986 unsigned offset, unsigned count, int zeroing)
988 int err;
989 struct page *page = *pagep;
991 if (page && zeroing && count < PAGE_SIZE)
992 clear_highpage(page);
994 while (count) {
995 if (cs->write && cs->pipebufs && page) {
996 return fuse_ref_page(cs, page, offset, count);
997 } else if (!cs->len) {
998 if (cs->move_pages && page &&
999 offset == 0 && count == PAGE_SIZE) {
1000 err = fuse_try_move_page(cs, pagep);
1001 if (err <= 0)
1002 return err;
1003 } else {
1004 err = fuse_copy_fill(cs);
1005 if (err)
1006 return err;
1009 if (page) {
1010 void *mapaddr = kmap_atomic(page);
1011 void *buf = mapaddr + offset;
1012 offset += fuse_copy_do(cs, &buf, &count);
1013 kunmap_atomic(mapaddr);
1014 } else
1015 offset += fuse_copy_do(cs, NULL, &count);
1017 if (page && !cs->write)
1018 flush_dcache_page(page);
1019 return 0;
1022 /* Copy pages in the request to/from userspace buffer */
1023 static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
1024 int zeroing)
1026 unsigned i;
1027 struct fuse_req *req = cs->req;
1029 for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
1030 int err;
1031 unsigned offset = req->page_descs[i].offset;
1032 unsigned count = min(nbytes, req->page_descs[i].length);
1034 err = fuse_copy_page(cs, &req->pages[i], offset, count,
1035 zeroing);
1036 if (err)
1037 return err;
1039 nbytes -= count;
1041 return 0;
1044 /* Copy a single argument in the request to/from userspace buffer */
1045 static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
1047 while (size) {
1048 if (!cs->len) {
1049 int err = fuse_copy_fill(cs);
1050 if (err)
1051 return err;
1053 fuse_copy_do(cs, &val, &size);
1055 return 0;
1058 /* Copy request arguments to/from userspace buffer */
1059 static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
1060 unsigned argpages, struct fuse_arg *args,
1061 int zeroing)
1063 int err = 0;
1064 unsigned i;
1066 for (i = 0; !err && i < numargs; i++) {
1067 struct fuse_arg *arg = &args[i];
1068 if (i == numargs - 1 && argpages)
1069 err = fuse_copy_pages(cs, arg->size, zeroing);
1070 else
1071 err = fuse_copy_one(cs, arg->value, arg->size);
1073 return err;
1076 static int forget_pending(struct fuse_iqueue *fiq)
1078 return fiq->forget_list_head.next != NULL;
1081 static int request_pending(struct fuse_iqueue *fiq)
1083 return !list_empty(&fiq->pending) || !list_empty(&fiq->interrupts) ||
1084 forget_pending(fiq);
1088 * Transfer an interrupt request to userspace
1090 * Unlike other requests this is assembled on demand, without a need
1091 * to allocate a separate fuse_req structure.
1093 * Called with fiq->waitq.lock held, releases it
1095 static int fuse_read_interrupt(struct fuse_iqueue *fiq,
1096 struct fuse_copy_state *cs,
1097 size_t nbytes, struct fuse_req *req)
1098 __releases(fiq->waitq.lock)
1100 struct fuse_in_header ih;
1101 struct fuse_interrupt_in arg;
1102 unsigned reqsize = sizeof(ih) + sizeof(arg);
1103 int err;
1105 list_del_init(&req->intr_entry);
1106 req->intr_unique = fuse_get_unique(fiq);
1107 memset(&ih, 0, sizeof(ih));
1108 memset(&arg, 0, sizeof(arg));
1109 ih.len = reqsize;
1110 ih.opcode = FUSE_INTERRUPT;
1111 ih.unique = req->intr_unique;
1112 arg.unique = req->in.h.unique;
1114 spin_unlock(&fiq->waitq.lock);
1115 if (nbytes < reqsize)
1116 return -EINVAL;
1118 err = fuse_copy_one(cs, &ih, sizeof(ih));
1119 if (!err)
1120 err = fuse_copy_one(cs, &arg, sizeof(arg));
1121 fuse_copy_finish(cs);
1123 return err ? err : reqsize;
1126 static struct fuse_forget_link *dequeue_forget(struct fuse_iqueue *fiq,
1127 unsigned max,
1128 unsigned *countp)
1130 struct fuse_forget_link *head = fiq->forget_list_head.next;
1131 struct fuse_forget_link **newhead = &head;
1132 unsigned count;
1134 for (count = 0; *newhead != NULL && count < max; count++)
1135 newhead = &(*newhead)->next;
1137 fiq->forget_list_head.next = *newhead;
1138 *newhead = NULL;
1139 if (fiq->forget_list_head.next == NULL)
1140 fiq->forget_list_tail = &fiq->forget_list_head;
1142 if (countp != NULL)
1143 *countp = count;
1145 return head;
1148 static int fuse_read_single_forget(struct fuse_iqueue *fiq,
1149 struct fuse_copy_state *cs,
1150 size_t nbytes)
1151 __releases(fiq->waitq.lock)
1153 int err;
1154 struct fuse_forget_link *forget = dequeue_forget(fiq, 1, NULL);
1155 struct fuse_forget_in arg = {
1156 .nlookup = forget->forget_one.nlookup,
1158 struct fuse_in_header ih = {
1159 .opcode = FUSE_FORGET,
1160 .nodeid = forget->forget_one.nodeid,
1161 .unique = fuse_get_unique(fiq),
1162 .len = sizeof(ih) + sizeof(arg),
1165 spin_unlock(&fiq->waitq.lock);
1166 kfree(forget);
1167 if (nbytes < ih.len)
1168 return -EINVAL;
1170 err = fuse_copy_one(cs, &ih, sizeof(ih));
1171 if (!err)
1172 err = fuse_copy_one(cs, &arg, sizeof(arg));
1173 fuse_copy_finish(cs);
1175 if (err)
1176 return err;
1178 return ih.len;
1181 static int fuse_read_batch_forget(struct fuse_iqueue *fiq,
1182 struct fuse_copy_state *cs, size_t nbytes)
1183 __releases(fiq->waitq.lock)
1185 int err;
1186 unsigned max_forgets;
1187 unsigned count;
1188 struct fuse_forget_link *head;
1189 struct fuse_batch_forget_in arg = { .count = 0 };
1190 struct fuse_in_header ih = {
1191 .opcode = FUSE_BATCH_FORGET,
1192 .unique = fuse_get_unique(fiq),
1193 .len = sizeof(ih) + sizeof(arg),
1196 if (nbytes < ih.len) {
1197 spin_unlock(&fiq->waitq.lock);
1198 return -EINVAL;
1201 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
1202 head = dequeue_forget(fiq, max_forgets, &count);
1203 spin_unlock(&fiq->waitq.lock);
1205 arg.count = count;
1206 ih.len += count * sizeof(struct fuse_forget_one);
1207 err = fuse_copy_one(cs, &ih, sizeof(ih));
1208 if (!err)
1209 err = fuse_copy_one(cs, &arg, sizeof(arg));
1211 while (head) {
1212 struct fuse_forget_link *forget = head;
1214 if (!err) {
1215 err = fuse_copy_one(cs, &forget->forget_one,
1216 sizeof(forget->forget_one));
1218 head = forget->next;
1219 kfree(forget);
1222 fuse_copy_finish(cs);
1224 if (err)
1225 return err;
1227 return ih.len;
1230 static int fuse_read_forget(struct fuse_conn *fc, struct fuse_iqueue *fiq,
1231 struct fuse_copy_state *cs,
1232 size_t nbytes)
1233 __releases(fiq->waitq.lock)
1235 if (fc->minor < 16 || fiq->forget_list_head.next->next == NULL)
1236 return fuse_read_single_forget(fiq, cs, nbytes);
1237 else
1238 return fuse_read_batch_forget(fiq, cs, nbytes);
1242 * Read a single request into the userspace filesystem's buffer. This
1243 * function waits until a request is available, then removes it from
1244 * the pending list and copies request data to userspace buffer. If
1245 * no reply is needed (FORGET) or request has been aborted or there
1246 * was an error during the copying then it's finished by calling
1247 * request_end(). Otherwise add it to the processing list, and set
1248 * the 'sent' flag.
1250 static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file,
1251 struct fuse_copy_state *cs, size_t nbytes)
1253 ssize_t err;
1254 struct fuse_conn *fc = fud->fc;
1255 struct fuse_iqueue *fiq = &fc->iq;
1256 struct fuse_pqueue *fpq = &fud->pq;
1257 struct fuse_req *req;
1258 struct fuse_in *in;
1259 unsigned reqsize;
1261 restart:
1262 spin_lock(&fiq->waitq.lock);
1263 err = -EAGAIN;
1264 if ((file->f_flags & O_NONBLOCK) && fiq->connected &&
1265 !request_pending(fiq))
1266 goto err_unlock;
1268 err = wait_event_interruptible_exclusive_locked(fiq->waitq,
1269 !fiq->connected || request_pending(fiq));
1270 if (err)
1271 goto err_unlock;
1273 err = -ENODEV;
1274 if (!fiq->connected)
1275 goto err_unlock;
1277 if (!list_empty(&fiq->interrupts)) {
1278 req = list_entry(fiq->interrupts.next, struct fuse_req,
1279 intr_entry);
1280 return fuse_read_interrupt(fiq, cs, nbytes, req);
1283 if (forget_pending(fiq)) {
1284 if (list_empty(&fiq->pending) || fiq->forget_batch-- > 0)
1285 return fuse_read_forget(fc, fiq, cs, nbytes);
1287 if (fiq->forget_batch <= -8)
1288 fiq->forget_batch = 16;
1291 req = list_entry(fiq->pending.next, struct fuse_req, list);
1292 clear_bit(FR_PENDING, &req->flags);
1293 list_del_init(&req->list);
1294 spin_unlock(&fiq->waitq.lock);
1296 in = &req->in;
1297 reqsize = in->h.len;
1298 /* If request is too large, reply with an error and restart the read */
1299 if (nbytes < reqsize) {
1300 req->out.h.error = -EIO;
1301 /* SETXATTR is special, since it may contain too large data */
1302 if (in->h.opcode == FUSE_SETXATTR)
1303 req->out.h.error = -E2BIG;
1304 request_end(fc, req);
1305 goto restart;
1307 spin_lock(&fpq->lock);
1308 list_add(&req->list, &fpq->io);
1309 spin_unlock(&fpq->lock);
1310 cs->req = req;
1311 err = fuse_copy_one(cs, &in->h, sizeof(in->h));
1312 if (!err)
1313 err = fuse_copy_args(cs, in->numargs, in->argpages,
1314 (struct fuse_arg *) in->args, 0);
1315 fuse_copy_finish(cs);
1316 spin_lock(&fpq->lock);
1317 clear_bit(FR_LOCKED, &req->flags);
1318 if (!fpq->connected) {
1319 err = -ENODEV;
1320 goto out_end;
1322 if (err) {
1323 req->out.h.error = -EIO;
1324 goto out_end;
1326 if (!test_bit(FR_ISREPLY, &req->flags)) {
1327 err = reqsize;
1328 goto out_end;
1330 list_move_tail(&req->list, &fpq->processing);
1331 spin_unlock(&fpq->lock);
1332 set_bit(FR_SENT, &req->flags);
1333 /* matches barrier in request_wait_answer() */
1334 smp_mb__after_atomic();
1335 if (test_bit(FR_INTERRUPTED, &req->flags))
1336 queue_interrupt(fiq, req);
1338 return reqsize;
1340 out_end:
1341 if (!test_bit(FR_PRIVATE, &req->flags))
1342 list_del_init(&req->list);
1343 spin_unlock(&fpq->lock);
1344 request_end(fc, req);
1345 return err;
1347 err_unlock:
1348 spin_unlock(&fiq->waitq.lock);
1349 return err;
1352 static int fuse_dev_open(struct inode *inode, struct file *file)
1355 * The fuse device's file's private_data is used to hold
1356 * the fuse_conn(ection) when it is mounted, and is used to
1357 * keep track of whether the file has been mounted already.
1359 file->private_data = NULL;
1360 return 0;
1363 static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to)
1365 struct fuse_copy_state cs;
1366 struct file *file = iocb->ki_filp;
1367 struct fuse_dev *fud = fuse_get_dev(file);
1369 if (!fud)
1370 return -EPERM;
1372 if (!iter_is_iovec(to))
1373 return -EINVAL;
1375 fuse_copy_init(&cs, 1, to);
1377 return fuse_dev_do_read(fud, file, &cs, iov_iter_count(to));
1380 static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1381 struct pipe_inode_info *pipe,
1382 size_t len, unsigned int flags)
1384 int ret;
1385 int page_nr = 0;
1386 int do_wakeup = 0;
1387 struct pipe_buffer *bufs;
1388 struct fuse_copy_state cs;
1389 struct fuse_dev *fud = fuse_get_dev(in);
1391 if (!fud)
1392 return -EPERM;
1394 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
1395 if (!bufs)
1396 return -ENOMEM;
1398 fuse_copy_init(&cs, 1, NULL);
1399 cs.pipebufs = bufs;
1400 cs.pipe = pipe;
1401 ret = fuse_dev_do_read(fud, in, &cs, len);
1402 if (ret < 0)
1403 goto out;
1405 ret = 0;
1406 pipe_lock(pipe);
1408 if (!pipe->readers) {
1409 send_sig(SIGPIPE, current, 0);
1410 if (!ret)
1411 ret = -EPIPE;
1412 goto out_unlock;
1415 if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
1416 ret = -EIO;
1417 goto out_unlock;
1420 while (page_nr < cs.nr_segs) {
1421 int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
1422 struct pipe_buffer *buf = pipe->bufs + newbuf;
1424 buf->page = bufs[page_nr].page;
1425 buf->offset = bufs[page_nr].offset;
1426 buf->len = bufs[page_nr].len;
1428 * Need to be careful about this. Having buf->ops in module
1429 * code can Oops if the buffer persists after module unload.
1431 buf->ops = &nosteal_pipe_buf_ops;
1433 pipe->nrbufs++;
1434 page_nr++;
1435 ret += buf->len;
1437 if (pipe->files)
1438 do_wakeup = 1;
1441 out_unlock:
1442 pipe_unlock(pipe);
1444 if (do_wakeup) {
1445 smp_mb();
1446 if (waitqueue_active(&pipe->wait))
1447 wake_up_interruptible(&pipe->wait);
1448 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1451 out:
1452 for (; page_nr < cs.nr_segs; page_nr++)
1453 page_cache_release(bufs[page_nr].page);
1455 kfree(bufs);
1456 return ret;
1459 static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1460 struct fuse_copy_state *cs)
1462 struct fuse_notify_poll_wakeup_out outarg;
1463 int err = -EINVAL;
1465 if (size != sizeof(outarg))
1466 goto err;
1468 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1469 if (err)
1470 goto err;
1472 fuse_copy_finish(cs);
1473 return fuse_notify_poll_wakeup(fc, &outarg);
1475 err:
1476 fuse_copy_finish(cs);
1477 return err;
1480 static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1481 struct fuse_copy_state *cs)
1483 struct fuse_notify_inval_inode_out outarg;
1484 int err = -EINVAL;
1486 if (size != sizeof(outarg))
1487 goto err;
1489 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1490 if (err)
1491 goto err;
1492 fuse_copy_finish(cs);
1494 down_read(&fc->killsb);
1495 err = -ENOENT;
1496 if (fc->sb) {
1497 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1498 outarg.off, outarg.len);
1500 up_read(&fc->killsb);
1501 return err;
1503 err:
1504 fuse_copy_finish(cs);
1505 return err;
1508 static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1509 struct fuse_copy_state *cs)
1511 struct fuse_notify_inval_entry_out outarg;
1512 int err = -ENOMEM;
1513 char *buf;
1514 struct qstr name;
1516 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1517 if (!buf)
1518 goto err;
1520 err = -EINVAL;
1521 if (size < sizeof(outarg))
1522 goto err;
1524 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1525 if (err)
1526 goto err;
1528 err = -ENAMETOOLONG;
1529 if (outarg.namelen > FUSE_NAME_MAX)
1530 goto err;
1532 err = -EINVAL;
1533 if (size != sizeof(outarg) + outarg.namelen + 1)
1534 goto err;
1536 name.name = buf;
1537 name.len = outarg.namelen;
1538 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1539 if (err)
1540 goto err;
1541 fuse_copy_finish(cs);
1542 buf[outarg.namelen] = 0;
1543 name.hash = full_name_hash(name.name, name.len);
1545 down_read(&fc->killsb);
1546 err = -ENOENT;
1547 if (fc->sb)
1548 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
1549 up_read(&fc->killsb);
1550 kfree(buf);
1551 return err;
1553 err:
1554 kfree(buf);
1555 fuse_copy_finish(cs);
1556 return err;
1559 static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1560 struct fuse_copy_state *cs)
1562 struct fuse_notify_delete_out outarg;
1563 int err = -ENOMEM;
1564 char *buf;
1565 struct qstr name;
1567 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1568 if (!buf)
1569 goto err;
1571 err = -EINVAL;
1572 if (size < sizeof(outarg))
1573 goto err;
1575 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1576 if (err)
1577 goto err;
1579 err = -ENAMETOOLONG;
1580 if (outarg.namelen > FUSE_NAME_MAX)
1581 goto err;
1583 err = -EINVAL;
1584 if (size != sizeof(outarg) + outarg.namelen + 1)
1585 goto err;
1587 name.name = buf;
1588 name.len = outarg.namelen;
1589 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1590 if (err)
1591 goto err;
1592 fuse_copy_finish(cs);
1593 buf[outarg.namelen] = 0;
1594 name.hash = full_name_hash(name.name, name.len);
1596 down_read(&fc->killsb);
1597 err = -ENOENT;
1598 if (fc->sb)
1599 err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
1600 outarg.child, &name);
1601 up_read(&fc->killsb);
1602 kfree(buf);
1603 return err;
1605 err:
1606 kfree(buf);
1607 fuse_copy_finish(cs);
1608 return err;
1611 static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1612 struct fuse_copy_state *cs)
1614 struct fuse_notify_store_out outarg;
1615 struct inode *inode;
1616 struct address_space *mapping;
1617 u64 nodeid;
1618 int err;
1619 pgoff_t index;
1620 unsigned int offset;
1621 unsigned int num;
1622 loff_t file_size;
1623 loff_t end;
1625 err = -EINVAL;
1626 if (size < sizeof(outarg))
1627 goto out_finish;
1629 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1630 if (err)
1631 goto out_finish;
1633 err = -EINVAL;
1634 if (size - sizeof(outarg) != outarg.size)
1635 goto out_finish;
1637 nodeid = outarg.nodeid;
1639 down_read(&fc->killsb);
1641 err = -ENOENT;
1642 if (!fc->sb)
1643 goto out_up_killsb;
1645 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1646 if (!inode)
1647 goto out_up_killsb;
1649 mapping = inode->i_mapping;
1650 index = outarg.offset >> PAGE_CACHE_SHIFT;
1651 offset = outarg.offset & ~PAGE_CACHE_MASK;
1652 file_size = i_size_read(inode);
1653 end = outarg.offset + outarg.size;
1654 if (end > file_size) {
1655 file_size = end;
1656 fuse_write_update_size(inode, file_size);
1659 num = outarg.size;
1660 while (num) {
1661 struct page *page;
1662 unsigned int this_num;
1664 err = -ENOMEM;
1665 page = find_or_create_page(mapping, index,
1666 mapping_gfp_mask(mapping));
1667 if (!page)
1668 goto out_iput;
1670 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1671 err = fuse_copy_page(cs, &page, offset, this_num, 0);
1672 if (!err && offset == 0 &&
1673 (this_num == PAGE_CACHE_SIZE || file_size == end))
1674 SetPageUptodate(page);
1675 unlock_page(page);
1676 page_cache_release(page);
1678 if (err)
1679 goto out_iput;
1681 num -= this_num;
1682 offset = 0;
1683 index++;
1686 err = 0;
1688 out_iput:
1689 iput(inode);
1690 out_up_killsb:
1691 up_read(&fc->killsb);
1692 out_finish:
1693 fuse_copy_finish(cs);
1694 return err;
1697 static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_req *req)
1699 release_pages(req->pages, req->num_pages, false);
1702 static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
1703 struct fuse_notify_retrieve_out *outarg)
1705 int err;
1706 struct address_space *mapping = inode->i_mapping;
1707 struct fuse_req *req;
1708 pgoff_t index;
1709 loff_t file_size;
1710 unsigned int num;
1711 unsigned int offset;
1712 size_t total_len = 0;
1713 int num_pages;
1715 offset = outarg->offset & ~PAGE_CACHE_MASK;
1716 file_size = i_size_read(inode);
1718 num = outarg->size;
1719 if (outarg->offset > file_size)
1720 num = 0;
1721 else if (outarg->offset + num > file_size)
1722 num = file_size - outarg->offset;
1724 num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1725 num_pages = min(num_pages, FUSE_MAX_PAGES_PER_REQ);
1727 req = fuse_get_req(fc, num_pages);
1728 if (IS_ERR(req))
1729 return PTR_ERR(req);
1731 req->in.h.opcode = FUSE_NOTIFY_REPLY;
1732 req->in.h.nodeid = outarg->nodeid;
1733 req->in.numargs = 2;
1734 req->in.argpages = 1;
1735 req->page_descs[0].offset = offset;
1736 req->end = fuse_retrieve_end;
1738 index = outarg->offset >> PAGE_CACHE_SHIFT;
1740 while (num && req->num_pages < num_pages) {
1741 struct page *page;
1742 unsigned int this_num;
1744 page = find_get_page(mapping, index);
1745 if (!page)
1746 break;
1748 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1749 req->pages[req->num_pages] = page;
1750 req->page_descs[req->num_pages].length = this_num;
1751 req->num_pages++;
1753 offset = 0;
1754 num -= this_num;
1755 total_len += this_num;
1756 index++;
1758 req->misc.retrieve_in.offset = outarg->offset;
1759 req->misc.retrieve_in.size = total_len;
1760 req->in.args[0].size = sizeof(req->misc.retrieve_in);
1761 req->in.args[0].value = &req->misc.retrieve_in;
1762 req->in.args[1].size = total_len;
1764 err = fuse_request_send_notify_reply(fc, req, outarg->notify_unique);
1765 if (err)
1766 fuse_retrieve_end(fc, req);
1768 return err;
1771 static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1772 struct fuse_copy_state *cs)
1774 struct fuse_notify_retrieve_out outarg;
1775 struct inode *inode;
1776 int err;
1778 err = -EINVAL;
1779 if (size != sizeof(outarg))
1780 goto copy_finish;
1782 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1783 if (err)
1784 goto copy_finish;
1786 fuse_copy_finish(cs);
1788 down_read(&fc->killsb);
1789 err = -ENOENT;
1790 if (fc->sb) {
1791 u64 nodeid = outarg.nodeid;
1793 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1794 if (inode) {
1795 err = fuse_retrieve(fc, inode, &outarg);
1796 iput(inode);
1799 up_read(&fc->killsb);
1801 return err;
1803 copy_finish:
1804 fuse_copy_finish(cs);
1805 return err;
1808 static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1809 unsigned int size, struct fuse_copy_state *cs)
1811 /* Don't try to move pages (yet) */
1812 cs->move_pages = 0;
1814 switch (code) {
1815 case FUSE_NOTIFY_POLL:
1816 return fuse_notify_poll(fc, size, cs);
1818 case FUSE_NOTIFY_INVAL_INODE:
1819 return fuse_notify_inval_inode(fc, size, cs);
1821 case FUSE_NOTIFY_INVAL_ENTRY:
1822 return fuse_notify_inval_entry(fc, size, cs);
1824 case FUSE_NOTIFY_STORE:
1825 return fuse_notify_store(fc, size, cs);
1827 case FUSE_NOTIFY_RETRIEVE:
1828 return fuse_notify_retrieve(fc, size, cs);
1830 case FUSE_NOTIFY_DELETE:
1831 return fuse_notify_delete(fc, size, cs);
1833 default:
1834 fuse_copy_finish(cs);
1835 return -EINVAL;
1839 /* Look up request on processing list by unique ID */
1840 static struct fuse_req *request_find(struct fuse_pqueue *fpq, u64 unique)
1842 struct fuse_req *req;
1844 list_for_each_entry(req, &fpq->processing, list) {
1845 if (req->in.h.unique == unique || req->intr_unique == unique)
1846 return req;
1848 return NULL;
1851 static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
1852 unsigned nbytes)
1854 unsigned reqsize = sizeof(struct fuse_out_header);
1856 if (out->h.error)
1857 return nbytes != reqsize ? -EINVAL : 0;
1859 reqsize += len_args(out->numargs, out->args);
1861 if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
1862 return -EINVAL;
1863 else if (reqsize > nbytes) {
1864 struct fuse_arg *lastarg = &out->args[out->numargs-1];
1865 unsigned diffsize = reqsize - nbytes;
1866 if (diffsize > lastarg->size)
1867 return -EINVAL;
1868 lastarg->size -= diffsize;
1870 return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
1871 out->page_zeroing);
1875 * Write a single reply to a request. First the header is copied from
1876 * the write buffer. The request is then searched on the processing
1877 * list by the unique ID found in the header. If found, then remove
1878 * it from the list and copy the rest of the buffer to the request.
1879 * The request is finished by calling request_end()
1881 static ssize_t fuse_dev_do_write(struct fuse_dev *fud,
1882 struct fuse_copy_state *cs, size_t nbytes)
1884 int err;
1885 struct fuse_conn *fc = fud->fc;
1886 struct fuse_pqueue *fpq = &fud->pq;
1887 struct fuse_req *req;
1888 struct fuse_out_header oh;
1890 if (nbytes < sizeof(struct fuse_out_header))
1891 return -EINVAL;
1893 err = fuse_copy_one(cs, &oh, sizeof(oh));
1894 if (err)
1895 goto err_finish;
1897 err = -EINVAL;
1898 if (oh.len != nbytes)
1899 goto err_finish;
1902 * Zero oh.unique indicates unsolicited notification message
1903 * and error contains notification code.
1905 if (!oh.unique) {
1906 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
1907 return err ? err : nbytes;
1910 err = -EINVAL;
1911 if (oh.error <= -1000 || oh.error > 0)
1912 goto err_finish;
1914 spin_lock(&fpq->lock);
1915 err = -ENOENT;
1916 if (!fpq->connected)
1917 goto err_unlock_pq;
1919 req = request_find(fpq, oh.unique);
1920 if (!req)
1921 goto err_unlock_pq;
1923 /* Is it an interrupt reply? */
1924 if (req->intr_unique == oh.unique) {
1925 spin_unlock(&fpq->lock);
1927 err = -EINVAL;
1928 if (nbytes != sizeof(struct fuse_out_header))
1929 goto err_finish;
1931 if (oh.error == -ENOSYS)
1932 fc->no_interrupt = 1;
1933 else if (oh.error == -EAGAIN)
1934 queue_interrupt(&fc->iq, req);
1936 fuse_copy_finish(cs);
1937 return nbytes;
1940 clear_bit(FR_SENT, &req->flags);
1941 list_move(&req->list, &fpq->io);
1942 req->out.h = oh;
1943 set_bit(FR_LOCKED, &req->flags);
1944 spin_unlock(&fpq->lock);
1945 cs->req = req;
1946 if (!req->out.page_replace)
1947 cs->move_pages = 0;
1949 err = copy_out_args(cs, &req->out, nbytes);
1950 fuse_copy_finish(cs);
1952 spin_lock(&fpq->lock);
1953 clear_bit(FR_LOCKED, &req->flags);
1954 if (!fpq->connected)
1955 err = -ENOENT;
1956 else if (err)
1957 req->out.h.error = -EIO;
1958 if (!test_bit(FR_PRIVATE, &req->flags))
1959 list_del_init(&req->list);
1960 spin_unlock(&fpq->lock);
1962 request_end(fc, req);
1964 return err ? err : nbytes;
1966 err_unlock_pq:
1967 spin_unlock(&fpq->lock);
1968 err_finish:
1969 fuse_copy_finish(cs);
1970 return err;
1973 static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from)
1975 struct fuse_copy_state cs;
1976 struct fuse_dev *fud = fuse_get_dev(iocb->ki_filp);
1978 if (!fud)
1979 return -EPERM;
1981 if (!iter_is_iovec(from))
1982 return -EINVAL;
1984 fuse_copy_init(&cs, 0, from);
1986 return fuse_dev_do_write(fud, &cs, iov_iter_count(from));
1989 static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
1990 struct file *out, loff_t *ppos,
1991 size_t len, unsigned int flags)
1993 unsigned nbuf;
1994 unsigned idx;
1995 struct pipe_buffer *bufs;
1996 struct fuse_copy_state cs;
1997 struct fuse_dev *fud;
1998 size_t rem;
1999 ssize_t ret;
2001 fud = fuse_get_dev(out);
2002 if (!fud)
2003 return -EPERM;
2005 pipe_lock(pipe);
2007 bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
2008 if (!bufs) {
2009 pipe_unlock(pipe);
2010 return -ENOMEM;
2013 nbuf = 0;
2014 rem = 0;
2015 for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
2016 rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
2018 ret = -EINVAL;
2019 if (rem < len) {
2020 pipe_unlock(pipe);
2021 goto out;
2024 rem = len;
2025 while (rem) {
2026 struct pipe_buffer *ibuf;
2027 struct pipe_buffer *obuf;
2029 BUG_ON(nbuf >= pipe->buffers);
2030 BUG_ON(!pipe->nrbufs);
2031 ibuf = &pipe->bufs[pipe->curbuf];
2032 obuf = &bufs[nbuf];
2034 if (rem >= ibuf->len) {
2035 *obuf = *ibuf;
2036 ibuf->ops = NULL;
2037 pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
2038 pipe->nrbufs--;
2039 } else {
2040 ibuf->ops->get(pipe, ibuf);
2041 *obuf = *ibuf;
2042 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
2043 obuf->len = rem;
2044 ibuf->offset += obuf->len;
2045 ibuf->len -= obuf->len;
2047 nbuf++;
2048 rem -= obuf->len;
2050 pipe_unlock(pipe);
2052 fuse_copy_init(&cs, 0, NULL);
2053 cs.pipebufs = bufs;
2054 cs.nr_segs = nbuf;
2055 cs.pipe = pipe;
2057 if (flags & SPLICE_F_MOVE)
2058 cs.move_pages = 1;
2060 ret = fuse_dev_do_write(fud, &cs, len);
2062 for (idx = 0; idx < nbuf; idx++) {
2063 struct pipe_buffer *buf = &bufs[idx];
2064 buf->ops->release(pipe, buf);
2066 out:
2067 kfree(bufs);
2068 return ret;
2071 static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
2073 unsigned mask = POLLOUT | POLLWRNORM;
2074 struct fuse_iqueue *fiq;
2075 struct fuse_dev *fud = fuse_get_dev(file);
2077 if (!fud)
2078 return POLLERR;
2080 fiq = &fud->fc->iq;
2081 poll_wait(file, &fiq->waitq, wait);
2083 spin_lock(&fiq->waitq.lock);
2084 if (!fiq->connected)
2085 mask = POLLERR;
2086 else if (request_pending(fiq))
2087 mask |= POLLIN | POLLRDNORM;
2088 spin_unlock(&fiq->waitq.lock);
2090 return mask;
2094 * Abort all requests on the given list (pending or processing)
2096 * This function releases and reacquires fc->lock
2098 static void end_requests(struct fuse_conn *fc, struct list_head *head)
2100 while (!list_empty(head)) {
2101 struct fuse_req *req;
2102 req = list_entry(head->next, struct fuse_req, list);
2103 req->out.h.error = -ECONNABORTED;
2104 clear_bit(FR_SENT, &req->flags);
2105 list_del_init(&req->list);
2106 request_end(fc, req);
2110 static void end_polls(struct fuse_conn *fc)
2112 struct rb_node *p;
2114 p = rb_first(&fc->polled_files);
2116 while (p) {
2117 struct fuse_file *ff;
2118 ff = rb_entry(p, struct fuse_file, polled_node);
2119 wake_up_interruptible_all(&ff->poll_wait);
2121 p = rb_next(p);
2126 * Abort all requests.
2128 * Emergency exit in case of a malicious or accidental deadlock, or just a hung
2129 * filesystem.
2131 * The same effect is usually achievable through killing the filesystem daemon
2132 * and all users of the filesystem. The exception is the combination of an
2133 * asynchronous request and the tricky deadlock (see
2134 * Documentation/filesystems/fuse.txt).
2136 * Aborting requests under I/O goes as follows: 1: Separate out unlocked
2137 * requests, they should be finished off immediately. Locked requests will be
2138 * finished after unlock; see unlock_request(). 2: Finish off the unlocked
2139 * requests. It is possible that some request will finish before we can. This
2140 * is OK, the request will in that case be removed from the list before we touch
2141 * it.
2143 void fuse_abort_conn(struct fuse_conn *fc)
2145 struct fuse_iqueue *fiq = &fc->iq;
2147 spin_lock(&fc->lock);
2148 if (fc->connected) {
2149 struct fuse_dev *fud;
2150 struct fuse_req *req, *next;
2151 LIST_HEAD(to_end1);
2152 LIST_HEAD(to_end2);
2154 fc->connected = 0;
2155 fc->blocked = 0;
2156 fuse_set_initialized(fc);
2157 list_for_each_entry(fud, &fc->devices, entry) {
2158 struct fuse_pqueue *fpq = &fud->pq;
2160 spin_lock(&fpq->lock);
2161 fpq->connected = 0;
2162 list_for_each_entry_safe(req, next, &fpq->io, list) {
2163 req->out.h.error = -ECONNABORTED;
2164 spin_lock(&req->waitq.lock);
2165 set_bit(FR_ABORTED, &req->flags);
2166 if (!test_bit(FR_LOCKED, &req->flags)) {
2167 set_bit(FR_PRIVATE, &req->flags);
2168 __fuse_get_request(req);
2169 list_move(&req->list, &to_end1);
2171 spin_unlock(&req->waitq.lock);
2173 list_splice_init(&fpq->processing, &to_end2);
2174 spin_unlock(&fpq->lock);
2176 fc->max_background = UINT_MAX;
2177 flush_bg_queue(fc);
2179 spin_lock(&fiq->waitq.lock);
2180 fiq->connected = 0;
2181 list_splice_init(&fiq->pending, &to_end2);
2182 list_for_each_entry(req, &to_end2, list)
2183 clear_bit(FR_PENDING, &req->flags);
2184 while (forget_pending(fiq))
2185 kfree(dequeue_forget(fiq, 1, NULL));
2186 wake_up_all_locked(&fiq->waitq);
2187 spin_unlock(&fiq->waitq.lock);
2188 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
2189 end_polls(fc);
2190 wake_up_all(&fc->blocked_waitq);
2191 spin_unlock(&fc->lock);
2193 while (!list_empty(&to_end1)) {
2194 req = list_first_entry(&to_end1, struct fuse_req, list);
2195 list_del_init(&req->list);
2196 request_end(fc, req);
2198 end_requests(fc, &to_end2);
2199 } else {
2200 spin_unlock(&fc->lock);
2203 EXPORT_SYMBOL_GPL(fuse_abort_conn);
2205 void fuse_wait_aborted(struct fuse_conn *fc)
2207 wait_event(fc->blocked_waitq, atomic_read(&fc->num_waiting) == 0);
2210 int fuse_dev_release(struct inode *inode, struct file *file)
2212 struct fuse_dev *fud = fuse_get_dev(file);
2214 if (fud) {
2215 struct fuse_conn *fc = fud->fc;
2216 struct fuse_pqueue *fpq = &fud->pq;
2217 LIST_HEAD(to_end);
2219 spin_lock(&fpq->lock);
2220 WARN_ON(!list_empty(&fpq->io));
2221 list_splice_init(&fpq->processing, &to_end);
2222 spin_unlock(&fpq->lock);
2224 end_requests(fc, &to_end);
2226 /* Are we the last open device? */
2227 if (atomic_dec_and_test(&fc->dev_count)) {
2228 WARN_ON(fc->iq.fasync != NULL);
2229 fuse_abort_conn(fc);
2231 fuse_dev_free(fud);
2233 return 0;
2235 EXPORT_SYMBOL_GPL(fuse_dev_release);
2237 static int fuse_dev_fasync(int fd, struct file *file, int on)
2239 struct fuse_dev *fud = fuse_get_dev(file);
2241 if (!fud)
2242 return -EPERM;
2244 /* No locking - fasync_helper does its own locking */
2245 return fasync_helper(fd, file, on, &fud->fc->iq.fasync);
2248 static int fuse_device_clone(struct fuse_conn *fc, struct file *new)
2250 struct fuse_dev *fud;
2252 if (new->private_data)
2253 return -EINVAL;
2255 fud = fuse_dev_alloc(fc);
2256 if (!fud)
2257 return -ENOMEM;
2259 new->private_data = fud;
2260 atomic_inc(&fc->dev_count);
2262 return 0;
2265 static long fuse_dev_ioctl(struct file *file, unsigned int cmd,
2266 unsigned long arg)
2268 int err = -ENOTTY;
2270 if (cmd == FUSE_DEV_IOC_CLONE) {
2271 int oldfd;
2273 err = -EFAULT;
2274 if (!get_user(oldfd, (__u32 __user *) arg)) {
2275 struct file *old = fget(oldfd);
2277 err = -EINVAL;
2278 if (old) {
2279 struct fuse_dev *fud = NULL;
2282 * Check against file->f_op because CUSE
2283 * uses the same ioctl handler.
2285 if (old->f_op == file->f_op &&
2286 old->f_cred->user_ns == file->f_cred->user_ns)
2287 fud = fuse_get_dev(old);
2289 if (fud) {
2290 mutex_lock(&fuse_mutex);
2291 err = fuse_device_clone(fud->fc, file);
2292 mutex_unlock(&fuse_mutex);
2294 fput(old);
2298 return err;
2301 const struct file_operations fuse_dev_operations = {
2302 .owner = THIS_MODULE,
2303 .open = fuse_dev_open,
2304 .llseek = no_llseek,
2305 .read_iter = fuse_dev_read,
2306 .splice_read = fuse_dev_splice_read,
2307 .write_iter = fuse_dev_write,
2308 .splice_write = fuse_dev_splice_write,
2309 .poll = fuse_dev_poll,
2310 .release = fuse_dev_release,
2311 .fasync = fuse_dev_fasync,
2312 .unlocked_ioctl = fuse_dev_ioctl,
2313 .compat_ioctl = fuse_dev_ioctl,
2315 EXPORT_SYMBOL_GPL(fuse_dev_operations);
2317 static struct miscdevice fuse_miscdevice = {
2318 .minor = FUSE_MINOR,
2319 .name = "fuse",
2320 .fops = &fuse_dev_operations,
2323 int __init fuse_dev_init(void)
2325 int err = -ENOMEM;
2326 fuse_req_cachep = kmem_cache_create("fuse_request",
2327 sizeof(struct fuse_req),
2328 0, 0, NULL);
2329 if (!fuse_req_cachep)
2330 goto out;
2332 err = misc_register(&fuse_miscdevice);
2333 if (err)
2334 goto out_cache_clean;
2336 return 0;
2338 out_cache_clean:
2339 kmem_cache_destroy(fuse_req_cachep);
2340 out:
2341 return err;
2344 void fuse_dev_cleanup(void)
2346 misc_deregister(&fuse_miscdevice);
2347 kmem_cache_destroy(fuse_req_cachep);