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