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