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