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