Commit | Line | Data |
---|---|---|
334f485d MS |
1 | /* |
2 | FUSE: Filesystem in Userspace | |
1729a16c | 3 | Copyright (C) 2001-2008 Miklos Szeredi <miklos@szeredi.hu> |
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> | |
14 | #include <linux/uio.h> | |
15 | #include <linux/miscdevice.h> | |
16 | #include <linux/pagemap.h> | |
17 | #include <linux/file.h> | |
18 | #include <linux/slab.h> | |
19 | ||
20 | MODULE_ALIAS_MISCDEV(FUSE_MINOR); | |
21 | ||
e18b890b | 22 | static struct kmem_cache *fuse_req_cachep; |
334f485d | 23 | |
8bfc016d | 24 | static struct fuse_conn *fuse_get_conn(struct file *file) |
334f485d | 25 | { |
0720b315 MS |
26 | /* |
27 | * Lockless access is OK, because file->private data is set | |
28 | * once during mount and is valid until the file is released. | |
29 | */ | |
30 | return file->private_data; | |
334f485d MS |
31 | } |
32 | ||
8bfc016d | 33 | static void fuse_request_init(struct fuse_req *req) |
334f485d MS |
34 | { |
35 | memset(req, 0, sizeof(*req)); | |
36 | INIT_LIST_HEAD(&req->list); | |
a4d27e75 | 37 | INIT_LIST_HEAD(&req->intr_entry); |
334f485d MS |
38 | init_waitqueue_head(&req->waitq); |
39 | atomic_set(&req->count, 1); | |
40 | } | |
41 | ||
42 | struct fuse_req *fuse_request_alloc(void) | |
43 | { | |
e94b1766 | 44 | struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, GFP_KERNEL); |
334f485d MS |
45 | if (req) |
46 | fuse_request_init(req); | |
47 | return req; | |
48 | } | |
49 | ||
3be5a52b MS |
50 | struct fuse_req *fuse_request_alloc_nofs(void) |
51 | { | |
52 | struct fuse_req *req = kmem_cache_alloc(fuse_req_cachep, GFP_NOFS); | |
53 | if (req) | |
54 | fuse_request_init(req); | |
55 | return req; | |
56 | } | |
57 | ||
334f485d MS |
58 | void fuse_request_free(struct fuse_req *req) |
59 | { | |
60 | kmem_cache_free(fuse_req_cachep, req); | |
61 | } | |
62 | ||
8bfc016d | 63 | static void block_sigs(sigset_t *oldset) |
334f485d MS |
64 | { |
65 | sigset_t mask; | |
66 | ||
67 | siginitsetinv(&mask, sigmask(SIGKILL)); | |
68 | sigprocmask(SIG_BLOCK, &mask, oldset); | |
69 | } | |
70 | ||
8bfc016d | 71 | static void restore_sigs(sigset_t *oldset) |
334f485d MS |
72 | { |
73 | sigprocmask(SIG_SETMASK, oldset, NULL); | |
74 | } | |
75 | ||
334f485d MS |
76 | static void __fuse_get_request(struct fuse_req *req) |
77 | { | |
78 | atomic_inc(&req->count); | |
79 | } | |
80 | ||
81 | /* Must be called with > 1 refcount */ | |
82 | static void __fuse_put_request(struct fuse_req *req) | |
83 | { | |
84 | BUG_ON(atomic_read(&req->count) < 2); | |
85 | atomic_dec(&req->count); | |
86 | } | |
87 | ||
33649c91 MS |
88 | static void fuse_req_init_context(struct fuse_req *req) |
89 | { | |
2186a71c DH |
90 | req->in.h.uid = current_fsuid(); |
91 | req->in.h.gid = current_fsgid(); | |
33649c91 MS |
92 | req->in.h.pid = current->pid; |
93 | } | |
94 | ||
ce1d5a49 | 95 | struct fuse_req *fuse_get_req(struct fuse_conn *fc) |
334f485d | 96 | { |
08a53cdc MS |
97 | struct fuse_req *req; |
98 | sigset_t oldset; | |
9bc5ddda | 99 | int intr; |
08a53cdc MS |
100 | int err; |
101 | ||
9bc5ddda | 102 | atomic_inc(&fc->num_waiting); |
08a53cdc | 103 | block_sigs(&oldset); |
9bc5ddda | 104 | intr = wait_event_interruptible(fc->blocked_waitq, !fc->blocked); |
08a53cdc | 105 | restore_sigs(&oldset); |
9bc5ddda MS |
106 | err = -EINTR; |
107 | if (intr) | |
108 | goto out; | |
08a53cdc | 109 | |
51eb01e7 MS |
110 | err = -ENOTCONN; |
111 | if (!fc->connected) | |
112 | goto out; | |
113 | ||
08a53cdc | 114 | req = fuse_request_alloc(); |
9bc5ddda | 115 | err = -ENOMEM; |
ce1d5a49 | 116 | if (!req) |
9bc5ddda | 117 | goto out; |
334f485d | 118 | |
33649c91 | 119 | fuse_req_init_context(req); |
9bc5ddda | 120 | req->waiting = 1; |
334f485d | 121 | return req; |
9bc5ddda MS |
122 | |
123 | out: | |
124 | atomic_dec(&fc->num_waiting); | |
125 | return ERR_PTR(err); | |
334f485d MS |
126 | } |
127 | ||
33649c91 MS |
128 | /* |
129 | * Return request in fuse_file->reserved_req. However that may | |
130 | * currently be in use. If that is the case, wait for it to become | |
131 | * available. | |
132 | */ | |
133 | static struct fuse_req *get_reserved_req(struct fuse_conn *fc, | |
134 | struct file *file) | |
135 | { | |
136 | struct fuse_req *req = NULL; | |
137 | struct fuse_file *ff = file->private_data; | |
138 | ||
139 | do { | |
de5e3dec | 140 | wait_event(fc->reserved_req_waitq, ff->reserved_req); |
33649c91 MS |
141 | spin_lock(&fc->lock); |
142 | if (ff->reserved_req) { | |
143 | req = ff->reserved_req; | |
144 | ff->reserved_req = NULL; | |
145 | get_file(file); | |
146 | req->stolen_file = file; | |
147 | } | |
148 | spin_unlock(&fc->lock); | |
149 | } while (!req); | |
150 | ||
151 | return req; | |
152 | } | |
153 | ||
154 | /* | |
155 | * Put stolen request back into fuse_file->reserved_req | |
156 | */ | |
157 | static void put_reserved_req(struct fuse_conn *fc, struct fuse_req *req) | |
158 | { | |
159 | struct file *file = req->stolen_file; | |
160 | struct fuse_file *ff = file->private_data; | |
161 | ||
162 | spin_lock(&fc->lock); | |
163 | fuse_request_init(req); | |
164 | BUG_ON(ff->reserved_req); | |
165 | ff->reserved_req = req; | |
de5e3dec | 166 | wake_up_all(&fc->reserved_req_waitq); |
33649c91 MS |
167 | spin_unlock(&fc->lock); |
168 | fput(file); | |
169 | } | |
170 | ||
171 | /* | |
172 | * Gets a requests for a file operation, always succeeds | |
173 | * | |
174 | * This is used for sending the FLUSH request, which must get to | |
175 | * userspace, due to POSIX locks which may need to be unlocked. | |
176 | * | |
177 | * If allocation fails due to OOM, use the reserved request in | |
178 | * fuse_file. | |
179 | * | |
180 | * This is very unlikely to deadlock accidentally, since the | |
181 | * filesystem should not have it's own file open. If deadlock is | |
182 | * intentional, it can still be broken by "aborting" the filesystem. | |
183 | */ | |
184 | struct fuse_req *fuse_get_req_nofail(struct fuse_conn *fc, struct file *file) | |
185 | { | |
186 | struct fuse_req *req; | |
187 | ||
188 | atomic_inc(&fc->num_waiting); | |
189 | wait_event(fc->blocked_waitq, !fc->blocked); | |
190 | req = fuse_request_alloc(); | |
191 | if (!req) | |
192 | req = get_reserved_req(fc, file); | |
193 | ||
194 | fuse_req_init_context(req); | |
195 | req->waiting = 1; | |
196 | return req; | |
197 | } | |
198 | ||
334f485d | 199 | void fuse_put_request(struct fuse_conn *fc, struct fuse_req *req) |
7128ec2a MS |
200 | { |
201 | if (atomic_dec_and_test(&req->count)) { | |
9bc5ddda MS |
202 | if (req->waiting) |
203 | atomic_dec(&fc->num_waiting); | |
33649c91 MS |
204 | |
205 | if (req->stolen_file) | |
206 | put_reserved_req(fc, req); | |
207 | else | |
208 | fuse_request_free(req); | |
7128ec2a MS |
209 | } |
210 | } | |
211 | ||
d12def1b MS |
212 | static unsigned len_args(unsigned numargs, struct fuse_arg *args) |
213 | { | |
214 | unsigned nbytes = 0; | |
215 | unsigned i; | |
216 | ||
217 | for (i = 0; i < numargs; i++) | |
218 | nbytes += args[i].size; | |
219 | ||
220 | return nbytes; | |
221 | } | |
222 | ||
223 | static u64 fuse_get_unique(struct fuse_conn *fc) | |
224 | { | |
225 | fc->reqctr++; | |
226 | /* zero is special */ | |
227 | if (fc->reqctr == 0) | |
228 | fc->reqctr = 1; | |
229 | ||
230 | return fc->reqctr; | |
231 | } | |
232 | ||
233 | static void queue_request(struct fuse_conn *fc, struct fuse_req *req) | |
234 | { | |
235 | req->in.h.unique = fuse_get_unique(fc); | |
236 | req->in.h.len = sizeof(struct fuse_in_header) + | |
237 | len_args(req->in.numargs, (struct fuse_arg *) req->in.args); | |
238 | list_add_tail(&req->list, &fc->pending); | |
239 | req->state = FUSE_REQ_PENDING; | |
240 | if (!req->waiting) { | |
241 | req->waiting = 1; | |
242 | atomic_inc(&fc->num_waiting); | |
243 | } | |
244 | wake_up(&fc->waitq); | |
245 | kill_fasync(&fc->fasync, SIGIO, POLL_IN); | |
246 | } | |
247 | ||
248 | static void flush_bg_queue(struct fuse_conn *fc) | |
249 | { | |
250 | while (fc->active_background < FUSE_MAX_BACKGROUND && | |
251 | !list_empty(&fc->bg_queue)) { | |
252 | struct fuse_req *req; | |
253 | ||
254 | req = list_entry(fc->bg_queue.next, struct fuse_req, list); | |
255 | list_del(&req->list); | |
256 | fc->active_background++; | |
257 | queue_request(fc, req); | |
258 | } | |
259 | } | |
260 | ||
334f485d MS |
261 | /* |
262 | * This function is called when a request is finished. Either a reply | |
f9a2842e | 263 | * has arrived or it was aborted (and not yet sent) or some error |
f43b155a | 264 | * occurred during communication with userspace, or the device file |
51eb01e7 MS |
265 | * was closed. The requester thread is woken up (if still waiting), |
266 | * the 'end' callback is called if given, else the reference to the | |
267 | * request is released | |
7128ec2a | 268 | * |
d7133114 | 269 | * Called with fc->lock, unlocks it |
334f485d MS |
270 | */ |
271 | static void request_end(struct fuse_conn *fc, struct fuse_req *req) | |
5d9ec854 | 272 | __releases(&fc->lock) |
334f485d | 273 | { |
51eb01e7 MS |
274 | void (*end) (struct fuse_conn *, struct fuse_req *) = req->end; |
275 | req->end = NULL; | |
d77a1d5b | 276 | list_del(&req->list); |
a4d27e75 | 277 | list_del(&req->intr_entry); |
83cfd493 | 278 | req->state = FUSE_REQ_FINISHED; |
51eb01e7 MS |
279 | if (req->background) { |
280 | if (fc->num_background == FUSE_MAX_BACKGROUND) { | |
281 | fc->blocked = 0; | |
282 | wake_up_all(&fc->blocked_waitq); | |
283 | } | |
f92b99b9 MS |
284 | if (fc->num_background == FUSE_CONGESTION_THRESHOLD) { |
285 | clear_bdi_congested(&fc->bdi, READ); | |
286 | clear_bdi_congested(&fc->bdi, WRITE); | |
287 | } | |
51eb01e7 | 288 | fc->num_background--; |
d12def1b MS |
289 | fc->active_background--; |
290 | flush_bg_queue(fc); | |
334f485d | 291 | } |
51eb01e7 | 292 | spin_unlock(&fc->lock); |
51eb01e7 MS |
293 | wake_up(&req->waitq); |
294 | if (end) | |
295 | end(fc, req); | |
e9bb09dd | 296 | fuse_put_request(fc, req); |
334f485d MS |
297 | } |
298 | ||
a4d27e75 MS |
299 | static void wait_answer_interruptible(struct fuse_conn *fc, |
300 | struct fuse_req *req) | |
5d9ec854 HH |
301 | __releases(&fc->lock) |
302 | __acquires(&fc->lock) | |
a4d27e75 MS |
303 | { |
304 | if (signal_pending(current)) | |
305 | return; | |
306 | ||
307 | spin_unlock(&fc->lock); | |
308 | wait_event_interruptible(req->waitq, req->state == FUSE_REQ_FINISHED); | |
309 | spin_lock(&fc->lock); | |
310 | } | |
311 | ||
312 | static void queue_interrupt(struct fuse_conn *fc, struct fuse_req *req) | |
313 | { | |
314 | list_add_tail(&req->intr_entry, &fc->interrupts); | |
315 | wake_up(&fc->waitq); | |
316 | kill_fasync(&fc->fasync, SIGIO, POLL_IN); | |
317 | } | |
318 | ||
7c352bdf | 319 | static void request_wait_answer(struct fuse_conn *fc, struct fuse_req *req) |
5d9ec854 HH |
320 | __releases(&fc->lock) |
321 | __acquires(&fc->lock) | |
334f485d | 322 | { |
a4d27e75 MS |
323 | if (!fc->no_interrupt) { |
324 | /* Any signal may interrupt this */ | |
325 | wait_answer_interruptible(fc, req); | |
334f485d | 326 | |
a4d27e75 MS |
327 | if (req->aborted) |
328 | goto aborted; | |
329 | if (req->state == FUSE_REQ_FINISHED) | |
330 | return; | |
331 | ||
332 | req->interrupted = 1; | |
333 | if (req->state == FUSE_REQ_SENT) | |
334 | queue_interrupt(fc, req); | |
335 | } | |
336 | ||
a131de0a | 337 | if (!req->force) { |
a4d27e75 MS |
338 | sigset_t oldset; |
339 | ||
340 | /* Only fatal signals may interrupt this */ | |
51eb01e7 | 341 | block_sigs(&oldset); |
a4d27e75 | 342 | wait_answer_interruptible(fc, req); |
51eb01e7 | 343 | restore_sigs(&oldset); |
a131de0a MS |
344 | |
345 | if (req->aborted) | |
346 | goto aborted; | |
347 | if (req->state == FUSE_REQ_FINISHED) | |
348 | return; | |
349 | ||
350 | /* Request is not yet in userspace, bail out */ | |
351 | if (req->state == FUSE_REQ_PENDING) { | |
352 | list_del(&req->list); | |
353 | __fuse_put_request(req); | |
354 | req->out.h.error = -EINTR; | |
355 | return; | |
356 | } | |
51eb01e7 | 357 | } |
334f485d | 358 | |
a131de0a MS |
359 | /* |
360 | * Either request is already in userspace, or it was forced. | |
361 | * Wait it out. | |
362 | */ | |
363 | spin_unlock(&fc->lock); | |
364 | wait_event(req->waitq, req->state == FUSE_REQ_FINISHED); | |
365 | spin_lock(&fc->lock); | |
a4d27e75 | 366 | |
a131de0a MS |
367 | if (!req->aborted) |
368 | return; | |
a4d27e75 MS |
369 | |
370 | aborted: | |
a131de0a | 371 | BUG_ON(req->state != FUSE_REQ_FINISHED); |
334f485d MS |
372 | if (req->locked) { |
373 | /* This is uninterruptible sleep, because data is | |
374 | being copied to/from the buffers of req. During | |
375 | locked state, there mustn't be any filesystem | |
376 | operation (e.g. page fault), since that could lead | |
377 | to deadlock */ | |
d7133114 | 378 | spin_unlock(&fc->lock); |
334f485d | 379 | wait_event(req->waitq, !req->locked); |
d7133114 | 380 | spin_lock(&fc->lock); |
334f485d | 381 | } |
334f485d MS |
382 | } |
383 | ||
b93f858a | 384 | void fuse_request_send(struct fuse_conn *fc, struct fuse_req *req) |
334f485d MS |
385 | { |
386 | req->isreply = 1; | |
d7133114 | 387 | spin_lock(&fc->lock); |
1e9a4ed9 | 388 | if (!fc->connected) |
334f485d MS |
389 | req->out.h.error = -ENOTCONN; |
390 | else if (fc->conn_error) | |
391 | req->out.h.error = -ECONNREFUSED; | |
392 | else { | |
393 | queue_request(fc, req); | |
394 | /* acquire extra reference, since request is still needed | |
395 | after request_end() */ | |
396 | __fuse_get_request(req); | |
397 | ||
7c352bdf | 398 | request_wait_answer(fc, req); |
334f485d | 399 | } |
d7133114 | 400 | spin_unlock(&fc->lock); |
334f485d MS |
401 | } |
402 | ||
b93f858a TH |
403 | static void fuse_request_send_nowait_locked(struct fuse_conn *fc, |
404 | struct fuse_req *req) | |
d12def1b MS |
405 | { |
406 | req->background = 1; | |
407 | fc->num_background++; | |
408 | if (fc->num_background == FUSE_MAX_BACKGROUND) | |
409 | fc->blocked = 1; | |
410 | if (fc->num_background == FUSE_CONGESTION_THRESHOLD) { | |
411 | set_bdi_congested(&fc->bdi, READ); | |
412 | set_bdi_congested(&fc->bdi, WRITE); | |
413 | } | |
414 | list_add_tail(&req->list, &fc->bg_queue); | |
415 | flush_bg_queue(fc); | |
416 | } | |
417 | ||
b93f858a | 418 | static void fuse_request_send_nowait(struct fuse_conn *fc, struct fuse_req *req) |
334f485d | 419 | { |
d7133114 | 420 | spin_lock(&fc->lock); |
1e9a4ed9 | 421 | if (fc->connected) { |
b93f858a | 422 | fuse_request_send_nowait_locked(fc, req); |
d7133114 | 423 | spin_unlock(&fc->lock); |
334f485d MS |
424 | } else { |
425 | req->out.h.error = -ENOTCONN; | |
426 | request_end(fc, req); | |
427 | } | |
428 | } | |
429 | ||
b93f858a | 430 | void fuse_request_send_noreply(struct fuse_conn *fc, struct fuse_req *req) |
334f485d MS |
431 | { |
432 | req->isreply = 0; | |
b93f858a | 433 | fuse_request_send_nowait(fc, req); |
334f485d MS |
434 | } |
435 | ||
b93f858a | 436 | void fuse_request_send_background(struct fuse_conn *fc, struct fuse_req *req) |
334f485d MS |
437 | { |
438 | req->isreply = 1; | |
b93f858a | 439 | fuse_request_send_nowait(fc, req); |
334f485d MS |
440 | } |
441 | ||
3be5a52b MS |
442 | /* |
443 | * Called under fc->lock | |
444 | * | |
445 | * fc->connected must have been checked previously | |
446 | */ | |
b93f858a TH |
447 | void fuse_request_send_background_locked(struct fuse_conn *fc, |
448 | struct fuse_req *req) | |
3be5a52b MS |
449 | { |
450 | req->isreply = 1; | |
b93f858a | 451 | fuse_request_send_nowait_locked(fc, req); |
3be5a52b MS |
452 | } |
453 | ||
334f485d MS |
454 | /* |
455 | * Lock the request. Up to the next unlock_request() there mustn't be | |
456 | * anything that could cause a page-fault. If the request was already | |
f9a2842e | 457 | * aborted bail out. |
334f485d | 458 | */ |
d7133114 | 459 | static int lock_request(struct fuse_conn *fc, struct fuse_req *req) |
334f485d MS |
460 | { |
461 | int err = 0; | |
462 | if (req) { | |
d7133114 | 463 | spin_lock(&fc->lock); |
f9a2842e | 464 | if (req->aborted) |
334f485d MS |
465 | err = -ENOENT; |
466 | else | |
467 | req->locked = 1; | |
d7133114 | 468 | spin_unlock(&fc->lock); |
334f485d MS |
469 | } |
470 | return err; | |
471 | } | |
472 | ||
473 | /* | |
f9a2842e | 474 | * Unlock request. If it was aborted during being locked, the |
334f485d MS |
475 | * requester thread is currently waiting for it to be unlocked, so |
476 | * wake it up. | |
477 | */ | |
d7133114 | 478 | static void unlock_request(struct fuse_conn *fc, struct fuse_req *req) |
334f485d MS |
479 | { |
480 | if (req) { | |
d7133114 | 481 | spin_lock(&fc->lock); |
334f485d | 482 | req->locked = 0; |
f9a2842e | 483 | if (req->aborted) |
334f485d | 484 | wake_up(&req->waitq); |
d7133114 | 485 | spin_unlock(&fc->lock); |
334f485d MS |
486 | } |
487 | } | |
488 | ||
489 | struct fuse_copy_state { | |
d7133114 | 490 | struct fuse_conn *fc; |
334f485d MS |
491 | int write; |
492 | struct fuse_req *req; | |
493 | const struct iovec *iov; | |
494 | unsigned long nr_segs; | |
495 | unsigned long seglen; | |
496 | unsigned long addr; | |
497 | struct page *pg; | |
498 | void *mapaddr; | |
499 | void *buf; | |
500 | unsigned len; | |
501 | }; | |
502 | ||
d7133114 MS |
503 | static void fuse_copy_init(struct fuse_copy_state *cs, struct fuse_conn *fc, |
504 | int write, struct fuse_req *req, | |
505 | const struct iovec *iov, unsigned long nr_segs) | |
334f485d MS |
506 | { |
507 | memset(cs, 0, sizeof(*cs)); | |
d7133114 | 508 | cs->fc = fc; |
334f485d MS |
509 | cs->write = write; |
510 | cs->req = req; | |
511 | cs->iov = iov; | |
512 | cs->nr_segs = nr_segs; | |
513 | } | |
514 | ||
515 | /* Unmap and put previous page of userspace buffer */ | |
8bfc016d | 516 | static void fuse_copy_finish(struct fuse_copy_state *cs) |
334f485d MS |
517 | { |
518 | if (cs->mapaddr) { | |
519 | kunmap_atomic(cs->mapaddr, KM_USER0); | |
520 | if (cs->write) { | |
521 | flush_dcache_page(cs->pg); | |
522 | set_page_dirty_lock(cs->pg); | |
523 | } | |
524 | put_page(cs->pg); | |
525 | cs->mapaddr = NULL; | |
526 | } | |
527 | } | |
528 | ||
529 | /* | |
530 | * Get another pagefull of userspace buffer, and map it to kernel | |
531 | * address space, and lock request | |
532 | */ | |
533 | static int fuse_copy_fill(struct fuse_copy_state *cs) | |
534 | { | |
535 | unsigned long offset; | |
536 | int err; | |
537 | ||
d7133114 | 538 | unlock_request(cs->fc, cs->req); |
334f485d MS |
539 | fuse_copy_finish(cs); |
540 | if (!cs->seglen) { | |
541 | BUG_ON(!cs->nr_segs); | |
542 | cs->seglen = cs->iov[0].iov_len; | |
543 | cs->addr = (unsigned long) cs->iov[0].iov_base; | |
1729a16c MS |
544 | cs->iov++; |
545 | cs->nr_segs--; | |
334f485d MS |
546 | } |
547 | down_read(¤t->mm->mmap_sem); | |
548 | err = get_user_pages(current, current->mm, cs->addr, 1, cs->write, 0, | |
549 | &cs->pg, NULL); | |
550 | up_read(¤t->mm->mmap_sem); | |
551 | if (err < 0) | |
552 | return err; | |
553 | BUG_ON(err != 1); | |
554 | offset = cs->addr % PAGE_SIZE; | |
555 | cs->mapaddr = kmap_atomic(cs->pg, KM_USER0); | |
556 | cs->buf = cs->mapaddr + offset; | |
557 | cs->len = min(PAGE_SIZE - offset, cs->seglen); | |
558 | cs->seglen -= cs->len; | |
559 | cs->addr += cs->len; | |
560 | ||
d7133114 | 561 | return lock_request(cs->fc, cs->req); |
334f485d MS |
562 | } |
563 | ||
564 | /* Do as much copy to/from userspace buffer as we can */ | |
8bfc016d | 565 | static int fuse_copy_do(struct fuse_copy_state *cs, void **val, unsigned *size) |
334f485d MS |
566 | { |
567 | unsigned ncpy = min(*size, cs->len); | |
568 | if (val) { | |
569 | if (cs->write) | |
570 | memcpy(cs->buf, *val, ncpy); | |
571 | else | |
572 | memcpy(*val, cs->buf, ncpy); | |
573 | *val += ncpy; | |
574 | } | |
575 | *size -= ncpy; | |
576 | cs->len -= ncpy; | |
577 | cs->buf += ncpy; | |
578 | return ncpy; | |
579 | } | |
580 | ||
581 | /* | |
582 | * Copy a page in the request to/from the userspace buffer. Must be | |
583 | * done atomically | |
584 | */ | |
8bfc016d MS |
585 | static int fuse_copy_page(struct fuse_copy_state *cs, struct page *page, |
586 | unsigned offset, unsigned count, int zeroing) | |
334f485d MS |
587 | { |
588 | if (page && zeroing && count < PAGE_SIZE) { | |
589 | void *mapaddr = kmap_atomic(page, KM_USER1); | |
590 | memset(mapaddr, 0, PAGE_SIZE); | |
591 | kunmap_atomic(mapaddr, KM_USER1); | |
592 | } | |
593 | while (count) { | |
1729a16c MS |
594 | if (!cs->len) { |
595 | int err = fuse_copy_fill(cs); | |
596 | if (err) | |
597 | return err; | |
598 | } | |
334f485d MS |
599 | if (page) { |
600 | void *mapaddr = kmap_atomic(page, KM_USER1); | |
601 | void *buf = mapaddr + offset; | |
602 | offset += fuse_copy_do(cs, &buf, &count); | |
603 | kunmap_atomic(mapaddr, KM_USER1); | |
604 | } else | |
605 | offset += fuse_copy_do(cs, NULL, &count); | |
606 | } | |
607 | if (page && !cs->write) | |
608 | flush_dcache_page(page); | |
609 | return 0; | |
610 | } | |
611 | ||
612 | /* Copy pages in the request to/from userspace buffer */ | |
613 | static int fuse_copy_pages(struct fuse_copy_state *cs, unsigned nbytes, | |
614 | int zeroing) | |
615 | { | |
616 | unsigned i; | |
617 | struct fuse_req *req = cs->req; | |
618 | unsigned offset = req->page_offset; | |
619 | unsigned count = min(nbytes, (unsigned) PAGE_SIZE - offset); | |
620 | ||
621 | for (i = 0; i < req->num_pages && (nbytes || zeroing); i++) { | |
622 | struct page *page = req->pages[i]; | |
623 | int err = fuse_copy_page(cs, page, offset, count, zeroing); | |
624 | if (err) | |
625 | return err; | |
626 | ||
627 | nbytes -= count; | |
628 | count = min(nbytes, (unsigned) PAGE_SIZE); | |
629 | offset = 0; | |
630 | } | |
631 | return 0; | |
632 | } | |
633 | ||
634 | /* Copy a single argument in the request to/from userspace buffer */ | |
635 | static int fuse_copy_one(struct fuse_copy_state *cs, void *val, unsigned size) | |
636 | { | |
637 | while (size) { | |
1729a16c MS |
638 | if (!cs->len) { |
639 | int err = fuse_copy_fill(cs); | |
640 | if (err) | |
641 | return err; | |
642 | } | |
334f485d MS |
643 | fuse_copy_do(cs, &val, &size); |
644 | } | |
645 | return 0; | |
646 | } | |
647 | ||
648 | /* Copy request arguments to/from userspace buffer */ | |
649 | static int fuse_copy_args(struct fuse_copy_state *cs, unsigned numargs, | |
650 | unsigned argpages, struct fuse_arg *args, | |
651 | int zeroing) | |
652 | { | |
653 | int err = 0; | |
654 | unsigned i; | |
655 | ||
656 | for (i = 0; !err && i < numargs; i++) { | |
657 | struct fuse_arg *arg = &args[i]; | |
658 | if (i == numargs - 1 && argpages) | |
659 | err = fuse_copy_pages(cs, arg->size, zeroing); | |
660 | else | |
661 | err = fuse_copy_one(cs, arg->value, arg->size); | |
662 | } | |
663 | return err; | |
664 | } | |
665 | ||
a4d27e75 MS |
666 | static int request_pending(struct fuse_conn *fc) |
667 | { | |
668 | return !list_empty(&fc->pending) || !list_empty(&fc->interrupts); | |
669 | } | |
670 | ||
334f485d MS |
671 | /* Wait until a request is available on the pending list */ |
672 | static void request_wait(struct fuse_conn *fc) | |
5d9ec854 HH |
673 | __releases(&fc->lock) |
674 | __acquires(&fc->lock) | |
334f485d MS |
675 | { |
676 | DECLARE_WAITQUEUE(wait, current); | |
677 | ||
678 | add_wait_queue_exclusive(&fc->waitq, &wait); | |
a4d27e75 | 679 | while (fc->connected && !request_pending(fc)) { |
334f485d MS |
680 | set_current_state(TASK_INTERRUPTIBLE); |
681 | if (signal_pending(current)) | |
682 | break; | |
683 | ||
d7133114 | 684 | spin_unlock(&fc->lock); |
334f485d | 685 | schedule(); |
d7133114 | 686 | spin_lock(&fc->lock); |
334f485d MS |
687 | } |
688 | set_current_state(TASK_RUNNING); | |
689 | remove_wait_queue(&fc->waitq, &wait); | |
690 | } | |
691 | ||
a4d27e75 MS |
692 | /* |
693 | * Transfer an interrupt request to userspace | |
694 | * | |
695 | * Unlike other requests this is assembled on demand, without a need | |
696 | * to allocate a separate fuse_req structure. | |
697 | * | |
698 | * Called with fc->lock held, releases it | |
699 | */ | |
700 | static int fuse_read_interrupt(struct fuse_conn *fc, struct fuse_req *req, | |
701 | const struct iovec *iov, unsigned long nr_segs) | |
5d9ec854 | 702 | __releases(&fc->lock) |
a4d27e75 MS |
703 | { |
704 | struct fuse_copy_state cs; | |
705 | struct fuse_in_header ih; | |
706 | struct fuse_interrupt_in arg; | |
707 | unsigned reqsize = sizeof(ih) + sizeof(arg); | |
708 | int err; | |
709 | ||
710 | list_del_init(&req->intr_entry); | |
711 | req->intr_unique = fuse_get_unique(fc); | |
712 | memset(&ih, 0, sizeof(ih)); | |
713 | memset(&arg, 0, sizeof(arg)); | |
714 | ih.len = reqsize; | |
715 | ih.opcode = FUSE_INTERRUPT; | |
716 | ih.unique = req->intr_unique; | |
717 | arg.unique = req->in.h.unique; | |
718 | ||
719 | spin_unlock(&fc->lock); | |
720 | if (iov_length(iov, nr_segs) < reqsize) | |
721 | return -EINVAL; | |
722 | ||
723 | fuse_copy_init(&cs, fc, 1, NULL, iov, nr_segs); | |
724 | err = fuse_copy_one(&cs, &ih, sizeof(ih)); | |
725 | if (!err) | |
726 | err = fuse_copy_one(&cs, &arg, sizeof(arg)); | |
727 | fuse_copy_finish(&cs); | |
728 | ||
729 | return err ? err : reqsize; | |
730 | } | |
731 | ||
334f485d MS |
732 | /* |
733 | * Read a single request into the userspace filesystem's buffer. This | |
734 | * function waits until a request is available, then removes it from | |
735 | * the pending list and copies request data to userspace buffer. If | |
f9a2842e MS |
736 | * no reply is needed (FORGET) or request has been aborted or there |
737 | * was an error during the copying then it's finished by calling | |
334f485d MS |
738 | * request_end(). Otherwise add it to the processing list, and set |
739 | * the 'sent' flag. | |
740 | */ | |
ee0b3e67 BP |
741 | static ssize_t fuse_dev_read(struct kiocb *iocb, const struct iovec *iov, |
742 | unsigned long nr_segs, loff_t pos) | |
334f485d MS |
743 | { |
744 | int err; | |
334f485d MS |
745 | struct fuse_req *req; |
746 | struct fuse_in *in; | |
747 | struct fuse_copy_state cs; | |
748 | unsigned reqsize; | |
ee0b3e67 | 749 | struct file *file = iocb->ki_filp; |
0720b315 MS |
750 | struct fuse_conn *fc = fuse_get_conn(file); |
751 | if (!fc) | |
752 | return -EPERM; | |
334f485d | 753 | |
1d3d752b | 754 | restart: |
d7133114 | 755 | spin_lock(&fc->lock); |
e5ac1d1e JD |
756 | err = -EAGAIN; |
757 | if ((file->f_flags & O_NONBLOCK) && fc->connected && | |
a4d27e75 | 758 | !request_pending(fc)) |
e5ac1d1e JD |
759 | goto err_unlock; |
760 | ||
334f485d MS |
761 | request_wait(fc); |
762 | err = -ENODEV; | |
9ba7cbba | 763 | if (!fc->connected) |
334f485d MS |
764 | goto err_unlock; |
765 | err = -ERESTARTSYS; | |
a4d27e75 | 766 | if (!request_pending(fc)) |
334f485d MS |
767 | goto err_unlock; |
768 | ||
a4d27e75 MS |
769 | if (!list_empty(&fc->interrupts)) { |
770 | req = list_entry(fc->interrupts.next, struct fuse_req, | |
771 | intr_entry); | |
772 | return fuse_read_interrupt(fc, req, iov, nr_segs); | |
773 | } | |
774 | ||
334f485d | 775 | req = list_entry(fc->pending.next, struct fuse_req, list); |
83cfd493 | 776 | req->state = FUSE_REQ_READING; |
d77a1d5b | 777 | list_move(&req->list, &fc->io); |
334f485d MS |
778 | |
779 | in = &req->in; | |
1d3d752b MS |
780 | reqsize = in->h.len; |
781 | /* If request is too large, reply with an error and restart the read */ | |
782 | if (iov_length(iov, nr_segs) < reqsize) { | |
783 | req->out.h.error = -EIO; | |
784 | /* SETXATTR is special, since it may contain too large data */ | |
785 | if (in->h.opcode == FUSE_SETXATTR) | |
786 | req->out.h.error = -E2BIG; | |
787 | request_end(fc, req); | |
788 | goto restart; | |
334f485d | 789 | } |
d7133114 MS |
790 | spin_unlock(&fc->lock); |
791 | fuse_copy_init(&cs, fc, 1, req, iov, nr_segs); | |
1d3d752b MS |
792 | err = fuse_copy_one(&cs, &in->h, sizeof(in->h)); |
793 | if (!err) | |
794 | err = fuse_copy_args(&cs, in->numargs, in->argpages, | |
795 | (struct fuse_arg *) in->args, 0); | |
334f485d | 796 | fuse_copy_finish(&cs); |
d7133114 | 797 | spin_lock(&fc->lock); |
334f485d | 798 | req->locked = 0; |
c9c9d7df MS |
799 | if (req->aborted) { |
800 | request_end(fc, req); | |
801 | return -ENODEV; | |
802 | } | |
334f485d | 803 | if (err) { |
c9c9d7df | 804 | req->out.h.error = -EIO; |
334f485d MS |
805 | request_end(fc, req); |
806 | return err; | |
807 | } | |
808 | if (!req->isreply) | |
809 | request_end(fc, req); | |
810 | else { | |
83cfd493 | 811 | req->state = FUSE_REQ_SENT; |
d77a1d5b | 812 | list_move_tail(&req->list, &fc->processing); |
a4d27e75 MS |
813 | if (req->interrupted) |
814 | queue_interrupt(fc, req); | |
d7133114 | 815 | spin_unlock(&fc->lock); |
334f485d MS |
816 | } |
817 | return reqsize; | |
818 | ||
819 | err_unlock: | |
d7133114 | 820 | spin_unlock(&fc->lock); |
334f485d MS |
821 | return err; |
822 | } | |
823 | ||
95668a69 TH |
824 | static int fuse_notify_poll(struct fuse_conn *fc, unsigned int size, |
825 | struct fuse_copy_state *cs) | |
826 | { | |
827 | struct fuse_notify_poll_wakeup_out outarg; | |
828 | int err; | |
829 | ||
830 | if (size != sizeof(outarg)) | |
831 | return -EINVAL; | |
832 | ||
833 | err = fuse_copy_one(cs, &outarg, sizeof(outarg)); | |
834 | if (err) | |
835 | return err; | |
836 | ||
837 | return fuse_notify_poll_wakeup(fc, &outarg); | |
838 | } | |
839 | ||
8599396b TH |
840 | static int fuse_notify(struct fuse_conn *fc, enum fuse_notify_code code, |
841 | unsigned int size, struct fuse_copy_state *cs) | |
842 | { | |
843 | switch (code) { | |
95668a69 TH |
844 | case FUSE_NOTIFY_POLL: |
845 | return fuse_notify_poll(fc, size, cs); | |
846 | ||
8599396b TH |
847 | default: |
848 | return -EINVAL; | |
849 | } | |
850 | } | |
851 | ||
334f485d MS |
852 | /* Look up request on processing list by unique ID */ |
853 | static struct fuse_req *request_find(struct fuse_conn *fc, u64 unique) | |
854 | { | |
855 | struct list_head *entry; | |
856 | ||
857 | list_for_each(entry, &fc->processing) { | |
858 | struct fuse_req *req; | |
859 | req = list_entry(entry, struct fuse_req, list); | |
a4d27e75 | 860 | if (req->in.h.unique == unique || req->intr_unique == unique) |
334f485d MS |
861 | return req; |
862 | } | |
863 | return NULL; | |
864 | } | |
865 | ||
866 | static int copy_out_args(struct fuse_copy_state *cs, struct fuse_out *out, | |
867 | unsigned nbytes) | |
868 | { | |
869 | unsigned reqsize = sizeof(struct fuse_out_header); | |
870 | ||
871 | if (out->h.error) | |
872 | return nbytes != reqsize ? -EINVAL : 0; | |
873 | ||
874 | reqsize += len_args(out->numargs, out->args); | |
875 | ||
876 | if (reqsize < nbytes || (reqsize > nbytes && !out->argvar)) | |
877 | return -EINVAL; | |
878 | else if (reqsize > nbytes) { | |
879 | struct fuse_arg *lastarg = &out->args[out->numargs-1]; | |
880 | unsigned diffsize = reqsize - nbytes; | |
881 | if (diffsize > lastarg->size) | |
882 | return -EINVAL; | |
883 | lastarg->size -= diffsize; | |
884 | } | |
885 | return fuse_copy_args(cs, out->numargs, out->argpages, out->args, | |
886 | out->page_zeroing); | |
887 | } | |
888 | ||
889 | /* | |
890 | * Write a single reply to a request. First the header is copied from | |
891 | * the write buffer. The request is then searched on the processing | |
892 | * list by the unique ID found in the header. If found, then remove | |
893 | * it from the list and copy the rest of the buffer to the request. | |
894 | * The request is finished by calling request_end() | |
895 | */ | |
ee0b3e67 BP |
896 | static ssize_t fuse_dev_write(struct kiocb *iocb, const struct iovec *iov, |
897 | unsigned long nr_segs, loff_t pos) | |
334f485d MS |
898 | { |
899 | int err; | |
900 | unsigned nbytes = iov_length(iov, nr_segs); | |
901 | struct fuse_req *req; | |
902 | struct fuse_out_header oh; | |
903 | struct fuse_copy_state cs; | |
ee0b3e67 | 904 | struct fuse_conn *fc = fuse_get_conn(iocb->ki_filp); |
334f485d | 905 | if (!fc) |
a87046d8 | 906 | return -EPERM; |
334f485d | 907 | |
d7133114 | 908 | fuse_copy_init(&cs, fc, 0, NULL, iov, nr_segs); |
334f485d MS |
909 | if (nbytes < sizeof(struct fuse_out_header)) |
910 | return -EINVAL; | |
911 | ||
912 | err = fuse_copy_one(&cs, &oh, sizeof(oh)); | |
913 | if (err) | |
914 | goto err_finish; | |
8599396b TH |
915 | |
916 | err = -EINVAL; | |
917 | if (oh.len != nbytes) | |
918 | goto err_finish; | |
919 | ||
920 | /* | |
921 | * Zero oh.unique indicates unsolicited notification message | |
922 | * and error contains notification code. | |
923 | */ | |
924 | if (!oh.unique) { | |
925 | err = fuse_notify(fc, oh.error, nbytes - sizeof(oh), &cs); | |
926 | fuse_copy_finish(&cs); | |
927 | return err ? err : nbytes; | |
928 | } | |
929 | ||
334f485d | 930 | err = -EINVAL; |
8599396b | 931 | if (oh.error <= -1000 || oh.error > 0) |
334f485d MS |
932 | goto err_finish; |
933 | ||
d7133114 | 934 | spin_lock(&fc->lock); |
69a53bf2 MS |
935 | err = -ENOENT; |
936 | if (!fc->connected) | |
937 | goto err_unlock; | |
938 | ||
334f485d | 939 | req = request_find(fc, oh.unique); |
334f485d MS |
940 | if (!req) |
941 | goto err_unlock; | |
942 | ||
f9a2842e | 943 | if (req->aborted) { |
d7133114 | 944 | spin_unlock(&fc->lock); |
334f485d | 945 | fuse_copy_finish(&cs); |
d7133114 | 946 | spin_lock(&fc->lock); |
222f1d69 | 947 | request_end(fc, req); |
334f485d MS |
948 | return -ENOENT; |
949 | } | |
a4d27e75 MS |
950 | /* Is it an interrupt reply? */ |
951 | if (req->intr_unique == oh.unique) { | |
952 | err = -EINVAL; | |
953 | if (nbytes != sizeof(struct fuse_out_header)) | |
954 | goto err_unlock; | |
955 | ||
956 | if (oh.error == -ENOSYS) | |
957 | fc->no_interrupt = 1; | |
958 | else if (oh.error == -EAGAIN) | |
959 | queue_interrupt(fc, req); | |
960 | ||
961 | spin_unlock(&fc->lock); | |
962 | fuse_copy_finish(&cs); | |
963 | return nbytes; | |
964 | } | |
965 | ||
966 | req->state = FUSE_REQ_WRITING; | |
d77a1d5b | 967 | list_move(&req->list, &fc->io); |
334f485d MS |
968 | req->out.h = oh; |
969 | req->locked = 1; | |
970 | cs.req = req; | |
d7133114 | 971 | spin_unlock(&fc->lock); |
334f485d MS |
972 | |
973 | err = copy_out_args(&cs, &req->out, nbytes); | |
974 | fuse_copy_finish(&cs); | |
975 | ||
d7133114 | 976 | spin_lock(&fc->lock); |
334f485d MS |
977 | req->locked = 0; |
978 | if (!err) { | |
f9a2842e | 979 | if (req->aborted) |
334f485d | 980 | err = -ENOENT; |
f9a2842e | 981 | } else if (!req->aborted) |
334f485d MS |
982 | req->out.h.error = -EIO; |
983 | request_end(fc, req); | |
984 | ||
985 | return err ? err : nbytes; | |
986 | ||
987 | err_unlock: | |
d7133114 | 988 | spin_unlock(&fc->lock); |
334f485d MS |
989 | err_finish: |
990 | fuse_copy_finish(&cs); | |
991 | return err; | |
992 | } | |
993 | ||
334f485d MS |
994 | static unsigned fuse_dev_poll(struct file *file, poll_table *wait) |
995 | { | |
334f485d | 996 | unsigned mask = POLLOUT | POLLWRNORM; |
7025d9ad | 997 | struct fuse_conn *fc = fuse_get_conn(file); |
334f485d | 998 | if (!fc) |
7025d9ad | 999 | return POLLERR; |
334f485d MS |
1000 | |
1001 | poll_wait(file, &fc->waitq, wait); | |
1002 | ||
d7133114 | 1003 | spin_lock(&fc->lock); |
7025d9ad MS |
1004 | if (!fc->connected) |
1005 | mask = POLLERR; | |
a4d27e75 | 1006 | else if (request_pending(fc)) |
7025d9ad | 1007 | mask |= POLLIN | POLLRDNORM; |
d7133114 | 1008 | spin_unlock(&fc->lock); |
334f485d MS |
1009 | |
1010 | return mask; | |
1011 | } | |
1012 | ||
69a53bf2 MS |
1013 | /* |
1014 | * Abort all requests on the given list (pending or processing) | |
1015 | * | |
d7133114 | 1016 | * This function releases and reacquires fc->lock |
69a53bf2 | 1017 | */ |
334f485d | 1018 | static void end_requests(struct fuse_conn *fc, struct list_head *head) |
5d9ec854 HH |
1019 | __releases(&fc->lock) |
1020 | __acquires(&fc->lock) | |
334f485d MS |
1021 | { |
1022 | while (!list_empty(head)) { | |
1023 | struct fuse_req *req; | |
1024 | req = list_entry(head->next, struct fuse_req, list); | |
334f485d MS |
1025 | req->out.h.error = -ECONNABORTED; |
1026 | request_end(fc, req); | |
d7133114 | 1027 | spin_lock(&fc->lock); |
334f485d MS |
1028 | } |
1029 | } | |
1030 | ||
69a53bf2 MS |
1031 | /* |
1032 | * Abort requests under I/O | |
1033 | * | |
f9a2842e | 1034 | * The requests are set to aborted and finished, and the request |
69a53bf2 MS |
1035 | * waiter is woken up. This will make request_wait_answer() wait |
1036 | * until the request is unlocked and then return. | |
64c6d8ed MS |
1037 | * |
1038 | * If the request is asynchronous, then the end function needs to be | |
1039 | * called after waiting for the request to be unlocked (if it was | |
1040 | * locked). | |
69a53bf2 MS |
1041 | */ |
1042 | static void end_io_requests(struct fuse_conn *fc) | |
5d9ec854 HH |
1043 | __releases(&fc->lock) |
1044 | __acquires(&fc->lock) | |
69a53bf2 MS |
1045 | { |
1046 | while (!list_empty(&fc->io)) { | |
64c6d8ed MS |
1047 | struct fuse_req *req = |
1048 | list_entry(fc->io.next, struct fuse_req, list); | |
1049 | void (*end) (struct fuse_conn *, struct fuse_req *) = req->end; | |
1050 | ||
f9a2842e | 1051 | req->aborted = 1; |
69a53bf2 MS |
1052 | req->out.h.error = -ECONNABORTED; |
1053 | req->state = FUSE_REQ_FINISHED; | |
1054 | list_del_init(&req->list); | |
1055 | wake_up(&req->waitq); | |
64c6d8ed MS |
1056 | if (end) { |
1057 | req->end = NULL; | |
64c6d8ed | 1058 | __fuse_get_request(req); |
d7133114 | 1059 | spin_unlock(&fc->lock); |
64c6d8ed MS |
1060 | wait_event(req->waitq, !req->locked); |
1061 | end(fc, req); | |
e9bb09dd | 1062 | fuse_put_request(fc, req); |
d7133114 | 1063 | spin_lock(&fc->lock); |
64c6d8ed | 1064 | } |
69a53bf2 MS |
1065 | } |
1066 | } | |
1067 | ||
1068 | /* | |
1069 | * Abort all requests. | |
1070 | * | |
1071 | * Emergency exit in case of a malicious or accidental deadlock, or | |
1072 | * just a hung filesystem. | |
1073 | * | |
1074 | * The same effect is usually achievable through killing the | |
1075 | * filesystem daemon and all users of the filesystem. The exception | |
1076 | * is the combination of an asynchronous request and the tricky | |
1077 | * deadlock (see Documentation/filesystems/fuse.txt). | |
1078 | * | |
1079 | * During the aborting, progression of requests from the pending and | |
1080 | * processing lists onto the io list, and progression of new requests | |
1081 | * onto the pending list is prevented by req->connected being false. | |
1082 | * | |
1083 | * Progression of requests under I/O to the processing list is | |
f9a2842e MS |
1084 | * prevented by the req->aborted flag being true for these requests. |
1085 | * For this reason requests on the io list must be aborted first. | |
69a53bf2 MS |
1086 | */ |
1087 | void fuse_abort_conn(struct fuse_conn *fc) | |
1088 | { | |
d7133114 | 1089 | spin_lock(&fc->lock); |
69a53bf2 MS |
1090 | if (fc->connected) { |
1091 | fc->connected = 0; | |
51eb01e7 | 1092 | fc->blocked = 0; |
69a53bf2 MS |
1093 | end_io_requests(fc); |
1094 | end_requests(fc, &fc->pending); | |
1095 | end_requests(fc, &fc->processing); | |
1096 | wake_up_all(&fc->waitq); | |
51eb01e7 | 1097 | wake_up_all(&fc->blocked_waitq); |
385a17bf | 1098 | kill_fasync(&fc->fasync, SIGIO, POLL_IN); |
69a53bf2 | 1099 | } |
d7133114 | 1100 | spin_unlock(&fc->lock); |
69a53bf2 MS |
1101 | } |
1102 | ||
334f485d MS |
1103 | static int fuse_dev_release(struct inode *inode, struct file *file) |
1104 | { | |
0720b315 | 1105 | struct fuse_conn *fc = fuse_get_conn(file); |
334f485d | 1106 | if (fc) { |
d7133114 | 1107 | spin_lock(&fc->lock); |
1e9a4ed9 | 1108 | fc->connected = 0; |
334f485d MS |
1109 | end_requests(fc, &fc->pending); |
1110 | end_requests(fc, &fc->processing); | |
d7133114 | 1111 | spin_unlock(&fc->lock); |
bafa9654 | 1112 | fuse_conn_put(fc); |
385a17bf | 1113 | } |
f543f253 | 1114 | |
334f485d MS |
1115 | return 0; |
1116 | } | |
1117 | ||
385a17bf JD |
1118 | static int fuse_dev_fasync(int fd, struct file *file, int on) |
1119 | { | |
1120 | struct fuse_conn *fc = fuse_get_conn(file); | |
1121 | if (!fc) | |
a87046d8 | 1122 | return -EPERM; |
385a17bf JD |
1123 | |
1124 | /* No locking - fasync_helper does its own locking */ | |
1125 | return fasync_helper(fd, file, on, &fc->fasync); | |
1126 | } | |
1127 | ||
4b6f5d20 | 1128 | const struct file_operations fuse_dev_operations = { |
334f485d MS |
1129 | .owner = THIS_MODULE, |
1130 | .llseek = no_llseek, | |
ee0b3e67 BP |
1131 | .read = do_sync_read, |
1132 | .aio_read = fuse_dev_read, | |
1133 | .write = do_sync_write, | |
1134 | .aio_write = fuse_dev_write, | |
334f485d MS |
1135 | .poll = fuse_dev_poll, |
1136 | .release = fuse_dev_release, | |
385a17bf | 1137 | .fasync = fuse_dev_fasync, |
334f485d MS |
1138 | }; |
1139 | ||
1140 | static struct miscdevice fuse_miscdevice = { | |
1141 | .minor = FUSE_MINOR, | |
1142 | .name = "fuse", | |
1143 | .fops = &fuse_dev_operations, | |
1144 | }; | |
1145 | ||
1146 | int __init fuse_dev_init(void) | |
1147 | { | |
1148 | int err = -ENOMEM; | |
1149 | fuse_req_cachep = kmem_cache_create("fuse_request", | |
1150 | sizeof(struct fuse_req), | |
20c2df83 | 1151 | 0, 0, NULL); |
334f485d MS |
1152 | if (!fuse_req_cachep) |
1153 | goto out; | |
1154 | ||
1155 | err = misc_register(&fuse_miscdevice); | |
1156 | if (err) | |
1157 | goto out_cache_clean; | |
1158 | ||
1159 | return 0; | |
1160 | ||
1161 | out_cache_clean: | |
1162 | kmem_cache_destroy(fuse_req_cachep); | |
1163 | out: | |
1164 | return err; | |
1165 | } | |
1166 | ||
1167 | void fuse_dev_cleanup(void) | |
1168 | { | |
1169 | misc_deregister(&fuse_miscdevice); | |
1170 | kmem_cache_destroy(fuse_req_cachep); | |
1171 | } |