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