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