2 FUSE: Filesystem in Userspace
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/sched/signal.h>
15 #include <linux/uio.h>
16 #include <linux/miscdevice.h>
17 #include <linux/pagemap.h>
18 #include <linux/file.h>
19 #include <linux/slab.h>
20 #include <linux/pipe_fs_i.h>
21 #include <linux/swap.h>
22 #include <linux/splice.h>
23 #include <linux/sched.h>
25 MODULE_ALIAS_MISCDEV(FUSE_MINOR);
26 MODULE_ALIAS("devname:fuse");
28 /* Ordinary requests have even IDs, while interrupts IDs are odd */
29 #define FUSE_INT_REQ_BIT (1ULL << 0)
30 #define FUSE_REQ_ID_STEP (1ULL << 1)
32 static struct kmem_cache *fuse_req_cachep;
34 static struct fuse_dev *fuse_get_dev(struct file *file)
37 * Lockless access is OK, because file->private data is set
38 * once during mount and is valid until the file is released.
40 return READ_ONCE(file->private_data);
43 static void fuse_request_init(struct fuse_req *req)
45 INIT_LIST_HEAD(&req->list);
46 INIT_LIST_HEAD(&req->intr_entry);
47 init_waitqueue_head(&req->waitq);
48 refcount_set(&req->count, 1);
49 __set_bit(FR_PENDING, &req->flags);
52 static struct fuse_req *fuse_request_alloc(gfp_t flags)
54 struct fuse_req *req = kmem_cache_zalloc(fuse_req_cachep, flags);
56 fuse_request_init(req);
61 static void fuse_request_free(struct fuse_req *req)
63 kmem_cache_free(fuse_req_cachep, req);
66 static void __fuse_get_request(struct fuse_req *req)
68 refcount_inc(&req->count);
71 /* Must be called with > 1 refcount */
72 static void __fuse_put_request(struct fuse_req *req)
74 refcount_dec(&req->count);
77 void fuse_set_initialized(struct fuse_conn *fc)
79 /* Make sure stores before this are seen on another CPU */
84 static bool fuse_block_alloc(struct fuse_conn *fc, bool for_background)
86 return !fc->initialized || (for_background && fc->blocked);
89 static void fuse_drop_waiting(struct fuse_conn *fc)
92 * lockess check of fc->connected is okay, because atomic_dec_and_test()
93 * provides a memory barrier mached with the one in fuse_wait_aborted()
94 * to ensure no wake-up is missed.
96 if (atomic_dec_and_test(&fc->num_waiting) &&
97 !READ_ONCE(fc->connected)) {
98 /* wake up aborters */
99 wake_up_all(&fc->blocked_waitq);
103 static void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req);
105 static struct fuse_req *fuse_get_req(struct fuse_conn *fc, bool for_background)
107 struct fuse_req *req;
109 atomic_inc(&fc->num_waiting);
111 if (fuse_block_alloc(fc, for_background)) {
113 if (wait_event_killable_exclusive(fc->blocked_waitq,
114 !fuse_block_alloc(fc, for_background)))
117 /* Matches smp_wmb() in fuse_set_initialized() */
128 req = fuse_request_alloc(GFP_KERNEL);
132 wake_up(&fc->blocked_waitq);
136 req->in.h.uid = from_kuid(fc->user_ns, current_fsuid());
137 req->in.h.gid = from_kgid(fc->user_ns, current_fsgid());
138 req->in.h.pid = pid_nr_ns(task_pid(current), fc->pid_ns);
140 __set_bit(FR_WAITING, &req->flags);
142 __set_bit(FR_BACKGROUND, &req->flags);
144 if (unlikely(req->in.h.uid == ((uid_t)-1) ||
145 req->in.h.gid == ((gid_t)-1))) {
146 fuse_put_request(fc, req);
147 return ERR_PTR(-EOVERFLOW);
152 fuse_drop_waiting(fc);
156 static void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
158 if (refcount_dec_and_test(&req->count)) {
159 if (test_bit(FR_BACKGROUND, &req->flags)) {
161 * We get here in the unlikely case that a background
162 * request was allocated but not sent
164 spin_lock(&fc->bg_lock);
166 wake_up(&fc->blocked_waitq);
167 spin_unlock(&fc->bg_lock);
170 if (test_bit(FR_WAITING, &req->flags)) {
171 __clear_bit(FR_WAITING, &req->flags);
172 fuse_drop_waiting(fc);
175 fuse_request_free(req);
179 unsigned int fuse_len_args(unsigned int numargs, struct fuse_arg *args)
184 for (i = 0; i < numargs; i++)
185 nbytes += args[i].size;
189 EXPORT_SYMBOL_GPL(fuse_len_args);
191 u64 fuse_get_unique(struct fuse_iqueue *fiq)
193 fiq->reqctr += FUSE_REQ_ID_STEP;
196 EXPORT_SYMBOL_GPL(fuse_get_unique);
198 static unsigned int fuse_req_hash(u64 unique)
200 return hash_long(unique & ~FUSE_INT_REQ_BIT, FUSE_PQ_HASH_BITS);
204 * A new request is available, wake fiq->waitq
206 static void fuse_dev_wake_and_unlock(struct fuse_iqueue *fiq)
207 __releases(fiq->lock)
209 wake_up(&fiq->waitq);
210 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
211 spin_unlock(&fiq->lock);
214 const struct fuse_iqueue_ops fuse_dev_fiq_ops = {
215 .wake_forget_and_unlock = fuse_dev_wake_and_unlock,
216 .wake_interrupt_and_unlock = fuse_dev_wake_and_unlock,
217 .wake_pending_and_unlock = fuse_dev_wake_and_unlock,
219 EXPORT_SYMBOL_GPL(fuse_dev_fiq_ops);
221 static void queue_request_and_unlock(struct fuse_iqueue *fiq,
222 struct fuse_req *req)
223 __releases(fiq->lock)
225 req->in.h.len = sizeof(struct fuse_in_header) +
226 fuse_len_args(req->args->in_numargs,
227 (struct fuse_arg *) req->args->in_args);
228 list_add_tail(&req->list, &fiq->pending);
229 fiq->ops->wake_pending_and_unlock(fiq);
232 void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
233 u64 nodeid, u64 nlookup)
235 struct fuse_iqueue *fiq = &fc->iq;
237 forget->forget_one.nodeid = nodeid;
238 forget->forget_one.nlookup = nlookup;
240 spin_lock(&fiq->lock);
241 if (fiq->connected) {
242 fiq->forget_list_tail->next = forget;
243 fiq->forget_list_tail = forget;
244 fiq->ops->wake_forget_and_unlock(fiq);
247 spin_unlock(&fiq->lock);
251 static void flush_bg_queue(struct fuse_conn *fc)
253 struct fuse_iqueue *fiq = &fc->iq;
255 while (fc->active_background < fc->max_background &&
256 !list_empty(&fc->bg_queue)) {
257 struct fuse_req *req;
259 req = list_first_entry(&fc->bg_queue, struct fuse_req, list);
260 list_del(&req->list);
261 fc->active_background++;
262 spin_lock(&fiq->lock);
263 req->in.h.unique = fuse_get_unique(fiq);
264 queue_request_and_unlock(fiq, req);
269 * This function is called when a request is finished. Either a reply
270 * has arrived or it was aborted (and not yet sent) or some error
271 * occurred during communication with userspace, or the device file
272 * was closed. The requester thread is woken up (if still waiting),
273 * the 'end' callback is called if given, else the reference to the
274 * request is released
276 void fuse_request_end(struct fuse_conn *fc, struct fuse_req *req)
278 struct fuse_iqueue *fiq = &fc->iq;
280 if (test_and_set_bit(FR_FINISHED, &req->flags))
284 * test_and_set_bit() implies smp_mb() between bit
285 * changing and below intr_entry check. Pairs with
286 * smp_mb() from queue_interrupt().
288 if (!list_empty(&req->intr_entry)) {
289 spin_lock(&fiq->lock);
290 list_del_init(&req->intr_entry);
291 spin_unlock(&fiq->lock);
293 WARN_ON(test_bit(FR_PENDING, &req->flags));
294 WARN_ON(test_bit(FR_SENT, &req->flags));
295 if (test_bit(FR_BACKGROUND, &req->flags)) {
296 spin_lock(&fc->bg_lock);
297 clear_bit(FR_BACKGROUND, &req->flags);
298 if (fc->num_background == fc->max_background) {
300 wake_up(&fc->blocked_waitq);
301 } else if (!fc->blocked) {
303 * Wake up next waiter, if any. It's okay to use
304 * waitqueue_active(), as we've already synced up
305 * fc->blocked with waiters with the wake_up() call
308 if (waitqueue_active(&fc->blocked_waitq))
309 wake_up(&fc->blocked_waitq);
312 if (fc->num_background == fc->congestion_threshold && fc->sb) {
313 clear_bdi_congested(fc->sb->s_bdi, BLK_RW_SYNC);
314 clear_bdi_congested(fc->sb->s_bdi, BLK_RW_ASYNC);
316 fc->num_background--;
317 fc->active_background--;
319 spin_unlock(&fc->bg_lock);
321 /* Wake up waiter sleeping in request_wait_answer() */
322 wake_up(&req->waitq);
325 if (test_bit(FR_ASYNC, &req->flags))
326 req->args->end(fc, req->args, req->out.h.error);
328 fuse_put_request(fc, req);
330 EXPORT_SYMBOL_GPL(fuse_request_end);
332 static int queue_interrupt(struct fuse_iqueue *fiq, struct fuse_req *req)
334 spin_lock(&fiq->lock);
335 /* Check for we've sent request to interrupt this req */
336 if (unlikely(!test_bit(FR_INTERRUPTED, &req->flags))) {
337 spin_unlock(&fiq->lock);
341 if (list_empty(&req->intr_entry)) {
342 list_add_tail(&req->intr_entry, &fiq->interrupts);
344 * Pairs with smp_mb() implied by test_and_set_bit()
345 * from fuse_request_end().
348 if (test_bit(FR_FINISHED, &req->flags)) {
349 list_del_init(&req->intr_entry);
350 spin_unlock(&fiq->lock);
353 fiq->ops->wake_interrupt_and_unlock(fiq);
355 spin_unlock(&fiq->lock);
360 static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
362 struct fuse_iqueue *fiq = &fc->iq;
365 if (!fc->no_interrupt) {
366 /* Any signal may interrupt this */
367 err = wait_event_interruptible(req->waitq,
368 test_bit(FR_FINISHED, &req->flags));
372 set_bit(FR_INTERRUPTED, &req->flags);
373 /* matches barrier in fuse_dev_do_read() */
374 smp_mb__after_atomic();
375 if (test_bit(FR_SENT, &req->flags))
376 queue_interrupt(fiq, req);
379 if (!test_bit(FR_FORCE, &req->flags)) {
380 /* Only fatal signals may interrupt this */
381 err = wait_event_killable(req->waitq,
382 test_bit(FR_FINISHED, &req->flags));
386 spin_lock(&fiq->lock);
387 /* Request is not yet in userspace, bail out */
388 if (test_bit(FR_PENDING, &req->flags)) {
389 list_del(&req->list);
390 spin_unlock(&fiq->lock);
391 __fuse_put_request(req);
392 req->out.h.error = -EINTR;
395 spin_unlock(&fiq->lock);
399 * Either request is already in userspace, or it was forced.
402 wait_event(req->waitq, test_bit(FR_FINISHED, &req->flags));
405 static void __fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
407 struct fuse_iqueue *fiq = &fc->iq;
409 BUG_ON(test_bit(FR_BACKGROUND, &req->flags));
410 spin_lock(&fiq->lock);
411 if (!fiq->connected) {
412 spin_unlock(&fiq->lock);
413 req->out.h.error = -ENOTCONN;
415 req->in.h.unique = fuse_get_unique(fiq);
416 /* acquire extra reference, since request is still needed
417 after fuse_request_end() */
418 __fuse_get_request(req);
419 queue_request_and_unlock(fiq, req);
421 request_wait_answer(fc, req);
422 /* Pairs with smp_wmb() in fuse_request_end() */
427 static void fuse_adjust_compat(struct fuse_conn *fc, struct fuse_args *args)
429 if (fc->minor < 4 && args->opcode == FUSE_STATFS)
430 args->out_args[0].size = FUSE_COMPAT_STATFS_SIZE;
433 switch (args->opcode) {
440 args->out_args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
444 args->out_args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
448 if (fc->minor < 12) {
449 switch (args->opcode) {
451 args->in_args[0].size = sizeof(struct fuse_open_in);
454 args->in_args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE;
460 static void fuse_force_creds(struct fuse_conn *fc, struct fuse_req *req)
462 req->in.h.uid = from_kuid_munged(fc->user_ns, current_fsuid());
463 req->in.h.gid = from_kgid_munged(fc->user_ns, current_fsgid());
464 req->in.h.pid = pid_nr_ns(task_pid(current), fc->pid_ns);
467 static void fuse_args_to_req(struct fuse_req *req, struct fuse_args *args)
469 req->in.h.opcode = args->opcode;
470 req->in.h.nodeid = args->nodeid;
473 __set_bit(FR_ASYNC, &req->flags);
476 ssize_t fuse_simple_request(struct fuse_conn *fc, struct fuse_args *args)
478 struct fuse_req *req;
482 atomic_inc(&fc->num_waiting);
483 req = fuse_request_alloc(GFP_KERNEL | __GFP_NOFAIL);
486 fuse_force_creds(fc, req);
488 __set_bit(FR_WAITING, &req->flags);
489 __set_bit(FR_FORCE, &req->flags);
491 WARN_ON(args->nocreds);
492 req = fuse_get_req(fc, false);
497 /* Needs to be done after fuse_get_req() so that fc->minor is valid */
498 fuse_adjust_compat(fc, args);
499 fuse_args_to_req(req, args);
502 __set_bit(FR_ISREPLY, &req->flags);
503 __fuse_request_send(fc, req);
504 ret = req->out.h.error;
505 if (!ret && args->out_argvar) {
506 BUG_ON(args->out_numargs == 0);
507 ret = args->out_args[args->out_numargs - 1].size;
509 fuse_put_request(fc, req);
514 static bool fuse_request_queue_background(struct fuse_conn *fc,
515 struct fuse_req *req)
519 WARN_ON(!test_bit(FR_BACKGROUND, &req->flags));
520 if (!test_bit(FR_WAITING, &req->flags)) {
521 __set_bit(FR_WAITING, &req->flags);
522 atomic_inc(&fc->num_waiting);
524 __set_bit(FR_ISREPLY, &req->flags);
525 spin_lock(&fc->bg_lock);
526 if (likely(fc->connected)) {
527 fc->num_background++;
528 if (fc->num_background == fc->max_background)
530 if (fc->num_background == fc->congestion_threshold && fc->sb) {
531 set_bdi_congested(fc->sb->s_bdi, BLK_RW_SYNC);
532 set_bdi_congested(fc->sb->s_bdi, BLK_RW_ASYNC);
534 list_add_tail(&req->list, &fc->bg_queue);
538 spin_unlock(&fc->bg_lock);
543 int fuse_simple_background(struct fuse_conn *fc, struct fuse_args *args,
546 struct fuse_req *req;
549 WARN_ON(!args->nocreds);
550 req = fuse_request_alloc(gfp_flags);
553 __set_bit(FR_BACKGROUND, &req->flags);
555 WARN_ON(args->nocreds);
556 req = fuse_get_req(fc, true);
561 fuse_args_to_req(req, args);
563 if (!fuse_request_queue_background(fc, req)) {
564 fuse_put_request(fc, req);
570 EXPORT_SYMBOL_GPL(fuse_simple_background);
572 static int fuse_simple_notify_reply(struct fuse_conn *fc,
573 struct fuse_args *args, u64 unique)
575 struct fuse_req *req;
576 struct fuse_iqueue *fiq = &fc->iq;
579 req = fuse_get_req(fc, false);
583 __clear_bit(FR_ISREPLY, &req->flags);
584 req->in.h.unique = unique;
586 fuse_args_to_req(req, args);
588 spin_lock(&fiq->lock);
589 if (fiq->connected) {
590 queue_request_and_unlock(fiq, req);
593 spin_unlock(&fiq->lock);
594 fuse_put_request(fc, req);
601 * Lock the request. Up to the next unlock_request() there mustn't be
602 * anything that could cause a page-fault. If the request was already
605 static int lock_request(struct fuse_req *req)
609 spin_lock(&req->waitq.lock);
610 if (test_bit(FR_ABORTED, &req->flags))
613 set_bit(FR_LOCKED, &req->flags);
614 spin_unlock(&req->waitq.lock);
620 * Unlock request. If it was aborted while locked, caller is responsible
621 * for unlocking and ending the request.
623 static int unlock_request(struct fuse_req *req)
627 spin_lock(&req->waitq.lock);
628 if (test_bit(FR_ABORTED, &req->flags))
631 clear_bit(FR_LOCKED, &req->flags);
632 spin_unlock(&req->waitq.lock);
637 struct fuse_copy_state {
639 struct fuse_req *req;
640 struct iov_iter *iter;
641 struct pipe_buffer *pipebufs;
642 struct pipe_buffer *currbuf;
643 struct pipe_inode_info *pipe;
644 unsigned long nr_segs;
648 unsigned move_pages:1;
651 static void fuse_copy_init(struct fuse_copy_state *cs, int write,
652 struct iov_iter *iter)
654 memset(cs, 0, sizeof(*cs));
659 /* Unmap and put previous page of userspace buffer */
660 static void fuse_copy_finish(struct fuse_copy_state *cs)
663 struct pipe_buffer *buf = cs->currbuf;
666 buf->len = PAGE_SIZE - cs->len;
670 flush_dcache_page(cs->pg);
671 set_page_dirty_lock(cs->pg);
679 * Get another pagefull of userspace buffer, and map it to kernel
680 * address space, and lock request
682 static int fuse_copy_fill(struct fuse_copy_state *cs)
687 err = unlock_request(cs->req);
691 fuse_copy_finish(cs);
693 struct pipe_buffer *buf = cs->pipebufs;
696 err = pipe_buf_confirm(cs->pipe, buf);
700 BUG_ON(!cs->nr_segs);
703 cs->offset = buf->offset;
708 if (cs->nr_segs >= cs->pipe->max_usage)
711 page = alloc_page(GFP_HIGHUSER);
728 err = iov_iter_get_pages(cs->iter, &page, PAGE_SIZE, 1, &off);
735 iov_iter_advance(cs->iter, err);
738 return lock_request(cs->req);
741 /* Do as much copy to/from userspace buffer as we can */
742 static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
744 unsigned ncpy = min(*size, cs->len);
746 void *pgaddr = kmap_atomic(cs->pg);
747 void *buf = pgaddr + cs->offset;
750 memcpy(buf, *val, ncpy);
752 memcpy(*val, buf, ncpy);
754 kunmap_atomic(pgaddr);
763 static int fuse_check_page(struct page *page)
765 if (page_mapcount(page) ||
766 page->mapping != NULL ||
767 (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
775 dump_page(page, "fuse: trying to steal weird page");
781 static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
784 struct page *oldpage = *pagep;
785 struct page *newpage;
786 struct pipe_buffer *buf = cs->pipebufs;
788 err = unlock_request(cs->req);
792 fuse_copy_finish(cs);
794 err = pipe_buf_confirm(cs->pipe, buf);
798 BUG_ON(!cs->nr_segs);
804 if (cs->len != PAGE_SIZE)
807 if (!pipe_buf_try_steal(cs->pipe, buf))
812 if (!PageUptodate(newpage))
813 SetPageUptodate(newpage);
815 ClearPageMappedToDisk(newpage);
817 if (fuse_check_page(newpage) != 0)
818 goto out_fallback_unlock;
821 * This is a new and locked page, it shouldn't be mapped or
822 * have any special flags on it
824 if (WARN_ON(page_mapped(oldpage)))
825 goto out_fallback_unlock;
826 if (WARN_ON(page_has_private(oldpage)))
827 goto out_fallback_unlock;
828 if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
829 goto out_fallback_unlock;
830 if (WARN_ON(PageMlocked(oldpage)))
831 goto out_fallback_unlock;
833 err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
835 unlock_page(newpage);
841 if (!(buf->flags & PIPE_BUF_FLAG_LRU))
842 lru_cache_add(newpage);
845 spin_lock(&cs->req->waitq.lock);
846 if (test_bit(FR_ABORTED, &cs->req->flags))
850 spin_unlock(&cs->req->waitq.lock);
853 unlock_page(newpage);
858 unlock_page(oldpage);
865 unlock_page(newpage);
868 cs->offset = buf->offset;
870 err = lock_request(cs->req);
877 static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
878 unsigned offset, unsigned count)
880 struct pipe_buffer *buf;
883 if (cs->nr_segs >= cs->pipe->max_usage)
886 err = unlock_request(cs->req);
890 fuse_copy_finish(cs);
895 buf->offset = offset;
906 * Copy a page in the request to/from the userspace buffer. Must be
909 static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
910 unsigned offset, unsigned count, int zeroing)
913 struct page *page = *pagep;
915 if (page && zeroing && count < PAGE_SIZE)
916 clear_highpage(page);
919 if (cs->write && cs->pipebufs && page) {
920 return fuse_ref_page(cs, page, offset, count);
921 } else if (!cs->len) {
922 if (cs->move_pages && page &&
923 offset == 0 && count == PAGE_SIZE) {
924 err = fuse_try_move_page(cs, pagep);
928 err = fuse_copy_fill(cs);
934 void *mapaddr = kmap_atomic(page);
935 void *buf = mapaddr + offset;
936 offset += fuse_copy_do(cs, &buf, &count);
937 kunmap_atomic(mapaddr);
939 offset += fuse_copy_do(cs, NULL, &count);
941 if (page && !cs->write)
942 flush_dcache_page(page);
946 /* Copy pages in the request to/from userspace buffer */
947 static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
951 struct fuse_req *req = cs->req;
952 struct fuse_args_pages *ap = container_of(req->args, typeof(*ap), args);
955 for (i = 0; i < ap->num_pages && (nbytes || zeroing); i++) {
957 unsigned int offset = ap->descs[i].offset;
958 unsigned int count = min(nbytes, ap->descs[i].length);
960 err = fuse_copy_page(cs, &ap->pages[i], offset, count, zeroing);
969 /* Copy a single argument in the request to/from userspace buffer */
970 static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
974 int err = fuse_copy_fill(cs);
978 fuse_copy_do(cs, &val, &size);
983 /* Copy request arguments to/from userspace buffer */
984 static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
985 unsigned argpages, struct fuse_arg *args,
991 for (i = 0; !err && i < numargs; i++) {
992 struct fuse_arg *arg = &args[i];
993 if (i == numargs - 1 && argpages)
994 err = fuse_copy_pages(cs, arg->size, zeroing);
996 err = fuse_copy_one(cs, arg->value, arg->size);
1001 static int forget_pending(struct fuse_iqueue *fiq)
1003 return fiq->forget_list_head.next != NULL;
1006 static int request_pending(struct fuse_iqueue *fiq)
1008 return !list_empty(&fiq->pending) || !list_empty(&fiq->interrupts) ||
1009 forget_pending(fiq);
1013 * Transfer an interrupt request to userspace
1015 * Unlike other requests this is assembled on demand, without a need
1016 * to allocate a separate fuse_req structure.
1018 * Called with fiq->lock held, releases it
1020 static int fuse_read_interrupt(struct fuse_iqueue *fiq,
1021 struct fuse_copy_state *cs,
1022 size_t nbytes, struct fuse_req *req)
1023 __releases(fiq->lock)
1025 struct fuse_in_header ih;
1026 struct fuse_interrupt_in arg;
1027 unsigned reqsize = sizeof(ih) + sizeof(arg);
1030 list_del_init(&req->intr_entry);
1031 memset(&ih, 0, sizeof(ih));
1032 memset(&arg, 0, sizeof(arg));
1034 ih.opcode = FUSE_INTERRUPT;
1035 ih.unique = (req->in.h.unique | FUSE_INT_REQ_BIT);
1036 arg.unique = req->in.h.unique;
1038 spin_unlock(&fiq->lock);
1039 if (nbytes < reqsize)
1042 err = fuse_copy_one(cs, &ih, sizeof(ih));
1044 err = fuse_copy_one(cs, &arg, sizeof(arg));
1045 fuse_copy_finish(cs);
1047 return err ? err : reqsize;
1050 struct fuse_forget_link *fuse_dequeue_forget(struct fuse_iqueue *fiq,
1052 unsigned int *countp)
1054 struct fuse_forget_link *head = fiq->forget_list_head.next;
1055 struct fuse_forget_link **newhead = &head;
1058 for (count = 0; *newhead != NULL && count < max; count++)
1059 newhead = &(*newhead)->next;
1061 fiq->forget_list_head.next = *newhead;
1063 if (fiq->forget_list_head.next == NULL)
1064 fiq->forget_list_tail = &fiq->forget_list_head;
1071 EXPORT_SYMBOL(fuse_dequeue_forget);
1073 static int fuse_read_single_forget(struct fuse_iqueue *fiq,
1074 struct fuse_copy_state *cs,
1076 __releases(fiq->lock)
1079 struct fuse_forget_link *forget = fuse_dequeue_forget(fiq, 1, NULL);
1080 struct fuse_forget_in arg = {
1081 .nlookup = forget->forget_one.nlookup,
1083 struct fuse_in_header ih = {
1084 .opcode = FUSE_FORGET,
1085 .nodeid = forget->forget_one.nodeid,
1086 .unique = fuse_get_unique(fiq),
1087 .len = sizeof(ih) + sizeof(arg),
1090 spin_unlock(&fiq->lock);
1092 if (nbytes < ih.len)
1095 err = fuse_copy_one(cs, &ih, sizeof(ih));
1097 err = fuse_copy_one(cs, &arg, sizeof(arg));
1098 fuse_copy_finish(cs);
1106 static int fuse_read_batch_forget(struct fuse_iqueue *fiq,
1107 struct fuse_copy_state *cs, size_t nbytes)
1108 __releases(fiq->lock)
1111 unsigned max_forgets;
1113 struct fuse_forget_link *head;
1114 struct fuse_batch_forget_in arg = { .count = 0 };
1115 struct fuse_in_header ih = {
1116 .opcode = FUSE_BATCH_FORGET,
1117 .unique = fuse_get_unique(fiq),
1118 .len = sizeof(ih) + sizeof(arg),
1121 if (nbytes < ih.len) {
1122 spin_unlock(&fiq->lock);
1126 max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
1127 head = fuse_dequeue_forget(fiq, max_forgets, &count);
1128 spin_unlock(&fiq->lock);
1131 ih.len += count * sizeof(struct fuse_forget_one);
1132 err = fuse_copy_one(cs, &ih, sizeof(ih));
1134 err = fuse_copy_one(cs, &arg, sizeof(arg));
1137 struct fuse_forget_link *forget = head;
1140 err = fuse_copy_one(cs, &forget->forget_one,
1141 sizeof(forget->forget_one));
1143 head = forget->next;
1147 fuse_copy_finish(cs);
1155 static int fuse_read_forget(struct fuse_conn *fc, struct fuse_iqueue *fiq,
1156 struct fuse_copy_state *cs,
1158 __releases(fiq->lock)
1160 if (fc->minor < 16 || fiq->forget_list_head.next->next == NULL)
1161 return fuse_read_single_forget(fiq, cs, nbytes);
1163 return fuse_read_batch_forget(fiq, cs, nbytes);
1167 * Read a single request into the userspace filesystem's buffer. This
1168 * function waits until a request is available, then removes it from
1169 * the pending list and copies request data to userspace buffer. If
1170 * no reply is needed (FORGET) or request has been aborted or there
1171 * was an error during the copying then it's finished by calling
1172 * fuse_request_end(). Otherwise add it to the processing list, and set
1175 static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file,
1176 struct fuse_copy_state *cs, size_t nbytes)
1179 struct fuse_conn *fc = fud->fc;
1180 struct fuse_iqueue *fiq = &fc->iq;
1181 struct fuse_pqueue *fpq = &fud->pq;
1182 struct fuse_req *req;
1183 struct fuse_args *args;
1188 * Require sane minimum read buffer - that has capacity for fixed part
1189 * of any request header + negotiated max_write room for data.
1191 * Historically libfuse reserves 4K for fixed header room, but e.g.
1192 * GlusterFS reserves only 80 bytes
1194 * = `sizeof(fuse_in_header) + sizeof(fuse_write_in)`
1196 * which is the absolute minimum any sane filesystem should be using
1199 if (nbytes < max_t(size_t, FUSE_MIN_READ_BUFFER,
1200 sizeof(struct fuse_in_header) +
1201 sizeof(struct fuse_write_in) +
1207 spin_lock(&fiq->lock);
1208 if (!fiq->connected || request_pending(fiq))
1210 spin_unlock(&fiq->lock);
1212 if (file->f_flags & O_NONBLOCK)
1214 err = wait_event_interruptible_exclusive(fiq->waitq,
1215 !fiq->connected || request_pending(fiq));
1220 if (!fiq->connected) {
1221 err = fc->aborted ? -ECONNABORTED : -ENODEV;
1225 if (!list_empty(&fiq->interrupts)) {
1226 req = list_entry(fiq->interrupts.next, struct fuse_req,
1228 return fuse_read_interrupt(fiq, cs, nbytes, req);
1231 if (forget_pending(fiq)) {
1232 if (list_empty(&fiq->pending) || fiq->forget_batch-- > 0)
1233 return fuse_read_forget(fc, fiq, cs, nbytes);
1235 if (fiq->forget_batch <= -8)
1236 fiq->forget_batch = 16;
1239 req = list_entry(fiq->pending.next, struct fuse_req, list);
1240 clear_bit(FR_PENDING, &req->flags);
1241 list_del_init(&req->list);
1242 spin_unlock(&fiq->lock);
1245 reqsize = req->in.h.len;
1247 /* If request is too large, reply with an error and restart the read */
1248 if (nbytes < reqsize) {
1249 req->out.h.error = -EIO;
1250 /* SETXATTR is special, since it may contain too large data */
1251 if (args->opcode == FUSE_SETXATTR)
1252 req->out.h.error = -E2BIG;
1253 fuse_request_end(fc, req);
1256 spin_lock(&fpq->lock);
1257 list_add(&req->list, &fpq->io);
1258 spin_unlock(&fpq->lock);
1260 err = fuse_copy_one(cs, &req->in.h, sizeof(req->in.h));
1262 err = fuse_copy_args(cs, args->in_numargs, args->in_pages,
1263 (struct fuse_arg *) args->in_args, 0);
1264 fuse_copy_finish(cs);
1265 spin_lock(&fpq->lock);
1266 clear_bit(FR_LOCKED, &req->flags);
1267 if (!fpq->connected) {
1268 err = fc->aborted ? -ECONNABORTED : -ENODEV;
1272 req->out.h.error = -EIO;
1275 if (!test_bit(FR_ISREPLY, &req->flags)) {
1279 hash = fuse_req_hash(req->in.h.unique);
1280 list_move_tail(&req->list, &fpq->processing[hash]);
1281 __fuse_get_request(req);
1282 set_bit(FR_SENT, &req->flags);
1283 spin_unlock(&fpq->lock);
1284 /* matches barrier in request_wait_answer() */
1285 smp_mb__after_atomic();
1286 if (test_bit(FR_INTERRUPTED, &req->flags))
1287 queue_interrupt(fiq, req);
1288 fuse_put_request(fc, req);
1293 if (!test_bit(FR_PRIVATE, &req->flags))
1294 list_del_init(&req->list);
1295 spin_unlock(&fpq->lock);
1296 fuse_request_end(fc, req);
1300 spin_unlock(&fiq->lock);
1304 static int fuse_dev_open(struct inode *inode, struct file *file)
1307 * The fuse device's file's private_data is used to hold
1308 * the fuse_conn(ection) when it is mounted, and is used to
1309 * keep track of whether the file has been mounted already.
1311 file->private_data = NULL;
1315 static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to)
1317 struct fuse_copy_state cs;
1318 struct file *file = iocb->ki_filp;
1319 struct fuse_dev *fud = fuse_get_dev(file);
1324 if (!iter_is_iovec(to))
1327 fuse_copy_init(&cs, 1, to);
1329 return fuse_dev_do_read(fud, file, &cs, iov_iter_count(to));
1332 static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1333 struct pipe_inode_info *pipe,
1334 size_t len, unsigned int flags)
1338 struct pipe_buffer *bufs;
1339 struct fuse_copy_state cs;
1340 struct fuse_dev *fud = fuse_get_dev(in);
1345 bufs = kvmalloc_array(pipe->max_usage, sizeof(struct pipe_buffer),
1350 fuse_copy_init(&cs, 1, NULL);
1353 ret = fuse_dev_do_read(fud, in, &cs, len);
1357 if (pipe_occupancy(pipe->head, pipe->tail) + cs.nr_segs > pipe->max_usage) {
1362 for (ret = total = 0; page_nr < cs.nr_segs; total += ret) {
1364 * Need to be careful about this. Having buf->ops in module
1365 * code can Oops if the buffer persists after module unload.
1367 bufs[page_nr].ops = &nosteal_pipe_buf_ops;
1368 bufs[page_nr].flags = 0;
1369 ret = add_to_pipe(pipe, &bufs[page_nr++]);
1370 if (unlikely(ret < 0))
1376 for (; page_nr < cs.nr_segs; page_nr++)
1377 put_page(bufs[page_nr].page);
1383 static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1384 struct fuse_copy_state *cs)
1386 struct fuse_notify_poll_wakeup_out outarg;
1389 if (size != sizeof(outarg))
1392 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1396 fuse_copy_finish(cs);
1397 return fuse_notify_poll_wakeup(fc, &outarg);
1400 fuse_copy_finish(cs);
1404 static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1405 struct fuse_copy_state *cs)
1407 struct fuse_notify_inval_inode_out outarg;
1410 if (size != sizeof(outarg))
1413 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1416 fuse_copy_finish(cs);
1418 down_read(&fc->killsb);
1421 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1422 outarg.off, outarg.len);
1424 up_read(&fc->killsb);
1428 fuse_copy_finish(cs);
1432 static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1433 struct fuse_copy_state *cs)
1435 struct fuse_notify_inval_entry_out outarg;
1440 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1445 if (size < sizeof(outarg))
1448 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1452 err = -ENAMETOOLONG;
1453 if (outarg.namelen > FUSE_NAME_MAX)
1457 if (size != sizeof(outarg) + outarg.namelen + 1)
1461 name.len = outarg.namelen;
1462 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1465 fuse_copy_finish(cs);
1466 buf[outarg.namelen] = 0;
1468 down_read(&fc->killsb);
1471 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
1472 up_read(&fc->killsb);
1478 fuse_copy_finish(cs);
1482 static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1483 struct fuse_copy_state *cs)
1485 struct fuse_notify_delete_out outarg;
1490 buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1495 if (size < sizeof(outarg))
1498 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1502 err = -ENAMETOOLONG;
1503 if (outarg.namelen > FUSE_NAME_MAX)
1507 if (size != sizeof(outarg) + outarg.namelen + 1)
1511 name.len = outarg.namelen;
1512 err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1515 fuse_copy_finish(cs);
1516 buf[outarg.namelen] = 0;
1518 down_read(&fc->killsb);
1521 err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
1522 outarg.child, &name);
1523 up_read(&fc->killsb);
1529 fuse_copy_finish(cs);
1533 static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1534 struct fuse_copy_state *cs)
1536 struct fuse_notify_store_out outarg;
1537 struct inode *inode;
1538 struct address_space *mapping;
1542 unsigned int offset;
1548 if (size < sizeof(outarg))
1551 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1556 if (size - sizeof(outarg) != outarg.size)
1559 nodeid = outarg.nodeid;
1561 down_read(&fc->killsb);
1567 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1571 mapping = inode->i_mapping;
1572 index = outarg.offset >> PAGE_SHIFT;
1573 offset = outarg.offset & ~PAGE_MASK;
1574 file_size = i_size_read(inode);
1575 end = outarg.offset + outarg.size;
1576 if (end > file_size) {
1578 fuse_write_update_size(inode, file_size);
1584 unsigned int this_num;
1587 page = find_or_create_page(mapping, index,
1588 mapping_gfp_mask(mapping));
1592 this_num = min_t(unsigned, num, PAGE_SIZE - offset);
1593 err = fuse_copy_page(cs, &page, offset, this_num, 0);
1594 if (!err && offset == 0 &&
1595 (this_num == PAGE_SIZE || file_size == end))
1596 SetPageUptodate(page);
1613 up_read(&fc->killsb);
1615 fuse_copy_finish(cs);
1619 struct fuse_retrieve_args {
1620 struct fuse_args_pages ap;
1621 struct fuse_notify_retrieve_in inarg;
1624 static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_args *args,
1627 struct fuse_retrieve_args *ra =
1628 container_of(args, typeof(*ra), ap.args);
1630 release_pages(ra->ap.pages, ra->ap.num_pages);
1634 static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
1635 struct fuse_notify_retrieve_out *outarg)
1638 struct address_space *mapping = inode->i_mapping;
1642 unsigned int offset;
1643 size_t total_len = 0;
1644 unsigned int num_pages;
1645 struct fuse_retrieve_args *ra;
1646 size_t args_size = sizeof(*ra);
1647 struct fuse_args_pages *ap;
1648 struct fuse_args *args;
1650 offset = outarg->offset & ~PAGE_MASK;
1651 file_size = i_size_read(inode);
1653 num = min(outarg->size, fc->max_write);
1654 if (outarg->offset > file_size)
1656 else if (outarg->offset + num > file_size)
1657 num = file_size - outarg->offset;
1659 num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1660 num_pages = min(num_pages, fc->max_pages);
1662 args_size += num_pages * (sizeof(ap->pages[0]) + sizeof(ap->descs[0]));
1664 ra = kzalloc(args_size, GFP_KERNEL);
1669 ap->pages = (void *) (ra + 1);
1670 ap->descs = (void *) (ap->pages + num_pages);
1673 args->nodeid = outarg->nodeid;
1674 args->opcode = FUSE_NOTIFY_REPLY;
1675 args->in_numargs = 2;
1676 args->in_pages = true;
1677 args->end = fuse_retrieve_end;
1679 index = outarg->offset >> PAGE_SHIFT;
1681 while (num && ap->num_pages < num_pages) {
1683 unsigned int this_num;
1685 page = find_get_page(mapping, index);
1689 this_num = min_t(unsigned, num, PAGE_SIZE - offset);
1690 ap->pages[ap->num_pages] = page;
1691 ap->descs[ap->num_pages].offset = offset;
1692 ap->descs[ap->num_pages].length = this_num;
1697 total_len += this_num;
1700 ra->inarg.offset = outarg->offset;
1701 ra->inarg.size = total_len;
1702 args->in_args[0].size = sizeof(ra->inarg);
1703 args->in_args[0].value = &ra->inarg;
1704 args->in_args[1].size = total_len;
1706 err = fuse_simple_notify_reply(fc, args, outarg->notify_unique);
1708 fuse_retrieve_end(fc, args, err);
1713 static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1714 struct fuse_copy_state *cs)
1716 struct fuse_notify_retrieve_out outarg;
1717 struct inode *inode;
1721 if (size != sizeof(outarg))
1724 err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1728 fuse_copy_finish(cs);
1730 down_read(&fc->killsb);
1733 u64 nodeid = outarg.nodeid;
1735 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1737 err = fuse_retrieve(fc, inode, &outarg);
1741 up_read(&fc->killsb);
1746 fuse_copy_finish(cs);
1750 static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1751 unsigned int size, struct fuse_copy_state *cs)
1753 /* Don't try to move pages (yet) */
1757 case FUSE_NOTIFY_POLL:
1758 return fuse_notify_poll(fc, size, cs);
1760 case FUSE_NOTIFY_INVAL_INODE:
1761 return fuse_notify_inval_inode(fc, size, cs);
1763 case FUSE_NOTIFY_INVAL_ENTRY:
1764 return fuse_notify_inval_entry(fc, size, cs);
1766 case FUSE_NOTIFY_STORE:
1767 return fuse_notify_store(fc, size, cs);
1769 case FUSE_NOTIFY_RETRIEVE:
1770 return fuse_notify_retrieve(fc, size, cs);
1772 case FUSE_NOTIFY_DELETE:
1773 return fuse_notify_delete(fc, size, cs);
1776 fuse_copy_finish(cs);
1781 /* Look up request on processing list by unique ID */
1782 static struct fuse_req *request_find(struct fuse_pqueue *fpq, u64 unique)
1784 unsigned int hash = fuse_req_hash(unique);
1785 struct fuse_req *req;
1787 list_for_each_entry(req, &fpq->processing[hash], list) {
1788 if (req->in.h.unique == unique)
1794 static int copy_out_args(struct fuse_copy_state *cs, struct fuse_args *args,
1797 unsigned reqsize = sizeof(struct fuse_out_header);
1799 reqsize += fuse_len_args(args->out_numargs, args->out_args);
1801 if (reqsize < nbytes || (reqsize > nbytes && !args->out_argvar))
1803 else if (reqsize > nbytes) {
1804 struct fuse_arg *lastarg = &args->out_args[args->out_numargs-1];
1805 unsigned diffsize = reqsize - nbytes;
1807 if (diffsize > lastarg->size)
1809 lastarg->size -= diffsize;
1811 return fuse_copy_args(cs, args->out_numargs, args->out_pages,
1812 args->out_args, args->page_zeroing);
1816 * Write a single reply to a request. First the header is copied from
1817 * the write buffer. The request is then searched on the processing
1818 * list by the unique ID found in the header. If found, then remove
1819 * it from the list and copy the rest of the buffer to the request.
1820 * The request is finished by calling fuse_request_end().
1822 static ssize_t fuse_dev_do_write(struct fuse_dev *fud,
1823 struct fuse_copy_state *cs, size_t nbytes)
1826 struct fuse_conn *fc = fud->fc;
1827 struct fuse_pqueue *fpq = &fud->pq;
1828 struct fuse_req *req;
1829 struct fuse_out_header oh;
1832 if (nbytes < sizeof(struct fuse_out_header))
1835 err = fuse_copy_one(cs, &oh, sizeof(oh));
1840 if (oh.len != nbytes)
1844 * Zero oh.unique indicates unsolicited notification message
1845 * and error contains notification code.
1848 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
1853 if (oh.error <= -1000 || oh.error > 0)
1856 spin_lock(&fpq->lock);
1859 req = request_find(fpq, oh.unique & ~FUSE_INT_REQ_BIT);
1863 spin_unlock(&fpq->lock);
1867 /* Is it an interrupt reply ID? */
1868 if (oh.unique & FUSE_INT_REQ_BIT) {
1869 __fuse_get_request(req);
1870 spin_unlock(&fpq->lock);
1873 if (nbytes != sizeof(struct fuse_out_header))
1875 else if (oh.error == -ENOSYS)
1876 fc->no_interrupt = 1;
1877 else if (oh.error == -EAGAIN)
1878 err = queue_interrupt(&fc->iq, req);
1880 fuse_put_request(fc, req);
1885 clear_bit(FR_SENT, &req->flags);
1886 list_move(&req->list, &fpq->io);
1888 set_bit(FR_LOCKED, &req->flags);
1889 spin_unlock(&fpq->lock);
1891 if (!req->args->page_replace)
1895 err = nbytes != sizeof(oh) ? -EINVAL : 0;
1897 err = copy_out_args(cs, req->args, nbytes);
1898 fuse_copy_finish(cs);
1900 spin_lock(&fpq->lock);
1901 clear_bit(FR_LOCKED, &req->flags);
1902 if (!fpq->connected)
1905 req->out.h.error = -EIO;
1906 if (!test_bit(FR_PRIVATE, &req->flags))
1907 list_del_init(&req->list);
1908 spin_unlock(&fpq->lock);
1910 fuse_request_end(fc, req);
1912 return err ? err : nbytes;
1915 fuse_copy_finish(cs);
1919 static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from)
1921 struct fuse_copy_state cs;
1922 struct fuse_dev *fud = fuse_get_dev(iocb->ki_filp);
1927 if (!iter_is_iovec(from))
1930 fuse_copy_init(&cs, 0, from);
1932 return fuse_dev_do_write(fud, &cs, iov_iter_count(from));
1935 static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
1936 struct file *out, loff_t *ppos,
1937 size_t len, unsigned int flags)
1939 unsigned int head, tail, mask, count;
1942 struct pipe_buffer *bufs;
1943 struct fuse_copy_state cs;
1944 struct fuse_dev *fud;
1948 fud = fuse_get_dev(out);
1956 mask = pipe->ring_size - 1;
1957 count = head - tail;
1959 bufs = kvmalloc_array(count, sizeof(struct pipe_buffer), GFP_KERNEL);
1967 for (idx = tail; idx != head && rem < len; idx++)
1968 rem += pipe->bufs[idx & mask].len;
1976 struct pipe_buffer *ibuf;
1977 struct pipe_buffer *obuf;
1979 if (WARN_ON(nbuf >= count || tail == head))
1982 ibuf = &pipe->bufs[tail & mask];
1985 if (rem >= ibuf->len) {
1991 if (!pipe_buf_get(pipe, ibuf))
1995 obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
1997 ibuf->offset += obuf->len;
1998 ibuf->len -= obuf->len;
2005 fuse_copy_init(&cs, 0, NULL);
2010 if (flags & SPLICE_F_MOVE)
2013 ret = fuse_dev_do_write(fud, &cs, len);
2017 for (idx = 0; idx < nbuf; idx++)
2018 pipe_buf_release(pipe, &bufs[idx]);
2025 static __poll_t fuse_dev_poll(struct file *file, poll_table *wait)
2027 __poll_t mask = EPOLLOUT | EPOLLWRNORM;
2028 struct fuse_iqueue *fiq;
2029 struct fuse_dev *fud = fuse_get_dev(file);
2035 poll_wait(file, &fiq->waitq, wait);
2037 spin_lock(&fiq->lock);
2038 if (!fiq->connected)
2040 else if (request_pending(fiq))
2041 mask |= EPOLLIN | EPOLLRDNORM;
2042 spin_unlock(&fiq->lock);
2047 /* Abort all requests on the given list (pending or processing) */
2048 static void end_requests(struct fuse_conn *fc, struct list_head *head)
2050 while (!list_empty(head)) {
2051 struct fuse_req *req;
2052 req = list_entry(head->next, struct fuse_req, list);
2053 req->out.h.error = -ECONNABORTED;
2054 clear_bit(FR_SENT, &req->flags);
2055 list_del_init(&req->list);
2056 fuse_request_end(fc, req);
2060 static void end_polls(struct fuse_conn *fc)
2064 p = rb_first(&fc->polled_files);
2067 struct fuse_file *ff;
2068 ff = rb_entry(p, struct fuse_file, polled_node);
2069 wake_up_interruptible_all(&ff->poll_wait);
2076 * Abort all requests.
2078 * Emergency exit in case of a malicious or accidental deadlock, or just a hung
2081 * The same effect is usually achievable through killing the filesystem daemon
2082 * and all users of the filesystem. The exception is the combination of an
2083 * asynchronous request and the tricky deadlock (see
2084 * Documentation/filesystems/fuse.rst).
2086 * Aborting requests under I/O goes as follows: 1: Separate out unlocked
2087 * requests, they should be finished off immediately. Locked requests will be
2088 * finished after unlock; see unlock_request(). 2: Finish off the unlocked
2089 * requests. It is possible that some request will finish before we can. This
2090 * is OK, the request will in that case be removed from the list before we touch
2093 void fuse_abort_conn(struct fuse_conn *fc)
2095 struct fuse_iqueue *fiq = &fc->iq;
2097 spin_lock(&fc->lock);
2098 if (fc->connected) {
2099 struct fuse_dev *fud;
2100 struct fuse_req *req, *next;
2104 /* Background queuing checks fc->connected under bg_lock */
2105 spin_lock(&fc->bg_lock);
2107 spin_unlock(&fc->bg_lock);
2109 fuse_set_initialized(fc);
2110 list_for_each_entry(fud, &fc->devices, entry) {
2111 struct fuse_pqueue *fpq = &fud->pq;
2113 spin_lock(&fpq->lock);
2115 list_for_each_entry_safe(req, next, &fpq->io, list) {
2116 req->out.h.error = -ECONNABORTED;
2117 spin_lock(&req->waitq.lock);
2118 set_bit(FR_ABORTED, &req->flags);
2119 if (!test_bit(FR_LOCKED, &req->flags)) {
2120 set_bit(FR_PRIVATE, &req->flags);
2121 __fuse_get_request(req);
2122 list_move(&req->list, &to_end);
2124 spin_unlock(&req->waitq.lock);
2126 for (i = 0; i < FUSE_PQ_HASH_SIZE; i++)
2127 list_splice_tail_init(&fpq->processing[i],
2129 spin_unlock(&fpq->lock);
2131 spin_lock(&fc->bg_lock);
2133 fc->max_background = UINT_MAX;
2135 spin_unlock(&fc->bg_lock);
2137 spin_lock(&fiq->lock);
2139 list_for_each_entry(req, &fiq->pending, list)
2140 clear_bit(FR_PENDING, &req->flags);
2141 list_splice_tail_init(&fiq->pending, &to_end);
2142 while (forget_pending(fiq))
2143 kfree(fuse_dequeue_forget(fiq, 1, NULL));
2144 wake_up_all(&fiq->waitq);
2145 spin_unlock(&fiq->lock);
2146 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
2148 wake_up_all(&fc->blocked_waitq);
2149 spin_unlock(&fc->lock);
2151 end_requests(fc, &to_end);
2153 spin_unlock(&fc->lock);
2156 EXPORT_SYMBOL_GPL(fuse_abort_conn);
2158 void fuse_wait_aborted(struct fuse_conn *fc)
2160 /* matches implicit memory barrier in fuse_drop_waiting() */
2162 wait_event(fc->blocked_waitq, atomic_read(&fc->num_waiting) == 0);
2165 int fuse_dev_release(struct inode *inode, struct file *file)
2167 struct fuse_dev *fud = fuse_get_dev(file);
2170 struct fuse_conn *fc = fud->fc;
2171 struct fuse_pqueue *fpq = &fud->pq;
2175 spin_lock(&fpq->lock);
2176 WARN_ON(!list_empty(&fpq->io));
2177 for (i = 0; i < FUSE_PQ_HASH_SIZE; i++)
2178 list_splice_init(&fpq->processing[i], &to_end);
2179 spin_unlock(&fpq->lock);
2181 end_requests(fc, &to_end);
2183 /* Are we the last open device? */
2184 if (atomic_dec_and_test(&fc->dev_count)) {
2185 WARN_ON(fc->iq.fasync != NULL);
2186 fuse_abort_conn(fc);
2192 EXPORT_SYMBOL_GPL(fuse_dev_release);
2194 static int fuse_dev_fasync(int fd, struct file *file, int on)
2196 struct fuse_dev *fud = fuse_get_dev(file);
2201 /* No locking - fasync_helper does its own locking */
2202 return fasync_helper(fd, file, on, &fud->fc->iq.fasync);
2205 static int fuse_device_clone(struct fuse_conn *fc, struct file *new)
2207 struct fuse_dev *fud;
2209 if (new->private_data)
2212 fud = fuse_dev_alloc_install(fc);
2216 new->private_data = fud;
2217 atomic_inc(&fc->dev_count);
2222 static long fuse_dev_ioctl(struct file *file, unsigned int cmd,
2227 if (cmd == FUSE_DEV_IOC_CLONE) {
2231 if (!get_user(oldfd, (__u32 __user *) arg)) {
2232 struct file *old = fget(oldfd);
2236 struct fuse_dev *fud = NULL;
2239 * Check against file->f_op because CUSE
2240 * uses the same ioctl handler.
2242 if (old->f_op == file->f_op &&
2243 old->f_cred->user_ns == file->f_cred->user_ns)
2244 fud = fuse_get_dev(old);
2247 mutex_lock(&fuse_mutex);
2248 err = fuse_device_clone(fud->fc, file);
2249 mutex_unlock(&fuse_mutex);
2258 const struct file_operations fuse_dev_operations = {
2259 .owner = THIS_MODULE,
2260 .open = fuse_dev_open,
2261 .llseek = no_llseek,
2262 .read_iter = fuse_dev_read,
2263 .splice_read = fuse_dev_splice_read,
2264 .write_iter = fuse_dev_write,
2265 .splice_write = fuse_dev_splice_write,
2266 .poll = fuse_dev_poll,
2267 .release = fuse_dev_release,
2268 .fasync = fuse_dev_fasync,
2269 .unlocked_ioctl = fuse_dev_ioctl,
2270 .compat_ioctl = compat_ptr_ioctl,
2272 EXPORT_SYMBOL_GPL(fuse_dev_operations);
2274 static struct miscdevice fuse_miscdevice = {
2275 .minor = FUSE_MINOR,
2277 .fops = &fuse_dev_operations,
2280 int __init fuse_dev_init(void)
2283 fuse_req_cachep = kmem_cache_create("fuse_request",
2284 sizeof(struct fuse_req),
2286 if (!fuse_req_cachep)
2289 err = misc_register(&fuse_miscdevice);
2291 goto out_cache_clean;
2296 kmem_cache_destroy(fuse_req_cachep);
2301 void fuse_dev_cleanup(void)
2303 misc_deregister(&fuse_miscdevice);
2304 kmem_cache_destroy(fuse_req_cachep);