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