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