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