~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

TOMOYO Linux Cross Reference
Linux/fs/fuse/dev.c

Version: ~ [ linux-5.15-rc5 ] ~ [ linux-5.14.11 ] ~ [ linux-5.13.19 ] ~ [ linux-5.12.19 ] ~ [ linux-5.11.22 ] ~ [ linux-5.10.72 ] ~ [ linux-5.9.16 ] ~ [ linux-5.8.18 ] ~ [ linux-5.7.19 ] ~ [ linux-5.6.19 ] ~ [ linux-5.5.19 ] ~ [ linux-5.4.152 ] ~ [ linux-5.3.18 ] ~ [ linux-5.2.21 ] ~ [ linux-5.1.21 ] ~ [ linux-5.0.21 ] ~ [ linux-4.20.17 ] ~ [ linux-4.19.210 ] ~ [ linux-4.18.20 ] ~ [ linux-4.17.19 ] ~ [ linux-4.16.18 ] ~ [ linux-4.15.18 ] ~ [ linux-4.14.250 ] ~ [ linux-4.13.16 ] ~ [ linux-4.12.14 ] ~ [ linux-4.11.12 ] ~ [ linux-4.10.17 ] ~ [ linux-4.9.286 ] ~ [ linux-4.8.17 ] ~ [ linux-4.7.10 ] ~ [ linux-4.6.7 ] ~ [ linux-4.5.7 ] ~ [ linux-4.4.288 ] ~ [ linux-4.3.6 ] ~ [ linux-4.2.8 ] ~ [ linux-4.1.52 ] ~ [ linux-4.0.9 ] ~ [ linux-3.18.140 ] ~ [ linux-3.16.85 ] ~ [ linux-3.14.79 ] ~ [ linux-3.12.74 ] ~ [ linux-3.10.108 ] ~ [ linux-2.6.32.71 ] ~ [ linux-2.6.0 ] ~ [ linux-2.4.37.11 ] ~ [ unix-v6-master ] ~ [ ccs-tools-1.8.5 ] ~ [ policy-sample ] ~
Architecture: ~ [ i386 ] ~ [ alpha ] ~ [ m68k ] ~ [ mips ] ~ [ ppc ] ~ [ sparc ] ~ [ sparc64 ] ~

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

~ [ source navigation ] ~ [ diff markup ] ~ [ identifier search ] ~

kernel.org | git.kernel.org | LWN.net | Project Home | Wiki (Japanese) | Wiki (English) | SVN repository | Mail admin

Linux® is a registered trademark of Linus Torvalds in the United States and other countries.
TOMOYO® is a registered trademark of NTT DATA CORPORATION.

osdn.jp