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