drm/rockchip: lvds: if port is null return -EINVAL
[firefly-linux-kernel-4.4.55.git] / fs / fuse / dev.c
1 /*
2   FUSE: Filesystem in Userspace
3   Copyright (C) 2001-2008  Miklos Szeredi <miklos@szeredi.hu>
4
5   This program can be distributed under the terms of the GNU GPL.
6   See the file COPYING.
7 */
8
9 #include "fuse_i.h"
10
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/namei.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/freezer.h>
24
25 MODULE_ALIAS_MISCDEV(FUSE_MINOR);
26 MODULE_ALIAS("devname:fuse");
27
28 static struct kmem_cache *fuse_req_cachep;
29
30 static struct fuse_dev *fuse_get_dev(struct file *file)
31 {
32         /*
33          * Lockless access is OK, because file->private data is set
34          * once during mount and is valid until the file is released.
35          */
36         return ACCESS_ONCE(file->private_data);
37 }
38
39 static void fuse_request_init(struct fuse_req *req, struct page **pages,
40                               struct fuse_page_desc *page_descs,
41                               unsigned npages)
42 {
43         memset(req, 0, sizeof(*req));
44         memset(pages, 0, sizeof(*pages) * npages);
45         memset(page_descs, 0, sizeof(*page_descs) * npages);
46         INIT_LIST_HEAD(&req->list);
47         INIT_LIST_HEAD(&req->intr_entry);
48         init_waitqueue_head(&req->waitq);
49         atomic_set(&req->count, 1);
50         req->pages = pages;
51         req->page_descs = page_descs;
52         req->max_pages = npages;
53         __set_bit(FR_PENDING, &req->flags);
54 }
55
56 static struct fuse_req *__fuse_request_alloc(unsigned npages, gfp_t flags)
57 {
58         struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, flags);
59         if (req) {
60                 struct page **pages;
61                 struct fuse_page_desc *page_descs;
62
63                 if (npages <= FUSE_REQ_INLINE_PAGES) {
64                         pages = req->inline_pages;
65                         page_descs = req->inline_page_descs;
66                 } else {
67                         pages = kmalloc(sizeof(struct page *) * npages, flags);
68                         page_descs = kmalloc(sizeof(struct fuse_page_desc) *
69                                              npages, flags);
70                 }
71
72                 if (!pages || !page_descs) {
73                         kfree(pages);
74                         kfree(page_descs);
75                         kmem_cache_free(fuse_req_cachep, req);
76                         return NULL;
77                 }
78
79                 fuse_request_init(req, pages, page_descs, npages);
80         }
81         return req;
82 }
83
84 struct fuse_req *fuse_request_alloc(unsigned npages)
85 {
86         return __fuse_request_alloc(npages, GFP_KERNEL);
87 }
88 EXPORT_SYMBOL_GPL(fuse_request_alloc);
89
90 struct fuse_req *fuse_request_alloc_nofs(unsigned npages)
91 {
92         return __fuse_request_alloc(npages, GFP_NOFS);
93 }
94
95 void fuse_request_free(struct fuse_req *req)
96 {
97         if (req->pages != req->inline_pages) {
98                 kfree(req->pages);
99                 kfree(req->page_descs);
100         }
101         kmem_cache_free(fuse_req_cachep, req);
102 }
103
104 static void block_sigs(sigset_t *oldset)
105 {
106         sigset_t mask;
107
108         siginitsetinv(&mask, sigmask(SIGKILL));
109         sigprocmask(SIG_BLOCK, &mask, oldset);
110 }
111
112 static void restore_sigs(sigset_t *oldset)
113 {
114         sigprocmask(SIG_SETMASK, oldset, NULL);
115 }
116
117 void __fuse_get_request(struct fuse_req *req)
118 {
119         atomic_inc(&req->count);
120 }
121
122 /* Must be called with > 1 refcount */
123 static void __fuse_put_request(struct fuse_req *req)
124 {
125         BUG_ON(atomic_read(&req->count) < 2);
126         atomic_dec(&req->count);
127 }
128
129 static void fuse_req_init_context(struct fuse_req *req)
130 {
131         req->in.h.uid = from_kuid_munged(&init_user_ns, current_fsuid());
132         req->in.h.gid = from_kgid_munged(&init_user_ns, current_fsgid());
133         req->in.h.pid = current->pid;
134 }
135
136 void fuse_set_initialized(struct fuse_conn *fc)
137 {
138         /* Make sure stores before this are seen on another CPU */
139         smp_wmb();
140         fc->initialized = 1;
141 }
142
143 static bool fuse_block_alloc(struct fuse_conn *fc, bool for_background)
144 {
145         return !fc->initialized || (for_background && fc->blocked);
146 }
147
148 static struct fuse_req *__fuse_get_req(struct fuse_conn *fc, unsigned npages,
149                                        bool for_background)
150 {
151         struct fuse_req *req;
152         int err;
153         atomic_inc(&fc->num_waiting);
154
155         if (fuse_block_alloc(fc, for_background)) {
156                 sigset_t oldset;
157                 int intr;
158
159                 block_sigs(&oldset);
160                 intr = wait_event_interruptible_exclusive(fc->blocked_waitq,
161                                 !fuse_block_alloc(fc, for_background));
162                 restore_sigs(&oldset);
163                 err = -EINTR;
164                 if (intr)
165                         goto out;
166         }
167         /* Matches smp_wmb() in fuse_set_initialized() */
168         smp_rmb();
169
170         err = -ENOTCONN;
171         if (!fc->connected)
172                 goto out;
173
174         err = -ECONNREFUSED;
175         if (fc->conn_error)
176                 goto out;
177
178         req = fuse_request_alloc(npages);
179         err = -ENOMEM;
180         if (!req) {
181                 if (for_background)
182                         wake_up(&fc->blocked_waitq);
183                 goto out;
184         }
185
186         fuse_req_init_context(req);
187         __set_bit(FR_WAITING, &req->flags);
188         if (for_background)
189                 __set_bit(FR_BACKGROUND, &req->flags);
190
191         return req;
192
193  out:
194         atomic_dec(&fc->num_waiting);
195         return ERR_PTR(err);
196 }
197
198 struct fuse_req *fuse_get_req(struct fuse_conn *fc, unsigned npages)
199 {
200         return __fuse_get_req(fc, npages, false);
201 }
202 EXPORT_SYMBOL_GPL(fuse_get_req);
203
204 struct fuse_req *fuse_get_req_for_background(struct fuse_conn *fc,
205                                              unsigned npages)
206 {
207         return __fuse_get_req(fc, npages, true);
208 }
209 EXPORT_SYMBOL_GPL(fuse_get_req_for_background);
210
211 /*
212  * Return request in fuse_file->reserved_req.  However that may
213  * currently be in use.  If that is the case, wait for it to become
214  * available.
215  */
216 static struct fuse_req *get_reserved_req(struct fuse_conn *fc,
217                                          struct file *file)
218 {
219         struct fuse_req *req = NULL;
220         struct fuse_file *ff = file->private_data;
221
222         do {
223                 wait_event(fc->reserved_req_waitq, ff->reserved_req);
224                 spin_lock(&fc->lock);
225                 if (ff->reserved_req) {
226                         req = ff->reserved_req;
227                         ff->reserved_req = NULL;
228                         req->stolen_file = get_file(file);
229                 }
230                 spin_unlock(&fc->lock);
231         } while (!req);
232
233         return req;
234 }
235
236 /*
237  * Put stolen request back into fuse_file->reserved_req
238  */
239 static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req)
240 {
241         struct file *file = req->stolen_file;
242         struct fuse_file *ff = file->private_data;
243
244         spin_lock(&fc->lock);
245         fuse_request_init(req, req->pages, req->page_descs, req->max_pages);
246         BUG_ON(ff->reserved_req);
247         ff->reserved_req = req;
248         wake_up_all(&fc->reserved_req_waitq);
249         spin_unlock(&fc->lock);
250         fput(file);
251 }
252
253 /*
254  * Gets a requests for a file operation, always succeeds
255  *
256  * This is used for sending the FLUSH request, which must get to
257  * userspace, due to POSIX locks which may need to be unlocked.
258  *
259  * If allocation fails due to OOM, use the reserved request in
260  * fuse_file.
261  *
262  * This is very unlikely to deadlock accidentally, since the
263  * filesystem should not have it's own file open.  If deadlock is
264  * intentional, it can still be broken by "aborting" the filesystem.
265  */
266 struct fuse_req *fuse_get_req_nofail_nopages(struct fuse_conn *fc,
267                                              struct file *file)
268 {
269         struct fuse_req *req;
270
271         atomic_inc(&fc->num_waiting);
272         wait_event(fc->blocked_waitq, fc->initialized);
273         /* Matches smp_wmb() in fuse_set_initialized() */
274         smp_rmb();
275         req = fuse_request_alloc(0);
276         if (!req)
277                 req = get_reserved_req(fc, file);
278
279         fuse_req_init_context(req);
280         __set_bit(FR_WAITING, &req->flags);
281         __clear_bit(FR_BACKGROUND, &req->flags);
282         return req;
283 }
284
285 void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req)
286 {
287         if (atomic_dec_and_test(&req->count)) {
288                 if (test_bit(FR_BACKGROUND, &req->flags)) {
289                         /*
290                          * We get here in the unlikely case that a background
291                          * request was allocated but not sent
292                          */
293                         spin_lock(&fc->lock);
294                         if (!fc->blocked)
295                                 wake_up(&fc->blocked_waitq);
296                         spin_unlock(&fc->lock);
297                 }
298
299                 if (test_bit(FR_WAITING, &req->flags)) {
300                         __clear_bit(FR_WAITING, &req->flags);
301                         atomic_dec(&fc->num_waiting);
302                 }
303
304                 if (req->stolen_file)
305                         put_reserved_req(fc, req);
306                 else
307                         fuse_request_free(req);
308         }
309 }
310 EXPORT_SYMBOL_GPL(fuse_put_request);
311
312 static unsigned len_args(unsigned numargs, struct fuse_arg *args)
313 {
314         unsigned nbytes = 0;
315         unsigned i;
316
317         for (i = 0; i < numargs; i++)
318                 nbytes += args[i].size;
319
320         return nbytes;
321 }
322
323 static u64 fuse_get_unique(struct fuse_iqueue *fiq)
324 {
325         return ++fiq->reqctr;
326 }
327
328 static void queue_request(struct fuse_iqueue *fiq, struct fuse_req *req)
329 {
330         req->in.h.len = sizeof(struct fuse_in_header) +
331                 len_args(req->in.numargs, (struct fuse_arg *) req->in.args);
332         list_add_tail(&req->list, &fiq->pending);
333         wake_up_locked(&fiq->waitq);
334         kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
335 }
336
337 void fuse_queue_forget(struct fuse_conn *fc, struct fuse_forget_link *forget,
338                        u64 nodeid, u64 nlookup)
339 {
340         struct fuse_iqueue *fiq = &fc->iq;
341
342         forget->forget_one.nodeid = nodeid;
343         forget->forget_one.nlookup = nlookup;
344
345         spin_lock(&fiq->waitq.lock);
346         if (fiq->connected) {
347                 fiq->forget_list_tail->next = forget;
348                 fiq->forget_list_tail = forget;
349                 wake_up_locked(&fiq->waitq);
350                 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
351         } else {
352                 kfree(forget);
353         }
354         spin_unlock(&fiq->waitq.lock);
355 }
356
357 static void flush_bg_queue(struct fuse_conn *fc)
358 {
359         while (fc->active_background < fc->max_background &&
360                !list_empty(&fc->bg_queue)) {
361                 struct fuse_req *req;
362                 struct fuse_iqueue *fiq = &fc->iq;
363
364                 req = list_entry(fc->bg_queue.next, struct fuse_req, list);
365                 list_del(&req->list);
366                 fc->active_background++;
367                 spin_lock(&fiq->waitq.lock);
368                 req->in.h.unique = fuse_get_unique(fiq);
369                 queue_request(fiq, req);
370                 spin_unlock(&fiq->waitq.lock);
371         }
372 }
373
374 /*
375  * This function is called when a request is finished.  Either a reply
376  * has arrived or it was aborted (and not yet sent) or some error
377  * occurred during communication with userspace, or the device file
378  * was closed.  The requester thread is woken up (if still waiting),
379  * the 'end' callback is called if given, else the reference to the
380  * request is released
381  */
382 static void request_end(struct fuse_conn *fc, struct fuse_req *req)
383 {
384         struct fuse_iqueue *fiq = &fc->iq;
385
386         if (test_and_set_bit(FR_FINISHED, &req->flags))
387                 return;
388
389         spin_lock(&fiq->waitq.lock);
390         list_del_init(&req->intr_entry);
391         spin_unlock(&fiq->waitq.lock);
392         WARN_ON(test_bit(FR_PENDING, &req->flags));
393         WARN_ON(test_bit(FR_SENT, &req->flags));
394         if (test_bit(FR_BACKGROUND, &req->flags)) {
395                 spin_lock(&fc->lock);
396                 clear_bit(FR_BACKGROUND, &req->flags);
397                 if (fc->num_background == fc->max_background)
398                         fc->blocked = 0;
399
400                 /* Wake up next waiter, if any */
401                 if (!fc->blocked && waitqueue_active(&fc->blocked_waitq))
402                         wake_up(&fc->blocked_waitq);
403
404                 if (fc->num_background == fc->congestion_threshold &&
405                     fc->connected && fc->bdi_initialized) {
406                         clear_bdi_congested(&fc->bdi, BLK_RW_SYNC);
407                         clear_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
408                 }
409                 fc->num_background--;
410                 fc->active_background--;
411                 flush_bg_queue(fc);
412                 spin_unlock(&fc->lock);
413         }
414         wake_up(&req->waitq);
415         if (req->end)
416                 req->end(fc, req);
417         fuse_put_request(fc, req);
418 }
419
420 static void queue_interrupt(struct fuse_iqueue *fiq, struct fuse_req *req)
421 {
422         spin_lock(&fiq->waitq.lock);
423         if (test_bit(FR_FINISHED, &req->flags)) {
424                 spin_unlock(&fiq->waitq.lock);
425                 return;
426         }
427         if (list_empty(&req->intr_entry)) {
428                 list_add_tail(&req->intr_entry, &fiq->interrupts);
429                 wake_up_locked(&fiq->waitq);
430         }
431         spin_unlock(&fiq->waitq.lock);
432         kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
433 }
434
435 static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req)
436 {
437         struct fuse_iqueue *fiq = &fc->iq;
438         int err;
439
440         if (!fc->no_interrupt) {
441                 /* Any signal may interrupt this */
442                 err = wait_event_interruptible(req->waitq,
443                                         test_bit(FR_FINISHED, &req->flags));
444                 if (!err)
445                         return;
446
447                 set_bit(FR_INTERRUPTED, &req->flags);
448                 /* matches barrier in fuse_dev_do_read() */
449                 smp_mb__after_atomic();
450                 if (test_bit(FR_SENT, &req->flags))
451                         queue_interrupt(fiq, req);
452         }
453
454         if (!test_bit(FR_FORCE, &req->flags)) {
455                 sigset_t oldset;
456
457                 /* Only fatal signals may interrupt this */
458                 block_sigs(&oldset);
459                 err = wait_event_interruptible(req->waitq,
460                                         test_bit(FR_FINISHED, &req->flags));
461                 restore_sigs(&oldset);
462
463                 if (!err)
464                         return;
465
466                 spin_lock(&fiq->waitq.lock);
467                 /* Request is not yet in userspace, bail out */
468                 if (test_bit(FR_PENDING, &req->flags)) {
469                         list_del(&req->list);
470                         spin_unlock(&fiq->waitq.lock);
471                         __fuse_put_request(req);
472                         req->out.h.error = -EINTR;
473                         return;
474                 }
475                 spin_unlock(&fiq->waitq.lock);
476         }
477
478         /*
479          * Either request is already in userspace, or it was forced.
480          * Wait it out.
481          */
482         while (!test_bit(FR_FINISHED, &req->flags))
483                 wait_event_freezable(req->waitq,
484                                 test_bit(FR_FINISHED, &req->flags));
485 }
486
487 static void __fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
488 {
489         struct fuse_iqueue *fiq = &fc->iq;
490
491         BUG_ON(test_bit(FR_BACKGROUND, &req->flags));
492         spin_lock(&fiq->waitq.lock);
493         if (!fiq->connected) {
494                 spin_unlock(&fiq->waitq.lock);
495                 req->out.h.error = -ENOTCONN;
496         } else {
497                 req->in.h.unique = fuse_get_unique(fiq);
498                 queue_request(fiq, req);
499                 /* acquire extra reference, since request is still needed
500                    after request_end() */
501                 __fuse_get_request(req);
502                 spin_unlock(&fiq->waitq.lock);
503
504                 request_wait_answer(fc, req);
505                 /* Pairs with smp_wmb() in request_end() */
506                 smp_rmb();
507         }
508 }
509
510 void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req)
511 {
512         __set_bit(FR_ISREPLY, &req->flags);
513         if (!test_bit(FR_WAITING, &req->flags)) {
514                 __set_bit(FR_WAITING, &req->flags);
515                 atomic_inc(&fc->num_waiting);
516         }
517         __fuse_request_send(fc, req);
518 }
519 EXPORT_SYMBOL_GPL(fuse_request_send);
520
521 static void fuse_adjust_compat(struct fuse_conn *fc, struct fuse_args *args)
522 {
523         if (fc->minor < 4 && args->in.h.opcode == FUSE_STATFS)
524                 args->out.args[0].size = FUSE_COMPAT_STATFS_SIZE;
525
526         if (fc->minor < 9) {
527                 switch (args->in.h.opcode) {
528                 case FUSE_LOOKUP:
529                 case FUSE_CREATE:
530                 case FUSE_MKNOD:
531                 case FUSE_MKDIR:
532                 case FUSE_SYMLINK:
533                 case FUSE_LINK:
534                         args->out.args[0].size = FUSE_COMPAT_ENTRY_OUT_SIZE;
535                         break;
536                 case FUSE_GETATTR:
537                 case FUSE_SETATTR:
538                         args->out.args[0].size = FUSE_COMPAT_ATTR_OUT_SIZE;
539                         break;
540                 }
541         }
542         if (fc->minor < 12) {
543                 switch (args->in.h.opcode) {
544                 case FUSE_CREATE:
545                         args->in.args[0].size = sizeof(struct fuse_open_in);
546                         break;
547                 case FUSE_MKNOD:
548                         args->in.args[0].size = FUSE_COMPAT_MKNOD_IN_SIZE;
549                         break;
550                 }
551         }
552 }
553
554 ssize_t fuse_simple_request(struct fuse_conn *fc, struct fuse_args *args)
555 {
556         struct fuse_req *req;
557         ssize_t ret;
558
559         req = fuse_get_req(fc, 0);
560         if (IS_ERR(req))
561                 return PTR_ERR(req);
562
563         /* Needs to be done after fuse_get_req() so that fc->minor is valid */
564         fuse_adjust_compat(fc, args);
565
566         req->in.h.opcode = args->in.h.opcode;
567         req->in.h.nodeid = args->in.h.nodeid;
568         req->in.numargs = args->in.numargs;
569         memcpy(req->in.args, args->in.args,
570                args->in.numargs * sizeof(struct fuse_in_arg));
571         req->out.argvar = args->out.argvar;
572         req->out.numargs = args->out.numargs;
573         memcpy(req->out.args, args->out.args,
574                args->out.numargs * sizeof(struct fuse_arg));
575         fuse_request_send(fc, req);
576         ret = req->out.h.error;
577         if (!ret && args->out.argvar) {
578                 BUG_ON(args->out.numargs != 1);
579                 ret = req->out.args[0].size;
580         }
581         fuse_put_request(fc, req);
582
583         return ret;
584 }
585
586 /*
587  * Called under fc->lock
588  *
589  * fc->connected must have been checked previously
590  */
591 void fuse_request_send_background_locked(struct fuse_conn *fc,
592                                          struct fuse_req *req)
593 {
594         BUG_ON(!test_bit(FR_BACKGROUND, &req->flags));
595         if (!test_bit(FR_WAITING, &req->flags)) {
596                 __set_bit(FR_WAITING, &req->flags);
597                 atomic_inc(&fc->num_waiting);
598         }
599         __set_bit(FR_ISREPLY, &req->flags);
600         fc->num_background++;
601         if (fc->num_background == fc->max_background)
602                 fc->blocked = 1;
603         if (fc->num_background == fc->congestion_threshold &&
604             fc->bdi_initialized) {
605                 set_bdi_congested(&fc->bdi, BLK_RW_SYNC);
606                 set_bdi_congested(&fc->bdi, BLK_RW_ASYNC);
607         }
608         list_add_tail(&req->list, &fc->bg_queue);
609         flush_bg_queue(fc);
610 }
611
612 void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req)
613 {
614         BUG_ON(!req->end);
615         spin_lock(&fc->lock);
616         if (fc->connected) {
617                 fuse_request_send_background_locked(fc, req);
618                 spin_unlock(&fc->lock);
619         } else {
620                 spin_unlock(&fc->lock);
621                 req->out.h.error = -ENOTCONN;
622                 req->end(fc, req);
623                 fuse_put_request(fc, req);
624         }
625 }
626 EXPORT_SYMBOL_GPL(fuse_request_send_background);
627
628 static int fuse_request_send_notify_reply(struct fuse_conn *fc,
629                                           struct fuse_req *req, u64 unique)
630 {
631         int err = -ENODEV;
632         struct fuse_iqueue *fiq = &fc->iq;
633
634         __clear_bit(FR_ISREPLY, &req->flags);
635         req->in.h.unique = unique;
636         spin_lock(&fiq->waitq.lock);
637         if (fiq->connected) {
638                 queue_request(fiq, req);
639                 err = 0;
640         }
641         spin_unlock(&fiq->waitq.lock);
642
643         return err;
644 }
645
646 void fuse_force_forget(struct file *file, u64 nodeid)
647 {
648         struct inode *inode = file_inode(file);
649         struct fuse_conn *fc = get_fuse_conn(inode);
650         struct fuse_req *req;
651         struct fuse_forget_in inarg;
652
653         memset(&inarg, 0, sizeof(inarg));
654         inarg.nlookup = 1;
655         req = fuse_get_req_nofail_nopages(fc, file);
656         req->in.h.opcode = FUSE_FORGET;
657         req->in.h.nodeid = nodeid;
658         req->in.numargs = 1;
659         req->in.args[0].size = sizeof(inarg);
660         req->in.args[0].value = &inarg;
661         __clear_bit(FR_ISREPLY, &req->flags);
662         __fuse_request_send(fc, req);
663         /* ignore errors */
664         fuse_put_request(fc, req);
665 }
666
667 /*
668  * Lock the request.  Up to the next unlock_request() there mustn't be
669  * anything that could cause a page-fault.  If the request was already
670  * aborted bail out.
671  */
672 static int lock_request(struct fuse_req *req)
673 {
674         int err = 0;
675         if (req) {
676                 spin_lock(&req->waitq.lock);
677                 if (test_bit(FR_ABORTED, &req->flags))
678                         err = -ENOENT;
679                 else
680                         set_bit(FR_LOCKED, &req->flags);
681                 spin_unlock(&req->waitq.lock);
682         }
683         return err;
684 }
685
686 /*
687  * Unlock request.  If it was aborted while locked, caller is responsible
688  * for unlocking and ending the request.
689  */
690 static int unlock_request(struct fuse_req *req)
691 {
692         int err = 0;
693         if (req) {
694                 spin_lock(&req->waitq.lock);
695                 if (test_bit(FR_ABORTED, &req->flags))
696                         err = -ENOENT;
697                 else
698                         clear_bit(FR_LOCKED, &req->flags);
699                 spin_unlock(&req->waitq.lock);
700         }
701         return err;
702 }
703
704 struct fuse_copy_state {
705         int write;
706         struct fuse_req *req;
707         struct iov_iter *iter;
708         struct pipe_buffer *pipebufs;
709         struct pipe_buffer *currbuf;
710         struct pipe_inode_info *pipe;
711         unsigned long nr_segs;
712         struct page *pg;
713         unsigned len;
714         unsigned offset;
715         unsigned move_pages:1;
716 };
717
718 static void fuse_copy_init(struct fuse_copy_state *cs, int write,
719                            struct iov_iter *iter)
720 {
721         memset(cs, 0, sizeof(*cs));
722         cs->write = write;
723         cs->iter = iter;
724 }
725
726 /* Unmap and put previous page of userspace buffer */
727 static void fuse_copy_finish(struct fuse_copy_state *cs)
728 {
729         if (cs->currbuf) {
730                 struct pipe_buffer *buf = cs->currbuf;
731
732                 if (cs->write)
733                         buf->len = PAGE_SIZE - cs->len;
734                 cs->currbuf = NULL;
735         } else if (cs->pg) {
736                 if (cs->write) {
737                         flush_dcache_page(cs->pg);
738                         set_page_dirty_lock(cs->pg);
739                 }
740                 put_page(cs->pg);
741         }
742         cs->pg = NULL;
743 }
744
745 /*
746  * Get another pagefull of userspace buffer, and map it to kernel
747  * address space, and lock request
748  */
749 static int fuse_copy_fill(struct fuse_copy_state *cs)
750 {
751         struct page *page;
752         int err;
753
754         err = unlock_request(cs->req);
755         if (err)
756                 return err;
757
758         fuse_copy_finish(cs);
759         if (cs->pipebufs) {
760                 struct pipe_buffer *buf = cs->pipebufs;
761
762                 if (!cs->write) {
763                         err = buf->ops->confirm(cs->pipe, buf);
764                         if (err)
765                                 return err;
766
767                         BUG_ON(!cs->nr_segs);
768                         cs->currbuf = buf;
769                         cs->pg = buf->page;
770                         cs->offset = buf->offset;
771                         cs->len = buf->len;
772                         cs->pipebufs++;
773                         cs->nr_segs--;
774                 } else {
775                         if (cs->nr_segs == cs->pipe->buffers)
776                                 return -EIO;
777
778                         page = alloc_page(GFP_HIGHUSER);
779                         if (!page)
780                                 return -ENOMEM;
781
782                         buf->page = page;
783                         buf->offset = 0;
784                         buf->len = 0;
785
786                         cs->currbuf = buf;
787                         cs->pg = page;
788                         cs->offset = 0;
789                         cs->len = PAGE_SIZE;
790                         cs->pipebufs++;
791                         cs->nr_segs++;
792                 }
793         } else {
794                 size_t off;
795                 err = iov_iter_get_pages(cs->iter, &page, PAGE_SIZE, 1, &off);
796                 if (err < 0)
797                         return err;
798                 BUG_ON(!err);
799                 cs->len = err;
800                 cs->offset = off;
801                 cs->pg = page;
802                 cs->offset = off;
803                 iov_iter_advance(cs->iter, err);
804         }
805
806         return lock_request(cs->req);
807 }
808
809 /* Do as much copy to/from userspace buffer as we can */
810 static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size)
811 {
812         unsigned ncpy = min(*size, cs->len);
813         if (val) {
814                 void *pgaddr = kmap_atomic(cs->pg);
815                 void *buf = pgaddr + cs->offset;
816
817                 if (cs->write)
818                         memcpy(buf, *val, ncpy);
819                 else
820                         memcpy(*val, buf, ncpy);
821
822                 kunmap_atomic(pgaddr);
823                 *val += ncpy;
824         }
825         *size -= ncpy;
826         cs->len -= ncpy;
827         cs->offset += ncpy;
828         return ncpy;
829 }
830
831 static int fuse_check_page(struct page *page)
832 {
833         if (page_mapcount(page) ||
834             page->mapping != NULL ||
835             page_count(page) != 1 ||
836             (page->flags & PAGE_FLAGS_CHECK_AT_PREP &
837              ~(1 << PG_locked |
838                1 << PG_referenced |
839                1 << PG_uptodate |
840                1 << PG_lru |
841                1 << PG_active |
842                1 << PG_reclaim))) {
843                 printk(KERN_WARNING "fuse: trying to steal weird page\n");
844                 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);
845                 return 1;
846         }
847         return 0;
848 }
849
850 static int fuse_try_move_page(struct fuse_copy_state *cs, struct page **pagep)
851 {
852         int err;
853         struct page *oldpage = *pagep;
854         struct page *newpage;
855         struct pipe_buffer *buf = cs->pipebufs;
856
857         err = unlock_request(cs->req);
858         if (err)
859                 return err;
860
861         fuse_copy_finish(cs);
862
863         err = buf->ops->confirm(cs->pipe, buf);
864         if (err)
865                 return err;
866
867         BUG_ON(!cs->nr_segs);
868         cs->currbuf = buf;
869         cs->len = buf->len;
870         cs->pipebufs++;
871         cs->nr_segs--;
872
873         if (cs->len != PAGE_SIZE)
874                 goto out_fallback;
875
876         if (buf->ops->steal(cs->pipe, buf) != 0)
877                 goto out_fallback;
878
879         newpage = buf->page;
880
881         if (!PageUptodate(newpage))
882                 SetPageUptodate(newpage);
883
884         ClearPageMappedToDisk(newpage);
885
886         if (fuse_check_page(newpage) != 0)
887                 goto out_fallback_unlock;
888
889         /*
890          * This is a new and locked page, it shouldn't be mapped or
891          * have any special flags on it
892          */
893         if (WARN_ON(page_mapped(oldpage)))
894                 goto out_fallback_unlock;
895         if (WARN_ON(page_has_private(oldpage)))
896                 goto out_fallback_unlock;
897         if (WARN_ON(PageDirty(oldpage) || PageWriteback(oldpage)))
898                 goto out_fallback_unlock;
899         if (WARN_ON(PageMlocked(oldpage)))
900                 goto out_fallback_unlock;
901
902         err = replace_page_cache_page(oldpage, newpage, GFP_KERNEL);
903         if (err) {
904                 unlock_page(newpage);
905                 return err;
906         }
907
908         page_cache_get(newpage);
909
910         if (!(buf->flags & PIPE_BUF_FLAG_LRU))
911                 lru_cache_add_file(newpage);
912
913         err = 0;
914         spin_lock(&cs->req->waitq.lock);
915         if (test_bit(FR_ABORTED, &cs->req->flags))
916                 err = -ENOENT;
917         else
918                 *pagep = newpage;
919         spin_unlock(&cs->req->waitq.lock);
920
921         if (err) {
922                 unlock_page(newpage);
923                 page_cache_release(newpage);
924                 return err;
925         }
926
927         unlock_page(oldpage);
928         page_cache_release(oldpage);
929         cs->len = 0;
930
931         return 0;
932
933 out_fallback_unlock:
934         unlock_page(newpage);
935 out_fallback:
936         cs->pg = buf->page;
937         cs->offset = buf->offset;
938
939         err = lock_request(cs->req);
940         if (err)
941                 return err;
942
943         return 1;
944 }
945
946 static int fuse_ref_page(struct fuse_copy_state *cs, struct page *page,
947                          unsigned offset, unsigned count)
948 {
949         struct pipe_buffer *buf;
950         int err;
951
952         if (cs->nr_segs == cs->pipe->buffers)
953                 return -EIO;
954
955         err = unlock_request(cs->req);
956         if (err)
957                 return err;
958
959         fuse_copy_finish(cs);
960
961         buf = cs->pipebufs;
962         page_cache_get(page);
963         buf->page = page;
964         buf->offset = offset;
965         buf->len = count;
966
967         cs->pipebufs++;
968         cs->nr_segs++;
969         cs->len = 0;
970
971         return 0;
972 }
973
974 /*
975  * Copy a page in the request to/from the userspace buffer.  Must be
976  * done atomically
977  */
978 static int fuse_copy_page(struct fuse_copy_state *cs, struct page **pagep,
979                           unsigned offset, unsigned count, int zeroing)
980 {
981         int err;
982         struct page *page = *pagep;
983
984         if (page && zeroing && count < PAGE_SIZE)
985                 clear_highpage(page);
986
987         while (count) {
988                 if (cs->write && cs->pipebufs && page) {
989                         return fuse_ref_page(cs, page, offset, count);
990                 } else if (!cs->len) {
991                         if (cs->move_pages && page &&
992                             offset == 0 && count == PAGE_SIZE) {
993                                 err = fuse_try_move_page(cs, pagep);
994                                 if (err <= 0)
995                                         return err;
996                         } else {
997                                 err = fuse_copy_fill(cs);
998                                 if (err)
999                                         return err;
1000                         }
1001                 }
1002                 if (page) {
1003                         void *mapaddr = kmap_atomic(page);
1004                         void *buf = mapaddr + offset;
1005                         offset += fuse_copy_do(cs, &buf, &count);
1006                         kunmap_atomic(mapaddr);
1007                 } else
1008                         offset += fuse_copy_do(cs, NULL, &count);
1009         }
1010         if (page && !cs->write)
1011                 flush_dcache_page(page);
1012         return 0;
1013 }
1014
1015 /* Copy pages in the request to/from userspace buffer */
1016 static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes,
1017                            int zeroing)
1018 {
1019         unsigned i;
1020         struct fuse_req *req = cs->req;
1021
1022         for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) {
1023                 int err;
1024                 unsigned offset = req->page_descs[i].offset;
1025                 unsigned count = min(nbytes, req->page_descs[i].length);
1026
1027                 err = fuse_copy_page(cs, &req->pages[i], offset, count,
1028                                      zeroing);
1029                 if (err)
1030                         return err;
1031
1032                 nbytes -= count;
1033         }
1034         return 0;
1035 }
1036
1037 /* Copy a single argument in the request to/from userspace buffer */
1038 static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size)
1039 {
1040         while (size) {
1041                 if (!cs->len) {
1042                         int err = fuse_copy_fill(cs);
1043                         if (err)
1044                                 return err;
1045                 }
1046                 fuse_copy_do(cs, &val, &size);
1047         }
1048         return 0;
1049 }
1050
1051 /* Copy request arguments to/from userspace buffer */
1052 static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs,
1053                           unsigned argpages, struct fuse_arg *args,
1054                           int zeroing)
1055 {
1056         int err = 0;
1057         unsigned i;
1058
1059         for (i = 0; !err && i < numargs; i++)  {
1060                 struct fuse_arg *arg = &args[i];
1061                 if (i == numargs - 1 && argpages)
1062                         err = fuse_copy_pages(cs, arg->size, zeroing);
1063                 else
1064                         err = fuse_copy_one(cs, arg->value, arg->size);
1065         }
1066         return err;
1067 }
1068
1069 static int forget_pending(struct fuse_iqueue *fiq)
1070 {
1071         return fiq->forget_list_head.next != NULL;
1072 }
1073
1074 static int request_pending(struct fuse_iqueue *fiq)
1075 {
1076         return !list_empty(&fiq->pending) || !list_empty(&fiq->interrupts) ||
1077                 forget_pending(fiq);
1078 }
1079
1080 /*
1081  * Transfer an interrupt request to userspace
1082  *
1083  * Unlike other requests this is assembled on demand, without a need
1084  * to allocate a separate fuse_req structure.
1085  *
1086  * Called with fiq->waitq.lock held, releases it
1087  */
1088 static int fuse_read_interrupt(struct fuse_iqueue *fiq,
1089                                struct fuse_copy_state *cs,
1090                                size_t nbytes, struct fuse_req *req)
1091 __releases(fiq->waitq.lock)
1092 {
1093         struct fuse_in_header ih;
1094         struct fuse_interrupt_in arg;
1095         unsigned reqsize = sizeof(ih) + sizeof(arg);
1096         int err;
1097
1098         list_del_init(&req->intr_entry);
1099         req->intr_unique = fuse_get_unique(fiq);
1100         memset(&ih, 0, sizeof(ih));
1101         memset(&arg, 0, sizeof(arg));
1102         ih.len = reqsize;
1103         ih.opcode = FUSE_INTERRUPT;
1104         ih.unique = req->intr_unique;
1105         arg.unique = req->in.h.unique;
1106
1107         spin_unlock(&fiq->waitq.lock);
1108         if (nbytes < reqsize)
1109                 return -EINVAL;
1110
1111         err = fuse_copy_one(cs, &ih, sizeof(ih));
1112         if (!err)
1113                 err = fuse_copy_one(cs, &arg, sizeof(arg));
1114         fuse_copy_finish(cs);
1115
1116         return err ? err : reqsize;
1117 }
1118
1119 static struct fuse_forget_link *dequeue_forget(struct fuse_iqueue *fiq,
1120                                                unsigned max,
1121                                                unsigned *countp)
1122 {
1123         struct fuse_forget_link *head = fiq->forget_list_head.next;
1124         struct fuse_forget_link **newhead = &head;
1125         unsigned count;
1126
1127         for (count = 0; *newhead != NULL && count < max; count++)
1128                 newhead = &(*newhead)->next;
1129
1130         fiq->forget_list_head.next = *newhead;
1131         *newhead = NULL;
1132         if (fiq->forget_list_head.next == NULL)
1133                 fiq->forget_list_tail = &fiq->forget_list_head;
1134
1135         if (countp != NULL)
1136                 *countp = count;
1137
1138         return head;
1139 }
1140
1141 static int fuse_read_single_forget(struct fuse_iqueue *fiq,
1142                                    struct fuse_copy_state *cs,
1143                                    size_t nbytes)
1144 __releases(fiq->waitq.lock)
1145 {
1146         int err;
1147         struct fuse_forget_link *forget = dequeue_forget(fiq, 1, NULL);
1148         struct fuse_forget_in arg = {
1149                 .nlookup = forget->forget_one.nlookup,
1150         };
1151         struct fuse_in_header ih = {
1152                 .opcode = FUSE_FORGET,
1153                 .nodeid = forget->forget_one.nodeid,
1154                 .unique = fuse_get_unique(fiq),
1155                 .len = sizeof(ih) + sizeof(arg),
1156         };
1157
1158         spin_unlock(&fiq->waitq.lock);
1159         kfree(forget);
1160         if (nbytes < ih.len)
1161                 return -EINVAL;
1162
1163         err = fuse_copy_one(cs, &ih, sizeof(ih));
1164         if (!err)
1165                 err = fuse_copy_one(cs, &arg, sizeof(arg));
1166         fuse_copy_finish(cs);
1167
1168         if (err)
1169                 return err;
1170
1171         return ih.len;
1172 }
1173
1174 static int fuse_read_batch_forget(struct fuse_iqueue *fiq,
1175                                    struct fuse_copy_state *cs, size_t nbytes)
1176 __releases(fiq->waitq.lock)
1177 {
1178         int err;
1179         unsigned max_forgets;
1180         unsigned count;
1181         struct fuse_forget_link *head;
1182         struct fuse_batch_forget_in arg = { .count = 0 };
1183         struct fuse_in_header ih = {
1184                 .opcode = FUSE_BATCH_FORGET,
1185                 .unique = fuse_get_unique(fiq),
1186                 .len = sizeof(ih) + sizeof(arg),
1187         };
1188
1189         if (nbytes < ih.len) {
1190                 spin_unlock(&fiq->waitq.lock);
1191                 return -EINVAL;
1192         }
1193
1194         max_forgets = (nbytes - ih.len) / sizeof(struct fuse_forget_one);
1195         head = dequeue_forget(fiq, max_forgets, &count);
1196         spin_unlock(&fiq->waitq.lock);
1197
1198         arg.count = count;
1199         ih.len += count * sizeof(struct fuse_forget_one);
1200         err = fuse_copy_one(cs, &ih, sizeof(ih));
1201         if (!err)
1202                 err = fuse_copy_one(cs, &arg, sizeof(arg));
1203
1204         while (head) {
1205                 struct fuse_forget_link *forget = head;
1206
1207                 if (!err) {
1208                         err = fuse_copy_one(cs, &forget->forget_one,
1209                                             sizeof(forget->forget_one));
1210                 }
1211                 head = forget->next;
1212                 kfree(forget);
1213         }
1214
1215         fuse_copy_finish(cs);
1216
1217         if (err)
1218                 return err;
1219
1220         return ih.len;
1221 }
1222
1223 static int fuse_read_forget(struct fuse_conn *fc, struct fuse_iqueue *fiq,
1224                             struct fuse_copy_state *cs,
1225                             size_t nbytes)
1226 __releases(fiq->waitq.lock)
1227 {
1228         if (fc->minor < 16 || fiq->forget_list_head.next->next == NULL)
1229                 return fuse_read_single_forget(fiq, cs, nbytes);
1230         else
1231                 return fuse_read_batch_forget(fiq, cs, nbytes);
1232 }
1233
1234 /*
1235  * Read a single request into the userspace filesystem's buffer.  This
1236  * function waits until a request is available, then removes it from
1237  * the pending list and copies request data to userspace buffer.  If
1238  * no reply is needed (FORGET) or request has been aborted or there
1239  * was an error during the copying then it's finished by calling
1240  * request_end().  Otherwise add it to the processing list, and set
1241  * the 'sent' flag.
1242  */
1243 static ssize_t fuse_dev_do_read(struct fuse_dev *fud, struct file *file,
1244                                 struct fuse_copy_state *cs, size_t nbytes)
1245 {
1246         ssize_t err;
1247         struct fuse_conn *fc = fud->fc;
1248         struct fuse_iqueue *fiq = &fc->iq;
1249         struct fuse_pqueue *fpq = &fud->pq;
1250         struct fuse_req *req;
1251         struct fuse_in *in;
1252         unsigned reqsize;
1253
1254  restart:
1255         spin_lock(&fiq->waitq.lock);
1256         err = -EAGAIN;
1257         if ((file->f_flags & O_NONBLOCK) && fiq->connected &&
1258             !request_pending(fiq))
1259                 goto err_unlock;
1260
1261         err = wait_event_interruptible_exclusive_locked(fiq->waitq,
1262                                 !fiq->connected || request_pending(fiq));
1263         if (err)
1264                 goto err_unlock;
1265
1266         err = -ENODEV;
1267         if (!fiq->connected)
1268                 goto err_unlock;
1269
1270         if (!list_empty(&fiq->interrupts)) {
1271                 req = list_entry(fiq->interrupts.next, struct fuse_req,
1272                                  intr_entry);
1273                 return fuse_read_interrupt(fiq, cs, nbytes, req);
1274         }
1275
1276         if (forget_pending(fiq)) {
1277                 if (list_empty(&fiq->pending) || fiq->forget_batch-- > 0)
1278                         return fuse_read_forget(fc, fiq, cs, nbytes);
1279
1280                 if (fiq->forget_batch <= -8)
1281                         fiq->forget_batch = 16;
1282         }
1283
1284         req = list_entry(fiq->pending.next, struct fuse_req, list);
1285         clear_bit(FR_PENDING, &req->flags);
1286         list_del_init(&req->list);
1287         spin_unlock(&fiq->waitq.lock);
1288
1289         in = &req->in;
1290         reqsize = in->h.len;
1291         /* If request is too large, reply with an error and restart the read */
1292         if (nbytes < reqsize) {
1293                 req->out.h.error = -EIO;
1294                 /* SETXATTR is special, since it may contain too large data */
1295                 if (in->h.opcode == FUSE_SETXATTR)
1296                         req->out.h.error = -E2BIG;
1297                 request_end(fc, req);
1298                 goto restart;
1299         }
1300         spin_lock(&fpq->lock);
1301         list_add(&req->list, &fpq->io);
1302         spin_unlock(&fpq->lock);
1303         cs->req = req;
1304         err = fuse_copy_one(cs, &in->h, sizeof(in->h));
1305         if (!err)
1306                 err = fuse_copy_args(cs, in->numargs, in->argpages,
1307                                      (struct fuse_arg *) in->args, 0);
1308         fuse_copy_finish(cs);
1309         spin_lock(&fpq->lock);
1310         clear_bit(FR_LOCKED, &req->flags);
1311         if (!fpq->connected) {
1312                 err = -ENODEV;
1313                 goto out_end;
1314         }
1315         if (err) {
1316                 req->out.h.error = -EIO;
1317                 goto out_end;
1318         }
1319         if (!test_bit(FR_ISREPLY, &req->flags)) {
1320                 err = reqsize;
1321                 goto out_end;
1322         }
1323         list_move_tail(&req->list, &fpq->processing);
1324         spin_unlock(&fpq->lock);
1325         set_bit(FR_SENT, &req->flags);
1326         /* matches barrier in request_wait_answer() */
1327         smp_mb__after_atomic();
1328         if (test_bit(FR_INTERRUPTED, &req->flags))
1329                 queue_interrupt(fiq, req);
1330
1331         return reqsize;
1332
1333 out_end:
1334         if (!test_bit(FR_PRIVATE, &req->flags))
1335                 list_del_init(&req->list);
1336         spin_unlock(&fpq->lock);
1337         request_end(fc, req);
1338         return err;
1339
1340  err_unlock:
1341         spin_unlock(&fiq->waitq.lock);
1342         return err;
1343 }
1344
1345 static int fuse_dev_open(struct inode *inode, struct file *file)
1346 {
1347         /*
1348          * The fuse device's file's private_data is used to hold
1349          * the fuse_conn(ection) when it is mounted, and is used to
1350          * keep track of whether the file has been mounted already.
1351          */
1352         file->private_data = NULL;
1353         return 0;
1354 }
1355
1356 static ssize_t fuse_dev_read(struct kiocb *iocb, struct iov_iter *to)
1357 {
1358         struct fuse_copy_state cs;
1359         struct file *file = iocb->ki_filp;
1360         struct fuse_dev *fud = fuse_get_dev(file);
1361
1362         if (!fud)
1363                 return -EPERM;
1364
1365         if (!iter_is_iovec(to))
1366                 return -EINVAL;
1367
1368         fuse_copy_init(&cs, 1, to);
1369
1370         return fuse_dev_do_read(fud, file, &cs, iov_iter_count(to));
1371 }
1372
1373 static ssize_t fuse_dev_splice_read(struct file *in, loff_t *ppos,
1374                                     struct pipe_inode_info *pipe,
1375                                     size_t len, unsigned int flags)
1376 {
1377         int ret;
1378         int page_nr = 0;
1379         int do_wakeup = 0;
1380         struct pipe_buffer *bufs;
1381         struct fuse_copy_state cs;
1382         struct fuse_dev *fud = fuse_get_dev(in);
1383
1384         if (!fud)
1385                 return -EPERM;
1386
1387         bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
1388         if (!bufs)
1389                 return -ENOMEM;
1390
1391         fuse_copy_init(&cs, 1, NULL);
1392         cs.pipebufs = bufs;
1393         cs.pipe = pipe;
1394         ret = fuse_dev_do_read(fud, in, &cs, len);
1395         if (ret < 0)
1396                 goto out;
1397
1398         ret = 0;
1399         pipe_lock(pipe);
1400
1401         if (!pipe->readers) {
1402                 send_sig(SIGPIPE, current, 0);
1403                 if (!ret)
1404                         ret = -EPIPE;
1405                 goto out_unlock;
1406         }
1407
1408         if (pipe->nrbufs + cs.nr_segs > pipe->buffers) {
1409                 ret = -EIO;
1410                 goto out_unlock;
1411         }
1412
1413         while (page_nr < cs.nr_segs) {
1414                 int newbuf = (pipe->curbuf + pipe->nrbufs) & (pipe->buffers - 1);
1415                 struct pipe_buffer *buf = pipe->bufs + newbuf;
1416
1417                 buf->page = bufs[page_nr].page;
1418                 buf->offset = bufs[page_nr].offset;
1419                 buf->len = bufs[page_nr].len;
1420                 /*
1421                  * Need to be careful about this.  Having buf->ops in module
1422                  * code can Oops if the buffer persists after module unload.
1423                  */
1424                 buf->ops = &nosteal_pipe_buf_ops;
1425
1426                 pipe->nrbufs++;
1427                 page_nr++;
1428                 ret += buf->len;
1429
1430                 if (pipe->files)
1431                         do_wakeup = 1;
1432         }
1433
1434 out_unlock:
1435         pipe_unlock(pipe);
1436
1437         if (do_wakeup) {
1438                 smp_mb();
1439                 if (waitqueue_active(&pipe->wait))
1440                         wake_up_interruptible(&pipe->wait);
1441                 kill_fasync(&pipe->fasync_readers, SIGIO, POLL_IN);
1442         }
1443
1444 out:
1445         for (; page_nr < cs.nr_segs; page_nr++)
1446                 page_cache_release(bufs[page_nr].page);
1447
1448         kfree(bufs);
1449         return ret;
1450 }
1451
1452 static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size,
1453                             struct fuse_copy_state *cs)
1454 {
1455         struct fuse_notify_poll_wakeup_out outarg;
1456         int err = -EINVAL;
1457
1458         if (size != sizeof(outarg))
1459                 goto err;
1460
1461         err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1462         if (err)
1463                 goto err;
1464
1465         fuse_copy_finish(cs);
1466         return fuse_notify_poll_wakeup(fc, &outarg);
1467
1468 err:
1469         fuse_copy_finish(cs);
1470         return err;
1471 }
1472
1473 static int fuse_notify_inval_inode(struct fuse_conn *fc, unsigned int size,
1474                                    struct fuse_copy_state *cs)
1475 {
1476         struct fuse_notify_inval_inode_out outarg;
1477         int err = -EINVAL;
1478
1479         if (size != sizeof(outarg))
1480                 goto err;
1481
1482         err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1483         if (err)
1484                 goto err;
1485         fuse_copy_finish(cs);
1486
1487         down_read(&fc->killsb);
1488         err = -ENOENT;
1489         if (fc->sb) {
1490                 err = fuse_reverse_inval_inode(fc->sb, outarg.ino,
1491                                                outarg.off, outarg.len);
1492         }
1493         up_read(&fc->killsb);
1494         return err;
1495
1496 err:
1497         fuse_copy_finish(cs);
1498         return err;
1499 }
1500
1501 static int fuse_notify_inval_entry(struct fuse_conn *fc, unsigned int size,
1502                                    struct fuse_copy_state *cs)
1503 {
1504         struct fuse_notify_inval_entry_out outarg;
1505         int err = -ENOMEM;
1506         char *buf;
1507         struct qstr name;
1508
1509         buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1510         if (!buf)
1511                 goto err;
1512
1513         err = -EINVAL;
1514         if (size < sizeof(outarg))
1515                 goto err;
1516
1517         err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1518         if (err)
1519                 goto err;
1520
1521         err = -ENAMETOOLONG;
1522         if (outarg.namelen > FUSE_NAME_MAX)
1523                 goto err;
1524
1525         err = -EINVAL;
1526         if (size != sizeof(outarg) + outarg.namelen + 1)
1527                 goto err;
1528
1529         name.name = buf;
1530         name.len = outarg.namelen;
1531         err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1532         if (err)
1533                 goto err;
1534         fuse_copy_finish(cs);
1535         buf[outarg.namelen] = 0;
1536         name.hash = full_name_hash(name.name, name.len);
1537
1538         down_read(&fc->killsb);
1539         err = -ENOENT;
1540         if (fc->sb)
1541                 err = fuse_reverse_inval_entry(fc->sb, outarg.parent, 0, &name);
1542         up_read(&fc->killsb);
1543         kfree(buf);
1544         return err;
1545
1546 err:
1547         kfree(buf);
1548         fuse_copy_finish(cs);
1549         return err;
1550 }
1551
1552 static int fuse_notify_delete(struct fuse_conn *fc, unsigned int size,
1553                               struct fuse_copy_state *cs)
1554 {
1555         struct fuse_notify_delete_out outarg;
1556         int err = -ENOMEM;
1557         char *buf;
1558         struct qstr name;
1559
1560         buf = kzalloc(FUSE_NAME_MAX + 1, GFP_KERNEL);
1561         if (!buf)
1562                 goto err;
1563
1564         err = -EINVAL;
1565         if (size < sizeof(outarg))
1566                 goto err;
1567
1568         err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1569         if (err)
1570                 goto err;
1571
1572         err = -ENAMETOOLONG;
1573         if (outarg.namelen > FUSE_NAME_MAX)
1574                 goto err;
1575
1576         err = -EINVAL;
1577         if (size != sizeof(outarg) + outarg.namelen + 1)
1578                 goto err;
1579
1580         name.name = buf;
1581         name.len = outarg.namelen;
1582         err = fuse_copy_one(cs, buf, outarg.namelen + 1);
1583         if (err)
1584                 goto err;
1585         fuse_copy_finish(cs);
1586         buf[outarg.namelen] = 0;
1587         name.hash = full_name_hash(name.name, name.len);
1588
1589         down_read(&fc->killsb);
1590         err = -ENOENT;
1591         if (fc->sb)
1592                 err = fuse_reverse_inval_entry(fc->sb, outarg.parent,
1593                                                outarg.child, &name);
1594         up_read(&fc->killsb);
1595         kfree(buf);
1596         return err;
1597
1598 err:
1599         kfree(buf);
1600         fuse_copy_finish(cs);
1601         return err;
1602 }
1603
1604 static int fuse_notify_store(struct fuse_conn *fc, unsigned int size,
1605                              struct fuse_copy_state *cs)
1606 {
1607         struct fuse_notify_store_out outarg;
1608         struct inode *inode;
1609         struct address_space *mapping;
1610         u64 nodeid;
1611         int err;
1612         pgoff_t index;
1613         unsigned int offset;
1614         unsigned int num;
1615         loff_t file_size;
1616         loff_t end;
1617
1618         err = -EINVAL;
1619         if (size < sizeof(outarg))
1620                 goto out_finish;
1621
1622         err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1623         if (err)
1624                 goto out_finish;
1625
1626         err = -EINVAL;
1627         if (size - sizeof(outarg) != outarg.size)
1628                 goto out_finish;
1629
1630         nodeid = outarg.nodeid;
1631
1632         down_read(&fc->killsb);
1633
1634         err = -ENOENT;
1635         if (!fc->sb)
1636                 goto out_up_killsb;
1637
1638         inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1639         if (!inode)
1640                 goto out_up_killsb;
1641
1642         mapping = inode->i_mapping;
1643         index = outarg.offset >> PAGE_CACHE_SHIFT;
1644         offset = outarg.offset & ~PAGE_CACHE_MASK;
1645         file_size = i_size_read(inode);
1646         end = outarg.offset + outarg.size;
1647         if (end > file_size) {
1648                 file_size = end;
1649                 fuse_write_update_size(inode, file_size);
1650         }
1651
1652         num = outarg.size;
1653         while (num) {
1654                 struct page *page;
1655                 unsigned int this_num;
1656
1657                 err = -ENOMEM;
1658                 page = find_or_create_page(mapping, index,
1659                                            mapping_gfp_mask(mapping));
1660                 if (!page)
1661                         goto out_iput;
1662
1663                 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1664                 err = fuse_copy_page(cs, &page, offset, this_num, 0);
1665                 if (!err && offset == 0 &&
1666                     (this_num == PAGE_CACHE_SIZE || file_size == end))
1667                         SetPageUptodate(page);
1668                 unlock_page(page);
1669                 page_cache_release(page);
1670
1671                 if (err)
1672                         goto out_iput;
1673
1674                 num -= this_num;
1675                 offset = 0;
1676                 index++;
1677         }
1678
1679         err = 0;
1680
1681 out_iput:
1682         iput(inode);
1683 out_up_killsb:
1684         up_read(&fc->killsb);
1685 out_finish:
1686         fuse_copy_finish(cs);
1687         return err;
1688 }
1689
1690 static void fuse_retrieve_end(struct fuse_conn *fc, struct fuse_req *req)
1691 {
1692         release_pages(req->pages, req->num_pages, false);
1693 }
1694
1695 static int fuse_retrieve(struct fuse_conn *fc, struct inode *inode,
1696                          struct fuse_notify_retrieve_out *outarg)
1697 {
1698         int err;
1699         struct address_space *mapping = inode->i_mapping;
1700         struct fuse_req *req;
1701         pgoff_t index;
1702         loff_t file_size;
1703         unsigned int num;
1704         unsigned int offset;
1705         size_t total_len = 0;
1706         int num_pages;
1707
1708         offset = outarg->offset & ~PAGE_CACHE_MASK;
1709         file_size = i_size_read(inode);
1710
1711         num = outarg->size;
1712         if (outarg->offset > file_size)
1713                 num = 0;
1714         else if (outarg->offset + num > file_size)
1715                 num = file_size - outarg->offset;
1716
1717         num_pages = (num + offset + PAGE_SIZE - 1) >> PAGE_SHIFT;
1718         num_pages = min(num_pages, FUSE_MAX_PAGES_PER_REQ);
1719
1720         req = fuse_get_req(fc, num_pages);
1721         if (IS_ERR(req))
1722                 return PTR_ERR(req);
1723
1724         req->in.h.opcode = FUSE_NOTIFY_REPLY;
1725         req->in.h.nodeid = outarg->nodeid;
1726         req->in.numargs = 2;
1727         req->in.argpages = 1;
1728         req->page_descs[0].offset = offset;
1729         req->end = fuse_retrieve_end;
1730
1731         index = outarg->offset >> PAGE_CACHE_SHIFT;
1732
1733         while (num && req->num_pages < num_pages) {
1734                 struct page *page;
1735                 unsigned int this_num;
1736
1737                 page = find_get_page(mapping, index);
1738                 if (!page)
1739                         break;
1740
1741                 this_num = min_t(unsigned, num, PAGE_CACHE_SIZE - offset);
1742                 req->pages[req->num_pages] = page;
1743                 req->page_descs[req->num_pages].length = this_num;
1744                 req->num_pages++;
1745
1746                 offset = 0;
1747                 num -= this_num;
1748                 total_len += this_num;
1749                 index++;
1750         }
1751         req->misc.retrieve_in.offset = outarg->offset;
1752         req->misc.retrieve_in.size = total_len;
1753         req->in.args[0].size = sizeof(req->misc.retrieve_in);
1754         req->in.args[0].value = &req->misc.retrieve_in;
1755         req->in.args[1].size = total_len;
1756
1757         err = fuse_request_send_notify_reply(fc, req, outarg->notify_unique);
1758         if (err)
1759                 fuse_retrieve_end(fc, req);
1760
1761         return err;
1762 }
1763
1764 static int fuse_notify_retrieve(struct fuse_conn *fc, unsigned int size,
1765                                 struct fuse_copy_state *cs)
1766 {
1767         struct fuse_notify_retrieve_out outarg;
1768         struct inode *inode;
1769         int err;
1770
1771         err = -EINVAL;
1772         if (size != sizeof(outarg))
1773                 goto copy_finish;
1774
1775         err = fuse_copy_one(cs, &outarg, sizeof(outarg));
1776         if (err)
1777                 goto copy_finish;
1778
1779         fuse_copy_finish(cs);
1780
1781         down_read(&fc->killsb);
1782         err = -ENOENT;
1783         if (fc->sb) {
1784                 u64 nodeid = outarg.nodeid;
1785
1786                 inode = ilookup5(fc->sb, nodeid, fuse_inode_eq, &nodeid);
1787                 if (inode) {
1788                         err = fuse_retrieve(fc, inode, &outarg);
1789                         iput(inode);
1790                 }
1791         }
1792         up_read(&fc->killsb);
1793
1794         return err;
1795
1796 copy_finish:
1797         fuse_copy_finish(cs);
1798         return err;
1799 }
1800
1801 static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code,
1802                        unsigned int size, struct fuse_copy_state *cs)
1803 {
1804         /* Don't try to move pages (yet) */
1805         cs->move_pages = 0;
1806
1807         switch (code) {
1808         case FUSE_NOTIFY_POLL:
1809                 return fuse_notify_poll(fc, size, cs);
1810
1811         case FUSE_NOTIFY_INVAL_INODE:
1812                 return fuse_notify_inval_inode(fc, size, cs);
1813
1814         case FUSE_NOTIFY_INVAL_ENTRY:
1815                 return fuse_notify_inval_entry(fc, size, cs);
1816
1817         case FUSE_NOTIFY_STORE:
1818                 return fuse_notify_store(fc, size, cs);
1819
1820         case FUSE_NOTIFY_RETRIEVE:
1821                 return fuse_notify_retrieve(fc, size, cs);
1822
1823         case FUSE_NOTIFY_DELETE:
1824                 return fuse_notify_delete(fc, size, cs);
1825
1826         default:
1827                 fuse_copy_finish(cs);
1828                 return -EINVAL;
1829         }
1830 }
1831
1832 /* Look up request on processing list by unique ID */
1833 static struct fuse_req *request_find(struct fuse_pqueue *fpq, u64 unique)
1834 {
1835         struct fuse_req *req;
1836
1837         list_for_each_entry(req, &fpq->processing, list) {
1838                 if (req->in.h.unique == unique || req->intr_unique == unique)
1839                         return req;
1840         }
1841         return NULL;
1842 }
1843
1844 static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out,
1845                          unsigned nbytes)
1846 {
1847         unsigned reqsize = sizeof(struct fuse_out_header);
1848
1849         if (out->h.error)
1850                 return nbytes != reqsize ? -EINVAL : 0;
1851
1852         reqsize += len_args(out->numargs, out->args);
1853
1854         if (reqsize < nbytes || (reqsize > nbytes && !out->argvar))
1855                 return -EINVAL;
1856         else if (reqsize > nbytes) {
1857                 struct fuse_arg *lastarg = &out->args[out->numargs-1];
1858                 unsigned diffsize = reqsize - nbytes;
1859                 if (diffsize > lastarg->size)
1860                         return -EINVAL;
1861                 lastarg->size -= diffsize;
1862         }
1863         return fuse_copy_args(cs, out->numargs, out->argpages, out->args,
1864                               out->page_zeroing);
1865 }
1866
1867 /*
1868  * Write a single reply to a request.  First the header is copied from
1869  * the write buffer.  The request is then searched on the processing
1870  * list by the unique ID found in the header.  If found, then remove
1871  * it from the list and copy the rest of the buffer to the request.
1872  * The request is finished by calling request_end()
1873  */
1874 static ssize_t fuse_dev_do_write(struct fuse_dev *fud,
1875                                  struct fuse_copy_state *cs, size_t nbytes)
1876 {
1877         int err;
1878         struct fuse_conn *fc = fud->fc;
1879         struct fuse_pqueue *fpq = &fud->pq;
1880         struct fuse_req *req;
1881         struct fuse_out_header oh;
1882
1883         if (nbytes < sizeof(struct fuse_out_header))
1884                 return -EINVAL;
1885
1886         err = fuse_copy_one(cs, &oh, sizeof(oh));
1887         if (err)
1888                 goto err_finish;
1889
1890         err = -EINVAL;
1891         if (oh.len != nbytes)
1892                 goto err_finish;
1893
1894         /*
1895          * Zero oh.unique indicates unsolicited notification message
1896          * and error contains notification code.
1897          */
1898         if (!oh.unique) {
1899                 err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), cs);
1900                 return err ? err : nbytes;
1901         }
1902
1903         err = -EINVAL;
1904         if (oh.error <= -1000 || oh.error > 0)
1905                 goto err_finish;
1906
1907         spin_lock(&fpq->lock);
1908         err = -ENOENT;
1909         if (!fpq->connected)
1910                 goto err_unlock_pq;
1911
1912         req = request_find(fpq, oh.unique);
1913         if (!req)
1914                 goto err_unlock_pq;
1915
1916         /* Is it an interrupt reply? */
1917         if (req->intr_unique == oh.unique) {
1918                 spin_unlock(&fpq->lock);
1919
1920                 err = -EINVAL;
1921                 if (nbytes != sizeof(struct fuse_out_header))
1922                         goto err_finish;
1923
1924                 if (oh.error == -ENOSYS)
1925                         fc->no_interrupt = 1;
1926                 else if (oh.error == -EAGAIN)
1927                         queue_interrupt(&fc->iq, req);
1928
1929                 fuse_copy_finish(cs);
1930                 return nbytes;
1931         }
1932
1933         clear_bit(FR_SENT, &req->flags);
1934         list_move(&req->list, &fpq->io);
1935         req->out.h = oh;
1936         set_bit(FR_LOCKED, &req->flags);
1937         spin_unlock(&fpq->lock);
1938         cs->req = req;
1939         if (!req->out.page_replace)
1940                 cs->move_pages = 0;
1941
1942         err = copy_out_args(cs, &req->out, nbytes);
1943         if (req->in.h.opcode == FUSE_CANONICAL_PATH) {
1944                 req->out.h.error = kern_path((char *)req->out.args[0].value, 0,
1945                                                         req->canonical_path);
1946         }
1947         fuse_copy_finish(cs);
1948
1949         spin_lock(&fpq->lock);
1950         clear_bit(FR_LOCKED, &req->flags);
1951         if (!fpq->connected)
1952                 err = -ENOENT;
1953         else if (err)
1954                 req->out.h.error = -EIO;
1955         if (!test_bit(FR_PRIVATE, &req->flags))
1956                 list_del_init(&req->list);
1957         spin_unlock(&fpq->lock);
1958
1959         request_end(fc, req);
1960
1961         return err ? err : nbytes;
1962
1963  err_unlock_pq:
1964         spin_unlock(&fpq->lock);
1965  err_finish:
1966         fuse_copy_finish(cs);
1967         return err;
1968 }
1969
1970 static ssize_t fuse_dev_write(struct kiocb *iocb, struct iov_iter *from)
1971 {
1972         struct fuse_copy_state cs;
1973         struct fuse_dev *fud = fuse_get_dev(iocb->ki_filp);
1974
1975         if (!fud)
1976                 return -EPERM;
1977
1978         if (!iter_is_iovec(from))
1979                 return -EINVAL;
1980
1981         fuse_copy_init(&cs, 0, from);
1982
1983         return fuse_dev_do_write(fud, &cs, iov_iter_count(from));
1984 }
1985
1986 static ssize_t fuse_dev_splice_write(struct pipe_inode_info *pipe,
1987                                      struct file *out, loff_t *ppos,
1988                                      size_t len, unsigned int flags)
1989 {
1990         unsigned nbuf;
1991         unsigned idx;
1992         struct pipe_buffer *bufs;
1993         struct fuse_copy_state cs;
1994         struct fuse_dev *fud;
1995         size_t rem;
1996         ssize_t ret;
1997
1998         fud = fuse_get_dev(out);
1999         if (!fud)
2000                 return -EPERM;
2001
2002         bufs = kmalloc(pipe->buffers * sizeof(struct pipe_buffer), GFP_KERNEL);
2003         if (!bufs)
2004                 return -ENOMEM;
2005
2006         pipe_lock(pipe);
2007         nbuf = 0;
2008         rem = 0;
2009         for (idx = 0; idx < pipe->nrbufs && rem < len; idx++)
2010                 rem += pipe->bufs[(pipe->curbuf + idx) & (pipe->buffers - 1)].len;
2011
2012         ret = -EINVAL;
2013         if (rem < len) {
2014                 pipe_unlock(pipe);
2015                 goto out;
2016         }
2017
2018         rem = len;
2019         while (rem) {
2020                 struct pipe_buffer *ibuf;
2021                 struct pipe_buffer *obuf;
2022
2023                 BUG_ON(nbuf >= pipe->buffers);
2024                 BUG_ON(!pipe->nrbufs);
2025                 ibuf = &pipe->bufs[pipe->curbuf];
2026                 obuf = &bufs[nbuf];
2027
2028                 if (rem >= ibuf->len) {
2029                         *obuf = *ibuf;
2030                         ibuf->ops = NULL;
2031                         pipe->curbuf = (pipe->curbuf + 1) & (pipe->buffers - 1);
2032                         pipe->nrbufs--;
2033                 } else {
2034                         ibuf->ops->get(pipe, ibuf);
2035                         *obuf = *ibuf;
2036                         obuf->flags &= ~PIPE_BUF_FLAG_GIFT;
2037                         obuf->len = rem;
2038                         ibuf->offset += obuf->len;
2039                         ibuf->len -= obuf->len;
2040                 }
2041                 nbuf++;
2042                 rem -= obuf->len;
2043         }
2044         pipe_unlock(pipe);
2045
2046         fuse_copy_init(&cs, 0, NULL);
2047         cs.pipebufs = bufs;
2048         cs.nr_segs = nbuf;
2049         cs.pipe = pipe;
2050
2051         if (flags & SPLICE_F_MOVE)
2052                 cs.move_pages = 1;
2053
2054         ret = fuse_dev_do_write(fud, &cs, len);
2055
2056         for (idx = 0; idx < nbuf; idx++) {
2057                 struct pipe_buffer *buf = &bufs[idx];
2058                 buf->ops->release(pipe, buf);
2059         }
2060 out:
2061         kfree(bufs);
2062         return ret;
2063 }
2064
2065 static unsigned fuse_dev_poll(struct file *file, poll_table *wait)
2066 {
2067         unsigned mask = POLLOUT | POLLWRNORM;
2068         struct fuse_iqueue *fiq;
2069         struct fuse_dev *fud = fuse_get_dev(file);
2070
2071         if (!fud)
2072                 return POLLERR;
2073
2074         fiq = &fud->fc->iq;
2075         poll_wait(file, &fiq->waitq, wait);
2076
2077         spin_lock(&fiq->waitq.lock);
2078         if (!fiq->connected)
2079                 mask = POLLERR;
2080         else if (request_pending(fiq))
2081                 mask |= POLLIN | POLLRDNORM;
2082         spin_unlock(&fiq->waitq.lock);
2083
2084         return mask;
2085 }
2086
2087 /*
2088  * Abort all requests on the given list (pending or processing)
2089  *
2090  * This function releases and reacquires fc->lock
2091  */
2092 static void end_requests(struct fuse_conn *fc, struct list_head *head)
2093 {
2094         while (!list_empty(head)) {
2095                 struct fuse_req *req;
2096                 req = list_entry(head->next, struct fuse_req, list);
2097                 req->out.h.error = -ECONNABORTED;
2098                 clear_bit(FR_SENT, &req->flags);
2099                 list_del_init(&req->list);
2100                 request_end(fc, req);
2101         }
2102 }
2103
2104 static void end_polls(struct fuse_conn *fc)
2105 {
2106         struct rb_node *p;
2107
2108         p = rb_first(&fc->polled_files);
2109
2110         while (p) {
2111                 struct fuse_file *ff;
2112                 ff = rb_entry(p, struct fuse_file, polled_node);
2113                 wake_up_interruptible_all(&ff->poll_wait);
2114
2115                 p = rb_next(p);
2116         }
2117 }
2118
2119 /*
2120  * Abort all requests.
2121  *
2122  * Emergency exit in case of a malicious or accidental deadlock, or just a hung
2123  * filesystem.
2124  *
2125  * The same effect is usually achievable through killing the filesystem daemon
2126  * and all users of the filesystem.  The exception is the combination of an
2127  * asynchronous request and the tricky deadlock (see
2128  * Documentation/filesystems/fuse.txt).
2129  *
2130  * Aborting requests under I/O goes as follows: 1: Separate out unlocked
2131  * requests, they should be finished off immediately.  Locked requests will be
2132  * finished after unlock; see unlock_request(). 2: Finish off the unlocked
2133  * requests.  It is possible that some request will finish before we can.  This
2134  * is OK, the request will in that case be removed from the list before we touch
2135  * it.
2136  */
2137 void fuse_abort_conn(struct fuse_conn *fc)
2138 {
2139         struct fuse_iqueue *fiq = &fc->iq;
2140
2141         spin_lock(&fc->lock);
2142         if (fc->connected) {
2143                 struct fuse_dev *fud;
2144                 struct fuse_req *req, *next;
2145                 LIST_HEAD(to_end1);
2146                 LIST_HEAD(to_end2);
2147
2148                 fc->connected = 0;
2149                 fc->blocked = 0;
2150                 fuse_set_initialized(fc);
2151                 list_for_each_entry(fud, &fc->devices, entry) {
2152                         struct fuse_pqueue *fpq = &fud->pq;
2153
2154                         spin_lock(&fpq->lock);
2155                         fpq->connected = 0;
2156                         list_for_each_entry_safe(req, next, &fpq->io, list) {
2157                                 req->out.h.error = -ECONNABORTED;
2158                                 spin_lock(&req->waitq.lock);
2159                                 set_bit(FR_ABORTED, &req->flags);
2160                                 if (!test_bit(FR_LOCKED, &req->flags)) {
2161                                         set_bit(FR_PRIVATE, &req->flags);
2162                                         list_move(&req->list, &to_end1);
2163                                 }
2164                                 spin_unlock(&req->waitq.lock);
2165                         }
2166                         list_splice_init(&fpq->processing, &to_end2);
2167                         spin_unlock(&fpq->lock);
2168                 }
2169                 fc->max_background = UINT_MAX;
2170                 flush_bg_queue(fc);
2171
2172                 spin_lock(&fiq->waitq.lock);
2173                 fiq->connected = 0;
2174                 list_splice_init(&fiq->pending, &to_end2);
2175                 list_for_each_entry(req, &to_end2, list)
2176                         clear_bit(FR_PENDING, &req->flags);
2177                 while (forget_pending(fiq))
2178                         kfree(dequeue_forget(fiq, 1, NULL));
2179                 wake_up_all_locked(&fiq->waitq);
2180                 spin_unlock(&fiq->waitq.lock);
2181                 kill_fasync(&fiq->fasync, SIGIO, POLL_IN);
2182                 end_polls(fc);
2183                 wake_up_all(&fc->blocked_waitq);
2184                 spin_unlock(&fc->lock);
2185
2186                 while (!list_empty(&to_end1)) {
2187                         req = list_first_entry(&to_end1, struct fuse_req, list);
2188                         __fuse_get_request(req);
2189                         list_del_init(&req->list);
2190                         request_end(fc, req);
2191                 }
2192                 end_requests(fc, &to_end2);
2193         } else {
2194                 spin_unlock(&fc->lock);
2195         }
2196 }
2197 EXPORT_SYMBOL_GPL(fuse_abort_conn);
2198
2199 int fuse_dev_release(struct inode *inode, struct file *file)
2200 {
2201         struct fuse_dev *fud = fuse_get_dev(file);
2202
2203         if (fud) {
2204                 struct fuse_conn *fc = fud->fc;
2205                 struct fuse_pqueue *fpq = &fud->pq;
2206
2207                 WARN_ON(!list_empty(&fpq->io));
2208                 end_requests(fc, &fpq->processing);
2209                 /* Are we the last open device? */
2210                 if (atomic_dec_and_test(&fc->dev_count)) {
2211                         WARN_ON(fc->iq.fasync != NULL);
2212                         fuse_abort_conn(fc);
2213                 }
2214                 fuse_dev_free(fud);
2215         }
2216         return 0;
2217 }
2218 EXPORT_SYMBOL_GPL(fuse_dev_release);
2219
2220 static int fuse_dev_fasync(int fd, struct file *file, int on)
2221 {
2222         struct fuse_dev *fud = fuse_get_dev(file);
2223
2224         if (!fud)
2225                 return -EPERM;
2226
2227         /* No locking - fasync_helper does its own locking */
2228         return fasync_helper(fd, file, on, &fud->fc->iq.fasync);
2229 }
2230
2231 static int fuse_device_clone(struct fuse_conn *fc, struct file *new)
2232 {
2233         struct fuse_dev *fud;
2234
2235         if (new->private_data)
2236                 return -EINVAL;
2237
2238         fud = fuse_dev_alloc(fc);
2239         if (!fud)
2240                 return -ENOMEM;
2241
2242         new->private_data = fud;
2243         atomic_inc(&fc->dev_count);
2244
2245         return 0;
2246 }
2247
2248 static long fuse_dev_ioctl(struct file *file, unsigned int cmd,
2249                            unsigned long arg)
2250 {
2251         int err = -ENOTTY;
2252
2253         if (cmd == FUSE_DEV_IOC_CLONE) {
2254                 int oldfd;
2255
2256                 err = -EFAULT;
2257                 if (!get_user(oldfd, (__u32 __user *) arg)) {
2258                         struct file *old = fget(oldfd);
2259
2260                         err = -EINVAL;
2261                         if (old) {
2262                                 struct fuse_dev *fud = NULL;
2263
2264                                 /*
2265                                  * Check against file->f_op because CUSE
2266                                  * uses the same ioctl handler.
2267                                  */
2268                                 if (old->f_op == file->f_op &&
2269                                     old->f_cred->user_ns == file->f_cred->user_ns)
2270                                         fud = fuse_get_dev(old);
2271
2272                                 if (fud) {
2273                                         mutex_lock(&fuse_mutex);
2274                                         err = fuse_device_clone(fud->fc, file);
2275                                         mutex_unlock(&fuse_mutex);
2276                                 }
2277                                 fput(old);
2278                         }
2279                 }
2280         }
2281         return err;
2282 }
2283
2284 const struct file_operations fuse_dev_operations = {
2285         .owner          = THIS_MODULE,
2286         .open           = fuse_dev_open,
2287         .llseek         = no_llseek,
2288         .read_iter      = fuse_dev_read,
2289         .splice_read    = fuse_dev_splice_read,
2290         .write_iter     = fuse_dev_write,
2291         .splice_write   = fuse_dev_splice_write,
2292         .poll           = fuse_dev_poll,
2293         .release        = fuse_dev_release,
2294         .fasync         = fuse_dev_fasync,
2295         .unlocked_ioctl = fuse_dev_ioctl,
2296         .compat_ioctl   = fuse_dev_ioctl,
2297 };
2298 EXPORT_SYMBOL_GPL(fuse_dev_operations);
2299
2300 static struct miscdevice fuse_miscdevice = {
2301         .minor = FUSE_MINOR,
2302         .name  = "fuse",
2303         .fops = &fuse_dev_operations,
2304 };
2305
2306 int __init fuse_dev_init(void)
2307 {
2308         int err = -ENOMEM;
2309         fuse_req_cachep = kmem_cache_create("fuse_request",
2310                                             sizeof(struct fuse_req),
2311                                             0, 0, NULL);
2312         if (!fuse_req_cachep)
2313                 goto out;
2314
2315         err = misc_register(&fuse_miscdevice);
2316         if (err)
2317                 goto out_cache_clean;
2318
2319         return 0;
2320
2321  out_cache_clean:
2322         kmem_cache_destroy(fuse_req_cachep);
2323  out:
2324         return err;
2325 }
2326
2327 void fuse_dev_cleanup(void)
2328 {
2329         misc_deregister(&fuse_miscdevice);
2330         kmem_cache_destroy(fuse_req_cachep);
2331 }