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