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