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