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